gdb: convert "set debug dwarf-read" to new style
[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 DWARF"_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 (dwarf2_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 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18096 (which may be different from NAME) to the architecture back-end to allow
18097 it to guess the correct format if necessary. */
18098
18099 static struct type *
18100 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
18101 const char *name_hint, enum bfd_endian byte_order)
18102 {
18103 struct gdbarch *gdbarch = objfile->arch ();
18104 const struct floatformat **format;
18105 struct type *type;
18106
18107 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18108 if (format)
18109 type = init_float_type (objfile, bits, name, format, byte_order);
18110 else
18111 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18112
18113 return type;
18114 }
18115
18116 /* Allocate an integer type of size BITS and name NAME. */
18117
18118 static struct type *
18119 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18120 int bits, int unsigned_p, const char *name)
18121 {
18122 struct type *type;
18123
18124 /* Versions of Intel's C Compiler generate an integer type called "void"
18125 instead of using DW_TAG_unspecified_type. This has been seen on
18126 at least versions 14, 17, and 18. */
18127 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18128 && strcmp (name, "void") == 0)
18129 type = objfile_type (objfile)->builtin_void;
18130 else
18131 type = init_integer_type (objfile, bits, unsigned_p, name);
18132
18133 return type;
18134 }
18135
18136 /* Initialise and return a floating point type of size BITS suitable for
18137 use as a component of a complex number. The NAME_HINT is passed through
18138 when initialising the floating point type and is the name of the complex
18139 type.
18140
18141 As DWARF doesn't currently provide an explicit name for the components
18142 of a complex number, but it can be helpful to have these components
18143 named, we try to select a suitable name based on the size of the
18144 component. */
18145 static struct type *
18146 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18147 struct objfile *objfile,
18148 int bits, const char *name_hint,
18149 enum bfd_endian byte_order)
18150 {
18151 gdbarch *gdbarch = objfile->arch ();
18152 struct type *tt = nullptr;
18153
18154 /* Try to find a suitable floating point builtin type of size BITS.
18155 We're going to use the name of this type as the name for the complex
18156 target type that we are about to create. */
18157 switch (cu->language)
18158 {
18159 case language_fortran:
18160 switch (bits)
18161 {
18162 case 32:
18163 tt = builtin_f_type (gdbarch)->builtin_real;
18164 break;
18165 case 64:
18166 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18167 break;
18168 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18169 case 128:
18170 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18171 break;
18172 }
18173 break;
18174 default:
18175 switch (bits)
18176 {
18177 case 32:
18178 tt = builtin_type (gdbarch)->builtin_float;
18179 break;
18180 case 64:
18181 tt = builtin_type (gdbarch)->builtin_double;
18182 break;
18183 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18184 case 128:
18185 tt = builtin_type (gdbarch)->builtin_long_double;
18186 break;
18187 }
18188 break;
18189 }
18190
18191 /* If the type we found doesn't match the size we were looking for, then
18192 pretend we didn't find a type at all, the complex target type we
18193 create will then be nameless. */
18194 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18195 tt = nullptr;
18196
18197 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18198 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18199 }
18200
18201 /* Find a representation of a given base type and install
18202 it in the TYPE field of the die. */
18203
18204 static struct type *
18205 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18206 {
18207 struct objfile *objfile = cu->per_objfile->objfile;
18208 struct type *type;
18209 struct attribute *attr;
18210 int encoding = 0, bits = 0;
18211 const char *name;
18212 gdbarch *arch;
18213
18214 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18215 if (attr != nullptr && attr->form_is_constant ())
18216 encoding = attr->constant_value (0);
18217 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18218 if (attr != nullptr)
18219 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18220 name = dwarf2_name (die, cu);
18221 if (!name)
18222 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18223
18224 arch = objfile->arch ();
18225 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18226
18227 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18228 if (attr != nullptr && attr->form_is_constant ())
18229 {
18230 int endianity = attr->constant_value (0);
18231
18232 switch (endianity)
18233 {
18234 case DW_END_big:
18235 byte_order = BFD_ENDIAN_BIG;
18236 break;
18237 case DW_END_little:
18238 byte_order = BFD_ENDIAN_LITTLE;
18239 break;
18240 default:
18241 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18242 break;
18243 }
18244 }
18245
18246 switch (encoding)
18247 {
18248 case DW_ATE_address:
18249 /* Turn DW_ATE_address into a void * pointer. */
18250 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18251 type = init_pointer_type (objfile, bits, name, type);
18252 break;
18253 case DW_ATE_boolean:
18254 type = init_boolean_type (objfile, bits, 1, name);
18255 break;
18256 case DW_ATE_complex_float:
18257 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18258 byte_order);
18259 if (type->code () == TYPE_CODE_ERROR)
18260 {
18261 if (name == nullptr)
18262 {
18263 struct obstack *obstack
18264 = &cu->per_objfile->objfile->objfile_obstack;
18265 name = obconcat (obstack, "_Complex ", type->name (),
18266 nullptr);
18267 }
18268 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18269 }
18270 else
18271 type = init_complex_type (name, type);
18272 break;
18273 case DW_ATE_decimal_float:
18274 type = init_decfloat_type (objfile, bits, name);
18275 break;
18276 case DW_ATE_float:
18277 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18278 break;
18279 case DW_ATE_signed:
18280 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18281 break;
18282 case DW_ATE_unsigned:
18283 if (cu->language == language_fortran
18284 && name
18285 && startswith (name, "character("))
18286 type = init_character_type (objfile, bits, 1, name);
18287 else
18288 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18289 break;
18290 case DW_ATE_signed_char:
18291 if (cu->language == language_ada || cu->language == language_m2
18292 || cu->language == language_pascal
18293 || cu->language == language_fortran)
18294 type = init_character_type (objfile, bits, 0, name);
18295 else
18296 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18297 break;
18298 case DW_ATE_unsigned_char:
18299 if (cu->language == language_ada || cu->language == language_m2
18300 || cu->language == language_pascal
18301 || cu->language == language_fortran
18302 || cu->language == language_rust)
18303 type = init_character_type (objfile, bits, 1, name);
18304 else
18305 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18306 break;
18307 case DW_ATE_UTF:
18308 {
18309 if (bits == 16)
18310 type = builtin_type (arch)->builtin_char16;
18311 else if (bits == 32)
18312 type = builtin_type (arch)->builtin_char32;
18313 else
18314 {
18315 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18316 bits);
18317 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18318 }
18319 return set_die_type (die, type, cu);
18320 }
18321 break;
18322
18323 default:
18324 complaint (_("unsupported DW_AT_encoding: '%s'"),
18325 dwarf_type_encoding_name (encoding));
18326 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18327 break;
18328 }
18329
18330 if (name && strcmp (name, "char") == 0)
18331 type->set_has_no_signedness (true);
18332
18333 maybe_set_alignment (cu, die, type);
18334
18335 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18336
18337 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18338 {
18339 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18340 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18341 {
18342 unsigned real_bit_size = attr->as_unsigned ();
18343 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18344 /* Only use the attributes if they make sense together. */
18345 if (attr == nullptr
18346 || (attr->as_unsigned () + real_bit_size
18347 <= 8 * TYPE_LENGTH (type)))
18348 {
18349 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18350 = real_bit_size;
18351 if (attr != nullptr)
18352 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18353 = attr->as_unsigned ();
18354 }
18355 }
18356 }
18357
18358 return set_die_type (die, type, cu);
18359 }
18360
18361 /* Parse dwarf attribute if it's a block, reference or constant and put the
18362 resulting value of the attribute into struct bound_prop.
18363 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18364
18365 static int
18366 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18367 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18368 struct type *default_type)
18369 {
18370 struct dwarf2_property_baton *baton;
18371 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18372 struct objfile *objfile = per_objfile->objfile;
18373 struct obstack *obstack = &objfile->objfile_obstack;
18374
18375 gdb_assert (default_type != NULL);
18376
18377 if (attr == NULL || prop == NULL)
18378 return 0;
18379
18380 if (attr->form_is_block ())
18381 {
18382 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18383 baton->property_type = default_type;
18384 baton->locexpr.per_cu = cu->per_cu;
18385 baton->locexpr.per_objfile = per_objfile;
18386
18387 struct dwarf_block *block = attr->as_block ();
18388 baton->locexpr.size = block->size;
18389 baton->locexpr.data = block->data;
18390 switch (attr->name)
18391 {
18392 case DW_AT_string_length:
18393 baton->locexpr.is_reference = true;
18394 break;
18395 default:
18396 baton->locexpr.is_reference = false;
18397 break;
18398 }
18399
18400 prop->set_locexpr (baton);
18401 gdb_assert (prop->baton () != NULL);
18402 }
18403 else if (attr->form_is_ref ())
18404 {
18405 struct dwarf2_cu *target_cu = cu;
18406 struct die_info *target_die;
18407 struct attribute *target_attr;
18408
18409 target_die = follow_die_ref (die, attr, &target_cu);
18410 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18411 if (target_attr == NULL)
18412 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18413 target_cu);
18414 if (target_attr == NULL)
18415 return 0;
18416
18417 switch (target_attr->name)
18418 {
18419 case DW_AT_location:
18420 if (target_attr->form_is_section_offset ())
18421 {
18422 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18423 baton->property_type = die_type (target_die, target_cu);
18424 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18425 prop->set_loclist (baton);
18426 gdb_assert (prop->baton () != NULL);
18427 }
18428 else if (target_attr->form_is_block ())
18429 {
18430 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18431 baton->property_type = die_type (target_die, target_cu);
18432 baton->locexpr.per_cu = cu->per_cu;
18433 baton->locexpr.per_objfile = per_objfile;
18434 struct dwarf_block *block = target_attr->as_block ();
18435 baton->locexpr.size = block->size;
18436 baton->locexpr.data = block->data;
18437 baton->locexpr.is_reference = true;
18438 prop->set_locexpr (baton);
18439 gdb_assert (prop->baton () != NULL);
18440 }
18441 else
18442 {
18443 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18444 "dynamic property");
18445 return 0;
18446 }
18447 break;
18448 case DW_AT_data_member_location:
18449 {
18450 LONGEST offset;
18451
18452 if (!handle_data_member_location (target_die, target_cu,
18453 &offset))
18454 return 0;
18455
18456 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18457 baton->property_type = read_type_die (target_die->parent,
18458 target_cu);
18459 baton->offset_info.offset = offset;
18460 baton->offset_info.type = die_type (target_die, target_cu);
18461 prop->set_addr_offset (baton);
18462 break;
18463 }
18464 }
18465 }
18466 else if (attr->form_is_constant ())
18467 prop->set_const_val (attr->constant_value (0));
18468 else
18469 {
18470 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18471 dwarf2_name (die, cu));
18472 return 0;
18473 }
18474
18475 return 1;
18476 }
18477
18478 /* See read.h. */
18479
18480 struct type *
18481 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
18482 {
18483 struct type *int_type;
18484
18485 /* Helper macro to examine the various builtin types. */
18486 #define TRY_TYPE(F) \
18487 int_type = (unsigned_p \
18488 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18489 : objfile_type (objfile)->builtin_ ## F); \
18490 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18491 return int_type
18492
18493 TRY_TYPE (char);
18494 TRY_TYPE (short);
18495 TRY_TYPE (int);
18496 TRY_TYPE (long);
18497 TRY_TYPE (long_long);
18498
18499 #undef TRY_TYPE
18500
18501 gdb_assert_not_reached ("unable to find suitable integer type");
18502 }
18503
18504 /* See read.h. */
18505
18506 struct type *
18507 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
18508 {
18509 int addr_size = this->per_cu->addr_size ();
18510 return this->per_objfile->int_type (addr_size, unsigned_p);
18511 }
18512
18513 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18514 present (which is valid) then compute the default type based on the
18515 compilation units address size. */
18516
18517 static struct type *
18518 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18519 {
18520 struct type *index_type = die_type (die, cu);
18521
18522 /* Dwarf-2 specifications explicitly allows to create subrange types
18523 without specifying a base type.
18524 In that case, the base type must be set to the type of
18525 the lower bound, upper bound or count, in that order, if any of these
18526 three attributes references an object that has a type.
18527 If no base type is found, the Dwarf-2 specifications say that
18528 a signed integer type of size equal to the size of an address should
18529 be used.
18530 For the following C code: `extern char gdb_int [];'
18531 GCC produces an empty range DIE.
18532 FIXME: muller/2010-05-28: Possible references to object for low bound,
18533 high bound or count are not yet handled by this code. */
18534 if (index_type->code () == TYPE_CODE_VOID)
18535 index_type = cu->addr_sized_int_type (false);
18536
18537 return index_type;
18538 }
18539
18540 /* Read the given DW_AT_subrange DIE. */
18541
18542 static struct type *
18543 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18544 {
18545 struct type *base_type, *orig_base_type;
18546 struct type *range_type;
18547 struct attribute *attr;
18548 struct dynamic_prop low, high;
18549 int low_default_is_valid;
18550 int high_bound_is_count = 0;
18551 const char *name;
18552 ULONGEST negative_mask;
18553
18554 orig_base_type = read_subrange_index_type (die, cu);
18555
18556 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18557 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18558 creating the range type, but we use the result of check_typedef
18559 when examining properties of the type. */
18560 base_type = check_typedef (orig_base_type);
18561
18562 /* The die_type call above may have already set the type for this DIE. */
18563 range_type = get_die_type (die, cu);
18564 if (range_type)
18565 return range_type;
18566
18567 high.set_const_val (0);
18568
18569 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18570 omitting DW_AT_lower_bound. */
18571 switch (cu->language)
18572 {
18573 case language_c:
18574 case language_cplus:
18575 low.set_const_val (0);
18576 low_default_is_valid = 1;
18577 break;
18578 case language_fortran:
18579 low.set_const_val (1);
18580 low_default_is_valid = 1;
18581 break;
18582 case language_d:
18583 case language_objc:
18584 case language_rust:
18585 low.set_const_val (0);
18586 low_default_is_valid = (cu->header.version >= 4);
18587 break;
18588 case language_ada:
18589 case language_m2:
18590 case language_pascal:
18591 low.set_const_val (1);
18592 low_default_is_valid = (cu->header.version >= 4);
18593 break;
18594 default:
18595 low.set_const_val (0);
18596 low_default_is_valid = 0;
18597 break;
18598 }
18599
18600 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18601 if (attr != nullptr)
18602 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18603 else if (!low_default_is_valid)
18604 complaint (_("Missing DW_AT_lower_bound "
18605 "- DIE at %s [in module %s]"),
18606 sect_offset_str (die->sect_off),
18607 objfile_name (cu->per_objfile->objfile));
18608
18609 struct attribute *attr_ub, *attr_count;
18610 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18611 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18612 {
18613 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18614 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18615 {
18616 /* If bounds are constant do the final calculation here. */
18617 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18618 high.set_const_val (low.const_val () + high.const_val () - 1);
18619 else
18620 high_bound_is_count = 1;
18621 }
18622 else
18623 {
18624 if (attr_ub != NULL)
18625 complaint (_("Unresolved DW_AT_upper_bound "
18626 "- DIE at %s [in module %s]"),
18627 sect_offset_str (die->sect_off),
18628 objfile_name (cu->per_objfile->objfile));
18629 if (attr_count != NULL)
18630 complaint (_("Unresolved DW_AT_count "
18631 "- DIE at %s [in module %s]"),
18632 sect_offset_str (die->sect_off),
18633 objfile_name (cu->per_objfile->objfile));
18634 }
18635 }
18636
18637 LONGEST bias = 0;
18638 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18639 if (bias_attr != nullptr && bias_attr->form_is_constant ())
18640 bias = bias_attr->constant_value (0);
18641
18642 /* Normally, the DWARF producers are expected to use a signed
18643 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18644 But this is unfortunately not always the case, as witnessed
18645 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18646 is used instead. To work around that ambiguity, we treat
18647 the bounds as signed, and thus sign-extend their values, when
18648 the base type is signed. */
18649 negative_mask =
18650 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18651 if (low.kind () == PROP_CONST
18652 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
18653 low.set_const_val (low.const_val () | negative_mask);
18654 if (high.kind () == PROP_CONST
18655 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
18656 high.set_const_val (high.const_val () | negative_mask);
18657
18658 /* Check for bit and byte strides. */
18659 struct dynamic_prop byte_stride_prop;
18660 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18661 if (attr_byte_stride != nullptr)
18662 {
18663 struct type *prop_type = cu->addr_sized_int_type (false);
18664 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18665 prop_type);
18666 }
18667
18668 struct dynamic_prop bit_stride_prop;
18669 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18670 if (attr_bit_stride != nullptr)
18671 {
18672 /* It only makes sense to have either a bit or byte stride. */
18673 if (attr_byte_stride != nullptr)
18674 {
18675 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18676 "- DIE at %s [in module %s]"),
18677 sect_offset_str (die->sect_off),
18678 objfile_name (cu->per_objfile->objfile));
18679 attr_bit_stride = nullptr;
18680 }
18681 else
18682 {
18683 struct type *prop_type = cu->addr_sized_int_type (false);
18684 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18685 prop_type);
18686 }
18687 }
18688
18689 if (attr_byte_stride != nullptr
18690 || attr_bit_stride != nullptr)
18691 {
18692 bool byte_stride_p = (attr_byte_stride != nullptr);
18693 struct dynamic_prop *stride
18694 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18695
18696 range_type
18697 = create_range_type_with_stride (NULL, orig_base_type, &low,
18698 &high, bias, stride, byte_stride_p);
18699 }
18700 else
18701 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18702
18703 if (high_bound_is_count)
18704 range_type->bounds ()->flag_upper_bound_is_count = 1;
18705
18706 /* Ada expects an empty array on no boundary attributes. */
18707 if (attr == NULL && cu->language != language_ada)
18708 range_type->bounds ()->high.set_undefined ();
18709
18710 name = dwarf2_name (die, cu);
18711 if (name)
18712 range_type->set_name (name);
18713
18714 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18715 if (attr != nullptr)
18716 TYPE_LENGTH (range_type) = attr->constant_value (0);
18717
18718 maybe_set_alignment (cu, die, range_type);
18719
18720 set_die_type (die, range_type, cu);
18721
18722 /* set_die_type should be already done. */
18723 set_descriptive_type (range_type, die, cu);
18724
18725 return range_type;
18726 }
18727
18728 static struct type *
18729 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18730 {
18731 struct type *type;
18732
18733 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
18734 type->set_name (dwarf2_name (die, cu));
18735
18736 /* In Ada, an unspecified type is typically used when the description
18737 of the type is deferred to a different unit. When encountering
18738 such a type, we treat it as a stub, and try to resolve it later on,
18739 when needed. */
18740 if (cu->language == language_ada)
18741 type->set_is_stub (true);
18742
18743 return set_die_type (die, type, cu);
18744 }
18745
18746 /* Read a single die and all its descendents. Set the die's sibling
18747 field to NULL; set other fields in the die correctly, and set all
18748 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18749 location of the info_ptr after reading all of those dies. PARENT
18750 is the parent of the die in question. */
18751
18752 static struct die_info *
18753 read_die_and_children (const struct die_reader_specs *reader,
18754 const gdb_byte *info_ptr,
18755 const gdb_byte **new_info_ptr,
18756 struct die_info *parent)
18757 {
18758 struct die_info *die;
18759 const gdb_byte *cur_ptr;
18760
18761 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18762 if (die == NULL)
18763 {
18764 *new_info_ptr = cur_ptr;
18765 return NULL;
18766 }
18767 store_in_ref_table (die, reader->cu);
18768
18769 if (die->has_children)
18770 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18771 else
18772 {
18773 die->child = NULL;
18774 *new_info_ptr = cur_ptr;
18775 }
18776
18777 die->sibling = NULL;
18778 die->parent = parent;
18779 return die;
18780 }
18781
18782 /* Read a die, all of its descendents, and all of its siblings; set
18783 all of the fields of all of the dies correctly. Arguments are as
18784 in read_die_and_children. */
18785
18786 static struct die_info *
18787 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18788 const gdb_byte *info_ptr,
18789 const gdb_byte **new_info_ptr,
18790 struct die_info *parent)
18791 {
18792 struct die_info *first_die, *last_sibling;
18793 const gdb_byte *cur_ptr;
18794
18795 cur_ptr = info_ptr;
18796 first_die = last_sibling = NULL;
18797
18798 while (1)
18799 {
18800 struct die_info *die
18801 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18802
18803 if (die == NULL)
18804 {
18805 *new_info_ptr = cur_ptr;
18806 return first_die;
18807 }
18808
18809 if (!first_die)
18810 first_die = die;
18811 else
18812 last_sibling->sibling = die;
18813
18814 last_sibling = die;
18815 }
18816 }
18817
18818 /* Read a die, all of its descendents, and all of its siblings; set
18819 all of the fields of all of the dies correctly. Arguments are as
18820 in read_die_and_children.
18821 This the main entry point for reading a DIE and all its children. */
18822
18823 static struct die_info *
18824 read_die_and_siblings (const struct die_reader_specs *reader,
18825 const gdb_byte *info_ptr,
18826 const gdb_byte **new_info_ptr,
18827 struct die_info *parent)
18828 {
18829 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18830 new_info_ptr, parent);
18831
18832 if (dwarf_die_debug)
18833 {
18834 fprintf_unfiltered (gdb_stdlog,
18835 "Read die from %s@0x%x of %s:\n",
18836 reader->die_section->get_name (),
18837 (unsigned) (info_ptr - reader->die_section->buffer),
18838 bfd_get_filename (reader->abfd));
18839 dump_die (die, dwarf_die_debug);
18840 }
18841
18842 return die;
18843 }
18844
18845 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18846 attributes.
18847 The caller is responsible for filling in the extra attributes
18848 and updating (*DIEP)->num_attrs.
18849 Set DIEP to point to a newly allocated die with its information,
18850 except for its child, sibling, and parent fields. */
18851
18852 static const gdb_byte *
18853 read_full_die_1 (const struct die_reader_specs *reader,
18854 struct die_info **diep, const gdb_byte *info_ptr,
18855 int num_extra_attrs)
18856 {
18857 unsigned int abbrev_number, bytes_read, i;
18858 struct abbrev_info *abbrev;
18859 struct die_info *die;
18860 struct dwarf2_cu *cu = reader->cu;
18861 bfd *abfd = reader->abfd;
18862
18863 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18864 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18865 info_ptr += bytes_read;
18866 if (!abbrev_number)
18867 {
18868 *diep = NULL;
18869 return info_ptr;
18870 }
18871
18872 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18873 if (!abbrev)
18874 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18875 abbrev_number,
18876 bfd_get_filename (abfd));
18877
18878 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18879 die->sect_off = sect_off;
18880 die->tag = abbrev->tag;
18881 die->abbrev = abbrev_number;
18882 die->has_children = abbrev->has_children;
18883
18884 /* Make the result usable.
18885 The caller needs to update num_attrs after adding the extra
18886 attributes. */
18887 die->num_attrs = abbrev->num_attrs;
18888
18889 bool any_need_reprocess = false;
18890 for (i = 0; i < abbrev->num_attrs; ++i)
18891 {
18892 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18893 info_ptr);
18894 if (die->attrs[i].requires_reprocessing_p ())
18895 any_need_reprocess = true;
18896 }
18897
18898 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18899 if (attr != nullptr && attr->form_is_unsigned ())
18900 cu->str_offsets_base = attr->as_unsigned ();
18901
18902 attr = die->attr (DW_AT_loclists_base);
18903 if (attr != nullptr)
18904 cu->loclist_base = attr->as_unsigned ();
18905
18906 auto maybe_addr_base = die->addr_base ();
18907 if (maybe_addr_base.has_value ())
18908 cu->addr_base = *maybe_addr_base;
18909
18910 attr = die->attr (DW_AT_rnglists_base);
18911 if (attr != nullptr)
18912 cu->ranges_base = attr->as_unsigned ();
18913
18914 if (any_need_reprocess)
18915 {
18916 for (i = 0; i < abbrev->num_attrs; ++i)
18917 {
18918 if (die->attrs[i].requires_reprocessing_p ())
18919 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18920 }
18921 }
18922 *diep = die;
18923 return info_ptr;
18924 }
18925
18926 /* Read a die and all its attributes.
18927 Set DIEP to point to a newly allocated die with its information,
18928 except for its child, sibling, and parent fields. */
18929
18930 static const gdb_byte *
18931 read_full_die (const struct die_reader_specs *reader,
18932 struct die_info **diep, const gdb_byte *info_ptr)
18933 {
18934 const gdb_byte *result;
18935
18936 result = read_full_die_1 (reader, diep, info_ptr, 0);
18937
18938 if (dwarf_die_debug)
18939 {
18940 fprintf_unfiltered (gdb_stdlog,
18941 "Read die from %s@0x%x of %s:\n",
18942 reader->die_section->get_name (),
18943 (unsigned) (info_ptr - reader->die_section->buffer),
18944 bfd_get_filename (reader->abfd));
18945 dump_die (*diep, dwarf_die_debug);
18946 }
18947
18948 return result;
18949 }
18950 \f
18951
18952 /* Returns nonzero if TAG represents a type that we might generate a partial
18953 symbol for. */
18954
18955 static int
18956 is_type_tag_for_partial (int tag, enum language lang)
18957 {
18958 switch (tag)
18959 {
18960 #if 0
18961 /* Some types that would be reasonable to generate partial symbols for,
18962 that we don't at present. Note that normally this does not
18963 matter, mainly because C compilers don't give names to these
18964 types, but instead emit DW_TAG_typedef. */
18965 case DW_TAG_file_type:
18966 case DW_TAG_ptr_to_member_type:
18967 case DW_TAG_set_type:
18968 case DW_TAG_string_type:
18969 case DW_TAG_subroutine_type:
18970 #endif
18971
18972 /* GNAT may emit an array with a name, but no typedef, so we
18973 need to make a symbol in this case. */
18974 case DW_TAG_array_type:
18975 return lang == language_ada;
18976
18977 case DW_TAG_base_type:
18978 case DW_TAG_class_type:
18979 case DW_TAG_interface_type:
18980 case DW_TAG_enumeration_type:
18981 case DW_TAG_structure_type:
18982 case DW_TAG_subrange_type:
18983 case DW_TAG_typedef:
18984 case DW_TAG_union_type:
18985 return 1;
18986 default:
18987 return 0;
18988 }
18989 }
18990
18991 /* Load all DIEs that are interesting for partial symbols into memory. */
18992
18993 static struct partial_die_info *
18994 load_partial_dies (const struct die_reader_specs *reader,
18995 const gdb_byte *info_ptr, int building_psymtab)
18996 {
18997 struct dwarf2_cu *cu = reader->cu;
18998 struct objfile *objfile = cu->per_objfile->objfile;
18999 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
19000 unsigned int bytes_read;
19001 unsigned int load_all = 0;
19002 int nesting_level = 1;
19003
19004 parent_die = NULL;
19005 last_die = NULL;
19006
19007 gdb_assert (cu->per_cu != NULL);
19008 if (cu->per_cu->load_all_dies)
19009 load_all = 1;
19010
19011 cu->partial_dies
19012 = htab_create_alloc_ex (cu->header.length / 12,
19013 partial_die_hash,
19014 partial_die_eq,
19015 NULL,
19016 &cu->comp_unit_obstack,
19017 hashtab_obstack_allocate,
19018 dummy_obstack_deallocate);
19019
19020 while (1)
19021 {
19022 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
19023
19024 /* A NULL abbrev means the end of a series of children. */
19025 if (abbrev == NULL)
19026 {
19027 if (--nesting_level == 0)
19028 return first_die;
19029
19030 info_ptr += bytes_read;
19031 last_die = parent_die;
19032 parent_die = parent_die->die_parent;
19033 continue;
19034 }
19035
19036 /* Check for template arguments. We never save these; if
19037 they're seen, we just mark the parent, and go on our way. */
19038 if (parent_die != NULL
19039 && cu->language == language_cplus
19040 && (abbrev->tag == DW_TAG_template_type_param
19041 || abbrev->tag == DW_TAG_template_value_param))
19042 {
19043 parent_die->has_template_arguments = 1;
19044
19045 if (!load_all)
19046 {
19047 /* We don't need a partial DIE for the template argument. */
19048 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19049 continue;
19050 }
19051 }
19052
19053 /* We only recurse into c++ subprograms looking for template arguments.
19054 Skip their other children. */
19055 if (!load_all
19056 && cu->language == language_cplus
19057 && parent_die != NULL
19058 && parent_die->tag == DW_TAG_subprogram
19059 && abbrev->tag != DW_TAG_inlined_subroutine)
19060 {
19061 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19062 continue;
19063 }
19064
19065 /* Check whether this DIE is interesting enough to save. Normally
19066 we would not be interested in members here, but there may be
19067 later variables referencing them via DW_AT_specification (for
19068 static members). */
19069 if (!load_all
19070 && !is_type_tag_for_partial (abbrev->tag, cu->language)
19071 && abbrev->tag != DW_TAG_constant
19072 && abbrev->tag != DW_TAG_enumerator
19073 && abbrev->tag != DW_TAG_subprogram
19074 && abbrev->tag != DW_TAG_inlined_subroutine
19075 && abbrev->tag != DW_TAG_lexical_block
19076 && abbrev->tag != DW_TAG_variable
19077 && abbrev->tag != DW_TAG_namespace
19078 && abbrev->tag != DW_TAG_module
19079 && abbrev->tag != DW_TAG_member
19080 && abbrev->tag != DW_TAG_imported_unit
19081 && abbrev->tag != DW_TAG_imported_declaration)
19082 {
19083 /* Otherwise we skip to the next sibling, if any. */
19084 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19085 continue;
19086 }
19087
19088 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19089 abbrev);
19090
19091 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19092
19093 /* This two-pass algorithm for processing partial symbols has a
19094 high cost in cache pressure. Thus, handle some simple cases
19095 here which cover the majority of C partial symbols. DIEs
19096 which neither have specification tags in them, nor could have
19097 specification tags elsewhere pointing at them, can simply be
19098 processed and discarded.
19099
19100 This segment is also optional; scan_partial_symbols and
19101 add_partial_symbol will handle these DIEs if we chain
19102 them in normally. When compilers which do not emit large
19103 quantities of duplicate debug information are more common,
19104 this code can probably be removed. */
19105
19106 /* Any complete simple types at the top level (pretty much all
19107 of them, for a language without namespaces), can be processed
19108 directly. */
19109 if (parent_die == NULL
19110 && pdi.has_specification == 0
19111 && pdi.is_declaration == 0
19112 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19113 || pdi.tag == DW_TAG_base_type
19114 || pdi.tag == DW_TAG_array_type
19115 || pdi.tag == DW_TAG_subrange_type))
19116 {
19117 if (building_psymtab && pdi.raw_name != NULL)
19118 add_partial_symbol (&pdi, cu);
19119
19120 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19121 continue;
19122 }
19123
19124 /* The exception for DW_TAG_typedef with has_children above is
19125 a workaround of GCC PR debug/47510. In the case of this complaint
19126 type_name_or_error will error on such types later.
19127
19128 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19129 it could not find the child DIEs referenced later, this is checked
19130 above. In correct DWARF DW_TAG_typedef should have no children. */
19131
19132 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19133 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19134 "- DIE at %s [in module %s]"),
19135 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19136
19137 /* If we're at the second level, and we're an enumerator, and
19138 our parent has no specification (meaning possibly lives in a
19139 namespace elsewhere), then we can add the partial symbol now
19140 instead of queueing it. */
19141 if (pdi.tag == DW_TAG_enumerator
19142 && parent_die != NULL
19143 && parent_die->die_parent == NULL
19144 && parent_die->tag == DW_TAG_enumeration_type
19145 && parent_die->has_specification == 0)
19146 {
19147 if (pdi.raw_name == NULL)
19148 complaint (_("malformed enumerator DIE ignored"));
19149 else if (building_psymtab)
19150 add_partial_symbol (&pdi, cu);
19151
19152 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19153 continue;
19154 }
19155
19156 struct partial_die_info *part_die
19157 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19158
19159 /* We'll save this DIE so link it in. */
19160 part_die->die_parent = parent_die;
19161 part_die->die_sibling = NULL;
19162 part_die->die_child = NULL;
19163
19164 if (last_die && last_die == parent_die)
19165 last_die->die_child = part_die;
19166 else if (last_die)
19167 last_die->die_sibling = part_die;
19168
19169 last_die = part_die;
19170
19171 if (first_die == NULL)
19172 first_die = part_die;
19173
19174 /* Maybe add the DIE to the hash table. Not all DIEs that we
19175 find interesting need to be in the hash table, because we
19176 also have the parent/sibling/child chains; only those that we
19177 might refer to by offset later during partial symbol reading.
19178
19179 For now this means things that might have be the target of a
19180 DW_AT_specification, DW_AT_abstract_origin, or
19181 DW_AT_extension. DW_AT_extension will refer only to
19182 namespaces; DW_AT_abstract_origin refers to functions (and
19183 many things under the function DIE, but we do not recurse
19184 into function DIEs during partial symbol reading) and
19185 possibly variables as well; DW_AT_specification refers to
19186 declarations. Declarations ought to have the DW_AT_declaration
19187 flag. It happens that GCC forgets to put it in sometimes, but
19188 only for functions, not for types.
19189
19190 Adding more things than necessary to the hash table is harmless
19191 except for the performance cost. Adding too few will result in
19192 wasted time in find_partial_die, when we reread the compilation
19193 unit with load_all_dies set. */
19194
19195 if (load_all
19196 || abbrev->tag == DW_TAG_constant
19197 || abbrev->tag == DW_TAG_subprogram
19198 || abbrev->tag == DW_TAG_variable
19199 || abbrev->tag == DW_TAG_namespace
19200 || part_die->is_declaration)
19201 {
19202 void **slot;
19203
19204 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19205 to_underlying (part_die->sect_off),
19206 INSERT);
19207 *slot = part_die;
19208 }
19209
19210 /* For some DIEs we want to follow their children (if any). For C
19211 we have no reason to follow the children of structures; for other
19212 languages we have to, so that we can get at method physnames
19213 to infer fully qualified class names, for DW_AT_specification,
19214 and for C++ template arguments. For C++, we also look one level
19215 inside functions to find template arguments (if the name of the
19216 function does not already contain the template arguments).
19217
19218 For Ada and Fortran, we need to scan the children of subprograms
19219 and lexical blocks as well because these languages allow the
19220 definition of nested entities that could be interesting for the
19221 debugger, such as nested subprograms for instance. */
19222 if (last_die->has_children
19223 && (load_all
19224 || last_die->tag == DW_TAG_namespace
19225 || last_die->tag == DW_TAG_module
19226 || last_die->tag == DW_TAG_enumeration_type
19227 || (cu->language == language_cplus
19228 && last_die->tag == DW_TAG_subprogram
19229 && (last_die->raw_name == NULL
19230 || strchr (last_die->raw_name, '<') == NULL))
19231 || (cu->language != language_c
19232 && (last_die->tag == DW_TAG_class_type
19233 || last_die->tag == DW_TAG_interface_type
19234 || last_die->tag == DW_TAG_structure_type
19235 || last_die->tag == DW_TAG_union_type))
19236 || ((cu->language == language_ada
19237 || cu->language == language_fortran)
19238 && (last_die->tag == DW_TAG_subprogram
19239 || last_die->tag == DW_TAG_lexical_block))))
19240 {
19241 nesting_level++;
19242 parent_die = last_die;
19243 continue;
19244 }
19245
19246 /* Otherwise we skip to the next sibling, if any. */
19247 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19248
19249 /* Back to the top, do it again. */
19250 }
19251 }
19252
19253 partial_die_info::partial_die_info (sect_offset sect_off_,
19254 struct abbrev_info *abbrev)
19255 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19256 {
19257 }
19258
19259 /* See class definition. */
19260
19261 const char *
19262 partial_die_info::name (dwarf2_cu *cu)
19263 {
19264 if (!canonical_name && raw_name != nullptr)
19265 {
19266 struct objfile *objfile = cu->per_objfile->objfile;
19267 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19268 canonical_name = 1;
19269 }
19270
19271 return raw_name;
19272 }
19273
19274 /* Read a minimal amount of information into the minimal die structure.
19275 INFO_PTR should point just after the initial uleb128 of a DIE. */
19276
19277 const gdb_byte *
19278 partial_die_info::read (const struct die_reader_specs *reader,
19279 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19280 {
19281 struct dwarf2_cu *cu = reader->cu;
19282 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19283 unsigned int i;
19284 int has_low_pc_attr = 0;
19285 int has_high_pc_attr = 0;
19286 int high_pc_relative = 0;
19287
19288 for (i = 0; i < abbrev.num_attrs; ++i)
19289 {
19290 attribute attr;
19291 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19292 /* String and address offsets that need to do the reprocessing have
19293 already been read at this point, so there is no need to wait until
19294 the loop terminates to do the reprocessing. */
19295 if (attr.requires_reprocessing_p ())
19296 read_attribute_reprocess (reader, &attr, tag);
19297 /* Store the data if it is of an attribute we want to keep in a
19298 partial symbol table. */
19299 switch (attr.name)
19300 {
19301 case DW_AT_name:
19302 switch (tag)
19303 {
19304 case DW_TAG_compile_unit:
19305 case DW_TAG_partial_unit:
19306 case DW_TAG_type_unit:
19307 /* Compilation units have a DW_AT_name that is a filename, not
19308 a source language identifier. */
19309 case DW_TAG_enumeration_type:
19310 case DW_TAG_enumerator:
19311 /* These tags always have simple identifiers already; no need
19312 to canonicalize them. */
19313 canonical_name = 1;
19314 raw_name = attr.as_string ();
19315 break;
19316 default:
19317 canonical_name = 0;
19318 raw_name = attr.as_string ();
19319 break;
19320 }
19321 break;
19322 case DW_AT_linkage_name:
19323 case DW_AT_MIPS_linkage_name:
19324 /* Note that both forms of linkage name might appear. We
19325 assume they will be the same, and we only store the last
19326 one we see. */
19327 linkage_name = attr.as_string ();
19328 break;
19329 case DW_AT_low_pc:
19330 has_low_pc_attr = 1;
19331 lowpc = attr.as_address ();
19332 break;
19333 case DW_AT_high_pc:
19334 has_high_pc_attr = 1;
19335 highpc = attr.as_address ();
19336 if (cu->header.version >= 4 && attr.form_is_constant ())
19337 high_pc_relative = 1;
19338 break;
19339 case DW_AT_location:
19340 /* Support the .debug_loc offsets. */
19341 if (attr.form_is_block ())
19342 {
19343 d.locdesc = attr.as_block ();
19344 }
19345 else if (attr.form_is_section_offset ())
19346 {
19347 dwarf2_complex_location_expr_complaint ();
19348 }
19349 else
19350 {
19351 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19352 "partial symbol information");
19353 }
19354 break;
19355 case DW_AT_external:
19356 is_external = attr.as_boolean ();
19357 break;
19358 case DW_AT_declaration:
19359 is_declaration = attr.as_boolean ();
19360 break;
19361 case DW_AT_type:
19362 has_type = 1;
19363 break;
19364 case DW_AT_abstract_origin:
19365 case DW_AT_specification:
19366 case DW_AT_extension:
19367 has_specification = 1;
19368 spec_offset = attr.get_ref_die_offset ();
19369 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19370 || cu->per_cu->is_dwz);
19371 break;
19372 case DW_AT_sibling:
19373 /* Ignore absolute siblings, they might point outside of
19374 the current compile unit. */
19375 if (attr.form == DW_FORM_ref_addr)
19376 complaint (_("ignoring absolute DW_AT_sibling"));
19377 else
19378 {
19379 const gdb_byte *buffer = reader->buffer;
19380 sect_offset off = attr.get_ref_die_offset ();
19381 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19382
19383 if (sibling_ptr < info_ptr)
19384 complaint (_("DW_AT_sibling points backwards"));
19385 else if (sibling_ptr > reader->buffer_end)
19386 reader->die_section->overflow_complaint ();
19387 else
19388 sibling = sibling_ptr;
19389 }
19390 break;
19391 case DW_AT_byte_size:
19392 has_byte_size = 1;
19393 break;
19394 case DW_AT_const_value:
19395 has_const_value = 1;
19396 break;
19397 case DW_AT_calling_convention:
19398 /* DWARF doesn't provide a way to identify a program's source-level
19399 entry point. DW_AT_calling_convention attributes are only meant
19400 to describe functions' calling conventions.
19401
19402 However, because it's a necessary piece of information in
19403 Fortran, and before DWARF 4 DW_CC_program was the only
19404 piece of debugging information whose definition refers to
19405 a 'main program' at all, several compilers marked Fortran
19406 main programs with DW_CC_program --- even when those
19407 functions use the standard calling conventions.
19408
19409 Although DWARF now specifies a way to provide this
19410 information, we support this practice for backward
19411 compatibility. */
19412 if (attr.constant_value (0) == DW_CC_program
19413 && cu->language == language_fortran)
19414 main_subprogram = 1;
19415 break;
19416 case DW_AT_inline:
19417 {
19418 LONGEST value = attr.constant_value (-1);
19419 if (value == DW_INL_inlined
19420 || value == DW_INL_declared_inlined)
19421 may_be_inlined = 1;
19422 }
19423 break;
19424
19425 case DW_AT_import:
19426 if (tag == DW_TAG_imported_unit)
19427 {
19428 d.sect_off = attr.get_ref_die_offset ();
19429 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19430 || cu->per_cu->is_dwz);
19431 }
19432 break;
19433
19434 case DW_AT_main_subprogram:
19435 main_subprogram = attr.as_boolean ();
19436 break;
19437
19438 case DW_AT_ranges:
19439 {
19440 /* DW_AT_rnglists_base does not apply to DIEs from the DWO
19441 skeleton. We take advantage of the fact the DW_AT_ranges
19442 does not appear in DW_TAG_compile_unit of DWO files.
19443
19444 Attributes of the form DW_FORM_rnglistx have already had
19445 their value changed by read_rnglist_index and already
19446 include DW_AT_rnglists_base, so don't need to add the ranges
19447 base, either. */
19448 int need_ranges_base = (tag != DW_TAG_compile_unit
19449 && attr.form != DW_FORM_rnglistx);
19450 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19451 but that requires a full DIE, so instead we just
19452 reimplement it. */
19453 unsigned int ranges_offset = (attr.constant_value (0)
19454 + (need_ranges_base
19455 ? cu->ranges_base
19456 : 0));
19457
19458 /* Value of the DW_AT_ranges attribute is the offset in the
19459 .debug_ranges section. */
19460 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19461 nullptr, tag))
19462 has_pc_info = 1;
19463 }
19464 break;
19465
19466 default:
19467 break;
19468 }
19469 }
19470
19471 /* For Ada, if both the name and the linkage name appear, we prefer
19472 the latter. This lets "catch exception" work better, regardless
19473 of the order in which the name and linkage name were emitted.
19474 Really, though, this is just a workaround for the fact that gdb
19475 doesn't store both the name and the linkage name. */
19476 if (cu->language == language_ada && linkage_name != nullptr)
19477 raw_name = linkage_name;
19478
19479 if (high_pc_relative)
19480 highpc += lowpc;
19481
19482 if (has_low_pc_attr && has_high_pc_attr)
19483 {
19484 /* When using the GNU linker, .gnu.linkonce. sections are used to
19485 eliminate duplicate copies of functions and vtables and such.
19486 The linker will arbitrarily choose one and discard the others.
19487 The AT_*_pc values for such functions refer to local labels in
19488 these sections. If the section from that file was discarded, the
19489 labels are not in the output, so the relocs get a value of 0.
19490 If this is a discarded function, mark the pc bounds as invalid,
19491 so that GDB will ignore it. */
19492 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19493 {
19494 struct objfile *objfile = per_objfile->objfile;
19495 struct gdbarch *gdbarch = objfile->arch ();
19496
19497 complaint (_("DW_AT_low_pc %s is zero "
19498 "for DIE at %s [in module %s]"),
19499 paddress (gdbarch, lowpc),
19500 sect_offset_str (sect_off),
19501 objfile_name (objfile));
19502 }
19503 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19504 else if (lowpc >= highpc)
19505 {
19506 struct objfile *objfile = per_objfile->objfile;
19507 struct gdbarch *gdbarch = objfile->arch ();
19508
19509 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19510 "for DIE at %s [in module %s]"),
19511 paddress (gdbarch, lowpc),
19512 paddress (gdbarch, highpc),
19513 sect_offset_str (sect_off),
19514 objfile_name (objfile));
19515 }
19516 else
19517 has_pc_info = 1;
19518 }
19519
19520 return info_ptr;
19521 }
19522
19523 /* Find a cached partial DIE at OFFSET in CU. */
19524
19525 struct partial_die_info *
19526 dwarf2_cu::find_partial_die (sect_offset sect_off)
19527 {
19528 struct partial_die_info *lookup_die = NULL;
19529 struct partial_die_info part_die (sect_off);
19530
19531 lookup_die = ((struct partial_die_info *)
19532 htab_find_with_hash (partial_dies, &part_die,
19533 to_underlying (sect_off)));
19534
19535 return lookup_die;
19536 }
19537
19538 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19539 except in the case of .debug_types DIEs which do not reference
19540 outside their CU (they do however referencing other types via
19541 DW_FORM_ref_sig8). */
19542
19543 static const struct cu_partial_die_info
19544 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19545 {
19546 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19547 struct objfile *objfile = per_objfile->objfile;
19548 struct partial_die_info *pd = NULL;
19549
19550 if (offset_in_dwz == cu->per_cu->is_dwz
19551 && cu->header.offset_in_cu_p (sect_off))
19552 {
19553 pd = cu->find_partial_die (sect_off);
19554 if (pd != NULL)
19555 return { cu, pd };
19556 /* We missed recording what we needed.
19557 Load all dies and try again. */
19558 }
19559 else
19560 {
19561 /* TUs don't reference other CUs/TUs (except via type signatures). */
19562 if (cu->per_cu->is_debug_types)
19563 {
19564 error (_("Dwarf Error: Type Unit at offset %s contains"
19565 " external reference to offset %s [in module %s].\n"),
19566 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19567 bfd_get_filename (objfile->obfd));
19568 }
19569 dwarf2_per_cu_data *per_cu
19570 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19571 per_objfile);
19572
19573 cu = per_objfile->get_cu (per_cu);
19574 if (cu == NULL || cu->partial_dies == NULL)
19575 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19576
19577 cu = per_objfile->get_cu (per_cu);
19578
19579 cu->last_used = 0;
19580 pd = cu->find_partial_die (sect_off);
19581 }
19582
19583 /* If we didn't find it, and not all dies have been loaded,
19584 load them all and try again. */
19585
19586 if (pd == NULL && cu->per_cu->load_all_dies == 0)
19587 {
19588 cu->per_cu->load_all_dies = 1;
19589
19590 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19591 THIS_CU->cu may already be in use. So we can't just free it and
19592 replace its DIEs with the ones we read in. Instead, we leave those
19593 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19594 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19595 set. */
19596 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
19597
19598 pd = cu->find_partial_die (sect_off);
19599 }
19600
19601 if (pd == NULL)
19602 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
19603 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19604 return { cu, pd };
19605 }
19606
19607 /* See if we can figure out if the class lives in a namespace. We do
19608 this by looking for a member function; its demangled name will
19609 contain namespace info, if there is any. */
19610
19611 static void
19612 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19613 struct dwarf2_cu *cu)
19614 {
19615 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19616 what template types look like, because the demangler
19617 frequently doesn't give the same name as the debug info. We
19618 could fix this by only using the demangled name to get the
19619 prefix (but see comment in read_structure_type). */
19620
19621 struct partial_die_info *real_pdi;
19622 struct partial_die_info *child_pdi;
19623
19624 /* If this DIE (this DIE's specification, if any) has a parent, then
19625 we should not do this. We'll prepend the parent's fully qualified
19626 name when we create the partial symbol. */
19627
19628 real_pdi = struct_pdi;
19629 while (real_pdi->has_specification)
19630 {
19631 auto res = find_partial_die (real_pdi->spec_offset,
19632 real_pdi->spec_is_dwz, cu);
19633 real_pdi = res.pdi;
19634 cu = res.cu;
19635 }
19636
19637 if (real_pdi->die_parent != NULL)
19638 return;
19639
19640 for (child_pdi = struct_pdi->die_child;
19641 child_pdi != NULL;
19642 child_pdi = child_pdi->die_sibling)
19643 {
19644 if (child_pdi->tag == DW_TAG_subprogram
19645 && child_pdi->linkage_name != NULL)
19646 {
19647 gdb::unique_xmalloc_ptr<char> actual_class_name
19648 (cu->language_defn->class_name_from_physname
19649 (child_pdi->linkage_name));
19650 if (actual_class_name != NULL)
19651 {
19652 struct objfile *objfile = cu->per_objfile->objfile;
19653 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19654 struct_pdi->canonical_name = 1;
19655 }
19656 break;
19657 }
19658 }
19659 }
19660
19661 /* Return true if a DIE with TAG may have the DW_AT_const_value
19662 attribute. */
19663
19664 static bool
19665 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19666 {
19667 switch (tag)
19668 {
19669 case DW_TAG_constant:
19670 case DW_TAG_enumerator:
19671 case DW_TAG_formal_parameter:
19672 case DW_TAG_template_value_param:
19673 case DW_TAG_variable:
19674 return true;
19675 }
19676
19677 return false;
19678 }
19679
19680 void
19681 partial_die_info::fixup (struct dwarf2_cu *cu)
19682 {
19683 /* Once we've fixed up a die, there's no point in doing so again.
19684 This also avoids a memory leak if we were to call
19685 guess_partial_die_structure_name multiple times. */
19686 if (fixup_called)
19687 return;
19688
19689 /* If we found a reference attribute and the DIE has no name, try
19690 to find a name in the referred to DIE. */
19691
19692 if (raw_name == NULL && has_specification)
19693 {
19694 struct partial_die_info *spec_die;
19695
19696 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19697 spec_die = res.pdi;
19698 cu = res.cu;
19699
19700 spec_die->fixup (cu);
19701
19702 if (spec_die->raw_name)
19703 {
19704 raw_name = spec_die->raw_name;
19705 canonical_name = spec_die->canonical_name;
19706
19707 /* Copy DW_AT_external attribute if it is set. */
19708 if (spec_die->is_external)
19709 is_external = spec_die->is_external;
19710 }
19711 }
19712
19713 if (!has_const_value && has_specification
19714 && can_have_DW_AT_const_value_p (tag))
19715 {
19716 struct partial_die_info *spec_die;
19717
19718 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19719 spec_die = res.pdi;
19720 cu = res.cu;
19721
19722 spec_die->fixup (cu);
19723
19724 if (spec_die->has_const_value)
19725 {
19726 /* Copy DW_AT_const_value attribute if it is set. */
19727 has_const_value = spec_die->has_const_value;
19728 }
19729 }
19730
19731 /* Set default names for some unnamed DIEs. */
19732
19733 if (raw_name == NULL && tag == DW_TAG_namespace)
19734 {
19735 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19736 canonical_name = 1;
19737 }
19738
19739 /* If there is no parent die to provide a namespace, and there are
19740 children, see if we can determine the namespace from their linkage
19741 name. */
19742 if (cu->language == language_cplus
19743 && !cu->per_objfile->per_bfd->types.empty ()
19744 && die_parent == NULL
19745 && has_children
19746 && (tag == DW_TAG_class_type
19747 || tag == DW_TAG_structure_type
19748 || tag == DW_TAG_union_type))
19749 guess_partial_die_structure_name (this, cu);
19750
19751 /* GCC might emit a nameless struct or union that has a linkage
19752 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19753 if (raw_name == NULL
19754 && (tag == DW_TAG_class_type
19755 || tag == DW_TAG_interface_type
19756 || tag == DW_TAG_structure_type
19757 || tag == DW_TAG_union_type)
19758 && linkage_name != NULL)
19759 {
19760 gdb::unique_xmalloc_ptr<char> demangled
19761 (gdb_demangle (linkage_name, DMGL_TYPES));
19762 if (demangled != nullptr)
19763 {
19764 const char *base;
19765
19766 /* Strip any leading namespaces/classes, keep only the base name.
19767 DW_AT_name for named DIEs does not contain the prefixes. */
19768 base = strrchr (demangled.get (), ':');
19769 if (base && base > demangled.get () && base[-1] == ':')
19770 base++;
19771 else
19772 base = demangled.get ();
19773
19774 struct objfile *objfile = cu->per_objfile->objfile;
19775 raw_name = objfile->intern (base);
19776 canonical_name = 1;
19777 }
19778 }
19779
19780 fixup_called = 1;
19781 }
19782
19783 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
19784 contents from the given SECTION in the HEADER. */
19785 static void
19786 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
19787 struct dwarf2_section_info *section)
19788 {
19789 unsigned int bytes_read;
19790 bfd *abfd = section->get_bfd_owner ();
19791 const gdb_byte *info_ptr = section->buffer;
19792 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19793 info_ptr += bytes_read;
19794 header->version = read_2_bytes (abfd, info_ptr);
19795 info_ptr += 2;
19796 header->addr_size = read_1_byte (abfd, info_ptr);
19797 info_ptr += 1;
19798 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19799 info_ptr += 1;
19800 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19801 }
19802
19803 /* Return the DW_AT_loclists_base value for the CU. */
19804 static ULONGEST
19805 lookup_loclist_base (struct dwarf2_cu *cu)
19806 {
19807 /* For the .dwo unit, the loclist_base points to the first offset following
19808 the header. The header consists of the following entities-
19809 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19810 bit format)
19811 2. version (2 bytes)
19812 3. address size (1 byte)
19813 4. segment selector size (1 byte)
19814 5. offset entry count (4 bytes)
19815 These sizes are derived as per the DWARFv5 standard. */
19816 if (cu->dwo_unit != nullptr)
19817 {
19818 if (cu->header.initial_length_size == 4)
19819 return LOCLIST_HEADER_SIZE32;
19820 return LOCLIST_HEADER_SIZE64;
19821 }
19822 return cu->loclist_base;
19823 }
19824
19825 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19826 array of offsets in the .debug_loclists section. */
19827 static CORE_ADDR
19828 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19829 {
19830 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19831 struct objfile *objfile = per_objfile->objfile;
19832 bfd *abfd = objfile->obfd;
19833 ULONGEST loclist_base = lookup_loclist_base (cu);
19834 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19835
19836 section->read (objfile);
19837 if (section->buffer == NULL)
19838 complaint (_("DW_FORM_loclistx used without .debug_loclists "
19839 "section [in module %s]"), objfile_name (objfile));
19840 struct loclists_rnglists_header header;
19841 read_loclists_rnglists_header (&header, section);
19842 if (loclist_index >= header.offset_entry_count)
19843 complaint (_("DW_FORM_loclistx pointing outside of "
19844 ".debug_loclists offset array [in module %s]"),
19845 objfile_name (objfile));
19846 if (loclist_base + loclist_index * cu->header.offset_size
19847 >= section->size)
19848 complaint (_("DW_FORM_loclistx pointing outside of "
19849 ".debug_loclists section [in module %s]"),
19850 objfile_name (objfile));
19851 const gdb_byte *info_ptr
19852 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
19853
19854 if (cu->header.offset_size == 4)
19855 return bfd_get_32 (abfd, info_ptr) + loclist_base;
19856 else
19857 return bfd_get_64 (abfd, info_ptr) + loclist_base;
19858 }
19859
19860 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19861 array of offsets in the .debug_rnglists section. */
19862 static CORE_ADDR
19863 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19864 dwarf_tag tag)
19865 {
19866 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19867 struct objfile *objfile = dwarf2_per_objfile->objfile;
19868 bfd *abfd = objfile->obfd;
19869 ULONGEST rnglist_header_size =
19870 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19871 : RNGLIST_HEADER_SIZE64);
19872 ULONGEST rnglist_base =
19873 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->ranges_base;
19874 ULONGEST start_offset =
19875 rnglist_base + rnglist_index * cu->header.offset_size;
19876
19877 /* Get rnglists section. */
19878 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19879
19880 /* Read the rnglists section content. */
19881 section->read (objfile);
19882 if (section->buffer == nullptr)
19883 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19884 "[in module %s]"),
19885 objfile_name (objfile));
19886
19887 /* Verify the rnglist index is valid. */
19888 struct loclists_rnglists_header header;
19889 read_loclists_rnglists_header (&header, section);
19890 if (rnglist_index >= header.offset_entry_count)
19891 error (_("DW_FORM_rnglistx index pointing outside of "
19892 ".debug_rnglists offset array [in module %s]"),
19893 objfile_name (objfile));
19894
19895 /* Validate that the offset is within the section's range. */
19896 if (start_offset >= section->size)
19897 error (_("DW_FORM_rnglistx pointing outside of "
19898 ".debug_rnglists section [in module %s]"),
19899 objfile_name (objfile));
19900
19901 /* Validate that reading won't go beyond the end of the section. */
19902 if (start_offset + cu->header.offset_size > rnglist_base + section->size)
19903 error (_("Reading DW_FORM_rnglistx index beyond end of"
19904 ".debug_rnglists section [in module %s]"),
19905 objfile_name (objfile));
19906
19907 const gdb_byte *info_ptr = section->buffer + start_offset;
19908
19909 if (cu->header.offset_size == 4)
19910 return read_4_bytes (abfd, info_ptr) + rnglist_base;
19911 else
19912 return read_8_bytes (abfd, info_ptr) + rnglist_base;
19913 }
19914
19915 /* Process the attributes that had to be skipped in the first round. These
19916 attributes are the ones that need str_offsets_base or addr_base attributes.
19917 They could not have been processed in the first round, because at the time
19918 the values of str_offsets_base or addr_base may not have been known. */
19919 static void
19920 read_attribute_reprocess (const struct die_reader_specs *reader,
19921 struct attribute *attr, dwarf_tag tag)
19922 {
19923 struct dwarf2_cu *cu = reader->cu;
19924 switch (attr->form)
19925 {
19926 case DW_FORM_addrx:
19927 case DW_FORM_GNU_addr_index:
19928 attr->set_address (read_addr_index (cu,
19929 attr->as_unsigned_reprocess ()));
19930 break;
19931 case DW_FORM_loclistx:
19932 attr->set_address (read_loclist_index (cu, attr->as_unsigned ()));
19933 break;
19934 case DW_FORM_rnglistx:
19935 attr->set_address (read_rnglist_index (cu, attr->as_unsigned (), tag));
19936 break;
19937 case DW_FORM_strx:
19938 case DW_FORM_strx1:
19939 case DW_FORM_strx2:
19940 case DW_FORM_strx3:
19941 case DW_FORM_strx4:
19942 case DW_FORM_GNU_str_index:
19943 {
19944 unsigned int str_index = attr->as_unsigned_reprocess ();
19945 gdb_assert (!attr->canonical_string_p ());
19946 if (reader->dwo_file != NULL)
19947 attr->set_string_noncanonical (read_dwo_str_index (reader,
19948 str_index));
19949 else
19950 attr->set_string_noncanonical (read_stub_str_index (cu,
19951 str_index));
19952 break;
19953 }
19954 default:
19955 gdb_assert_not_reached (_("Unexpected DWARF form."));
19956 }
19957 }
19958
19959 /* Read an attribute value described by an attribute form. */
19960
19961 static const gdb_byte *
19962 read_attribute_value (const struct die_reader_specs *reader,
19963 struct attribute *attr, unsigned form,
19964 LONGEST implicit_const, const gdb_byte *info_ptr)
19965 {
19966 struct dwarf2_cu *cu = reader->cu;
19967 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19968 struct objfile *objfile = per_objfile->objfile;
19969 bfd *abfd = reader->abfd;
19970 struct comp_unit_head *cu_header = &cu->header;
19971 unsigned int bytes_read;
19972 struct dwarf_block *blk;
19973
19974 attr->form = (enum dwarf_form) form;
19975 switch (form)
19976 {
19977 case DW_FORM_ref_addr:
19978 if (cu->header.version == 2)
19979 attr->set_unsigned (cu->header.read_address (abfd, info_ptr,
19980 &bytes_read));
19981 else
19982 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
19983 &bytes_read));
19984 info_ptr += bytes_read;
19985 break;
19986 case DW_FORM_GNU_ref_alt:
19987 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
19988 &bytes_read));
19989 info_ptr += bytes_read;
19990 break;
19991 case DW_FORM_addr:
19992 {
19993 struct gdbarch *gdbarch = objfile->arch ();
19994 CORE_ADDR addr = cu->header.read_address (abfd, info_ptr, &bytes_read);
19995 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
19996 attr->set_address (addr);
19997 info_ptr += bytes_read;
19998 }
19999 break;
20000 case DW_FORM_block2:
20001 blk = dwarf_alloc_block (cu);
20002 blk->size = read_2_bytes (abfd, info_ptr);
20003 info_ptr += 2;
20004 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20005 info_ptr += blk->size;
20006 attr->set_block (blk);
20007 break;
20008 case DW_FORM_block4:
20009 blk = dwarf_alloc_block (cu);
20010 blk->size = read_4_bytes (abfd, info_ptr);
20011 info_ptr += 4;
20012 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20013 info_ptr += blk->size;
20014 attr->set_block (blk);
20015 break;
20016 case DW_FORM_data2:
20017 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20018 info_ptr += 2;
20019 break;
20020 case DW_FORM_data4:
20021 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20022 info_ptr += 4;
20023 break;
20024 case DW_FORM_data8:
20025 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20026 info_ptr += 8;
20027 break;
20028 case DW_FORM_data16:
20029 blk = dwarf_alloc_block (cu);
20030 blk->size = 16;
20031 blk->data = read_n_bytes (abfd, info_ptr, 16);
20032 info_ptr += 16;
20033 attr->set_block (blk);
20034 break;
20035 case DW_FORM_sec_offset:
20036 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20037 &bytes_read));
20038 info_ptr += bytes_read;
20039 break;
20040 case DW_FORM_loclistx:
20041 {
20042 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20043 &bytes_read));
20044 info_ptr += bytes_read;
20045 }
20046 break;
20047 case DW_FORM_string:
20048 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20049 &bytes_read));
20050 info_ptr += bytes_read;
20051 break;
20052 case DW_FORM_strp:
20053 if (!cu->per_cu->is_dwz)
20054 {
20055 attr->set_string_noncanonical
20056 (read_indirect_string (per_objfile,
20057 abfd, info_ptr, cu_header,
20058 &bytes_read));
20059 info_ptr += bytes_read;
20060 break;
20061 }
20062 /* FALLTHROUGH */
20063 case DW_FORM_line_strp:
20064 if (!cu->per_cu->is_dwz)
20065 {
20066 attr->set_string_noncanonical
20067 (per_objfile->read_line_string (info_ptr, cu_header,
20068 &bytes_read));
20069 info_ptr += bytes_read;
20070 break;
20071 }
20072 /* FALLTHROUGH */
20073 case DW_FORM_GNU_strp_alt:
20074 {
20075 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
20076 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20077 &bytes_read);
20078
20079 attr->set_string_noncanonical
20080 (dwz->read_string (objfile, str_offset));
20081 info_ptr += bytes_read;
20082 }
20083 break;
20084 case DW_FORM_exprloc:
20085 case DW_FORM_block:
20086 blk = dwarf_alloc_block (cu);
20087 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20088 info_ptr += bytes_read;
20089 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20090 info_ptr += blk->size;
20091 attr->set_block (blk);
20092 break;
20093 case DW_FORM_block1:
20094 blk = dwarf_alloc_block (cu);
20095 blk->size = read_1_byte (abfd, info_ptr);
20096 info_ptr += 1;
20097 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20098 info_ptr += blk->size;
20099 attr->set_block (blk);
20100 break;
20101 case DW_FORM_data1:
20102 case DW_FORM_flag:
20103 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20104 info_ptr += 1;
20105 break;
20106 case DW_FORM_flag_present:
20107 attr->set_unsigned (1);
20108 break;
20109 case DW_FORM_sdata:
20110 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20111 info_ptr += bytes_read;
20112 break;
20113 case DW_FORM_rnglistx:
20114 {
20115 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20116 &bytes_read));
20117 info_ptr += bytes_read;
20118 }
20119 break;
20120 case DW_FORM_udata:
20121 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20122 info_ptr += bytes_read;
20123 break;
20124 case DW_FORM_ref1:
20125 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20126 + read_1_byte (abfd, info_ptr)));
20127 info_ptr += 1;
20128 break;
20129 case DW_FORM_ref2:
20130 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20131 + read_2_bytes (abfd, info_ptr)));
20132 info_ptr += 2;
20133 break;
20134 case DW_FORM_ref4:
20135 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20136 + read_4_bytes (abfd, info_ptr)));
20137 info_ptr += 4;
20138 break;
20139 case DW_FORM_ref8:
20140 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20141 + read_8_bytes (abfd, info_ptr)));
20142 info_ptr += 8;
20143 break;
20144 case DW_FORM_ref_sig8:
20145 attr->set_signature (read_8_bytes (abfd, info_ptr));
20146 info_ptr += 8;
20147 break;
20148 case DW_FORM_ref_udata:
20149 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20150 + read_unsigned_leb128 (abfd, info_ptr,
20151 &bytes_read)));
20152 info_ptr += bytes_read;
20153 break;
20154 case DW_FORM_indirect:
20155 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20156 info_ptr += bytes_read;
20157 if (form == DW_FORM_implicit_const)
20158 {
20159 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20160 info_ptr += bytes_read;
20161 }
20162 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20163 info_ptr);
20164 break;
20165 case DW_FORM_implicit_const:
20166 attr->set_signed (implicit_const);
20167 break;
20168 case DW_FORM_addrx:
20169 case DW_FORM_GNU_addr_index:
20170 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20171 &bytes_read));
20172 info_ptr += bytes_read;
20173 break;
20174 case DW_FORM_strx:
20175 case DW_FORM_strx1:
20176 case DW_FORM_strx2:
20177 case DW_FORM_strx3:
20178 case DW_FORM_strx4:
20179 case DW_FORM_GNU_str_index:
20180 {
20181 ULONGEST str_index;
20182 if (form == DW_FORM_strx1)
20183 {
20184 str_index = read_1_byte (abfd, info_ptr);
20185 info_ptr += 1;
20186 }
20187 else if (form == DW_FORM_strx2)
20188 {
20189 str_index = read_2_bytes (abfd, info_ptr);
20190 info_ptr += 2;
20191 }
20192 else if (form == DW_FORM_strx3)
20193 {
20194 str_index = read_3_bytes (abfd, info_ptr);
20195 info_ptr += 3;
20196 }
20197 else if (form == DW_FORM_strx4)
20198 {
20199 str_index = read_4_bytes (abfd, info_ptr);
20200 info_ptr += 4;
20201 }
20202 else
20203 {
20204 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20205 info_ptr += bytes_read;
20206 }
20207 attr->set_unsigned_reprocess (str_index);
20208 }
20209 break;
20210 default:
20211 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20212 dwarf_form_name (form),
20213 bfd_get_filename (abfd));
20214 }
20215
20216 /* Super hack. */
20217 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20218 attr->form = DW_FORM_GNU_ref_alt;
20219
20220 /* We have seen instances where the compiler tried to emit a byte
20221 size attribute of -1 which ended up being encoded as an unsigned
20222 0xffffffff. Although 0xffffffff is technically a valid size value,
20223 an object of this size seems pretty unlikely so we can relatively
20224 safely treat these cases as if the size attribute was invalid and
20225 treat them as zero by default. */
20226 if (attr->name == DW_AT_byte_size
20227 && form == DW_FORM_data4
20228 && attr->as_unsigned () >= 0xffffffff)
20229 {
20230 complaint
20231 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20232 hex_string (attr->as_unsigned ()));
20233 attr->set_unsigned (0);
20234 }
20235
20236 return info_ptr;
20237 }
20238
20239 /* Read an attribute described by an abbreviated attribute. */
20240
20241 static const gdb_byte *
20242 read_attribute (const struct die_reader_specs *reader,
20243 struct attribute *attr, struct attr_abbrev *abbrev,
20244 const gdb_byte *info_ptr)
20245 {
20246 attr->name = abbrev->name;
20247 attr->string_is_canonical = 0;
20248 attr->requires_reprocessing = 0;
20249 return read_attribute_value (reader, attr, abbrev->form,
20250 abbrev->implicit_const, info_ptr);
20251 }
20252
20253 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20254
20255 static const char *
20256 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20257 LONGEST str_offset)
20258 {
20259 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20260 str_offset, "DW_FORM_strp");
20261 }
20262
20263 /* Return pointer to string at .debug_str offset as read from BUF.
20264 BUF is assumed to be in a compilation unit described by CU_HEADER.
20265 Return *BYTES_READ_PTR count of bytes read from BUF. */
20266
20267 static const char *
20268 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20269 const gdb_byte *buf,
20270 const struct comp_unit_head *cu_header,
20271 unsigned int *bytes_read_ptr)
20272 {
20273 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20274
20275 return read_indirect_string_at_offset (per_objfile, str_offset);
20276 }
20277
20278 /* See read.h. */
20279
20280 const char *
20281 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20282 const struct comp_unit_head *cu_header,
20283 unsigned int *bytes_read_ptr)
20284 {
20285 bfd *abfd = objfile->obfd;
20286 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20287
20288 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20289 }
20290
20291 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20292 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20293 ADDR_SIZE is the size of addresses from the CU header. */
20294
20295 static CORE_ADDR
20296 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20297 gdb::optional<ULONGEST> addr_base, int addr_size)
20298 {
20299 struct objfile *objfile = per_objfile->objfile;
20300 bfd *abfd = objfile->obfd;
20301 const gdb_byte *info_ptr;
20302 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20303
20304 per_objfile->per_bfd->addr.read (objfile);
20305 if (per_objfile->per_bfd->addr.buffer == NULL)
20306 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20307 objfile_name (objfile));
20308 if (addr_base_or_zero + addr_index * addr_size
20309 >= per_objfile->per_bfd->addr.size)
20310 error (_("DW_FORM_addr_index pointing outside of "
20311 ".debug_addr section [in module %s]"),
20312 objfile_name (objfile));
20313 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20314 + addr_index * addr_size);
20315 if (addr_size == 4)
20316 return bfd_get_32 (abfd, info_ptr);
20317 else
20318 return bfd_get_64 (abfd, info_ptr);
20319 }
20320
20321 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20322
20323 static CORE_ADDR
20324 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20325 {
20326 return read_addr_index_1 (cu->per_objfile, addr_index,
20327 cu->addr_base, cu->header.addr_size);
20328 }
20329
20330 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20331
20332 static CORE_ADDR
20333 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20334 unsigned int *bytes_read)
20335 {
20336 bfd *abfd = cu->per_objfile->objfile->obfd;
20337 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20338
20339 return read_addr_index (cu, addr_index);
20340 }
20341
20342 /* See read.h. */
20343
20344 CORE_ADDR
20345 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20346 dwarf2_per_objfile *per_objfile,
20347 unsigned int addr_index)
20348 {
20349 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20350 gdb::optional<ULONGEST> addr_base;
20351 int addr_size;
20352
20353 /* We need addr_base and addr_size.
20354 If we don't have PER_CU->cu, we have to get it.
20355 Nasty, but the alternative is storing the needed info in PER_CU,
20356 which at this point doesn't seem justified: it's not clear how frequently
20357 it would get used and it would increase the size of every PER_CU.
20358 Entry points like dwarf2_per_cu_addr_size do a similar thing
20359 so we're not in uncharted territory here.
20360 Alas we need to be a bit more complicated as addr_base is contained
20361 in the DIE.
20362
20363 We don't need to read the entire CU(/TU).
20364 We just need the header and top level die.
20365
20366 IWBN to use the aging mechanism to let us lazily later discard the CU.
20367 For now we skip this optimization. */
20368
20369 if (cu != NULL)
20370 {
20371 addr_base = cu->addr_base;
20372 addr_size = cu->header.addr_size;
20373 }
20374 else
20375 {
20376 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20377 addr_base = reader.cu->addr_base;
20378 addr_size = reader.cu->header.addr_size;
20379 }
20380
20381 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20382 }
20383
20384 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20385 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20386 DWO file. */
20387
20388 static const char *
20389 read_str_index (struct dwarf2_cu *cu,
20390 struct dwarf2_section_info *str_section,
20391 struct dwarf2_section_info *str_offsets_section,
20392 ULONGEST str_offsets_base, ULONGEST str_index)
20393 {
20394 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20395 struct objfile *objfile = per_objfile->objfile;
20396 const char *objf_name = objfile_name (objfile);
20397 bfd *abfd = objfile->obfd;
20398 const gdb_byte *info_ptr;
20399 ULONGEST str_offset;
20400 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20401
20402 str_section->read (objfile);
20403 str_offsets_section->read (objfile);
20404 if (str_section->buffer == NULL)
20405 error (_("%s used without %s section"
20406 " in CU at offset %s [in module %s]"),
20407 form_name, str_section->get_name (),
20408 sect_offset_str (cu->header.sect_off), objf_name);
20409 if (str_offsets_section->buffer == NULL)
20410 error (_("%s used without %s section"
20411 " in CU at offset %s [in module %s]"),
20412 form_name, str_section->get_name (),
20413 sect_offset_str (cu->header.sect_off), objf_name);
20414 info_ptr = (str_offsets_section->buffer
20415 + str_offsets_base
20416 + str_index * cu->header.offset_size);
20417 if (cu->header.offset_size == 4)
20418 str_offset = bfd_get_32 (abfd, info_ptr);
20419 else
20420 str_offset = bfd_get_64 (abfd, info_ptr);
20421 if (str_offset >= str_section->size)
20422 error (_("Offset from %s pointing outside of"
20423 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20424 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20425 return (const char *) (str_section->buffer + str_offset);
20426 }
20427
20428 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20429
20430 static const char *
20431 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20432 {
20433 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20434 ? reader->cu->header.addr_size : 0;
20435 return read_str_index (reader->cu,
20436 &reader->dwo_file->sections.str,
20437 &reader->dwo_file->sections.str_offsets,
20438 str_offsets_base, str_index);
20439 }
20440
20441 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20442
20443 static const char *
20444 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20445 {
20446 struct objfile *objfile = cu->per_objfile->objfile;
20447 const char *objf_name = objfile_name (objfile);
20448 static const char form_name[] = "DW_FORM_GNU_str_index";
20449 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20450
20451 if (!cu->str_offsets_base.has_value ())
20452 error (_("%s used in Fission stub without %s"
20453 " in CU at offset 0x%lx [in module %s]"),
20454 form_name, str_offsets_attr_name,
20455 (long) cu->header.offset_size, objf_name);
20456
20457 return read_str_index (cu,
20458 &cu->per_objfile->per_bfd->str,
20459 &cu->per_objfile->per_bfd->str_offsets,
20460 *cu->str_offsets_base, str_index);
20461 }
20462
20463 /* Return the length of an LEB128 number in BUF. */
20464
20465 static int
20466 leb128_size (const gdb_byte *buf)
20467 {
20468 const gdb_byte *begin = buf;
20469 gdb_byte byte;
20470
20471 while (1)
20472 {
20473 byte = *buf++;
20474 if ((byte & 128) == 0)
20475 return buf - begin;
20476 }
20477 }
20478
20479 static void
20480 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20481 {
20482 switch (lang)
20483 {
20484 case DW_LANG_C89:
20485 case DW_LANG_C99:
20486 case DW_LANG_C11:
20487 case DW_LANG_C:
20488 case DW_LANG_UPC:
20489 cu->language = language_c;
20490 break;
20491 case DW_LANG_Java:
20492 case DW_LANG_C_plus_plus:
20493 case DW_LANG_C_plus_plus_11:
20494 case DW_LANG_C_plus_plus_14:
20495 cu->language = language_cplus;
20496 break;
20497 case DW_LANG_D:
20498 cu->language = language_d;
20499 break;
20500 case DW_LANG_Fortran77:
20501 case DW_LANG_Fortran90:
20502 case DW_LANG_Fortran95:
20503 case DW_LANG_Fortran03:
20504 case DW_LANG_Fortran08:
20505 cu->language = language_fortran;
20506 break;
20507 case DW_LANG_Go:
20508 cu->language = language_go;
20509 break;
20510 case DW_LANG_Mips_Assembler:
20511 cu->language = language_asm;
20512 break;
20513 case DW_LANG_Ada83:
20514 case DW_LANG_Ada95:
20515 cu->language = language_ada;
20516 break;
20517 case DW_LANG_Modula2:
20518 cu->language = language_m2;
20519 break;
20520 case DW_LANG_Pascal83:
20521 cu->language = language_pascal;
20522 break;
20523 case DW_LANG_ObjC:
20524 cu->language = language_objc;
20525 break;
20526 case DW_LANG_Rust:
20527 case DW_LANG_Rust_old:
20528 cu->language = language_rust;
20529 break;
20530 case DW_LANG_Cobol74:
20531 case DW_LANG_Cobol85:
20532 default:
20533 cu->language = language_minimal;
20534 break;
20535 }
20536 cu->language_defn = language_def (cu->language);
20537 }
20538
20539 /* Return the named attribute or NULL if not there. */
20540
20541 static struct attribute *
20542 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20543 {
20544 for (;;)
20545 {
20546 unsigned int i;
20547 struct attribute *spec = NULL;
20548
20549 for (i = 0; i < die->num_attrs; ++i)
20550 {
20551 if (die->attrs[i].name == name)
20552 return &die->attrs[i];
20553 if (die->attrs[i].name == DW_AT_specification
20554 || die->attrs[i].name == DW_AT_abstract_origin)
20555 spec = &die->attrs[i];
20556 }
20557
20558 if (!spec)
20559 break;
20560
20561 die = follow_die_ref (die, spec, &cu);
20562 }
20563
20564 return NULL;
20565 }
20566
20567 /* Return the string associated with a string-typed attribute, or NULL if it
20568 is either not found or is of an incorrect type. */
20569
20570 static const char *
20571 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20572 {
20573 struct attribute *attr;
20574 const char *str = NULL;
20575
20576 attr = dwarf2_attr (die, name, cu);
20577
20578 if (attr != NULL)
20579 {
20580 str = attr->as_string ();
20581 if (str == nullptr)
20582 complaint (_("string type expected for attribute %s for "
20583 "DIE at %s in module %s"),
20584 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20585 objfile_name (cu->per_objfile->objfile));
20586 }
20587
20588 return str;
20589 }
20590
20591 /* Return the dwo name or NULL if not present. If present, it is in either
20592 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20593 static const char *
20594 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20595 {
20596 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20597 if (dwo_name == nullptr)
20598 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20599 return dwo_name;
20600 }
20601
20602 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20603 and holds a non-zero value. This function should only be used for
20604 DW_FORM_flag or DW_FORM_flag_present attributes. */
20605
20606 static int
20607 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20608 {
20609 struct attribute *attr = dwarf2_attr (die, name, cu);
20610
20611 return attr != nullptr && attr->as_boolean ();
20612 }
20613
20614 static int
20615 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20616 {
20617 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20618 which value is non-zero. However, we have to be careful with
20619 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20620 (via dwarf2_flag_true_p) follows this attribute. So we may
20621 end up accidently finding a declaration attribute that belongs
20622 to a different DIE referenced by the specification attribute,
20623 even though the given DIE does not have a declaration attribute. */
20624 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20625 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20626 }
20627
20628 /* Return the die giving the specification for DIE, if there is
20629 one. *SPEC_CU is the CU containing DIE on input, and the CU
20630 containing the return value on output. If there is no
20631 specification, but there is an abstract origin, that is
20632 returned. */
20633
20634 static struct die_info *
20635 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20636 {
20637 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20638 *spec_cu);
20639
20640 if (spec_attr == NULL)
20641 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20642
20643 if (spec_attr == NULL)
20644 return NULL;
20645 else
20646 return follow_die_ref (die, spec_attr, spec_cu);
20647 }
20648
20649 /* Stub for free_line_header to match void * callback types. */
20650
20651 static void
20652 free_line_header_voidp (void *arg)
20653 {
20654 struct line_header *lh = (struct line_header *) arg;
20655
20656 delete lh;
20657 }
20658
20659 /* A convenience function to find the proper .debug_line section for a CU. */
20660
20661 static struct dwarf2_section_info *
20662 get_debug_line_section (struct dwarf2_cu *cu)
20663 {
20664 struct dwarf2_section_info *section;
20665 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20666
20667 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20668 DWO file. */
20669 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20670 section = &cu->dwo_unit->dwo_file->sections.line;
20671 else if (cu->per_cu->is_dwz)
20672 {
20673 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
20674
20675 section = &dwz->line;
20676 }
20677 else
20678 section = &per_objfile->per_bfd->line;
20679
20680 return section;
20681 }
20682
20683 /* Read the statement program header starting at OFFSET in
20684 .debug_line, or .debug_line.dwo. Return a pointer
20685 to a struct line_header, allocated using xmalloc.
20686 Returns NULL if there is a problem reading the header, e.g., if it
20687 has a version we don't understand.
20688
20689 NOTE: the strings in the include directory and file name tables of
20690 the returned object point into the dwarf line section buffer,
20691 and must not be freed. */
20692
20693 static line_header_up
20694 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20695 {
20696 struct dwarf2_section_info *section;
20697 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20698
20699 section = get_debug_line_section (cu);
20700 section->read (per_objfile->objfile);
20701 if (section->buffer == NULL)
20702 {
20703 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20704 complaint (_("missing .debug_line.dwo section"));
20705 else
20706 complaint (_("missing .debug_line section"));
20707 return 0;
20708 }
20709
20710 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
20711 per_objfile, section, &cu->header);
20712 }
20713
20714 /* Subroutine of dwarf_decode_lines to simplify it.
20715 Return the file name of the psymtab for the given file_entry.
20716 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20717 If space for the result is malloc'd, *NAME_HOLDER will be set.
20718 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20719
20720 static const char *
20721 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20722 const dwarf2_psymtab *pst,
20723 const char *comp_dir,
20724 gdb::unique_xmalloc_ptr<char> *name_holder)
20725 {
20726 const char *include_name = fe.name;
20727 const char *include_name_to_compare = include_name;
20728 const char *pst_filename;
20729 int file_is_pst;
20730
20731 const char *dir_name = fe.include_dir (lh);
20732
20733 gdb::unique_xmalloc_ptr<char> hold_compare;
20734 if (!IS_ABSOLUTE_PATH (include_name)
20735 && (dir_name != NULL || comp_dir != NULL))
20736 {
20737 /* Avoid creating a duplicate psymtab for PST.
20738 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20739 Before we do the comparison, however, we need to account
20740 for DIR_NAME and COMP_DIR.
20741 First prepend dir_name (if non-NULL). If we still don't
20742 have an absolute path prepend comp_dir (if non-NULL).
20743 However, the directory we record in the include-file's
20744 psymtab does not contain COMP_DIR (to match the
20745 corresponding symtab(s)).
20746
20747 Example:
20748
20749 bash$ cd /tmp
20750 bash$ gcc -g ./hello.c
20751 include_name = "hello.c"
20752 dir_name = "."
20753 DW_AT_comp_dir = comp_dir = "/tmp"
20754 DW_AT_name = "./hello.c"
20755
20756 */
20757
20758 if (dir_name != NULL)
20759 {
20760 name_holder->reset (concat (dir_name, SLASH_STRING,
20761 include_name, (char *) NULL));
20762 include_name = name_holder->get ();
20763 include_name_to_compare = include_name;
20764 }
20765 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20766 {
20767 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20768 include_name, (char *) NULL));
20769 include_name_to_compare = hold_compare.get ();
20770 }
20771 }
20772
20773 pst_filename = pst->filename;
20774 gdb::unique_xmalloc_ptr<char> copied_name;
20775 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20776 {
20777 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20778 pst_filename, (char *) NULL));
20779 pst_filename = copied_name.get ();
20780 }
20781
20782 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20783
20784 if (file_is_pst)
20785 return NULL;
20786 return include_name;
20787 }
20788
20789 /* State machine to track the state of the line number program. */
20790
20791 class lnp_state_machine
20792 {
20793 public:
20794 /* Initialize a machine state for the start of a line number
20795 program. */
20796 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20797 bool record_lines_p);
20798
20799 file_entry *current_file ()
20800 {
20801 /* lh->file_names is 0-based, but the file name numbers in the
20802 statement program are 1-based. */
20803 return m_line_header->file_name_at (m_file);
20804 }
20805
20806 /* Record the line in the state machine. END_SEQUENCE is true if
20807 we're processing the end of a sequence. */
20808 void record_line (bool end_sequence);
20809
20810 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
20811 nop-out rest of the lines in this sequence. */
20812 void check_line_address (struct dwarf2_cu *cu,
20813 const gdb_byte *line_ptr,
20814 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20815
20816 void handle_set_discriminator (unsigned int discriminator)
20817 {
20818 m_discriminator = discriminator;
20819 m_line_has_non_zero_discriminator |= discriminator != 0;
20820 }
20821
20822 /* Handle DW_LNE_set_address. */
20823 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20824 {
20825 m_op_index = 0;
20826 address += baseaddr;
20827 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20828 }
20829
20830 /* Handle DW_LNS_advance_pc. */
20831 void handle_advance_pc (CORE_ADDR adjust);
20832
20833 /* Handle a special opcode. */
20834 void handle_special_opcode (unsigned char op_code);
20835
20836 /* Handle DW_LNS_advance_line. */
20837 void handle_advance_line (int line_delta)
20838 {
20839 advance_line (line_delta);
20840 }
20841
20842 /* Handle DW_LNS_set_file. */
20843 void handle_set_file (file_name_index file);
20844
20845 /* Handle DW_LNS_negate_stmt. */
20846 void handle_negate_stmt ()
20847 {
20848 m_is_stmt = !m_is_stmt;
20849 }
20850
20851 /* Handle DW_LNS_const_add_pc. */
20852 void handle_const_add_pc ();
20853
20854 /* Handle DW_LNS_fixed_advance_pc. */
20855 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20856 {
20857 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20858 m_op_index = 0;
20859 }
20860
20861 /* Handle DW_LNS_copy. */
20862 void handle_copy ()
20863 {
20864 record_line (false);
20865 m_discriminator = 0;
20866 }
20867
20868 /* Handle DW_LNE_end_sequence. */
20869 void handle_end_sequence ()
20870 {
20871 m_currently_recording_lines = true;
20872 }
20873
20874 private:
20875 /* Advance the line by LINE_DELTA. */
20876 void advance_line (int line_delta)
20877 {
20878 m_line += line_delta;
20879
20880 if (line_delta != 0)
20881 m_line_has_non_zero_discriminator = m_discriminator != 0;
20882 }
20883
20884 struct dwarf2_cu *m_cu;
20885
20886 gdbarch *m_gdbarch;
20887
20888 /* True if we're recording lines.
20889 Otherwise we're building partial symtabs and are just interested in
20890 finding include files mentioned by the line number program. */
20891 bool m_record_lines_p;
20892
20893 /* The line number header. */
20894 line_header *m_line_header;
20895
20896 /* These are part of the standard DWARF line number state machine,
20897 and initialized according to the DWARF spec. */
20898
20899 unsigned char m_op_index = 0;
20900 /* The line table index of the current file. */
20901 file_name_index m_file = 1;
20902 unsigned int m_line = 1;
20903
20904 /* These are initialized in the constructor. */
20905
20906 CORE_ADDR m_address;
20907 bool m_is_stmt;
20908 unsigned int m_discriminator;
20909
20910 /* Additional bits of state we need to track. */
20911
20912 /* The last file that we called dwarf2_start_subfile for.
20913 This is only used for TLLs. */
20914 unsigned int m_last_file = 0;
20915 /* The last file a line number was recorded for. */
20916 struct subfile *m_last_subfile = NULL;
20917
20918 /* The address of the last line entry. */
20919 CORE_ADDR m_last_address;
20920
20921 /* Set to true when a previous line at the same address (using
20922 m_last_address) had m_is_stmt true. This is reset to false when a
20923 line entry at a new address (m_address different to m_last_address) is
20924 processed. */
20925 bool m_stmt_at_address = false;
20926
20927 /* When true, record the lines we decode. */
20928 bool m_currently_recording_lines = false;
20929
20930 /* The last line number that was recorded, used to coalesce
20931 consecutive entries for the same line. This can happen, for
20932 example, when discriminators are present. PR 17276. */
20933 unsigned int m_last_line = 0;
20934 bool m_line_has_non_zero_discriminator = false;
20935 };
20936
20937 void
20938 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20939 {
20940 CORE_ADDR addr_adj = (((m_op_index + adjust)
20941 / m_line_header->maximum_ops_per_instruction)
20942 * m_line_header->minimum_instruction_length);
20943 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20944 m_op_index = ((m_op_index + adjust)
20945 % m_line_header->maximum_ops_per_instruction);
20946 }
20947
20948 void
20949 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20950 {
20951 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20952 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20953 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20954 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20955 / m_line_header->maximum_ops_per_instruction)
20956 * m_line_header->minimum_instruction_length);
20957 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20958 m_op_index = ((m_op_index + adj_opcode_d)
20959 % m_line_header->maximum_ops_per_instruction);
20960
20961 int line_delta = m_line_header->line_base + adj_opcode_r;
20962 advance_line (line_delta);
20963 record_line (false);
20964 m_discriminator = 0;
20965 }
20966
20967 void
20968 lnp_state_machine::handle_set_file (file_name_index file)
20969 {
20970 m_file = file;
20971
20972 const file_entry *fe = current_file ();
20973 if (fe == NULL)
20974 dwarf2_debug_line_missing_file_complaint ();
20975 else if (m_record_lines_p)
20976 {
20977 const char *dir = fe->include_dir (m_line_header);
20978
20979 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20980 m_line_has_non_zero_discriminator = m_discriminator != 0;
20981 dwarf2_start_subfile (m_cu, fe->name, dir);
20982 }
20983 }
20984
20985 void
20986 lnp_state_machine::handle_const_add_pc ()
20987 {
20988 CORE_ADDR adjust
20989 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20990
20991 CORE_ADDR addr_adj
20992 = (((m_op_index + adjust)
20993 / m_line_header->maximum_ops_per_instruction)
20994 * m_line_header->minimum_instruction_length);
20995
20996 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20997 m_op_index = ((m_op_index + adjust)
20998 % m_line_header->maximum_ops_per_instruction);
20999 }
21000
21001 /* Return non-zero if we should add LINE to the line number table.
21002 LINE is the line to add, LAST_LINE is the last line that was added,
21003 LAST_SUBFILE is the subfile for LAST_LINE.
21004 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21005 had a non-zero discriminator.
21006
21007 We have to be careful in the presence of discriminators.
21008 E.g., for this line:
21009
21010 for (i = 0; i < 100000; i++);
21011
21012 clang can emit four line number entries for that one line,
21013 each with a different discriminator.
21014 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21015
21016 However, we want gdb to coalesce all four entries into one.
21017 Otherwise the user could stepi into the middle of the line and
21018 gdb would get confused about whether the pc really was in the
21019 middle of the line.
21020
21021 Things are further complicated by the fact that two consecutive
21022 line number entries for the same line is a heuristic used by gcc
21023 to denote the end of the prologue. So we can't just discard duplicate
21024 entries, we have to be selective about it. The heuristic we use is
21025 that we only collapse consecutive entries for the same line if at least
21026 one of those entries has a non-zero discriminator. PR 17276.
21027
21028 Note: Addresses in the line number state machine can never go backwards
21029 within one sequence, thus this coalescing is ok. */
21030
21031 static int
21032 dwarf_record_line_p (struct dwarf2_cu *cu,
21033 unsigned int line, unsigned int last_line,
21034 int line_has_non_zero_discriminator,
21035 struct subfile *last_subfile)
21036 {
21037 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21038 return 1;
21039 if (line != last_line)
21040 return 1;
21041 /* Same line for the same file that we've seen already.
21042 As a last check, for pr 17276, only record the line if the line
21043 has never had a non-zero discriminator. */
21044 if (!line_has_non_zero_discriminator)
21045 return 1;
21046 return 0;
21047 }
21048
21049 /* Use the CU's builder to record line number LINE beginning at
21050 address ADDRESS in the line table of subfile SUBFILE. */
21051
21052 static void
21053 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21054 unsigned int line, CORE_ADDR address, bool is_stmt,
21055 struct dwarf2_cu *cu)
21056 {
21057 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21058
21059 if (dwarf_line_debug)
21060 {
21061 fprintf_unfiltered (gdb_stdlog,
21062 "Recording line %u, file %s, address %s\n",
21063 line, lbasename (subfile->name),
21064 paddress (gdbarch, address));
21065 }
21066
21067 if (cu != nullptr)
21068 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
21069 }
21070
21071 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21072 Mark the end of a set of line number records.
21073 The arguments are the same as for dwarf_record_line_1.
21074 If SUBFILE is NULL the request is ignored. */
21075
21076 static void
21077 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21078 CORE_ADDR address, struct dwarf2_cu *cu)
21079 {
21080 if (subfile == NULL)
21081 return;
21082
21083 if (dwarf_line_debug)
21084 {
21085 fprintf_unfiltered (gdb_stdlog,
21086 "Finishing current line, file %s, address %s\n",
21087 lbasename (subfile->name),
21088 paddress (gdbarch, address));
21089 }
21090
21091 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
21092 }
21093
21094 void
21095 lnp_state_machine::record_line (bool end_sequence)
21096 {
21097 if (dwarf_line_debug)
21098 {
21099 fprintf_unfiltered (gdb_stdlog,
21100 "Processing actual line %u: file %u,"
21101 " address %s, is_stmt %u, discrim %u%s\n",
21102 m_line, m_file,
21103 paddress (m_gdbarch, m_address),
21104 m_is_stmt, m_discriminator,
21105 (end_sequence ? "\t(end sequence)" : ""));
21106 }
21107
21108 file_entry *fe = current_file ();
21109
21110 if (fe == NULL)
21111 dwarf2_debug_line_missing_file_complaint ();
21112 /* For now we ignore lines not starting on an instruction boundary.
21113 But not when processing end_sequence for compatibility with the
21114 previous version of the code. */
21115 else if (m_op_index == 0 || end_sequence)
21116 {
21117 fe->included_p = 1;
21118 if (m_record_lines_p)
21119 {
21120 /* When we switch files we insert an end maker in the first file,
21121 switch to the second file and add a new line entry. The
21122 problem is that the end marker inserted in the first file will
21123 discard any previous line entries at the same address. If the
21124 line entries in the first file are marked as is-stmt, while
21125 the new line in the second file is non-stmt, then this means
21126 the end marker will discard is-stmt lines so we can have a
21127 non-stmt line. This means that there are less addresses at
21128 which the user can insert a breakpoint.
21129
21130 To improve this we track the last address in m_last_address,
21131 and whether we have seen an is-stmt at this address. Then
21132 when switching files, if we have seen a stmt at the current
21133 address, and we are switching to create a non-stmt line, then
21134 discard the new line. */
21135 bool file_changed
21136 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21137 bool ignore_this_line
21138 = ((file_changed && !end_sequence && m_last_address == m_address
21139 && !m_is_stmt && m_stmt_at_address)
21140 || (!end_sequence && m_line == 0));
21141
21142 if ((file_changed && !ignore_this_line) || end_sequence)
21143 {
21144 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21145 m_currently_recording_lines ? m_cu : nullptr);
21146 }
21147
21148 if (!end_sequence && !ignore_this_line)
21149 {
21150 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21151
21152 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21153 m_line_has_non_zero_discriminator,
21154 m_last_subfile))
21155 {
21156 buildsym_compunit *builder = m_cu->get_builder ();
21157 dwarf_record_line_1 (m_gdbarch,
21158 builder->get_current_subfile (),
21159 m_line, m_address, is_stmt,
21160 m_currently_recording_lines ? m_cu : nullptr);
21161 }
21162 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21163 m_last_line = m_line;
21164 }
21165 }
21166 }
21167
21168 /* Track whether we have seen any m_is_stmt true at m_address in case we
21169 have multiple line table entries all at m_address. */
21170 if (m_last_address != m_address)
21171 {
21172 m_stmt_at_address = false;
21173 m_last_address = m_address;
21174 }
21175 m_stmt_at_address |= m_is_stmt;
21176 }
21177
21178 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21179 line_header *lh, bool record_lines_p)
21180 {
21181 m_cu = cu;
21182 m_gdbarch = arch;
21183 m_record_lines_p = record_lines_p;
21184 m_line_header = lh;
21185
21186 m_currently_recording_lines = true;
21187
21188 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21189 was a line entry for it so that the backend has a chance to adjust it
21190 and also record it in case it needs it. This is currently used by MIPS
21191 code, cf. `mips_adjust_dwarf2_line'. */
21192 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21193 m_is_stmt = lh->default_is_stmt;
21194 m_discriminator = 0;
21195
21196 m_last_address = m_address;
21197 m_stmt_at_address = false;
21198 }
21199
21200 void
21201 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21202 const gdb_byte *line_ptr,
21203 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21204 {
21205 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21206 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21207 located at 0x0. In this case, additionally check that if
21208 ADDRESS < UNRELOCATED_LOWPC. */
21209
21210 if ((address == 0 && address < unrelocated_lowpc)
21211 || address == (CORE_ADDR) -1)
21212 {
21213 /* This line table is for a function which has been
21214 GCd by the linker. Ignore it. PR gdb/12528 */
21215
21216 struct objfile *objfile = cu->per_objfile->objfile;
21217 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21218
21219 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21220 line_offset, objfile_name (objfile));
21221 m_currently_recording_lines = false;
21222 /* Note: m_currently_recording_lines is left as false until we see
21223 DW_LNE_end_sequence. */
21224 }
21225 }
21226
21227 /* Subroutine of dwarf_decode_lines to simplify it.
21228 Process the line number information in LH.
21229 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21230 program in order to set included_p for every referenced header. */
21231
21232 static void
21233 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21234 const int decode_for_pst_p, CORE_ADDR lowpc)
21235 {
21236 const gdb_byte *line_ptr, *extended_end;
21237 const gdb_byte *line_end;
21238 unsigned int bytes_read, extended_len;
21239 unsigned char op_code, extended_op;
21240 CORE_ADDR baseaddr;
21241 struct objfile *objfile = cu->per_objfile->objfile;
21242 bfd *abfd = objfile->obfd;
21243 struct gdbarch *gdbarch = objfile->arch ();
21244 /* True if we're recording line info (as opposed to building partial
21245 symtabs and just interested in finding include files mentioned by
21246 the line number program). */
21247 bool record_lines_p = !decode_for_pst_p;
21248
21249 baseaddr = objfile->text_section_offset ();
21250
21251 line_ptr = lh->statement_program_start;
21252 line_end = lh->statement_program_end;
21253
21254 /* Read the statement sequences until there's nothing left. */
21255 while (line_ptr < line_end)
21256 {
21257 /* The DWARF line number program state machine. Reset the state
21258 machine at the start of each sequence. */
21259 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21260 bool end_sequence = false;
21261
21262 if (record_lines_p)
21263 {
21264 /* Start a subfile for the current file of the state
21265 machine. */
21266 const file_entry *fe = state_machine.current_file ();
21267
21268 if (fe != NULL)
21269 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21270 }
21271
21272 /* Decode the table. */
21273 while (line_ptr < line_end && !end_sequence)
21274 {
21275 op_code = read_1_byte (abfd, line_ptr);
21276 line_ptr += 1;
21277
21278 if (op_code >= lh->opcode_base)
21279 {
21280 /* Special opcode. */
21281 state_machine.handle_special_opcode (op_code);
21282 }
21283 else switch (op_code)
21284 {
21285 case DW_LNS_extended_op:
21286 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21287 &bytes_read);
21288 line_ptr += bytes_read;
21289 extended_end = line_ptr + extended_len;
21290 extended_op = read_1_byte (abfd, line_ptr);
21291 line_ptr += 1;
21292 if (DW_LNE_lo_user <= extended_op
21293 && extended_op <= DW_LNE_hi_user)
21294 {
21295 /* Vendor extension, ignore. */
21296 line_ptr = extended_end;
21297 break;
21298 }
21299 switch (extended_op)
21300 {
21301 case DW_LNE_end_sequence:
21302 state_machine.handle_end_sequence ();
21303 end_sequence = true;
21304 break;
21305 case DW_LNE_set_address:
21306 {
21307 CORE_ADDR address
21308 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21309 line_ptr += bytes_read;
21310
21311 state_machine.check_line_address (cu, line_ptr,
21312 lowpc - baseaddr, address);
21313 state_machine.handle_set_address (baseaddr, address);
21314 }
21315 break;
21316 case DW_LNE_define_file:
21317 {
21318 const char *cur_file;
21319 unsigned int mod_time, length;
21320 dir_index dindex;
21321
21322 cur_file = read_direct_string (abfd, line_ptr,
21323 &bytes_read);
21324 line_ptr += bytes_read;
21325 dindex = (dir_index)
21326 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21327 line_ptr += bytes_read;
21328 mod_time =
21329 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21330 line_ptr += bytes_read;
21331 length =
21332 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21333 line_ptr += bytes_read;
21334 lh->add_file_name (cur_file, dindex, mod_time, length);
21335 }
21336 break;
21337 case DW_LNE_set_discriminator:
21338 {
21339 /* The discriminator is not interesting to the
21340 debugger; just ignore it. We still need to
21341 check its value though:
21342 if there are consecutive entries for the same
21343 (non-prologue) line we want to coalesce them.
21344 PR 17276. */
21345 unsigned int discr
21346 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21347 line_ptr += bytes_read;
21348
21349 state_machine.handle_set_discriminator (discr);
21350 }
21351 break;
21352 default:
21353 complaint (_("mangled .debug_line section"));
21354 return;
21355 }
21356 /* Make sure that we parsed the extended op correctly. If e.g.
21357 we expected a different address size than the producer used,
21358 we may have read the wrong number of bytes. */
21359 if (line_ptr != extended_end)
21360 {
21361 complaint (_("mangled .debug_line section"));
21362 return;
21363 }
21364 break;
21365 case DW_LNS_copy:
21366 state_machine.handle_copy ();
21367 break;
21368 case DW_LNS_advance_pc:
21369 {
21370 CORE_ADDR adjust
21371 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21372 line_ptr += bytes_read;
21373
21374 state_machine.handle_advance_pc (adjust);
21375 }
21376 break;
21377 case DW_LNS_advance_line:
21378 {
21379 int line_delta
21380 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21381 line_ptr += bytes_read;
21382
21383 state_machine.handle_advance_line (line_delta);
21384 }
21385 break;
21386 case DW_LNS_set_file:
21387 {
21388 file_name_index file
21389 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21390 &bytes_read);
21391 line_ptr += bytes_read;
21392
21393 state_machine.handle_set_file (file);
21394 }
21395 break;
21396 case DW_LNS_set_column:
21397 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21398 line_ptr += bytes_read;
21399 break;
21400 case DW_LNS_negate_stmt:
21401 state_machine.handle_negate_stmt ();
21402 break;
21403 case DW_LNS_set_basic_block:
21404 break;
21405 /* Add to the address register of the state machine the
21406 address increment value corresponding to special opcode
21407 255. I.e., this value is scaled by the minimum
21408 instruction length since special opcode 255 would have
21409 scaled the increment. */
21410 case DW_LNS_const_add_pc:
21411 state_machine.handle_const_add_pc ();
21412 break;
21413 case DW_LNS_fixed_advance_pc:
21414 {
21415 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21416 line_ptr += 2;
21417
21418 state_machine.handle_fixed_advance_pc (addr_adj);
21419 }
21420 break;
21421 default:
21422 {
21423 /* Unknown standard opcode, ignore it. */
21424 int i;
21425
21426 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21427 {
21428 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21429 line_ptr += bytes_read;
21430 }
21431 }
21432 }
21433 }
21434
21435 if (!end_sequence)
21436 dwarf2_debug_line_missing_end_sequence_complaint ();
21437
21438 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21439 in which case we still finish recording the last line). */
21440 state_machine.record_line (true);
21441 }
21442 }
21443
21444 /* Decode the Line Number Program (LNP) for the given line_header
21445 structure and CU. The actual information extracted and the type
21446 of structures created from the LNP depends on the value of PST.
21447
21448 1. If PST is NULL, then this procedure uses the data from the program
21449 to create all necessary symbol tables, and their linetables.
21450
21451 2. If PST is not NULL, this procedure reads the program to determine
21452 the list of files included by the unit represented by PST, and
21453 builds all the associated partial symbol tables.
21454
21455 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21456 It is used for relative paths in the line table.
21457 NOTE: When processing partial symtabs (pst != NULL),
21458 comp_dir == pst->dirname.
21459
21460 NOTE: It is important that psymtabs have the same file name (via strcmp)
21461 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21462 symtab we don't use it in the name of the psymtabs we create.
21463 E.g. expand_line_sal requires this when finding psymtabs to expand.
21464 A good testcase for this is mb-inline.exp.
21465
21466 LOWPC is the lowest address in CU (or 0 if not known).
21467
21468 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21469 for its PC<->lines mapping information. Otherwise only the filename
21470 table is read in. */
21471
21472 static void
21473 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21474 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21475 CORE_ADDR lowpc, int decode_mapping)
21476 {
21477 struct objfile *objfile = cu->per_objfile->objfile;
21478 const int decode_for_pst_p = (pst != NULL);
21479
21480 if (decode_mapping)
21481 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21482
21483 if (decode_for_pst_p)
21484 {
21485 /* Now that we're done scanning the Line Header Program, we can
21486 create the psymtab of each included file. */
21487 for (auto &file_entry : lh->file_names ())
21488 if (file_entry.included_p == 1)
21489 {
21490 gdb::unique_xmalloc_ptr<char> name_holder;
21491 const char *include_name =
21492 psymtab_include_file_name (lh, file_entry, pst,
21493 comp_dir, &name_holder);
21494 if (include_name != NULL)
21495 dwarf2_create_include_psymtab (include_name, pst, objfile);
21496 }
21497 }
21498 else
21499 {
21500 /* Make sure a symtab is created for every file, even files
21501 which contain only variables (i.e. no code with associated
21502 line numbers). */
21503 buildsym_compunit *builder = cu->get_builder ();
21504 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21505
21506 for (auto &fe : lh->file_names ())
21507 {
21508 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21509 if (builder->get_current_subfile ()->symtab == NULL)
21510 {
21511 builder->get_current_subfile ()->symtab
21512 = allocate_symtab (cust,
21513 builder->get_current_subfile ()->name);
21514 }
21515 fe.symtab = builder->get_current_subfile ()->symtab;
21516 }
21517 }
21518 }
21519
21520 /* Start a subfile for DWARF. FILENAME is the name of the file and
21521 DIRNAME the name of the source directory which contains FILENAME
21522 or NULL if not known.
21523 This routine tries to keep line numbers from identical absolute and
21524 relative file names in a common subfile.
21525
21526 Using the `list' example from the GDB testsuite, which resides in
21527 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21528 of /srcdir/list0.c yields the following debugging information for list0.c:
21529
21530 DW_AT_name: /srcdir/list0.c
21531 DW_AT_comp_dir: /compdir
21532 files.files[0].name: list0.h
21533 files.files[0].dir: /srcdir
21534 files.files[1].name: list0.c
21535 files.files[1].dir: /srcdir
21536
21537 The line number information for list0.c has to end up in a single
21538 subfile, so that `break /srcdir/list0.c:1' works as expected.
21539 start_subfile will ensure that this happens provided that we pass the
21540 concatenation of files.files[1].dir and files.files[1].name as the
21541 subfile's name. */
21542
21543 static void
21544 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21545 const char *dirname)
21546 {
21547 gdb::unique_xmalloc_ptr<char> copy;
21548
21549 /* In order not to lose the line information directory,
21550 we concatenate it to the filename when it makes sense.
21551 Note that the Dwarf3 standard says (speaking of filenames in line
21552 information): ``The directory index is ignored for file names
21553 that represent full path names''. Thus ignoring dirname in the
21554 `else' branch below isn't an issue. */
21555
21556 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21557 {
21558 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21559 filename = copy.get ();
21560 }
21561
21562 cu->get_builder ()->start_subfile (filename);
21563 }
21564
21565 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21566 buildsym_compunit constructor. */
21567
21568 struct compunit_symtab *
21569 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21570 CORE_ADDR low_pc)
21571 {
21572 gdb_assert (m_builder == nullptr);
21573
21574 m_builder.reset (new struct buildsym_compunit
21575 (this->per_objfile->objfile,
21576 name, comp_dir, language, low_pc));
21577
21578 list_in_scope = get_builder ()->get_file_symbols ();
21579
21580 get_builder ()->record_debugformat ("DWARF 2");
21581 get_builder ()->record_producer (producer);
21582
21583 processing_has_namespace_info = false;
21584
21585 return get_builder ()->get_compunit_symtab ();
21586 }
21587
21588 static void
21589 var_decode_location (struct attribute *attr, struct symbol *sym,
21590 struct dwarf2_cu *cu)
21591 {
21592 struct objfile *objfile = cu->per_objfile->objfile;
21593 struct comp_unit_head *cu_header = &cu->header;
21594
21595 /* NOTE drow/2003-01-30: There used to be a comment and some special
21596 code here to turn a symbol with DW_AT_external and a
21597 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21598 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21599 with some versions of binutils) where shared libraries could have
21600 relocations against symbols in their debug information - the
21601 minimal symbol would have the right address, but the debug info
21602 would not. It's no longer necessary, because we will explicitly
21603 apply relocations when we read in the debug information now. */
21604
21605 /* A DW_AT_location attribute with no contents indicates that a
21606 variable has been optimized away. */
21607 if (attr->form_is_block () && attr->as_block ()->size == 0)
21608 {
21609 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21610 return;
21611 }
21612
21613 /* Handle one degenerate form of location expression specially, to
21614 preserve GDB's previous behavior when section offsets are
21615 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21616 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21617
21618 if (attr->form_is_block ())
21619 {
21620 struct dwarf_block *block = attr->as_block ();
21621
21622 if ((block->data[0] == DW_OP_addr
21623 && block->size == 1 + cu_header->addr_size)
21624 || ((block->data[0] == DW_OP_GNU_addr_index
21625 || block->data[0] == DW_OP_addrx)
21626 && (block->size
21627 == 1 + leb128_size (&block->data[1]))))
21628 {
21629 unsigned int dummy;
21630
21631 if (block->data[0] == DW_OP_addr)
21632 SET_SYMBOL_VALUE_ADDRESS
21633 (sym, cu->header.read_address (objfile->obfd,
21634 block->data + 1,
21635 &dummy));
21636 else
21637 SET_SYMBOL_VALUE_ADDRESS
21638 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21639 &dummy));
21640 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21641 fixup_symbol_section (sym, objfile);
21642 SET_SYMBOL_VALUE_ADDRESS
21643 (sym,
21644 SYMBOL_VALUE_ADDRESS (sym)
21645 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
21646 return;
21647 }
21648 }
21649
21650 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21651 expression evaluator, and use LOC_COMPUTED only when necessary
21652 (i.e. when the value of a register or memory location is
21653 referenced, or a thread-local block, etc.). Then again, it might
21654 not be worthwhile. I'm assuming that it isn't unless performance
21655 or memory numbers show me otherwise. */
21656
21657 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21658
21659 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21660 cu->has_loclist = true;
21661 }
21662
21663 /* Given a pointer to a DWARF information entry, figure out if we need
21664 to make a symbol table entry for it, and if so, create a new entry
21665 and return a pointer to it.
21666 If TYPE is NULL, determine symbol type from the die, otherwise
21667 used the passed type.
21668 If SPACE is not NULL, use it to hold the new symbol. If it is
21669 NULL, allocate a new symbol on the objfile's obstack. */
21670
21671 static struct symbol *
21672 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21673 struct symbol *space)
21674 {
21675 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21676 struct objfile *objfile = per_objfile->objfile;
21677 struct gdbarch *gdbarch = objfile->arch ();
21678 struct symbol *sym = NULL;
21679 const char *name;
21680 struct attribute *attr = NULL;
21681 struct attribute *attr2 = NULL;
21682 CORE_ADDR baseaddr;
21683 struct pending **list_to_add = NULL;
21684
21685 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21686
21687 baseaddr = objfile->text_section_offset ();
21688
21689 name = dwarf2_name (die, cu);
21690 if (name)
21691 {
21692 int suppress_add = 0;
21693
21694 if (space)
21695 sym = space;
21696 else
21697 sym = new (&objfile->objfile_obstack) symbol;
21698 OBJSTAT (objfile, n_syms++);
21699
21700 /* Cache this symbol's name and the name's demangled form (if any). */
21701 sym->set_language (cu->language, &objfile->objfile_obstack);
21702 /* Fortran does not have mangling standard and the mangling does differ
21703 between gfortran, iFort etc. */
21704 const char *physname
21705 = (cu->language == language_fortran
21706 ? dwarf2_full_name (name, die, cu)
21707 : dwarf2_physname (name, die, cu));
21708 const char *linkagename = dw2_linkage_name (die, cu);
21709
21710 if (linkagename == nullptr || cu->language == language_ada)
21711 sym->set_linkage_name (physname);
21712 else
21713 {
21714 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21715 sym->set_linkage_name (linkagename);
21716 }
21717
21718 /* Default assumptions.
21719 Use the passed type or decode it from the die. */
21720 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21721 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21722 if (type != NULL)
21723 SYMBOL_TYPE (sym) = type;
21724 else
21725 SYMBOL_TYPE (sym) = die_type (die, cu);
21726 attr = dwarf2_attr (die,
21727 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21728 cu);
21729 if (attr != nullptr)
21730 SYMBOL_LINE (sym) = attr->constant_value (0);
21731
21732 attr = dwarf2_attr (die,
21733 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21734 cu);
21735 if (attr != nullptr && attr->form_is_unsigned ())
21736 {
21737 file_name_index file_index
21738 = (file_name_index) attr->as_unsigned ();
21739 struct file_entry *fe;
21740
21741 if (cu->line_header != NULL)
21742 fe = cu->line_header->file_name_at (file_index);
21743 else
21744 fe = NULL;
21745
21746 if (fe == NULL)
21747 complaint (_("file index out of range"));
21748 else
21749 symbol_set_symtab (sym, fe->symtab);
21750 }
21751
21752 switch (die->tag)
21753 {
21754 case DW_TAG_label:
21755 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21756 if (attr != nullptr)
21757 {
21758 CORE_ADDR addr;
21759
21760 addr = attr->as_address ();
21761 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21762 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21763 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21764 }
21765 else
21766 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21767 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21768 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21769 add_symbol_to_list (sym, cu->list_in_scope);
21770 break;
21771 case DW_TAG_subprogram:
21772 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21773 finish_block. */
21774 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21775 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21776 if ((attr2 != nullptr && attr2->as_boolean ())
21777 || cu->language == language_ada
21778 || cu->language == language_fortran)
21779 {
21780 /* Subprograms marked external are stored as a global symbol.
21781 Ada and Fortran subprograms, whether marked external or
21782 not, are always stored as a global symbol, because we want
21783 to be able to access them globally. For instance, we want
21784 to be able to break on a nested subprogram without having
21785 to specify the context. */
21786 list_to_add = cu->get_builder ()->get_global_symbols ();
21787 }
21788 else
21789 {
21790 list_to_add = cu->list_in_scope;
21791 }
21792 break;
21793 case DW_TAG_inlined_subroutine:
21794 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21795 finish_block. */
21796 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21797 SYMBOL_INLINED (sym) = 1;
21798 list_to_add = cu->list_in_scope;
21799 break;
21800 case DW_TAG_template_value_param:
21801 suppress_add = 1;
21802 /* Fall through. */
21803 case DW_TAG_constant:
21804 case DW_TAG_variable:
21805 case DW_TAG_member:
21806 /* Compilation with minimal debug info may result in
21807 variables with missing type entries. Change the
21808 misleading `void' type to something sensible. */
21809 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
21810 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21811
21812 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21813 /* In the case of DW_TAG_member, we should only be called for
21814 static const members. */
21815 if (die->tag == DW_TAG_member)
21816 {
21817 /* dwarf2_add_field uses die_is_declaration,
21818 so we do the same. */
21819 gdb_assert (die_is_declaration (die, cu));
21820 gdb_assert (attr);
21821 }
21822 if (attr != nullptr)
21823 {
21824 dwarf2_const_value (attr, sym, cu);
21825 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21826 if (!suppress_add)
21827 {
21828 if (attr2 != nullptr && attr2->as_boolean ())
21829 list_to_add = cu->get_builder ()->get_global_symbols ();
21830 else
21831 list_to_add = cu->list_in_scope;
21832 }
21833 break;
21834 }
21835 attr = dwarf2_attr (die, DW_AT_location, cu);
21836 if (attr != nullptr)
21837 {
21838 var_decode_location (attr, sym, cu);
21839 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21840
21841 /* Fortran explicitly imports any global symbols to the local
21842 scope by DW_TAG_common_block. */
21843 if (cu->language == language_fortran && die->parent
21844 && die->parent->tag == DW_TAG_common_block)
21845 attr2 = NULL;
21846
21847 if (SYMBOL_CLASS (sym) == LOC_STATIC
21848 && SYMBOL_VALUE_ADDRESS (sym) == 0
21849 && !per_objfile->per_bfd->has_section_at_zero)
21850 {
21851 /* When a static variable is eliminated by the linker,
21852 the corresponding debug information is not stripped
21853 out, but the variable address is set to null;
21854 do not add such variables into symbol table. */
21855 }
21856 else if (attr2 != nullptr && attr2->as_boolean ())
21857 {
21858 if (SYMBOL_CLASS (sym) == LOC_STATIC
21859 && (objfile->flags & OBJF_MAINLINE) == 0
21860 && per_objfile->per_bfd->can_copy)
21861 {
21862 /* A global static variable might be subject to
21863 copy relocation. We first check for a local
21864 minsym, though, because maybe the symbol was
21865 marked hidden, in which case this would not
21866 apply. */
21867 bound_minimal_symbol found
21868 = (lookup_minimal_symbol_linkage
21869 (sym->linkage_name (), objfile));
21870 if (found.minsym != nullptr)
21871 sym->maybe_copied = 1;
21872 }
21873
21874 /* A variable with DW_AT_external is never static,
21875 but it may be block-scoped. */
21876 list_to_add
21877 = ((cu->list_in_scope
21878 == cu->get_builder ()->get_file_symbols ())
21879 ? cu->get_builder ()->get_global_symbols ()
21880 : cu->list_in_scope);
21881 }
21882 else
21883 list_to_add = cu->list_in_scope;
21884 }
21885 else
21886 {
21887 /* We do not know the address of this symbol.
21888 If it is an external symbol and we have type information
21889 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21890 The address of the variable will then be determined from
21891 the minimal symbol table whenever the variable is
21892 referenced. */
21893 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21894
21895 /* Fortran explicitly imports any global symbols to the local
21896 scope by DW_TAG_common_block. */
21897 if (cu->language == language_fortran && die->parent
21898 && die->parent->tag == DW_TAG_common_block)
21899 {
21900 /* SYMBOL_CLASS doesn't matter here because
21901 read_common_block is going to reset it. */
21902 if (!suppress_add)
21903 list_to_add = cu->list_in_scope;
21904 }
21905 else if (attr2 != nullptr && attr2->as_boolean ()
21906 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21907 {
21908 /* A variable with DW_AT_external is never static, but it
21909 may be block-scoped. */
21910 list_to_add
21911 = ((cu->list_in_scope
21912 == cu->get_builder ()->get_file_symbols ())
21913 ? cu->get_builder ()->get_global_symbols ()
21914 : cu->list_in_scope);
21915
21916 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21917 }
21918 else if (!die_is_declaration (die, cu))
21919 {
21920 /* Use the default LOC_OPTIMIZED_OUT class. */
21921 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21922 if (!suppress_add)
21923 list_to_add = cu->list_in_scope;
21924 }
21925 }
21926 break;
21927 case DW_TAG_formal_parameter:
21928 {
21929 /* If we are inside a function, mark this as an argument. If
21930 not, we might be looking at an argument to an inlined function
21931 when we do not have enough information to show inlined frames;
21932 pretend it's a local variable in that case so that the user can
21933 still see it. */
21934 struct context_stack *curr
21935 = cu->get_builder ()->get_current_context_stack ();
21936 if (curr != nullptr && curr->name != nullptr)
21937 SYMBOL_IS_ARGUMENT (sym) = 1;
21938 attr = dwarf2_attr (die, DW_AT_location, cu);
21939 if (attr != nullptr)
21940 {
21941 var_decode_location (attr, sym, cu);
21942 }
21943 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21944 if (attr != nullptr)
21945 {
21946 dwarf2_const_value (attr, sym, cu);
21947 }
21948
21949 list_to_add = cu->list_in_scope;
21950 }
21951 break;
21952 case DW_TAG_unspecified_parameters:
21953 /* From varargs functions; gdb doesn't seem to have any
21954 interest in this information, so just ignore it for now.
21955 (FIXME?) */
21956 break;
21957 case DW_TAG_template_type_param:
21958 suppress_add = 1;
21959 /* Fall through. */
21960 case DW_TAG_class_type:
21961 case DW_TAG_interface_type:
21962 case DW_TAG_structure_type:
21963 case DW_TAG_union_type:
21964 case DW_TAG_set_type:
21965 case DW_TAG_enumeration_type:
21966 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21967 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21968
21969 {
21970 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21971 really ever be static objects: otherwise, if you try
21972 to, say, break of a class's method and you're in a file
21973 which doesn't mention that class, it won't work unless
21974 the check for all static symbols in lookup_symbol_aux
21975 saves you. See the OtherFileClass tests in
21976 gdb.c++/namespace.exp. */
21977
21978 if (!suppress_add)
21979 {
21980 buildsym_compunit *builder = cu->get_builder ();
21981 list_to_add
21982 = (cu->list_in_scope == builder->get_file_symbols ()
21983 && cu->language == language_cplus
21984 ? builder->get_global_symbols ()
21985 : cu->list_in_scope);
21986
21987 /* The semantics of C++ state that "struct foo {
21988 ... }" also defines a typedef for "foo". */
21989 if (cu->language == language_cplus
21990 || cu->language == language_ada
21991 || cu->language == language_d
21992 || cu->language == language_rust)
21993 {
21994 /* The symbol's name is already allocated along
21995 with this objfile, so we don't need to
21996 duplicate it for the type. */
21997 if (SYMBOL_TYPE (sym)->name () == 0)
21998 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21999 }
22000 }
22001 }
22002 break;
22003 case DW_TAG_typedef:
22004 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22005 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22006 list_to_add = cu->list_in_scope;
22007 break;
22008 case DW_TAG_array_type:
22009 case DW_TAG_base_type:
22010 case DW_TAG_subrange_type:
22011 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22012 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22013 list_to_add = cu->list_in_scope;
22014 break;
22015 case DW_TAG_enumerator:
22016 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22017 if (attr != nullptr)
22018 {
22019 dwarf2_const_value (attr, sym, cu);
22020 }
22021 {
22022 /* NOTE: carlton/2003-11-10: See comment above in the
22023 DW_TAG_class_type, etc. block. */
22024
22025 list_to_add
22026 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22027 && cu->language == language_cplus
22028 ? cu->get_builder ()->get_global_symbols ()
22029 : cu->list_in_scope);
22030 }
22031 break;
22032 case DW_TAG_imported_declaration:
22033 case DW_TAG_namespace:
22034 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22035 list_to_add = cu->get_builder ()->get_global_symbols ();
22036 break;
22037 case DW_TAG_module:
22038 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22039 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22040 list_to_add = cu->get_builder ()->get_global_symbols ();
22041 break;
22042 case DW_TAG_common_block:
22043 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22044 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22045 add_symbol_to_list (sym, cu->list_in_scope);
22046 break;
22047 default:
22048 /* Not a tag we recognize. Hopefully we aren't processing
22049 trash data, but since we must specifically ignore things
22050 we don't recognize, there is nothing else we should do at
22051 this point. */
22052 complaint (_("unsupported tag: '%s'"),
22053 dwarf_tag_name (die->tag));
22054 break;
22055 }
22056
22057 if (suppress_add)
22058 {
22059 sym->hash_next = objfile->template_symbols;
22060 objfile->template_symbols = sym;
22061 list_to_add = NULL;
22062 }
22063
22064 if (list_to_add != NULL)
22065 add_symbol_to_list (sym, list_to_add);
22066
22067 /* For the benefit of old versions of GCC, check for anonymous
22068 namespaces based on the demangled name. */
22069 if (!cu->processing_has_namespace_info
22070 && cu->language == language_cplus)
22071 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22072 }
22073 return (sym);
22074 }
22075
22076 /* Given an attr with a DW_FORM_dataN value in host byte order,
22077 zero-extend it as appropriate for the symbol's type. The DWARF
22078 standard (v4) is not entirely clear about the meaning of using
22079 DW_FORM_dataN for a constant with a signed type, where the type is
22080 wider than the data. The conclusion of a discussion on the DWARF
22081 list was that this is unspecified. We choose to always zero-extend
22082 because that is the interpretation long in use by GCC. */
22083
22084 static gdb_byte *
22085 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22086 struct dwarf2_cu *cu, LONGEST *value, int bits)
22087 {
22088 struct objfile *objfile = cu->per_objfile->objfile;
22089 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22090 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22091 LONGEST l = attr->constant_value (0);
22092
22093 if (bits < sizeof (*value) * 8)
22094 {
22095 l &= ((LONGEST) 1 << bits) - 1;
22096 *value = l;
22097 }
22098 else if (bits == sizeof (*value) * 8)
22099 *value = l;
22100 else
22101 {
22102 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22103 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22104 return bytes;
22105 }
22106
22107 return NULL;
22108 }
22109
22110 /* Read a constant value from an attribute. Either set *VALUE, or if
22111 the value does not fit in *VALUE, set *BYTES - either already
22112 allocated on the objfile obstack, or newly allocated on OBSTACK,
22113 or, set *BATON, if we translated the constant to a location
22114 expression. */
22115
22116 static void
22117 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22118 const char *name, struct obstack *obstack,
22119 struct dwarf2_cu *cu,
22120 LONGEST *value, const gdb_byte **bytes,
22121 struct dwarf2_locexpr_baton **baton)
22122 {
22123 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22124 struct objfile *objfile = per_objfile->objfile;
22125 struct comp_unit_head *cu_header = &cu->header;
22126 struct dwarf_block *blk;
22127 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22128 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22129
22130 *value = 0;
22131 *bytes = NULL;
22132 *baton = NULL;
22133
22134 switch (attr->form)
22135 {
22136 case DW_FORM_addr:
22137 case DW_FORM_addrx:
22138 case DW_FORM_GNU_addr_index:
22139 {
22140 gdb_byte *data;
22141
22142 if (TYPE_LENGTH (type) != cu_header->addr_size)
22143 dwarf2_const_value_length_mismatch_complaint (name,
22144 cu_header->addr_size,
22145 TYPE_LENGTH (type));
22146 /* Symbols of this form are reasonably rare, so we just
22147 piggyback on the existing location code rather than writing
22148 a new implementation of symbol_computed_ops. */
22149 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22150 (*baton)->per_objfile = per_objfile;
22151 (*baton)->per_cu = cu->per_cu;
22152 gdb_assert ((*baton)->per_cu);
22153
22154 (*baton)->size = 2 + cu_header->addr_size;
22155 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22156 (*baton)->data = data;
22157
22158 data[0] = DW_OP_addr;
22159 store_unsigned_integer (&data[1], cu_header->addr_size,
22160 byte_order, attr->as_address ());
22161 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22162 }
22163 break;
22164 case DW_FORM_string:
22165 case DW_FORM_strp:
22166 case DW_FORM_strx:
22167 case DW_FORM_GNU_str_index:
22168 case DW_FORM_GNU_strp_alt:
22169 /* The string is already allocated on the objfile obstack, point
22170 directly to it. */
22171 *bytes = (const gdb_byte *) attr->as_string ();
22172 break;
22173 case DW_FORM_block1:
22174 case DW_FORM_block2:
22175 case DW_FORM_block4:
22176 case DW_FORM_block:
22177 case DW_FORM_exprloc:
22178 case DW_FORM_data16:
22179 blk = attr->as_block ();
22180 if (TYPE_LENGTH (type) != blk->size)
22181 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22182 TYPE_LENGTH (type));
22183 *bytes = blk->data;
22184 break;
22185
22186 /* The DW_AT_const_value attributes are supposed to carry the
22187 symbol's value "represented as it would be on the target
22188 architecture." By the time we get here, it's already been
22189 converted to host endianness, so we just need to sign- or
22190 zero-extend it as appropriate. */
22191 case DW_FORM_data1:
22192 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22193 break;
22194 case DW_FORM_data2:
22195 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22196 break;
22197 case DW_FORM_data4:
22198 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22199 break;
22200 case DW_FORM_data8:
22201 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22202 break;
22203
22204 case DW_FORM_sdata:
22205 case DW_FORM_implicit_const:
22206 *value = attr->as_signed ();
22207 break;
22208
22209 case DW_FORM_udata:
22210 *value = attr->as_unsigned ();
22211 break;
22212
22213 default:
22214 complaint (_("unsupported const value attribute form: '%s'"),
22215 dwarf_form_name (attr->form));
22216 *value = 0;
22217 break;
22218 }
22219 }
22220
22221
22222 /* Copy constant value from an attribute to a symbol. */
22223
22224 static void
22225 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22226 struct dwarf2_cu *cu)
22227 {
22228 struct objfile *objfile = cu->per_objfile->objfile;
22229 LONGEST value;
22230 const gdb_byte *bytes;
22231 struct dwarf2_locexpr_baton *baton;
22232
22233 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22234 sym->print_name (),
22235 &objfile->objfile_obstack, cu,
22236 &value, &bytes, &baton);
22237
22238 if (baton != NULL)
22239 {
22240 SYMBOL_LOCATION_BATON (sym) = baton;
22241 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22242 }
22243 else if (bytes != NULL)
22244 {
22245 SYMBOL_VALUE_BYTES (sym) = bytes;
22246 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22247 }
22248 else
22249 {
22250 SYMBOL_VALUE (sym) = value;
22251 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22252 }
22253 }
22254
22255 /* Return the type of the die in question using its DW_AT_type attribute. */
22256
22257 static struct type *
22258 die_type (struct die_info *die, struct dwarf2_cu *cu)
22259 {
22260 struct attribute *type_attr;
22261
22262 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22263 if (!type_attr)
22264 {
22265 struct objfile *objfile = cu->per_objfile->objfile;
22266 /* A missing DW_AT_type represents a void type. */
22267 return objfile_type (objfile)->builtin_void;
22268 }
22269
22270 return lookup_die_type (die, type_attr, cu);
22271 }
22272
22273 /* True iff CU's producer generates GNAT Ada auxiliary information
22274 that allows to find parallel types through that information instead
22275 of having to do expensive parallel lookups by type name. */
22276
22277 static int
22278 need_gnat_info (struct dwarf2_cu *cu)
22279 {
22280 /* Assume that the Ada compiler was GNAT, which always produces
22281 the auxiliary information. */
22282 return (cu->language == language_ada);
22283 }
22284
22285 /* Return the auxiliary type of the die in question using its
22286 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22287 attribute is not present. */
22288
22289 static struct type *
22290 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22291 {
22292 struct attribute *type_attr;
22293
22294 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22295 if (!type_attr)
22296 return NULL;
22297
22298 return lookup_die_type (die, type_attr, cu);
22299 }
22300
22301 /* If DIE has a descriptive_type attribute, then set the TYPE's
22302 descriptive type accordingly. */
22303
22304 static void
22305 set_descriptive_type (struct type *type, struct die_info *die,
22306 struct dwarf2_cu *cu)
22307 {
22308 struct type *descriptive_type = die_descriptive_type (die, cu);
22309
22310 if (descriptive_type)
22311 {
22312 ALLOCATE_GNAT_AUX_TYPE (type);
22313 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22314 }
22315 }
22316
22317 /* Return the containing type of the die in question using its
22318 DW_AT_containing_type attribute. */
22319
22320 static struct type *
22321 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22322 {
22323 struct attribute *type_attr;
22324 struct objfile *objfile = cu->per_objfile->objfile;
22325
22326 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22327 if (!type_attr)
22328 error (_("Dwarf Error: Problem turning containing type into gdb type "
22329 "[in module %s]"), objfile_name (objfile));
22330
22331 return lookup_die_type (die, type_attr, cu);
22332 }
22333
22334 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22335
22336 static struct type *
22337 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22338 {
22339 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22340 struct objfile *objfile = per_objfile->objfile;
22341 char *saved;
22342
22343 std::string message
22344 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22345 objfile_name (objfile),
22346 sect_offset_str (cu->header.sect_off),
22347 sect_offset_str (die->sect_off));
22348 saved = obstack_strdup (&objfile->objfile_obstack, message);
22349
22350 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22351 }
22352
22353 /* Look up the type of DIE in CU using its type attribute ATTR.
22354 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22355 DW_AT_containing_type.
22356 If there is no type substitute an error marker. */
22357
22358 static struct type *
22359 lookup_die_type (struct die_info *die, const struct attribute *attr,
22360 struct dwarf2_cu *cu)
22361 {
22362 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22363 struct objfile *objfile = per_objfile->objfile;
22364 struct type *this_type;
22365
22366 gdb_assert (attr->name == DW_AT_type
22367 || attr->name == DW_AT_GNAT_descriptive_type
22368 || attr->name == DW_AT_containing_type);
22369
22370 /* First see if we have it cached. */
22371
22372 if (attr->form == DW_FORM_GNU_ref_alt)
22373 {
22374 struct dwarf2_per_cu_data *per_cu;
22375 sect_offset sect_off = attr->get_ref_die_offset ();
22376
22377 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22378 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22379 }
22380 else if (attr->form_is_ref ())
22381 {
22382 sect_offset sect_off = attr->get_ref_die_offset ();
22383
22384 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22385 }
22386 else if (attr->form == DW_FORM_ref_sig8)
22387 {
22388 ULONGEST signature = attr->as_signature ();
22389
22390 return get_signatured_type (die, signature, cu);
22391 }
22392 else
22393 {
22394 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22395 " at %s [in module %s]"),
22396 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22397 objfile_name (objfile));
22398 return build_error_marker_type (cu, die);
22399 }
22400
22401 /* If not cached we need to read it in. */
22402
22403 if (this_type == NULL)
22404 {
22405 struct die_info *type_die = NULL;
22406 struct dwarf2_cu *type_cu = cu;
22407
22408 if (attr->form_is_ref ())
22409 type_die = follow_die_ref (die, attr, &type_cu);
22410 if (type_die == NULL)
22411 return build_error_marker_type (cu, die);
22412 /* If we find the type now, it's probably because the type came
22413 from an inter-CU reference and the type's CU got expanded before
22414 ours. */
22415 this_type = read_type_die (type_die, type_cu);
22416 }
22417
22418 /* If we still don't have a type use an error marker. */
22419
22420 if (this_type == NULL)
22421 return build_error_marker_type (cu, die);
22422
22423 return this_type;
22424 }
22425
22426 /* Return the type in DIE, CU.
22427 Returns NULL for invalid types.
22428
22429 This first does a lookup in die_type_hash,
22430 and only reads the die in if necessary.
22431
22432 NOTE: This can be called when reading in partial or full symbols. */
22433
22434 static struct type *
22435 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22436 {
22437 struct type *this_type;
22438
22439 this_type = get_die_type (die, cu);
22440 if (this_type)
22441 return this_type;
22442
22443 return read_type_die_1 (die, cu);
22444 }
22445
22446 /* Read the type in DIE, CU.
22447 Returns NULL for invalid types. */
22448
22449 static struct type *
22450 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22451 {
22452 struct type *this_type = NULL;
22453
22454 switch (die->tag)
22455 {
22456 case DW_TAG_class_type:
22457 case DW_TAG_interface_type:
22458 case DW_TAG_structure_type:
22459 case DW_TAG_union_type:
22460 this_type = read_structure_type (die, cu);
22461 break;
22462 case DW_TAG_enumeration_type:
22463 this_type = read_enumeration_type (die, cu);
22464 break;
22465 case DW_TAG_subprogram:
22466 case DW_TAG_subroutine_type:
22467 case DW_TAG_inlined_subroutine:
22468 this_type = read_subroutine_type (die, cu);
22469 break;
22470 case DW_TAG_array_type:
22471 this_type = read_array_type (die, cu);
22472 break;
22473 case DW_TAG_set_type:
22474 this_type = read_set_type (die, cu);
22475 break;
22476 case DW_TAG_pointer_type:
22477 this_type = read_tag_pointer_type (die, cu);
22478 break;
22479 case DW_TAG_ptr_to_member_type:
22480 this_type = read_tag_ptr_to_member_type (die, cu);
22481 break;
22482 case DW_TAG_reference_type:
22483 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22484 break;
22485 case DW_TAG_rvalue_reference_type:
22486 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22487 break;
22488 case DW_TAG_const_type:
22489 this_type = read_tag_const_type (die, cu);
22490 break;
22491 case DW_TAG_volatile_type:
22492 this_type = read_tag_volatile_type (die, cu);
22493 break;
22494 case DW_TAG_restrict_type:
22495 this_type = read_tag_restrict_type (die, cu);
22496 break;
22497 case DW_TAG_string_type:
22498 this_type = read_tag_string_type (die, cu);
22499 break;
22500 case DW_TAG_typedef:
22501 this_type = read_typedef (die, cu);
22502 break;
22503 case DW_TAG_subrange_type:
22504 this_type = read_subrange_type (die, cu);
22505 break;
22506 case DW_TAG_base_type:
22507 this_type = read_base_type (die, cu);
22508 break;
22509 case DW_TAG_unspecified_type:
22510 this_type = read_unspecified_type (die, cu);
22511 break;
22512 case DW_TAG_namespace:
22513 this_type = read_namespace_type (die, cu);
22514 break;
22515 case DW_TAG_module:
22516 this_type = read_module_type (die, cu);
22517 break;
22518 case DW_TAG_atomic_type:
22519 this_type = read_tag_atomic_type (die, cu);
22520 break;
22521 default:
22522 complaint (_("unexpected tag in read_type_die: '%s'"),
22523 dwarf_tag_name (die->tag));
22524 break;
22525 }
22526
22527 return this_type;
22528 }
22529
22530 /* See if we can figure out if the class lives in a namespace. We do
22531 this by looking for a member function; its demangled name will
22532 contain namespace info, if there is any.
22533 Return the computed name or NULL.
22534 Space for the result is allocated on the objfile's obstack.
22535 This is the full-die version of guess_partial_die_structure_name.
22536 In this case we know DIE has no useful parent. */
22537
22538 static const char *
22539 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22540 {
22541 struct die_info *spec_die;
22542 struct dwarf2_cu *spec_cu;
22543 struct die_info *child;
22544 struct objfile *objfile = cu->per_objfile->objfile;
22545
22546 spec_cu = cu;
22547 spec_die = die_specification (die, &spec_cu);
22548 if (spec_die != NULL)
22549 {
22550 die = spec_die;
22551 cu = spec_cu;
22552 }
22553
22554 for (child = die->child;
22555 child != NULL;
22556 child = child->sibling)
22557 {
22558 if (child->tag == DW_TAG_subprogram)
22559 {
22560 const char *linkage_name = dw2_linkage_name (child, cu);
22561
22562 if (linkage_name != NULL)
22563 {
22564 gdb::unique_xmalloc_ptr<char> actual_name
22565 (cu->language_defn->class_name_from_physname (linkage_name));
22566 const char *name = NULL;
22567
22568 if (actual_name != NULL)
22569 {
22570 const char *die_name = dwarf2_name (die, cu);
22571
22572 if (die_name != NULL
22573 && strcmp (die_name, actual_name.get ()) != 0)
22574 {
22575 /* Strip off the class name from the full name.
22576 We want the prefix. */
22577 int die_name_len = strlen (die_name);
22578 int actual_name_len = strlen (actual_name.get ());
22579 const char *ptr = actual_name.get ();
22580
22581 /* Test for '::' as a sanity check. */
22582 if (actual_name_len > die_name_len + 2
22583 && ptr[actual_name_len - die_name_len - 1] == ':')
22584 name = obstack_strndup (
22585 &objfile->per_bfd->storage_obstack,
22586 ptr, actual_name_len - die_name_len - 2);
22587 }
22588 }
22589 return name;
22590 }
22591 }
22592 }
22593
22594 return NULL;
22595 }
22596
22597 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22598 prefix part in such case. See
22599 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22600
22601 static const char *
22602 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22603 {
22604 struct attribute *attr;
22605 const char *base;
22606
22607 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22608 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22609 return NULL;
22610
22611 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22612 return NULL;
22613
22614 attr = dw2_linkage_name_attr (die, cu);
22615 const char *attr_name = attr->as_string ();
22616 if (attr == NULL || attr_name == NULL)
22617 return NULL;
22618
22619 /* dwarf2_name had to be already called. */
22620 gdb_assert (attr->canonical_string_p ());
22621
22622 /* Strip the base name, keep any leading namespaces/classes. */
22623 base = strrchr (attr_name, ':');
22624 if (base == NULL || base == attr_name || base[-1] != ':')
22625 return "";
22626
22627 struct objfile *objfile = cu->per_objfile->objfile;
22628 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22629 attr_name,
22630 &base[-1] - attr_name);
22631 }
22632
22633 /* Return the name of the namespace/class that DIE is defined within,
22634 or "" if we can't tell. The caller should not xfree the result.
22635
22636 For example, if we're within the method foo() in the following
22637 code:
22638
22639 namespace N {
22640 class C {
22641 void foo () {
22642 }
22643 };
22644 }
22645
22646 then determine_prefix on foo's die will return "N::C". */
22647
22648 static const char *
22649 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22650 {
22651 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22652 struct die_info *parent, *spec_die;
22653 struct dwarf2_cu *spec_cu;
22654 struct type *parent_type;
22655 const char *retval;
22656
22657 if (cu->language != language_cplus
22658 && cu->language != language_fortran && cu->language != language_d
22659 && cu->language != language_rust)
22660 return "";
22661
22662 retval = anonymous_struct_prefix (die, cu);
22663 if (retval)
22664 return retval;
22665
22666 /* We have to be careful in the presence of DW_AT_specification.
22667 For example, with GCC 3.4, given the code
22668
22669 namespace N {
22670 void foo() {
22671 // Definition of N::foo.
22672 }
22673 }
22674
22675 then we'll have a tree of DIEs like this:
22676
22677 1: DW_TAG_compile_unit
22678 2: DW_TAG_namespace // N
22679 3: DW_TAG_subprogram // declaration of N::foo
22680 4: DW_TAG_subprogram // definition of N::foo
22681 DW_AT_specification // refers to die #3
22682
22683 Thus, when processing die #4, we have to pretend that we're in
22684 the context of its DW_AT_specification, namely the contex of die
22685 #3. */
22686 spec_cu = cu;
22687 spec_die = die_specification (die, &spec_cu);
22688 if (spec_die == NULL)
22689 parent = die->parent;
22690 else
22691 {
22692 parent = spec_die->parent;
22693 cu = spec_cu;
22694 }
22695
22696 if (parent == NULL)
22697 return "";
22698 else if (parent->building_fullname)
22699 {
22700 const char *name;
22701 const char *parent_name;
22702
22703 /* It has been seen on RealView 2.2 built binaries,
22704 DW_TAG_template_type_param types actually _defined_ as
22705 children of the parent class:
22706
22707 enum E {};
22708 template class <class Enum> Class{};
22709 Class<enum E> class_e;
22710
22711 1: DW_TAG_class_type (Class)
22712 2: DW_TAG_enumeration_type (E)
22713 3: DW_TAG_enumerator (enum1:0)
22714 3: DW_TAG_enumerator (enum2:1)
22715 ...
22716 2: DW_TAG_template_type_param
22717 DW_AT_type DW_FORM_ref_udata (E)
22718
22719 Besides being broken debug info, it can put GDB into an
22720 infinite loop. Consider:
22721
22722 When we're building the full name for Class<E>, we'll start
22723 at Class, and go look over its template type parameters,
22724 finding E. We'll then try to build the full name of E, and
22725 reach here. We're now trying to build the full name of E,
22726 and look over the parent DIE for containing scope. In the
22727 broken case, if we followed the parent DIE of E, we'd again
22728 find Class, and once again go look at its template type
22729 arguments, etc., etc. Simply don't consider such parent die
22730 as source-level parent of this die (it can't be, the language
22731 doesn't allow it), and break the loop here. */
22732 name = dwarf2_name (die, cu);
22733 parent_name = dwarf2_name (parent, cu);
22734 complaint (_("template param type '%s' defined within parent '%s'"),
22735 name ? name : "<unknown>",
22736 parent_name ? parent_name : "<unknown>");
22737 return "";
22738 }
22739 else
22740 switch (parent->tag)
22741 {
22742 case DW_TAG_namespace:
22743 parent_type = read_type_die (parent, cu);
22744 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22745 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22746 Work around this problem here. */
22747 if (cu->language == language_cplus
22748 && strcmp (parent_type->name (), "::") == 0)
22749 return "";
22750 /* We give a name to even anonymous namespaces. */
22751 return parent_type->name ();
22752 case DW_TAG_class_type:
22753 case DW_TAG_interface_type:
22754 case DW_TAG_structure_type:
22755 case DW_TAG_union_type:
22756 case DW_TAG_module:
22757 parent_type = read_type_die (parent, cu);
22758 if (parent_type->name () != NULL)
22759 return parent_type->name ();
22760 else
22761 /* An anonymous structure is only allowed non-static data
22762 members; no typedefs, no member functions, et cetera.
22763 So it does not need a prefix. */
22764 return "";
22765 case DW_TAG_compile_unit:
22766 case DW_TAG_partial_unit:
22767 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22768 if (cu->language == language_cplus
22769 && !per_objfile->per_bfd->types.empty ()
22770 && die->child != NULL
22771 && (die->tag == DW_TAG_class_type
22772 || die->tag == DW_TAG_structure_type
22773 || die->tag == DW_TAG_union_type))
22774 {
22775 const char *name = guess_full_die_structure_name (die, cu);
22776 if (name != NULL)
22777 return name;
22778 }
22779 return "";
22780 case DW_TAG_subprogram:
22781 /* Nested subroutines in Fortran get a prefix with the name
22782 of the parent's subroutine. */
22783 if (cu->language == language_fortran)
22784 {
22785 if ((die->tag == DW_TAG_subprogram)
22786 && (dwarf2_name (parent, cu) != NULL))
22787 return dwarf2_name (parent, cu);
22788 }
22789 return determine_prefix (parent, cu);
22790 case DW_TAG_enumeration_type:
22791 parent_type = read_type_die (parent, cu);
22792 if (TYPE_DECLARED_CLASS (parent_type))
22793 {
22794 if (parent_type->name () != NULL)
22795 return parent_type->name ();
22796 return "";
22797 }
22798 /* Fall through. */
22799 default:
22800 return determine_prefix (parent, cu);
22801 }
22802 }
22803
22804 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22805 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22806 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22807 an obconcat, otherwise allocate storage for the result. The CU argument is
22808 used to determine the language and hence, the appropriate separator. */
22809
22810 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22811
22812 static char *
22813 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22814 int physname, struct dwarf2_cu *cu)
22815 {
22816 const char *lead = "";
22817 const char *sep;
22818
22819 if (suffix == NULL || suffix[0] == '\0'
22820 || prefix == NULL || prefix[0] == '\0')
22821 sep = "";
22822 else if (cu->language == language_d)
22823 {
22824 /* For D, the 'main' function could be defined in any module, but it
22825 should never be prefixed. */
22826 if (strcmp (suffix, "D main") == 0)
22827 {
22828 prefix = "";
22829 sep = "";
22830 }
22831 else
22832 sep = ".";
22833 }
22834 else if (cu->language == language_fortran && physname)
22835 {
22836 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22837 DW_AT_MIPS_linkage_name is preferred and used instead. */
22838
22839 lead = "__";
22840 sep = "_MOD_";
22841 }
22842 else
22843 sep = "::";
22844
22845 if (prefix == NULL)
22846 prefix = "";
22847 if (suffix == NULL)
22848 suffix = "";
22849
22850 if (obs == NULL)
22851 {
22852 char *retval
22853 = ((char *)
22854 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22855
22856 strcpy (retval, lead);
22857 strcat (retval, prefix);
22858 strcat (retval, sep);
22859 strcat (retval, suffix);
22860 return retval;
22861 }
22862 else
22863 {
22864 /* We have an obstack. */
22865 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22866 }
22867 }
22868
22869 /* Get name of a die, return NULL if not found. */
22870
22871 static const char *
22872 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22873 struct objfile *objfile)
22874 {
22875 if (name && cu->language == language_cplus)
22876 {
22877 gdb::unique_xmalloc_ptr<char> canon_name
22878 = cp_canonicalize_string (name);
22879
22880 if (canon_name != nullptr)
22881 name = objfile->intern (canon_name.get ());
22882 }
22883
22884 return name;
22885 }
22886
22887 /* Get name of a die, return NULL if not found.
22888 Anonymous namespaces are converted to their magic string. */
22889
22890 static const char *
22891 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22892 {
22893 struct attribute *attr;
22894 struct objfile *objfile = cu->per_objfile->objfile;
22895
22896 attr = dwarf2_attr (die, DW_AT_name, cu);
22897 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
22898 if (attr_name == nullptr
22899 && die->tag != DW_TAG_namespace
22900 && die->tag != DW_TAG_class_type
22901 && die->tag != DW_TAG_interface_type
22902 && die->tag != DW_TAG_structure_type
22903 && die->tag != DW_TAG_union_type)
22904 return NULL;
22905
22906 switch (die->tag)
22907 {
22908 case DW_TAG_compile_unit:
22909 case DW_TAG_partial_unit:
22910 /* Compilation units have a DW_AT_name that is a filename, not
22911 a source language identifier. */
22912 case DW_TAG_enumeration_type:
22913 case DW_TAG_enumerator:
22914 /* These tags always have simple identifiers already; no need
22915 to canonicalize them. */
22916 return attr_name;
22917
22918 case DW_TAG_namespace:
22919 if (attr_name != nullptr)
22920 return attr_name;
22921 return CP_ANONYMOUS_NAMESPACE_STR;
22922
22923 case DW_TAG_class_type:
22924 case DW_TAG_interface_type:
22925 case DW_TAG_structure_type:
22926 case DW_TAG_union_type:
22927 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22928 structures or unions. These were of the form "._%d" in GCC 4.1,
22929 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22930 and GCC 4.4. We work around this problem by ignoring these. */
22931 if (attr_name != nullptr
22932 && (startswith (attr_name, "._")
22933 || startswith (attr_name, "<anonymous")))
22934 return NULL;
22935
22936 /* GCC might emit a nameless typedef that has a linkage name. See
22937 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22938 if (!attr || attr_name == NULL)
22939 {
22940 attr = dw2_linkage_name_attr (die, cu);
22941 attr_name = attr == nullptr ? nullptr : attr->as_string ();
22942 if (attr == NULL || attr_name == NULL)
22943 return NULL;
22944
22945 /* Avoid demangling attr_name the second time on a second
22946 call for the same DIE. */
22947 if (!attr->canonical_string_p ())
22948 {
22949 gdb::unique_xmalloc_ptr<char> demangled
22950 (gdb_demangle (attr_name, DMGL_TYPES));
22951 if (demangled == nullptr)
22952 return nullptr;
22953
22954 attr->set_string_canonical (objfile->intern (demangled.get ()));
22955 attr_name = attr->as_string ();
22956 }
22957
22958 /* Strip any leading namespaces/classes, keep only the
22959 base name. DW_AT_name for named DIEs does not
22960 contain the prefixes. */
22961 const char *base = strrchr (attr_name, ':');
22962 if (base && base > attr_name && base[-1] == ':')
22963 return &base[1];
22964 else
22965 return attr_name;
22966 }
22967 break;
22968
22969 default:
22970 break;
22971 }
22972
22973 if (!attr->canonical_string_p ())
22974 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
22975 objfile));
22976 return attr->as_string ();
22977 }
22978
22979 /* Return the die that this die in an extension of, or NULL if there
22980 is none. *EXT_CU is the CU containing DIE on input, and the CU
22981 containing the return value on output. */
22982
22983 static struct die_info *
22984 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22985 {
22986 struct attribute *attr;
22987
22988 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22989 if (attr == NULL)
22990 return NULL;
22991
22992 return follow_die_ref (die, attr, ext_cu);
22993 }
22994
22995 static void
22996 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22997 {
22998 unsigned int i;
22999
23000 print_spaces (indent, f);
23001 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23002 dwarf_tag_name (die->tag), die->abbrev,
23003 sect_offset_str (die->sect_off));
23004
23005 if (die->parent != NULL)
23006 {
23007 print_spaces (indent, f);
23008 fprintf_unfiltered (f, " parent at offset: %s\n",
23009 sect_offset_str (die->parent->sect_off));
23010 }
23011
23012 print_spaces (indent, f);
23013 fprintf_unfiltered (f, " has children: %s\n",
23014 dwarf_bool_name (die->child != NULL));
23015
23016 print_spaces (indent, f);
23017 fprintf_unfiltered (f, " attributes:\n");
23018
23019 for (i = 0; i < die->num_attrs; ++i)
23020 {
23021 print_spaces (indent, f);
23022 fprintf_unfiltered (f, " %s (%s) ",
23023 dwarf_attr_name (die->attrs[i].name),
23024 dwarf_form_name (die->attrs[i].form));
23025
23026 switch (die->attrs[i].form)
23027 {
23028 case DW_FORM_addr:
23029 case DW_FORM_addrx:
23030 case DW_FORM_GNU_addr_index:
23031 fprintf_unfiltered (f, "address: ");
23032 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
23033 break;
23034 case DW_FORM_block2:
23035 case DW_FORM_block4:
23036 case DW_FORM_block:
23037 case DW_FORM_block1:
23038 fprintf_unfiltered (f, "block: size %s",
23039 pulongest (die->attrs[i].as_block ()->size));
23040 break;
23041 case DW_FORM_exprloc:
23042 fprintf_unfiltered (f, "expression: size %s",
23043 pulongest (die->attrs[i].as_block ()->size));
23044 break;
23045 case DW_FORM_data16:
23046 fprintf_unfiltered (f, "constant of 16 bytes");
23047 break;
23048 case DW_FORM_ref_addr:
23049 fprintf_unfiltered (f, "ref address: ");
23050 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23051 break;
23052 case DW_FORM_GNU_ref_alt:
23053 fprintf_unfiltered (f, "alt ref address: ");
23054 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23055 break;
23056 case DW_FORM_ref1:
23057 case DW_FORM_ref2:
23058 case DW_FORM_ref4:
23059 case DW_FORM_ref8:
23060 case DW_FORM_ref_udata:
23061 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23062 (long) (die->attrs[i].as_unsigned ()));
23063 break;
23064 case DW_FORM_data1:
23065 case DW_FORM_data2:
23066 case DW_FORM_data4:
23067 case DW_FORM_data8:
23068 case DW_FORM_udata:
23069 fprintf_unfiltered (f, "constant: %s",
23070 pulongest (die->attrs[i].as_unsigned ()));
23071 break;
23072 case DW_FORM_sec_offset:
23073 fprintf_unfiltered (f, "section offset: %s",
23074 pulongest (die->attrs[i].as_unsigned ()));
23075 break;
23076 case DW_FORM_ref_sig8:
23077 fprintf_unfiltered (f, "signature: %s",
23078 hex_string (die->attrs[i].as_signature ()));
23079 break;
23080 case DW_FORM_string:
23081 case DW_FORM_strp:
23082 case DW_FORM_line_strp:
23083 case DW_FORM_strx:
23084 case DW_FORM_GNU_str_index:
23085 case DW_FORM_GNU_strp_alt:
23086 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23087 die->attrs[i].as_string ()
23088 ? die->attrs[i].as_string () : "",
23089 die->attrs[i].canonical_string_p () ? "is" : "not");
23090 break;
23091 case DW_FORM_flag:
23092 if (die->attrs[i].as_boolean ())
23093 fprintf_unfiltered (f, "flag: TRUE");
23094 else
23095 fprintf_unfiltered (f, "flag: FALSE");
23096 break;
23097 case DW_FORM_flag_present:
23098 fprintf_unfiltered (f, "flag: TRUE");
23099 break;
23100 case DW_FORM_indirect:
23101 /* The reader will have reduced the indirect form to
23102 the "base form" so this form should not occur. */
23103 fprintf_unfiltered (f,
23104 "unexpected attribute form: DW_FORM_indirect");
23105 break;
23106 case DW_FORM_sdata:
23107 case DW_FORM_implicit_const:
23108 fprintf_unfiltered (f, "constant: %s",
23109 plongest (die->attrs[i].as_signed ()));
23110 break;
23111 default:
23112 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23113 die->attrs[i].form);
23114 break;
23115 }
23116 fprintf_unfiltered (f, "\n");
23117 }
23118 }
23119
23120 static void
23121 dump_die_for_error (struct die_info *die)
23122 {
23123 dump_die_shallow (gdb_stderr, 0, die);
23124 }
23125
23126 static void
23127 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23128 {
23129 int indent = level * 4;
23130
23131 gdb_assert (die != NULL);
23132
23133 if (level >= max_level)
23134 return;
23135
23136 dump_die_shallow (f, indent, die);
23137
23138 if (die->child != NULL)
23139 {
23140 print_spaces (indent, f);
23141 fprintf_unfiltered (f, " Children:");
23142 if (level + 1 < max_level)
23143 {
23144 fprintf_unfiltered (f, "\n");
23145 dump_die_1 (f, level + 1, max_level, die->child);
23146 }
23147 else
23148 {
23149 fprintf_unfiltered (f,
23150 " [not printed, max nesting level reached]\n");
23151 }
23152 }
23153
23154 if (die->sibling != NULL && level > 0)
23155 {
23156 dump_die_1 (f, level, max_level, die->sibling);
23157 }
23158 }
23159
23160 /* This is called from the pdie macro in gdbinit.in.
23161 It's not static so gcc will keep a copy callable from gdb. */
23162
23163 void
23164 dump_die (struct die_info *die, int max_level)
23165 {
23166 dump_die_1 (gdb_stdlog, 0, max_level, die);
23167 }
23168
23169 static void
23170 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23171 {
23172 void **slot;
23173
23174 slot = htab_find_slot_with_hash (cu->die_hash, die,
23175 to_underlying (die->sect_off),
23176 INSERT);
23177
23178 *slot = die;
23179 }
23180
23181 /* Follow reference or signature attribute ATTR of SRC_DIE.
23182 On entry *REF_CU is the CU of SRC_DIE.
23183 On exit *REF_CU is the CU of the result. */
23184
23185 static struct die_info *
23186 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23187 struct dwarf2_cu **ref_cu)
23188 {
23189 struct die_info *die;
23190
23191 if (attr->form_is_ref ())
23192 die = follow_die_ref (src_die, attr, ref_cu);
23193 else if (attr->form == DW_FORM_ref_sig8)
23194 die = follow_die_sig (src_die, attr, ref_cu);
23195 else
23196 {
23197 dump_die_for_error (src_die);
23198 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23199 objfile_name ((*ref_cu)->per_objfile->objfile));
23200 }
23201
23202 return die;
23203 }
23204
23205 /* Follow reference OFFSET.
23206 On entry *REF_CU is the CU of the source die referencing OFFSET.
23207 On exit *REF_CU is the CU of the result.
23208 Returns NULL if OFFSET is invalid. */
23209
23210 static struct die_info *
23211 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23212 struct dwarf2_cu **ref_cu)
23213 {
23214 struct die_info temp_die;
23215 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23216 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23217
23218 gdb_assert (cu->per_cu != NULL);
23219
23220 target_cu = cu;
23221
23222 if (cu->per_cu->is_debug_types)
23223 {
23224 /* .debug_types CUs cannot reference anything outside their CU.
23225 If they need to, they have to reference a signatured type via
23226 DW_FORM_ref_sig8. */
23227 if (!cu->header.offset_in_cu_p (sect_off))
23228 return NULL;
23229 }
23230 else if (offset_in_dwz != cu->per_cu->is_dwz
23231 || !cu->header.offset_in_cu_p (sect_off))
23232 {
23233 struct dwarf2_per_cu_data *per_cu;
23234
23235 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23236 per_objfile);
23237
23238 /* If necessary, add it to the queue and load its DIEs. */
23239 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
23240 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23241 false, cu->language);
23242
23243 target_cu = per_objfile->get_cu (per_cu);
23244 }
23245 else if (cu->dies == NULL)
23246 {
23247 /* We're loading full DIEs during partial symbol reading. */
23248 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23249 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23250 language_minimal);
23251 }
23252
23253 *ref_cu = target_cu;
23254 temp_die.sect_off = sect_off;
23255
23256 if (target_cu != cu)
23257 target_cu->ancestor = cu;
23258
23259 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23260 &temp_die,
23261 to_underlying (sect_off));
23262 }
23263
23264 /* Follow reference attribute ATTR of SRC_DIE.
23265 On entry *REF_CU is the CU of SRC_DIE.
23266 On exit *REF_CU is the CU of the result. */
23267
23268 static struct die_info *
23269 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23270 struct dwarf2_cu **ref_cu)
23271 {
23272 sect_offset sect_off = attr->get_ref_die_offset ();
23273 struct dwarf2_cu *cu = *ref_cu;
23274 struct die_info *die;
23275
23276 die = follow_die_offset (sect_off,
23277 (attr->form == DW_FORM_GNU_ref_alt
23278 || cu->per_cu->is_dwz),
23279 ref_cu);
23280 if (!die)
23281 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23282 "at %s [in module %s]"),
23283 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23284 objfile_name (cu->per_objfile->objfile));
23285
23286 return die;
23287 }
23288
23289 /* See read.h. */
23290
23291 struct dwarf2_locexpr_baton
23292 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23293 dwarf2_per_cu_data *per_cu,
23294 dwarf2_per_objfile *per_objfile,
23295 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23296 bool resolve_abstract_p)
23297 {
23298 struct die_info *die;
23299 struct attribute *attr;
23300 struct dwarf2_locexpr_baton retval;
23301 struct objfile *objfile = per_objfile->objfile;
23302
23303 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23304 if (cu == nullptr)
23305 cu = load_cu (per_cu, per_objfile, false);
23306
23307 if (cu == nullptr)
23308 {
23309 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23310 Instead just throw an error, not much else we can do. */
23311 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23312 sect_offset_str (sect_off), objfile_name (objfile));
23313 }
23314
23315 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23316 if (!die)
23317 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23318 sect_offset_str (sect_off), objfile_name (objfile));
23319
23320 attr = dwarf2_attr (die, DW_AT_location, cu);
23321 if (!attr && resolve_abstract_p
23322 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23323 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23324 {
23325 CORE_ADDR pc = get_frame_pc ();
23326 CORE_ADDR baseaddr = objfile->text_section_offset ();
23327 struct gdbarch *gdbarch = objfile->arch ();
23328
23329 for (const auto &cand_off
23330 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23331 {
23332 struct dwarf2_cu *cand_cu = cu;
23333 struct die_info *cand
23334 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23335 if (!cand
23336 || !cand->parent
23337 || cand->parent->tag != DW_TAG_subprogram)
23338 continue;
23339
23340 CORE_ADDR pc_low, pc_high;
23341 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23342 if (pc_low == ((CORE_ADDR) -1))
23343 continue;
23344 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23345 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23346 if (!(pc_low <= pc && pc < pc_high))
23347 continue;
23348
23349 die = cand;
23350 attr = dwarf2_attr (die, DW_AT_location, cu);
23351 break;
23352 }
23353 }
23354
23355 if (!attr)
23356 {
23357 /* DWARF: "If there is no such attribute, then there is no effect.".
23358 DATA is ignored if SIZE is 0. */
23359
23360 retval.data = NULL;
23361 retval.size = 0;
23362 }
23363 else if (attr->form_is_section_offset ())
23364 {
23365 struct dwarf2_loclist_baton loclist_baton;
23366 CORE_ADDR pc = get_frame_pc ();
23367 size_t size;
23368
23369 fill_in_loclist_baton (cu, &loclist_baton, attr);
23370
23371 retval.data = dwarf2_find_location_expression (&loclist_baton,
23372 &size, pc);
23373 retval.size = size;
23374 }
23375 else
23376 {
23377 if (!attr->form_is_block ())
23378 error (_("Dwarf Error: DIE at %s referenced in module %s "
23379 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23380 sect_offset_str (sect_off), objfile_name (objfile));
23381
23382 struct dwarf_block *block = attr->as_block ();
23383 retval.data = block->data;
23384 retval.size = block->size;
23385 }
23386 retval.per_objfile = per_objfile;
23387 retval.per_cu = cu->per_cu;
23388
23389 per_objfile->age_comp_units ();
23390
23391 return retval;
23392 }
23393
23394 /* See read.h. */
23395
23396 struct dwarf2_locexpr_baton
23397 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23398 dwarf2_per_cu_data *per_cu,
23399 dwarf2_per_objfile *per_objfile,
23400 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23401 {
23402 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23403
23404 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23405 get_frame_pc);
23406 }
23407
23408 /* Write a constant of a given type as target-ordered bytes into
23409 OBSTACK. */
23410
23411 static const gdb_byte *
23412 write_constant_as_bytes (struct obstack *obstack,
23413 enum bfd_endian byte_order,
23414 struct type *type,
23415 ULONGEST value,
23416 LONGEST *len)
23417 {
23418 gdb_byte *result;
23419
23420 *len = TYPE_LENGTH (type);
23421 result = (gdb_byte *) obstack_alloc (obstack, *len);
23422 store_unsigned_integer (result, *len, byte_order, value);
23423
23424 return result;
23425 }
23426
23427 /* See read.h. */
23428
23429 const gdb_byte *
23430 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23431 dwarf2_per_cu_data *per_cu,
23432 dwarf2_per_objfile *per_objfile,
23433 obstack *obstack,
23434 LONGEST *len)
23435 {
23436 struct die_info *die;
23437 struct attribute *attr;
23438 const gdb_byte *result = NULL;
23439 struct type *type;
23440 LONGEST value;
23441 enum bfd_endian byte_order;
23442 struct objfile *objfile = per_objfile->objfile;
23443
23444 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23445 if (cu == nullptr)
23446 cu = load_cu (per_cu, per_objfile, false);
23447
23448 if (cu == nullptr)
23449 {
23450 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23451 Instead just throw an error, not much else we can do. */
23452 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23453 sect_offset_str (sect_off), objfile_name (objfile));
23454 }
23455
23456 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23457 if (!die)
23458 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23459 sect_offset_str (sect_off), objfile_name (objfile));
23460
23461 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23462 if (attr == NULL)
23463 return NULL;
23464
23465 byte_order = (bfd_big_endian (objfile->obfd)
23466 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23467
23468 switch (attr->form)
23469 {
23470 case DW_FORM_addr:
23471 case DW_FORM_addrx:
23472 case DW_FORM_GNU_addr_index:
23473 {
23474 gdb_byte *tem;
23475
23476 *len = cu->header.addr_size;
23477 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23478 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23479 result = tem;
23480 }
23481 break;
23482 case DW_FORM_string:
23483 case DW_FORM_strp:
23484 case DW_FORM_strx:
23485 case DW_FORM_GNU_str_index:
23486 case DW_FORM_GNU_strp_alt:
23487 /* The string is already allocated on the objfile obstack, point
23488 directly to it. */
23489 {
23490 const char *attr_name = attr->as_string ();
23491 result = (const gdb_byte *) attr_name;
23492 *len = strlen (attr_name);
23493 }
23494 break;
23495 case DW_FORM_block1:
23496 case DW_FORM_block2:
23497 case DW_FORM_block4:
23498 case DW_FORM_block:
23499 case DW_FORM_exprloc:
23500 case DW_FORM_data16:
23501 {
23502 struct dwarf_block *block = attr->as_block ();
23503 result = block->data;
23504 *len = block->size;
23505 }
23506 break;
23507
23508 /* The DW_AT_const_value attributes are supposed to carry the
23509 symbol's value "represented as it would be on the target
23510 architecture." By the time we get here, it's already been
23511 converted to host endianness, so we just need to sign- or
23512 zero-extend it as appropriate. */
23513 case DW_FORM_data1:
23514 type = die_type (die, cu);
23515 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23516 if (result == NULL)
23517 result = write_constant_as_bytes (obstack, byte_order,
23518 type, value, len);
23519 break;
23520 case DW_FORM_data2:
23521 type = die_type (die, cu);
23522 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23523 if (result == NULL)
23524 result = write_constant_as_bytes (obstack, byte_order,
23525 type, value, len);
23526 break;
23527 case DW_FORM_data4:
23528 type = die_type (die, cu);
23529 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23530 if (result == NULL)
23531 result = write_constant_as_bytes (obstack, byte_order,
23532 type, value, len);
23533 break;
23534 case DW_FORM_data8:
23535 type = die_type (die, cu);
23536 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23537 if (result == NULL)
23538 result = write_constant_as_bytes (obstack, byte_order,
23539 type, value, len);
23540 break;
23541
23542 case DW_FORM_sdata:
23543 case DW_FORM_implicit_const:
23544 type = die_type (die, cu);
23545 result = write_constant_as_bytes (obstack, byte_order,
23546 type, attr->as_signed (), len);
23547 break;
23548
23549 case DW_FORM_udata:
23550 type = die_type (die, cu);
23551 result = write_constant_as_bytes (obstack, byte_order,
23552 type, attr->as_unsigned (), len);
23553 break;
23554
23555 default:
23556 complaint (_("unsupported const value attribute form: '%s'"),
23557 dwarf_form_name (attr->form));
23558 break;
23559 }
23560
23561 return result;
23562 }
23563
23564 /* See read.h. */
23565
23566 struct type *
23567 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23568 dwarf2_per_cu_data *per_cu,
23569 dwarf2_per_objfile *per_objfile)
23570 {
23571 struct die_info *die;
23572
23573 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23574 if (cu == nullptr)
23575 cu = load_cu (per_cu, per_objfile, false);
23576
23577 if (cu == nullptr)
23578 return nullptr;
23579
23580 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23581 if (!die)
23582 return NULL;
23583
23584 return die_type (die, cu);
23585 }
23586
23587 /* See read.h. */
23588
23589 struct type *
23590 dwarf2_get_die_type (cu_offset die_offset,
23591 dwarf2_per_cu_data *per_cu,
23592 dwarf2_per_objfile *per_objfile)
23593 {
23594 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23595 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
23596 }
23597
23598 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23599 On entry *REF_CU is the CU of SRC_DIE.
23600 On exit *REF_CU is the CU of the result.
23601 Returns NULL if the referenced DIE isn't found. */
23602
23603 static struct die_info *
23604 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23605 struct dwarf2_cu **ref_cu)
23606 {
23607 struct die_info temp_die;
23608 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23609 struct die_info *die;
23610 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
23611
23612
23613 /* While it might be nice to assert sig_type->type == NULL here,
23614 we can get here for DW_AT_imported_declaration where we need
23615 the DIE not the type. */
23616
23617 /* If necessary, add it to the queue and load its DIEs. */
23618
23619 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
23620 language_minimal))
23621 read_signatured_type (sig_type, per_objfile);
23622
23623 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
23624 gdb_assert (sig_cu != NULL);
23625 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23626 temp_die.sect_off = sig_type->type_offset_in_section;
23627 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23628 to_underlying (temp_die.sect_off));
23629 if (die)
23630 {
23631 /* For .gdb_index version 7 keep track of included TUs.
23632 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23633 if (per_objfile->per_bfd->index_table != NULL
23634 && per_objfile->per_bfd->index_table->version <= 7)
23635 {
23636 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23637 }
23638
23639 *ref_cu = sig_cu;
23640 if (sig_cu != cu)
23641 sig_cu->ancestor = cu;
23642
23643 return die;
23644 }
23645
23646 return NULL;
23647 }
23648
23649 /* Follow signatured type referenced by ATTR in SRC_DIE.
23650 On entry *REF_CU is the CU of SRC_DIE.
23651 On exit *REF_CU is the CU of the result.
23652 The result is the DIE of the type.
23653 If the referenced type cannot be found an error is thrown. */
23654
23655 static struct die_info *
23656 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23657 struct dwarf2_cu **ref_cu)
23658 {
23659 ULONGEST signature = attr->as_signature ();
23660 struct signatured_type *sig_type;
23661 struct die_info *die;
23662
23663 gdb_assert (attr->form == DW_FORM_ref_sig8);
23664
23665 sig_type = lookup_signatured_type (*ref_cu, signature);
23666 /* sig_type will be NULL if the signatured type is missing from
23667 the debug info. */
23668 if (sig_type == NULL)
23669 {
23670 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23671 " from DIE at %s [in module %s]"),
23672 hex_string (signature), sect_offset_str (src_die->sect_off),
23673 objfile_name ((*ref_cu)->per_objfile->objfile));
23674 }
23675
23676 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23677 if (die == NULL)
23678 {
23679 dump_die_for_error (src_die);
23680 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23681 " from DIE at %s [in module %s]"),
23682 hex_string (signature), sect_offset_str (src_die->sect_off),
23683 objfile_name ((*ref_cu)->per_objfile->objfile));
23684 }
23685
23686 return die;
23687 }
23688
23689 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23690 reading in and processing the type unit if necessary. */
23691
23692 static struct type *
23693 get_signatured_type (struct die_info *die, ULONGEST signature,
23694 struct dwarf2_cu *cu)
23695 {
23696 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23697 struct signatured_type *sig_type;
23698 struct dwarf2_cu *type_cu;
23699 struct die_info *type_die;
23700 struct type *type;
23701
23702 sig_type = lookup_signatured_type (cu, signature);
23703 /* sig_type will be NULL if the signatured type is missing from
23704 the debug info. */
23705 if (sig_type == NULL)
23706 {
23707 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23708 " from DIE at %s [in module %s]"),
23709 hex_string (signature), sect_offset_str (die->sect_off),
23710 objfile_name (per_objfile->objfile));
23711 return build_error_marker_type (cu, die);
23712 }
23713
23714 /* If we already know the type we're done. */
23715 type = per_objfile->get_type_for_signatured_type (sig_type);
23716 if (type != nullptr)
23717 return type;
23718
23719 type_cu = cu;
23720 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23721 if (type_die != NULL)
23722 {
23723 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23724 is created. This is important, for example, because for c++ classes
23725 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23726 type = read_type_die (type_die, type_cu);
23727 if (type == NULL)
23728 {
23729 complaint (_("Dwarf Error: Cannot build signatured type %s"
23730 " referenced from DIE at %s [in module %s]"),
23731 hex_string (signature), sect_offset_str (die->sect_off),
23732 objfile_name (per_objfile->objfile));
23733 type = build_error_marker_type (cu, die);
23734 }
23735 }
23736 else
23737 {
23738 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23739 " from DIE at %s [in module %s]"),
23740 hex_string (signature), sect_offset_str (die->sect_off),
23741 objfile_name (per_objfile->objfile));
23742 type = build_error_marker_type (cu, die);
23743 }
23744
23745 per_objfile->set_type_for_signatured_type (sig_type, type);
23746
23747 return type;
23748 }
23749
23750 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23751 reading in and processing the type unit if necessary. */
23752
23753 static struct type *
23754 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23755 struct dwarf2_cu *cu) /* ARI: editCase function */
23756 {
23757 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23758 if (attr->form_is_ref ())
23759 {
23760 struct dwarf2_cu *type_cu = cu;
23761 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23762
23763 return read_type_die (type_die, type_cu);
23764 }
23765 else if (attr->form == DW_FORM_ref_sig8)
23766 {
23767 return get_signatured_type (die, attr->as_signature (), cu);
23768 }
23769 else
23770 {
23771 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23772
23773 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23774 " at %s [in module %s]"),
23775 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23776 objfile_name (per_objfile->objfile));
23777 return build_error_marker_type (cu, die);
23778 }
23779 }
23780
23781 /* Load the DIEs associated with type unit PER_CU into memory. */
23782
23783 static void
23784 load_full_type_unit (dwarf2_per_cu_data *per_cu,
23785 dwarf2_per_objfile *per_objfile)
23786 {
23787 struct signatured_type *sig_type;
23788
23789 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23790 gdb_assert (! per_cu->type_unit_group_p ());
23791
23792 /* We have the per_cu, but we need the signatured_type.
23793 Fortunately this is an easy translation. */
23794 gdb_assert (per_cu->is_debug_types);
23795 sig_type = (struct signatured_type *) per_cu;
23796
23797 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
23798
23799 read_signatured_type (sig_type, per_objfile);
23800
23801 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
23802 }
23803
23804 /* Read in a signatured type and build its CU and DIEs.
23805 If the type is a stub for the real type in a DWO file,
23806 read in the real type from the DWO file as well. */
23807
23808 static void
23809 read_signatured_type (signatured_type *sig_type,
23810 dwarf2_per_objfile *per_objfile)
23811 {
23812 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23813
23814 gdb_assert (per_cu->is_debug_types);
23815 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
23816
23817 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
23818
23819 if (!reader.dummy_p)
23820 {
23821 struct dwarf2_cu *cu = reader.cu;
23822 const gdb_byte *info_ptr = reader.info_ptr;
23823
23824 gdb_assert (cu->die_hash == NULL);
23825 cu->die_hash =
23826 htab_create_alloc_ex (cu->header.length / 12,
23827 die_hash,
23828 die_eq,
23829 NULL,
23830 &cu->comp_unit_obstack,
23831 hashtab_obstack_allocate,
23832 dummy_obstack_deallocate);
23833
23834 if (reader.comp_unit_die->has_children)
23835 reader.comp_unit_die->child
23836 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23837 reader.comp_unit_die);
23838 cu->dies = reader.comp_unit_die;
23839 /* comp_unit_die is not stored in die_hash, no need. */
23840
23841 /* We try not to read any attributes in this function, because
23842 not all CUs needed for references have been loaded yet, and
23843 symbol table processing isn't initialized. But we have to
23844 set the CU language, or we won't be able to build types
23845 correctly. Similarly, if we do not read the producer, we can
23846 not apply producer-specific interpretation. */
23847 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23848
23849 reader.keep ();
23850 }
23851
23852 sig_type->per_cu.tu_read = 1;
23853 }
23854
23855 /* Decode simple location descriptions.
23856 Given a pointer to a dwarf block that defines a location, compute
23857 the location and return the value. If COMPUTED is non-null, it is
23858 set to true to indicate that decoding was successful, and false
23859 otherwise. If COMPUTED is null, then this function may emit a
23860 complaint. */
23861
23862 static CORE_ADDR
23863 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
23864 {
23865 struct objfile *objfile = cu->per_objfile->objfile;
23866 size_t i;
23867 size_t size = blk->size;
23868 const gdb_byte *data = blk->data;
23869 CORE_ADDR stack[64];
23870 int stacki;
23871 unsigned int bytes_read, unsnd;
23872 gdb_byte op;
23873
23874 if (computed != nullptr)
23875 *computed = false;
23876
23877 i = 0;
23878 stacki = 0;
23879 stack[stacki] = 0;
23880 stack[++stacki] = 0;
23881
23882 while (i < size)
23883 {
23884 op = data[i++];
23885 switch (op)
23886 {
23887 case DW_OP_lit0:
23888 case DW_OP_lit1:
23889 case DW_OP_lit2:
23890 case DW_OP_lit3:
23891 case DW_OP_lit4:
23892 case DW_OP_lit5:
23893 case DW_OP_lit6:
23894 case DW_OP_lit7:
23895 case DW_OP_lit8:
23896 case DW_OP_lit9:
23897 case DW_OP_lit10:
23898 case DW_OP_lit11:
23899 case DW_OP_lit12:
23900 case DW_OP_lit13:
23901 case DW_OP_lit14:
23902 case DW_OP_lit15:
23903 case DW_OP_lit16:
23904 case DW_OP_lit17:
23905 case DW_OP_lit18:
23906 case DW_OP_lit19:
23907 case DW_OP_lit20:
23908 case DW_OP_lit21:
23909 case DW_OP_lit22:
23910 case DW_OP_lit23:
23911 case DW_OP_lit24:
23912 case DW_OP_lit25:
23913 case DW_OP_lit26:
23914 case DW_OP_lit27:
23915 case DW_OP_lit28:
23916 case DW_OP_lit29:
23917 case DW_OP_lit30:
23918 case DW_OP_lit31:
23919 stack[++stacki] = op - DW_OP_lit0;
23920 break;
23921
23922 case DW_OP_reg0:
23923 case DW_OP_reg1:
23924 case DW_OP_reg2:
23925 case DW_OP_reg3:
23926 case DW_OP_reg4:
23927 case DW_OP_reg5:
23928 case DW_OP_reg6:
23929 case DW_OP_reg7:
23930 case DW_OP_reg8:
23931 case DW_OP_reg9:
23932 case DW_OP_reg10:
23933 case DW_OP_reg11:
23934 case DW_OP_reg12:
23935 case DW_OP_reg13:
23936 case DW_OP_reg14:
23937 case DW_OP_reg15:
23938 case DW_OP_reg16:
23939 case DW_OP_reg17:
23940 case DW_OP_reg18:
23941 case DW_OP_reg19:
23942 case DW_OP_reg20:
23943 case DW_OP_reg21:
23944 case DW_OP_reg22:
23945 case DW_OP_reg23:
23946 case DW_OP_reg24:
23947 case DW_OP_reg25:
23948 case DW_OP_reg26:
23949 case DW_OP_reg27:
23950 case DW_OP_reg28:
23951 case DW_OP_reg29:
23952 case DW_OP_reg30:
23953 case DW_OP_reg31:
23954 stack[++stacki] = op - DW_OP_reg0;
23955 if (i < size)
23956 {
23957 if (computed == nullptr)
23958 dwarf2_complex_location_expr_complaint ();
23959 else
23960 return 0;
23961 }
23962 break;
23963
23964 case DW_OP_regx:
23965 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23966 i += bytes_read;
23967 stack[++stacki] = unsnd;
23968 if (i < size)
23969 {
23970 if (computed == nullptr)
23971 dwarf2_complex_location_expr_complaint ();
23972 else
23973 return 0;
23974 }
23975 break;
23976
23977 case DW_OP_addr:
23978 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23979 &bytes_read);
23980 i += bytes_read;
23981 break;
23982
23983 case DW_OP_const1u:
23984 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23985 i += 1;
23986 break;
23987
23988 case DW_OP_const1s:
23989 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23990 i += 1;
23991 break;
23992
23993 case DW_OP_const2u:
23994 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23995 i += 2;
23996 break;
23997
23998 case DW_OP_const2s:
23999 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24000 i += 2;
24001 break;
24002
24003 case DW_OP_const4u:
24004 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24005 i += 4;
24006 break;
24007
24008 case DW_OP_const4s:
24009 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24010 i += 4;
24011 break;
24012
24013 case DW_OP_const8u:
24014 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24015 i += 8;
24016 break;
24017
24018 case DW_OP_constu:
24019 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24020 &bytes_read);
24021 i += bytes_read;
24022 break;
24023
24024 case DW_OP_consts:
24025 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24026 i += bytes_read;
24027 break;
24028
24029 case DW_OP_dup:
24030 stack[stacki + 1] = stack[stacki];
24031 stacki++;
24032 break;
24033
24034 case DW_OP_plus:
24035 stack[stacki - 1] += stack[stacki];
24036 stacki--;
24037 break;
24038
24039 case DW_OP_plus_uconst:
24040 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24041 &bytes_read);
24042 i += bytes_read;
24043 break;
24044
24045 case DW_OP_minus:
24046 stack[stacki - 1] -= stack[stacki];
24047 stacki--;
24048 break;
24049
24050 case DW_OP_deref:
24051 /* If we're not the last op, then we definitely can't encode
24052 this using GDB's address_class enum. This is valid for partial
24053 global symbols, although the variable's address will be bogus
24054 in the psymtab. */
24055 if (i < size)
24056 {
24057 if (computed == nullptr)
24058 dwarf2_complex_location_expr_complaint ();
24059 else
24060 return 0;
24061 }
24062 break;
24063
24064 case DW_OP_GNU_push_tls_address:
24065 case DW_OP_form_tls_address:
24066 /* The top of the stack has the offset from the beginning
24067 of the thread control block at which the variable is located. */
24068 /* Nothing should follow this operator, so the top of stack would
24069 be returned. */
24070 /* This is valid for partial global symbols, but the variable's
24071 address will be bogus in the psymtab. Make it always at least
24072 non-zero to not look as a variable garbage collected by linker
24073 which have DW_OP_addr 0. */
24074 if (i < size)
24075 {
24076 if (computed == nullptr)
24077 dwarf2_complex_location_expr_complaint ();
24078 else
24079 return 0;
24080 }
24081 stack[stacki]++;
24082 break;
24083
24084 case DW_OP_GNU_uninit:
24085 if (computed != nullptr)
24086 return 0;
24087 break;
24088
24089 case DW_OP_addrx:
24090 case DW_OP_GNU_addr_index:
24091 case DW_OP_GNU_const_index:
24092 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24093 &bytes_read);
24094 i += bytes_read;
24095 break;
24096
24097 default:
24098 if (computed == nullptr)
24099 {
24100 const char *name = get_DW_OP_name (op);
24101
24102 if (name)
24103 complaint (_("unsupported stack op: '%s'"),
24104 name);
24105 else
24106 complaint (_("unsupported stack op: '%02x'"),
24107 op);
24108 }
24109
24110 return (stack[stacki]);
24111 }
24112
24113 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24114 outside of the allocated space. Also enforce minimum>0. */
24115 if (stacki >= ARRAY_SIZE (stack) - 1)
24116 {
24117 if (computed == nullptr)
24118 complaint (_("location description stack overflow"));
24119 return 0;
24120 }
24121
24122 if (stacki <= 0)
24123 {
24124 if (computed == nullptr)
24125 complaint (_("location description stack underflow"));
24126 return 0;
24127 }
24128 }
24129
24130 if (computed != nullptr)
24131 *computed = true;
24132 return (stack[stacki]);
24133 }
24134
24135 /* memory allocation interface */
24136
24137 static struct dwarf_block *
24138 dwarf_alloc_block (struct dwarf2_cu *cu)
24139 {
24140 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24141 }
24142
24143 static struct die_info *
24144 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24145 {
24146 struct die_info *die;
24147 size_t size = sizeof (struct die_info);
24148
24149 if (num_attrs > 1)
24150 size += (num_attrs - 1) * sizeof (struct attribute);
24151
24152 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24153 memset (die, 0, sizeof (struct die_info));
24154 return (die);
24155 }
24156
24157 \f
24158
24159 /* Macro support. */
24160
24161 /* An overload of dwarf_decode_macros that finds the correct section
24162 and ensures it is read in before calling the other overload. */
24163
24164 static void
24165 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24166 int section_is_gnu)
24167 {
24168 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24169 struct objfile *objfile = per_objfile->objfile;
24170 const struct line_header *lh = cu->line_header;
24171 unsigned int offset_size = cu->header.offset_size;
24172 struct dwarf2_section_info *section;
24173 const char *section_name;
24174
24175 if (cu->dwo_unit != nullptr)
24176 {
24177 if (section_is_gnu)
24178 {
24179 section = &cu->dwo_unit->dwo_file->sections.macro;
24180 section_name = ".debug_macro.dwo";
24181 }
24182 else
24183 {
24184 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24185 section_name = ".debug_macinfo.dwo";
24186 }
24187 }
24188 else
24189 {
24190 if (section_is_gnu)
24191 {
24192 section = &per_objfile->per_bfd->macro;
24193 section_name = ".debug_macro";
24194 }
24195 else
24196 {
24197 section = &per_objfile->per_bfd->macinfo;
24198 section_name = ".debug_macinfo";
24199 }
24200 }
24201
24202 section->read (objfile);
24203 if (section->buffer == nullptr)
24204 {
24205 complaint (_("missing %s section"), section_name);
24206 return;
24207 }
24208
24209 buildsym_compunit *builder = cu->get_builder ();
24210
24211 struct dwarf2_section_info *str_offsets_section;
24212 struct dwarf2_section_info *str_section;
24213 ULONGEST str_offsets_base;
24214
24215 if (cu->dwo_unit != nullptr)
24216 {
24217 str_offsets_section = &cu->dwo_unit->dwo_file
24218 ->sections.str_offsets;
24219 str_section = &cu->dwo_unit->dwo_file->sections.str;
24220 str_offsets_base = cu->header.addr_size;
24221 }
24222 else
24223 {
24224 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24225 str_section = &per_objfile->per_bfd->str;
24226 str_offsets_base = *cu->str_offsets_base;
24227 }
24228
24229 dwarf_decode_macros (per_objfile, builder, section, lh,
24230 offset_size, offset, str_section, str_offsets_section,
24231 str_offsets_base, section_is_gnu);
24232 }
24233
24234 /* Return the .debug_loc section to use for CU.
24235 For DWO files use .debug_loc.dwo. */
24236
24237 static struct dwarf2_section_info *
24238 cu_debug_loc_section (struct dwarf2_cu *cu)
24239 {
24240 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24241
24242 if (cu->dwo_unit)
24243 {
24244 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24245
24246 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24247 }
24248 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24249 : &per_objfile->per_bfd->loc);
24250 }
24251
24252 /* Return the .debug_rnglists section to use for CU. */
24253 static struct dwarf2_section_info *
24254 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24255 {
24256 if (cu->header.version < 5)
24257 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24258 cu->header.version);
24259 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24260
24261 /* Make sure we read the .debug_rnglists section from the file that
24262 contains the DW_AT_ranges attribute we are reading. Normally that
24263 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24264 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24265 program. */
24266 if (cu->dwo_unit != nullptr
24267 && tag != DW_TAG_compile_unit
24268 && tag != DW_TAG_skeleton_unit)
24269 {
24270 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24271
24272 if (sections->rnglists.size > 0)
24273 return &sections->rnglists;
24274 else
24275 error (_(".debug_rnglists section is missing from .dwo file."));
24276 }
24277 return &dwarf2_per_objfile->per_bfd->rnglists;
24278 }
24279
24280 /* A helper function that fills in a dwarf2_loclist_baton. */
24281
24282 static void
24283 fill_in_loclist_baton (struct dwarf2_cu *cu,
24284 struct dwarf2_loclist_baton *baton,
24285 const struct attribute *attr)
24286 {
24287 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24288 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24289
24290 section->read (per_objfile->objfile);
24291
24292 baton->per_objfile = per_objfile;
24293 baton->per_cu = cu->per_cu;
24294 gdb_assert (baton->per_cu);
24295 /* We don't know how long the location list is, but make sure we
24296 don't run off the edge of the section. */
24297 baton->size = section->size - attr->as_unsigned ();
24298 baton->data = section->buffer + attr->as_unsigned ();
24299 if (cu->base_address.has_value ())
24300 baton->base_address = *cu->base_address;
24301 else
24302 baton->base_address = 0;
24303 baton->from_dwo = cu->dwo_unit != NULL;
24304 }
24305
24306 static void
24307 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24308 struct dwarf2_cu *cu, int is_block)
24309 {
24310 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24311 struct objfile *objfile = per_objfile->objfile;
24312 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24313
24314 if (attr->form_is_section_offset ()
24315 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24316 the section. If so, fall through to the complaint in the
24317 other branch. */
24318 && attr->as_unsigned () < section->get_size (objfile))
24319 {
24320 struct dwarf2_loclist_baton *baton;
24321
24322 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24323
24324 fill_in_loclist_baton (cu, baton, attr);
24325
24326 if (!cu->base_address.has_value ())
24327 complaint (_("Location list used without "
24328 "specifying the CU base address."));
24329
24330 SYMBOL_ACLASS_INDEX (sym) = (is_block
24331 ? dwarf2_loclist_block_index
24332 : dwarf2_loclist_index);
24333 SYMBOL_LOCATION_BATON (sym) = baton;
24334 }
24335 else
24336 {
24337 struct dwarf2_locexpr_baton *baton;
24338
24339 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24340 baton->per_objfile = per_objfile;
24341 baton->per_cu = cu->per_cu;
24342 gdb_assert (baton->per_cu);
24343
24344 if (attr->form_is_block ())
24345 {
24346 /* Note that we're just copying the block's data pointer
24347 here, not the actual data. We're still pointing into the
24348 info_buffer for SYM's objfile; right now we never release
24349 that buffer, but when we do clean up properly this may
24350 need to change. */
24351 struct dwarf_block *block = attr->as_block ();
24352 baton->size = block->size;
24353 baton->data = block->data;
24354 }
24355 else
24356 {
24357 dwarf2_invalid_attrib_class_complaint ("location description",
24358 sym->natural_name ());
24359 baton->size = 0;
24360 }
24361
24362 SYMBOL_ACLASS_INDEX (sym) = (is_block
24363 ? dwarf2_locexpr_block_index
24364 : dwarf2_locexpr_index);
24365 SYMBOL_LOCATION_BATON (sym) = baton;
24366 }
24367 }
24368
24369 /* See read.h. */
24370
24371 const comp_unit_head *
24372 dwarf2_per_cu_data::get_header () const
24373 {
24374 if (!m_header_read_in)
24375 {
24376 const gdb_byte *info_ptr
24377 = this->section->buffer + to_underlying (this->sect_off);
24378
24379 memset (&m_header, 0, sizeof (m_header));
24380
24381 read_comp_unit_head (&m_header, info_ptr, this->section,
24382 rcuh_kind::COMPILE);
24383 }
24384
24385 return &m_header;
24386 }
24387
24388 /* See read.h. */
24389
24390 int
24391 dwarf2_per_cu_data::addr_size () const
24392 {
24393 return this->get_header ()->addr_size;
24394 }
24395
24396 /* See read.h. */
24397
24398 int
24399 dwarf2_per_cu_data::offset_size () const
24400 {
24401 return this->get_header ()->offset_size;
24402 }
24403
24404 /* See read.h. */
24405
24406 int
24407 dwarf2_per_cu_data::ref_addr_size () const
24408 {
24409 const comp_unit_head *header = this->get_header ();
24410
24411 if (header->version == 2)
24412 return header->addr_size;
24413 else
24414 return header->offset_size;
24415 }
24416
24417 /* See read.h. */
24418
24419 struct type *
24420 dwarf2_cu::addr_type () const
24421 {
24422 struct objfile *objfile = this->per_objfile->objfile;
24423 struct type *void_type = objfile_type (objfile)->builtin_void;
24424 struct type *addr_type = lookup_pointer_type (void_type);
24425 int addr_size = this->per_cu->addr_size ();
24426
24427 if (TYPE_LENGTH (addr_type) == addr_size)
24428 return addr_type;
24429
24430 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
24431 return addr_type;
24432 }
24433
24434 /* A helper function for dwarf2_find_containing_comp_unit that returns
24435 the index of the result, and that searches a vector. It will
24436 return a result even if the offset in question does not actually
24437 occur in any CU. This is separate so that it can be unit
24438 tested. */
24439
24440 static int
24441 dwarf2_find_containing_comp_unit
24442 (sect_offset sect_off,
24443 unsigned int offset_in_dwz,
24444 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
24445 {
24446 int low, high;
24447
24448 low = 0;
24449 high = all_comp_units.size () - 1;
24450 while (high > low)
24451 {
24452 struct dwarf2_per_cu_data *mid_cu;
24453 int mid = low + (high - low) / 2;
24454
24455 mid_cu = all_comp_units[mid];
24456 if (mid_cu->is_dwz > offset_in_dwz
24457 || (mid_cu->is_dwz == offset_in_dwz
24458 && mid_cu->sect_off + mid_cu->length > sect_off))
24459 high = mid;
24460 else
24461 low = mid + 1;
24462 }
24463 gdb_assert (low == high);
24464 return low;
24465 }
24466
24467 /* Locate the .debug_info compilation unit from CU's objfile which contains
24468 the DIE at OFFSET. Raises an error on failure. */
24469
24470 static struct dwarf2_per_cu_data *
24471 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24472 unsigned int offset_in_dwz,
24473 dwarf2_per_objfile *per_objfile)
24474 {
24475 int low = dwarf2_find_containing_comp_unit
24476 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24477 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
24478
24479 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24480 {
24481 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24482 error (_("Dwarf Error: could not find partial DIE containing "
24483 "offset %s [in module %s]"),
24484 sect_offset_str (sect_off),
24485 bfd_get_filename (per_objfile->objfile->obfd));
24486
24487 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
24488 <= sect_off);
24489 return per_objfile->per_bfd->all_comp_units[low-1];
24490 }
24491 else
24492 {
24493 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
24494 && sect_off >= this_cu->sect_off + this_cu->length)
24495 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24496 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24497 return this_cu;
24498 }
24499 }
24500
24501 #if GDB_SELF_TEST
24502
24503 namespace selftests {
24504 namespace find_containing_comp_unit {
24505
24506 static void
24507 run_test ()
24508 {
24509 struct dwarf2_per_cu_data one {};
24510 struct dwarf2_per_cu_data two {};
24511 struct dwarf2_per_cu_data three {};
24512 struct dwarf2_per_cu_data four {};
24513
24514 one.length = 5;
24515 two.sect_off = sect_offset (one.length);
24516 two.length = 7;
24517
24518 three.length = 5;
24519 three.is_dwz = 1;
24520 four.sect_off = sect_offset (three.length);
24521 four.length = 7;
24522 four.is_dwz = 1;
24523
24524 std::vector<dwarf2_per_cu_data *> units;
24525 units.push_back (&one);
24526 units.push_back (&two);
24527 units.push_back (&three);
24528 units.push_back (&four);
24529
24530 int result;
24531
24532 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24533 SELF_CHECK (units[result] == &one);
24534 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24535 SELF_CHECK (units[result] == &one);
24536 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24537 SELF_CHECK (units[result] == &two);
24538
24539 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24540 SELF_CHECK (units[result] == &three);
24541 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24542 SELF_CHECK (units[result] == &three);
24543 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24544 SELF_CHECK (units[result] == &four);
24545 }
24546
24547 }
24548 }
24549
24550 #endif /* GDB_SELF_TEST */
24551
24552 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
24553
24554 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
24555 dwarf2_per_objfile *per_objfile)
24556 : per_cu (per_cu),
24557 per_objfile (per_objfile),
24558 mark (false),
24559 has_loclist (false),
24560 checked_producer (false),
24561 producer_is_gxx_lt_4_6 (false),
24562 producer_is_gcc_lt_4_3 (false),
24563 producer_is_icc (false),
24564 producer_is_icc_lt_14 (false),
24565 producer_is_codewarrior (false),
24566 processing_has_namespace_info (false)
24567 {
24568 }
24569
24570 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24571
24572 static void
24573 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24574 enum language pretend_language)
24575 {
24576 struct attribute *attr;
24577
24578 /* Set the language we're debugging. */
24579 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24580 if (attr != nullptr)
24581 set_cu_language (attr->constant_value (0), cu);
24582 else
24583 {
24584 cu->language = pretend_language;
24585 cu->language_defn = language_def (cu->language);
24586 }
24587
24588 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24589 }
24590
24591 /* See read.h. */
24592
24593 dwarf2_cu *
24594 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
24595 {
24596 auto it = m_dwarf2_cus.find (per_cu);
24597 if (it == m_dwarf2_cus.end ())
24598 return nullptr;
24599
24600 return it->second;
24601 }
24602
24603 /* See read.h. */
24604
24605 void
24606 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24607 {
24608 gdb_assert (this->get_cu (per_cu) == nullptr);
24609
24610 m_dwarf2_cus[per_cu] = cu;
24611 }
24612
24613 /* See read.h. */
24614
24615 void
24616 dwarf2_per_objfile::age_comp_units ()
24617 {
24618 /* Start by clearing all marks. */
24619 for (auto pair : m_dwarf2_cus)
24620 pair.second->mark = false;
24621
24622 /* Traverse all CUs, mark them and their dependencies if used recently
24623 enough. */
24624 for (auto pair : m_dwarf2_cus)
24625 {
24626 dwarf2_cu *cu = pair.second;
24627
24628 cu->last_used++;
24629 if (cu->last_used <= dwarf_max_cache_age)
24630 dwarf2_mark (cu);
24631 }
24632
24633 /* Delete all CUs still not marked. */
24634 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
24635 {
24636 dwarf2_cu *cu = it->second;
24637
24638 if (!cu->mark)
24639 {
24640 delete cu;
24641 it = m_dwarf2_cus.erase (it);
24642 }
24643 else
24644 it++;
24645 }
24646 }
24647
24648 /* See read.h. */
24649
24650 void
24651 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
24652 {
24653 auto it = m_dwarf2_cus.find (per_cu);
24654 if (it == m_dwarf2_cus.end ())
24655 return;
24656
24657 delete it->second;
24658
24659 m_dwarf2_cus.erase (it);
24660 }
24661
24662 dwarf2_per_objfile::~dwarf2_per_objfile ()
24663 {
24664 remove_all_cus ();
24665 }
24666
24667 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24668 We store these in a hash table separate from the DIEs, and preserve them
24669 when the DIEs are flushed out of cache.
24670
24671 The CU "per_cu" pointer is needed because offset alone is not enough to
24672 uniquely identify the type. A file may have multiple .debug_types sections,
24673 or the type may come from a DWO file. Furthermore, while it's more logical
24674 to use per_cu->section+offset, with Fission the section with the data is in
24675 the DWO file but we don't know that section at the point we need it.
24676 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24677 because we can enter the lookup routine, get_die_type_at_offset, from
24678 outside this file, and thus won't necessarily have PER_CU->cu.
24679 Fortunately, PER_CU is stable for the life of the objfile. */
24680
24681 struct dwarf2_per_cu_offset_and_type
24682 {
24683 const struct dwarf2_per_cu_data *per_cu;
24684 sect_offset sect_off;
24685 struct type *type;
24686 };
24687
24688 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24689
24690 static hashval_t
24691 per_cu_offset_and_type_hash (const void *item)
24692 {
24693 const struct dwarf2_per_cu_offset_and_type *ofs
24694 = (const struct dwarf2_per_cu_offset_and_type *) item;
24695
24696 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24697 }
24698
24699 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24700
24701 static int
24702 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24703 {
24704 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24705 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24706 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24707 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24708
24709 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24710 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24711 }
24712
24713 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24714 table if necessary. For convenience, return TYPE.
24715
24716 The DIEs reading must have careful ordering to:
24717 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24718 reading current DIE.
24719 * Not trying to dereference contents of still incompletely read in types
24720 while reading in other DIEs.
24721 * Enable referencing still incompletely read in types just by a pointer to
24722 the type without accessing its fields.
24723
24724 Therefore caller should follow these rules:
24725 * Try to fetch any prerequisite types we may need to build this DIE type
24726 before building the type and calling set_die_type.
24727 * After building type call set_die_type for current DIE as soon as
24728 possible before fetching more types to complete the current type.
24729 * Make the type as complete as possible before fetching more types. */
24730
24731 static struct type *
24732 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24733 bool skip_data_location)
24734 {
24735 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24736 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24737 struct objfile *objfile = per_objfile->objfile;
24738 struct attribute *attr;
24739 struct dynamic_prop prop;
24740
24741 /* For Ada types, make sure that the gnat-specific data is always
24742 initialized (if not already set). There are a few types where
24743 we should not be doing so, because the type-specific area is
24744 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24745 where the type-specific area is used to store the floatformat).
24746 But this is not a problem, because the gnat-specific information
24747 is actually not needed for these types. */
24748 if (need_gnat_info (cu)
24749 && type->code () != TYPE_CODE_FUNC
24750 && type->code () != TYPE_CODE_FLT
24751 && type->code () != TYPE_CODE_METHODPTR
24752 && type->code () != TYPE_CODE_MEMBERPTR
24753 && type->code () != TYPE_CODE_METHOD
24754 && !HAVE_GNAT_AUX_INFO (type))
24755 INIT_GNAT_SPECIFIC (type);
24756
24757 /* Read DW_AT_allocated and set in type. */
24758 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24759 if (attr != NULL)
24760 {
24761 struct type *prop_type = cu->addr_sized_int_type (false);
24762 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24763 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
24764 }
24765
24766 /* Read DW_AT_associated and set in type. */
24767 attr = dwarf2_attr (die, DW_AT_associated, cu);
24768 if (attr != NULL)
24769 {
24770 struct type *prop_type = cu->addr_sized_int_type (false);
24771 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24772 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
24773 }
24774
24775 /* Read DW_AT_data_location and set in type. */
24776 if (!skip_data_location)
24777 {
24778 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24779 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24780 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24781 }
24782
24783 if (per_objfile->die_type_hash == NULL)
24784 per_objfile->die_type_hash
24785 = htab_up (htab_create_alloc (127,
24786 per_cu_offset_and_type_hash,
24787 per_cu_offset_and_type_eq,
24788 NULL, xcalloc, xfree));
24789
24790 ofs.per_cu = cu->per_cu;
24791 ofs.sect_off = die->sect_off;
24792 ofs.type = type;
24793 slot = (struct dwarf2_per_cu_offset_and_type **)
24794 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
24795 if (*slot)
24796 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24797 sect_offset_str (die->sect_off));
24798 *slot = XOBNEW (&objfile->objfile_obstack,
24799 struct dwarf2_per_cu_offset_and_type);
24800 **slot = ofs;
24801 return type;
24802 }
24803
24804 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24805 or return NULL if the die does not have a saved type. */
24806
24807 static struct type *
24808 get_die_type_at_offset (sect_offset sect_off,
24809 dwarf2_per_cu_data *per_cu,
24810 dwarf2_per_objfile *per_objfile)
24811 {
24812 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24813
24814 if (per_objfile->die_type_hash == NULL)
24815 return NULL;
24816
24817 ofs.per_cu = per_cu;
24818 ofs.sect_off = sect_off;
24819 slot = ((struct dwarf2_per_cu_offset_and_type *)
24820 htab_find (per_objfile->die_type_hash.get (), &ofs));
24821 if (slot)
24822 return slot->type;
24823 else
24824 return NULL;
24825 }
24826
24827 /* Look up the type for DIE in CU in die_type_hash,
24828 or return NULL if DIE does not have a saved type. */
24829
24830 static struct type *
24831 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24832 {
24833 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
24834 }
24835
24836 /* Add a dependence relationship from CU to REF_PER_CU. */
24837
24838 static void
24839 dwarf2_add_dependence (struct dwarf2_cu *cu,
24840 struct dwarf2_per_cu_data *ref_per_cu)
24841 {
24842 void **slot;
24843
24844 if (cu->dependencies == NULL)
24845 cu->dependencies
24846 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24847 NULL, &cu->comp_unit_obstack,
24848 hashtab_obstack_allocate,
24849 dummy_obstack_deallocate);
24850
24851 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24852 if (*slot == NULL)
24853 *slot = ref_per_cu;
24854 }
24855
24856 /* Subroutine of dwarf2_mark to pass to htab_traverse.
24857 Set the mark field in every compilation unit in the
24858 cache that we must keep because we are keeping CU.
24859
24860 DATA is the dwarf2_per_objfile object in which to look up CUs. */
24861
24862 static int
24863 dwarf2_mark_helper (void **slot, void *data)
24864 {
24865 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
24866 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
24867 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
24868
24869 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24870 reading of the chain. As such dependencies remain valid it is not much
24871 useful to track and undo them during QUIT cleanups. */
24872 if (cu == nullptr)
24873 return 1;
24874
24875 if (cu->mark)
24876 return 1;
24877
24878 cu->mark = true;
24879
24880 if (cu->dependencies != nullptr)
24881 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
24882
24883 return 1;
24884 }
24885
24886 /* Set the mark field in CU and in every other compilation unit in the
24887 cache that we must keep because we are keeping CU. */
24888
24889 static void
24890 dwarf2_mark (struct dwarf2_cu *cu)
24891 {
24892 if (cu->mark)
24893 return;
24894
24895 cu->mark = true;
24896
24897 if (cu->dependencies != nullptr)
24898 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
24899 }
24900
24901 /* Trivial hash function for partial_die_info: the hash value of a DIE
24902 is its offset in .debug_info for this objfile. */
24903
24904 static hashval_t
24905 partial_die_hash (const void *item)
24906 {
24907 const struct partial_die_info *part_die
24908 = (const struct partial_die_info *) item;
24909
24910 return to_underlying (part_die->sect_off);
24911 }
24912
24913 /* Trivial comparison function for partial_die_info structures: two DIEs
24914 are equal if they have the same offset. */
24915
24916 static int
24917 partial_die_eq (const void *item_lhs, const void *item_rhs)
24918 {
24919 const struct partial_die_info *part_die_lhs
24920 = (const struct partial_die_info *) item_lhs;
24921 const struct partial_die_info *part_die_rhs
24922 = (const struct partial_die_info *) item_rhs;
24923
24924 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24925 }
24926
24927 struct cmd_list_element *set_dwarf_cmdlist;
24928 struct cmd_list_element *show_dwarf_cmdlist;
24929
24930 static void
24931 show_check_physname (struct ui_file *file, int from_tty,
24932 struct cmd_list_element *c, const char *value)
24933 {
24934 fprintf_filtered (file,
24935 _("Whether to check \"physname\" is %s.\n"),
24936 value);
24937 }
24938
24939 void _initialize_dwarf2_read ();
24940 void
24941 _initialize_dwarf2_read ()
24942 {
24943 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
24944 Set DWARF specific variables.\n\
24945 Configure DWARF variables such as the cache size."),
24946 &set_dwarf_cmdlist, "maintenance set dwarf ",
24947 0/*allow-unknown*/, &maintenance_set_cmdlist);
24948
24949 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
24950 Show DWARF specific variables.\n\
24951 Show DWARF variables such as the cache size."),
24952 &show_dwarf_cmdlist, "maintenance show dwarf ",
24953 0/*allow-unknown*/, &maintenance_show_cmdlist);
24954
24955 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24956 &dwarf_max_cache_age, _("\
24957 Set the upper bound on the age of cached DWARF compilation units."), _("\
24958 Show the upper bound on the age of cached DWARF compilation units."), _("\
24959 A higher limit means that cached compilation units will be stored\n\
24960 in memory longer, and more total memory will be used. Zero disables\n\
24961 caching, which can slow down startup."),
24962 NULL,
24963 show_dwarf_max_cache_age,
24964 &set_dwarf_cmdlist,
24965 &show_dwarf_cmdlist);
24966
24967 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24968 Set debugging of the DWARF reader."), _("\
24969 Show debugging of the DWARF reader."), _("\
24970 When enabled (non-zero), debugging messages are printed during DWARF\n\
24971 reading and symtab expansion. A value of 1 (one) provides basic\n\
24972 information. A value greater than 1 provides more verbose information."),
24973 NULL,
24974 NULL,
24975 &setdebuglist, &showdebuglist);
24976
24977 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24978 Set debugging of the DWARF DIE reader."), _("\
24979 Show debugging of the DWARF DIE reader."), _("\
24980 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24981 The value is the maximum depth to print."),
24982 NULL,
24983 NULL,
24984 &setdebuglist, &showdebuglist);
24985
24986 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24987 Set debugging of the dwarf line reader."), _("\
24988 Show debugging of the dwarf line reader."), _("\
24989 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24990 A value of 1 (one) provides basic information.\n\
24991 A value greater than 1 provides more verbose information."),
24992 NULL,
24993 NULL,
24994 &setdebuglist, &showdebuglist);
24995
24996 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24997 Set cross-checking of \"physname\" code against demangler."), _("\
24998 Show cross-checking of \"physname\" code against demangler."), _("\
24999 When enabled, GDB's internal \"physname\" code is checked against\n\
25000 the demangler."),
25001 NULL, show_check_physname,
25002 &setdebuglist, &showdebuglist);
25003
25004 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25005 no_class, &use_deprecated_index_sections, _("\
25006 Set whether to use deprecated gdb_index sections."), _("\
25007 Show whether to use deprecated gdb_index sections."), _("\
25008 When enabled, deprecated .gdb_index sections are used anyway.\n\
25009 Normally they are ignored either because of a missing feature or\n\
25010 performance issue.\n\
25011 Warning: This option must be enabled before gdb reads the file."),
25012 NULL,
25013 NULL,
25014 &setlist, &showlist);
25015
25016 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25017 &dwarf2_locexpr_funcs);
25018 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25019 &dwarf2_loclist_funcs);
25020
25021 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25022 &dwarf2_block_frame_base_locexpr_funcs);
25023 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25024 &dwarf2_block_frame_base_loclist_funcs);
25025
25026 #if GDB_SELF_TEST
25027 selftests::register_test ("dw2_expand_symtabs_matching",
25028 selftests::dw2_expand_symtabs_matching::run_test);
25029 selftests::register_test ("dwarf2_find_containing_comp_unit",
25030 selftests::find_containing_comp_unit::run_test);
25031 #endif
25032 }
This page took 0.6757 seconds and 5 git commands to generate.