gdbtypes.h: Get rid of the TYPE_FIXED_POINT_INFO macro
[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 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
97
98 #define dwarf_read_debug_printf(fmt, ...) \
99 do \
100 { \
101 if (dwarf_read_debug >= 1) \
102 debug_prefixed_printf ("dwarf-read", __func__, fmt, ##__VA_ARGS__); \
103 } \
104 while (0)
105
106 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
107
108 #define dwarf_read_debug_printf_v(fmt, ...) \
109 do \
110 { \
111 if (dwarf_read_debug >= 2) \
112 debug_prefixed_printf ("dwarf-read", __func__, fmt, ##__VA_ARGS__); \
113 } \
114 while (0)
115
116 /* When non-zero, dump DIEs after they are read in. */
117 static unsigned int dwarf_die_debug = 0;
118
119 /* When non-zero, dump line number entries as they are read in. */
120 unsigned int dwarf_line_debug = 0;
121
122 /* When true, cross-check physname against demangler. */
123 static bool check_physname = false;
124
125 /* When true, do not reject deprecated .gdb_index sections. */
126 static bool use_deprecated_index_sections = false;
127
128 /* This is used to store the data that is always per objfile. */
129 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
130
131 /* These are used to store the dwarf2_per_bfd objects.
132
133 objfiles having the same BFD, which doesn't require relocations, are going to
134 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
135
136 Other objfiles are not going to share a dwarf2_per_bfd with any other
137 objfiles, so they'll have their own version kept in the _objfile_data_key
138 version. */
139 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
140 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
141
142 /* The "aclass" indices for various kinds of computed DWARF symbols. */
143
144 static int dwarf2_locexpr_index;
145 static int dwarf2_loclist_index;
146 static int dwarf2_locexpr_block_index;
147 static int dwarf2_loclist_block_index;
148
149 /* Size of .debug_loclists section header for 32-bit DWARF format. */
150 #define LOCLIST_HEADER_SIZE32 12
151
152 /* Size of .debug_loclists section header for 64-bit DWARF format. */
153 #define LOCLIST_HEADER_SIZE64 20
154
155 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
156 #define RNGLIST_HEADER_SIZE32 12
157
158 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
159 #define RNGLIST_HEADER_SIZE64 20
160
161 /* An index into a (C++) symbol name component in a symbol name as
162 recorded in the mapped_index's symbol table. For each C++ symbol
163 in the symbol table, we record one entry for the start of each
164 component in the symbol in a table of name components, and then
165 sort the table, in order to be able to binary search symbol names,
166 ignoring leading namespaces, both completion and regular look up.
167 For example, for symbol "A::B::C", we'll have an entry that points
168 to "A::B::C", another that points to "B::C", and another for "C".
169 Note that function symbols in GDB index have no parameter
170 information, just the function/method names. You can convert a
171 name_component to a "const char *" using the
172 'mapped_index::symbol_name_at(offset_type)' method. */
173
174 struct name_component
175 {
176 /* Offset in the symbol name where the component starts. Stored as
177 a (32-bit) offset instead of a pointer to save memory and improve
178 locality on 64-bit architectures. */
179 offset_type name_offset;
180
181 /* The symbol's index in the symbol and constant pool tables of a
182 mapped_index. */
183 offset_type idx;
184 };
185
186 /* Base class containing bits shared by both .gdb_index and
187 .debug_name indexes. */
188
189 struct mapped_index_base
190 {
191 mapped_index_base () = default;
192 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
193
194 /* The name_component table (a sorted vector). See name_component's
195 description above. */
196 std::vector<name_component> name_components;
197
198 /* How NAME_COMPONENTS is sorted. */
199 enum case_sensitivity name_components_casing;
200
201 /* Return the number of names in the symbol table. */
202 virtual size_t symbol_name_count () const = 0;
203
204 /* Get the name of the symbol at IDX in the symbol table. */
205 virtual const char *symbol_name_at
206 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
207
208 /* Return whether the name at IDX in the symbol table should be
209 ignored. */
210 virtual bool symbol_name_slot_invalid (offset_type idx) const
211 {
212 return false;
213 }
214
215 /* Build the symbol name component sorted vector, if we haven't
216 yet. */
217 void build_name_components (dwarf2_per_objfile *per_objfile);
218
219 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
220 possible matches for LN_NO_PARAMS in the name component
221 vector. */
222 std::pair<std::vector<name_component>::const_iterator,
223 std::vector<name_component>::const_iterator>
224 find_name_components_bounds (const lookup_name_info &ln_no_params,
225 enum language lang,
226 dwarf2_per_objfile *per_objfile) const;
227
228 /* Prevent deleting/destroying via a base class pointer. */
229 protected:
230 ~mapped_index_base() = default;
231 };
232
233 /* A description of the mapped index. The file format is described in
234 a comment by the code that writes the index. */
235 struct mapped_index final : public mapped_index_base
236 {
237 /* A slot/bucket in the symbol table hash. */
238 struct symbol_table_slot
239 {
240 const offset_type name;
241 const offset_type vec;
242 };
243
244 /* Index data format version. */
245 int version = 0;
246
247 /* The address table data. */
248 gdb::array_view<const gdb_byte> address_table;
249
250 /* The symbol table, implemented as a hash table. */
251 gdb::array_view<symbol_table_slot> symbol_table;
252
253 /* A pointer to the constant pool. */
254 const char *constant_pool = nullptr;
255
256 bool symbol_name_slot_invalid (offset_type idx) const override
257 {
258 const auto &bucket = this->symbol_table[idx];
259 return bucket.name == 0 && bucket.vec == 0;
260 }
261
262 /* Convenience method to get at the name of the symbol at IDX in the
263 symbol table. */
264 const char *symbol_name_at
265 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
266 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
267
268 size_t symbol_name_count () const override
269 { return this->symbol_table.size (); }
270 };
271
272 /* A description of the mapped .debug_names.
273 Uninitialized map has CU_COUNT 0. */
274 struct mapped_debug_names final : public mapped_index_base
275 {
276 bfd_endian dwarf5_byte_order;
277 bool dwarf5_is_dwarf64;
278 bool augmentation_is_gdb;
279 uint8_t offset_size;
280 uint32_t cu_count = 0;
281 uint32_t tu_count, bucket_count, name_count;
282 const gdb_byte *cu_table_reordered, *tu_table_reordered;
283 const uint32_t *bucket_table_reordered, *hash_table_reordered;
284 const gdb_byte *name_table_string_offs_reordered;
285 const gdb_byte *name_table_entry_offs_reordered;
286 const gdb_byte *entry_pool;
287
288 struct index_val
289 {
290 ULONGEST dwarf_tag;
291 struct attr
292 {
293 /* Attribute name DW_IDX_*. */
294 ULONGEST dw_idx;
295
296 /* Attribute form DW_FORM_*. */
297 ULONGEST form;
298
299 /* Value if FORM is DW_FORM_implicit_const. */
300 LONGEST implicit_const;
301 };
302 std::vector<attr> attr_vec;
303 };
304
305 std::unordered_map<ULONGEST, index_val> abbrev_map;
306
307 const char *namei_to_name
308 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
309
310 /* Implementation of the mapped_index_base virtual interface, for
311 the name_components cache. */
312
313 const char *symbol_name_at
314 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
315 { return namei_to_name (idx, per_objfile); }
316
317 size_t symbol_name_count () const override
318 { return this->name_count; }
319 };
320
321 /* See dwarf2read.h. */
322
323 dwarf2_per_objfile *
324 get_dwarf2_per_objfile (struct objfile *objfile)
325 {
326 return dwarf2_objfile_data_key.get (objfile);
327 }
328
329 /* Default names of the debugging sections. */
330
331 /* Note that if the debugging section has been compressed, it might
332 have a name like .zdebug_info. */
333
334 static const struct dwarf2_debug_sections dwarf2_elf_names =
335 {
336 { ".debug_info", ".zdebug_info" },
337 { ".debug_abbrev", ".zdebug_abbrev" },
338 { ".debug_line", ".zdebug_line" },
339 { ".debug_loc", ".zdebug_loc" },
340 { ".debug_loclists", ".zdebug_loclists" },
341 { ".debug_macinfo", ".zdebug_macinfo" },
342 { ".debug_macro", ".zdebug_macro" },
343 { ".debug_str", ".zdebug_str" },
344 { ".debug_str_offsets", ".zdebug_str_offsets" },
345 { ".debug_line_str", ".zdebug_line_str" },
346 { ".debug_ranges", ".zdebug_ranges" },
347 { ".debug_rnglists", ".zdebug_rnglists" },
348 { ".debug_types", ".zdebug_types" },
349 { ".debug_addr", ".zdebug_addr" },
350 { ".debug_frame", ".zdebug_frame" },
351 { ".eh_frame", NULL },
352 { ".gdb_index", ".zgdb_index" },
353 { ".debug_names", ".zdebug_names" },
354 { ".debug_aranges", ".zdebug_aranges" },
355 23
356 };
357
358 /* List of DWO/DWP sections. */
359
360 static const struct dwop_section_names
361 {
362 struct dwarf2_section_names abbrev_dwo;
363 struct dwarf2_section_names info_dwo;
364 struct dwarf2_section_names line_dwo;
365 struct dwarf2_section_names loc_dwo;
366 struct dwarf2_section_names loclists_dwo;
367 struct dwarf2_section_names macinfo_dwo;
368 struct dwarf2_section_names macro_dwo;
369 struct dwarf2_section_names rnglists_dwo;
370 struct dwarf2_section_names str_dwo;
371 struct dwarf2_section_names str_offsets_dwo;
372 struct dwarf2_section_names types_dwo;
373 struct dwarf2_section_names cu_index;
374 struct dwarf2_section_names tu_index;
375 }
376 dwop_section_names =
377 {
378 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
379 { ".debug_info.dwo", ".zdebug_info.dwo" },
380 { ".debug_line.dwo", ".zdebug_line.dwo" },
381 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
382 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
383 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
384 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
385 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
386 { ".debug_str.dwo", ".zdebug_str.dwo" },
387 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
388 { ".debug_types.dwo", ".zdebug_types.dwo" },
389 { ".debug_cu_index", ".zdebug_cu_index" },
390 { ".debug_tu_index", ".zdebug_tu_index" },
391 };
392
393 /* local data types */
394
395 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
396 begin with a header, which contains the following information. */
397 struct loclists_rnglists_header
398 {
399 /* A 4-byte or 12-byte length containing the length of the
400 set of entries for this compilation unit, not including the
401 length field itself. */
402 unsigned int length;
403
404 /* A 2-byte version identifier. */
405 short version;
406
407 /* A 1-byte unsigned integer containing the size in bytes of an address on
408 the target system. */
409 unsigned char addr_size;
410
411 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
412 on the target system. */
413 unsigned char segment_collector_size;
414
415 /* A 4-byte count of the number of offsets that follow the header. */
416 unsigned int offset_entry_count;
417 };
418
419 /* Type used for delaying computation of method physnames.
420 See comments for compute_delayed_physnames. */
421 struct delayed_method_info
422 {
423 /* The type to which the method is attached, i.e., its parent class. */
424 struct type *type;
425
426 /* The index of the method in the type's function fieldlists. */
427 int fnfield_index;
428
429 /* The index of the method in the fieldlist. */
430 int index;
431
432 /* The name of the DIE. */
433 const char *name;
434
435 /* The DIE associated with this method. */
436 struct die_info *die;
437 };
438
439 /* Internal state when decoding a particular compilation unit. */
440 struct dwarf2_cu
441 {
442 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
443 dwarf2_per_objfile *per_objfile);
444
445 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
446
447 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
448 Create the set of symtabs used by this TU, or if this TU is sharing
449 symtabs with another TU and the symtabs have already been created
450 then restore those symtabs in the line header.
451 We don't need the pc/line-number mapping for type units. */
452 void setup_type_unit_groups (struct die_info *die);
453
454 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
455 buildsym_compunit constructor. */
456 struct compunit_symtab *start_symtab (const char *name,
457 const char *comp_dir,
458 CORE_ADDR low_pc);
459
460 /* Reset the builder. */
461 void reset_builder () { m_builder.reset (); }
462
463 /* Return a type that is a generic pointer type, the size of which
464 matches the address size given in the compilation unit header for
465 this CU. */
466 struct type *addr_type () const;
467
468 /* Find an integer type the same size as the address size given in
469 the compilation unit header for this CU. UNSIGNED_P controls if
470 the integer is unsigned or not. */
471 struct type *addr_sized_int_type (bool unsigned_p) const;
472
473 /* The header of the compilation unit. */
474 struct comp_unit_head header {};
475
476 /* Base address of this compilation unit. */
477 gdb::optional<CORE_ADDR> base_address;
478
479 /* The language we are debugging. */
480 enum language language = language_unknown;
481 const struct language_defn *language_defn = nullptr;
482
483 const char *producer = nullptr;
484
485 private:
486 /* The symtab builder for this CU. This is only non-NULL when full
487 symbols are being read. */
488 std::unique_ptr<buildsym_compunit> m_builder;
489
490 public:
491 /* The generic symbol table building routines have separate lists for
492 file scope symbols and all all other scopes (local scopes). So
493 we need to select the right one to pass to add_symbol_to_list().
494 We do it by keeping a pointer to the correct list in list_in_scope.
495
496 FIXME: The original dwarf code just treated the file scope as the
497 first local scope, and all other local scopes as nested local
498 scopes, and worked fine. Check to see if we really need to
499 distinguish these in buildsym.c. */
500 struct pending **list_in_scope = nullptr;
501
502 /* Hash table holding all the loaded partial DIEs
503 with partial_die->offset.SECT_OFF as hash. */
504 htab_t partial_dies = nullptr;
505
506 /* Storage for things with the same lifetime as this read-in compilation
507 unit, including partial DIEs. */
508 auto_obstack comp_unit_obstack;
509
510 /* Backlink to our per_cu entry. */
511 struct dwarf2_per_cu_data *per_cu;
512
513 /* The dwarf2_per_objfile that owns this. */
514 dwarf2_per_objfile *per_objfile;
515
516 /* How many compilation units ago was this CU last referenced? */
517 int last_used = 0;
518
519 /* A hash table of DIE cu_offset for following references with
520 die_info->offset.sect_off as hash. */
521 htab_t die_hash = nullptr;
522
523 /* Full DIEs if read in. */
524 struct die_info *dies = nullptr;
525
526 /* A set of pointers to dwarf2_per_cu_data objects for compilation
527 units referenced by this one. Only set during full symbol processing;
528 partial symbol tables do not have dependencies. */
529 htab_t dependencies = nullptr;
530
531 /* Header data from the line table, during full symbol processing. */
532 struct line_header *line_header = nullptr;
533 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
534 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
535 this is the DW_TAG_compile_unit die for this CU. We'll hold on
536 to the line header as long as this DIE is being processed. See
537 process_die_scope. */
538 die_info *line_header_die_owner = nullptr;
539
540 /* A list of methods which need to have physnames computed
541 after all type information has been read. */
542 std::vector<delayed_method_info> method_list;
543
544 /* To be copied to symtab->call_site_htab. */
545 htab_t call_site_htab = nullptr;
546
547 /* Non-NULL if this CU came from a DWO file.
548 There is an invariant here that is important to remember:
549 Except for attributes copied from the top level DIE in the "main"
550 (or "stub") file in preparation for reading the DWO file
551 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
552 Either there isn't a DWO file (in which case this is NULL and the point
553 is moot), or there is and either we're not going to read it (in which
554 case this is NULL) or there is and we are reading it (in which case this
555 is non-NULL). */
556 struct dwo_unit *dwo_unit = nullptr;
557
558 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
559 Note this value comes from the Fission stub CU/TU's DIE. */
560 gdb::optional<ULONGEST> addr_base;
561
562 /* The DW_AT_rnglists_base attribute if present.
563 Note this value comes from the Fission stub CU/TU's DIE.
564 Also note that the value is zero in the non-DWO case so this value can
565 be used without needing to know whether DWO files are in use or not.
566 N.B. This does not apply to DW_AT_ranges appearing in
567 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
568 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
569 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
570 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
571 ULONGEST ranges_base = 0;
572
573 /* The DW_AT_loclists_base attribute if present. */
574 ULONGEST loclist_base = 0;
575
576 /* When reading debug info generated by older versions of rustc, we
577 have to rewrite some union types to be struct types with a
578 variant part. This rewriting must be done after the CU is fully
579 read in, because otherwise at the point of rewriting some struct
580 type might not have been fully processed. So, we keep a list of
581 all such types here and process them after expansion. */
582 std::vector<struct type *> rust_unions;
583
584 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
585 files, the value is implicitly zero. For DWARF 5 version DWO files, the
586 value is often implicit and is the size of the header of
587 .debug_str_offsets section (8 or 4, depending on the address size). */
588 gdb::optional<ULONGEST> str_offsets_base;
589
590 /* Mark used when releasing cached dies. */
591 bool mark : 1;
592
593 /* This CU references .debug_loc. See the symtab->locations_valid field.
594 This test is imperfect as there may exist optimized debug code not using
595 any location list and still facing inlining issues if handled as
596 unoptimized code. For a future better test see GCC PR other/32998. */
597 bool has_loclist : 1;
598
599 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
600 if all the producer_is_* fields are valid. This information is cached
601 because profiling CU expansion showed excessive time spent in
602 producer_is_gxx_lt_4_6. */
603 bool checked_producer : 1;
604 bool producer_is_gxx_lt_4_6 : 1;
605 bool producer_is_gcc_lt_4_3 : 1;
606 bool producer_is_icc : 1;
607 bool producer_is_icc_lt_14 : 1;
608 bool producer_is_codewarrior : 1;
609
610 /* When true, the file that we're processing is known to have
611 debugging info for C++ namespaces. GCC 3.3.x did not produce
612 this information, but later versions do. */
613
614 bool processing_has_namespace_info : 1;
615
616 struct partial_die_info *find_partial_die (sect_offset sect_off);
617
618 /* If this CU was inherited by another CU (via specification,
619 abstract_origin, etc), this is the ancestor CU. */
620 dwarf2_cu *ancestor;
621
622 /* Get the buildsym_compunit for this CU. */
623 buildsym_compunit *get_builder ()
624 {
625 /* If this CU has a builder associated with it, use that. */
626 if (m_builder != nullptr)
627 return m_builder.get ();
628
629 /* Otherwise, search ancestors for a valid builder. */
630 if (ancestor != nullptr)
631 return ancestor->get_builder ();
632
633 return nullptr;
634 }
635 };
636
637 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
638 This includes type_unit_group and quick_file_names. */
639
640 struct stmt_list_hash
641 {
642 /* The DWO unit this table is from or NULL if there is none. */
643 struct dwo_unit *dwo_unit;
644
645 /* Offset in .debug_line or .debug_line.dwo. */
646 sect_offset line_sect_off;
647 };
648
649 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
650 an object of this type. This contains elements of type unit groups
651 that can be shared across objfiles. The non-shareable parts are in
652 type_unit_group_unshareable. */
653
654 struct type_unit_group
655 {
656 /* dwarf2read.c's main "handle" on a TU symtab.
657 To simplify things we create an artificial CU that "includes" all the
658 type units using this stmt_list so that the rest of the code still has
659 a "per_cu" handle on the symtab. */
660 struct dwarf2_per_cu_data per_cu;
661
662 /* The TUs that share this DW_AT_stmt_list entry.
663 This is added to while parsing type units to build partial symtabs,
664 and is deleted afterwards and not used again. */
665 std::vector<signatured_type *> *tus;
666
667 /* The data used to construct the hash key. */
668 struct stmt_list_hash hash;
669 };
670
671 /* These sections are what may appear in a (real or virtual) DWO file. */
672
673 struct dwo_sections
674 {
675 struct dwarf2_section_info abbrev;
676 struct dwarf2_section_info line;
677 struct dwarf2_section_info loc;
678 struct dwarf2_section_info loclists;
679 struct dwarf2_section_info macinfo;
680 struct dwarf2_section_info macro;
681 struct dwarf2_section_info rnglists;
682 struct dwarf2_section_info str;
683 struct dwarf2_section_info str_offsets;
684 /* In the case of a virtual DWO file, these two are unused. */
685 struct dwarf2_section_info info;
686 std::vector<dwarf2_section_info> types;
687 };
688
689 /* CUs/TUs in DWP/DWO files. */
690
691 struct dwo_unit
692 {
693 /* Backlink to the containing struct dwo_file. */
694 struct dwo_file *dwo_file;
695
696 /* The "id" that distinguishes this CU/TU.
697 .debug_info calls this "dwo_id", .debug_types calls this "signature".
698 Since signatures came first, we stick with it for consistency. */
699 ULONGEST signature;
700
701 /* The section this CU/TU lives in, in the DWO file. */
702 struct dwarf2_section_info *section;
703
704 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
705 sect_offset sect_off;
706 unsigned int length;
707
708 /* For types, offset in the type's DIE of the type defined by this TU. */
709 cu_offset type_offset_in_tu;
710 };
711
712 /* include/dwarf2.h defines the DWP section codes.
713 It defines a max value but it doesn't define a min value, which we
714 use for error checking, so provide one. */
715
716 enum dwp_v2_section_ids
717 {
718 DW_SECT_MIN = 1
719 };
720
721 /* Data for one DWO file.
722
723 This includes virtual DWO files (a virtual DWO file is a DWO file as it
724 appears in a DWP file). DWP files don't really have DWO files per se -
725 comdat folding of types "loses" the DWO file they came from, and from
726 a high level view DWP files appear to contain a mass of random types.
727 However, to maintain consistency with the non-DWP case we pretend DWP
728 files contain virtual DWO files, and we assign each TU with one virtual
729 DWO file (generally based on the line and abbrev section offsets -
730 a heuristic that seems to work in practice). */
731
732 struct dwo_file
733 {
734 dwo_file () = default;
735 DISABLE_COPY_AND_ASSIGN (dwo_file);
736
737 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
738 For virtual DWO files the name is constructed from the section offsets
739 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
740 from related CU+TUs. */
741 const char *dwo_name = nullptr;
742
743 /* The DW_AT_comp_dir attribute. */
744 const char *comp_dir = nullptr;
745
746 /* The bfd, when the file is open. Otherwise this is NULL.
747 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
748 gdb_bfd_ref_ptr dbfd;
749
750 /* The sections that make up this DWO file.
751 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
752 sections (for lack of a better name). */
753 struct dwo_sections sections {};
754
755 /* The CUs in the file.
756 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
757 an extension to handle LLVM's Link Time Optimization output (where
758 multiple source files may be compiled into a single object/dwo pair). */
759 htab_up cus;
760
761 /* Table of TUs in the file.
762 Each element is a struct dwo_unit. */
763 htab_up tus;
764 };
765
766 /* These sections are what may appear in a DWP file. */
767
768 struct dwp_sections
769 {
770 /* These are used by all DWP versions (1, 2 and 5). */
771 struct dwarf2_section_info str;
772 struct dwarf2_section_info cu_index;
773 struct dwarf2_section_info tu_index;
774
775 /* These are only used by DWP version 2 and version 5 files.
776 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
777 sections are referenced by section number, and are not recorded here.
778 In DWP version 2 or 5 there is at most one copy of all these sections,
779 each section being (effectively) comprised of the concatenation of all of
780 the individual sections that exist in the version 1 format.
781 To keep the code simple we treat each of these concatenated pieces as a
782 section itself (a virtual section?). */
783 struct dwarf2_section_info abbrev;
784 struct dwarf2_section_info info;
785 struct dwarf2_section_info line;
786 struct dwarf2_section_info loc;
787 struct dwarf2_section_info loclists;
788 struct dwarf2_section_info macinfo;
789 struct dwarf2_section_info macro;
790 struct dwarf2_section_info rnglists;
791 struct dwarf2_section_info str_offsets;
792 struct dwarf2_section_info types;
793 };
794
795 /* These sections are what may appear in a virtual DWO file in DWP version 1.
796 A virtual DWO file is a DWO file as it appears in a DWP file. */
797
798 struct virtual_v1_dwo_sections
799 {
800 struct dwarf2_section_info abbrev;
801 struct dwarf2_section_info line;
802 struct dwarf2_section_info loc;
803 struct dwarf2_section_info macinfo;
804 struct dwarf2_section_info macro;
805 struct dwarf2_section_info str_offsets;
806 /* Each DWP hash table entry records one CU or one TU.
807 That is recorded here, and copied to dwo_unit.section. */
808 struct dwarf2_section_info info_or_types;
809 };
810
811 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
812 In version 2, the sections of the DWO files are concatenated together
813 and stored in one section of that name. Thus each ELF section contains
814 several "virtual" sections. */
815
816 struct virtual_v2_or_v5_dwo_sections
817 {
818 bfd_size_type abbrev_offset;
819 bfd_size_type abbrev_size;
820
821 bfd_size_type line_offset;
822 bfd_size_type line_size;
823
824 bfd_size_type loc_offset;
825 bfd_size_type loc_size;
826
827 bfd_size_type loclists_offset;
828 bfd_size_type loclists_size;
829
830 bfd_size_type macinfo_offset;
831 bfd_size_type macinfo_size;
832
833 bfd_size_type macro_offset;
834 bfd_size_type macro_size;
835
836 bfd_size_type rnglists_offset;
837 bfd_size_type rnglists_size;
838
839 bfd_size_type str_offsets_offset;
840 bfd_size_type str_offsets_size;
841
842 /* Each DWP hash table entry records one CU or one TU.
843 That is recorded here, and copied to dwo_unit.section. */
844 bfd_size_type info_or_types_offset;
845 bfd_size_type info_or_types_size;
846 };
847
848 /* Contents of DWP hash tables. */
849
850 struct dwp_hash_table
851 {
852 uint32_t version, nr_columns;
853 uint32_t nr_units, nr_slots;
854 const gdb_byte *hash_table, *unit_table;
855 union
856 {
857 struct
858 {
859 const gdb_byte *indices;
860 } v1;
861 struct
862 {
863 /* This is indexed by column number and gives the id of the section
864 in that column. */
865 #define MAX_NR_V2_DWO_SECTIONS \
866 (1 /* .debug_info or .debug_types */ \
867 + 1 /* .debug_abbrev */ \
868 + 1 /* .debug_line */ \
869 + 1 /* .debug_loc */ \
870 + 1 /* .debug_str_offsets */ \
871 + 1 /* .debug_macro or .debug_macinfo */)
872 int section_ids[MAX_NR_V2_DWO_SECTIONS];
873 const gdb_byte *offsets;
874 const gdb_byte *sizes;
875 } v2;
876 struct
877 {
878 /* This is indexed by column number and gives the id of the section
879 in that column. */
880 #define MAX_NR_V5_DWO_SECTIONS \
881 (1 /* .debug_info */ \
882 + 1 /* .debug_abbrev */ \
883 + 1 /* .debug_line */ \
884 + 1 /* .debug_loclists */ \
885 + 1 /* .debug_str_offsets */ \
886 + 1 /* .debug_macro */ \
887 + 1 /* .debug_rnglists */)
888 int section_ids[MAX_NR_V5_DWO_SECTIONS];
889 const gdb_byte *offsets;
890 const gdb_byte *sizes;
891 } v5;
892 } section_pool;
893 };
894
895 /* Data for one DWP file. */
896
897 struct dwp_file
898 {
899 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
900 : name (name_),
901 dbfd (std::move (abfd))
902 {
903 }
904
905 /* Name of the file. */
906 const char *name;
907
908 /* File format version. */
909 int version = 0;
910
911 /* The bfd. */
912 gdb_bfd_ref_ptr dbfd;
913
914 /* Section info for this file. */
915 struct dwp_sections sections {};
916
917 /* Table of CUs in the file. */
918 const struct dwp_hash_table *cus = nullptr;
919
920 /* Table of TUs in the file. */
921 const struct dwp_hash_table *tus = nullptr;
922
923 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
924 htab_up loaded_cus;
925 htab_up loaded_tus;
926
927 /* Table to map ELF section numbers to their sections.
928 This is only needed for the DWP V1 file format. */
929 unsigned int num_sections = 0;
930 asection **elf_sections = nullptr;
931 };
932
933 /* Struct used to pass misc. parameters to read_die_and_children, et
934 al. which are used for both .debug_info and .debug_types dies.
935 All parameters here are unchanging for the life of the call. This
936 struct exists to abstract away the constant parameters of die reading. */
937
938 struct die_reader_specs
939 {
940 /* The bfd of die_section. */
941 bfd* abfd;
942
943 /* The CU of the DIE we are parsing. */
944 struct dwarf2_cu *cu;
945
946 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
947 struct dwo_file *dwo_file;
948
949 /* The section the die comes from.
950 This is either .debug_info or .debug_types, or the .dwo variants. */
951 struct dwarf2_section_info *die_section;
952
953 /* die_section->buffer. */
954 const gdb_byte *buffer;
955
956 /* The end of the buffer. */
957 const gdb_byte *buffer_end;
958
959 /* The abbreviation table to use when reading the DIEs. */
960 struct abbrev_table *abbrev_table;
961 };
962
963 /* A subclass of die_reader_specs that holds storage and has complex
964 constructor and destructor behavior. */
965
966 class cutu_reader : public die_reader_specs
967 {
968 public:
969
970 cutu_reader (dwarf2_per_cu_data *this_cu,
971 dwarf2_per_objfile *per_objfile,
972 struct abbrev_table *abbrev_table,
973 dwarf2_cu *existing_cu,
974 bool skip_partial);
975
976 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
977 dwarf2_per_objfile *per_objfile,
978 struct dwarf2_cu *parent_cu = nullptr,
979 struct dwo_file *dwo_file = nullptr);
980
981 DISABLE_COPY_AND_ASSIGN (cutu_reader);
982
983 const gdb_byte *info_ptr = nullptr;
984 struct die_info *comp_unit_die = nullptr;
985 bool dummy_p = false;
986
987 /* Release the new CU, putting it on the chain. This cannot be done
988 for dummy CUs. */
989 void keep ();
990
991 private:
992 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
993 dwarf2_per_objfile *per_objfile,
994 dwarf2_cu *existing_cu);
995
996 struct dwarf2_per_cu_data *m_this_cu;
997 std::unique_ptr<dwarf2_cu> m_new_cu;
998
999 /* The ordinary abbreviation table. */
1000 abbrev_table_up m_abbrev_table_holder;
1001
1002 /* The DWO abbreviation table. */
1003 abbrev_table_up m_dwo_abbrev_table;
1004 };
1005
1006 /* When we construct a partial symbol table entry we only
1007 need this much information. */
1008 struct partial_die_info : public allocate_on_obstack
1009 {
1010 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1011
1012 /* Disable assign but still keep copy ctor, which is needed
1013 load_partial_dies. */
1014 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1015
1016 /* Adjust the partial die before generating a symbol for it. This
1017 function may set the is_external flag or change the DIE's
1018 name. */
1019 void fixup (struct dwarf2_cu *cu);
1020
1021 /* Read a minimal amount of information into the minimal die
1022 structure. */
1023 const gdb_byte *read (const struct die_reader_specs *reader,
1024 const struct abbrev_info &abbrev,
1025 const gdb_byte *info_ptr);
1026
1027 /* Compute the name of this partial DIE. This memoizes the
1028 result, so it is safe to call multiple times. */
1029 const char *name (dwarf2_cu *cu);
1030
1031 /* Offset of this DIE. */
1032 const sect_offset sect_off;
1033
1034 /* DWARF-2 tag for this DIE. */
1035 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1036
1037 /* Assorted flags describing the data found in this DIE. */
1038 const unsigned int has_children : 1;
1039
1040 unsigned int is_external : 1;
1041 unsigned int is_declaration : 1;
1042 unsigned int has_type : 1;
1043 unsigned int has_specification : 1;
1044 unsigned int has_pc_info : 1;
1045 unsigned int may_be_inlined : 1;
1046
1047 /* This DIE has been marked DW_AT_main_subprogram. */
1048 unsigned int main_subprogram : 1;
1049
1050 /* Flag set if the SCOPE field of this structure has been
1051 computed. */
1052 unsigned int scope_set : 1;
1053
1054 /* Flag set if the DIE has a byte_size attribute. */
1055 unsigned int has_byte_size : 1;
1056
1057 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1058 unsigned int has_const_value : 1;
1059
1060 /* Flag set if any of the DIE's children are template arguments. */
1061 unsigned int has_template_arguments : 1;
1062
1063 /* Flag set if fixup has been called on this die. */
1064 unsigned int fixup_called : 1;
1065
1066 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1067 unsigned int is_dwz : 1;
1068
1069 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1070 unsigned int spec_is_dwz : 1;
1071
1072 unsigned int canonical_name : 1;
1073
1074 /* The name of this DIE. Normally the value of DW_AT_name, but
1075 sometimes a default name for unnamed DIEs. */
1076 const char *raw_name = nullptr;
1077
1078 /* The linkage name, if present. */
1079 const char *linkage_name = nullptr;
1080
1081 /* The scope to prepend to our children. This is generally
1082 allocated on the comp_unit_obstack, so will disappear
1083 when this compilation unit leaves the cache. */
1084 const char *scope = nullptr;
1085
1086 /* Some data associated with the partial DIE. The tag determines
1087 which field is live. */
1088 union
1089 {
1090 /* The location description associated with this DIE, if any. */
1091 struct dwarf_block *locdesc;
1092 /* The offset of an import, for DW_TAG_imported_unit. */
1093 sect_offset sect_off;
1094 } d {};
1095
1096 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1097 CORE_ADDR lowpc = 0;
1098 CORE_ADDR highpc = 0;
1099
1100 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1101 DW_AT_sibling, if any. */
1102 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1103 could return DW_AT_sibling values to its caller load_partial_dies. */
1104 const gdb_byte *sibling = nullptr;
1105
1106 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1107 DW_AT_specification (or DW_AT_abstract_origin or
1108 DW_AT_extension). */
1109 sect_offset spec_offset {};
1110
1111 /* Pointers to this DIE's parent, first child, and next sibling,
1112 if any. */
1113 struct partial_die_info *die_parent = nullptr;
1114 struct partial_die_info *die_child = nullptr;
1115 struct partial_die_info *die_sibling = nullptr;
1116
1117 friend struct partial_die_info *
1118 dwarf2_cu::find_partial_die (sect_offset sect_off);
1119
1120 private:
1121 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1122 partial_die_info (sect_offset sect_off)
1123 : partial_die_info (sect_off, DW_TAG_padding, 0)
1124 {
1125 }
1126
1127 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1128 int has_children_)
1129 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1130 {
1131 is_external = 0;
1132 is_declaration = 0;
1133 has_type = 0;
1134 has_specification = 0;
1135 has_pc_info = 0;
1136 may_be_inlined = 0;
1137 main_subprogram = 0;
1138 scope_set = 0;
1139 has_byte_size = 0;
1140 has_const_value = 0;
1141 has_template_arguments = 0;
1142 fixup_called = 0;
1143 is_dwz = 0;
1144 spec_is_dwz = 0;
1145 canonical_name = 0;
1146 }
1147 };
1148
1149 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1150 but this would require a corresponding change in unpack_field_as_long
1151 and friends. */
1152 static int bits_per_byte = 8;
1153
1154 struct variant_part_builder;
1155
1156 /* When reading a variant, we track a bit more information about the
1157 field, and store it in an object of this type. */
1158
1159 struct variant_field
1160 {
1161 int first_field = -1;
1162 int last_field = -1;
1163
1164 /* A variant can contain other variant parts. */
1165 std::vector<variant_part_builder> variant_parts;
1166
1167 /* If we see a DW_TAG_variant, then this will be set if this is the
1168 default branch. */
1169 bool default_branch = false;
1170 /* If we see a DW_AT_discr_value, then this will be the discriminant
1171 value. */
1172 ULONGEST discriminant_value = 0;
1173 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1174 data. */
1175 struct dwarf_block *discr_list_data = nullptr;
1176 };
1177
1178 /* This represents a DW_TAG_variant_part. */
1179
1180 struct variant_part_builder
1181 {
1182 /* The offset of the discriminant field. */
1183 sect_offset discriminant_offset {};
1184
1185 /* Variants that are direct children of this variant part. */
1186 std::vector<variant_field> variants;
1187
1188 /* True if we're currently reading a variant. */
1189 bool processing_variant = false;
1190 };
1191
1192 struct nextfield
1193 {
1194 int accessibility = 0;
1195 int virtuality = 0;
1196 /* Variant parts need to find the discriminant, which is a DIE
1197 reference. We track the section offset of each field to make
1198 this link. */
1199 sect_offset offset;
1200 struct field field {};
1201 };
1202
1203 struct fnfieldlist
1204 {
1205 const char *name = nullptr;
1206 std::vector<struct fn_field> fnfields;
1207 };
1208
1209 /* The routines that read and process dies for a C struct or C++ class
1210 pass lists of data member fields and lists of member function fields
1211 in an instance of a field_info structure, as defined below. */
1212 struct field_info
1213 {
1214 /* List of data member and baseclasses fields. */
1215 std::vector<struct nextfield> fields;
1216 std::vector<struct nextfield> baseclasses;
1217
1218 /* Set if the accessibility of one of the fields is not public. */
1219 bool non_public_fields = false;
1220
1221 /* Member function fieldlist array, contains name of possibly overloaded
1222 member function, number of overloaded member functions and a pointer
1223 to the head of the member function field chain. */
1224 std::vector<struct fnfieldlist> fnfieldlists;
1225
1226 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1227 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1228 std::vector<struct decl_field> typedef_field_list;
1229
1230 /* Nested types defined by this class and the number of elements in this
1231 list. */
1232 std::vector<struct decl_field> nested_types_list;
1233
1234 /* If non-null, this is the variant part we are currently
1235 reading. */
1236 variant_part_builder *current_variant_part = nullptr;
1237 /* This holds all the top-level variant parts attached to the type
1238 we're reading. */
1239 std::vector<variant_part_builder> variant_parts;
1240
1241 /* Return the total number of fields (including baseclasses). */
1242 int nfields () const
1243 {
1244 return fields.size () + baseclasses.size ();
1245 }
1246 };
1247
1248 /* Loaded secondary compilation units are kept in memory until they
1249 have not been referenced for the processing of this many
1250 compilation units. Set this to zero to disable caching. Cache
1251 sizes of up to at least twenty will improve startup time for
1252 typical inter-CU-reference binaries, at an obvious memory cost. */
1253 static int dwarf_max_cache_age = 5;
1254 static void
1255 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1256 struct cmd_list_element *c, const char *value)
1257 {
1258 fprintf_filtered (file, _("The upper bound on the age of cached "
1259 "DWARF compilation units is %s.\n"),
1260 value);
1261 }
1262 \f
1263 /* local function prototypes */
1264
1265 static void dwarf2_find_base_address (struct die_info *die,
1266 struct dwarf2_cu *cu);
1267
1268 static dwarf2_psymtab *create_partial_symtab
1269 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1270 const char *name);
1271
1272 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1273 const gdb_byte *info_ptr,
1274 struct die_info *type_unit_die);
1275
1276 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1277
1278 static void scan_partial_symbols (struct partial_die_info *,
1279 CORE_ADDR *, CORE_ADDR *,
1280 int, struct dwarf2_cu *);
1281
1282 static void add_partial_symbol (struct partial_die_info *,
1283 struct dwarf2_cu *);
1284
1285 static void add_partial_namespace (struct partial_die_info *pdi,
1286 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1287 int set_addrmap, struct dwarf2_cu *cu);
1288
1289 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1290 CORE_ADDR *highpc, int set_addrmap,
1291 struct dwarf2_cu *cu);
1292
1293 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1294 struct dwarf2_cu *cu);
1295
1296 static void add_partial_subprogram (struct partial_die_info *pdi,
1297 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1298 int need_pc, struct dwarf2_cu *cu);
1299
1300 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1301
1302 static struct partial_die_info *load_partial_dies
1303 (const struct die_reader_specs *, const gdb_byte *, int);
1304
1305 /* A pair of partial_die_info and compilation unit. */
1306 struct cu_partial_die_info
1307 {
1308 /* The compilation unit of the partial_die_info. */
1309 struct dwarf2_cu *cu;
1310 /* A partial_die_info. */
1311 struct partial_die_info *pdi;
1312
1313 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1314 : cu (cu),
1315 pdi (pdi)
1316 { /* Nothing. */ }
1317
1318 private:
1319 cu_partial_die_info () = delete;
1320 };
1321
1322 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1323 struct dwarf2_cu *);
1324
1325 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1326 struct attribute *, struct attr_abbrev *,
1327 const gdb_byte *);
1328
1329 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1330 struct attribute *attr, dwarf_tag tag);
1331
1332 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1333
1334 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1335 dwarf2_section_info *, sect_offset);
1336
1337 static const char *read_indirect_string
1338 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1339 const struct comp_unit_head *, unsigned int *);
1340
1341 static const char *read_indirect_string_at_offset
1342 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1343
1344 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1345 const gdb_byte *,
1346 unsigned int *);
1347
1348 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1349 ULONGEST str_index);
1350
1351 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1352 ULONGEST str_index);
1353
1354 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1355
1356 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1357 struct dwarf2_cu *);
1358
1359 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1360 struct dwarf2_cu *cu);
1361
1362 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1363
1364 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1365 struct dwarf2_cu *cu);
1366
1367 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1368
1369 static struct die_info *die_specification (struct die_info *die,
1370 struct dwarf2_cu **);
1371
1372 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1373 struct dwarf2_cu *cu);
1374
1375 static void dwarf_decode_lines (struct line_header *, const char *,
1376 struct dwarf2_cu *, dwarf2_psymtab *,
1377 CORE_ADDR, int decode_mapping);
1378
1379 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1380 const char *);
1381
1382 static struct symbol *new_symbol (struct die_info *, struct type *,
1383 struct dwarf2_cu *, struct symbol * = NULL);
1384
1385 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1386 struct dwarf2_cu *);
1387
1388 static void dwarf2_const_value_attr (const struct attribute *attr,
1389 struct type *type,
1390 const char *name,
1391 struct obstack *obstack,
1392 struct dwarf2_cu *cu, LONGEST *value,
1393 const gdb_byte **bytes,
1394 struct dwarf2_locexpr_baton **baton);
1395
1396 static struct type *read_subrange_index_type (struct die_info *die,
1397 struct dwarf2_cu *cu);
1398
1399 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1400
1401 static int need_gnat_info (struct dwarf2_cu *);
1402
1403 static struct type *die_descriptive_type (struct die_info *,
1404 struct dwarf2_cu *);
1405
1406 static void set_descriptive_type (struct type *, struct die_info *,
1407 struct dwarf2_cu *);
1408
1409 static struct type *die_containing_type (struct die_info *,
1410 struct dwarf2_cu *);
1411
1412 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1413 struct dwarf2_cu *);
1414
1415 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1416
1417 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1418
1419 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1420
1421 static char *typename_concat (struct obstack *obs, const char *prefix,
1422 const char *suffix, int physname,
1423 struct dwarf2_cu *cu);
1424
1425 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1426
1427 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1428
1429 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1430
1431 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1432
1433 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1434
1435 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1436
1437 /* Return the .debug_loclists section to use for cu. */
1438 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1439
1440 /* Return the .debug_rnglists section to use for cu. */
1441 static struct dwarf2_section_info *cu_debug_rnglists_section
1442 (struct dwarf2_cu *cu, dwarf_tag tag);
1443
1444 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1445 values. Keep the items ordered with increasing constraints compliance. */
1446 enum pc_bounds_kind
1447 {
1448 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1449 PC_BOUNDS_NOT_PRESENT,
1450
1451 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1452 were present but they do not form a valid range of PC addresses. */
1453 PC_BOUNDS_INVALID,
1454
1455 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1456 PC_BOUNDS_RANGES,
1457
1458 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1459 PC_BOUNDS_HIGH_LOW,
1460 };
1461
1462 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1463 CORE_ADDR *, CORE_ADDR *,
1464 struct dwarf2_cu *,
1465 dwarf2_psymtab *);
1466
1467 static void get_scope_pc_bounds (struct die_info *,
1468 CORE_ADDR *, CORE_ADDR *,
1469 struct dwarf2_cu *);
1470
1471 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1472 CORE_ADDR, struct dwarf2_cu *);
1473
1474 static void dwarf2_add_field (struct field_info *, struct die_info *,
1475 struct dwarf2_cu *);
1476
1477 static void dwarf2_attach_fields_to_type (struct field_info *,
1478 struct type *, struct dwarf2_cu *);
1479
1480 static void dwarf2_add_member_fn (struct field_info *,
1481 struct die_info *, struct type *,
1482 struct dwarf2_cu *);
1483
1484 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1485 struct type *,
1486 struct dwarf2_cu *);
1487
1488 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1489
1490 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1491
1492 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1493
1494 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1495
1496 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1497
1498 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1499
1500 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1501
1502 static struct type *read_module_type (struct die_info *die,
1503 struct dwarf2_cu *cu);
1504
1505 static const char *namespace_name (struct die_info *die,
1506 int *is_anonymous, struct dwarf2_cu *);
1507
1508 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1509
1510 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1511 bool * = nullptr);
1512
1513 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1514 struct dwarf2_cu *);
1515
1516 static struct die_info *read_die_and_siblings_1
1517 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1518 struct die_info *);
1519
1520 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1521 const gdb_byte *info_ptr,
1522 const gdb_byte **new_info_ptr,
1523 struct die_info *parent);
1524
1525 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1526 struct die_info **, const gdb_byte *,
1527 int);
1528
1529 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1530 struct die_info **, const gdb_byte *);
1531
1532 static void process_die (struct die_info *, struct dwarf2_cu *);
1533
1534 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1535 struct objfile *);
1536
1537 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1538
1539 static const char *dwarf2_full_name (const char *name,
1540 struct die_info *die,
1541 struct dwarf2_cu *cu);
1542
1543 static const char *dwarf2_physname (const char *name, struct die_info *die,
1544 struct dwarf2_cu *cu);
1545
1546 static struct die_info *dwarf2_extension (struct die_info *die,
1547 struct dwarf2_cu **);
1548
1549 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1550
1551 static void dump_die_for_error (struct die_info *);
1552
1553 static void dump_die_1 (struct ui_file *, int level, int max_level,
1554 struct die_info *);
1555
1556 /*static*/ void dump_die (struct die_info *, int max_level);
1557
1558 static void store_in_ref_table (struct die_info *,
1559 struct dwarf2_cu *);
1560
1561 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1562 const struct attribute *,
1563 struct dwarf2_cu **);
1564
1565 static struct die_info *follow_die_ref (struct die_info *,
1566 const struct attribute *,
1567 struct dwarf2_cu **);
1568
1569 static struct die_info *follow_die_sig (struct die_info *,
1570 const struct attribute *,
1571 struct dwarf2_cu **);
1572
1573 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1574 struct dwarf2_cu *);
1575
1576 static struct type *get_DW_AT_signature_type (struct die_info *,
1577 const struct attribute *,
1578 struct dwarf2_cu *);
1579
1580 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1581 dwarf2_per_objfile *per_objfile);
1582
1583 static void read_signatured_type (signatured_type *sig_type,
1584 dwarf2_per_objfile *per_objfile);
1585
1586 static int attr_to_dynamic_prop (const struct attribute *attr,
1587 struct die_info *die, struct dwarf2_cu *cu,
1588 struct dynamic_prop *prop, struct type *type);
1589
1590 /* memory allocation interface */
1591
1592 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1593
1594 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1595
1596 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1597
1598 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1599 struct dwarf2_loclist_baton *baton,
1600 const struct attribute *attr);
1601
1602 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1603 struct symbol *sym,
1604 struct dwarf2_cu *cu,
1605 int is_block);
1606
1607 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1608 const gdb_byte *info_ptr,
1609 struct abbrev_info *abbrev);
1610
1611 static hashval_t partial_die_hash (const void *item);
1612
1613 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1614
1615 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1616 (sect_offset sect_off, unsigned int offset_in_dwz,
1617 dwarf2_per_objfile *per_objfile);
1618
1619 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1620 struct die_info *comp_unit_die,
1621 enum language pretend_language);
1622
1623 static struct type *set_die_type (struct die_info *, struct type *,
1624 struct dwarf2_cu *, bool = false);
1625
1626 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1627
1628 static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1629
1630 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1631 dwarf2_per_objfile *per_objfile,
1632 dwarf2_cu *existing_cu,
1633 bool skip_partial,
1634 enum language pretend_language);
1635
1636 static void process_full_comp_unit (dwarf2_cu *cu,
1637 enum language pretend_language);
1638
1639 static void process_full_type_unit (dwarf2_cu *cu,
1640 enum language pretend_language);
1641
1642 static void dwarf2_add_dependence (struct dwarf2_cu *,
1643 struct dwarf2_per_cu_data *);
1644
1645 static void dwarf2_mark (struct dwarf2_cu *);
1646
1647 static struct type *get_die_type_at_offset (sect_offset,
1648 dwarf2_per_cu_data *per_cu,
1649 dwarf2_per_objfile *per_objfile);
1650
1651 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1652
1653 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1654 dwarf2_per_objfile *per_objfile,
1655 enum language pretend_language);
1656
1657 static void process_queue (dwarf2_per_objfile *per_objfile);
1658
1659 /* Class, the destructor of which frees all allocated queue entries. This
1660 will only have work to do if an error was thrown while processing the
1661 dwarf. If no error was thrown then the queue entries should have all
1662 been processed, and freed, as we went along. */
1663
1664 class dwarf2_queue_guard
1665 {
1666 public:
1667 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1668 : m_per_objfile (per_objfile)
1669 {
1670 }
1671
1672 /* Free any entries remaining on the queue. There should only be
1673 entries left if we hit an error while processing the dwarf. */
1674 ~dwarf2_queue_guard ()
1675 {
1676 /* Ensure that no memory is allocated by the queue. */
1677 std::queue<dwarf2_queue_item> empty;
1678 std::swap (m_per_objfile->per_bfd->queue, empty);
1679 }
1680
1681 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1682
1683 private:
1684 dwarf2_per_objfile *m_per_objfile;
1685 };
1686
1687 dwarf2_queue_item::~dwarf2_queue_item ()
1688 {
1689 /* Anything still marked queued is likely to be in an
1690 inconsistent state, so discard it. */
1691 if (per_cu->queued)
1692 {
1693 per_objfile->remove_cu (per_cu);
1694 per_cu->queued = 0;
1695 }
1696 }
1697
1698 /* The return type of find_file_and_directory. Note, the enclosed
1699 string pointers are only valid while this object is valid. */
1700
1701 struct file_and_directory
1702 {
1703 /* The filename. This is never NULL. */
1704 const char *name;
1705
1706 /* The compilation directory. NULL if not known. If we needed to
1707 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1708 points directly to the DW_AT_comp_dir string attribute owned by
1709 the obstack that owns the DIE. */
1710 const char *comp_dir;
1711
1712 /* If we needed to build a new string for comp_dir, this is what
1713 owns the storage. */
1714 std::string comp_dir_storage;
1715 };
1716
1717 static file_and_directory find_file_and_directory (struct die_info *die,
1718 struct dwarf2_cu *cu);
1719
1720 static htab_up allocate_signatured_type_table ();
1721
1722 static htab_up allocate_dwo_unit_table ();
1723
1724 static struct dwo_unit *lookup_dwo_unit_in_dwp
1725 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1726 const char *comp_dir, ULONGEST signature, int is_debug_types);
1727
1728 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1729
1730 static struct dwo_unit *lookup_dwo_comp_unit
1731 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1732 ULONGEST signature);
1733
1734 static struct dwo_unit *lookup_dwo_type_unit
1735 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1736
1737 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1738
1739 /* A unique pointer to a dwo_file. */
1740
1741 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1742
1743 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1744
1745 static void check_producer (struct dwarf2_cu *cu);
1746
1747 static void free_line_header_voidp (void *arg);
1748 \f
1749 /* Various complaints about symbol reading that don't abort the process. */
1750
1751 static void
1752 dwarf2_debug_line_missing_file_complaint (void)
1753 {
1754 complaint (_(".debug_line section has line data without a file"));
1755 }
1756
1757 static void
1758 dwarf2_debug_line_missing_end_sequence_complaint (void)
1759 {
1760 complaint (_(".debug_line section has line "
1761 "program sequence without an end"));
1762 }
1763
1764 static void
1765 dwarf2_complex_location_expr_complaint (void)
1766 {
1767 complaint (_("location expression too complex"));
1768 }
1769
1770 static void
1771 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1772 int arg3)
1773 {
1774 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1775 arg1, arg2, arg3);
1776 }
1777
1778 static void
1779 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1780 {
1781 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1782 arg1, arg2);
1783 }
1784
1785 /* Hash function for line_header_hash. */
1786
1787 static hashval_t
1788 line_header_hash (const struct line_header *ofs)
1789 {
1790 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1791 }
1792
1793 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1794
1795 static hashval_t
1796 line_header_hash_voidp (const void *item)
1797 {
1798 const struct line_header *ofs = (const struct line_header *) item;
1799
1800 return line_header_hash (ofs);
1801 }
1802
1803 /* Equality function for line_header_hash. */
1804
1805 static int
1806 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1807 {
1808 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1809 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1810
1811 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1812 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1813 }
1814
1815 \f
1816
1817 /* See declaration. */
1818
1819 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1820 bool can_copy_)
1821 : obfd (obfd),
1822 can_copy (can_copy_)
1823 {
1824 if (names == NULL)
1825 names = &dwarf2_elf_names;
1826
1827 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1828 locate_sections (obfd, sec, *names);
1829 }
1830
1831 dwarf2_per_bfd::~dwarf2_per_bfd ()
1832 {
1833 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1834 per_cu->imported_symtabs_free ();
1835
1836 for (signatured_type *sig_type : all_type_units)
1837 sig_type->per_cu.imported_symtabs_free ();
1838
1839 /* Everything else should be on this->obstack. */
1840 }
1841
1842 /* See read.h. */
1843
1844 void
1845 dwarf2_per_objfile::remove_all_cus ()
1846 {
1847 for (auto pair : m_dwarf2_cus)
1848 delete pair.second;
1849
1850 m_dwarf2_cus.clear ();
1851 }
1852
1853 /* A helper class that calls free_cached_comp_units on
1854 destruction. */
1855
1856 class free_cached_comp_units
1857 {
1858 public:
1859
1860 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1861 : m_per_objfile (per_objfile)
1862 {
1863 }
1864
1865 ~free_cached_comp_units ()
1866 {
1867 m_per_objfile->remove_all_cus ();
1868 }
1869
1870 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1871
1872 private:
1873
1874 dwarf2_per_objfile *m_per_objfile;
1875 };
1876
1877 /* See read.h. */
1878
1879 bool
1880 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1881 {
1882 gdb_assert (per_cu->index < this->m_symtabs.size ());
1883
1884 return this->m_symtabs[per_cu->index] != nullptr;
1885 }
1886
1887 /* See read.h. */
1888
1889 compunit_symtab *
1890 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1891 {
1892 gdb_assert (per_cu->index < this->m_symtabs.size ());
1893
1894 return this->m_symtabs[per_cu->index];
1895 }
1896
1897 /* See read.h. */
1898
1899 void
1900 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1901 compunit_symtab *symtab)
1902 {
1903 gdb_assert (per_cu->index < this->m_symtabs.size ());
1904 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1905
1906 this->m_symtabs[per_cu->index] = symtab;
1907 }
1908
1909 /* Try to locate the sections we need for DWARF 2 debugging
1910 information and return true if we have enough to do something.
1911 NAMES points to the dwarf2 section names, or is NULL if the standard
1912 ELF names are used. CAN_COPY is true for formats where symbol
1913 interposition is possible and so symbol values must follow copy
1914 relocation rules. */
1915
1916 int
1917 dwarf2_has_info (struct objfile *objfile,
1918 const struct dwarf2_debug_sections *names,
1919 bool can_copy)
1920 {
1921 if (objfile->flags & OBJF_READNEVER)
1922 return 0;
1923
1924 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1925
1926 if (per_objfile == NULL)
1927 {
1928 dwarf2_per_bfd *per_bfd;
1929
1930 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
1931 doesn't require relocations and if there aren't partial symbols
1932 from some other reader. */
1933 if (!objfile_has_partial_symbols (objfile)
1934 && !gdb_bfd_requires_relocations (objfile->obfd))
1935 {
1936 /* See if one has been created for this BFD yet. */
1937 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1938
1939 if (per_bfd == nullptr)
1940 {
1941 /* No, create it now. */
1942 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1943 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1944 }
1945 }
1946 else
1947 {
1948 /* No sharing possible, create one specifically for this objfile. */
1949 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1950 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1951 }
1952
1953 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1954 }
1955
1956 return (!per_objfile->per_bfd->info.is_virtual
1957 && per_objfile->per_bfd->info.s.section != NULL
1958 && !per_objfile->per_bfd->abbrev.is_virtual
1959 && per_objfile->per_bfd->abbrev.s.section != NULL);
1960 }
1961
1962 /* When loading sections, we look either for uncompressed section or for
1963 compressed section names. */
1964
1965 static int
1966 section_is_p (const char *section_name,
1967 const struct dwarf2_section_names *names)
1968 {
1969 if (names->normal != NULL
1970 && strcmp (section_name, names->normal) == 0)
1971 return 1;
1972 if (names->compressed != NULL
1973 && strcmp (section_name, names->compressed) == 0)
1974 return 1;
1975 return 0;
1976 }
1977
1978 /* See declaration. */
1979
1980 void
1981 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1982 const dwarf2_debug_sections &names)
1983 {
1984 flagword aflag = bfd_section_flags (sectp);
1985
1986 if ((aflag & SEC_HAS_CONTENTS) == 0)
1987 {
1988 }
1989 else if (elf_section_data (sectp)->this_hdr.sh_size
1990 > bfd_get_file_size (abfd))
1991 {
1992 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1993 warning (_("Discarding section %s which has a section size (%s"
1994 ") larger than the file size [in module %s]"),
1995 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1996 bfd_get_filename (abfd));
1997 }
1998 else if (section_is_p (sectp->name, &names.info))
1999 {
2000 this->info.s.section = sectp;
2001 this->info.size = bfd_section_size (sectp);
2002 }
2003 else if (section_is_p (sectp->name, &names.abbrev))
2004 {
2005 this->abbrev.s.section = sectp;
2006 this->abbrev.size = bfd_section_size (sectp);
2007 }
2008 else if (section_is_p (sectp->name, &names.line))
2009 {
2010 this->line.s.section = sectp;
2011 this->line.size = bfd_section_size (sectp);
2012 }
2013 else if (section_is_p (sectp->name, &names.loc))
2014 {
2015 this->loc.s.section = sectp;
2016 this->loc.size = bfd_section_size (sectp);
2017 }
2018 else if (section_is_p (sectp->name, &names.loclists))
2019 {
2020 this->loclists.s.section = sectp;
2021 this->loclists.size = bfd_section_size (sectp);
2022 }
2023 else if (section_is_p (sectp->name, &names.macinfo))
2024 {
2025 this->macinfo.s.section = sectp;
2026 this->macinfo.size = bfd_section_size (sectp);
2027 }
2028 else if (section_is_p (sectp->name, &names.macro))
2029 {
2030 this->macro.s.section = sectp;
2031 this->macro.size = bfd_section_size (sectp);
2032 }
2033 else if (section_is_p (sectp->name, &names.str))
2034 {
2035 this->str.s.section = sectp;
2036 this->str.size = bfd_section_size (sectp);
2037 }
2038 else if (section_is_p (sectp->name, &names.str_offsets))
2039 {
2040 this->str_offsets.s.section = sectp;
2041 this->str_offsets.size = bfd_section_size (sectp);
2042 }
2043 else if (section_is_p (sectp->name, &names.line_str))
2044 {
2045 this->line_str.s.section = sectp;
2046 this->line_str.size = bfd_section_size (sectp);
2047 }
2048 else if (section_is_p (sectp->name, &names.addr))
2049 {
2050 this->addr.s.section = sectp;
2051 this->addr.size = bfd_section_size (sectp);
2052 }
2053 else if (section_is_p (sectp->name, &names.frame))
2054 {
2055 this->frame.s.section = sectp;
2056 this->frame.size = bfd_section_size (sectp);
2057 }
2058 else if (section_is_p (sectp->name, &names.eh_frame))
2059 {
2060 this->eh_frame.s.section = sectp;
2061 this->eh_frame.size = bfd_section_size (sectp);
2062 }
2063 else if (section_is_p (sectp->name, &names.ranges))
2064 {
2065 this->ranges.s.section = sectp;
2066 this->ranges.size = bfd_section_size (sectp);
2067 }
2068 else if (section_is_p (sectp->name, &names.rnglists))
2069 {
2070 this->rnglists.s.section = sectp;
2071 this->rnglists.size = bfd_section_size (sectp);
2072 }
2073 else if (section_is_p (sectp->name, &names.types))
2074 {
2075 struct dwarf2_section_info type_section;
2076
2077 memset (&type_section, 0, sizeof (type_section));
2078 type_section.s.section = sectp;
2079 type_section.size = bfd_section_size (sectp);
2080
2081 this->types.push_back (type_section);
2082 }
2083 else if (section_is_p (sectp->name, &names.gdb_index))
2084 {
2085 this->gdb_index.s.section = sectp;
2086 this->gdb_index.size = bfd_section_size (sectp);
2087 }
2088 else if (section_is_p (sectp->name, &names.debug_names))
2089 {
2090 this->debug_names.s.section = sectp;
2091 this->debug_names.size = bfd_section_size (sectp);
2092 }
2093 else if (section_is_p (sectp->name, &names.debug_aranges))
2094 {
2095 this->debug_aranges.s.section = sectp;
2096 this->debug_aranges.size = bfd_section_size (sectp);
2097 }
2098
2099 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2100 && bfd_section_vma (sectp) == 0)
2101 this->has_section_at_zero = true;
2102 }
2103
2104 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2105 SECTION_NAME. */
2106
2107 void
2108 dwarf2_get_section_info (struct objfile *objfile,
2109 enum dwarf2_section_enum sect,
2110 asection **sectp, const gdb_byte **bufp,
2111 bfd_size_type *sizep)
2112 {
2113 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2114 struct dwarf2_section_info *info;
2115
2116 /* We may see an objfile without any DWARF, in which case we just
2117 return nothing. */
2118 if (per_objfile == NULL)
2119 {
2120 *sectp = NULL;
2121 *bufp = NULL;
2122 *sizep = 0;
2123 return;
2124 }
2125 switch (sect)
2126 {
2127 case DWARF2_DEBUG_FRAME:
2128 info = &per_objfile->per_bfd->frame;
2129 break;
2130 case DWARF2_EH_FRAME:
2131 info = &per_objfile->per_bfd->eh_frame;
2132 break;
2133 default:
2134 gdb_assert_not_reached ("unexpected section");
2135 }
2136
2137 info->read (objfile);
2138
2139 *sectp = info->get_bfd_section ();
2140 *bufp = info->buffer;
2141 *sizep = info->size;
2142 }
2143
2144 /* A helper function to find the sections for a .dwz file. */
2145
2146 static void
2147 locate_dwz_sections (bfd *abfd, asection *sectp, dwz_file *dwz_file)
2148 {
2149 /* Note that we only support the standard ELF names, because .dwz
2150 is ELF-only (at the time of writing). */
2151 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2152 {
2153 dwz_file->abbrev.s.section = sectp;
2154 dwz_file->abbrev.size = bfd_section_size (sectp);
2155 }
2156 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2157 {
2158 dwz_file->info.s.section = sectp;
2159 dwz_file->info.size = bfd_section_size (sectp);
2160 }
2161 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2162 {
2163 dwz_file->str.s.section = sectp;
2164 dwz_file->str.size = bfd_section_size (sectp);
2165 }
2166 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2167 {
2168 dwz_file->line.s.section = sectp;
2169 dwz_file->line.size = bfd_section_size (sectp);
2170 }
2171 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2172 {
2173 dwz_file->macro.s.section = sectp;
2174 dwz_file->macro.size = bfd_section_size (sectp);
2175 }
2176 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2177 {
2178 dwz_file->gdb_index.s.section = sectp;
2179 dwz_file->gdb_index.size = bfd_section_size (sectp);
2180 }
2181 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2182 {
2183 dwz_file->debug_names.s.section = sectp;
2184 dwz_file->debug_names.size = bfd_section_size (sectp);
2185 }
2186 }
2187
2188 /* See dwarf2read.h. */
2189
2190 struct dwz_file *
2191 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2192 {
2193 const char *filename;
2194 bfd_size_type buildid_len_arg;
2195 size_t buildid_len;
2196 bfd_byte *buildid;
2197
2198 if (per_bfd->dwz_file != NULL)
2199 return per_bfd->dwz_file.get ();
2200
2201 bfd_set_error (bfd_error_no_error);
2202 gdb::unique_xmalloc_ptr<char> data
2203 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2204 &buildid_len_arg, &buildid));
2205 if (data == NULL)
2206 {
2207 if (bfd_get_error () == bfd_error_no_error)
2208 return NULL;
2209 error (_("could not read '.gnu_debugaltlink' section: %s"),
2210 bfd_errmsg (bfd_get_error ()));
2211 }
2212
2213 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2214
2215 buildid_len = (size_t) buildid_len_arg;
2216
2217 filename = data.get ();
2218
2219 std::string abs_storage;
2220 if (!IS_ABSOLUTE_PATH (filename))
2221 {
2222 gdb::unique_xmalloc_ptr<char> abs
2223 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2224
2225 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2226 filename = abs_storage.c_str ();
2227 }
2228
2229 /* First try the file name given in the section. If that doesn't
2230 work, try to use the build-id instead. */
2231 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2232 if (dwz_bfd != NULL)
2233 {
2234 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2235 dwz_bfd.reset (nullptr);
2236 }
2237
2238 if (dwz_bfd == NULL)
2239 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2240
2241 if (dwz_bfd == nullptr)
2242 {
2243 gdb::unique_xmalloc_ptr<char> alt_filename;
2244 const char *origname = bfd_get_filename (per_bfd->obfd);
2245
2246 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2247 buildid_len,
2248 origname,
2249 &alt_filename));
2250
2251 if (fd.get () >= 0)
2252 {
2253 /* File successfully retrieved from server. */
2254 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2255
2256 if (dwz_bfd == nullptr)
2257 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2258 alt_filename.get ());
2259 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2260 dwz_bfd.reset (nullptr);
2261 }
2262 }
2263
2264 if (dwz_bfd == NULL)
2265 error (_("could not find '.gnu_debugaltlink' file for %s"),
2266 bfd_get_filename (per_bfd->obfd));
2267
2268 std::unique_ptr<struct dwz_file> result
2269 (new struct dwz_file (std::move (dwz_bfd)));
2270
2271 for (asection *sec : gdb_bfd_sections (result->dwz_bfd))
2272 locate_dwz_sections (result->dwz_bfd.get (), sec, result.get ());
2273
2274 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2275 per_bfd->dwz_file = std::move (result);
2276 return per_bfd->dwz_file.get ();
2277 }
2278 \f
2279 /* DWARF quick_symbols_functions support. */
2280
2281 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2282 unique line tables, so we maintain a separate table of all .debug_line
2283 derived entries to support the sharing.
2284 All the quick functions need is the list of file names. We discard the
2285 line_header when we're done and don't need to record it here. */
2286 struct quick_file_names
2287 {
2288 /* The data used to construct the hash key. */
2289 struct stmt_list_hash hash;
2290
2291 /* The number of entries in file_names, real_names. */
2292 unsigned int num_file_names;
2293
2294 /* The file names from the line table, after being run through
2295 file_full_name. */
2296 const char **file_names;
2297
2298 /* The file names from the line table after being run through
2299 gdb_realpath. These are computed lazily. */
2300 const char **real_names;
2301 };
2302
2303 /* When using the index (and thus not using psymtabs), each CU has an
2304 object of this type. This is used to hold information needed by
2305 the various "quick" methods. */
2306 struct dwarf2_per_cu_quick_data
2307 {
2308 /* The file table. This can be NULL if there was no file table
2309 or it's currently not read in.
2310 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2311 struct quick_file_names *file_names;
2312
2313 /* A temporary mark bit used when iterating over all CUs in
2314 expand_symtabs_matching. */
2315 unsigned int mark : 1;
2316
2317 /* True if we've tried to read the file table and found there isn't one.
2318 There will be no point in trying to read it again next time. */
2319 unsigned int no_file_data : 1;
2320 };
2321
2322 /* Utility hash function for a stmt_list_hash. */
2323
2324 static hashval_t
2325 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2326 {
2327 hashval_t v = 0;
2328
2329 if (stmt_list_hash->dwo_unit != NULL)
2330 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2331 v += to_underlying (stmt_list_hash->line_sect_off);
2332 return v;
2333 }
2334
2335 /* Utility equality function for a stmt_list_hash. */
2336
2337 static int
2338 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2339 const struct stmt_list_hash *rhs)
2340 {
2341 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2342 return 0;
2343 if (lhs->dwo_unit != NULL
2344 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2345 return 0;
2346
2347 return lhs->line_sect_off == rhs->line_sect_off;
2348 }
2349
2350 /* Hash function for a quick_file_names. */
2351
2352 static hashval_t
2353 hash_file_name_entry (const void *e)
2354 {
2355 const struct quick_file_names *file_data
2356 = (const struct quick_file_names *) e;
2357
2358 return hash_stmt_list_entry (&file_data->hash);
2359 }
2360
2361 /* Equality function for a quick_file_names. */
2362
2363 static int
2364 eq_file_name_entry (const void *a, const void *b)
2365 {
2366 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2367 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2368
2369 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2370 }
2371
2372 /* Delete function for a quick_file_names. */
2373
2374 static void
2375 delete_file_name_entry (void *e)
2376 {
2377 struct quick_file_names *file_data = (struct quick_file_names *) e;
2378 int i;
2379
2380 for (i = 0; i < file_data->num_file_names; ++i)
2381 {
2382 xfree ((void*) file_data->file_names[i]);
2383 if (file_data->real_names)
2384 xfree ((void*) file_data->real_names[i]);
2385 }
2386
2387 /* The space for the struct itself lives on the obstack, so we don't
2388 free it here. */
2389 }
2390
2391 /* Create a quick_file_names hash table. */
2392
2393 static htab_up
2394 create_quick_file_names_table (unsigned int nr_initial_entries)
2395 {
2396 return htab_up (htab_create_alloc (nr_initial_entries,
2397 hash_file_name_entry, eq_file_name_entry,
2398 delete_file_name_entry, xcalloc, xfree));
2399 }
2400
2401 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2402 function is unrelated to symtabs, symtab would have to be created afterwards.
2403 You should call age_cached_comp_units after processing the CU. */
2404
2405 static dwarf2_cu *
2406 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2407 bool skip_partial)
2408 {
2409 if (per_cu->is_debug_types)
2410 load_full_type_unit (per_cu, per_objfile);
2411 else
2412 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2413 skip_partial, language_minimal);
2414
2415 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2416 if (cu == nullptr)
2417 return nullptr; /* Dummy CU. */
2418
2419 dwarf2_find_base_address (cu->dies, cu);
2420
2421 return cu;
2422 }
2423
2424 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2425
2426 static void
2427 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2428 dwarf2_per_objfile *per_objfile, bool skip_partial)
2429 {
2430 /* Skip type_unit_groups, reading the type units they contain
2431 is handled elsewhere. */
2432 if (per_cu->type_unit_group_p ())
2433 return;
2434
2435 /* The destructor of dwarf2_queue_guard frees any entries left on
2436 the queue. After this point we're guaranteed to leave this function
2437 with the dwarf queue empty. */
2438 dwarf2_queue_guard q_guard (per_objfile);
2439
2440 if (!per_objfile->symtab_set_p (per_cu))
2441 {
2442 queue_comp_unit (per_cu, per_objfile, language_minimal);
2443 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2444
2445 /* If we just loaded a CU from a DWO, and we're working with an index
2446 that may badly handle TUs, load all the TUs in that DWO as well.
2447 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2448 if (!per_cu->is_debug_types
2449 && cu != NULL
2450 && cu->dwo_unit != NULL
2451 && per_objfile->per_bfd->index_table != NULL
2452 && per_objfile->per_bfd->index_table->version <= 7
2453 /* DWP files aren't supported yet. */
2454 && get_dwp_file (per_objfile) == NULL)
2455 queue_and_load_all_dwo_tus (cu);
2456 }
2457
2458 process_queue (per_objfile);
2459
2460 /* Age the cache, releasing compilation units that have not
2461 been used recently. */
2462 per_objfile->age_comp_units ();
2463 }
2464
2465 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2466 the per-objfile for which this symtab is instantiated.
2467
2468 Returns the resulting symbol table. */
2469
2470 static struct compunit_symtab *
2471 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2472 dwarf2_per_objfile *per_objfile,
2473 bool skip_partial)
2474 {
2475 gdb_assert (per_objfile->per_bfd->using_index);
2476
2477 if (!per_objfile->symtab_set_p (per_cu))
2478 {
2479 free_cached_comp_units freer (per_objfile);
2480 scoped_restore decrementer = increment_reading_symtab ();
2481 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2482 process_cu_includes (per_objfile);
2483 }
2484
2485 return per_objfile->get_symtab (per_cu);
2486 }
2487
2488 /* See declaration. */
2489
2490 dwarf2_per_cu_data *
2491 dwarf2_per_bfd::get_cutu (int index)
2492 {
2493 if (index >= this->all_comp_units.size ())
2494 {
2495 index -= this->all_comp_units.size ();
2496 gdb_assert (index < this->all_type_units.size ());
2497 return &this->all_type_units[index]->per_cu;
2498 }
2499
2500 return this->all_comp_units[index];
2501 }
2502
2503 /* See declaration. */
2504
2505 dwarf2_per_cu_data *
2506 dwarf2_per_bfd::get_cu (int index)
2507 {
2508 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2509
2510 return this->all_comp_units[index];
2511 }
2512
2513 /* See declaration. */
2514
2515 signatured_type *
2516 dwarf2_per_bfd::get_tu (int index)
2517 {
2518 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2519
2520 return this->all_type_units[index];
2521 }
2522
2523 /* See read.h. */
2524
2525 dwarf2_per_cu_data *
2526 dwarf2_per_bfd::allocate_per_cu ()
2527 {
2528 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2529 result->per_bfd = this;
2530 result->index = m_num_psymtabs++;
2531 return result;
2532 }
2533
2534 /* See read.h. */
2535
2536 signatured_type *
2537 dwarf2_per_bfd::allocate_signatured_type ()
2538 {
2539 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2540 result->per_cu.per_bfd = this;
2541 result->per_cu.index = m_num_psymtabs++;
2542 return result;
2543 }
2544
2545 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2546 obstack, and constructed with the specified field values. */
2547
2548 static dwarf2_per_cu_data *
2549 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2550 struct dwarf2_section_info *section,
2551 int is_dwz,
2552 sect_offset sect_off, ULONGEST length)
2553 {
2554 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
2555 the_cu->sect_off = sect_off;
2556 the_cu->length = length;
2557 the_cu->section = section;
2558 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2559 struct dwarf2_per_cu_quick_data);
2560 the_cu->is_dwz = is_dwz;
2561 return the_cu;
2562 }
2563
2564 /* A helper for create_cus_from_index that handles a given list of
2565 CUs. */
2566
2567 static void
2568 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2569 const gdb_byte *cu_list, offset_type n_elements,
2570 struct dwarf2_section_info *section,
2571 int is_dwz)
2572 {
2573 for (offset_type i = 0; i < n_elements; i += 2)
2574 {
2575 gdb_static_assert (sizeof (ULONGEST) >= 8);
2576
2577 sect_offset sect_off
2578 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2579 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2580 cu_list += 2 * 8;
2581
2582 dwarf2_per_cu_data *per_cu
2583 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2584 length);
2585 per_bfd->all_comp_units.push_back (per_cu);
2586 }
2587 }
2588
2589 /* Read the CU list from the mapped index, and use it to create all
2590 the CU objects for PER_BFD. */
2591
2592 static void
2593 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2594 const gdb_byte *cu_list, offset_type cu_list_elements,
2595 const gdb_byte *dwz_list, offset_type dwz_elements)
2596 {
2597 gdb_assert (per_bfd->all_comp_units.empty ());
2598 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2599
2600 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2601 &per_bfd->info, 0);
2602
2603 if (dwz_elements == 0)
2604 return;
2605
2606 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2607 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2608 &dwz->info, 1);
2609 }
2610
2611 /* Create the signatured type hash table from the index. */
2612
2613 static void
2614 create_signatured_type_table_from_index
2615 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2616 const gdb_byte *bytes, offset_type elements)
2617 {
2618 gdb_assert (per_bfd->all_type_units.empty ());
2619 per_bfd->all_type_units.reserve (elements / 3);
2620
2621 htab_up sig_types_hash = allocate_signatured_type_table ();
2622
2623 for (offset_type i = 0; i < elements; i += 3)
2624 {
2625 struct signatured_type *sig_type;
2626 ULONGEST signature;
2627 void **slot;
2628 cu_offset type_offset_in_tu;
2629
2630 gdb_static_assert (sizeof (ULONGEST) >= 8);
2631 sect_offset sect_off
2632 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2633 type_offset_in_tu
2634 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2635 BFD_ENDIAN_LITTLE);
2636 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2637 bytes += 3 * 8;
2638
2639 sig_type = per_bfd->allocate_signatured_type ();
2640 sig_type->signature = signature;
2641 sig_type->type_offset_in_tu = type_offset_in_tu;
2642 sig_type->per_cu.is_debug_types = 1;
2643 sig_type->per_cu.section = section;
2644 sig_type->per_cu.sect_off = sect_off;
2645 sig_type->per_cu.v.quick
2646 = OBSTACK_ZALLOC (&per_bfd->obstack,
2647 struct dwarf2_per_cu_quick_data);
2648
2649 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2650 *slot = sig_type;
2651
2652 per_bfd->all_type_units.push_back (sig_type);
2653 }
2654
2655 per_bfd->signatured_types = std::move (sig_types_hash);
2656 }
2657
2658 /* Create the signatured type hash table from .debug_names. */
2659
2660 static void
2661 create_signatured_type_table_from_debug_names
2662 (dwarf2_per_objfile *per_objfile,
2663 const mapped_debug_names &map,
2664 struct dwarf2_section_info *section,
2665 struct dwarf2_section_info *abbrev_section)
2666 {
2667 struct objfile *objfile = per_objfile->objfile;
2668
2669 section->read (objfile);
2670 abbrev_section->read (objfile);
2671
2672 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2673 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2674
2675 htab_up sig_types_hash = allocate_signatured_type_table ();
2676
2677 for (uint32_t i = 0; i < map.tu_count; ++i)
2678 {
2679 struct signatured_type *sig_type;
2680 void **slot;
2681
2682 sect_offset sect_off
2683 = (sect_offset) (extract_unsigned_integer
2684 (map.tu_table_reordered + i * map.offset_size,
2685 map.offset_size,
2686 map.dwarf5_byte_order));
2687
2688 comp_unit_head cu_header;
2689 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2690 abbrev_section,
2691 section->buffer + to_underlying (sect_off),
2692 rcuh_kind::TYPE);
2693
2694 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
2695 sig_type->signature = cu_header.signature;
2696 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2697 sig_type->per_cu.is_debug_types = 1;
2698 sig_type->per_cu.section = section;
2699 sig_type->per_cu.sect_off = sect_off;
2700 sig_type->per_cu.v.quick
2701 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2702 struct dwarf2_per_cu_quick_data);
2703
2704 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2705 *slot = sig_type;
2706
2707 per_objfile->per_bfd->all_type_units.push_back (sig_type);
2708 }
2709
2710 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2711 }
2712
2713 /* Read the address map data from the mapped index, and use it to
2714 populate the objfile's psymtabs_addrmap. */
2715
2716 static void
2717 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2718 struct mapped_index *index)
2719 {
2720 struct objfile *objfile = per_objfile->objfile;
2721 struct gdbarch *gdbarch = objfile->arch ();
2722 const gdb_byte *iter, *end;
2723 struct addrmap *mutable_map;
2724 CORE_ADDR baseaddr;
2725
2726 auto_obstack temp_obstack;
2727
2728 mutable_map = addrmap_create_mutable (&temp_obstack);
2729
2730 iter = index->address_table.data ();
2731 end = iter + index->address_table.size ();
2732
2733 baseaddr = objfile->text_section_offset ();
2734
2735 while (iter < end)
2736 {
2737 ULONGEST hi, lo, cu_index;
2738 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2739 iter += 8;
2740 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2741 iter += 8;
2742 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2743 iter += 4;
2744
2745 if (lo > hi)
2746 {
2747 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2748 hex_string (lo), hex_string (hi));
2749 continue;
2750 }
2751
2752 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
2753 {
2754 complaint (_(".gdb_index address table has invalid CU number %u"),
2755 (unsigned) cu_index);
2756 continue;
2757 }
2758
2759 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2760 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2761 addrmap_set_empty (mutable_map, lo, hi - 1,
2762 per_objfile->per_bfd->get_cu (cu_index));
2763 }
2764
2765 objfile->partial_symtabs->psymtabs_addrmap
2766 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2767 }
2768
2769 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2770 populate the objfile's psymtabs_addrmap. */
2771
2772 static void
2773 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2774 struct dwarf2_section_info *section)
2775 {
2776 struct objfile *objfile = per_objfile->objfile;
2777 bfd *abfd = objfile->obfd;
2778 struct gdbarch *gdbarch = objfile->arch ();
2779 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2780
2781 auto_obstack temp_obstack;
2782 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2783
2784 std::unordered_map<sect_offset,
2785 dwarf2_per_cu_data *,
2786 gdb::hash_enum<sect_offset>>
2787 debug_info_offset_to_per_cu;
2788 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
2789 {
2790 const auto insertpair
2791 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2792 if (!insertpair.second)
2793 {
2794 warning (_("Section .debug_aranges in %s has duplicate "
2795 "debug_info_offset %s, ignoring .debug_aranges."),
2796 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2797 return;
2798 }
2799 }
2800
2801 section->read (objfile);
2802
2803 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2804
2805 const gdb_byte *addr = section->buffer;
2806
2807 while (addr < section->buffer + section->size)
2808 {
2809 const gdb_byte *const entry_addr = addr;
2810 unsigned int bytes_read;
2811
2812 const LONGEST entry_length = read_initial_length (abfd, addr,
2813 &bytes_read);
2814 addr += bytes_read;
2815
2816 const gdb_byte *const entry_end = addr + entry_length;
2817 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2818 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2819 if (addr + entry_length > section->buffer + section->size)
2820 {
2821 warning (_("Section .debug_aranges in %s entry at offset %s "
2822 "length %s exceeds section length %s, "
2823 "ignoring .debug_aranges."),
2824 objfile_name (objfile),
2825 plongest (entry_addr - section->buffer),
2826 plongest (bytes_read + entry_length),
2827 pulongest (section->size));
2828 return;
2829 }
2830
2831 /* The version number. */
2832 const uint16_t version = read_2_bytes (abfd, addr);
2833 addr += 2;
2834 if (version != 2)
2835 {
2836 warning (_("Section .debug_aranges in %s entry at offset %s "
2837 "has unsupported version %d, ignoring .debug_aranges."),
2838 objfile_name (objfile),
2839 plongest (entry_addr - section->buffer), version);
2840 return;
2841 }
2842
2843 const uint64_t debug_info_offset
2844 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2845 addr += offset_size;
2846 const auto per_cu_it
2847 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2848 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2849 {
2850 warning (_("Section .debug_aranges in %s entry at offset %s "
2851 "debug_info_offset %s does not exists, "
2852 "ignoring .debug_aranges."),
2853 objfile_name (objfile),
2854 plongest (entry_addr - section->buffer),
2855 pulongest (debug_info_offset));
2856 return;
2857 }
2858 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2859
2860 const uint8_t address_size = *addr++;
2861 if (address_size < 1 || address_size > 8)
2862 {
2863 warning (_("Section .debug_aranges in %s entry at offset %s "
2864 "address_size %u is invalid, ignoring .debug_aranges."),
2865 objfile_name (objfile),
2866 plongest (entry_addr - section->buffer), address_size);
2867 return;
2868 }
2869
2870 const uint8_t segment_selector_size = *addr++;
2871 if (segment_selector_size != 0)
2872 {
2873 warning (_("Section .debug_aranges in %s entry at offset %s "
2874 "segment_selector_size %u is not supported, "
2875 "ignoring .debug_aranges."),
2876 objfile_name (objfile),
2877 plongest (entry_addr - section->buffer),
2878 segment_selector_size);
2879 return;
2880 }
2881
2882 /* Must pad to an alignment boundary that is twice the address
2883 size. It is undocumented by the DWARF standard but GCC does
2884 use it. */
2885 for (size_t padding = ((-(addr - section->buffer))
2886 & (2 * address_size - 1));
2887 padding > 0; padding--)
2888 if (*addr++ != 0)
2889 {
2890 warning (_("Section .debug_aranges in %s entry at offset %s "
2891 "padding is not zero, ignoring .debug_aranges."),
2892 objfile_name (objfile),
2893 plongest (entry_addr - section->buffer));
2894 return;
2895 }
2896
2897 for (;;)
2898 {
2899 if (addr + 2 * address_size > entry_end)
2900 {
2901 warning (_("Section .debug_aranges in %s entry at offset %s "
2902 "address list is not properly terminated, "
2903 "ignoring .debug_aranges."),
2904 objfile_name (objfile),
2905 plongest (entry_addr - section->buffer));
2906 return;
2907 }
2908 ULONGEST start = extract_unsigned_integer (addr, address_size,
2909 dwarf5_byte_order);
2910 addr += address_size;
2911 ULONGEST length = extract_unsigned_integer (addr, address_size,
2912 dwarf5_byte_order);
2913 addr += address_size;
2914 if (start == 0 && length == 0)
2915 break;
2916 if (start == 0 && !per_objfile->per_bfd->has_section_at_zero)
2917 {
2918 /* Symbol was eliminated due to a COMDAT group. */
2919 continue;
2920 }
2921 ULONGEST end = start + length;
2922 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2923 - baseaddr);
2924 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2925 - baseaddr);
2926 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2927 }
2928 }
2929
2930 objfile->partial_symtabs->psymtabs_addrmap
2931 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2932 }
2933
2934 /* Find a slot in the mapped index INDEX for the object named NAME.
2935 If NAME is found, set *VEC_OUT to point to the CU vector in the
2936 constant pool and return true. If NAME cannot be found, return
2937 false. */
2938
2939 static bool
2940 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2941 offset_type **vec_out)
2942 {
2943 offset_type hash;
2944 offset_type slot, step;
2945 int (*cmp) (const char *, const char *);
2946
2947 gdb::unique_xmalloc_ptr<char> without_params;
2948 if (current_language->la_language == language_cplus
2949 || current_language->la_language == language_fortran
2950 || current_language->la_language == language_d)
2951 {
2952 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2953 not contain any. */
2954
2955 if (strchr (name, '(') != NULL)
2956 {
2957 without_params = cp_remove_params (name);
2958
2959 if (without_params != NULL)
2960 name = without_params.get ();
2961 }
2962 }
2963
2964 /* Index version 4 did not support case insensitive searches. But the
2965 indices for case insensitive languages are built in lowercase, therefore
2966 simulate our NAME being searched is also lowercased. */
2967 hash = mapped_index_string_hash ((index->version == 4
2968 && case_sensitivity == case_sensitive_off
2969 ? 5 : index->version),
2970 name);
2971
2972 slot = hash & (index->symbol_table.size () - 1);
2973 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2974 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2975
2976 for (;;)
2977 {
2978 const char *str;
2979
2980 const auto &bucket = index->symbol_table[slot];
2981 if (bucket.name == 0 && bucket.vec == 0)
2982 return false;
2983
2984 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2985 if (!cmp (name, str))
2986 {
2987 *vec_out = (offset_type *) (index->constant_pool
2988 + MAYBE_SWAP (bucket.vec));
2989 return true;
2990 }
2991
2992 slot = (slot + step) & (index->symbol_table.size () - 1);
2993 }
2994 }
2995
2996 /* A helper function that reads the .gdb_index from BUFFER and fills
2997 in MAP. FILENAME is the name of the file containing the data;
2998 it is used for error reporting. DEPRECATED_OK is true if it is
2999 ok to use deprecated sections.
3000
3001 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3002 out parameters that are filled in with information about the CU and
3003 TU lists in the section.
3004
3005 Returns true if all went well, false otherwise. */
3006
3007 static bool
3008 read_gdb_index_from_buffer (const char *filename,
3009 bool deprecated_ok,
3010 gdb::array_view<const gdb_byte> buffer,
3011 struct mapped_index *map,
3012 const gdb_byte **cu_list,
3013 offset_type *cu_list_elements,
3014 const gdb_byte **types_list,
3015 offset_type *types_list_elements)
3016 {
3017 const gdb_byte *addr = &buffer[0];
3018
3019 /* Version check. */
3020 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3021 /* Versions earlier than 3 emitted every copy of a psymbol. This
3022 causes the index to behave very poorly for certain requests. Version 3
3023 contained incomplete addrmap. So, it seems better to just ignore such
3024 indices. */
3025 if (version < 4)
3026 {
3027 static int warning_printed = 0;
3028 if (!warning_printed)
3029 {
3030 warning (_("Skipping obsolete .gdb_index section in %s."),
3031 filename);
3032 warning_printed = 1;
3033 }
3034 return 0;
3035 }
3036 /* Index version 4 uses a different hash function than index version
3037 5 and later.
3038
3039 Versions earlier than 6 did not emit psymbols for inlined
3040 functions. Using these files will cause GDB not to be able to
3041 set breakpoints on inlined functions by name, so we ignore these
3042 indices unless the user has done
3043 "set use-deprecated-index-sections on". */
3044 if (version < 6 && !deprecated_ok)
3045 {
3046 static int warning_printed = 0;
3047 if (!warning_printed)
3048 {
3049 warning (_("\
3050 Skipping deprecated .gdb_index section in %s.\n\
3051 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3052 to use the section anyway."),
3053 filename);
3054 warning_printed = 1;
3055 }
3056 return 0;
3057 }
3058 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3059 of the TU (for symbols coming from TUs),
3060 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3061 Plus gold-generated indices can have duplicate entries for global symbols,
3062 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3063 These are just performance bugs, and we can't distinguish gdb-generated
3064 indices from gold-generated ones, so issue no warning here. */
3065
3066 /* Indexes with higher version than the one supported by GDB may be no
3067 longer backward compatible. */
3068 if (version > 8)
3069 return 0;
3070
3071 map->version = version;
3072
3073 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3074
3075 int i = 0;
3076 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3077 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3078 / 8);
3079 ++i;
3080
3081 *types_list = addr + MAYBE_SWAP (metadata[i]);
3082 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3083 - MAYBE_SWAP (metadata[i]))
3084 / 8);
3085 ++i;
3086
3087 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3088 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3089 map->address_table
3090 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3091 ++i;
3092
3093 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3094 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3095 map->symbol_table
3096 = gdb::array_view<mapped_index::symbol_table_slot>
3097 ((mapped_index::symbol_table_slot *) symbol_table,
3098 (mapped_index::symbol_table_slot *) symbol_table_end);
3099
3100 ++i;
3101 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3102
3103 return 1;
3104 }
3105
3106 /* Callback types for dwarf2_read_gdb_index. */
3107
3108 typedef gdb::function_view
3109 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3110 get_gdb_index_contents_ftype;
3111 typedef gdb::function_view
3112 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3113 get_gdb_index_contents_dwz_ftype;
3114
3115 /* Read .gdb_index. If everything went ok, initialize the "quick"
3116 elements of all the CUs and return 1. Otherwise, return 0. */
3117
3118 static int
3119 dwarf2_read_gdb_index
3120 (dwarf2_per_objfile *per_objfile,
3121 get_gdb_index_contents_ftype get_gdb_index_contents,
3122 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3123 {
3124 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3125 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3126 struct dwz_file *dwz;
3127 struct objfile *objfile = per_objfile->objfile;
3128 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3129
3130 gdb::array_view<const gdb_byte> main_index_contents
3131 = get_gdb_index_contents (objfile, per_bfd);
3132
3133 if (main_index_contents.empty ())
3134 return 0;
3135
3136 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3137 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3138 use_deprecated_index_sections,
3139 main_index_contents, map.get (), &cu_list,
3140 &cu_list_elements, &types_list,
3141 &types_list_elements))
3142 return 0;
3143
3144 /* Don't use the index if it's empty. */
3145 if (map->symbol_table.empty ())
3146 return 0;
3147
3148 /* If there is a .dwz file, read it so we can get its CU list as
3149 well. */
3150 dwz = dwarf2_get_dwz_file (per_bfd);
3151 if (dwz != NULL)
3152 {
3153 struct mapped_index dwz_map;
3154 const gdb_byte *dwz_types_ignore;
3155 offset_type dwz_types_elements_ignore;
3156
3157 gdb::array_view<const gdb_byte> dwz_index_content
3158 = get_gdb_index_contents_dwz (objfile, dwz);
3159
3160 if (dwz_index_content.empty ())
3161 return 0;
3162
3163 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3164 1, dwz_index_content, &dwz_map,
3165 &dwz_list, &dwz_list_elements,
3166 &dwz_types_ignore,
3167 &dwz_types_elements_ignore))
3168 {
3169 warning (_("could not read '.gdb_index' section from %s; skipping"),
3170 bfd_get_filename (dwz->dwz_bfd.get ()));
3171 return 0;
3172 }
3173 }
3174
3175 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3176 dwz_list_elements);
3177
3178 if (types_list_elements)
3179 {
3180 /* We can only handle a single .debug_types when we have an
3181 index. */
3182 if (per_bfd->types.size () != 1)
3183 return 0;
3184
3185 dwarf2_section_info *section = &per_bfd->types[0];
3186
3187 create_signatured_type_table_from_index (per_bfd, section, types_list,
3188 types_list_elements);
3189 }
3190
3191 create_addrmap_from_index (per_objfile, map.get ());
3192
3193 per_bfd->index_table = std::move (map);
3194 per_bfd->using_index = 1;
3195 per_bfd->quick_file_names_table =
3196 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3197
3198 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
3199 objfiles using the same BFD. */
3200 gdb_assert (per_bfd->partial_symtabs == nullptr);
3201 per_bfd->partial_symtabs = objfile->partial_symtabs;
3202
3203 return 1;
3204 }
3205
3206 /* die_reader_func for dw2_get_file_names. */
3207
3208 static void
3209 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3210 const gdb_byte *info_ptr,
3211 struct die_info *comp_unit_die)
3212 {
3213 struct dwarf2_cu *cu = reader->cu;
3214 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3215 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3216 struct dwarf2_per_cu_data *lh_cu;
3217 struct attribute *attr;
3218 void **slot;
3219 struct quick_file_names *qfn;
3220
3221 gdb_assert (! this_cu->is_debug_types);
3222
3223 /* Our callers never want to match partial units -- instead they
3224 will match the enclosing full CU. */
3225 if (comp_unit_die->tag == DW_TAG_partial_unit)
3226 {
3227 this_cu->v.quick->no_file_data = 1;
3228 return;
3229 }
3230
3231 lh_cu = this_cu;
3232 slot = NULL;
3233
3234 line_header_up lh;
3235 sect_offset line_offset {};
3236
3237 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3238 if (attr != nullptr && attr->form_is_unsigned ())
3239 {
3240 struct quick_file_names find_entry;
3241
3242 line_offset = (sect_offset) attr->as_unsigned ();
3243
3244 /* We may have already read in this line header (TU line header sharing).
3245 If we have we're done. */
3246 find_entry.hash.dwo_unit = cu->dwo_unit;
3247 find_entry.hash.line_sect_off = line_offset;
3248 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3249 &find_entry, INSERT);
3250 if (*slot != NULL)
3251 {
3252 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3253 return;
3254 }
3255
3256 lh = dwarf_decode_line_header (line_offset, cu);
3257 }
3258 if (lh == NULL)
3259 {
3260 lh_cu->v.quick->no_file_data = 1;
3261 return;
3262 }
3263
3264 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3265 qfn->hash.dwo_unit = cu->dwo_unit;
3266 qfn->hash.line_sect_off = line_offset;
3267 gdb_assert (slot != NULL);
3268 *slot = qfn;
3269
3270 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3271
3272 int offset = 0;
3273 if (strcmp (fnd.name, "<unknown>") != 0)
3274 ++offset;
3275
3276 qfn->num_file_names = offset + lh->file_names_size ();
3277 qfn->file_names =
3278 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3279 qfn->num_file_names);
3280 if (offset != 0)
3281 qfn->file_names[0] = xstrdup (fnd.name);
3282 for (int i = 0; i < lh->file_names_size (); ++i)
3283 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3284 fnd.comp_dir).release ();
3285 qfn->real_names = NULL;
3286
3287 lh_cu->v.quick->file_names = qfn;
3288 }
3289
3290 /* A helper for the "quick" functions which attempts to read the line
3291 table for THIS_CU. */
3292
3293 static struct quick_file_names *
3294 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3295 dwarf2_per_objfile *per_objfile)
3296 {
3297 /* This should never be called for TUs. */
3298 gdb_assert (! this_cu->is_debug_types);
3299 /* Nor type unit groups. */
3300 gdb_assert (! this_cu->type_unit_group_p ());
3301
3302 if (this_cu->v.quick->file_names != NULL)
3303 return this_cu->v.quick->file_names;
3304 /* If we know there is no line data, no point in looking again. */
3305 if (this_cu->v.quick->no_file_data)
3306 return NULL;
3307
3308 cutu_reader reader (this_cu, per_objfile);
3309 if (!reader.dummy_p)
3310 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3311
3312 if (this_cu->v.quick->no_file_data)
3313 return NULL;
3314 return this_cu->v.quick->file_names;
3315 }
3316
3317 /* A helper for the "quick" functions which computes and caches the
3318 real path for a given file name from the line table. */
3319
3320 static const char *
3321 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3322 struct quick_file_names *qfn, int index)
3323 {
3324 if (qfn->real_names == NULL)
3325 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3326 qfn->num_file_names, const char *);
3327
3328 if (qfn->real_names[index] == NULL)
3329 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3330
3331 return qfn->real_names[index];
3332 }
3333
3334 static struct symtab *
3335 dw2_find_last_source_symtab (struct objfile *objfile)
3336 {
3337 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3338 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3339 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3340
3341 if (cust == NULL)
3342 return NULL;
3343
3344 return compunit_primary_filetab (cust);
3345 }
3346
3347 /* Traversal function for dw2_forget_cached_source_info. */
3348
3349 static int
3350 dw2_free_cached_file_names (void **slot, void *info)
3351 {
3352 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3353
3354 if (file_data->real_names)
3355 {
3356 int i;
3357
3358 for (i = 0; i < file_data->num_file_names; ++i)
3359 {
3360 xfree ((void*) file_data->real_names[i]);
3361 file_data->real_names[i] = NULL;
3362 }
3363 }
3364
3365 return 1;
3366 }
3367
3368 static void
3369 dw2_forget_cached_source_info (struct objfile *objfile)
3370 {
3371 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3372
3373 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3374 dw2_free_cached_file_names, NULL);
3375 }
3376
3377 /* Helper function for dw2_map_symtabs_matching_filename that expands
3378 the symtabs and calls the iterator. */
3379
3380 static int
3381 dw2_map_expand_apply (struct objfile *objfile,
3382 struct dwarf2_per_cu_data *per_cu,
3383 const char *name, const char *real_path,
3384 gdb::function_view<bool (symtab *)> callback)
3385 {
3386 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3387
3388 /* Don't visit already-expanded CUs. */
3389 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3390 if (per_objfile->symtab_set_p (per_cu))
3391 return 0;
3392
3393 /* This may expand more than one symtab, and we want to iterate over
3394 all of them. */
3395 dw2_instantiate_symtab (per_cu, per_objfile, false);
3396
3397 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3398 last_made, callback);
3399 }
3400
3401 /* Implementation of the map_symtabs_matching_filename method. */
3402
3403 static bool
3404 dw2_map_symtabs_matching_filename
3405 (struct objfile *objfile, const char *name, const char *real_path,
3406 gdb::function_view<bool (symtab *)> callback)
3407 {
3408 const char *name_basename = lbasename (name);
3409 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3410
3411 /* The rule is CUs specify all the files, including those used by
3412 any TU, so there's no need to scan TUs here. */
3413
3414 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3415 {
3416 /* We only need to look at symtabs not already expanded. */
3417 if (per_objfile->symtab_set_p (per_cu))
3418 continue;
3419
3420 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3421 if (file_data == NULL)
3422 continue;
3423
3424 for (int j = 0; j < file_data->num_file_names; ++j)
3425 {
3426 const char *this_name = file_data->file_names[j];
3427 const char *this_real_name;
3428
3429 if (compare_filenames_for_search (this_name, name))
3430 {
3431 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3432 callback))
3433 return true;
3434 continue;
3435 }
3436
3437 /* Before we invoke realpath, which can get expensive when many
3438 files are involved, do a quick comparison of the basenames. */
3439 if (! basenames_may_differ
3440 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3441 continue;
3442
3443 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
3444 if (compare_filenames_for_search (this_real_name, name))
3445 {
3446 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3447 callback))
3448 return true;
3449 continue;
3450 }
3451
3452 if (real_path != NULL)
3453 {
3454 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3455 gdb_assert (IS_ABSOLUTE_PATH (name));
3456 if (this_real_name != NULL
3457 && FILENAME_CMP (real_path, this_real_name) == 0)
3458 {
3459 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3460 callback))
3461 return true;
3462 continue;
3463 }
3464 }
3465 }
3466 }
3467
3468 return false;
3469 }
3470
3471 /* Struct used to manage iterating over all CUs looking for a symbol. */
3472
3473 struct dw2_symtab_iterator
3474 {
3475 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3476 dwarf2_per_objfile *per_objfile;
3477 /* If set, only look for symbols that match that block. Valid values are
3478 GLOBAL_BLOCK and STATIC_BLOCK. */
3479 gdb::optional<block_enum> block_index;
3480 /* The kind of symbol we're looking for. */
3481 domain_enum domain;
3482 /* The list of CUs from the index entry of the symbol,
3483 or NULL if not found. */
3484 offset_type *vec;
3485 /* The next element in VEC to look at. */
3486 int next;
3487 /* The number of elements in VEC, or zero if there is no match. */
3488 int length;
3489 /* Have we seen a global version of the symbol?
3490 If so we can ignore all further global instances.
3491 This is to work around gold/15646, inefficient gold-generated
3492 indices. */
3493 int global_seen;
3494 };
3495
3496 /* Initialize the index symtab iterator ITER, common part. */
3497
3498 static void
3499 dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3500 dwarf2_per_objfile *per_objfile,
3501 gdb::optional<block_enum> block_index,
3502 domain_enum domain)
3503 {
3504 iter->per_objfile = per_objfile;
3505 iter->block_index = block_index;
3506 iter->domain = domain;
3507 iter->next = 0;
3508 iter->global_seen = 0;
3509 iter->vec = NULL;
3510 iter->length = 0;
3511 }
3512
3513 /* Initialize the index symtab iterator ITER, const char *NAME variant. */
3514
3515 static void
3516 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3517 dwarf2_per_objfile *per_objfile,
3518 gdb::optional<block_enum> block_index,
3519 domain_enum domain,
3520 const char *name)
3521 {
3522 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3523
3524 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3525 /* index is NULL if OBJF_READNOW. */
3526 if (index == NULL)
3527 return;
3528
3529 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3530 iter->length = MAYBE_SWAP (*iter->vec);
3531 }
3532
3533 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3534
3535 static void
3536 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3537 dwarf2_per_objfile *per_objfile,
3538 gdb::optional<block_enum> block_index,
3539 domain_enum domain, offset_type namei)
3540 {
3541 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3542
3543 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3544 /* index is NULL if OBJF_READNOW. */
3545 if (index == NULL)
3546 return;
3547
3548 gdb_assert (!index->symbol_name_slot_invalid (namei));
3549 const auto &bucket = index->symbol_table[namei];
3550
3551 iter->vec = (offset_type *) (index->constant_pool
3552 + MAYBE_SWAP (bucket.vec));
3553 iter->length = MAYBE_SWAP (*iter->vec);
3554 }
3555
3556 /* Return the next matching CU or NULL if there are no more. */
3557
3558 static struct dwarf2_per_cu_data *
3559 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3560 {
3561 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3562
3563 for ( ; iter->next < iter->length; ++iter->next)
3564 {
3565 offset_type cu_index_and_attrs =
3566 MAYBE_SWAP (iter->vec[iter->next + 1]);
3567 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3568 gdb_index_symbol_kind symbol_kind =
3569 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3570 /* Only check the symbol attributes if they're present.
3571 Indices prior to version 7 don't record them,
3572 and indices >= 7 may elide them for certain symbols
3573 (gold does this). */
3574 int attrs_valid =
3575 (per_objfile->per_bfd->index_table->version >= 7
3576 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3577
3578 /* Don't crash on bad data. */
3579 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3580 + per_objfile->per_bfd->all_type_units.size ()))
3581 {
3582 complaint (_(".gdb_index entry has bad CU index"
3583 " [in module %s]"), objfile_name (per_objfile->objfile));
3584 continue;
3585 }
3586
3587 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3588
3589 /* Skip if already read in. */
3590 if (per_objfile->symtab_set_p (per_cu))
3591 continue;
3592
3593 /* Check static vs global. */
3594 if (attrs_valid)
3595 {
3596 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3597
3598 if (iter->block_index.has_value ())
3599 {
3600 bool want_static = *iter->block_index == STATIC_BLOCK;
3601
3602 if (is_static != want_static)
3603 continue;
3604 }
3605
3606 /* Work around gold/15646. */
3607 if (!is_static
3608 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3609 {
3610 if (iter->global_seen)
3611 continue;
3612
3613 iter->global_seen = 1;
3614 }
3615 }
3616
3617 /* Only check the symbol's kind if it has one. */
3618 if (attrs_valid)
3619 {
3620 switch (iter->domain)
3621 {
3622 case VAR_DOMAIN:
3623 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3624 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3625 /* Some types are also in VAR_DOMAIN. */
3626 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3627 continue;
3628 break;
3629 case STRUCT_DOMAIN:
3630 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3631 continue;
3632 break;
3633 case LABEL_DOMAIN:
3634 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3635 continue;
3636 break;
3637 case MODULE_DOMAIN:
3638 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3639 continue;
3640 break;
3641 default:
3642 break;
3643 }
3644 }
3645
3646 ++iter->next;
3647 return per_cu;
3648 }
3649
3650 return NULL;
3651 }
3652
3653 static struct compunit_symtab *
3654 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3655 const char *name, domain_enum domain)
3656 {
3657 struct compunit_symtab *stab_best = NULL;
3658 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3659
3660 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3661
3662 struct dw2_symtab_iterator iter;
3663 struct dwarf2_per_cu_data *per_cu;
3664
3665 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
3666
3667 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3668 {
3669 struct symbol *sym, *with_opaque = NULL;
3670 struct compunit_symtab *stab
3671 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3672 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3673 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3674
3675 sym = block_find_symbol (block, name, domain,
3676 block_find_non_opaque_type_preferred,
3677 &with_opaque);
3678
3679 /* Some caution must be observed with overloaded functions
3680 and methods, since the index will not contain any overload
3681 information (but NAME might contain it). */
3682
3683 if (sym != NULL
3684 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3685 return stab;
3686 if (with_opaque != NULL
3687 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3688 stab_best = stab;
3689
3690 /* Keep looking through other CUs. */
3691 }
3692
3693 return stab_best;
3694 }
3695
3696 static void
3697 dw2_print_stats (struct objfile *objfile)
3698 {
3699 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3700 int total = (per_objfile->per_bfd->all_comp_units.size ()
3701 + per_objfile->per_bfd->all_type_units.size ());
3702 int count = 0;
3703
3704 for (int i = 0; i < total; ++i)
3705 {
3706 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3707
3708 if (!per_objfile->symtab_set_p (per_cu))
3709 ++count;
3710 }
3711 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3712 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3713 }
3714
3715 /* This dumps minimal information about the index.
3716 It is called via "mt print objfiles".
3717 One use is to verify .gdb_index has been loaded by the
3718 gdb.dwarf2/gdb-index.exp testcase. */
3719
3720 static void
3721 dw2_dump (struct objfile *objfile)
3722 {
3723 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3724
3725 gdb_assert (per_objfile->per_bfd->using_index);
3726 printf_filtered (".gdb_index:");
3727 if (per_objfile->per_bfd->index_table != NULL)
3728 {
3729 printf_filtered (" version %d\n",
3730 per_objfile->per_bfd->index_table->version);
3731 }
3732 else
3733 printf_filtered (" faked for \"readnow\"\n");
3734 printf_filtered ("\n");
3735 }
3736
3737 static void
3738 dw2_expand_symtabs_for_function (struct objfile *objfile,
3739 const char *func_name)
3740 {
3741 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3742
3743 struct dw2_symtab_iterator iter;
3744 struct dwarf2_per_cu_data *per_cu;
3745
3746 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
3747
3748 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3749 dw2_instantiate_symtab (per_cu, per_objfile, false);
3750
3751 }
3752
3753 static void
3754 dw2_expand_all_symtabs (struct objfile *objfile)
3755 {
3756 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3757 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3758 + per_objfile->per_bfd->all_type_units.size ());
3759
3760 for (int i = 0; i < total_units; ++i)
3761 {
3762 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3763
3764 /* We don't want to directly expand a partial CU, because if we
3765 read it with the wrong language, then assertion failures can
3766 be triggered later on. See PR symtab/23010. So, tell
3767 dw2_instantiate_symtab to skip partial CUs -- any important
3768 partial CU will be read via DW_TAG_imported_unit anyway. */
3769 dw2_instantiate_symtab (per_cu, per_objfile, true);
3770 }
3771 }
3772
3773 static void
3774 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3775 const char *fullname)
3776 {
3777 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3778
3779 /* We don't need to consider type units here.
3780 This is only called for examining code, e.g. expand_line_sal.
3781 There can be an order of magnitude (or more) more type units
3782 than comp units, and we avoid them if we can. */
3783
3784 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3785 {
3786 /* We only need to look at symtabs not already expanded. */
3787 if (per_objfile->symtab_set_p (per_cu))
3788 continue;
3789
3790 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3791 if (file_data == NULL)
3792 continue;
3793
3794 for (int j = 0; j < file_data->num_file_names; ++j)
3795 {
3796 const char *this_fullname = file_data->file_names[j];
3797
3798 if (filename_cmp (this_fullname, fullname) == 0)
3799 {
3800 dw2_instantiate_symtab (per_cu, per_objfile, false);
3801 break;
3802 }
3803 }
3804 }
3805 }
3806
3807 static void
3808 dw2_expand_symtabs_matching_symbol
3809 (mapped_index_base &index,
3810 const lookup_name_info &lookup_name_in,
3811 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3812 enum search_domain kind,
3813 gdb::function_view<bool (offset_type)> match_callback,
3814 dwarf2_per_objfile *per_objfile);
3815
3816 static void
3817 dw2_expand_symtabs_matching_one
3818 (dwarf2_per_cu_data *per_cu,
3819 dwarf2_per_objfile *per_objfile,
3820 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3821 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3822
3823 static void
3824 dw2_map_matching_symbols
3825 (struct objfile *objfile,
3826 const lookup_name_info &name, domain_enum domain,
3827 int global,
3828 gdb::function_view<symbol_found_callback_ftype> callback,
3829 symbol_compare_ftype *ordered_compare)
3830 {
3831 /* Used for Ada. */
3832 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3833
3834 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3835
3836 if (per_objfile->per_bfd->index_table != nullptr)
3837 {
3838 mapped_index &index = *per_objfile->per_bfd->index_table;
3839
3840 const char *match_name = name.ada ().lookup_name ().c_str ();
3841 auto matcher = [&] (const char *symname)
3842 {
3843 if (ordered_compare == nullptr)
3844 return true;
3845 return ordered_compare (symname, match_name) == 0;
3846 };
3847
3848 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3849 [&] (offset_type namei)
3850 {
3851 struct dw2_symtab_iterator iter;
3852 struct dwarf2_per_cu_data *per_cu;
3853
3854 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3855 namei);
3856 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3857 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3858 nullptr);
3859 return true;
3860 }, per_objfile);
3861 }
3862 else
3863 {
3864 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3865 proceed assuming all symtabs have been read in. */
3866 }
3867
3868 for (compunit_symtab *cust : objfile->compunits ())
3869 {
3870 const struct block *block;
3871
3872 if (cust == NULL)
3873 continue;
3874 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3875 if (!iterate_over_symbols_terminated (block, name,
3876 domain, callback))
3877 return;
3878 }
3879 }
3880
3881 /* Starting from a search name, return the string that finds the upper
3882 bound of all strings that start with SEARCH_NAME in a sorted name
3883 list. Returns the empty string to indicate that the upper bound is
3884 the end of the list. */
3885
3886 static std::string
3887 make_sort_after_prefix_name (const char *search_name)
3888 {
3889 /* When looking to complete "func", we find the upper bound of all
3890 symbols that start with "func" by looking for where we'd insert
3891 the closest string that would follow "func" in lexicographical
3892 order. Usually, that's "func"-with-last-character-incremented,
3893 i.e. "fund". Mind non-ASCII characters, though. Usually those
3894 will be UTF-8 multi-byte sequences, but we can't be certain.
3895 Especially mind the 0xff character, which is a valid character in
3896 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3897 rule out compilers allowing it in identifiers. Note that
3898 conveniently, strcmp/strcasecmp are specified to compare
3899 characters interpreted as unsigned char. So what we do is treat
3900 the whole string as a base 256 number composed of a sequence of
3901 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3902 to 0, and carries 1 to the following more-significant position.
3903 If the very first character in SEARCH_NAME ends up incremented
3904 and carries/overflows, then the upper bound is the end of the
3905 list. The string after the empty string is also the empty
3906 string.
3907
3908 Some examples of this operation:
3909
3910 SEARCH_NAME => "+1" RESULT
3911
3912 "abc" => "abd"
3913 "ab\xff" => "ac"
3914 "\xff" "a" "\xff" => "\xff" "b"
3915 "\xff" => ""
3916 "\xff\xff" => ""
3917 "" => ""
3918
3919 Then, with these symbols for example:
3920
3921 func
3922 func1
3923 fund
3924
3925 completing "func" looks for symbols between "func" and
3926 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3927 which finds "func" and "func1", but not "fund".
3928
3929 And with:
3930
3931 funcÿ (Latin1 'ÿ' [0xff])
3932 funcÿ1
3933 fund
3934
3935 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3936 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3937
3938 And with:
3939
3940 ÿÿ (Latin1 'ÿ' [0xff])
3941 ÿÿ1
3942
3943 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3944 the end of the list.
3945 */
3946 std::string after = search_name;
3947 while (!after.empty () && (unsigned char) after.back () == 0xff)
3948 after.pop_back ();
3949 if (!after.empty ())
3950 after.back () = (unsigned char) after.back () + 1;
3951 return after;
3952 }
3953
3954 /* See declaration. */
3955
3956 std::pair<std::vector<name_component>::const_iterator,
3957 std::vector<name_component>::const_iterator>
3958 mapped_index_base::find_name_components_bounds
3959 (const lookup_name_info &lookup_name_without_params, language lang,
3960 dwarf2_per_objfile *per_objfile) const
3961 {
3962 auto *name_cmp
3963 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3964
3965 const char *lang_name
3966 = lookup_name_without_params.language_lookup_name (lang);
3967
3968 /* Comparison function object for lower_bound that matches against a
3969 given symbol name. */
3970 auto lookup_compare_lower = [&] (const name_component &elem,
3971 const char *name)
3972 {
3973 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3974 const char *elem_name = elem_qualified + elem.name_offset;
3975 return name_cmp (elem_name, name) < 0;
3976 };
3977
3978 /* Comparison function object for upper_bound that matches against a
3979 given symbol name. */
3980 auto lookup_compare_upper = [&] (const char *name,
3981 const name_component &elem)
3982 {
3983 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3984 const char *elem_name = elem_qualified + elem.name_offset;
3985 return name_cmp (name, elem_name) < 0;
3986 };
3987
3988 auto begin = this->name_components.begin ();
3989 auto end = this->name_components.end ();
3990
3991 /* Find the lower bound. */
3992 auto lower = [&] ()
3993 {
3994 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3995 return begin;
3996 else
3997 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3998 } ();
3999
4000 /* Find the upper bound. */
4001 auto upper = [&] ()
4002 {
4003 if (lookup_name_without_params.completion_mode ())
4004 {
4005 /* In completion mode, we want UPPER to point past all
4006 symbols names that have the same prefix. I.e., with
4007 these symbols, and completing "func":
4008
4009 function << lower bound
4010 function1
4011 other_function << upper bound
4012
4013 We find the upper bound by looking for the insertion
4014 point of "func"-with-last-character-incremented,
4015 i.e. "fund". */
4016 std::string after = make_sort_after_prefix_name (lang_name);
4017 if (after.empty ())
4018 return end;
4019 return std::lower_bound (lower, end, after.c_str (),
4020 lookup_compare_lower);
4021 }
4022 else
4023 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4024 } ();
4025
4026 return {lower, upper};
4027 }
4028
4029 /* See declaration. */
4030
4031 void
4032 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
4033 {
4034 if (!this->name_components.empty ())
4035 return;
4036
4037 this->name_components_casing = case_sensitivity;
4038 auto *name_cmp
4039 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4040
4041 /* The code below only knows how to break apart components of C++
4042 symbol names (and other languages that use '::' as
4043 namespace/module separator) and Ada symbol names. */
4044 auto count = this->symbol_name_count ();
4045 for (offset_type idx = 0; idx < count; idx++)
4046 {
4047 if (this->symbol_name_slot_invalid (idx))
4048 continue;
4049
4050 const char *name = this->symbol_name_at (idx, per_objfile);
4051
4052 /* Add each name component to the name component table. */
4053 unsigned int previous_len = 0;
4054
4055 if (strstr (name, "::") != nullptr)
4056 {
4057 for (unsigned int current_len = cp_find_first_component (name);
4058 name[current_len] != '\0';
4059 current_len += cp_find_first_component (name + current_len))
4060 {
4061 gdb_assert (name[current_len] == ':');
4062 this->name_components.push_back ({previous_len, idx});
4063 /* Skip the '::'. */
4064 current_len += 2;
4065 previous_len = current_len;
4066 }
4067 }
4068 else
4069 {
4070 /* Handle the Ada encoded (aka mangled) form here. */
4071 for (const char *iter = strstr (name, "__");
4072 iter != nullptr;
4073 iter = strstr (iter, "__"))
4074 {
4075 this->name_components.push_back ({previous_len, idx});
4076 iter += 2;
4077 previous_len = iter - name;
4078 }
4079 }
4080
4081 this->name_components.push_back ({previous_len, idx});
4082 }
4083
4084 /* Sort name_components elements by name. */
4085 auto name_comp_compare = [&] (const name_component &left,
4086 const name_component &right)
4087 {
4088 const char *left_qualified
4089 = this->symbol_name_at (left.idx, per_objfile);
4090 const char *right_qualified
4091 = this->symbol_name_at (right.idx, per_objfile);
4092
4093 const char *left_name = left_qualified + left.name_offset;
4094 const char *right_name = right_qualified + right.name_offset;
4095
4096 return name_cmp (left_name, right_name) < 0;
4097 };
4098
4099 std::sort (this->name_components.begin (),
4100 this->name_components.end (),
4101 name_comp_compare);
4102 }
4103
4104 /* Helper for dw2_expand_symtabs_matching that works with a
4105 mapped_index_base instead of the containing objfile. This is split
4106 to a separate function in order to be able to unit test the
4107 name_components matching using a mock mapped_index_base. For each
4108 symbol name that matches, calls MATCH_CALLBACK, passing it the
4109 symbol's index in the mapped_index_base symbol table. */
4110
4111 static void
4112 dw2_expand_symtabs_matching_symbol
4113 (mapped_index_base &index,
4114 const lookup_name_info &lookup_name_in,
4115 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4116 enum search_domain kind,
4117 gdb::function_view<bool (offset_type)> match_callback,
4118 dwarf2_per_objfile *per_objfile)
4119 {
4120 lookup_name_info lookup_name_without_params
4121 = lookup_name_in.make_ignore_params ();
4122
4123 /* Build the symbol name component sorted vector, if we haven't
4124 yet. */
4125 index.build_name_components (per_objfile);
4126
4127 /* The same symbol may appear more than once in the range though.
4128 E.g., if we're looking for symbols that complete "w", and we have
4129 a symbol named "w1::w2", we'll find the two name components for
4130 that same symbol in the range. To be sure we only call the
4131 callback once per symbol, we first collect the symbol name
4132 indexes that matched in a temporary vector and ignore
4133 duplicates. */
4134 std::vector<offset_type> matches;
4135
4136 struct name_and_matcher
4137 {
4138 symbol_name_matcher_ftype *matcher;
4139 const char *name;
4140
4141 bool operator== (const name_and_matcher &other) const
4142 {
4143 return matcher == other.matcher && strcmp (name, other.name) == 0;
4144 }
4145 };
4146
4147 /* A vector holding all the different symbol name matchers, for all
4148 languages. */
4149 std::vector<name_and_matcher> matchers;
4150
4151 for (int i = 0; i < nr_languages; i++)
4152 {
4153 enum language lang_e = (enum language) i;
4154
4155 const language_defn *lang = language_def (lang_e);
4156 symbol_name_matcher_ftype *name_matcher
4157 = lang->get_symbol_name_matcher (lookup_name_without_params);
4158
4159 name_and_matcher key {
4160 name_matcher,
4161 lookup_name_without_params.language_lookup_name (lang_e)
4162 };
4163
4164 /* Don't insert the same comparison routine more than once.
4165 Note that we do this linear walk. This is not a problem in
4166 practice because the number of supported languages is
4167 low. */
4168 if (std::find (matchers.begin (), matchers.end (), key)
4169 != matchers.end ())
4170 continue;
4171 matchers.push_back (std::move (key));
4172
4173 auto bounds
4174 = index.find_name_components_bounds (lookup_name_without_params,
4175 lang_e, per_objfile);
4176
4177 /* Now for each symbol name in range, check to see if we have a name
4178 match, and if so, call the MATCH_CALLBACK callback. */
4179
4180 for (; bounds.first != bounds.second; ++bounds.first)
4181 {
4182 const char *qualified
4183 = index.symbol_name_at (bounds.first->idx, per_objfile);
4184
4185 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4186 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4187 continue;
4188
4189 matches.push_back (bounds.first->idx);
4190 }
4191 }
4192
4193 std::sort (matches.begin (), matches.end ());
4194
4195 /* Finally call the callback, once per match. */
4196 ULONGEST prev = -1;
4197 for (offset_type idx : matches)
4198 {
4199 if (prev != idx)
4200 {
4201 if (!match_callback (idx))
4202 break;
4203 prev = idx;
4204 }
4205 }
4206
4207 /* Above we use a type wider than idx's for 'prev', since 0 and
4208 (offset_type)-1 are both possible values. */
4209 static_assert (sizeof (prev) > sizeof (offset_type), "");
4210 }
4211
4212 #if GDB_SELF_TEST
4213
4214 namespace selftests { namespace dw2_expand_symtabs_matching {
4215
4216 /* A mock .gdb_index/.debug_names-like name index table, enough to
4217 exercise dw2_expand_symtabs_matching_symbol, which works with the
4218 mapped_index_base interface. Builds an index from the symbol list
4219 passed as parameter to the constructor. */
4220 class mock_mapped_index : public mapped_index_base
4221 {
4222 public:
4223 mock_mapped_index (gdb::array_view<const char *> symbols)
4224 : m_symbol_table (symbols)
4225 {}
4226
4227 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4228
4229 /* Return the number of names in the symbol table. */
4230 size_t symbol_name_count () const override
4231 {
4232 return m_symbol_table.size ();
4233 }
4234
4235 /* Get the name of the symbol at IDX in the symbol table. */
4236 const char *symbol_name_at
4237 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
4238 {
4239 return m_symbol_table[idx];
4240 }
4241
4242 private:
4243 gdb::array_view<const char *> m_symbol_table;
4244 };
4245
4246 /* Convenience function that converts a NULL pointer to a "<null>"
4247 string, to pass to print routines. */
4248
4249 static const char *
4250 string_or_null (const char *str)
4251 {
4252 return str != NULL ? str : "<null>";
4253 }
4254
4255 /* Check if a lookup_name_info built from
4256 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4257 index. EXPECTED_LIST is the list of expected matches, in expected
4258 matching order. If no match expected, then an empty list is
4259 specified. Returns true on success. On failure prints a warning
4260 indicating the file:line that failed, and returns false. */
4261
4262 static bool
4263 check_match (const char *file, int line,
4264 mock_mapped_index &mock_index,
4265 const char *name, symbol_name_match_type match_type,
4266 bool completion_mode,
4267 std::initializer_list<const char *> expected_list,
4268 dwarf2_per_objfile *per_objfile)
4269 {
4270 lookup_name_info lookup_name (name, match_type, completion_mode);
4271
4272 bool matched = true;
4273
4274 auto mismatch = [&] (const char *expected_str,
4275 const char *got)
4276 {
4277 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4278 "expected=\"%s\", got=\"%s\"\n"),
4279 file, line,
4280 (match_type == symbol_name_match_type::FULL
4281 ? "FULL" : "WILD"),
4282 name, string_or_null (expected_str), string_or_null (got));
4283 matched = false;
4284 };
4285
4286 auto expected_it = expected_list.begin ();
4287 auto expected_end = expected_list.end ();
4288
4289 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4290 NULL, ALL_DOMAIN,
4291 [&] (offset_type idx)
4292 {
4293 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
4294 const char *expected_str
4295 = expected_it == expected_end ? NULL : *expected_it++;
4296
4297 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4298 mismatch (expected_str, matched_name);
4299 return true;
4300 }, per_objfile);
4301
4302 const char *expected_str
4303 = expected_it == expected_end ? NULL : *expected_it++;
4304 if (expected_str != NULL)
4305 mismatch (expected_str, NULL);
4306
4307 return matched;
4308 }
4309
4310 /* The symbols added to the mock mapped_index for testing (in
4311 canonical form). */
4312 static const char *test_symbols[] = {
4313 "function",
4314 "std::bar",
4315 "std::zfunction",
4316 "std::zfunction2",
4317 "w1::w2",
4318 "ns::foo<char*>",
4319 "ns::foo<int>",
4320 "ns::foo<long>",
4321 "ns2::tmpl<int>::foo2",
4322 "(anonymous namespace)::A::B::C",
4323
4324 /* These are used to check that the increment-last-char in the
4325 matching algorithm for completion doesn't match "t1_fund" when
4326 completing "t1_func". */
4327 "t1_func",
4328 "t1_func1",
4329 "t1_fund",
4330 "t1_fund1",
4331
4332 /* A UTF-8 name with multi-byte sequences to make sure that
4333 cp-name-parser understands this as a single identifier ("função"
4334 is "function" in PT). */
4335 u8"u8função",
4336
4337 /* \377 (0xff) is Latin1 'ÿ'. */
4338 "yfunc\377",
4339
4340 /* \377 (0xff) is Latin1 'ÿ'. */
4341 "\377",
4342 "\377\377123",
4343
4344 /* A name with all sorts of complications. Starts with "z" to make
4345 it easier for the completion tests below. */
4346 #define Z_SYM_NAME \
4347 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4348 "::tuple<(anonymous namespace)::ui*, " \
4349 "std::default_delete<(anonymous namespace)::ui>, void>"
4350
4351 Z_SYM_NAME
4352 };
4353
4354 /* Returns true if the mapped_index_base::find_name_component_bounds
4355 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4356 in completion mode. */
4357
4358 static bool
4359 check_find_bounds_finds (mapped_index_base &index,
4360 const char *search_name,
4361 gdb::array_view<const char *> expected_syms,
4362 dwarf2_per_objfile *per_objfile)
4363 {
4364 lookup_name_info lookup_name (search_name,
4365 symbol_name_match_type::FULL, true);
4366
4367 auto bounds = index.find_name_components_bounds (lookup_name,
4368 language_cplus,
4369 per_objfile);
4370
4371 size_t distance = std::distance (bounds.first, bounds.second);
4372 if (distance != expected_syms.size ())
4373 return false;
4374
4375 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4376 {
4377 auto nc_elem = bounds.first + exp_elem;
4378 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4379 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4380 return false;
4381 }
4382
4383 return true;
4384 }
4385
4386 /* Test the lower-level mapped_index::find_name_component_bounds
4387 method. */
4388
4389 static void
4390 test_mapped_index_find_name_component_bounds ()
4391 {
4392 mock_mapped_index mock_index (test_symbols);
4393
4394 mock_index.build_name_components (NULL /* per_objfile */);
4395
4396 /* Test the lower-level mapped_index::find_name_component_bounds
4397 method in completion mode. */
4398 {
4399 static const char *expected_syms[] = {
4400 "t1_func",
4401 "t1_func1",
4402 };
4403
4404 SELF_CHECK (check_find_bounds_finds
4405 (mock_index, "t1_func", expected_syms,
4406 NULL /* per_objfile */));
4407 }
4408
4409 /* Check that the increment-last-char in the name matching algorithm
4410 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4411 {
4412 static const char *expected_syms1[] = {
4413 "\377",
4414 "\377\377123",
4415 };
4416 SELF_CHECK (check_find_bounds_finds
4417 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4418
4419 static const char *expected_syms2[] = {
4420 "\377\377123",
4421 };
4422 SELF_CHECK (check_find_bounds_finds
4423 (mock_index, "\377\377", expected_syms2,
4424 NULL /* per_objfile */));
4425 }
4426 }
4427
4428 /* Test dw2_expand_symtabs_matching_symbol. */
4429
4430 static void
4431 test_dw2_expand_symtabs_matching_symbol ()
4432 {
4433 mock_mapped_index mock_index (test_symbols);
4434
4435 /* We let all tests run until the end even if some fails, for debug
4436 convenience. */
4437 bool any_mismatch = false;
4438
4439 /* Create the expected symbols list (an initializer_list). Needed
4440 because lists have commas, and we need to pass them to CHECK,
4441 which is a macro. */
4442 #define EXPECT(...) { __VA_ARGS__ }
4443
4444 /* Wrapper for check_match that passes down the current
4445 __FILE__/__LINE__. */
4446 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4447 any_mismatch |= !check_match (__FILE__, __LINE__, \
4448 mock_index, \
4449 NAME, MATCH_TYPE, COMPLETION_MODE, \
4450 EXPECTED_LIST, NULL)
4451
4452 /* Identity checks. */
4453 for (const char *sym : test_symbols)
4454 {
4455 /* Should be able to match all existing symbols. */
4456 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4457 EXPECT (sym));
4458
4459 /* Should be able to match all existing symbols with
4460 parameters. */
4461 std::string with_params = std::string (sym) + "(int)";
4462 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4463 EXPECT (sym));
4464
4465 /* Should be able to match all existing symbols with
4466 parameters and qualifiers. */
4467 with_params = std::string (sym) + " ( int ) const";
4468 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4469 EXPECT (sym));
4470
4471 /* This should really find sym, but cp-name-parser.y doesn't
4472 know about lvalue/rvalue qualifiers yet. */
4473 with_params = std::string (sym) + " ( int ) &&";
4474 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4475 {});
4476 }
4477
4478 /* Check that the name matching algorithm for completion doesn't get
4479 confused with Latin1 'ÿ' / 0xff. */
4480 {
4481 static const char str[] = "\377";
4482 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4483 EXPECT ("\377", "\377\377123"));
4484 }
4485
4486 /* Check that the increment-last-char in the matching algorithm for
4487 completion doesn't match "t1_fund" when completing "t1_func". */
4488 {
4489 static const char str[] = "t1_func";
4490 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4491 EXPECT ("t1_func", "t1_func1"));
4492 }
4493
4494 /* Check that completion mode works at each prefix of the expected
4495 symbol name. */
4496 {
4497 static const char str[] = "function(int)";
4498 size_t len = strlen (str);
4499 std::string lookup;
4500
4501 for (size_t i = 1; i < len; i++)
4502 {
4503 lookup.assign (str, i);
4504 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4505 EXPECT ("function"));
4506 }
4507 }
4508
4509 /* While "w" is a prefix of both components, the match function
4510 should still only be called once. */
4511 {
4512 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4513 EXPECT ("w1::w2"));
4514 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4515 EXPECT ("w1::w2"));
4516 }
4517
4518 /* Same, with a "complicated" symbol. */
4519 {
4520 static const char str[] = Z_SYM_NAME;
4521 size_t len = strlen (str);
4522 std::string lookup;
4523
4524 for (size_t i = 1; i < len; i++)
4525 {
4526 lookup.assign (str, i);
4527 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4528 EXPECT (Z_SYM_NAME));
4529 }
4530 }
4531
4532 /* In FULL mode, an incomplete symbol doesn't match. */
4533 {
4534 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4535 {});
4536 }
4537
4538 /* A complete symbol with parameters matches any overload, since the
4539 index has no overload info. */
4540 {
4541 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4542 EXPECT ("std::zfunction", "std::zfunction2"));
4543 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4544 EXPECT ("std::zfunction", "std::zfunction2"));
4545 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4546 EXPECT ("std::zfunction", "std::zfunction2"));
4547 }
4548
4549 /* Check that whitespace is ignored appropriately. A symbol with a
4550 template argument list. */
4551 {
4552 static const char expected[] = "ns::foo<int>";
4553 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4554 EXPECT (expected));
4555 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4556 EXPECT (expected));
4557 }
4558
4559 /* Check that whitespace is ignored appropriately. A symbol with a
4560 template argument list that includes a pointer. */
4561 {
4562 static const char expected[] = "ns::foo<char*>";
4563 /* Try both completion and non-completion modes. */
4564 static const bool completion_mode[2] = {false, true};
4565 for (size_t i = 0; i < 2; i++)
4566 {
4567 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4568 completion_mode[i], EXPECT (expected));
4569 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4570 completion_mode[i], EXPECT (expected));
4571
4572 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4573 completion_mode[i], EXPECT (expected));
4574 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4575 completion_mode[i], EXPECT (expected));
4576 }
4577 }
4578
4579 {
4580 /* Check method qualifiers are ignored. */
4581 static const char expected[] = "ns::foo<char*>";
4582 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4583 symbol_name_match_type::FULL, true, EXPECT (expected));
4584 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4585 symbol_name_match_type::FULL, true, EXPECT (expected));
4586 CHECK_MATCH ("foo < char * > ( int ) const",
4587 symbol_name_match_type::WILD, true, EXPECT (expected));
4588 CHECK_MATCH ("foo < char * > ( int ) &&",
4589 symbol_name_match_type::WILD, true, EXPECT (expected));
4590 }
4591
4592 /* Test lookup names that don't match anything. */
4593 {
4594 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4595 {});
4596
4597 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4598 {});
4599 }
4600
4601 /* Some wild matching tests, exercising "(anonymous namespace)",
4602 which should not be confused with a parameter list. */
4603 {
4604 static const char *syms[] = {
4605 "A::B::C",
4606 "B::C",
4607 "C",
4608 "A :: B :: C ( int )",
4609 "B :: C ( int )",
4610 "C ( int )",
4611 };
4612
4613 for (const char *s : syms)
4614 {
4615 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4616 EXPECT ("(anonymous namespace)::A::B::C"));
4617 }
4618 }
4619
4620 {
4621 static const char expected[] = "ns2::tmpl<int>::foo2";
4622 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4623 EXPECT (expected));
4624 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4625 EXPECT (expected));
4626 }
4627
4628 SELF_CHECK (!any_mismatch);
4629
4630 #undef EXPECT
4631 #undef CHECK_MATCH
4632 }
4633
4634 static void
4635 run_test ()
4636 {
4637 test_mapped_index_find_name_component_bounds ();
4638 test_dw2_expand_symtabs_matching_symbol ();
4639 }
4640
4641 }} // namespace selftests::dw2_expand_symtabs_matching
4642
4643 #endif /* GDB_SELF_TEST */
4644
4645 /* If FILE_MATCHER is NULL or if PER_CU has
4646 dwarf2_per_cu_quick_data::MARK set (see
4647 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4648 EXPANSION_NOTIFY on it. */
4649
4650 static void
4651 dw2_expand_symtabs_matching_one
4652 (dwarf2_per_cu_data *per_cu,
4653 dwarf2_per_objfile *per_objfile,
4654 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4655 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4656 {
4657 if (file_matcher == NULL || per_cu->v.quick->mark)
4658 {
4659 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4660
4661 compunit_symtab *symtab
4662 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4663 gdb_assert (symtab != nullptr);
4664
4665 if (expansion_notify != NULL && symtab_was_null)
4666 expansion_notify (symtab);
4667 }
4668 }
4669
4670 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4671 matched, to expand corresponding CUs that were marked. IDX is the
4672 index of the symbol name that matched. */
4673
4674 static void
4675 dw2_expand_marked_cus
4676 (dwarf2_per_objfile *per_objfile, offset_type idx,
4677 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4678 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4679 search_domain kind)
4680 {
4681 offset_type *vec, vec_len, vec_idx;
4682 bool global_seen = false;
4683 mapped_index &index = *per_objfile->per_bfd->index_table;
4684
4685 vec = (offset_type *) (index.constant_pool
4686 + MAYBE_SWAP (index.symbol_table[idx].vec));
4687 vec_len = MAYBE_SWAP (vec[0]);
4688 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4689 {
4690 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4691 /* This value is only valid for index versions >= 7. */
4692 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4693 gdb_index_symbol_kind symbol_kind =
4694 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4695 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4696 /* Only check the symbol attributes if they're present.
4697 Indices prior to version 7 don't record them,
4698 and indices >= 7 may elide them for certain symbols
4699 (gold does this). */
4700 int attrs_valid =
4701 (index.version >= 7
4702 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4703
4704 /* Work around gold/15646. */
4705 if (attrs_valid
4706 && !is_static
4707 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4708 {
4709 if (global_seen)
4710 continue;
4711
4712 global_seen = true;
4713 }
4714
4715 /* Only check the symbol's kind if it has one. */
4716 if (attrs_valid)
4717 {
4718 switch (kind)
4719 {
4720 case VARIABLES_DOMAIN:
4721 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4722 continue;
4723 break;
4724 case FUNCTIONS_DOMAIN:
4725 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4726 continue;
4727 break;
4728 case TYPES_DOMAIN:
4729 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4730 continue;
4731 break;
4732 case MODULES_DOMAIN:
4733 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4734 continue;
4735 break;
4736 default:
4737 break;
4738 }
4739 }
4740
4741 /* Don't crash on bad data. */
4742 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4743 + per_objfile->per_bfd->all_type_units.size ()))
4744 {
4745 complaint (_(".gdb_index entry has bad CU index"
4746 " [in module %s]"), objfile_name (per_objfile->objfile));
4747 continue;
4748 }
4749
4750 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4751 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4752 expansion_notify);
4753 }
4754 }
4755
4756 /* If FILE_MATCHER is non-NULL, set all the
4757 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4758 that match FILE_MATCHER. */
4759
4760 static void
4761 dw_expand_symtabs_matching_file_matcher
4762 (dwarf2_per_objfile *per_objfile,
4763 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4764 {
4765 if (file_matcher == NULL)
4766 return;
4767
4768 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4769 htab_eq_pointer,
4770 NULL, xcalloc, xfree));
4771 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4772 htab_eq_pointer,
4773 NULL, xcalloc, xfree));
4774
4775 /* The rule is CUs specify all the files, including those used by
4776 any TU, so there's no need to scan TUs here. */
4777
4778 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4779 {
4780 QUIT;
4781
4782 per_cu->v.quick->mark = 0;
4783
4784 /* We only need to look at symtabs not already expanded. */
4785 if (per_objfile->symtab_set_p (per_cu))
4786 continue;
4787
4788 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4789 if (file_data == NULL)
4790 continue;
4791
4792 if (htab_find (visited_not_found.get (), file_data) != NULL)
4793 continue;
4794 else if (htab_find (visited_found.get (), file_data) != NULL)
4795 {
4796 per_cu->v.quick->mark = 1;
4797 continue;
4798 }
4799
4800 for (int j = 0; j < file_data->num_file_names; ++j)
4801 {
4802 const char *this_real_name;
4803
4804 if (file_matcher (file_data->file_names[j], false))
4805 {
4806 per_cu->v.quick->mark = 1;
4807 break;
4808 }
4809
4810 /* Before we invoke realpath, which can get expensive when many
4811 files are involved, do a quick comparison of the basenames. */
4812 if (!basenames_may_differ
4813 && !file_matcher (lbasename (file_data->file_names[j]),
4814 true))
4815 continue;
4816
4817 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4818 if (file_matcher (this_real_name, false))
4819 {
4820 per_cu->v.quick->mark = 1;
4821 break;
4822 }
4823 }
4824
4825 void **slot = htab_find_slot (per_cu->v.quick->mark
4826 ? visited_found.get ()
4827 : visited_not_found.get (),
4828 file_data, INSERT);
4829 *slot = file_data;
4830 }
4831 }
4832
4833 static void
4834 dw2_expand_symtabs_matching
4835 (struct objfile *objfile,
4836 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4837 const lookup_name_info *lookup_name,
4838 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4839 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4840 enum search_domain kind)
4841 {
4842 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4843
4844 /* index_table is NULL if OBJF_READNOW. */
4845 if (!per_objfile->per_bfd->index_table)
4846 return;
4847
4848 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4849
4850 if (symbol_matcher == NULL && lookup_name == NULL)
4851 {
4852 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4853 {
4854 QUIT;
4855
4856 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4857 file_matcher, expansion_notify);
4858 }
4859 return;
4860 }
4861
4862 mapped_index &index = *per_objfile->per_bfd->index_table;
4863
4864 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4865 symbol_matcher,
4866 kind, [&] (offset_type idx)
4867 {
4868 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4869 kind);
4870 return true;
4871 }, per_objfile);
4872 }
4873
4874 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4875 symtab. */
4876
4877 static struct compunit_symtab *
4878 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4879 CORE_ADDR pc)
4880 {
4881 int i;
4882
4883 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4884 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4885 return cust;
4886
4887 if (cust->includes == NULL)
4888 return NULL;
4889
4890 for (i = 0; cust->includes[i]; ++i)
4891 {
4892 struct compunit_symtab *s = cust->includes[i];
4893
4894 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4895 if (s != NULL)
4896 return s;
4897 }
4898
4899 return NULL;
4900 }
4901
4902 static struct compunit_symtab *
4903 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4904 struct bound_minimal_symbol msymbol,
4905 CORE_ADDR pc,
4906 struct obj_section *section,
4907 int warn_if_readin)
4908 {
4909 struct dwarf2_per_cu_data *data;
4910 struct compunit_symtab *result;
4911
4912 if (!objfile->partial_symtabs->psymtabs_addrmap)
4913 return NULL;
4914
4915 CORE_ADDR baseaddr = objfile->text_section_offset ();
4916 data = (struct dwarf2_per_cu_data *) addrmap_find
4917 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4918 if (!data)
4919 return NULL;
4920
4921 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4922 if (warn_if_readin && per_objfile->symtab_set_p (data))
4923 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4924 paddress (objfile->arch (), pc));
4925
4926 result = recursively_find_pc_sect_compunit_symtab
4927 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4928
4929 gdb_assert (result != NULL);
4930 return result;
4931 }
4932
4933 static void
4934 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4935 void *data, int need_fullname)
4936 {
4937 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4938
4939 if (!per_objfile->per_bfd->filenames_cache)
4940 {
4941 per_objfile->per_bfd->filenames_cache.emplace ();
4942
4943 htab_up visited (htab_create_alloc (10,
4944 htab_hash_pointer, htab_eq_pointer,
4945 NULL, xcalloc, xfree));
4946
4947 /* The rule is CUs specify all the files, including those used
4948 by any TU, so there's no need to scan TUs here. We can
4949 ignore file names coming from already-expanded CUs. */
4950
4951 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4952 {
4953 if (per_objfile->symtab_set_p (per_cu))
4954 {
4955 void **slot = htab_find_slot (visited.get (),
4956 per_cu->v.quick->file_names,
4957 INSERT);
4958
4959 *slot = per_cu->v.quick->file_names;
4960 }
4961 }
4962
4963 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4964 {
4965 /* We only need to look at symtabs not already expanded. */
4966 if (per_objfile->symtab_set_p (per_cu))
4967 continue;
4968
4969 quick_file_names *file_data
4970 = dw2_get_file_names (per_cu, per_objfile);
4971 if (file_data == NULL)
4972 continue;
4973
4974 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4975 if (*slot)
4976 {
4977 /* Already visited. */
4978 continue;
4979 }
4980 *slot = file_data;
4981
4982 for (int j = 0; j < file_data->num_file_names; ++j)
4983 {
4984 const char *filename = file_data->file_names[j];
4985 per_objfile->per_bfd->filenames_cache->seen (filename);
4986 }
4987 }
4988 }
4989
4990 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4991 {
4992 gdb::unique_xmalloc_ptr<char> this_real_name;
4993
4994 if (need_fullname)
4995 this_real_name = gdb_realpath (filename);
4996 (*fun) (filename, this_real_name.get (), data);
4997 });
4998 }
4999
5000 static int
5001 dw2_has_symbols (struct objfile *objfile)
5002 {
5003 return 1;
5004 }
5005
5006 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5007 {
5008 dw2_has_symbols,
5009 dw2_find_last_source_symtab,
5010 dw2_forget_cached_source_info,
5011 dw2_map_symtabs_matching_filename,
5012 dw2_lookup_symbol,
5013 NULL,
5014 dw2_print_stats,
5015 dw2_dump,
5016 dw2_expand_symtabs_for_function,
5017 dw2_expand_all_symtabs,
5018 dw2_expand_symtabs_with_fullname,
5019 dw2_map_matching_symbols,
5020 dw2_expand_symtabs_matching,
5021 dw2_find_pc_sect_compunit_symtab,
5022 NULL,
5023 dw2_map_symbol_filenames
5024 };
5025
5026 /* DWARF-5 debug_names reader. */
5027
5028 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5029 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5030
5031 /* A helper function that reads the .debug_names section in SECTION
5032 and fills in MAP. FILENAME is the name of the file containing the
5033 section; it is used for error reporting.
5034
5035 Returns true if all went well, false otherwise. */
5036
5037 static bool
5038 read_debug_names_from_section (struct objfile *objfile,
5039 const char *filename,
5040 struct dwarf2_section_info *section,
5041 mapped_debug_names &map)
5042 {
5043 if (section->empty ())
5044 return false;
5045
5046 /* Older elfutils strip versions could keep the section in the main
5047 executable while splitting it for the separate debug info file. */
5048 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5049 return false;
5050
5051 section->read (objfile);
5052
5053 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
5054
5055 const gdb_byte *addr = section->buffer;
5056
5057 bfd *const abfd = section->get_bfd_owner ();
5058
5059 unsigned int bytes_read;
5060 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5061 addr += bytes_read;
5062
5063 map.dwarf5_is_dwarf64 = bytes_read != 4;
5064 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5065 if (bytes_read + length != section->size)
5066 {
5067 /* There may be multiple per-CU indices. */
5068 warning (_("Section .debug_names in %s length %s does not match "
5069 "section length %s, ignoring .debug_names."),
5070 filename, plongest (bytes_read + length),
5071 pulongest (section->size));
5072 return false;
5073 }
5074
5075 /* The version number. */
5076 uint16_t version = read_2_bytes (abfd, addr);
5077 addr += 2;
5078 if (version != 5)
5079 {
5080 warning (_("Section .debug_names in %s has unsupported version %d, "
5081 "ignoring .debug_names."),
5082 filename, version);
5083 return false;
5084 }
5085
5086 /* Padding. */
5087 uint16_t padding = read_2_bytes (abfd, addr);
5088 addr += 2;
5089 if (padding != 0)
5090 {
5091 warning (_("Section .debug_names in %s has unsupported padding %d, "
5092 "ignoring .debug_names."),
5093 filename, padding);
5094 return false;
5095 }
5096
5097 /* comp_unit_count - The number of CUs in the CU list. */
5098 map.cu_count = read_4_bytes (abfd, addr);
5099 addr += 4;
5100
5101 /* local_type_unit_count - The number of TUs in the local TU
5102 list. */
5103 map.tu_count = read_4_bytes (abfd, addr);
5104 addr += 4;
5105
5106 /* foreign_type_unit_count - The number of TUs in the foreign TU
5107 list. */
5108 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5109 addr += 4;
5110 if (foreign_tu_count != 0)
5111 {
5112 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5113 "ignoring .debug_names."),
5114 filename, static_cast<unsigned long> (foreign_tu_count));
5115 return false;
5116 }
5117
5118 /* bucket_count - The number of hash buckets in the hash lookup
5119 table. */
5120 map.bucket_count = read_4_bytes (abfd, addr);
5121 addr += 4;
5122
5123 /* name_count - The number of unique names in the index. */
5124 map.name_count = read_4_bytes (abfd, addr);
5125 addr += 4;
5126
5127 /* abbrev_table_size - The size in bytes of the abbreviations
5128 table. */
5129 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5130 addr += 4;
5131
5132 /* augmentation_string_size - The size in bytes of the augmentation
5133 string. This value is rounded up to a multiple of 4. */
5134 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5135 addr += 4;
5136 map.augmentation_is_gdb = ((augmentation_string_size
5137 == sizeof (dwarf5_augmentation))
5138 && memcmp (addr, dwarf5_augmentation,
5139 sizeof (dwarf5_augmentation)) == 0);
5140 augmentation_string_size += (-augmentation_string_size) & 3;
5141 addr += augmentation_string_size;
5142
5143 /* List of CUs */
5144 map.cu_table_reordered = addr;
5145 addr += map.cu_count * map.offset_size;
5146
5147 /* List of Local TUs */
5148 map.tu_table_reordered = addr;
5149 addr += map.tu_count * map.offset_size;
5150
5151 /* Hash Lookup Table */
5152 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5153 addr += map.bucket_count * 4;
5154 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5155 addr += map.name_count * 4;
5156
5157 /* Name Table */
5158 map.name_table_string_offs_reordered = addr;
5159 addr += map.name_count * map.offset_size;
5160 map.name_table_entry_offs_reordered = addr;
5161 addr += map.name_count * map.offset_size;
5162
5163 const gdb_byte *abbrev_table_start = addr;
5164 for (;;)
5165 {
5166 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5167 addr += bytes_read;
5168 if (index_num == 0)
5169 break;
5170
5171 const auto insertpair
5172 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5173 if (!insertpair.second)
5174 {
5175 warning (_("Section .debug_names in %s has duplicate index %s, "
5176 "ignoring .debug_names."),
5177 filename, pulongest (index_num));
5178 return false;
5179 }
5180 mapped_debug_names::index_val &indexval = insertpair.first->second;
5181 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5182 addr += bytes_read;
5183
5184 for (;;)
5185 {
5186 mapped_debug_names::index_val::attr attr;
5187 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5188 addr += bytes_read;
5189 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5190 addr += bytes_read;
5191 if (attr.form == DW_FORM_implicit_const)
5192 {
5193 attr.implicit_const = read_signed_leb128 (abfd, addr,
5194 &bytes_read);
5195 addr += bytes_read;
5196 }
5197 if (attr.dw_idx == 0 && attr.form == 0)
5198 break;
5199 indexval.attr_vec.push_back (std::move (attr));
5200 }
5201 }
5202 if (addr != abbrev_table_start + abbrev_table_size)
5203 {
5204 warning (_("Section .debug_names in %s has abbreviation_table "
5205 "of size %s vs. written as %u, ignoring .debug_names."),
5206 filename, plongest (addr - abbrev_table_start),
5207 abbrev_table_size);
5208 return false;
5209 }
5210 map.entry_pool = addr;
5211
5212 return true;
5213 }
5214
5215 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5216 list. */
5217
5218 static void
5219 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
5220 const mapped_debug_names &map,
5221 dwarf2_section_info &section,
5222 bool is_dwz)
5223 {
5224 if (!map.augmentation_is_gdb)
5225 {
5226 for (uint32_t i = 0; i < map.cu_count; ++i)
5227 {
5228 sect_offset sect_off
5229 = (sect_offset) (extract_unsigned_integer
5230 (map.cu_table_reordered + i * map.offset_size,
5231 map.offset_size,
5232 map.dwarf5_byte_order));
5233 /* We don't know the length of the CU, because the CU list in a
5234 .debug_names index can be incomplete, so we can't use the start of
5235 the next CU as end of this CU. We create the CUs here with length 0,
5236 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5237 dwarf2_per_cu_data *per_cu
5238 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5239 per_bfd->all_comp_units.push_back (per_cu);
5240 }
5241 }
5242
5243 sect_offset sect_off_prev;
5244 for (uint32_t i = 0; i <= map.cu_count; ++i)
5245 {
5246 sect_offset sect_off_next;
5247 if (i < map.cu_count)
5248 {
5249 sect_off_next
5250 = (sect_offset) (extract_unsigned_integer
5251 (map.cu_table_reordered + i * map.offset_size,
5252 map.offset_size,
5253 map.dwarf5_byte_order));
5254 }
5255 else
5256 sect_off_next = (sect_offset) section.size;
5257 if (i >= 1)
5258 {
5259 const ULONGEST length = sect_off_next - sect_off_prev;
5260 dwarf2_per_cu_data *per_cu
5261 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5262 sect_off_prev, length);
5263 per_bfd->all_comp_units.push_back (per_cu);
5264 }
5265 sect_off_prev = sect_off_next;
5266 }
5267 }
5268
5269 /* Read the CU list from the mapped index, and use it to create all
5270 the CU objects for this dwarf2_per_objfile. */
5271
5272 static void
5273 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5274 const mapped_debug_names &map,
5275 const mapped_debug_names &dwz_map)
5276 {
5277 gdb_assert (per_bfd->all_comp_units.empty ());
5278 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5279
5280 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5281 false /* is_dwz */);
5282
5283 if (dwz_map.cu_count == 0)
5284 return;
5285
5286 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5287 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5288 true /* is_dwz */);
5289 }
5290
5291 /* Read .debug_names. If everything went ok, initialize the "quick"
5292 elements of all the CUs and return true. Otherwise, return false. */
5293
5294 static bool
5295 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
5296 {
5297 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5298 mapped_debug_names dwz_map;
5299 struct objfile *objfile = per_objfile->objfile;
5300 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5301
5302 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5303 &per_objfile->per_bfd->debug_names, *map))
5304 return false;
5305
5306 /* Don't use the index if it's empty. */
5307 if (map->name_count == 0)
5308 return false;
5309
5310 /* If there is a .dwz file, read it so we can get its CU list as
5311 well. */
5312 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5313 if (dwz != NULL)
5314 {
5315 if (!read_debug_names_from_section (objfile,
5316 bfd_get_filename (dwz->dwz_bfd.get ()),
5317 &dwz->debug_names, dwz_map))
5318 {
5319 warning (_("could not read '.debug_names' section from %s; skipping"),
5320 bfd_get_filename (dwz->dwz_bfd.get ()));
5321 return false;
5322 }
5323 }
5324
5325 create_cus_from_debug_names (per_bfd, *map, dwz_map);
5326
5327 if (map->tu_count != 0)
5328 {
5329 /* We can only handle a single .debug_types when we have an
5330 index. */
5331 if (per_bfd->types.size () != 1)
5332 return false;
5333
5334 dwarf2_section_info *section = &per_bfd->types[0];
5335
5336 create_signatured_type_table_from_debug_names
5337 (per_objfile, *map, section, &per_bfd->abbrev);
5338 }
5339
5340 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
5341
5342 per_bfd->debug_names_table = std::move (map);
5343 per_bfd->using_index = 1;
5344 per_bfd->quick_file_names_table =
5345 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
5346
5347 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5348 objfiles using the same BFD. */
5349 gdb_assert (per_bfd->partial_symtabs == nullptr);
5350 per_bfd->partial_symtabs = objfile->partial_symtabs;
5351
5352 return true;
5353 }
5354
5355 /* Type used to manage iterating over all CUs looking for a symbol for
5356 .debug_names. */
5357
5358 class dw2_debug_names_iterator
5359 {
5360 public:
5361 dw2_debug_names_iterator (const mapped_debug_names &map,
5362 gdb::optional<block_enum> block_index,
5363 domain_enum domain,
5364 const char *name, dwarf2_per_objfile *per_objfile)
5365 : m_map (map), m_block_index (block_index), m_domain (domain),
5366 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5367 m_per_objfile (per_objfile)
5368 {}
5369
5370 dw2_debug_names_iterator (const mapped_debug_names &map,
5371 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
5372 : m_map (map),
5373 m_search (search),
5374 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5375 m_per_objfile (per_objfile)
5376 {}
5377
5378 dw2_debug_names_iterator (const mapped_debug_names &map,
5379 block_enum block_index, domain_enum domain,
5380 uint32_t namei, dwarf2_per_objfile *per_objfile)
5381 : m_map (map), m_block_index (block_index), m_domain (domain),
5382 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5383 m_per_objfile (per_objfile)
5384 {}
5385
5386 /* Return the next matching CU or NULL if there are no more. */
5387 dwarf2_per_cu_data *next ();
5388
5389 private:
5390 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5391 const char *name,
5392 dwarf2_per_objfile *per_objfile);
5393 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5394 uint32_t namei,
5395 dwarf2_per_objfile *per_objfile);
5396
5397 /* The internalized form of .debug_names. */
5398 const mapped_debug_names &m_map;
5399
5400 /* If set, only look for symbols that match that block. Valid values are
5401 GLOBAL_BLOCK and STATIC_BLOCK. */
5402 const gdb::optional<block_enum> m_block_index;
5403
5404 /* The kind of symbol we're looking for. */
5405 const domain_enum m_domain = UNDEF_DOMAIN;
5406 const search_domain m_search = ALL_DOMAIN;
5407
5408 /* The list of CUs from the index entry of the symbol, or NULL if
5409 not found. */
5410 const gdb_byte *m_addr;
5411
5412 dwarf2_per_objfile *m_per_objfile;
5413 };
5414
5415 const char *
5416 mapped_debug_names::namei_to_name
5417 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5418 {
5419 const ULONGEST namei_string_offs
5420 = extract_unsigned_integer ((name_table_string_offs_reordered
5421 + namei * offset_size),
5422 offset_size,
5423 dwarf5_byte_order);
5424 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5425 }
5426
5427 /* Find a slot in .debug_names for the object named NAME. If NAME is
5428 found, return pointer to its pool data. If NAME cannot be found,
5429 return NULL. */
5430
5431 const gdb_byte *
5432 dw2_debug_names_iterator::find_vec_in_debug_names
5433 (const mapped_debug_names &map, const char *name,
5434 dwarf2_per_objfile *per_objfile)
5435 {
5436 int (*cmp) (const char *, const char *);
5437
5438 gdb::unique_xmalloc_ptr<char> without_params;
5439 if (current_language->la_language == language_cplus
5440 || current_language->la_language == language_fortran
5441 || current_language->la_language == language_d)
5442 {
5443 /* NAME is already canonical. Drop any qualifiers as
5444 .debug_names does not contain any. */
5445
5446 if (strchr (name, '(') != NULL)
5447 {
5448 without_params = cp_remove_params (name);
5449 if (without_params != NULL)
5450 name = without_params.get ();
5451 }
5452 }
5453
5454 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5455
5456 const uint32_t full_hash = dwarf5_djb_hash (name);
5457 uint32_t namei
5458 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5459 (map.bucket_table_reordered
5460 + (full_hash % map.bucket_count)), 4,
5461 map.dwarf5_byte_order);
5462 if (namei == 0)
5463 return NULL;
5464 --namei;
5465 if (namei >= map.name_count)
5466 {
5467 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5468 "[in module %s]"),
5469 namei, map.name_count,
5470 objfile_name (per_objfile->objfile));
5471 return NULL;
5472 }
5473
5474 for (;;)
5475 {
5476 const uint32_t namei_full_hash
5477 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5478 (map.hash_table_reordered + namei), 4,
5479 map.dwarf5_byte_order);
5480 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5481 return NULL;
5482
5483 if (full_hash == namei_full_hash)
5484 {
5485 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5486
5487 #if 0 /* An expensive sanity check. */
5488 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5489 {
5490 complaint (_("Wrong .debug_names hash for string at index %u "
5491 "[in module %s]"),
5492 namei, objfile_name (dwarf2_per_objfile->objfile));
5493 return NULL;
5494 }
5495 #endif
5496
5497 if (cmp (namei_string, name) == 0)
5498 {
5499 const ULONGEST namei_entry_offs
5500 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5501 + namei * map.offset_size),
5502 map.offset_size, map.dwarf5_byte_order);
5503 return map.entry_pool + namei_entry_offs;
5504 }
5505 }
5506
5507 ++namei;
5508 if (namei >= map.name_count)
5509 return NULL;
5510 }
5511 }
5512
5513 const gdb_byte *
5514 dw2_debug_names_iterator::find_vec_in_debug_names
5515 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5516 {
5517 if (namei >= map.name_count)
5518 {
5519 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5520 "[in module %s]"),
5521 namei, map.name_count,
5522 objfile_name (per_objfile->objfile));
5523 return NULL;
5524 }
5525
5526 const ULONGEST namei_entry_offs
5527 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5528 + namei * map.offset_size),
5529 map.offset_size, map.dwarf5_byte_order);
5530 return map.entry_pool + namei_entry_offs;
5531 }
5532
5533 /* See dw2_debug_names_iterator. */
5534
5535 dwarf2_per_cu_data *
5536 dw2_debug_names_iterator::next ()
5537 {
5538 if (m_addr == NULL)
5539 return NULL;
5540
5541 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5542 struct objfile *objfile = m_per_objfile->objfile;
5543 bfd *const abfd = objfile->obfd;
5544
5545 again:
5546
5547 unsigned int bytes_read;
5548 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5549 m_addr += bytes_read;
5550 if (abbrev == 0)
5551 return NULL;
5552
5553 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5554 if (indexval_it == m_map.abbrev_map.cend ())
5555 {
5556 complaint (_("Wrong .debug_names undefined abbrev code %s "
5557 "[in module %s]"),
5558 pulongest (abbrev), objfile_name (objfile));
5559 return NULL;
5560 }
5561 const mapped_debug_names::index_val &indexval = indexval_it->second;
5562 enum class symbol_linkage {
5563 unknown,
5564 static_,
5565 extern_,
5566 } symbol_linkage_ = symbol_linkage::unknown;
5567 dwarf2_per_cu_data *per_cu = NULL;
5568 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5569 {
5570 ULONGEST ull;
5571 switch (attr.form)
5572 {
5573 case DW_FORM_implicit_const:
5574 ull = attr.implicit_const;
5575 break;
5576 case DW_FORM_flag_present:
5577 ull = 1;
5578 break;
5579 case DW_FORM_udata:
5580 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5581 m_addr += bytes_read;
5582 break;
5583 case DW_FORM_ref4:
5584 ull = read_4_bytes (abfd, m_addr);
5585 m_addr += 4;
5586 break;
5587 case DW_FORM_ref8:
5588 ull = read_8_bytes (abfd, m_addr);
5589 m_addr += 8;
5590 break;
5591 case DW_FORM_ref_sig8:
5592 ull = read_8_bytes (abfd, m_addr);
5593 m_addr += 8;
5594 break;
5595 default:
5596 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5597 dwarf_form_name (attr.form),
5598 objfile_name (objfile));
5599 return NULL;
5600 }
5601 switch (attr.dw_idx)
5602 {
5603 case DW_IDX_compile_unit:
5604 /* Don't crash on bad data. */
5605 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
5606 {
5607 complaint (_(".debug_names entry has bad CU index %s"
5608 " [in module %s]"),
5609 pulongest (ull),
5610 objfile_name (objfile));
5611 continue;
5612 }
5613 per_cu = per_bfd->get_cutu (ull);
5614 break;
5615 case DW_IDX_type_unit:
5616 /* Don't crash on bad data. */
5617 if (ull >= per_bfd->all_type_units.size ())
5618 {
5619 complaint (_(".debug_names entry has bad TU index %s"
5620 " [in module %s]"),
5621 pulongest (ull),
5622 objfile_name (objfile));
5623 continue;
5624 }
5625 per_cu = &per_bfd->get_tu (ull)->per_cu;
5626 break;
5627 case DW_IDX_die_offset:
5628 /* In a per-CU index (as opposed to a per-module index), index
5629 entries without CU attribute implicitly refer to the single CU. */
5630 if (per_cu == NULL)
5631 per_cu = per_bfd->get_cu (0);
5632 break;
5633 case DW_IDX_GNU_internal:
5634 if (!m_map.augmentation_is_gdb)
5635 break;
5636 symbol_linkage_ = symbol_linkage::static_;
5637 break;
5638 case DW_IDX_GNU_external:
5639 if (!m_map.augmentation_is_gdb)
5640 break;
5641 symbol_linkage_ = symbol_linkage::extern_;
5642 break;
5643 }
5644 }
5645
5646 /* Skip if already read in. */
5647 if (m_per_objfile->symtab_set_p (per_cu))
5648 goto again;
5649
5650 /* Check static vs global. */
5651 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5652 {
5653 const bool want_static = *m_block_index == STATIC_BLOCK;
5654 const bool symbol_is_static =
5655 symbol_linkage_ == symbol_linkage::static_;
5656 if (want_static != symbol_is_static)
5657 goto again;
5658 }
5659
5660 /* Match dw2_symtab_iter_next, symbol_kind
5661 and debug_names::psymbol_tag. */
5662 switch (m_domain)
5663 {
5664 case VAR_DOMAIN:
5665 switch (indexval.dwarf_tag)
5666 {
5667 case DW_TAG_variable:
5668 case DW_TAG_subprogram:
5669 /* Some types are also in VAR_DOMAIN. */
5670 case DW_TAG_typedef:
5671 case DW_TAG_structure_type:
5672 break;
5673 default:
5674 goto again;
5675 }
5676 break;
5677 case STRUCT_DOMAIN:
5678 switch (indexval.dwarf_tag)
5679 {
5680 case DW_TAG_typedef:
5681 case DW_TAG_structure_type:
5682 break;
5683 default:
5684 goto again;
5685 }
5686 break;
5687 case LABEL_DOMAIN:
5688 switch (indexval.dwarf_tag)
5689 {
5690 case 0:
5691 case DW_TAG_variable:
5692 break;
5693 default:
5694 goto again;
5695 }
5696 break;
5697 case MODULE_DOMAIN:
5698 switch (indexval.dwarf_tag)
5699 {
5700 case DW_TAG_module:
5701 break;
5702 default:
5703 goto again;
5704 }
5705 break;
5706 default:
5707 break;
5708 }
5709
5710 /* Match dw2_expand_symtabs_matching, symbol_kind and
5711 debug_names::psymbol_tag. */
5712 switch (m_search)
5713 {
5714 case VARIABLES_DOMAIN:
5715 switch (indexval.dwarf_tag)
5716 {
5717 case DW_TAG_variable:
5718 break;
5719 default:
5720 goto again;
5721 }
5722 break;
5723 case FUNCTIONS_DOMAIN:
5724 switch (indexval.dwarf_tag)
5725 {
5726 case DW_TAG_subprogram:
5727 break;
5728 default:
5729 goto again;
5730 }
5731 break;
5732 case TYPES_DOMAIN:
5733 switch (indexval.dwarf_tag)
5734 {
5735 case DW_TAG_typedef:
5736 case DW_TAG_structure_type:
5737 break;
5738 default:
5739 goto again;
5740 }
5741 break;
5742 case MODULES_DOMAIN:
5743 switch (indexval.dwarf_tag)
5744 {
5745 case DW_TAG_module:
5746 break;
5747 default:
5748 goto again;
5749 }
5750 default:
5751 break;
5752 }
5753
5754 return per_cu;
5755 }
5756
5757 static struct compunit_symtab *
5758 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5759 const char *name, domain_enum domain)
5760 {
5761 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5762
5763 const auto &mapp = per_objfile->per_bfd->debug_names_table;
5764 if (!mapp)
5765 {
5766 /* index is NULL if OBJF_READNOW. */
5767 return NULL;
5768 }
5769 const auto &map = *mapp;
5770
5771 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
5772
5773 struct compunit_symtab *stab_best = NULL;
5774 struct dwarf2_per_cu_data *per_cu;
5775 while ((per_cu = iter.next ()) != NULL)
5776 {
5777 struct symbol *sym, *with_opaque = NULL;
5778 compunit_symtab *stab
5779 = dw2_instantiate_symtab (per_cu, per_objfile, false);
5780 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5781 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5782
5783 sym = block_find_symbol (block, name, domain,
5784 block_find_non_opaque_type_preferred,
5785 &with_opaque);
5786
5787 /* Some caution must be observed with overloaded functions and
5788 methods, since the index will not contain any overload
5789 information (but NAME might contain it). */
5790
5791 if (sym != NULL
5792 && strcmp_iw (sym->search_name (), name) == 0)
5793 return stab;
5794 if (with_opaque != NULL
5795 && strcmp_iw (with_opaque->search_name (), name) == 0)
5796 stab_best = stab;
5797
5798 /* Keep looking through other CUs. */
5799 }
5800
5801 return stab_best;
5802 }
5803
5804 /* This dumps minimal information about .debug_names. It is called
5805 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5806 uses this to verify that .debug_names has been loaded. */
5807
5808 static void
5809 dw2_debug_names_dump (struct objfile *objfile)
5810 {
5811 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5812
5813 gdb_assert (per_objfile->per_bfd->using_index);
5814 printf_filtered (".debug_names:");
5815 if (per_objfile->per_bfd->debug_names_table)
5816 printf_filtered (" exists\n");
5817 else
5818 printf_filtered (" faked for \"readnow\"\n");
5819 printf_filtered ("\n");
5820 }
5821
5822 static void
5823 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5824 const char *func_name)
5825 {
5826 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5827
5828 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5829 if (per_objfile->per_bfd->debug_names_table)
5830 {
5831 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5832
5833 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
5834 per_objfile);
5835
5836 struct dwarf2_per_cu_data *per_cu;
5837 while ((per_cu = iter.next ()) != NULL)
5838 dw2_instantiate_symtab (per_cu, per_objfile, false);
5839 }
5840 }
5841
5842 static void
5843 dw2_debug_names_map_matching_symbols
5844 (struct objfile *objfile,
5845 const lookup_name_info &name, domain_enum domain,
5846 int global,
5847 gdb::function_view<symbol_found_callback_ftype> callback,
5848 symbol_compare_ftype *ordered_compare)
5849 {
5850 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5851
5852 /* debug_names_table is NULL if OBJF_READNOW. */
5853 if (!per_objfile->per_bfd->debug_names_table)
5854 return;
5855
5856 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5857 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5858
5859 const char *match_name = name.ada ().lookup_name ().c_str ();
5860 auto matcher = [&] (const char *symname)
5861 {
5862 if (ordered_compare == nullptr)
5863 return true;
5864 return ordered_compare (symname, match_name) == 0;
5865 };
5866
5867 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5868 [&] (offset_type namei)
5869 {
5870 /* The name was matched, now expand corresponding CUs that were
5871 marked. */
5872 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
5873 per_objfile);
5874
5875 struct dwarf2_per_cu_data *per_cu;
5876 while ((per_cu = iter.next ()) != NULL)
5877 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5878 nullptr);
5879 return true;
5880 }, per_objfile);
5881
5882 /* It's a shame we couldn't do this inside the
5883 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5884 that have already been expanded. Instead, this loop matches what
5885 the psymtab code does. */
5886 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5887 {
5888 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
5889 if (symtab != nullptr)
5890 {
5891 const struct block *block
5892 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5893 if (!iterate_over_symbols_terminated (block, name,
5894 domain, callback))
5895 break;
5896 }
5897 }
5898 }
5899
5900 static void
5901 dw2_debug_names_expand_symtabs_matching
5902 (struct objfile *objfile,
5903 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5904 const lookup_name_info *lookup_name,
5905 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5906 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5907 enum search_domain kind)
5908 {
5909 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5910
5911 /* debug_names_table is NULL if OBJF_READNOW. */
5912 if (!per_objfile->per_bfd->debug_names_table)
5913 return;
5914
5915 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5916
5917 if (symbol_matcher == NULL && lookup_name == NULL)
5918 {
5919 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5920 {
5921 QUIT;
5922
5923 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5924 expansion_notify);
5925 }
5926 return;
5927 }
5928
5929 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5930
5931 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5932 symbol_matcher,
5933 kind, [&] (offset_type namei)
5934 {
5935 /* The name was matched, now expand corresponding CUs that were
5936 marked. */
5937 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
5938
5939 struct dwarf2_per_cu_data *per_cu;
5940 while ((per_cu = iter.next ()) != NULL)
5941 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5942 expansion_notify);
5943 return true;
5944 }, per_objfile);
5945 }
5946
5947 const struct quick_symbol_functions dwarf2_debug_names_functions =
5948 {
5949 dw2_has_symbols,
5950 dw2_find_last_source_symtab,
5951 dw2_forget_cached_source_info,
5952 dw2_map_symtabs_matching_filename,
5953 dw2_debug_names_lookup_symbol,
5954 NULL,
5955 dw2_print_stats,
5956 dw2_debug_names_dump,
5957 dw2_debug_names_expand_symtabs_for_function,
5958 dw2_expand_all_symtabs,
5959 dw2_expand_symtabs_with_fullname,
5960 dw2_debug_names_map_matching_symbols,
5961 dw2_debug_names_expand_symtabs_matching,
5962 dw2_find_pc_sect_compunit_symtab,
5963 NULL,
5964 dw2_map_symbol_filenames
5965 };
5966
5967 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5968 to either a dwarf2_per_bfd or dwz_file object. */
5969
5970 template <typename T>
5971 static gdb::array_view<const gdb_byte>
5972 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5973 {
5974 dwarf2_section_info *section = &section_owner->gdb_index;
5975
5976 if (section->empty ())
5977 return {};
5978
5979 /* Older elfutils strip versions could keep the section in the main
5980 executable while splitting it for the separate debug info file. */
5981 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5982 return {};
5983
5984 section->read (obj);
5985
5986 /* dwarf2_section_info::size is a bfd_size_type, while
5987 gdb::array_view works with size_t. On 32-bit hosts, with
5988 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5989 is 32-bit. So we need an explicit narrowing conversion here.
5990 This is fine, because it's impossible to allocate or mmap an
5991 array/buffer larger than what size_t can represent. */
5992 return gdb::make_array_view (section->buffer, section->size);
5993 }
5994
5995 /* Lookup the index cache for the contents of the index associated to
5996 DWARF2_OBJ. */
5997
5998 static gdb::array_view<const gdb_byte>
5999 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
6000 {
6001 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6002 if (build_id == nullptr)
6003 return {};
6004
6005 return global_index_cache.lookup_gdb_index (build_id,
6006 &dwarf2_per_bfd->index_cache_res);
6007 }
6008
6009 /* Same as the above, but for DWZ. */
6010
6011 static gdb::array_view<const gdb_byte>
6012 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6013 {
6014 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6015 if (build_id == nullptr)
6016 return {};
6017
6018 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6019 }
6020
6021 /* See symfile.h. */
6022
6023 bool
6024 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6025 {
6026 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6027 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6028
6029 /* If we're about to read full symbols, don't bother with the
6030 indices. In this case we also don't care if some other debug
6031 format is making psymtabs, because they are all about to be
6032 expanded anyway. */
6033 if ((objfile->flags & OBJF_READNOW))
6034 {
6035 /* When using READNOW, the using_index flag (set below) indicates that
6036 PER_BFD was already initialized, when we loaded some other objfile. */
6037 if (per_bfd->using_index)
6038 {
6039 *index_kind = dw_index_kind::GDB_INDEX;
6040 per_objfile->resize_symtabs ();
6041 return true;
6042 }
6043
6044 per_bfd->using_index = 1;
6045 create_all_comp_units (per_objfile);
6046 create_all_type_units (per_objfile);
6047 per_bfd->quick_file_names_table
6048 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
6049 per_objfile->resize_symtabs ();
6050
6051 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6052 + per_bfd->all_type_units.size ()); ++i)
6053 {
6054 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
6055
6056 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6057 struct dwarf2_per_cu_quick_data);
6058 }
6059
6060 /* Return 1 so that gdb sees the "quick" functions. However,
6061 these functions will be no-ops because we will have expanded
6062 all symtabs. */
6063 *index_kind = dw_index_kind::GDB_INDEX;
6064 return true;
6065 }
6066
6067 /* Was a debug names index already read when we processed an objfile sharing
6068 PER_BFD? */
6069 if (per_bfd->debug_names_table != nullptr)
6070 {
6071 *index_kind = dw_index_kind::DEBUG_NAMES;
6072 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
6073 per_objfile->resize_symtabs ();
6074 return true;
6075 }
6076
6077 /* Was a GDB index already read when we processed an objfile sharing
6078 PER_BFD? */
6079 if (per_bfd->index_table != nullptr)
6080 {
6081 *index_kind = dw_index_kind::GDB_INDEX;
6082 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
6083 per_objfile->resize_symtabs ();
6084 return true;
6085 }
6086
6087 /* There might already be partial symtabs built for this BFD. This happens
6088 when loading the same binary twice with the index-cache enabled. If so,
6089 don't try to read an index. The objfile / per_objfile initialization will
6090 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6091 code path. */
6092 if (per_bfd->partial_symtabs != nullptr)
6093 return false;
6094
6095 if (dwarf2_read_debug_names (per_objfile))
6096 {
6097 *index_kind = dw_index_kind::DEBUG_NAMES;
6098 per_objfile->resize_symtabs ();
6099 return true;
6100 }
6101
6102 if (dwarf2_read_gdb_index (per_objfile,
6103 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
6104 get_gdb_index_contents_from_section<dwz_file>))
6105 {
6106 *index_kind = dw_index_kind::GDB_INDEX;
6107 per_objfile->resize_symtabs ();
6108 return true;
6109 }
6110
6111 /* ... otherwise, try to find the index in the index cache. */
6112 if (dwarf2_read_gdb_index (per_objfile,
6113 get_gdb_index_contents_from_cache,
6114 get_gdb_index_contents_from_cache_dwz))
6115 {
6116 global_index_cache.hit ();
6117 *index_kind = dw_index_kind::GDB_INDEX;
6118 per_objfile->resize_symtabs ();
6119 return true;
6120 }
6121
6122 global_index_cache.miss ();
6123 return false;
6124 }
6125
6126 \f
6127
6128 /* Build a partial symbol table. */
6129
6130 void
6131 dwarf2_build_psymtabs (struct objfile *objfile)
6132 {
6133 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6134 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6135
6136 if (per_bfd->partial_symtabs != nullptr)
6137 {
6138 /* Partial symbols were already read, so now we can simply
6139 attach them. */
6140 objfile->partial_symtabs = per_bfd->partial_symtabs;
6141 per_objfile->resize_symtabs ();
6142 return;
6143 }
6144
6145 try
6146 {
6147 /* This isn't really ideal: all the data we allocate on the
6148 objfile's obstack is still uselessly kept around. However,
6149 freeing it seems unsafe. */
6150 psymtab_discarder psymtabs (objfile);
6151 dwarf2_build_psymtabs_hard (per_objfile);
6152 psymtabs.keep ();
6153
6154 per_objfile->resize_symtabs ();
6155
6156 /* (maybe) store an index in the cache. */
6157 global_index_cache.store (per_objfile);
6158 }
6159 catch (const gdb_exception_error &except)
6160 {
6161 exception_print (gdb_stderr, except);
6162 }
6163
6164 /* Finish by setting the local reference to partial symtabs, so that
6165 we don't try to read them again if reading another objfile with the same
6166 BFD. If we can't in fact share, this won't make a difference anyway as
6167 the dwarf2_per_bfd object won't be shared. */
6168 per_bfd->partial_symtabs = objfile->partial_symtabs;
6169 }
6170
6171 /* Find the base address of the compilation unit for range lists and
6172 location lists. It will normally be specified by DW_AT_low_pc.
6173 In DWARF-3 draft 4, the base address could be overridden by
6174 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6175 compilation units with discontinuous ranges. */
6176
6177 static void
6178 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6179 {
6180 struct attribute *attr;
6181
6182 cu->base_address.reset ();
6183
6184 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6185 if (attr != nullptr)
6186 cu->base_address = attr->as_address ();
6187 else
6188 {
6189 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6190 if (attr != nullptr)
6191 cu->base_address = attr->as_address ();
6192 }
6193 }
6194
6195 /* Helper function that returns the proper abbrev section for
6196 THIS_CU. */
6197
6198 static struct dwarf2_section_info *
6199 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6200 {
6201 struct dwarf2_section_info *abbrev;
6202 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6203
6204 if (this_cu->is_dwz)
6205 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6206 else
6207 abbrev = &per_bfd->abbrev;
6208
6209 return abbrev;
6210 }
6211
6212 /* Fetch the abbreviation table offset from a comp or type unit header. */
6213
6214 static sect_offset
6215 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
6216 struct dwarf2_section_info *section,
6217 sect_offset sect_off)
6218 {
6219 bfd *abfd = section->get_bfd_owner ();
6220 const gdb_byte *info_ptr;
6221 unsigned int initial_length_size, offset_size;
6222 uint16_t version;
6223
6224 section->read (per_objfile->objfile);
6225 info_ptr = section->buffer + to_underlying (sect_off);
6226 read_initial_length (abfd, info_ptr, &initial_length_size);
6227 offset_size = initial_length_size == 4 ? 4 : 8;
6228 info_ptr += initial_length_size;
6229
6230 version = read_2_bytes (abfd, info_ptr);
6231 info_ptr += 2;
6232 if (version >= 5)
6233 {
6234 /* Skip unit type and address size. */
6235 info_ptr += 2;
6236 }
6237
6238 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6239 }
6240
6241 /* A partial symtab that is used only for include files. */
6242 struct dwarf2_include_psymtab : public partial_symtab
6243 {
6244 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6245 : partial_symtab (filename, objfile)
6246 {
6247 }
6248
6249 void read_symtab (struct objfile *objfile) override
6250 {
6251 /* It's an include file, no symbols to read for it.
6252 Everything is in the includer symtab. */
6253
6254 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6255 expansion of the includer psymtab. We use the dependencies[0] field to
6256 model the includer. But if we go the regular route of calling
6257 expand_psymtab here, and having expand_psymtab call expand_dependencies
6258 to expand the includer, we'll only use expand_psymtab on the includer
6259 (making it a non-toplevel psymtab), while if we expand the includer via
6260 another path, we'll use read_symtab (making it a toplevel psymtab).
6261 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6262 psymtab, and trigger read_symtab on the includer here directly. */
6263 includer ()->read_symtab (objfile);
6264 }
6265
6266 void expand_psymtab (struct objfile *objfile) override
6267 {
6268 /* This is not called by read_symtab, and should not be called by any
6269 expand_dependencies. */
6270 gdb_assert (false);
6271 }
6272
6273 bool readin_p (struct objfile *objfile) const override
6274 {
6275 return includer ()->readin_p (objfile);
6276 }
6277
6278 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6279 {
6280 return nullptr;
6281 }
6282
6283 private:
6284 partial_symtab *includer () const
6285 {
6286 /* An include psymtab has exactly one dependency: the psymtab that
6287 includes it. */
6288 gdb_assert (this->number_of_dependencies == 1);
6289 return this->dependencies[0];
6290 }
6291 };
6292
6293 /* Allocate a new partial symtab for file named NAME and mark this new
6294 partial symtab as being an include of PST. */
6295
6296 static void
6297 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6298 struct objfile *objfile)
6299 {
6300 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6301
6302 if (!IS_ABSOLUTE_PATH (subpst->filename))
6303 subpst->dirname = pst->dirname;
6304
6305 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6306 subpst->dependencies[0] = pst;
6307 subpst->number_of_dependencies = 1;
6308 }
6309
6310 /* Read the Line Number Program data and extract the list of files
6311 included by the source file represented by PST. Build an include
6312 partial symtab for each of these included files. */
6313
6314 static void
6315 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6316 struct die_info *die,
6317 dwarf2_psymtab *pst)
6318 {
6319 line_header_up lh;
6320 struct attribute *attr;
6321
6322 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6323 if (attr != nullptr && attr->form_is_unsigned ())
6324 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
6325 if (lh == NULL)
6326 return; /* No linetable, so no includes. */
6327
6328 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6329 that we pass in the raw text_low here; that is ok because we're
6330 only decoding the line table to make include partial symtabs, and
6331 so the addresses aren't really used. */
6332 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6333 pst->raw_text_low (), 1);
6334 }
6335
6336 static hashval_t
6337 hash_signatured_type (const void *item)
6338 {
6339 const struct signatured_type *sig_type
6340 = (const struct signatured_type *) item;
6341
6342 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6343 return sig_type->signature;
6344 }
6345
6346 static int
6347 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6348 {
6349 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6350 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6351
6352 return lhs->signature == rhs->signature;
6353 }
6354
6355 /* Allocate a hash table for signatured types. */
6356
6357 static htab_up
6358 allocate_signatured_type_table ()
6359 {
6360 return htab_up (htab_create_alloc (41,
6361 hash_signatured_type,
6362 eq_signatured_type,
6363 NULL, xcalloc, xfree));
6364 }
6365
6366 /* A helper function to add a signatured type CU to a table. */
6367
6368 static int
6369 add_signatured_type_cu_to_table (void **slot, void *datum)
6370 {
6371 struct signatured_type *sigt = (struct signatured_type *) *slot;
6372 std::vector<signatured_type *> *all_type_units
6373 = (std::vector<signatured_type *> *) datum;
6374
6375 all_type_units->push_back (sigt);
6376
6377 return 1;
6378 }
6379
6380 /* A helper for create_debug_types_hash_table. Read types from SECTION
6381 and fill them into TYPES_HTAB. It will process only type units,
6382 therefore DW_UT_type. */
6383
6384 static void
6385 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6386 struct dwo_file *dwo_file,
6387 dwarf2_section_info *section, htab_up &types_htab,
6388 rcuh_kind section_kind)
6389 {
6390 struct objfile *objfile = per_objfile->objfile;
6391 struct dwarf2_section_info *abbrev_section;
6392 bfd *abfd;
6393 const gdb_byte *info_ptr, *end_ptr;
6394
6395 abbrev_section = (dwo_file != NULL
6396 ? &dwo_file->sections.abbrev
6397 : &per_objfile->per_bfd->abbrev);
6398
6399 dwarf_read_debug_printf ("Reading %s for %s:",
6400 section->get_name (),
6401 abbrev_section->get_file_name ());
6402
6403 section->read (objfile);
6404 info_ptr = section->buffer;
6405
6406 if (info_ptr == NULL)
6407 return;
6408
6409 /* We can't set abfd until now because the section may be empty or
6410 not present, in which case the bfd is unknown. */
6411 abfd = section->get_bfd_owner ();
6412
6413 /* We don't use cutu_reader here because we don't need to read
6414 any dies: the signature is in the header. */
6415
6416 end_ptr = info_ptr + section->size;
6417 while (info_ptr < end_ptr)
6418 {
6419 struct signatured_type *sig_type;
6420 struct dwo_unit *dwo_tu;
6421 void **slot;
6422 const gdb_byte *ptr = info_ptr;
6423 struct comp_unit_head header;
6424 unsigned int length;
6425
6426 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6427
6428 /* Initialize it due to a false compiler warning. */
6429 header.signature = -1;
6430 header.type_cu_offset_in_tu = (cu_offset) -1;
6431
6432 /* We need to read the type's signature in order to build the hash
6433 table, but we don't need anything else just yet. */
6434
6435 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6436 abbrev_section, ptr, section_kind);
6437
6438 length = header.get_length ();
6439
6440 /* Skip dummy type units. */
6441 if (ptr >= info_ptr + length
6442 || peek_abbrev_code (abfd, ptr) == 0
6443 || (header.unit_type != DW_UT_type
6444 && header.unit_type != DW_UT_split_type))
6445 {
6446 info_ptr += length;
6447 continue;
6448 }
6449
6450 if (types_htab == NULL)
6451 {
6452 if (dwo_file)
6453 types_htab = allocate_dwo_unit_table ();
6454 else
6455 types_htab = allocate_signatured_type_table ();
6456 }
6457
6458 if (dwo_file)
6459 {
6460 sig_type = NULL;
6461 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6462 dwo_tu->dwo_file = dwo_file;
6463 dwo_tu->signature = header.signature;
6464 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6465 dwo_tu->section = section;
6466 dwo_tu->sect_off = sect_off;
6467 dwo_tu->length = length;
6468 }
6469 else
6470 {
6471 /* N.B.: type_offset is not usable if this type uses a DWO file.
6472 The real type_offset is in the DWO file. */
6473 dwo_tu = NULL;
6474 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6475 sig_type->signature = header.signature;
6476 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6477 sig_type->per_cu.is_debug_types = 1;
6478 sig_type->per_cu.section = section;
6479 sig_type->per_cu.sect_off = sect_off;
6480 sig_type->per_cu.length = length;
6481 }
6482
6483 slot = htab_find_slot (types_htab.get (),
6484 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6485 INSERT);
6486 gdb_assert (slot != NULL);
6487 if (*slot != NULL)
6488 {
6489 sect_offset dup_sect_off;
6490
6491 if (dwo_file)
6492 {
6493 const struct dwo_unit *dup_tu
6494 = (const struct dwo_unit *) *slot;
6495
6496 dup_sect_off = dup_tu->sect_off;
6497 }
6498 else
6499 {
6500 const struct signatured_type *dup_tu
6501 = (const struct signatured_type *) *slot;
6502
6503 dup_sect_off = dup_tu->per_cu.sect_off;
6504 }
6505
6506 complaint (_("debug type entry at offset %s is duplicate to"
6507 " the entry at offset %s, signature %s"),
6508 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6509 hex_string (header.signature));
6510 }
6511 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6512
6513 dwarf_read_debug_printf_v (" offset %s, signature %s",
6514 sect_offset_str (sect_off),
6515 hex_string (header.signature));
6516
6517 info_ptr += length;
6518 }
6519 }
6520
6521 /* Create the hash table of all entries in the .debug_types
6522 (or .debug_types.dwo) section(s).
6523 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6524 otherwise it is NULL.
6525
6526 The result is a pointer to the hash table or NULL if there are no types.
6527
6528 Note: This function processes DWO files only, not DWP files. */
6529
6530 static void
6531 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6532 struct dwo_file *dwo_file,
6533 gdb::array_view<dwarf2_section_info> type_sections,
6534 htab_up &types_htab)
6535 {
6536 for (dwarf2_section_info &section : type_sections)
6537 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6538 rcuh_kind::TYPE);
6539 }
6540
6541 /* Create the hash table of all entries in the .debug_types section,
6542 and initialize all_type_units.
6543 The result is zero if there is an error (e.g. missing .debug_types section),
6544 otherwise non-zero. */
6545
6546 static int
6547 create_all_type_units (dwarf2_per_objfile *per_objfile)
6548 {
6549 htab_up types_htab;
6550
6551 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6552 types_htab, rcuh_kind::COMPILE);
6553 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6554 types_htab);
6555 if (types_htab == NULL)
6556 {
6557 per_objfile->per_bfd->signatured_types = NULL;
6558 return 0;
6559 }
6560
6561 per_objfile->per_bfd->signatured_types = std::move (types_htab);
6562
6563 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6564 per_objfile->per_bfd->all_type_units.reserve
6565 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
6566
6567 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
6568 add_signatured_type_cu_to_table,
6569 &per_objfile->per_bfd->all_type_units);
6570
6571 return 1;
6572 }
6573
6574 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6575 If SLOT is non-NULL, it is the entry to use in the hash table.
6576 Otherwise we find one. */
6577
6578 static struct signatured_type *
6579 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6580 {
6581 if (per_objfile->per_bfd->all_type_units.size ()
6582 == per_objfile->per_bfd->all_type_units.capacity ())
6583 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6584
6585 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6586
6587 per_objfile->resize_symtabs ();
6588
6589 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6590 sig_type->signature = sig;
6591 sig_type->per_cu.is_debug_types = 1;
6592 if (per_objfile->per_bfd->using_index)
6593 {
6594 sig_type->per_cu.v.quick =
6595 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6596 struct dwarf2_per_cu_quick_data);
6597 }
6598
6599 if (slot == NULL)
6600 {
6601 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6602 sig_type, INSERT);
6603 }
6604 gdb_assert (*slot == NULL);
6605 *slot = sig_type;
6606 /* The rest of sig_type must be filled in by the caller. */
6607 return sig_type;
6608 }
6609
6610 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6611 Fill in SIG_ENTRY with DWO_ENTRY. */
6612
6613 static void
6614 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6615 struct signatured_type *sig_entry,
6616 struct dwo_unit *dwo_entry)
6617 {
6618 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6619
6620 /* Make sure we're not clobbering something we don't expect to. */
6621 gdb_assert (! sig_entry->per_cu.queued);
6622 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6623 if (per_bfd->using_index)
6624 {
6625 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6626 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6627 }
6628 else
6629 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6630 gdb_assert (sig_entry->signature == dwo_entry->signature);
6631 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6632 gdb_assert (sig_entry->type_unit_group == NULL);
6633 gdb_assert (sig_entry->dwo_unit == NULL);
6634
6635 sig_entry->per_cu.section = dwo_entry->section;
6636 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6637 sig_entry->per_cu.length = dwo_entry->length;
6638 sig_entry->per_cu.reading_dwo_directly = 1;
6639 sig_entry->per_cu.per_bfd = per_bfd;
6640 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6641 sig_entry->dwo_unit = dwo_entry;
6642 }
6643
6644 /* Subroutine of lookup_signatured_type.
6645 If we haven't read the TU yet, create the signatured_type data structure
6646 for a TU to be read in directly from a DWO file, bypassing the stub.
6647 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6648 using .gdb_index, then when reading a CU we want to stay in the DWO file
6649 containing that CU. Otherwise we could end up reading several other DWO
6650 files (due to comdat folding) to process the transitive closure of all the
6651 mentioned TUs, and that can be slow. The current DWO file will have every
6652 type signature that it needs.
6653 We only do this for .gdb_index because in the psymtab case we already have
6654 to read all the DWOs to build the type unit groups. */
6655
6656 static struct signatured_type *
6657 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6658 {
6659 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6660 struct dwo_file *dwo_file;
6661 struct dwo_unit find_dwo_entry, *dwo_entry;
6662 struct signatured_type find_sig_entry, *sig_entry;
6663 void **slot;
6664
6665 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6666
6667 /* If TU skeletons have been removed then we may not have read in any
6668 TUs yet. */
6669 if (per_objfile->per_bfd->signatured_types == NULL)
6670 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6671
6672 /* We only ever need to read in one copy of a signatured type.
6673 Use the global signatured_types array to do our own comdat-folding
6674 of types. If this is the first time we're reading this TU, and
6675 the TU has an entry in .gdb_index, replace the recorded data from
6676 .gdb_index with this TU. */
6677
6678 find_sig_entry.signature = sig;
6679 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6680 &find_sig_entry, INSERT);
6681 sig_entry = (struct signatured_type *) *slot;
6682
6683 /* We can get here with the TU already read, *or* in the process of being
6684 read. Don't reassign the global entry to point to this DWO if that's
6685 the case. Also note that if the TU is already being read, it may not
6686 have come from a DWO, the program may be a mix of Fission-compiled
6687 code and non-Fission-compiled code. */
6688
6689 /* Have we already tried to read this TU?
6690 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6691 needn't exist in the global table yet). */
6692 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6693 return sig_entry;
6694
6695 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6696 dwo_unit of the TU itself. */
6697 dwo_file = cu->dwo_unit->dwo_file;
6698
6699 /* Ok, this is the first time we're reading this TU. */
6700 if (dwo_file->tus == NULL)
6701 return NULL;
6702 find_dwo_entry.signature = sig;
6703 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6704 &find_dwo_entry);
6705 if (dwo_entry == NULL)
6706 return NULL;
6707
6708 /* If the global table doesn't have an entry for this TU, add one. */
6709 if (sig_entry == NULL)
6710 sig_entry = add_type_unit (per_objfile, sig, slot);
6711
6712 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6713 sig_entry->per_cu.tu_read = 1;
6714 return sig_entry;
6715 }
6716
6717 /* Subroutine of lookup_signatured_type.
6718 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6719 then try the DWP file. If the TU stub (skeleton) has been removed then
6720 it won't be in .gdb_index. */
6721
6722 static struct signatured_type *
6723 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6724 {
6725 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6726 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6727 struct dwo_unit *dwo_entry;
6728 struct signatured_type find_sig_entry, *sig_entry;
6729 void **slot;
6730
6731 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6732 gdb_assert (dwp_file != NULL);
6733
6734 /* If TU skeletons have been removed then we may not have read in any
6735 TUs yet. */
6736 if (per_objfile->per_bfd->signatured_types == NULL)
6737 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6738
6739 find_sig_entry.signature = sig;
6740 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6741 &find_sig_entry, INSERT);
6742 sig_entry = (struct signatured_type *) *slot;
6743
6744 /* Have we already tried to read this TU?
6745 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6746 needn't exist in the global table yet). */
6747 if (sig_entry != NULL)
6748 return sig_entry;
6749
6750 if (dwp_file->tus == NULL)
6751 return NULL;
6752 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6753 1 /* is_debug_types */);
6754 if (dwo_entry == NULL)
6755 return NULL;
6756
6757 sig_entry = add_type_unit (per_objfile, sig, slot);
6758 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6759
6760 return sig_entry;
6761 }
6762
6763 /* Lookup a signature based type for DW_FORM_ref_sig8.
6764 Returns NULL if signature SIG is not present in the table.
6765 It is up to the caller to complain about this. */
6766
6767 static struct signatured_type *
6768 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6769 {
6770 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6771
6772 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6773 {
6774 /* We're in a DWO/DWP file, and we're using .gdb_index.
6775 These cases require special processing. */
6776 if (get_dwp_file (per_objfile) == NULL)
6777 return lookup_dwo_signatured_type (cu, sig);
6778 else
6779 return lookup_dwp_signatured_type (cu, sig);
6780 }
6781 else
6782 {
6783 struct signatured_type find_entry, *entry;
6784
6785 if (per_objfile->per_bfd->signatured_types == NULL)
6786 return NULL;
6787 find_entry.signature = sig;
6788 entry = ((struct signatured_type *)
6789 htab_find (per_objfile->per_bfd->signatured_types.get (),
6790 &find_entry));
6791 return entry;
6792 }
6793 }
6794
6795 /* Low level DIE reading support. */
6796
6797 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6798
6799 static void
6800 init_cu_die_reader (struct die_reader_specs *reader,
6801 struct dwarf2_cu *cu,
6802 struct dwarf2_section_info *section,
6803 struct dwo_file *dwo_file,
6804 struct abbrev_table *abbrev_table)
6805 {
6806 gdb_assert (section->readin && section->buffer != NULL);
6807 reader->abfd = section->get_bfd_owner ();
6808 reader->cu = cu;
6809 reader->dwo_file = dwo_file;
6810 reader->die_section = section;
6811 reader->buffer = section->buffer;
6812 reader->buffer_end = section->buffer + section->size;
6813 reader->abbrev_table = abbrev_table;
6814 }
6815
6816 /* Subroutine of cutu_reader to simplify it.
6817 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6818 There's just a lot of work to do, and cutu_reader is big enough
6819 already.
6820
6821 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6822 from it to the DIE in the DWO. If NULL we are skipping the stub.
6823 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6824 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6825 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6826 STUB_COMP_DIR may be non-NULL.
6827 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6828 are filled in with the info of the DIE from the DWO file.
6829 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6830 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6831 kept around for at least as long as *RESULT_READER.
6832
6833 The result is non-zero if a valid (non-dummy) DIE was found. */
6834
6835 static int
6836 read_cutu_die_from_dwo (dwarf2_cu *cu,
6837 struct dwo_unit *dwo_unit,
6838 struct die_info *stub_comp_unit_die,
6839 const char *stub_comp_dir,
6840 struct die_reader_specs *result_reader,
6841 const gdb_byte **result_info_ptr,
6842 struct die_info **result_comp_unit_die,
6843 abbrev_table_up *result_dwo_abbrev_table)
6844 {
6845 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6846 dwarf2_per_cu_data *per_cu = cu->per_cu;
6847 struct objfile *objfile = per_objfile->objfile;
6848 bfd *abfd;
6849 const gdb_byte *begin_info_ptr, *info_ptr;
6850 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6851 int i,num_extra_attrs;
6852 struct dwarf2_section_info *dwo_abbrev_section;
6853 struct die_info *comp_unit_die;
6854
6855 /* At most one of these may be provided. */
6856 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6857
6858 /* These attributes aren't processed until later:
6859 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6860 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6861 referenced later. However, these attributes are found in the stub
6862 which we won't have later. In order to not impose this complication
6863 on the rest of the code, we read them here and copy them to the
6864 DWO CU/TU die. */
6865
6866 stmt_list = NULL;
6867 low_pc = NULL;
6868 high_pc = NULL;
6869 ranges = NULL;
6870 comp_dir = NULL;
6871
6872 if (stub_comp_unit_die != NULL)
6873 {
6874 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6875 DWO file. */
6876 if (!per_cu->is_debug_types)
6877 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6878 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6879 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6880 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6881 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6882
6883 cu->addr_base = stub_comp_unit_die->addr_base ();
6884
6885 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6886 here (if needed). We need the value before we can process
6887 DW_AT_ranges. */
6888 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6889 }
6890 else if (stub_comp_dir != NULL)
6891 {
6892 /* Reconstruct the comp_dir attribute to simplify the code below. */
6893 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6894 comp_dir->name = DW_AT_comp_dir;
6895 comp_dir->form = DW_FORM_string;
6896 comp_dir->set_string_noncanonical (stub_comp_dir);
6897 }
6898
6899 /* Set up for reading the DWO CU/TU. */
6900 cu->dwo_unit = dwo_unit;
6901 dwarf2_section_info *section = dwo_unit->section;
6902 section->read (objfile);
6903 abfd = section->get_bfd_owner ();
6904 begin_info_ptr = info_ptr = (section->buffer
6905 + to_underlying (dwo_unit->sect_off));
6906 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6907
6908 if (per_cu->is_debug_types)
6909 {
6910 signatured_type *sig_type = (struct signatured_type *) per_cu;
6911
6912 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6913 section, dwo_abbrev_section,
6914 info_ptr, rcuh_kind::TYPE);
6915 /* This is not an assert because it can be caused by bad debug info. */
6916 if (sig_type->signature != cu->header.signature)
6917 {
6918 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6919 " TU at offset %s [in module %s]"),
6920 hex_string (sig_type->signature),
6921 hex_string (cu->header.signature),
6922 sect_offset_str (dwo_unit->sect_off),
6923 bfd_get_filename (abfd));
6924 }
6925 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6926 /* For DWOs coming from DWP files, we don't know the CU length
6927 nor the type's offset in the TU until now. */
6928 dwo_unit->length = cu->header.get_length ();
6929 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6930
6931 /* Establish the type offset that can be used to lookup the type.
6932 For DWO files, we don't know it until now. */
6933 sig_type->type_offset_in_section
6934 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6935 }
6936 else
6937 {
6938 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6939 section, dwo_abbrev_section,
6940 info_ptr, rcuh_kind::COMPILE);
6941 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6942 /* For DWOs coming from DWP files, we don't know the CU length
6943 until now. */
6944 dwo_unit->length = cu->header.get_length ();
6945 }
6946
6947 dwo_abbrev_section->read (objfile);
6948 *result_dwo_abbrev_table
6949 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6950 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6951 result_dwo_abbrev_table->get ());
6952
6953 /* Read in the die, but leave space to copy over the attributes
6954 from the stub. This has the benefit of simplifying the rest of
6955 the code - all the work to maintain the illusion of a single
6956 DW_TAG_{compile,type}_unit DIE is done here. */
6957 num_extra_attrs = ((stmt_list != NULL)
6958 + (low_pc != NULL)
6959 + (high_pc != NULL)
6960 + (ranges != NULL)
6961 + (comp_dir != NULL));
6962 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6963 num_extra_attrs);
6964
6965 /* Copy over the attributes from the stub to the DIE we just read in. */
6966 comp_unit_die = *result_comp_unit_die;
6967 i = comp_unit_die->num_attrs;
6968 if (stmt_list != NULL)
6969 comp_unit_die->attrs[i++] = *stmt_list;
6970 if (low_pc != NULL)
6971 comp_unit_die->attrs[i++] = *low_pc;
6972 if (high_pc != NULL)
6973 comp_unit_die->attrs[i++] = *high_pc;
6974 if (ranges != NULL)
6975 comp_unit_die->attrs[i++] = *ranges;
6976 if (comp_dir != NULL)
6977 comp_unit_die->attrs[i++] = *comp_dir;
6978 comp_unit_die->num_attrs += num_extra_attrs;
6979
6980 if (dwarf_die_debug)
6981 {
6982 fprintf_unfiltered (gdb_stdlog,
6983 "Read die from %s@0x%x of %s:\n",
6984 section->get_name (),
6985 (unsigned) (begin_info_ptr - section->buffer),
6986 bfd_get_filename (abfd));
6987 dump_die (comp_unit_die, dwarf_die_debug);
6988 }
6989
6990 /* Skip dummy compilation units. */
6991 if (info_ptr >= begin_info_ptr + dwo_unit->length
6992 || peek_abbrev_code (abfd, info_ptr) == 0)
6993 return 0;
6994
6995 *result_info_ptr = info_ptr;
6996 return 1;
6997 }
6998
6999 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7000 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7001 signature is part of the header. */
7002 static gdb::optional<ULONGEST>
7003 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7004 {
7005 if (cu->header.version >= 5)
7006 return cu->header.signature;
7007 struct attribute *attr;
7008 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7009 if (attr == nullptr || !attr->form_is_unsigned ())
7010 return gdb::optional<ULONGEST> ();
7011 return attr->as_unsigned ();
7012 }
7013
7014 /* Subroutine of cutu_reader to simplify it.
7015 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7016 Returns NULL if the specified DWO unit cannot be found. */
7017
7018 static struct dwo_unit *
7019 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
7020 {
7021 dwarf2_per_cu_data *per_cu = cu->per_cu;
7022 struct dwo_unit *dwo_unit;
7023 const char *comp_dir;
7024
7025 gdb_assert (cu != NULL);
7026
7027 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7028 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7029 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7030
7031 if (per_cu->is_debug_types)
7032 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
7033 else
7034 {
7035 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7036
7037 if (!signature.has_value ())
7038 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7039 " [in module %s]"),
7040 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7041
7042 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
7043 }
7044
7045 return dwo_unit;
7046 }
7047
7048 /* Subroutine of cutu_reader to simplify it.
7049 See it for a description of the parameters.
7050 Read a TU directly from a DWO file, bypassing the stub. */
7051
7052 void
7053 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7054 dwarf2_per_objfile *per_objfile,
7055 dwarf2_cu *existing_cu)
7056 {
7057 struct signatured_type *sig_type;
7058
7059 /* Verify we can do the following downcast, and that we have the
7060 data we need. */
7061 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7062 sig_type = (struct signatured_type *) this_cu;
7063 gdb_assert (sig_type->dwo_unit != NULL);
7064
7065 dwarf2_cu *cu;
7066
7067 if (existing_cu != nullptr)
7068 {
7069 cu = existing_cu;
7070 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
7071 /* There's no need to do the rereading_dwo_cu handling that
7072 cutu_reader does since we don't read the stub. */
7073 }
7074 else
7075 {
7076 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7077 in per_objfile yet. */
7078 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7079 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7080 cu = m_new_cu.get ();
7081 }
7082
7083 /* A future optimization, if needed, would be to use an existing
7084 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7085 could share abbrev tables. */
7086
7087 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
7088 NULL /* stub_comp_unit_die */,
7089 sig_type->dwo_unit->dwo_file->comp_dir,
7090 this, &info_ptr,
7091 &comp_unit_die,
7092 &m_dwo_abbrev_table) == 0)
7093 {
7094 /* Dummy die. */
7095 dummy_p = true;
7096 }
7097 }
7098
7099 /* Initialize a CU (or TU) and read its DIEs.
7100 If the CU defers to a DWO file, read the DWO file as well.
7101
7102 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7103 Otherwise the table specified in the comp unit header is read in and used.
7104 This is an optimization for when we already have the abbrev table.
7105
7106 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7107 allocated. */
7108
7109 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7110 dwarf2_per_objfile *per_objfile,
7111 struct abbrev_table *abbrev_table,
7112 dwarf2_cu *existing_cu,
7113 bool skip_partial)
7114 : die_reader_specs {},
7115 m_this_cu (this_cu)
7116 {
7117 struct objfile *objfile = per_objfile->objfile;
7118 struct dwarf2_section_info *section = this_cu->section;
7119 bfd *abfd = section->get_bfd_owner ();
7120 const gdb_byte *begin_info_ptr;
7121 struct signatured_type *sig_type = NULL;
7122 struct dwarf2_section_info *abbrev_section;
7123 /* Non-zero if CU currently points to a DWO file and we need to
7124 reread it. When this happens we need to reread the skeleton die
7125 before we can reread the DWO file (this only applies to CUs, not TUs). */
7126 int rereading_dwo_cu = 0;
7127
7128 if (dwarf_die_debug)
7129 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7130 this_cu->is_debug_types ? "type" : "comp",
7131 sect_offset_str (this_cu->sect_off));
7132
7133 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7134 file (instead of going through the stub), short-circuit all of this. */
7135 if (this_cu->reading_dwo_directly)
7136 {
7137 /* Narrow down the scope of possibilities to have to understand. */
7138 gdb_assert (this_cu->is_debug_types);
7139 gdb_assert (abbrev_table == NULL);
7140 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
7141 return;
7142 }
7143
7144 /* This is cheap if the section is already read in. */
7145 section->read (objfile);
7146
7147 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7148
7149 abbrev_section = get_abbrev_section_for_cu (this_cu);
7150
7151 dwarf2_cu *cu;
7152
7153 if (existing_cu != nullptr)
7154 {
7155 cu = existing_cu;
7156 /* If this CU is from a DWO file we need to start over, we need to
7157 refetch the attributes from the skeleton CU.
7158 This could be optimized by retrieving those attributes from when we
7159 were here the first time: the previous comp_unit_die was stored in
7160 comp_unit_obstack. But there's no data yet that we need this
7161 optimization. */
7162 if (cu->dwo_unit != NULL)
7163 rereading_dwo_cu = 1;
7164 }
7165 else
7166 {
7167 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7168 in per_objfile yet. */
7169 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7170 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7171 cu = m_new_cu.get ();
7172 }
7173
7174 /* Get the header. */
7175 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7176 {
7177 /* We already have the header, there's no need to read it in again. */
7178 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7179 }
7180 else
7181 {
7182 if (this_cu->is_debug_types)
7183 {
7184 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7185 section, abbrev_section,
7186 info_ptr, rcuh_kind::TYPE);
7187
7188 /* Since per_cu is the first member of struct signatured_type,
7189 we can go from a pointer to one to a pointer to the other. */
7190 sig_type = (struct signatured_type *) this_cu;
7191 gdb_assert (sig_type->signature == cu->header.signature);
7192 gdb_assert (sig_type->type_offset_in_tu
7193 == cu->header.type_cu_offset_in_tu);
7194 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7195
7196 /* LENGTH has not been set yet for type units if we're
7197 using .gdb_index. */
7198 this_cu->length = cu->header.get_length ();
7199
7200 /* Establish the type offset that can be used to lookup the type. */
7201 sig_type->type_offset_in_section =
7202 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7203
7204 this_cu->dwarf_version = cu->header.version;
7205 }
7206 else
7207 {
7208 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7209 section, abbrev_section,
7210 info_ptr,
7211 rcuh_kind::COMPILE);
7212
7213 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7214 if (this_cu->length == 0)
7215 this_cu->length = cu->header.get_length ();
7216 else
7217 gdb_assert (this_cu->length == cu->header.get_length ());
7218 this_cu->dwarf_version = cu->header.version;
7219 }
7220 }
7221
7222 /* Skip dummy compilation units. */
7223 if (info_ptr >= begin_info_ptr + this_cu->length
7224 || peek_abbrev_code (abfd, info_ptr) == 0)
7225 {
7226 dummy_p = true;
7227 return;
7228 }
7229
7230 /* If we don't have them yet, read the abbrevs for this compilation unit.
7231 And if we need to read them now, make sure they're freed when we're
7232 done. */
7233 if (abbrev_table != NULL)
7234 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7235 else
7236 {
7237 abbrev_section->read (objfile);
7238 m_abbrev_table_holder
7239 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
7240 abbrev_table = m_abbrev_table_holder.get ();
7241 }
7242
7243 /* Read the top level CU/TU die. */
7244 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7245 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7246
7247 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7248 {
7249 dummy_p = true;
7250 return;
7251 }
7252
7253 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7254 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7255 table from the DWO file and pass the ownership over to us. It will be
7256 referenced from READER, so we must make sure to free it after we're done
7257 with READER.
7258
7259 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7260 DWO CU, that this test will fail (the attribute will not be present). */
7261 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7262 if (dwo_name != nullptr)
7263 {
7264 struct dwo_unit *dwo_unit;
7265 struct die_info *dwo_comp_unit_die;
7266
7267 if (comp_unit_die->has_children)
7268 {
7269 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7270 " has children (offset %s) [in module %s]"),
7271 sect_offset_str (this_cu->sect_off),
7272 bfd_get_filename (abfd));
7273 }
7274 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7275 if (dwo_unit != NULL)
7276 {
7277 if (read_cutu_die_from_dwo (cu, dwo_unit,
7278 comp_unit_die, NULL,
7279 this, &info_ptr,
7280 &dwo_comp_unit_die,
7281 &m_dwo_abbrev_table) == 0)
7282 {
7283 /* Dummy die. */
7284 dummy_p = true;
7285 return;
7286 }
7287 comp_unit_die = dwo_comp_unit_die;
7288 }
7289 else
7290 {
7291 /* Yikes, we couldn't find the rest of the DIE, we only have
7292 the stub. A complaint has already been logged. There's
7293 not much more we can do except pass on the stub DIE to
7294 die_reader_func. We don't want to throw an error on bad
7295 debug info. */
7296 }
7297 }
7298 }
7299
7300 void
7301 cutu_reader::keep ()
7302 {
7303 /* Done, clean up. */
7304 gdb_assert (!dummy_p);
7305 if (m_new_cu != NULL)
7306 {
7307 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7308 now. */
7309 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7310 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7311 }
7312 }
7313
7314 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7315 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7316 assumed to have already done the lookup to find the DWO file).
7317
7318 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7319 THIS_CU->is_debug_types, but nothing else.
7320
7321 We fill in THIS_CU->length.
7322
7323 THIS_CU->cu is always freed when done.
7324 This is done in order to not leave THIS_CU->cu in a state where we have
7325 to care whether it refers to the "main" CU or the DWO CU.
7326
7327 When parent_cu is passed, it is used to provide a default value for
7328 str_offsets_base and addr_base from the parent. */
7329
7330 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7331 dwarf2_per_objfile *per_objfile,
7332 struct dwarf2_cu *parent_cu,
7333 struct dwo_file *dwo_file)
7334 : die_reader_specs {},
7335 m_this_cu (this_cu)
7336 {
7337 struct objfile *objfile = per_objfile->objfile;
7338 struct dwarf2_section_info *section = this_cu->section;
7339 bfd *abfd = section->get_bfd_owner ();
7340 struct dwarf2_section_info *abbrev_section;
7341 const gdb_byte *begin_info_ptr, *info_ptr;
7342
7343 if (dwarf_die_debug)
7344 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7345 this_cu->is_debug_types ? "type" : "comp",
7346 sect_offset_str (this_cu->sect_off));
7347
7348 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7349
7350 abbrev_section = (dwo_file != NULL
7351 ? &dwo_file->sections.abbrev
7352 : get_abbrev_section_for_cu (this_cu));
7353
7354 /* This is cheap if the section is already read in. */
7355 section->read (objfile);
7356
7357 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7358
7359 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7360 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7361 section, abbrev_section, info_ptr,
7362 (this_cu->is_debug_types
7363 ? rcuh_kind::TYPE
7364 : rcuh_kind::COMPILE));
7365
7366 if (parent_cu != nullptr)
7367 {
7368 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7369 m_new_cu->addr_base = parent_cu->addr_base;
7370 }
7371 this_cu->length = m_new_cu->header.get_length ();
7372
7373 /* Skip dummy compilation units. */
7374 if (info_ptr >= begin_info_ptr + this_cu->length
7375 || peek_abbrev_code (abfd, info_ptr) == 0)
7376 {
7377 dummy_p = true;
7378 return;
7379 }
7380
7381 abbrev_section->read (objfile);
7382 m_abbrev_table_holder
7383 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
7384
7385 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7386 m_abbrev_table_holder.get ());
7387 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7388 }
7389
7390 \f
7391 /* Type Unit Groups.
7392
7393 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7394 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7395 so that all types coming from the same compilation (.o file) are grouped
7396 together. A future step could be to put the types in the same symtab as
7397 the CU the types ultimately came from. */
7398
7399 static hashval_t
7400 hash_type_unit_group (const void *item)
7401 {
7402 const struct type_unit_group *tu_group
7403 = (const struct type_unit_group *) item;
7404
7405 return hash_stmt_list_entry (&tu_group->hash);
7406 }
7407
7408 static int
7409 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7410 {
7411 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7412 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7413
7414 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7415 }
7416
7417 /* Allocate a hash table for type unit groups. */
7418
7419 static htab_up
7420 allocate_type_unit_groups_table ()
7421 {
7422 return htab_up (htab_create_alloc (3,
7423 hash_type_unit_group,
7424 eq_type_unit_group,
7425 NULL, xcalloc, xfree));
7426 }
7427
7428 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7429 partial symtabs. We combine several TUs per psymtab to not let the size
7430 of any one psymtab grow too big. */
7431 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7432 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7433
7434 /* Helper routine for get_type_unit_group.
7435 Create the type_unit_group object used to hold one or more TUs. */
7436
7437 static struct type_unit_group *
7438 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7439 {
7440 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7441 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7442 struct dwarf2_per_cu_data *per_cu;
7443 struct type_unit_group *tu_group;
7444
7445 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
7446 per_cu = &tu_group->per_cu;
7447 per_cu->per_bfd = per_bfd;
7448
7449 if (per_bfd->using_index)
7450 {
7451 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7452 struct dwarf2_per_cu_quick_data);
7453 }
7454 else
7455 {
7456 unsigned int line_offset = to_underlying (line_offset_struct);
7457 dwarf2_psymtab *pst;
7458 std::string name;
7459
7460 /* Give the symtab a useful name for debug purposes. */
7461 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7462 name = string_printf ("<type_units_%d>",
7463 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7464 else
7465 name = string_printf ("<type_units_at_0x%x>", line_offset);
7466
7467 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
7468 pst->anonymous = true;
7469 }
7470
7471 tu_group->hash.dwo_unit = cu->dwo_unit;
7472 tu_group->hash.line_sect_off = line_offset_struct;
7473
7474 return tu_group;
7475 }
7476
7477 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7478 STMT_LIST is a DW_AT_stmt_list attribute. */
7479
7480 static struct type_unit_group *
7481 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7482 {
7483 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7484 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7485 struct type_unit_group *tu_group;
7486 void **slot;
7487 unsigned int line_offset;
7488 struct type_unit_group type_unit_group_for_lookup;
7489
7490 if (per_objfile->per_bfd->type_unit_groups == NULL)
7491 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7492
7493 /* Do we need to create a new group, or can we use an existing one? */
7494
7495 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
7496 {
7497 line_offset = stmt_list->as_unsigned ();
7498 ++tu_stats->nr_symtab_sharers;
7499 }
7500 else
7501 {
7502 /* Ugh, no stmt_list. Rare, but we have to handle it.
7503 We can do various things here like create one group per TU or
7504 spread them over multiple groups to split up the expansion work.
7505 To avoid worst case scenarios (too many groups or too large groups)
7506 we, umm, group them in bunches. */
7507 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7508 | (tu_stats->nr_stmt_less_type_units
7509 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7510 ++tu_stats->nr_stmt_less_type_units;
7511 }
7512
7513 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7514 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7515 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7516 &type_unit_group_for_lookup, INSERT);
7517 if (*slot != NULL)
7518 {
7519 tu_group = (struct type_unit_group *) *slot;
7520 gdb_assert (tu_group != NULL);
7521 }
7522 else
7523 {
7524 sect_offset line_offset_struct = (sect_offset) line_offset;
7525 tu_group = create_type_unit_group (cu, line_offset_struct);
7526 *slot = tu_group;
7527 ++tu_stats->nr_symtabs;
7528 }
7529
7530 return tu_group;
7531 }
7532 \f
7533 /* Partial symbol tables. */
7534
7535 /* Create a psymtab named NAME and assign it to PER_CU.
7536
7537 The caller must fill in the following details:
7538 dirname, textlow, texthigh. */
7539
7540 static dwarf2_psymtab *
7541 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7542 dwarf2_per_objfile *per_objfile,
7543 const char *name)
7544 {
7545 struct objfile *objfile = per_objfile->objfile;
7546 dwarf2_psymtab *pst;
7547
7548 pst = new dwarf2_psymtab (name, objfile, per_cu);
7549
7550 pst->psymtabs_addrmap_supported = true;
7551
7552 /* This is the glue that links PST into GDB's symbol API. */
7553 per_cu->v.psymtab = pst;
7554
7555 return pst;
7556 }
7557
7558 /* DIE reader function for process_psymtab_comp_unit. */
7559
7560 static void
7561 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7562 const gdb_byte *info_ptr,
7563 struct die_info *comp_unit_die,
7564 enum language pretend_language)
7565 {
7566 struct dwarf2_cu *cu = reader->cu;
7567 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7568 struct objfile *objfile = per_objfile->objfile;
7569 struct gdbarch *gdbarch = objfile->arch ();
7570 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7571 CORE_ADDR baseaddr;
7572 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7573 dwarf2_psymtab *pst;
7574 enum pc_bounds_kind cu_bounds_kind;
7575 const char *filename;
7576
7577 gdb_assert (! per_cu->is_debug_types);
7578
7579 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7580
7581 /* Allocate a new partial symbol table structure. */
7582 gdb::unique_xmalloc_ptr<char> debug_filename;
7583 static const char artificial[] = "<artificial>";
7584 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7585 if (filename == NULL)
7586 filename = "";
7587 else if (strcmp (filename, artificial) == 0)
7588 {
7589 debug_filename.reset (concat (artificial, "@",
7590 sect_offset_str (per_cu->sect_off),
7591 (char *) NULL));
7592 filename = debug_filename.get ();
7593 }
7594
7595 pst = create_partial_symtab (per_cu, per_objfile, filename);
7596
7597 /* This must be done before calling dwarf2_build_include_psymtabs. */
7598 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7599
7600 baseaddr = objfile->text_section_offset ();
7601
7602 dwarf2_find_base_address (comp_unit_die, cu);
7603
7604 /* Possibly set the default values of LOWPC and HIGHPC from
7605 `DW_AT_ranges'. */
7606 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7607 &best_highpc, cu, pst);
7608 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7609 {
7610 CORE_ADDR low
7611 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7612 - baseaddr);
7613 CORE_ADDR high
7614 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7615 - baseaddr - 1);
7616 /* Store the contiguous range if it is not empty; it can be
7617 empty for CUs with no code. */
7618 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7619 low, high, pst);
7620 }
7621
7622 /* Check if comp unit has_children.
7623 If so, read the rest of the partial symbols from this comp unit.
7624 If not, there's no more debug_info for this comp unit. */
7625 if (comp_unit_die->has_children)
7626 {
7627 struct partial_die_info *first_die;
7628 CORE_ADDR lowpc, highpc;
7629
7630 lowpc = ((CORE_ADDR) -1);
7631 highpc = ((CORE_ADDR) 0);
7632
7633 first_die = load_partial_dies (reader, info_ptr, 1);
7634
7635 scan_partial_symbols (first_die, &lowpc, &highpc,
7636 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7637
7638 /* If we didn't find a lowpc, set it to highpc to avoid
7639 complaints from `maint check'. */
7640 if (lowpc == ((CORE_ADDR) -1))
7641 lowpc = highpc;
7642
7643 /* If the compilation unit didn't have an explicit address range,
7644 then use the information extracted from its child dies. */
7645 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7646 {
7647 best_lowpc = lowpc;
7648 best_highpc = highpc;
7649 }
7650 }
7651 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7652 best_lowpc + baseaddr)
7653 - baseaddr);
7654 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7655 best_highpc + baseaddr)
7656 - baseaddr);
7657
7658 pst->end ();
7659
7660 if (!cu->per_cu->imported_symtabs_empty ())
7661 {
7662 int i;
7663 int len = cu->per_cu->imported_symtabs_size ();
7664
7665 /* Fill in 'dependencies' here; we fill in 'users' in a
7666 post-pass. */
7667 pst->number_of_dependencies = len;
7668 pst->dependencies
7669 = objfile->partial_symtabs->allocate_dependencies (len);
7670 for (i = 0; i < len; ++i)
7671 {
7672 pst->dependencies[i]
7673 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7674 }
7675
7676 cu->per_cu->imported_symtabs_free ();
7677 }
7678
7679 /* Get the list of files included in the current compilation unit,
7680 and build a psymtab for each of them. */
7681 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7682
7683 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7684 ", %d global, %d static syms",
7685 per_cu->is_debug_types ? "type" : "comp",
7686 sect_offset_str (per_cu->sect_off),
7687 paddress (gdbarch, pst->text_low (objfile)),
7688 paddress (gdbarch, pst->text_high (objfile)),
7689 (int) pst->global_psymbols.size (),
7690 (int) pst->static_psymbols.size ());
7691 }
7692
7693 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7694 Process compilation unit THIS_CU for a psymtab. */
7695
7696 static void
7697 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7698 dwarf2_per_objfile *per_objfile,
7699 bool want_partial_unit,
7700 enum language pretend_language)
7701 {
7702 /* If this compilation unit was already read in, free the
7703 cached copy in order to read it in again. This is
7704 necessary because we skipped some symbols when we first
7705 read in the compilation unit (see load_partial_dies).
7706 This problem could be avoided, but the benefit is unclear. */
7707 per_objfile->remove_cu (this_cu);
7708
7709 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7710
7711 switch (reader.comp_unit_die->tag)
7712 {
7713 case DW_TAG_compile_unit:
7714 this_cu->unit_type = DW_UT_compile;
7715 break;
7716 case DW_TAG_partial_unit:
7717 this_cu->unit_type = DW_UT_partial;
7718 break;
7719 default:
7720 abort ();
7721 }
7722
7723 if (reader.dummy_p)
7724 {
7725 /* Nothing. */
7726 }
7727 else if (this_cu->is_debug_types)
7728 build_type_psymtabs_reader (&reader, reader.info_ptr,
7729 reader.comp_unit_die);
7730 else if (want_partial_unit
7731 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7732 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7733 reader.comp_unit_die,
7734 pretend_language);
7735
7736 this_cu->lang = reader.cu->language;
7737
7738 /* Age out any secondary CUs. */
7739 per_objfile->age_comp_units ();
7740 }
7741
7742 /* Reader function for build_type_psymtabs. */
7743
7744 static void
7745 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7746 const gdb_byte *info_ptr,
7747 struct die_info *type_unit_die)
7748 {
7749 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7750 struct dwarf2_cu *cu = reader->cu;
7751 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7752 struct signatured_type *sig_type;
7753 struct type_unit_group *tu_group;
7754 struct attribute *attr;
7755 struct partial_die_info *first_die;
7756 CORE_ADDR lowpc, highpc;
7757 dwarf2_psymtab *pst;
7758
7759 gdb_assert (per_cu->is_debug_types);
7760 sig_type = (struct signatured_type *) per_cu;
7761
7762 if (! type_unit_die->has_children)
7763 return;
7764
7765 attr = type_unit_die->attr (DW_AT_stmt_list);
7766 tu_group = get_type_unit_group (cu, attr);
7767
7768 if (tu_group->tus == nullptr)
7769 tu_group->tus = new std::vector<signatured_type *>;
7770 tu_group->tus->push_back (sig_type);
7771
7772 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7773 pst = create_partial_symtab (per_cu, per_objfile, "");
7774 pst->anonymous = true;
7775
7776 first_die = load_partial_dies (reader, info_ptr, 1);
7777
7778 lowpc = (CORE_ADDR) -1;
7779 highpc = (CORE_ADDR) 0;
7780 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7781
7782 pst->end ();
7783 }
7784
7785 /* Struct used to sort TUs by their abbreviation table offset. */
7786
7787 struct tu_abbrev_offset
7788 {
7789 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7790 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7791 {}
7792
7793 signatured_type *sig_type;
7794 sect_offset abbrev_offset;
7795 };
7796
7797 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7798
7799 static bool
7800 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7801 const struct tu_abbrev_offset &b)
7802 {
7803 return a.abbrev_offset < b.abbrev_offset;
7804 }
7805
7806 /* Efficiently read all the type units.
7807 This does the bulk of the work for build_type_psymtabs.
7808
7809 The efficiency is because we sort TUs by the abbrev table they use and
7810 only read each abbrev table once. In one program there are 200K TUs
7811 sharing 8K abbrev tables.
7812
7813 The main purpose of this function is to support building the
7814 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7815 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7816 can collapse the search space by grouping them by stmt_list.
7817 The savings can be significant, in the same program from above the 200K TUs
7818 share 8K stmt_list tables.
7819
7820 FUNC is expected to call get_type_unit_group, which will create the
7821 struct type_unit_group if necessary and add it to
7822 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7823
7824 static void
7825 build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
7826 {
7827 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7828 abbrev_table_up abbrev_table;
7829 sect_offset abbrev_offset;
7830
7831 /* It's up to the caller to not call us multiple times. */
7832 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7833
7834 if (per_objfile->per_bfd->all_type_units.empty ())
7835 return;
7836
7837 /* TUs typically share abbrev tables, and there can be way more TUs than
7838 abbrev tables. Sort by abbrev table to reduce the number of times we
7839 read each abbrev table in.
7840 Alternatives are to punt or to maintain a cache of abbrev tables.
7841 This is simpler and efficient enough for now.
7842
7843 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7844 symtab to use). Typically TUs with the same abbrev offset have the same
7845 stmt_list value too so in practice this should work well.
7846
7847 The basic algorithm here is:
7848
7849 sort TUs by abbrev table
7850 for each TU with same abbrev table:
7851 read abbrev table if first user
7852 read TU top level DIE
7853 [IWBN if DWO skeletons had DW_AT_stmt_list]
7854 call FUNC */
7855
7856 dwarf_read_debug_printf ("Building type unit groups ...");
7857
7858 /* Sort in a separate table to maintain the order of all_type_units
7859 for .gdb_index: TU indices directly index all_type_units. */
7860 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7861 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
7862
7863 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
7864 sorted_by_abbrev.emplace_back
7865 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
7866 sig_type->per_cu.sect_off));
7867
7868 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7869 sort_tu_by_abbrev_offset);
7870
7871 abbrev_offset = (sect_offset) ~(unsigned) 0;
7872
7873 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7874 {
7875 /* Switch to the next abbrev table if necessary. */
7876 if (abbrev_table == NULL
7877 || tu.abbrev_offset != abbrev_offset)
7878 {
7879 abbrev_offset = tu.abbrev_offset;
7880 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7881 abbrev_table =
7882 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7883 ++tu_stats->nr_uniq_abbrev_tables;
7884 }
7885
7886 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
7887 abbrev_table.get (), nullptr, false);
7888 if (!reader.dummy_p)
7889 build_type_psymtabs_reader (&reader, reader.info_ptr,
7890 reader.comp_unit_die);
7891 }
7892 }
7893
7894 /* Print collected type unit statistics. */
7895
7896 static void
7897 print_tu_stats (dwarf2_per_objfile *per_objfile)
7898 {
7899 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7900
7901 dwarf_read_debug_printf ("Type unit statistics:");
7902 dwarf_read_debug_printf (" %zu TUs",
7903 per_objfile->per_bfd->all_type_units.size ());
7904 dwarf_read_debug_printf (" %d uniq abbrev tables",
7905 tu_stats->nr_uniq_abbrev_tables);
7906 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7907 tu_stats->nr_symtabs);
7908 dwarf_read_debug_printf (" %d symtab sharers",
7909 tu_stats->nr_symtab_sharers);
7910 dwarf_read_debug_printf (" %d type units without a stmt_list",
7911 tu_stats->nr_stmt_less_type_units);
7912 dwarf_read_debug_printf (" %d all_type_units reallocs",
7913 tu_stats->nr_all_type_units_reallocs);
7914 }
7915
7916 /* Traversal function for build_type_psymtabs. */
7917
7918 static int
7919 build_type_psymtab_dependencies (void **slot, void *info)
7920 {
7921 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7922 struct objfile *objfile = per_objfile->objfile;
7923 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7924 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7925 dwarf2_psymtab *pst = per_cu->v.psymtab;
7926 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7927 int i;
7928
7929 gdb_assert (len > 0);
7930 gdb_assert (per_cu->type_unit_group_p ());
7931
7932 pst->number_of_dependencies = len;
7933 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7934 for (i = 0; i < len; ++i)
7935 {
7936 struct signatured_type *iter = tu_group->tus->at (i);
7937 gdb_assert (iter->per_cu.is_debug_types);
7938 pst->dependencies[i] = iter->per_cu.v.psymtab;
7939 iter->type_unit_group = tu_group;
7940 }
7941
7942 delete tu_group->tus;
7943 tu_group->tus = nullptr;
7944
7945 return 1;
7946 }
7947
7948 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7949 Build partial symbol tables for the .debug_types comp-units. */
7950
7951 static void
7952 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7953 {
7954 if (! create_all_type_units (per_objfile))
7955 return;
7956
7957 build_type_psymtabs_1 (per_objfile);
7958 }
7959
7960 /* Traversal function for process_skeletonless_type_unit.
7961 Read a TU in a DWO file and build partial symbols for it. */
7962
7963 static int
7964 process_skeletonless_type_unit (void **slot, void *info)
7965 {
7966 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7967 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7968 struct signatured_type find_entry, *entry;
7969
7970 /* If this TU doesn't exist in the global table, add it and read it in. */
7971
7972 if (per_objfile->per_bfd->signatured_types == NULL)
7973 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7974
7975 find_entry.signature = dwo_unit->signature;
7976 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7977 &find_entry, INSERT);
7978 /* If we've already seen this type there's nothing to do. What's happening
7979 is we're doing our own version of comdat-folding here. */
7980 if (*slot != NULL)
7981 return 1;
7982
7983 /* This does the job that create_all_type_units would have done for
7984 this TU. */
7985 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7986 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7987 *slot = entry;
7988
7989 /* This does the job that build_type_psymtabs_1 would have done. */
7990 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
7991 if (!reader.dummy_p)
7992 build_type_psymtabs_reader (&reader, reader.info_ptr,
7993 reader.comp_unit_die);
7994
7995 return 1;
7996 }
7997
7998 /* Traversal function for process_skeletonless_type_units. */
7999
8000 static int
8001 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8002 {
8003 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8004
8005 if (dwo_file->tus != NULL)
8006 htab_traverse_noresize (dwo_file->tus.get (),
8007 process_skeletonless_type_unit, info);
8008
8009 return 1;
8010 }
8011
8012 /* Scan all TUs of DWO files, verifying we've processed them.
8013 This is needed in case a TU was emitted without its skeleton.
8014 Note: This can't be done until we know what all the DWO files are. */
8015
8016 static void
8017 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
8018 {
8019 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8020 if (get_dwp_file (per_objfile) == NULL
8021 && per_objfile->per_bfd->dwo_files != NULL)
8022 {
8023 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
8024 process_dwo_file_for_skeletonless_type_units,
8025 per_objfile);
8026 }
8027 }
8028
8029 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8030
8031 static void
8032 set_partial_user (dwarf2_per_objfile *per_objfile)
8033 {
8034 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8035 {
8036 dwarf2_psymtab *pst = per_cu->v.psymtab;
8037
8038 if (pst == NULL)
8039 continue;
8040
8041 for (int j = 0; j < pst->number_of_dependencies; ++j)
8042 {
8043 /* Set the 'user' field only if it is not already set. */
8044 if (pst->dependencies[j]->user == NULL)
8045 pst->dependencies[j]->user = pst;
8046 }
8047 }
8048 }
8049
8050 /* Build the partial symbol table by doing a quick pass through the
8051 .debug_info and .debug_abbrev sections. */
8052
8053 static void
8054 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
8055 {
8056 struct objfile *objfile = per_objfile->objfile;
8057
8058 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
8059 objfile_name (objfile));
8060
8061 scoped_restore restore_reading_psyms
8062 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
8063 true);
8064
8065 per_objfile->per_bfd->info.read (objfile);
8066
8067 /* Any cached compilation units will be linked by the per-objfile
8068 read_in_chain. Make sure to free them when we're done. */
8069 free_cached_comp_units freer (per_objfile);
8070
8071 build_type_psymtabs (per_objfile);
8072
8073 create_all_comp_units (per_objfile);
8074
8075 /* Create a temporary address map on a temporary obstack. We later
8076 copy this to the final obstack. */
8077 auto_obstack temp_obstack;
8078
8079 scoped_restore save_psymtabs_addrmap
8080 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8081 addrmap_create_mutable (&temp_obstack));
8082
8083 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8084 {
8085 if (per_cu->v.psymtab != NULL)
8086 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8087 continue;
8088 process_psymtab_comp_unit (per_cu, per_objfile, false,
8089 language_minimal);
8090 }
8091
8092 /* This has to wait until we read the CUs, we need the list of DWOs. */
8093 process_skeletonless_type_units (per_objfile);
8094
8095 /* Now that all TUs have been processed we can fill in the dependencies. */
8096 if (per_objfile->per_bfd->type_unit_groups != NULL)
8097 {
8098 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8099 build_type_psymtab_dependencies, per_objfile);
8100 }
8101
8102 if (dwarf_read_debug > 0)
8103 print_tu_stats (per_objfile);
8104
8105 set_partial_user (per_objfile);
8106
8107 objfile->partial_symtabs->psymtabs_addrmap
8108 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8109 objfile->partial_symtabs->obstack ());
8110 /* At this point we want to keep the address map. */
8111 save_psymtabs_addrmap.release ();
8112
8113 dwarf_read_debug_printf ("Done building psymtabs of %s",
8114 objfile_name (objfile));
8115 }
8116
8117 /* Load the partial DIEs for a secondary CU into memory.
8118 This is also used when rereading a primary CU with load_all_dies. */
8119
8120 static void
8121 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8122 dwarf2_per_objfile *per_objfile,
8123 dwarf2_cu *existing_cu)
8124 {
8125 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
8126
8127 if (!reader.dummy_p)
8128 {
8129 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8130 language_minimal);
8131
8132 /* Check if comp unit has_children.
8133 If so, read the rest of the partial symbols from this comp unit.
8134 If not, there's no more debug_info for this comp unit. */
8135 if (reader.comp_unit_die->has_children)
8136 load_partial_dies (&reader, reader.info_ptr, 0);
8137
8138 reader.keep ();
8139 }
8140 }
8141
8142 static void
8143 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
8144 struct dwarf2_section_info *section,
8145 struct dwarf2_section_info *abbrev_section,
8146 unsigned int is_dwz)
8147 {
8148 const gdb_byte *info_ptr;
8149 struct objfile *objfile = per_objfile->objfile;
8150
8151 dwarf_read_debug_printf ("Reading %s for %s",
8152 section->get_name (),
8153 section->get_file_name ());
8154
8155 section->read (objfile);
8156
8157 info_ptr = section->buffer;
8158
8159 while (info_ptr < section->buffer + section->size)
8160 {
8161 struct dwarf2_per_cu_data *this_cu;
8162
8163 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8164
8165 comp_unit_head cu_header;
8166 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
8167 abbrev_section, info_ptr,
8168 rcuh_kind::COMPILE);
8169
8170 /* Save the compilation unit for later lookup. */
8171 if (cu_header.unit_type != DW_UT_type)
8172 this_cu = per_objfile->per_bfd->allocate_per_cu ();
8173 else
8174 {
8175 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
8176 sig_type->signature = cu_header.signature;
8177 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8178 this_cu = &sig_type->per_cu;
8179 }
8180 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8181 this_cu->sect_off = sect_off;
8182 this_cu->length = cu_header.length + cu_header.initial_length_size;
8183 this_cu->is_dwz = is_dwz;
8184 this_cu->section = section;
8185
8186 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8187
8188 info_ptr = info_ptr + this_cu->length;
8189 }
8190 }
8191
8192 /* Create a list of all compilation units in OBJFILE.
8193 This is only done for -readnow and building partial symtabs. */
8194
8195 static void
8196 create_all_comp_units (dwarf2_per_objfile *per_objfile)
8197 {
8198 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8199 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8200 &per_objfile->per_bfd->abbrev, 0);
8201
8202 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8203 if (dwz != NULL)
8204 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
8205 }
8206
8207 /* Process all loaded DIEs for compilation unit CU, starting at
8208 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8209 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8210 DW_AT_ranges). See the comments of add_partial_subprogram on how
8211 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8212
8213 static void
8214 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8215 CORE_ADDR *highpc, int set_addrmap,
8216 struct dwarf2_cu *cu)
8217 {
8218 struct partial_die_info *pdi;
8219
8220 /* Now, march along the PDI's, descending into ones which have
8221 interesting children but skipping the children of the other ones,
8222 until we reach the end of the compilation unit. */
8223
8224 pdi = first_die;
8225
8226 while (pdi != NULL)
8227 {
8228 pdi->fixup (cu);
8229
8230 /* Anonymous namespaces or modules have no name but have interesting
8231 children, so we need to look at them. Ditto for anonymous
8232 enums. */
8233
8234 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
8235 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8236 || pdi->tag == DW_TAG_imported_unit
8237 || pdi->tag == DW_TAG_inlined_subroutine)
8238 {
8239 switch (pdi->tag)
8240 {
8241 case DW_TAG_subprogram:
8242 case DW_TAG_inlined_subroutine:
8243 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8244 if (cu->language == language_cplus)
8245 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8246 set_addrmap, cu);
8247 break;
8248 case DW_TAG_constant:
8249 case DW_TAG_variable:
8250 case DW_TAG_typedef:
8251 case DW_TAG_union_type:
8252 if (!pdi->is_declaration
8253 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8254 {
8255 add_partial_symbol (pdi, cu);
8256 }
8257 break;
8258 case DW_TAG_class_type:
8259 case DW_TAG_interface_type:
8260 case DW_TAG_structure_type:
8261 if (!pdi->is_declaration)
8262 {
8263 add_partial_symbol (pdi, cu);
8264 }
8265 if ((cu->language == language_rust
8266 || cu->language == language_cplus) && pdi->has_children)
8267 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8268 set_addrmap, cu);
8269 break;
8270 case DW_TAG_enumeration_type:
8271 if (!pdi->is_declaration)
8272 add_partial_enumeration (pdi, cu);
8273 break;
8274 case DW_TAG_base_type:
8275 case DW_TAG_subrange_type:
8276 /* File scope base type definitions are added to the partial
8277 symbol table. */
8278 add_partial_symbol (pdi, cu);
8279 break;
8280 case DW_TAG_namespace:
8281 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8282 break;
8283 case DW_TAG_module:
8284 if (!pdi->is_declaration)
8285 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8286 break;
8287 case DW_TAG_imported_unit:
8288 {
8289 struct dwarf2_per_cu_data *per_cu;
8290
8291 /* For now we don't handle imported units in type units. */
8292 if (cu->per_cu->is_debug_types)
8293 {
8294 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8295 " supported in type units [in module %s]"),
8296 objfile_name (cu->per_objfile->objfile));
8297 }
8298
8299 per_cu = dwarf2_find_containing_comp_unit
8300 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8301
8302 /* Go read the partial unit, if needed. */
8303 if (per_cu->v.psymtab == NULL)
8304 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8305 cu->language);
8306
8307 cu->per_cu->imported_symtabs_push (per_cu);
8308 }
8309 break;
8310 case DW_TAG_imported_declaration:
8311 add_partial_symbol (pdi, cu);
8312 break;
8313 default:
8314 break;
8315 }
8316 }
8317
8318 /* If the die has a sibling, skip to the sibling. */
8319
8320 pdi = pdi->die_sibling;
8321 }
8322 }
8323
8324 /* Functions used to compute the fully scoped name of a partial DIE.
8325
8326 Normally, this is simple. For C++, the parent DIE's fully scoped
8327 name is concatenated with "::" and the partial DIE's name.
8328 Enumerators are an exception; they use the scope of their parent
8329 enumeration type, i.e. the name of the enumeration type is not
8330 prepended to the enumerator.
8331
8332 There are two complexities. One is DW_AT_specification; in this
8333 case "parent" means the parent of the target of the specification,
8334 instead of the direct parent of the DIE. The other is compilers
8335 which do not emit DW_TAG_namespace; in this case we try to guess
8336 the fully qualified name of structure types from their members'
8337 linkage names. This must be done using the DIE's children rather
8338 than the children of any DW_AT_specification target. We only need
8339 to do this for structures at the top level, i.e. if the target of
8340 any DW_AT_specification (if any; otherwise the DIE itself) does not
8341 have a parent. */
8342
8343 /* Compute the scope prefix associated with PDI's parent, in
8344 compilation unit CU. The result will be allocated on CU's
8345 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8346 field. NULL is returned if no prefix is necessary. */
8347 static const char *
8348 partial_die_parent_scope (struct partial_die_info *pdi,
8349 struct dwarf2_cu *cu)
8350 {
8351 const char *grandparent_scope;
8352 struct partial_die_info *parent, *real_pdi;
8353
8354 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8355 then this means the parent of the specification DIE. */
8356
8357 real_pdi = pdi;
8358 while (real_pdi->has_specification)
8359 {
8360 auto res = find_partial_die (real_pdi->spec_offset,
8361 real_pdi->spec_is_dwz, cu);
8362 real_pdi = res.pdi;
8363 cu = res.cu;
8364 }
8365
8366 parent = real_pdi->die_parent;
8367 if (parent == NULL)
8368 return NULL;
8369
8370 if (parent->scope_set)
8371 return parent->scope;
8372
8373 parent->fixup (cu);
8374
8375 grandparent_scope = partial_die_parent_scope (parent, cu);
8376
8377 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8378 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8379 Work around this problem here. */
8380 if (cu->language == language_cplus
8381 && parent->tag == DW_TAG_namespace
8382 && strcmp (parent->name (cu), "::") == 0
8383 && grandparent_scope == NULL)
8384 {
8385 parent->scope = NULL;
8386 parent->scope_set = 1;
8387 return NULL;
8388 }
8389
8390 /* Nested subroutines in Fortran get a prefix. */
8391 if (pdi->tag == DW_TAG_enumerator)
8392 /* Enumerators should not get the name of the enumeration as a prefix. */
8393 parent->scope = grandparent_scope;
8394 else if (parent->tag == DW_TAG_namespace
8395 || parent->tag == DW_TAG_module
8396 || parent->tag == DW_TAG_structure_type
8397 || parent->tag == DW_TAG_class_type
8398 || parent->tag == DW_TAG_interface_type
8399 || parent->tag == DW_TAG_union_type
8400 || parent->tag == DW_TAG_enumeration_type
8401 || (cu->language == language_fortran
8402 && parent->tag == DW_TAG_subprogram
8403 && pdi->tag == DW_TAG_subprogram))
8404 {
8405 if (grandparent_scope == NULL)
8406 parent->scope = parent->name (cu);
8407 else
8408 parent->scope = typename_concat (&cu->comp_unit_obstack,
8409 grandparent_scope,
8410 parent->name (cu), 0, cu);
8411 }
8412 else
8413 {
8414 /* FIXME drow/2004-04-01: What should we be doing with
8415 function-local names? For partial symbols, we should probably be
8416 ignoring them. */
8417 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8418 dwarf_tag_name (parent->tag),
8419 sect_offset_str (pdi->sect_off));
8420 parent->scope = grandparent_scope;
8421 }
8422
8423 parent->scope_set = 1;
8424 return parent->scope;
8425 }
8426
8427 /* Return the fully scoped name associated with PDI, from compilation unit
8428 CU. The result will be allocated with malloc. */
8429
8430 static gdb::unique_xmalloc_ptr<char>
8431 partial_die_full_name (struct partial_die_info *pdi,
8432 struct dwarf2_cu *cu)
8433 {
8434 const char *parent_scope;
8435
8436 /* If this is a template instantiation, we can not work out the
8437 template arguments from partial DIEs. So, unfortunately, we have
8438 to go through the full DIEs. At least any work we do building
8439 types here will be reused if full symbols are loaded later. */
8440 if (pdi->has_template_arguments)
8441 {
8442 pdi->fixup (cu);
8443
8444 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8445 {
8446 struct die_info *die;
8447 struct attribute attr;
8448 struct dwarf2_cu *ref_cu = cu;
8449
8450 /* DW_FORM_ref_addr is using section offset. */
8451 attr.name = (enum dwarf_attribute) 0;
8452 attr.form = DW_FORM_ref_addr;
8453 attr.u.unsnd = to_underlying (pdi->sect_off);
8454 die = follow_die_ref (NULL, &attr, &ref_cu);
8455
8456 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8457 }
8458 }
8459
8460 parent_scope = partial_die_parent_scope (pdi, cu);
8461 if (parent_scope == NULL)
8462 return NULL;
8463 else
8464 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8465 pdi->name (cu),
8466 0, cu));
8467 }
8468
8469 static void
8470 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8471 {
8472 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8473 struct objfile *objfile = per_objfile->objfile;
8474 struct gdbarch *gdbarch = objfile->arch ();
8475 CORE_ADDR addr = 0;
8476 const char *actual_name = NULL;
8477 CORE_ADDR baseaddr;
8478
8479 baseaddr = objfile->text_section_offset ();
8480
8481 gdb::unique_xmalloc_ptr<char> built_actual_name
8482 = partial_die_full_name (pdi, cu);
8483 if (built_actual_name != NULL)
8484 actual_name = built_actual_name.get ();
8485
8486 if (actual_name == NULL)
8487 actual_name = pdi->name (cu);
8488
8489 partial_symbol psymbol;
8490 memset (&psymbol, 0, sizeof (psymbol));
8491 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8492 psymbol.ginfo.section = -1;
8493
8494 /* The code below indicates that the psymbol should be installed by
8495 setting this. */
8496 gdb::optional<psymbol_placement> where;
8497
8498 switch (pdi->tag)
8499 {
8500 case DW_TAG_inlined_subroutine:
8501 case DW_TAG_subprogram:
8502 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8503 - baseaddr);
8504 if (pdi->is_external
8505 || cu->language == language_ada
8506 || (cu->language == language_fortran
8507 && pdi->die_parent != NULL
8508 && pdi->die_parent->tag == DW_TAG_subprogram))
8509 {
8510 /* Normally, only "external" DIEs are part of the global scope.
8511 But in Ada and Fortran, we want to be able to access nested
8512 procedures globally. So all Ada and Fortran subprograms are
8513 stored in the global scope. */
8514 where = psymbol_placement::GLOBAL;
8515 }
8516 else
8517 where = psymbol_placement::STATIC;
8518
8519 psymbol.domain = VAR_DOMAIN;
8520 psymbol.aclass = LOC_BLOCK;
8521 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8522 psymbol.ginfo.value.address = addr;
8523
8524 if (pdi->main_subprogram && actual_name != NULL)
8525 set_objfile_main_name (objfile, actual_name, cu->language);
8526 break;
8527 case DW_TAG_constant:
8528 psymbol.domain = VAR_DOMAIN;
8529 psymbol.aclass = LOC_STATIC;
8530 where = (pdi->is_external
8531 ? psymbol_placement::GLOBAL
8532 : psymbol_placement::STATIC);
8533 break;
8534 case DW_TAG_variable:
8535 if (pdi->d.locdesc)
8536 addr = decode_locdesc (pdi->d.locdesc, cu);
8537
8538 if (pdi->d.locdesc
8539 && addr == 0
8540 && !per_objfile->per_bfd->has_section_at_zero)
8541 {
8542 /* A global or static variable may also have been stripped
8543 out by the linker if unused, in which case its address
8544 will be nullified; do not add such variables into partial
8545 symbol table then. */
8546 }
8547 else if (pdi->is_external)
8548 {
8549 /* Global Variable.
8550 Don't enter into the minimal symbol tables as there is
8551 a minimal symbol table entry from the ELF symbols already.
8552 Enter into partial symbol table if it has a location
8553 descriptor or a type.
8554 If the location descriptor is missing, new_symbol will create
8555 a LOC_UNRESOLVED symbol, the address of the variable will then
8556 be determined from the minimal symbol table whenever the variable
8557 is referenced.
8558 The address for the partial symbol table entry is not
8559 used by GDB, but it comes in handy for debugging partial symbol
8560 table building. */
8561
8562 if (pdi->d.locdesc || pdi->has_type)
8563 {
8564 psymbol.domain = VAR_DOMAIN;
8565 psymbol.aclass = LOC_STATIC;
8566 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8567 psymbol.ginfo.value.address = addr;
8568 where = psymbol_placement::GLOBAL;
8569 }
8570 }
8571 else
8572 {
8573 int has_loc = pdi->d.locdesc != NULL;
8574
8575 /* Static Variable. Skip symbols whose value we cannot know (those
8576 without location descriptors or constant values). */
8577 if (!has_loc && !pdi->has_const_value)
8578 return;
8579
8580 psymbol.domain = VAR_DOMAIN;
8581 psymbol.aclass = LOC_STATIC;
8582 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8583 if (has_loc)
8584 psymbol.ginfo.value.address = addr;
8585 where = psymbol_placement::STATIC;
8586 }
8587 break;
8588 case DW_TAG_array_type:
8589 case DW_TAG_typedef:
8590 case DW_TAG_base_type:
8591 case DW_TAG_subrange_type:
8592 psymbol.domain = VAR_DOMAIN;
8593 psymbol.aclass = LOC_TYPEDEF;
8594 where = psymbol_placement::STATIC;
8595 break;
8596 case DW_TAG_imported_declaration:
8597 case DW_TAG_namespace:
8598 psymbol.domain = VAR_DOMAIN;
8599 psymbol.aclass = LOC_TYPEDEF;
8600 where = psymbol_placement::GLOBAL;
8601 break;
8602 case DW_TAG_module:
8603 /* With Fortran 77 there might be a "BLOCK DATA" module
8604 available without any name. If so, we skip the module as it
8605 doesn't bring any value. */
8606 if (actual_name != nullptr)
8607 {
8608 psymbol.domain = MODULE_DOMAIN;
8609 psymbol.aclass = LOC_TYPEDEF;
8610 where = psymbol_placement::GLOBAL;
8611 }
8612 break;
8613 case DW_TAG_class_type:
8614 case DW_TAG_interface_type:
8615 case DW_TAG_structure_type:
8616 case DW_TAG_union_type:
8617 case DW_TAG_enumeration_type:
8618 /* Skip external references. The DWARF standard says in the section
8619 about "Structure, Union, and Class Type Entries": "An incomplete
8620 structure, union or class type is represented by a structure,
8621 union or class entry that does not have a byte size attribute
8622 and that has a DW_AT_declaration attribute." */
8623 if (!pdi->has_byte_size && pdi->is_declaration)
8624 return;
8625
8626 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8627 static vs. global. */
8628 psymbol.domain = STRUCT_DOMAIN;
8629 psymbol.aclass = LOC_TYPEDEF;
8630 where = (cu->language == language_cplus
8631 ? psymbol_placement::GLOBAL
8632 : psymbol_placement::STATIC);
8633 break;
8634 case DW_TAG_enumerator:
8635 psymbol.domain = VAR_DOMAIN;
8636 psymbol.aclass = LOC_CONST;
8637 where = (cu->language == language_cplus
8638 ? psymbol_placement::GLOBAL
8639 : psymbol_placement::STATIC);
8640 break;
8641 default:
8642 break;
8643 }
8644
8645 if (where.has_value ())
8646 {
8647 if (built_actual_name != nullptr)
8648 actual_name = objfile->intern (actual_name);
8649 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8650 psymbol.ginfo.set_linkage_name (actual_name);
8651 else
8652 {
8653 psymbol.ginfo.set_demangled_name (actual_name,
8654 &objfile->objfile_obstack);
8655 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8656 }
8657 cu->per_cu->v.psymtab->add_psymbol (psymbol, *where, objfile);
8658 }
8659 }
8660
8661 /* Read a partial die corresponding to a namespace; also, add a symbol
8662 corresponding to that namespace to the symbol table. NAMESPACE is
8663 the name of the enclosing namespace. */
8664
8665 static void
8666 add_partial_namespace (struct partial_die_info *pdi,
8667 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8668 int set_addrmap, struct dwarf2_cu *cu)
8669 {
8670 /* Add a symbol for the namespace. */
8671
8672 add_partial_symbol (pdi, cu);
8673
8674 /* Now scan partial symbols in that namespace. */
8675
8676 if (pdi->has_children)
8677 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8678 }
8679
8680 /* Read a partial die corresponding to a Fortran module. */
8681
8682 static void
8683 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8684 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8685 {
8686 /* Add a symbol for the namespace. */
8687
8688 add_partial_symbol (pdi, cu);
8689
8690 /* Now scan partial symbols in that module. */
8691
8692 if (pdi->has_children)
8693 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8694 }
8695
8696 /* Read a partial die corresponding to a subprogram or an inlined
8697 subprogram and create a partial symbol for that subprogram.
8698 When the CU language allows it, this routine also defines a partial
8699 symbol for each nested subprogram that this subprogram contains.
8700 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8701 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8702
8703 PDI may also be a lexical block, in which case we simply search
8704 recursively for subprograms defined inside that lexical block.
8705 Again, this is only performed when the CU language allows this
8706 type of definitions. */
8707
8708 static void
8709 add_partial_subprogram (struct partial_die_info *pdi,
8710 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8711 int set_addrmap, struct dwarf2_cu *cu)
8712 {
8713 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8714 {
8715 if (pdi->has_pc_info)
8716 {
8717 if (pdi->lowpc < *lowpc)
8718 *lowpc = pdi->lowpc;
8719 if (pdi->highpc > *highpc)
8720 *highpc = pdi->highpc;
8721 if (set_addrmap)
8722 {
8723 struct objfile *objfile = cu->per_objfile->objfile;
8724 struct gdbarch *gdbarch = objfile->arch ();
8725 CORE_ADDR baseaddr;
8726 CORE_ADDR this_highpc;
8727 CORE_ADDR this_lowpc;
8728
8729 baseaddr = objfile->text_section_offset ();
8730 this_lowpc
8731 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8732 pdi->lowpc + baseaddr)
8733 - baseaddr);
8734 this_highpc
8735 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8736 pdi->highpc + baseaddr)
8737 - baseaddr);
8738 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8739 this_lowpc, this_highpc - 1,
8740 cu->per_cu->v.psymtab);
8741 }
8742 }
8743
8744 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8745 {
8746 if (!pdi->is_declaration)
8747 /* Ignore subprogram DIEs that do not have a name, they are
8748 illegal. Do not emit a complaint at this point, we will
8749 do so when we convert this psymtab into a symtab. */
8750 if (pdi->name (cu))
8751 add_partial_symbol (pdi, cu);
8752 }
8753 }
8754
8755 if (! pdi->has_children)
8756 return;
8757
8758 if (cu->language == language_ada || cu->language == language_fortran)
8759 {
8760 pdi = pdi->die_child;
8761 while (pdi != NULL)
8762 {
8763 pdi->fixup (cu);
8764 if (pdi->tag == DW_TAG_subprogram
8765 || pdi->tag == DW_TAG_inlined_subroutine
8766 || pdi->tag == DW_TAG_lexical_block)
8767 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8768 pdi = pdi->die_sibling;
8769 }
8770 }
8771 }
8772
8773 /* Read a partial die corresponding to an enumeration type. */
8774
8775 static void
8776 add_partial_enumeration (struct partial_die_info *enum_pdi,
8777 struct dwarf2_cu *cu)
8778 {
8779 struct partial_die_info *pdi;
8780
8781 if (enum_pdi->name (cu) != NULL)
8782 add_partial_symbol (enum_pdi, cu);
8783
8784 pdi = enum_pdi->die_child;
8785 while (pdi)
8786 {
8787 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8788 complaint (_("malformed enumerator DIE ignored"));
8789 else
8790 add_partial_symbol (pdi, cu);
8791 pdi = pdi->die_sibling;
8792 }
8793 }
8794
8795 /* Return the initial uleb128 in the die at INFO_PTR. */
8796
8797 static unsigned int
8798 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8799 {
8800 unsigned int bytes_read;
8801
8802 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8803 }
8804
8805 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8806 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8807
8808 Return the corresponding abbrev, or NULL if the number is zero (indicating
8809 an empty DIE). In either case *BYTES_READ will be set to the length of
8810 the initial number. */
8811
8812 static struct abbrev_info *
8813 peek_die_abbrev (const die_reader_specs &reader,
8814 const gdb_byte *info_ptr, unsigned int *bytes_read)
8815 {
8816 dwarf2_cu *cu = reader.cu;
8817 bfd *abfd = cu->per_objfile->objfile->obfd;
8818 unsigned int abbrev_number
8819 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8820
8821 if (abbrev_number == 0)
8822 return NULL;
8823
8824 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8825 if (!abbrev)
8826 {
8827 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8828 " at offset %s [in module %s]"),
8829 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8830 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8831 }
8832
8833 return abbrev;
8834 }
8835
8836 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8837 Returns a pointer to the end of a series of DIEs, terminated by an empty
8838 DIE. Any children of the skipped DIEs will also be skipped. */
8839
8840 static const gdb_byte *
8841 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8842 {
8843 while (1)
8844 {
8845 unsigned int bytes_read;
8846 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8847
8848 if (abbrev == NULL)
8849 return info_ptr + bytes_read;
8850 else
8851 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8852 }
8853 }
8854
8855 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8856 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8857 abbrev corresponding to that skipped uleb128 should be passed in
8858 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8859 children. */
8860
8861 static const gdb_byte *
8862 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8863 struct abbrev_info *abbrev)
8864 {
8865 unsigned int bytes_read;
8866 struct attribute attr;
8867 bfd *abfd = reader->abfd;
8868 struct dwarf2_cu *cu = reader->cu;
8869 const gdb_byte *buffer = reader->buffer;
8870 const gdb_byte *buffer_end = reader->buffer_end;
8871 unsigned int form, i;
8872
8873 for (i = 0; i < abbrev->num_attrs; i++)
8874 {
8875 /* The only abbrev we care about is DW_AT_sibling. */
8876 if (abbrev->attrs[i].name == DW_AT_sibling)
8877 {
8878 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8879 if (attr.form == DW_FORM_ref_addr)
8880 complaint (_("ignoring absolute DW_AT_sibling"));
8881 else
8882 {
8883 sect_offset off = attr.get_ref_die_offset ();
8884 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8885
8886 if (sibling_ptr < info_ptr)
8887 complaint (_("DW_AT_sibling points backwards"));
8888 else if (sibling_ptr > reader->buffer_end)
8889 reader->die_section->overflow_complaint ();
8890 else
8891 return sibling_ptr;
8892 }
8893 }
8894
8895 /* If it isn't DW_AT_sibling, skip this attribute. */
8896 form = abbrev->attrs[i].form;
8897 skip_attribute:
8898 switch (form)
8899 {
8900 case DW_FORM_ref_addr:
8901 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8902 and later it is offset sized. */
8903 if (cu->header.version == 2)
8904 info_ptr += cu->header.addr_size;
8905 else
8906 info_ptr += cu->header.offset_size;
8907 break;
8908 case DW_FORM_GNU_ref_alt:
8909 info_ptr += cu->header.offset_size;
8910 break;
8911 case DW_FORM_addr:
8912 info_ptr += cu->header.addr_size;
8913 break;
8914 case DW_FORM_data1:
8915 case DW_FORM_ref1:
8916 case DW_FORM_flag:
8917 case DW_FORM_strx1:
8918 info_ptr += 1;
8919 break;
8920 case DW_FORM_flag_present:
8921 case DW_FORM_implicit_const:
8922 break;
8923 case DW_FORM_data2:
8924 case DW_FORM_ref2:
8925 case DW_FORM_strx2:
8926 info_ptr += 2;
8927 break;
8928 case DW_FORM_strx3:
8929 info_ptr += 3;
8930 break;
8931 case DW_FORM_data4:
8932 case DW_FORM_ref4:
8933 case DW_FORM_strx4:
8934 info_ptr += 4;
8935 break;
8936 case DW_FORM_data8:
8937 case DW_FORM_ref8:
8938 case DW_FORM_ref_sig8:
8939 info_ptr += 8;
8940 break;
8941 case DW_FORM_data16:
8942 info_ptr += 16;
8943 break;
8944 case DW_FORM_string:
8945 read_direct_string (abfd, info_ptr, &bytes_read);
8946 info_ptr += bytes_read;
8947 break;
8948 case DW_FORM_sec_offset:
8949 case DW_FORM_strp:
8950 case DW_FORM_GNU_strp_alt:
8951 info_ptr += cu->header.offset_size;
8952 break;
8953 case DW_FORM_exprloc:
8954 case DW_FORM_block:
8955 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8956 info_ptr += bytes_read;
8957 break;
8958 case DW_FORM_block1:
8959 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8960 break;
8961 case DW_FORM_block2:
8962 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8963 break;
8964 case DW_FORM_block4:
8965 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8966 break;
8967 case DW_FORM_addrx:
8968 case DW_FORM_strx:
8969 case DW_FORM_sdata:
8970 case DW_FORM_udata:
8971 case DW_FORM_ref_udata:
8972 case DW_FORM_GNU_addr_index:
8973 case DW_FORM_GNU_str_index:
8974 case DW_FORM_rnglistx:
8975 case DW_FORM_loclistx:
8976 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8977 break;
8978 case DW_FORM_indirect:
8979 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8980 info_ptr += bytes_read;
8981 /* We need to continue parsing from here, so just go back to
8982 the top. */
8983 goto skip_attribute;
8984
8985 default:
8986 error (_("Dwarf Error: Cannot handle %s "
8987 "in DWARF reader [in module %s]"),
8988 dwarf_form_name (form),
8989 bfd_get_filename (abfd));
8990 }
8991 }
8992
8993 if (abbrev->has_children)
8994 return skip_children (reader, info_ptr);
8995 else
8996 return info_ptr;
8997 }
8998
8999 /* Locate ORIG_PDI's sibling.
9000 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9001
9002 static const gdb_byte *
9003 locate_pdi_sibling (const struct die_reader_specs *reader,
9004 struct partial_die_info *orig_pdi,
9005 const gdb_byte *info_ptr)
9006 {
9007 /* Do we know the sibling already? */
9008
9009 if (orig_pdi->sibling)
9010 return orig_pdi->sibling;
9011
9012 /* Are there any children to deal with? */
9013
9014 if (!orig_pdi->has_children)
9015 return info_ptr;
9016
9017 /* Skip the children the long way. */
9018
9019 return skip_children (reader, info_ptr);
9020 }
9021
9022 /* Expand this partial symbol table into a full symbol table. SELF is
9023 not NULL. */
9024
9025 void
9026 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9027 {
9028 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9029
9030 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
9031
9032 /* If this psymtab is constructed from a debug-only objfile, the
9033 has_section_at_zero flag will not necessarily be correct. We
9034 can get the correct value for this flag by looking at the data
9035 associated with the (presumably stripped) associated objfile. */
9036 if (objfile->separate_debug_objfile_backlink)
9037 {
9038 dwarf2_per_objfile *per_objfile_backlink
9039 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9040
9041 per_objfile->per_bfd->has_section_at_zero
9042 = per_objfile_backlink->per_bfd->has_section_at_zero;
9043 }
9044
9045 expand_psymtab (objfile);
9046
9047 process_cu_includes (per_objfile);
9048 }
9049 \f
9050 /* Reading in full CUs. */
9051
9052 /* Add PER_CU to the queue. */
9053
9054 static void
9055 queue_comp_unit (dwarf2_per_cu_data *per_cu,
9056 dwarf2_per_objfile *per_objfile,
9057 enum language pretend_language)
9058 {
9059 per_cu->queued = 1;
9060 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
9061 }
9062
9063 /* If PER_CU is not yet queued, add it to the queue.
9064 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9065 dependency.
9066 The result is non-zero if PER_CU was queued, otherwise the result is zero
9067 meaning either PER_CU is already queued or it is already loaded.
9068
9069 N.B. There is an invariant here that if a CU is queued then it is loaded.
9070 The caller is required to load PER_CU if we return non-zero. */
9071
9072 static int
9073 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9074 dwarf2_per_cu_data *per_cu,
9075 dwarf2_per_objfile *per_objfile,
9076 enum language pretend_language)
9077 {
9078 /* We may arrive here during partial symbol reading, if we need full
9079 DIEs to process an unusual case (e.g. template arguments). Do
9080 not queue PER_CU, just tell our caller to load its DIEs. */
9081 if (per_cu->per_bfd->reading_partial_symbols)
9082 {
9083 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9084
9085 if (cu == NULL || cu->dies == NULL)
9086 return 1;
9087 return 0;
9088 }
9089
9090 /* Mark the dependence relation so that we don't flush PER_CU
9091 too early. */
9092 if (dependent_cu != NULL)
9093 dwarf2_add_dependence (dependent_cu, per_cu);
9094
9095 /* If it's already on the queue, we have nothing to do. */
9096 if (per_cu->queued)
9097 return 0;
9098
9099 /* If the compilation unit is already loaded, just mark it as
9100 used. */
9101 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9102 if (cu != nullptr)
9103 {
9104 cu->last_used = 0;
9105 return 0;
9106 }
9107
9108 /* Add it to the queue. */
9109 queue_comp_unit (per_cu, per_objfile, pretend_language);
9110
9111 return 1;
9112 }
9113
9114 /* Process the queue. */
9115
9116 static void
9117 process_queue (dwarf2_per_objfile *per_objfile)
9118 {
9119 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
9120 objfile_name (per_objfile->objfile));
9121
9122 /* The queue starts out with one item, but following a DIE reference
9123 may load a new CU, adding it to the end of the queue. */
9124 while (!per_objfile->per_bfd->queue.empty ())
9125 {
9126 dwarf2_queue_item &item = per_objfile->per_bfd->queue.front ();
9127 dwarf2_per_cu_data *per_cu = item.per_cu;
9128
9129 if (!per_objfile->symtab_set_p (per_cu))
9130 {
9131 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9132
9133 /* Skip dummy CUs. */
9134 if (cu != nullptr)
9135 {
9136 unsigned int debug_print_threshold;
9137 char buf[100];
9138
9139 if (per_cu->is_debug_types)
9140 {
9141 struct signatured_type *sig_type =
9142 (struct signatured_type *) per_cu;
9143
9144 sprintf (buf, "TU %s at offset %s",
9145 hex_string (sig_type->signature),
9146 sect_offset_str (per_cu->sect_off));
9147 /* There can be 100s of TUs.
9148 Only print them in verbose mode. */
9149 debug_print_threshold = 2;
9150 }
9151 else
9152 {
9153 sprintf (buf, "CU at offset %s",
9154 sect_offset_str (per_cu->sect_off));
9155 debug_print_threshold = 1;
9156 }
9157
9158 if (dwarf_read_debug >= debug_print_threshold)
9159 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
9160
9161 if (per_cu->is_debug_types)
9162 process_full_type_unit (cu, item.pretend_language);
9163 else
9164 process_full_comp_unit (cu, item.pretend_language);
9165
9166 if (dwarf_read_debug >= debug_print_threshold)
9167 dwarf_read_debug_printf ("Done expanding %s", buf);
9168 }
9169 }
9170
9171 per_cu->queued = 0;
9172 per_objfile->per_bfd->queue.pop ();
9173 }
9174
9175 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
9176 objfile_name (per_objfile->objfile));
9177 }
9178
9179 /* Read in full symbols for PST, and anything it depends on. */
9180
9181 void
9182 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9183 {
9184 gdb_assert (!readin_p (objfile));
9185
9186 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9187 free_cached_comp_units freer (per_objfile);
9188 expand_dependencies (objfile);
9189
9190 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9191 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9192 }
9193
9194 /* See psympriv.h. */
9195
9196 bool
9197 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9198 {
9199 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9200 return per_objfile->symtab_set_p (per_cu_data);
9201 }
9202
9203 /* See psympriv.h. */
9204
9205 compunit_symtab *
9206 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9207 {
9208 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9209 return per_objfile->get_symtab (per_cu_data);
9210 }
9211
9212 /* Trivial hash function for die_info: the hash value of a DIE
9213 is its offset in .debug_info for this objfile. */
9214
9215 static hashval_t
9216 die_hash (const void *item)
9217 {
9218 const struct die_info *die = (const struct die_info *) item;
9219
9220 return to_underlying (die->sect_off);
9221 }
9222
9223 /* Trivial comparison function for die_info structures: two DIEs
9224 are equal if they have the same offset. */
9225
9226 static int
9227 die_eq (const void *item_lhs, const void *item_rhs)
9228 {
9229 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9230 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9231
9232 return die_lhs->sect_off == die_rhs->sect_off;
9233 }
9234
9235 /* Load the DIEs associated with PER_CU into memory.
9236
9237 In some cases, the caller, while reading partial symbols, will need to load
9238 the full symbols for the CU for some reason. It will already have a
9239 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9240 rather than creating a new one. */
9241
9242 static void
9243 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9244 dwarf2_per_objfile *per_objfile,
9245 dwarf2_cu *existing_cu,
9246 bool skip_partial,
9247 enum language pretend_language)
9248 {
9249 gdb_assert (! this_cu->is_debug_types);
9250
9251 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9252 if (reader.dummy_p)
9253 return;
9254
9255 struct dwarf2_cu *cu = reader.cu;
9256 const gdb_byte *info_ptr = reader.info_ptr;
9257
9258 gdb_assert (cu->die_hash == NULL);
9259 cu->die_hash =
9260 htab_create_alloc_ex (cu->header.length / 12,
9261 die_hash,
9262 die_eq,
9263 NULL,
9264 &cu->comp_unit_obstack,
9265 hashtab_obstack_allocate,
9266 dummy_obstack_deallocate);
9267
9268 if (reader.comp_unit_die->has_children)
9269 reader.comp_unit_die->child
9270 = read_die_and_siblings (&reader, reader.info_ptr,
9271 &info_ptr, reader.comp_unit_die);
9272 cu->dies = reader.comp_unit_die;
9273 /* comp_unit_die is not stored in die_hash, no need. */
9274
9275 /* We try not to read any attributes in this function, because not
9276 all CUs needed for references have been loaded yet, and symbol
9277 table processing isn't initialized. But we have to set the CU language,
9278 or we won't be able to build types correctly.
9279 Similarly, if we do not read the producer, we can not apply
9280 producer-specific interpretation. */
9281 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9282
9283 reader.keep ();
9284 }
9285
9286 /* Add a DIE to the delayed physname list. */
9287
9288 static void
9289 add_to_method_list (struct type *type, int fnfield_index, int index,
9290 const char *name, struct die_info *die,
9291 struct dwarf2_cu *cu)
9292 {
9293 struct delayed_method_info mi;
9294 mi.type = type;
9295 mi.fnfield_index = fnfield_index;
9296 mi.index = index;
9297 mi.name = name;
9298 mi.die = die;
9299 cu->method_list.push_back (mi);
9300 }
9301
9302 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9303 "const" / "volatile". If so, decrements LEN by the length of the
9304 modifier and return true. Otherwise return false. */
9305
9306 template<size_t N>
9307 static bool
9308 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9309 {
9310 size_t mod_len = sizeof (mod) - 1;
9311 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9312 {
9313 len -= mod_len;
9314 return true;
9315 }
9316 return false;
9317 }
9318
9319 /* Compute the physnames of any methods on the CU's method list.
9320
9321 The computation of method physnames is delayed in order to avoid the
9322 (bad) condition that one of the method's formal parameters is of an as yet
9323 incomplete type. */
9324
9325 static void
9326 compute_delayed_physnames (struct dwarf2_cu *cu)
9327 {
9328 /* Only C++ delays computing physnames. */
9329 if (cu->method_list.empty ())
9330 return;
9331 gdb_assert (cu->language == language_cplus);
9332
9333 for (const delayed_method_info &mi : cu->method_list)
9334 {
9335 const char *physname;
9336 struct fn_fieldlist *fn_flp
9337 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9338 physname = dwarf2_physname (mi.name, mi.die, cu);
9339 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9340 = physname ? physname : "";
9341
9342 /* Since there's no tag to indicate whether a method is a
9343 const/volatile overload, extract that information out of the
9344 demangled name. */
9345 if (physname != NULL)
9346 {
9347 size_t len = strlen (physname);
9348
9349 while (1)
9350 {
9351 if (physname[len] == ')') /* shortcut */
9352 break;
9353 else if (check_modifier (physname, len, " const"))
9354 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9355 else if (check_modifier (physname, len, " volatile"))
9356 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9357 else
9358 break;
9359 }
9360 }
9361 }
9362
9363 /* The list is no longer needed. */
9364 cu->method_list.clear ();
9365 }
9366
9367 /* Go objects should be embedded in a DW_TAG_module DIE,
9368 and it's not clear if/how imported objects will appear.
9369 To keep Go support simple until that's worked out,
9370 go back through what we've read and create something usable.
9371 We could do this while processing each DIE, and feels kinda cleaner,
9372 but that way is more invasive.
9373 This is to, for example, allow the user to type "p var" or "b main"
9374 without having to specify the package name, and allow lookups
9375 of module.object to work in contexts that use the expression
9376 parser. */
9377
9378 static void
9379 fixup_go_packaging (struct dwarf2_cu *cu)
9380 {
9381 gdb::unique_xmalloc_ptr<char> package_name;
9382 struct pending *list;
9383 int i;
9384
9385 for (list = *cu->get_builder ()->get_global_symbols ();
9386 list != NULL;
9387 list = list->next)
9388 {
9389 for (i = 0; i < list->nsyms; ++i)
9390 {
9391 struct symbol *sym = list->symbol[i];
9392
9393 if (sym->language () == language_go
9394 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9395 {
9396 gdb::unique_xmalloc_ptr<char> this_package_name
9397 (go_symbol_package_name (sym));
9398
9399 if (this_package_name == NULL)
9400 continue;
9401 if (package_name == NULL)
9402 package_name = std::move (this_package_name);
9403 else
9404 {
9405 struct objfile *objfile = cu->per_objfile->objfile;
9406 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9407 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9408 (symbol_symtab (sym) != NULL
9409 ? symtab_to_filename_for_display
9410 (symbol_symtab (sym))
9411 : objfile_name (objfile)),
9412 this_package_name.get (), package_name.get ());
9413 }
9414 }
9415 }
9416 }
9417
9418 if (package_name != NULL)
9419 {
9420 struct objfile *objfile = cu->per_objfile->objfile;
9421 const char *saved_package_name = objfile->intern (package_name.get ());
9422 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9423 saved_package_name);
9424 struct symbol *sym;
9425
9426 sym = new (&objfile->objfile_obstack) symbol;
9427 sym->set_language (language_go, &objfile->objfile_obstack);
9428 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9429 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9430 e.g., "main" finds the "main" module and not C's main(). */
9431 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9432 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9433 SYMBOL_TYPE (sym) = type;
9434
9435 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9436 }
9437 }
9438
9439 /* Allocate a fully-qualified name consisting of the two parts on the
9440 obstack. */
9441
9442 static const char *
9443 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9444 {
9445 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9446 }
9447
9448 /* A helper that allocates a variant part to attach to a Rust enum
9449 type. OBSTACK is where the results should be allocated. TYPE is
9450 the type we're processing. DISCRIMINANT_INDEX is the index of the
9451 discriminant. It must be the index of one of the fields of TYPE,
9452 or -1 to mean there is no discriminant (univariant enum).
9453 DEFAULT_INDEX is the index of the default field; or -1 if there is
9454 no default. RANGES is indexed by "effective" field number (the
9455 field index, but omitting the discriminant and default fields) and
9456 must hold the discriminant values used by the variants. Note that
9457 RANGES must have a lifetime at least as long as OBSTACK -- either
9458 already allocated on it, or static. */
9459
9460 static void
9461 alloc_rust_variant (struct obstack *obstack, struct type *type,
9462 int discriminant_index, int default_index,
9463 gdb::array_view<discriminant_range> ranges)
9464 {
9465 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9466 gdb_assert (discriminant_index == -1
9467 || (discriminant_index >= 0
9468 && discriminant_index < type->num_fields ()));
9469 gdb_assert (default_index == -1
9470 || (default_index >= 0 && default_index < type->num_fields ()));
9471
9472 /* We have one variant for each non-discriminant field. */
9473 int n_variants = type->num_fields ();
9474 if (discriminant_index != -1)
9475 --n_variants;
9476
9477 variant *variants = new (obstack) variant[n_variants];
9478 int var_idx = 0;
9479 int range_idx = 0;
9480 for (int i = 0; i < type->num_fields (); ++i)
9481 {
9482 if (i == discriminant_index)
9483 continue;
9484
9485 variants[var_idx].first_field = i;
9486 variants[var_idx].last_field = i + 1;
9487
9488 /* The default field does not need a range, but other fields do.
9489 We skipped the discriminant above. */
9490 if (i != default_index)
9491 {
9492 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9493 ++range_idx;
9494 }
9495
9496 ++var_idx;
9497 }
9498
9499 gdb_assert (range_idx == ranges.size ());
9500 gdb_assert (var_idx == n_variants);
9501
9502 variant_part *part = new (obstack) variant_part;
9503 part->discriminant_index = discriminant_index;
9504 /* If there is no discriminant, then whether it is signed is of no
9505 consequence. */
9506 part->is_unsigned
9507 = (discriminant_index == -1
9508 ? false
9509 : type->field (discriminant_index).type ()->is_unsigned ());
9510 part->variants = gdb::array_view<variant> (variants, n_variants);
9511
9512 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9513 gdb::array_view<variant_part> *prop_value
9514 = new (storage) gdb::array_view<variant_part> (part, 1);
9515
9516 struct dynamic_prop prop;
9517 prop.set_variant_parts (prop_value);
9518
9519 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9520 }
9521
9522 /* Some versions of rustc emitted enums in an unusual way.
9523
9524 Ordinary enums were emitted as unions. The first element of each
9525 structure in the union was named "RUST$ENUM$DISR". This element
9526 held the discriminant.
9527
9528 These versions of Rust also implemented the "non-zero"
9529 optimization. When the enum had two values, and one is empty and
9530 the other holds a pointer that cannot be zero, the pointer is used
9531 as the discriminant, with a zero value meaning the empty variant.
9532 Here, the union's first member is of the form
9533 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9534 where the fieldnos are the indices of the fields that should be
9535 traversed in order to find the field (which may be several fields deep)
9536 and the variantname is the name of the variant of the case when the
9537 field is zero.
9538
9539 This function recognizes whether TYPE is of one of these forms,
9540 and, if so, smashes it to be a variant type. */
9541
9542 static void
9543 quirk_rust_enum (struct type *type, struct objfile *objfile)
9544 {
9545 gdb_assert (type->code () == TYPE_CODE_UNION);
9546
9547 /* We don't need to deal with empty enums. */
9548 if (type->num_fields () == 0)
9549 return;
9550
9551 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9552 if (type->num_fields () == 1
9553 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9554 {
9555 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9556
9557 /* Decode the field name to find the offset of the
9558 discriminant. */
9559 ULONGEST bit_offset = 0;
9560 struct type *field_type = type->field (0).type ();
9561 while (name[0] >= '0' && name[0] <= '9')
9562 {
9563 char *tail;
9564 unsigned long index = strtoul (name, &tail, 10);
9565 name = tail;
9566 if (*name != '$'
9567 || index >= field_type->num_fields ()
9568 || (TYPE_FIELD_LOC_KIND (field_type, index)
9569 != FIELD_LOC_KIND_BITPOS))
9570 {
9571 complaint (_("Could not parse Rust enum encoding string \"%s\""
9572 "[in module %s]"),
9573 TYPE_FIELD_NAME (type, 0),
9574 objfile_name (objfile));
9575 return;
9576 }
9577 ++name;
9578
9579 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9580 field_type = field_type->field (index).type ();
9581 }
9582
9583 /* Smash this type to be a structure type. We have to do this
9584 because the type has already been recorded. */
9585 type->set_code (TYPE_CODE_STRUCT);
9586 type->set_num_fields (3);
9587 /* Save the field we care about. */
9588 struct field saved_field = type->field (0);
9589 type->set_fields
9590 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9591
9592 /* Put the discriminant at index 0. */
9593 type->field (0).set_type (field_type);
9594 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9595 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9596 SET_FIELD_BITPOS (type->field (0), bit_offset);
9597
9598 /* The order of fields doesn't really matter, so put the real
9599 field at index 1 and the data-less field at index 2. */
9600 type->field (1) = saved_field;
9601 TYPE_FIELD_NAME (type, 1)
9602 = rust_last_path_segment (type->field (1).type ()->name ());
9603 type->field (1).type ()->set_name
9604 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9605 TYPE_FIELD_NAME (type, 1)));
9606
9607 const char *dataless_name
9608 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9609 name);
9610 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9611 dataless_name);
9612 type->field (2).set_type (dataless_type);
9613 /* NAME points into the original discriminant name, which
9614 already has the correct lifetime. */
9615 TYPE_FIELD_NAME (type, 2) = name;
9616 SET_FIELD_BITPOS (type->field (2), 0);
9617
9618 /* Indicate that this is a variant type. */
9619 static discriminant_range ranges[1] = { { 0, 0 } };
9620 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9621 }
9622 /* A union with a single anonymous field is probably an old-style
9623 univariant enum. */
9624 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9625 {
9626 /* Smash this type to be a structure type. We have to do this
9627 because the type has already been recorded. */
9628 type->set_code (TYPE_CODE_STRUCT);
9629
9630 struct type *field_type = type->field (0).type ();
9631 const char *variant_name
9632 = rust_last_path_segment (field_type->name ());
9633 TYPE_FIELD_NAME (type, 0) = variant_name;
9634 field_type->set_name
9635 (rust_fully_qualify (&objfile->objfile_obstack,
9636 type->name (), variant_name));
9637
9638 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9639 }
9640 else
9641 {
9642 struct type *disr_type = nullptr;
9643 for (int i = 0; i < type->num_fields (); ++i)
9644 {
9645 disr_type = type->field (i).type ();
9646
9647 if (disr_type->code () != TYPE_CODE_STRUCT)
9648 {
9649 /* All fields of a true enum will be structs. */
9650 return;
9651 }
9652 else if (disr_type->num_fields () == 0)
9653 {
9654 /* Could be data-less variant, so keep going. */
9655 disr_type = nullptr;
9656 }
9657 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9658 "RUST$ENUM$DISR") != 0)
9659 {
9660 /* Not a Rust enum. */
9661 return;
9662 }
9663 else
9664 {
9665 /* Found one. */
9666 break;
9667 }
9668 }
9669
9670 /* If we got here without a discriminant, then it's probably
9671 just a union. */
9672 if (disr_type == nullptr)
9673 return;
9674
9675 /* Smash this type to be a structure type. We have to do this
9676 because the type has already been recorded. */
9677 type->set_code (TYPE_CODE_STRUCT);
9678
9679 /* Make space for the discriminant field. */
9680 struct field *disr_field = &disr_type->field (0);
9681 field *new_fields
9682 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9683 * sizeof (struct field)));
9684 memcpy (new_fields + 1, type->fields (),
9685 type->num_fields () * sizeof (struct field));
9686 type->set_fields (new_fields);
9687 type->set_num_fields (type->num_fields () + 1);
9688
9689 /* Install the discriminant at index 0 in the union. */
9690 type->field (0) = *disr_field;
9691 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9692 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9693
9694 /* We need a way to find the correct discriminant given a
9695 variant name. For convenience we build a map here. */
9696 struct type *enum_type = disr_field->type ();
9697 std::unordered_map<std::string, ULONGEST> discriminant_map;
9698 for (int i = 0; i < enum_type->num_fields (); ++i)
9699 {
9700 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9701 {
9702 const char *name
9703 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9704 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9705 }
9706 }
9707
9708 int n_fields = type->num_fields ();
9709 /* We don't need a range entry for the discriminant, but we do
9710 need one for every other field, as there is no default
9711 variant. */
9712 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9713 discriminant_range,
9714 n_fields - 1);
9715 /* Skip the discriminant here. */
9716 for (int i = 1; i < n_fields; ++i)
9717 {
9718 /* Find the final word in the name of this variant's type.
9719 That name can be used to look up the correct
9720 discriminant. */
9721 const char *variant_name
9722 = rust_last_path_segment (type->field (i).type ()->name ());
9723
9724 auto iter = discriminant_map.find (variant_name);
9725 if (iter != discriminant_map.end ())
9726 {
9727 ranges[i - 1].low = iter->second;
9728 ranges[i - 1].high = iter->second;
9729 }
9730
9731 /* In Rust, each element should have the size of the
9732 enclosing enum. */
9733 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9734
9735 /* Remove the discriminant field, if it exists. */
9736 struct type *sub_type = type->field (i).type ();
9737 if (sub_type->num_fields () > 0)
9738 {
9739 sub_type->set_num_fields (sub_type->num_fields () - 1);
9740 sub_type->set_fields (sub_type->fields () + 1);
9741 }
9742 TYPE_FIELD_NAME (type, i) = variant_name;
9743 sub_type->set_name
9744 (rust_fully_qualify (&objfile->objfile_obstack,
9745 type->name (), variant_name));
9746 }
9747
9748 /* Indicate that this is a variant type. */
9749 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9750 gdb::array_view<discriminant_range> (ranges,
9751 n_fields - 1));
9752 }
9753 }
9754
9755 /* Rewrite some Rust unions to be structures with variants parts. */
9756
9757 static void
9758 rust_union_quirks (struct dwarf2_cu *cu)
9759 {
9760 gdb_assert (cu->language == language_rust);
9761 for (type *type_ : cu->rust_unions)
9762 quirk_rust_enum (type_, cu->per_objfile->objfile);
9763 /* We don't need this any more. */
9764 cu->rust_unions.clear ();
9765 }
9766
9767 /* See read.h. */
9768
9769 type_unit_group_unshareable *
9770 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9771 {
9772 auto iter = this->m_type_units.find (tu_group);
9773 if (iter != this->m_type_units.end ())
9774 return iter->second.get ();
9775
9776 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9777 type_unit_group_unshareable *result = uniq.get ();
9778 this->m_type_units[tu_group] = std::move (uniq);
9779 return result;
9780 }
9781
9782 struct type *
9783 dwarf2_per_objfile::get_type_for_signatured_type
9784 (signatured_type *sig_type) const
9785 {
9786 auto iter = this->m_type_map.find (sig_type);
9787 if (iter == this->m_type_map.end ())
9788 return nullptr;
9789
9790 return iter->second;
9791 }
9792
9793 void dwarf2_per_objfile::set_type_for_signatured_type
9794 (signatured_type *sig_type, struct type *type)
9795 {
9796 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9797
9798 this->m_type_map[sig_type] = type;
9799 }
9800
9801 /* A helper function for computing the list of all symbol tables
9802 included by PER_CU. */
9803
9804 static void
9805 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9806 htab_t all_children, htab_t all_type_symtabs,
9807 dwarf2_per_cu_data *per_cu,
9808 dwarf2_per_objfile *per_objfile,
9809 struct compunit_symtab *immediate_parent)
9810 {
9811 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9812 if (*slot != NULL)
9813 {
9814 /* This inclusion and its children have been processed. */
9815 return;
9816 }
9817
9818 *slot = per_cu;
9819
9820 /* Only add a CU if it has a symbol table. */
9821 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9822 if (cust != NULL)
9823 {
9824 /* If this is a type unit only add its symbol table if we haven't
9825 seen it yet (type unit per_cu's can share symtabs). */
9826 if (per_cu->is_debug_types)
9827 {
9828 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9829 if (*slot == NULL)
9830 {
9831 *slot = cust;
9832 result->push_back (cust);
9833 if (cust->user == NULL)
9834 cust->user = immediate_parent;
9835 }
9836 }
9837 else
9838 {
9839 result->push_back (cust);
9840 if (cust->user == NULL)
9841 cust->user = immediate_parent;
9842 }
9843 }
9844
9845 if (!per_cu->imported_symtabs_empty ())
9846 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9847 {
9848 recursively_compute_inclusions (result, all_children,
9849 all_type_symtabs, ptr, per_objfile,
9850 cust);
9851 }
9852 }
9853
9854 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9855 PER_CU. */
9856
9857 static void
9858 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9859 dwarf2_per_objfile *per_objfile)
9860 {
9861 gdb_assert (! per_cu->is_debug_types);
9862
9863 if (!per_cu->imported_symtabs_empty ())
9864 {
9865 int len;
9866 std::vector<compunit_symtab *> result_symtabs;
9867 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9868
9869 /* If we don't have a symtab, we can just skip this case. */
9870 if (cust == NULL)
9871 return;
9872
9873 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9874 htab_eq_pointer,
9875 NULL, xcalloc, xfree));
9876 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9877 htab_eq_pointer,
9878 NULL, xcalloc, xfree));
9879
9880 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9881 {
9882 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9883 all_type_symtabs.get (), ptr,
9884 per_objfile, cust);
9885 }
9886
9887 /* Now we have a transitive closure of all the included symtabs. */
9888 len = result_symtabs.size ();
9889 cust->includes
9890 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9891 struct compunit_symtab *, len + 1);
9892 memcpy (cust->includes, result_symtabs.data (),
9893 len * sizeof (compunit_symtab *));
9894 cust->includes[len] = NULL;
9895 }
9896 }
9897
9898 /* Compute the 'includes' field for the symtabs of all the CUs we just
9899 read. */
9900
9901 static void
9902 process_cu_includes (dwarf2_per_objfile *per_objfile)
9903 {
9904 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9905 {
9906 if (! iter->is_debug_types)
9907 compute_compunit_symtab_includes (iter, per_objfile);
9908 }
9909
9910 per_objfile->per_bfd->just_read_cus.clear ();
9911 }
9912
9913 /* Generate full symbol information for CU, whose DIEs have
9914 already been loaded into memory. */
9915
9916 static void
9917 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9918 {
9919 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9920 struct objfile *objfile = per_objfile->objfile;
9921 struct gdbarch *gdbarch = objfile->arch ();
9922 CORE_ADDR lowpc, highpc;
9923 struct compunit_symtab *cust;
9924 CORE_ADDR baseaddr;
9925 struct block *static_block;
9926 CORE_ADDR addr;
9927
9928 baseaddr = objfile->text_section_offset ();
9929
9930 /* Clear the list here in case something was left over. */
9931 cu->method_list.clear ();
9932
9933 cu->language = pretend_language;
9934 cu->language_defn = language_def (cu->language);
9935
9936 dwarf2_find_base_address (cu->dies, cu);
9937
9938 /* Do line number decoding in read_file_scope () */
9939 process_die (cu->dies, cu);
9940
9941 /* For now fudge the Go package. */
9942 if (cu->language == language_go)
9943 fixup_go_packaging (cu);
9944
9945 /* Now that we have processed all the DIEs in the CU, all the types
9946 should be complete, and it should now be safe to compute all of the
9947 physnames. */
9948 compute_delayed_physnames (cu);
9949
9950 if (cu->language == language_rust)
9951 rust_union_quirks (cu);
9952
9953 /* Some compilers don't define a DW_AT_high_pc attribute for the
9954 compilation unit. If the DW_AT_high_pc is missing, synthesize
9955 it, by scanning the DIE's below the compilation unit. */
9956 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9957
9958 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9959 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9960
9961 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9962 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9963 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9964 addrmap to help ensure it has an accurate map of pc values belonging to
9965 this comp unit. */
9966 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9967
9968 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9969 SECT_OFF_TEXT (objfile),
9970 0);
9971
9972 if (cust != NULL)
9973 {
9974 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9975
9976 /* Set symtab language to language from DW_AT_language. If the
9977 compilation is from a C file generated by language preprocessors, do
9978 not set the language if it was already deduced by start_subfile. */
9979 if (!(cu->language == language_c
9980 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9981 COMPUNIT_FILETABS (cust)->language = cu->language;
9982
9983 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9984 produce DW_AT_location with location lists but it can be possibly
9985 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9986 there were bugs in prologue debug info, fixed later in GCC-4.5
9987 by "unwind info for epilogues" patch (which is not directly related).
9988
9989 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9990 needed, it would be wrong due to missing DW_AT_producer there.
9991
9992 Still one can confuse GDB by using non-standard GCC compilation
9993 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9994 */
9995 if (cu->has_loclist && gcc_4_minor >= 5)
9996 cust->locations_valid = 1;
9997
9998 if (gcc_4_minor >= 5)
9999 cust->epilogue_unwind_valid = 1;
10000
10001 cust->call_site_htab = cu->call_site_htab;
10002 }
10003
10004 per_objfile->set_symtab (cu->per_cu, cust);
10005
10006 /* Push it for inclusion processing later. */
10007 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
10008
10009 /* Not needed any more. */
10010 cu->reset_builder ();
10011 }
10012
10013 /* Generate full symbol information for type unit CU, whose DIEs have
10014 already been loaded into memory. */
10015
10016 static void
10017 process_full_type_unit (dwarf2_cu *cu,
10018 enum language pretend_language)
10019 {
10020 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10021 struct objfile *objfile = per_objfile->objfile;
10022 struct compunit_symtab *cust;
10023 struct signatured_type *sig_type;
10024
10025 gdb_assert (cu->per_cu->is_debug_types);
10026 sig_type = (struct signatured_type *) cu->per_cu;
10027
10028 /* Clear the list here in case something was left over. */
10029 cu->method_list.clear ();
10030
10031 cu->language = pretend_language;
10032 cu->language_defn = language_def (cu->language);
10033
10034 /* The symbol tables are set up in read_type_unit_scope. */
10035 process_die (cu->dies, cu);
10036
10037 /* For now fudge the Go package. */
10038 if (cu->language == language_go)
10039 fixup_go_packaging (cu);
10040
10041 /* Now that we have processed all the DIEs in the CU, all the types
10042 should be complete, and it should now be safe to compute all of the
10043 physnames. */
10044 compute_delayed_physnames (cu);
10045
10046 if (cu->language == language_rust)
10047 rust_union_quirks (cu);
10048
10049 /* TUs share symbol tables.
10050 If this is the first TU to use this symtab, complete the construction
10051 of it with end_expandable_symtab. Otherwise, complete the addition of
10052 this TU's symbols to the existing symtab. */
10053 type_unit_group_unshareable *tug_unshare =
10054 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
10055 if (tug_unshare->compunit_symtab == NULL)
10056 {
10057 buildsym_compunit *builder = cu->get_builder ();
10058 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10059 tug_unshare->compunit_symtab = cust;
10060
10061 if (cust != NULL)
10062 {
10063 /* Set symtab language to language from DW_AT_language. If the
10064 compilation is from a C file generated by language preprocessors,
10065 do not set the language if it was already deduced by
10066 start_subfile. */
10067 if (!(cu->language == language_c
10068 && COMPUNIT_FILETABS (cust)->language != language_c))
10069 COMPUNIT_FILETABS (cust)->language = cu->language;
10070 }
10071 }
10072 else
10073 {
10074 cu->get_builder ()->augment_type_symtab ();
10075 cust = tug_unshare->compunit_symtab;
10076 }
10077
10078 per_objfile->set_symtab (cu->per_cu, cust);
10079
10080 /* Not needed any more. */
10081 cu->reset_builder ();
10082 }
10083
10084 /* Process an imported unit DIE. */
10085
10086 static void
10087 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10088 {
10089 struct attribute *attr;
10090
10091 /* For now we don't handle imported units in type units. */
10092 if (cu->per_cu->is_debug_types)
10093 {
10094 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10095 " supported in type units [in module %s]"),
10096 objfile_name (cu->per_objfile->objfile));
10097 }
10098
10099 attr = dwarf2_attr (die, DW_AT_import, cu);
10100 if (attr != NULL)
10101 {
10102 sect_offset sect_off = attr->get_ref_die_offset ();
10103 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10104 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10105 dwarf2_per_cu_data *per_cu
10106 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
10107
10108 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10109 into another compilation unit, at root level. Regard this as a hint,
10110 and ignore it. */
10111 if (die->parent && die->parent->parent == NULL
10112 && per_cu->unit_type == DW_UT_compile
10113 && per_cu->lang == language_cplus)
10114 return;
10115
10116 /* If necessary, add it to the queue and load its DIEs. */
10117 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
10118 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10119 false, cu->language);
10120
10121 cu->per_cu->imported_symtabs_push (per_cu);
10122 }
10123 }
10124
10125 /* RAII object that represents a process_die scope: i.e.,
10126 starts/finishes processing a DIE. */
10127 class process_die_scope
10128 {
10129 public:
10130 process_die_scope (die_info *die, dwarf2_cu *cu)
10131 : m_die (die), m_cu (cu)
10132 {
10133 /* We should only be processing DIEs not already in process. */
10134 gdb_assert (!m_die->in_process);
10135 m_die->in_process = true;
10136 }
10137
10138 ~process_die_scope ()
10139 {
10140 m_die->in_process = false;
10141
10142 /* If we're done processing the DIE for the CU that owns the line
10143 header, we don't need the line header anymore. */
10144 if (m_cu->line_header_die_owner == m_die)
10145 {
10146 delete m_cu->line_header;
10147 m_cu->line_header = NULL;
10148 m_cu->line_header_die_owner = NULL;
10149 }
10150 }
10151
10152 private:
10153 die_info *m_die;
10154 dwarf2_cu *m_cu;
10155 };
10156
10157 /* Process a die and its children. */
10158
10159 static void
10160 process_die (struct die_info *die, struct dwarf2_cu *cu)
10161 {
10162 process_die_scope scope (die, cu);
10163
10164 switch (die->tag)
10165 {
10166 case DW_TAG_padding:
10167 break;
10168 case DW_TAG_compile_unit:
10169 case DW_TAG_partial_unit:
10170 read_file_scope (die, cu);
10171 break;
10172 case DW_TAG_type_unit:
10173 read_type_unit_scope (die, cu);
10174 break;
10175 case DW_TAG_subprogram:
10176 /* Nested subprograms in Fortran get a prefix. */
10177 if (cu->language == language_fortran
10178 && die->parent != NULL
10179 && die->parent->tag == DW_TAG_subprogram)
10180 cu->processing_has_namespace_info = true;
10181 /* Fall through. */
10182 case DW_TAG_inlined_subroutine:
10183 read_func_scope (die, cu);
10184 break;
10185 case DW_TAG_lexical_block:
10186 case DW_TAG_try_block:
10187 case DW_TAG_catch_block:
10188 read_lexical_block_scope (die, cu);
10189 break;
10190 case DW_TAG_call_site:
10191 case DW_TAG_GNU_call_site:
10192 read_call_site_scope (die, cu);
10193 break;
10194 case DW_TAG_class_type:
10195 case DW_TAG_interface_type:
10196 case DW_TAG_structure_type:
10197 case DW_TAG_union_type:
10198 process_structure_scope (die, cu);
10199 break;
10200 case DW_TAG_enumeration_type:
10201 process_enumeration_scope (die, cu);
10202 break;
10203
10204 /* These dies have a type, but processing them does not create
10205 a symbol or recurse to process the children. Therefore we can
10206 read them on-demand through read_type_die. */
10207 case DW_TAG_subroutine_type:
10208 case DW_TAG_set_type:
10209 case DW_TAG_pointer_type:
10210 case DW_TAG_ptr_to_member_type:
10211 case DW_TAG_reference_type:
10212 case DW_TAG_rvalue_reference_type:
10213 case DW_TAG_string_type:
10214 break;
10215
10216 case DW_TAG_array_type:
10217 /* We only need to handle this case for Ada -- in other
10218 languages, it's normal for the compiler to emit a typedef
10219 instead. */
10220 if (cu->language != language_ada)
10221 break;
10222 /* FALLTHROUGH */
10223 case DW_TAG_base_type:
10224 case DW_TAG_subrange_type:
10225 case DW_TAG_typedef:
10226 /* Add a typedef symbol for the type definition, if it has a
10227 DW_AT_name. */
10228 new_symbol (die, read_type_die (die, cu), cu);
10229 break;
10230 case DW_TAG_common_block:
10231 read_common_block (die, cu);
10232 break;
10233 case DW_TAG_common_inclusion:
10234 break;
10235 case DW_TAG_namespace:
10236 cu->processing_has_namespace_info = true;
10237 read_namespace (die, cu);
10238 break;
10239 case DW_TAG_module:
10240 cu->processing_has_namespace_info = true;
10241 read_module (die, cu);
10242 break;
10243 case DW_TAG_imported_declaration:
10244 cu->processing_has_namespace_info = true;
10245 if (read_namespace_alias (die, cu))
10246 break;
10247 /* The declaration is not a global namespace alias. */
10248 /* Fall through. */
10249 case DW_TAG_imported_module:
10250 cu->processing_has_namespace_info = true;
10251 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10252 || cu->language != language_fortran))
10253 complaint (_("Tag '%s' has unexpected children"),
10254 dwarf_tag_name (die->tag));
10255 read_import_statement (die, cu);
10256 break;
10257
10258 case DW_TAG_imported_unit:
10259 process_imported_unit_die (die, cu);
10260 break;
10261
10262 case DW_TAG_variable:
10263 read_variable (die, cu);
10264 break;
10265
10266 default:
10267 new_symbol (die, NULL, cu);
10268 break;
10269 }
10270 }
10271 \f
10272 /* DWARF name computation. */
10273
10274 /* A helper function for dwarf2_compute_name which determines whether DIE
10275 needs to have the name of the scope prepended to the name listed in the
10276 die. */
10277
10278 static int
10279 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10280 {
10281 struct attribute *attr;
10282
10283 switch (die->tag)
10284 {
10285 case DW_TAG_namespace:
10286 case DW_TAG_typedef:
10287 case DW_TAG_class_type:
10288 case DW_TAG_interface_type:
10289 case DW_TAG_structure_type:
10290 case DW_TAG_union_type:
10291 case DW_TAG_enumeration_type:
10292 case DW_TAG_enumerator:
10293 case DW_TAG_subprogram:
10294 case DW_TAG_inlined_subroutine:
10295 case DW_TAG_member:
10296 case DW_TAG_imported_declaration:
10297 return 1;
10298
10299 case DW_TAG_variable:
10300 case DW_TAG_constant:
10301 /* We only need to prefix "globally" visible variables. These include
10302 any variable marked with DW_AT_external or any variable that
10303 lives in a namespace. [Variables in anonymous namespaces
10304 require prefixing, but they are not DW_AT_external.] */
10305
10306 if (dwarf2_attr (die, DW_AT_specification, cu))
10307 {
10308 struct dwarf2_cu *spec_cu = cu;
10309
10310 return die_needs_namespace (die_specification (die, &spec_cu),
10311 spec_cu);
10312 }
10313
10314 attr = dwarf2_attr (die, DW_AT_external, cu);
10315 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10316 && die->parent->tag != DW_TAG_module)
10317 return 0;
10318 /* A variable in a lexical block of some kind does not need a
10319 namespace, even though in C++ such variables may be external
10320 and have a mangled name. */
10321 if (die->parent->tag == DW_TAG_lexical_block
10322 || die->parent->tag == DW_TAG_try_block
10323 || die->parent->tag == DW_TAG_catch_block
10324 || die->parent->tag == DW_TAG_subprogram)
10325 return 0;
10326 return 1;
10327
10328 default:
10329 return 0;
10330 }
10331 }
10332
10333 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10334 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10335 defined for the given DIE. */
10336
10337 static struct attribute *
10338 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10339 {
10340 struct attribute *attr;
10341
10342 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10343 if (attr == NULL)
10344 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10345
10346 return attr;
10347 }
10348
10349 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10350 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10351 defined for the given DIE. */
10352
10353 static const char *
10354 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10355 {
10356 const char *linkage_name;
10357
10358 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10359 if (linkage_name == NULL)
10360 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10361
10362 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10363 See https://github.com/rust-lang/rust/issues/32925. */
10364 if (cu->language == language_rust && linkage_name != NULL
10365 && strchr (linkage_name, '{') != NULL)
10366 linkage_name = NULL;
10367
10368 return linkage_name;
10369 }
10370
10371 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10372 compute the physname for the object, which include a method's:
10373 - formal parameters (C++),
10374 - receiver type (Go),
10375
10376 The term "physname" is a bit confusing.
10377 For C++, for example, it is the demangled name.
10378 For Go, for example, it's the mangled name.
10379
10380 For Ada, return the DIE's linkage name rather than the fully qualified
10381 name. PHYSNAME is ignored..
10382
10383 The result is allocated on the objfile->per_bfd's obstack and
10384 canonicalized. */
10385
10386 static const char *
10387 dwarf2_compute_name (const char *name,
10388 struct die_info *die, struct dwarf2_cu *cu,
10389 int physname)
10390 {
10391 struct objfile *objfile = cu->per_objfile->objfile;
10392
10393 if (name == NULL)
10394 name = dwarf2_name (die, cu);
10395
10396 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10397 but otherwise compute it by typename_concat inside GDB.
10398 FIXME: Actually this is not really true, or at least not always true.
10399 It's all very confusing. compute_and_set_names doesn't try to demangle
10400 Fortran names because there is no mangling standard. So new_symbol
10401 will set the demangled name to the result of dwarf2_full_name, and it is
10402 the demangled name that GDB uses if it exists. */
10403 if (cu->language == language_ada
10404 || (cu->language == language_fortran && physname))
10405 {
10406 /* For Ada unit, we prefer the linkage name over the name, as
10407 the former contains the exported name, which the user expects
10408 to be able to reference. Ideally, we want the user to be able
10409 to reference this entity using either natural or linkage name,
10410 but we haven't started looking at this enhancement yet. */
10411 const char *linkage_name = dw2_linkage_name (die, cu);
10412
10413 if (linkage_name != NULL)
10414 return linkage_name;
10415 }
10416
10417 /* These are the only languages we know how to qualify names in. */
10418 if (name != NULL
10419 && (cu->language == language_cplus
10420 || cu->language == language_fortran || cu->language == language_d
10421 || cu->language == language_rust))
10422 {
10423 if (die_needs_namespace (die, cu))
10424 {
10425 const char *prefix;
10426 const char *canonical_name = NULL;
10427
10428 string_file buf;
10429
10430 prefix = determine_prefix (die, cu);
10431 if (*prefix != '\0')
10432 {
10433 gdb::unique_xmalloc_ptr<char> prefixed_name
10434 (typename_concat (NULL, prefix, name, physname, cu));
10435
10436 buf.puts (prefixed_name.get ());
10437 }
10438 else
10439 buf.puts (name);
10440
10441 /* Template parameters may be specified in the DIE's DW_AT_name, or
10442 as children with DW_TAG_template_type_param or
10443 DW_TAG_value_type_param. If the latter, add them to the name
10444 here. If the name already has template parameters, then
10445 skip this step; some versions of GCC emit both, and
10446 it is more efficient to use the pre-computed name.
10447
10448 Something to keep in mind about this process: it is very
10449 unlikely, or in some cases downright impossible, to produce
10450 something that will match the mangled name of a function.
10451 If the definition of the function has the same debug info,
10452 we should be able to match up with it anyway. But fallbacks
10453 using the minimal symbol, for instance to find a method
10454 implemented in a stripped copy of libstdc++, will not work.
10455 If we do not have debug info for the definition, we will have to
10456 match them up some other way.
10457
10458 When we do name matching there is a related problem with function
10459 templates; two instantiated function templates are allowed to
10460 differ only by their return types, which we do not add here. */
10461
10462 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10463 {
10464 struct attribute *attr;
10465 struct die_info *child;
10466 int first = 1;
10467
10468 die->building_fullname = 1;
10469
10470 for (child = die->child; child != NULL; child = child->sibling)
10471 {
10472 struct type *type;
10473 LONGEST value;
10474 const gdb_byte *bytes;
10475 struct dwarf2_locexpr_baton *baton;
10476 struct value *v;
10477
10478 if (child->tag != DW_TAG_template_type_param
10479 && child->tag != DW_TAG_template_value_param)
10480 continue;
10481
10482 if (first)
10483 {
10484 buf.puts ("<");
10485 first = 0;
10486 }
10487 else
10488 buf.puts (", ");
10489
10490 attr = dwarf2_attr (child, DW_AT_type, cu);
10491 if (attr == NULL)
10492 {
10493 complaint (_("template parameter missing DW_AT_type"));
10494 buf.puts ("UNKNOWN_TYPE");
10495 continue;
10496 }
10497 type = die_type (child, cu);
10498
10499 if (child->tag == DW_TAG_template_type_param)
10500 {
10501 c_print_type (type, "", &buf, -1, 0, cu->language,
10502 &type_print_raw_options);
10503 continue;
10504 }
10505
10506 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10507 if (attr == NULL)
10508 {
10509 complaint (_("template parameter missing "
10510 "DW_AT_const_value"));
10511 buf.puts ("UNKNOWN_VALUE");
10512 continue;
10513 }
10514
10515 dwarf2_const_value_attr (attr, type, name,
10516 &cu->comp_unit_obstack, cu,
10517 &value, &bytes, &baton);
10518
10519 if (type->has_no_signedness ())
10520 /* GDB prints characters as NUMBER 'CHAR'. If that's
10521 changed, this can use value_print instead. */
10522 c_printchar (value, type, &buf);
10523 else
10524 {
10525 struct value_print_options opts;
10526
10527 if (baton != NULL)
10528 v = dwarf2_evaluate_loc_desc (type, NULL,
10529 baton->data,
10530 baton->size,
10531 baton->per_cu,
10532 baton->per_objfile);
10533 else if (bytes != NULL)
10534 {
10535 v = allocate_value (type);
10536 memcpy (value_contents_writeable (v), bytes,
10537 TYPE_LENGTH (type));
10538 }
10539 else
10540 v = value_from_longest (type, value);
10541
10542 /* Specify decimal so that we do not depend on
10543 the radix. */
10544 get_formatted_print_options (&opts, 'd');
10545 opts.raw = 1;
10546 value_print (v, &buf, &opts);
10547 release_value (v);
10548 }
10549 }
10550
10551 die->building_fullname = 0;
10552
10553 if (!first)
10554 {
10555 /* Close the argument list, with a space if necessary
10556 (nested templates). */
10557 if (!buf.empty () && buf.string ().back () == '>')
10558 buf.puts (" >");
10559 else
10560 buf.puts (">");
10561 }
10562 }
10563
10564 /* For C++ methods, append formal parameter type
10565 information, if PHYSNAME. */
10566
10567 if (physname && die->tag == DW_TAG_subprogram
10568 && cu->language == language_cplus)
10569 {
10570 struct type *type = read_type_die (die, cu);
10571
10572 c_type_print_args (type, &buf, 1, cu->language,
10573 &type_print_raw_options);
10574
10575 if (cu->language == language_cplus)
10576 {
10577 /* Assume that an artificial first parameter is
10578 "this", but do not crash if it is not. RealView
10579 marks unnamed (and thus unused) parameters as
10580 artificial; there is no way to differentiate
10581 the two cases. */
10582 if (type->num_fields () > 0
10583 && TYPE_FIELD_ARTIFICIAL (type, 0)
10584 && type->field (0).type ()->code () == TYPE_CODE_PTR
10585 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10586 buf.puts (" const");
10587 }
10588 }
10589
10590 const std::string &intermediate_name = buf.string ();
10591
10592 if (cu->language == language_cplus)
10593 canonical_name
10594 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10595 objfile);
10596
10597 /* If we only computed INTERMEDIATE_NAME, or if
10598 INTERMEDIATE_NAME is already canonical, then we need to
10599 intern it. */
10600 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10601 name = objfile->intern (intermediate_name);
10602 else
10603 name = canonical_name;
10604 }
10605 }
10606
10607 return name;
10608 }
10609
10610 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10611 If scope qualifiers are appropriate they will be added. The result
10612 will be allocated on the storage_obstack, or NULL if the DIE does
10613 not have a name. NAME may either be from a previous call to
10614 dwarf2_name or NULL.
10615
10616 The output string will be canonicalized (if C++). */
10617
10618 static const char *
10619 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10620 {
10621 return dwarf2_compute_name (name, die, cu, 0);
10622 }
10623
10624 /* Construct a physname for the given DIE in CU. NAME may either be
10625 from a previous call to dwarf2_name or NULL. The result will be
10626 allocated on the objfile_objstack or NULL if the DIE does not have a
10627 name.
10628
10629 The output string will be canonicalized (if C++). */
10630
10631 static const char *
10632 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10633 {
10634 struct objfile *objfile = cu->per_objfile->objfile;
10635 const char *retval, *mangled = NULL, *canon = NULL;
10636 int need_copy = 1;
10637
10638 /* In this case dwarf2_compute_name is just a shortcut not building anything
10639 on its own. */
10640 if (!die_needs_namespace (die, cu))
10641 return dwarf2_compute_name (name, die, cu, 1);
10642
10643 if (cu->language != language_rust)
10644 mangled = dw2_linkage_name (die, cu);
10645
10646 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10647 has computed. */
10648 gdb::unique_xmalloc_ptr<char> demangled;
10649 if (mangled != NULL)
10650 {
10651
10652 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
10653 {
10654 /* Do nothing (do not demangle the symbol name). */
10655 }
10656 else
10657 {
10658 /* Use DMGL_RET_DROP for C++ template functions to suppress
10659 their return type. It is easier for GDB users to search
10660 for such functions as `name(params)' than `long name(params)'.
10661 In such case the minimal symbol names do not match the full
10662 symbol names but for template functions there is never a need
10663 to look up their definition from their declaration so
10664 the only disadvantage remains the minimal symbol variant
10665 `long name(params)' does not have the proper inferior type. */
10666 demangled.reset (gdb_demangle (mangled,
10667 (DMGL_PARAMS | DMGL_ANSI
10668 | DMGL_RET_DROP)));
10669 }
10670 if (demangled)
10671 canon = demangled.get ();
10672 else
10673 {
10674 canon = mangled;
10675 need_copy = 0;
10676 }
10677 }
10678
10679 if (canon == NULL || check_physname)
10680 {
10681 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10682
10683 if (canon != NULL && strcmp (physname, canon) != 0)
10684 {
10685 /* It may not mean a bug in GDB. The compiler could also
10686 compute DW_AT_linkage_name incorrectly. But in such case
10687 GDB would need to be bug-to-bug compatible. */
10688
10689 complaint (_("Computed physname <%s> does not match demangled <%s> "
10690 "(from linkage <%s>) - DIE at %s [in module %s]"),
10691 physname, canon, mangled, sect_offset_str (die->sect_off),
10692 objfile_name (objfile));
10693
10694 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10695 is available here - over computed PHYSNAME. It is safer
10696 against both buggy GDB and buggy compilers. */
10697
10698 retval = canon;
10699 }
10700 else
10701 {
10702 retval = physname;
10703 need_copy = 0;
10704 }
10705 }
10706 else
10707 retval = canon;
10708
10709 if (need_copy)
10710 retval = objfile->intern (retval);
10711
10712 return retval;
10713 }
10714
10715 /* Inspect DIE in CU for a namespace alias. If one exists, record
10716 a new symbol for it.
10717
10718 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10719
10720 static int
10721 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10722 {
10723 struct attribute *attr;
10724
10725 /* If the die does not have a name, this is not a namespace
10726 alias. */
10727 attr = dwarf2_attr (die, DW_AT_name, cu);
10728 if (attr != NULL)
10729 {
10730 int num;
10731 struct die_info *d = die;
10732 struct dwarf2_cu *imported_cu = cu;
10733
10734 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10735 keep inspecting DIEs until we hit the underlying import. */
10736 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10737 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10738 {
10739 attr = dwarf2_attr (d, DW_AT_import, cu);
10740 if (attr == NULL)
10741 break;
10742
10743 d = follow_die_ref (d, attr, &imported_cu);
10744 if (d->tag != DW_TAG_imported_declaration)
10745 break;
10746 }
10747
10748 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10749 {
10750 complaint (_("DIE at %s has too many recursively imported "
10751 "declarations"), sect_offset_str (d->sect_off));
10752 return 0;
10753 }
10754
10755 if (attr != NULL)
10756 {
10757 struct type *type;
10758 sect_offset sect_off = attr->get_ref_die_offset ();
10759
10760 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10761 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10762 {
10763 /* This declaration is a global namespace alias. Add
10764 a symbol for it whose type is the aliased namespace. */
10765 new_symbol (die, type, cu);
10766 return 1;
10767 }
10768 }
10769 }
10770
10771 return 0;
10772 }
10773
10774 /* Return the using directives repository (global or local?) to use in the
10775 current context for CU.
10776
10777 For Ada, imported declarations can materialize renamings, which *may* be
10778 global. However it is impossible (for now?) in DWARF to distinguish
10779 "external" imported declarations and "static" ones. As all imported
10780 declarations seem to be static in all other languages, make them all CU-wide
10781 global only in Ada. */
10782
10783 static struct using_direct **
10784 using_directives (struct dwarf2_cu *cu)
10785 {
10786 if (cu->language == language_ada
10787 && cu->get_builder ()->outermost_context_p ())
10788 return cu->get_builder ()->get_global_using_directives ();
10789 else
10790 return cu->get_builder ()->get_local_using_directives ();
10791 }
10792
10793 /* Read the import statement specified by the given die and record it. */
10794
10795 static void
10796 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10797 {
10798 struct objfile *objfile = cu->per_objfile->objfile;
10799 struct attribute *import_attr;
10800 struct die_info *imported_die, *child_die;
10801 struct dwarf2_cu *imported_cu;
10802 const char *imported_name;
10803 const char *imported_name_prefix;
10804 const char *canonical_name;
10805 const char *import_alias;
10806 const char *imported_declaration = NULL;
10807 const char *import_prefix;
10808 std::vector<const char *> excludes;
10809
10810 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10811 if (import_attr == NULL)
10812 {
10813 complaint (_("Tag '%s' has no DW_AT_import"),
10814 dwarf_tag_name (die->tag));
10815 return;
10816 }
10817
10818 imported_cu = cu;
10819 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10820 imported_name = dwarf2_name (imported_die, imported_cu);
10821 if (imported_name == NULL)
10822 {
10823 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10824
10825 The import in the following code:
10826 namespace A
10827 {
10828 typedef int B;
10829 }
10830
10831 int main ()
10832 {
10833 using A::B;
10834 B b;
10835 return b;
10836 }
10837
10838 ...
10839 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10840 <52> DW_AT_decl_file : 1
10841 <53> DW_AT_decl_line : 6
10842 <54> DW_AT_import : <0x75>
10843 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10844 <59> DW_AT_name : B
10845 <5b> DW_AT_decl_file : 1
10846 <5c> DW_AT_decl_line : 2
10847 <5d> DW_AT_type : <0x6e>
10848 ...
10849 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10850 <76> DW_AT_byte_size : 4
10851 <77> DW_AT_encoding : 5 (signed)
10852
10853 imports the wrong die ( 0x75 instead of 0x58 ).
10854 This case will be ignored until the gcc bug is fixed. */
10855 return;
10856 }
10857
10858 /* Figure out the local name after import. */
10859 import_alias = dwarf2_name (die, cu);
10860
10861 /* Figure out where the statement is being imported to. */
10862 import_prefix = determine_prefix (die, cu);
10863
10864 /* Figure out what the scope of the imported die is and prepend it
10865 to the name of the imported die. */
10866 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10867
10868 if (imported_die->tag != DW_TAG_namespace
10869 && imported_die->tag != DW_TAG_module)
10870 {
10871 imported_declaration = imported_name;
10872 canonical_name = imported_name_prefix;
10873 }
10874 else if (strlen (imported_name_prefix) > 0)
10875 canonical_name = obconcat (&objfile->objfile_obstack,
10876 imported_name_prefix,
10877 (cu->language == language_d ? "." : "::"),
10878 imported_name, (char *) NULL);
10879 else
10880 canonical_name = imported_name;
10881
10882 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10883 for (child_die = die->child; child_die && child_die->tag;
10884 child_die = child_die->sibling)
10885 {
10886 /* DWARF-4: A Fortran use statement with a “rename list” may be
10887 represented by an imported module entry with an import attribute
10888 referring to the module and owned entries corresponding to those
10889 entities that are renamed as part of being imported. */
10890
10891 if (child_die->tag != DW_TAG_imported_declaration)
10892 {
10893 complaint (_("child DW_TAG_imported_declaration expected "
10894 "- DIE at %s [in module %s]"),
10895 sect_offset_str (child_die->sect_off),
10896 objfile_name (objfile));
10897 continue;
10898 }
10899
10900 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10901 if (import_attr == NULL)
10902 {
10903 complaint (_("Tag '%s' has no DW_AT_import"),
10904 dwarf_tag_name (child_die->tag));
10905 continue;
10906 }
10907
10908 imported_cu = cu;
10909 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10910 &imported_cu);
10911 imported_name = dwarf2_name (imported_die, imported_cu);
10912 if (imported_name == NULL)
10913 {
10914 complaint (_("child DW_TAG_imported_declaration has unknown "
10915 "imported name - DIE at %s [in module %s]"),
10916 sect_offset_str (child_die->sect_off),
10917 objfile_name (objfile));
10918 continue;
10919 }
10920
10921 excludes.push_back (imported_name);
10922
10923 process_die (child_die, cu);
10924 }
10925
10926 add_using_directive (using_directives (cu),
10927 import_prefix,
10928 canonical_name,
10929 import_alias,
10930 imported_declaration,
10931 excludes,
10932 0,
10933 &objfile->objfile_obstack);
10934 }
10935
10936 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10937 types, but gives them a size of zero. Starting with version 14,
10938 ICC is compatible with GCC. */
10939
10940 static bool
10941 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10942 {
10943 if (!cu->checked_producer)
10944 check_producer (cu);
10945
10946 return cu->producer_is_icc_lt_14;
10947 }
10948
10949 /* ICC generates a DW_AT_type for C void functions. This was observed on
10950 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10951 which says that void functions should not have a DW_AT_type. */
10952
10953 static bool
10954 producer_is_icc (struct dwarf2_cu *cu)
10955 {
10956 if (!cu->checked_producer)
10957 check_producer (cu);
10958
10959 return cu->producer_is_icc;
10960 }
10961
10962 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10963 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10964 this, it was first present in GCC release 4.3.0. */
10965
10966 static bool
10967 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10968 {
10969 if (!cu->checked_producer)
10970 check_producer (cu);
10971
10972 return cu->producer_is_gcc_lt_4_3;
10973 }
10974
10975 static file_and_directory
10976 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10977 {
10978 file_and_directory res;
10979
10980 /* Find the filename. Do not use dwarf2_name here, since the filename
10981 is not a source language identifier. */
10982 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10983 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10984
10985 if (res.comp_dir == NULL
10986 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10987 && IS_ABSOLUTE_PATH (res.name))
10988 {
10989 res.comp_dir_storage = ldirname (res.name);
10990 if (!res.comp_dir_storage.empty ())
10991 res.comp_dir = res.comp_dir_storage.c_str ();
10992 }
10993 if (res.comp_dir != NULL)
10994 {
10995 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10996 directory, get rid of it. */
10997 const char *cp = strchr (res.comp_dir, ':');
10998
10999 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11000 res.comp_dir = cp + 1;
11001 }
11002
11003 if (res.name == NULL)
11004 res.name = "<unknown>";
11005
11006 return res;
11007 }
11008
11009 /* Handle DW_AT_stmt_list for a compilation unit.
11010 DIE is the DW_TAG_compile_unit die for CU.
11011 COMP_DIR is the compilation directory. LOWPC is passed to
11012 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11013
11014 static void
11015 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11016 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11017 {
11018 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11019 struct attribute *attr;
11020 struct line_header line_header_local;
11021 hashval_t line_header_local_hash;
11022 void **slot;
11023 int decode_mapping;
11024
11025 gdb_assert (! cu->per_cu->is_debug_types);
11026
11027 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11028 if (attr == NULL || !attr->form_is_unsigned ())
11029 return;
11030
11031 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11032
11033 /* The line header hash table is only created if needed (it exists to
11034 prevent redundant reading of the line table for partial_units).
11035 If we're given a partial_unit, we'll need it. If we're given a
11036 compile_unit, then use the line header hash table if it's already
11037 created, but don't create one just yet. */
11038
11039 if (per_objfile->line_header_hash == NULL
11040 && die->tag == DW_TAG_partial_unit)
11041 {
11042 per_objfile->line_header_hash
11043 .reset (htab_create_alloc (127, line_header_hash_voidp,
11044 line_header_eq_voidp,
11045 free_line_header_voidp,
11046 xcalloc, xfree));
11047 }
11048
11049 line_header_local.sect_off = line_offset;
11050 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11051 line_header_local_hash = line_header_hash (&line_header_local);
11052 if (per_objfile->line_header_hash != NULL)
11053 {
11054 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11055 &line_header_local,
11056 line_header_local_hash, NO_INSERT);
11057
11058 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11059 is not present in *SLOT (since if there is something in *SLOT then
11060 it will be for a partial_unit). */
11061 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11062 {
11063 gdb_assert (*slot != NULL);
11064 cu->line_header = (struct line_header *) *slot;
11065 return;
11066 }
11067 }
11068
11069 /* dwarf_decode_line_header does not yet provide sufficient information.
11070 We always have to call also dwarf_decode_lines for it. */
11071 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11072 if (lh == NULL)
11073 return;
11074
11075 cu->line_header = lh.release ();
11076 cu->line_header_die_owner = die;
11077
11078 if (per_objfile->line_header_hash == NULL)
11079 slot = NULL;
11080 else
11081 {
11082 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11083 &line_header_local,
11084 line_header_local_hash, INSERT);
11085 gdb_assert (slot != NULL);
11086 }
11087 if (slot != NULL && *slot == NULL)
11088 {
11089 /* This newly decoded line number information unit will be owned
11090 by line_header_hash hash table. */
11091 *slot = cu->line_header;
11092 cu->line_header_die_owner = NULL;
11093 }
11094 else
11095 {
11096 /* We cannot free any current entry in (*slot) as that struct line_header
11097 may be already used by multiple CUs. Create only temporary decoded
11098 line_header for this CU - it may happen at most once for each line
11099 number information unit. And if we're not using line_header_hash
11100 then this is what we want as well. */
11101 gdb_assert (die->tag != DW_TAG_partial_unit);
11102 }
11103 decode_mapping = (die->tag != DW_TAG_partial_unit);
11104 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11105 decode_mapping);
11106
11107 }
11108
11109 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11110
11111 static void
11112 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11113 {
11114 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11115 struct objfile *objfile = per_objfile->objfile;
11116 struct gdbarch *gdbarch = objfile->arch ();
11117 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11118 CORE_ADDR highpc = ((CORE_ADDR) 0);
11119 struct attribute *attr;
11120 struct die_info *child_die;
11121 CORE_ADDR baseaddr;
11122
11123 prepare_one_comp_unit (cu, die, cu->language);
11124 baseaddr = objfile->text_section_offset ();
11125
11126 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11127
11128 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11129 from finish_block. */
11130 if (lowpc == ((CORE_ADDR) -1))
11131 lowpc = highpc;
11132 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11133
11134 file_and_directory fnd = find_file_and_directory (die, cu);
11135
11136 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11137 standardised yet. As a workaround for the language detection we fall
11138 back to the DW_AT_producer string. */
11139 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11140 cu->language = language_opencl;
11141
11142 /* Similar hack for Go. */
11143 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11144 set_cu_language (DW_LANG_Go, cu);
11145
11146 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11147
11148 /* Decode line number information if present. We do this before
11149 processing child DIEs, so that the line header table is available
11150 for DW_AT_decl_file. */
11151 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11152
11153 /* Process all dies in compilation unit. */
11154 if (die->child != NULL)
11155 {
11156 child_die = die->child;
11157 while (child_die && child_die->tag)
11158 {
11159 process_die (child_die, cu);
11160 child_die = child_die->sibling;
11161 }
11162 }
11163
11164 /* Decode macro information, if present. Dwarf 2 macro information
11165 refers to information in the line number info statement program
11166 header, so we can only read it if we've read the header
11167 successfully. */
11168 attr = dwarf2_attr (die, DW_AT_macros, cu);
11169 if (attr == NULL)
11170 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11171 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11172 {
11173 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11174 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11175
11176 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
11177 }
11178 else
11179 {
11180 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11181 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11182 {
11183 unsigned int macro_offset = attr->as_unsigned ();
11184
11185 dwarf_decode_macros (cu, macro_offset, 0);
11186 }
11187 }
11188 }
11189
11190 void
11191 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11192 {
11193 struct type_unit_group *tu_group;
11194 int first_time;
11195 struct attribute *attr;
11196 unsigned int i;
11197 struct signatured_type *sig_type;
11198
11199 gdb_assert (per_cu->is_debug_types);
11200 sig_type = (struct signatured_type *) per_cu;
11201
11202 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11203
11204 /* If we're using .gdb_index (includes -readnow) then
11205 per_cu->type_unit_group may not have been set up yet. */
11206 if (sig_type->type_unit_group == NULL)
11207 sig_type->type_unit_group = get_type_unit_group (this, attr);
11208 tu_group = sig_type->type_unit_group;
11209
11210 /* If we've already processed this stmt_list there's no real need to
11211 do it again, we could fake it and just recreate the part we need
11212 (file name,index -> symtab mapping). If data shows this optimization
11213 is useful we can do it then. */
11214 type_unit_group_unshareable *tug_unshare
11215 = per_objfile->get_type_unit_group_unshareable (tu_group);
11216 first_time = tug_unshare->compunit_symtab == NULL;
11217
11218 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11219 debug info. */
11220 line_header_up lh;
11221 if (attr != NULL && attr->form_is_unsigned ())
11222 {
11223 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11224 lh = dwarf_decode_line_header (line_offset, this);
11225 }
11226 if (lh == NULL)
11227 {
11228 if (first_time)
11229 start_symtab ("", NULL, 0);
11230 else
11231 {
11232 gdb_assert (tug_unshare->symtabs == NULL);
11233 gdb_assert (m_builder == nullptr);
11234 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11235 m_builder.reset (new struct buildsym_compunit
11236 (COMPUNIT_OBJFILE (cust), "",
11237 COMPUNIT_DIRNAME (cust),
11238 compunit_language (cust),
11239 0, cust));
11240 list_in_scope = get_builder ()->get_file_symbols ();
11241 }
11242 return;
11243 }
11244
11245 line_header = lh.release ();
11246 line_header_die_owner = die;
11247
11248 if (first_time)
11249 {
11250 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11251
11252 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11253 still initializing it, and our caller (a few levels up)
11254 process_full_type_unit still needs to know if this is the first
11255 time. */
11256
11257 tug_unshare->symtabs
11258 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11259 struct symtab *, line_header->file_names_size ());
11260
11261 auto &file_names = line_header->file_names ();
11262 for (i = 0; i < file_names.size (); ++i)
11263 {
11264 file_entry &fe = file_names[i];
11265 dwarf2_start_subfile (this, fe.name,
11266 fe.include_dir (line_header));
11267 buildsym_compunit *b = get_builder ();
11268 if (b->get_current_subfile ()->symtab == NULL)
11269 {
11270 /* NOTE: start_subfile will recognize when it's been
11271 passed a file it has already seen. So we can't
11272 assume there's a simple mapping from
11273 cu->line_header->file_names to subfiles, plus
11274 cu->line_header->file_names may contain dups. */
11275 b->get_current_subfile ()->symtab
11276 = allocate_symtab (cust, b->get_current_subfile ()->name);
11277 }
11278
11279 fe.symtab = b->get_current_subfile ()->symtab;
11280 tug_unshare->symtabs[i] = fe.symtab;
11281 }
11282 }
11283 else
11284 {
11285 gdb_assert (m_builder == nullptr);
11286 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11287 m_builder.reset (new struct buildsym_compunit
11288 (COMPUNIT_OBJFILE (cust), "",
11289 COMPUNIT_DIRNAME (cust),
11290 compunit_language (cust),
11291 0, cust));
11292 list_in_scope = get_builder ()->get_file_symbols ();
11293
11294 auto &file_names = line_header->file_names ();
11295 for (i = 0; i < file_names.size (); ++i)
11296 {
11297 file_entry &fe = file_names[i];
11298 fe.symtab = tug_unshare->symtabs[i];
11299 }
11300 }
11301
11302 /* The main symtab is allocated last. Type units don't have DW_AT_name
11303 so they don't have a "real" (so to speak) symtab anyway.
11304 There is later code that will assign the main symtab to all symbols
11305 that don't have one. We need to handle the case of a symbol with a
11306 missing symtab (DW_AT_decl_file) anyway. */
11307 }
11308
11309 /* Process DW_TAG_type_unit.
11310 For TUs we want to skip the first top level sibling if it's not the
11311 actual type being defined by this TU. In this case the first top
11312 level sibling is there to provide context only. */
11313
11314 static void
11315 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11316 {
11317 struct die_info *child_die;
11318
11319 prepare_one_comp_unit (cu, die, language_minimal);
11320
11321 /* Initialize (or reinitialize) the machinery for building symtabs.
11322 We do this before processing child DIEs, so that the line header table
11323 is available for DW_AT_decl_file. */
11324 cu->setup_type_unit_groups (die);
11325
11326 if (die->child != NULL)
11327 {
11328 child_die = die->child;
11329 while (child_die && child_die->tag)
11330 {
11331 process_die (child_die, cu);
11332 child_die = child_die->sibling;
11333 }
11334 }
11335 }
11336 \f
11337 /* DWO/DWP files.
11338
11339 http://gcc.gnu.org/wiki/DebugFission
11340 http://gcc.gnu.org/wiki/DebugFissionDWP
11341
11342 To simplify handling of both DWO files ("object" files with the DWARF info)
11343 and DWP files (a file with the DWOs packaged up into one file), we treat
11344 DWP files as having a collection of virtual DWO files. */
11345
11346 static hashval_t
11347 hash_dwo_file (const void *item)
11348 {
11349 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11350 hashval_t hash;
11351
11352 hash = htab_hash_string (dwo_file->dwo_name);
11353 if (dwo_file->comp_dir != NULL)
11354 hash += htab_hash_string (dwo_file->comp_dir);
11355 return hash;
11356 }
11357
11358 static int
11359 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11360 {
11361 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11362 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11363
11364 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11365 return 0;
11366 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11367 return lhs->comp_dir == rhs->comp_dir;
11368 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11369 }
11370
11371 /* Allocate a hash table for DWO files. */
11372
11373 static htab_up
11374 allocate_dwo_file_hash_table ()
11375 {
11376 auto delete_dwo_file = [] (void *item)
11377 {
11378 struct dwo_file *dwo_file = (struct dwo_file *) item;
11379
11380 delete dwo_file;
11381 };
11382
11383 return htab_up (htab_create_alloc (41,
11384 hash_dwo_file,
11385 eq_dwo_file,
11386 delete_dwo_file,
11387 xcalloc, xfree));
11388 }
11389
11390 /* Lookup DWO file DWO_NAME. */
11391
11392 static void **
11393 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11394 const char *dwo_name,
11395 const char *comp_dir)
11396 {
11397 struct dwo_file find_entry;
11398 void **slot;
11399
11400 if (per_objfile->per_bfd->dwo_files == NULL)
11401 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11402
11403 find_entry.dwo_name = dwo_name;
11404 find_entry.comp_dir = comp_dir;
11405 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11406 INSERT);
11407
11408 return slot;
11409 }
11410
11411 static hashval_t
11412 hash_dwo_unit (const void *item)
11413 {
11414 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11415
11416 /* This drops the top 32 bits of the id, but is ok for a hash. */
11417 return dwo_unit->signature;
11418 }
11419
11420 static int
11421 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11422 {
11423 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11424 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11425
11426 /* The signature is assumed to be unique within the DWO file.
11427 So while object file CU dwo_id's always have the value zero,
11428 that's OK, assuming each object file DWO file has only one CU,
11429 and that's the rule for now. */
11430 return lhs->signature == rhs->signature;
11431 }
11432
11433 /* Allocate a hash table for DWO CUs,TUs.
11434 There is one of these tables for each of CUs,TUs for each DWO file. */
11435
11436 static htab_up
11437 allocate_dwo_unit_table ()
11438 {
11439 /* Start out with a pretty small number.
11440 Generally DWO files contain only one CU and maybe some TUs. */
11441 return htab_up (htab_create_alloc (3,
11442 hash_dwo_unit,
11443 eq_dwo_unit,
11444 NULL, xcalloc, xfree));
11445 }
11446
11447 /* die_reader_func for create_dwo_cu. */
11448
11449 static void
11450 create_dwo_cu_reader (const struct die_reader_specs *reader,
11451 const gdb_byte *info_ptr,
11452 struct die_info *comp_unit_die,
11453 struct dwo_file *dwo_file,
11454 struct dwo_unit *dwo_unit)
11455 {
11456 struct dwarf2_cu *cu = reader->cu;
11457 sect_offset sect_off = cu->per_cu->sect_off;
11458 struct dwarf2_section_info *section = cu->per_cu->section;
11459
11460 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11461 if (!signature.has_value ())
11462 {
11463 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11464 " its dwo_id [in module %s]"),
11465 sect_offset_str (sect_off), dwo_file->dwo_name);
11466 return;
11467 }
11468
11469 dwo_unit->dwo_file = dwo_file;
11470 dwo_unit->signature = *signature;
11471 dwo_unit->section = section;
11472 dwo_unit->sect_off = sect_off;
11473 dwo_unit->length = cu->per_cu->length;
11474
11475 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11476 sect_offset_str (sect_off),
11477 hex_string (dwo_unit->signature));
11478 }
11479
11480 /* Create the dwo_units for the CUs in a DWO_FILE.
11481 Note: This function processes DWO files only, not DWP files. */
11482
11483 static void
11484 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11485 dwarf2_cu *cu, struct dwo_file &dwo_file,
11486 dwarf2_section_info &section, htab_up &cus_htab)
11487 {
11488 struct objfile *objfile = per_objfile->objfile;
11489 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11490 const gdb_byte *info_ptr, *end_ptr;
11491
11492 section.read (objfile);
11493 info_ptr = section.buffer;
11494
11495 if (info_ptr == NULL)
11496 return;
11497
11498 dwarf_read_debug_printf ("Reading %s for %s:",
11499 section.get_name (),
11500 section.get_file_name ());
11501
11502 end_ptr = info_ptr + section.size;
11503 while (info_ptr < end_ptr)
11504 {
11505 struct dwarf2_per_cu_data per_cu;
11506 struct dwo_unit read_unit {};
11507 struct dwo_unit *dwo_unit;
11508 void **slot;
11509 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11510
11511 memset (&per_cu, 0, sizeof (per_cu));
11512 per_cu.per_bfd = per_bfd;
11513 per_cu.is_debug_types = 0;
11514 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11515 per_cu.section = &section;
11516
11517 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11518 if (!reader.dummy_p)
11519 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11520 &dwo_file, &read_unit);
11521 info_ptr += per_cu.length;
11522
11523 // If the unit could not be parsed, skip it.
11524 if (read_unit.dwo_file == NULL)
11525 continue;
11526
11527 if (cus_htab == NULL)
11528 cus_htab = allocate_dwo_unit_table ();
11529
11530 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11531 struct dwo_unit);
11532 *dwo_unit = read_unit;
11533 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11534 gdb_assert (slot != NULL);
11535 if (*slot != NULL)
11536 {
11537 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11538 sect_offset dup_sect_off = dup_cu->sect_off;
11539
11540 complaint (_("debug cu entry at offset %s is duplicate to"
11541 " the entry at offset %s, signature %s"),
11542 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11543 hex_string (dwo_unit->signature));
11544 }
11545 *slot = (void *)dwo_unit;
11546 }
11547 }
11548
11549 /* DWP file .debug_{cu,tu}_index section format:
11550 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11551 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11552
11553 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11554 officially standard DWP format was published with DWARF v5 and is called
11555 Version 5. There are no versions 3 or 4.
11556
11557 DWP Version 1:
11558
11559 Both index sections have the same format, and serve to map a 64-bit
11560 signature to a set of section numbers. Each section begins with a header,
11561 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11562 indexes, and a pool of 32-bit section numbers. The index sections will be
11563 aligned at 8-byte boundaries in the file.
11564
11565 The index section header consists of:
11566
11567 V, 32 bit version number
11568 -, 32 bits unused
11569 N, 32 bit number of compilation units or type units in the index
11570 M, 32 bit number of slots in the hash table
11571
11572 Numbers are recorded using the byte order of the application binary.
11573
11574 The hash table begins at offset 16 in the section, and consists of an array
11575 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11576 order of the application binary). Unused slots in the hash table are 0.
11577 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11578
11579 The parallel table begins immediately after the hash table
11580 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11581 array of 32-bit indexes (using the byte order of the application binary),
11582 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11583 table contains a 32-bit index into the pool of section numbers. For unused
11584 hash table slots, the corresponding entry in the parallel table will be 0.
11585
11586 The pool of section numbers begins immediately following the hash table
11587 (at offset 16 + 12 * M from the beginning of the section). The pool of
11588 section numbers consists of an array of 32-bit words (using the byte order
11589 of the application binary). Each item in the array is indexed starting
11590 from 0. The hash table entry provides the index of the first section
11591 number in the set. Additional section numbers in the set follow, and the
11592 set is terminated by a 0 entry (section number 0 is not used in ELF).
11593
11594 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11595 section must be the first entry in the set, and the .debug_abbrev.dwo must
11596 be the second entry. Other members of the set may follow in any order.
11597
11598 ---
11599
11600 DWP Versions 2 and 5:
11601
11602 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11603 and the entries in the index tables are now offsets into these sections.
11604 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11605 section.
11606
11607 Index Section Contents:
11608 Header
11609 Hash Table of Signatures dwp_hash_table.hash_table
11610 Parallel Table of Indices dwp_hash_table.unit_table
11611 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11612 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11613
11614 The index section header consists of:
11615
11616 V, 32 bit version number
11617 L, 32 bit number of columns in the table of section offsets
11618 N, 32 bit number of compilation units or type units in the index
11619 M, 32 bit number of slots in the hash table
11620
11621 Numbers are recorded using the byte order of the application binary.
11622
11623 The hash table has the same format as version 1.
11624 The parallel table of indices has the same format as version 1,
11625 except that the entries are origin-1 indices into the table of sections
11626 offsets and the table of section sizes.
11627
11628 The table of offsets begins immediately following the parallel table
11629 (at offset 16 + 12 * M from the beginning of the section). The table is
11630 a two-dimensional array of 32-bit words (using the byte order of the
11631 application binary), with L columns and N+1 rows, in row-major order.
11632 Each row in the array is indexed starting from 0. The first row provides
11633 a key to the remaining rows: each column in this row provides an identifier
11634 for a debug section, and the offsets in the same column of subsequent rows
11635 refer to that section. The section identifiers for Version 2 are:
11636
11637 DW_SECT_INFO 1 .debug_info.dwo
11638 DW_SECT_TYPES 2 .debug_types.dwo
11639 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11640 DW_SECT_LINE 4 .debug_line.dwo
11641 DW_SECT_LOC 5 .debug_loc.dwo
11642 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11643 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11644 DW_SECT_MACRO 8 .debug_macro.dwo
11645
11646 The section identifiers for Version 5 are:
11647
11648 DW_SECT_INFO_V5 1 .debug_info.dwo
11649 DW_SECT_RESERVED_V5 2 --
11650 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11651 DW_SECT_LINE_V5 4 .debug_line.dwo
11652 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11653 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11654 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11655 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11656
11657 The offsets provided by the CU and TU index sections are the base offsets
11658 for the contributions made by each CU or TU to the corresponding section
11659 in the package file. Each CU and TU header contains an abbrev_offset
11660 field, used to find the abbreviations table for that CU or TU within the
11661 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11662 be interpreted as relative to the base offset given in the index section.
11663 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11664 should be interpreted as relative to the base offset for .debug_line.dwo,
11665 and offsets into other debug sections obtained from DWARF attributes should
11666 also be interpreted as relative to the corresponding base offset.
11667
11668 The table of sizes begins immediately following the table of offsets.
11669 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11670 with L columns and N rows, in row-major order. Each row in the array is
11671 indexed starting from 1 (row 0 is shared by the two tables).
11672
11673 ---
11674
11675 Hash table lookup is handled the same in version 1 and 2:
11676
11677 We assume that N and M will not exceed 2^32 - 1.
11678 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11679
11680 Given a 64-bit compilation unit signature or a type signature S, an entry
11681 in the hash table is located as follows:
11682
11683 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11684 the low-order k bits all set to 1.
11685
11686 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11687
11688 3) If the hash table entry at index H matches the signature, use that
11689 entry. If the hash table entry at index H is unused (all zeroes),
11690 terminate the search: the signature is not present in the table.
11691
11692 4) Let H = (H + H') modulo M. Repeat at Step 3.
11693
11694 Because M > N and H' and M are relatively prime, the search is guaranteed
11695 to stop at an unused slot or find the match. */
11696
11697 /* Create a hash table to map DWO IDs to their CU/TU entry in
11698 .debug_{info,types}.dwo in DWP_FILE.
11699 Returns NULL if there isn't one.
11700 Note: This function processes DWP files only, not DWO files. */
11701
11702 static struct dwp_hash_table *
11703 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11704 struct dwp_file *dwp_file, int is_debug_types)
11705 {
11706 struct objfile *objfile = per_objfile->objfile;
11707 bfd *dbfd = dwp_file->dbfd.get ();
11708 const gdb_byte *index_ptr, *index_end;
11709 struct dwarf2_section_info *index;
11710 uint32_t version, nr_columns, nr_units, nr_slots;
11711 struct dwp_hash_table *htab;
11712
11713 if (is_debug_types)
11714 index = &dwp_file->sections.tu_index;
11715 else
11716 index = &dwp_file->sections.cu_index;
11717
11718 if (index->empty ())
11719 return NULL;
11720 index->read (objfile);
11721
11722 index_ptr = index->buffer;
11723 index_end = index_ptr + index->size;
11724
11725 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11726 For now it's safe to just read 4 bytes (particularly as it's difficult to
11727 tell if you're dealing with Version 5 before you've read the version). */
11728 version = read_4_bytes (dbfd, index_ptr);
11729 index_ptr += 4;
11730 if (version == 2 || version == 5)
11731 nr_columns = read_4_bytes (dbfd, index_ptr);
11732 else
11733 nr_columns = 0;
11734 index_ptr += 4;
11735 nr_units = read_4_bytes (dbfd, index_ptr);
11736 index_ptr += 4;
11737 nr_slots = read_4_bytes (dbfd, index_ptr);
11738 index_ptr += 4;
11739
11740 if (version != 1 && version != 2 && version != 5)
11741 {
11742 error (_("Dwarf Error: unsupported DWP file version (%s)"
11743 " [in module %s]"),
11744 pulongest (version), dwp_file->name);
11745 }
11746 if (nr_slots != (nr_slots & -nr_slots))
11747 {
11748 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11749 " is not power of 2 [in module %s]"),
11750 pulongest (nr_slots), dwp_file->name);
11751 }
11752
11753 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11754 htab->version = version;
11755 htab->nr_columns = nr_columns;
11756 htab->nr_units = nr_units;
11757 htab->nr_slots = nr_slots;
11758 htab->hash_table = index_ptr;
11759 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11760
11761 /* Exit early if the table is empty. */
11762 if (nr_slots == 0 || nr_units == 0
11763 || (version == 2 && nr_columns == 0)
11764 || (version == 5 && nr_columns == 0))
11765 {
11766 /* All must be zero. */
11767 if (nr_slots != 0 || nr_units != 0
11768 || (version == 2 && nr_columns != 0)
11769 || (version == 5 && nr_columns != 0))
11770 {
11771 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11772 " all zero [in modules %s]"),
11773 dwp_file->name);
11774 }
11775 return htab;
11776 }
11777
11778 if (version == 1)
11779 {
11780 htab->section_pool.v1.indices =
11781 htab->unit_table + sizeof (uint32_t) * nr_slots;
11782 /* It's harder to decide whether the section is too small in v1.
11783 V1 is deprecated anyway so we punt. */
11784 }
11785 else if (version == 2)
11786 {
11787 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11788 int *ids = htab->section_pool.v2.section_ids;
11789 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11790 /* Reverse map for error checking. */
11791 int ids_seen[DW_SECT_MAX + 1];
11792 int i;
11793
11794 if (nr_columns < 2)
11795 {
11796 error (_("Dwarf Error: bad DWP hash table, too few columns"
11797 " in section table [in module %s]"),
11798 dwp_file->name);
11799 }
11800 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11801 {
11802 error (_("Dwarf Error: bad DWP hash table, too many columns"
11803 " in section table [in module %s]"),
11804 dwp_file->name);
11805 }
11806 memset (ids, 255, sizeof_ids);
11807 memset (ids_seen, 255, sizeof (ids_seen));
11808 for (i = 0; i < nr_columns; ++i)
11809 {
11810 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11811
11812 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11813 {
11814 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11815 " in section table [in module %s]"),
11816 id, dwp_file->name);
11817 }
11818 if (ids_seen[id] != -1)
11819 {
11820 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11821 " id %d in section table [in module %s]"),
11822 id, dwp_file->name);
11823 }
11824 ids_seen[id] = i;
11825 ids[i] = id;
11826 }
11827 /* Must have exactly one info or types section. */
11828 if (((ids_seen[DW_SECT_INFO] != -1)
11829 + (ids_seen[DW_SECT_TYPES] != -1))
11830 != 1)
11831 {
11832 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11833 " DWO info/types section [in module %s]"),
11834 dwp_file->name);
11835 }
11836 /* Must have an abbrev section. */
11837 if (ids_seen[DW_SECT_ABBREV] == -1)
11838 {
11839 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11840 " section [in module %s]"),
11841 dwp_file->name);
11842 }
11843 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11844 htab->section_pool.v2.sizes =
11845 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11846 * nr_units * nr_columns);
11847 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11848 * nr_units * nr_columns))
11849 > index_end)
11850 {
11851 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11852 " [in module %s]"),
11853 dwp_file->name);
11854 }
11855 }
11856 else /* version == 5 */
11857 {
11858 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11859 int *ids = htab->section_pool.v5.section_ids;
11860 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11861 /* Reverse map for error checking. */
11862 int ids_seen[DW_SECT_MAX_V5 + 1];
11863
11864 if (nr_columns < 2)
11865 {
11866 error (_("Dwarf Error: bad DWP hash table, too few columns"
11867 " in section table [in module %s]"),
11868 dwp_file->name);
11869 }
11870 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11871 {
11872 error (_("Dwarf Error: bad DWP hash table, too many columns"
11873 " in section table [in module %s]"),
11874 dwp_file->name);
11875 }
11876 memset (ids, 255, sizeof_ids);
11877 memset (ids_seen, 255, sizeof (ids_seen));
11878 for (int i = 0; i < nr_columns; ++i)
11879 {
11880 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11881
11882 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11883 {
11884 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11885 " in section table [in module %s]"),
11886 id, dwp_file->name);
11887 }
11888 if (ids_seen[id] != -1)
11889 {
11890 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11891 " id %d in section table [in module %s]"),
11892 id, dwp_file->name);
11893 }
11894 ids_seen[id] = i;
11895 ids[i] = id;
11896 }
11897 /* Must have seen an info section. */
11898 if (ids_seen[DW_SECT_INFO_V5] == -1)
11899 {
11900 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11901 " DWO info/types section [in module %s]"),
11902 dwp_file->name);
11903 }
11904 /* Must have an abbrev section. */
11905 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11906 {
11907 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11908 " section [in module %s]"),
11909 dwp_file->name);
11910 }
11911 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11912 htab->section_pool.v5.sizes
11913 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11914 * nr_units * nr_columns);
11915 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11916 * nr_units * nr_columns))
11917 > index_end)
11918 {
11919 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11920 " [in module %s]"),
11921 dwp_file->name);
11922 }
11923 }
11924
11925 return htab;
11926 }
11927
11928 /* Update SECTIONS with the data from SECTP.
11929
11930 This function is like the other "locate" section routines, but in
11931 this context the sections to read comes from the DWP V1 hash table,
11932 not the full ELF section table.
11933
11934 The result is non-zero for success, or zero if an error was found. */
11935
11936 static int
11937 locate_v1_virtual_dwo_sections (asection *sectp,
11938 struct virtual_v1_dwo_sections *sections)
11939 {
11940 const struct dwop_section_names *names = &dwop_section_names;
11941
11942 if (section_is_p (sectp->name, &names->abbrev_dwo))
11943 {
11944 /* There can be only one. */
11945 if (sections->abbrev.s.section != NULL)
11946 return 0;
11947 sections->abbrev.s.section = sectp;
11948 sections->abbrev.size = bfd_section_size (sectp);
11949 }
11950 else if (section_is_p (sectp->name, &names->info_dwo)
11951 || section_is_p (sectp->name, &names->types_dwo))
11952 {
11953 /* There can be only one. */
11954 if (sections->info_or_types.s.section != NULL)
11955 return 0;
11956 sections->info_or_types.s.section = sectp;
11957 sections->info_or_types.size = bfd_section_size (sectp);
11958 }
11959 else if (section_is_p (sectp->name, &names->line_dwo))
11960 {
11961 /* There can be only one. */
11962 if (sections->line.s.section != NULL)
11963 return 0;
11964 sections->line.s.section = sectp;
11965 sections->line.size = bfd_section_size (sectp);
11966 }
11967 else if (section_is_p (sectp->name, &names->loc_dwo))
11968 {
11969 /* There can be only one. */
11970 if (sections->loc.s.section != NULL)
11971 return 0;
11972 sections->loc.s.section = sectp;
11973 sections->loc.size = bfd_section_size (sectp);
11974 }
11975 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11976 {
11977 /* There can be only one. */
11978 if (sections->macinfo.s.section != NULL)
11979 return 0;
11980 sections->macinfo.s.section = sectp;
11981 sections->macinfo.size = bfd_section_size (sectp);
11982 }
11983 else if (section_is_p (sectp->name, &names->macro_dwo))
11984 {
11985 /* There can be only one. */
11986 if (sections->macro.s.section != NULL)
11987 return 0;
11988 sections->macro.s.section = sectp;
11989 sections->macro.size = bfd_section_size (sectp);
11990 }
11991 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11992 {
11993 /* There can be only one. */
11994 if (sections->str_offsets.s.section != NULL)
11995 return 0;
11996 sections->str_offsets.s.section = sectp;
11997 sections->str_offsets.size = bfd_section_size (sectp);
11998 }
11999 else
12000 {
12001 /* No other kind of section is valid. */
12002 return 0;
12003 }
12004
12005 return 1;
12006 }
12007
12008 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12009 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12010 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12011 This is for DWP version 1 files. */
12012
12013 static struct dwo_unit *
12014 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
12015 struct dwp_file *dwp_file,
12016 uint32_t unit_index,
12017 const char *comp_dir,
12018 ULONGEST signature, int is_debug_types)
12019 {
12020 const struct dwp_hash_table *dwp_htab =
12021 is_debug_types ? dwp_file->tus : dwp_file->cus;
12022 bfd *dbfd = dwp_file->dbfd.get ();
12023 const char *kind = is_debug_types ? "TU" : "CU";
12024 struct dwo_file *dwo_file;
12025 struct dwo_unit *dwo_unit;
12026 struct virtual_v1_dwo_sections sections;
12027 void **dwo_file_slot;
12028 int i;
12029
12030 gdb_assert (dwp_file->version == 1);
12031
12032 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
12033 kind, pulongest (unit_index), hex_string (signature),
12034 dwp_file->name);
12035
12036 /* Fetch the sections of this DWO unit.
12037 Put a limit on the number of sections we look for so that bad data
12038 doesn't cause us to loop forever. */
12039
12040 #define MAX_NR_V1_DWO_SECTIONS \
12041 (1 /* .debug_info or .debug_types */ \
12042 + 1 /* .debug_abbrev */ \
12043 + 1 /* .debug_line */ \
12044 + 1 /* .debug_loc */ \
12045 + 1 /* .debug_str_offsets */ \
12046 + 1 /* .debug_macro or .debug_macinfo */ \
12047 + 1 /* trailing zero */)
12048
12049 memset (&sections, 0, sizeof (sections));
12050
12051 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12052 {
12053 asection *sectp;
12054 uint32_t section_nr =
12055 read_4_bytes (dbfd,
12056 dwp_htab->section_pool.v1.indices
12057 + (unit_index + i) * sizeof (uint32_t));
12058
12059 if (section_nr == 0)
12060 break;
12061 if (section_nr >= dwp_file->num_sections)
12062 {
12063 error (_("Dwarf Error: bad DWP hash table, section number too large"
12064 " [in module %s]"),
12065 dwp_file->name);
12066 }
12067
12068 sectp = dwp_file->elf_sections[section_nr];
12069 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12070 {
12071 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12072 " [in module %s]"),
12073 dwp_file->name);
12074 }
12075 }
12076
12077 if (i < 2
12078 || sections.info_or_types.empty ()
12079 || sections.abbrev.empty ())
12080 {
12081 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12082 " [in module %s]"),
12083 dwp_file->name);
12084 }
12085 if (i == MAX_NR_V1_DWO_SECTIONS)
12086 {
12087 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12088 " [in module %s]"),
12089 dwp_file->name);
12090 }
12091
12092 /* It's easier for the rest of the code if we fake a struct dwo_file and
12093 have dwo_unit "live" in that. At least for now.
12094
12095 The DWP file can be made up of a random collection of CUs and TUs.
12096 However, for each CU + set of TUs that came from the same original DWO
12097 file, we can combine them back into a virtual DWO file to save space
12098 (fewer struct dwo_file objects to allocate). Remember that for really
12099 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12100
12101 std::string virtual_dwo_name =
12102 string_printf ("virtual-dwo/%d-%d-%d-%d",
12103 sections.abbrev.get_id (),
12104 sections.line.get_id (),
12105 sections.loc.get_id (),
12106 sections.str_offsets.get_id ());
12107 /* Can we use an existing virtual DWO file? */
12108 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12109 comp_dir);
12110 /* Create one if necessary. */
12111 if (*dwo_file_slot == NULL)
12112 {
12113 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12114 virtual_dwo_name.c_str ());
12115
12116 dwo_file = new struct dwo_file;
12117 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12118 dwo_file->comp_dir = comp_dir;
12119 dwo_file->sections.abbrev = sections.abbrev;
12120 dwo_file->sections.line = sections.line;
12121 dwo_file->sections.loc = sections.loc;
12122 dwo_file->sections.macinfo = sections.macinfo;
12123 dwo_file->sections.macro = sections.macro;
12124 dwo_file->sections.str_offsets = sections.str_offsets;
12125 /* The "str" section is global to the entire DWP file. */
12126 dwo_file->sections.str = dwp_file->sections.str;
12127 /* The info or types section is assigned below to dwo_unit,
12128 there's no need to record it in dwo_file.
12129 Also, we can't simply record type sections in dwo_file because
12130 we record a pointer into the vector in dwo_unit. As we collect more
12131 types we'll grow the vector and eventually have to reallocate space
12132 for it, invalidating all copies of pointers into the previous
12133 contents. */
12134 *dwo_file_slot = dwo_file;
12135 }
12136 else
12137 {
12138 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12139 virtual_dwo_name.c_str ());
12140
12141 dwo_file = (struct dwo_file *) *dwo_file_slot;
12142 }
12143
12144 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12145 dwo_unit->dwo_file = dwo_file;
12146 dwo_unit->signature = signature;
12147 dwo_unit->section =
12148 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12149 *dwo_unit->section = sections.info_or_types;
12150 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12151
12152 return dwo_unit;
12153 }
12154
12155 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12156 simplify them. Given a pointer to the containing section SECTION, and
12157 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12158 virtual section of just that piece. */
12159
12160 static struct dwarf2_section_info
12161 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12162 struct dwarf2_section_info *section,
12163 bfd_size_type offset, bfd_size_type size)
12164 {
12165 struct dwarf2_section_info result;
12166 asection *sectp;
12167
12168 gdb_assert (section != NULL);
12169 gdb_assert (!section->is_virtual);
12170
12171 memset (&result, 0, sizeof (result));
12172 result.s.containing_section = section;
12173 result.is_virtual = true;
12174
12175 if (size == 0)
12176 return result;
12177
12178 sectp = section->get_bfd_section ();
12179
12180 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12181 bounds of the real section. This is a pretty-rare event, so just
12182 flag an error (easier) instead of a warning and trying to cope. */
12183 if (sectp == NULL
12184 || offset + size > bfd_section_size (sectp))
12185 {
12186 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
12187 " in section %s [in module %s]"),
12188 sectp ? bfd_section_name (sectp) : "<unknown>",
12189 objfile_name (per_objfile->objfile));
12190 }
12191
12192 result.virtual_offset = offset;
12193 result.size = size;
12194 return result;
12195 }
12196
12197 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12198 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12199 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12200 This is for DWP version 2 files. */
12201
12202 static struct dwo_unit *
12203 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
12204 struct dwp_file *dwp_file,
12205 uint32_t unit_index,
12206 const char *comp_dir,
12207 ULONGEST signature, int is_debug_types)
12208 {
12209 const struct dwp_hash_table *dwp_htab =
12210 is_debug_types ? dwp_file->tus : dwp_file->cus;
12211 bfd *dbfd = dwp_file->dbfd.get ();
12212 const char *kind = is_debug_types ? "TU" : "CU";
12213 struct dwo_file *dwo_file;
12214 struct dwo_unit *dwo_unit;
12215 struct virtual_v2_or_v5_dwo_sections sections;
12216 void **dwo_file_slot;
12217 int i;
12218
12219 gdb_assert (dwp_file->version == 2);
12220
12221 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12222 kind, pulongest (unit_index), hex_string (signature),
12223 dwp_file->name);
12224
12225 /* Fetch the section offsets of this DWO unit. */
12226
12227 memset (&sections, 0, sizeof (sections));
12228
12229 for (i = 0; i < dwp_htab->nr_columns; ++i)
12230 {
12231 uint32_t offset = read_4_bytes (dbfd,
12232 dwp_htab->section_pool.v2.offsets
12233 + (((unit_index - 1) * dwp_htab->nr_columns
12234 + i)
12235 * sizeof (uint32_t)));
12236 uint32_t size = read_4_bytes (dbfd,
12237 dwp_htab->section_pool.v2.sizes
12238 + (((unit_index - 1) * dwp_htab->nr_columns
12239 + i)
12240 * sizeof (uint32_t)));
12241
12242 switch (dwp_htab->section_pool.v2.section_ids[i])
12243 {
12244 case DW_SECT_INFO:
12245 case DW_SECT_TYPES:
12246 sections.info_or_types_offset = offset;
12247 sections.info_or_types_size = size;
12248 break;
12249 case DW_SECT_ABBREV:
12250 sections.abbrev_offset = offset;
12251 sections.abbrev_size = size;
12252 break;
12253 case DW_SECT_LINE:
12254 sections.line_offset = offset;
12255 sections.line_size = size;
12256 break;
12257 case DW_SECT_LOC:
12258 sections.loc_offset = offset;
12259 sections.loc_size = size;
12260 break;
12261 case DW_SECT_STR_OFFSETS:
12262 sections.str_offsets_offset = offset;
12263 sections.str_offsets_size = size;
12264 break;
12265 case DW_SECT_MACINFO:
12266 sections.macinfo_offset = offset;
12267 sections.macinfo_size = size;
12268 break;
12269 case DW_SECT_MACRO:
12270 sections.macro_offset = offset;
12271 sections.macro_size = size;
12272 break;
12273 }
12274 }
12275
12276 /* It's easier for the rest of the code if we fake a struct dwo_file and
12277 have dwo_unit "live" in that. At least for now.
12278
12279 The DWP file can be made up of a random collection of CUs and TUs.
12280 However, for each CU + set of TUs that came from the same original DWO
12281 file, we can combine them back into a virtual DWO file to save space
12282 (fewer struct dwo_file objects to allocate). Remember that for really
12283 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12284
12285 std::string virtual_dwo_name =
12286 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12287 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12288 (long) (sections.line_size ? sections.line_offset : 0),
12289 (long) (sections.loc_size ? sections.loc_offset : 0),
12290 (long) (sections.str_offsets_size
12291 ? sections.str_offsets_offset : 0));
12292 /* Can we use an existing virtual DWO file? */
12293 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12294 comp_dir);
12295 /* Create one if necessary. */
12296 if (*dwo_file_slot == NULL)
12297 {
12298 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12299 virtual_dwo_name.c_str ());
12300
12301 dwo_file = new struct dwo_file;
12302 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12303 dwo_file->comp_dir = comp_dir;
12304 dwo_file->sections.abbrev =
12305 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12306 sections.abbrev_offset,
12307 sections.abbrev_size);
12308 dwo_file->sections.line =
12309 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12310 sections.line_offset,
12311 sections.line_size);
12312 dwo_file->sections.loc =
12313 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12314 sections.loc_offset, sections.loc_size);
12315 dwo_file->sections.macinfo =
12316 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12317 sections.macinfo_offset,
12318 sections.macinfo_size);
12319 dwo_file->sections.macro =
12320 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12321 sections.macro_offset,
12322 sections.macro_size);
12323 dwo_file->sections.str_offsets =
12324 create_dwp_v2_or_v5_section (per_objfile,
12325 &dwp_file->sections.str_offsets,
12326 sections.str_offsets_offset,
12327 sections.str_offsets_size);
12328 /* The "str" section is global to the entire DWP file. */
12329 dwo_file->sections.str = dwp_file->sections.str;
12330 /* The info or types section is assigned below to dwo_unit,
12331 there's no need to record it in dwo_file.
12332 Also, we can't simply record type sections in dwo_file because
12333 we record a pointer into the vector in dwo_unit. As we collect more
12334 types we'll grow the vector and eventually have to reallocate space
12335 for it, invalidating all copies of pointers into the previous
12336 contents. */
12337 *dwo_file_slot = dwo_file;
12338 }
12339 else
12340 {
12341 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12342 virtual_dwo_name.c_str ());
12343
12344 dwo_file = (struct dwo_file *) *dwo_file_slot;
12345 }
12346
12347 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12348 dwo_unit->dwo_file = dwo_file;
12349 dwo_unit->signature = signature;
12350 dwo_unit->section =
12351 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12352 *dwo_unit->section = create_dwp_v2_or_v5_section
12353 (per_objfile,
12354 is_debug_types
12355 ? &dwp_file->sections.types
12356 : &dwp_file->sections.info,
12357 sections.info_or_types_offset,
12358 sections.info_or_types_size);
12359 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12360
12361 return dwo_unit;
12362 }
12363
12364 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12365 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12366 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12367 This is for DWP version 5 files. */
12368
12369 static struct dwo_unit *
12370 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12371 struct dwp_file *dwp_file,
12372 uint32_t unit_index,
12373 const char *comp_dir,
12374 ULONGEST signature, int is_debug_types)
12375 {
12376 const struct dwp_hash_table *dwp_htab
12377 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12378 bfd *dbfd = dwp_file->dbfd.get ();
12379 const char *kind = is_debug_types ? "TU" : "CU";
12380 struct dwo_file *dwo_file;
12381 struct dwo_unit *dwo_unit;
12382 struct virtual_v2_or_v5_dwo_sections sections {};
12383 void **dwo_file_slot;
12384
12385 gdb_assert (dwp_file->version == 5);
12386
12387 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12388 kind, pulongest (unit_index), hex_string (signature),
12389 dwp_file->name);
12390
12391 /* Fetch the section offsets of this DWO unit. */
12392
12393 /* memset (&sections, 0, sizeof (sections)); */
12394
12395 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12396 {
12397 uint32_t offset = read_4_bytes (dbfd,
12398 dwp_htab->section_pool.v5.offsets
12399 + (((unit_index - 1)
12400 * dwp_htab->nr_columns
12401 + i)
12402 * sizeof (uint32_t)));
12403 uint32_t size = read_4_bytes (dbfd,
12404 dwp_htab->section_pool.v5.sizes
12405 + (((unit_index - 1) * dwp_htab->nr_columns
12406 + i)
12407 * sizeof (uint32_t)));
12408
12409 switch (dwp_htab->section_pool.v5.section_ids[i])
12410 {
12411 case DW_SECT_ABBREV_V5:
12412 sections.abbrev_offset = offset;
12413 sections.abbrev_size = size;
12414 break;
12415 case DW_SECT_INFO_V5:
12416 sections.info_or_types_offset = offset;
12417 sections.info_or_types_size = size;
12418 break;
12419 case DW_SECT_LINE_V5:
12420 sections.line_offset = offset;
12421 sections.line_size = size;
12422 break;
12423 case DW_SECT_LOCLISTS_V5:
12424 sections.loclists_offset = offset;
12425 sections.loclists_size = size;
12426 break;
12427 case DW_SECT_MACRO_V5:
12428 sections.macro_offset = offset;
12429 sections.macro_size = size;
12430 break;
12431 case DW_SECT_RNGLISTS_V5:
12432 sections.rnglists_offset = offset;
12433 sections.rnglists_size = size;
12434 break;
12435 case DW_SECT_STR_OFFSETS_V5:
12436 sections.str_offsets_offset = offset;
12437 sections.str_offsets_size = size;
12438 break;
12439 case DW_SECT_RESERVED_V5:
12440 default:
12441 break;
12442 }
12443 }
12444
12445 /* It's easier for the rest of the code if we fake a struct dwo_file and
12446 have dwo_unit "live" in that. At least for now.
12447
12448 The DWP file can be made up of a random collection of CUs and TUs.
12449 However, for each CU + set of TUs that came from the same original DWO
12450 file, we can combine them back into a virtual DWO file to save space
12451 (fewer struct dwo_file objects to allocate). Remember that for really
12452 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12453
12454 std::string virtual_dwo_name =
12455 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
12456 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12457 (long) (sections.line_size ? sections.line_offset : 0),
12458 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12459 (long) (sections.str_offsets_size
12460 ? sections.str_offsets_offset : 0),
12461 (long) (sections.macro_size ? sections.macro_offset : 0),
12462 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
12463 /* Can we use an existing virtual DWO file? */
12464 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
12465 virtual_dwo_name.c_str (),
12466 comp_dir);
12467 /* Create one if necessary. */
12468 if (*dwo_file_slot == NULL)
12469 {
12470 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12471 virtual_dwo_name.c_str ());
12472
12473 dwo_file = new struct dwo_file;
12474 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12475 dwo_file->comp_dir = comp_dir;
12476 dwo_file->sections.abbrev =
12477 create_dwp_v2_or_v5_section (per_objfile,
12478 &dwp_file->sections.abbrev,
12479 sections.abbrev_offset,
12480 sections.abbrev_size);
12481 dwo_file->sections.line =
12482 create_dwp_v2_or_v5_section (per_objfile,
12483 &dwp_file->sections.line,
12484 sections.line_offset, sections.line_size);
12485 dwo_file->sections.macro =
12486 create_dwp_v2_or_v5_section (per_objfile,
12487 &dwp_file->sections.macro,
12488 sections.macro_offset,
12489 sections.macro_size);
12490 dwo_file->sections.loclists =
12491 create_dwp_v2_or_v5_section (per_objfile,
12492 &dwp_file->sections.loclists,
12493 sections.loclists_offset,
12494 sections.loclists_size);
12495 dwo_file->sections.rnglists =
12496 create_dwp_v2_or_v5_section (per_objfile,
12497 &dwp_file->sections.rnglists,
12498 sections.rnglists_offset,
12499 sections.rnglists_size);
12500 dwo_file->sections.str_offsets =
12501 create_dwp_v2_or_v5_section (per_objfile,
12502 &dwp_file->sections.str_offsets,
12503 sections.str_offsets_offset,
12504 sections.str_offsets_size);
12505 /* The "str" section is global to the entire DWP file. */
12506 dwo_file->sections.str = dwp_file->sections.str;
12507 /* The info or types section is assigned below to dwo_unit,
12508 there's no need to record it in dwo_file.
12509 Also, we can't simply record type sections in dwo_file because
12510 we record a pointer into the vector in dwo_unit. As we collect more
12511 types we'll grow the vector and eventually have to reallocate space
12512 for it, invalidating all copies of pointers into the previous
12513 contents. */
12514 *dwo_file_slot = dwo_file;
12515 }
12516 else
12517 {
12518 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12519 virtual_dwo_name.c_str ());
12520
12521 dwo_file = (struct dwo_file *) *dwo_file_slot;
12522 }
12523
12524 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12525 dwo_unit->dwo_file = dwo_file;
12526 dwo_unit->signature = signature;
12527 dwo_unit->section
12528 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12529 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12530 &dwp_file->sections.info,
12531 sections.info_or_types_offset,
12532 sections.info_or_types_size);
12533 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12534
12535 return dwo_unit;
12536 }
12537
12538 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12539 Returns NULL if the signature isn't found. */
12540
12541 static struct dwo_unit *
12542 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12543 struct dwp_file *dwp_file, const char *comp_dir,
12544 ULONGEST signature, int is_debug_types)
12545 {
12546 const struct dwp_hash_table *dwp_htab =
12547 is_debug_types ? dwp_file->tus : dwp_file->cus;
12548 bfd *dbfd = dwp_file->dbfd.get ();
12549 uint32_t mask = dwp_htab->nr_slots - 1;
12550 uint32_t hash = signature & mask;
12551 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12552 unsigned int i;
12553 void **slot;
12554 struct dwo_unit find_dwo_cu;
12555
12556 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12557 find_dwo_cu.signature = signature;
12558 slot = htab_find_slot (is_debug_types
12559 ? dwp_file->loaded_tus.get ()
12560 : dwp_file->loaded_cus.get (),
12561 &find_dwo_cu, INSERT);
12562
12563 if (*slot != NULL)
12564 return (struct dwo_unit *) *slot;
12565
12566 /* Use a for loop so that we don't loop forever on bad debug info. */
12567 for (i = 0; i < dwp_htab->nr_slots; ++i)
12568 {
12569 ULONGEST signature_in_table;
12570
12571 signature_in_table =
12572 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12573 if (signature_in_table == signature)
12574 {
12575 uint32_t unit_index =
12576 read_4_bytes (dbfd,
12577 dwp_htab->unit_table + hash * sizeof (uint32_t));
12578
12579 if (dwp_file->version == 1)
12580 {
12581 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12582 unit_index, comp_dir,
12583 signature, is_debug_types);
12584 }
12585 else if (dwp_file->version == 2)
12586 {
12587 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12588 unit_index, comp_dir,
12589 signature, is_debug_types);
12590 }
12591 else /* version == 5 */
12592 {
12593 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12594 unit_index, comp_dir,
12595 signature, is_debug_types);
12596 }
12597 return (struct dwo_unit *) *slot;
12598 }
12599 if (signature_in_table == 0)
12600 return NULL;
12601 hash = (hash + hash2) & mask;
12602 }
12603
12604 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12605 " [in module %s]"),
12606 dwp_file->name);
12607 }
12608
12609 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12610 Open the file specified by FILE_NAME and hand it off to BFD for
12611 preliminary analysis. Return a newly initialized bfd *, which
12612 includes a canonicalized copy of FILE_NAME.
12613 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12614 SEARCH_CWD is true if the current directory is to be searched.
12615 It will be searched before debug-file-directory.
12616 If successful, the file is added to the bfd include table of the
12617 objfile's bfd (see gdb_bfd_record_inclusion).
12618 If unable to find/open the file, return NULL.
12619 NOTE: This function is derived from symfile_bfd_open. */
12620
12621 static gdb_bfd_ref_ptr
12622 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12623 const char *file_name, int is_dwp, int search_cwd)
12624 {
12625 int desc;
12626 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12627 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12628 to debug_file_directory. */
12629 const char *search_path;
12630 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12631
12632 gdb::unique_xmalloc_ptr<char> search_path_holder;
12633 if (search_cwd)
12634 {
12635 if (*debug_file_directory != '\0')
12636 {
12637 search_path_holder.reset (concat (".", dirname_separator_string,
12638 debug_file_directory,
12639 (char *) NULL));
12640 search_path = search_path_holder.get ();
12641 }
12642 else
12643 search_path = ".";
12644 }
12645 else
12646 search_path = debug_file_directory;
12647
12648 openp_flags flags = OPF_RETURN_REALPATH;
12649 if (is_dwp)
12650 flags |= OPF_SEARCH_IN_PATH;
12651
12652 gdb::unique_xmalloc_ptr<char> absolute_name;
12653 desc = openp (search_path, flags, file_name,
12654 O_RDONLY | O_BINARY, &absolute_name);
12655 if (desc < 0)
12656 return NULL;
12657
12658 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12659 gnutarget, desc));
12660 if (sym_bfd == NULL)
12661 return NULL;
12662 bfd_set_cacheable (sym_bfd.get (), 1);
12663
12664 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12665 return NULL;
12666
12667 /* Success. Record the bfd as having been included by the objfile's bfd.
12668 This is important because things like demangled_names_hash lives in the
12669 objfile's per_bfd space and may have references to things like symbol
12670 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12671 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12672
12673 return sym_bfd;
12674 }
12675
12676 /* Try to open DWO file FILE_NAME.
12677 COMP_DIR is the DW_AT_comp_dir attribute.
12678 The result is the bfd handle of the file.
12679 If there is a problem finding or opening the file, return NULL.
12680 Upon success, the canonicalized path of the file is stored in the bfd,
12681 same as symfile_bfd_open. */
12682
12683 static gdb_bfd_ref_ptr
12684 open_dwo_file (dwarf2_per_objfile *per_objfile,
12685 const char *file_name, const char *comp_dir)
12686 {
12687 if (IS_ABSOLUTE_PATH (file_name))
12688 return try_open_dwop_file (per_objfile, file_name,
12689 0 /*is_dwp*/, 0 /*search_cwd*/);
12690
12691 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12692
12693 if (comp_dir != NULL)
12694 {
12695 gdb::unique_xmalloc_ptr<char> path_to_try
12696 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12697
12698 /* NOTE: If comp_dir is a relative path, this will also try the
12699 search path, which seems useful. */
12700 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12701 0 /*is_dwp*/,
12702 1 /*search_cwd*/));
12703 if (abfd != NULL)
12704 return abfd;
12705 }
12706
12707 /* That didn't work, try debug-file-directory, which, despite its name,
12708 is a list of paths. */
12709
12710 if (*debug_file_directory == '\0')
12711 return NULL;
12712
12713 return try_open_dwop_file (per_objfile, file_name,
12714 0 /*is_dwp*/, 1 /*search_cwd*/);
12715 }
12716
12717 /* This function is mapped across the sections and remembers the offset and
12718 size of each of the DWO debugging sections we are interested in. */
12719
12720 static void
12721 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12722 dwo_sections *dwo_sections)
12723 {
12724 const struct dwop_section_names *names = &dwop_section_names;
12725
12726 if (section_is_p (sectp->name, &names->abbrev_dwo))
12727 {
12728 dwo_sections->abbrev.s.section = sectp;
12729 dwo_sections->abbrev.size = bfd_section_size (sectp);
12730 }
12731 else if (section_is_p (sectp->name, &names->info_dwo))
12732 {
12733 dwo_sections->info.s.section = sectp;
12734 dwo_sections->info.size = bfd_section_size (sectp);
12735 }
12736 else if (section_is_p (sectp->name, &names->line_dwo))
12737 {
12738 dwo_sections->line.s.section = sectp;
12739 dwo_sections->line.size = bfd_section_size (sectp);
12740 }
12741 else if (section_is_p (sectp->name, &names->loc_dwo))
12742 {
12743 dwo_sections->loc.s.section = sectp;
12744 dwo_sections->loc.size = bfd_section_size (sectp);
12745 }
12746 else if (section_is_p (sectp->name, &names->loclists_dwo))
12747 {
12748 dwo_sections->loclists.s.section = sectp;
12749 dwo_sections->loclists.size = bfd_section_size (sectp);
12750 }
12751 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12752 {
12753 dwo_sections->macinfo.s.section = sectp;
12754 dwo_sections->macinfo.size = bfd_section_size (sectp);
12755 }
12756 else if (section_is_p (sectp->name, &names->macro_dwo))
12757 {
12758 dwo_sections->macro.s.section = sectp;
12759 dwo_sections->macro.size = bfd_section_size (sectp);
12760 }
12761 else if (section_is_p (sectp->name, &names->rnglists_dwo))
12762 {
12763 dwo_sections->rnglists.s.section = sectp;
12764 dwo_sections->rnglists.size = bfd_section_size (sectp);
12765 }
12766 else if (section_is_p (sectp->name, &names->str_dwo))
12767 {
12768 dwo_sections->str.s.section = sectp;
12769 dwo_sections->str.size = bfd_section_size (sectp);
12770 }
12771 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12772 {
12773 dwo_sections->str_offsets.s.section = sectp;
12774 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12775 }
12776 else if (section_is_p (sectp->name, &names->types_dwo))
12777 {
12778 struct dwarf2_section_info type_section;
12779
12780 memset (&type_section, 0, sizeof (type_section));
12781 type_section.s.section = sectp;
12782 type_section.size = bfd_section_size (sectp);
12783 dwo_sections->types.push_back (type_section);
12784 }
12785 }
12786
12787 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12788 by PER_CU. This is for the non-DWP case.
12789 The result is NULL if DWO_NAME can't be found. */
12790
12791 static struct dwo_file *
12792 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12793 const char *comp_dir)
12794 {
12795 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12796
12797 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12798 if (dbfd == NULL)
12799 {
12800 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12801
12802 return NULL;
12803 }
12804
12805 dwo_file_up dwo_file (new struct dwo_file);
12806 dwo_file->dwo_name = dwo_name;
12807 dwo_file->comp_dir = comp_dir;
12808 dwo_file->dbfd = std::move (dbfd);
12809
12810 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12811 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12812 &dwo_file->sections);
12813
12814 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12815 dwo_file->cus);
12816
12817 if (cu->per_cu->dwarf_version < 5)
12818 {
12819 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12820 dwo_file->sections.types, dwo_file->tus);
12821 }
12822 else
12823 {
12824 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12825 &dwo_file->sections.info, dwo_file->tus,
12826 rcuh_kind::TYPE);
12827 }
12828
12829 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12830
12831 return dwo_file.release ();
12832 }
12833
12834 /* This function is mapped across the sections and remembers the offset and
12835 size of each of the DWP debugging sections common to version 1 and 2 that
12836 we are interested in. */
12837
12838 static void
12839 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12840 dwp_file *dwp_file)
12841 {
12842 const struct dwop_section_names *names = &dwop_section_names;
12843 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12844
12845 /* Record the ELF section number for later lookup: this is what the
12846 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12847 gdb_assert (elf_section_nr < dwp_file->num_sections);
12848 dwp_file->elf_sections[elf_section_nr] = sectp;
12849
12850 /* Look for specific sections that we need. */
12851 if (section_is_p (sectp->name, &names->str_dwo))
12852 {
12853 dwp_file->sections.str.s.section = sectp;
12854 dwp_file->sections.str.size = bfd_section_size (sectp);
12855 }
12856 else if (section_is_p (sectp->name, &names->cu_index))
12857 {
12858 dwp_file->sections.cu_index.s.section = sectp;
12859 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12860 }
12861 else if (section_is_p (sectp->name, &names->tu_index))
12862 {
12863 dwp_file->sections.tu_index.s.section = sectp;
12864 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12865 }
12866 }
12867
12868 /* This function is mapped across the sections and remembers the offset and
12869 size of each of the DWP version 2 debugging sections that we are interested
12870 in. This is split into a separate function because we don't know if we
12871 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12872
12873 static void
12874 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12875 {
12876 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12877 const struct dwop_section_names *names = &dwop_section_names;
12878 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12879
12880 /* Record the ELF section number for later lookup: this is what the
12881 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12882 gdb_assert (elf_section_nr < dwp_file->num_sections);
12883 dwp_file->elf_sections[elf_section_nr] = sectp;
12884
12885 /* Look for specific sections that we need. */
12886 if (section_is_p (sectp->name, &names->abbrev_dwo))
12887 {
12888 dwp_file->sections.abbrev.s.section = sectp;
12889 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12890 }
12891 else if (section_is_p (sectp->name, &names->info_dwo))
12892 {
12893 dwp_file->sections.info.s.section = sectp;
12894 dwp_file->sections.info.size = bfd_section_size (sectp);
12895 }
12896 else if (section_is_p (sectp->name, &names->line_dwo))
12897 {
12898 dwp_file->sections.line.s.section = sectp;
12899 dwp_file->sections.line.size = bfd_section_size (sectp);
12900 }
12901 else if (section_is_p (sectp->name, &names->loc_dwo))
12902 {
12903 dwp_file->sections.loc.s.section = sectp;
12904 dwp_file->sections.loc.size = bfd_section_size (sectp);
12905 }
12906 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12907 {
12908 dwp_file->sections.macinfo.s.section = sectp;
12909 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12910 }
12911 else if (section_is_p (sectp->name, &names->macro_dwo))
12912 {
12913 dwp_file->sections.macro.s.section = sectp;
12914 dwp_file->sections.macro.size = bfd_section_size (sectp);
12915 }
12916 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12917 {
12918 dwp_file->sections.str_offsets.s.section = sectp;
12919 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12920 }
12921 else if (section_is_p (sectp->name, &names->types_dwo))
12922 {
12923 dwp_file->sections.types.s.section = sectp;
12924 dwp_file->sections.types.size = bfd_section_size (sectp);
12925 }
12926 }
12927
12928 /* This function is mapped across the sections and remembers the offset and
12929 size of each of the DWP version 5 debugging sections that we are interested
12930 in. This is split into a separate function because we don't know if we
12931 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12932
12933 static void
12934 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12935 {
12936 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12937 const struct dwop_section_names *names = &dwop_section_names;
12938 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12939
12940 /* Record the ELF section number for later lookup: this is what the
12941 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12942 gdb_assert (elf_section_nr < dwp_file->num_sections);
12943 dwp_file->elf_sections[elf_section_nr] = sectp;
12944
12945 /* Look for specific sections that we need. */
12946 if (section_is_p (sectp->name, &names->abbrev_dwo))
12947 {
12948 dwp_file->sections.abbrev.s.section = sectp;
12949 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12950 }
12951 else if (section_is_p (sectp->name, &names->info_dwo))
12952 {
12953 dwp_file->sections.info.s.section = sectp;
12954 dwp_file->sections.info.size = bfd_section_size (sectp);
12955 }
12956 else if (section_is_p (sectp->name, &names->line_dwo))
12957 {
12958 dwp_file->sections.line.s.section = sectp;
12959 dwp_file->sections.line.size = bfd_section_size (sectp);
12960 }
12961 else if (section_is_p (sectp->name, &names->loclists_dwo))
12962 {
12963 dwp_file->sections.loclists.s.section = sectp;
12964 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12965 }
12966 else if (section_is_p (sectp->name, &names->macro_dwo))
12967 {
12968 dwp_file->sections.macro.s.section = sectp;
12969 dwp_file->sections.macro.size = bfd_section_size (sectp);
12970 }
12971 else if (section_is_p (sectp->name, &names->rnglists_dwo))
12972 {
12973 dwp_file->sections.rnglists.s.section = sectp;
12974 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12975 }
12976 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12977 {
12978 dwp_file->sections.str_offsets.s.section = sectp;
12979 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12980 }
12981 }
12982
12983 /* Hash function for dwp_file loaded CUs/TUs. */
12984
12985 static hashval_t
12986 hash_dwp_loaded_cutus (const void *item)
12987 {
12988 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12989
12990 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12991 return dwo_unit->signature;
12992 }
12993
12994 /* Equality function for dwp_file loaded CUs/TUs. */
12995
12996 static int
12997 eq_dwp_loaded_cutus (const void *a, const void *b)
12998 {
12999 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13000 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13001
13002 return dua->signature == dub->signature;
13003 }
13004
13005 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13006
13007 static htab_up
13008 allocate_dwp_loaded_cutus_table ()
13009 {
13010 return htab_up (htab_create_alloc (3,
13011 hash_dwp_loaded_cutus,
13012 eq_dwp_loaded_cutus,
13013 NULL, xcalloc, xfree));
13014 }
13015
13016 /* Try to open DWP file FILE_NAME.
13017 The result is the bfd handle of the file.
13018 If there is a problem finding or opening the file, return NULL.
13019 Upon success, the canonicalized path of the file is stored in the bfd,
13020 same as symfile_bfd_open. */
13021
13022 static gdb_bfd_ref_ptr
13023 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
13024 {
13025 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
13026 1 /*is_dwp*/,
13027 1 /*search_cwd*/));
13028 if (abfd != NULL)
13029 return abfd;
13030
13031 /* Work around upstream bug 15652.
13032 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13033 [Whether that's a "bug" is debatable, but it is getting in our way.]
13034 We have no real idea where the dwp file is, because gdb's realpath-ing
13035 of the executable's path may have discarded the needed info.
13036 [IWBN if the dwp file name was recorded in the executable, akin to
13037 .gnu_debuglink, but that doesn't exist yet.]
13038 Strip the directory from FILE_NAME and search again. */
13039 if (*debug_file_directory != '\0')
13040 {
13041 /* Don't implicitly search the current directory here.
13042 If the user wants to search "." to handle this case,
13043 it must be added to debug-file-directory. */
13044 return try_open_dwop_file (per_objfile, lbasename (file_name),
13045 1 /*is_dwp*/,
13046 0 /*search_cwd*/);
13047 }
13048
13049 return NULL;
13050 }
13051
13052 /* Initialize the use of the DWP file for the current objfile.
13053 By convention the name of the DWP file is ${objfile}.dwp.
13054 The result is NULL if it can't be found. */
13055
13056 static std::unique_ptr<struct dwp_file>
13057 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
13058 {
13059 struct objfile *objfile = per_objfile->objfile;
13060
13061 /* Try to find first .dwp for the binary file before any symbolic links
13062 resolving. */
13063
13064 /* If the objfile is a debug file, find the name of the real binary
13065 file and get the name of dwp file from there. */
13066 std::string dwp_name;
13067 if (objfile->separate_debug_objfile_backlink != NULL)
13068 {
13069 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13070 const char *backlink_basename = lbasename (backlink->original_name);
13071
13072 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13073 }
13074 else
13075 dwp_name = objfile->original_name;
13076
13077 dwp_name += ".dwp";
13078
13079 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
13080 if (dbfd == NULL
13081 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13082 {
13083 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13084 dwp_name = objfile_name (objfile);
13085 dwp_name += ".dwp";
13086 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
13087 }
13088
13089 if (dbfd == NULL)
13090 {
13091 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
13092
13093 return std::unique_ptr<dwp_file> ();
13094 }
13095
13096 const char *name = bfd_get_filename (dbfd.get ());
13097 std::unique_ptr<struct dwp_file> dwp_file
13098 (new struct dwp_file (name, std::move (dbfd)));
13099
13100 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13101 dwp_file->elf_sections =
13102 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
13103 dwp_file->num_sections, asection *);
13104
13105 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13106 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13107 dwp_file.get ());
13108
13109 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
13110
13111 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
13112
13113 /* The DWP file version is stored in the hash table. Oh well. */
13114 if (dwp_file->cus && dwp_file->tus
13115 && dwp_file->cus->version != dwp_file->tus->version)
13116 {
13117 /* Technically speaking, we should try to limp along, but this is
13118 pretty bizarre. We use pulongest here because that's the established
13119 portability solution (e.g, we cannot use %u for uint32_t). */
13120 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13121 " TU version %s [in DWP file %s]"),
13122 pulongest (dwp_file->cus->version),
13123 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13124 }
13125
13126 if (dwp_file->cus)
13127 dwp_file->version = dwp_file->cus->version;
13128 else if (dwp_file->tus)
13129 dwp_file->version = dwp_file->tus->version;
13130 else
13131 dwp_file->version = 2;
13132
13133 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13134 {
13135 if (dwp_file->version == 2)
13136 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13137 dwp_file.get ());
13138 else
13139 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13140 dwp_file.get ());
13141 }
13142
13143 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13144 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
13145
13146 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
13147 dwarf_read_debug_printf (" %s CUs, %s TUs",
13148 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13149 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13150
13151 return dwp_file;
13152 }
13153
13154 /* Wrapper around open_and_init_dwp_file, only open it once. */
13155
13156 static struct dwp_file *
13157 get_dwp_file (dwarf2_per_objfile *per_objfile)
13158 {
13159 if (!per_objfile->per_bfd->dwp_checked)
13160 {
13161 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13162 per_objfile->per_bfd->dwp_checked = 1;
13163 }
13164 return per_objfile->per_bfd->dwp_file.get ();
13165 }
13166
13167 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13168 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13169 or in the DWP file for the objfile, referenced by THIS_UNIT.
13170 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13171 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13172
13173 This is called, for example, when wanting to read a variable with a
13174 complex location. Therefore we don't want to do file i/o for every call.
13175 Therefore we don't want to look for a DWO file on every call.
13176 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13177 then we check if we've already seen DWO_NAME, and only THEN do we check
13178 for a DWO file.
13179
13180 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13181 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13182
13183 static struct dwo_unit *
13184 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13185 ULONGEST signature, int is_debug_types)
13186 {
13187 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13188 struct objfile *objfile = per_objfile->objfile;
13189 const char *kind = is_debug_types ? "TU" : "CU";
13190 void **dwo_file_slot;
13191 struct dwo_file *dwo_file;
13192 struct dwp_file *dwp_file;
13193
13194 /* First see if there's a DWP file.
13195 If we have a DWP file but didn't find the DWO inside it, don't
13196 look for the original DWO file. It makes gdb behave differently
13197 depending on whether one is debugging in the build tree. */
13198
13199 dwp_file = get_dwp_file (per_objfile);
13200 if (dwp_file != NULL)
13201 {
13202 const struct dwp_hash_table *dwp_htab =
13203 is_debug_types ? dwp_file->tus : dwp_file->cus;
13204
13205 if (dwp_htab != NULL)
13206 {
13207 struct dwo_unit *dwo_cutu =
13208 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13209 is_debug_types);
13210
13211 if (dwo_cutu != NULL)
13212 {
13213 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13214 kind, hex_string (signature),
13215 host_address_to_string (dwo_cutu));
13216
13217 return dwo_cutu;
13218 }
13219 }
13220 }
13221 else
13222 {
13223 /* No DWP file, look for the DWO file. */
13224
13225 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
13226 if (*dwo_file_slot == NULL)
13227 {
13228 /* Read in the file and build a table of the CUs/TUs it contains. */
13229 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
13230 }
13231 /* NOTE: This will be NULL if unable to open the file. */
13232 dwo_file = (struct dwo_file *) *dwo_file_slot;
13233
13234 if (dwo_file != NULL)
13235 {
13236 struct dwo_unit *dwo_cutu = NULL;
13237
13238 if (is_debug_types && dwo_file->tus)
13239 {
13240 struct dwo_unit find_dwo_cutu;
13241
13242 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13243 find_dwo_cutu.signature = signature;
13244 dwo_cutu
13245 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13246 &find_dwo_cutu);
13247 }
13248 else if (!is_debug_types && dwo_file->cus)
13249 {
13250 struct dwo_unit find_dwo_cutu;
13251
13252 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13253 find_dwo_cutu.signature = signature;
13254 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
13255 &find_dwo_cutu);
13256 }
13257
13258 if (dwo_cutu != NULL)
13259 {
13260 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13261 kind, dwo_name, hex_string (signature),
13262 host_address_to_string (dwo_cutu));
13263
13264 return dwo_cutu;
13265 }
13266 }
13267 }
13268
13269 /* We didn't find it. This could mean a dwo_id mismatch, or
13270 someone deleted the DWO/DWP file, or the search path isn't set up
13271 correctly to find the file. */
13272
13273 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13274 kind, dwo_name, hex_string (signature));
13275
13276 /* This is a warning and not a complaint because it can be caused by
13277 pilot error (e.g., user accidentally deleting the DWO). */
13278 {
13279 /* Print the name of the DWP file if we looked there, helps the user
13280 better diagnose the problem. */
13281 std::string dwp_text;
13282
13283 if (dwp_file != NULL)
13284 dwp_text = string_printf (" [in DWP file %s]",
13285 lbasename (dwp_file->name));
13286
13287 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13288 " [in module %s]"),
13289 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13290 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
13291 }
13292 return NULL;
13293 }
13294
13295 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13296 See lookup_dwo_cutu_unit for details. */
13297
13298 static struct dwo_unit *
13299 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13300 ULONGEST signature)
13301 {
13302 gdb_assert (!cu->per_cu->is_debug_types);
13303
13304 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
13305 }
13306
13307 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13308 See lookup_dwo_cutu_unit for details. */
13309
13310 static struct dwo_unit *
13311 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
13312 {
13313 gdb_assert (cu->per_cu->is_debug_types);
13314
13315 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13316
13317 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
13318 }
13319
13320 /* Traversal function for queue_and_load_all_dwo_tus. */
13321
13322 static int
13323 queue_and_load_dwo_tu (void **slot, void *info)
13324 {
13325 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13326 dwarf2_cu *cu = (dwarf2_cu *) info;
13327 ULONGEST signature = dwo_unit->signature;
13328 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
13329
13330 if (sig_type != NULL)
13331 {
13332 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13333
13334 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13335 a real dependency of PER_CU on SIG_TYPE. That is detected later
13336 while processing PER_CU. */
13337 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
13338 load_full_type_unit (sig_cu, cu->per_objfile);
13339 cu->per_cu->imported_symtabs_push (sig_cu);
13340 }
13341
13342 return 1;
13343 }
13344
13345 /* Queue all TUs contained in the DWO of CU to be read in.
13346 The DWO may have the only definition of the type, though it may not be
13347 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13348 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13349
13350 static void
13351 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
13352 {
13353 struct dwo_unit *dwo_unit;
13354 struct dwo_file *dwo_file;
13355
13356 gdb_assert (cu != nullptr);
13357 gdb_assert (!cu->per_cu->is_debug_types);
13358 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
13359
13360 dwo_unit = cu->dwo_unit;
13361 gdb_assert (dwo_unit != NULL);
13362
13363 dwo_file = dwo_unit->dwo_file;
13364 if (dwo_file->tus != NULL)
13365 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
13366 }
13367
13368 /* Read in various DIEs. */
13369
13370 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13371 Inherit only the children of the DW_AT_abstract_origin DIE not being
13372 already referenced by DW_AT_abstract_origin from the children of the
13373 current DIE. */
13374
13375 static void
13376 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13377 {
13378 struct die_info *child_die;
13379 sect_offset *offsetp;
13380 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13381 struct die_info *origin_die;
13382 /* Iterator of the ORIGIN_DIE children. */
13383 struct die_info *origin_child_die;
13384 struct attribute *attr;
13385 struct dwarf2_cu *origin_cu;
13386 struct pending **origin_previous_list_in_scope;
13387
13388 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13389 if (!attr)
13390 return;
13391
13392 /* Note that following die references may follow to a die in a
13393 different cu. */
13394
13395 origin_cu = cu;
13396 origin_die = follow_die_ref (die, attr, &origin_cu);
13397
13398 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13399 symbols in. */
13400 origin_previous_list_in_scope = origin_cu->list_in_scope;
13401 origin_cu->list_in_scope = cu->list_in_scope;
13402
13403 if (die->tag != origin_die->tag
13404 && !(die->tag == DW_TAG_inlined_subroutine
13405 && origin_die->tag == DW_TAG_subprogram))
13406 complaint (_("DIE %s and its abstract origin %s have different tags"),
13407 sect_offset_str (die->sect_off),
13408 sect_offset_str (origin_die->sect_off));
13409
13410 std::vector<sect_offset> offsets;
13411
13412 for (child_die = die->child;
13413 child_die && child_die->tag;
13414 child_die = child_die->sibling)
13415 {
13416 struct die_info *child_origin_die;
13417 struct dwarf2_cu *child_origin_cu;
13418
13419 /* We are trying to process concrete instance entries:
13420 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13421 it's not relevant to our analysis here. i.e. detecting DIEs that are
13422 present in the abstract instance but not referenced in the concrete
13423 one. */
13424 if (child_die->tag == DW_TAG_call_site
13425 || child_die->tag == DW_TAG_GNU_call_site)
13426 continue;
13427
13428 /* For each CHILD_DIE, find the corresponding child of
13429 ORIGIN_DIE. If there is more than one layer of
13430 DW_AT_abstract_origin, follow them all; there shouldn't be,
13431 but GCC versions at least through 4.4 generate this (GCC PR
13432 40573). */
13433 child_origin_die = child_die;
13434 child_origin_cu = cu;
13435 while (1)
13436 {
13437 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13438 child_origin_cu);
13439 if (attr == NULL)
13440 break;
13441 child_origin_die = follow_die_ref (child_origin_die, attr,
13442 &child_origin_cu);
13443 }
13444
13445 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13446 counterpart may exist. */
13447 if (child_origin_die != child_die)
13448 {
13449 if (child_die->tag != child_origin_die->tag
13450 && !(child_die->tag == DW_TAG_inlined_subroutine
13451 && child_origin_die->tag == DW_TAG_subprogram))
13452 complaint (_("Child DIE %s and its abstract origin %s have "
13453 "different tags"),
13454 sect_offset_str (child_die->sect_off),
13455 sect_offset_str (child_origin_die->sect_off));
13456 if (child_origin_die->parent != origin_die)
13457 complaint (_("Child DIE %s and its abstract origin %s have "
13458 "different parents"),
13459 sect_offset_str (child_die->sect_off),
13460 sect_offset_str (child_origin_die->sect_off));
13461 else
13462 offsets.push_back (child_origin_die->sect_off);
13463 }
13464 }
13465 std::sort (offsets.begin (), offsets.end ());
13466 sect_offset *offsets_end = offsets.data () + offsets.size ();
13467 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13468 if (offsetp[-1] == *offsetp)
13469 complaint (_("Multiple children of DIE %s refer "
13470 "to DIE %s as their abstract origin"),
13471 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13472
13473 offsetp = offsets.data ();
13474 origin_child_die = origin_die->child;
13475 while (origin_child_die && origin_child_die->tag)
13476 {
13477 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13478 while (offsetp < offsets_end
13479 && *offsetp < origin_child_die->sect_off)
13480 offsetp++;
13481 if (offsetp >= offsets_end
13482 || *offsetp > origin_child_die->sect_off)
13483 {
13484 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13485 Check whether we're already processing ORIGIN_CHILD_DIE.
13486 This can happen with mutually referenced abstract_origins.
13487 PR 16581. */
13488 if (!origin_child_die->in_process)
13489 process_die (origin_child_die, origin_cu);
13490 }
13491 origin_child_die = origin_child_die->sibling;
13492 }
13493 origin_cu->list_in_scope = origin_previous_list_in_scope;
13494
13495 if (cu != origin_cu)
13496 compute_delayed_physnames (origin_cu);
13497 }
13498
13499 static void
13500 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13501 {
13502 struct objfile *objfile = cu->per_objfile->objfile;
13503 struct gdbarch *gdbarch = objfile->arch ();
13504 struct context_stack *newobj;
13505 CORE_ADDR lowpc;
13506 CORE_ADDR highpc;
13507 struct die_info *child_die;
13508 struct attribute *attr, *call_line, *call_file;
13509 const char *name;
13510 CORE_ADDR baseaddr;
13511 struct block *block;
13512 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13513 std::vector<struct symbol *> template_args;
13514 struct template_symbol *templ_func = NULL;
13515
13516 if (inlined_func)
13517 {
13518 /* If we do not have call site information, we can't show the
13519 caller of this inlined function. That's too confusing, so
13520 only use the scope for local variables. */
13521 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13522 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13523 if (call_line == NULL || call_file == NULL)
13524 {
13525 read_lexical_block_scope (die, cu);
13526 return;
13527 }
13528 }
13529
13530 baseaddr = objfile->text_section_offset ();
13531
13532 name = dwarf2_name (die, cu);
13533
13534 /* Ignore functions with missing or empty names. These are actually
13535 illegal according to the DWARF standard. */
13536 if (name == NULL)
13537 {
13538 complaint (_("missing name for subprogram DIE at %s"),
13539 sect_offset_str (die->sect_off));
13540 return;
13541 }
13542
13543 /* Ignore functions with missing or invalid low and high pc attributes. */
13544 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13545 <= PC_BOUNDS_INVALID)
13546 {
13547 attr = dwarf2_attr (die, DW_AT_external, cu);
13548 if (attr == nullptr || !attr->as_boolean ())
13549 complaint (_("cannot get low and high bounds "
13550 "for subprogram DIE at %s"),
13551 sect_offset_str (die->sect_off));
13552 return;
13553 }
13554
13555 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13556 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13557
13558 /* If we have any template arguments, then we must allocate a
13559 different sort of symbol. */
13560 for (child_die = die->child; child_die; child_die = child_die->sibling)
13561 {
13562 if (child_die->tag == DW_TAG_template_type_param
13563 || child_die->tag == DW_TAG_template_value_param)
13564 {
13565 templ_func = new (&objfile->objfile_obstack) template_symbol;
13566 templ_func->subclass = SYMBOL_TEMPLATE;
13567 break;
13568 }
13569 }
13570
13571 newobj = cu->get_builder ()->push_context (0, lowpc);
13572 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13573 (struct symbol *) templ_func);
13574
13575 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13576 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13577 cu->language);
13578
13579 /* If there is a location expression for DW_AT_frame_base, record
13580 it. */
13581 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13582 if (attr != nullptr)
13583 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13584
13585 /* If there is a location for the static link, record it. */
13586 newobj->static_link = NULL;
13587 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13588 if (attr != nullptr)
13589 {
13590 newobj->static_link
13591 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13592 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13593 cu->addr_type ());
13594 }
13595
13596 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13597
13598 if (die->child != NULL)
13599 {
13600 child_die = die->child;
13601 while (child_die && child_die->tag)
13602 {
13603 if (child_die->tag == DW_TAG_template_type_param
13604 || child_die->tag == DW_TAG_template_value_param)
13605 {
13606 struct symbol *arg = new_symbol (child_die, NULL, cu);
13607
13608 if (arg != NULL)
13609 template_args.push_back (arg);
13610 }
13611 else
13612 process_die (child_die, cu);
13613 child_die = child_die->sibling;
13614 }
13615 }
13616
13617 inherit_abstract_dies (die, cu);
13618
13619 /* If we have a DW_AT_specification, we might need to import using
13620 directives from the context of the specification DIE. See the
13621 comment in determine_prefix. */
13622 if (cu->language == language_cplus
13623 && dwarf2_attr (die, DW_AT_specification, cu))
13624 {
13625 struct dwarf2_cu *spec_cu = cu;
13626 struct die_info *spec_die = die_specification (die, &spec_cu);
13627
13628 while (spec_die)
13629 {
13630 child_die = spec_die->child;
13631 while (child_die && child_die->tag)
13632 {
13633 if (child_die->tag == DW_TAG_imported_module)
13634 process_die (child_die, spec_cu);
13635 child_die = child_die->sibling;
13636 }
13637
13638 /* In some cases, GCC generates specification DIEs that
13639 themselves contain DW_AT_specification attributes. */
13640 spec_die = die_specification (spec_die, &spec_cu);
13641 }
13642 }
13643
13644 struct context_stack cstk = cu->get_builder ()->pop_context ();
13645 /* Make a block for the local symbols within. */
13646 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13647 cstk.static_link, lowpc, highpc);
13648
13649 /* For C++, set the block's scope. */
13650 if ((cu->language == language_cplus
13651 || cu->language == language_fortran
13652 || cu->language == language_d
13653 || cu->language == language_rust)
13654 && cu->processing_has_namespace_info)
13655 block_set_scope (block, determine_prefix (die, cu),
13656 &objfile->objfile_obstack);
13657
13658 /* If we have address ranges, record them. */
13659 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13660
13661 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13662
13663 /* Attach template arguments to function. */
13664 if (!template_args.empty ())
13665 {
13666 gdb_assert (templ_func != NULL);
13667
13668 templ_func->n_template_arguments = template_args.size ();
13669 templ_func->template_arguments
13670 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13671 templ_func->n_template_arguments);
13672 memcpy (templ_func->template_arguments,
13673 template_args.data (),
13674 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13675
13676 /* Make sure that the symtab is set on the new symbols. Even
13677 though they don't appear in this symtab directly, other parts
13678 of gdb assume that symbols do, and this is reasonably
13679 true. */
13680 for (symbol *sym : template_args)
13681 symbol_set_symtab (sym, symbol_symtab (templ_func));
13682 }
13683
13684 /* In C++, we can have functions nested inside functions (e.g., when
13685 a function declares a class that has methods). This means that
13686 when we finish processing a function scope, we may need to go
13687 back to building a containing block's symbol lists. */
13688 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13689 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13690
13691 /* If we've finished processing a top-level function, subsequent
13692 symbols go in the file symbol list. */
13693 if (cu->get_builder ()->outermost_context_p ())
13694 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13695 }
13696
13697 /* Process all the DIES contained within a lexical block scope. Start
13698 a new scope, process the dies, and then close the scope. */
13699
13700 static void
13701 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13702 {
13703 struct objfile *objfile = cu->per_objfile->objfile;
13704 struct gdbarch *gdbarch = objfile->arch ();
13705 CORE_ADDR lowpc, highpc;
13706 struct die_info *child_die;
13707 CORE_ADDR baseaddr;
13708
13709 baseaddr = objfile->text_section_offset ();
13710
13711 /* Ignore blocks with missing or invalid low and high pc attributes. */
13712 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13713 as multiple lexical blocks? Handling children in a sane way would
13714 be nasty. Might be easier to properly extend generic blocks to
13715 describe ranges. */
13716 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13717 {
13718 case PC_BOUNDS_NOT_PRESENT:
13719 /* DW_TAG_lexical_block has no attributes, process its children as if
13720 there was no wrapping by that DW_TAG_lexical_block.
13721 GCC does no longer produces such DWARF since GCC r224161. */
13722 for (child_die = die->child;
13723 child_die != NULL && child_die->tag;
13724 child_die = child_die->sibling)
13725 {
13726 /* We might already be processing this DIE. This can happen
13727 in an unusual circumstance -- where a subroutine A
13728 appears lexically in another subroutine B, but A actually
13729 inlines B. The recursion is broken here, rather than in
13730 inherit_abstract_dies, because it seems better to simply
13731 drop concrete children here. */
13732 if (!child_die->in_process)
13733 process_die (child_die, cu);
13734 }
13735 return;
13736 case PC_BOUNDS_INVALID:
13737 return;
13738 }
13739 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13740 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13741
13742 cu->get_builder ()->push_context (0, lowpc);
13743 if (die->child != NULL)
13744 {
13745 child_die = die->child;
13746 while (child_die && child_die->tag)
13747 {
13748 process_die (child_die, cu);
13749 child_die = child_die->sibling;
13750 }
13751 }
13752 inherit_abstract_dies (die, cu);
13753 struct context_stack cstk = cu->get_builder ()->pop_context ();
13754
13755 if (*cu->get_builder ()->get_local_symbols () != NULL
13756 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13757 {
13758 struct block *block
13759 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13760 cstk.start_addr, highpc);
13761
13762 /* Note that recording ranges after traversing children, as we
13763 do here, means that recording a parent's ranges entails
13764 walking across all its children's ranges as they appear in
13765 the address map, which is quadratic behavior.
13766
13767 It would be nicer to record the parent's ranges before
13768 traversing its children, simply overriding whatever you find
13769 there. But since we don't even decide whether to create a
13770 block until after we've traversed its children, that's hard
13771 to do. */
13772 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13773 }
13774 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13775 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13776 }
13777
13778 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13779
13780 static void
13781 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13782 {
13783 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13784 struct objfile *objfile = per_objfile->objfile;
13785 struct gdbarch *gdbarch = objfile->arch ();
13786 CORE_ADDR pc, baseaddr;
13787 struct attribute *attr;
13788 struct call_site *call_site, call_site_local;
13789 void **slot;
13790 int nparams;
13791 struct die_info *child_die;
13792
13793 baseaddr = objfile->text_section_offset ();
13794
13795 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13796 if (attr == NULL)
13797 {
13798 /* This was a pre-DWARF-5 GNU extension alias
13799 for DW_AT_call_return_pc. */
13800 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13801 }
13802 if (!attr)
13803 {
13804 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13805 "DIE %s [in module %s]"),
13806 sect_offset_str (die->sect_off), objfile_name (objfile));
13807 return;
13808 }
13809 pc = attr->as_address () + baseaddr;
13810 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13811
13812 if (cu->call_site_htab == NULL)
13813 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13814 NULL, &objfile->objfile_obstack,
13815 hashtab_obstack_allocate, NULL);
13816 call_site_local.pc = pc;
13817 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13818 if (*slot != NULL)
13819 {
13820 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13821 "DIE %s [in module %s]"),
13822 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13823 objfile_name (objfile));
13824 return;
13825 }
13826
13827 /* Count parameters at the caller. */
13828
13829 nparams = 0;
13830 for (child_die = die->child; child_die && child_die->tag;
13831 child_die = child_die->sibling)
13832 {
13833 if (child_die->tag != DW_TAG_call_site_parameter
13834 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13835 {
13836 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13837 "DW_TAG_call_site child DIE %s [in module %s]"),
13838 child_die->tag, sect_offset_str (child_die->sect_off),
13839 objfile_name (objfile));
13840 continue;
13841 }
13842
13843 nparams++;
13844 }
13845
13846 call_site
13847 = ((struct call_site *)
13848 obstack_alloc (&objfile->objfile_obstack,
13849 sizeof (*call_site)
13850 + (sizeof (*call_site->parameter) * (nparams - 1))));
13851 *slot = call_site;
13852 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13853 call_site->pc = pc;
13854
13855 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13856 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13857 {
13858 struct die_info *func_die;
13859
13860 /* Skip also over DW_TAG_inlined_subroutine. */
13861 for (func_die = die->parent;
13862 func_die && func_die->tag != DW_TAG_subprogram
13863 && func_die->tag != DW_TAG_subroutine_type;
13864 func_die = func_die->parent);
13865
13866 /* DW_AT_call_all_calls is a superset
13867 of DW_AT_call_all_tail_calls. */
13868 if (func_die
13869 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13870 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13871 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13872 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13873 {
13874 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13875 not complete. But keep CALL_SITE for look ups via call_site_htab,
13876 both the initial caller containing the real return address PC and
13877 the final callee containing the current PC of a chain of tail
13878 calls do not need to have the tail call list complete. But any
13879 function candidate for a virtual tail call frame searched via
13880 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13881 determined unambiguously. */
13882 }
13883 else
13884 {
13885 struct type *func_type = NULL;
13886
13887 if (func_die)
13888 func_type = get_die_type (func_die, cu);
13889 if (func_type != NULL)
13890 {
13891 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13892
13893 /* Enlist this call site to the function. */
13894 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13895 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13896 }
13897 else
13898 complaint (_("Cannot find function owning DW_TAG_call_site "
13899 "DIE %s [in module %s]"),
13900 sect_offset_str (die->sect_off), objfile_name (objfile));
13901 }
13902 }
13903
13904 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13905 if (attr == NULL)
13906 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13907 if (attr == NULL)
13908 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13909 if (attr == NULL)
13910 {
13911 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13912 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13913 }
13914 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13915 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13916 /* Keep NULL DWARF_BLOCK. */;
13917 else if (attr->form_is_block ())
13918 {
13919 struct dwarf2_locexpr_baton *dlbaton;
13920 struct dwarf_block *block = attr->as_block ();
13921
13922 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13923 dlbaton->data = block->data;
13924 dlbaton->size = block->size;
13925 dlbaton->per_objfile = per_objfile;
13926 dlbaton->per_cu = cu->per_cu;
13927
13928 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13929 }
13930 else if (attr->form_is_ref ())
13931 {
13932 struct dwarf2_cu *target_cu = cu;
13933 struct die_info *target_die;
13934
13935 target_die = follow_die_ref (die, attr, &target_cu);
13936 gdb_assert (target_cu->per_objfile->objfile == objfile);
13937 if (die_is_declaration (target_die, target_cu))
13938 {
13939 const char *target_physname;
13940
13941 /* Prefer the mangled name; otherwise compute the demangled one. */
13942 target_physname = dw2_linkage_name (target_die, target_cu);
13943 if (target_physname == NULL)
13944 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13945 if (target_physname == NULL)
13946 complaint (_("DW_AT_call_target target DIE has invalid "
13947 "physname, for referencing DIE %s [in module %s]"),
13948 sect_offset_str (die->sect_off), objfile_name (objfile));
13949 else
13950 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13951 }
13952 else
13953 {
13954 CORE_ADDR lowpc;
13955
13956 /* DW_AT_entry_pc should be preferred. */
13957 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13958 <= PC_BOUNDS_INVALID)
13959 complaint (_("DW_AT_call_target target DIE has invalid "
13960 "low pc, for referencing DIE %s [in module %s]"),
13961 sect_offset_str (die->sect_off), objfile_name (objfile));
13962 else
13963 {
13964 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13965 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13966 }
13967 }
13968 }
13969 else
13970 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13971 "block nor reference, for DIE %s [in module %s]"),
13972 sect_offset_str (die->sect_off), objfile_name (objfile));
13973
13974 call_site->per_cu = cu->per_cu;
13975 call_site->per_objfile = per_objfile;
13976
13977 for (child_die = die->child;
13978 child_die && child_die->tag;
13979 child_die = child_die->sibling)
13980 {
13981 struct call_site_parameter *parameter;
13982 struct attribute *loc, *origin;
13983
13984 if (child_die->tag != DW_TAG_call_site_parameter
13985 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13986 {
13987 /* Already printed the complaint above. */
13988 continue;
13989 }
13990
13991 gdb_assert (call_site->parameter_count < nparams);
13992 parameter = &call_site->parameter[call_site->parameter_count];
13993
13994 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13995 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13996 register is contained in DW_AT_call_value. */
13997
13998 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13999 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14000 if (origin == NULL)
14001 {
14002 /* This was a pre-DWARF-5 GNU extension alias
14003 for DW_AT_call_parameter. */
14004 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14005 }
14006 if (loc == NULL && origin != NULL && origin->form_is_ref ())
14007 {
14008 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14009
14010 sect_offset sect_off = origin->get_ref_die_offset ();
14011 if (!cu->header.offset_in_cu_p (sect_off))
14012 {
14013 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14014 binding can be done only inside one CU. Such referenced DIE
14015 therefore cannot be even moved to DW_TAG_partial_unit. */
14016 complaint (_("DW_AT_call_parameter offset is not in CU for "
14017 "DW_TAG_call_site child DIE %s [in module %s]"),
14018 sect_offset_str (child_die->sect_off),
14019 objfile_name (objfile));
14020 continue;
14021 }
14022 parameter->u.param_cu_off
14023 = (cu_offset) (sect_off - cu->header.sect_off);
14024 }
14025 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
14026 {
14027 complaint (_("No DW_FORM_block* DW_AT_location for "
14028 "DW_TAG_call_site child DIE %s [in module %s]"),
14029 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14030 continue;
14031 }
14032 else
14033 {
14034 struct dwarf_block *block = loc->as_block ();
14035
14036 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14037 (block->data, &block->data[block->size]);
14038 if (parameter->u.dwarf_reg != -1)
14039 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14040 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14041 &block->data[block->size],
14042 &parameter->u.fb_offset))
14043 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14044 else
14045 {
14046 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14047 "for DW_FORM_block* DW_AT_location is supported for "
14048 "DW_TAG_call_site child DIE %s "
14049 "[in module %s]"),
14050 sect_offset_str (child_die->sect_off),
14051 objfile_name (objfile));
14052 continue;
14053 }
14054 }
14055
14056 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14057 if (attr == NULL)
14058 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14059 if (attr == NULL || !attr->form_is_block ())
14060 {
14061 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14062 "DW_TAG_call_site child DIE %s [in module %s]"),
14063 sect_offset_str (child_die->sect_off),
14064 objfile_name (objfile));
14065 continue;
14066 }
14067
14068 struct dwarf_block *block = attr->as_block ();
14069 parameter->value = block->data;
14070 parameter->value_size = block->size;
14071
14072 /* Parameters are not pre-cleared by memset above. */
14073 parameter->data_value = NULL;
14074 parameter->data_value_size = 0;
14075 call_site->parameter_count++;
14076
14077 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14078 if (attr == NULL)
14079 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14080 if (attr != nullptr)
14081 {
14082 if (!attr->form_is_block ())
14083 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14084 "DW_TAG_call_site child DIE %s [in module %s]"),
14085 sect_offset_str (child_die->sect_off),
14086 objfile_name (objfile));
14087 else
14088 {
14089 block = attr->as_block ();
14090 parameter->data_value = block->data;
14091 parameter->data_value_size = block->size;
14092 }
14093 }
14094 }
14095 }
14096
14097 /* Helper function for read_variable. If DIE represents a virtual
14098 table, then return the type of the concrete object that is
14099 associated with the virtual table. Otherwise, return NULL. */
14100
14101 static struct type *
14102 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14103 {
14104 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14105 if (attr == NULL)
14106 return NULL;
14107
14108 /* Find the type DIE. */
14109 struct die_info *type_die = NULL;
14110 struct dwarf2_cu *type_cu = cu;
14111
14112 if (attr->form_is_ref ())
14113 type_die = follow_die_ref (die, attr, &type_cu);
14114 if (type_die == NULL)
14115 return NULL;
14116
14117 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14118 return NULL;
14119 return die_containing_type (type_die, type_cu);
14120 }
14121
14122 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14123
14124 static void
14125 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14126 {
14127 struct rust_vtable_symbol *storage = NULL;
14128
14129 if (cu->language == language_rust)
14130 {
14131 struct type *containing_type = rust_containing_type (die, cu);
14132
14133 if (containing_type != NULL)
14134 {
14135 struct objfile *objfile = cu->per_objfile->objfile;
14136
14137 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
14138 storage->concrete_type = containing_type;
14139 storage->subclass = SYMBOL_RUST_VTABLE;
14140 }
14141 }
14142
14143 struct symbol *res = new_symbol (die, NULL, cu, storage);
14144 struct attribute *abstract_origin
14145 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14146 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14147 if (res == NULL && loc && abstract_origin)
14148 {
14149 /* We have a variable without a name, but with a location and an abstract
14150 origin. This may be a concrete instance of an abstract variable
14151 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14152 later. */
14153 struct dwarf2_cu *origin_cu = cu;
14154 struct die_info *origin_die
14155 = follow_die_ref (die, abstract_origin, &origin_cu);
14156 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14157 per_objfile->per_bfd->abstract_to_concrete
14158 [origin_die->sect_off].push_back (die->sect_off);
14159 }
14160 }
14161
14162 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14163 reading .debug_rnglists.
14164 Callback's type should be:
14165 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14166 Return true if the attributes are present and valid, otherwise,
14167 return false. */
14168
14169 template <typename Callback>
14170 static bool
14171 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14172 dwarf_tag tag, Callback &&callback)
14173 {
14174 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14175 struct objfile *objfile = per_objfile->objfile;
14176 bfd *obfd = objfile->obfd;
14177 /* Base address selection entry. */
14178 gdb::optional<CORE_ADDR> base;
14179 const gdb_byte *buffer;
14180 CORE_ADDR baseaddr;
14181 bool overflow = false;
14182 ULONGEST addr_index;
14183 struct dwarf2_section_info *rnglists_section;
14184
14185 base = cu->base_address;
14186 rnglists_section = cu_debug_rnglists_section (cu, tag);
14187 rnglists_section->read (objfile);
14188
14189 if (offset >= rnglists_section->size)
14190 {
14191 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14192 offset);
14193 return false;
14194 }
14195 buffer = rnglists_section->buffer + offset;
14196
14197 baseaddr = objfile->text_section_offset ();
14198
14199 while (1)
14200 {
14201 /* Initialize it due to a false compiler warning. */
14202 CORE_ADDR range_beginning = 0, range_end = 0;
14203 const gdb_byte *buf_end = (rnglists_section->buffer
14204 + rnglists_section->size);
14205 unsigned int bytes_read;
14206
14207 if (buffer == buf_end)
14208 {
14209 overflow = true;
14210 break;
14211 }
14212 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14213 switch (rlet)
14214 {
14215 case DW_RLE_end_of_list:
14216 break;
14217 case DW_RLE_base_address:
14218 if (buffer + cu->header.addr_size > buf_end)
14219 {
14220 overflow = true;
14221 break;
14222 }
14223 base = cu->header.read_address (obfd, buffer, &bytes_read);
14224 buffer += bytes_read;
14225 break;
14226 case DW_RLE_base_addressx:
14227 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14228 buffer += bytes_read;
14229 base = read_addr_index (cu, addr_index);
14230 break;
14231 case DW_RLE_start_length:
14232 if (buffer + cu->header.addr_size > buf_end)
14233 {
14234 overflow = true;
14235 break;
14236 }
14237 range_beginning = cu->header.read_address (obfd, buffer,
14238 &bytes_read);
14239 buffer += bytes_read;
14240 range_end = (range_beginning
14241 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14242 buffer += bytes_read;
14243 if (buffer > buf_end)
14244 {
14245 overflow = true;
14246 break;
14247 }
14248 break;
14249 case DW_RLE_startx_length:
14250 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14251 buffer += bytes_read;
14252 range_beginning = read_addr_index (cu, addr_index);
14253 if (buffer > buf_end)
14254 {
14255 overflow = true;
14256 break;
14257 }
14258 range_end = (range_beginning
14259 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14260 buffer += bytes_read;
14261 break;
14262 case DW_RLE_offset_pair:
14263 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14264 buffer += bytes_read;
14265 if (buffer > buf_end)
14266 {
14267 overflow = true;
14268 break;
14269 }
14270 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14271 buffer += bytes_read;
14272 if (buffer > buf_end)
14273 {
14274 overflow = true;
14275 break;
14276 }
14277 break;
14278 case DW_RLE_start_end:
14279 if (buffer + 2 * cu->header.addr_size > buf_end)
14280 {
14281 overflow = true;
14282 break;
14283 }
14284 range_beginning = cu->header.read_address (obfd, buffer,
14285 &bytes_read);
14286 buffer += bytes_read;
14287 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
14288 buffer += bytes_read;
14289 break;
14290 case DW_RLE_startx_endx:
14291 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14292 buffer += bytes_read;
14293 range_beginning = read_addr_index (cu, addr_index);
14294 if (buffer > buf_end)
14295 {
14296 overflow = true;
14297 break;
14298 }
14299 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14300 buffer += bytes_read;
14301 range_end = read_addr_index (cu, addr_index);
14302 break;
14303 default:
14304 complaint (_("Invalid .debug_rnglists data (no base address)"));
14305 return false;
14306 }
14307 if (rlet == DW_RLE_end_of_list || overflow)
14308 break;
14309 if (rlet == DW_RLE_base_address)
14310 continue;
14311
14312 if (range_beginning > range_end)
14313 {
14314 /* Inverted range entries are invalid. */
14315 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14316 return false;
14317 }
14318
14319 /* Empty range entries have no effect. */
14320 if (range_beginning == range_end)
14321 continue;
14322
14323 /* Only DW_RLE_offset_pair needs the base address added. */
14324 if (rlet == DW_RLE_offset_pair)
14325 {
14326 if (!base.has_value ())
14327 {
14328 /* We have no valid base address for the DW_RLE_offset_pair. */
14329 complaint (_("Invalid .debug_rnglists data (no base address for "
14330 "DW_RLE_offset_pair)"));
14331 return false;
14332 }
14333
14334 range_beginning += *base;
14335 range_end += *base;
14336 }
14337
14338 /* A not-uncommon case of bad debug info.
14339 Don't pollute the addrmap with bad data. */
14340 if (range_beginning + baseaddr == 0
14341 && !per_objfile->per_bfd->has_section_at_zero)
14342 {
14343 complaint (_(".debug_rnglists entry has start address of zero"
14344 " [in module %s]"), objfile_name (objfile));
14345 continue;
14346 }
14347
14348 callback (range_beginning, range_end);
14349 }
14350
14351 if (overflow)
14352 {
14353 complaint (_("Offset %d is not terminated "
14354 "for DW_AT_ranges attribute"),
14355 offset);
14356 return false;
14357 }
14358
14359 return true;
14360 }
14361
14362 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14363 Callback's type should be:
14364 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14365 Return 1 if the attributes are present and valid, otherwise, return 0. */
14366
14367 template <typename Callback>
14368 static int
14369 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
14370 Callback &&callback)
14371 {
14372 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14373 struct objfile *objfile = per_objfile->objfile;
14374 struct comp_unit_head *cu_header = &cu->header;
14375 bfd *obfd = objfile->obfd;
14376 unsigned int addr_size = cu_header->addr_size;
14377 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14378 /* Base address selection entry. */
14379 gdb::optional<CORE_ADDR> base;
14380 unsigned int dummy;
14381 const gdb_byte *buffer;
14382 CORE_ADDR baseaddr;
14383
14384 if (cu_header->version >= 5)
14385 return dwarf2_rnglists_process (offset, cu, tag, callback);
14386
14387 base = cu->base_address;
14388
14389 per_objfile->per_bfd->ranges.read (objfile);
14390 if (offset >= per_objfile->per_bfd->ranges.size)
14391 {
14392 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14393 offset);
14394 return 0;
14395 }
14396 buffer = per_objfile->per_bfd->ranges.buffer + offset;
14397
14398 baseaddr = objfile->text_section_offset ();
14399
14400 while (1)
14401 {
14402 CORE_ADDR range_beginning, range_end;
14403
14404 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
14405 buffer += addr_size;
14406 range_end = cu->header.read_address (obfd, buffer, &dummy);
14407 buffer += addr_size;
14408 offset += 2 * addr_size;
14409
14410 /* An end of list marker is a pair of zero addresses. */
14411 if (range_beginning == 0 && range_end == 0)
14412 /* Found the end of list entry. */
14413 break;
14414
14415 /* Each base address selection entry is a pair of 2 values.
14416 The first is the largest possible address, the second is
14417 the base address. Check for a base address here. */
14418 if ((range_beginning & mask) == mask)
14419 {
14420 /* If we found the largest possible address, then we already
14421 have the base address in range_end. */
14422 base = range_end;
14423 continue;
14424 }
14425
14426 if (!base.has_value ())
14427 {
14428 /* We have no valid base address for the ranges
14429 data. */
14430 complaint (_("Invalid .debug_ranges data (no base address)"));
14431 return 0;
14432 }
14433
14434 if (range_beginning > range_end)
14435 {
14436 /* Inverted range entries are invalid. */
14437 complaint (_("Invalid .debug_ranges data (inverted range)"));
14438 return 0;
14439 }
14440
14441 /* Empty range entries have no effect. */
14442 if (range_beginning == range_end)
14443 continue;
14444
14445 range_beginning += *base;
14446 range_end += *base;
14447
14448 /* A not-uncommon case of bad debug info.
14449 Don't pollute the addrmap with bad data. */
14450 if (range_beginning + baseaddr == 0
14451 && !per_objfile->per_bfd->has_section_at_zero)
14452 {
14453 complaint (_(".debug_ranges entry has start address of zero"
14454 " [in module %s]"), objfile_name (objfile));
14455 continue;
14456 }
14457
14458 callback (range_beginning, range_end);
14459 }
14460
14461 return 1;
14462 }
14463
14464 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14465 Return 1 if the attributes are present and valid, otherwise, return 0.
14466 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14467
14468 static int
14469 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14470 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14471 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14472 {
14473 struct objfile *objfile = cu->per_objfile->objfile;
14474 struct gdbarch *gdbarch = objfile->arch ();
14475 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14476 int low_set = 0;
14477 CORE_ADDR low = 0;
14478 CORE_ADDR high = 0;
14479 int retval;
14480
14481 retval = dwarf2_ranges_process (offset, cu, tag,
14482 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14483 {
14484 if (ranges_pst != NULL)
14485 {
14486 CORE_ADDR lowpc;
14487 CORE_ADDR highpc;
14488
14489 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14490 range_beginning + baseaddr)
14491 - baseaddr);
14492 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14493 range_end + baseaddr)
14494 - baseaddr);
14495 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14496 lowpc, highpc - 1, ranges_pst);
14497 }
14498
14499 /* FIXME: This is recording everything as a low-high
14500 segment of consecutive addresses. We should have a
14501 data structure for discontiguous block ranges
14502 instead. */
14503 if (! low_set)
14504 {
14505 low = range_beginning;
14506 high = range_end;
14507 low_set = 1;
14508 }
14509 else
14510 {
14511 if (range_beginning < low)
14512 low = range_beginning;
14513 if (range_end > high)
14514 high = range_end;
14515 }
14516 });
14517 if (!retval)
14518 return 0;
14519
14520 if (! low_set)
14521 /* If the first entry is an end-of-list marker, the range
14522 describes an empty scope, i.e. no instructions. */
14523 return 0;
14524
14525 if (low_return)
14526 *low_return = low;
14527 if (high_return)
14528 *high_return = high;
14529 return 1;
14530 }
14531
14532 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14533 definition for the return value. *LOWPC and *HIGHPC are set iff
14534 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14535
14536 static enum pc_bounds_kind
14537 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14538 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14539 dwarf2_psymtab *pst)
14540 {
14541 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14542 struct attribute *attr;
14543 struct attribute *attr_high;
14544 CORE_ADDR low = 0;
14545 CORE_ADDR high = 0;
14546 enum pc_bounds_kind ret;
14547
14548 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14549 if (attr_high)
14550 {
14551 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14552 if (attr != nullptr)
14553 {
14554 low = attr->as_address ();
14555 high = attr_high->as_address ();
14556 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14557 high += low;
14558 }
14559 else
14560 /* Found high w/o low attribute. */
14561 return PC_BOUNDS_INVALID;
14562
14563 /* Found consecutive range of addresses. */
14564 ret = PC_BOUNDS_HIGH_LOW;
14565 }
14566 else
14567 {
14568 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14569 if (attr != nullptr && attr->form_is_unsigned ())
14570 {
14571 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14572 We take advantage of the fact that DW_AT_ranges does not appear
14573 in DW_TAG_compile_unit of DWO files.
14574
14575 Attributes of the form DW_FORM_rnglistx have already had their
14576 value changed by read_rnglist_index and already include
14577 DW_AT_rnglists_base, so don't need to add the ranges base,
14578 either. */
14579 int need_ranges_base = (die->tag != DW_TAG_compile_unit
14580 && attr->form != DW_FORM_rnglistx);
14581 unsigned int ranges_offset = (attr->as_unsigned ()
14582 + (need_ranges_base
14583 ? cu->ranges_base
14584 : 0));
14585
14586 /* Value of the DW_AT_ranges attribute is the offset in the
14587 .debug_ranges section. */
14588 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14589 die->tag))
14590 return PC_BOUNDS_INVALID;
14591 /* Found discontinuous range of addresses. */
14592 ret = PC_BOUNDS_RANGES;
14593 }
14594 else
14595 return PC_BOUNDS_NOT_PRESENT;
14596 }
14597
14598 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14599 if (high <= low)
14600 return PC_BOUNDS_INVALID;
14601
14602 /* When using the GNU linker, .gnu.linkonce. sections are used to
14603 eliminate duplicate copies of functions and vtables and such.
14604 The linker will arbitrarily choose one and discard the others.
14605 The AT_*_pc values for such functions refer to local labels in
14606 these sections. If the section from that file was discarded, the
14607 labels are not in the output, so the relocs get a value of 0.
14608 If this is a discarded function, mark the pc bounds as invalid,
14609 so that GDB will ignore it. */
14610 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14611 return PC_BOUNDS_INVALID;
14612
14613 *lowpc = low;
14614 if (highpc)
14615 *highpc = high;
14616 return ret;
14617 }
14618
14619 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14620 its low and high PC addresses. Do nothing if these addresses could not
14621 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14622 and HIGHPC to the high address if greater than HIGHPC. */
14623
14624 static void
14625 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14626 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14627 struct dwarf2_cu *cu)
14628 {
14629 CORE_ADDR low, high;
14630 struct die_info *child = die->child;
14631
14632 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14633 {
14634 *lowpc = std::min (*lowpc, low);
14635 *highpc = std::max (*highpc, high);
14636 }
14637
14638 /* If the language does not allow nested subprograms (either inside
14639 subprograms or lexical blocks), we're done. */
14640 if (cu->language != language_ada)
14641 return;
14642
14643 /* Check all the children of the given DIE. If it contains nested
14644 subprograms, then check their pc bounds. Likewise, we need to
14645 check lexical blocks as well, as they may also contain subprogram
14646 definitions. */
14647 while (child && child->tag)
14648 {
14649 if (child->tag == DW_TAG_subprogram
14650 || child->tag == DW_TAG_lexical_block)
14651 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14652 child = child->sibling;
14653 }
14654 }
14655
14656 /* Get the low and high pc's represented by the scope DIE, and store
14657 them in *LOWPC and *HIGHPC. If the correct values can't be
14658 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14659
14660 static void
14661 get_scope_pc_bounds (struct die_info *die,
14662 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14663 struct dwarf2_cu *cu)
14664 {
14665 CORE_ADDR best_low = (CORE_ADDR) -1;
14666 CORE_ADDR best_high = (CORE_ADDR) 0;
14667 CORE_ADDR current_low, current_high;
14668
14669 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14670 >= PC_BOUNDS_RANGES)
14671 {
14672 best_low = current_low;
14673 best_high = current_high;
14674 }
14675 else
14676 {
14677 struct die_info *child = die->child;
14678
14679 while (child && child->tag)
14680 {
14681 switch (child->tag) {
14682 case DW_TAG_subprogram:
14683 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14684 break;
14685 case DW_TAG_namespace:
14686 case DW_TAG_module:
14687 /* FIXME: carlton/2004-01-16: Should we do this for
14688 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14689 that current GCC's always emit the DIEs corresponding
14690 to definitions of methods of classes as children of a
14691 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14692 the DIEs giving the declarations, which could be
14693 anywhere). But I don't see any reason why the
14694 standards says that they have to be there. */
14695 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14696
14697 if (current_low != ((CORE_ADDR) -1))
14698 {
14699 best_low = std::min (best_low, current_low);
14700 best_high = std::max (best_high, current_high);
14701 }
14702 break;
14703 default:
14704 /* Ignore. */
14705 break;
14706 }
14707
14708 child = child->sibling;
14709 }
14710 }
14711
14712 *lowpc = best_low;
14713 *highpc = best_high;
14714 }
14715
14716 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14717 in DIE. */
14718
14719 static void
14720 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14721 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14722 {
14723 struct objfile *objfile = cu->per_objfile->objfile;
14724 struct gdbarch *gdbarch = objfile->arch ();
14725 struct attribute *attr;
14726 struct attribute *attr_high;
14727
14728 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14729 if (attr_high)
14730 {
14731 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14732 if (attr != nullptr)
14733 {
14734 CORE_ADDR low = attr->as_address ();
14735 CORE_ADDR high = attr_high->as_address ();
14736
14737 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14738 high += low;
14739
14740 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14741 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14742 cu->get_builder ()->record_block_range (block, low, high - 1);
14743 }
14744 }
14745
14746 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14747 if (attr != nullptr && attr->form_is_unsigned ())
14748 {
14749 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14750 We take advantage of the fact that DW_AT_ranges does not appear
14751 in DW_TAG_compile_unit of DWO files.
14752
14753 Attributes of the form DW_FORM_rnglistx have already had their
14754 value changed by read_rnglist_index and already include
14755 DW_AT_rnglists_base, so don't need to add the ranges base,
14756 either. */
14757 int need_ranges_base = (die->tag != DW_TAG_compile_unit
14758 && attr->form != DW_FORM_rnglistx);
14759
14760 /* The value of the DW_AT_ranges attribute is the offset of the
14761 address range list in the .debug_ranges section. */
14762 unsigned long offset = (attr->as_unsigned ()
14763 + (need_ranges_base ? cu->ranges_base : 0));
14764
14765 std::vector<blockrange> blockvec;
14766 dwarf2_ranges_process (offset, cu, die->tag,
14767 [&] (CORE_ADDR start, CORE_ADDR end)
14768 {
14769 start += baseaddr;
14770 end += baseaddr;
14771 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14772 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14773 cu->get_builder ()->record_block_range (block, start, end - 1);
14774 blockvec.emplace_back (start, end);
14775 });
14776
14777 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14778 }
14779 }
14780
14781 /* Check whether the producer field indicates either of GCC < 4.6, or the
14782 Intel C/C++ compiler, and cache the result in CU. */
14783
14784 static void
14785 check_producer (struct dwarf2_cu *cu)
14786 {
14787 int major, minor;
14788
14789 if (cu->producer == NULL)
14790 {
14791 /* For unknown compilers expect their behavior is DWARF version
14792 compliant.
14793
14794 GCC started to support .debug_types sections by -gdwarf-4 since
14795 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14796 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14797 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14798 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14799 }
14800 else if (producer_is_gcc (cu->producer, &major, &minor))
14801 {
14802 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14803 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14804 }
14805 else if (producer_is_icc (cu->producer, &major, &minor))
14806 {
14807 cu->producer_is_icc = true;
14808 cu->producer_is_icc_lt_14 = major < 14;
14809 }
14810 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14811 cu->producer_is_codewarrior = true;
14812 else
14813 {
14814 /* For other non-GCC compilers, expect their behavior is DWARF version
14815 compliant. */
14816 }
14817
14818 cu->checked_producer = true;
14819 }
14820
14821 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14822 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14823 during 4.6.0 experimental. */
14824
14825 static bool
14826 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14827 {
14828 if (!cu->checked_producer)
14829 check_producer (cu);
14830
14831 return cu->producer_is_gxx_lt_4_6;
14832 }
14833
14834
14835 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14836 with incorrect is_stmt attributes. */
14837
14838 static bool
14839 producer_is_codewarrior (struct dwarf2_cu *cu)
14840 {
14841 if (!cu->checked_producer)
14842 check_producer (cu);
14843
14844 return cu->producer_is_codewarrior;
14845 }
14846
14847 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14848 If that attribute is not available, return the appropriate
14849 default. */
14850
14851 static enum dwarf_access_attribute
14852 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14853 {
14854 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14855 if (attr != nullptr)
14856 {
14857 LONGEST value = attr->constant_value (-1);
14858 if (value == DW_ACCESS_public
14859 || value == DW_ACCESS_protected
14860 || value == DW_ACCESS_private)
14861 return (dwarf_access_attribute) value;
14862 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14863 plongest (value));
14864 }
14865
14866 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14867 {
14868 /* The default DWARF 2 accessibility for members is public, the default
14869 accessibility for inheritance is private. */
14870
14871 if (die->tag != DW_TAG_inheritance)
14872 return DW_ACCESS_public;
14873 else
14874 return DW_ACCESS_private;
14875 }
14876 else
14877 {
14878 /* DWARF 3+ defines the default accessibility a different way. The same
14879 rules apply now for DW_TAG_inheritance as for the members and it only
14880 depends on the container kind. */
14881
14882 if (die->parent->tag == DW_TAG_class_type)
14883 return DW_ACCESS_private;
14884 else
14885 return DW_ACCESS_public;
14886 }
14887 }
14888
14889 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14890 offset. If the attribute was not found return 0, otherwise return
14891 1. If it was found but could not properly be handled, set *OFFSET
14892 to 0. */
14893
14894 static int
14895 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14896 LONGEST *offset)
14897 {
14898 struct attribute *attr;
14899
14900 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14901 if (attr != NULL)
14902 {
14903 *offset = 0;
14904
14905 /* Note that we do not check for a section offset first here.
14906 This is because DW_AT_data_member_location is new in DWARF 4,
14907 so if we see it, we can assume that a constant form is really
14908 a constant and not a section offset. */
14909 if (attr->form_is_constant ())
14910 *offset = attr->constant_value (0);
14911 else if (attr->form_is_section_offset ())
14912 dwarf2_complex_location_expr_complaint ();
14913 else if (attr->form_is_block ())
14914 *offset = decode_locdesc (attr->as_block (), cu);
14915 else
14916 dwarf2_complex_location_expr_complaint ();
14917
14918 return 1;
14919 }
14920
14921 return 0;
14922 }
14923
14924 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14925
14926 static void
14927 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14928 struct field *field)
14929 {
14930 struct attribute *attr;
14931
14932 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14933 if (attr != NULL)
14934 {
14935 if (attr->form_is_constant ())
14936 {
14937 LONGEST offset = attr->constant_value (0);
14938 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14939 }
14940 else if (attr->form_is_section_offset ())
14941 dwarf2_complex_location_expr_complaint ();
14942 else if (attr->form_is_block ())
14943 {
14944 bool handled;
14945 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
14946 if (handled)
14947 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14948 else
14949 {
14950 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14951 struct objfile *objfile = per_objfile->objfile;
14952 struct dwarf2_locexpr_baton *dlbaton
14953 = XOBNEW (&objfile->objfile_obstack,
14954 struct dwarf2_locexpr_baton);
14955 dlbaton->data = attr->as_block ()->data;
14956 dlbaton->size = attr->as_block ()->size;
14957 /* When using this baton, we want to compute the address
14958 of the field, not the value. This is why
14959 is_reference is set to false here. */
14960 dlbaton->is_reference = false;
14961 dlbaton->per_objfile = per_objfile;
14962 dlbaton->per_cu = cu->per_cu;
14963
14964 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14965 }
14966 }
14967 else
14968 dwarf2_complex_location_expr_complaint ();
14969 }
14970 }
14971
14972 /* Add an aggregate field to the field list. */
14973
14974 static void
14975 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14976 struct dwarf2_cu *cu)
14977 {
14978 struct objfile *objfile = cu->per_objfile->objfile;
14979 struct gdbarch *gdbarch = objfile->arch ();
14980 struct nextfield *new_field;
14981 struct attribute *attr;
14982 struct field *fp;
14983 const char *fieldname = "";
14984
14985 if (die->tag == DW_TAG_inheritance)
14986 {
14987 fip->baseclasses.emplace_back ();
14988 new_field = &fip->baseclasses.back ();
14989 }
14990 else
14991 {
14992 fip->fields.emplace_back ();
14993 new_field = &fip->fields.back ();
14994 }
14995
14996 new_field->offset = die->sect_off;
14997
14998 new_field->accessibility = dwarf2_access_attribute (die, cu);
14999 if (new_field->accessibility != DW_ACCESS_public)
15000 fip->non_public_fields = true;
15001
15002 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15003 if (attr != nullptr)
15004 new_field->virtuality = attr->as_virtuality ();
15005 else
15006 new_field->virtuality = DW_VIRTUALITY_none;
15007
15008 fp = &new_field->field;
15009
15010 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15011 {
15012 /* Data member other than a C++ static data member. */
15013
15014 /* Get type of field. */
15015 fp->set_type (die_type (die, cu));
15016
15017 SET_FIELD_BITPOS (*fp, 0);
15018
15019 /* Get bit size of field (zero if none). */
15020 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15021 if (attr != nullptr)
15022 {
15023 FIELD_BITSIZE (*fp) = attr->constant_value (0);
15024 }
15025 else
15026 {
15027 FIELD_BITSIZE (*fp) = 0;
15028 }
15029
15030 /* Get bit offset of field. */
15031 handle_data_member_location (die, cu, fp);
15032 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15033 if (attr != nullptr && attr->form_is_constant ())
15034 {
15035 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15036 {
15037 /* For big endian bits, the DW_AT_bit_offset gives the
15038 additional bit offset from the MSB of the containing
15039 anonymous object to the MSB of the field. We don't
15040 have to do anything special since we don't need to
15041 know the size of the anonymous object. */
15042 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15043 + attr->constant_value (0)));
15044 }
15045 else
15046 {
15047 /* For little endian bits, compute the bit offset to the
15048 MSB of the anonymous object, subtract off the number of
15049 bits from the MSB of the field to the MSB of the
15050 object, and then subtract off the number of bits of
15051 the field itself. The result is the bit offset of
15052 the LSB of the field. */
15053 int anonymous_size;
15054 int bit_offset = attr->constant_value (0);
15055
15056 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15057 if (attr != nullptr && attr->form_is_constant ())
15058 {
15059 /* The size of the anonymous object containing
15060 the bit field is explicit, so use the
15061 indicated size (in bytes). */
15062 anonymous_size = attr->constant_value (0);
15063 }
15064 else
15065 {
15066 /* The size of the anonymous object containing
15067 the bit field must be inferred from the type
15068 attribute of the data member containing the
15069 bit field. */
15070 anonymous_size = TYPE_LENGTH (fp->type ());
15071 }
15072 SET_FIELD_BITPOS (*fp,
15073 (FIELD_BITPOS (*fp)
15074 + anonymous_size * bits_per_byte
15075 - bit_offset - FIELD_BITSIZE (*fp)));
15076 }
15077 }
15078 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15079 if (attr != NULL)
15080 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15081 + attr->constant_value (0)));
15082
15083 /* Get name of field. */
15084 fieldname = dwarf2_name (die, cu);
15085 if (fieldname == NULL)
15086 fieldname = "";
15087
15088 /* The name is already allocated along with this objfile, so we don't
15089 need to duplicate it for the type. */
15090 fp->name = fieldname;
15091
15092 /* Change accessibility for artificial fields (e.g. virtual table
15093 pointer or virtual base class pointer) to private. */
15094 if (dwarf2_attr (die, DW_AT_artificial, cu))
15095 {
15096 FIELD_ARTIFICIAL (*fp) = 1;
15097 new_field->accessibility = DW_ACCESS_private;
15098 fip->non_public_fields = true;
15099 }
15100 }
15101 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15102 {
15103 /* C++ static member. */
15104
15105 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15106 is a declaration, but all versions of G++ as of this writing
15107 (so through at least 3.2.1) incorrectly generate
15108 DW_TAG_variable tags. */
15109
15110 const char *physname;
15111
15112 /* Get name of field. */
15113 fieldname = dwarf2_name (die, cu);
15114 if (fieldname == NULL)
15115 return;
15116
15117 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15118 if (attr
15119 /* Only create a symbol if this is an external value.
15120 new_symbol checks this and puts the value in the global symbol
15121 table, which we want. If it is not external, new_symbol
15122 will try to put the value in cu->list_in_scope which is wrong. */
15123 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15124 {
15125 /* A static const member, not much different than an enum as far as
15126 we're concerned, except that we can support more types. */
15127 new_symbol (die, NULL, cu);
15128 }
15129
15130 /* Get physical name. */
15131 physname = dwarf2_physname (fieldname, die, cu);
15132
15133 /* The name is already allocated along with this objfile, so we don't
15134 need to duplicate it for the type. */
15135 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15136 fp->set_type (die_type (die, cu));
15137 FIELD_NAME (*fp) = fieldname;
15138 }
15139 else if (die->tag == DW_TAG_inheritance)
15140 {
15141 /* C++ base class field. */
15142 handle_data_member_location (die, cu, fp);
15143 FIELD_BITSIZE (*fp) = 0;
15144 fp->set_type (die_type (die, cu));
15145 FIELD_NAME (*fp) = fp->type ()->name ();
15146 }
15147 else
15148 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15149 }
15150
15151 /* Can the type given by DIE define another type? */
15152
15153 static bool
15154 type_can_define_types (const struct die_info *die)
15155 {
15156 switch (die->tag)
15157 {
15158 case DW_TAG_typedef:
15159 case DW_TAG_class_type:
15160 case DW_TAG_structure_type:
15161 case DW_TAG_union_type:
15162 case DW_TAG_enumeration_type:
15163 return true;
15164
15165 default:
15166 return false;
15167 }
15168 }
15169
15170 /* Add a type definition defined in the scope of the FIP's class. */
15171
15172 static void
15173 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15174 struct dwarf2_cu *cu)
15175 {
15176 struct decl_field fp;
15177 memset (&fp, 0, sizeof (fp));
15178
15179 gdb_assert (type_can_define_types (die));
15180
15181 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15182 fp.name = dwarf2_name (die, cu);
15183 fp.type = read_type_die (die, cu);
15184
15185 /* Save accessibility. */
15186 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15187 switch (accessibility)
15188 {
15189 case DW_ACCESS_public:
15190 /* The assumed value if neither private nor protected. */
15191 break;
15192 case DW_ACCESS_private:
15193 fp.is_private = 1;
15194 break;
15195 case DW_ACCESS_protected:
15196 fp.is_protected = 1;
15197 break;
15198 }
15199
15200 if (die->tag == DW_TAG_typedef)
15201 fip->typedef_field_list.push_back (fp);
15202 else
15203 fip->nested_types_list.push_back (fp);
15204 }
15205
15206 /* A convenience typedef that's used when finding the discriminant
15207 field for a variant part. */
15208 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15209 offset_map_type;
15210
15211 /* Compute the discriminant range for a given variant. OBSTACK is
15212 where the results will be stored. VARIANT is the variant to
15213 process. IS_UNSIGNED indicates whether the discriminant is signed
15214 or unsigned. */
15215
15216 static const gdb::array_view<discriminant_range>
15217 convert_variant_range (struct obstack *obstack, const variant_field &variant,
15218 bool is_unsigned)
15219 {
15220 std::vector<discriminant_range> ranges;
15221
15222 if (variant.default_branch)
15223 return {};
15224
15225 if (variant.discr_list_data == nullptr)
15226 {
15227 discriminant_range r
15228 = {variant.discriminant_value, variant.discriminant_value};
15229 ranges.push_back (r);
15230 }
15231 else
15232 {
15233 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15234 variant.discr_list_data->size);
15235 while (!data.empty ())
15236 {
15237 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15238 {
15239 complaint (_("invalid discriminant marker: %d"), data[0]);
15240 break;
15241 }
15242 bool is_range = data[0] == DW_DSC_range;
15243 data = data.slice (1);
15244
15245 ULONGEST low, high;
15246 unsigned int bytes_read;
15247
15248 if (data.empty ())
15249 {
15250 complaint (_("DW_AT_discr_list missing low value"));
15251 break;
15252 }
15253 if (is_unsigned)
15254 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15255 else
15256 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15257 &bytes_read);
15258 data = data.slice (bytes_read);
15259
15260 if (is_range)
15261 {
15262 if (data.empty ())
15263 {
15264 complaint (_("DW_AT_discr_list missing high value"));
15265 break;
15266 }
15267 if (is_unsigned)
15268 high = read_unsigned_leb128 (nullptr, data.data (),
15269 &bytes_read);
15270 else
15271 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15272 &bytes_read);
15273 data = data.slice (bytes_read);
15274 }
15275 else
15276 high = low;
15277
15278 ranges.push_back ({ low, high });
15279 }
15280 }
15281
15282 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15283 ranges.size ());
15284 std::copy (ranges.begin (), ranges.end (), result);
15285 return gdb::array_view<discriminant_range> (result, ranges.size ());
15286 }
15287
15288 static const gdb::array_view<variant_part> create_variant_parts
15289 (struct obstack *obstack,
15290 const offset_map_type &offset_map,
15291 struct field_info *fi,
15292 const std::vector<variant_part_builder> &variant_parts);
15293
15294 /* Fill in a "struct variant" for a given variant field. RESULT is
15295 the variant to fill in. OBSTACK is where any needed allocations
15296 will be done. OFFSET_MAP holds the mapping from section offsets to
15297 fields for the type. FI describes the fields of the type we're
15298 processing. FIELD is the variant field we're converting. */
15299
15300 static void
15301 create_one_variant (variant &result, struct obstack *obstack,
15302 const offset_map_type &offset_map,
15303 struct field_info *fi, const variant_field &field)
15304 {
15305 result.discriminants = convert_variant_range (obstack, field, false);
15306 result.first_field = field.first_field + fi->baseclasses.size ();
15307 result.last_field = field.last_field + fi->baseclasses.size ();
15308 result.parts = create_variant_parts (obstack, offset_map, fi,
15309 field.variant_parts);
15310 }
15311
15312 /* Fill in a "struct variant_part" for a given variant part. RESULT
15313 is the variant part to fill in. OBSTACK is where any needed
15314 allocations will be done. OFFSET_MAP holds the mapping from
15315 section offsets to fields for the type. FI describes the fields of
15316 the type we're processing. BUILDER is the variant part to be
15317 converted. */
15318
15319 static void
15320 create_one_variant_part (variant_part &result,
15321 struct obstack *obstack,
15322 const offset_map_type &offset_map,
15323 struct field_info *fi,
15324 const variant_part_builder &builder)
15325 {
15326 auto iter = offset_map.find (builder.discriminant_offset);
15327 if (iter == offset_map.end ())
15328 {
15329 result.discriminant_index = -1;
15330 /* Doesn't matter. */
15331 result.is_unsigned = false;
15332 }
15333 else
15334 {
15335 result.discriminant_index = iter->second;
15336 result.is_unsigned
15337 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
15338 }
15339
15340 size_t n = builder.variants.size ();
15341 variant *output = new (obstack) variant[n];
15342 for (size_t i = 0; i < n; ++i)
15343 create_one_variant (output[i], obstack, offset_map, fi,
15344 builder.variants[i]);
15345
15346 result.variants = gdb::array_view<variant> (output, n);
15347 }
15348
15349 /* Create a vector of variant parts that can be attached to a type.
15350 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15351 holds the mapping from section offsets to fields for the type. FI
15352 describes the fields of the type we're processing. VARIANT_PARTS
15353 is the vector to convert. */
15354
15355 static const gdb::array_view<variant_part>
15356 create_variant_parts (struct obstack *obstack,
15357 const offset_map_type &offset_map,
15358 struct field_info *fi,
15359 const std::vector<variant_part_builder> &variant_parts)
15360 {
15361 if (variant_parts.empty ())
15362 return {};
15363
15364 size_t n = variant_parts.size ();
15365 variant_part *result = new (obstack) variant_part[n];
15366 for (size_t i = 0; i < n; ++i)
15367 create_one_variant_part (result[i], obstack, offset_map, fi,
15368 variant_parts[i]);
15369
15370 return gdb::array_view<variant_part> (result, n);
15371 }
15372
15373 /* Compute the variant part vector for FIP, attaching it to TYPE when
15374 done. */
15375
15376 static void
15377 add_variant_property (struct field_info *fip, struct type *type,
15378 struct dwarf2_cu *cu)
15379 {
15380 /* Map section offsets of fields to their field index. Note the
15381 field index here does not take the number of baseclasses into
15382 account. */
15383 offset_map_type offset_map;
15384 for (int i = 0; i < fip->fields.size (); ++i)
15385 offset_map[fip->fields[i].offset] = i;
15386
15387 struct objfile *objfile = cu->per_objfile->objfile;
15388 gdb::array_view<variant_part> parts
15389 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15390 fip->variant_parts);
15391
15392 struct dynamic_prop prop;
15393 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15394 obstack_copy (&objfile->objfile_obstack, &parts,
15395 sizeof (parts)));
15396
15397 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
15398 }
15399
15400 /* Create the vector of fields, and attach it to the type. */
15401
15402 static void
15403 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15404 struct dwarf2_cu *cu)
15405 {
15406 int nfields = fip->nfields ();
15407
15408 /* Record the field count, allocate space for the array of fields,
15409 and create blank accessibility bitfields if necessary. */
15410 type->set_num_fields (nfields);
15411 type->set_fields
15412 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15413
15414 if (fip->non_public_fields && cu->language != language_ada)
15415 {
15416 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15417
15418 TYPE_FIELD_PRIVATE_BITS (type) =
15419 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15420 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15421
15422 TYPE_FIELD_PROTECTED_BITS (type) =
15423 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15424 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15425
15426 TYPE_FIELD_IGNORE_BITS (type) =
15427 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15428 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15429 }
15430
15431 /* If the type has baseclasses, allocate and clear a bit vector for
15432 TYPE_FIELD_VIRTUAL_BITS. */
15433 if (!fip->baseclasses.empty () && cu->language != language_ada)
15434 {
15435 int num_bytes = B_BYTES (fip->baseclasses.size ());
15436 unsigned char *pointer;
15437
15438 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15439 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15440 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15441 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15442 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15443 }
15444
15445 if (!fip->variant_parts.empty ())
15446 add_variant_property (fip, type, cu);
15447
15448 /* Copy the saved-up fields into the field vector. */
15449 for (int i = 0; i < nfields; ++i)
15450 {
15451 struct nextfield &field
15452 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15453 : fip->fields[i - fip->baseclasses.size ()]);
15454
15455 type->field (i) = field.field;
15456 switch (field.accessibility)
15457 {
15458 case DW_ACCESS_private:
15459 if (cu->language != language_ada)
15460 SET_TYPE_FIELD_PRIVATE (type, i);
15461 break;
15462
15463 case DW_ACCESS_protected:
15464 if (cu->language != language_ada)
15465 SET_TYPE_FIELD_PROTECTED (type, i);
15466 break;
15467
15468 case DW_ACCESS_public:
15469 break;
15470
15471 default:
15472 /* Unknown accessibility. Complain and treat it as public. */
15473 {
15474 complaint (_("unsupported accessibility %d"),
15475 field.accessibility);
15476 }
15477 break;
15478 }
15479 if (i < fip->baseclasses.size ())
15480 {
15481 switch (field.virtuality)
15482 {
15483 case DW_VIRTUALITY_virtual:
15484 case DW_VIRTUALITY_pure_virtual:
15485 if (cu->language == language_ada)
15486 error (_("unexpected virtuality in component of Ada type"));
15487 SET_TYPE_FIELD_VIRTUAL (type, i);
15488 break;
15489 }
15490 }
15491 }
15492 }
15493
15494 /* Return true if this member function is a constructor, false
15495 otherwise. */
15496
15497 static int
15498 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15499 {
15500 const char *fieldname;
15501 const char *type_name;
15502 int len;
15503
15504 if (die->parent == NULL)
15505 return 0;
15506
15507 if (die->parent->tag != DW_TAG_structure_type
15508 && die->parent->tag != DW_TAG_union_type
15509 && die->parent->tag != DW_TAG_class_type)
15510 return 0;
15511
15512 fieldname = dwarf2_name (die, cu);
15513 type_name = dwarf2_name (die->parent, cu);
15514 if (fieldname == NULL || type_name == NULL)
15515 return 0;
15516
15517 len = strlen (fieldname);
15518 return (strncmp (fieldname, type_name, len) == 0
15519 && (type_name[len] == '\0' || type_name[len] == '<'));
15520 }
15521
15522 /* Add a member function to the proper fieldlist. */
15523
15524 static void
15525 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15526 struct type *type, struct dwarf2_cu *cu)
15527 {
15528 struct objfile *objfile = cu->per_objfile->objfile;
15529 struct attribute *attr;
15530 int i;
15531 struct fnfieldlist *flp = nullptr;
15532 struct fn_field *fnp;
15533 const char *fieldname;
15534 struct type *this_type;
15535
15536 if (cu->language == language_ada)
15537 error (_("unexpected member function in Ada type"));
15538
15539 /* Get name of member function. */
15540 fieldname = dwarf2_name (die, cu);
15541 if (fieldname == NULL)
15542 return;
15543
15544 /* Look up member function name in fieldlist. */
15545 for (i = 0; i < fip->fnfieldlists.size (); i++)
15546 {
15547 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15548 {
15549 flp = &fip->fnfieldlists[i];
15550 break;
15551 }
15552 }
15553
15554 /* Create a new fnfieldlist if necessary. */
15555 if (flp == nullptr)
15556 {
15557 fip->fnfieldlists.emplace_back ();
15558 flp = &fip->fnfieldlists.back ();
15559 flp->name = fieldname;
15560 i = fip->fnfieldlists.size () - 1;
15561 }
15562
15563 /* Create a new member function field and add it to the vector of
15564 fnfieldlists. */
15565 flp->fnfields.emplace_back ();
15566 fnp = &flp->fnfields.back ();
15567
15568 /* Delay processing of the physname until later. */
15569 if (cu->language == language_cplus)
15570 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15571 die, cu);
15572 else
15573 {
15574 const char *physname = dwarf2_physname (fieldname, die, cu);
15575 fnp->physname = physname ? physname : "";
15576 }
15577
15578 fnp->type = alloc_type (objfile);
15579 this_type = read_type_die (die, cu);
15580 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15581 {
15582 int nparams = this_type->num_fields ();
15583
15584 /* TYPE is the domain of this method, and THIS_TYPE is the type
15585 of the method itself (TYPE_CODE_METHOD). */
15586 smash_to_method_type (fnp->type, type,
15587 TYPE_TARGET_TYPE (this_type),
15588 this_type->fields (),
15589 this_type->num_fields (),
15590 this_type->has_varargs ());
15591
15592 /* Handle static member functions.
15593 Dwarf2 has no clean way to discern C++ static and non-static
15594 member functions. G++ helps GDB by marking the first
15595 parameter for non-static member functions (which is the this
15596 pointer) as artificial. We obtain this information from
15597 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15598 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15599 fnp->voffset = VOFFSET_STATIC;
15600 }
15601 else
15602 complaint (_("member function type missing for '%s'"),
15603 dwarf2_full_name (fieldname, die, cu));
15604
15605 /* Get fcontext from DW_AT_containing_type if present. */
15606 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15607 fnp->fcontext = die_containing_type (die, cu);
15608
15609 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15610 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15611
15612 /* Get accessibility. */
15613 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15614 switch (accessibility)
15615 {
15616 case DW_ACCESS_private:
15617 fnp->is_private = 1;
15618 break;
15619 case DW_ACCESS_protected:
15620 fnp->is_protected = 1;
15621 break;
15622 }
15623
15624 /* Check for artificial methods. */
15625 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15626 if (attr && attr->as_boolean ())
15627 fnp->is_artificial = 1;
15628
15629 /* Check for defaulted methods. */
15630 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15631 if (attr != nullptr)
15632 fnp->defaulted = attr->defaulted ();
15633
15634 /* Check for deleted methods. */
15635 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15636 if (attr != nullptr && attr->as_boolean ())
15637 fnp->is_deleted = 1;
15638
15639 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15640
15641 /* Get index in virtual function table if it is a virtual member
15642 function. For older versions of GCC, this is an offset in the
15643 appropriate virtual table, as specified by DW_AT_containing_type.
15644 For everyone else, it is an expression to be evaluated relative
15645 to the object address. */
15646
15647 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15648 if (attr != nullptr)
15649 {
15650 if (attr->form_is_block () && attr->as_block ()->size > 0)
15651 {
15652 struct dwarf_block *block = attr->as_block ();
15653
15654 if (block->data[0] == DW_OP_constu)
15655 {
15656 /* Old-style GCC. */
15657 fnp->voffset = decode_locdesc (block, cu) + 2;
15658 }
15659 else if (block->data[0] == DW_OP_deref
15660 || (block->size > 1
15661 && block->data[0] == DW_OP_deref_size
15662 && block->data[1] == cu->header.addr_size))
15663 {
15664 fnp->voffset = decode_locdesc (block, cu);
15665 if ((fnp->voffset % cu->header.addr_size) != 0)
15666 dwarf2_complex_location_expr_complaint ();
15667 else
15668 fnp->voffset /= cu->header.addr_size;
15669 fnp->voffset += 2;
15670 }
15671 else
15672 dwarf2_complex_location_expr_complaint ();
15673
15674 if (!fnp->fcontext)
15675 {
15676 /* If there is no `this' field and no DW_AT_containing_type,
15677 we cannot actually find a base class context for the
15678 vtable! */
15679 if (this_type->num_fields () == 0
15680 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15681 {
15682 complaint (_("cannot determine context for virtual member "
15683 "function \"%s\" (offset %s)"),
15684 fieldname, sect_offset_str (die->sect_off));
15685 }
15686 else
15687 {
15688 fnp->fcontext
15689 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15690 }
15691 }
15692 }
15693 else if (attr->form_is_section_offset ())
15694 {
15695 dwarf2_complex_location_expr_complaint ();
15696 }
15697 else
15698 {
15699 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15700 fieldname);
15701 }
15702 }
15703 else
15704 {
15705 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15706 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15707 {
15708 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15709 complaint (_("Member function \"%s\" (offset %s) is virtual "
15710 "but the vtable offset is not specified"),
15711 fieldname, sect_offset_str (die->sect_off));
15712 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15713 TYPE_CPLUS_DYNAMIC (type) = 1;
15714 }
15715 }
15716 }
15717
15718 /* Create the vector of member function fields, and attach it to the type. */
15719
15720 static void
15721 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15722 struct dwarf2_cu *cu)
15723 {
15724 if (cu->language == language_ada)
15725 error (_("unexpected member functions in Ada type"));
15726
15727 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15728 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15729 TYPE_ALLOC (type,
15730 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15731
15732 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15733 {
15734 struct fnfieldlist &nf = fip->fnfieldlists[i];
15735 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15736
15737 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15738 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15739 fn_flp->fn_fields = (struct fn_field *)
15740 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15741
15742 for (int k = 0; k < nf.fnfields.size (); ++k)
15743 fn_flp->fn_fields[k] = nf.fnfields[k];
15744 }
15745
15746 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15747 }
15748
15749 /* Returns non-zero if NAME is the name of a vtable member in CU's
15750 language, zero otherwise. */
15751 static int
15752 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15753 {
15754 static const char vptr[] = "_vptr";
15755
15756 /* Look for the C++ form of the vtable. */
15757 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15758 return 1;
15759
15760 return 0;
15761 }
15762
15763 /* GCC outputs unnamed structures that are really pointers to member
15764 functions, with the ABI-specified layout. If TYPE describes
15765 such a structure, smash it into a member function type.
15766
15767 GCC shouldn't do this; it should just output pointer to member DIEs.
15768 This is GCC PR debug/28767. */
15769
15770 static void
15771 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15772 {
15773 struct type *pfn_type, *self_type, *new_type;
15774
15775 /* Check for a structure with no name and two children. */
15776 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15777 return;
15778
15779 /* Check for __pfn and __delta members. */
15780 if (TYPE_FIELD_NAME (type, 0) == NULL
15781 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15782 || TYPE_FIELD_NAME (type, 1) == NULL
15783 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15784 return;
15785
15786 /* Find the type of the method. */
15787 pfn_type = type->field (0).type ();
15788 if (pfn_type == NULL
15789 || pfn_type->code () != TYPE_CODE_PTR
15790 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15791 return;
15792
15793 /* Look for the "this" argument. */
15794 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15795 if (pfn_type->num_fields () == 0
15796 /* || pfn_type->field (0).type () == NULL */
15797 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15798 return;
15799
15800 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15801 new_type = alloc_type (objfile);
15802 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15803 pfn_type->fields (), pfn_type->num_fields (),
15804 pfn_type->has_varargs ());
15805 smash_to_methodptr_type (type, new_type);
15806 }
15807
15808 /* While some versions of GCC will generate complicated DWARF for an
15809 array (see quirk_ada_thick_pointer), more recent versions were
15810 modified to emit an explicit thick pointer structure. However, in
15811 this case, the array still has DWARF expressions for its ranges,
15812 and these must be ignored. */
15813
15814 static void
15815 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15816 struct type *type)
15817 {
15818 gdb_assert (cu->language == language_ada);
15819
15820 /* Check for a structure with two children. */
15821 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15822 return;
15823
15824 /* Check for P_ARRAY and P_BOUNDS members. */
15825 if (TYPE_FIELD_NAME (type, 0) == NULL
15826 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15827 || TYPE_FIELD_NAME (type, 1) == NULL
15828 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15829 return;
15830
15831 /* Make sure we're looking at a pointer to an array. */
15832 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15833 return;
15834 struct type *ary_type = TYPE_TARGET_TYPE (type->field (0).type ());
15835
15836 while (ary_type->code () == TYPE_CODE_ARRAY)
15837 {
15838 /* The Ada code already knows how to handle these types, so all
15839 that we need to do is turn the bounds into static bounds. */
15840 struct type *index_type = ary_type->index_type ();
15841
15842 index_type->bounds ()->low.set_const_val (1);
15843 index_type->bounds ()->high.set_const_val (0);
15844
15845 /* Handle multi-dimensional arrays. */
15846 ary_type = TYPE_TARGET_TYPE (ary_type);
15847 }
15848 }
15849
15850 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15851 appropriate error checking and issuing complaints if there is a
15852 problem. */
15853
15854 static ULONGEST
15855 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15856 {
15857 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15858
15859 if (attr == nullptr)
15860 return 0;
15861
15862 if (!attr->form_is_constant ())
15863 {
15864 complaint (_("DW_AT_alignment must have constant form"
15865 " - DIE at %s [in module %s]"),
15866 sect_offset_str (die->sect_off),
15867 objfile_name (cu->per_objfile->objfile));
15868 return 0;
15869 }
15870
15871 LONGEST val = attr->constant_value (0);
15872 if (val < 0)
15873 {
15874 complaint (_("DW_AT_alignment value must not be negative"
15875 " - DIE at %s [in module %s]"),
15876 sect_offset_str (die->sect_off),
15877 objfile_name (cu->per_objfile->objfile));
15878 return 0;
15879 }
15880 ULONGEST align = val;
15881
15882 if (align == 0)
15883 {
15884 complaint (_("DW_AT_alignment value must not be zero"
15885 " - DIE at %s [in module %s]"),
15886 sect_offset_str (die->sect_off),
15887 objfile_name (cu->per_objfile->objfile));
15888 return 0;
15889 }
15890 if ((align & (align - 1)) != 0)
15891 {
15892 complaint (_("DW_AT_alignment value must be a power of 2"
15893 " - DIE at %s [in module %s]"),
15894 sect_offset_str (die->sect_off),
15895 objfile_name (cu->per_objfile->objfile));
15896 return 0;
15897 }
15898
15899 return align;
15900 }
15901
15902 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15903 the alignment for TYPE. */
15904
15905 static void
15906 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15907 struct type *type)
15908 {
15909 if (!set_type_align (type, get_alignment (cu, die)))
15910 complaint (_("DW_AT_alignment value too large"
15911 " - DIE at %s [in module %s]"),
15912 sect_offset_str (die->sect_off),
15913 objfile_name (cu->per_objfile->objfile));
15914 }
15915
15916 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15917 constant for a type, according to DWARF5 spec, Table 5.5. */
15918
15919 static bool
15920 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15921 {
15922 switch (value)
15923 {
15924 case DW_CC_normal:
15925 case DW_CC_pass_by_reference:
15926 case DW_CC_pass_by_value:
15927 return true;
15928
15929 default:
15930 complaint (_("unrecognized DW_AT_calling_convention value "
15931 "(%s) for a type"), pulongest (value));
15932 return false;
15933 }
15934 }
15935
15936 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15937 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15938 also according to GNU-specific values (see include/dwarf2.h). */
15939
15940 static bool
15941 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15942 {
15943 switch (value)
15944 {
15945 case DW_CC_normal:
15946 case DW_CC_program:
15947 case DW_CC_nocall:
15948 return true;
15949
15950 case DW_CC_GNU_renesas_sh:
15951 case DW_CC_GNU_borland_fastcall_i386:
15952 case DW_CC_GDB_IBM_OpenCL:
15953 return true;
15954
15955 default:
15956 complaint (_("unrecognized DW_AT_calling_convention value "
15957 "(%s) for a subroutine"), pulongest (value));
15958 return false;
15959 }
15960 }
15961
15962 /* Called when we find the DIE that starts a structure or union scope
15963 (definition) to create a type for the structure or union. Fill in
15964 the type's name and general properties; the members will not be
15965 processed until process_structure_scope. A symbol table entry for
15966 the type will also not be done until process_structure_scope (assuming
15967 the type has a name).
15968
15969 NOTE: we need to call these functions regardless of whether or not the
15970 DIE has a DW_AT_name attribute, since it might be an anonymous
15971 structure or union. This gets the type entered into our set of
15972 user defined types. */
15973
15974 static struct type *
15975 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15976 {
15977 struct objfile *objfile = cu->per_objfile->objfile;
15978 struct type *type;
15979 struct attribute *attr;
15980 const char *name;
15981
15982 /* If the definition of this type lives in .debug_types, read that type.
15983 Don't follow DW_AT_specification though, that will take us back up
15984 the chain and we want to go down. */
15985 attr = die->attr (DW_AT_signature);
15986 if (attr != nullptr)
15987 {
15988 type = get_DW_AT_signature_type (die, attr, cu);
15989
15990 /* The type's CU may not be the same as CU.
15991 Ensure TYPE is recorded with CU in die_type_hash. */
15992 return set_die_type (die, type, cu);
15993 }
15994
15995 type = alloc_type (objfile);
15996 INIT_CPLUS_SPECIFIC (type);
15997
15998 name = dwarf2_name (die, cu);
15999 if (name != NULL)
16000 {
16001 if (cu->language == language_cplus
16002 || cu->language == language_d
16003 || cu->language == language_rust)
16004 {
16005 const char *full_name = dwarf2_full_name (name, die, cu);
16006
16007 /* dwarf2_full_name might have already finished building the DIE's
16008 type. If so, there is no need to continue. */
16009 if (get_die_type (die, cu) != NULL)
16010 return get_die_type (die, cu);
16011
16012 type->set_name (full_name);
16013 }
16014 else
16015 {
16016 /* The name is already allocated along with this objfile, so
16017 we don't need to duplicate it for the type. */
16018 type->set_name (name);
16019 }
16020 }
16021
16022 if (die->tag == DW_TAG_structure_type)
16023 {
16024 type->set_code (TYPE_CODE_STRUCT);
16025 }
16026 else if (die->tag == DW_TAG_union_type)
16027 {
16028 type->set_code (TYPE_CODE_UNION);
16029 }
16030 else
16031 {
16032 type->set_code (TYPE_CODE_STRUCT);
16033 }
16034
16035 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16036 TYPE_DECLARED_CLASS (type) = 1;
16037
16038 /* Store the calling convention in the type if it's available in
16039 the die. Otherwise the calling convention remains set to
16040 the default value DW_CC_normal. */
16041 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16042 if (attr != nullptr
16043 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
16044 {
16045 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16046 TYPE_CPLUS_CALLING_CONVENTION (type)
16047 = (enum dwarf_calling_convention) (attr->constant_value (0));
16048 }
16049
16050 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16051 if (attr != nullptr)
16052 {
16053 if (attr->form_is_constant ())
16054 TYPE_LENGTH (type) = attr->constant_value (0);
16055 else
16056 {
16057 struct dynamic_prop prop;
16058 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
16059 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
16060 TYPE_LENGTH (type) = 0;
16061 }
16062 }
16063 else
16064 {
16065 TYPE_LENGTH (type) = 0;
16066 }
16067
16068 maybe_set_alignment (cu, die, type);
16069
16070 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16071 {
16072 /* ICC<14 does not output the required DW_AT_declaration on
16073 incomplete types, but gives them a size of zero. */
16074 type->set_is_stub (true);
16075 }
16076 else
16077 type->set_stub_is_supported (true);
16078
16079 if (die_is_declaration (die, cu))
16080 type->set_is_stub (true);
16081 else if (attr == NULL && die->child == NULL
16082 && producer_is_realview (cu->producer))
16083 /* RealView does not output the required DW_AT_declaration
16084 on incomplete types. */
16085 type->set_is_stub (true);
16086
16087 /* We need to add the type field to the die immediately so we don't
16088 infinitely recurse when dealing with pointers to the structure
16089 type within the structure itself. */
16090 set_die_type (die, type, cu);
16091
16092 /* set_die_type should be already done. */
16093 set_descriptive_type (type, die, cu);
16094
16095 return type;
16096 }
16097
16098 static void handle_struct_member_die
16099 (struct die_info *child_die,
16100 struct type *type,
16101 struct field_info *fi,
16102 std::vector<struct symbol *> *template_args,
16103 struct dwarf2_cu *cu);
16104
16105 /* A helper for handle_struct_member_die that handles
16106 DW_TAG_variant_part. */
16107
16108 static void
16109 handle_variant_part (struct die_info *die, struct type *type,
16110 struct field_info *fi,
16111 std::vector<struct symbol *> *template_args,
16112 struct dwarf2_cu *cu)
16113 {
16114 variant_part_builder *new_part;
16115 if (fi->current_variant_part == nullptr)
16116 {
16117 fi->variant_parts.emplace_back ();
16118 new_part = &fi->variant_parts.back ();
16119 }
16120 else if (!fi->current_variant_part->processing_variant)
16121 {
16122 complaint (_("nested DW_TAG_variant_part seen "
16123 "- DIE at %s [in module %s]"),
16124 sect_offset_str (die->sect_off),
16125 objfile_name (cu->per_objfile->objfile));
16126 return;
16127 }
16128 else
16129 {
16130 variant_field &current = fi->current_variant_part->variants.back ();
16131 current.variant_parts.emplace_back ();
16132 new_part = &current.variant_parts.back ();
16133 }
16134
16135 /* When we recurse, we want callees to add to this new variant
16136 part. */
16137 scoped_restore save_current_variant_part
16138 = make_scoped_restore (&fi->current_variant_part, new_part);
16139
16140 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16141 if (discr == NULL)
16142 {
16143 /* It's a univariant form, an extension we support. */
16144 }
16145 else if (discr->form_is_ref ())
16146 {
16147 struct dwarf2_cu *target_cu = cu;
16148 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16149
16150 new_part->discriminant_offset = target_die->sect_off;
16151 }
16152 else
16153 {
16154 complaint (_("DW_AT_discr does not have DIE reference form"
16155 " - DIE at %s [in module %s]"),
16156 sect_offset_str (die->sect_off),
16157 objfile_name (cu->per_objfile->objfile));
16158 }
16159
16160 for (die_info *child_die = die->child;
16161 child_die != NULL;
16162 child_die = child_die->sibling)
16163 handle_struct_member_die (child_die, type, fi, template_args, cu);
16164 }
16165
16166 /* A helper for handle_struct_member_die that handles
16167 DW_TAG_variant. */
16168
16169 static void
16170 handle_variant (struct die_info *die, struct type *type,
16171 struct field_info *fi,
16172 std::vector<struct symbol *> *template_args,
16173 struct dwarf2_cu *cu)
16174 {
16175 if (fi->current_variant_part == nullptr)
16176 {
16177 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16178 "- DIE at %s [in module %s]"),
16179 sect_offset_str (die->sect_off),
16180 objfile_name (cu->per_objfile->objfile));
16181 return;
16182 }
16183 if (fi->current_variant_part->processing_variant)
16184 {
16185 complaint (_("nested DW_TAG_variant seen "
16186 "- DIE at %s [in module %s]"),
16187 sect_offset_str (die->sect_off),
16188 objfile_name (cu->per_objfile->objfile));
16189 return;
16190 }
16191
16192 scoped_restore save_processing_variant
16193 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16194 true);
16195
16196 fi->current_variant_part->variants.emplace_back ();
16197 variant_field &variant = fi->current_variant_part->variants.back ();
16198 variant.first_field = fi->fields.size ();
16199
16200 /* In a variant we want to get the discriminant and also add a
16201 field for our sole member child. */
16202 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
16203 if (discr == nullptr || !discr->form_is_constant ())
16204 {
16205 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
16206 if (discr == nullptr || discr->as_block ()->size == 0)
16207 variant.default_branch = true;
16208 else
16209 variant.discr_list_data = discr->as_block ();
16210 }
16211 else
16212 variant.discriminant_value = discr->constant_value (0);
16213
16214 for (die_info *variant_child = die->child;
16215 variant_child != NULL;
16216 variant_child = variant_child->sibling)
16217 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16218
16219 variant.last_field = fi->fields.size ();
16220 }
16221
16222 /* A helper for process_structure_scope that handles a single member
16223 DIE. */
16224
16225 static void
16226 handle_struct_member_die (struct die_info *child_die, struct type *type,
16227 struct field_info *fi,
16228 std::vector<struct symbol *> *template_args,
16229 struct dwarf2_cu *cu)
16230 {
16231 if (child_die->tag == DW_TAG_member
16232 || child_die->tag == DW_TAG_variable)
16233 {
16234 /* NOTE: carlton/2002-11-05: A C++ static data member
16235 should be a DW_TAG_member that is a declaration, but
16236 all versions of G++ as of this writing (so through at
16237 least 3.2.1) incorrectly generate DW_TAG_variable
16238 tags for them instead. */
16239 dwarf2_add_field (fi, child_die, cu);
16240 }
16241 else if (child_die->tag == DW_TAG_subprogram)
16242 {
16243 /* Rust doesn't have member functions in the C++ sense.
16244 However, it does emit ordinary functions as children
16245 of a struct DIE. */
16246 if (cu->language == language_rust)
16247 read_func_scope (child_die, cu);
16248 else
16249 {
16250 /* C++ member function. */
16251 dwarf2_add_member_fn (fi, child_die, type, cu);
16252 }
16253 }
16254 else if (child_die->tag == DW_TAG_inheritance)
16255 {
16256 /* C++ base class field. */
16257 dwarf2_add_field (fi, child_die, cu);
16258 }
16259 else if (type_can_define_types (child_die))
16260 dwarf2_add_type_defn (fi, child_die, cu);
16261 else if (child_die->tag == DW_TAG_template_type_param
16262 || child_die->tag == DW_TAG_template_value_param)
16263 {
16264 struct symbol *arg = new_symbol (child_die, NULL, cu);
16265
16266 if (arg != NULL)
16267 template_args->push_back (arg);
16268 }
16269 else if (child_die->tag == DW_TAG_variant_part)
16270 handle_variant_part (child_die, type, fi, template_args, cu);
16271 else if (child_die->tag == DW_TAG_variant)
16272 handle_variant (child_die, type, fi, template_args, cu);
16273 }
16274
16275 /* Finish creating a structure or union type, including filling in
16276 its members and creating a symbol for it. */
16277
16278 static void
16279 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16280 {
16281 struct objfile *objfile = cu->per_objfile->objfile;
16282 struct die_info *child_die;
16283 struct type *type;
16284
16285 type = get_die_type (die, cu);
16286 if (type == NULL)
16287 type = read_structure_type (die, cu);
16288
16289 bool has_template_parameters = false;
16290 if (die->child != NULL && ! die_is_declaration (die, cu))
16291 {
16292 struct field_info fi;
16293 std::vector<struct symbol *> template_args;
16294
16295 child_die = die->child;
16296
16297 while (child_die && child_die->tag)
16298 {
16299 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16300 child_die = child_die->sibling;
16301 }
16302
16303 /* Attach template arguments to type. */
16304 if (!template_args.empty ())
16305 {
16306 has_template_parameters = true;
16307 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16308 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16309 TYPE_TEMPLATE_ARGUMENTS (type)
16310 = XOBNEWVEC (&objfile->objfile_obstack,
16311 struct symbol *,
16312 TYPE_N_TEMPLATE_ARGUMENTS (type));
16313 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16314 template_args.data (),
16315 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16316 * sizeof (struct symbol *)));
16317 }
16318
16319 /* Attach fields and member functions to the type. */
16320 if (fi.nfields () > 0)
16321 dwarf2_attach_fields_to_type (&fi, type, cu);
16322 if (!fi.fnfieldlists.empty ())
16323 {
16324 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16325
16326 /* Get the type which refers to the base class (possibly this
16327 class itself) which contains the vtable pointer for the current
16328 class from the DW_AT_containing_type attribute. This use of
16329 DW_AT_containing_type is a GNU extension. */
16330
16331 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16332 {
16333 struct type *t = die_containing_type (die, cu);
16334
16335 set_type_vptr_basetype (type, t);
16336 if (type == t)
16337 {
16338 int i;
16339
16340 /* Our own class provides vtbl ptr. */
16341 for (i = t->num_fields () - 1;
16342 i >= TYPE_N_BASECLASSES (t);
16343 --i)
16344 {
16345 const char *fieldname = TYPE_FIELD_NAME (t, i);
16346
16347 if (is_vtable_name (fieldname, cu))
16348 {
16349 set_type_vptr_fieldno (type, i);
16350 break;
16351 }
16352 }
16353
16354 /* Complain if virtual function table field not found. */
16355 if (i < TYPE_N_BASECLASSES (t))
16356 complaint (_("virtual function table pointer "
16357 "not found when defining class '%s'"),
16358 type->name () ? type->name () : "");
16359 }
16360 else
16361 {
16362 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16363 }
16364 }
16365 else if (cu->producer
16366 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16367 {
16368 /* The IBM XLC compiler does not provide direct indication
16369 of the containing type, but the vtable pointer is
16370 always named __vfp. */
16371
16372 int i;
16373
16374 for (i = type->num_fields () - 1;
16375 i >= TYPE_N_BASECLASSES (type);
16376 --i)
16377 {
16378 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16379 {
16380 set_type_vptr_fieldno (type, i);
16381 set_type_vptr_basetype (type, type);
16382 break;
16383 }
16384 }
16385 }
16386 }
16387
16388 /* Copy fi.typedef_field_list linked list elements content into the
16389 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16390 if (!fi.typedef_field_list.empty ())
16391 {
16392 int count = fi.typedef_field_list.size ();
16393
16394 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16395 TYPE_TYPEDEF_FIELD_ARRAY (type)
16396 = ((struct decl_field *)
16397 TYPE_ALLOC (type,
16398 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16399 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16400
16401 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16402 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16403 }
16404
16405 /* Copy fi.nested_types_list linked list elements content into the
16406 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16407 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16408 {
16409 int count = fi.nested_types_list.size ();
16410
16411 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16412 TYPE_NESTED_TYPES_ARRAY (type)
16413 = ((struct decl_field *)
16414 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16415 TYPE_NESTED_TYPES_COUNT (type) = count;
16416
16417 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16418 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16419 }
16420 }
16421
16422 quirk_gcc_member_function_pointer (type, objfile);
16423 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16424 cu->rust_unions.push_back (type);
16425 else if (cu->language == language_ada)
16426 quirk_ada_thick_pointer_struct (die, cu, type);
16427
16428 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16429 snapshots) has been known to create a die giving a declaration
16430 for a class that has, as a child, a die giving a definition for a
16431 nested class. So we have to process our children even if the
16432 current die is a declaration. Normally, of course, a declaration
16433 won't have any children at all. */
16434
16435 child_die = die->child;
16436
16437 while (child_die != NULL && child_die->tag)
16438 {
16439 if (child_die->tag == DW_TAG_member
16440 || child_die->tag == DW_TAG_variable
16441 || child_die->tag == DW_TAG_inheritance
16442 || child_die->tag == DW_TAG_template_value_param
16443 || child_die->tag == DW_TAG_template_type_param)
16444 {
16445 /* Do nothing. */
16446 }
16447 else
16448 process_die (child_die, cu);
16449
16450 child_die = child_die->sibling;
16451 }
16452
16453 /* Do not consider external references. According to the DWARF standard,
16454 these DIEs are identified by the fact that they have no byte_size
16455 attribute, and a declaration attribute. */
16456 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16457 || !die_is_declaration (die, cu)
16458 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16459 {
16460 struct symbol *sym = new_symbol (die, type, cu);
16461
16462 if (has_template_parameters)
16463 {
16464 struct symtab *symtab;
16465 if (sym != nullptr)
16466 symtab = symbol_symtab (sym);
16467 else if (cu->line_header != nullptr)
16468 {
16469 /* Any related symtab will do. */
16470 symtab
16471 = cu->line_header->file_names ()[0].symtab;
16472 }
16473 else
16474 {
16475 symtab = nullptr;
16476 complaint (_("could not find suitable "
16477 "symtab for template parameter"
16478 " - DIE at %s [in module %s]"),
16479 sect_offset_str (die->sect_off),
16480 objfile_name (objfile));
16481 }
16482
16483 if (symtab != nullptr)
16484 {
16485 /* Make sure that the symtab is set on the new symbols.
16486 Even though they don't appear in this symtab directly,
16487 other parts of gdb assume that symbols do, and this is
16488 reasonably true. */
16489 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16490 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16491 }
16492 }
16493 }
16494 }
16495
16496 /* Assuming DIE is an enumeration type, and TYPE is its associated
16497 type, update TYPE using some information only available in DIE's
16498 children. In particular, the fields are computed. */
16499
16500 static void
16501 update_enumeration_type_from_children (struct die_info *die,
16502 struct type *type,
16503 struct dwarf2_cu *cu)
16504 {
16505 struct die_info *child_die;
16506 int unsigned_enum = 1;
16507 int flag_enum = 1;
16508
16509 auto_obstack obstack;
16510 std::vector<struct field> fields;
16511
16512 for (child_die = die->child;
16513 child_die != NULL && child_die->tag;
16514 child_die = child_die->sibling)
16515 {
16516 struct attribute *attr;
16517 LONGEST value;
16518 const gdb_byte *bytes;
16519 struct dwarf2_locexpr_baton *baton;
16520 const char *name;
16521
16522 if (child_die->tag != DW_TAG_enumerator)
16523 continue;
16524
16525 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16526 if (attr == NULL)
16527 continue;
16528
16529 name = dwarf2_name (child_die, cu);
16530 if (name == NULL)
16531 name = "<anonymous enumerator>";
16532
16533 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16534 &value, &bytes, &baton);
16535 if (value < 0)
16536 {
16537 unsigned_enum = 0;
16538 flag_enum = 0;
16539 }
16540 else
16541 {
16542 if (count_one_bits_ll (value) >= 2)
16543 flag_enum = 0;
16544 }
16545
16546 fields.emplace_back ();
16547 struct field &field = fields.back ();
16548 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16549 SET_FIELD_ENUMVAL (field, value);
16550 }
16551
16552 if (!fields.empty ())
16553 {
16554 type->set_num_fields (fields.size ());
16555 type->set_fields
16556 ((struct field *)
16557 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16558 memcpy (type->fields (), fields.data (),
16559 sizeof (struct field) * fields.size ());
16560 }
16561
16562 if (unsigned_enum)
16563 type->set_is_unsigned (true);
16564
16565 if (flag_enum)
16566 TYPE_FLAG_ENUM (type) = 1;
16567 }
16568
16569 /* Given a DW_AT_enumeration_type die, set its type. We do not
16570 complete the type's fields yet, or create any symbols. */
16571
16572 static struct type *
16573 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16574 {
16575 struct objfile *objfile = cu->per_objfile->objfile;
16576 struct type *type;
16577 struct attribute *attr;
16578 const char *name;
16579
16580 /* If the definition of this type lives in .debug_types, read that type.
16581 Don't follow DW_AT_specification though, that will take us back up
16582 the chain and we want to go down. */
16583 attr = die->attr (DW_AT_signature);
16584 if (attr != nullptr)
16585 {
16586 type = get_DW_AT_signature_type (die, attr, cu);
16587
16588 /* The type's CU may not be the same as CU.
16589 Ensure TYPE is recorded with CU in die_type_hash. */
16590 return set_die_type (die, type, cu);
16591 }
16592
16593 type = alloc_type (objfile);
16594
16595 type->set_code (TYPE_CODE_ENUM);
16596 name = dwarf2_full_name (NULL, die, cu);
16597 if (name != NULL)
16598 type->set_name (name);
16599
16600 attr = dwarf2_attr (die, DW_AT_type, cu);
16601 if (attr != NULL)
16602 {
16603 struct type *underlying_type = die_type (die, cu);
16604
16605 TYPE_TARGET_TYPE (type) = underlying_type;
16606 }
16607
16608 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16609 if (attr != nullptr)
16610 {
16611 TYPE_LENGTH (type) = attr->constant_value (0);
16612 }
16613 else
16614 {
16615 TYPE_LENGTH (type) = 0;
16616 }
16617
16618 maybe_set_alignment (cu, die, type);
16619
16620 /* The enumeration DIE can be incomplete. In Ada, any type can be
16621 declared as private in the package spec, and then defined only
16622 inside the package body. Such types are known as Taft Amendment
16623 Types. When another package uses such a type, an incomplete DIE
16624 may be generated by the compiler. */
16625 if (die_is_declaration (die, cu))
16626 type->set_is_stub (true);
16627
16628 /* If this type has an underlying type that is not a stub, then we
16629 may use its attributes. We always use the "unsigned" attribute
16630 in this situation, because ordinarily we guess whether the type
16631 is unsigned -- but the guess can be wrong and the underlying type
16632 can tell us the reality. However, we defer to a local size
16633 attribute if one exists, because this lets the compiler override
16634 the underlying type if needed. */
16635 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16636 {
16637 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16638 underlying_type = check_typedef (underlying_type);
16639
16640 type->set_is_unsigned (underlying_type->is_unsigned ());
16641
16642 if (TYPE_LENGTH (type) == 0)
16643 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16644
16645 if (TYPE_RAW_ALIGN (type) == 0
16646 && TYPE_RAW_ALIGN (underlying_type) != 0)
16647 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16648 }
16649
16650 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16651
16652 set_die_type (die, type, cu);
16653
16654 /* Finish the creation of this type by using the enum's children.
16655 Note that, as usual, this must come after set_die_type to avoid
16656 infinite recursion when trying to compute the names of the
16657 enumerators. */
16658 update_enumeration_type_from_children (die, type, cu);
16659
16660 return type;
16661 }
16662
16663 /* Given a pointer to a die which begins an enumeration, process all
16664 the dies that define the members of the enumeration, and create the
16665 symbol for the enumeration type.
16666
16667 NOTE: We reverse the order of the element list. */
16668
16669 static void
16670 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16671 {
16672 struct type *this_type;
16673
16674 this_type = get_die_type (die, cu);
16675 if (this_type == NULL)
16676 this_type = read_enumeration_type (die, cu);
16677
16678 if (die->child != NULL)
16679 {
16680 struct die_info *child_die;
16681 const char *name;
16682
16683 child_die = die->child;
16684 while (child_die && child_die->tag)
16685 {
16686 if (child_die->tag != DW_TAG_enumerator)
16687 {
16688 process_die (child_die, cu);
16689 }
16690 else
16691 {
16692 name = dwarf2_name (child_die, cu);
16693 if (name)
16694 new_symbol (child_die, this_type, cu);
16695 }
16696
16697 child_die = child_die->sibling;
16698 }
16699 }
16700
16701 /* If we are reading an enum from a .debug_types unit, and the enum
16702 is a declaration, and the enum is not the signatured type in the
16703 unit, then we do not want to add a symbol for it. Adding a
16704 symbol would in some cases obscure the true definition of the
16705 enum, giving users an incomplete type when the definition is
16706 actually available. Note that we do not want to do this for all
16707 enums which are just declarations, because C++0x allows forward
16708 enum declarations. */
16709 if (cu->per_cu->is_debug_types
16710 && die_is_declaration (die, cu))
16711 {
16712 struct signatured_type *sig_type;
16713
16714 sig_type = (struct signatured_type *) cu->per_cu;
16715 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16716 if (sig_type->type_offset_in_section != die->sect_off)
16717 return;
16718 }
16719
16720 new_symbol (die, this_type, cu);
16721 }
16722
16723 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16724 expression for an index type and finds the corresponding field
16725 offset in the hidden "P_BOUNDS" structure. Returns true on success
16726 and updates *FIELD, false if it fails to recognize an
16727 expression. */
16728
16729 static bool
16730 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16731 int *bounds_offset, struct field *field,
16732 struct dwarf2_cu *cu)
16733 {
16734 struct attribute *attr = dwarf2_attr (die, name, cu);
16735 if (attr == nullptr || !attr->form_is_block ())
16736 return false;
16737
16738 const struct dwarf_block *block = attr->as_block ();
16739 const gdb_byte *start = block->data;
16740 const gdb_byte *end = block->data + block->size;
16741
16742 /* The expression to recognize generally looks like:
16743
16744 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16745 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16746
16747 However, the second "plus_uconst" may be missing:
16748
16749 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16750 DW_OP_deref_size: 4)
16751
16752 This happens when the field is at the start of the structure.
16753
16754 Also, the final deref may not be sized:
16755
16756 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16757 DW_OP_deref)
16758
16759 This happens when the size of the index type happens to be the
16760 same as the architecture's word size. This can occur with or
16761 without the second plus_uconst. */
16762
16763 if (end - start < 2)
16764 return false;
16765 if (*start++ != DW_OP_push_object_address)
16766 return false;
16767 if (*start++ != DW_OP_plus_uconst)
16768 return false;
16769
16770 uint64_t this_bound_off;
16771 start = gdb_read_uleb128 (start, end, &this_bound_off);
16772 if (start == nullptr || (int) this_bound_off != this_bound_off)
16773 return false;
16774 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16775 is consistent among all bounds. */
16776 if (*bounds_offset == -1)
16777 *bounds_offset = this_bound_off;
16778 else if (*bounds_offset != this_bound_off)
16779 return false;
16780
16781 if (start == end || *start++ != DW_OP_deref)
16782 return false;
16783
16784 int offset = 0;
16785 if (start ==end)
16786 return false;
16787 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16788 {
16789 /* This means an offset of 0. */
16790 }
16791 else if (*start++ != DW_OP_plus_uconst)
16792 return false;
16793 else
16794 {
16795 /* The size is the parameter to DW_OP_plus_uconst. */
16796 uint64_t val;
16797 start = gdb_read_uleb128 (start, end, &val);
16798 if (start == nullptr)
16799 return false;
16800 if ((int) val != val)
16801 return false;
16802 offset = val;
16803 }
16804
16805 if (start == end)
16806 return false;
16807
16808 uint64_t size;
16809 if (*start == DW_OP_deref_size)
16810 {
16811 start = gdb_read_uleb128 (start + 1, end, &size);
16812 if (start == nullptr)
16813 return false;
16814 }
16815 else if (*start == DW_OP_deref)
16816 {
16817 size = cu->header.addr_size;
16818 ++start;
16819 }
16820 else
16821 return false;
16822
16823 SET_FIELD_BITPOS (*field, 8 * offset);
16824 if (size != TYPE_LENGTH (field->type ()))
16825 FIELD_BITSIZE (*field) = 8 * size;
16826
16827 return true;
16828 }
16829
16830 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16831 some kinds of Ada arrays:
16832
16833 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16834 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16835 <11e0> DW_AT_data_location: 2 byte block: 97 6
16836 (DW_OP_push_object_address; DW_OP_deref)
16837 <11e3> DW_AT_type : <0x1173>
16838 <11e7> DW_AT_sibling : <0x1201>
16839 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16840 <11ec> DW_AT_type : <0x1206>
16841 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16842 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16843 DW_OP_deref_size: 4)
16844 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16845 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16846 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16847
16848 This actually represents a "thick pointer", which is a structure
16849 with two elements: one that is a pointer to the array data, and one
16850 that is a pointer to another structure; this second structure holds
16851 the array bounds.
16852
16853 This returns a new type on success, or nullptr if this didn't
16854 recognize the type. */
16855
16856 static struct type *
16857 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16858 struct type *type)
16859 {
16860 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16861 /* So far we've only seen this with block form. */
16862 if (attr == nullptr || !attr->form_is_block ())
16863 return nullptr;
16864
16865 /* Note that this will fail if the structure layout is changed by
16866 the compiler. However, we have no good way to recognize some
16867 other layout, because we don't know what expression the compiler
16868 might choose to emit should this happen. */
16869 struct dwarf_block *blk = attr->as_block ();
16870 if (blk->size != 2
16871 || blk->data[0] != DW_OP_push_object_address
16872 || blk->data[1] != DW_OP_deref)
16873 return nullptr;
16874
16875 int bounds_offset = -1;
16876 int max_align = -1;
16877 std::vector<struct field> range_fields;
16878 for (struct die_info *child_die = die->child;
16879 child_die;
16880 child_die = child_die->sibling)
16881 {
16882 if (child_die->tag == DW_TAG_subrange_type)
16883 {
16884 struct type *underlying = read_subrange_index_type (child_die, cu);
16885
16886 int this_align = type_align (underlying);
16887 if (this_align > max_align)
16888 max_align = this_align;
16889
16890 range_fields.emplace_back ();
16891 range_fields.emplace_back ();
16892
16893 struct field &lower = range_fields[range_fields.size () - 2];
16894 struct field &upper = range_fields[range_fields.size () - 1];
16895
16896 lower.set_type (underlying);
16897 FIELD_ARTIFICIAL (lower) = 1;
16898
16899 upper.set_type (underlying);
16900 FIELD_ARTIFICIAL (upper) = 1;
16901
16902 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16903 &bounds_offset, &lower, cu)
16904 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16905 &bounds_offset, &upper, cu))
16906 return nullptr;
16907 }
16908 }
16909
16910 /* This shouldn't really happen, but double-check that we found
16911 where the bounds are stored. */
16912 if (bounds_offset == -1)
16913 return nullptr;
16914
16915 struct objfile *objfile = cu->per_objfile->objfile;
16916 for (int i = 0; i < range_fields.size (); i += 2)
16917 {
16918 char name[20];
16919
16920 /* Set the name of each field in the bounds. */
16921 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16922 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16923 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16924 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16925 }
16926
16927 struct type *bounds = alloc_type (objfile);
16928 bounds->set_code (TYPE_CODE_STRUCT);
16929
16930 bounds->set_num_fields (range_fields.size ());
16931 bounds->set_fields
16932 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16933 * sizeof (struct field))));
16934 memcpy (bounds->fields (), range_fields.data (),
16935 bounds->num_fields () * sizeof (struct field));
16936
16937 int last_fieldno = range_fields.size () - 1;
16938 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
16939 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16940 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16941
16942 /* Rewrite the existing array type in place. Specifically, we
16943 remove any dynamic properties we might have read, and we replace
16944 the index types. */
16945 struct type *iter = type;
16946 for (int i = 0; i < range_fields.size (); i += 2)
16947 {
16948 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16949 iter->main_type->dyn_prop_list = nullptr;
16950 iter->set_index_type
16951 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16952 iter = TYPE_TARGET_TYPE (iter);
16953 }
16954
16955 struct type *result = alloc_type (objfile);
16956 result->set_code (TYPE_CODE_STRUCT);
16957
16958 result->set_num_fields (2);
16959 result->set_fields
16960 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16961 * sizeof (struct field))));
16962
16963 /* The names are chosen to coincide with what the compiler does with
16964 -fgnat-encodings=all, which the Ada code in gdb already
16965 understands. */
16966 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
16967 result->field (0).set_type (lookup_pointer_type (type));
16968
16969 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
16970 result->field (1).set_type (lookup_pointer_type (bounds));
16971 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
16972
16973 result->set_name (type->name ());
16974 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16975 + TYPE_LENGTH (result->field (1).type ()));
16976
16977 return result;
16978 }
16979
16980 /* Extract all information from a DW_TAG_array_type DIE and put it in
16981 the DIE's type field. For now, this only handles one dimensional
16982 arrays. */
16983
16984 static struct type *
16985 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16986 {
16987 struct objfile *objfile = cu->per_objfile->objfile;
16988 struct die_info *child_die;
16989 struct type *type;
16990 struct type *element_type, *range_type, *index_type;
16991 struct attribute *attr;
16992 const char *name;
16993 struct dynamic_prop *byte_stride_prop = NULL;
16994 unsigned int bit_stride = 0;
16995
16996 element_type = die_type (die, cu);
16997
16998 /* The die_type call above may have already set the type for this DIE. */
16999 type = get_die_type (die, cu);
17000 if (type)
17001 return type;
17002
17003 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
17004 if (attr != NULL)
17005 {
17006 int stride_ok;
17007 struct type *prop_type = cu->addr_sized_int_type (false);
17008
17009 byte_stride_prop
17010 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
17011 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
17012 prop_type);
17013 if (!stride_ok)
17014 {
17015 complaint (_("unable to read array DW_AT_byte_stride "
17016 " - DIE at %s [in module %s]"),
17017 sect_offset_str (die->sect_off),
17018 objfile_name (cu->per_objfile->objfile));
17019 /* Ignore this attribute. We will likely not be able to print
17020 arrays of this type correctly, but there is little we can do
17021 to help if we cannot read the attribute's value. */
17022 byte_stride_prop = NULL;
17023 }
17024 }
17025
17026 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
17027 if (attr != NULL)
17028 bit_stride = attr->constant_value (0);
17029
17030 /* Irix 6.2 native cc creates array types without children for
17031 arrays with unspecified length. */
17032 if (die->child == NULL)
17033 {
17034 index_type = objfile_type (objfile)->builtin_int;
17035 range_type = create_static_range_type (NULL, index_type, 0, -1);
17036 type = create_array_type_with_stride (NULL, element_type, range_type,
17037 byte_stride_prop, bit_stride);
17038 return set_die_type (die, type, cu);
17039 }
17040
17041 std::vector<struct type *> range_types;
17042 child_die = die->child;
17043 while (child_die && child_die->tag)
17044 {
17045 if (child_die->tag == DW_TAG_subrange_type)
17046 {
17047 struct type *child_type = read_type_die (child_die, cu);
17048
17049 if (child_type != NULL)
17050 {
17051 /* The range type was succesfully read. Save it for the
17052 array type creation. */
17053 range_types.push_back (child_type);
17054 }
17055 }
17056 child_die = child_die->sibling;
17057 }
17058
17059 /* Dwarf2 dimensions are output from left to right, create the
17060 necessary array types in backwards order. */
17061
17062 type = element_type;
17063
17064 if (read_array_order (die, cu) == DW_ORD_col_major)
17065 {
17066 int i = 0;
17067
17068 while (i < range_types.size ())
17069 {
17070 type = create_array_type_with_stride (NULL, type, range_types[i++],
17071 byte_stride_prop, bit_stride);
17072 bit_stride = 0;
17073 byte_stride_prop = nullptr;
17074 }
17075 }
17076 else
17077 {
17078 size_t ndim = range_types.size ();
17079 while (ndim-- > 0)
17080 {
17081 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17082 byte_stride_prop, bit_stride);
17083 bit_stride = 0;
17084 byte_stride_prop = nullptr;
17085 }
17086 }
17087
17088 /* Understand Dwarf2 support for vector types (like they occur on
17089 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17090 array type. This is not part of the Dwarf2/3 standard yet, but a
17091 custom vendor extension. The main difference between a regular
17092 array and the vector variant is that vectors are passed by value
17093 to functions. */
17094 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
17095 if (attr != nullptr)
17096 make_vector_type (type);
17097
17098 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17099 implementation may choose to implement triple vectors using this
17100 attribute. */
17101 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17102 if (attr != nullptr && attr->form_is_unsigned ())
17103 {
17104 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17105 TYPE_LENGTH (type) = attr->as_unsigned ();
17106 else
17107 complaint (_("DW_AT_byte_size for array type smaller "
17108 "than the total size of elements"));
17109 }
17110
17111 name = dwarf2_name (die, cu);
17112 if (name)
17113 type->set_name (name);
17114
17115 maybe_set_alignment (cu, die, type);
17116
17117 struct type *replacement_type = nullptr;
17118 if (cu->language == language_ada)
17119 {
17120 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17121 if (replacement_type != nullptr)
17122 type = replacement_type;
17123 }
17124
17125 /* Install the type in the die. */
17126 set_die_type (die, type, cu, replacement_type != nullptr);
17127
17128 /* set_die_type should be already done. */
17129 set_descriptive_type (type, die, cu);
17130
17131 return type;
17132 }
17133
17134 static enum dwarf_array_dim_ordering
17135 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
17136 {
17137 struct attribute *attr;
17138
17139 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17140
17141 if (attr != nullptr)
17142 {
17143 LONGEST val = attr->constant_value (-1);
17144 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17145 return (enum dwarf_array_dim_ordering) val;
17146 }
17147
17148 /* GNU F77 is a special case, as at 08/2004 array type info is the
17149 opposite order to the dwarf2 specification, but data is still
17150 laid out as per normal fortran.
17151
17152 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17153 version checking. */
17154
17155 if (cu->language == language_fortran
17156 && cu->producer && strstr (cu->producer, "GNU F77"))
17157 {
17158 return DW_ORD_row_major;
17159 }
17160
17161 switch (cu->language_defn->array_ordering ())
17162 {
17163 case array_column_major:
17164 return DW_ORD_col_major;
17165 case array_row_major:
17166 default:
17167 return DW_ORD_row_major;
17168 };
17169 }
17170
17171 /* Extract all information from a DW_TAG_set_type DIE and put it in
17172 the DIE's type field. */
17173
17174 static struct type *
17175 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17176 {
17177 struct type *domain_type, *set_type;
17178 struct attribute *attr;
17179
17180 domain_type = die_type (die, cu);
17181
17182 /* The die_type call above may have already set the type for this DIE. */
17183 set_type = get_die_type (die, cu);
17184 if (set_type)
17185 return set_type;
17186
17187 set_type = create_set_type (NULL, domain_type);
17188
17189 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17190 if (attr != nullptr && attr->form_is_unsigned ())
17191 TYPE_LENGTH (set_type) = attr->as_unsigned ();
17192
17193 maybe_set_alignment (cu, die, set_type);
17194
17195 return set_die_type (die, set_type, cu);
17196 }
17197
17198 /* A helper for read_common_block that creates a locexpr baton.
17199 SYM is the symbol which we are marking as computed.
17200 COMMON_DIE is the DIE for the common block.
17201 COMMON_LOC is the location expression attribute for the common
17202 block itself.
17203 MEMBER_LOC is the location expression attribute for the particular
17204 member of the common block that we are processing.
17205 CU is the CU from which the above come. */
17206
17207 static void
17208 mark_common_block_symbol_computed (struct symbol *sym,
17209 struct die_info *common_die,
17210 struct attribute *common_loc,
17211 struct attribute *member_loc,
17212 struct dwarf2_cu *cu)
17213 {
17214 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17215 struct objfile *objfile = per_objfile->objfile;
17216 struct dwarf2_locexpr_baton *baton;
17217 gdb_byte *ptr;
17218 unsigned int cu_off;
17219 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
17220 LONGEST offset = 0;
17221
17222 gdb_assert (common_loc && member_loc);
17223 gdb_assert (common_loc->form_is_block ());
17224 gdb_assert (member_loc->form_is_block ()
17225 || member_loc->form_is_constant ());
17226
17227 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
17228 baton->per_objfile = per_objfile;
17229 baton->per_cu = cu->per_cu;
17230 gdb_assert (baton->per_cu);
17231
17232 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17233
17234 if (member_loc->form_is_constant ())
17235 {
17236 offset = member_loc->constant_value (0);
17237 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17238 }
17239 else
17240 baton->size += member_loc->as_block ()->size;
17241
17242 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
17243 baton->data = ptr;
17244
17245 *ptr++ = DW_OP_call4;
17246 cu_off = common_die->sect_off - cu->per_cu->sect_off;
17247 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17248 ptr += 4;
17249
17250 if (member_loc->form_is_constant ())
17251 {
17252 *ptr++ = DW_OP_addr;
17253 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17254 ptr += cu->header.addr_size;
17255 }
17256 else
17257 {
17258 /* We have to copy the data here, because DW_OP_call4 will only
17259 use a DW_AT_location attribute. */
17260 struct dwarf_block *block = member_loc->as_block ();
17261 memcpy (ptr, block->data, block->size);
17262 ptr += block->size;
17263 }
17264
17265 *ptr++ = DW_OP_plus;
17266 gdb_assert (ptr - baton->data == baton->size);
17267
17268 SYMBOL_LOCATION_BATON (sym) = baton;
17269 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17270 }
17271
17272 /* Create appropriate locally-scoped variables for all the
17273 DW_TAG_common_block entries. Also create a struct common_block
17274 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17275 is used to separate the common blocks name namespace from regular
17276 variable names. */
17277
17278 static void
17279 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17280 {
17281 struct attribute *attr;
17282
17283 attr = dwarf2_attr (die, DW_AT_location, cu);
17284 if (attr != nullptr)
17285 {
17286 /* Support the .debug_loc offsets. */
17287 if (attr->form_is_block ())
17288 {
17289 /* Ok. */
17290 }
17291 else if (attr->form_is_section_offset ())
17292 {
17293 dwarf2_complex_location_expr_complaint ();
17294 attr = NULL;
17295 }
17296 else
17297 {
17298 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17299 "common block member");
17300 attr = NULL;
17301 }
17302 }
17303
17304 if (die->child != NULL)
17305 {
17306 struct objfile *objfile = cu->per_objfile->objfile;
17307 struct die_info *child_die;
17308 size_t n_entries = 0, size;
17309 struct common_block *common_block;
17310 struct symbol *sym;
17311
17312 for (child_die = die->child;
17313 child_die && child_die->tag;
17314 child_die = child_die->sibling)
17315 ++n_entries;
17316
17317 size = (sizeof (struct common_block)
17318 + (n_entries - 1) * sizeof (struct symbol *));
17319 common_block
17320 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17321 size);
17322 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17323 common_block->n_entries = 0;
17324
17325 for (child_die = die->child;
17326 child_die && child_die->tag;
17327 child_die = child_die->sibling)
17328 {
17329 /* Create the symbol in the DW_TAG_common_block block in the current
17330 symbol scope. */
17331 sym = new_symbol (child_die, NULL, cu);
17332 if (sym != NULL)
17333 {
17334 struct attribute *member_loc;
17335
17336 common_block->contents[common_block->n_entries++] = sym;
17337
17338 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17339 cu);
17340 if (member_loc)
17341 {
17342 /* GDB has handled this for a long time, but it is
17343 not specified by DWARF. It seems to have been
17344 emitted by gfortran at least as recently as:
17345 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17346 complaint (_("Variable in common block has "
17347 "DW_AT_data_member_location "
17348 "- DIE at %s [in module %s]"),
17349 sect_offset_str (child_die->sect_off),
17350 objfile_name (objfile));
17351
17352 if (member_loc->form_is_section_offset ())
17353 dwarf2_complex_location_expr_complaint ();
17354 else if (member_loc->form_is_constant ()
17355 || member_loc->form_is_block ())
17356 {
17357 if (attr != nullptr)
17358 mark_common_block_symbol_computed (sym, die, attr,
17359 member_loc, cu);
17360 }
17361 else
17362 dwarf2_complex_location_expr_complaint ();
17363 }
17364 }
17365 }
17366
17367 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17368 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17369 }
17370 }
17371
17372 /* Create a type for a C++ namespace. */
17373
17374 static struct type *
17375 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17376 {
17377 struct objfile *objfile = cu->per_objfile->objfile;
17378 const char *previous_prefix, *name;
17379 int is_anonymous;
17380 struct type *type;
17381
17382 /* For extensions, reuse the type of the original namespace. */
17383 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17384 {
17385 struct die_info *ext_die;
17386 struct dwarf2_cu *ext_cu = cu;
17387
17388 ext_die = dwarf2_extension (die, &ext_cu);
17389 type = read_type_die (ext_die, ext_cu);
17390
17391 /* EXT_CU may not be the same as CU.
17392 Ensure TYPE is recorded with CU in die_type_hash. */
17393 return set_die_type (die, type, cu);
17394 }
17395
17396 name = namespace_name (die, &is_anonymous, cu);
17397
17398 /* Now build the name of the current namespace. */
17399
17400 previous_prefix = determine_prefix (die, cu);
17401 if (previous_prefix[0] != '\0')
17402 name = typename_concat (&objfile->objfile_obstack,
17403 previous_prefix, name, 0, cu);
17404
17405 /* Create the type. */
17406 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17407
17408 return set_die_type (die, type, cu);
17409 }
17410
17411 /* Read a namespace scope. */
17412
17413 static void
17414 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17415 {
17416 struct objfile *objfile = cu->per_objfile->objfile;
17417 int is_anonymous;
17418
17419 /* Add a symbol associated to this if we haven't seen the namespace
17420 before. Also, add a using directive if it's an anonymous
17421 namespace. */
17422
17423 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17424 {
17425 struct type *type;
17426
17427 type = read_type_die (die, cu);
17428 new_symbol (die, type, cu);
17429
17430 namespace_name (die, &is_anonymous, cu);
17431 if (is_anonymous)
17432 {
17433 const char *previous_prefix = determine_prefix (die, cu);
17434
17435 std::vector<const char *> excludes;
17436 add_using_directive (using_directives (cu),
17437 previous_prefix, type->name (), NULL,
17438 NULL, excludes, 0, &objfile->objfile_obstack);
17439 }
17440 }
17441
17442 if (die->child != NULL)
17443 {
17444 struct die_info *child_die = die->child;
17445
17446 while (child_die && child_die->tag)
17447 {
17448 process_die (child_die, cu);
17449 child_die = child_die->sibling;
17450 }
17451 }
17452 }
17453
17454 /* Read a Fortran module as type. This DIE can be only a declaration used for
17455 imported module. Still we need that type as local Fortran "use ... only"
17456 declaration imports depend on the created type in determine_prefix. */
17457
17458 static struct type *
17459 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17460 {
17461 struct objfile *objfile = cu->per_objfile->objfile;
17462 const char *module_name;
17463 struct type *type;
17464
17465 module_name = dwarf2_name (die, cu);
17466 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17467
17468 return set_die_type (die, type, cu);
17469 }
17470
17471 /* Read a Fortran module. */
17472
17473 static void
17474 read_module (struct die_info *die, struct dwarf2_cu *cu)
17475 {
17476 struct die_info *child_die = die->child;
17477 struct type *type;
17478
17479 type = read_type_die (die, cu);
17480 new_symbol (die, type, cu);
17481
17482 while (child_die && child_die->tag)
17483 {
17484 process_die (child_die, cu);
17485 child_die = child_die->sibling;
17486 }
17487 }
17488
17489 /* Return the name of the namespace represented by DIE. Set
17490 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17491 namespace. */
17492
17493 static const char *
17494 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17495 {
17496 struct die_info *current_die;
17497 const char *name = NULL;
17498
17499 /* Loop through the extensions until we find a name. */
17500
17501 for (current_die = die;
17502 current_die != NULL;
17503 current_die = dwarf2_extension (die, &cu))
17504 {
17505 /* We don't use dwarf2_name here so that we can detect the absence
17506 of a name -> anonymous namespace. */
17507 name = dwarf2_string_attr (die, DW_AT_name, cu);
17508
17509 if (name != NULL)
17510 break;
17511 }
17512
17513 /* Is it an anonymous namespace? */
17514
17515 *is_anonymous = (name == NULL);
17516 if (*is_anonymous)
17517 name = CP_ANONYMOUS_NAMESPACE_STR;
17518
17519 return name;
17520 }
17521
17522 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17523 the user defined type vector. */
17524
17525 static struct type *
17526 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17527 {
17528 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17529 struct comp_unit_head *cu_header = &cu->header;
17530 struct type *type;
17531 struct attribute *attr_byte_size;
17532 struct attribute *attr_address_class;
17533 int byte_size, addr_class;
17534 struct type *target_type;
17535
17536 target_type = die_type (die, cu);
17537
17538 /* The die_type call above may have already set the type for this DIE. */
17539 type = get_die_type (die, cu);
17540 if (type)
17541 return type;
17542
17543 type = lookup_pointer_type (target_type);
17544
17545 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17546 if (attr_byte_size)
17547 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17548 else
17549 byte_size = cu_header->addr_size;
17550
17551 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17552 if (attr_address_class)
17553 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17554 else
17555 addr_class = DW_ADDR_none;
17556
17557 ULONGEST alignment = get_alignment (cu, die);
17558
17559 /* If the pointer size, alignment, or address class is different
17560 than the default, create a type variant marked as such and set
17561 the length accordingly. */
17562 if (TYPE_LENGTH (type) != byte_size
17563 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17564 && alignment != TYPE_RAW_ALIGN (type))
17565 || addr_class != DW_ADDR_none)
17566 {
17567 if (gdbarch_address_class_type_flags_p (gdbarch))
17568 {
17569 type_instance_flags type_flags
17570 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17571 addr_class);
17572 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17573 == 0);
17574 type = make_type_with_address_space (type, type_flags);
17575 }
17576 else if (TYPE_LENGTH (type) != byte_size)
17577 {
17578 complaint (_("invalid pointer size %d"), byte_size);
17579 }
17580 else if (TYPE_RAW_ALIGN (type) != alignment)
17581 {
17582 complaint (_("Invalid DW_AT_alignment"
17583 " - DIE at %s [in module %s]"),
17584 sect_offset_str (die->sect_off),
17585 objfile_name (cu->per_objfile->objfile));
17586 }
17587 else
17588 {
17589 /* Should we also complain about unhandled address classes? */
17590 }
17591 }
17592
17593 TYPE_LENGTH (type) = byte_size;
17594 set_type_align (type, alignment);
17595 return set_die_type (die, type, cu);
17596 }
17597
17598 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17599 the user defined type vector. */
17600
17601 static struct type *
17602 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17603 {
17604 struct type *type;
17605 struct type *to_type;
17606 struct type *domain;
17607
17608 to_type = die_type (die, cu);
17609 domain = die_containing_type (die, cu);
17610
17611 /* The calls above may have already set the type for this DIE. */
17612 type = get_die_type (die, cu);
17613 if (type)
17614 return type;
17615
17616 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17617 type = lookup_methodptr_type (to_type);
17618 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17619 {
17620 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17621
17622 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17623 to_type->fields (), to_type->num_fields (),
17624 to_type->has_varargs ());
17625 type = lookup_methodptr_type (new_type);
17626 }
17627 else
17628 type = lookup_memberptr_type (to_type, domain);
17629
17630 return set_die_type (die, type, cu);
17631 }
17632
17633 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17634 the user defined type vector. */
17635
17636 static struct type *
17637 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17638 enum type_code refcode)
17639 {
17640 struct comp_unit_head *cu_header = &cu->header;
17641 struct type *type, *target_type;
17642 struct attribute *attr;
17643
17644 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17645
17646 target_type = die_type (die, cu);
17647
17648 /* The die_type call above may have already set the type for this DIE. */
17649 type = get_die_type (die, cu);
17650 if (type)
17651 return type;
17652
17653 type = lookup_reference_type (target_type, refcode);
17654 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17655 if (attr != nullptr)
17656 {
17657 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17658 }
17659 else
17660 {
17661 TYPE_LENGTH (type) = cu_header->addr_size;
17662 }
17663 maybe_set_alignment (cu, die, type);
17664 return set_die_type (die, type, cu);
17665 }
17666
17667 /* Add the given cv-qualifiers to the element type of the array. GCC
17668 outputs DWARF type qualifiers that apply to an array, not the
17669 element type. But GDB relies on the array element type to carry
17670 the cv-qualifiers. This mimics section 6.7.3 of the C99
17671 specification. */
17672
17673 static struct type *
17674 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17675 struct type *base_type, int cnst, int voltl)
17676 {
17677 struct type *el_type, *inner_array;
17678
17679 base_type = copy_type (base_type);
17680 inner_array = base_type;
17681
17682 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17683 {
17684 TYPE_TARGET_TYPE (inner_array) =
17685 copy_type (TYPE_TARGET_TYPE (inner_array));
17686 inner_array = TYPE_TARGET_TYPE (inner_array);
17687 }
17688
17689 el_type = TYPE_TARGET_TYPE (inner_array);
17690 cnst |= TYPE_CONST (el_type);
17691 voltl |= TYPE_VOLATILE (el_type);
17692 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17693
17694 return set_die_type (die, base_type, cu);
17695 }
17696
17697 static struct type *
17698 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17699 {
17700 struct type *base_type, *cv_type;
17701
17702 base_type = die_type (die, cu);
17703
17704 /* The die_type call above may have already set the type for this DIE. */
17705 cv_type = get_die_type (die, cu);
17706 if (cv_type)
17707 return cv_type;
17708
17709 /* In case the const qualifier is applied to an array type, the element type
17710 is so qualified, not the array type (section 6.7.3 of C99). */
17711 if (base_type->code () == TYPE_CODE_ARRAY)
17712 return add_array_cv_type (die, cu, base_type, 1, 0);
17713
17714 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17715 return set_die_type (die, cv_type, cu);
17716 }
17717
17718 static struct type *
17719 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17720 {
17721 struct type *base_type, *cv_type;
17722
17723 base_type = die_type (die, cu);
17724
17725 /* The die_type call above may have already set the type for this DIE. */
17726 cv_type = get_die_type (die, cu);
17727 if (cv_type)
17728 return cv_type;
17729
17730 /* In case the volatile qualifier is applied to an array type, the
17731 element type is so qualified, not the array type (section 6.7.3
17732 of C99). */
17733 if (base_type->code () == TYPE_CODE_ARRAY)
17734 return add_array_cv_type (die, cu, base_type, 0, 1);
17735
17736 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17737 return set_die_type (die, cv_type, cu);
17738 }
17739
17740 /* Handle DW_TAG_restrict_type. */
17741
17742 static struct type *
17743 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17744 {
17745 struct type *base_type, *cv_type;
17746
17747 base_type = die_type (die, cu);
17748
17749 /* The die_type call above may have already set the type for this DIE. */
17750 cv_type = get_die_type (die, cu);
17751 if (cv_type)
17752 return cv_type;
17753
17754 cv_type = make_restrict_type (base_type);
17755 return set_die_type (die, cv_type, cu);
17756 }
17757
17758 /* Handle DW_TAG_atomic_type. */
17759
17760 static struct type *
17761 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17762 {
17763 struct type *base_type, *cv_type;
17764
17765 base_type = die_type (die, cu);
17766
17767 /* The die_type call above may have already set the type for this DIE. */
17768 cv_type = get_die_type (die, cu);
17769 if (cv_type)
17770 return cv_type;
17771
17772 cv_type = make_atomic_type (base_type);
17773 return set_die_type (die, cv_type, cu);
17774 }
17775
17776 /* Extract all information from a DW_TAG_string_type DIE and add to
17777 the user defined type vector. It isn't really a user defined type,
17778 but it behaves like one, with other DIE's using an AT_user_def_type
17779 attribute to reference it. */
17780
17781 static struct type *
17782 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17783 {
17784 struct objfile *objfile = cu->per_objfile->objfile;
17785 struct gdbarch *gdbarch = objfile->arch ();
17786 struct type *type, *range_type, *index_type, *char_type;
17787 struct attribute *attr;
17788 struct dynamic_prop prop;
17789 bool length_is_constant = true;
17790 LONGEST length;
17791
17792 /* There are a couple of places where bit sizes might be made use of
17793 when parsing a DW_TAG_string_type, however, no producer that we know
17794 of make use of these. Handling bit sizes that are a multiple of the
17795 byte size is easy enough, but what about other bit sizes? Lets deal
17796 with that problem when we have to. Warn about these attributes being
17797 unsupported, then parse the type and ignore them like we always
17798 have. */
17799 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17800 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17801 {
17802 static bool warning_printed = false;
17803 if (!warning_printed)
17804 {
17805 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17806 "currently supported on DW_TAG_string_type."));
17807 warning_printed = true;
17808 }
17809 }
17810
17811 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17812 if (attr != nullptr && !attr->form_is_constant ())
17813 {
17814 /* The string length describes the location at which the length of
17815 the string can be found. The size of the length field can be
17816 specified with one of the attributes below. */
17817 struct type *prop_type;
17818 struct attribute *len
17819 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17820 if (len == nullptr)
17821 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17822 if (len != nullptr && len->form_is_constant ())
17823 {
17824 /* Pass 0 as the default as we know this attribute is constant
17825 and the default value will not be returned. */
17826 LONGEST sz = len->constant_value (0);
17827 prop_type = cu->per_objfile->int_type (sz, true);
17828 }
17829 else
17830 {
17831 /* If the size is not specified then we assume it is the size of
17832 an address on this target. */
17833 prop_type = cu->addr_sized_int_type (true);
17834 }
17835
17836 /* Convert the attribute into a dynamic property. */
17837 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17838 length = 1;
17839 else
17840 length_is_constant = false;
17841 }
17842 else if (attr != nullptr)
17843 {
17844 /* This DW_AT_string_length just contains the length with no
17845 indirection. There's no need to create a dynamic property in this
17846 case. Pass 0 for the default value as we know it will not be
17847 returned in this case. */
17848 length = attr->constant_value (0);
17849 }
17850 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17851 {
17852 /* We don't currently support non-constant byte sizes for strings. */
17853 length = attr->constant_value (1);
17854 }
17855 else
17856 {
17857 /* Use 1 as a fallback length if we have nothing else. */
17858 length = 1;
17859 }
17860
17861 index_type = objfile_type (objfile)->builtin_int;
17862 if (length_is_constant)
17863 range_type = create_static_range_type (NULL, index_type, 1, length);
17864 else
17865 {
17866 struct dynamic_prop low_bound;
17867
17868 low_bound.set_const_val (1);
17869 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17870 }
17871 char_type = language_string_char_type (cu->language_defn, gdbarch);
17872 type = create_string_type (NULL, char_type, range_type);
17873
17874 return set_die_type (die, type, cu);
17875 }
17876
17877 /* Assuming that DIE corresponds to a function, returns nonzero
17878 if the function is prototyped. */
17879
17880 static int
17881 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17882 {
17883 struct attribute *attr;
17884
17885 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17886 if (attr && attr->as_boolean ())
17887 return 1;
17888
17889 /* The DWARF standard implies that the DW_AT_prototyped attribute
17890 is only meaningful for C, but the concept also extends to other
17891 languages that allow unprototyped functions (Eg: Objective C).
17892 For all other languages, assume that functions are always
17893 prototyped. */
17894 if (cu->language != language_c
17895 && cu->language != language_objc
17896 && cu->language != language_opencl)
17897 return 1;
17898
17899 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17900 prototyped and unprototyped functions; default to prototyped,
17901 since that is more common in modern code (and RealView warns
17902 about unprototyped functions). */
17903 if (producer_is_realview (cu->producer))
17904 return 1;
17905
17906 return 0;
17907 }
17908
17909 /* Handle DIES due to C code like:
17910
17911 struct foo
17912 {
17913 int (*funcp)(int a, long l);
17914 int b;
17915 };
17916
17917 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17918
17919 static struct type *
17920 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17921 {
17922 struct objfile *objfile = cu->per_objfile->objfile;
17923 struct type *type; /* Type that this function returns. */
17924 struct type *ftype; /* Function that returns above type. */
17925 struct attribute *attr;
17926
17927 type = die_type (die, cu);
17928
17929 /* The die_type call above may have already set the type for this DIE. */
17930 ftype = get_die_type (die, cu);
17931 if (ftype)
17932 return ftype;
17933
17934 ftype = lookup_function_type (type);
17935
17936 if (prototyped_function_p (die, cu))
17937 ftype->set_is_prototyped (true);
17938
17939 /* Store the calling convention in the type if it's available in
17940 the subroutine die. Otherwise set the calling convention to
17941 the default value DW_CC_normal. */
17942 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17943 if (attr != nullptr
17944 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
17945 TYPE_CALLING_CONVENTION (ftype)
17946 = (enum dwarf_calling_convention) attr->constant_value (0);
17947 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17948 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17949 else
17950 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17951
17952 /* Record whether the function returns normally to its caller or not
17953 if the DWARF producer set that information. */
17954 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17955 if (attr && attr->as_boolean ())
17956 TYPE_NO_RETURN (ftype) = 1;
17957
17958 /* We need to add the subroutine type to the die immediately so
17959 we don't infinitely recurse when dealing with parameters
17960 declared as the same subroutine type. */
17961 set_die_type (die, ftype, cu);
17962
17963 if (die->child != NULL)
17964 {
17965 struct type *void_type = objfile_type (objfile)->builtin_void;
17966 struct die_info *child_die;
17967 int nparams, iparams;
17968
17969 /* Count the number of parameters.
17970 FIXME: GDB currently ignores vararg functions, but knows about
17971 vararg member functions. */
17972 nparams = 0;
17973 child_die = die->child;
17974 while (child_die && child_die->tag)
17975 {
17976 if (child_die->tag == DW_TAG_formal_parameter)
17977 nparams++;
17978 else if (child_die->tag == DW_TAG_unspecified_parameters)
17979 ftype->set_has_varargs (true);
17980
17981 child_die = child_die->sibling;
17982 }
17983
17984 /* Allocate storage for parameters and fill them in. */
17985 ftype->set_num_fields (nparams);
17986 ftype->set_fields
17987 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17988
17989 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17990 even if we error out during the parameters reading below. */
17991 for (iparams = 0; iparams < nparams; iparams++)
17992 ftype->field (iparams).set_type (void_type);
17993
17994 iparams = 0;
17995 child_die = die->child;
17996 while (child_die && child_die->tag)
17997 {
17998 if (child_die->tag == DW_TAG_formal_parameter)
17999 {
18000 struct type *arg_type;
18001
18002 /* DWARF version 2 has no clean way to discern C++
18003 static and non-static member functions. G++ helps
18004 GDB by marking the first parameter for non-static
18005 member functions (which is the this pointer) as
18006 artificial. We pass this information to
18007 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
18008
18009 DWARF version 3 added DW_AT_object_pointer, which GCC
18010 4.5 does not yet generate. */
18011 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
18012 if (attr != nullptr)
18013 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
18014 else
18015 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
18016 arg_type = die_type (child_die, cu);
18017
18018 /* RealView does not mark THIS as const, which the testsuite
18019 expects. GCC marks THIS as const in method definitions,
18020 but not in the class specifications (GCC PR 43053). */
18021 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
18022 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18023 {
18024 int is_this = 0;
18025 struct dwarf2_cu *arg_cu = cu;
18026 const char *name = dwarf2_name (child_die, cu);
18027
18028 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
18029 if (attr != nullptr)
18030 {
18031 /* If the compiler emits this, use it. */
18032 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18033 is_this = 1;
18034 }
18035 else if (name && strcmp (name, "this") == 0)
18036 /* Function definitions will have the argument names. */
18037 is_this = 1;
18038 else if (name == NULL && iparams == 0)
18039 /* Declarations may not have the names, so like
18040 elsewhere in GDB, assume an artificial first
18041 argument is "this". */
18042 is_this = 1;
18043
18044 if (is_this)
18045 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18046 arg_type, 0);
18047 }
18048
18049 ftype->field (iparams).set_type (arg_type);
18050 iparams++;
18051 }
18052 child_die = child_die->sibling;
18053 }
18054 }
18055
18056 return ftype;
18057 }
18058
18059 static struct type *
18060 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
18061 {
18062 struct objfile *objfile = cu->per_objfile->objfile;
18063 const char *name = NULL;
18064 struct type *this_type, *target_type;
18065
18066 name = dwarf2_full_name (NULL, die, cu);
18067 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
18068 this_type->set_target_is_stub (true);
18069 set_die_type (die, this_type, cu);
18070 target_type = die_type (die, cu);
18071 if (target_type != this_type)
18072 TYPE_TARGET_TYPE (this_type) = target_type;
18073 else
18074 {
18075 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18076 spec and cause infinite loops in GDB. */
18077 complaint (_("Self-referential DW_TAG_typedef "
18078 "- DIE at %s [in module %s]"),
18079 sect_offset_str (die->sect_off), objfile_name (objfile));
18080 TYPE_TARGET_TYPE (this_type) = NULL;
18081 }
18082 if (name == NULL)
18083 {
18084 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18085 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18086 Handle these by just returning the target type, rather than
18087 constructing an anonymous typedef type and trying to handle this
18088 elsewhere. */
18089 set_die_type (die, target_type, cu);
18090 return target_type;
18091 }
18092 return this_type;
18093 }
18094
18095 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18096 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18097
18098 If the numerator and/or numerator attribute is missing,
18099 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18100 untouched. */
18101
18102 static void
18103 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
18104 LONGEST *numerator, LONGEST *denominator)
18105 {
18106 struct attribute *num_attr, *denom_attr;
18107
18108 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18109 if (num_attr == nullptr)
18110 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18111 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18112
18113 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18114 if (denom_attr == nullptr)
18115 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18116 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18117
18118 if (num_attr == nullptr || denom_attr == nullptr)
18119 return;
18120
18121 *numerator = num_attr->constant_value (1);
18122 *denominator = denom_attr->constant_value (1);
18123 }
18124
18125 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
18126 rational constant, rather than a signed one.
18127
18128 If the rational constant has a negative value, a complaint
18129 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18130
18131 static void
18132 get_dwarf2_unsigned_rational_constant (struct die_info *die,
18133 struct dwarf2_cu *cu,
18134 ULONGEST *numerator,
18135 ULONGEST *denominator)
18136 {
18137 LONGEST num = 1, denom = 1;
18138
18139 get_dwarf2_rational_constant (die, cu, &num, &denom);
18140 if (num < 0 && denom < 0)
18141 {
18142 num = -num;
18143 denom = -denom;
18144 }
18145 else if (num < 0)
18146 {
18147 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18148 " in DIE at %s"),
18149 sect_offset_str (die->sect_off));
18150 return;
18151 }
18152 else if (denom < 0)
18153 {
18154 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18155 " in DIE at %s"),
18156 sect_offset_str (die->sect_off));
18157 return;
18158 }
18159
18160 *numerator = num;
18161 *denominator = denom;
18162 }
18163
18164 /* Assuming DIE corresponds to a fixed point type, finish the creation
18165 of the corresponding TYPE by setting its type-specific data.
18166 CU is the DIE's CU. */
18167
18168 static void
18169 finish_fixed_point_type (struct type *type, struct die_info *die,
18170 struct dwarf2_cu *cu)
18171 {
18172 struct attribute *attr;
18173 /* Numerator and denominator of our fixed-point type's scaling factor.
18174 The default is a scaling factor of 1, which we use as a fallback
18175 when we are not able to decode it (problem with the debugging info,
18176 unsupported forms, bug in GDB, etc...). Using that as the default
18177 allows us to at least print the unscaled value, which might still
18178 be useful to a user. */
18179 ULONGEST scale_num = 1;
18180 ULONGEST scale_denom = 1;
18181
18182 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18183 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18184
18185 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18186 if (!attr)
18187 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18188 if (!attr)
18189 attr = dwarf2_attr (die, DW_AT_small, cu);
18190
18191 if (attr == nullptr)
18192 {
18193 /* Scaling factor not found. Assume a scaling factor of 1,
18194 and hope for the best. At least the user will be able to see
18195 the encoded value. */
18196 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18197 sect_offset_str (die->sect_off));
18198 }
18199 else if (attr->name == DW_AT_binary_scale)
18200 {
18201 LONGEST scale_exp = attr->constant_value (0);
18202 ULONGEST *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18203
18204 *num_or_denom = 1 << std::abs (scale_exp);
18205 }
18206 else if (attr->name == DW_AT_decimal_scale)
18207 {
18208 LONGEST scale_exp = attr->constant_value (0);
18209 ULONGEST *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18210
18211 *num_or_denom = uinteger_pow (10, std::abs (scale_exp));
18212 }
18213 else if (attr->name == DW_AT_small)
18214 {
18215 struct die_info *scale_die;
18216 struct dwarf2_cu *scale_cu = cu;
18217
18218 scale_die = follow_die_ref (die, attr, &scale_cu);
18219 if (scale_die->tag == DW_TAG_constant)
18220 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18221 &scale_num, &scale_denom);
18222 else
18223 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18224 " (DIE at %s)"),
18225 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18226 }
18227 else
18228 {
18229 complaint (_("unsupported scale attribute %s for fixed-point type"
18230 " (DIE at %s)"),
18231 dwarf_attr_name (attr->name),
18232 sect_offset_str (die->sect_off));
18233 }
18234
18235 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
18236
18237 gdb_mpz tmp_z (scale_num);
18238 mpz_set (mpq_numref (scaling_factor.val), tmp_z.val);
18239
18240 tmp_z = scale_denom;
18241 mpz_set (mpq_denref (scaling_factor.val), tmp_z.val);
18242
18243 mpq_canonicalize (scaling_factor.val);
18244 }
18245
18246 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18247 (which may be different from NAME) to the architecture back-end to allow
18248 it to guess the correct format if necessary. */
18249
18250 static struct type *
18251 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
18252 const char *name_hint, enum bfd_endian byte_order)
18253 {
18254 struct gdbarch *gdbarch = objfile->arch ();
18255 const struct floatformat **format;
18256 struct type *type;
18257
18258 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18259 if (format)
18260 type = init_float_type (objfile, bits, name, format, byte_order);
18261 else
18262 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18263
18264 return type;
18265 }
18266
18267 /* Allocate an integer type of size BITS and name NAME. */
18268
18269 static struct type *
18270 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18271 int bits, int unsigned_p, const char *name)
18272 {
18273 struct type *type;
18274
18275 /* Versions of Intel's C Compiler generate an integer type called "void"
18276 instead of using DW_TAG_unspecified_type. This has been seen on
18277 at least versions 14, 17, and 18. */
18278 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18279 && strcmp (name, "void") == 0)
18280 type = objfile_type (objfile)->builtin_void;
18281 else
18282 type = init_integer_type (objfile, bits, unsigned_p, name);
18283
18284 return type;
18285 }
18286
18287 /* Return true if DIE has a DW_AT_small attribute whose value is
18288 a constant rational, where both the numerator and denominator
18289 are equal to zero.
18290
18291 CU is the DIE's Compilation Unit. */
18292
18293 static bool
18294 has_zero_over_zero_small_attribute (struct die_info *die,
18295 struct dwarf2_cu *cu)
18296 {
18297 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18298 if (attr == nullptr)
18299 return false;
18300
18301 struct dwarf2_cu *scale_cu = cu;
18302 struct die_info *scale_die
18303 = follow_die_ref (die, attr, &scale_cu);
18304
18305 if (scale_die->tag != DW_TAG_constant)
18306 return false;
18307
18308 LONGEST num = 1, denom = 1;
18309 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18310 return (num == 0 && denom == 0);
18311 }
18312
18313 /* Initialise and return a floating point type of size BITS suitable for
18314 use as a component of a complex number. The NAME_HINT is passed through
18315 when initialising the floating point type and is the name of the complex
18316 type.
18317
18318 As DWARF doesn't currently provide an explicit name for the components
18319 of a complex number, but it can be helpful to have these components
18320 named, we try to select a suitable name based on the size of the
18321 component. */
18322 static struct type *
18323 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18324 struct objfile *objfile,
18325 int bits, const char *name_hint,
18326 enum bfd_endian byte_order)
18327 {
18328 gdbarch *gdbarch = objfile->arch ();
18329 struct type *tt = nullptr;
18330
18331 /* Try to find a suitable floating point builtin type of size BITS.
18332 We're going to use the name of this type as the name for the complex
18333 target type that we are about to create. */
18334 switch (cu->language)
18335 {
18336 case language_fortran:
18337 switch (bits)
18338 {
18339 case 32:
18340 tt = builtin_f_type (gdbarch)->builtin_real;
18341 break;
18342 case 64:
18343 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18344 break;
18345 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18346 case 128:
18347 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18348 break;
18349 }
18350 break;
18351 default:
18352 switch (bits)
18353 {
18354 case 32:
18355 tt = builtin_type (gdbarch)->builtin_float;
18356 break;
18357 case 64:
18358 tt = builtin_type (gdbarch)->builtin_double;
18359 break;
18360 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18361 case 128:
18362 tt = builtin_type (gdbarch)->builtin_long_double;
18363 break;
18364 }
18365 break;
18366 }
18367
18368 /* If the type we found doesn't match the size we were looking for, then
18369 pretend we didn't find a type at all, the complex target type we
18370 create will then be nameless. */
18371 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18372 tt = nullptr;
18373
18374 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18375 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18376 }
18377
18378 /* Find a representation of a given base type and install
18379 it in the TYPE field of the die. */
18380
18381 static struct type *
18382 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18383 {
18384 struct objfile *objfile = cu->per_objfile->objfile;
18385 struct type *type;
18386 struct attribute *attr;
18387 int encoding = 0, bits = 0;
18388 const char *name;
18389 gdbarch *arch;
18390
18391 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18392 if (attr != nullptr && attr->form_is_constant ())
18393 encoding = attr->constant_value (0);
18394 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18395 if (attr != nullptr)
18396 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18397 name = dwarf2_name (die, cu);
18398 if (!name)
18399 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18400
18401 arch = objfile->arch ();
18402 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18403
18404 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18405 if (attr != nullptr && attr->form_is_constant ())
18406 {
18407 int endianity = attr->constant_value (0);
18408
18409 switch (endianity)
18410 {
18411 case DW_END_big:
18412 byte_order = BFD_ENDIAN_BIG;
18413 break;
18414 case DW_END_little:
18415 byte_order = BFD_ENDIAN_LITTLE;
18416 break;
18417 default:
18418 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18419 break;
18420 }
18421 }
18422
18423 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18424 && cu->language == language_ada
18425 && has_zero_over_zero_small_attribute (die, cu))
18426 {
18427 /* brobecker/2018-02-24: This is a fixed point type for which
18428 the scaling factor is represented as fraction whose value
18429 does not make sense (zero divided by zero), so we should
18430 normally never see these. However, there is a small category
18431 of fixed point types for which GNAT is unable to provide
18432 the scaling factor via the standard DWARF mechanisms, and
18433 for which the info is provided via the GNAT encodings instead.
18434 This is likely what this DIE is about.
18435
18436 Ideally, GNAT should be declaring this type the same way
18437 it declares other fixed point types when using the legacy
18438 GNAT encoding, which is to use a simple signed or unsigned
18439 base type. A report to the GNAT team has been created to
18440 look into it. In the meantime, pretend this type is a simple
18441 signed or unsigned integral, rather than a fixed point type,
18442 to avoid any confusion later on as to how to process this type. */
18443 encoding = (encoding == DW_ATE_signed_fixed
18444 ? DW_ATE_signed
18445 : DW_ATE_unsigned);
18446 }
18447
18448 switch (encoding)
18449 {
18450 case DW_ATE_address:
18451 /* Turn DW_ATE_address into a void * pointer. */
18452 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18453 type = init_pointer_type (objfile, bits, name, type);
18454 break;
18455 case DW_ATE_boolean:
18456 type = init_boolean_type (objfile, bits, 1, name);
18457 break;
18458 case DW_ATE_complex_float:
18459 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18460 byte_order);
18461 if (type->code () == TYPE_CODE_ERROR)
18462 {
18463 if (name == nullptr)
18464 {
18465 struct obstack *obstack
18466 = &cu->per_objfile->objfile->objfile_obstack;
18467 name = obconcat (obstack, "_Complex ", type->name (),
18468 nullptr);
18469 }
18470 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18471 }
18472 else
18473 type = init_complex_type (name, type);
18474 break;
18475 case DW_ATE_decimal_float:
18476 type = init_decfloat_type (objfile, bits, name);
18477 break;
18478 case DW_ATE_float:
18479 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18480 break;
18481 case DW_ATE_signed:
18482 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18483 break;
18484 case DW_ATE_unsigned:
18485 if (cu->language == language_fortran
18486 && name
18487 && startswith (name, "character("))
18488 type = init_character_type (objfile, bits, 1, name);
18489 else
18490 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18491 break;
18492 case DW_ATE_signed_char:
18493 if (cu->language == language_ada || cu->language == language_m2
18494 || cu->language == language_pascal
18495 || cu->language == language_fortran)
18496 type = init_character_type (objfile, bits, 0, name);
18497 else
18498 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18499 break;
18500 case DW_ATE_unsigned_char:
18501 if (cu->language == language_ada || cu->language == language_m2
18502 || cu->language == language_pascal
18503 || cu->language == language_fortran
18504 || cu->language == language_rust)
18505 type = init_character_type (objfile, bits, 1, name);
18506 else
18507 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18508 break;
18509 case DW_ATE_UTF:
18510 {
18511 if (bits == 16)
18512 type = builtin_type (arch)->builtin_char16;
18513 else if (bits == 32)
18514 type = builtin_type (arch)->builtin_char32;
18515 else
18516 {
18517 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18518 bits);
18519 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18520 }
18521 return set_die_type (die, type, cu);
18522 }
18523 break;
18524 case DW_ATE_signed_fixed:
18525 type = init_fixed_point_type (objfile, bits, 0, name);
18526 finish_fixed_point_type (type, die, cu);
18527 break;
18528 case DW_ATE_unsigned_fixed:
18529 type = init_fixed_point_type (objfile, bits, 1, name);
18530 finish_fixed_point_type (type, die, cu);
18531 break;
18532
18533 default:
18534 complaint (_("unsupported DW_AT_encoding: '%s'"),
18535 dwarf_type_encoding_name (encoding));
18536 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18537 break;
18538 }
18539
18540 if (name && strcmp (name, "char") == 0)
18541 type->set_has_no_signedness (true);
18542
18543 maybe_set_alignment (cu, die, type);
18544
18545 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18546
18547 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18548 {
18549 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18550 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18551 {
18552 unsigned real_bit_size = attr->as_unsigned ();
18553 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18554 /* Only use the attributes if they make sense together. */
18555 if (attr == nullptr
18556 || (attr->as_unsigned () + real_bit_size
18557 <= 8 * TYPE_LENGTH (type)))
18558 {
18559 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18560 = real_bit_size;
18561 if (attr != nullptr)
18562 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18563 = attr->as_unsigned ();
18564 }
18565 }
18566 }
18567
18568 return set_die_type (die, type, cu);
18569 }
18570
18571 /* Parse dwarf attribute if it's a block, reference or constant and put the
18572 resulting value of the attribute into struct bound_prop.
18573 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18574
18575 static int
18576 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18577 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18578 struct type *default_type)
18579 {
18580 struct dwarf2_property_baton *baton;
18581 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18582 struct objfile *objfile = per_objfile->objfile;
18583 struct obstack *obstack = &objfile->objfile_obstack;
18584
18585 gdb_assert (default_type != NULL);
18586
18587 if (attr == NULL || prop == NULL)
18588 return 0;
18589
18590 if (attr->form_is_block ())
18591 {
18592 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18593 baton->property_type = default_type;
18594 baton->locexpr.per_cu = cu->per_cu;
18595 baton->locexpr.per_objfile = per_objfile;
18596
18597 struct dwarf_block *block = attr->as_block ();
18598 baton->locexpr.size = block->size;
18599 baton->locexpr.data = block->data;
18600 switch (attr->name)
18601 {
18602 case DW_AT_string_length:
18603 baton->locexpr.is_reference = true;
18604 break;
18605 default:
18606 baton->locexpr.is_reference = false;
18607 break;
18608 }
18609
18610 prop->set_locexpr (baton);
18611 gdb_assert (prop->baton () != NULL);
18612 }
18613 else if (attr->form_is_ref ())
18614 {
18615 struct dwarf2_cu *target_cu = cu;
18616 struct die_info *target_die;
18617 struct attribute *target_attr;
18618
18619 target_die = follow_die_ref (die, attr, &target_cu);
18620 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18621 if (target_attr == NULL)
18622 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18623 target_cu);
18624 if (target_attr == NULL)
18625 return 0;
18626
18627 switch (target_attr->name)
18628 {
18629 case DW_AT_location:
18630 if (target_attr->form_is_section_offset ())
18631 {
18632 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18633 baton->property_type = die_type (target_die, target_cu);
18634 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18635 prop->set_loclist (baton);
18636 gdb_assert (prop->baton () != NULL);
18637 }
18638 else if (target_attr->form_is_block ())
18639 {
18640 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18641 baton->property_type = die_type (target_die, target_cu);
18642 baton->locexpr.per_cu = cu->per_cu;
18643 baton->locexpr.per_objfile = per_objfile;
18644 struct dwarf_block *block = target_attr->as_block ();
18645 baton->locexpr.size = block->size;
18646 baton->locexpr.data = block->data;
18647 baton->locexpr.is_reference = true;
18648 prop->set_locexpr (baton);
18649 gdb_assert (prop->baton () != NULL);
18650 }
18651 else
18652 {
18653 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18654 "dynamic property");
18655 return 0;
18656 }
18657 break;
18658 case DW_AT_data_member_location:
18659 {
18660 LONGEST offset;
18661
18662 if (!handle_data_member_location (target_die, target_cu,
18663 &offset))
18664 return 0;
18665
18666 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18667 baton->property_type = read_type_die (target_die->parent,
18668 target_cu);
18669 baton->offset_info.offset = offset;
18670 baton->offset_info.type = die_type (target_die, target_cu);
18671 prop->set_addr_offset (baton);
18672 break;
18673 }
18674 }
18675 }
18676 else if (attr->form_is_constant ())
18677 prop->set_const_val (attr->constant_value (0));
18678 else
18679 {
18680 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18681 dwarf2_name (die, cu));
18682 return 0;
18683 }
18684
18685 return 1;
18686 }
18687
18688 /* See read.h. */
18689
18690 struct type *
18691 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
18692 {
18693 struct type *int_type;
18694
18695 /* Helper macro to examine the various builtin types. */
18696 #define TRY_TYPE(F) \
18697 int_type = (unsigned_p \
18698 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18699 : objfile_type (objfile)->builtin_ ## F); \
18700 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18701 return int_type
18702
18703 TRY_TYPE (char);
18704 TRY_TYPE (short);
18705 TRY_TYPE (int);
18706 TRY_TYPE (long);
18707 TRY_TYPE (long_long);
18708
18709 #undef TRY_TYPE
18710
18711 gdb_assert_not_reached ("unable to find suitable integer type");
18712 }
18713
18714 /* See read.h. */
18715
18716 struct type *
18717 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
18718 {
18719 int addr_size = this->per_cu->addr_size ();
18720 return this->per_objfile->int_type (addr_size, unsigned_p);
18721 }
18722
18723 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18724 present (which is valid) then compute the default type based on the
18725 compilation units address size. */
18726
18727 static struct type *
18728 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18729 {
18730 struct type *index_type = die_type (die, cu);
18731
18732 /* Dwarf-2 specifications explicitly allows to create subrange types
18733 without specifying a base type.
18734 In that case, the base type must be set to the type of
18735 the lower bound, upper bound or count, in that order, if any of these
18736 three attributes references an object that has a type.
18737 If no base type is found, the Dwarf-2 specifications say that
18738 a signed integer type of size equal to the size of an address should
18739 be used.
18740 For the following C code: `extern char gdb_int [];'
18741 GCC produces an empty range DIE.
18742 FIXME: muller/2010-05-28: Possible references to object for low bound,
18743 high bound or count are not yet handled by this code. */
18744 if (index_type->code () == TYPE_CODE_VOID)
18745 index_type = cu->addr_sized_int_type (false);
18746
18747 return index_type;
18748 }
18749
18750 /* Read the given DW_AT_subrange DIE. */
18751
18752 static struct type *
18753 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18754 {
18755 struct type *base_type, *orig_base_type;
18756 struct type *range_type;
18757 struct attribute *attr;
18758 struct dynamic_prop low, high;
18759 int low_default_is_valid;
18760 int high_bound_is_count = 0;
18761 const char *name;
18762 ULONGEST negative_mask;
18763
18764 orig_base_type = read_subrange_index_type (die, cu);
18765
18766 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18767 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18768 creating the range type, but we use the result of check_typedef
18769 when examining properties of the type. */
18770 base_type = check_typedef (orig_base_type);
18771
18772 /* The die_type call above may have already set the type for this DIE. */
18773 range_type = get_die_type (die, cu);
18774 if (range_type)
18775 return range_type;
18776
18777 high.set_const_val (0);
18778
18779 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18780 omitting DW_AT_lower_bound. */
18781 switch (cu->language)
18782 {
18783 case language_c:
18784 case language_cplus:
18785 low.set_const_val (0);
18786 low_default_is_valid = 1;
18787 break;
18788 case language_fortran:
18789 low.set_const_val (1);
18790 low_default_is_valid = 1;
18791 break;
18792 case language_d:
18793 case language_objc:
18794 case language_rust:
18795 low.set_const_val (0);
18796 low_default_is_valid = (cu->header.version >= 4);
18797 break;
18798 case language_ada:
18799 case language_m2:
18800 case language_pascal:
18801 low.set_const_val (1);
18802 low_default_is_valid = (cu->header.version >= 4);
18803 break;
18804 default:
18805 low.set_const_val (0);
18806 low_default_is_valid = 0;
18807 break;
18808 }
18809
18810 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18811 if (attr != nullptr)
18812 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18813 else if (!low_default_is_valid)
18814 complaint (_("Missing DW_AT_lower_bound "
18815 "- DIE at %s [in module %s]"),
18816 sect_offset_str (die->sect_off),
18817 objfile_name (cu->per_objfile->objfile));
18818
18819 struct attribute *attr_ub, *attr_count;
18820 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18821 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18822 {
18823 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18824 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18825 {
18826 /* If bounds are constant do the final calculation here. */
18827 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18828 high.set_const_val (low.const_val () + high.const_val () - 1);
18829 else
18830 high_bound_is_count = 1;
18831 }
18832 else
18833 {
18834 if (attr_ub != NULL)
18835 complaint (_("Unresolved DW_AT_upper_bound "
18836 "- DIE at %s [in module %s]"),
18837 sect_offset_str (die->sect_off),
18838 objfile_name (cu->per_objfile->objfile));
18839 if (attr_count != NULL)
18840 complaint (_("Unresolved DW_AT_count "
18841 "- DIE at %s [in module %s]"),
18842 sect_offset_str (die->sect_off),
18843 objfile_name (cu->per_objfile->objfile));
18844 }
18845 }
18846
18847 LONGEST bias = 0;
18848 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18849 if (bias_attr != nullptr && bias_attr->form_is_constant ())
18850 bias = bias_attr->constant_value (0);
18851
18852 /* Normally, the DWARF producers are expected to use a signed
18853 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18854 But this is unfortunately not always the case, as witnessed
18855 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18856 is used instead. To work around that ambiguity, we treat
18857 the bounds as signed, and thus sign-extend their values, when
18858 the base type is signed. */
18859 negative_mask =
18860 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18861 if (low.kind () == PROP_CONST
18862 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
18863 low.set_const_val (low.const_val () | negative_mask);
18864 if (high.kind () == PROP_CONST
18865 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
18866 high.set_const_val (high.const_val () | negative_mask);
18867
18868 /* Check for bit and byte strides. */
18869 struct dynamic_prop byte_stride_prop;
18870 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18871 if (attr_byte_stride != nullptr)
18872 {
18873 struct type *prop_type = cu->addr_sized_int_type (false);
18874 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18875 prop_type);
18876 }
18877
18878 struct dynamic_prop bit_stride_prop;
18879 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18880 if (attr_bit_stride != nullptr)
18881 {
18882 /* It only makes sense to have either a bit or byte stride. */
18883 if (attr_byte_stride != nullptr)
18884 {
18885 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18886 "- DIE at %s [in module %s]"),
18887 sect_offset_str (die->sect_off),
18888 objfile_name (cu->per_objfile->objfile));
18889 attr_bit_stride = nullptr;
18890 }
18891 else
18892 {
18893 struct type *prop_type = cu->addr_sized_int_type (false);
18894 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18895 prop_type);
18896 }
18897 }
18898
18899 if (attr_byte_stride != nullptr
18900 || attr_bit_stride != nullptr)
18901 {
18902 bool byte_stride_p = (attr_byte_stride != nullptr);
18903 struct dynamic_prop *stride
18904 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18905
18906 range_type
18907 = create_range_type_with_stride (NULL, orig_base_type, &low,
18908 &high, bias, stride, byte_stride_p);
18909 }
18910 else
18911 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18912
18913 if (high_bound_is_count)
18914 range_type->bounds ()->flag_upper_bound_is_count = 1;
18915
18916 /* Ada expects an empty array on no boundary attributes. */
18917 if (attr == NULL && cu->language != language_ada)
18918 range_type->bounds ()->high.set_undefined ();
18919
18920 name = dwarf2_name (die, cu);
18921 if (name)
18922 range_type->set_name (name);
18923
18924 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18925 if (attr != nullptr)
18926 TYPE_LENGTH (range_type) = attr->constant_value (0);
18927
18928 maybe_set_alignment (cu, die, range_type);
18929
18930 set_die_type (die, range_type, cu);
18931
18932 /* set_die_type should be already done. */
18933 set_descriptive_type (range_type, die, cu);
18934
18935 return range_type;
18936 }
18937
18938 static struct type *
18939 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18940 {
18941 struct type *type;
18942
18943 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
18944 type->set_name (dwarf2_name (die, cu));
18945
18946 /* In Ada, an unspecified type is typically used when the description
18947 of the type is deferred to a different unit. When encountering
18948 such a type, we treat it as a stub, and try to resolve it later on,
18949 when needed. */
18950 if (cu->language == language_ada)
18951 type->set_is_stub (true);
18952
18953 return set_die_type (die, type, cu);
18954 }
18955
18956 /* Read a single die and all its descendents. Set the die's sibling
18957 field to NULL; set other fields in the die correctly, and set all
18958 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18959 location of the info_ptr after reading all of those dies. PARENT
18960 is the parent of the die in question. */
18961
18962 static struct die_info *
18963 read_die_and_children (const struct die_reader_specs *reader,
18964 const gdb_byte *info_ptr,
18965 const gdb_byte **new_info_ptr,
18966 struct die_info *parent)
18967 {
18968 struct die_info *die;
18969 const gdb_byte *cur_ptr;
18970
18971 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18972 if (die == NULL)
18973 {
18974 *new_info_ptr = cur_ptr;
18975 return NULL;
18976 }
18977 store_in_ref_table (die, reader->cu);
18978
18979 if (die->has_children)
18980 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18981 else
18982 {
18983 die->child = NULL;
18984 *new_info_ptr = cur_ptr;
18985 }
18986
18987 die->sibling = NULL;
18988 die->parent = parent;
18989 return die;
18990 }
18991
18992 /* Read a die, all of its descendents, and all of its siblings; set
18993 all of the fields of all of the dies correctly. Arguments are as
18994 in read_die_and_children. */
18995
18996 static struct die_info *
18997 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18998 const gdb_byte *info_ptr,
18999 const gdb_byte **new_info_ptr,
19000 struct die_info *parent)
19001 {
19002 struct die_info *first_die, *last_sibling;
19003 const gdb_byte *cur_ptr;
19004
19005 cur_ptr = info_ptr;
19006 first_die = last_sibling = NULL;
19007
19008 while (1)
19009 {
19010 struct die_info *die
19011 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
19012
19013 if (die == NULL)
19014 {
19015 *new_info_ptr = cur_ptr;
19016 return first_die;
19017 }
19018
19019 if (!first_die)
19020 first_die = die;
19021 else
19022 last_sibling->sibling = die;
19023
19024 last_sibling = die;
19025 }
19026 }
19027
19028 /* Read a die, all of its descendents, and all of its siblings; set
19029 all of the fields of all of the dies correctly. Arguments are as
19030 in read_die_and_children.
19031 This the main entry point for reading a DIE and all its children. */
19032
19033 static struct die_info *
19034 read_die_and_siblings (const struct die_reader_specs *reader,
19035 const gdb_byte *info_ptr,
19036 const gdb_byte **new_info_ptr,
19037 struct die_info *parent)
19038 {
19039 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19040 new_info_ptr, parent);
19041
19042 if (dwarf_die_debug)
19043 {
19044 fprintf_unfiltered (gdb_stdlog,
19045 "Read die from %s@0x%x of %s:\n",
19046 reader->die_section->get_name (),
19047 (unsigned) (info_ptr - reader->die_section->buffer),
19048 bfd_get_filename (reader->abfd));
19049 dump_die (die, dwarf_die_debug);
19050 }
19051
19052 return die;
19053 }
19054
19055 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19056 attributes.
19057 The caller is responsible for filling in the extra attributes
19058 and updating (*DIEP)->num_attrs.
19059 Set DIEP to point to a newly allocated die with its information,
19060 except for its child, sibling, and parent fields. */
19061
19062 static const gdb_byte *
19063 read_full_die_1 (const struct die_reader_specs *reader,
19064 struct die_info **diep, const gdb_byte *info_ptr,
19065 int num_extra_attrs)
19066 {
19067 unsigned int abbrev_number, bytes_read, i;
19068 struct abbrev_info *abbrev;
19069 struct die_info *die;
19070 struct dwarf2_cu *cu = reader->cu;
19071 bfd *abfd = reader->abfd;
19072
19073 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
19074 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19075 info_ptr += bytes_read;
19076 if (!abbrev_number)
19077 {
19078 *diep = NULL;
19079 return info_ptr;
19080 }
19081
19082 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
19083 if (!abbrev)
19084 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19085 abbrev_number,
19086 bfd_get_filename (abfd));
19087
19088 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
19089 die->sect_off = sect_off;
19090 die->tag = abbrev->tag;
19091 die->abbrev = abbrev_number;
19092 die->has_children = abbrev->has_children;
19093
19094 /* Make the result usable.
19095 The caller needs to update num_attrs after adding the extra
19096 attributes. */
19097 die->num_attrs = abbrev->num_attrs;
19098
19099 bool any_need_reprocess = false;
19100 for (i = 0; i < abbrev->num_attrs; ++i)
19101 {
19102 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19103 info_ptr);
19104 if (die->attrs[i].requires_reprocessing_p ())
19105 any_need_reprocess = true;
19106 }
19107
19108 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
19109 if (attr != nullptr && attr->form_is_unsigned ())
19110 cu->str_offsets_base = attr->as_unsigned ();
19111
19112 attr = die->attr (DW_AT_loclists_base);
19113 if (attr != nullptr)
19114 cu->loclist_base = attr->as_unsigned ();
19115
19116 auto maybe_addr_base = die->addr_base ();
19117 if (maybe_addr_base.has_value ())
19118 cu->addr_base = *maybe_addr_base;
19119
19120 attr = die->attr (DW_AT_rnglists_base);
19121 if (attr != nullptr)
19122 cu->ranges_base = attr->as_unsigned ();
19123
19124 if (any_need_reprocess)
19125 {
19126 for (i = 0; i < abbrev->num_attrs; ++i)
19127 {
19128 if (die->attrs[i].requires_reprocessing_p ())
19129 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19130 }
19131 }
19132 *diep = die;
19133 return info_ptr;
19134 }
19135
19136 /* Read a die and all its attributes.
19137 Set DIEP to point to a newly allocated die with its information,
19138 except for its child, sibling, and parent fields. */
19139
19140 static const gdb_byte *
19141 read_full_die (const struct die_reader_specs *reader,
19142 struct die_info **diep, const gdb_byte *info_ptr)
19143 {
19144 const gdb_byte *result;
19145
19146 result = read_full_die_1 (reader, diep, info_ptr, 0);
19147
19148 if (dwarf_die_debug)
19149 {
19150 fprintf_unfiltered (gdb_stdlog,
19151 "Read die from %s@0x%x of %s:\n",
19152 reader->die_section->get_name (),
19153 (unsigned) (info_ptr - reader->die_section->buffer),
19154 bfd_get_filename (reader->abfd));
19155 dump_die (*diep, dwarf_die_debug);
19156 }
19157
19158 return result;
19159 }
19160 \f
19161
19162 /* Returns nonzero if TAG represents a type that we might generate a partial
19163 symbol for. */
19164
19165 static int
19166 is_type_tag_for_partial (int tag, enum language lang)
19167 {
19168 switch (tag)
19169 {
19170 #if 0
19171 /* Some types that would be reasonable to generate partial symbols for,
19172 that we don't at present. Note that normally this does not
19173 matter, mainly because C compilers don't give names to these
19174 types, but instead emit DW_TAG_typedef. */
19175 case DW_TAG_file_type:
19176 case DW_TAG_ptr_to_member_type:
19177 case DW_TAG_set_type:
19178 case DW_TAG_string_type:
19179 case DW_TAG_subroutine_type:
19180 #endif
19181
19182 /* GNAT may emit an array with a name, but no typedef, so we
19183 need to make a symbol in this case. */
19184 case DW_TAG_array_type:
19185 return lang == language_ada;
19186
19187 case DW_TAG_base_type:
19188 case DW_TAG_class_type:
19189 case DW_TAG_interface_type:
19190 case DW_TAG_enumeration_type:
19191 case DW_TAG_structure_type:
19192 case DW_TAG_subrange_type:
19193 case DW_TAG_typedef:
19194 case DW_TAG_union_type:
19195 return 1;
19196 default:
19197 return 0;
19198 }
19199 }
19200
19201 /* Load all DIEs that are interesting for partial symbols into memory. */
19202
19203 static struct partial_die_info *
19204 load_partial_dies (const struct die_reader_specs *reader,
19205 const gdb_byte *info_ptr, int building_psymtab)
19206 {
19207 struct dwarf2_cu *cu = reader->cu;
19208 struct objfile *objfile = cu->per_objfile->objfile;
19209 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
19210 unsigned int bytes_read;
19211 unsigned int load_all = 0;
19212 int nesting_level = 1;
19213
19214 parent_die = NULL;
19215 last_die = NULL;
19216
19217 gdb_assert (cu->per_cu != NULL);
19218 if (cu->per_cu->load_all_dies)
19219 load_all = 1;
19220
19221 cu->partial_dies
19222 = htab_create_alloc_ex (cu->header.length / 12,
19223 partial_die_hash,
19224 partial_die_eq,
19225 NULL,
19226 &cu->comp_unit_obstack,
19227 hashtab_obstack_allocate,
19228 dummy_obstack_deallocate);
19229
19230 while (1)
19231 {
19232 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
19233
19234 /* A NULL abbrev means the end of a series of children. */
19235 if (abbrev == NULL)
19236 {
19237 if (--nesting_level == 0)
19238 return first_die;
19239
19240 info_ptr += bytes_read;
19241 last_die = parent_die;
19242 parent_die = parent_die->die_parent;
19243 continue;
19244 }
19245
19246 /* Check for template arguments. We never save these; if
19247 they're seen, we just mark the parent, and go on our way. */
19248 if (parent_die != NULL
19249 && cu->language == language_cplus
19250 && (abbrev->tag == DW_TAG_template_type_param
19251 || abbrev->tag == DW_TAG_template_value_param))
19252 {
19253 parent_die->has_template_arguments = 1;
19254
19255 if (!load_all)
19256 {
19257 /* We don't need a partial DIE for the template argument. */
19258 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19259 continue;
19260 }
19261 }
19262
19263 /* We only recurse into c++ subprograms looking for template arguments.
19264 Skip their other children. */
19265 if (!load_all
19266 && cu->language == language_cplus
19267 && parent_die != NULL
19268 && parent_die->tag == DW_TAG_subprogram
19269 && abbrev->tag != DW_TAG_inlined_subroutine)
19270 {
19271 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19272 continue;
19273 }
19274
19275 /* Check whether this DIE is interesting enough to save. Normally
19276 we would not be interested in members here, but there may be
19277 later variables referencing them via DW_AT_specification (for
19278 static members). */
19279 if (!load_all
19280 && !is_type_tag_for_partial (abbrev->tag, cu->language)
19281 && abbrev->tag != DW_TAG_constant
19282 && abbrev->tag != DW_TAG_enumerator
19283 && abbrev->tag != DW_TAG_subprogram
19284 && abbrev->tag != DW_TAG_inlined_subroutine
19285 && abbrev->tag != DW_TAG_lexical_block
19286 && abbrev->tag != DW_TAG_variable
19287 && abbrev->tag != DW_TAG_namespace
19288 && abbrev->tag != DW_TAG_module
19289 && abbrev->tag != DW_TAG_member
19290 && abbrev->tag != DW_TAG_imported_unit
19291 && abbrev->tag != DW_TAG_imported_declaration)
19292 {
19293 /* Otherwise we skip to the next sibling, if any. */
19294 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19295 continue;
19296 }
19297
19298 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19299 abbrev);
19300
19301 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19302
19303 /* This two-pass algorithm for processing partial symbols has a
19304 high cost in cache pressure. Thus, handle some simple cases
19305 here which cover the majority of C partial symbols. DIEs
19306 which neither have specification tags in them, nor could have
19307 specification tags elsewhere pointing at them, can simply be
19308 processed and discarded.
19309
19310 This segment is also optional; scan_partial_symbols and
19311 add_partial_symbol will handle these DIEs if we chain
19312 them in normally. When compilers which do not emit large
19313 quantities of duplicate debug information are more common,
19314 this code can probably be removed. */
19315
19316 /* Any complete simple types at the top level (pretty much all
19317 of them, for a language without namespaces), can be processed
19318 directly. */
19319 if (parent_die == NULL
19320 && pdi.has_specification == 0
19321 && pdi.is_declaration == 0
19322 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19323 || pdi.tag == DW_TAG_base_type
19324 || pdi.tag == DW_TAG_array_type
19325 || pdi.tag == DW_TAG_subrange_type))
19326 {
19327 if (building_psymtab && pdi.raw_name != NULL)
19328 add_partial_symbol (&pdi, cu);
19329
19330 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19331 continue;
19332 }
19333
19334 /* The exception for DW_TAG_typedef with has_children above is
19335 a workaround of GCC PR debug/47510. In the case of this complaint
19336 type_name_or_error will error on such types later.
19337
19338 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19339 it could not find the child DIEs referenced later, this is checked
19340 above. In correct DWARF DW_TAG_typedef should have no children. */
19341
19342 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19343 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19344 "- DIE at %s [in module %s]"),
19345 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19346
19347 /* If we're at the second level, and we're an enumerator, and
19348 our parent has no specification (meaning possibly lives in a
19349 namespace elsewhere), then we can add the partial symbol now
19350 instead of queueing it. */
19351 if (pdi.tag == DW_TAG_enumerator
19352 && parent_die != NULL
19353 && parent_die->die_parent == NULL
19354 && parent_die->tag == DW_TAG_enumeration_type
19355 && parent_die->has_specification == 0)
19356 {
19357 if (pdi.raw_name == NULL)
19358 complaint (_("malformed enumerator DIE ignored"));
19359 else if (building_psymtab)
19360 add_partial_symbol (&pdi, cu);
19361
19362 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19363 continue;
19364 }
19365
19366 struct partial_die_info *part_die
19367 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19368
19369 /* We'll save this DIE so link it in. */
19370 part_die->die_parent = parent_die;
19371 part_die->die_sibling = NULL;
19372 part_die->die_child = NULL;
19373
19374 if (last_die && last_die == parent_die)
19375 last_die->die_child = part_die;
19376 else if (last_die)
19377 last_die->die_sibling = part_die;
19378
19379 last_die = part_die;
19380
19381 if (first_die == NULL)
19382 first_die = part_die;
19383
19384 /* Maybe add the DIE to the hash table. Not all DIEs that we
19385 find interesting need to be in the hash table, because we
19386 also have the parent/sibling/child chains; only those that we
19387 might refer to by offset later during partial symbol reading.
19388
19389 For now this means things that might have be the target of a
19390 DW_AT_specification, DW_AT_abstract_origin, or
19391 DW_AT_extension. DW_AT_extension will refer only to
19392 namespaces; DW_AT_abstract_origin refers to functions (and
19393 many things under the function DIE, but we do not recurse
19394 into function DIEs during partial symbol reading) and
19395 possibly variables as well; DW_AT_specification refers to
19396 declarations. Declarations ought to have the DW_AT_declaration
19397 flag. It happens that GCC forgets to put it in sometimes, but
19398 only for functions, not for types.
19399
19400 Adding more things than necessary to the hash table is harmless
19401 except for the performance cost. Adding too few will result in
19402 wasted time in find_partial_die, when we reread the compilation
19403 unit with load_all_dies set. */
19404
19405 if (load_all
19406 || abbrev->tag == DW_TAG_constant
19407 || abbrev->tag == DW_TAG_subprogram
19408 || abbrev->tag == DW_TAG_variable
19409 || abbrev->tag == DW_TAG_namespace
19410 || part_die->is_declaration)
19411 {
19412 void **slot;
19413
19414 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19415 to_underlying (part_die->sect_off),
19416 INSERT);
19417 *slot = part_die;
19418 }
19419
19420 /* For some DIEs we want to follow their children (if any). For C
19421 we have no reason to follow the children of structures; for other
19422 languages we have to, so that we can get at method physnames
19423 to infer fully qualified class names, for DW_AT_specification,
19424 and for C++ template arguments. For C++, we also look one level
19425 inside functions to find template arguments (if the name of the
19426 function does not already contain the template arguments).
19427
19428 For Ada and Fortran, we need to scan the children of subprograms
19429 and lexical blocks as well because these languages allow the
19430 definition of nested entities that could be interesting for the
19431 debugger, such as nested subprograms for instance. */
19432 if (last_die->has_children
19433 && (load_all
19434 || last_die->tag == DW_TAG_namespace
19435 || last_die->tag == DW_TAG_module
19436 || last_die->tag == DW_TAG_enumeration_type
19437 || (cu->language == language_cplus
19438 && last_die->tag == DW_TAG_subprogram
19439 && (last_die->raw_name == NULL
19440 || strchr (last_die->raw_name, '<') == NULL))
19441 || (cu->language != language_c
19442 && (last_die->tag == DW_TAG_class_type
19443 || last_die->tag == DW_TAG_interface_type
19444 || last_die->tag == DW_TAG_structure_type
19445 || last_die->tag == DW_TAG_union_type))
19446 || ((cu->language == language_ada
19447 || cu->language == language_fortran)
19448 && (last_die->tag == DW_TAG_subprogram
19449 || last_die->tag == DW_TAG_lexical_block))))
19450 {
19451 nesting_level++;
19452 parent_die = last_die;
19453 continue;
19454 }
19455
19456 /* Otherwise we skip to the next sibling, if any. */
19457 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19458
19459 /* Back to the top, do it again. */
19460 }
19461 }
19462
19463 partial_die_info::partial_die_info (sect_offset sect_off_,
19464 struct abbrev_info *abbrev)
19465 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19466 {
19467 }
19468
19469 /* See class definition. */
19470
19471 const char *
19472 partial_die_info::name (dwarf2_cu *cu)
19473 {
19474 if (!canonical_name && raw_name != nullptr)
19475 {
19476 struct objfile *objfile = cu->per_objfile->objfile;
19477 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19478 canonical_name = 1;
19479 }
19480
19481 return raw_name;
19482 }
19483
19484 /* Read a minimal amount of information into the minimal die structure.
19485 INFO_PTR should point just after the initial uleb128 of a DIE. */
19486
19487 const gdb_byte *
19488 partial_die_info::read (const struct die_reader_specs *reader,
19489 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19490 {
19491 struct dwarf2_cu *cu = reader->cu;
19492 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19493 unsigned int i;
19494 int has_low_pc_attr = 0;
19495 int has_high_pc_attr = 0;
19496 int high_pc_relative = 0;
19497
19498 for (i = 0; i < abbrev.num_attrs; ++i)
19499 {
19500 attribute attr;
19501 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19502 /* String and address offsets that need to do the reprocessing have
19503 already been read at this point, so there is no need to wait until
19504 the loop terminates to do the reprocessing. */
19505 if (attr.requires_reprocessing_p ())
19506 read_attribute_reprocess (reader, &attr, tag);
19507 /* Store the data if it is of an attribute we want to keep in a
19508 partial symbol table. */
19509 switch (attr.name)
19510 {
19511 case DW_AT_name:
19512 switch (tag)
19513 {
19514 case DW_TAG_compile_unit:
19515 case DW_TAG_partial_unit:
19516 case DW_TAG_type_unit:
19517 /* Compilation units have a DW_AT_name that is a filename, not
19518 a source language identifier. */
19519 case DW_TAG_enumeration_type:
19520 case DW_TAG_enumerator:
19521 /* These tags always have simple identifiers already; no need
19522 to canonicalize them. */
19523 canonical_name = 1;
19524 raw_name = attr.as_string ();
19525 break;
19526 default:
19527 canonical_name = 0;
19528 raw_name = attr.as_string ();
19529 break;
19530 }
19531 break;
19532 case DW_AT_linkage_name:
19533 case DW_AT_MIPS_linkage_name:
19534 /* Note that both forms of linkage name might appear. We
19535 assume they will be the same, and we only store the last
19536 one we see. */
19537 linkage_name = attr.as_string ();
19538 break;
19539 case DW_AT_low_pc:
19540 has_low_pc_attr = 1;
19541 lowpc = attr.as_address ();
19542 break;
19543 case DW_AT_high_pc:
19544 has_high_pc_attr = 1;
19545 highpc = attr.as_address ();
19546 if (cu->header.version >= 4 && attr.form_is_constant ())
19547 high_pc_relative = 1;
19548 break;
19549 case DW_AT_location:
19550 /* Support the .debug_loc offsets. */
19551 if (attr.form_is_block ())
19552 {
19553 d.locdesc = attr.as_block ();
19554 }
19555 else if (attr.form_is_section_offset ())
19556 {
19557 dwarf2_complex_location_expr_complaint ();
19558 }
19559 else
19560 {
19561 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19562 "partial symbol information");
19563 }
19564 break;
19565 case DW_AT_external:
19566 is_external = attr.as_boolean ();
19567 break;
19568 case DW_AT_declaration:
19569 is_declaration = attr.as_boolean ();
19570 break;
19571 case DW_AT_type:
19572 has_type = 1;
19573 break;
19574 case DW_AT_abstract_origin:
19575 case DW_AT_specification:
19576 case DW_AT_extension:
19577 has_specification = 1;
19578 spec_offset = attr.get_ref_die_offset ();
19579 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19580 || cu->per_cu->is_dwz);
19581 break;
19582 case DW_AT_sibling:
19583 /* Ignore absolute siblings, they might point outside of
19584 the current compile unit. */
19585 if (attr.form == DW_FORM_ref_addr)
19586 complaint (_("ignoring absolute DW_AT_sibling"));
19587 else
19588 {
19589 const gdb_byte *buffer = reader->buffer;
19590 sect_offset off = attr.get_ref_die_offset ();
19591 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19592
19593 if (sibling_ptr < info_ptr)
19594 complaint (_("DW_AT_sibling points backwards"));
19595 else if (sibling_ptr > reader->buffer_end)
19596 reader->die_section->overflow_complaint ();
19597 else
19598 sibling = sibling_ptr;
19599 }
19600 break;
19601 case DW_AT_byte_size:
19602 has_byte_size = 1;
19603 break;
19604 case DW_AT_const_value:
19605 has_const_value = 1;
19606 break;
19607 case DW_AT_calling_convention:
19608 /* DWARF doesn't provide a way to identify a program's source-level
19609 entry point. DW_AT_calling_convention attributes are only meant
19610 to describe functions' calling conventions.
19611
19612 However, because it's a necessary piece of information in
19613 Fortran, and before DWARF 4 DW_CC_program was the only
19614 piece of debugging information whose definition refers to
19615 a 'main program' at all, several compilers marked Fortran
19616 main programs with DW_CC_program --- even when those
19617 functions use the standard calling conventions.
19618
19619 Although DWARF now specifies a way to provide this
19620 information, we support this practice for backward
19621 compatibility. */
19622 if (attr.constant_value (0) == DW_CC_program
19623 && cu->language == language_fortran)
19624 main_subprogram = 1;
19625 break;
19626 case DW_AT_inline:
19627 {
19628 LONGEST value = attr.constant_value (-1);
19629 if (value == DW_INL_inlined
19630 || value == DW_INL_declared_inlined)
19631 may_be_inlined = 1;
19632 }
19633 break;
19634
19635 case DW_AT_import:
19636 if (tag == DW_TAG_imported_unit)
19637 {
19638 d.sect_off = attr.get_ref_die_offset ();
19639 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19640 || cu->per_cu->is_dwz);
19641 }
19642 break;
19643
19644 case DW_AT_main_subprogram:
19645 main_subprogram = attr.as_boolean ();
19646 break;
19647
19648 case DW_AT_ranges:
19649 {
19650 /* DW_AT_rnglists_base does not apply to DIEs from the DWO
19651 skeleton. We take advantage of the fact the DW_AT_ranges
19652 does not appear in DW_TAG_compile_unit of DWO files.
19653
19654 Attributes of the form DW_FORM_rnglistx have already had
19655 their value changed by read_rnglist_index and already
19656 include DW_AT_rnglists_base, so don't need to add the ranges
19657 base, either. */
19658 int need_ranges_base = (tag != DW_TAG_compile_unit
19659 && attr.form != DW_FORM_rnglistx);
19660 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19661 but that requires a full DIE, so instead we just
19662 reimplement it. */
19663 unsigned int ranges_offset = (attr.constant_value (0)
19664 + (need_ranges_base
19665 ? cu->ranges_base
19666 : 0));
19667
19668 /* Value of the DW_AT_ranges attribute is the offset in the
19669 .debug_ranges section. */
19670 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19671 nullptr, tag))
19672 has_pc_info = 1;
19673 }
19674 break;
19675
19676 default:
19677 break;
19678 }
19679 }
19680
19681 /* For Ada, if both the name and the linkage name appear, we prefer
19682 the latter. This lets "catch exception" work better, regardless
19683 of the order in which the name and linkage name were emitted.
19684 Really, though, this is just a workaround for the fact that gdb
19685 doesn't store both the name and the linkage name. */
19686 if (cu->language == language_ada && linkage_name != nullptr)
19687 raw_name = linkage_name;
19688
19689 if (high_pc_relative)
19690 highpc += lowpc;
19691
19692 if (has_low_pc_attr && has_high_pc_attr)
19693 {
19694 /* When using the GNU linker, .gnu.linkonce. sections are used to
19695 eliminate duplicate copies of functions and vtables and such.
19696 The linker will arbitrarily choose one and discard the others.
19697 The AT_*_pc values for such functions refer to local labels in
19698 these sections. If the section from that file was discarded, the
19699 labels are not in the output, so the relocs get a value of 0.
19700 If this is a discarded function, mark the pc bounds as invalid,
19701 so that GDB will ignore it. */
19702 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19703 {
19704 struct objfile *objfile = per_objfile->objfile;
19705 struct gdbarch *gdbarch = objfile->arch ();
19706
19707 complaint (_("DW_AT_low_pc %s is zero "
19708 "for DIE at %s [in module %s]"),
19709 paddress (gdbarch, lowpc),
19710 sect_offset_str (sect_off),
19711 objfile_name (objfile));
19712 }
19713 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19714 else if (lowpc >= highpc)
19715 {
19716 struct objfile *objfile = per_objfile->objfile;
19717 struct gdbarch *gdbarch = objfile->arch ();
19718
19719 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19720 "for DIE at %s [in module %s]"),
19721 paddress (gdbarch, lowpc),
19722 paddress (gdbarch, highpc),
19723 sect_offset_str (sect_off),
19724 objfile_name (objfile));
19725 }
19726 else
19727 has_pc_info = 1;
19728 }
19729
19730 return info_ptr;
19731 }
19732
19733 /* Find a cached partial DIE at OFFSET in CU. */
19734
19735 struct partial_die_info *
19736 dwarf2_cu::find_partial_die (sect_offset sect_off)
19737 {
19738 struct partial_die_info *lookup_die = NULL;
19739 struct partial_die_info part_die (sect_off);
19740
19741 lookup_die = ((struct partial_die_info *)
19742 htab_find_with_hash (partial_dies, &part_die,
19743 to_underlying (sect_off)));
19744
19745 return lookup_die;
19746 }
19747
19748 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19749 except in the case of .debug_types DIEs which do not reference
19750 outside their CU (they do however referencing other types via
19751 DW_FORM_ref_sig8). */
19752
19753 static const struct cu_partial_die_info
19754 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19755 {
19756 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19757 struct objfile *objfile = per_objfile->objfile;
19758 struct partial_die_info *pd = NULL;
19759
19760 if (offset_in_dwz == cu->per_cu->is_dwz
19761 && cu->header.offset_in_cu_p (sect_off))
19762 {
19763 pd = cu->find_partial_die (sect_off);
19764 if (pd != NULL)
19765 return { cu, pd };
19766 /* We missed recording what we needed.
19767 Load all dies and try again. */
19768 }
19769 else
19770 {
19771 /* TUs don't reference other CUs/TUs (except via type signatures). */
19772 if (cu->per_cu->is_debug_types)
19773 {
19774 error (_("Dwarf Error: Type Unit at offset %s contains"
19775 " external reference to offset %s [in module %s].\n"),
19776 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19777 bfd_get_filename (objfile->obfd));
19778 }
19779 dwarf2_per_cu_data *per_cu
19780 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19781 per_objfile);
19782
19783 cu = per_objfile->get_cu (per_cu);
19784 if (cu == NULL || cu->partial_dies == NULL)
19785 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19786
19787 cu = per_objfile->get_cu (per_cu);
19788
19789 cu->last_used = 0;
19790 pd = cu->find_partial_die (sect_off);
19791 }
19792
19793 /* If we didn't find it, and not all dies have been loaded,
19794 load them all and try again. */
19795
19796 if (pd == NULL && cu->per_cu->load_all_dies == 0)
19797 {
19798 cu->per_cu->load_all_dies = 1;
19799
19800 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19801 THIS_CU->cu may already be in use. So we can't just free it and
19802 replace its DIEs with the ones we read in. Instead, we leave those
19803 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19804 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19805 set. */
19806 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
19807
19808 pd = cu->find_partial_die (sect_off);
19809 }
19810
19811 if (pd == NULL)
19812 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
19813 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19814 return { cu, pd };
19815 }
19816
19817 /* See if we can figure out if the class lives in a namespace. We do
19818 this by looking for a member function; its demangled name will
19819 contain namespace info, if there is any. */
19820
19821 static void
19822 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19823 struct dwarf2_cu *cu)
19824 {
19825 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19826 what template types look like, because the demangler
19827 frequently doesn't give the same name as the debug info. We
19828 could fix this by only using the demangled name to get the
19829 prefix (but see comment in read_structure_type). */
19830
19831 struct partial_die_info *real_pdi;
19832 struct partial_die_info *child_pdi;
19833
19834 /* If this DIE (this DIE's specification, if any) has a parent, then
19835 we should not do this. We'll prepend the parent's fully qualified
19836 name when we create the partial symbol. */
19837
19838 real_pdi = struct_pdi;
19839 while (real_pdi->has_specification)
19840 {
19841 auto res = find_partial_die (real_pdi->spec_offset,
19842 real_pdi->spec_is_dwz, cu);
19843 real_pdi = res.pdi;
19844 cu = res.cu;
19845 }
19846
19847 if (real_pdi->die_parent != NULL)
19848 return;
19849
19850 for (child_pdi = struct_pdi->die_child;
19851 child_pdi != NULL;
19852 child_pdi = child_pdi->die_sibling)
19853 {
19854 if (child_pdi->tag == DW_TAG_subprogram
19855 && child_pdi->linkage_name != NULL)
19856 {
19857 gdb::unique_xmalloc_ptr<char> actual_class_name
19858 (cu->language_defn->class_name_from_physname
19859 (child_pdi->linkage_name));
19860 if (actual_class_name != NULL)
19861 {
19862 struct objfile *objfile = cu->per_objfile->objfile;
19863 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19864 struct_pdi->canonical_name = 1;
19865 }
19866 break;
19867 }
19868 }
19869 }
19870
19871 /* Return true if a DIE with TAG may have the DW_AT_const_value
19872 attribute. */
19873
19874 static bool
19875 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19876 {
19877 switch (tag)
19878 {
19879 case DW_TAG_constant:
19880 case DW_TAG_enumerator:
19881 case DW_TAG_formal_parameter:
19882 case DW_TAG_template_value_param:
19883 case DW_TAG_variable:
19884 return true;
19885 }
19886
19887 return false;
19888 }
19889
19890 void
19891 partial_die_info::fixup (struct dwarf2_cu *cu)
19892 {
19893 /* Once we've fixed up a die, there's no point in doing so again.
19894 This also avoids a memory leak if we were to call
19895 guess_partial_die_structure_name multiple times. */
19896 if (fixup_called)
19897 return;
19898
19899 /* If we found a reference attribute and the DIE has no name, try
19900 to find a name in the referred to DIE. */
19901
19902 if (raw_name == NULL && has_specification)
19903 {
19904 struct partial_die_info *spec_die;
19905
19906 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19907 spec_die = res.pdi;
19908 cu = res.cu;
19909
19910 spec_die->fixup (cu);
19911
19912 if (spec_die->raw_name)
19913 {
19914 raw_name = spec_die->raw_name;
19915 canonical_name = spec_die->canonical_name;
19916
19917 /* Copy DW_AT_external attribute if it is set. */
19918 if (spec_die->is_external)
19919 is_external = spec_die->is_external;
19920 }
19921 }
19922
19923 if (!has_const_value && has_specification
19924 && can_have_DW_AT_const_value_p (tag))
19925 {
19926 struct partial_die_info *spec_die;
19927
19928 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19929 spec_die = res.pdi;
19930 cu = res.cu;
19931
19932 spec_die->fixup (cu);
19933
19934 if (spec_die->has_const_value)
19935 {
19936 /* Copy DW_AT_const_value attribute if it is set. */
19937 has_const_value = spec_die->has_const_value;
19938 }
19939 }
19940
19941 /* Set default names for some unnamed DIEs. */
19942
19943 if (raw_name == NULL && tag == DW_TAG_namespace)
19944 {
19945 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19946 canonical_name = 1;
19947 }
19948
19949 /* If there is no parent die to provide a namespace, and there are
19950 children, see if we can determine the namespace from their linkage
19951 name. */
19952 if (cu->language == language_cplus
19953 && !cu->per_objfile->per_bfd->types.empty ()
19954 && die_parent == NULL
19955 && has_children
19956 && (tag == DW_TAG_class_type
19957 || tag == DW_TAG_structure_type
19958 || tag == DW_TAG_union_type))
19959 guess_partial_die_structure_name (this, cu);
19960
19961 /* GCC might emit a nameless struct or union that has a linkage
19962 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19963 if (raw_name == NULL
19964 && (tag == DW_TAG_class_type
19965 || tag == DW_TAG_interface_type
19966 || tag == DW_TAG_structure_type
19967 || tag == DW_TAG_union_type)
19968 && linkage_name != NULL)
19969 {
19970 gdb::unique_xmalloc_ptr<char> demangled
19971 (gdb_demangle (linkage_name, DMGL_TYPES));
19972 if (demangled != nullptr)
19973 {
19974 const char *base;
19975
19976 /* Strip any leading namespaces/classes, keep only the base name.
19977 DW_AT_name for named DIEs does not contain the prefixes. */
19978 base = strrchr (demangled.get (), ':');
19979 if (base && base > demangled.get () && base[-1] == ':')
19980 base++;
19981 else
19982 base = demangled.get ();
19983
19984 struct objfile *objfile = cu->per_objfile->objfile;
19985 raw_name = objfile->intern (base);
19986 canonical_name = 1;
19987 }
19988 }
19989
19990 fixup_called = 1;
19991 }
19992
19993 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
19994 contents from the given SECTION in the HEADER. */
19995 static void
19996 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
19997 struct dwarf2_section_info *section)
19998 {
19999 unsigned int bytes_read;
20000 bfd *abfd = section->get_bfd_owner ();
20001 const gdb_byte *info_ptr = section->buffer;
20002 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20003 info_ptr += bytes_read;
20004 header->version = read_2_bytes (abfd, info_ptr);
20005 info_ptr += 2;
20006 header->addr_size = read_1_byte (abfd, info_ptr);
20007 info_ptr += 1;
20008 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20009 info_ptr += 1;
20010 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20011 }
20012
20013 /* Return the DW_AT_loclists_base value for the CU. */
20014 static ULONGEST
20015 lookup_loclist_base (struct dwarf2_cu *cu)
20016 {
20017 /* For the .dwo unit, the loclist_base points to the first offset following
20018 the header. The header consists of the following entities-
20019 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20020 bit format)
20021 2. version (2 bytes)
20022 3. address size (1 byte)
20023 4. segment selector size (1 byte)
20024 5. offset entry count (4 bytes)
20025 These sizes are derived as per the DWARFv5 standard. */
20026 if (cu->dwo_unit != nullptr)
20027 {
20028 if (cu->header.initial_length_size == 4)
20029 return LOCLIST_HEADER_SIZE32;
20030 return LOCLIST_HEADER_SIZE64;
20031 }
20032 return cu->loclist_base;
20033 }
20034
20035 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20036 array of offsets in the .debug_loclists section. */
20037 static CORE_ADDR
20038 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20039 {
20040 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20041 struct objfile *objfile = per_objfile->objfile;
20042 bfd *abfd = objfile->obfd;
20043 ULONGEST loclist_base = lookup_loclist_base (cu);
20044 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20045
20046 section->read (objfile);
20047 if (section->buffer == NULL)
20048 complaint (_("DW_FORM_loclistx used without .debug_loclists "
20049 "section [in module %s]"), objfile_name (objfile));
20050 struct loclists_rnglists_header header;
20051 read_loclists_rnglists_header (&header, section);
20052 if (loclist_index >= header.offset_entry_count)
20053 complaint (_("DW_FORM_loclistx pointing outside of "
20054 ".debug_loclists offset array [in module %s]"),
20055 objfile_name (objfile));
20056 if (loclist_base + loclist_index * cu->header.offset_size
20057 >= section->size)
20058 complaint (_("DW_FORM_loclistx pointing outside of "
20059 ".debug_loclists section [in module %s]"),
20060 objfile_name (objfile));
20061 const gdb_byte *info_ptr
20062 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
20063
20064 if (cu->header.offset_size == 4)
20065 return bfd_get_32 (abfd, info_ptr) + loclist_base;
20066 else
20067 return bfd_get_64 (abfd, info_ptr) + loclist_base;
20068 }
20069
20070 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20071 array of offsets in the .debug_rnglists section. */
20072 static CORE_ADDR
20073 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20074 dwarf_tag tag)
20075 {
20076 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20077 struct objfile *objfile = dwarf2_per_objfile->objfile;
20078 bfd *abfd = objfile->obfd;
20079 ULONGEST rnglist_header_size =
20080 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20081 : RNGLIST_HEADER_SIZE64);
20082 ULONGEST rnglist_base =
20083 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->ranges_base;
20084 ULONGEST start_offset =
20085 rnglist_base + rnglist_index * cu->header.offset_size;
20086
20087 /* Get rnglists section. */
20088 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20089
20090 /* Read the rnglists section content. */
20091 section->read (objfile);
20092 if (section->buffer == nullptr)
20093 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20094 "[in module %s]"),
20095 objfile_name (objfile));
20096
20097 /* Verify the rnglist index is valid. */
20098 struct loclists_rnglists_header header;
20099 read_loclists_rnglists_header (&header, section);
20100 if (rnglist_index >= header.offset_entry_count)
20101 error (_("DW_FORM_rnglistx index pointing outside of "
20102 ".debug_rnglists offset array [in module %s]"),
20103 objfile_name (objfile));
20104
20105 /* Validate that the offset is within the section's range. */
20106 if (start_offset >= section->size)
20107 error (_("DW_FORM_rnglistx pointing outside of "
20108 ".debug_rnglists section [in module %s]"),
20109 objfile_name (objfile));
20110
20111 /* Validate that reading won't go beyond the end of the section. */
20112 if (start_offset + cu->header.offset_size > rnglist_base + section->size)
20113 error (_("Reading DW_FORM_rnglistx index beyond end of"
20114 ".debug_rnglists section [in module %s]"),
20115 objfile_name (objfile));
20116
20117 const gdb_byte *info_ptr = section->buffer + start_offset;
20118
20119 if (cu->header.offset_size == 4)
20120 return read_4_bytes (abfd, info_ptr) + rnglist_base;
20121 else
20122 return read_8_bytes (abfd, info_ptr) + rnglist_base;
20123 }
20124
20125 /* Process the attributes that had to be skipped in the first round. These
20126 attributes are the ones that need str_offsets_base or addr_base attributes.
20127 They could not have been processed in the first round, because at the time
20128 the values of str_offsets_base or addr_base may not have been known. */
20129 static void
20130 read_attribute_reprocess (const struct die_reader_specs *reader,
20131 struct attribute *attr, dwarf_tag tag)
20132 {
20133 struct dwarf2_cu *cu = reader->cu;
20134 switch (attr->form)
20135 {
20136 case DW_FORM_addrx:
20137 case DW_FORM_GNU_addr_index:
20138 attr->set_address (read_addr_index (cu,
20139 attr->as_unsigned_reprocess ()));
20140 break;
20141 case DW_FORM_loclistx:
20142 attr->set_address (read_loclist_index (cu, attr->as_unsigned ()));
20143 break;
20144 case DW_FORM_rnglistx:
20145 attr->set_address (read_rnglist_index (cu, attr->as_unsigned (), tag));
20146 break;
20147 case DW_FORM_strx:
20148 case DW_FORM_strx1:
20149 case DW_FORM_strx2:
20150 case DW_FORM_strx3:
20151 case DW_FORM_strx4:
20152 case DW_FORM_GNU_str_index:
20153 {
20154 unsigned int str_index = attr->as_unsigned_reprocess ();
20155 gdb_assert (!attr->canonical_string_p ());
20156 if (reader->dwo_file != NULL)
20157 attr->set_string_noncanonical (read_dwo_str_index (reader,
20158 str_index));
20159 else
20160 attr->set_string_noncanonical (read_stub_str_index (cu,
20161 str_index));
20162 break;
20163 }
20164 default:
20165 gdb_assert_not_reached (_("Unexpected DWARF form."));
20166 }
20167 }
20168
20169 /* Read an attribute value described by an attribute form. */
20170
20171 static const gdb_byte *
20172 read_attribute_value (const struct die_reader_specs *reader,
20173 struct attribute *attr, unsigned form,
20174 LONGEST implicit_const, const gdb_byte *info_ptr)
20175 {
20176 struct dwarf2_cu *cu = reader->cu;
20177 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20178 struct objfile *objfile = per_objfile->objfile;
20179 bfd *abfd = reader->abfd;
20180 struct comp_unit_head *cu_header = &cu->header;
20181 unsigned int bytes_read;
20182 struct dwarf_block *blk;
20183
20184 attr->form = (enum dwarf_form) form;
20185 switch (form)
20186 {
20187 case DW_FORM_ref_addr:
20188 if (cu->header.version == 2)
20189 attr->set_unsigned (cu->header.read_address (abfd, info_ptr,
20190 &bytes_read));
20191 else
20192 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20193 &bytes_read));
20194 info_ptr += bytes_read;
20195 break;
20196 case DW_FORM_GNU_ref_alt:
20197 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20198 &bytes_read));
20199 info_ptr += bytes_read;
20200 break;
20201 case DW_FORM_addr:
20202 {
20203 struct gdbarch *gdbarch = objfile->arch ();
20204 CORE_ADDR addr = cu->header.read_address (abfd, info_ptr, &bytes_read);
20205 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20206 attr->set_address (addr);
20207 info_ptr += bytes_read;
20208 }
20209 break;
20210 case DW_FORM_block2:
20211 blk = dwarf_alloc_block (cu);
20212 blk->size = read_2_bytes (abfd, info_ptr);
20213 info_ptr += 2;
20214 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20215 info_ptr += blk->size;
20216 attr->set_block (blk);
20217 break;
20218 case DW_FORM_block4:
20219 blk = dwarf_alloc_block (cu);
20220 blk->size = read_4_bytes (abfd, info_ptr);
20221 info_ptr += 4;
20222 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20223 info_ptr += blk->size;
20224 attr->set_block (blk);
20225 break;
20226 case DW_FORM_data2:
20227 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20228 info_ptr += 2;
20229 break;
20230 case DW_FORM_data4:
20231 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20232 info_ptr += 4;
20233 break;
20234 case DW_FORM_data8:
20235 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20236 info_ptr += 8;
20237 break;
20238 case DW_FORM_data16:
20239 blk = dwarf_alloc_block (cu);
20240 blk->size = 16;
20241 blk->data = read_n_bytes (abfd, info_ptr, 16);
20242 info_ptr += 16;
20243 attr->set_block (blk);
20244 break;
20245 case DW_FORM_sec_offset:
20246 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20247 &bytes_read));
20248 info_ptr += bytes_read;
20249 break;
20250 case DW_FORM_loclistx:
20251 {
20252 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20253 &bytes_read));
20254 info_ptr += bytes_read;
20255 }
20256 break;
20257 case DW_FORM_string:
20258 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20259 &bytes_read));
20260 info_ptr += bytes_read;
20261 break;
20262 case DW_FORM_strp:
20263 if (!cu->per_cu->is_dwz)
20264 {
20265 attr->set_string_noncanonical
20266 (read_indirect_string (per_objfile,
20267 abfd, info_ptr, cu_header,
20268 &bytes_read));
20269 info_ptr += bytes_read;
20270 break;
20271 }
20272 /* FALLTHROUGH */
20273 case DW_FORM_line_strp:
20274 if (!cu->per_cu->is_dwz)
20275 {
20276 attr->set_string_noncanonical
20277 (per_objfile->read_line_string (info_ptr, cu_header,
20278 &bytes_read));
20279 info_ptr += bytes_read;
20280 break;
20281 }
20282 /* FALLTHROUGH */
20283 case DW_FORM_GNU_strp_alt:
20284 {
20285 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
20286 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20287 &bytes_read);
20288
20289 attr->set_string_noncanonical
20290 (dwz->read_string (objfile, str_offset));
20291 info_ptr += bytes_read;
20292 }
20293 break;
20294 case DW_FORM_exprloc:
20295 case DW_FORM_block:
20296 blk = dwarf_alloc_block (cu);
20297 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20298 info_ptr += bytes_read;
20299 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20300 info_ptr += blk->size;
20301 attr->set_block (blk);
20302 break;
20303 case DW_FORM_block1:
20304 blk = dwarf_alloc_block (cu);
20305 blk->size = read_1_byte (abfd, info_ptr);
20306 info_ptr += 1;
20307 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20308 info_ptr += blk->size;
20309 attr->set_block (blk);
20310 break;
20311 case DW_FORM_data1:
20312 case DW_FORM_flag:
20313 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20314 info_ptr += 1;
20315 break;
20316 case DW_FORM_flag_present:
20317 attr->set_unsigned (1);
20318 break;
20319 case DW_FORM_sdata:
20320 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20321 info_ptr += bytes_read;
20322 break;
20323 case DW_FORM_rnglistx:
20324 {
20325 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20326 &bytes_read));
20327 info_ptr += bytes_read;
20328 }
20329 break;
20330 case DW_FORM_udata:
20331 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20332 info_ptr += bytes_read;
20333 break;
20334 case DW_FORM_ref1:
20335 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20336 + read_1_byte (abfd, info_ptr)));
20337 info_ptr += 1;
20338 break;
20339 case DW_FORM_ref2:
20340 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20341 + read_2_bytes (abfd, info_ptr)));
20342 info_ptr += 2;
20343 break;
20344 case DW_FORM_ref4:
20345 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20346 + read_4_bytes (abfd, info_ptr)));
20347 info_ptr += 4;
20348 break;
20349 case DW_FORM_ref8:
20350 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20351 + read_8_bytes (abfd, info_ptr)));
20352 info_ptr += 8;
20353 break;
20354 case DW_FORM_ref_sig8:
20355 attr->set_signature (read_8_bytes (abfd, info_ptr));
20356 info_ptr += 8;
20357 break;
20358 case DW_FORM_ref_udata:
20359 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20360 + read_unsigned_leb128 (abfd, info_ptr,
20361 &bytes_read)));
20362 info_ptr += bytes_read;
20363 break;
20364 case DW_FORM_indirect:
20365 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20366 info_ptr += bytes_read;
20367 if (form == DW_FORM_implicit_const)
20368 {
20369 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20370 info_ptr += bytes_read;
20371 }
20372 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20373 info_ptr);
20374 break;
20375 case DW_FORM_implicit_const:
20376 attr->set_signed (implicit_const);
20377 break;
20378 case DW_FORM_addrx:
20379 case DW_FORM_GNU_addr_index:
20380 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20381 &bytes_read));
20382 info_ptr += bytes_read;
20383 break;
20384 case DW_FORM_strx:
20385 case DW_FORM_strx1:
20386 case DW_FORM_strx2:
20387 case DW_FORM_strx3:
20388 case DW_FORM_strx4:
20389 case DW_FORM_GNU_str_index:
20390 {
20391 ULONGEST str_index;
20392 if (form == DW_FORM_strx1)
20393 {
20394 str_index = read_1_byte (abfd, info_ptr);
20395 info_ptr += 1;
20396 }
20397 else if (form == DW_FORM_strx2)
20398 {
20399 str_index = read_2_bytes (abfd, info_ptr);
20400 info_ptr += 2;
20401 }
20402 else if (form == DW_FORM_strx3)
20403 {
20404 str_index = read_3_bytes (abfd, info_ptr);
20405 info_ptr += 3;
20406 }
20407 else if (form == DW_FORM_strx4)
20408 {
20409 str_index = read_4_bytes (abfd, info_ptr);
20410 info_ptr += 4;
20411 }
20412 else
20413 {
20414 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20415 info_ptr += bytes_read;
20416 }
20417 attr->set_unsigned_reprocess (str_index);
20418 }
20419 break;
20420 default:
20421 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20422 dwarf_form_name (form),
20423 bfd_get_filename (abfd));
20424 }
20425
20426 /* Super hack. */
20427 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20428 attr->form = DW_FORM_GNU_ref_alt;
20429
20430 /* We have seen instances where the compiler tried to emit a byte
20431 size attribute of -1 which ended up being encoded as an unsigned
20432 0xffffffff. Although 0xffffffff is technically a valid size value,
20433 an object of this size seems pretty unlikely so we can relatively
20434 safely treat these cases as if the size attribute was invalid and
20435 treat them as zero by default. */
20436 if (attr->name == DW_AT_byte_size
20437 && form == DW_FORM_data4
20438 && attr->as_unsigned () >= 0xffffffff)
20439 {
20440 complaint
20441 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20442 hex_string (attr->as_unsigned ()));
20443 attr->set_unsigned (0);
20444 }
20445
20446 return info_ptr;
20447 }
20448
20449 /* Read an attribute described by an abbreviated attribute. */
20450
20451 static const gdb_byte *
20452 read_attribute (const struct die_reader_specs *reader,
20453 struct attribute *attr, struct attr_abbrev *abbrev,
20454 const gdb_byte *info_ptr)
20455 {
20456 attr->name = abbrev->name;
20457 attr->string_is_canonical = 0;
20458 attr->requires_reprocessing = 0;
20459 return read_attribute_value (reader, attr, abbrev->form,
20460 abbrev->implicit_const, info_ptr);
20461 }
20462
20463 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20464
20465 static const char *
20466 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20467 LONGEST str_offset)
20468 {
20469 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20470 str_offset, "DW_FORM_strp");
20471 }
20472
20473 /* Return pointer to string at .debug_str offset as read from BUF.
20474 BUF is assumed to be in a compilation unit described by CU_HEADER.
20475 Return *BYTES_READ_PTR count of bytes read from BUF. */
20476
20477 static const char *
20478 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20479 const gdb_byte *buf,
20480 const struct comp_unit_head *cu_header,
20481 unsigned int *bytes_read_ptr)
20482 {
20483 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20484
20485 return read_indirect_string_at_offset (per_objfile, str_offset);
20486 }
20487
20488 /* See read.h. */
20489
20490 const char *
20491 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20492 const struct comp_unit_head *cu_header,
20493 unsigned int *bytes_read_ptr)
20494 {
20495 bfd *abfd = objfile->obfd;
20496 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20497
20498 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20499 }
20500
20501 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20502 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20503 ADDR_SIZE is the size of addresses from the CU header. */
20504
20505 static CORE_ADDR
20506 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20507 gdb::optional<ULONGEST> addr_base, int addr_size)
20508 {
20509 struct objfile *objfile = per_objfile->objfile;
20510 bfd *abfd = objfile->obfd;
20511 const gdb_byte *info_ptr;
20512 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20513
20514 per_objfile->per_bfd->addr.read (objfile);
20515 if (per_objfile->per_bfd->addr.buffer == NULL)
20516 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20517 objfile_name (objfile));
20518 if (addr_base_or_zero + addr_index * addr_size
20519 >= per_objfile->per_bfd->addr.size)
20520 error (_("DW_FORM_addr_index pointing outside of "
20521 ".debug_addr section [in module %s]"),
20522 objfile_name (objfile));
20523 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20524 + addr_index * addr_size);
20525 if (addr_size == 4)
20526 return bfd_get_32 (abfd, info_ptr);
20527 else
20528 return bfd_get_64 (abfd, info_ptr);
20529 }
20530
20531 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20532
20533 static CORE_ADDR
20534 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20535 {
20536 return read_addr_index_1 (cu->per_objfile, addr_index,
20537 cu->addr_base, cu->header.addr_size);
20538 }
20539
20540 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20541
20542 static CORE_ADDR
20543 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20544 unsigned int *bytes_read)
20545 {
20546 bfd *abfd = cu->per_objfile->objfile->obfd;
20547 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20548
20549 return read_addr_index (cu, addr_index);
20550 }
20551
20552 /* See read.h. */
20553
20554 CORE_ADDR
20555 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20556 dwarf2_per_objfile *per_objfile,
20557 unsigned int addr_index)
20558 {
20559 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20560 gdb::optional<ULONGEST> addr_base;
20561 int addr_size;
20562
20563 /* We need addr_base and addr_size.
20564 If we don't have PER_CU->cu, we have to get it.
20565 Nasty, but the alternative is storing the needed info in PER_CU,
20566 which at this point doesn't seem justified: it's not clear how frequently
20567 it would get used and it would increase the size of every PER_CU.
20568 Entry points like dwarf2_per_cu_addr_size do a similar thing
20569 so we're not in uncharted territory here.
20570 Alas we need to be a bit more complicated as addr_base is contained
20571 in the DIE.
20572
20573 We don't need to read the entire CU(/TU).
20574 We just need the header and top level die.
20575
20576 IWBN to use the aging mechanism to let us lazily later discard the CU.
20577 For now we skip this optimization. */
20578
20579 if (cu != NULL)
20580 {
20581 addr_base = cu->addr_base;
20582 addr_size = cu->header.addr_size;
20583 }
20584 else
20585 {
20586 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20587 addr_base = reader.cu->addr_base;
20588 addr_size = reader.cu->header.addr_size;
20589 }
20590
20591 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20592 }
20593
20594 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20595 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20596 DWO file. */
20597
20598 static const char *
20599 read_str_index (struct dwarf2_cu *cu,
20600 struct dwarf2_section_info *str_section,
20601 struct dwarf2_section_info *str_offsets_section,
20602 ULONGEST str_offsets_base, ULONGEST str_index)
20603 {
20604 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20605 struct objfile *objfile = per_objfile->objfile;
20606 const char *objf_name = objfile_name (objfile);
20607 bfd *abfd = objfile->obfd;
20608 const gdb_byte *info_ptr;
20609 ULONGEST str_offset;
20610 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20611
20612 str_section->read (objfile);
20613 str_offsets_section->read (objfile);
20614 if (str_section->buffer == NULL)
20615 error (_("%s used without %s section"
20616 " in CU at offset %s [in module %s]"),
20617 form_name, str_section->get_name (),
20618 sect_offset_str (cu->header.sect_off), objf_name);
20619 if (str_offsets_section->buffer == NULL)
20620 error (_("%s used without %s section"
20621 " in CU at offset %s [in module %s]"),
20622 form_name, str_section->get_name (),
20623 sect_offset_str (cu->header.sect_off), objf_name);
20624 info_ptr = (str_offsets_section->buffer
20625 + str_offsets_base
20626 + str_index * cu->header.offset_size);
20627 if (cu->header.offset_size == 4)
20628 str_offset = bfd_get_32 (abfd, info_ptr);
20629 else
20630 str_offset = bfd_get_64 (abfd, info_ptr);
20631 if (str_offset >= str_section->size)
20632 error (_("Offset from %s pointing outside of"
20633 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20634 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20635 return (const char *) (str_section->buffer + str_offset);
20636 }
20637
20638 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20639
20640 static const char *
20641 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20642 {
20643 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20644 ? reader->cu->header.addr_size : 0;
20645 return read_str_index (reader->cu,
20646 &reader->dwo_file->sections.str,
20647 &reader->dwo_file->sections.str_offsets,
20648 str_offsets_base, str_index);
20649 }
20650
20651 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20652
20653 static const char *
20654 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20655 {
20656 struct objfile *objfile = cu->per_objfile->objfile;
20657 const char *objf_name = objfile_name (objfile);
20658 static const char form_name[] = "DW_FORM_GNU_str_index";
20659 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20660
20661 if (!cu->str_offsets_base.has_value ())
20662 error (_("%s used in Fission stub without %s"
20663 " in CU at offset 0x%lx [in module %s]"),
20664 form_name, str_offsets_attr_name,
20665 (long) cu->header.offset_size, objf_name);
20666
20667 return read_str_index (cu,
20668 &cu->per_objfile->per_bfd->str,
20669 &cu->per_objfile->per_bfd->str_offsets,
20670 *cu->str_offsets_base, str_index);
20671 }
20672
20673 /* Return the length of an LEB128 number in BUF. */
20674
20675 static int
20676 leb128_size (const gdb_byte *buf)
20677 {
20678 const gdb_byte *begin = buf;
20679 gdb_byte byte;
20680
20681 while (1)
20682 {
20683 byte = *buf++;
20684 if ((byte & 128) == 0)
20685 return buf - begin;
20686 }
20687 }
20688
20689 static void
20690 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20691 {
20692 switch (lang)
20693 {
20694 case DW_LANG_C89:
20695 case DW_LANG_C99:
20696 case DW_LANG_C11:
20697 case DW_LANG_C:
20698 case DW_LANG_UPC:
20699 cu->language = language_c;
20700 break;
20701 case DW_LANG_Java:
20702 case DW_LANG_C_plus_plus:
20703 case DW_LANG_C_plus_plus_11:
20704 case DW_LANG_C_plus_plus_14:
20705 cu->language = language_cplus;
20706 break;
20707 case DW_LANG_D:
20708 cu->language = language_d;
20709 break;
20710 case DW_LANG_Fortran77:
20711 case DW_LANG_Fortran90:
20712 case DW_LANG_Fortran95:
20713 case DW_LANG_Fortran03:
20714 case DW_LANG_Fortran08:
20715 cu->language = language_fortran;
20716 break;
20717 case DW_LANG_Go:
20718 cu->language = language_go;
20719 break;
20720 case DW_LANG_Mips_Assembler:
20721 cu->language = language_asm;
20722 break;
20723 case DW_LANG_Ada83:
20724 case DW_LANG_Ada95:
20725 cu->language = language_ada;
20726 break;
20727 case DW_LANG_Modula2:
20728 cu->language = language_m2;
20729 break;
20730 case DW_LANG_Pascal83:
20731 cu->language = language_pascal;
20732 break;
20733 case DW_LANG_ObjC:
20734 cu->language = language_objc;
20735 break;
20736 case DW_LANG_Rust:
20737 case DW_LANG_Rust_old:
20738 cu->language = language_rust;
20739 break;
20740 case DW_LANG_Cobol74:
20741 case DW_LANG_Cobol85:
20742 default:
20743 cu->language = language_minimal;
20744 break;
20745 }
20746 cu->language_defn = language_def (cu->language);
20747 }
20748
20749 /* Return the named attribute or NULL if not there. */
20750
20751 static struct attribute *
20752 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20753 {
20754 for (;;)
20755 {
20756 unsigned int i;
20757 struct attribute *spec = NULL;
20758
20759 for (i = 0; i < die->num_attrs; ++i)
20760 {
20761 if (die->attrs[i].name == name)
20762 return &die->attrs[i];
20763 if (die->attrs[i].name == DW_AT_specification
20764 || die->attrs[i].name == DW_AT_abstract_origin)
20765 spec = &die->attrs[i];
20766 }
20767
20768 if (!spec)
20769 break;
20770
20771 die = follow_die_ref (die, spec, &cu);
20772 }
20773
20774 return NULL;
20775 }
20776
20777 /* Return the string associated with a string-typed attribute, or NULL if it
20778 is either not found or is of an incorrect type. */
20779
20780 static const char *
20781 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20782 {
20783 struct attribute *attr;
20784 const char *str = NULL;
20785
20786 attr = dwarf2_attr (die, name, cu);
20787
20788 if (attr != NULL)
20789 {
20790 str = attr->as_string ();
20791 if (str == nullptr)
20792 complaint (_("string type expected for attribute %s for "
20793 "DIE at %s in module %s"),
20794 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20795 objfile_name (cu->per_objfile->objfile));
20796 }
20797
20798 return str;
20799 }
20800
20801 /* Return the dwo name or NULL if not present. If present, it is in either
20802 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20803 static const char *
20804 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20805 {
20806 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20807 if (dwo_name == nullptr)
20808 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20809 return dwo_name;
20810 }
20811
20812 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20813 and holds a non-zero value. This function should only be used for
20814 DW_FORM_flag or DW_FORM_flag_present attributes. */
20815
20816 static int
20817 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20818 {
20819 struct attribute *attr = dwarf2_attr (die, name, cu);
20820
20821 return attr != nullptr && attr->as_boolean ();
20822 }
20823
20824 static int
20825 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20826 {
20827 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20828 which value is non-zero. However, we have to be careful with
20829 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20830 (via dwarf2_flag_true_p) follows this attribute. So we may
20831 end up accidently finding a declaration attribute that belongs
20832 to a different DIE referenced by the specification attribute,
20833 even though the given DIE does not have a declaration attribute. */
20834 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20835 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20836 }
20837
20838 /* Return the die giving the specification for DIE, if there is
20839 one. *SPEC_CU is the CU containing DIE on input, and the CU
20840 containing the return value on output. If there is no
20841 specification, but there is an abstract origin, that is
20842 returned. */
20843
20844 static struct die_info *
20845 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20846 {
20847 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20848 *spec_cu);
20849
20850 if (spec_attr == NULL)
20851 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20852
20853 if (spec_attr == NULL)
20854 return NULL;
20855 else
20856 return follow_die_ref (die, spec_attr, spec_cu);
20857 }
20858
20859 /* Stub for free_line_header to match void * callback types. */
20860
20861 static void
20862 free_line_header_voidp (void *arg)
20863 {
20864 struct line_header *lh = (struct line_header *) arg;
20865
20866 delete lh;
20867 }
20868
20869 /* A convenience function to find the proper .debug_line section for a CU. */
20870
20871 static struct dwarf2_section_info *
20872 get_debug_line_section (struct dwarf2_cu *cu)
20873 {
20874 struct dwarf2_section_info *section;
20875 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20876
20877 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20878 DWO file. */
20879 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20880 section = &cu->dwo_unit->dwo_file->sections.line;
20881 else if (cu->per_cu->is_dwz)
20882 {
20883 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
20884
20885 section = &dwz->line;
20886 }
20887 else
20888 section = &per_objfile->per_bfd->line;
20889
20890 return section;
20891 }
20892
20893 /* Read the statement program header starting at OFFSET in
20894 .debug_line, or .debug_line.dwo. Return a pointer
20895 to a struct line_header, allocated using xmalloc.
20896 Returns NULL if there is a problem reading the header, e.g., if it
20897 has a version we don't understand.
20898
20899 NOTE: the strings in the include directory and file name tables of
20900 the returned object point into the dwarf line section buffer,
20901 and must not be freed. */
20902
20903 static line_header_up
20904 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20905 {
20906 struct dwarf2_section_info *section;
20907 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20908
20909 section = get_debug_line_section (cu);
20910 section->read (per_objfile->objfile);
20911 if (section->buffer == NULL)
20912 {
20913 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20914 complaint (_("missing .debug_line.dwo section"));
20915 else
20916 complaint (_("missing .debug_line section"));
20917 return 0;
20918 }
20919
20920 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
20921 per_objfile, section, &cu->header);
20922 }
20923
20924 /* Subroutine of dwarf_decode_lines to simplify it.
20925 Return the file name of the psymtab for the given file_entry.
20926 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20927 If space for the result is malloc'd, *NAME_HOLDER will be set.
20928 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20929
20930 static const char *
20931 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20932 const dwarf2_psymtab *pst,
20933 const char *comp_dir,
20934 gdb::unique_xmalloc_ptr<char> *name_holder)
20935 {
20936 const char *include_name = fe.name;
20937 const char *include_name_to_compare = include_name;
20938 const char *pst_filename;
20939 int file_is_pst;
20940
20941 const char *dir_name = fe.include_dir (lh);
20942
20943 gdb::unique_xmalloc_ptr<char> hold_compare;
20944 if (!IS_ABSOLUTE_PATH (include_name)
20945 && (dir_name != NULL || comp_dir != NULL))
20946 {
20947 /* Avoid creating a duplicate psymtab for PST.
20948 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20949 Before we do the comparison, however, we need to account
20950 for DIR_NAME and COMP_DIR.
20951 First prepend dir_name (if non-NULL). If we still don't
20952 have an absolute path prepend comp_dir (if non-NULL).
20953 However, the directory we record in the include-file's
20954 psymtab does not contain COMP_DIR (to match the
20955 corresponding symtab(s)).
20956
20957 Example:
20958
20959 bash$ cd /tmp
20960 bash$ gcc -g ./hello.c
20961 include_name = "hello.c"
20962 dir_name = "."
20963 DW_AT_comp_dir = comp_dir = "/tmp"
20964 DW_AT_name = "./hello.c"
20965
20966 */
20967
20968 if (dir_name != NULL)
20969 {
20970 name_holder->reset (concat (dir_name, SLASH_STRING,
20971 include_name, (char *) NULL));
20972 include_name = name_holder->get ();
20973 include_name_to_compare = include_name;
20974 }
20975 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20976 {
20977 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20978 include_name, (char *) NULL));
20979 include_name_to_compare = hold_compare.get ();
20980 }
20981 }
20982
20983 pst_filename = pst->filename;
20984 gdb::unique_xmalloc_ptr<char> copied_name;
20985 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20986 {
20987 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20988 pst_filename, (char *) NULL));
20989 pst_filename = copied_name.get ();
20990 }
20991
20992 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20993
20994 if (file_is_pst)
20995 return NULL;
20996 return include_name;
20997 }
20998
20999 /* State machine to track the state of the line number program. */
21000
21001 class lnp_state_machine
21002 {
21003 public:
21004 /* Initialize a machine state for the start of a line number
21005 program. */
21006 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21007 bool record_lines_p);
21008
21009 file_entry *current_file ()
21010 {
21011 /* lh->file_names is 0-based, but the file name numbers in the
21012 statement program are 1-based. */
21013 return m_line_header->file_name_at (m_file);
21014 }
21015
21016 /* Record the line in the state machine. END_SEQUENCE is true if
21017 we're processing the end of a sequence. */
21018 void record_line (bool end_sequence);
21019
21020 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
21021 nop-out rest of the lines in this sequence. */
21022 void check_line_address (struct dwarf2_cu *cu,
21023 const gdb_byte *line_ptr,
21024 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
21025
21026 void handle_set_discriminator (unsigned int discriminator)
21027 {
21028 m_discriminator = discriminator;
21029 m_line_has_non_zero_discriminator |= discriminator != 0;
21030 }
21031
21032 /* Handle DW_LNE_set_address. */
21033 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21034 {
21035 m_op_index = 0;
21036 address += baseaddr;
21037 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21038 }
21039
21040 /* Handle DW_LNS_advance_pc. */
21041 void handle_advance_pc (CORE_ADDR adjust);
21042
21043 /* Handle a special opcode. */
21044 void handle_special_opcode (unsigned char op_code);
21045
21046 /* Handle DW_LNS_advance_line. */
21047 void handle_advance_line (int line_delta)
21048 {
21049 advance_line (line_delta);
21050 }
21051
21052 /* Handle DW_LNS_set_file. */
21053 void handle_set_file (file_name_index file);
21054
21055 /* Handle DW_LNS_negate_stmt. */
21056 void handle_negate_stmt ()
21057 {
21058 m_is_stmt = !m_is_stmt;
21059 }
21060
21061 /* Handle DW_LNS_const_add_pc. */
21062 void handle_const_add_pc ();
21063
21064 /* Handle DW_LNS_fixed_advance_pc. */
21065 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21066 {
21067 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21068 m_op_index = 0;
21069 }
21070
21071 /* Handle DW_LNS_copy. */
21072 void handle_copy ()
21073 {
21074 record_line (false);
21075 m_discriminator = 0;
21076 }
21077
21078 /* Handle DW_LNE_end_sequence. */
21079 void handle_end_sequence ()
21080 {
21081 m_currently_recording_lines = true;
21082 }
21083
21084 private:
21085 /* Advance the line by LINE_DELTA. */
21086 void advance_line (int line_delta)
21087 {
21088 m_line += line_delta;
21089
21090 if (line_delta != 0)
21091 m_line_has_non_zero_discriminator = m_discriminator != 0;
21092 }
21093
21094 struct dwarf2_cu *m_cu;
21095
21096 gdbarch *m_gdbarch;
21097
21098 /* True if we're recording lines.
21099 Otherwise we're building partial symtabs and are just interested in
21100 finding include files mentioned by the line number program. */
21101 bool m_record_lines_p;
21102
21103 /* The line number header. */
21104 line_header *m_line_header;
21105
21106 /* These are part of the standard DWARF line number state machine,
21107 and initialized according to the DWARF spec. */
21108
21109 unsigned char m_op_index = 0;
21110 /* The line table index of the current file. */
21111 file_name_index m_file = 1;
21112 unsigned int m_line = 1;
21113
21114 /* These are initialized in the constructor. */
21115
21116 CORE_ADDR m_address;
21117 bool m_is_stmt;
21118 unsigned int m_discriminator;
21119
21120 /* Additional bits of state we need to track. */
21121
21122 /* The last file that we called dwarf2_start_subfile for.
21123 This is only used for TLLs. */
21124 unsigned int m_last_file = 0;
21125 /* The last file a line number was recorded for. */
21126 struct subfile *m_last_subfile = NULL;
21127
21128 /* The address of the last line entry. */
21129 CORE_ADDR m_last_address;
21130
21131 /* Set to true when a previous line at the same address (using
21132 m_last_address) had m_is_stmt true. This is reset to false when a
21133 line entry at a new address (m_address different to m_last_address) is
21134 processed. */
21135 bool m_stmt_at_address = false;
21136
21137 /* When true, record the lines we decode. */
21138 bool m_currently_recording_lines = false;
21139
21140 /* The last line number that was recorded, used to coalesce
21141 consecutive entries for the same line. This can happen, for
21142 example, when discriminators are present. PR 17276. */
21143 unsigned int m_last_line = 0;
21144 bool m_line_has_non_zero_discriminator = false;
21145 };
21146
21147 void
21148 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21149 {
21150 CORE_ADDR addr_adj = (((m_op_index + adjust)
21151 / m_line_header->maximum_ops_per_instruction)
21152 * m_line_header->minimum_instruction_length);
21153 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21154 m_op_index = ((m_op_index + adjust)
21155 % m_line_header->maximum_ops_per_instruction);
21156 }
21157
21158 void
21159 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21160 {
21161 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21162 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21163 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21164 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
21165 / m_line_header->maximum_ops_per_instruction)
21166 * m_line_header->minimum_instruction_length);
21167 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21168 m_op_index = ((m_op_index + adj_opcode_d)
21169 % m_line_header->maximum_ops_per_instruction);
21170
21171 int line_delta = m_line_header->line_base + adj_opcode_r;
21172 advance_line (line_delta);
21173 record_line (false);
21174 m_discriminator = 0;
21175 }
21176
21177 void
21178 lnp_state_machine::handle_set_file (file_name_index file)
21179 {
21180 m_file = file;
21181
21182 const file_entry *fe = current_file ();
21183 if (fe == NULL)
21184 dwarf2_debug_line_missing_file_complaint ();
21185 else if (m_record_lines_p)
21186 {
21187 const char *dir = fe->include_dir (m_line_header);
21188
21189 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21190 m_line_has_non_zero_discriminator = m_discriminator != 0;
21191 dwarf2_start_subfile (m_cu, fe->name, dir);
21192 }
21193 }
21194
21195 void
21196 lnp_state_machine::handle_const_add_pc ()
21197 {
21198 CORE_ADDR adjust
21199 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21200
21201 CORE_ADDR addr_adj
21202 = (((m_op_index + adjust)
21203 / m_line_header->maximum_ops_per_instruction)
21204 * m_line_header->minimum_instruction_length);
21205
21206 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21207 m_op_index = ((m_op_index + adjust)
21208 % m_line_header->maximum_ops_per_instruction);
21209 }
21210
21211 /* Return non-zero if we should add LINE to the line number table.
21212 LINE is the line to add, LAST_LINE is the last line that was added,
21213 LAST_SUBFILE is the subfile for LAST_LINE.
21214 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21215 had a non-zero discriminator.
21216
21217 We have to be careful in the presence of discriminators.
21218 E.g., for this line:
21219
21220 for (i = 0; i < 100000; i++);
21221
21222 clang can emit four line number entries for that one line,
21223 each with a different discriminator.
21224 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21225
21226 However, we want gdb to coalesce all four entries into one.
21227 Otherwise the user could stepi into the middle of the line and
21228 gdb would get confused about whether the pc really was in the
21229 middle of the line.
21230
21231 Things are further complicated by the fact that two consecutive
21232 line number entries for the same line is a heuristic used by gcc
21233 to denote the end of the prologue. So we can't just discard duplicate
21234 entries, we have to be selective about it. The heuristic we use is
21235 that we only collapse consecutive entries for the same line if at least
21236 one of those entries has a non-zero discriminator. PR 17276.
21237
21238 Note: Addresses in the line number state machine can never go backwards
21239 within one sequence, thus this coalescing is ok. */
21240
21241 static int
21242 dwarf_record_line_p (struct dwarf2_cu *cu,
21243 unsigned int line, unsigned int last_line,
21244 int line_has_non_zero_discriminator,
21245 struct subfile *last_subfile)
21246 {
21247 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21248 return 1;
21249 if (line != last_line)
21250 return 1;
21251 /* Same line for the same file that we've seen already.
21252 As a last check, for pr 17276, only record the line if the line
21253 has never had a non-zero discriminator. */
21254 if (!line_has_non_zero_discriminator)
21255 return 1;
21256 return 0;
21257 }
21258
21259 /* Use the CU's builder to record line number LINE beginning at
21260 address ADDRESS in the line table of subfile SUBFILE. */
21261
21262 static void
21263 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21264 unsigned int line, CORE_ADDR address, bool is_stmt,
21265 struct dwarf2_cu *cu)
21266 {
21267 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21268
21269 if (dwarf_line_debug)
21270 {
21271 fprintf_unfiltered (gdb_stdlog,
21272 "Recording line %u, file %s, address %s\n",
21273 line, lbasename (subfile->name),
21274 paddress (gdbarch, address));
21275 }
21276
21277 if (cu != nullptr)
21278 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
21279 }
21280
21281 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21282 Mark the end of a set of line number records.
21283 The arguments are the same as for dwarf_record_line_1.
21284 If SUBFILE is NULL the request is ignored. */
21285
21286 static void
21287 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21288 CORE_ADDR address, struct dwarf2_cu *cu)
21289 {
21290 if (subfile == NULL)
21291 return;
21292
21293 if (dwarf_line_debug)
21294 {
21295 fprintf_unfiltered (gdb_stdlog,
21296 "Finishing current line, file %s, address %s\n",
21297 lbasename (subfile->name),
21298 paddress (gdbarch, address));
21299 }
21300
21301 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
21302 }
21303
21304 void
21305 lnp_state_machine::record_line (bool end_sequence)
21306 {
21307 if (dwarf_line_debug)
21308 {
21309 fprintf_unfiltered (gdb_stdlog,
21310 "Processing actual line %u: file %u,"
21311 " address %s, is_stmt %u, discrim %u%s\n",
21312 m_line, m_file,
21313 paddress (m_gdbarch, m_address),
21314 m_is_stmt, m_discriminator,
21315 (end_sequence ? "\t(end sequence)" : ""));
21316 }
21317
21318 file_entry *fe = current_file ();
21319
21320 if (fe == NULL)
21321 dwarf2_debug_line_missing_file_complaint ();
21322 /* For now we ignore lines not starting on an instruction boundary.
21323 But not when processing end_sequence for compatibility with the
21324 previous version of the code. */
21325 else if (m_op_index == 0 || end_sequence)
21326 {
21327 fe->included_p = 1;
21328 if (m_record_lines_p)
21329 {
21330 /* When we switch files we insert an end maker in the first file,
21331 switch to the second file and add a new line entry. The
21332 problem is that the end marker inserted in the first file will
21333 discard any previous line entries at the same address. If the
21334 line entries in the first file are marked as is-stmt, while
21335 the new line in the second file is non-stmt, then this means
21336 the end marker will discard is-stmt lines so we can have a
21337 non-stmt line. This means that there are less addresses at
21338 which the user can insert a breakpoint.
21339
21340 To improve this we track the last address in m_last_address,
21341 and whether we have seen an is-stmt at this address. Then
21342 when switching files, if we have seen a stmt at the current
21343 address, and we are switching to create a non-stmt line, then
21344 discard the new line. */
21345 bool file_changed
21346 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21347 bool ignore_this_line
21348 = ((file_changed && !end_sequence && m_last_address == m_address
21349 && !m_is_stmt && m_stmt_at_address)
21350 || (!end_sequence && m_line == 0));
21351
21352 if ((file_changed && !ignore_this_line) || end_sequence)
21353 {
21354 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21355 m_currently_recording_lines ? m_cu : nullptr);
21356 }
21357
21358 if (!end_sequence && !ignore_this_line)
21359 {
21360 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21361
21362 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21363 m_line_has_non_zero_discriminator,
21364 m_last_subfile))
21365 {
21366 buildsym_compunit *builder = m_cu->get_builder ();
21367 dwarf_record_line_1 (m_gdbarch,
21368 builder->get_current_subfile (),
21369 m_line, m_address, is_stmt,
21370 m_currently_recording_lines ? m_cu : nullptr);
21371 }
21372 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21373 m_last_line = m_line;
21374 }
21375 }
21376 }
21377
21378 /* Track whether we have seen any m_is_stmt true at m_address in case we
21379 have multiple line table entries all at m_address. */
21380 if (m_last_address != m_address)
21381 {
21382 m_stmt_at_address = false;
21383 m_last_address = m_address;
21384 }
21385 m_stmt_at_address |= m_is_stmt;
21386 }
21387
21388 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21389 line_header *lh, bool record_lines_p)
21390 {
21391 m_cu = cu;
21392 m_gdbarch = arch;
21393 m_record_lines_p = record_lines_p;
21394 m_line_header = lh;
21395
21396 m_currently_recording_lines = true;
21397
21398 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21399 was a line entry for it so that the backend has a chance to adjust it
21400 and also record it in case it needs it. This is currently used by MIPS
21401 code, cf. `mips_adjust_dwarf2_line'. */
21402 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21403 m_is_stmt = lh->default_is_stmt;
21404 m_discriminator = 0;
21405
21406 m_last_address = m_address;
21407 m_stmt_at_address = false;
21408 }
21409
21410 void
21411 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21412 const gdb_byte *line_ptr,
21413 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21414 {
21415 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21416 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21417 located at 0x0. In this case, additionally check that if
21418 ADDRESS < UNRELOCATED_LOWPC. */
21419
21420 if ((address == 0 && address < unrelocated_lowpc)
21421 || address == (CORE_ADDR) -1)
21422 {
21423 /* This line table is for a function which has been
21424 GCd by the linker. Ignore it. PR gdb/12528 */
21425
21426 struct objfile *objfile = cu->per_objfile->objfile;
21427 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21428
21429 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21430 line_offset, objfile_name (objfile));
21431 m_currently_recording_lines = false;
21432 /* Note: m_currently_recording_lines is left as false until we see
21433 DW_LNE_end_sequence. */
21434 }
21435 }
21436
21437 /* Subroutine of dwarf_decode_lines to simplify it.
21438 Process the line number information in LH.
21439 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21440 program in order to set included_p for every referenced header. */
21441
21442 static void
21443 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21444 const int decode_for_pst_p, CORE_ADDR lowpc)
21445 {
21446 const gdb_byte *line_ptr, *extended_end;
21447 const gdb_byte *line_end;
21448 unsigned int bytes_read, extended_len;
21449 unsigned char op_code, extended_op;
21450 CORE_ADDR baseaddr;
21451 struct objfile *objfile = cu->per_objfile->objfile;
21452 bfd *abfd = objfile->obfd;
21453 struct gdbarch *gdbarch = objfile->arch ();
21454 /* True if we're recording line info (as opposed to building partial
21455 symtabs and just interested in finding include files mentioned by
21456 the line number program). */
21457 bool record_lines_p = !decode_for_pst_p;
21458
21459 baseaddr = objfile->text_section_offset ();
21460
21461 line_ptr = lh->statement_program_start;
21462 line_end = lh->statement_program_end;
21463
21464 /* Read the statement sequences until there's nothing left. */
21465 while (line_ptr < line_end)
21466 {
21467 /* The DWARF line number program state machine. Reset the state
21468 machine at the start of each sequence. */
21469 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21470 bool end_sequence = false;
21471
21472 if (record_lines_p)
21473 {
21474 /* Start a subfile for the current file of the state
21475 machine. */
21476 const file_entry *fe = state_machine.current_file ();
21477
21478 if (fe != NULL)
21479 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21480 }
21481
21482 /* Decode the table. */
21483 while (line_ptr < line_end && !end_sequence)
21484 {
21485 op_code = read_1_byte (abfd, line_ptr);
21486 line_ptr += 1;
21487
21488 if (op_code >= lh->opcode_base)
21489 {
21490 /* Special opcode. */
21491 state_machine.handle_special_opcode (op_code);
21492 }
21493 else switch (op_code)
21494 {
21495 case DW_LNS_extended_op:
21496 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21497 &bytes_read);
21498 line_ptr += bytes_read;
21499 extended_end = line_ptr + extended_len;
21500 extended_op = read_1_byte (abfd, line_ptr);
21501 line_ptr += 1;
21502 if (DW_LNE_lo_user <= extended_op
21503 && extended_op <= DW_LNE_hi_user)
21504 {
21505 /* Vendor extension, ignore. */
21506 line_ptr = extended_end;
21507 break;
21508 }
21509 switch (extended_op)
21510 {
21511 case DW_LNE_end_sequence:
21512 state_machine.handle_end_sequence ();
21513 end_sequence = true;
21514 break;
21515 case DW_LNE_set_address:
21516 {
21517 CORE_ADDR address
21518 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21519 line_ptr += bytes_read;
21520
21521 state_machine.check_line_address (cu, line_ptr,
21522 lowpc - baseaddr, address);
21523 state_machine.handle_set_address (baseaddr, address);
21524 }
21525 break;
21526 case DW_LNE_define_file:
21527 {
21528 const char *cur_file;
21529 unsigned int mod_time, length;
21530 dir_index dindex;
21531
21532 cur_file = read_direct_string (abfd, line_ptr,
21533 &bytes_read);
21534 line_ptr += bytes_read;
21535 dindex = (dir_index)
21536 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21537 line_ptr += bytes_read;
21538 mod_time =
21539 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21540 line_ptr += bytes_read;
21541 length =
21542 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21543 line_ptr += bytes_read;
21544 lh->add_file_name (cur_file, dindex, mod_time, length);
21545 }
21546 break;
21547 case DW_LNE_set_discriminator:
21548 {
21549 /* The discriminator is not interesting to the
21550 debugger; just ignore it. We still need to
21551 check its value though:
21552 if there are consecutive entries for the same
21553 (non-prologue) line we want to coalesce them.
21554 PR 17276. */
21555 unsigned int discr
21556 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21557 line_ptr += bytes_read;
21558
21559 state_machine.handle_set_discriminator (discr);
21560 }
21561 break;
21562 default:
21563 complaint (_("mangled .debug_line section"));
21564 return;
21565 }
21566 /* Make sure that we parsed the extended op correctly. If e.g.
21567 we expected a different address size than the producer used,
21568 we may have read the wrong number of bytes. */
21569 if (line_ptr != extended_end)
21570 {
21571 complaint (_("mangled .debug_line section"));
21572 return;
21573 }
21574 break;
21575 case DW_LNS_copy:
21576 state_machine.handle_copy ();
21577 break;
21578 case DW_LNS_advance_pc:
21579 {
21580 CORE_ADDR adjust
21581 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21582 line_ptr += bytes_read;
21583
21584 state_machine.handle_advance_pc (adjust);
21585 }
21586 break;
21587 case DW_LNS_advance_line:
21588 {
21589 int line_delta
21590 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21591 line_ptr += bytes_read;
21592
21593 state_machine.handle_advance_line (line_delta);
21594 }
21595 break;
21596 case DW_LNS_set_file:
21597 {
21598 file_name_index file
21599 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21600 &bytes_read);
21601 line_ptr += bytes_read;
21602
21603 state_machine.handle_set_file (file);
21604 }
21605 break;
21606 case DW_LNS_set_column:
21607 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21608 line_ptr += bytes_read;
21609 break;
21610 case DW_LNS_negate_stmt:
21611 state_machine.handle_negate_stmt ();
21612 break;
21613 case DW_LNS_set_basic_block:
21614 break;
21615 /* Add to the address register of the state machine the
21616 address increment value corresponding to special opcode
21617 255. I.e., this value is scaled by the minimum
21618 instruction length since special opcode 255 would have
21619 scaled the increment. */
21620 case DW_LNS_const_add_pc:
21621 state_machine.handle_const_add_pc ();
21622 break;
21623 case DW_LNS_fixed_advance_pc:
21624 {
21625 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21626 line_ptr += 2;
21627
21628 state_machine.handle_fixed_advance_pc (addr_adj);
21629 }
21630 break;
21631 default:
21632 {
21633 /* Unknown standard opcode, ignore it. */
21634 int i;
21635
21636 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21637 {
21638 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21639 line_ptr += bytes_read;
21640 }
21641 }
21642 }
21643 }
21644
21645 if (!end_sequence)
21646 dwarf2_debug_line_missing_end_sequence_complaint ();
21647
21648 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21649 in which case we still finish recording the last line). */
21650 state_machine.record_line (true);
21651 }
21652 }
21653
21654 /* Decode the Line Number Program (LNP) for the given line_header
21655 structure and CU. The actual information extracted and the type
21656 of structures created from the LNP depends on the value of PST.
21657
21658 1. If PST is NULL, then this procedure uses the data from the program
21659 to create all necessary symbol tables, and their linetables.
21660
21661 2. If PST is not NULL, this procedure reads the program to determine
21662 the list of files included by the unit represented by PST, and
21663 builds all the associated partial symbol tables.
21664
21665 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21666 It is used for relative paths in the line table.
21667 NOTE: When processing partial symtabs (pst != NULL),
21668 comp_dir == pst->dirname.
21669
21670 NOTE: It is important that psymtabs have the same file name (via strcmp)
21671 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21672 symtab we don't use it in the name of the psymtabs we create.
21673 E.g. expand_line_sal requires this when finding psymtabs to expand.
21674 A good testcase for this is mb-inline.exp.
21675
21676 LOWPC is the lowest address in CU (or 0 if not known).
21677
21678 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21679 for its PC<->lines mapping information. Otherwise only the filename
21680 table is read in. */
21681
21682 static void
21683 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21684 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21685 CORE_ADDR lowpc, int decode_mapping)
21686 {
21687 struct objfile *objfile = cu->per_objfile->objfile;
21688 const int decode_for_pst_p = (pst != NULL);
21689
21690 if (decode_mapping)
21691 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21692
21693 if (decode_for_pst_p)
21694 {
21695 /* Now that we're done scanning the Line Header Program, we can
21696 create the psymtab of each included file. */
21697 for (auto &file_entry : lh->file_names ())
21698 if (file_entry.included_p == 1)
21699 {
21700 gdb::unique_xmalloc_ptr<char> name_holder;
21701 const char *include_name =
21702 psymtab_include_file_name (lh, file_entry, pst,
21703 comp_dir, &name_holder);
21704 if (include_name != NULL)
21705 dwarf2_create_include_psymtab (include_name, pst, objfile);
21706 }
21707 }
21708 else
21709 {
21710 /* Make sure a symtab is created for every file, even files
21711 which contain only variables (i.e. no code with associated
21712 line numbers). */
21713 buildsym_compunit *builder = cu->get_builder ();
21714 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21715
21716 for (auto &fe : lh->file_names ())
21717 {
21718 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21719 if (builder->get_current_subfile ()->symtab == NULL)
21720 {
21721 builder->get_current_subfile ()->symtab
21722 = allocate_symtab (cust,
21723 builder->get_current_subfile ()->name);
21724 }
21725 fe.symtab = builder->get_current_subfile ()->symtab;
21726 }
21727 }
21728 }
21729
21730 /* Start a subfile for DWARF. FILENAME is the name of the file and
21731 DIRNAME the name of the source directory which contains FILENAME
21732 or NULL if not known.
21733 This routine tries to keep line numbers from identical absolute and
21734 relative file names in a common subfile.
21735
21736 Using the `list' example from the GDB testsuite, which resides in
21737 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21738 of /srcdir/list0.c yields the following debugging information for list0.c:
21739
21740 DW_AT_name: /srcdir/list0.c
21741 DW_AT_comp_dir: /compdir
21742 files.files[0].name: list0.h
21743 files.files[0].dir: /srcdir
21744 files.files[1].name: list0.c
21745 files.files[1].dir: /srcdir
21746
21747 The line number information for list0.c has to end up in a single
21748 subfile, so that `break /srcdir/list0.c:1' works as expected.
21749 start_subfile will ensure that this happens provided that we pass the
21750 concatenation of files.files[1].dir and files.files[1].name as the
21751 subfile's name. */
21752
21753 static void
21754 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21755 const char *dirname)
21756 {
21757 gdb::unique_xmalloc_ptr<char> copy;
21758
21759 /* In order not to lose the line information directory,
21760 we concatenate it to the filename when it makes sense.
21761 Note that the Dwarf3 standard says (speaking of filenames in line
21762 information): ``The directory index is ignored for file names
21763 that represent full path names''. Thus ignoring dirname in the
21764 `else' branch below isn't an issue. */
21765
21766 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21767 {
21768 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21769 filename = copy.get ();
21770 }
21771
21772 cu->get_builder ()->start_subfile (filename);
21773 }
21774
21775 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21776 buildsym_compunit constructor. */
21777
21778 struct compunit_symtab *
21779 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21780 CORE_ADDR low_pc)
21781 {
21782 gdb_assert (m_builder == nullptr);
21783
21784 m_builder.reset (new struct buildsym_compunit
21785 (this->per_objfile->objfile,
21786 name, comp_dir, language, low_pc));
21787
21788 list_in_scope = get_builder ()->get_file_symbols ();
21789
21790 get_builder ()->record_debugformat ("DWARF 2");
21791 get_builder ()->record_producer (producer);
21792
21793 processing_has_namespace_info = false;
21794
21795 return get_builder ()->get_compunit_symtab ();
21796 }
21797
21798 static void
21799 var_decode_location (struct attribute *attr, struct symbol *sym,
21800 struct dwarf2_cu *cu)
21801 {
21802 struct objfile *objfile = cu->per_objfile->objfile;
21803 struct comp_unit_head *cu_header = &cu->header;
21804
21805 /* NOTE drow/2003-01-30: There used to be a comment and some special
21806 code here to turn a symbol with DW_AT_external and a
21807 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21808 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21809 with some versions of binutils) where shared libraries could have
21810 relocations against symbols in their debug information - the
21811 minimal symbol would have the right address, but the debug info
21812 would not. It's no longer necessary, because we will explicitly
21813 apply relocations when we read in the debug information now. */
21814
21815 /* A DW_AT_location attribute with no contents indicates that a
21816 variable has been optimized away. */
21817 if (attr->form_is_block () && attr->as_block ()->size == 0)
21818 {
21819 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21820 return;
21821 }
21822
21823 /* Handle one degenerate form of location expression specially, to
21824 preserve GDB's previous behavior when section offsets are
21825 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21826 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21827
21828 if (attr->form_is_block ())
21829 {
21830 struct dwarf_block *block = attr->as_block ();
21831
21832 if ((block->data[0] == DW_OP_addr
21833 && block->size == 1 + cu_header->addr_size)
21834 || ((block->data[0] == DW_OP_GNU_addr_index
21835 || block->data[0] == DW_OP_addrx)
21836 && (block->size
21837 == 1 + leb128_size (&block->data[1]))))
21838 {
21839 unsigned int dummy;
21840
21841 if (block->data[0] == DW_OP_addr)
21842 SET_SYMBOL_VALUE_ADDRESS
21843 (sym, cu->header.read_address (objfile->obfd,
21844 block->data + 1,
21845 &dummy));
21846 else
21847 SET_SYMBOL_VALUE_ADDRESS
21848 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21849 &dummy));
21850 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21851 fixup_symbol_section (sym, objfile);
21852 SET_SYMBOL_VALUE_ADDRESS
21853 (sym,
21854 SYMBOL_VALUE_ADDRESS (sym)
21855 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
21856 return;
21857 }
21858 }
21859
21860 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21861 expression evaluator, and use LOC_COMPUTED only when necessary
21862 (i.e. when the value of a register or memory location is
21863 referenced, or a thread-local block, etc.). Then again, it might
21864 not be worthwhile. I'm assuming that it isn't unless performance
21865 or memory numbers show me otherwise. */
21866
21867 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21868
21869 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21870 cu->has_loclist = true;
21871 }
21872
21873 /* Given a pointer to a DWARF information entry, figure out if we need
21874 to make a symbol table entry for it, and if so, create a new entry
21875 and return a pointer to it.
21876 If TYPE is NULL, determine symbol type from the die, otherwise
21877 used the passed type.
21878 If SPACE is not NULL, use it to hold the new symbol. If it is
21879 NULL, allocate a new symbol on the objfile's obstack. */
21880
21881 static struct symbol *
21882 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21883 struct symbol *space)
21884 {
21885 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21886 struct objfile *objfile = per_objfile->objfile;
21887 struct gdbarch *gdbarch = objfile->arch ();
21888 struct symbol *sym = NULL;
21889 const char *name;
21890 struct attribute *attr = NULL;
21891 struct attribute *attr2 = NULL;
21892 CORE_ADDR baseaddr;
21893 struct pending **list_to_add = NULL;
21894
21895 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21896
21897 baseaddr = objfile->text_section_offset ();
21898
21899 name = dwarf2_name (die, cu);
21900 if (name)
21901 {
21902 int suppress_add = 0;
21903
21904 if (space)
21905 sym = space;
21906 else
21907 sym = new (&objfile->objfile_obstack) symbol;
21908 OBJSTAT (objfile, n_syms++);
21909
21910 /* Cache this symbol's name and the name's demangled form (if any). */
21911 sym->set_language (cu->language, &objfile->objfile_obstack);
21912 /* Fortran does not have mangling standard and the mangling does differ
21913 between gfortran, iFort etc. */
21914 const char *physname
21915 = (cu->language == language_fortran
21916 ? dwarf2_full_name (name, die, cu)
21917 : dwarf2_physname (name, die, cu));
21918 const char *linkagename = dw2_linkage_name (die, cu);
21919
21920 if (linkagename == nullptr || cu->language == language_ada)
21921 sym->set_linkage_name (physname);
21922 else
21923 {
21924 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21925 sym->set_linkage_name (linkagename);
21926 }
21927
21928 /* Default assumptions.
21929 Use the passed type or decode it from the die. */
21930 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21931 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21932 if (type != NULL)
21933 SYMBOL_TYPE (sym) = type;
21934 else
21935 SYMBOL_TYPE (sym) = die_type (die, cu);
21936 attr = dwarf2_attr (die,
21937 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21938 cu);
21939 if (attr != nullptr)
21940 SYMBOL_LINE (sym) = attr->constant_value (0);
21941
21942 attr = dwarf2_attr (die,
21943 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21944 cu);
21945 if (attr != nullptr && attr->form_is_unsigned ())
21946 {
21947 file_name_index file_index
21948 = (file_name_index) attr->as_unsigned ();
21949 struct file_entry *fe;
21950
21951 if (cu->line_header != NULL)
21952 fe = cu->line_header->file_name_at (file_index);
21953 else
21954 fe = NULL;
21955
21956 if (fe == NULL)
21957 complaint (_("file index out of range"));
21958 else
21959 symbol_set_symtab (sym, fe->symtab);
21960 }
21961
21962 switch (die->tag)
21963 {
21964 case DW_TAG_label:
21965 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21966 if (attr != nullptr)
21967 {
21968 CORE_ADDR addr;
21969
21970 addr = attr->as_address ();
21971 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21972 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21973 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21974 }
21975 else
21976 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21977 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21978 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21979 add_symbol_to_list (sym, cu->list_in_scope);
21980 break;
21981 case DW_TAG_subprogram:
21982 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21983 finish_block. */
21984 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21985 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21986 if ((attr2 != nullptr && attr2->as_boolean ())
21987 || cu->language == language_ada
21988 || cu->language == language_fortran)
21989 {
21990 /* Subprograms marked external are stored as a global symbol.
21991 Ada and Fortran subprograms, whether marked external or
21992 not, are always stored as a global symbol, because we want
21993 to be able to access them globally. For instance, we want
21994 to be able to break on a nested subprogram without having
21995 to specify the context. */
21996 list_to_add = cu->get_builder ()->get_global_symbols ();
21997 }
21998 else
21999 {
22000 list_to_add = cu->list_in_scope;
22001 }
22002 break;
22003 case DW_TAG_inlined_subroutine:
22004 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22005 finish_block. */
22006 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22007 SYMBOL_INLINED (sym) = 1;
22008 list_to_add = cu->list_in_scope;
22009 break;
22010 case DW_TAG_template_value_param:
22011 suppress_add = 1;
22012 /* Fall through. */
22013 case DW_TAG_constant:
22014 case DW_TAG_variable:
22015 case DW_TAG_member:
22016 /* Compilation with minimal debug info may result in
22017 variables with missing type entries. Change the
22018 misleading `void' type to something sensible. */
22019 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
22020 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
22021
22022 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22023 /* In the case of DW_TAG_member, we should only be called for
22024 static const members. */
22025 if (die->tag == DW_TAG_member)
22026 {
22027 /* dwarf2_add_field uses die_is_declaration,
22028 so we do the same. */
22029 gdb_assert (die_is_declaration (die, cu));
22030 gdb_assert (attr);
22031 }
22032 if (attr != nullptr)
22033 {
22034 dwarf2_const_value (attr, sym, cu);
22035 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22036 if (!suppress_add)
22037 {
22038 if (attr2 != nullptr && attr2->as_boolean ())
22039 list_to_add = cu->get_builder ()->get_global_symbols ();
22040 else
22041 list_to_add = cu->list_in_scope;
22042 }
22043 break;
22044 }
22045 attr = dwarf2_attr (die, DW_AT_location, cu);
22046 if (attr != nullptr)
22047 {
22048 var_decode_location (attr, sym, cu);
22049 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22050
22051 /* Fortran explicitly imports any global symbols to the local
22052 scope by DW_TAG_common_block. */
22053 if (cu->language == language_fortran && die->parent
22054 && die->parent->tag == DW_TAG_common_block)
22055 attr2 = NULL;
22056
22057 if (SYMBOL_CLASS (sym) == LOC_STATIC
22058 && SYMBOL_VALUE_ADDRESS (sym) == 0
22059 && !per_objfile->per_bfd->has_section_at_zero)
22060 {
22061 /* When a static variable is eliminated by the linker,
22062 the corresponding debug information is not stripped
22063 out, but the variable address is set to null;
22064 do not add such variables into symbol table. */
22065 }
22066 else if (attr2 != nullptr && attr2->as_boolean ())
22067 {
22068 if (SYMBOL_CLASS (sym) == LOC_STATIC
22069 && (objfile->flags & OBJF_MAINLINE) == 0
22070 && per_objfile->per_bfd->can_copy)
22071 {
22072 /* A global static variable might be subject to
22073 copy relocation. We first check for a local
22074 minsym, though, because maybe the symbol was
22075 marked hidden, in which case this would not
22076 apply. */
22077 bound_minimal_symbol found
22078 = (lookup_minimal_symbol_linkage
22079 (sym->linkage_name (), objfile));
22080 if (found.minsym != nullptr)
22081 sym->maybe_copied = 1;
22082 }
22083
22084 /* A variable with DW_AT_external is never static,
22085 but it may be block-scoped. */
22086 list_to_add
22087 = ((cu->list_in_scope
22088 == cu->get_builder ()->get_file_symbols ())
22089 ? cu->get_builder ()->get_global_symbols ()
22090 : cu->list_in_scope);
22091 }
22092 else
22093 list_to_add = cu->list_in_scope;
22094 }
22095 else
22096 {
22097 /* We do not know the address of this symbol.
22098 If it is an external symbol and we have type information
22099 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22100 The address of the variable will then be determined from
22101 the minimal symbol table whenever the variable is
22102 referenced. */
22103 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22104
22105 /* Fortran explicitly imports any global symbols to the local
22106 scope by DW_TAG_common_block. */
22107 if (cu->language == language_fortran && die->parent
22108 && die->parent->tag == DW_TAG_common_block)
22109 {
22110 /* SYMBOL_CLASS doesn't matter here because
22111 read_common_block is going to reset it. */
22112 if (!suppress_add)
22113 list_to_add = cu->list_in_scope;
22114 }
22115 else if (attr2 != nullptr && attr2->as_boolean ()
22116 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22117 {
22118 /* A variable with DW_AT_external is never static, but it
22119 may be block-scoped. */
22120 list_to_add
22121 = ((cu->list_in_scope
22122 == cu->get_builder ()->get_file_symbols ())
22123 ? cu->get_builder ()->get_global_symbols ()
22124 : cu->list_in_scope);
22125
22126 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22127 }
22128 else if (!die_is_declaration (die, cu))
22129 {
22130 /* Use the default LOC_OPTIMIZED_OUT class. */
22131 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22132 if (!suppress_add)
22133 list_to_add = cu->list_in_scope;
22134 }
22135 }
22136 break;
22137 case DW_TAG_formal_parameter:
22138 {
22139 /* If we are inside a function, mark this as an argument. If
22140 not, we might be looking at an argument to an inlined function
22141 when we do not have enough information to show inlined frames;
22142 pretend it's a local variable in that case so that the user can
22143 still see it. */
22144 struct context_stack *curr
22145 = cu->get_builder ()->get_current_context_stack ();
22146 if (curr != nullptr && curr->name != nullptr)
22147 SYMBOL_IS_ARGUMENT (sym) = 1;
22148 attr = dwarf2_attr (die, DW_AT_location, cu);
22149 if (attr != nullptr)
22150 {
22151 var_decode_location (attr, sym, cu);
22152 }
22153 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22154 if (attr != nullptr)
22155 {
22156 dwarf2_const_value (attr, sym, cu);
22157 }
22158
22159 list_to_add = cu->list_in_scope;
22160 }
22161 break;
22162 case DW_TAG_unspecified_parameters:
22163 /* From varargs functions; gdb doesn't seem to have any
22164 interest in this information, so just ignore it for now.
22165 (FIXME?) */
22166 break;
22167 case DW_TAG_template_type_param:
22168 suppress_add = 1;
22169 /* Fall through. */
22170 case DW_TAG_class_type:
22171 case DW_TAG_interface_type:
22172 case DW_TAG_structure_type:
22173 case DW_TAG_union_type:
22174 case DW_TAG_set_type:
22175 case DW_TAG_enumeration_type:
22176 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22177 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22178
22179 {
22180 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22181 really ever be static objects: otherwise, if you try
22182 to, say, break of a class's method and you're in a file
22183 which doesn't mention that class, it won't work unless
22184 the check for all static symbols in lookup_symbol_aux
22185 saves you. See the OtherFileClass tests in
22186 gdb.c++/namespace.exp. */
22187
22188 if (!suppress_add)
22189 {
22190 buildsym_compunit *builder = cu->get_builder ();
22191 list_to_add
22192 = (cu->list_in_scope == builder->get_file_symbols ()
22193 && cu->language == language_cplus
22194 ? builder->get_global_symbols ()
22195 : cu->list_in_scope);
22196
22197 /* The semantics of C++ state that "struct foo {
22198 ... }" also defines a typedef for "foo". */
22199 if (cu->language == language_cplus
22200 || cu->language == language_ada
22201 || cu->language == language_d
22202 || cu->language == language_rust)
22203 {
22204 /* The symbol's name is already allocated along
22205 with this objfile, so we don't need to
22206 duplicate it for the type. */
22207 if (SYMBOL_TYPE (sym)->name () == 0)
22208 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
22209 }
22210 }
22211 }
22212 break;
22213 case DW_TAG_typedef:
22214 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22215 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22216 list_to_add = cu->list_in_scope;
22217 break;
22218 case DW_TAG_array_type:
22219 case DW_TAG_base_type:
22220 case DW_TAG_subrange_type:
22221 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22222 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22223 list_to_add = cu->list_in_scope;
22224 break;
22225 case DW_TAG_enumerator:
22226 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22227 if (attr != nullptr)
22228 {
22229 dwarf2_const_value (attr, sym, cu);
22230 }
22231 {
22232 /* NOTE: carlton/2003-11-10: See comment above in the
22233 DW_TAG_class_type, etc. block. */
22234
22235 list_to_add
22236 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22237 && cu->language == language_cplus
22238 ? cu->get_builder ()->get_global_symbols ()
22239 : cu->list_in_scope);
22240 }
22241 break;
22242 case DW_TAG_imported_declaration:
22243 case DW_TAG_namespace:
22244 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22245 list_to_add = cu->get_builder ()->get_global_symbols ();
22246 break;
22247 case DW_TAG_module:
22248 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22249 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22250 list_to_add = cu->get_builder ()->get_global_symbols ();
22251 break;
22252 case DW_TAG_common_block:
22253 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22254 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22255 add_symbol_to_list (sym, cu->list_in_scope);
22256 break;
22257 default:
22258 /* Not a tag we recognize. Hopefully we aren't processing
22259 trash data, but since we must specifically ignore things
22260 we don't recognize, there is nothing else we should do at
22261 this point. */
22262 complaint (_("unsupported tag: '%s'"),
22263 dwarf_tag_name (die->tag));
22264 break;
22265 }
22266
22267 if (suppress_add)
22268 {
22269 sym->hash_next = objfile->template_symbols;
22270 objfile->template_symbols = sym;
22271 list_to_add = NULL;
22272 }
22273
22274 if (list_to_add != NULL)
22275 add_symbol_to_list (sym, list_to_add);
22276
22277 /* For the benefit of old versions of GCC, check for anonymous
22278 namespaces based on the demangled name. */
22279 if (!cu->processing_has_namespace_info
22280 && cu->language == language_cplus)
22281 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22282 }
22283 return (sym);
22284 }
22285
22286 /* Given an attr with a DW_FORM_dataN value in host byte order,
22287 zero-extend it as appropriate for the symbol's type. The DWARF
22288 standard (v4) is not entirely clear about the meaning of using
22289 DW_FORM_dataN for a constant with a signed type, where the type is
22290 wider than the data. The conclusion of a discussion on the DWARF
22291 list was that this is unspecified. We choose to always zero-extend
22292 because that is the interpretation long in use by GCC. */
22293
22294 static gdb_byte *
22295 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22296 struct dwarf2_cu *cu, LONGEST *value, int bits)
22297 {
22298 struct objfile *objfile = cu->per_objfile->objfile;
22299 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22300 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22301 LONGEST l = attr->constant_value (0);
22302
22303 if (bits < sizeof (*value) * 8)
22304 {
22305 l &= ((LONGEST) 1 << bits) - 1;
22306 *value = l;
22307 }
22308 else if (bits == sizeof (*value) * 8)
22309 *value = l;
22310 else
22311 {
22312 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22313 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22314 return bytes;
22315 }
22316
22317 return NULL;
22318 }
22319
22320 /* Read a constant value from an attribute. Either set *VALUE, or if
22321 the value does not fit in *VALUE, set *BYTES - either already
22322 allocated on the objfile obstack, or newly allocated on OBSTACK,
22323 or, set *BATON, if we translated the constant to a location
22324 expression. */
22325
22326 static void
22327 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22328 const char *name, struct obstack *obstack,
22329 struct dwarf2_cu *cu,
22330 LONGEST *value, const gdb_byte **bytes,
22331 struct dwarf2_locexpr_baton **baton)
22332 {
22333 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22334 struct objfile *objfile = per_objfile->objfile;
22335 struct comp_unit_head *cu_header = &cu->header;
22336 struct dwarf_block *blk;
22337 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22338 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22339
22340 *value = 0;
22341 *bytes = NULL;
22342 *baton = NULL;
22343
22344 switch (attr->form)
22345 {
22346 case DW_FORM_addr:
22347 case DW_FORM_addrx:
22348 case DW_FORM_GNU_addr_index:
22349 {
22350 gdb_byte *data;
22351
22352 if (TYPE_LENGTH (type) != cu_header->addr_size)
22353 dwarf2_const_value_length_mismatch_complaint (name,
22354 cu_header->addr_size,
22355 TYPE_LENGTH (type));
22356 /* Symbols of this form are reasonably rare, so we just
22357 piggyback on the existing location code rather than writing
22358 a new implementation of symbol_computed_ops. */
22359 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22360 (*baton)->per_objfile = per_objfile;
22361 (*baton)->per_cu = cu->per_cu;
22362 gdb_assert ((*baton)->per_cu);
22363
22364 (*baton)->size = 2 + cu_header->addr_size;
22365 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22366 (*baton)->data = data;
22367
22368 data[0] = DW_OP_addr;
22369 store_unsigned_integer (&data[1], cu_header->addr_size,
22370 byte_order, attr->as_address ());
22371 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22372 }
22373 break;
22374 case DW_FORM_string:
22375 case DW_FORM_strp:
22376 case DW_FORM_strx:
22377 case DW_FORM_GNU_str_index:
22378 case DW_FORM_GNU_strp_alt:
22379 /* The string is already allocated on the objfile obstack, point
22380 directly to it. */
22381 *bytes = (const gdb_byte *) attr->as_string ();
22382 break;
22383 case DW_FORM_block1:
22384 case DW_FORM_block2:
22385 case DW_FORM_block4:
22386 case DW_FORM_block:
22387 case DW_FORM_exprloc:
22388 case DW_FORM_data16:
22389 blk = attr->as_block ();
22390 if (TYPE_LENGTH (type) != blk->size)
22391 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22392 TYPE_LENGTH (type));
22393 *bytes = blk->data;
22394 break;
22395
22396 /* The DW_AT_const_value attributes are supposed to carry the
22397 symbol's value "represented as it would be on the target
22398 architecture." By the time we get here, it's already been
22399 converted to host endianness, so we just need to sign- or
22400 zero-extend it as appropriate. */
22401 case DW_FORM_data1:
22402 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22403 break;
22404 case DW_FORM_data2:
22405 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22406 break;
22407 case DW_FORM_data4:
22408 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22409 break;
22410 case DW_FORM_data8:
22411 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22412 break;
22413
22414 case DW_FORM_sdata:
22415 case DW_FORM_implicit_const:
22416 *value = attr->as_signed ();
22417 break;
22418
22419 case DW_FORM_udata:
22420 *value = attr->as_unsigned ();
22421 break;
22422
22423 default:
22424 complaint (_("unsupported const value attribute form: '%s'"),
22425 dwarf_form_name (attr->form));
22426 *value = 0;
22427 break;
22428 }
22429 }
22430
22431
22432 /* Copy constant value from an attribute to a symbol. */
22433
22434 static void
22435 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22436 struct dwarf2_cu *cu)
22437 {
22438 struct objfile *objfile = cu->per_objfile->objfile;
22439 LONGEST value;
22440 const gdb_byte *bytes;
22441 struct dwarf2_locexpr_baton *baton;
22442
22443 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22444 sym->print_name (),
22445 &objfile->objfile_obstack, cu,
22446 &value, &bytes, &baton);
22447
22448 if (baton != NULL)
22449 {
22450 SYMBOL_LOCATION_BATON (sym) = baton;
22451 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22452 }
22453 else if (bytes != NULL)
22454 {
22455 SYMBOL_VALUE_BYTES (sym) = bytes;
22456 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22457 }
22458 else
22459 {
22460 SYMBOL_VALUE (sym) = value;
22461 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22462 }
22463 }
22464
22465 /* Return the type of the die in question using its DW_AT_type attribute. */
22466
22467 static struct type *
22468 die_type (struct die_info *die, struct dwarf2_cu *cu)
22469 {
22470 struct attribute *type_attr;
22471
22472 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22473 if (!type_attr)
22474 {
22475 struct objfile *objfile = cu->per_objfile->objfile;
22476 /* A missing DW_AT_type represents a void type. */
22477 return objfile_type (objfile)->builtin_void;
22478 }
22479
22480 return lookup_die_type (die, type_attr, cu);
22481 }
22482
22483 /* True iff CU's producer generates GNAT Ada auxiliary information
22484 that allows to find parallel types through that information instead
22485 of having to do expensive parallel lookups by type name. */
22486
22487 static int
22488 need_gnat_info (struct dwarf2_cu *cu)
22489 {
22490 /* Assume that the Ada compiler was GNAT, which always produces
22491 the auxiliary information. */
22492 return (cu->language == language_ada);
22493 }
22494
22495 /* Return the auxiliary type of the die in question using its
22496 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22497 attribute is not present. */
22498
22499 static struct type *
22500 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22501 {
22502 struct attribute *type_attr;
22503
22504 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22505 if (!type_attr)
22506 return NULL;
22507
22508 return lookup_die_type (die, type_attr, cu);
22509 }
22510
22511 /* If DIE has a descriptive_type attribute, then set the TYPE's
22512 descriptive type accordingly. */
22513
22514 static void
22515 set_descriptive_type (struct type *type, struct die_info *die,
22516 struct dwarf2_cu *cu)
22517 {
22518 struct type *descriptive_type = die_descriptive_type (die, cu);
22519
22520 if (descriptive_type)
22521 {
22522 ALLOCATE_GNAT_AUX_TYPE (type);
22523 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22524 }
22525 }
22526
22527 /* Return the containing type of the die in question using its
22528 DW_AT_containing_type attribute. */
22529
22530 static struct type *
22531 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22532 {
22533 struct attribute *type_attr;
22534 struct objfile *objfile = cu->per_objfile->objfile;
22535
22536 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22537 if (!type_attr)
22538 error (_("Dwarf Error: Problem turning containing type into gdb type "
22539 "[in module %s]"), objfile_name (objfile));
22540
22541 return lookup_die_type (die, type_attr, cu);
22542 }
22543
22544 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22545
22546 static struct type *
22547 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22548 {
22549 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22550 struct objfile *objfile = per_objfile->objfile;
22551 char *saved;
22552
22553 std::string message
22554 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22555 objfile_name (objfile),
22556 sect_offset_str (cu->header.sect_off),
22557 sect_offset_str (die->sect_off));
22558 saved = obstack_strdup (&objfile->objfile_obstack, message);
22559
22560 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22561 }
22562
22563 /* Look up the type of DIE in CU using its type attribute ATTR.
22564 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22565 DW_AT_containing_type.
22566 If there is no type substitute an error marker. */
22567
22568 static struct type *
22569 lookup_die_type (struct die_info *die, const struct attribute *attr,
22570 struct dwarf2_cu *cu)
22571 {
22572 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22573 struct objfile *objfile = per_objfile->objfile;
22574 struct type *this_type;
22575
22576 gdb_assert (attr->name == DW_AT_type
22577 || attr->name == DW_AT_GNAT_descriptive_type
22578 || attr->name == DW_AT_containing_type);
22579
22580 /* First see if we have it cached. */
22581
22582 if (attr->form == DW_FORM_GNU_ref_alt)
22583 {
22584 struct dwarf2_per_cu_data *per_cu;
22585 sect_offset sect_off = attr->get_ref_die_offset ();
22586
22587 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22588 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22589 }
22590 else if (attr->form_is_ref ())
22591 {
22592 sect_offset sect_off = attr->get_ref_die_offset ();
22593
22594 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22595 }
22596 else if (attr->form == DW_FORM_ref_sig8)
22597 {
22598 ULONGEST signature = attr->as_signature ();
22599
22600 return get_signatured_type (die, signature, cu);
22601 }
22602 else
22603 {
22604 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22605 " at %s [in module %s]"),
22606 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22607 objfile_name (objfile));
22608 return build_error_marker_type (cu, die);
22609 }
22610
22611 /* If not cached we need to read it in. */
22612
22613 if (this_type == NULL)
22614 {
22615 struct die_info *type_die = NULL;
22616 struct dwarf2_cu *type_cu = cu;
22617
22618 if (attr->form_is_ref ())
22619 type_die = follow_die_ref (die, attr, &type_cu);
22620 if (type_die == NULL)
22621 return build_error_marker_type (cu, die);
22622 /* If we find the type now, it's probably because the type came
22623 from an inter-CU reference and the type's CU got expanded before
22624 ours. */
22625 this_type = read_type_die (type_die, type_cu);
22626 }
22627
22628 /* If we still don't have a type use an error marker. */
22629
22630 if (this_type == NULL)
22631 return build_error_marker_type (cu, die);
22632
22633 return this_type;
22634 }
22635
22636 /* Return the type in DIE, CU.
22637 Returns NULL for invalid types.
22638
22639 This first does a lookup in die_type_hash,
22640 and only reads the die in if necessary.
22641
22642 NOTE: This can be called when reading in partial or full symbols. */
22643
22644 static struct type *
22645 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22646 {
22647 struct type *this_type;
22648
22649 this_type = get_die_type (die, cu);
22650 if (this_type)
22651 return this_type;
22652
22653 return read_type_die_1 (die, cu);
22654 }
22655
22656 /* Read the type in DIE, CU.
22657 Returns NULL for invalid types. */
22658
22659 static struct type *
22660 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22661 {
22662 struct type *this_type = NULL;
22663
22664 switch (die->tag)
22665 {
22666 case DW_TAG_class_type:
22667 case DW_TAG_interface_type:
22668 case DW_TAG_structure_type:
22669 case DW_TAG_union_type:
22670 this_type = read_structure_type (die, cu);
22671 break;
22672 case DW_TAG_enumeration_type:
22673 this_type = read_enumeration_type (die, cu);
22674 break;
22675 case DW_TAG_subprogram:
22676 case DW_TAG_subroutine_type:
22677 case DW_TAG_inlined_subroutine:
22678 this_type = read_subroutine_type (die, cu);
22679 break;
22680 case DW_TAG_array_type:
22681 this_type = read_array_type (die, cu);
22682 break;
22683 case DW_TAG_set_type:
22684 this_type = read_set_type (die, cu);
22685 break;
22686 case DW_TAG_pointer_type:
22687 this_type = read_tag_pointer_type (die, cu);
22688 break;
22689 case DW_TAG_ptr_to_member_type:
22690 this_type = read_tag_ptr_to_member_type (die, cu);
22691 break;
22692 case DW_TAG_reference_type:
22693 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22694 break;
22695 case DW_TAG_rvalue_reference_type:
22696 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22697 break;
22698 case DW_TAG_const_type:
22699 this_type = read_tag_const_type (die, cu);
22700 break;
22701 case DW_TAG_volatile_type:
22702 this_type = read_tag_volatile_type (die, cu);
22703 break;
22704 case DW_TAG_restrict_type:
22705 this_type = read_tag_restrict_type (die, cu);
22706 break;
22707 case DW_TAG_string_type:
22708 this_type = read_tag_string_type (die, cu);
22709 break;
22710 case DW_TAG_typedef:
22711 this_type = read_typedef (die, cu);
22712 break;
22713 case DW_TAG_subrange_type:
22714 this_type = read_subrange_type (die, cu);
22715 break;
22716 case DW_TAG_base_type:
22717 this_type = read_base_type (die, cu);
22718 break;
22719 case DW_TAG_unspecified_type:
22720 this_type = read_unspecified_type (die, cu);
22721 break;
22722 case DW_TAG_namespace:
22723 this_type = read_namespace_type (die, cu);
22724 break;
22725 case DW_TAG_module:
22726 this_type = read_module_type (die, cu);
22727 break;
22728 case DW_TAG_atomic_type:
22729 this_type = read_tag_atomic_type (die, cu);
22730 break;
22731 default:
22732 complaint (_("unexpected tag in read_type_die: '%s'"),
22733 dwarf_tag_name (die->tag));
22734 break;
22735 }
22736
22737 return this_type;
22738 }
22739
22740 /* See if we can figure out if the class lives in a namespace. We do
22741 this by looking for a member function; its demangled name will
22742 contain namespace info, if there is any.
22743 Return the computed name or NULL.
22744 Space for the result is allocated on the objfile's obstack.
22745 This is the full-die version of guess_partial_die_structure_name.
22746 In this case we know DIE has no useful parent. */
22747
22748 static const char *
22749 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22750 {
22751 struct die_info *spec_die;
22752 struct dwarf2_cu *spec_cu;
22753 struct die_info *child;
22754 struct objfile *objfile = cu->per_objfile->objfile;
22755
22756 spec_cu = cu;
22757 spec_die = die_specification (die, &spec_cu);
22758 if (spec_die != NULL)
22759 {
22760 die = spec_die;
22761 cu = spec_cu;
22762 }
22763
22764 for (child = die->child;
22765 child != NULL;
22766 child = child->sibling)
22767 {
22768 if (child->tag == DW_TAG_subprogram)
22769 {
22770 const char *linkage_name = dw2_linkage_name (child, cu);
22771
22772 if (linkage_name != NULL)
22773 {
22774 gdb::unique_xmalloc_ptr<char> actual_name
22775 (cu->language_defn->class_name_from_physname (linkage_name));
22776 const char *name = NULL;
22777
22778 if (actual_name != NULL)
22779 {
22780 const char *die_name = dwarf2_name (die, cu);
22781
22782 if (die_name != NULL
22783 && strcmp (die_name, actual_name.get ()) != 0)
22784 {
22785 /* Strip off the class name from the full name.
22786 We want the prefix. */
22787 int die_name_len = strlen (die_name);
22788 int actual_name_len = strlen (actual_name.get ());
22789 const char *ptr = actual_name.get ();
22790
22791 /* Test for '::' as a sanity check. */
22792 if (actual_name_len > die_name_len + 2
22793 && ptr[actual_name_len - die_name_len - 1] == ':')
22794 name = obstack_strndup (
22795 &objfile->per_bfd->storage_obstack,
22796 ptr, actual_name_len - die_name_len - 2);
22797 }
22798 }
22799 return name;
22800 }
22801 }
22802 }
22803
22804 return NULL;
22805 }
22806
22807 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22808 prefix part in such case. See
22809 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22810
22811 static const char *
22812 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22813 {
22814 struct attribute *attr;
22815 const char *base;
22816
22817 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22818 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22819 return NULL;
22820
22821 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22822 return NULL;
22823
22824 attr = dw2_linkage_name_attr (die, cu);
22825 const char *attr_name = attr->as_string ();
22826 if (attr == NULL || attr_name == NULL)
22827 return NULL;
22828
22829 /* dwarf2_name had to be already called. */
22830 gdb_assert (attr->canonical_string_p ());
22831
22832 /* Strip the base name, keep any leading namespaces/classes. */
22833 base = strrchr (attr_name, ':');
22834 if (base == NULL || base == attr_name || base[-1] != ':')
22835 return "";
22836
22837 struct objfile *objfile = cu->per_objfile->objfile;
22838 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22839 attr_name,
22840 &base[-1] - attr_name);
22841 }
22842
22843 /* Return the name of the namespace/class that DIE is defined within,
22844 or "" if we can't tell. The caller should not xfree the result.
22845
22846 For example, if we're within the method foo() in the following
22847 code:
22848
22849 namespace N {
22850 class C {
22851 void foo () {
22852 }
22853 };
22854 }
22855
22856 then determine_prefix on foo's die will return "N::C". */
22857
22858 static const char *
22859 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22860 {
22861 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22862 struct die_info *parent, *spec_die;
22863 struct dwarf2_cu *spec_cu;
22864 struct type *parent_type;
22865 const char *retval;
22866
22867 if (cu->language != language_cplus
22868 && cu->language != language_fortran && cu->language != language_d
22869 && cu->language != language_rust)
22870 return "";
22871
22872 retval = anonymous_struct_prefix (die, cu);
22873 if (retval)
22874 return retval;
22875
22876 /* We have to be careful in the presence of DW_AT_specification.
22877 For example, with GCC 3.4, given the code
22878
22879 namespace N {
22880 void foo() {
22881 // Definition of N::foo.
22882 }
22883 }
22884
22885 then we'll have a tree of DIEs like this:
22886
22887 1: DW_TAG_compile_unit
22888 2: DW_TAG_namespace // N
22889 3: DW_TAG_subprogram // declaration of N::foo
22890 4: DW_TAG_subprogram // definition of N::foo
22891 DW_AT_specification // refers to die #3
22892
22893 Thus, when processing die #4, we have to pretend that we're in
22894 the context of its DW_AT_specification, namely the contex of die
22895 #3. */
22896 spec_cu = cu;
22897 spec_die = die_specification (die, &spec_cu);
22898 if (spec_die == NULL)
22899 parent = die->parent;
22900 else
22901 {
22902 parent = spec_die->parent;
22903 cu = spec_cu;
22904 }
22905
22906 if (parent == NULL)
22907 return "";
22908 else if (parent->building_fullname)
22909 {
22910 const char *name;
22911 const char *parent_name;
22912
22913 /* It has been seen on RealView 2.2 built binaries,
22914 DW_TAG_template_type_param types actually _defined_ as
22915 children of the parent class:
22916
22917 enum E {};
22918 template class <class Enum> Class{};
22919 Class<enum E> class_e;
22920
22921 1: DW_TAG_class_type (Class)
22922 2: DW_TAG_enumeration_type (E)
22923 3: DW_TAG_enumerator (enum1:0)
22924 3: DW_TAG_enumerator (enum2:1)
22925 ...
22926 2: DW_TAG_template_type_param
22927 DW_AT_type DW_FORM_ref_udata (E)
22928
22929 Besides being broken debug info, it can put GDB into an
22930 infinite loop. Consider:
22931
22932 When we're building the full name for Class<E>, we'll start
22933 at Class, and go look over its template type parameters,
22934 finding E. We'll then try to build the full name of E, and
22935 reach here. We're now trying to build the full name of E,
22936 and look over the parent DIE for containing scope. In the
22937 broken case, if we followed the parent DIE of E, we'd again
22938 find Class, and once again go look at its template type
22939 arguments, etc., etc. Simply don't consider such parent die
22940 as source-level parent of this die (it can't be, the language
22941 doesn't allow it), and break the loop here. */
22942 name = dwarf2_name (die, cu);
22943 parent_name = dwarf2_name (parent, cu);
22944 complaint (_("template param type '%s' defined within parent '%s'"),
22945 name ? name : "<unknown>",
22946 parent_name ? parent_name : "<unknown>");
22947 return "";
22948 }
22949 else
22950 switch (parent->tag)
22951 {
22952 case DW_TAG_namespace:
22953 parent_type = read_type_die (parent, cu);
22954 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22955 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22956 Work around this problem here. */
22957 if (cu->language == language_cplus
22958 && strcmp (parent_type->name (), "::") == 0)
22959 return "";
22960 /* We give a name to even anonymous namespaces. */
22961 return parent_type->name ();
22962 case DW_TAG_class_type:
22963 case DW_TAG_interface_type:
22964 case DW_TAG_structure_type:
22965 case DW_TAG_union_type:
22966 case DW_TAG_module:
22967 parent_type = read_type_die (parent, cu);
22968 if (parent_type->name () != NULL)
22969 return parent_type->name ();
22970 else
22971 /* An anonymous structure is only allowed non-static data
22972 members; no typedefs, no member functions, et cetera.
22973 So it does not need a prefix. */
22974 return "";
22975 case DW_TAG_compile_unit:
22976 case DW_TAG_partial_unit:
22977 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22978 if (cu->language == language_cplus
22979 && !per_objfile->per_bfd->types.empty ()
22980 && die->child != NULL
22981 && (die->tag == DW_TAG_class_type
22982 || die->tag == DW_TAG_structure_type
22983 || die->tag == DW_TAG_union_type))
22984 {
22985 const char *name = guess_full_die_structure_name (die, cu);
22986 if (name != NULL)
22987 return name;
22988 }
22989 return "";
22990 case DW_TAG_subprogram:
22991 /* Nested subroutines in Fortran get a prefix with the name
22992 of the parent's subroutine. */
22993 if (cu->language == language_fortran)
22994 {
22995 if ((die->tag == DW_TAG_subprogram)
22996 && (dwarf2_name (parent, cu) != NULL))
22997 return dwarf2_name (parent, cu);
22998 }
22999 return determine_prefix (parent, cu);
23000 case DW_TAG_enumeration_type:
23001 parent_type = read_type_die (parent, cu);
23002 if (TYPE_DECLARED_CLASS (parent_type))
23003 {
23004 if (parent_type->name () != NULL)
23005 return parent_type->name ();
23006 return "";
23007 }
23008 /* Fall through. */
23009 default:
23010 return determine_prefix (parent, cu);
23011 }
23012 }
23013
23014 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23015 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23016 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23017 an obconcat, otherwise allocate storage for the result. The CU argument is
23018 used to determine the language and hence, the appropriate separator. */
23019
23020 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
23021
23022 static char *
23023 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
23024 int physname, struct dwarf2_cu *cu)
23025 {
23026 const char *lead = "";
23027 const char *sep;
23028
23029 if (suffix == NULL || suffix[0] == '\0'
23030 || prefix == NULL || prefix[0] == '\0')
23031 sep = "";
23032 else if (cu->language == language_d)
23033 {
23034 /* For D, the 'main' function could be defined in any module, but it
23035 should never be prefixed. */
23036 if (strcmp (suffix, "D main") == 0)
23037 {
23038 prefix = "";
23039 sep = "";
23040 }
23041 else
23042 sep = ".";
23043 }
23044 else if (cu->language == language_fortran && physname)
23045 {
23046 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23047 DW_AT_MIPS_linkage_name is preferred and used instead. */
23048
23049 lead = "__";
23050 sep = "_MOD_";
23051 }
23052 else
23053 sep = "::";
23054
23055 if (prefix == NULL)
23056 prefix = "";
23057 if (suffix == NULL)
23058 suffix = "";
23059
23060 if (obs == NULL)
23061 {
23062 char *retval
23063 = ((char *)
23064 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
23065
23066 strcpy (retval, lead);
23067 strcat (retval, prefix);
23068 strcat (retval, sep);
23069 strcat (retval, suffix);
23070 return retval;
23071 }
23072 else
23073 {
23074 /* We have an obstack. */
23075 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
23076 }
23077 }
23078
23079 /* Get name of a die, return NULL if not found. */
23080
23081 static const char *
23082 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23083 struct objfile *objfile)
23084 {
23085 if (name && cu->language == language_cplus)
23086 {
23087 gdb::unique_xmalloc_ptr<char> canon_name
23088 = cp_canonicalize_string (name);
23089
23090 if (canon_name != nullptr)
23091 name = objfile->intern (canon_name.get ());
23092 }
23093
23094 return name;
23095 }
23096
23097 /* Get name of a die, return NULL if not found.
23098 Anonymous namespaces are converted to their magic string. */
23099
23100 static const char *
23101 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23102 {
23103 struct attribute *attr;
23104 struct objfile *objfile = cu->per_objfile->objfile;
23105
23106 attr = dwarf2_attr (die, DW_AT_name, cu);
23107 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23108 if (attr_name == nullptr
23109 && die->tag != DW_TAG_namespace
23110 && die->tag != DW_TAG_class_type
23111 && die->tag != DW_TAG_interface_type
23112 && die->tag != DW_TAG_structure_type
23113 && die->tag != DW_TAG_union_type)
23114 return NULL;
23115
23116 switch (die->tag)
23117 {
23118 case DW_TAG_compile_unit:
23119 case DW_TAG_partial_unit:
23120 /* Compilation units have a DW_AT_name that is a filename, not
23121 a source language identifier. */
23122 case DW_TAG_enumeration_type:
23123 case DW_TAG_enumerator:
23124 /* These tags always have simple identifiers already; no need
23125 to canonicalize them. */
23126 return attr_name;
23127
23128 case DW_TAG_namespace:
23129 if (attr_name != nullptr)
23130 return attr_name;
23131 return CP_ANONYMOUS_NAMESPACE_STR;
23132
23133 case DW_TAG_class_type:
23134 case DW_TAG_interface_type:
23135 case DW_TAG_structure_type:
23136 case DW_TAG_union_type:
23137 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23138 structures or unions. These were of the form "._%d" in GCC 4.1,
23139 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23140 and GCC 4.4. We work around this problem by ignoring these. */
23141 if (attr_name != nullptr
23142 && (startswith (attr_name, "._")
23143 || startswith (attr_name, "<anonymous")))
23144 return NULL;
23145
23146 /* GCC might emit a nameless typedef that has a linkage name. See
23147 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23148 if (!attr || attr_name == NULL)
23149 {
23150 attr = dw2_linkage_name_attr (die, cu);
23151 attr_name = attr == nullptr ? nullptr : attr->as_string ();
23152 if (attr == NULL || attr_name == NULL)
23153 return NULL;
23154
23155 /* Avoid demangling attr_name the second time on a second
23156 call for the same DIE. */
23157 if (!attr->canonical_string_p ())
23158 {
23159 gdb::unique_xmalloc_ptr<char> demangled
23160 (gdb_demangle (attr_name, DMGL_TYPES));
23161 if (demangled == nullptr)
23162 return nullptr;
23163
23164 attr->set_string_canonical (objfile->intern (demangled.get ()));
23165 attr_name = attr->as_string ();
23166 }
23167
23168 /* Strip any leading namespaces/classes, keep only the
23169 base name. DW_AT_name for named DIEs does not
23170 contain the prefixes. */
23171 const char *base = strrchr (attr_name, ':');
23172 if (base && base > attr_name && base[-1] == ':')
23173 return &base[1];
23174 else
23175 return attr_name;
23176 }
23177 break;
23178
23179 default:
23180 break;
23181 }
23182
23183 if (!attr->canonical_string_p ())
23184 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23185 objfile));
23186 return attr->as_string ();
23187 }
23188
23189 /* Return the die that this die in an extension of, or NULL if there
23190 is none. *EXT_CU is the CU containing DIE on input, and the CU
23191 containing the return value on output. */
23192
23193 static struct die_info *
23194 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23195 {
23196 struct attribute *attr;
23197
23198 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23199 if (attr == NULL)
23200 return NULL;
23201
23202 return follow_die_ref (die, attr, ext_cu);
23203 }
23204
23205 static void
23206 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23207 {
23208 unsigned int i;
23209
23210 print_spaces (indent, f);
23211 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23212 dwarf_tag_name (die->tag), die->abbrev,
23213 sect_offset_str (die->sect_off));
23214
23215 if (die->parent != NULL)
23216 {
23217 print_spaces (indent, f);
23218 fprintf_unfiltered (f, " parent at offset: %s\n",
23219 sect_offset_str (die->parent->sect_off));
23220 }
23221
23222 print_spaces (indent, f);
23223 fprintf_unfiltered (f, " has children: %s\n",
23224 dwarf_bool_name (die->child != NULL));
23225
23226 print_spaces (indent, f);
23227 fprintf_unfiltered (f, " attributes:\n");
23228
23229 for (i = 0; i < die->num_attrs; ++i)
23230 {
23231 print_spaces (indent, f);
23232 fprintf_unfiltered (f, " %s (%s) ",
23233 dwarf_attr_name (die->attrs[i].name),
23234 dwarf_form_name (die->attrs[i].form));
23235
23236 switch (die->attrs[i].form)
23237 {
23238 case DW_FORM_addr:
23239 case DW_FORM_addrx:
23240 case DW_FORM_GNU_addr_index:
23241 fprintf_unfiltered (f, "address: ");
23242 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
23243 break;
23244 case DW_FORM_block2:
23245 case DW_FORM_block4:
23246 case DW_FORM_block:
23247 case DW_FORM_block1:
23248 fprintf_unfiltered (f, "block: size %s",
23249 pulongest (die->attrs[i].as_block ()->size));
23250 break;
23251 case DW_FORM_exprloc:
23252 fprintf_unfiltered (f, "expression: size %s",
23253 pulongest (die->attrs[i].as_block ()->size));
23254 break;
23255 case DW_FORM_data16:
23256 fprintf_unfiltered (f, "constant of 16 bytes");
23257 break;
23258 case DW_FORM_ref_addr:
23259 fprintf_unfiltered (f, "ref address: ");
23260 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23261 break;
23262 case DW_FORM_GNU_ref_alt:
23263 fprintf_unfiltered (f, "alt ref address: ");
23264 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23265 break;
23266 case DW_FORM_ref1:
23267 case DW_FORM_ref2:
23268 case DW_FORM_ref4:
23269 case DW_FORM_ref8:
23270 case DW_FORM_ref_udata:
23271 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23272 (long) (die->attrs[i].as_unsigned ()));
23273 break;
23274 case DW_FORM_data1:
23275 case DW_FORM_data2:
23276 case DW_FORM_data4:
23277 case DW_FORM_data8:
23278 case DW_FORM_udata:
23279 fprintf_unfiltered (f, "constant: %s",
23280 pulongest (die->attrs[i].as_unsigned ()));
23281 break;
23282 case DW_FORM_sec_offset:
23283 fprintf_unfiltered (f, "section offset: %s",
23284 pulongest (die->attrs[i].as_unsigned ()));
23285 break;
23286 case DW_FORM_ref_sig8:
23287 fprintf_unfiltered (f, "signature: %s",
23288 hex_string (die->attrs[i].as_signature ()));
23289 break;
23290 case DW_FORM_string:
23291 case DW_FORM_strp:
23292 case DW_FORM_line_strp:
23293 case DW_FORM_strx:
23294 case DW_FORM_GNU_str_index:
23295 case DW_FORM_GNU_strp_alt:
23296 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23297 die->attrs[i].as_string ()
23298 ? die->attrs[i].as_string () : "",
23299 die->attrs[i].canonical_string_p () ? "is" : "not");
23300 break;
23301 case DW_FORM_flag:
23302 if (die->attrs[i].as_boolean ())
23303 fprintf_unfiltered (f, "flag: TRUE");
23304 else
23305 fprintf_unfiltered (f, "flag: FALSE");
23306 break;
23307 case DW_FORM_flag_present:
23308 fprintf_unfiltered (f, "flag: TRUE");
23309 break;
23310 case DW_FORM_indirect:
23311 /* The reader will have reduced the indirect form to
23312 the "base form" so this form should not occur. */
23313 fprintf_unfiltered (f,
23314 "unexpected attribute form: DW_FORM_indirect");
23315 break;
23316 case DW_FORM_sdata:
23317 case DW_FORM_implicit_const:
23318 fprintf_unfiltered (f, "constant: %s",
23319 plongest (die->attrs[i].as_signed ()));
23320 break;
23321 default:
23322 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23323 die->attrs[i].form);
23324 break;
23325 }
23326 fprintf_unfiltered (f, "\n");
23327 }
23328 }
23329
23330 static void
23331 dump_die_for_error (struct die_info *die)
23332 {
23333 dump_die_shallow (gdb_stderr, 0, die);
23334 }
23335
23336 static void
23337 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23338 {
23339 int indent = level * 4;
23340
23341 gdb_assert (die != NULL);
23342
23343 if (level >= max_level)
23344 return;
23345
23346 dump_die_shallow (f, indent, die);
23347
23348 if (die->child != NULL)
23349 {
23350 print_spaces (indent, f);
23351 fprintf_unfiltered (f, " Children:");
23352 if (level + 1 < max_level)
23353 {
23354 fprintf_unfiltered (f, "\n");
23355 dump_die_1 (f, level + 1, max_level, die->child);
23356 }
23357 else
23358 {
23359 fprintf_unfiltered (f,
23360 " [not printed, max nesting level reached]\n");
23361 }
23362 }
23363
23364 if (die->sibling != NULL && level > 0)
23365 {
23366 dump_die_1 (f, level, max_level, die->sibling);
23367 }
23368 }
23369
23370 /* This is called from the pdie macro in gdbinit.in.
23371 It's not static so gcc will keep a copy callable from gdb. */
23372
23373 void
23374 dump_die (struct die_info *die, int max_level)
23375 {
23376 dump_die_1 (gdb_stdlog, 0, max_level, die);
23377 }
23378
23379 static void
23380 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23381 {
23382 void **slot;
23383
23384 slot = htab_find_slot_with_hash (cu->die_hash, die,
23385 to_underlying (die->sect_off),
23386 INSERT);
23387
23388 *slot = die;
23389 }
23390
23391 /* Follow reference or signature attribute ATTR of SRC_DIE.
23392 On entry *REF_CU is the CU of SRC_DIE.
23393 On exit *REF_CU is the CU of the result. */
23394
23395 static struct die_info *
23396 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23397 struct dwarf2_cu **ref_cu)
23398 {
23399 struct die_info *die;
23400
23401 if (attr->form_is_ref ())
23402 die = follow_die_ref (src_die, attr, ref_cu);
23403 else if (attr->form == DW_FORM_ref_sig8)
23404 die = follow_die_sig (src_die, attr, ref_cu);
23405 else
23406 {
23407 dump_die_for_error (src_die);
23408 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23409 objfile_name ((*ref_cu)->per_objfile->objfile));
23410 }
23411
23412 return die;
23413 }
23414
23415 /* Follow reference OFFSET.
23416 On entry *REF_CU is the CU of the source die referencing OFFSET.
23417 On exit *REF_CU is the CU of the result.
23418 Returns NULL if OFFSET is invalid. */
23419
23420 static struct die_info *
23421 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23422 struct dwarf2_cu **ref_cu)
23423 {
23424 struct die_info temp_die;
23425 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23426 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23427
23428 gdb_assert (cu->per_cu != NULL);
23429
23430 target_cu = cu;
23431
23432 if (cu->per_cu->is_debug_types)
23433 {
23434 /* .debug_types CUs cannot reference anything outside their CU.
23435 If they need to, they have to reference a signatured type via
23436 DW_FORM_ref_sig8. */
23437 if (!cu->header.offset_in_cu_p (sect_off))
23438 return NULL;
23439 }
23440 else if (offset_in_dwz != cu->per_cu->is_dwz
23441 || !cu->header.offset_in_cu_p (sect_off))
23442 {
23443 struct dwarf2_per_cu_data *per_cu;
23444
23445 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23446 per_objfile);
23447
23448 /* If necessary, add it to the queue and load its DIEs. */
23449 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
23450 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23451 false, cu->language);
23452
23453 target_cu = per_objfile->get_cu (per_cu);
23454 }
23455 else if (cu->dies == NULL)
23456 {
23457 /* We're loading full DIEs during partial symbol reading. */
23458 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23459 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23460 language_minimal);
23461 }
23462
23463 *ref_cu = target_cu;
23464 temp_die.sect_off = sect_off;
23465
23466 if (target_cu != cu)
23467 target_cu->ancestor = cu;
23468
23469 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23470 &temp_die,
23471 to_underlying (sect_off));
23472 }
23473
23474 /* Follow reference attribute ATTR of SRC_DIE.
23475 On entry *REF_CU is the CU of SRC_DIE.
23476 On exit *REF_CU is the CU of the result. */
23477
23478 static struct die_info *
23479 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23480 struct dwarf2_cu **ref_cu)
23481 {
23482 sect_offset sect_off = attr->get_ref_die_offset ();
23483 struct dwarf2_cu *cu = *ref_cu;
23484 struct die_info *die;
23485
23486 die = follow_die_offset (sect_off,
23487 (attr->form == DW_FORM_GNU_ref_alt
23488 || cu->per_cu->is_dwz),
23489 ref_cu);
23490 if (!die)
23491 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23492 "at %s [in module %s]"),
23493 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23494 objfile_name (cu->per_objfile->objfile));
23495
23496 return die;
23497 }
23498
23499 /* See read.h. */
23500
23501 struct dwarf2_locexpr_baton
23502 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23503 dwarf2_per_cu_data *per_cu,
23504 dwarf2_per_objfile *per_objfile,
23505 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23506 bool resolve_abstract_p)
23507 {
23508 struct die_info *die;
23509 struct attribute *attr;
23510 struct dwarf2_locexpr_baton retval;
23511 struct objfile *objfile = per_objfile->objfile;
23512
23513 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23514 if (cu == nullptr)
23515 cu = load_cu (per_cu, per_objfile, false);
23516
23517 if (cu == nullptr)
23518 {
23519 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23520 Instead just throw an error, not much else we can do. */
23521 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23522 sect_offset_str (sect_off), objfile_name (objfile));
23523 }
23524
23525 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23526 if (!die)
23527 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23528 sect_offset_str (sect_off), objfile_name (objfile));
23529
23530 attr = dwarf2_attr (die, DW_AT_location, cu);
23531 if (!attr && resolve_abstract_p
23532 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23533 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23534 {
23535 CORE_ADDR pc = get_frame_pc ();
23536 CORE_ADDR baseaddr = objfile->text_section_offset ();
23537 struct gdbarch *gdbarch = objfile->arch ();
23538
23539 for (const auto &cand_off
23540 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23541 {
23542 struct dwarf2_cu *cand_cu = cu;
23543 struct die_info *cand
23544 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23545 if (!cand
23546 || !cand->parent
23547 || cand->parent->tag != DW_TAG_subprogram)
23548 continue;
23549
23550 CORE_ADDR pc_low, pc_high;
23551 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23552 if (pc_low == ((CORE_ADDR) -1))
23553 continue;
23554 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23555 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23556 if (!(pc_low <= pc && pc < pc_high))
23557 continue;
23558
23559 die = cand;
23560 attr = dwarf2_attr (die, DW_AT_location, cu);
23561 break;
23562 }
23563 }
23564
23565 if (!attr)
23566 {
23567 /* DWARF: "If there is no such attribute, then there is no effect.".
23568 DATA is ignored if SIZE is 0. */
23569
23570 retval.data = NULL;
23571 retval.size = 0;
23572 }
23573 else if (attr->form_is_section_offset ())
23574 {
23575 struct dwarf2_loclist_baton loclist_baton;
23576 CORE_ADDR pc = get_frame_pc ();
23577 size_t size;
23578
23579 fill_in_loclist_baton (cu, &loclist_baton, attr);
23580
23581 retval.data = dwarf2_find_location_expression (&loclist_baton,
23582 &size, pc);
23583 retval.size = size;
23584 }
23585 else
23586 {
23587 if (!attr->form_is_block ())
23588 error (_("Dwarf Error: DIE at %s referenced in module %s "
23589 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23590 sect_offset_str (sect_off), objfile_name (objfile));
23591
23592 struct dwarf_block *block = attr->as_block ();
23593 retval.data = block->data;
23594 retval.size = block->size;
23595 }
23596 retval.per_objfile = per_objfile;
23597 retval.per_cu = cu->per_cu;
23598
23599 per_objfile->age_comp_units ();
23600
23601 return retval;
23602 }
23603
23604 /* See read.h. */
23605
23606 struct dwarf2_locexpr_baton
23607 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23608 dwarf2_per_cu_data *per_cu,
23609 dwarf2_per_objfile *per_objfile,
23610 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23611 {
23612 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23613
23614 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23615 get_frame_pc);
23616 }
23617
23618 /* Write a constant of a given type as target-ordered bytes into
23619 OBSTACK. */
23620
23621 static const gdb_byte *
23622 write_constant_as_bytes (struct obstack *obstack,
23623 enum bfd_endian byte_order,
23624 struct type *type,
23625 ULONGEST value,
23626 LONGEST *len)
23627 {
23628 gdb_byte *result;
23629
23630 *len = TYPE_LENGTH (type);
23631 result = (gdb_byte *) obstack_alloc (obstack, *len);
23632 store_unsigned_integer (result, *len, byte_order, value);
23633
23634 return result;
23635 }
23636
23637 /* See read.h. */
23638
23639 const gdb_byte *
23640 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23641 dwarf2_per_cu_data *per_cu,
23642 dwarf2_per_objfile *per_objfile,
23643 obstack *obstack,
23644 LONGEST *len)
23645 {
23646 struct die_info *die;
23647 struct attribute *attr;
23648 const gdb_byte *result = NULL;
23649 struct type *type;
23650 LONGEST value;
23651 enum bfd_endian byte_order;
23652 struct objfile *objfile = per_objfile->objfile;
23653
23654 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23655 if (cu == nullptr)
23656 cu = load_cu (per_cu, per_objfile, false);
23657
23658 if (cu == nullptr)
23659 {
23660 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23661 Instead just throw an error, not much else we can do. */
23662 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23663 sect_offset_str (sect_off), objfile_name (objfile));
23664 }
23665
23666 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23667 if (!die)
23668 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23669 sect_offset_str (sect_off), objfile_name (objfile));
23670
23671 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23672 if (attr == NULL)
23673 return NULL;
23674
23675 byte_order = (bfd_big_endian (objfile->obfd)
23676 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23677
23678 switch (attr->form)
23679 {
23680 case DW_FORM_addr:
23681 case DW_FORM_addrx:
23682 case DW_FORM_GNU_addr_index:
23683 {
23684 gdb_byte *tem;
23685
23686 *len = cu->header.addr_size;
23687 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23688 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23689 result = tem;
23690 }
23691 break;
23692 case DW_FORM_string:
23693 case DW_FORM_strp:
23694 case DW_FORM_strx:
23695 case DW_FORM_GNU_str_index:
23696 case DW_FORM_GNU_strp_alt:
23697 /* The string is already allocated on the objfile obstack, point
23698 directly to it. */
23699 {
23700 const char *attr_name = attr->as_string ();
23701 result = (const gdb_byte *) attr_name;
23702 *len = strlen (attr_name);
23703 }
23704 break;
23705 case DW_FORM_block1:
23706 case DW_FORM_block2:
23707 case DW_FORM_block4:
23708 case DW_FORM_block:
23709 case DW_FORM_exprloc:
23710 case DW_FORM_data16:
23711 {
23712 struct dwarf_block *block = attr->as_block ();
23713 result = block->data;
23714 *len = block->size;
23715 }
23716 break;
23717
23718 /* The DW_AT_const_value attributes are supposed to carry the
23719 symbol's value "represented as it would be on the target
23720 architecture." By the time we get here, it's already been
23721 converted to host endianness, so we just need to sign- or
23722 zero-extend it as appropriate. */
23723 case DW_FORM_data1:
23724 type = die_type (die, cu);
23725 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23726 if (result == NULL)
23727 result = write_constant_as_bytes (obstack, byte_order,
23728 type, value, len);
23729 break;
23730 case DW_FORM_data2:
23731 type = die_type (die, cu);
23732 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23733 if (result == NULL)
23734 result = write_constant_as_bytes (obstack, byte_order,
23735 type, value, len);
23736 break;
23737 case DW_FORM_data4:
23738 type = die_type (die, cu);
23739 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23740 if (result == NULL)
23741 result = write_constant_as_bytes (obstack, byte_order,
23742 type, value, len);
23743 break;
23744 case DW_FORM_data8:
23745 type = die_type (die, cu);
23746 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23747 if (result == NULL)
23748 result = write_constant_as_bytes (obstack, byte_order,
23749 type, value, len);
23750 break;
23751
23752 case DW_FORM_sdata:
23753 case DW_FORM_implicit_const:
23754 type = die_type (die, cu);
23755 result = write_constant_as_bytes (obstack, byte_order,
23756 type, attr->as_signed (), len);
23757 break;
23758
23759 case DW_FORM_udata:
23760 type = die_type (die, cu);
23761 result = write_constant_as_bytes (obstack, byte_order,
23762 type, attr->as_unsigned (), len);
23763 break;
23764
23765 default:
23766 complaint (_("unsupported const value attribute form: '%s'"),
23767 dwarf_form_name (attr->form));
23768 break;
23769 }
23770
23771 return result;
23772 }
23773
23774 /* See read.h. */
23775
23776 struct type *
23777 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23778 dwarf2_per_cu_data *per_cu,
23779 dwarf2_per_objfile *per_objfile)
23780 {
23781 struct die_info *die;
23782
23783 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23784 if (cu == nullptr)
23785 cu = load_cu (per_cu, per_objfile, false);
23786
23787 if (cu == nullptr)
23788 return nullptr;
23789
23790 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23791 if (!die)
23792 return NULL;
23793
23794 return die_type (die, cu);
23795 }
23796
23797 /* See read.h. */
23798
23799 struct type *
23800 dwarf2_get_die_type (cu_offset die_offset,
23801 dwarf2_per_cu_data *per_cu,
23802 dwarf2_per_objfile *per_objfile)
23803 {
23804 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23805 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
23806 }
23807
23808 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23809 On entry *REF_CU is the CU of SRC_DIE.
23810 On exit *REF_CU is the CU of the result.
23811 Returns NULL if the referenced DIE isn't found. */
23812
23813 static struct die_info *
23814 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23815 struct dwarf2_cu **ref_cu)
23816 {
23817 struct die_info temp_die;
23818 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23819 struct die_info *die;
23820 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
23821
23822
23823 /* While it might be nice to assert sig_type->type == NULL here,
23824 we can get here for DW_AT_imported_declaration where we need
23825 the DIE not the type. */
23826
23827 /* If necessary, add it to the queue and load its DIEs. */
23828
23829 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
23830 language_minimal))
23831 read_signatured_type (sig_type, per_objfile);
23832
23833 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
23834 gdb_assert (sig_cu != NULL);
23835 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23836 temp_die.sect_off = sig_type->type_offset_in_section;
23837 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23838 to_underlying (temp_die.sect_off));
23839 if (die)
23840 {
23841 /* For .gdb_index version 7 keep track of included TUs.
23842 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23843 if (per_objfile->per_bfd->index_table != NULL
23844 && per_objfile->per_bfd->index_table->version <= 7)
23845 {
23846 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23847 }
23848
23849 *ref_cu = sig_cu;
23850 if (sig_cu != cu)
23851 sig_cu->ancestor = cu;
23852
23853 return die;
23854 }
23855
23856 return NULL;
23857 }
23858
23859 /* Follow signatured type referenced by ATTR in SRC_DIE.
23860 On entry *REF_CU is the CU of SRC_DIE.
23861 On exit *REF_CU is the CU of the result.
23862 The result is the DIE of the type.
23863 If the referenced type cannot be found an error is thrown. */
23864
23865 static struct die_info *
23866 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23867 struct dwarf2_cu **ref_cu)
23868 {
23869 ULONGEST signature = attr->as_signature ();
23870 struct signatured_type *sig_type;
23871 struct die_info *die;
23872
23873 gdb_assert (attr->form == DW_FORM_ref_sig8);
23874
23875 sig_type = lookup_signatured_type (*ref_cu, signature);
23876 /* sig_type will be NULL if the signatured type is missing from
23877 the debug info. */
23878 if (sig_type == NULL)
23879 {
23880 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23881 " from DIE at %s [in module %s]"),
23882 hex_string (signature), sect_offset_str (src_die->sect_off),
23883 objfile_name ((*ref_cu)->per_objfile->objfile));
23884 }
23885
23886 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23887 if (die == NULL)
23888 {
23889 dump_die_for_error (src_die);
23890 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23891 " from DIE at %s [in module %s]"),
23892 hex_string (signature), sect_offset_str (src_die->sect_off),
23893 objfile_name ((*ref_cu)->per_objfile->objfile));
23894 }
23895
23896 return die;
23897 }
23898
23899 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23900 reading in and processing the type unit if necessary. */
23901
23902 static struct type *
23903 get_signatured_type (struct die_info *die, ULONGEST signature,
23904 struct dwarf2_cu *cu)
23905 {
23906 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23907 struct signatured_type *sig_type;
23908 struct dwarf2_cu *type_cu;
23909 struct die_info *type_die;
23910 struct type *type;
23911
23912 sig_type = lookup_signatured_type (cu, signature);
23913 /* sig_type will be NULL if the signatured type is missing from
23914 the debug info. */
23915 if (sig_type == NULL)
23916 {
23917 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23918 " from DIE at %s [in module %s]"),
23919 hex_string (signature), sect_offset_str (die->sect_off),
23920 objfile_name (per_objfile->objfile));
23921 return build_error_marker_type (cu, die);
23922 }
23923
23924 /* If we already know the type we're done. */
23925 type = per_objfile->get_type_for_signatured_type (sig_type);
23926 if (type != nullptr)
23927 return type;
23928
23929 type_cu = cu;
23930 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23931 if (type_die != NULL)
23932 {
23933 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23934 is created. This is important, for example, because for c++ classes
23935 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23936 type = read_type_die (type_die, type_cu);
23937 if (type == NULL)
23938 {
23939 complaint (_("Dwarf Error: Cannot build signatured type %s"
23940 " referenced from DIE at %s [in module %s]"),
23941 hex_string (signature), sect_offset_str (die->sect_off),
23942 objfile_name (per_objfile->objfile));
23943 type = build_error_marker_type (cu, die);
23944 }
23945 }
23946 else
23947 {
23948 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23949 " from DIE at %s [in module %s]"),
23950 hex_string (signature), sect_offset_str (die->sect_off),
23951 objfile_name (per_objfile->objfile));
23952 type = build_error_marker_type (cu, die);
23953 }
23954
23955 per_objfile->set_type_for_signatured_type (sig_type, type);
23956
23957 return type;
23958 }
23959
23960 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23961 reading in and processing the type unit if necessary. */
23962
23963 static struct type *
23964 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23965 struct dwarf2_cu *cu) /* ARI: editCase function */
23966 {
23967 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23968 if (attr->form_is_ref ())
23969 {
23970 struct dwarf2_cu *type_cu = cu;
23971 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23972
23973 return read_type_die (type_die, type_cu);
23974 }
23975 else if (attr->form == DW_FORM_ref_sig8)
23976 {
23977 return get_signatured_type (die, attr->as_signature (), cu);
23978 }
23979 else
23980 {
23981 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23982
23983 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23984 " at %s [in module %s]"),
23985 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23986 objfile_name (per_objfile->objfile));
23987 return build_error_marker_type (cu, die);
23988 }
23989 }
23990
23991 /* Load the DIEs associated with type unit PER_CU into memory. */
23992
23993 static void
23994 load_full_type_unit (dwarf2_per_cu_data *per_cu,
23995 dwarf2_per_objfile *per_objfile)
23996 {
23997 struct signatured_type *sig_type;
23998
23999 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24000 gdb_assert (! per_cu->type_unit_group_p ());
24001
24002 /* We have the per_cu, but we need the signatured_type.
24003 Fortunately this is an easy translation. */
24004 gdb_assert (per_cu->is_debug_types);
24005 sig_type = (struct signatured_type *) per_cu;
24006
24007 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24008
24009 read_signatured_type (sig_type, per_objfile);
24010
24011 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
24012 }
24013
24014 /* Read in a signatured type and build its CU and DIEs.
24015 If the type is a stub for the real type in a DWO file,
24016 read in the real type from the DWO file as well. */
24017
24018 static void
24019 read_signatured_type (signatured_type *sig_type,
24020 dwarf2_per_objfile *per_objfile)
24021 {
24022 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24023
24024 gdb_assert (per_cu->is_debug_types);
24025 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24026
24027 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
24028
24029 if (!reader.dummy_p)
24030 {
24031 struct dwarf2_cu *cu = reader.cu;
24032 const gdb_byte *info_ptr = reader.info_ptr;
24033
24034 gdb_assert (cu->die_hash == NULL);
24035 cu->die_hash =
24036 htab_create_alloc_ex (cu->header.length / 12,
24037 die_hash,
24038 die_eq,
24039 NULL,
24040 &cu->comp_unit_obstack,
24041 hashtab_obstack_allocate,
24042 dummy_obstack_deallocate);
24043
24044 if (reader.comp_unit_die->has_children)
24045 reader.comp_unit_die->child
24046 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24047 reader.comp_unit_die);
24048 cu->dies = reader.comp_unit_die;
24049 /* comp_unit_die is not stored in die_hash, no need. */
24050
24051 /* We try not to read any attributes in this function, because
24052 not all CUs needed for references have been loaded yet, and
24053 symbol table processing isn't initialized. But we have to
24054 set the CU language, or we won't be able to build types
24055 correctly. Similarly, if we do not read the producer, we can
24056 not apply producer-specific interpretation. */
24057 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24058
24059 reader.keep ();
24060 }
24061
24062 sig_type->per_cu.tu_read = 1;
24063 }
24064
24065 /* Decode simple location descriptions.
24066 Given a pointer to a dwarf block that defines a location, compute
24067 the location and return the value. If COMPUTED is non-null, it is
24068 set to true to indicate that decoding was successful, and false
24069 otherwise. If COMPUTED is null, then this function may emit a
24070 complaint. */
24071
24072 static CORE_ADDR
24073 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
24074 {
24075 struct objfile *objfile = cu->per_objfile->objfile;
24076 size_t i;
24077 size_t size = blk->size;
24078 const gdb_byte *data = blk->data;
24079 CORE_ADDR stack[64];
24080 int stacki;
24081 unsigned int bytes_read, unsnd;
24082 gdb_byte op;
24083
24084 if (computed != nullptr)
24085 *computed = false;
24086
24087 i = 0;
24088 stacki = 0;
24089 stack[stacki] = 0;
24090 stack[++stacki] = 0;
24091
24092 while (i < size)
24093 {
24094 op = data[i++];
24095 switch (op)
24096 {
24097 case DW_OP_lit0:
24098 case DW_OP_lit1:
24099 case DW_OP_lit2:
24100 case DW_OP_lit3:
24101 case DW_OP_lit4:
24102 case DW_OP_lit5:
24103 case DW_OP_lit6:
24104 case DW_OP_lit7:
24105 case DW_OP_lit8:
24106 case DW_OP_lit9:
24107 case DW_OP_lit10:
24108 case DW_OP_lit11:
24109 case DW_OP_lit12:
24110 case DW_OP_lit13:
24111 case DW_OP_lit14:
24112 case DW_OP_lit15:
24113 case DW_OP_lit16:
24114 case DW_OP_lit17:
24115 case DW_OP_lit18:
24116 case DW_OP_lit19:
24117 case DW_OP_lit20:
24118 case DW_OP_lit21:
24119 case DW_OP_lit22:
24120 case DW_OP_lit23:
24121 case DW_OP_lit24:
24122 case DW_OP_lit25:
24123 case DW_OP_lit26:
24124 case DW_OP_lit27:
24125 case DW_OP_lit28:
24126 case DW_OP_lit29:
24127 case DW_OP_lit30:
24128 case DW_OP_lit31:
24129 stack[++stacki] = op - DW_OP_lit0;
24130 break;
24131
24132 case DW_OP_reg0:
24133 case DW_OP_reg1:
24134 case DW_OP_reg2:
24135 case DW_OP_reg3:
24136 case DW_OP_reg4:
24137 case DW_OP_reg5:
24138 case DW_OP_reg6:
24139 case DW_OP_reg7:
24140 case DW_OP_reg8:
24141 case DW_OP_reg9:
24142 case DW_OP_reg10:
24143 case DW_OP_reg11:
24144 case DW_OP_reg12:
24145 case DW_OP_reg13:
24146 case DW_OP_reg14:
24147 case DW_OP_reg15:
24148 case DW_OP_reg16:
24149 case DW_OP_reg17:
24150 case DW_OP_reg18:
24151 case DW_OP_reg19:
24152 case DW_OP_reg20:
24153 case DW_OP_reg21:
24154 case DW_OP_reg22:
24155 case DW_OP_reg23:
24156 case DW_OP_reg24:
24157 case DW_OP_reg25:
24158 case DW_OP_reg26:
24159 case DW_OP_reg27:
24160 case DW_OP_reg28:
24161 case DW_OP_reg29:
24162 case DW_OP_reg30:
24163 case DW_OP_reg31:
24164 stack[++stacki] = op - DW_OP_reg0;
24165 if (i < size)
24166 {
24167 if (computed == nullptr)
24168 dwarf2_complex_location_expr_complaint ();
24169 else
24170 return 0;
24171 }
24172 break;
24173
24174 case DW_OP_regx:
24175 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24176 i += bytes_read;
24177 stack[++stacki] = unsnd;
24178 if (i < size)
24179 {
24180 if (computed == nullptr)
24181 dwarf2_complex_location_expr_complaint ();
24182 else
24183 return 0;
24184 }
24185 break;
24186
24187 case DW_OP_addr:
24188 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24189 &bytes_read);
24190 i += bytes_read;
24191 break;
24192
24193 case DW_OP_const1u:
24194 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24195 i += 1;
24196 break;
24197
24198 case DW_OP_const1s:
24199 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24200 i += 1;
24201 break;
24202
24203 case DW_OP_const2u:
24204 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24205 i += 2;
24206 break;
24207
24208 case DW_OP_const2s:
24209 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24210 i += 2;
24211 break;
24212
24213 case DW_OP_const4u:
24214 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24215 i += 4;
24216 break;
24217
24218 case DW_OP_const4s:
24219 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24220 i += 4;
24221 break;
24222
24223 case DW_OP_const8u:
24224 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24225 i += 8;
24226 break;
24227
24228 case DW_OP_constu:
24229 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24230 &bytes_read);
24231 i += bytes_read;
24232 break;
24233
24234 case DW_OP_consts:
24235 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24236 i += bytes_read;
24237 break;
24238
24239 case DW_OP_dup:
24240 stack[stacki + 1] = stack[stacki];
24241 stacki++;
24242 break;
24243
24244 case DW_OP_plus:
24245 stack[stacki - 1] += stack[stacki];
24246 stacki--;
24247 break;
24248
24249 case DW_OP_plus_uconst:
24250 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24251 &bytes_read);
24252 i += bytes_read;
24253 break;
24254
24255 case DW_OP_minus:
24256 stack[stacki - 1] -= stack[stacki];
24257 stacki--;
24258 break;
24259
24260 case DW_OP_deref:
24261 /* If we're not the last op, then we definitely can't encode
24262 this using GDB's address_class enum. This is valid for partial
24263 global symbols, although the variable's address will be bogus
24264 in the psymtab. */
24265 if (i < size)
24266 {
24267 if (computed == nullptr)
24268 dwarf2_complex_location_expr_complaint ();
24269 else
24270 return 0;
24271 }
24272 break;
24273
24274 case DW_OP_GNU_push_tls_address:
24275 case DW_OP_form_tls_address:
24276 /* The top of the stack has the offset from the beginning
24277 of the thread control block at which the variable is located. */
24278 /* Nothing should follow this operator, so the top of stack would
24279 be returned. */
24280 /* This is valid for partial global symbols, but the variable's
24281 address will be bogus in the psymtab. Make it always at least
24282 non-zero to not look as a variable garbage collected by linker
24283 which have DW_OP_addr 0. */
24284 if (i < size)
24285 {
24286 if (computed == nullptr)
24287 dwarf2_complex_location_expr_complaint ();
24288 else
24289 return 0;
24290 }
24291 stack[stacki]++;
24292 break;
24293
24294 case DW_OP_GNU_uninit:
24295 if (computed != nullptr)
24296 return 0;
24297 break;
24298
24299 case DW_OP_addrx:
24300 case DW_OP_GNU_addr_index:
24301 case DW_OP_GNU_const_index:
24302 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24303 &bytes_read);
24304 i += bytes_read;
24305 break;
24306
24307 default:
24308 if (computed == nullptr)
24309 {
24310 const char *name = get_DW_OP_name (op);
24311
24312 if (name)
24313 complaint (_("unsupported stack op: '%s'"),
24314 name);
24315 else
24316 complaint (_("unsupported stack op: '%02x'"),
24317 op);
24318 }
24319
24320 return (stack[stacki]);
24321 }
24322
24323 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24324 outside of the allocated space. Also enforce minimum>0. */
24325 if (stacki >= ARRAY_SIZE (stack) - 1)
24326 {
24327 if (computed == nullptr)
24328 complaint (_("location description stack overflow"));
24329 return 0;
24330 }
24331
24332 if (stacki <= 0)
24333 {
24334 if (computed == nullptr)
24335 complaint (_("location description stack underflow"));
24336 return 0;
24337 }
24338 }
24339
24340 if (computed != nullptr)
24341 *computed = true;
24342 return (stack[stacki]);
24343 }
24344
24345 /* memory allocation interface */
24346
24347 static struct dwarf_block *
24348 dwarf_alloc_block (struct dwarf2_cu *cu)
24349 {
24350 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24351 }
24352
24353 static struct die_info *
24354 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24355 {
24356 struct die_info *die;
24357 size_t size = sizeof (struct die_info);
24358
24359 if (num_attrs > 1)
24360 size += (num_attrs - 1) * sizeof (struct attribute);
24361
24362 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24363 memset (die, 0, sizeof (struct die_info));
24364 return (die);
24365 }
24366
24367 \f
24368
24369 /* Macro support. */
24370
24371 /* An overload of dwarf_decode_macros that finds the correct section
24372 and ensures it is read in before calling the other overload. */
24373
24374 static void
24375 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24376 int section_is_gnu)
24377 {
24378 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24379 struct objfile *objfile = per_objfile->objfile;
24380 const struct line_header *lh = cu->line_header;
24381 unsigned int offset_size = cu->header.offset_size;
24382 struct dwarf2_section_info *section;
24383 const char *section_name;
24384
24385 if (cu->dwo_unit != nullptr)
24386 {
24387 if (section_is_gnu)
24388 {
24389 section = &cu->dwo_unit->dwo_file->sections.macro;
24390 section_name = ".debug_macro.dwo";
24391 }
24392 else
24393 {
24394 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24395 section_name = ".debug_macinfo.dwo";
24396 }
24397 }
24398 else
24399 {
24400 if (section_is_gnu)
24401 {
24402 section = &per_objfile->per_bfd->macro;
24403 section_name = ".debug_macro";
24404 }
24405 else
24406 {
24407 section = &per_objfile->per_bfd->macinfo;
24408 section_name = ".debug_macinfo";
24409 }
24410 }
24411
24412 section->read (objfile);
24413 if (section->buffer == nullptr)
24414 {
24415 complaint (_("missing %s section"), section_name);
24416 return;
24417 }
24418
24419 buildsym_compunit *builder = cu->get_builder ();
24420
24421 struct dwarf2_section_info *str_offsets_section;
24422 struct dwarf2_section_info *str_section;
24423 ULONGEST str_offsets_base;
24424
24425 if (cu->dwo_unit != nullptr)
24426 {
24427 str_offsets_section = &cu->dwo_unit->dwo_file
24428 ->sections.str_offsets;
24429 str_section = &cu->dwo_unit->dwo_file->sections.str;
24430 str_offsets_base = cu->header.addr_size;
24431 }
24432 else
24433 {
24434 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24435 str_section = &per_objfile->per_bfd->str;
24436 str_offsets_base = *cu->str_offsets_base;
24437 }
24438
24439 dwarf_decode_macros (per_objfile, builder, section, lh,
24440 offset_size, offset, str_section, str_offsets_section,
24441 str_offsets_base, section_is_gnu);
24442 }
24443
24444 /* Return the .debug_loc section to use for CU.
24445 For DWO files use .debug_loc.dwo. */
24446
24447 static struct dwarf2_section_info *
24448 cu_debug_loc_section (struct dwarf2_cu *cu)
24449 {
24450 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24451
24452 if (cu->dwo_unit)
24453 {
24454 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24455
24456 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24457 }
24458 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24459 : &per_objfile->per_bfd->loc);
24460 }
24461
24462 /* Return the .debug_rnglists section to use for CU. */
24463 static struct dwarf2_section_info *
24464 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24465 {
24466 if (cu->header.version < 5)
24467 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24468 cu->header.version);
24469 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24470
24471 /* Make sure we read the .debug_rnglists section from the file that
24472 contains the DW_AT_ranges attribute we are reading. Normally that
24473 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24474 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24475 program. */
24476 if (cu->dwo_unit != nullptr
24477 && tag != DW_TAG_compile_unit
24478 && tag != DW_TAG_skeleton_unit)
24479 {
24480 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24481
24482 if (sections->rnglists.size > 0)
24483 return &sections->rnglists;
24484 else
24485 error (_(".debug_rnglists section is missing from .dwo file."));
24486 }
24487 return &dwarf2_per_objfile->per_bfd->rnglists;
24488 }
24489
24490 /* A helper function that fills in a dwarf2_loclist_baton. */
24491
24492 static void
24493 fill_in_loclist_baton (struct dwarf2_cu *cu,
24494 struct dwarf2_loclist_baton *baton,
24495 const struct attribute *attr)
24496 {
24497 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24498 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24499
24500 section->read (per_objfile->objfile);
24501
24502 baton->per_objfile = per_objfile;
24503 baton->per_cu = cu->per_cu;
24504 gdb_assert (baton->per_cu);
24505 /* We don't know how long the location list is, but make sure we
24506 don't run off the edge of the section. */
24507 baton->size = section->size - attr->as_unsigned ();
24508 baton->data = section->buffer + attr->as_unsigned ();
24509 if (cu->base_address.has_value ())
24510 baton->base_address = *cu->base_address;
24511 else
24512 baton->base_address = 0;
24513 baton->from_dwo = cu->dwo_unit != NULL;
24514 }
24515
24516 static void
24517 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24518 struct dwarf2_cu *cu, int is_block)
24519 {
24520 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24521 struct objfile *objfile = per_objfile->objfile;
24522 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24523
24524 if (attr->form_is_section_offset ()
24525 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24526 the section. If so, fall through to the complaint in the
24527 other branch. */
24528 && attr->as_unsigned () < section->get_size (objfile))
24529 {
24530 struct dwarf2_loclist_baton *baton;
24531
24532 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24533
24534 fill_in_loclist_baton (cu, baton, attr);
24535
24536 if (!cu->base_address.has_value ())
24537 complaint (_("Location list used without "
24538 "specifying the CU base address."));
24539
24540 SYMBOL_ACLASS_INDEX (sym) = (is_block
24541 ? dwarf2_loclist_block_index
24542 : dwarf2_loclist_index);
24543 SYMBOL_LOCATION_BATON (sym) = baton;
24544 }
24545 else
24546 {
24547 struct dwarf2_locexpr_baton *baton;
24548
24549 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24550 baton->per_objfile = per_objfile;
24551 baton->per_cu = cu->per_cu;
24552 gdb_assert (baton->per_cu);
24553
24554 if (attr->form_is_block ())
24555 {
24556 /* Note that we're just copying the block's data pointer
24557 here, not the actual data. We're still pointing into the
24558 info_buffer for SYM's objfile; right now we never release
24559 that buffer, but when we do clean up properly this may
24560 need to change. */
24561 struct dwarf_block *block = attr->as_block ();
24562 baton->size = block->size;
24563 baton->data = block->data;
24564 }
24565 else
24566 {
24567 dwarf2_invalid_attrib_class_complaint ("location description",
24568 sym->natural_name ());
24569 baton->size = 0;
24570 }
24571
24572 SYMBOL_ACLASS_INDEX (sym) = (is_block
24573 ? dwarf2_locexpr_block_index
24574 : dwarf2_locexpr_index);
24575 SYMBOL_LOCATION_BATON (sym) = baton;
24576 }
24577 }
24578
24579 /* See read.h. */
24580
24581 const comp_unit_head *
24582 dwarf2_per_cu_data::get_header () const
24583 {
24584 if (!m_header_read_in)
24585 {
24586 const gdb_byte *info_ptr
24587 = this->section->buffer + to_underlying (this->sect_off);
24588
24589 memset (&m_header, 0, sizeof (m_header));
24590
24591 read_comp_unit_head (&m_header, info_ptr, this->section,
24592 rcuh_kind::COMPILE);
24593 }
24594
24595 return &m_header;
24596 }
24597
24598 /* See read.h. */
24599
24600 int
24601 dwarf2_per_cu_data::addr_size () const
24602 {
24603 return this->get_header ()->addr_size;
24604 }
24605
24606 /* See read.h. */
24607
24608 int
24609 dwarf2_per_cu_data::offset_size () const
24610 {
24611 return this->get_header ()->offset_size;
24612 }
24613
24614 /* See read.h. */
24615
24616 int
24617 dwarf2_per_cu_data::ref_addr_size () const
24618 {
24619 const comp_unit_head *header = this->get_header ();
24620
24621 if (header->version == 2)
24622 return header->addr_size;
24623 else
24624 return header->offset_size;
24625 }
24626
24627 /* See read.h. */
24628
24629 struct type *
24630 dwarf2_cu::addr_type () const
24631 {
24632 struct objfile *objfile = this->per_objfile->objfile;
24633 struct type *void_type = objfile_type (objfile)->builtin_void;
24634 struct type *addr_type = lookup_pointer_type (void_type);
24635 int addr_size = this->per_cu->addr_size ();
24636
24637 if (TYPE_LENGTH (addr_type) == addr_size)
24638 return addr_type;
24639
24640 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
24641 return addr_type;
24642 }
24643
24644 /* A helper function for dwarf2_find_containing_comp_unit that returns
24645 the index of the result, and that searches a vector. It will
24646 return a result even if the offset in question does not actually
24647 occur in any CU. This is separate so that it can be unit
24648 tested. */
24649
24650 static int
24651 dwarf2_find_containing_comp_unit
24652 (sect_offset sect_off,
24653 unsigned int offset_in_dwz,
24654 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
24655 {
24656 int low, high;
24657
24658 low = 0;
24659 high = all_comp_units.size () - 1;
24660 while (high > low)
24661 {
24662 struct dwarf2_per_cu_data *mid_cu;
24663 int mid = low + (high - low) / 2;
24664
24665 mid_cu = all_comp_units[mid];
24666 if (mid_cu->is_dwz > offset_in_dwz
24667 || (mid_cu->is_dwz == offset_in_dwz
24668 && mid_cu->sect_off + mid_cu->length > sect_off))
24669 high = mid;
24670 else
24671 low = mid + 1;
24672 }
24673 gdb_assert (low == high);
24674 return low;
24675 }
24676
24677 /* Locate the .debug_info compilation unit from CU's objfile which contains
24678 the DIE at OFFSET. Raises an error on failure. */
24679
24680 static struct dwarf2_per_cu_data *
24681 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24682 unsigned int offset_in_dwz,
24683 dwarf2_per_objfile *per_objfile)
24684 {
24685 int low = dwarf2_find_containing_comp_unit
24686 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24687 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
24688
24689 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24690 {
24691 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24692 error (_("Dwarf Error: could not find partial DIE containing "
24693 "offset %s [in module %s]"),
24694 sect_offset_str (sect_off),
24695 bfd_get_filename (per_objfile->objfile->obfd));
24696
24697 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
24698 <= sect_off);
24699 return per_objfile->per_bfd->all_comp_units[low-1];
24700 }
24701 else
24702 {
24703 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
24704 && sect_off >= this_cu->sect_off + this_cu->length)
24705 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24706 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24707 return this_cu;
24708 }
24709 }
24710
24711 #if GDB_SELF_TEST
24712
24713 namespace selftests {
24714 namespace find_containing_comp_unit {
24715
24716 static void
24717 run_test ()
24718 {
24719 struct dwarf2_per_cu_data one {};
24720 struct dwarf2_per_cu_data two {};
24721 struct dwarf2_per_cu_data three {};
24722 struct dwarf2_per_cu_data four {};
24723
24724 one.length = 5;
24725 two.sect_off = sect_offset (one.length);
24726 two.length = 7;
24727
24728 three.length = 5;
24729 three.is_dwz = 1;
24730 four.sect_off = sect_offset (three.length);
24731 four.length = 7;
24732 four.is_dwz = 1;
24733
24734 std::vector<dwarf2_per_cu_data *> units;
24735 units.push_back (&one);
24736 units.push_back (&two);
24737 units.push_back (&three);
24738 units.push_back (&four);
24739
24740 int result;
24741
24742 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24743 SELF_CHECK (units[result] == &one);
24744 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24745 SELF_CHECK (units[result] == &one);
24746 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24747 SELF_CHECK (units[result] == &two);
24748
24749 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24750 SELF_CHECK (units[result] == &three);
24751 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24752 SELF_CHECK (units[result] == &three);
24753 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24754 SELF_CHECK (units[result] == &four);
24755 }
24756
24757 }
24758 }
24759
24760 #endif /* GDB_SELF_TEST */
24761
24762 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
24763
24764 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
24765 dwarf2_per_objfile *per_objfile)
24766 : per_cu (per_cu),
24767 per_objfile (per_objfile),
24768 mark (false),
24769 has_loclist (false),
24770 checked_producer (false),
24771 producer_is_gxx_lt_4_6 (false),
24772 producer_is_gcc_lt_4_3 (false),
24773 producer_is_icc (false),
24774 producer_is_icc_lt_14 (false),
24775 producer_is_codewarrior (false),
24776 processing_has_namespace_info (false)
24777 {
24778 }
24779
24780 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24781
24782 static void
24783 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24784 enum language pretend_language)
24785 {
24786 struct attribute *attr;
24787
24788 /* Set the language we're debugging. */
24789 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24790 if (attr != nullptr)
24791 set_cu_language (attr->constant_value (0), cu);
24792 else
24793 {
24794 cu->language = pretend_language;
24795 cu->language_defn = language_def (cu->language);
24796 }
24797
24798 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24799 }
24800
24801 /* See read.h. */
24802
24803 dwarf2_cu *
24804 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
24805 {
24806 auto it = m_dwarf2_cus.find (per_cu);
24807 if (it == m_dwarf2_cus.end ())
24808 return nullptr;
24809
24810 return it->second;
24811 }
24812
24813 /* See read.h. */
24814
24815 void
24816 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24817 {
24818 gdb_assert (this->get_cu (per_cu) == nullptr);
24819
24820 m_dwarf2_cus[per_cu] = cu;
24821 }
24822
24823 /* See read.h. */
24824
24825 void
24826 dwarf2_per_objfile::age_comp_units ()
24827 {
24828 /* Start by clearing all marks. */
24829 for (auto pair : m_dwarf2_cus)
24830 pair.second->mark = false;
24831
24832 /* Traverse all CUs, mark them and their dependencies if used recently
24833 enough. */
24834 for (auto pair : m_dwarf2_cus)
24835 {
24836 dwarf2_cu *cu = pair.second;
24837
24838 cu->last_used++;
24839 if (cu->last_used <= dwarf_max_cache_age)
24840 dwarf2_mark (cu);
24841 }
24842
24843 /* Delete all CUs still not marked. */
24844 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
24845 {
24846 dwarf2_cu *cu = it->second;
24847
24848 if (!cu->mark)
24849 {
24850 delete cu;
24851 it = m_dwarf2_cus.erase (it);
24852 }
24853 else
24854 it++;
24855 }
24856 }
24857
24858 /* See read.h. */
24859
24860 void
24861 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
24862 {
24863 auto it = m_dwarf2_cus.find (per_cu);
24864 if (it == m_dwarf2_cus.end ())
24865 return;
24866
24867 delete it->second;
24868
24869 m_dwarf2_cus.erase (it);
24870 }
24871
24872 dwarf2_per_objfile::~dwarf2_per_objfile ()
24873 {
24874 remove_all_cus ();
24875 }
24876
24877 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24878 We store these in a hash table separate from the DIEs, and preserve them
24879 when the DIEs are flushed out of cache.
24880
24881 The CU "per_cu" pointer is needed because offset alone is not enough to
24882 uniquely identify the type. A file may have multiple .debug_types sections,
24883 or the type may come from a DWO file. Furthermore, while it's more logical
24884 to use per_cu->section+offset, with Fission the section with the data is in
24885 the DWO file but we don't know that section at the point we need it.
24886 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24887 because we can enter the lookup routine, get_die_type_at_offset, from
24888 outside this file, and thus won't necessarily have PER_CU->cu.
24889 Fortunately, PER_CU is stable for the life of the objfile. */
24890
24891 struct dwarf2_per_cu_offset_and_type
24892 {
24893 const struct dwarf2_per_cu_data *per_cu;
24894 sect_offset sect_off;
24895 struct type *type;
24896 };
24897
24898 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24899
24900 static hashval_t
24901 per_cu_offset_and_type_hash (const void *item)
24902 {
24903 const struct dwarf2_per_cu_offset_and_type *ofs
24904 = (const struct dwarf2_per_cu_offset_and_type *) item;
24905
24906 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24907 }
24908
24909 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24910
24911 static int
24912 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24913 {
24914 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24915 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24916 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24917 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24918
24919 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24920 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24921 }
24922
24923 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24924 table if necessary. For convenience, return TYPE.
24925
24926 The DIEs reading must have careful ordering to:
24927 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24928 reading current DIE.
24929 * Not trying to dereference contents of still incompletely read in types
24930 while reading in other DIEs.
24931 * Enable referencing still incompletely read in types just by a pointer to
24932 the type without accessing its fields.
24933
24934 Therefore caller should follow these rules:
24935 * Try to fetch any prerequisite types we may need to build this DIE type
24936 before building the type and calling set_die_type.
24937 * After building type call set_die_type for current DIE as soon as
24938 possible before fetching more types to complete the current type.
24939 * Make the type as complete as possible before fetching more types. */
24940
24941 static struct type *
24942 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24943 bool skip_data_location)
24944 {
24945 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24946 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24947 struct objfile *objfile = per_objfile->objfile;
24948 struct attribute *attr;
24949 struct dynamic_prop prop;
24950
24951 /* For Ada types, make sure that the gnat-specific data is always
24952 initialized (if not already set). There are a few types where
24953 we should not be doing so, because the type-specific area is
24954 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24955 where the type-specific area is used to store the floatformat).
24956 But this is not a problem, because the gnat-specific information
24957 is actually not needed for these types. */
24958 if (need_gnat_info (cu)
24959 && type->code () != TYPE_CODE_FUNC
24960 && type->code () != TYPE_CODE_FLT
24961 && type->code () != TYPE_CODE_METHODPTR
24962 && type->code () != TYPE_CODE_MEMBERPTR
24963 && type->code () != TYPE_CODE_METHOD
24964 && type->code () != TYPE_CODE_FIXED_POINT
24965 && !HAVE_GNAT_AUX_INFO (type))
24966 INIT_GNAT_SPECIFIC (type);
24967
24968 /* Read DW_AT_allocated and set in type. */
24969 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24970 if (attr != NULL)
24971 {
24972 struct type *prop_type = cu->addr_sized_int_type (false);
24973 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24974 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
24975 }
24976
24977 /* Read DW_AT_associated and set in type. */
24978 attr = dwarf2_attr (die, DW_AT_associated, cu);
24979 if (attr != NULL)
24980 {
24981 struct type *prop_type = cu->addr_sized_int_type (false);
24982 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24983 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
24984 }
24985
24986 /* Read DW_AT_data_location and set in type. */
24987 if (!skip_data_location)
24988 {
24989 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24990 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24991 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24992 }
24993
24994 if (per_objfile->die_type_hash == NULL)
24995 per_objfile->die_type_hash
24996 = htab_up (htab_create_alloc (127,
24997 per_cu_offset_and_type_hash,
24998 per_cu_offset_and_type_eq,
24999 NULL, xcalloc, xfree));
25000
25001 ofs.per_cu = cu->per_cu;
25002 ofs.sect_off = die->sect_off;
25003 ofs.type = type;
25004 slot = (struct dwarf2_per_cu_offset_and_type **)
25005 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
25006 if (*slot)
25007 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25008 sect_offset_str (die->sect_off));
25009 *slot = XOBNEW (&objfile->objfile_obstack,
25010 struct dwarf2_per_cu_offset_and_type);
25011 **slot = ofs;
25012 return type;
25013 }
25014
25015 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25016 or return NULL if the die does not have a saved type. */
25017
25018 static struct type *
25019 get_die_type_at_offset (sect_offset sect_off,
25020 dwarf2_per_cu_data *per_cu,
25021 dwarf2_per_objfile *per_objfile)
25022 {
25023 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25024
25025 if (per_objfile->die_type_hash == NULL)
25026 return NULL;
25027
25028 ofs.per_cu = per_cu;
25029 ofs.sect_off = sect_off;
25030 slot = ((struct dwarf2_per_cu_offset_and_type *)
25031 htab_find (per_objfile->die_type_hash.get (), &ofs));
25032 if (slot)
25033 return slot->type;
25034 else
25035 return NULL;
25036 }
25037
25038 /* Look up the type for DIE in CU in die_type_hash,
25039 or return NULL if DIE does not have a saved type. */
25040
25041 static struct type *
25042 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25043 {
25044 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
25045 }
25046
25047 /* Add a dependence relationship from CU to REF_PER_CU. */
25048
25049 static void
25050 dwarf2_add_dependence (struct dwarf2_cu *cu,
25051 struct dwarf2_per_cu_data *ref_per_cu)
25052 {
25053 void **slot;
25054
25055 if (cu->dependencies == NULL)
25056 cu->dependencies
25057 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25058 NULL, &cu->comp_unit_obstack,
25059 hashtab_obstack_allocate,
25060 dummy_obstack_deallocate);
25061
25062 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25063 if (*slot == NULL)
25064 *slot = ref_per_cu;
25065 }
25066
25067 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25068 Set the mark field in every compilation unit in the
25069 cache that we must keep because we are keeping CU.
25070
25071 DATA is the dwarf2_per_objfile object in which to look up CUs. */
25072
25073 static int
25074 dwarf2_mark_helper (void **slot, void *data)
25075 {
25076 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25077 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25078 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
25079
25080 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25081 reading of the chain. As such dependencies remain valid it is not much
25082 useful to track and undo them during QUIT cleanups. */
25083 if (cu == nullptr)
25084 return 1;
25085
25086 if (cu->mark)
25087 return 1;
25088
25089 cu->mark = true;
25090
25091 if (cu->dependencies != nullptr)
25092 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
25093
25094 return 1;
25095 }
25096
25097 /* Set the mark field in CU and in every other compilation unit in the
25098 cache that we must keep because we are keeping CU. */
25099
25100 static void
25101 dwarf2_mark (struct dwarf2_cu *cu)
25102 {
25103 if (cu->mark)
25104 return;
25105
25106 cu->mark = true;
25107
25108 if (cu->dependencies != nullptr)
25109 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
25110 }
25111
25112 /* Trivial hash function for partial_die_info: the hash value of a DIE
25113 is its offset in .debug_info for this objfile. */
25114
25115 static hashval_t
25116 partial_die_hash (const void *item)
25117 {
25118 const struct partial_die_info *part_die
25119 = (const struct partial_die_info *) item;
25120
25121 return to_underlying (part_die->sect_off);
25122 }
25123
25124 /* Trivial comparison function for partial_die_info structures: two DIEs
25125 are equal if they have the same offset. */
25126
25127 static int
25128 partial_die_eq (const void *item_lhs, const void *item_rhs)
25129 {
25130 const struct partial_die_info *part_die_lhs
25131 = (const struct partial_die_info *) item_lhs;
25132 const struct partial_die_info *part_die_rhs
25133 = (const struct partial_die_info *) item_rhs;
25134
25135 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25136 }
25137
25138 struct cmd_list_element *set_dwarf_cmdlist;
25139 struct cmd_list_element *show_dwarf_cmdlist;
25140
25141 static void
25142 show_check_physname (struct ui_file *file, int from_tty,
25143 struct cmd_list_element *c, const char *value)
25144 {
25145 fprintf_filtered (file,
25146 _("Whether to check \"physname\" is %s.\n"),
25147 value);
25148 }
25149
25150 void _initialize_dwarf2_read ();
25151 void
25152 _initialize_dwarf2_read ()
25153 {
25154 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
25155 Set DWARF specific variables.\n\
25156 Configure DWARF variables such as the cache size."),
25157 &set_dwarf_cmdlist, "maintenance set dwarf ",
25158 0/*allow-unknown*/, &maintenance_set_cmdlist);
25159
25160 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
25161 Show DWARF specific variables.\n\
25162 Show DWARF variables such as the cache size."),
25163 &show_dwarf_cmdlist, "maintenance show dwarf ",
25164 0/*allow-unknown*/, &maintenance_show_cmdlist);
25165
25166 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25167 &dwarf_max_cache_age, _("\
25168 Set the upper bound on the age of cached DWARF compilation units."), _("\
25169 Show the upper bound on the age of cached DWARF compilation units."), _("\
25170 A higher limit means that cached compilation units will be stored\n\
25171 in memory longer, and more total memory will be used. Zero disables\n\
25172 caching, which can slow down startup."),
25173 NULL,
25174 show_dwarf_max_cache_age,
25175 &set_dwarf_cmdlist,
25176 &show_dwarf_cmdlist);
25177
25178 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25179 Set debugging of the DWARF reader."), _("\
25180 Show debugging of the DWARF reader."), _("\
25181 When enabled (non-zero), debugging messages are printed during DWARF\n\
25182 reading and symtab expansion. A value of 1 (one) provides basic\n\
25183 information. A value greater than 1 provides more verbose information."),
25184 NULL,
25185 NULL,
25186 &setdebuglist, &showdebuglist);
25187
25188 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25189 Set debugging of the DWARF DIE reader."), _("\
25190 Show debugging of the DWARF DIE reader."), _("\
25191 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25192 The value is the maximum depth to print."),
25193 NULL,
25194 NULL,
25195 &setdebuglist, &showdebuglist);
25196
25197 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25198 Set debugging of the dwarf line reader."), _("\
25199 Show debugging of the dwarf line reader."), _("\
25200 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25201 A value of 1 (one) provides basic information.\n\
25202 A value greater than 1 provides more verbose information."),
25203 NULL,
25204 NULL,
25205 &setdebuglist, &showdebuglist);
25206
25207 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25208 Set cross-checking of \"physname\" code against demangler."), _("\
25209 Show cross-checking of \"physname\" code against demangler."), _("\
25210 When enabled, GDB's internal \"physname\" code is checked against\n\
25211 the demangler."),
25212 NULL, show_check_physname,
25213 &setdebuglist, &showdebuglist);
25214
25215 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25216 no_class, &use_deprecated_index_sections, _("\
25217 Set whether to use deprecated gdb_index sections."), _("\
25218 Show whether to use deprecated gdb_index sections."), _("\
25219 When enabled, deprecated .gdb_index sections are used anyway.\n\
25220 Normally they are ignored either because of a missing feature or\n\
25221 performance issue.\n\
25222 Warning: This option must be enabled before gdb reads the file."),
25223 NULL,
25224 NULL,
25225 &setlist, &showlist);
25226
25227 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25228 &dwarf2_locexpr_funcs);
25229 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25230 &dwarf2_loclist_funcs);
25231
25232 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25233 &dwarf2_block_frame_base_locexpr_funcs);
25234 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25235 &dwarf2_block_frame_base_loclist_funcs);
25236
25237 #if GDB_SELF_TEST
25238 selftests::register_test ("dw2_expand_symtabs_matching",
25239 selftests::dw2_expand_symtabs_matching::run_test);
25240 selftests::register_test ("dwarf2_find_containing_comp_unit",
25241 selftests::find_containing_comp_unit::run_test);
25242 #endif
25243 }
This page took 0.599329 seconds and 4 git commands to generate.