Change map_matching_symbols to take a lookup_name_info
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2019 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 "dwarf2read.h"
33 #include "dwarf-index-cache.h"
34 #include "dwarf-index-common.h"
35 #include "bfd.h"
36 #include "elf-bfd.h"
37 #include "symtab.h"
38 #include "gdbtypes.h"
39 #include "objfiles.h"
40 #include "dwarf2.h"
41 #include "buildsym.h"
42 #include "demangle.h"
43 #include "gdb-demangle.h"
44 #include "expression.h"
45 #include "filenames.h" /* for DOSish file names */
46 #include "macrotab.h"
47 #include "language.h"
48 #include "complaints.h"
49 #include "dwarf2expr.h"
50 #include "dwarf2loc.h"
51 #include "cp-support.h"
52 #include "hashtab.h"
53 #include "command.h"
54 #include "gdbcmd.h"
55 #include "block.h"
56 #include "addrmap.h"
57 #include "typeprint.h"
58 #include "psympriv.h"
59 #include <sys/stat.h>
60 #include "completer.h"
61 #include "gdbsupport/vec.h"
62 #include "c-lang.h"
63 #include "go-lang.h"
64 #include "valprint.h"
65 #include "gdbcore.h" /* for gnutarget */
66 #include "gdb/gdb-index.h"
67 #include <ctype.h>
68 #include "gdb_bfd.h"
69 #include "f-lang.h"
70 #include "source.h"
71 #include "gdbsupport/filestuff.h"
72 #include "build-id.h"
73 #include "namespace.h"
74 #include "gdbsupport/gdb_unlinker.h"
75 #include "gdbsupport/function-view.h"
76 #include "gdbsupport/gdb_optional.h"
77 #include "gdbsupport/underlying.h"
78 #include "gdbsupport/byte-vector.h"
79 #include "gdbsupport/hash_enum.h"
80 #include "filename-seen-cache.h"
81 #include "producer.h"
82 #include <fcntl.h>
83 #include <sys/types.h>
84 #include <algorithm>
85 #include <unordered_set>
86 #include <unordered_map>
87 #include "gdbsupport/selftest.h"
88 #include <cmath>
89 #include <set>
90 #include <forward_list>
91 #include "rust-lang.h"
92 #include "gdbsupport/pathstuff.h"
93
94 /* When == 1, print basic high level tracing messages.
95 When > 1, be more verbose.
96 This is in contrast to the low level DIE reading of dwarf_die_debug. */
97 static unsigned int dwarf_read_debug = 0;
98
99 /* When non-zero, dump DIEs after they are read in. */
100 static unsigned int dwarf_die_debug = 0;
101
102 /* When non-zero, dump line number entries as they are read in. */
103 static unsigned int dwarf_line_debug = 0;
104
105 /* When non-zero, cross-check physname against demangler. */
106 static int check_physname = 0;
107
108 /* When non-zero, do not reject deprecated .gdb_index sections. */
109 static int use_deprecated_index_sections = 0;
110
111 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
112
113 /* The "aclass" indices for various kinds of computed DWARF symbols. */
114
115 static int dwarf2_locexpr_index;
116 static int dwarf2_loclist_index;
117 static int dwarf2_locexpr_block_index;
118 static int dwarf2_loclist_block_index;
119
120 /* An index into a (C++) symbol name component in a symbol name as
121 recorded in the mapped_index's symbol table. For each C++ symbol
122 in the symbol table, we record one entry for the start of each
123 component in the symbol in a table of name components, and then
124 sort the table, in order to be able to binary search symbol names,
125 ignoring leading namespaces, both completion and regular look up.
126 For example, for symbol "A::B::C", we'll have an entry that points
127 to "A::B::C", another that points to "B::C", and another for "C".
128 Note that function symbols in GDB index have no parameter
129 information, just the function/method names. You can convert a
130 name_component to a "const char *" using the
131 'mapped_index::symbol_name_at(offset_type)' method. */
132
133 struct name_component
134 {
135 /* Offset in the symbol name where the component starts. Stored as
136 a (32-bit) offset instead of a pointer to save memory and improve
137 locality on 64-bit architectures. */
138 offset_type name_offset;
139
140 /* The symbol's index in the symbol and constant pool tables of a
141 mapped_index. */
142 offset_type idx;
143 };
144
145 /* Base class containing bits shared by both .gdb_index and
146 .debug_name indexes. */
147
148 struct mapped_index_base
149 {
150 mapped_index_base () = default;
151 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
152
153 /* The name_component table (a sorted vector). See name_component's
154 description above. */
155 std::vector<name_component> name_components;
156
157 /* How NAME_COMPONENTS is sorted. */
158 enum case_sensitivity name_components_casing;
159
160 /* Return the number of names in the symbol table. */
161 virtual size_t symbol_name_count () const = 0;
162
163 /* Get the name of the symbol at IDX in the symbol table. */
164 virtual const char *symbol_name_at (offset_type idx) const = 0;
165
166 /* Return whether the name at IDX in the symbol table should be
167 ignored. */
168 virtual bool symbol_name_slot_invalid (offset_type idx) const
169 {
170 return false;
171 }
172
173 /* Build the symbol name component sorted vector, if we haven't
174 yet. */
175 void build_name_components ();
176
177 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
178 possible matches for LN_NO_PARAMS in the name component
179 vector. */
180 std::pair<std::vector<name_component>::const_iterator,
181 std::vector<name_component>::const_iterator>
182 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
183
184 /* Prevent deleting/destroying via a base class pointer. */
185 protected:
186 ~mapped_index_base() = default;
187 };
188
189 /* A description of the mapped index. The file format is described in
190 a comment by the code that writes the index. */
191 struct mapped_index final : public mapped_index_base
192 {
193 /* A slot/bucket in the symbol table hash. */
194 struct symbol_table_slot
195 {
196 const offset_type name;
197 const offset_type vec;
198 };
199
200 /* Index data format version. */
201 int version = 0;
202
203 /* The address table data. */
204 gdb::array_view<const gdb_byte> address_table;
205
206 /* The symbol table, implemented as a hash table. */
207 gdb::array_view<symbol_table_slot> symbol_table;
208
209 /* A pointer to the constant pool. */
210 const char *constant_pool = nullptr;
211
212 bool symbol_name_slot_invalid (offset_type idx) const override
213 {
214 const auto &bucket = this->symbol_table[idx];
215 return bucket.name == 0 && bucket.vec == 0;
216 }
217
218 /* Convenience method to get at the name of the symbol at IDX in the
219 symbol table. */
220 const char *symbol_name_at (offset_type idx) const override
221 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
222
223 size_t symbol_name_count () const override
224 { return this->symbol_table.size (); }
225 };
226
227 /* A description of the mapped .debug_names.
228 Uninitialized map has CU_COUNT 0. */
229 struct mapped_debug_names final : public mapped_index_base
230 {
231 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
232 : dwarf2_per_objfile (dwarf2_per_objfile_)
233 {}
234
235 struct dwarf2_per_objfile *dwarf2_per_objfile;
236 bfd_endian dwarf5_byte_order;
237 bool dwarf5_is_dwarf64;
238 bool augmentation_is_gdb;
239 uint8_t offset_size;
240 uint32_t cu_count = 0;
241 uint32_t tu_count, bucket_count, name_count;
242 const gdb_byte *cu_table_reordered, *tu_table_reordered;
243 const uint32_t *bucket_table_reordered, *hash_table_reordered;
244 const gdb_byte *name_table_string_offs_reordered;
245 const gdb_byte *name_table_entry_offs_reordered;
246 const gdb_byte *entry_pool;
247
248 struct index_val
249 {
250 ULONGEST dwarf_tag;
251 struct attr
252 {
253 /* Attribute name DW_IDX_*. */
254 ULONGEST dw_idx;
255
256 /* Attribute form DW_FORM_*. */
257 ULONGEST form;
258
259 /* Value if FORM is DW_FORM_implicit_const. */
260 LONGEST implicit_const;
261 };
262 std::vector<attr> attr_vec;
263 };
264
265 std::unordered_map<ULONGEST, index_val> abbrev_map;
266
267 const char *namei_to_name (uint32_t namei) const;
268
269 /* Implementation of the mapped_index_base virtual interface, for
270 the name_components cache. */
271
272 const char *symbol_name_at (offset_type idx) const override
273 { return namei_to_name (idx); }
274
275 size_t symbol_name_count () const override
276 { return this->name_count; }
277 };
278
279 /* See dwarf2read.h. */
280
281 dwarf2_per_objfile *
282 get_dwarf2_per_objfile (struct objfile *objfile)
283 {
284 return dwarf2_objfile_data_key.get (objfile);
285 }
286
287 /* Default names of the debugging sections. */
288
289 /* Note that if the debugging section has been compressed, it might
290 have a name like .zdebug_info. */
291
292 static const struct dwarf2_debug_sections dwarf2_elf_names =
293 {
294 { ".debug_info", ".zdebug_info" },
295 { ".debug_abbrev", ".zdebug_abbrev" },
296 { ".debug_line", ".zdebug_line" },
297 { ".debug_loc", ".zdebug_loc" },
298 { ".debug_loclists", ".zdebug_loclists" },
299 { ".debug_macinfo", ".zdebug_macinfo" },
300 { ".debug_macro", ".zdebug_macro" },
301 { ".debug_str", ".zdebug_str" },
302 { ".debug_line_str", ".zdebug_line_str" },
303 { ".debug_ranges", ".zdebug_ranges" },
304 { ".debug_rnglists", ".zdebug_rnglists" },
305 { ".debug_types", ".zdebug_types" },
306 { ".debug_addr", ".zdebug_addr" },
307 { ".debug_frame", ".zdebug_frame" },
308 { ".eh_frame", NULL },
309 { ".gdb_index", ".zgdb_index" },
310 { ".debug_names", ".zdebug_names" },
311 { ".debug_aranges", ".zdebug_aranges" },
312 23
313 };
314
315 /* List of DWO/DWP sections. */
316
317 static const struct dwop_section_names
318 {
319 struct dwarf2_section_names abbrev_dwo;
320 struct dwarf2_section_names info_dwo;
321 struct dwarf2_section_names line_dwo;
322 struct dwarf2_section_names loc_dwo;
323 struct dwarf2_section_names loclists_dwo;
324 struct dwarf2_section_names macinfo_dwo;
325 struct dwarf2_section_names macro_dwo;
326 struct dwarf2_section_names str_dwo;
327 struct dwarf2_section_names str_offsets_dwo;
328 struct dwarf2_section_names types_dwo;
329 struct dwarf2_section_names cu_index;
330 struct dwarf2_section_names tu_index;
331 }
332 dwop_section_names =
333 {
334 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
335 { ".debug_info.dwo", ".zdebug_info.dwo" },
336 { ".debug_line.dwo", ".zdebug_line.dwo" },
337 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
338 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
339 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
340 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
341 { ".debug_str.dwo", ".zdebug_str.dwo" },
342 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
343 { ".debug_types.dwo", ".zdebug_types.dwo" },
344 { ".debug_cu_index", ".zdebug_cu_index" },
345 { ".debug_tu_index", ".zdebug_tu_index" },
346 };
347
348 /* local data types */
349
350 /* The data in a compilation unit header, after target2host
351 translation, looks like this. */
352 struct comp_unit_head
353 {
354 unsigned int length;
355 short version;
356 unsigned char addr_size;
357 unsigned char signed_addr_p;
358 sect_offset abbrev_sect_off;
359
360 /* Size of file offsets; either 4 or 8. */
361 unsigned int offset_size;
362
363 /* Size of the length field; either 4 or 12. */
364 unsigned int initial_length_size;
365
366 enum dwarf_unit_type unit_type;
367
368 /* Offset to the first byte of this compilation unit header in the
369 .debug_info section, for resolving relative reference dies. */
370 sect_offset sect_off;
371
372 /* Offset to first die in this cu from the start of the cu.
373 This will be the first byte following the compilation unit header. */
374 cu_offset first_die_cu_offset;
375
376
377 /* 64-bit signature of this unit. For type units, it denotes the signature of
378 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
379 Also used in DWARF 5, to denote the dwo id when the unit type is
380 DW_UT_skeleton or DW_UT_split_compile. */
381 ULONGEST signature;
382
383 /* For types, offset in the type's DIE of the type defined by this TU. */
384 cu_offset type_cu_offset_in_tu;
385 };
386
387 /* Type used for delaying computation of method physnames.
388 See comments for compute_delayed_physnames. */
389 struct delayed_method_info
390 {
391 /* The type to which the method is attached, i.e., its parent class. */
392 struct type *type;
393
394 /* The index of the method in the type's function fieldlists. */
395 int fnfield_index;
396
397 /* The index of the method in the fieldlist. */
398 int index;
399
400 /* The name of the DIE. */
401 const char *name;
402
403 /* The DIE associated with this method. */
404 struct die_info *die;
405 };
406
407 /* Internal state when decoding a particular compilation unit. */
408 struct dwarf2_cu
409 {
410 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
411 ~dwarf2_cu ();
412
413 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
414
415 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
416 Create the set of symtabs used by this TU, or if this TU is sharing
417 symtabs with another TU and the symtabs have already been created
418 then restore those symtabs in the line header.
419 We don't need the pc/line-number mapping for type units. */
420 void setup_type_unit_groups (struct die_info *die);
421
422 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
423 buildsym_compunit constructor. */
424 struct compunit_symtab *start_symtab (const char *name,
425 const char *comp_dir,
426 CORE_ADDR low_pc);
427
428 /* Reset the builder. */
429 void reset_builder () { m_builder.reset (); }
430
431 /* The header of the compilation unit. */
432 struct comp_unit_head header {};
433
434 /* Base address of this compilation unit. */
435 CORE_ADDR base_address = 0;
436
437 /* Non-zero if base_address has been set. */
438 int base_known = 0;
439
440 /* The language we are debugging. */
441 enum language language = language_unknown;
442 const struct language_defn *language_defn = nullptr;
443
444 const char *producer = nullptr;
445
446 private:
447 /* The symtab builder for this CU. This is only non-NULL when full
448 symbols are being read. */
449 std::unique_ptr<buildsym_compunit> m_builder;
450
451 public:
452 /* The generic symbol table building routines have separate lists for
453 file scope symbols and all all other scopes (local scopes). So
454 we need to select the right one to pass to add_symbol_to_list().
455 We do it by keeping a pointer to the correct list in list_in_scope.
456
457 FIXME: The original dwarf code just treated the file scope as the
458 first local scope, and all other local scopes as nested local
459 scopes, and worked fine. Check to see if we really need to
460 distinguish these in buildsym.c. */
461 struct pending **list_in_scope = nullptr;
462
463 /* Hash table holding all the loaded partial DIEs
464 with partial_die->offset.SECT_OFF as hash. */
465 htab_t partial_dies = nullptr;
466
467 /* Storage for things with the same lifetime as this read-in compilation
468 unit, including partial DIEs. */
469 auto_obstack comp_unit_obstack;
470
471 /* When multiple dwarf2_cu structures are living in memory, this field
472 chains them all together, so that they can be released efficiently.
473 We will probably also want a generation counter so that most-recently-used
474 compilation units are cached... */
475 struct dwarf2_per_cu_data *read_in_chain = nullptr;
476
477 /* Backlink to our per_cu entry. */
478 struct dwarf2_per_cu_data *per_cu;
479
480 /* How many compilation units ago was this CU last referenced? */
481 int last_used = 0;
482
483 /* A hash table of DIE cu_offset for following references with
484 die_info->offset.sect_off as hash. */
485 htab_t die_hash = nullptr;
486
487 /* Full DIEs if read in. */
488 struct die_info *dies = nullptr;
489
490 /* A set of pointers to dwarf2_per_cu_data objects for compilation
491 units referenced by this one. Only set during full symbol processing;
492 partial symbol tables do not have dependencies. */
493 htab_t dependencies = nullptr;
494
495 /* Header data from the line table, during full symbol processing. */
496 struct line_header *line_header = nullptr;
497 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
498 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
499 this is the DW_TAG_compile_unit die for this CU. We'll hold on
500 to the line header as long as this DIE is being processed. See
501 process_die_scope. */
502 die_info *line_header_die_owner = nullptr;
503
504 /* A list of methods which need to have physnames computed
505 after all type information has been read. */
506 std::vector<delayed_method_info> method_list;
507
508 /* To be copied to symtab->call_site_htab. */
509 htab_t call_site_htab = nullptr;
510
511 /* Non-NULL if this CU came from a DWO file.
512 There is an invariant here that is important to remember:
513 Except for attributes copied from the top level DIE in the "main"
514 (or "stub") file in preparation for reading the DWO file
515 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
516 Either there isn't a DWO file (in which case this is NULL and the point
517 is moot), or there is and either we're not going to read it (in which
518 case this is NULL) or there is and we are reading it (in which case this
519 is non-NULL). */
520 struct dwo_unit *dwo_unit = nullptr;
521
522 /* The DW_AT_addr_base attribute if present, zero otherwise
523 (zero is a valid value though).
524 Note this value comes from the Fission stub CU/TU's DIE. */
525 ULONGEST addr_base = 0;
526
527 /* The DW_AT_ranges_base attribute if present, zero otherwise
528 (zero is a valid value though).
529 Note this value comes from the Fission stub CU/TU's DIE.
530 Also note that the value is zero in the non-DWO case so this value can
531 be used without needing to know whether DWO files are in use or not.
532 N.B. This does not apply to DW_AT_ranges appearing in
533 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
534 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
535 DW_AT_ranges_base *would* have to be applied, and we'd have to care
536 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
537 ULONGEST ranges_base = 0;
538
539 /* When reading debug info generated by older versions of rustc, we
540 have to rewrite some union types to be struct types with a
541 variant part. This rewriting must be done after the CU is fully
542 read in, because otherwise at the point of rewriting some struct
543 type might not have been fully processed. So, we keep a list of
544 all such types here and process them after expansion. */
545 std::vector<struct type *> rust_unions;
546
547 /* Mark used when releasing cached dies. */
548 bool mark : 1;
549
550 /* This CU references .debug_loc. See the symtab->locations_valid field.
551 This test is imperfect as there may exist optimized debug code not using
552 any location list and still facing inlining issues if handled as
553 unoptimized code. For a future better test see GCC PR other/32998. */
554 bool has_loclist : 1;
555
556 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
557 if all the producer_is_* fields are valid. This information is cached
558 because profiling CU expansion showed excessive time spent in
559 producer_is_gxx_lt_4_6. */
560 bool checked_producer : 1;
561 bool producer_is_gxx_lt_4_6 : 1;
562 bool producer_is_gcc_lt_4_3 : 1;
563 bool producer_is_icc : 1;
564 bool producer_is_icc_lt_14 : 1;
565 bool producer_is_codewarrior : 1;
566
567 /* When true, the file that we're processing is known to have
568 debugging info for C++ namespaces. GCC 3.3.x did not produce
569 this information, but later versions do. */
570
571 bool processing_has_namespace_info : 1;
572
573 struct partial_die_info *find_partial_die (sect_offset sect_off);
574
575 /* If this CU was inherited by another CU (via specification,
576 abstract_origin, etc), this is the ancestor CU. */
577 dwarf2_cu *ancestor;
578
579 /* Get the buildsym_compunit for this CU. */
580 buildsym_compunit *get_builder ()
581 {
582 /* If this CU has a builder associated with it, use that. */
583 if (m_builder != nullptr)
584 return m_builder.get ();
585
586 /* Otherwise, search ancestors for a valid builder. */
587 if (ancestor != nullptr)
588 return ancestor->get_builder ();
589
590 return nullptr;
591 }
592 };
593
594 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
595 This includes type_unit_group and quick_file_names. */
596
597 struct stmt_list_hash
598 {
599 /* The DWO unit this table is from or NULL if there is none. */
600 struct dwo_unit *dwo_unit;
601
602 /* Offset in .debug_line or .debug_line.dwo. */
603 sect_offset line_sect_off;
604 };
605
606 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
607 an object of this type. */
608
609 struct type_unit_group
610 {
611 /* dwarf2read.c's main "handle" on a TU symtab.
612 To simplify things we create an artificial CU that "includes" all the
613 type units using this stmt_list so that the rest of the code still has
614 a "per_cu" handle on the symtab.
615 This PER_CU is recognized by having no section. */
616 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
617 struct dwarf2_per_cu_data per_cu;
618
619 /* The TUs that share this DW_AT_stmt_list entry.
620 This is added to while parsing type units to build partial symtabs,
621 and is deleted afterwards and not used again. */
622 VEC (sig_type_ptr) *tus;
623
624 /* The compunit symtab.
625 Type units in a group needn't all be defined in the same source file,
626 so we create an essentially anonymous symtab as the compunit symtab. */
627 struct compunit_symtab *compunit_symtab;
628
629 /* The data used to construct the hash key. */
630 struct stmt_list_hash hash;
631
632 /* The number of symtabs from the line header.
633 The value here must match line_header.num_file_names. */
634 unsigned int num_symtabs;
635
636 /* The symbol tables for this TU (obtained from the files listed in
637 DW_AT_stmt_list).
638 WARNING: The order of entries here must match the order of entries
639 in the line header. After the first TU using this type_unit_group, the
640 line header for the subsequent TUs is recreated from this. This is done
641 because we need to use the same symtabs for each TU using the same
642 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
643 there's no guarantee the line header doesn't have duplicate entries. */
644 struct symtab **symtabs;
645 };
646
647 /* These sections are what may appear in a (real or virtual) DWO file. */
648
649 struct dwo_sections
650 {
651 struct dwarf2_section_info abbrev;
652 struct dwarf2_section_info line;
653 struct dwarf2_section_info loc;
654 struct dwarf2_section_info loclists;
655 struct dwarf2_section_info macinfo;
656 struct dwarf2_section_info macro;
657 struct dwarf2_section_info str;
658 struct dwarf2_section_info str_offsets;
659 /* In the case of a virtual DWO file, these two are unused. */
660 struct dwarf2_section_info info;
661 std::vector<dwarf2_section_info> types;
662 };
663
664 /* CUs/TUs in DWP/DWO files. */
665
666 struct dwo_unit
667 {
668 /* Backlink to the containing struct dwo_file. */
669 struct dwo_file *dwo_file;
670
671 /* The "id" that distinguishes this CU/TU.
672 .debug_info calls this "dwo_id", .debug_types calls this "signature".
673 Since signatures came first, we stick with it for consistency. */
674 ULONGEST signature;
675
676 /* The section this CU/TU lives in, in the DWO file. */
677 struct dwarf2_section_info *section;
678
679 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
680 sect_offset sect_off;
681 unsigned int length;
682
683 /* For types, offset in the type's DIE of the type defined by this TU. */
684 cu_offset type_offset_in_tu;
685 };
686
687 /* include/dwarf2.h defines the DWP section codes.
688 It defines a max value but it doesn't define a min value, which we
689 use for error checking, so provide one. */
690
691 enum dwp_v2_section_ids
692 {
693 DW_SECT_MIN = 1
694 };
695
696 /* Data for one DWO file.
697
698 This includes virtual DWO files (a virtual DWO file is a DWO file as it
699 appears in a DWP file). DWP files don't really have DWO files per se -
700 comdat folding of types "loses" the DWO file they came from, and from
701 a high level view DWP files appear to contain a mass of random types.
702 However, to maintain consistency with the non-DWP case we pretend DWP
703 files contain virtual DWO files, and we assign each TU with one virtual
704 DWO file (generally based on the line and abbrev section offsets -
705 a heuristic that seems to work in practice). */
706
707 struct dwo_file
708 {
709 dwo_file () = default;
710 DISABLE_COPY_AND_ASSIGN (dwo_file);
711
712 /* The DW_AT_GNU_dwo_name attribute.
713 For virtual DWO files the name is constructed from the section offsets
714 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
715 from related CU+TUs. */
716 const char *dwo_name = nullptr;
717
718 /* The DW_AT_comp_dir attribute. */
719 const char *comp_dir = nullptr;
720
721 /* The bfd, when the file is open. Otherwise this is NULL.
722 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
723 gdb_bfd_ref_ptr dbfd;
724
725 /* The sections that make up this DWO file.
726 Remember that for virtual DWO files in DWP V2, these are virtual
727 sections (for lack of a better name). */
728 struct dwo_sections sections {};
729
730 /* The CUs in the file.
731 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
732 an extension to handle LLVM's Link Time Optimization output (where
733 multiple source files may be compiled into a single object/dwo pair). */
734 htab_t cus {};
735
736 /* Table of TUs in the file.
737 Each element is a struct dwo_unit. */
738 htab_t tus {};
739 };
740
741 /* These sections are what may appear in a DWP file. */
742
743 struct dwp_sections
744 {
745 /* These are used by both DWP version 1 and 2. */
746 struct dwarf2_section_info str;
747 struct dwarf2_section_info cu_index;
748 struct dwarf2_section_info tu_index;
749
750 /* These are only used by DWP version 2 files.
751 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
752 sections are referenced by section number, and are not recorded here.
753 In DWP version 2 there is at most one copy of all these sections, each
754 section being (effectively) comprised of the concatenation of all of the
755 individual sections that exist in the version 1 format.
756 To keep the code simple we treat each of these concatenated pieces as a
757 section itself (a virtual section?). */
758 struct dwarf2_section_info abbrev;
759 struct dwarf2_section_info info;
760 struct dwarf2_section_info line;
761 struct dwarf2_section_info loc;
762 struct dwarf2_section_info macinfo;
763 struct dwarf2_section_info macro;
764 struct dwarf2_section_info str_offsets;
765 struct dwarf2_section_info types;
766 };
767
768 /* These sections are what may appear in a virtual DWO file in DWP version 1.
769 A virtual DWO file is a DWO file as it appears in a DWP file. */
770
771 struct virtual_v1_dwo_sections
772 {
773 struct dwarf2_section_info abbrev;
774 struct dwarf2_section_info line;
775 struct dwarf2_section_info loc;
776 struct dwarf2_section_info macinfo;
777 struct dwarf2_section_info macro;
778 struct dwarf2_section_info str_offsets;
779 /* Each DWP hash table entry records one CU or one TU.
780 That is recorded here, and copied to dwo_unit.section. */
781 struct dwarf2_section_info info_or_types;
782 };
783
784 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
785 In version 2, the sections of the DWO files are concatenated together
786 and stored in one section of that name. Thus each ELF section contains
787 several "virtual" sections. */
788
789 struct virtual_v2_dwo_sections
790 {
791 bfd_size_type abbrev_offset;
792 bfd_size_type abbrev_size;
793
794 bfd_size_type line_offset;
795 bfd_size_type line_size;
796
797 bfd_size_type loc_offset;
798 bfd_size_type loc_size;
799
800 bfd_size_type macinfo_offset;
801 bfd_size_type macinfo_size;
802
803 bfd_size_type macro_offset;
804 bfd_size_type macro_size;
805
806 bfd_size_type str_offsets_offset;
807 bfd_size_type str_offsets_size;
808
809 /* Each DWP hash table entry records one CU or one TU.
810 That is recorded here, and copied to dwo_unit.section. */
811 bfd_size_type info_or_types_offset;
812 bfd_size_type info_or_types_size;
813 };
814
815 /* Contents of DWP hash tables. */
816
817 struct dwp_hash_table
818 {
819 uint32_t version, nr_columns;
820 uint32_t nr_units, nr_slots;
821 const gdb_byte *hash_table, *unit_table;
822 union
823 {
824 struct
825 {
826 const gdb_byte *indices;
827 } v1;
828 struct
829 {
830 /* This is indexed by column number and gives the id of the section
831 in that column. */
832 #define MAX_NR_V2_DWO_SECTIONS \
833 (1 /* .debug_info or .debug_types */ \
834 + 1 /* .debug_abbrev */ \
835 + 1 /* .debug_line */ \
836 + 1 /* .debug_loc */ \
837 + 1 /* .debug_str_offsets */ \
838 + 1 /* .debug_macro or .debug_macinfo */)
839 int section_ids[MAX_NR_V2_DWO_SECTIONS];
840 const gdb_byte *offsets;
841 const gdb_byte *sizes;
842 } v2;
843 } section_pool;
844 };
845
846 /* Data for one DWP file. */
847
848 struct dwp_file
849 {
850 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
851 : name (name_),
852 dbfd (std::move (abfd))
853 {
854 }
855
856 /* Name of the file. */
857 const char *name;
858
859 /* File format version. */
860 int version = 0;
861
862 /* The bfd. */
863 gdb_bfd_ref_ptr dbfd;
864
865 /* Section info for this file. */
866 struct dwp_sections sections {};
867
868 /* Table of CUs in the file. */
869 const struct dwp_hash_table *cus = nullptr;
870
871 /* Table of TUs in the file. */
872 const struct dwp_hash_table *tus = nullptr;
873
874 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
875 htab_t loaded_cus {};
876 htab_t loaded_tus {};
877
878 /* Table to map ELF section numbers to their sections.
879 This is only needed for the DWP V1 file format. */
880 unsigned int num_sections = 0;
881 asection **elf_sections = nullptr;
882 };
883
884 /* Struct used to pass misc. parameters to read_die_and_children, et
885 al. which are used for both .debug_info and .debug_types dies.
886 All parameters here are unchanging for the life of the call. This
887 struct exists to abstract away the constant parameters of die reading. */
888
889 struct die_reader_specs
890 {
891 /* The bfd of die_section. */
892 bfd* abfd;
893
894 /* The CU of the DIE we are parsing. */
895 struct dwarf2_cu *cu;
896
897 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
898 struct dwo_file *dwo_file;
899
900 /* The section the die comes from.
901 This is either .debug_info or .debug_types, or the .dwo variants. */
902 struct dwarf2_section_info *die_section;
903
904 /* die_section->buffer. */
905 const gdb_byte *buffer;
906
907 /* The end of the buffer. */
908 const gdb_byte *buffer_end;
909
910 /* The value of the DW_AT_comp_dir attribute. */
911 const char *comp_dir;
912
913 /* The abbreviation table to use when reading the DIEs. */
914 struct abbrev_table *abbrev_table;
915 };
916
917 /* Type of function passed to init_cutu_and_read_dies, et.al. */
918 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
919 const gdb_byte *info_ptr,
920 struct die_info *comp_unit_die,
921 int has_children,
922 void *data);
923
924 /* A 1-based directory index. This is a strong typedef to prevent
925 accidentally using a directory index as a 0-based index into an
926 array/vector. */
927 enum class dir_index : unsigned int {};
928
929 /* Likewise, a 1-based file name index. */
930 enum class file_name_index : unsigned int {};
931
932 struct file_entry
933 {
934 file_entry () = default;
935
936 file_entry (const char *name_, dir_index d_index_,
937 unsigned int mod_time_, unsigned int length_)
938 : name (name_),
939 d_index (d_index_),
940 mod_time (mod_time_),
941 length (length_)
942 {}
943
944 /* Return the include directory at D_INDEX stored in LH. Returns
945 NULL if D_INDEX is out of bounds. */
946 const char *include_dir (const line_header *lh) const;
947
948 /* The file name. Note this is an observing pointer. The memory is
949 owned by debug_line_buffer. */
950 const char *name {};
951
952 /* The directory index (1-based). */
953 dir_index d_index {};
954
955 unsigned int mod_time {};
956
957 unsigned int length {};
958
959 /* True if referenced by the Line Number Program. */
960 bool included_p {};
961
962 /* The associated symbol table, if any. */
963 struct symtab *symtab {};
964 };
965
966 /* The line number information for a compilation unit (found in the
967 .debug_line section) begins with a "statement program header",
968 which contains the following information. */
969 struct line_header
970 {
971 line_header ()
972 : offset_in_dwz {}
973 {}
974
975 /* Add an entry to the include directory table. */
976 void add_include_dir (const char *include_dir);
977
978 /* Add an entry to the file name table. */
979 void add_file_name (const char *name, dir_index d_index,
980 unsigned int mod_time, unsigned int length);
981
982 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
983 is out of bounds. */
984 const char *include_dir_at (dir_index index) const
985 {
986 /* Convert directory index number (1-based) to vector index
987 (0-based). */
988 size_t vec_index = to_underlying (index) - 1;
989
990 if (vec_index >= include_dirs.size ())
991 return NULL;
992 return include_dirs[vec_index];
993 }
994
995 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
996 is out of bounds. */
997 file_entry *file_name_at (file_name_index index)
998 {
999 /* Convert file name index number (1-based) to vector index
1000 (0-based). */
1001 size_t vec_index = to_underlying (index) - 1;
1002
1003 if (vec_index >= file_names.size ())
1004 return NULL;
1005 return &file_names[vec_index];
1006 }
1007
1008 /* Offset of line number information in .debug_line section. */
1009 sect_offset sect_off {};
1010
1011 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1012 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1013
1014 unsigned int total_length {};
1015 unsigned short version {};
1016 unsigned int header_length {};
1017 unsigned char minimum_instruction_length {};
1018 unsigned char maximum_ops_per_instruction {};
1019 unsigned char default_is_stmt {};
1020 int line_base {};
1021 unsigned char line_range {};
1022 unsigned char opcode_base {};
1023
1024 /* standard_opcode_lengths[i] is the number of operands for the
1025 standard opcode whose value is i. This means that
1026 standard_opcode_lengths[0] is unused, and the last meaningful
1027 element is standard_opcode_lengths[opcode_base - 1]. */
1028 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1029
1030 /* The include_directories table. Note these are observing
1031 pointers. The memory is owned by debug_line_buffer. */
1032 std::vector<const char *> include_dirs;
1033
1034 /* The file_names table. */
1035 std::vector<file_entry> file_names;
1036
1037 /* The start and end of the statement program following this
1038 header. These point into dwarf2_per_objfile->line_buffer. */
1039 const gdb_byte *statement_program_start {}, *statement_program_end {};
1040 };
1041
1042 typedef std::unique_ptr<line_header> line_header_up;
1043
1044 const char *
1045 file_entry::include_dir (const line_header *lh) const
1046 {
1047 return lh->include_dir_at (d_index);
1048 }
1049
1050 /* When we construct a partial symbol table entry we only
1051 need this much information. */
1052 struct partial_die_info : public allocate_on_obstack
1053 {
1054 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1055
1056 /* Disable assign but still keep copy ctor, which is needed
1057 load_partial_dies. */
1058 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1059
1060 /* Adjust the partial die before generating a symbol for it. This
1061 function may set the is_external flag or change the DIE's
1062 name. */
1063 void fixup (struct dwarf2_cu *cu);
1064
1065 /* Read a minimal amount of information into the minimal die
1066 structure. */
1067 const gdb_byte *read (const struct die_reader_specs *reader,
1068 const struct abbrev_info &abbrev,
1069 const gdb_byte *info_ptr);
1070
1071 /* Offset of this DIE. */
1072 const sect_offset sect_off;
1073
1074 /* DWARF-2 tag for this DIE. */
1075 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1076
1077 /* Assorted flags describing the data found in this DIE. */
1078 const unsigned int has_children : 1;
1079
1080 unsigned int is_external : 1;
1081 unsigned int is_declaration : 1;
1082 unsigned int has_type : 1;
1083 unsigned int has_specification : 1;
1084 unsigned int has_pc_info : 1;
1085 unsigned int may_be_inlined : 1;
1086
1087 /* This DIE has been marked DW_AT_main_subprogram. */
1088 unsigned int main_subprogram : 1;
1089
1090 /* Flag set if the SCOPE field of this structure has been
1091 computed. */
1092 unsigned int scope_set : 1;
1093
1094 /* Flag set if the DIE has a byte_size attribute. */
1095 unsigned int has_byte_size : 1;
1096
1097 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1098 unsigned int has_const_value : 1;
1099
1100 /* Flag set if any of the DIE's children are template arguments. */
1101 unsigned int has_template_arguments : 1;
1102
1103 /* Flag set if fixup has been called on this die. */
1104 unsigned int fixup_called : 1;
1105
1106 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1107 unsigned int is_dwz : 1;
1108
1109 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1110 unsigned int spec_is_dwz : 1;
1111
1112 /* The name of this DIE. Normally the value of DW_AT_name, but
1113 sometimes a default name for unnamed DIEs. */
1114 const char *name = nullptr;
1115
1116 /* The linkage name, if present. */
1117 const char *linkage_name = nullptr;
1118
1119 /* The scope to prepend to our children. This is generally
1120 allocated on the comp_unit_obstack, so will disappear
1121 when this compilation unit leaves the cache. */
1122 const char *scope = nullptr;
1123
1124 /* Some data associated with the partial DIE. The tag determines
1125 which field is live. */
1126 union
1127 {
1128 /* The location description associated with this DIE, if any. */
1129 struct dwarf_block *locdesc;
1130 /* The offset of an import, for DW_TAG_imported_unit. */
1131 sect_offset sect_off;
1132 } d {};
1133
1134 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1135 CORE_ADDR lowpc = 0;
1136 CORE_ADDR highpc = 0;
1137
1138 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1139 DW_AT_sibling, if any. */
1140 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1141 could return DW_AT_sibling values to its caller load_partial_dies. */
1142 const gdb_byte *sibling = nullptr;
1143
1144 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1145 DW_AT_specification (or DW_AT_abstract_origin or
1146 DW_AT_extension). */
1147 sect_offset spec_offset {};
1148
1149 /* Pointers to this DIE's parent, first child, and next sibling,
1150 if any. */
1151 struct partial_die_info *die_parent = nullptr;
1152 struct partial_die_info *die_child = nullptr;
1153 struct partial_die_info *die_sibling = nullptr;
1154
1155 friend struct partial_die_info *
1156 dwarf2_cu::find_partial_die (sect_offset sect_off);
1157
1158 private:
1159 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1160 partial_die_info (sect_offset sect_off)
1161 : partial_die_info (sect_off, DW_TAG_padding, 0)
1162 {
1163 }
1164
1165 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1166 int has_children_)
1167 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1168 {
1169 is_external = 0;
1170 is_declaration = 0;
1171 has_type = 0;
1172 has_specification = 0;
1173 has_pc_info = 0;
1174 may_be_inlined = 0;
1175 main_subprogram = 0;
1176 scope_set = 0;
1177 has_byte_size = 0;
1178 has_const_value = 0;
1179 has_template_arguments = 0;
1180 fixup_called = 0;
1181 is_dwz = 0;
1182 spec_is_dwz = 0;
1183 }
1184 };
1185
1186 /* This data structure holds the information of an abbrev. */
1187 struct abbrev_info
1188 {
1189 unsigned int number; /* number identifying abbrev */
1190 enum dwarf_tag tag; /* dwarf tag */
1191 unsigned short has_children; /* boolean */
1192 unsigned short num_attrs; /* number of attributes */
1193 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1194 struct abbrev_info *next; /* next in chain */
1195 };
1196
1197 struct attr_abbrev
1198 {
1199 ENUM_BITFIELD(dwarf_attribute) name : 16;
1200 ENUM_BITFIELD(dwarf_form) form : 16;
1201
1202 /* It is valid only if FORM is DW_FORM_implicit_const. */
1203 LONGEST implicit_const;
1204 };
1205
1206 /* Size of abbrev_table.abbrev_hash_table. */
1207 #define ABBREV_HASH_SIZE 121
1208
1209 /* Top level data structure to contain an abbreviation table. */
1210
1211 struct abbrev_table
1212 {
1213 explicit abbrev_table (sect_offset off)
1214 : sect_off (off)
1215 {
1216 m_abbrevs =
1217 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1218 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1219 }
1220
1221 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1222
1223 /* Allocate space for a struct abbrev_info object in
1224 ABBREV_TABLE. */
1225 struct abbrev_info *alloc_abbrev ();
1226
1227 /* Add an abbreviation to the table. */
1228 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1229
1230 /* Look up an abbrev in the table.
1231 Returns NULL if the abbrev is not found. */
1232
1233 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1234
1235
1236 /* Where the abbrev table came from.
1237 This is used as a sanity check when the table is used. */
1238 const sect_offset sect_off;
1239
1240 /* Storage for the abbrev table. */
1241 auto_obstack abbrev_obstack;
1242
1243 private:
1244
1245 /* Hash table of abbrevs.
1246 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1247 It could be statically allocated, but the previous code didn't so we
1248 don't either. */
1249 struct abbrev_info **m_abbrevs;
1250 };
1251
1252 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1253
1254 /* Attributes have a name and a value. */
1255 struct attribute
1256 {
1257 ENUM_BITFIELD(dwarf_attribute) name : 16;
1258 ENUM_BITFIELD(dwarf_form) form : 15;
1259
1260 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1261 field should be in u.str (existing only for DW_STRING) but it is kept
1262 here for better struct attribute alignment. */
1263 unsigned int string_is_canonical : 1;
1264
1265 union
1266 {
1267 const char *str;
1268 struct dwarf_block *blk;
1269 ULONGEST unsnd;
1270 LONGEST snd;
1271 CORE_ADDR addr;
1272 ULONGEST signature;
1273 }
1274 u;
1275 };
1276
1277 /* This data structure holds a complete die structure. */
1278 struct die_info
1279 {
1280 /* DWARF-2 tag for this DIE. */
1281 ENUM_BITFIELD(dwarf_tag) tag : 16;
1282
1283 /* Number of attributes */
1284 unsigned char num_attrs;
1285
1286 /* True if we're presently building the full type name for the
1287 type derived from this DIE. */
1288 unsigned char building_fullname : 1;
1289
1290 /* True if this die is in process. PR 16581. */
1291 unsigned char in_process : 1;
1292
1293 /* Abbrev number */
1294 unsigned int abbrev;
1295
1296 /* Offset in .debug_info or .debug_types section. */
1297 sect_offset sect_off;
1298
1299 /* The dies in a compilation unit form an n-ary tree. PARENT
1300 points to this die's parent; CHILD points to the first child of
1301 this node; and all the children of a given node are chained
1302 together via their SIBLING fields. */
1303 struct die_info *child; /* Its first child, if any. */
1304 struct die_info *sibling; /* Its next sibling, if any. */
1305 struct die_info *parent; /* Its parent, if any. */
1306
1307 /* An array of attributes, with NUM_ATTRS elements. There may be
1308 zero, but it's not common and zero-sized arrays are not
1309 sufficiently portable C. */
1310 struct attribute attrs[1];
1311 };
1312
1313 /* Get at parts of an attribute structure. */
1314
1315 #define DW_STRING(attr) ((attr)->u.str)
1316 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1317 #define DW_UNSND(attr) ((attr)->u.unsnd)
1318 #define DW_BLOCK(attr) ((attr)->u.blk)
1319 #define DW_SND(attr) ((attr)->u.snd)
1320 #define DW_ADDR(attr) ((attr)->u.addr)
1321 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1322
1323 /* Blocks are a bunch of untyped bytes. */
1324 struct dwarf_block
1325 {
1326 size_t size;
1327
1328 /* Valid only if SIZE is not zero. */
1329 const gdb_byte *data;
1330 };
1331
1332 #ifndef ATTR_ALLOC_CHUNK
1333 #define ATTR_ALLOC_CHUNK 4
1334 #endif
1335
1336 /* Allocate fields for structs, unions and enums in this size. */
1337 #ifndef DW_FIELD_ALLOC_CHUNK
1338 #define DW_FIELD_ALLOC_CHUNK 4
1339 #endif
1340
1341 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1342 but this would require a corresponding change in unpack_field_as_long
1343 and friends. */
1344 static int bits_per_byte = 8;
1345
1346 /* When reading a variant or variant part, we track a bit more
1347 information about the field, and store it in an object of this
1348 type. */
1349
1350 struct variant_field
1351 {
1352 /* If we see a DW_TAG_variant, then this will be the discriminant
1353 value. */
1354 ULONGEST discriminant_value;
1355 /* If we see a DW_TAG_variant, then this will be set if this is the
1356 default branch. */
1357 bool default_branch;
1358 /* While reading a DW_TAG_variant_part, this will be set if this
1359 field is the discriminant. */
1360 bool is_discriminant;
1361 };
1362
1363 struct nextfield
1364 {
1365 int accessibility = 0;
1366 int virtuality = 0;
1367 /* Extra information to describe a variant or variant part. */
1368 struct variant_field variant {};
1369 struct field field {};
1370 };
1371
1372 struct fnfieldlist
1373 {
1374 const char *name = nullptr;
1375 std::vector<struct fn_field> fnfields;
1376 };
1377
1378 /* The routines that read and process dies for a C struct or C++ class
1379 pass lists of data member fields and lists of member function fields
1380 in an instance of a field_info structure, as defined below. */
1381 struct field_info
1382 {
1383 /* List of data member and baseclasses fields. */
1384 std::vector<struct nextfield> fields;
1385 std::vector<struct nextfield> baseclasses;
1386
1387 /* Number of fields (including baseclasses). */
1388 int nfields = 0;
1389
1390 /* Set if the accesibility of one of the fields is not public. */
1391 int non_public_fields = 0;
1392
1393 /* Member function fieldlist array, contains name of possibly overloaded
1394 member function, number of overloaded member functions and a pointer
1395 to the head of the member function field chain. */
1396 std::vector<struct fnfieldlist> fnfieldlists;
1397
1398 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1399 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1400 std::vector<struct decl_field> typedef_field_list;
1401
1402 /* Nested types defined by this class and the number of elements in this
1403 list. */
1404 std::vector<struct decl_field> nested_types_list;
1405 };
1406
1407 /* One item on the queue of compilation units to read in full symbols
1408 for. */
1409 struct dwarf2_queue_item
1410 {
1411 struct dwarf2_per_cu_data *per_cu;
1412 enum language pretend_language;
1413 struct dwarf2_queue_item *next;
1414 };
1415
1416 /* The current queue. */
1417 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1418
1419 /* Loaded secondary compilation units are kept in memory until they
1420 have not been referenced for the processing of this many
1421 compilation units. Set this to zero to disable caching. Cache
1422 sizes of up to at least twenty will improve startup time for
1423 typical inter-CU-reference binaries, at an obvious memory cost. */
1424 static int dwarf_max_cache_age = 5;
1425 static void
1426 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1427 struct cmd_list_element *c, const char *value)
1428 {
1429 fprintf_filtered (file, _("The upper bound on the age of cached "
1430 "DWARF compilation units is %s.\n"),
1431 value);
1432 }
1433 \f
1434 /* local function prototypes */
1435
1436 static const char *get_section_name (const struct dwarf2_section_info *);
1437
1438 static const char *get_section_file_name (const struct dwarf2_section_info *);
1439
1440 static void dwarf2_find_base_address (struct die_info *die,
1441 struct dwarf2_cu *cu);
1442
1443 static struct partial_symtab *create_partial_symtab
1444 (struct dwarf2_per_cu_data *per_cu, const char *name);
1445
1446 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1447 const gdb_byte *info_ptr,
1448 struct die_info *type_unit_die,
1449 int has_children, void *data);
1450
1451 static void dwarf2_build_psymtabs_hard
1452 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1453
1454 static void scan_partial_symbols (struct partial_die_info *,
1455 CORE_ADDR *, CORE_ADDR *,
1456 int, struct dwarf2_cu *);
1457
1458 static void add_partial_symbol (struct partial_die_info *,
1459 struct dwarf2_cu *);
1460
1461 static void add_partial_namespace (struct partial_die_info *pdi,
1462 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1463 int set_addrmap, struct dwarf2_cu *cu);
1464
1465 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1466 CORE_ADDR *highpc, int set_addrmap,
1467 struct dwarf2_cu *cu);
1468
1469 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1470 struct dwarf2_cu *cu);
1471
1472 static void add_partial_subprogram (struct partial_die_info *pdi,
1473 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1474 int need_pc, struct dwarf2_cu *cu);
1475
1476 static void dwarf2_read_symtab (struct partial_symtab *,
1477 struct objfile *);
1478
1479 static void psymtab_to_symtab_1 (struct partial_symtab *);
1480
1481 static abbrev_table_up abbrev_table_read_table
1482 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1483 sect_offset);
1484
1485 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1486
1487 static struct partial_die_info *load_partial_dies
1488 (const struct die_reader_specs *, const gdb_byte *, int);
1489
1490 /* A pair of partial_die_info and compilation unit. */
1491 struct cu_partial_die_info
1492 {
1493 /* The compilation unit of the partial_die_info. */
1494 struct dwarf2_cu *cu;
1495 /* A partial_die_info. */
1496 struct partial_die_info *pdi;
1497
1498 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1499 : cu (cu),
1500 pdi (pdi)
1501 { /* Nothhing. */ }
1502
1503 private:
1504 cu_partial_die_info () = delete;
1505 };
1506
1507 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1508 struct dwarf2_cu *);
1509
1510 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1511 struct attribute *, struct attr_abbrev *,
1512 const gdb_byte *);
1513
1514 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1515
1516 static int read_1_signed_byte (bfd *, const gdb_byte *);
1517
1518 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1519
1520 /* Read the next three bytes (little-endian order) as an unsigned integer. */
1521 static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1522
1523 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1524
1525 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1526
1527 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1528 unsigned int *);
1529
1530 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1531
1532 static LONGEST read_checked_initial_length_and_offset
1533 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1534 unsigned int *, unsigned int *);
1535
1536 static LONGEST read_offset (bfd *, const gdb_byte *,
1537 const struct comp_unit_head *,
1538 unsigned int *);
1539
1540 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1541
1542 static sect_offset read_abbrev_offset
1543 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1544 struct dwarf2_section_info *, sect_offset);
1545
1546 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1547
1548 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1549
1550 static const char *read_indirect_string
1551 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1552 const struct comp_unit_head *, unsigned int *);
1553
1554 static const char *read_indirect_line_string
1555 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1556 const struct comp_unit_head *, unsigned int *);
1557
1558 static const char *read_indirect_string_at_offset
1559 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1560 LONGEST str_offset);
1561
1562 static const char *read_indirect_string_from_dwz
1563 (struct objfile *objfile, struct dwz_file *, LONGEST);
1564
1565 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1566
1567 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1568 const gdb_byte *,
1569 unsigned int *);
1570
1571 static const char *read_str_index (const struct die_reader_specs *reader,
1572 ULONGEST str_index);
1573
1574 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1575
1576 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1577 struct dwarf2_cu *);
1578
1579 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1580 unsigned int);
1581
1582 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1583 struct dwarf2_cu *cu);
1584
1585 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1586
1587 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1588 struct dwarf2_cu *cu);
1589
1590 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1591
1592 static struct die_info *die_specification (struct die_info *die,
1593 struct dwarf2_cu **);
1594
1595 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1596 struct dwarf2_cu *cu);
1597
1598 static void dwarf_decode_lines (struct line_header *, const char *,
1599 struct dwarf2_cu *, struct partial_symtab *,
1600 CORE_ADDR, int decode_mapping);
1601
1602 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1603 const char *);
1604
1605 static struct symbol *new_symbol (struct die_info *, struct type *,
1606 struct dwarf2_cu *, struct symbol * = NULL);
1607
1608 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1609 struct dwarf2_cu *);
1610
1611 static void dwarf2_const_value_attr (const struct attribute *attr,
1612 struct type *type,
1613 const char *name,
1614 struct obstack *obstack,
1615 struct dwarf2_cu *cu, LONGEST *value,
1616 const gdb_byte **bytes,
1617 struct dwarf2_locexpr_baton **baton);
1618
1619 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1620
1621 static int need_gnat_info (struct dwarf2_cu *);
1622
1623 static struct type *die_descriptive_type (struct die_info *,
1624 struct dwarf2_cu *);
1625
1626 static void set_descriptive_type (struct type *, struct die_info *,
1627 struct dwarf2_cu *);
1628
1629 static struct type *die_containing_type (struct die_info *,
1630 struct dwarf2_cu *);
1631
1632 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1633 struct dwarf2_cu *);
1634
1635 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1636
1637 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1638
1639 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1640
1641 static char *typename_concat (struct obstack *obs, const char *prefix,
1642 const char *suffix, int physname,
1643 struct dwarf2_cu *cu);
1644
1645 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1646
1647 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1648
1649 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1650
1651 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1652
1653 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1654
1655 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1656
1657 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1658 struct dwarf2_cu *, struct partial_symtab *);
1659
1660 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1661 values. Keep the items ordered with increasing constraints compliance. */
1662 enum pc_bounds_kind
1663 {
1664 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1665 PC_BOUNDS_NOT_PRESENT,
1666
1667 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1668 were present but they do not form a valid range of PC addresses. */
1669 PC_BOUNDS_INVALID,
1670
1671 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1672 PC_BOUNDS_RANGES,
1673
1674 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1675 PC_BOUNDS_HIGH_LOW,
1676 };
1677
1678 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1679 CORE_ADDR *, CORE_ADDR *,
1680 struct dwarf2_cu *,
1681 struct partial_symtab *);
1682
1683 static void get_scope_pc_bounds (struct die_info *,
1684 CORE_ADDR *, CORE_ADDR *,
1685 struct dwarf2_cu *);
1686
1687 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1688 CORE_ADDR, struct dwarf2_cu *);
1689
1690 static void dwarf2_add_field (struct field_info *, struct die_info *,
1691 struct dwarf2_cu *);
1692
1693 static void dwarf2_attach_fields_to_type (struct field_info *,
1694 struct type *, struct dwarf2_cu *);
1695
1696 static void dwarf2_add_member_fn (struct field_info *,
1697 struct die_info *, struct type *,
1698 struct dwarf2_cu *);
1699
1700 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1701 struct type *,
1702 struct dwarf2_cu *);
1703
1704 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1705
1706 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1707
1708 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1709
1710 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1711
1712 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1713
1714 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1715
1716 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1717
1718 static struct type *read_module_type (struct die_info *die,
1719 struct dwarf2_cu *cu);
1720
1721 static const char *namespace_name (struct die_info *die,
1722 int *is_anonymous, struct dwarf2_cu *);
1723
1724 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1725
1726 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1727
1728 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1729 struct dwarf2_cu *);
1730
1731 static struct die_info *read_die_and_siblings_1
1732 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1733 struct die_info *);
1734
1735 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1736 const gdb_byte *info_ptr,
1737 const gdb_byte **new_info_ptr,
1738 struct die_info *parent);
1739
1740 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1741 struct die_info **, const gdb_byte *,
1742 int *, int);
1743
1744 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1745 struct die_info **, const gdb_byte *,
1746 int *);
1747
1748 static void process_die (struct die_info *, struct dwarf2_cu *);
1749
1750 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1751 struct obstack *);
1752
1753 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1754
1755 static const char *dwarf2_full_name (const char *name,
1756 struct die_info *die,
1757 struct dwarf2_cu *cu);
1758
1759 static const char *dwarf2_physname (const char *name, struct die_info *die,
1760 struct dwarf2_cu *cu);
1761
1762 static struct die_info *dwarf2_extension (struct die_info *die,
1763 struct dwarf2_cu **);
1764
1765 static const char *dwarf_tag_name (unsigned int);
1766
1767 static const char *dwarf_attr_name (unsigned int);
1768
1769 static const char *dwarf_unit_type_name (int unit_type);
1770
1771 static const char *dwarf_form_name (unsigned int);
1772
1773 static const char *dwarf_bool_name (unsigned int);
1774
1775 static const char *dwarf_type_encoding_name (unsigned int);
1776
1777 static struct die_info *sibling_die (struct die_info *);
1778
1779 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1780
1781 static void dump_die_for_error (struct die_info *);
1782
1783 static void dump_die_1 (struct ui_file *, int level, int max_level,
1784 struct die_info *);
1785
1786 /*static*/ void dump_die (struct die_info *, int max_level);
1787
1788 static void store_in_ref_table (struct die_info *,
1789 struct dwarf2_cu *);
1790
1791 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1792
1793 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1794
1795 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1796 const struct attribute *,
1797 struct dwarf2_cu **);
1798
1799 static struct die_info *follow_die_ref (struct die_info *,
1800 const struct attribute *,
1801 struct dwarf2_cu **);
1802
1803 static struct die_info *follow_die_sig (struct die_info *,
1804 const struct attribute *,
1805 struct dwarf2_cu **);
1806
1807 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1808 struct dwarf2_cu *);
1809
1810 static struct type *get_DW_AT_signature_type (struct die_info *,
1811 const struct attribute *,
1812 struct dwarf2_cu *);
1813
1814 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1815
1816 static void read_signatured_type (struct signatured_type *);
1817
1818 static int attr_to_dynamic_prop (const struct attribute *attr,
1819 struct die_info *die, struct dwarf2_cu *cu,
1820 struct dynamic_prop *prop, struct type *type);
1821
1822 /* memory allocation interface */
1823
1824 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1825
1826 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1827
1828 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1829
1830 static int attr_form_is_block (const struct attribute *);
1831
1832 static int attr_form_is_section_offset (const struct attribute *);
1833
1834 static int attr_form_is_constant (const struct attribute *);
1835
1836 static int attr_form_is_ref (const struct attribute *);
1837
1838 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1839 struct dwarf2_loclist_baton *baton,
1840 const struct attribute *attr);
1841
1842 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1843 struct symbol *sym,
1844 struct dwarf2_cu *cu,
1845 int is_block);
1846
1847 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1848 const gdb_byte *info_ptr,
1849 struct abbrev_info *abbrev);
1850
1851 static hashval_t partial_die_hash (const void *item);
1852
1853 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1854
1855 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1856 (sect_offset sect_off, unsigned int offset_in_dwz,
1857 struct dwarf2_per_objfile *dwarf2_per_objfile);
1858
1859 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1860 struct die_info *comp_unit_die,
1861 enum language pretend_language);
1862
1863 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1864
1865 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1866
1867 static struct type *set_die_type (struct die_info *, struct type *,
1868 struct dwarf2_cu *);
1869
1870 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1871
1872 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1873
1874 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1875 enum language);
1876
1877 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1878 enum language);
1879
1880 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1881 enum language);
1882
1883 static void dwarf2_add_dependence (struct dwarf2_cu *,
1884 struct dwarf2_per_cu_data *);
1885
1886 static void dwarf2_mark (struct dwarf2_cu *);
1887
1888 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1889
1890 static struct type *get_die_type_at_offset (sect_offset,
1891 struct dwarf2_per_cu_data *);
1892
1893 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1894
1895 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1896 enum language pretend_language);
1897
1898 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1899
1900 static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1901 static struct type *dwarf2_per_cu_addr_sized_int_type
1902 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1903
1904 /* Class, the destructor of which frees all allocated queue entries. This
1905 will only have work to do if an error was thrown while processing the
1906 dwarf. If no error was thrown then the queue entries should have all
1907 been processed, and freed, as we went along. */
1908
1909 class dwarf2_queue_guard
1910 {
1911 public:
1912 dwarf2_queue_guard () = default;
1913
1914 /* Free any entries remaining on the queue. There should only be
1915 entries left if we hit an error while processing the dwarf. */
1916 ~dwarf2_queue_guard ()
1917 {
1918 struct dwarf2_queue_item *item, *last;
1919
1920 item = dwarf2_queue;
1921 while (item)
1922 {
1923 /* Anything still marked queued is likely to be in an
1924 inconsistent state, so discard it. */
1925 if (item->per_cu->queued)
1926 {
1927 if (item->per_cu->cu != NULL)
1928 free_one_cached_comp_unit (item->per_cu);
1929 item->per_cu->queued = 0;
1930 }
1931
1932 last = item;
1933 item = item->next;
1934 xfree (last);
1935 }
1936
1937 dwarf2_queue = dwarf2_queue_tail = NULL;
1938 }
1939 };
1940
1941 /* The return type of find_file_and_directory. Note, the enclosed
1942 string pointers are only valid while this object is valid. */
1943
1944 struct file_and_directory
1945 {
1946 /* The filename. This is never NULL. */
1947 const char *name;
1948
1949 /* The compilation directory. NULL if not known. If we needed to
1950 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1951 points directly to the DW_AT_comp_dir string attribute owned by
1952 the obstack that owns the DIE. */
1953 const char *comp_dir;
1954
1955 /* If we needed to build a new string for comp_dir, this is what
1956 owns the storage. */
1957 std::string comp_dir_storage;
1958 };
1959
1960 static file_and_directory find_file_and_directory (struct die_info *die,
1961 struct dwarf2_cu *cu);
1962
1963 static char *file_full_name (int file, struct line_header *lh,
1964 const char *comp_dir);
1965
1966 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1967 enum class rcuh_kind { COMPILE, TYPE };
1968
1969 static const gdb_byte *read_and_check_comp_unit_head
1970 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1971 struct comp_unit_head *header,
1972 struct dwarf2_section_info *section,
1973 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1974 rcuh_kind section_kind);
1975
1976 static void init_cutu_and_read_dies
1977 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1978 int use_existing_cu, int keep, bool skip_partial,
1979 die_reader_func_ftype *die_reader_func, void *data);
1980
1981 static void init_cutu_and_read_dies_simple
1982 (struct dwarf2_per_cu_data *this_cu,
1983 die_reader_func_ftype *die_reader_func, void *data);
1984
1985 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1986
1987 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1988
1989 static struct dwo_unit *lookup_dwo_unit_in_dwp
1990 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1991 struct dwp_file *dwp_file, const char *comp_dir,
1992 ULONGEST signature, int is_debug_types);
1993
1994 static struct dwp_file *get_dwp_file
1995 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1996
1997 static struct dwo_unit *lookup_dwo_comp_unit
1998 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1999
2000 static struct dwo_unit *lookup_dwo_type_unit
2001 (struct signatured_type *, const char *, const char *);
2002
2003 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2004
2005 /* A unique pointer to a dwo_file. */
2006
2007 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
2008
2009 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2010
2011 static void check_producer (struct dwarf2_cu *cu);
2012
2013 static void free_line_header_voidp (void *arg);
2014 \f
2015 /* Various complaints about symbol reading that don't abort the process. */
2016
2017 static void
2018 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2019 {
2020 complaint (_("statement list doesn't fit in .debug_line section"));
2021 }
2022
2023 static void
2024 dwarf2_debug_line_missing_file_complaint (void)
2025 {
2026 complaint (_(".debug_line section has line data without a file"));
2027 }
2028
2029 static void
2030 dwarf2_debug_line_missing_end_sequence_complaint (void)
2031 {
2032 complaint (_(".debug_line section has line "
2033 "program sequence without an end"));
2034 }
2035
2036 static void
2037 dwarf2_complex_location_expr_complaint (void)
2038 {
2039 complaint (_("location expression too complex"));
2040 }
2041
2042 static void
2043 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2044 int arg3)
2045 {
2046 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
2047 arg1, arg2, arg3);
2048 }
2049
2050 static void
2051 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2052 {
2053 complaint (_("debug info runs off end of %s section"
2054 " [in module %s]"),
2055 get_section_name (section),
2056 get_section_file_name (section));
2057 }
2058
2059 static void
2060 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2061 {
2062 complaint (_("macro debug info contains a "
2063 "malformed macro definition:\n`%s'"),
2064 arg1);
2065 }
2066
2067 static void
2068 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2069 {
2070 complaint (_("invalid attribute class or form for '%s' in '%s'"),
2071 arg1, arg2);
2072 }
2073
2074 /* Hash function for line_header_hash. */
2075
2076 static hashval_t
2077 line_header_hash (const struct line_header *ofs)
2078 {
2079 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2080 }
2081
2082 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2083
2084 static hashval_t
2085 line_header_hash_voidp (const void *item)
2086 {
2087 const struct line_header *ofs = (const struct line_header *) item;
2088
2089 return line_header_hash (ofs);
2090 }
2091
2092 /* Equality function for line_header_hash. */
2093
2094 static int
2095 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2096 {
2097 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2098 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2099
2100 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2101 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2102 }
2103
2104 \f
2105
2106 /* Read the given attribute value as an address, taking the attribute's
2107 form into account. */
2108
2109 static CORE_ADDR
2110 attr_value_as_address (struct attribute *attr)
2111 {
2112 CORE_ADDR addr;
2113
2114 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2115 && attr->form != DW_FORM_GNU_addr_index)
2116 {
2117 /* Aside from a few clearly defined exceptions, attributes that
2118 contain an address must always be in DW_FORM_addr form.
2119 Unfortunately, some compilers happen to be violating this
2120 requirement by encoding addresses using other forms, such
2121 as DW_FORM_data4 for example. For those broken compilers,
2122 we try to do our best, without any guarantee of success,
2123 to interpret the address correctly. It would also be nice
2124 to generate a complaint, but that would require us to maintain
2125 a list of legitimate cases where a non-address form is allowed,
2126 as well as update callers to pass in at least the CU's DWARF
2127 version. This is more overhead than what we're willing to
2128 expand for a pretty rare case. */
2129 addr = DW_UNSND (attr);
2130 }
2131 else
2132 addr = DW_ADDR (attr);
2133
2134 return addr;
2135 }
2136
2137 /* See declaration. */
2138
2139 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2140 const dwarf2_debug_sections *names)
2141 : objfile (objfile_)
2142 {
2143 if (names == NULL)
2144 names = &dwarf2_elf_names;
2145
2146 bfd *obfd = objfile->obfd;
2147
2148 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2149 locate_sections (obfd, sec, *names);
2150 }
2151
2152 dwarf2_per_objfile::~dwarf2_per_objfile ()
2153 {
2154 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2155 free_cached_comp_units ();
2156
2157 if (quick_file_names_table)
2158 htab_delete (quick_file_names_table);
2159
2160 if (line_header_hash)
2161 htab_delete (line_header_hash);
2162
2163 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2164 VEC_free (dwarf2_per_cu_ptr, per_cu->imported_symtabs);
2165
2166 for (signatured_type *sig_type : all_type_units)
2167 VEC_free (dwarf2_per_cu_ptr, sig_type->per_cu.imported_symtabs);
2168
2169 /* Everything else should be on the objfile obstack. */
2170 }
2171
2172 /* See declaration. */
2173
2174 void
2175 dwarf2_per_objfile::free_cached_comp_units ()
2176 {
2177 dwarf2_per_cu_data *per_cu = read_in_chain;
2178 dwarf2_per_cu_data **last_chain = &read_in_chain;
2179 while (per_cu != NULL)
2180 {
2181 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2182
2183 delete per_cu->cu;
2184 *last_chain = next_cu;
2185 per_cu = next_cu;
2186 }
2187 }
2188
2189 /* A helper class that calls free_cached_comp_units on
2190 destruction. */
2191
2192 class free_cached_comp_units
2193 {
2194 public:
2195
2196 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2197 : m_per_objfile (per_objfile)
2198 {
2199 }
2200
2201 ~free_cached_comp_units ()
2202 {
2203 m_per_objfile->free_cached_comp_units ();
2204 }
2205
2206 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2207
2208 private:
2209
2210 dwarf2_per_objfile *m_per_objfile;
2211 };
2212
2213 /* Try to locate the sections we need for DWARF 2 debugging
2214 information and return true if we have enough to do something.
2215 NAMES points to the dwarf2 section names, or is NULL if the standard
2216 ELF names are used. */
2217
2218 int
2219 dwarf2_has_info (struct objfile *objfile,
2220 const struct dwarf2_debug_sections *names)
2221 {
2222 if (objfile->flags & OBJF_READNEVER)
2223 return 0;
2224
2225 struct dwarf2_per_objfile *dwarf2_per_objfile
2226 = get_dwarf2_per_objfile (objfile);
2227
2228 if (dwarf2_per_objfile == NULL)
2229 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2230 names);
2231
2232 return (!dwarf2_per_objfile->info.is_virtual
2233 && dwarf2_per_objfile->info.s.section != NULL
2234 && !dwarf2_per_objfile->abbrev.is_virtual
2235 && dwarf2_per_objfile->abbrev.s.section != NULL);
2236 }
2237
2238 /* Return the containing section of virtual section SECTION. */
2239
2240 static struct dwarf2_section_info *
2241 get_containing_section (const struct dwarf2_section_info *section)
2242 {
2243 gdb_assert (section->is_virtual);
2244 return section->s.containing_section;
2245 }
2246
2247 /* Return the bfd owner of SECTION. */
2248
2249 static struct bfd *
2250 get_section_bfd_owner (const struct dwarf2_section_info *section)
2251 {
2252 if (section->is_virtual)
2253 {
2254 section = get_containing_section (section);
2255 gdb_assert (!section->is_virtual);
2256 }
2257 return section->s.section->owner;
2258 }
2259
2260 /* Return the bfd section of SECTION.
2261 Returns NULL if the section is not present. */
2262
2263 static asection *
2264 get_section_bfd_section (const struct dwarf2_section_info *section)
2265 {
2266 if (section->is_virtual)
2267 {
2268 section = get_containing_section (section);
2269 gdb_assert (!section->is_virtual);
2270 }
2271 return section->s.section;
2272 }
2273
2274 /* Return the name of SECTION. */
2275
2276 static const char *
2277 get_section_name (const struct dwarf2_section_info *section)
2278 {
2279 asection *sectp = get_section_bfd_section (section);
2280
2281 gdb_assert (sectp != NULL);
2282 return bfd_section_name (get_section_bfd_owner (section), sectp);
2283 }
2284
2285 /* Return the name of the file SECTION is in. */
2286
2287 static const char *
2288 get_section_file_name (const struct dwarf2_section_info *section)
2289 {
2290 bfd *abfd = get_section_bfd_owner (section);
2291
2292 return bfd_get_filename (abfd);
2293 }
2294
2295 /* Return the id of SECTION.
2296 Returns 0 if SECTION doesn't exist. */
2297
2298 static int
2299 get_section_id (const struct dwarf2_section_info *section)
2300 {
2301 asection *sectp = get_section_bfd_section (section);
2302
2303 if (sectp == NULL)
2304 return 0;
2305 return sectp->id;
2306 }
2307
2308 /* Return the flags of SECTION.
2309 SECTION (or containing section if this is a virtual section) must exist. */
2310
2311 static int
2312 get_section_flags (const struct dwarf2_section_info *section)
2313 {
2314 asection *sectp = get_section_bfd_section (section);
2315
2316 gdb_assert (sectp != NULL);
2317 return bfd_get_section_flags (sectp->owner, sectp);
2318 }
2319
2320 /* When loading sections, we look either for uncompressed section or for
2321 compressed section names. */
2322
2323 static int
2324 section_is_p (const char *section_name,
2325 const struct dwarf2_section_names *names)
2326 {
2327 if (names->normal != NULL
2328 && strcmp (section_name, names->normal) == 0)
2329 return 1;
2330 if (names->compressed != NULL
2331 && strcmp (section_name, names->compressed) == 0)
2332 return 1;
2333 return 0;
2334 }
2335
2336 /* See declaration. */
2337
2338 void
2339 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2340 const dwarf2_debug_sections &names)
2341 {
2342 flagword aflag = bfd_get_section_flags (abfd, sectp);
2343
2344 if ((aflag & SEC_HAS_CONTENTS) == 0)
2345 {
2346 }
2347 else if (section_is_p (sectp->name, &names.info))
2348 {
2349 this->info.s.section = sectp;
2350 this->info.size = bfd_get_section_size (sectp);
2351 }
2352 else if (section_is_p (sectp->name, &names.abbrev))
2353 {
2354 this->abbrev.s.section = sectp;
2355 this->abbrev.size = bfd_get_section_size (sectp);
2356 }
2357 else if (section_is_p (sectp->name, &names.line))
2358 {
2359 this->line.s.section = sectp;
2360 this->line.size = bfd_get_section_size (sectp);
2361 }
2362 else if (section_is_p (sectp->name, &names.loc))
2363 {
2364 this->loc.s.section = sectp;
2365 this->loc.size = bfd_get_section_size (sectp);
2366 }
2367 else if (section_is_p (sectp->name, &names.loclists))
2368 {
2369 this->loclists.s.section = sectp;
2370 this->loclists.size = bfd_get_section_size (sectp);
2371 }
2372 else if (section_is_p (sectp->name, &names.macinfo))
2373 {
2374 this->macinfo.s.section = sectp;
2375 this->macinfo.size = bfd_get_section_size (sectp);
2376 }
2377 else if (section_is_p (sectp->name, &names.macro))
2378 {
2379 this->macro.s.section = sectp;
2380 this->macro.size = bfd_get_section_size (sectp);
2381 }
2382 else if (section_is_p (sectp->name, &names.str))
2383 {
2384 this->str.s.section = sectp;
2385 this->str.size = bfd_get_section_size (sectp);
2386 }
2387 else if (section_is_p (sectp->name, &names.line_str))
2388 {
2389 this->line_str.s.section = sectp;
2390 this->line_str.size = bfd_get_section_size (sectp);
2391 }
2392 else if (section_is_p (sectp->name, &names.addr))
2393 {
2394 this->addr.s.section = sectp;
2395 this->addr.size = bfd_get_section_size (sectp);
2396 }
2397 else if (section_is_p (sectp->name, &names.frame))
2398 {
2399 this->frame.s.section = sectp;
2400 this->frame.size = bfd_get_section_size (sectp);
2401 }
2402 else if (section_is_p (sectp->name, &names.eh_frame))
2403 {
2404 this->eh_frame.s.section = sectp;
2405 this->eh_frame.size = bfd_get_section_size (sectp);
2406 }
2407 else if (section_is_p (sectp->name, &names.ranges))
2408 {
2409 this->ranges.s.section = sectp;
2410 this->ranges.size = bfd_get_section_size (sectp);
2411 }
2412 else if (section_is_p (sectp->name, &names.rnglists))
2413 {
2414 this->rnglists.s.section = sectp;
2415 this->rnglists.size = bfd_get_section_size (sectp);
2416 }
2417 else if (section_is_p (sectp->name, &names.types))
2418 {
2419 struct dwarf2_section_info type_section;
2420
2421 memset (&type_section, 0, sizeof (type_section));
2422 type_section.s.section = sectp;
2423 type_section.size = bfd_get_section_size (sectp);
2424
2425 this->types.push_back (type_section);
2426 }
2427 else if (section_is_p (sectp->name, &names.gdb_index))
2428 {
2429 this->gdb_index.s.section = sectp;
2430 this->gdb_index.size = bfd_get_section_size (sectp);
2431 }
2432 else if (section_is_p (sectp->name, &names.debug_names))
2433 {
2434 this->debug_names.s.section = sectp;
2435 this->debug_names.size = bfd_get_section_size (sectp);
2436 }
2437 else if (section_is_p (sectp->name, &names.debug_aranges))
2438 {
2439 this->debug_aranges.s.section = sectp;
2440 this->debug_aranges.size = bfd_get_section_size (sectp);
2441 }
2442
2443 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2444 && bfd_section_vma (abfd, sectp) == 0)
2445 this->has_section_at_zero = true;
2446 }
2447
2448 /* A helper function that decides whether a section is empty,
2449 or not present. */
2450
2451 static int
2452 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2453 {
2454 if (section->is_virtual)
2455 return section->size == 0;
2456 return section->s.section == NULL || section->size == 0;
2457 }
2458
2459 /* See dwarf2read.h. */
2460
2461 void
2462 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2463 {
2464 asection *sectp;
2465 bfd *abfd;
2466 gdb_byte *buf, *retbuf;
2467
2468 if (info->readin)
2469 return;
2470 info->buffer = NULL;
2471 info->readin = true;
2472
2473 if (dwarf2_section_empty_p (info))
2474 return;
2475
2476 sectp = get_section_bfd_section (info);
2477
2478 /* If this is a virtual section we need to read in the real one first. */
2479 if (info->is_virtual)
2480 {
2481 struct dwarf2_section_info *containing_section =
2482 get_containing_section (info);
2483
2484 gdb_assert (sectp != NULL);
2485 if ((sectp->flags & SEC_RELOC) != 0)
2486 {
2487 error (_("Dwarf Error: DWP format V2 with relocations is not"
2488 " supported in section %s [in module %s]"),
2489 get_section_name (info), get_section_file_name (info));
2490 }
2491 dwarf2_read_section (objfile, containing_section);
2492 /* Other code should have already caught virtual sections that don't
2493 fit. */
2494 gdb_assert (info->virtual_offset + info->size
2495 <= containing_section->size);
2496 /* If the real section is empty or there was a problem reading the
2497 section we shouldn't get here. */
2498 gdb_assert (containing_section->buffer != NULL);
2499 info->buffer = containing_section->buffer + info->virtual_offset;
2500 return;
2501 }
2502
2503 /* If the section has relocations, we must read it ourselves.
2504 Otherwise we attach it to the BFD. */
2505 if ((sectp->flags & SEC_RELOC) == 0)
2506 {
2507 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2508 return;
2509 }
2510
2511 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2512 info->buffer = buf;
2513
2514 /* When debugging .o files, we may need to apply relocations; see
2515 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2516 We never compress sections in .o files, so we only need to
2517 try this when the section is not compressed. */
2518 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2519 if (retbuf != NULL)
2520 {
2521 info->buffer = retbuf;
2522 return;
2523 }
2524
2525 abfd = get_section_bfd_owner (info);
2526 gdb_assert (abfd != NULL);
2527
2528 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2529 || bfd_bread (buf, info->size, abfd) != info->size)
2530 {
2531 error (_("Dwarf Error: Can't read DWARF data"
2532 " in section %s [in module %s]"),
2533 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2534 }
2535 }
2536
2537 /* A helper function that returns the size of a section in a safe way.
2538 If you are positive that the section has been read before using the
2539 size, then it is safe to refer to the dwarf2_section_info object's
2540 "size" field directly. In other cases, you must call this
2541 function, because for compressed sections the size field is not set
2542 correctly until the section has been read. */
2543
2544 static bfd_size_type
2545 dwarf2_section_size (struct objfile *objfile,
2546 struct dwarf2_section_info *info)
2547 {
2548 if (!info->readin)
2549 dwarf2_read_section (objfile, info);
2550 return info->size;
2551 }
2552
2553 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2554 SECTION_NAME. */
2555
2556 void
2557 dwarf2_get_section_info (struct objfile *objfile,
2558 enum dwarf2_section_enum sect,
2559 asection **sectp, const gdb_byte **bufp,
2560 bfd_size_type *sizep)
2561 {
2562 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2563 struct dwarf2_section_info *info;
2564
2565 /* We may see an objfile without any DWARF, in which case we just
2566 return nothing. */
2567 if (data == NULL)
2568 {
2569 *sectp = NULL;
2570 *bufp = NULL;
2571 *sizep = 0;
2572 return;
2573 }
2574 switch (sect)
2575 {
2576 case DWARF2_DEBUG_FRAME:
2577 info = &data->frame;
2578 break;
2579 case DWARF2_EH_FRAME:
2580 info = &data->eh_frame;
2581 break;
2582 default:
2583 gdb_assert_not_reached ("unexpected section");
2584 }
2585
2586 dwarf2_read_section (objfile, info);
2587
2588 *sectp = get_section_bfd_section (info);
2589 *bufp = info->buffer;
2590 *sizep = info->size;
2591 }
2592
2593 /* A helper function to find the sections for a .dwz file. */
2594
2595 static void
2596 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2597 {
2598 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2599
2600 /* Note that we only support the standard ELF names, because .dwz
2601 is ELF-only (at the time of writing). */
2602 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2603 {
2604 dwz_file->abbrev.s.section = sectp;
2605 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2606 }
2607 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2608 {
2609 dwz_file->info.s.section = sectp;
2610 dwz_file->info.size = bfd_get_section_size (sectp);
2611 }
2612 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2613 {
2614 dwz_file->str.s.section = sectp;
2615 dwz_file->str.size = bfd_get_section_size (sectp);
2616 }
2617 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2618 {
2619 dwz_file->line.s.section = sectp;
2620 dwz_file->line.size = bfd_get_section_size (sectp);
2621 }
2622 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2623 {
2624 dwz_file->macro.s.section = sectp;
2625 dwz_file->macro.size = bfd_get_section_size (sectp);
2626 }
2627 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2628 {
2629 dwz_file->gdb_index.s.section = sectp;
2630 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2631 }
2632 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2633 {
2634 dwz_file->debug_names.s.section = sectp;
2635 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2636 }
2637 }
2638
2639 /* See dwarf2read.h. */
2640
2641 struct dwz_file *
2642 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2643 {
2644 const char *filename;
2645 bfd_size_type buildid_len_arg;
2646 size_t buildid_len;
2647 bfd_byte *buildid;
2648
2649 if (dwarf2_per_objfile->dwz_file != NULL)
2650 return dwarf2_per_objfile->dwz_file.get ();
2651
2652 bfd_set_error (bfd_error_no_error);
2653 gdb::unique_xmalloc_ptr<char> data
2654 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2655 &buildid_len_arg, &buildid));
2656 if (data == NULL)
2657 {
2658 if (bfd_get_error () == bfd_error_no_error)
2659 return NULL;
2660 error (_("could not read '.gnu_debugaltlink' section: %s"),
2661 bfd_errmsg (bfd_get_error ()));
2662 }
2663
2664 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2665
2666 buildid_len = (size_t) buildid_len_arg;
2667
2668 filename = data.get ();
2669
2670 std::string abs_storage;
2671 if (!IS_ABSOLUTE_PATH (filename))
2672 {
2673 gdb::unique_xmalloc_ptr<char> abs
2674 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2675
2676 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2677 filename = abs_storage.c_str ();
2678 }
2679
2680 /* First try the file name given in the section. If that doesn't
2681 work, try to use the build-id instead. */
2682 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2683 if (dwz_bfd != NULL)
2684 {
2685 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2686 dwz_bfd.reset (nullptr);
2687 }
2688
2689 if (dwz_bfd == NULL)
2690 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2691
2692 if (dwz_bfd == NULL)
2693 error (_("could not find '.gnu_debugaltlink' file for %s"),
2694 objfile_name (dwarf2_per_objfile->objfile));
2695
2696 std::unique_ptr<struct dwz_file> result
2697 (new struct dwz_file (std::move (dwz_bfd)));
2698
2699 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2700 result.get ());
2701
2702 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2703 result->dwz_bfd.get ());
2704 dwarf2_per_objfile->dwz_file = std::move (result);
2705 return dwarf2_per_objfile->dwz_file.get ();
2706 }
2707 \f
2708 /* DWARF quick_symbols_functions support. */
2709
2710 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2711 unique line tables, so we maintain a separate table of all .debug_line
2712 derived entries to support the sharing.
2713 All the quick functions need is the list of file names. We discard the
2714 line_header when we're done and don't need to record it here. */
2715 struct quick_file_names
2716 {
2717 /* The data used to construct the hash key. */
2718 struct stmt_list_hash hash;
2719
2720 /* The number of entries in file_names, real_names. */
2721 unsigned int num_file_names;
2722
2723 /* The file names from the line table, after being run through
2724 file_full_name. */
2725 const char **file_names;
2726
2727 /* The file names from the line table after being run through
2728 gdb_realpath. These are computed lazily. */
2729 const char **real_names;
2730 };
2731
2732 /* When using the index (and thus not using psymtabs), each CU has an
2733 object of this type. This is used to hold information needed by
2734 the various "quick" methods. */
2735 struct dwarf2_per_cu_quick_data
2736 {
2737 /* The file table. This can be NULL if there was no file table
2738 or it's currently not read in.
2739 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2740 struct quick_file_names *file_names;
2741
2742 /* The corresponding symbol table. This is NULL if symbols for this
2743 CU have not yet been read. */
2744 struct compunit_symtab *compunit_symtab;
2745
2746 /* A temporary mark bit used when iterating over all CUs in
2747 expand_symtabs_matching. */
2748 unsigned int mark : 1;
2749
2750 /* True if we've tried to read the file table and found there isn't one.
2751 There will be no point in trying to read it again next time. */
2752 unsigned int no_file_data : 1;
2753 };
2754
2755 /* Utility hash function for a stmt_list_hash. */
2756
2757 static hashval_t
2758 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2759 {
2760 hashval_t v = 0;
2761
2762 if (stmt_list_hash->dwo_unit != NULL)
2763 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2764 v += to_underlying (stmt_list_hash->line_sect_off);
2765 return v;
2766 }
2767
2768 /* Utility equality function for a stmt_list_hash. */
2769
2770 static int
2771 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2772 const struct stmt_list_hash *rhs)
2773 {
2774 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2775 return 0;
2776 if (lhs->dwo_unit != NULL
2777 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2778 return 0;
2779
2780 return lhs->line_sect_off == rhs->line_sect_off;
2781 }
2782
2783 /* Hash function for a quick_file_names. */
2784
2785 static hashval_t
2786 hash_file_name_entry (const void *e)
2787 {
2788 const struct quick_file_names *file_data
2789 = (const struct quick_file_names *) e;
2790
2791 return hash_stmt_list_entry (&file_data->hash);
2792 }
2793
2794 /* Equality function for a quick_file_names. */
2795
2796 static int
2797 eq_file_name_entry (const void *a, const void *b)
2798 {
2799 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2800 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2801
2802 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2803 }
2804
2805 /* Delete function for a quick_file_names. */
2806
2807 static void
2808 delete_file_name_entry (void *e)
2809 {
2810 struct quick_file_names *file_data = (struct quick_file_names *) e;
2811 int i;
2812
2813 for (i = 0; i < file_data->num_file_names; ++i)
2814 {
2815 xfree ((void*) file_data->file_names[i]);
2816 if (file_data->real_names)
2817 xfree ((void*) file_data->real_names[i]);
2818 }
2819
2820 /* The space for the struct itself lives on objfile_obstack,
2821 so we don't free it here. */
2822 }
2823
2824 /* Create a quick_file_names hash table. */
2825
2826 static htab_t
2827 create_quick_file_names_table (unsigned int nr_initial_entries)
2828 {
2829 return htab_create_alloc (nr_initial_entries,
2830 hash_file_name_entry, eq_file_name_entry,
2831 delete_file_name_entry, xcalloc, xfree);
2832 }
2833
2834 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2835 have to be created afterwards. You should call age_cached_comp_units after
2836 processing PER_CU->CU. dw2_setup must have been already called. */
2837
2838 static void
2839 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2840 {
2841 if (per_cu->is_debug_types)
2842 load_full_type_unit (per_cu);
2843 else
2844 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2845
2846 if (per_cu->cu == NULL)
2847 return; /* Dummy CU. */
2848
2849 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2850 }
2851
2852 /* Read in the symbols for PER_CU. */
2853
2854 static void
2855 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2856 {
2857 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2858
2859 /* Skip type_unit_groups, reading the type units they contain
2860 is handled elsewhere. */
2861 if (IS_TYPE_UNIT_GROUP (per_cu))
2862 return;
2863
2864 /* The destructor of dwarf2_queue_guard frees any entries left on
2865 the queue. After this point we're guaranteed to leave this function
2866 with the dwarf queue empty. */
2867 dwarf2_queue_guard q_guard;
2868
2869 if (dwarf2_per_objfile->using_index
2870 ? per_cu->v.quick->compunit_symtab == NULL
2871 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2872 {
2873 queue_comp_unit (per_cu, language_minimal);
2874 load_cu (per_cu, skip_partial);
2875
2876 /* If we just loaded a CU from a DWO, and we're working with an index
2877 that may badly handle TUs, load all the TUs in that DWO as well.
2878 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2879 if (!per_cu->is_debug_types
2880 && per_cu->cu != NULL
2881 && per_cu->cu->dwo_unit != NULL
2882 && dwarf2_per_objfile->index_table != NULL
2883 && dwarf2_per_objfile->index_table->version <= 7
2884 /* DWP files aren't supported yet. */
2885 && get_dwp_file (dwarf2_per_objfile) == NULL)
2886 queue_and_load_all_dwo_tus (per_cu);
2887 }
2888
2889 process_queue (dwarf2_per_objfile);
2890
2891 /* Age the cache, releasing compilation units that have not
2892 been used recently. */
2893 age_cached_comp_units (dwarf2_per_objfile);
2894 }
2895
2896 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2897 the objfile from which this CU came. Returns the resulting symbol
2898 table. */
2899
2900 static struct compunit_symtab *
2901 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2902 {
2903 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2904
2905 gdb_assert (dwarf2_per_objfile->using_index);
2906 if (!per_cu->v.quick->compunit_symtab)
2907 {
2908 free_cached_comp_units freer (dwarf2_per_objfile);
2909 scoped_restore decrementer = increment_reading_symtab ();
2910 dw2_do_instantiate_symtab (per_cu, skip_partial);
2911 process_cu_includes (dwarf2_per_objfile);
2912 }
2913
2914 return per_cu->v.quick->compunit_symtab;
2915 }
2916
2917 /* See declaration. */
2918
2919 dwarf2_per_cu_data *
2920 dwarf2_per_objfile::get_cutu (int index)
2921 {
2922 if (index >= this->all_comp_units.size ())
2923 {
2924 index -= this->all_comp_units.size ();
2925 gdb_assert (index < this->all_type_units.size ());
2926 return &this->all_type_units[index]->per_cu;
2927 }
2928
2929 return this->all_comp_units[index];
2930 }
2931
2932 /* See declaration. */
2933
2934 dwarf2_per_cu_data *
2935 dwarf2_per_objfile::get_cu (int index)
2936 {
2937 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2938
2939 return this->all_comp_units[index];
2940 }
2941
2942 /* See declaration. */
2943
2944 signatured_type *
2945 dwarf2_per_objfile::get_tu (int index)
2946 {
2947 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2948
2949 return this->all_type_units[index];
2950 }
2951
2952 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2953 objfile_obstack, and constructed with the specified field
2954 values. */
2955
2956 static dwarf2_per_cu_data *
2957 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2958 struct dwarf2_section_info *section,
2959 int is_dwz,
2960 sect_offset sect_off, ULONGEST length)
2961 {
2962 struct objfile *objfile = dwarf2_per_objfile->objfile;
2963 dwarf2_per_cu_data *the_cu
2964 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2965 struct dwarf2_per_cu_data);
2966 the_cu->sect_off = sect_off;
2967 the_cu->length = length;
2968 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2969 the_cu->section = section;
2970 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2971 struct dwarf2_per_cu_quick_data);
2972 the_cu->is_dwz = is_dwz;
2973 return the_cu;
2974 }
2975
2976 /* A helper for create_cus_from_index that handles a given list of
2977 CUs. */
2978
2979 static void
2980 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2981 const gdb_byte *cu_list, offset_type n_elements,
2982 struct dwarf2_section_info *section,
2983 int is_dwz)
2984 {
2985 for (offset_type i = 0; i < n_elements; i += 2)
2986 {
2987 gdb_static_assert (sizeof (ULONGEST) >= 8);
2988
2989 sect_offset sect_off
2990 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2991 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2992 cu_list += 2 * 8;
2993
2994 dwarf2_per_cu_data *per_cu
2995 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2996 sect_off, length);
2997 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2998 }
2999 }
3000
3001 /* Read the CU list from the mapped index, and use it to create all
3002 the CU objects for this objfile. */
3003
3004 static void
3005 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3006 const gdb_byte *cu_list, offset_type cu_list_elements,
3007 const gdb_byte *dwz_list, offset_type dwz_elements)
3008 {
3009 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3010 dwarf2_per_objfile->all_comp_units.reserve
3011 ((cu_list_elements + dwz_elements) / 2);
3012
3013 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3014 &dwarf2_per_objfile->info, 0);
3015
3016 if (dwz_elements == 0)
3017 return;
3018
3019 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3020 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3021 &dwz->info, 1);
3022 }
3023
3024 /* Create the signatured type hash table from the index. */
3025
3026 static void
3027 create_signatured_type_table_from_index
3028 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3029 struct dwarf2_section_info *section,
3030 const gdb_byte *bytes,
3031 offset_type elements)
3032 {
3033 struct objfile *objfile = dwarf2_per_objfile->objfile;
3034
3035 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3036 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
3037
3038 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3039
3040 for (offset_type i = 0; i < elements; i += 3)
3041 {
3042 struct signatured_type *sig_type;
3043 ULONGEST signature;
3044 void **slot;
3045 cu_offset type_offset_in_tu;
3046
3047 gdb_static_assert (sizeof (ULONGEST) >= 8);
3048 sect_offset sect_off
3049 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3050 type_offset_in_tu
3051 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3052 BFD_ENDIAN_LITTLE);
3053 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3054 bytes += 3 * 8;
3055
3056 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3057 struct signatured_type);
3058 sig_type->signature = signature;
3059 sig_type->type_offset_in_tu = type_offset_in_tu;
3060 sig_type->per_cu.is_debug_types = 1;
3061 sig_type->per_cu.section = section;
3062 sig_type->per_cu.sect_off = sect_off;
3063 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3064 sig_type->per_cu.v.quick
3065 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3066 struct dwarf2_per_cu_quick_data);
3067
3068 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3069 *slot = sig_type;
3070
3071 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3072 }
3073
3074 dwarf2_per_objfile->signatured_types = sig_types_hash;
3075 }
3076
3077 /* Create the signatured type hash table from .debug_names. */
3078
3079 static void
3080 create_signatured_type_table_from_debug_names
3081 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3082 const mapped_debug_names &map,
3083 struct dwarf2_section_info *section,
3084 struct dwarf2_section_info *abbrev_section)
3085 {
3086 struct objfile *objfile = dwarf2_per_objfile->objfile;
3087
3088 dwarf2_read_section (objfile, section);
3089 dwarf2_read_section (objfile, abbrev_section);
3090
3091 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3092 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
3093
3094 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3095
3096 for (uint32_t i = 0; i < map.tu_count; ++i)
3097 {
3098 struct signatured_type *sig_type;
3099 void **slot;
3100
3101 sect_offset sect_off
3102 = (sect_offset) (extract_unsigned_integer
3103 (map.tu_table_reordered + i * map.offset_size,
3104 map.offset_size,
3105 map.dwarf5_byte_order));
3106
3107 comp_unit_head cu_header;
3108 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3109 abbrev_section,
3110 section->buffer + to_underlying (sect_off),
3111 rcuh_kind::TYPE);
3112
3113 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3114 struct signatured_type);
3115 sig_type->signature = cu_header.signature;
3116 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3117 sig_type->per_cu.is_debug_types = 1;
3118 sig_type->per_cu.section = section;
3119 sig_type->per_cu.sect_off = sect_off;
3120 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3121 sig_type->per_cu.v.quick
3122 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3123 struct dwarf2_per_cu_quick_data);
3124
3125 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3126 *slot = sig_type;
3127
3128 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3129 }
3130
3131 dwarf2_per_objfile->signatured_types = sig_types_hash;
3132 }
3133
3134 /* Read the address map data from the mapped index, and use it to
3135 populate the objfile's psymtabs_addrmap. */
3136
3137 static void
3138 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3139 struct mapped_index *index)
3140 {
3141 struct objfile *objfile = dwarf2_per_objfile->objfile;
3142 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3143 const gdb_byte *iter, *end;
3144 struct addrmap *mutable_map;
3145 CORE_ADDR baseaddr;
3146
3147 auto_obstack temp_obstack;
3148
3149 mutable_map = addrmap_create_mutable (&temp_obstack);
3150
3151 iter = index->address_table.data ();
3152 end = iter + index->address_table.size ();
3153
3154 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3155
3156 while (iter < end)
3157 {
3158 ULONGEST hi, lo, cu_index;
3159 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3160 iter += 8;
3161 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3162 iter += 8;
3163 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3164 iter += 4;
3165
3166 if (lo > hi)
3167 {
3168 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
3169 hex_string (lo), hex_string (hi));
3170 continue;
3171 }
3172
3173 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
3174 {
3175 complaint (_(".gdb_index address table has invalid CU number %u"),
3176 (unsigned) cu_index);
3177 continue;
3178 }
3179
3180 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3181 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
3182 addrmap_set_empty (mutable_map, lo, hi - 1,
3183 dwarf2_per_objfile->get_cu (cu_index));
3184 }
3185
3186 objfile->partial_symtabs->psymtabs_addrmap
3187 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3188 }
3189
3190 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3191 populate the objfile's psymtabs_addrmap. */
3192
3193 static void
3194 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3195 struct dwarf2_section_info *section)
3196 {
3197 struct objfile *objfile = dwarf2_per_objfile->objfile;
3198 bfd *abfd = objfile->obfd;
3199 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3200 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3201 SECT_OFF_TEXT (objfile));
3202
3203 auto_obstack temp_obstack;
3204 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3205
3206 std::unordered_map<sect_offset,
3207 dwarf2_per_cu_data *,
3208 gdb::hash_enum<sect_offset>>
3209 debug_info_offset_to_per_cu;
3210 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3211 {
3212 const auto insertpair
3213 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3214 if (!insertpair.second)
3215 {
3216 warning (_("Section .debug_aranges in %s has duplicate "
3217 "debug_info_offset %s, ignoring .debug_aranges."),
3218 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3219 return;
3220 }
3221 }
3222
3223 dwarf2_read_section (objfile, section);
3224
3225 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3226
3227 const gdb_byte *addr = section->buffer;
3228
3229 while (addr < section->buffer + section->size)
3230 {
3231 const gdb_byte *const entry_addr = addr;
3232 unsigned int bytes_read;
3233
3234 const LONGEST entry_length = read_initial_length (abfd, addr,
3235 &bytes_read);
3236 addr += bytes_read;
3237
3238 const gdb_byte *const entry_end = addr + entry_length;
3239 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3240 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3241 if (addr + entry_length > section->buffer + section->size)
3242 {
3243 warning (_("Section .debug_aranges in %s entry at offset %s "
3244 "length %s exceeds section length %s, "
3245 "ignoring .debug_aranges."),
3246 objfile_name (objfile),
3247 plongest (entry_addr - section->buffer),
3248 plongest (bytes_read + entry_length),
3249 pulongest (section->size));
3250 return;
3251 }
3252
3253 /* The version number. */
3254 const uint16_t version = read_2_bytes (abfd, addr);
3255 addr += 2;
3256 if (version != 2)
3257 {
3258 warning (_("Section .debug_aranges in %s entry at offset %s "
3259 "has unsupported version %d, ignoring .debug_aranges."),
3260 objfile_name (objfile),
3261 plongest (entry_addr - section->buffer), version);
3262 return;
3263 }
3264
3265 const uint64_t debug_info_offset
3266 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3267 addr += offset_size;
3268 const auto per_cu_it
3269 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3270 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3271 {
3272 warning (_("Section .debug_aranges in %s entry at offset %s "
3273 "debug_info_offset %s does not exists, "
3274 "ignoring .debug_aranges."),
3275 objfile_name (objfile),
3276 plongest (entry_addr - section->buffer),
3277 pulongest (debug_info_offset));
3278 return;
3279 }
3280 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3281
3282 const uint8_t address_size = *addr++;
3283 if (address_size < 1 || address_size > 8)
3284 {
3285 warning (_("Section .debug_aranges in %s entry at offset %s "
3286 "address_size %u is invalid, ignoring .debug_aranges."),
3287 objfile_name (objfile),
3288 plongest (entry_addr - section->buffer), address_size);
3289 return;
3290 }
3291
3292 const uint8_t segment_selector_size = *addr++;
3293 if (segment_selector_size != 0)
3294 {
3295 warning (_("Section .debug_aranges in %s entry at offset %s "
3296 "segment_selector_size %u is not supported, "
3297 "ignoring .debug_aranges."),
3298 objfile_name (objfile),
3299 plongest (entry_addr - section->buffer),
3300 segment_selector_size);
3301 return;
3302 }
3303
3304 /* Must pad to an alignment boundary that is twice the address
3305 size. It is undocumented by the DWARF standard but GCC does
3306 use it. */
3307 for (size_t padding = ((-(addr - section->buffer))
3308 & (2 * address_size - 1));
3309 padding > 0; padding--)
3310 if (*addr++ != 0)
3311 {
3312 warning (_("Section .debug_aranges in %s entry at offset %s "
3313 "padding is not zero, ignoring .debug_aranges."),
3314 objfile_name (objfile),
3315 plongest (entry_addr - section->buffer));
3316 return;
3317 }
3318
3319 for (;;)
3320 {
3321 if (addr + 2 * address_size > entry_end)
3322 {
3323 warning (_("Section .debug_aranges in %s entry at offset %s "
3324 "address list is not properly terminated, "
3325 "ignoring .debug_aranges."),
3326 objfile_name (objfile),
3327 plongest (entry_addr - section->buffer));
3328 return;
3329 }
3330 ULONGEST start = extract_unsigned_integer (addr, address_size,
3331 dwarf5_byte_order);
3332 addr += address_size;
3333 ULONGEST length = extract_unsigned_integer (addr, address_size,
3334 dwarf5_byte_order);
3335 addr += address_size;
3336 if (start == 0 && length == 0)
3337 break;
3338 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3339 {
3340 /* Symbol was eliminated due to a COMDAT group. */
3341 continue;
3342 }
3343 ULONGEST end = start + length;
3344 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3345 - baseaddr);
3346 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3347 - baseaddr);
3348 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3349 }
3350 }
3351
3352 objfile->partial_symtabs->psymtabs_addrmap
3353 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3354 }
3355
3356 /* Find a slot in the mapped index INDEX for the object named NAME.
3357 If NAME is found, set *VEC_OUT to point to the CU vector in the
3358 constant pool and return true. If NAME cannot be found, return
3359 false. */
3360
3361 static bool
3362 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3363 offset_type **vec_out)
3364 {
3365 offset_type hash;
3366 offset_type slot, step;
3367 int (*cmp) (const char *, const char *);
3368
3369 gdb::unique_xmalloc_ptr<char> without_params;
3370 if (current_language->la_language == language_cplus
3371 || current_language->la_language == language_fortran
3372 || current_language->la_language == language_d)
3373 {
3374 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3375 not contain any. */
3376
3377 if (strchr (name, '(') != NULL)
3378 {
3379 without_params = cp_remove_params (name);
3380
3381 if (without_params != NULL)
3382 name = without_params.get ();
3383 }
3384 }
3385
3386 /* Index version 4 did not support case insensitive searches. But the
3387 indices for case insensitive languages are built in lowercase, therefore
3388 simulate our NAME being searched is also lowercased. */
3389 hash = mapped_index_string_hash ((index->version == 4
3390 && case_sensitivity == case_sensitive_off
3391 ? 5 : index->version),
3392 name);
3393
3394 slot = hash & (index->symbol_table.size () - 1);
3395 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3396 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3397
3398 for (;;)
3399 {
3400 const char *str;
3401
3402 const auto &bucket = index->symbol_table[slot];
3403 if (bucket.name == 0 && bucket.vec == 0)
3404 return false;
3405
3406 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3407 if (!cmp (name, str))
3408 {
3409 *vec_out = (offset_type *) (index->constant_pool
3410 + MAYBE_SWAP (bucket.vec));
3411 return true;
3412 }
3413
3414 slot = (slot + step) & (index->symbol_table.size () - 1);
3415 }
3416 }
3417
3418 /* A helper function that reads the .gdb_index from BUFFER and fills
3419 in MAP. FILENAME is the name of the file containing the data;
3420 it is used for error reporting. DEPRECATED_OK is true if it is
3421 ok to use deprecated sections.
3422
3423 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3424 out parameters that are filled in with information about the CU and
3425 TU lists in the section.
3426
3427 Returns true if all went well, false otherwise. */
3428
3429 static bool
3430 read_gdb_index_from_buffer (struct objfile *objfile,
3431 const char *filename,
3432 bool deprecated_ok,
3433 gdb::array_view<const gdb_byte> buffer,
3434 struct mapped_index *map,
3435 const gdb_byte **cu_list,
3436 offset_type *cu_list_elements,
3437 const gdb_byte **types_list,
3438 offset_type *types_list_elements)
3439 {
3440 const gdb_byte *addr = &buffer[0];
3441
3442 /* Version check. */
3443 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3444 /* Versions earlier than 3 emitted every copy of a psymbol. This
3445 causes the index to behave very poorly for certain requests. Version 3
3446 contained incomplete addrmap. So, it seems better to just ignore such
3447 indices. */
3448 if (version < 4)
3449 {
3450 static int warning_printed = 0;
3451 if (!warning_printed)
3452 {
3453 warning (_("Skipping obsolete .gdb_index section in %s."),
3454 filename);
3455 warning_printed = 1;
3456 }
3457 return 0;
3458 }
3459 /* Index version 4 uses a different hash function than index version
3460 5 and later.
3461
3462 Versions earlier than 6 did not emit psymbols for inlined
3463 functions. Using these files will cause GDB not to be able to
3464 set breakpoints on inlined functions by name, so we ignore these
3465 indices unless the user has done
3466 "set use-deprecated-index-sections on". */
3467 if (version < 6 && !deprecated_ok)
3468 {
3469 static int warning_printed = 0;
3470 if (!warning_printed)
3471 {
3472 warning (_("\
3473 Skipping deprecated .gdb_index section in %s.\n\
3474 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3475 to use the section anyway."),
3476 filename);
3477 warning_printed = 1;
3478 }
3479 return 0;
3480 }
3481 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3482 of the TU (for symbols coming from TUs),
3483 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3484 Plus gold-generated indices can have duplicate entries for global symbols,
3485 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3486 These are just performance bugs, and we can't distinguish gdb-generated
3487 indices from gold-generated ones, so issue no warning here. */
3488
3489 /* Indexes with higher version than the one supported by GDB may be no
3490 longer backward compatible. */
3491 if (version > 8)
3492 return 0;
3493
3494 map->version = version;
3495
3496 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3497
3498 int i = 0;
3499 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3500 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3501 / 8);
3502 ++i;
3503
3504 *types_list = addr + MAYBE_SWAP (metadata[i]);
3505 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3506 - MAYBE_SWAP (metadata[i]))
3507 / 8);
3508 ++i;
3509
3510 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3511 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3512 map->address_table
3513 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3514 ++i;
3515
3516 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3517 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3518 map->symbol_table
3519 = gdb::array_view<mapped_index::symbol_table_slot>
3520 ((mapped_index::symbol_table_slot *) symbol_table,
3521 (mapped_index::symbol_table_slot *) symbol_table_end);
3522
3523 ++i;
3524 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3525
3526 return 1;
3527 }
3528
3529 /* Callback types for dwarf2_read_gdb_index. */
3530
3531 typedef gdb::function_view
3532 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3533 get_gdb_index_contents_ftype;
3534 typedef gdb::function_view
3535 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3536 get_gdb_index_contents_dwz_ftype;
3537
3538 /* Read .gdb_index. If everything went ok, initialize the "quick"
3539 elements of all the CUs and return 1. Otherwise, return 0. */
3540
3541 static int
3542 dwarf2_read_gdb_index
3543 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3544 get_gdb_index_contents_ftype get_gdb_index_contents,
3545 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3546 {
3547 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3548 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3549 struct dwz_file *dwz;
3550 struct objfile *objfile = dwarf2_per_objfile->objfile;
3551
3552 gdb::array_view<const gdb_byte> main_index_contents
3553 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3554
3555 if (main_index_contents.empty ())
3556 return 0;
3557
3558 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3559 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3560 use_deprecated_index_sections,
3561 main_index_contents, map.get (), &cu_list,
3562 &cu_list_elements, &types_list,
3563 &types_list_elements))
3564 return 0;
3565
3566 /* Don't use the index if it's empty. */
3567 if (map->symbol_table.empty ())
3568 return 0;
3569
3570 /* If there is a .dwz file, read it so we can get its CU list as
3571 well. */
3572 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3573 if (dwz != NULL)
3574 {
3575 struct mapped_index dwz_map;
3576 const gdb_byte *dwz_types_ignore;
3577 offset_type dwz_types_elements_ignore;
3578
3579 gdb::array_view<const gdb_byte> dwz_index_content
3580 = get_gdb_index_contents_dwz (objfile, dwz);
3581
3582 if (dwz_index_content.empty ())
3583 return 0;
3584
3585 if (!read_gdb_index_from_buffer (objfile,
3586 bfd_get_filename (dwz->dwz_bfd), 1,
3587 dwz_index_content, &dwz_map,
3588 &dwz_list, &dwz_list_elements,
3589 &dwz_types_ignore,
3590 &dwz_types_elements_ignore))
3591 {
3592 warning (_("could not read '.gdb_index' section from %s; skipping"),
3593 bfd_get_filename (dwz->dwz_bfd));
3594 return 0;
3595 }
3596 }
3597
3598 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3599 dwz_list, dwz_list_elements);
3600
3601 if (types_list_elements)
3602 {
3603 /* We can only handle a single .debug_types when we have an
3604 index. */
3605 if (dwarf2_per_objfile->types.size () != 1)
3606 return 0;
3607
3608 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3609
3610 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3611 types_list, types_list_elements);
3612 }
3613
3614 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3615
3616 dwarf2_per_objfile->index_table = std::move (map);
3617 dwarf2_per_objfile->using_index = 1;
3618 dwarf2_per_objfile->quick_file_names_table =
3619 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3620
3621 return 1;
3622 }
3623
3624 /* die_reader_func for dw2_get_file_names. */
3625
3626 static void
3627 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3628 const gdb_byte *info_ptr,
3629 struct die_info *comp_unit_die,
3630 int has_children,
3631 void *data)
3632 {
3633 struct dwarf2_cu *cu = reader->cu;
3634 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3635 struct dwarf2_per_objfile *dwarf2_per_objfile
3636 = cu->per_cu->dwarf2_per_objfile;
3637 struct objfile *objfile = dwarf2_per_objfile->objfile;
3638 struct dwarf2_per_cu_data *lh_cu;
3639 struct attribute *attr;
3640 int i;
3641 void **slot;
3642 struct quick_file_names *qfn;
3643
3644 gdb_assert (! this_cu->is_debug_types);
3645
3646 /* Our callers never want to match partial units -- instead they
3647 will match the enclosing full CU. */
3648 if (comp_unit_die->tag == DW_TAG_partial_unit)
3649 {
3650 this_cu->v.quick->no_file_data = 1;
3651 return;
3652 }
3653
3654 lh_cu = this_cu;
3655 slot = NULL;
3656
3657 line_header_up lh;
3658 sect_offset line_offset {};
3659
3660 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3661 if (attr)
3662 {
3663 struct quick_file_names find_entry;
3664
3665 line_offset = (sect_offset) DW_UNSND (attr);
3666
3667 /* We may have already read in this line header (TU line header sharing).
3668 If we have we're done. */
3669 find_entry.hash.dwo_unit = cu->dwo_unit;
3670 find_entry.hash.line_sect_off = line_offset;
3671 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3672 &find_entry, INSERT);
3673 if (*slot != NULL)
3674 {
3675 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3676 return;
3677 }
3678
3679 lh = dwarf_decode_line_header (line_offset, cu);
3680 }
3681 if (lh == NULL)
3682 {
3683 lh_cu->v.quick->no_file_data = 1;
3684 return;
3685 }
3686
3687 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3688 qfn->hash.dwo_unit = cu->dwo_unit;
3689 qfn->hash.line_sect_off = line_offset;
3690 gdb_assert (slot != NULL);
3691 *slot = qfn;
3692
3693 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3694
3695 qfn->num_file_names = lh->file_names.size ();
3696 qfn->file_names =
3697 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3698 for (i = 0; i < lh->file_names.size (); ++i)
3699 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3700 qfn->real_names = NULL;
3701
3702 lh_cu->v.quick->file_names = qfn;
3703 }
3704
3705 /* A helper for the "quick" functions which attempts to read the line
3706 table for THIS_CU. */
3707
3708 static struct quick_file_names *
3709 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3710 {
3711 /* This should never be called for TUs. */
3712 gdb_assert (! this_cu->is_debug_types);
3713 /* Nor type unit groups. */
3714 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3715
3716 if (this_cu->v.quick->file_names != NULL)
3717 return this_cu->v.quick->file_names;
3718 /* If we know there is no line data, no point in looking again. */
3719 if (this_cu->v.quick->no_file_data)
3720 return NULL;
3721
3722 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3723
3724 if (this_cu->v.quick->no_file_data)
3725 return NULL;
3726 return this_cu->v.quick->file_names;
3727 }
3728
3729 /* A helper for the "quick" functions which computes and caches the
3730 real path for a given file name from the line table. */
3731
3732 static const char *
3733 dw2_get_real_path (struct objfile *objfile,
3734 struct quick_file_names *qfn, int index)
3735 {
3736 if (qfn->real_names == NULL)
3737 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3738 qfn->num_file_names, const char *);
3739
3740 if (qfn->real_names[index] == NULL)
3741 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3742
3743 return qfn->real_names[index];
3744 }
3745
3746 static struct symtab *
3747 dw2_find_last_source_symtab (struct objfile *objfile)
3748 {
3749 struct dwarf2_per_objfile *dwarf2_per_objfile
3750 = get_dwarf2_per_objfile (objfile);
3751 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3752 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3753
3754 if (cust == NULL)
3755 return NULL;
3756
3757 return compunit_primary_filetab (cust);
3758 }
3759
3760 /* Traversal function for dw2_forget_cached_source_info. */
3761
3762 static int
3763 dw2_free_cached_file_names (void **slot, void *info)
3764 {
3765 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3766
3767 if (file_data->real_names)
3768 {
3769 int i;
3770
3771 for (i = 0; i < file_data->num_file_names; ++i)
3772 {
3773 xfree ((void*) file_data->real_names[i]);
3774 file_data->real_names[i] = NULL;
3775 }
3776 }
3777
3778 return 1;
3779 }
3780
3781 static void
3782 dw2_forget_cached_source_info (struct objfile *objfile)
3783 {
3784 struct dwarf2_per_objfile *dwarf2_per_objfile
3785 = get_dwarf2_per_objfile (objfile);
3786
3787 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3788 dw2_free_cached_file_names, NULL);
3789 }
3790
3791 /* Helper function for dw2_map_symtabs_matching_filename that expands
3792 the symtabs and calls the iterator. */
3793
3794 static int
3795 dw2_map_expand_apply (struct objfile *objfile,
3796 struct dwarf2_per_cu_data *per_cu,
3797 const char *name, const char *real_path,
3798 gdb::function_view<bool (symtab *)> callback)
3799 {
3800 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3801
3802 /* Don't visit already-expanded CUs. */
3803 if (per_cu->v.quick->compunit_symtab)
3804 return 0;
3805
3806 /* This may expand more than one symtab, and we want to iterate over
3807 all of them. */
3808 dw2_instantiate_symtab (per_cu, false);
3809
3810 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3811 last_made, callback);
3812 }
3813
3814 /* Implementation of the map_symtabs_matching_filename method. */
3815
3816 static bool
3817 dw2_map_symtabs_matching_filename
3818 (struct objfile *objfile, const char *name, const char *real_path,
3819 gdb::function_view<bool (symtab *)> callback)
3820 {
3821 const char *name_basename = lbasename (name);
3822 struct dwarf2_per_objfile *dwarf2_per_objfile
3823 = get_dwarf2_per_objfile (objfile);
3824
3825 /* The rule is CUs specify all the files, including those used by
3826 any TU, so there's no need to scan TUs here. */
3827
3828 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3829 {
3830 /* We only need to look at symtabs not already expanded. */
3831 if (per_cu->v.quick->compunit_symtab)
3832 continue;
3833
3834 quick_file_names *file_data = dw2_get_file_names (per_cu);
3835 if (file_data == NULL)
3836 continue;
3837
3838 for (int j = 0; j < file_data->num_file_names; ++j)
3839 {
3840 const char *this_name = file_data->file_names[j];
3841 const char *this_real_name;
3842
3843 if (compare_filenames_for_search (this_name, name))
3844 {
3845 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3846 callback))
3847 return true;
3848 continue;
3849 }
3850
3851 /* Before we invoke realpath, which can get expensive when many
3852 files are involved, do a quick comparison of the basenames. */
3853 if (! basenames_may_differ
3854 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3855 continue;
3856
3857 this_real_name = dw2_get_real_path (objfile, file_data, j);
3858 if (compare_filenames_for_search (this_real_name, name))
3859 {
3860 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3861 callback))
3862 return true;
3863 continue;
3864 }
3865
3866 if (real_path != NULL)
3867 {
3868 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3869 gdb_assert (IS_ABSOLUTE_PATH (name));
3870 if (this_real_name != NULL
3871 && FILENAME_CMP (real_path, this_real_name) == 0)
3872 {
3873 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3874 callback))
3875 return true;
3876 continue;
3877 }
3878 }
3879 }
3880 }
3881
3882 return false;
3883 }
3884
3885 /* Struct used to manage iterating over all CUs looking for a symbol. */
3886
3887 struct dw2_symtab_iterator
3888 {
3889 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3890 struct dwarf2_per_objfile *dwarf2_per_objfile;
3891 /* If set, only look for symbols that match that block. Valid values are
3892 GLOBAL_BLOCK and STATIC_BLOCK. */
3893 gdb::optional<block_enum> block_index;
3894 /* The kind of symbol we're looking for. */
3895 domain_enum domain;
3896 /* The list of CUs from the index entry of the symbol,
3897 or NULL if not found. */
3898 offset_type *vec;
3899 /* The next element in VEC to look at. */
3900 int next;
3901 /* The number of elements in VEC, or zero if there is no match. */
3902 int length;
3903 /* Have we seen a global version of the symbol?
3904 If so we can ignore all further global instances.
3905 This is to work around gold/15646, inefficient gold-generated
3906 indices. */
3907 int global_seen;
3908 };
3909
3910 /* Initialize the index symtab iterator ITER. */
3911
3912 static void
3913 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3914 struct dwarf2_per_objfile *dwarf2_per_objfile,
3915 gdb::optional<block_enum> block_index,
3916 domain_enum domain,
3917 const char *name)
3918 {
3919 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3920 iter->block_index = block_index;
3921 iter->domain = domain;
3922 iter->next = 0;
3923 iter->global_seen = 0;
3924
3925 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3926
3927 /* index is NULL if OBJF_READNOW. */
3928 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3929 iter->length = MAYBE_SWAP (*iter->vec);
3930 else
3931 {
3932 iter->vec = NULL;
3933 iter->length = 0;
3934 }
3935 }
3936
3937 /* Return the next matching CU or NULL if there are no more. */
3938
3939 static struct dwarf2_per_cu_data *
3940 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3941 {
3942 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3943
3944 for ( ; iter->next < iter->length; ++iter->next)
3945 {
3946 offset_type cu_index_and_attrs =
3947 MAYBE_SWAP (iter->vec[iter->next + 1]);
3948 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3949 gdb_index_symbol_kind symbol_kind =
3950 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3951 /* Only check the symbol attributes if they're present.
3952 Indices prior to version 7 don't record them,
3953 and indices >= 7 may elide them for certain symbols
3954 (gold does this). */
3955 int attrs_valid =
3956 (dwarf2_per_objfile->index_table->version >= 7
3957 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3958
3959 /* Don't crash on bad data. */
3960 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3961 + dwarf2_per_objfile->all_type_units.size ()))
3962 {
3963 complaint (_(".gdb_index entry has bad CU index"
3964 " [in module %s]"),
3965 objfile_name (dwarf2_per_objfile->objfile));
3966 continue;
3967 }
3968
3969 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3970
3971 /* Skip if already read in. */
3972 if (per_cu->v.quick->compunit_symtab)
3973 continue;
3974
3975 /* Check static vs global. */
3976 if (attrs_valid)
3977 {
3978 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3979
3980 if (iter->block_index.has_value ())
3981 {
3982 bool want_static = *iter->block_index == STATIC_BLOCK;
3983
3984 if (is_static != want_static)
3985 continue;
3986 }
3987
3988 /* Work around gold/15646. */
3989 if (!is_static && iter->global_seen)
3990 continue;
3991 if (!is_static)
3992 iter->global_seen = 1;
3993 }
3994
3995 /* Only check the symbol's kind if it has one. */
3996 if (attrs_valid)
3997 {
3998 switch (iter->domain)
3999 {
4000 case VAR_DOMAIN:
4001 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4002 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4003 /* Some types are also in VAR_DOMAIN. */
4004 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4005 continue;
4006 break;
4007 case STRUCT_DOMAIN:
4008 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4009 continue;
4010 break;
4011 case LABEL_DOMAIN:
4012 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4013 continue;
4014 break;
4015 default:
4016 break;
4017 }
4018 }
4019
4020 ++iter->next;
4021 return per_cu;
4022 }
4023
4024 return NULL;
4025 }
4026
4027 static struct compunit_symtab *
4028 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
4029 const char *name, domain_enum domain)
4030 {
4031 struct compunit_symtab *stab_best = NULL;
4032 struct dwarf2_per_objfile *dwarf2_per_objfile
4033 = get_dwarf2_per_objfile (objfile);
4034
4035 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4036
4037 struct dw2_symtab_iterator iter;
4038 struct dwarf2_per_cu_data *per_cu;
4039
4040 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
4041
4042 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4043 {
4044 struct symbol *sym, *with_opaque = NULL;
4045 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
4046 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4047 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4048
4049 sym = block_find_symbol (block, name, domain,
4050 block_find_non_opaque_type_preferred,
4051 &with_opaque);
4052
4053 /* Some caution must be observed with overloaded functions
4054 and methods, since the index will not contain any overload
4055 information (but NAME might contain it). */
4056
4057 if (sym != NULL
4058 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4059 return stab;
4060 if (with_opaque != NULL
4061 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4062 stab_best = stab;
4063
4064 /* Keep looking through other CUs. */
4065 }
4066
4067 return stab_best;
4068 }
4069
4070 static void
4071 dw2_print_stats (struct objfile *objfile)
4072 {
4073 struct dwarf2_per_objfile *dwarf2_per_objfile
4074 = get_dwarf2_per_objfile (objfile);
4075 int total = (dwarf2_per_objfile->all_comp_units.size ()
4076 + dwarf2_per_objfile->all_type_units.size ());
4077 int count = 0;
4078
4079 for (int i = 0; i < total; ++i)
4080 {
4081 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4082
4083 if (!per_cu->v.quick->compunit_symtab)
4084 ++count;
4085 }
4086 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4087 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4088 }
4089
4090 /* This dumps minimal information about the index.
4091 It is called via "mt print objfiles".
4092 One use is to verify .gdb_index has been loaded by the
4093 gdb.dwarf2/gdb-index.exp testcase. */
4094
4095 static void
4096 dw2_dump (struct objfile *objfile)
4097 {
4098 struct dwarf2_per_objfile *dwarf2_per_objfile
4099 = get_dwarf2_per_objfile (objfile);
4100
4101 gdb_assert (dwarf2_per_objfile->using_index);
4102 printf_filtered (".gdb_index:");
4103 if (dwarf2_per_objfile->index_table != NULL)
4104 {
4105 printf_filtered (" version %d\n",
4106 dwarf2_per_objfile->index_table->version);
4107 }
4108 else
4109 printf_filtered (" faked for \"readnow\"\n");
4110 printf_filtered ("\n");
4111 }
4112
4113 static void
4114 dw2_expand_symtabs_for_function (struct objfile *objfile,
4115 const char *func_name)
4116 {
4117 struct dwarf2_per_objfile *dwarf2_per_objfile
4118 = get_dwarf2_per_objfile (objfile);
4119
4120 struct dw2_symtab_iterator iter;
4121 struct dwarf2_per_cu_data *per_cu;
4122
4123 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
4124
4125 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4126 dw2_instantiate_symtab (per_cu, false);
4127
4128 }
4129
4130 static void
4131 dw2_expand_all_symtabs (struct objfile *objfile)
4132 {
4133 struct dwarf2_per_objfile *dwarf2_per_objfile
4134 = get_dwarf2_per_objfile (objfile);
4135 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4136 + dwarf2_per_objfile->all_type_units.size ());
4137
4138 for (int i = 0; i < total_units; ++i)
4139 {
4140 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4141
4142 /* We don't want to directly expand a partial CU, because if we
4143 read it with the wrong language, then assertion failures can
4144 be triggered later on. See PR symtab/23010. So, tell
4145 dw2_instantiate_symtab to skip partial CUs -- any important
4146 partial CU will be read via DW_TAG_imported_unit anyway. */
4147 dw2_instantiate_symtab (per_cu, true);
4148 }
4149 }
4150
4151 static void
4152 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4153 const char *fullname)
4154 {
4155 struct dwarf2_per_objfile *dwarf2_per_objfile
4156 = get_dwarf2_per_objfile (objfile);
4157
4158 /* We don't need to consider type units here.
4159 This is only called for examining code, e.g. expand_line_sal.
4160 There can be an order of magnitude (or more) more type units
4161 than comp units, and we avoid them if we can. */
4162
4163 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4164 {
4165 /* We only need to look at symtabs not already expanded. */
4166 if (per_cu->v.quick->compunit_symtab)
4167 continue;
4168
4169 quick_file_names *file_data = dw2_get_file_names (per_cu);
4170 if (file_data == NULL)
4171 continue;
4172
4173 for (int j = 0; j < file_data->num_file_names; ++j)
4174 {
4175 const char *this_fullname = file_data->file_names[j];
4176
4177 if (filename_cmp (this_fullname, fullname) == 0)
4178 {
4179 dw2_instantiate_symtab (per_cu, false);
4180 break;
4181 }
4182 }
4183 }
4184 }
4185
4186 static void
4187 dw2_map_matching_symbols
4188 (struct objfile *objfile,
4189 const lookup_name_info &name, domain_enum domain,
4190 int global,
4191 gdb::function_view<symbol_found_callback_ftype> callback,
4192 symbol_compare_ftype *ordered_compare)
4193 {
4194 /* Currently unimplemented; used for Ada. The function can be called if the
4195 current language is Ada for a non-Ada objfile using GNU index. As Ada
4196 does not look for non-Ada symbols this function should just return. */
4197 }
4198
4199 /* Symbol name matcher for .gdb_index names.
4200
4201 Symbol names in .gdb_index have a few particularities:
4202
4203 - There's no indication of which is the language of each symbol.
4204
4205 Since each language has its own symbol name matching algorithm,
4206 and we don't know which language is the right one, we must match
4207 each symbol against all languages. This would be a potential
4208 performance problem if it were not mitigated by the
4209 mapped_index::name_components lookup table, which significantly
4210 reduces the number of times we need to call into this matcher,
4211 making it a non-issue.
4212
4213 - Symbol names in the index have no overload (parameter)
4214 information. I.e., in C++, "foo(int)" and "foo(long)" both
4215 appear as "foo" in the index, for example.
4216
4217 This means that the lookup names passed to the symbol name
4218 matcher functions must have no parameter information either
4219 because (e.g.) symbol search name "foo" does not match
4220 lookup-name "foo(int)" [while swapping search name for lookup
4221 name would match].
4222 */
4223 class gdb_index_symbol_name_matcher
4224 {
4225 public:
4226 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4227 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4228
4229 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4230 Returns true if any matcher matches. */
4231 bool matches (const char *symbol_name);
4232
4233 private:
4234 /* A reference to the lookup name we're matching against. */
4235 const lookup_name_info &m_lookup_name;
4236
4237 /* A vector holding all the different symbol name matchers, for all
4238 languages. */
4239 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4240 };
4241
4242 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4243 (const lookup_name_info &lookup_name)
4244 : m_lookup_name (lookup_name)
4245 {
4246 /* Prepare the vector of comparison functions upfront, to avoid
4247 doing the same work for each symbol. Care is taken to avoid
4248 matching with the same matcher more than once if/when multiple
4249 languages use the same matcher function. */
4250 auto &matchers = m_symbol_name_matcher_funcs;
4251 matchers.reserve (nr_languages);
4252
4253 matchers.push_back (default_symbol_name_matcher);
4254
4255 for (int i = 0; i < nr_languages; i++)
4256 {
4257 const language_defn *lang = language_def ((enum language) i);
4258 symbol_name_matcher_ftype *name_matcher
4259 = get_symbol_name_matcher (lang, m_lookup_name);
4260
4261 /* Don't insert the same comparison routine more than once.
4262 Note that we do this linear walk instead of a seemingly
4263 cheaper sorted insert, or use a std::set or something like
4264 that, because relative order of function addresses is not
4265 stable. This is not a problem in practice because the number
4266 of supported languages is low, and the cost here is tiny
4267 compared to the number of searches we'll do afterwards using
4268 this object. */
4269 if (name_matcher != default_symbol_name_matcher
4270 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4271 == matchers.end ()))
4272 matchers.push_back (name_matcher);
4273 }
4274 }
4275
4276 bool
4277 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4278 {
4279 for (auto matches_name : m_symbol_name_matcher_funcs)
4280 if (matches_name (symbol_name, m_lookup_name, NULL))
4281 return true;
4282
4283 return false;
4284 }
4285
4286 /* Starting from a search name, return the string that finds the upper
4287 bound of all strings that start with SEARCH_NAME in a sorted name
4288 list. Returns the empty string to indicate that the upper bound is
4289 the end of the list. */
4290
4291 static std::string
4292 make_sort_after_prefix_name (const char *search_name)
4293 {
4294 /* When looking to complete "func", we find the upper bound of all
4295 symbols that start with "func" by looking for where we'd insert
4296 the closest string that would follow "func" in lexicographical
4297 order. Usually, that's "func"-with-last-character-incremented,
4298 i.e. "fund". Mind non-ASCII characters, though. Usually those
4299 will be UTF-8 multi-byte sequences, but we can't be certain.
4300 Especially mind the 0xff character, which is a valid character in
4301 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4302 rule out compilers allowing it in identifiers. Note that
4303 conveniently, strcmp/strcasecmp are specified to compare
4304 characters interpreted as unsigned char. So what we do is treat
4305 the whole string as a base 256 number composed of a sequence of
4306 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4307 to 0, and carries 1 to the following more-significant position.
4308 If the very first character in SEARCH_NAME ends up incremented
4309 and carries/overflows, then the upper bound is the end of the
4310 list. The string after the empty string is also the empty
4311 string.
4312
4313 Some examples of this operation:
4314
4315 SEARCH_NAME => "+1" RESULT
4316
4317 "abc" => "abd"
4318 "ab\xff" => "ac"
4319 "\xff" "a" "\xff" => "\xff" "b"
4320 "\xff" => ""
4321 "\xff\xff" => ""
4322 "" => ""
4323
4324 Then, with these symbols for example:
4325
4326 func
4327 func1
4328 fund
4329
4330 completing "func" looks for symbols between "func" and
4331 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4332 which finds "func" and "func1", but not "fund".
4333
4334 And with:
4335
4336 funcÿ (Latin1 'ÿ' [0xff])
4337 funcÿ1
4338 fund
4339
4340 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4341 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4342
4343 And with:
4344
4345 ÿÿ (Latin1 'ÿ' [0xff])
4346 ÿÿ1
4347
4348 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4349 the end of the list.
4350 */
4351 std::string after = search_name;
4352 while (!after.empty () && (unsigned char) after.back () == 0xff)
4353 after.pop_back ();
4354 if (!after.empty ())
4355 after.back () = (unsigned char) after.back () + 1;
4356 return after;
4357 }
4358
4359 /* See declaration. */
4360
4361 std::pair<std::vector<name_component>::const_iterator,
4362 std::vector<name_component>::const_iterator>
4363 mapped_index_base::find_name_components_bounds
4364 (const lookup_name_info &lookup_name_without_params) const
4365 {
4366 auto *name_cmp
4367 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4368
4369 const char *cplus
4370 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4371
4372 /* Comparison function object for lower_bound that matches against a
4373 given symbol name. */
4374 auto lookup_compare_lower = [&] (const name_component &elem,
4375 const char *name)
4376 {
4377 const char *elem_qualified = this->symbol_name_at (elem.idx);
4378 const char *elem_name = elem_qualified + elem.name_offset;
4379 return name_cmp (elem_name, name) < 0;
4380 };
4381
4382 /* Comparison function object for upper_bound that matches against a
4383 given symbol name. */
4384 auto lookup_compare_upper = [&] (const char *name,
4385 const name_component &elem)
4386 {
4387 const char *elem_qualified = this->symbol_name_at (elem.idx);
4388 const char *elem_name = elem_qualified + elem.name_offset;
4389 return name_cmp (name, elem_name) < 0;
4390 };
4391
4392 auto begin = this->name_components.begin ();
4393 auto end = this->name_components.end ();
4394
4395 /* Find the lower bound. */
4396 auto lower = [&] ()
4397 {
4398 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4399 return begin;
4400 else
4401 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4402 } ();
4403
4404 /* Find the upper bound. */
4405 auto upper = [&] ()
4406 {
4407 if (lookup_name_without_params.completion_mode ())
4408 {
4409 /* In completion mode, we want UPPER to point past all
4410 symbols names that have the same prefix. I.e., with
4411 these symbols, and completing "func":
4412
4413 function << lower bound
4414 function1
4415 other_function << upper bound
4416
4417 We find the upper bound by looking for the insertion
4418 point of "func"-with-last-character-incremented,
4419 i.e. "fund". */
4420 std::string after = make_sort_after_prefix_name (cplus);
4421 if (after.empty ())
4422 return end;
4423 return std::lower_bound (lower, end, after.c_str (),
4424 lookup_compare_lower);
4425 }
4426 else
4427 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4428 } ();
4429
4430 return {lower, upper};
4431 }
4432
4433 /* See declaration. */
4434
4435 void
4436 mapped_index_base::build_name_components ()
4437 {
4438 if (!this->name_components.empty ())
4439 return;
4440
4441 this->name_components_casing = case_sensitivity;
4442 auto *name_cmp
4443 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4444
4445 /* The code below only knows how to break apart components of C++
4446 symbol names (and other languages that use '::' as
4447 namespace/module separator). If we add support for wild matching
4448 to some language that uses some other operator (E.g., Ada, Go and
4449 D use '.'), then we'll need to try splitting the symbol name
4450 according to that language too. Note that Ada does support wild
4451 matching, but doesn't currently support .gdb_index. */
4452 auto count = this->symbol_name_count ();
4453 for (offset_type idx = 0; idx < count; idx++)
4454 {
4455 if (this->symbol_name_slot_invalid (idx))
4456 continue;
4457
4458 const char *name = this->symbol_name_at (idx);
4459
4460 /* Add each name component to the name component table. */
4461 unsigned int previous_len = 0;
4462 for (unsigned int current_len = cp_find_first_component (name);
4463 name[current_len] != '\0';
4464 current_len += cp_find_first_component (name + current_len))
4465 {
4466 gdb_assert (name[current_len] == ':');
4467 this->name_components.push_back ({previous_len, idx});
4468 /* Skip the '::'. */
4469 current_len += 2;
4470 previous_len = current_len;
4471 }
4472 this->name_components.push_back ({previous_len, idx});
4473 }
4474
4475 /* Sort name_components elements by name. */
4476 auto name_comp_compare = [&] (const name_component &left,
4477 const name_component &right)
4478 {
4479 const char *left_qualified = this->symbol_name_at (left.idx);
4480 const char *right_qualified = this->symbol_name_at (right.idx);
4481
4482 const char *left_name = left_qualified + left.name_offset;
4483 const char *right_name = right_qualified + right.name_offset;
4484
4485 return name_cmp (left_name, right_name) < 0;
4486 };
4487
4488 std::sort (this->name_components.begin (),
4489 this->name_components.end (),
4490 name_comp_compare);
4491 }
4492
4493 /* Helper for dw2_expand_symtabs_matching that works with a
4494 mapped_index_base instead of the containing objfile. This is split
4495 to a separate function in order to be able to unit test the
4496 name_components matching using a mock mapped_index_base. For each
4497 symbol name that matches, calls MATCH_CALLBACK, passing it the
4498 symbol's index in the mapped_index_base symbol table. */
4499
4500 static void
4501 dw2_expand_symtabs_matching_symbol
4502 (mapped_index_base &index,
4503 const lookup_name_info &lookup_name_in,
4504 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4505 enum search_domain kind,
4506 gdb::function_view<void (offset_type)> match_callback)
4507 {
4508 lookup_name_info lookup_name_without_params
4509 = lookup_name_in.make_ignore_params ();
4510 gdb_index_symbol_name_matcher lookup_name_matcher
4511 (lookup_name_without_params);
4512
4513 /* Build the symbol name component sorted vector, if we haven't
4514 yet. */
4515 index.build_name_components ();
4516
4517 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4518
4519 /* Now for each symbol name in range, check to see if we have a name
4520 match, and if so, call the MATCH_CALLBACK callback. */
4521
4522 /* The same symbol may appear more than once in the range though.
4523 E.g., if we're looking for symbols that complete "w", and we have
4524 a symbol named "w1::w2", we'll find the two name components for
4525 that same symbol in the range. To be sure we only call the
4526 callback once per symbol, we first collect the symbol name
4527 indexes that matched in a temporary vector and ignore
4528 duplicates. */
4529 std::vector<offset_type> matches;
4530 matches.reserve (std::distance (bounds.first, bounds.second));
4531
4532 for (; bounds.first != bounds.second; ++bounds.first)
4533 {
4534 const char *qualified = index.symbol_name_at (bounds.first->idx);
4535
4536 if (!lookup_name_matcher.matches (qualified)
4537 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4538 continue;
4539
4540 matches.push_back (bounds.first->idx);
4541 }
4542
4543 std::sort (matches.begin (), matches.end ());
4544
4545 /* Finally call the callback, once per match. */
4546 ULONGEST prev = -1;
4547 for (offset_type idx : matches)
4548 {
4549 if (prev != idx)
4550 {
4551 match_callback (idx);
4552 prev = idx;
4553 }
4554 }
4555
4556 /* Above we use a type wider than idx's for 'prev', since 0 and
4557 (offset_type)-1 are both possible values. */
4558 static_assert (sizeof (prev) > sizeof (offset_type), "");
4559 }
4560
4561 #if GDB_SELF_TEST
4562
4563 namespace selftests { namespace dw2_expand_symtabs_matching {
4564
4565 /* A mock .gdb_index/.debug_names-like name index table, enough to
4566 exercise dw2_expand_symtabs_matching_symbol, which works with the
4567 mapped_index_base interface. Builds an index from the symbol list
4568 passed as parameter to the constructor. */
4569 class mock_mapped_index : public mapped_index_base
4570 {
4571 public:
4572 mock_mapped_index (gdb::array_view<const char *> symbols)
4573 : m_symbol_table (symbols)
4574 {}
4575
4576 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4577
4578 /* Return the number of names in the symbol table. */
4579 size_t symbol_name_count () const override
4580 {
4581 return m_symbol_table.size ();
4582 }
4583
4584 /* Get the name of the symbol at IDX in the symbol table. */
4585 const char *symbol_name_at (offset_type idx) const override
4586 {
4587 return m_symbol_table[idx];
4588 }
4589
4590 private:
4591 gdb::array_view<const char *> m_symbol_table;
4592 };
4593
4594 /* Convenience function that converts a NULL pointer to a "<null>"
4595 string, to pass to print routines. */
4596
4597 static const char *
4598 string_or_null (const char *str)
4599 {
4600 return str != NULL ? str : "<null>";
4601 }
4602
4603 /* Check if a lookup_name_info built from
4604 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4605 index. EXPECTED_LIST is the list of expected matches, in expected
4606 matching order. If no match expected, then an empty list is
4607 specified. Returns true on success. On failure prints a warning
4608 indicating the file:line that failed, and returns false. */
4609
4610 static bool
4611 check_match (const char *file, int line,
4612 mock_mapped_index &mock_index,
4613 const char *name, symbol_name_match_type match_type,
4614 bool completion_mode,
4615 std::initializer_list<const char *> expected_list)
4616 {
4617 lookup_name_info lookup_name (name, match_type, completion_mode);
4618
4619 bool matched = true;
4620
4621 auto mismatch = [&] (const char *expected_str,
4622 const char *got)
4623 {
4624 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4625 "expected=\"%s\", got=\"%s\"\n"),
4626 file, line,
4627 (match_type == symbol_name_match_type::FULL
4628 ? "FULL" : "WILD"),
4629 name, string_or_null (expected_str), string_or_null (got));
4630 matched = false;
4631 };
4632
4633 auto expected_it = expected_list.begin ();
4634 auto expected_end = expected_list.end ();
4635
4636 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4637 NULL, ALL_DOMAIN,
4638 [&] (offset_type idx)
4639 {
4640 const char *matched_name = mock_index.symbol_name_at (idx);
4641 const char *expected_str
4642 = expected_it == expected_end ? NULL : *expected_it++;
4643
4644 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4645 mismatch (expected_str, matched_name);
4646 });
4647
4648 const char *expected_str
4649 = expected_it == expected_end ? NULL : *expected_it++;
4650 if (expected_str != NULL)
4651 mismatch (expected_str, NULL);
4652
4653 return matched;
4654 }
4655
4656 /* The symbols added to the mock mapped_index for testing (in
4657 canonical form). */
4658 static const char *test_symbols[] = {
4659 "function",
4660 "std::bar",
4661 "std::zfunction",
4662 "std::zfunction2",
4663 "w1::w2",
4664 "ns::foo<char*>",
4665 "ns::foo<int>",
4666 "ns::foo<long>",
4667 "ns2::tmpl<int>::foo2",
4668 "(anonymous namespace)::A::B::C",
4669
4670 /* These are used to check that the increment-last-char in the
4671 matching algorithm for completion doesn't match "t1_fund" when
4672 completing "t1_func". */
4673 "t1_func",
4674 "t1_func1",
4675 "t1_fund",
4676 "t1_fund1",
4677
4678 /* A UTF-8 name with multi-byte sequences to make sure that
4679 cp-name-parser understands this as a single identifier ("função"
4680 is "function" in PT). */
4681 u8"u8função",
4682
4683 /* \377 (0xff) is Latin1 'ÿ'. */
4684 "yfunc\377",
4685
4686 /* \377 (0xff) is Latin1 'ÿ'. */
4687 "\377",
4688 "\377\377123",
4689
4690 /* A name with all sorts of complications. Starts with "z" to make
4691 it easier for the completion tests below. */
4692 #define Z_SYM_NAME \
4693 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4694 "::tuple<(anonymous namespace)::ui*, " \
4695 "std::default_delete<(anonymous namespace)::ui>, void>"
4696
4697 Z_SYM_NAME
4698 };
4699
4700 /* Returns true if the mapped_index_base::find_name_component_bounds
4701 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4702 in completion mode. */
4703
4704 static bool
4705 check_find_bounds_finds (mapped_index_base &index,
4706 const char *search_name,
4707 gdb::array_view<const char *> expected_syms)
4708 {
4709 lookup_name_info lookup_name (search_name,
4710 symbol_name_match_type::FULL, true);
4711
4712 auto bounds = index.find_name_components_bounds (lookup_name);
4713
4714 size_t distance = std::distance (bounds.first, bounds.second);
4715 if (distance != expected_syms.size ())
4716 return false;
4717
4718 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4719 {
4720 auto nc_elem = bounds.first + exp_elem;
4721 const char *qualified = index.symbol_name_at (nc_elem->idx);
4722 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4723 return false;
4724 }
4725
4726 return true;
4727 }
4728
4729 /* Test the lower-level mapped_index::find_name_component_bounds
4730 method. */
4731
4732 static void
4733 test_mapped_index_find_name_component_bounds ()
4734 {
4735 mock_mapped_index mock_index (test_symbols);
4736
4737 mock_index.build_name_components ();
4738
4739 /* Test the lower-level mapped_index::find_name_component_bounds
4740 method in completion mode. */
4741 {
4742 static const char *expected_syms[] = {
4743 "t1_func",
4744 "t1_func1",
4745 };
4746
4747 SELF_CHECK (check_find_bounds_finds (mock_index,
4748 "t1_func", expected_syms));
4749 }
4750
4751 /* Check that the increment-last-char in the name matching algorithm
4752 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4753 {
4754 static const char *expected_syms1[] = {
4755 "\377",
4756 "\377\377123",
4757 };
4758 SELF_CHECK (check_find_bounds_finds (mock_index,
4759 "\377", expected_syms1));
4760
4761 static const char *expected_syms2[] = {
4762 "\377\377123",
4763 };
4764 SELF_CHECK (check_find_bounds_finds (mock_index,
4765 "\377\377", expected_syms2));
4766 }
4767 }
4768
4769 /* Test dw2_expand_symtabs_matching_symbol. */
4770
4771 static void
4772 test_dw2_expand_symtabs_matching_symbol ()
4773 {
4774 mock_mapped_index mock_index (test_symbols);
4775
4776 /* We let all tests run until the end even if some fails, for debug
4777 convenience. */
4778 bool any_mismatch = false;
4779
4780 /* Create the expected symbols list (an initializer_list). Needed
4781 because lists have commas, and we need to pass them to CHECK,
4782 which is a macro. */
4783 #define EXPECT(...) { __VA_ARGS__ }
4784
4785 /* Wrapper for check_match that passes down the current
4786 __FILE__/__LINE__. */
4787 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4788 any_mismatch |= !check_match (__FILE__, __LINE__, \
4789 mock_index, \
4790 NAME, MATCH_TYPE, COMPLETION_MODE, \
4791 EXPECTED_LIST)
4792
4793 /* Identity checks. */
4794 for (const char *sym : test_symbols)
4795 {
4796 /* Should be able to match all existing symbols. */
4797 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4798 EXPECT (sym));
4799
4800 /* Should be able to match all existing symbols with
4801 parameters. */
4802 std::string with_params = std::string (sym) + "(int)";
4803 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4804 EXPECT (sym));
4805
4806 /* Should be able to match all existing symbols with
4807 parameters and qualifiers. */
4808 with_params = std::string (sym) + " ( int ) const";
4809 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4810 EXPECT (sym));
4811
4812 /* This should really find sym, but cp-name-parser.y doesn't
4813 know about lvalue/rvalue qualifiers yet. */
4814 with_params = std::string (sym) + " ( int ) &&";
4815 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4816 {});
4817 }
4818
4819 /* Check that the name matching algorithm for completion doesn't get
4820 confused with Latin1 'ÿ' / 0xff. */
4821 {
4822 static const char str[] = "\377";
4823 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4824 EXPECT ("\377", "\377\377123"));
4825 }
4826
4827 /* Check that the increment-last-char in the matching algorithm for
4828 completion doesn't match "t1_fund" when completing "t1_func". */
4829 {
4830 static const char str[] = "t1_func";
4831 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4832 EXPECT ("t1_func", "t1_func1"));
4833 }
4834
4835 /* Check that completion mode works at each prefix of the expected
4836 symbol name. */
4837 {
4838 static const char str[] = "function(int)";
4839 size_t len = strlen (str);
4840 std::string lookup;
4841
4842 for (size_t i = 1; i < len; i++)
4843 {
4844 lookup.assign (str, i);
4845 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4846 EXPECT ("function"));
4847 }
4848 }
4849
4850 /* While "w" is a prefix of both components, the match function
4851 should still only be called once. */
4852 {
4853 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4854 EXPECT ("w1::w2"));
4855 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4856 EXPECT ("w1::w2"));
4857 }
4858
4859 /* Same, with a "complicated" symbol. */
4860 {
4861 static const char str[] = Z_SYM_NAME;
4862 size_t len = strlen (str);
4863 std::string lookup;
4864
4865 for (size_t i = 1; i < len; i++)
4866 {
4867 lookup.assign (str, i);
4868 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4869 EXPECT (Z_SYM_NAME));
4870 }
4871 }
4872
4873 /* In FULL mode, an incomplete symbol doesn't match. */
4874 {
4875 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4876 {});
4877 }
4878
4879 /* A complete symbol with parameters matches any overload, since the
4880 index has no overload info. */
4881 {
4882 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4883 EXPECT ("std::zfunction", "std::zfunction2"));
4884 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4885 EXPECT ("std::zfunction", "std::zfunction2"));
4886 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4887 EXPECT ("std::zfunction", "std::zfunction2"));
4888 }
4889
4890 /* Check that whitespace is ignored appropriately. A symbol with a
4891 template argument list. */
4892 {
4893 static const char expected[] = "ns::foo<int>";
4894 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4895 EXPECT (expected));
4896 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4897 EXPECT (expected));
4898 }
4899
4900 /* Check that whitespace is ignored appropriately. A symbol with a
4901 template argument list that includes a pointer. */
4902 {
4903 static const char expected[] = "ns::foo<char*>";
4904 /* Try both completion and non-completion modes. */
4905 static const bool completion_mode[2] = {false, true};
4906 for (size_t i = 0; i < 2; i++)
4907 {
4908 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4909 completion_mode[i], EXPECT (expected));
4910 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4911 completion_mode[i], EXPECT (expected));
4912
4913 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4914 completion_mode[i], EXPECT (expected));
4915 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4916 completion_mode[i], EXPECT (expected));
4917 }
4918 }
4919
4920 {
4921 /* Check method qualifiers are ignored. */
4922 static const char expected[] = "ns::foo<char*>";
4923 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4924 symbol_name_match_type::FULL, true, EXPECT (expected));
4925 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4926 symbol_name_match_type::FULL, true, EXPECT (expected));
4927 CHECK_MATCH ("foo < char * > ( int ) const",
4928 symbol_name_match_type::WILD, true, EXPECT (expected));
4929 CHECK_MATCH ("foo < char * > ( int ) &&",
4930 symbol_name_match_type::WILD, true, EXPECT (expected));
4931 }
4932
4933 /* Test lookup names that don't match anything. */
4934 {
4935 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4936 {});
4937
4938 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4939 {});
4940 }
4941
4942 /* Some wild matching tests, exercising "(anonymous namespace)",
4943 which should not be confused with a parameter list. */
4944 {
4945 static const char *syms[] = {
4946 "A::B::C",
4947 "B::C",
4948 "C",
4949 "A :: B :: C ( int )",
4950 "B :: C ( int )",
4951 "C ( int )",
4952 };
4953
4954 for (const char *s : syms)
4955 {
4956 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4957 EXPECT ("(anonymous namespace)::A::B::C"));
4958 }
4959 }
4960
4961 {
4962 static const char expected[] = "ns2::tmpl<int>::foo2";
4963 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4964 EXPECT (expected));
4965 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4966 EXPECT (expected));
4967 }
4968
4969 SELF_CHECK (!any_mismatch);
4970
4971 #undef EXPECT
4972 #undef CHECK_MATCH
4973 }
4974
4975 static void
4976 run_test ()
4977 {
4978 test_mapped_index_find_name_component_bounds ();
4979 test_dw2_expand_symtabs_matching_symbol ();
4980 }
4981
4982 }} // namespace selftests::dw2_expand_symtabs_matching
4983
4984 #endif /* GDB_SELF_TEST */
4985
4986 /* If FILE_MATCHER is NULL or if PER_CU has
4987 dwarf2_per_cu_quick_data::MARK set (see
4988 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4989 EXPANSION_NOTIFY on it. */
4990
4991 static void
4992 dw2_expand_symtabs_matching_one
4993 (struct dwarf2_per_cu_data *per_cu,
4994 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4995 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4996 {
4997 if (file_matcher == NULL || per_cu->v.quick->mark)
4998 {
4999 bool symtab_was_null
5000 = (per_cu->v.quick->compunit_symtab == NULL);
5001
5002 dw2_instantiate_symtab (per_cu, false);
5003
5004 if (expansion_notify != NULL
5005 && symtab_was_null
5006 && per_cu->v.quick->compunit_symtab != NULL)
5007 expansion_notify (per_cu->v.quick->compunit_symtab);
5008 }
5009 }
5010
5011 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5012 matched, to expand corresponding CUs that were marked. IDX is the
5013 index of the symbol name that matched. */
5014
5015 static void
5016 dw2_expand_marked_cus
5017 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5018 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5019 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5020 search_domain kind)
5021 {
5022 offset_type *vec, vec_len, vec_idx;
5023 bool global_seen = false;
5024 mapped_index &index = *dwarf2_per_objfile->index_table;
5025
5026 vec = (offset_type *) (index.constant_pool
5027 + MAYBE_SWAP (index.symbol_table[idx].vec));
5028 vec_len = MAYBE_SWAP (vec[0]);
5029 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5030 {
5031 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5032 /* This value is only valid for index versions >= 7. */
5033 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5034 gdb_index_symbol_kind symbol_kind =
5035 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5036 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5037 /* Only check the symbol attributes if they're present.
5038 Indices prior to version 7 don't record them,
5039 and indices >= 7 may elide them for certain symbols
5040 (gold does this). */
5041 int attrs_valid =
5042 (index.version >= 7
5043 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5044
5045 /* Work around gold/15646. */
5046 if (attrs_valid)
5047 {
5048 if (!is_static && global_seen)
5049 continue;
5050 if (!is_static)
5051 global_seen = true;
5052 }
5053
5054 /* Only check the symbol's kind if it has one. */
5055 if (attrs_valid)
5056 {
5057 switch (kind)
5058 {
5059 case VARIABLES_DOMAIN:
5060 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5061 continue;
5062 break;
5063 case FUNCTIONS_DOMAIN:
5064 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5065 continue;
5066 break;
5067 case TYPES_DOMAIN:
5068 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5069 continue;
5070 break;
5071 default:
5072 break;
5073 }
5074 }
5075
5076 /* Don't crash on bad data. */
5077 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5078 + dwarf2_per_objfile->all_type_units.size ()))
5079 {
5080 complaint (_(".gdb_index entry has bad CU index"
5081 " [in module %s]"),
5082 objfile_name (dwarf2_per_objfile->objfile));
5083 continue;
5084 }
5085
5086 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5087 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5088 expansion_notify);
5089 }
5090 }
5091
5092 /* If FILE_MATCHER is non-NULL, set all the
5093 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5094 that match FILE_MATCHER. */
5095
5096 static void
5097 dw_expand_symtabs_matching_file_matcher
5098 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5099 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5100 {
5101 if (file_matcher == NULL)
5102 return;
5103
5104 objfile *const objfile = dwarf2_per_objfile->objfile;
5105
5106 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5107 htab_eq_pointer,
5108 NULL, xcalloc, xfree));
5109 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5110 htab_eq_pointer,
5111 NULL, xcalloc, xfree));
5112
5113 /* The rule is CUs specify all the files, including those used by
5114 any TU, so there's no need to scan TUs here. */
5115
5116 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5117 {
5118 QUIT;
5119
5120 per_cu->v.quick->mark = 0;
5121
5122 /* We only need to look at symtabs not already expanded. */
5123 if (per_cu->v.quick->compunit_symtab)
5124 continue;
5125
5126 quick_file_names *file_data = dw2_get_file_names (per_cu);
5127 if (file_data == NULL)
5128 continue;
5129
5130 if (htab_find (visited_not_found.get (), file_data) != NULL)
5131 continue;
5132 else if (htab_find (visited_found.get (), file_data) != NULL)
5133 {
5134 per_cu->v.quick->mark = 1;
5135 continue;
5136 }
5137
5138 for (int j = 0; j < file_data->num_file_names; ++j)
5139 {
5140 const char *this_real_name;
5141
5142 if (file_matcher (file_data->file_names[j], false))
5143 {
5144 per_cu->v.quick->mark = 1;
5145 break;
5146 }
5147
5148 /* Before we invoke realpath, which can get expensive when many
5149 files are involved, do a quick comparison of the basenames. */
5150 if (!basenames_may_differ
5151 && !file_matcher (lbasename (file_data->file_names[j]),
5152 true))
5153 continue;
5154
5155 this_real_name = dw2_get_real_path (objfile, file_data, j);
5156 if (file_matcher (this_real_name, false))
5157 {
5158 per_cu->v.quick->mark = 1;
5159 break;
5160 }
5161 }
5162
5163 void **slot = htab_find_slot (per_cu->v.quick->mark
5164 ? visited_found.get ()
5165 : visited_not_found.get (),
5166 file_data, INSERT);
5167 *slot = file_data;
5168 }
5169 }
5170
5171 static void
5172 dw2_expand_symtabs_matching
5173 (struct objfile *objfile,
5174 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5175 const lookup_name_info &lookup_name,
5176 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5177 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5178 enum search_domain kind)
5179 {
5180 struct dwarf2_per_objfile *dwarf2_per_objfile
5181 = get_dwarf2_per_objfile (objfile);
5182
5183 /* index_table is NULL if OBJF_READNOW. */
5184 if (!dwarf2_per_objfile->index_table)
5185 return;
5186
5187 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5188
5189 mapped_index &index = *dwarf2_per_objfile->index_table;
5190
5191 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5192 symbol_matcher,
5193 kind, [&] (offset_type idx)
5194 {
5195 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5196 expansion_notify, kind);
5197 });
5198 }
5199
5200 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5201 symtab. */
5202
5203 static struct compunit_symtab *
5204 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5205 CORE_ADDR pc)
5206 {
5207 int i;
5208
5209 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5210 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5211 return cust;
5212
5213 if (cust->includes == NULL)
5214 return NULL;
5215
5216 for (i = 0; cust->includes[i]; ++i)
5217 {
5218 struct compunit_symtab *s = cust->includes[i];
5219
5220 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5221 if (s != NULL)
5222 return s;
5223 }
5224
5225 return NULL;
5226 }
5227
5228 static struct compunit_symtab *
5229 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5230 struct bound_minimal_symbol msymbol,
5231 CORE_ADDR pc,
5232 struct obj_section *section,
5233 int warn_if_readin)
5234 {
5235 struct dwarf2_per_cu_data *data;
5236 struct compunit_symtab *result;
5237
5238 if (!objfile->partial_symtabs->psymtabs_addrmap)
5239 return NULL;
5240
5241 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5242 SECT_OFF_TEXT (objfile));
5243 data = (struct dwarf2_per_cu_data *) addrmap_find
5244 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
5245 if (!data)
5246 return NULL;
5247
5248 if (warn_if_readin && data->v.quick->compunit_symtab)
5249 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5250 paddress (get_objfile_arch (objfile), pc));
5251
5252 result
5253 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5254 false),
5255 pc);
5256 gdb_assert (result != NULL);
5257 return result;
5258 }
5259
5260 static void
5261 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5262 void *data, int need_fullname)
5263 {
5264 struct dwarf2_per_objfile *dwarf2_per_objfile
5265 = get_dwarf2_per_objfile (objfile);
5266
5267 if (!dwarf2_per_objfile->filenames_cache)
5268 {
5269 dwarf2_per_objfile->filenames_cache.emplace ();
5270
5271 htab_up visited (htab_create_alloc (10,
5272 htab_hash_pointer, htab_eq_pointer,
5273 NULL, xcalloc, xfree));
5274
5275 /* The rule is CUs specify all the files, including those used
5276 by any TU, so there's no need to scan TUs here. We can
5277 ignore file names coming from already-expanded CUs. */
5278
5279 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5280 {
5281 if (per_cu->v.quick->compunit_symtab)
5282 {
5283 void **slot = htab_find_slot (visited.get (),
5284 per_cu->v.quick->file_names,
5285 INSERT);
5286
5287 *slot = per_cu->v.quick->file_names;
5288 }
5289 }
5290
5291 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5292 {
5293 /* We only need to look at symtabs not already expanded. */
5294 if (per_cu->v.quick->compunit_symtab)
5295 continue;
5296
5297 quick_file_names *file_data = dw2_get_file_names (per_cu);
5298 if (file_data == NULL)
5299 continue;
5300
5301 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5302 if (*slot)
5303 {
5304 /* Already visited. */
5305 continue;
5306 }
5307 *slot = file_data;
5308
5309 for (int j = 0; j < file_data->num_file_names; ++j)
5310 {
5311 const char *filename = file_data->file_names[j];
5312 dwarf2_per_objfile->filenames_cache->seen (filename);
5313 }
5314 }
5315 }
5316
5317 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5318 {
5319 gdb::unique_xmalloc_ptr<char> this_real_name;
5320
5321 if (need_fullname)
5322 this_real_name = gdb_realpath (filename);
5323 (*fun) (filename, this_real_name.get (), data);
5324 });
5325 }
5326
5327 static int
5328 dw2_has_symbols (struct objfile *objfile)
5329 {
5330 return 1;
5331 }
5332
5333 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5334 {
5335 dw2_has_symbols,
5336 dw2_find_last_source_symtab,
5337 dw2_forget_cached_source_info,
5338 dw2_map_symtabs_matching_filename,
5339 dw2_lookup_symbol,
5340 dw2_print_stats,
5341 dw2_dump,
5342 dw2_expand_symtabs_for_function,
5343 dw2_expand_all_symtabs,
5344 dw2_expand_symtabs_with_fullname,
5345 dw2_map_matching_symbols,
5346 dw2_expand_symtabs_matching,
5347 dw2_find_pc_sect_compunit_symtab,
5348 NULL,
5349 dw2_map_symbol_filenames
5350 };
5351
5352 /* DWARF-5 debug_names reader. */
5353
5354 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5355 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5356
5357 /* A helper function that reads the .debug_names section in SECTION
5358 and fills in MAP. FILENAME is the name of the file containing the
5359 section; it is used for error reporting.
5360
5361 Returns true if all went well, false otherwise. */
5362
5363 static bool
5364 read_debug_names_from_section (struct objfile *objfile,
5365 const char *filename,
5366 struct dwarf2_section_info *section,
5367 mapped_debug_names &map)
5368 {
5369 if (dwarf2_section_empty_p (section))
5370 return false;
5371
5372 /* Older elfutils strip versions could keep the section in the main
5373 executable while splitting it for the separate debug info file. */
5374 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5375 return false;
5376
5377 dwarf2_read_section (objfile, section);
5378
5379 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5380
5381 const gdb_byte *addr = section->buffer;
5382
5383 bfd *const abfd = get_section_bfd_owner (section);
5384
5385 unsigned int bytes_read;
5386 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5387 addr += bytes_read;
5388
5389 map.dwarf5_is_dwarf64 = bytes_read != 4;
5390 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5391 if (bytes_read + length != section->size)
5392 {
5393 /* There may be multiple per-CU indices. */
5394 warning (_("Section .debug_names in %s length %s does not match "
5395 "section length %s, ignoring .debug_names."),
5396 filename, plongest (bytes_read + length),
5397 pulongest (section->size));
5398 return false;
5399 }
5400
5401 /* The version number. */
5402 uint16_t version = read_2_bytes (abfd, addr);
5403 addr += 2;
5404 if (version != 5)
5405 {
5406 warning (_("Section .debug_names in %s has unsupported version %d, "
5407 "ignoring .debug_names."),
5408 filename, version);
5409 return false;
5410 }
5411
5412 /* Padding. */
5413 uint16_t padding = read_2_bytes (abfd, addr);
5414 addr += 2;
5415 if (padding != 0)
5416 {
5417 warning (_("Section .debug_names in %s has unsupported padding %d, "
5418 "ignoring .debug_names."),
5419 filename, padding);
5420 return false;
5421 }
5422
5423 /* comp_unit_count - The number of CUs in the CU list. */
5424 map.cu_count = read_4_bytes (abfd, addr);
5425 addr += 4;
5426
5427 /* local_type_unit_count - The number of TUs in the local TU
5428 list. */
5429 map.tu_count = read_4_bytes (abfd, addr);
5430 addr += 4;
5431
5432 /* foreign_type_unit_count - The number of TUs in the foreign TU
5433 list. */
5434 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5435 addr += 4;
5436 if (foreign_tu_count != 0)
5437 {
5438 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5439 "ignoring .debug_names."),
5440 filename, static_cast<unsigned long> (foreign_tu_count));
5441 return false;
5442 }
5443
5444 /* bucket_count - The number of hash buckets in the hash lookup
5445 table. */
5446 map.bucket_count = read_4_bytes (abfd, addr);
5447 addr += 4;
5448
5449 /* name_count - The number of unique names in the index. */
5450 map.name_count = read_4_bytes (abfd, addr);
5451 addr += 4;
5452
5453 /* abbrev_table_size - The size in bytes of the abbreviations
5454 table. */
5455 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5456 addr += 4;
5457
5458 /* augmentation_string_size - The size in bytes of the augmentation
5459 string. This value is rounded up to a multiple of 4. */
5460 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5461 addr += 4;
5462 map.augmentation_is_gdb = ((augmentation_string_size
5463 == sizeof (dwarf5_augmentation))
5464 && memcmp (addr, dwarf5_augmentation,
5465 sizeof (dwarf5_augmentation)) == 0);
5466 augmentation_string_size += (-augmentation_string_size) & 3;
5467 addr += augmentation_string_size;
5468
5469 /* List of CUs */
5470 map.cu_table_reordered = addr;
5471 addr += map.cu_count * map.offset_size;
5472
5473 /* List of Local TUs */
5474 map.tu_table_reordered = addr;
5475 addr += map.tu_count * map.offset_size;
5476
5477 /* Hash Lookup Table */
5478 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5479 addr += map.bucket_count * 4;
5480 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5481 addr += map.name_count * 4;
5482
5483 /* Name Table */
5484 map.name_table_string_offs_reordered = addr;
5485 addr += map.name_count * map.offset_size;
5486 map.name_table_entry_offs_reordered = addr;
5487 addr += map.name_count * map.offset_size;
5488
5489 const gdb_byte *abbrev_table_start = addr;
5490 for (;;)
5491 {
5492 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5493 addr += bytes_read;
5494 if (index_num == 0)
5495 break;
5496
5497 const auto insertpair
5498 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5499 if (!insertpair.second)
5500 {
5501 warning (_("Section .debug_names in %s has duplicate index %s, "
5502 "ignoring .debug_names."),
5503 filename, pulongest (index_num));
5504 return false;
5505 }
5506 mapped_debug_names::index_val &indexval = insertpair.first->second;
5507 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5508 addr += bytes_read;
5509
5510 for (;;)
5511 {
5512 mapped_debug_names::index_val::attr attr;
5513 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5514 addr += bytes_read;
5515 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5516 addr += bytes_read;
5517 if (attr.form == DW_FORM_implicit_const)
5518 {
5519 attr.implicit_const = read_signed_leb128 (abfd, addr,
5520 &bytes_read);
5521 addr += bytes_read;
5522 }
5523 if (attr.dw_idx == 0 && attr.form == 0)
5524 break;
5525 indexval.attr_vec.push_back (std::move (attr));
5526 }
5527 }
5528 if (addr != abbrev_table_start + abbrev_table_size)
5529 {
5530 warning (_("Section .debug_names in %s has abbreviation_table "
5531 "of size %s vs. written as %u, ignoring .debug_names."),
5532 filename, plongest (addr - abbrev_table_start),
5533 abbrev_table_size);
5534 return false;
5535 }
5536 map.entry_pool = addr;
5537
5538 return true;
5539 }
5540
5541 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5542 list. */
5543
5544 static void
5545 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5546 const mapped_debug_names &map,
5547 dwarf2_section_info &section,
5548 bool is_dwz)
5549 {
5550 sect_offset sect_off_prev;
5551 for (uint32_t i = 0; i <= map.cu_count; ++i)
5552 {
5553 sect_offset sect_off_next;
5554 if (i < map.cu_count)
5555 {
5556 sect_off_next
5557 = (sect_offset) (extract_unsigned_integer
5558 (map.cu_table_reordered + i * map.offset_size,
5559 map.offset_size,
5560 map.dwarf5_byte_order));
5561 }
5562 else
5563 sect_off_next = (sect_offset) section.size;
5564 if (i >= 1)
5565 {
5566 const ULONGEST length = sect_off_next - sect_off_prev;
5567 dwarf2_per_cu_data *per_cu
5568 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5569 sect_off_prev, length);
5570 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5571 }
5572 sect_off_prev = sect_off_next;
5573 }
5574 }
5575
5576 /* Read the CU list from the mapped index, and use it to create all
5577 the CU objects for this dwarf2_per_objfile. */
5578
5579 static void
5580 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5581 const mapped_debug_names &map,
5582 const mapped_debug_names &dwz_map)
5583 {
5584 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5585 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5586
5587 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5588 dwarf2_per_objfile->info,
5589 false /* is_dwz */);
5590
5591 if (dwz_map.cu_count == 0)
5592 return;
5593
5594 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5595 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5596 true /* is_dwz */);
5597 }
5598
5599 /* Read .debug_names. If everything went ok, initialize the "quick"
5600 elements of all the CUs and return true. Otherwise, return false. */
5601
5602 static bool
5603 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5604 {
5605 std::unique_ptr<mapped_debug_names> map
5606 (new mapped_debug_names (dwarf2_per_objfile));
5607 mapped_debug_names dwz_map (dwarf2_per_objfile);
5608 struct objfile *objfile = dwarf2_per_objfile->objfile;
5609
5610 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5611 &dwarf2_per_objfile->debug_names,
5612 *map))
5613 return false;
5614
5615 /* Don't use the index if it's empty. */
5616 if (map->name_count == 0)
5617 return false;
5618
5619 /* If there is a .dwz file, read it so we can get its CU list as
5620 well. */
5621 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5622 if (dwz != NULL)
5623 {
5624 if (!read_debug_names_from_section (objfile,
5625 bfd_get_filename (dwz->dwz_bfd),
5626 &dwz->debug_names, dwz_map))
5627 {
5628 warning (_("could not read '.debug_names' section from %s; skipping"),
5629 bfd_get_filename (dwz->dwz_bfd));
5630 return false;
5631 }
5632 }
5633
5634 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5635
5636 if (map->tu_count != 0)
5637 {
5638 /* We can only handle a single .debug_types when we have an
5639 index. */
5640 if (dwarf2_per_objfile->types.size () != 1)
5641 return false;
5642
5643 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5644
5645 create_signatured_type_table_from_debug_names
5646 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5647 }
5648
5649 create_addrmap_from_aranges (dwarf2_per_objfile,
5650 &dwarf2_per_objfile->debug_aranges);
5651
5652 dwarf2_per_objfile->debug_names_table = std::move (map);
5653 dwarf2_per_objfile->using_index = 1;
5654 dwarf2_per_objfile->quick_file_names_table =
5655 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5656
5657 return true;
5658 }
5659
5660 /* Type used to manage iterating over all CUs looking for a symbol for
5661 .debug_names. */
5662
5663 class dw2_debug_names_iterator
5664 {
5665 public:
5666 dw2_debug_names_iterator (const mapped_debug_names &map,
5667 gdb::optional<block_enum> block_index,
5668 domain_enum domain,
5669 const char *name)
5670 : m_map (map), m_block_index (block_index), m_domain (domain),
5671 m_addr (find_vec_in_debug_names (map, name))
5672 {}
5673
5674 dw2_debug_names_iterator (const mapped_debug_names &map,
5675 search_domain search, uint32_t namei)
5676 : m_map (map),
5677 m_search (search),
5678 m_addr (find_vec_in_debug_names (map, namei))
5679 {}
5680
5681 /* Return the next matching CU or NULL if there are no more. */
5682 dwarf2_per_cu_data *next ();
5683
5684 private:
5685 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5686 const char *name);
5687 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5688 uint32_t namei);
5689
5690 /* The internalized form of .debug_names. */
5691 const mapped_debug_names &m_map;
5692
5693 /* If set, only look for symbols that match that block. Valid values are
5694 GLOBAL_BLOCK and STATIC_BLOCK. */
5695 const gdb::optional<block_enum> m_block_index;
5696
5697 /* The kind of symbol we're looking for. */
5698 const domain_enum m_domain = UNDEF_DOMAIN;
5699 const search_domain m_search = ALL_DOMAIN;
5700
5701 /* The list of CUs from the index entry of the symbol, or NULL if
5702 not found. */
5703 const gdb_byte *m_addr;
5704 };
5705
5706 const char *
5707 mapped_debug_names::namei_to_name (uint32_t namei) const
5708 {
5709 const ULONGEST namei_string_offs
5710 = extract_unsigned_integer ((name_table_string_offs_reordered
5711 + namei * offset_size),
5712 offset_size,
5713 dwarf5_byte_order);
5714 return read_indirect_string_at_offset
5715 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5716 }
5717
5718 /* Find a slot in .debug_names for the object named NAME. If NAME is
5719 found, return pointer to its pool data. If NAME cannot be found,
5720 return NULL. */
5721
5722 const gdb_byte *
5723 dw2_debug_names_iterator::find_vec_in_debug_names
5724 (const mapped_debug_names &map, const char *name)
5725 {
5726 int (*cmp) (const char *, const char *);
5727
5728 gdb::unique_xmalloc_ptr<char> without_params;
5729 if (current_language->la_language == language_cplus
5730 || current_language->la_language == language_fortran
5731 || current_language->la_language == language_d)
5732 {
5733 /* NAME is already canonical. Drop any qualifiers as
5734 .debug_names does not contain any. */
5735
5736 if (strchr (name, '(') != NULL)
5737 {
5738 without_params = cp_remove_params (name);
5739 if (without_params != NULL)
5740 name = without_params.get ();
5741 }
5742 }
5743
5744 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5745
5746 const uint32_t full_hash = dwarf5_djb_hash (name);
5747 uint32_t namei
5748 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5749 (map.bucket_table_reordered
5750 + (full_hash % map.bucket_count)), 4,
5751 map.dwarf5_byte_order);
5752 if (namei == 0)
5753 return NULL;
5754 --namei;
5755 if (namei >= map.name_count)
5756 {
5757 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5758 "[in module %s]"),
5759 namei, map.name_count,
5760 objfile_name (map.dwarf2_per_objfile->objfile));
5761 return NULL;
5762 }
5763
5764 for (;;)
5765 {
5766 const uint32_t namei_full_hash
5767 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5768 (map.hash_table_reordered + namei), 4,
5769 map.dwarf5_byte_order);
5770 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5771 return NULL;
5772
5773 if (full_hash == namei_full_hash)
5774 {
5775 const char *const namei_string = map.namei_to_name (namei);
5776
5777 #if 0 /* An expensive sanity check. */
5778 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5779 {
5780 complaint (_("Wrong .debug_names hash for string at index %u "
5781 "[in module %s]"),
5782 namei, objfile_name (dwarf2_per_objfile->objfile));
5783 return NULL;
5784 }
5785 #endif
5786
5787 if (cmp (namei_string, name) == 0)
5788 {
5789 const ULONGEST namei_entry_offs
5790 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5791 + namei * map.offset_size),
5792 map.offset_size, map.dwarf5_byte_order);
5793 return map.entry_pool + namei_entry_offs;
5794 }
5795 }
5796
5797 ++namei;
5798 if (namei >= map.name_count)
5799 return NULL;
5800 }
5801 }
5802
5803 const gdb_byte *
5804 dw2_debug_names_iterator::find_vec_in_debug_names
5805 (const mapped_debug_names &map, uint32_t namei)
5806 {
5807 if (namei >= map.name_count)
5808 {
5809 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5810 "[in module %s]"),
5811 namei, map.name_count,
5812 objfile_name (map.dwarf2_per_objfile->objfile));
5813 return NULL;
5814 }
5815
5816 const ULONGEST namei_entry_offs
5817 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5818 + namei * map.offset_size),
5819 map.offset_size, map.dwarf5_byte_order);
5820 return map.entry_pool + namei_entry_offs;
5821 }
5822
5823 /* See dw2_debug_names_iterator. */
5824
5825 dwarf2_per_cu_data *
5826 dw2_debug_names_iterator::next ()
5827 {
5828 if (m_addr == NULL)
5829 return NULL;
5830
5831 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5832 struct objfile *objfile = dwarf2_per_objfile->objfile;
5833 bfd *const abfd = objfile->obfd;
5834
5835 again:
5836
5837 unsigned int bytes_read;
5838 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5839 m_addr += bytes_read;
5840 if (abbrev == 0)
5841 return NULL;
5842
5843 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5844 if (indexval_it == m_map.abbrev_map.cend ())
5845 {
5846 complaint (_("Wrong .debug_names undefined abbrev code %s "
5847 "[in module %s]"),
5848 pulongest (abbrev), objfile_name (objfile));
5849 return NULL;
5850 }
5851 const mapped_debug_names::index_val &indexval = indexval_it->second;
5852 enum class symbol_linkage {
5853 unknown,
5854 static_,
5855 extern_,
5856 } symbol_linkage_ = symbol_linkage::unknown;
5857 dwarf2_per_cu_data *per_cu = NULL;
5858 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5859 {
5860 ULONGEST ull;
5861 switch (attr.form)
5862 {
5863 case DW_FORM_implicit_const:
5864 ull = attr.implicit_const;
5865 break;
5866 case DW_FORM_flag_present:
5867 ull = 1;
5868 break;
5869 case DW_FORM_udata:
5870 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5871 m_addr += bytes_read;
5872 break;
5873 default:
5874 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5875 dwarf_form_name (attr.form),
5876 objfile_name (objfile));
5877 return NULL;
5878 }
5879 switch (attr.dw_idx)
5880 {
5881 case DW_IDX_compile_unit:
5882 /* Don't crash on bad data. */
5883 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5884 {
5885 complaint (_(".debug_names entry has bad CU index %s"
5886 " [in module %s]"),
5887 pulongest (ull),
5888 objfile_name (dwarf2_per_objfile->objfile));
5889 continue;
5890 }
5891 per_cu = dwarf2_per_objfile->get_cutu (ull);
5892 break;
5893 case DW_IDX_type_unit:
5894 /* Don't crash on bad data. */
5895 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5896 {
5897 complaint (_(".debug_names entry has bad TU index %s"
5898 " [in module %s]"),
5899 pulongest (ull),
5900 objfile_name (dwarf2_per_objfile->objfile));
5901 continue;
5902 }
5903 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5904 break;
5905 case DW_IDX_GNU_internal:
5906 if (!m_map.augmentation_is_gdb)
5907 break;
5908 symbol_linkage_ = symbol_linkage::static_;
5909 break;
5910 case DW_IDX_GNU_external:
5911 if (!m_map.augmentation_is_gdb)
5912 break;
5913 symbol_linkage_ = symbol_linkage::extern_;
5914 break;
5915 }
5916 }
5917
5918 /* Skip if already read in. */
5919 if (per_cu->v.quick->compunit_symtab)
5920 goto again;
5921
5922 /* Check static vs global. */
5923 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5924 {
5925 const bool want_static = *m_block_index == STATIC_BLOCK;
5926 const bool symbol_is_static =
5927 symbol_linkage_ == symbol_linkage::static_;
5928 if (want_static != symbol_is_static)
5929 goto again;
5930 }
5931
5932 /* Match dw2_symtab_iter_next, symbol_kind
5933 and debug_names::psymbol_tag. */
5934 switch (m_domain)
5935 {
5936 case VAR_DOMAIN:
5937 switch (indexval.dwarf_tag)
5938 {
5939 case DW_TAG_variable:
5940 case DW_TAG_subprogram:
5941 /* Some types are also in VAR_DOMAIN. */
5942 case DW_TAG_typedef:
5943 case DW_TAG_structure_type:
5944 break;
5945 default:
5946 goto again;
5947 }
5948 break;
5949 case STRUCT_DOMAIN:
5950 switch (indexval.dwarf_tag)
5951 {
5952 case DW_TAG_typedef:
5953 case DW_TAG_structure_type:
5954 break;
5955 default:
5956 goto again;
5957 }
5958 break;
5959 case LABEL_DOMAIN:
5960 switch (indexval.dwarf_tag)
5961 {
5962 case 0:
5963 case DW_TAG_variable:
5964 break;
5965 default:
5966 goto again;
5967 }
5968 break;
5969 default:
5970 break;
5971 }
5972
5973 /* Match dw2_expand_symtabs_matching, symbol_kind and
5974 debug_names::psymbol_tag. */
5975 switch (m_search)
5976 {
5977 case VARIABLES_DOMAIN:
5978 switch (indexval.dwarf_tag)
5979 {
5980 case DW_TAG_variable:
5981 break;
5982 default:
5983 goto again;
5984 }
5985 break;
5986 case FUNCTIONS_DOMAIN:
5987 switch (indexval.dwarf_tag)
5988 {
5989 case DW_TAG_subprogram:
5990 break;
5991 default:
5992 goto again;
5993 }
5994 break;
5995 case TYPES_DOMAIN:
5996 switch (indexval.dwarf_tag)
5997 {
5998 case DW_TAG_typedef:
5999 case DW_TAG_structure_type:
6000 break;
6001 default:
6002 goto again;
6003 }
6004 break;
6005 default:
6006 break;
6007 }
6008
6009 return per_cu;
6010 }
6011
6012 static struct compunit_symtab *
6013 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
6014 const char *name, domain_enum domain)
6015 {
6016 struct dwarf2_per_objfile *dwarf2_per_objfile
6017 = get_dwarf2_per_objfile (objfile);
6018
6019 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6020 if (!mapp)
6021 {
6022 /* index is NULL if OBJF_READNOW. */
6023 return NULL;
6024 }
6025 const auto &map = *mapp;
6026
6027 dw2_debug_names_iterator iter (map, block_index, domain, name);
6028
6029 struct compunit_symtab *stab_best = NULL;
6030 struct dwarf2_per_cu_data *per_cu;
6031 while ((per_cu = iter.next ()) != NULL)
6032 {
6033 struct symbol *sym, *with_opaque = NULL;
6034 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
6035 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6036 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6037
6038 sym = block_find_symbol (block, name, domain,
6039 block_find_non_opaque_type_preferred,
6040 &with_opaque);
6041
6042 /* Some caution must be observed with overloaded functions and
6043 methods, since the index will not contain any overload
6044 information (but NAME might contain it). */
6045
6046 if (sym != NULL
6047 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6048 return stab;
6049 if (with_opaque != NULL
6050 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6051 stab_best = stab;
6052
6053 /* Keep looking through other CUs. */
6054 }
6055
6056 return stab_best;
6057 }
6058
6059 /* This dumps minimal information about .debug_names. It is called
6060 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6061 uses this to verify that .debug_names has been loaded. */
6062
6063 static void
6064 dw2_debug_names_dump (struct objfile *objfile)
6065 {
6066 struct dwarf2_per_objfile *dwarf2_per_objfile
6067 = get_dwarf2_per_objfile (objfile);
6068
6069 gdb_assert (dwarf2_per_objfile->using_index);
6070 printf_filtered (".debug_names:");
6071 if (dwarf2_per_objfile->debug_names_table)
6072 printf_filtered (" exists\n");
6073 else
6074 printf_filtered (" faked for \"readnow\"\n");
6075 printf_filtered ("\n");
6076 }
6077
6078 static void
6079 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6080 const char *func_name)
6081 {
6082 struct dwarf2_per_objfile *dwarf2_per_objfile
6083 = get_dwarf2_per_objfile (objfile);
6084
6085 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6086 if (dwarf2_per_objfile->debug_names_table)
6087 {
6088 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6089
6090 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
6091
6092 struct dwarf2_per_cu_data *per_cu;
6093 while ((per_cu = iter.next ()) != NULL)
6094 dw2_instantiate_symtab (per_cu, false);
6095 }
6096 }
6097
6098 static void
6099 dw2_debug_names_expand_symtabs_matching
6100 (struct objfile *objfile,
6101 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6102 const lookup_name_info &lookup_name,
6103 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6104 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6105 enum search_domain kind)
6106 {
6107 struct dwarf2_per_objfile *dwarf2_per_objfile
6108 = get_dwarf2_per_objfile (objfile);
6109
6110 /* debug_names_table is NULL if OBJF_READNOW. */
6111 if (!dwarf2_per_objfile->debug_names_table)
6112 return;
6113
6114 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6115
6116 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6117
6118 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6119 symbol_matcher,
6120 kind, [&] (offset_type namei)
6121 {
6122 /* The name was matched, now expand corresponding CUs that were
6123 marked. */
6124 dw2_debug_names_iterator iter (map, kind, namei);
6125
6126 struct dwarf2_per_cu_data *per_cu;
6127 while ((per_cu = iter.next ()) != NULL)
6128 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6129 expansion_notify);
6130 });
6131 }
6132
6133 const struct quick_symbol_functions dwarf2_debug_names_functions =
6134 {
6135 dw2_has_symbols,
6136 dw2_find_last_source_symtab,
6137 dw2_forget_cached_source_info,
6138 dw2_map_symtabs_matching_filename,
6139 dw2_debug_names_lookup_symbol,
6140 dw2_print_stats,
6141 dw2_debug_names_dump,
6142 dw2_debug_names_expand_symtabs_for_function,
6143 dw2_expand_all_symtabs,
6144 dw2_expand_symtabs_with_fullname,
6145 dw2_map_matching_symbols,
6146 dw2_debug_names_expand_symtabs_matching,
6147 dw2_find_pc_sect_compunit_symtab,
6148 NULL,
6149 dw2_map_symbol_filenames
6150 };
6151
6152 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6153 to either a dwarf2_per_objfile or dwz_file object. */
6154
6155 template <typename T>
6156 static gdb::array_view<const gdb_byte>
6157 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6158 {
6159 dwarf2_section_info *section = &section_owner->gdb_index;
6160
6161 if (dwarf2_section_empty_p (section))
6162 return {};
6163
6164 /* Older elfutils strip versions could keep the section in the main
6165 executable while splitting it for the separate debug info file. */
6166 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6167 return {};
6168
6169 dwarf2_read_section (obj, section);
6170
6171 /* dwarf2_section_info::size is a bfd_size_type, while
6172 gdb::array_view works with size_t. On 32-bit hosts, with
6173 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6174 is 32-bit. So we need an explicit narrowing conversion here.
6175 This is fine, because it's impossible to allocate or mmap an
6176 array/buffer larger than what size_t can represent. */
6177 return gdb::make_array_view (section->buffer, section->size);
6178 }
6179
6180 /* Lookup the index cache for the contents of the index associated to
6181 DWARF2_OBJ. */
6182
6183 static gdb::array_view<const gdb_byte>
6184 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6185 {
6186 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6187 if (build_id == nullptr)
6188 return {};
6189
6190 return global_index_cache.lookup_gdb_index (build_id,
6191 &dwarf2_obj->index_cache_res);
6192 }
6193
6194 /* Same as the above, but for DWZ. */
6195
6196 static gdb::array_view<const gdb_byte>
6197 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6198 {
6199 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6200 if (build_id == nullptr)
6201 return {};
6202
6203 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6204 }
6205
6206 /* See symfile.h. */
6207
6208 bool
6209 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6210 {
6211 struct dwarf2_per_objfile *dwarf2_per_objfile
6212 = get_dwarf2_per_objfile (objfile);
6213
6214 /* If we're about to read full symbols, don't bother with the
6215 indices. In this case we also don't care if some other debug
6216 format is making psymtabs, because they are all about to be
6217 expanded anyway. */
6218 if ((objfile->flags & OBJF_READNOW))
6219 {
6220 dwarf2_per_objfile->using_index = 1;
6221 create_all_comp_units (dwarf2_per_objfile);
6222 create_all_type_units (dwarf2_per_objfile);
6223 dwarf2_per_objfile->quick_file_names_table
6224 = create_quick_file_names_table
6225 (dwarf2_per_objfile->all_comp_units.size ());
6226
6227 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6228 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6229 {
6230 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6231
6232 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6233 struct dwarf2_per_cu_quick_data);
6234 }
6235
6236 /* Return 1 so that gdb sees the "quick" functions. However,
6237 these functions will be no-ops because we will have expanded
6238 all symtabs. */
6239 *index_kind = dw_index_kind::GDB_INDEX;
6240 return true;
6241 }
6242
6243 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6244 {
6245 *index_kind = dw_index_kind::DEBUG_NAMES;
6246 return true;
6247 }
6248
6249 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6250 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6251 get_gdb_index_contents_from_section<dwz_file>))
6252 {
6253 *index_kind = dw_index_kind::GDB_INDEX;
6254 return true;
6255 }
6256
6257 /* ... otherwise, try to find the index in the index cache. */
6258 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6259 get_gdb_index_contents_from_cache,
6260 get_gdb_index_contents_from_cache_dwz))
6261 {
6262 global_index_cache.hit ();
6263 *index_kind = dw_index_kind::GDB_INDEX;
6264 return true;
6265 }
6266
6267 global_index_cache.miss ();
6268 return false;
6269 }
6270
6271 \f
6272
6273 /* Build a partial symbol table. */
6274
6275 void
6276 dwarf2_build_psymtabs (struct objfile *objfile)
6277 {
6278 struct dwarf2_per_objfile *dwarf2_per_objfile
6279 = get_dwarf2_per_objfile (objfile);
6280
6281 init_psymbol_list (objfile, 1024);
6282
6283 try
6284 {
6285 /* This isn't really ideal: all the data we allocate on the
6286 objfile's obstack is still uselessly kept around. However,
6287 freeing it seems unsafe. */
6288 psymtab_discarder psymtabs (objfile);
6289 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6290 psymtabs.keep ();
6291
6292 /* (maybe) store an index in the cache. */
6293 global_index_cache.store (dwarf2_per_objfile);
6294 }
6295 catch (const gdb_exception_error &except)
6296 {
6297 exception_print (gdb_stderr, except);
6298 }
6299 }
6300
6301 /* Return the total length of the CU described by HEADER. */
6302
6303 static unsigned int
6304 get_cu_length (const struct comp_unit_head *header)
6305 {
6306 return header->initial_length_size + header->length;
6307 }
6308
6309 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6310
6311 static inline bool
6312 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6313 {
6314 sect_offset bottom = cu_header->sect_off;
6315 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6316
6317 return sect_off >= bottom && sect_off < top;
6318 }
6319
6320 /* Find the base address of the compilation unit for range lists and
6321 location lists. It will normally be specified by DW_AT_low_pc.
6322 In DWARF-3 draft 4, the base address could be overridden by
6323 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6324 compilation units with discontinuous ranges. */
6325
6326 static void
6327 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6328 {
6329 struct attribute *attr;
6330
6331 cu->base_known = 0;
6332 cu->base_address = 0;
6333
6334 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6335 if (attr)
6336 {
6337 cu->base_address = attr_value_as_address (attr);
6338 cu->base_known = 1;
6339 }
6340 else
6341 {
6342 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6343 if (attr)
6344 {
6345 cu->base_address = attr_value_as_address (attr);
6346 cu->base_known = 1;
6347 }
6348 }
6349 }
6350
6351 /* Read in the comp unit header information from the debug_info at info_ptr.
6352 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6353 NOTE: This leaves members offset, first_die_offset to be filled in
6354 by the caller. */
6355
6356 static const gdb_byte *
6357 read_comp_unit_head (struct comp_unit_head *cu_header,
6358 const gdb_byte *info_ptr,
6359 struct dwarf2_section_info *section,
6360 rcuh_kind section_kind)
6361 {
6362 int signed_addr;
6363 unsigned int bytes_read;
6364 const char *filename = get_section_file_name (section);
6365 bfd *abfd = get_section_bfd_owner (section);
6366
6367 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6368 cu_header->initial_length_size = bytes_read;
6369 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6370 info_ptr += bytes_read;
6371 cu_header->version = read_2_bytes (abfd, info_ptr);
6372 if (cu_header->version < 2 || cu_header->version > 5)
6373 error (_("Dwarf Error: wrong version in compilation unit header "
6374 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6375 cu_header->version, filename);
6376 info_ptr += 2;
6377 if (cu_header->version < 5)
6378 switch (section_kind)
6379 {
6380 case rcuh_kind::COMPILE:
6381 cu_header->unit_type = DW_UT_compile;
6382 break;
6383 case rcuh_kind::TYPE:
6384 cu_header->unit_type = DW_UT_type;
6385 break;
6386 default:
6387 internal_error (__FILE__, __LINE__,
6388 _("read_comp_unit_head: invalid section_kind"));
6389 }
6390 else
6391 {
6392 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6393 (read_1_byte (abfd, info_ptr));
6394 info_ptr += 1;
6395 switch (cu_header->unit_type)
6396 {
6397 case DW_UT_compile:
6398 case DW_UT_partial:
6399 case DW_UT_skeleton:
6400 case DW_UT_split_compile:
6401 if (section_kind != rcuh_kind::COMPILE)
6402 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6403 "(is %s, should be %s) [in module %s]"),
6404 dwarf_unit_type_name (cu_header->unit_type),
6405 dwarf_unit_type_name (DW_UT_type), filename);
6406 break;
6407 case DW_UT_type:
6408 case DW_UT_split_type:
6409 section_kind = rcuh_kind::TYPE;
6410 break;
6411 default:
6412 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6413 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6414 "[in module %s]"), cu_header->unit_type,
6415 dwarf_unit_type_name (DW_UT_compile),
6416 dwarf_unit_type_name (DW_UT_skeleton),
6417 dwarf_unit_type_name (DW_UT_split_compile),
6418 dwarf_unit_type_name (DW_UT_type),
6419 dwarf_unit_type_name (DW_UT_split_type), filename);
6420 }
6421
6422 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6423 info_ptr += 1;
6424 }
6425 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6426 cu_header,
6427 &bytes_read);
6428 info_ptr += bytes_read;
6429 if (cu_header->version < 5)
6430 {
6431 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6432 info_ptr += 1;
6433 }
6434 signed_addr = bfd_get_sign_extend_vma (abfd);
6435 if (signed_addr < 0)
6436 internal_error (__FILE__, __LINE__,
6437 _("read_comp_unit_head: dwarf from non elf file"));
6438 cu_header->signed_addr_p = signed_addr;
6439
6440 bool header_has_signature = section_kind == rcuh_kind::TYPE
6441 || cu_header->unit_type == DW_UT_skeleton
6442 || cu_header->unit_type == DW_UT_split_compile;
6443
6444 if (header_has_signature)
6445 {
6446 cu_header->signature = read_8_bytes (abfd, info_ptr);
6447 info_ptr += 8;
6448 }
6449
6450 if (section_kind == rcuh_kind::TYPE)
6451 {
6452 LONGEST type_offset;
6453 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6454 info_ptr += bytes_read;
6455 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6456 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6457 error (_("Dwarf Error: Too big type_offset in compilation unit "
6458 "header (is %s) [in module %s]"), plongest (type_offset),
6459 filename);
6460 }
6461
6462 return info_ptr;
6463 }
6464
6465 /* Helper function that returns the proper abbrev section for
6466 THIS_CU. */
6467
6468 static struct dwarf2_section_info *
6469 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6470 {
6471 struct dwarf2_section_info *abbrev;
6472 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6473
6474 if (this_cu->is_dwz)
6475 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6476 else
6477 abbrev = &dwarf2_per_objfile->abbrev;
6478
6479 return abbrev;
6480 }
6481
6482 /* Subroutine of read_and_check_comp_unit_head and
6483 read_and_check_type_unit_head to simplify them.
6484 Perform various error checking on the header. */
6485
6486 static void
6487 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6488 struct comp_unit_head *header,
6489 struct dwarf2_section_info *section,
6490 struct dwarf2_section_info *abbrev_section)
6491 {
6492 const char *filename = get_section_file_name (section);
6493
6494 if (to_underlying (header->abbrev_sect_off)
6495 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6496 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6497 "(offset %s + 6) [in module %s]"),
6498 sect_offset_str (header->abbrev_sect_off),
6499 sect_offset_str (header->sect_off),
6500 filename);
6501
6502 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6503 avoid potential 32-bit overflow. */
6504 if (((ULONGEST) header->sect_off + get_cu_length (header))
6505 > section->size)
6506 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6507 "(offset %s + 0) [in module %s]"),
6508 header->length, sect_offset_str (header->sect_off),
6509 filename);
6510 }
6511
6512 /* Read in a CU/TU header and perform some basic error checking.
6513 The contents of the header are stored in HEADER.
6514 The result is a pointer to the start of the first DIE. */
6515
6516 static const gdb_byte *
6517 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6518 struct comp_unit_head *header,
6519 struct dwarf2_section_info *section,
6520 struct dwarf2_section_info *abbrev_section,
6521 const gdb_byte *info_ptr,
6522 rcuh_kind section_kind)
6523 {
6524 const gdb_byte *beg_of_comp_unit = info_ptr;
6525
6526 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6527
6528 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6529
6530 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6531
6532 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6533 abbrev_section);
6534
6535 return info_ptr;
6536 }
6537
6538 /* Fetch the abbreviation table offset from a comp or type unit header. */
6539
6540 static sect_offset
6541 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6542 struct dwarf2_section_info *section,
6543 sect_offset sect_off)
6544 {
6545 bfd *abfd = get_section_bfd_owner (section);
6546 const gdb_byte *info_ptr;
6547 unsigned int initial_length_size, offset_size;
6548 uint16_t version;
6549
6550 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6551 info_ptr = section->buffer + to_underlying (sect_off);
6552 read_initial_length (abfd, info_ptr, &initial_length_size);
6553 offset_size = initial_length_size == 4 ? 4 : 8;
6554 info_ptr += initial_length_size;
6555
6556 version = read_2_bytes (abfd, info_ptr);
6557 info_ptr += 2;
6558 if (version >= 5)
6559 {
6560 /* Skip unit type and address size. */
6561 info_ptr += 2;
6562 }
6563
6564 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6565 }
6566
6567 /* Allocate a new partial symtab for file named NAME and mark this new
6568 partial symtab as being an include of PST. */
6569
6570 static void
6571 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6572 struct objfile *objfile)
6573 {
6574 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6575
6576 if (!IS_ABSOLUTE_PATH (subpst->filename))
6577 {
6578 /* It shares objfile->objfile_obstack. */
6579 subpst->dirname = pst->dirname;
6580 }
6581
6582 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6583 subpst->dependencies[0] = pst;
6584 subpst->number_of_dependencies = 1;
6585
6586 subpst->read_symtab = pst->read_symtab;
6587
6588 /* No private part is necessary for include psymtabs. This property
6589 can be used to differentiate between such include psymtabs and
6590 the regular ones. */
6591 subpst->read_symtab_private = NULL;
6592 }
6593
6594 /* Read the Line Number Program data and extract the list of files
6595 included by the source file represented by PST. Build an include
6596 partial symtab for each of these included files. */
6597
6598 static void
6599 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6600 struct die_info *die,
6601 struct partial_symtab *pst)
6602 {
6603 line_header_up lh;
6604 struct attribute *attr;
6605
6606 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6607 if (attr)
6608 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6609 if (lh == NULL)
6610 return; /* No linetable, so no includes. */
6611
6612 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6613 that we pass in the raw text_low here; that is ok because we're
6614 only decoding the line table to make include partial symtabs, and
6615 so the addresses aren't really used. */
6616 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6617 pst->raw_text_low (), 1);
6618 }
6619
6620 static hashval_t
6621 hash_signatured_type (const void *item)
6622 {
6623 const struct signatured_type *sig_type
6624 = (const struct signatured_type *) item;
6625
6626 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6627 return sig_type->signature;
6628 }
6629
6630 static int
6631 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6632 {
6633 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6634 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6635
6636 return lhs->signature == rhs->signature;
6637 }
6638
6639 /* Allocate a hash table for signatured types. */
6640
6641 static htab_t
6642 allocate_signatured_type_table (struct objfile *objfile)
6643 {
6644 return htab_create_alloc_ex (41,
6645 hash_signatured_type,
6646 eq_signatured_type,
6647 NULL,
6648 &objfile->objfile_obstack,
6649 hashtab_obstack_allocate,
6650 dummy_obstack_deallocate);
6651 }
6652
6653 /* A helper function to add a signatured type CU to a table. */
6654
6655 static int
6656 add_signatured_type_cu_to_table (void **slot, void *datum)
6657 {
6658 struct signatured_type *sigt = (struct signatured_type *) *slot;
6659 std::vector<signatured_type *> *all_type_units
6660 = (std::vector<signatured_type *> *) datum;
6661
6662 all_type_units->push_back (sigt);
6663
6664 return 1;
6665 }
6666
6667 /* A helper for create_debug_types_hash_table. Read types from SECTION
6668 and fill them into TYPES_HTAB. It will process only type units,
6669 therefore DW_UT_type. */
6670
6671 static void
6672 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6673 struct dwo_file *dwo_file,
6674 dwarf2_section_info *section, htab_t &types_htab,
6675 rcuh_kind section_kind)
6676 {
6677 struct objfile *objfile = dwarf2_per_objfile->objfile;
6678 struct dwarf2_section_info *abbrev_section;
6679 bfd *abfd;
6680 const gdb_byte *info_ptr, *end_ptr;
6681
6682 abbrev_section = (dwo_file != NULL
6683 ? &dwo_file->sections.abbrev
6684 : &dwarf2_per_objfile->abbrev);
6685
6686 if (dwarf_read_debug)
6687 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6688 get_section_name (section),
6689 get_section_file_name (abbrev_section));
6690
6691 dwarf2_read_section (objfile, section);
6692 info_ptr = section->buffer;
6693
6694 if (info_ptr == NULL)
6695 return;
6696
6697 /* We can't set abfd until now because the section may be empty or
6698 not present, in which case the bfd is unknown. */
6699 abfd = get_section_bfd_owner (section);
6700
6701 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6702 because we don't need to read any dies: the signature is in the
6703 header. */
6704
6705 end_ptr = info_ptr + section->size;
6706 while (info_ptr < end_ptr)
6707 {
6708 struct signatured_type *sig_type;
6709 struct dwo_unit *dwo_tu;
6710 void **slot;
6711 const gdb_byte *ptr = info_ptr;
6712 struct comp_unit_head header;
6713 unsigned int length;
6714
6715 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6716
6717 /* Initialize it due to a false compiler warning. */
6718 header.signature = -1;
6719 header.type_cu_offset_in_tu = (cu_offset) -1;
6720
6721 /* We need to read the type's signature in order to build the hash
6722 table, but we don't need anything else just yet. */
6723
6724 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6725 abbrev_section, ptr, section_kind);
6726
6727 length = get_cu_length (&header);
6728
6729 /* Skip dummy type units. */
6730 if (ptr >= info_ptr + length
6731 || peek_abbrev_code (abfd, ptr) == 0
6732 || header.unit_type != DW_UT_type)
6733 {
6734 info_ptr += length;
6735 continue;
6736 }
6737
6738 if (types_htab == NULL)
6739 {
6740 if (dwo_file)
6741 types_htab = allocate_dwo_unit_table (objfile);
6742 else
6743 types_htab = allocate_signatured_type_table (objfile);
6744 }
6745
6746 if (dwo_file)
6747 {
6748 sig_type = NULL;
6749 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6750 struct dwo_unit);
6751 dwo_tu->dwo_file = dwo_file;
6752 dwo_tu->signature = header.signature;
6753 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6754 dwo_tu->section = section;
6755 dwo_tu->sect_off = sect_off;
6756 dwo_tu->length = length;
6757 }
6758 else
6759 {
6760 /* N.B.: type_offset is not usable if this type uses a DWO file.
6761 The real type_offset is in the DWO file. */
6762 dwo_tu = NULL;
6763 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6764 struct signatured_type);
6765 sig_type->signature = header.signature;
6766 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6767 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6768 sig_type->per_cu.is_debug_types = 1;
6769 sig_type->per_cu.section = section;
6770 sig_type->per_cu.sect_off = sect_off;
6771 sig_type->per_cu.length = length;
6772 }
6773
6774 slot = htab_find_slot (types_htab,
6775 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6776 INSERT);
6777 gdb_assert (slot != NULL);
6778 if (*slot != NULL)
6779 {
6780 sect_offset dup_sect_off;
6781
6782 if (dwo_file)
6783 {
6784 const struct dwo_unit *dup_tu
6785 = (const struct dwo_unit *) *slot;
6786
6787 dup_sect_off = dup_tu->sect_off;
6788 }
6789 else
6790 {
6791 const struct signatured_type *dup_tu
6792 = (const struct signatured_type *) *slot;
6793
6794 dup_sect_off = dup_tu->per_cu.sect_off;
6795 }
6796
6797 complaint (_("debug type entry at offset %s is duplicate to"
6798 " the entry at offset %s, signature %s"),
6799 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6800 hex_string (header.signature));
6801 }
6802 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6803
6804 if (dwarf_read_debug > 1)
6805 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6806 sect_offset_str (sect_off),
6807 hex_string (header.signature));
6808
6809 info_ptr += length;
6810 }
6811 }
6812
6813 /* Create the hash table of all entries in the .debug_types
6814 (or .debug_types.dwo) section(s).
6815 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6816 otherwise it is NULL.
6817
6818 The result is a pointer to the hash table or NULL if there are no types.
6819
6820 Note: This function processes DWO files only, not DWP files. */
6821
6822 static void
6823 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6824 struct dwo_file *dwo_file,
6825 gdb::array_view<dwarf2_section_info> type_sections,
6826 htab_t &types_htab)
6827 {
6828 for (dwarf2_section_info &section : type_sections)
6829 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6830 types_htab, rcuh_kind::TYPE);
6831 }
6832
6833 /* Create the hash table of all entries in the .debug_types section,
6834 and initialize all_type_units.
6835 The result is zero if there is an error (e.g. missing .debug_types section),
6836 otherwise non-zero. */
6837
6838 static int
6839 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6840 {
6841 htab_t types_htab = NULL;
6842
6843 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6844 &dwarf2_per_objfile->info, types_htab,
6845 rcuh_kind::COMPILE);
6846 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6847 dwarf2_per_objfile->types, types_htab);
6848 if (types_htab == NULL)
6849 {
6850 dwarf2_per_objfile->signatured_types = NULL;
6851 return 0;
6852 }
6853
6854 dwarf2_per_objfile->signatured_types = types_htab;
6855
6856 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6857 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6858
6859 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6860 &dwarf2_per_objfile->all_type_units);
6861
6862 return 1;
6863 }
6864
6865 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6866 If SLOT is non-NULL, it is the entry to use in the hash table.
6867 Otherwise we find one. */
6868
6869 static struct signatured_type *
6870 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6871 void **slot)
6872 {
6873 struct objfile *objfile = dwarf2_per_objfile->objfile;
6874
6875 if (dwarf2_per_objfile->all_type_units.size ()
6876 == dwarf2_per_objfile->all_type_units.capacity ())
6877 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6878
6879 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6880 struct signatured_type);
6881
6882 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6883 sig_type->signature = sig;
6884 sig_type->per_cu.is_debug_types = 1;
6885 if (dwarf2_per_objfile->using_index)
6886 {
6887 sig_type->per_cu.v.quick =
6888 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6889 struct dwarf2_per_cu_quick_data);
6890 }
6891
6892 if (slot == NULL)
6893 {
6894 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6895 sig_type, INSERT);
6896 }
6897 gdb_assert (*slot == NULL);
6898 *slot = sig_type;
6899 /* The rest of sig_type must be filled in by the caller. */
6900 return sig_type;
6901 }
6902
6903 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6904 Fill in SIG_ENTRY with DWO_ENTRY. */
6905
6906 static void
6907 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6908 struct signatured_type *sig_entry,
6909 struct dwo_unit *dwo_entry)
6910 {
6911 /* Make sure we're not clobbering something we don't expect to. */
6912 gdb_assert (! sig_entry->per_cu.queued);
6913 gdb_assert (sig_entry->per_cu.cu == NULL);
6914 if (dwarf2_per_objfile->using_index)
6915 {
6916 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6917 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6918 }
6919 else
6920 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6921 gdb_assert (sig_entry->signature == dwo_entry->signature);
6922 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6923 gdb_assert (sig_entry->type_unit_group == NULL);
6924 gdb_assert (sig_entry->dwo_unit == NULL);
6925
6926 sig_entry->per_cu.section = dwo_entry->section;
6927 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6928 sig_entry->per_cu.length = dwo_entry->length;
6929 sig_entry->per_cu.reading_dwo_directly = 1;
6930 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6931 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6932 sig_entry->dwo_unit = dwo_entry;
6933 }
6934
6935 /* Subroutine of lookup_signatured_type.
6936 If we haven't read the TU yet, create the signatured_type data structure
6937 for a TU to be read in directly from a DWO file, bypassing the stub.
6938 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6939 using .gdb_index, then when reading a CU we want to stay in the DWO file
6940 containing that CU. Otherwise we could end up reading several other DWO
6941 files (due to comdat folding) to process the transitive closure of all the
6942 mentioned TUs, and that can be slow. The current DWO file will have every
6943 type signature that it needs.
6944 We only do this for .gdb_index because in the psymtab case we already have
6945 to read all the DWOs to build the type unit groups. */
6946
6947 static struct signatured_type *
6948 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6949 {
6950 struct dwarf2_per_objfile *dwarf2_per_objfile
6951 = cu->per_cu->dwarf2_per_objfile;
6952 struct objfile *objfile = dwarf2_per_objfile->objfile;
6953 struct dwo_file *dwo_file;
6954 struct dwo_unit find_dwo_entry, *dwo_entry;
6955 struct signatured_type find_sig_entry, *sig_entry;
6956 void **slot;
6957
6958 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6959
6960 /* If TU skeletons have been removed then we may not have read in any
6961 TUs yet. */
6962 if (dwarf2_per_objfile->signatured_types == NULL)
6963 {
6964 dwarf2_per_objfile->signatured_types
6965 = allocate_signatured_type_table (objfile);
6966 }
6967
6968 /* We only ever need to read in one copy of a signatured type.
6969 Use the global signatured_types array to do our own comdat-folding
6970 of types. If this is the first time we're reading this TU, and
6971 the TU has an entry in .gdb_index, replace the recorded data from
6972 .gdb_index with this TU. */
6973
6974 find_sig_entry.signature = sig;
6975 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6976 &find_sig_entry, INSERT);
6977 sig_entry = (struct signatured_type *) *slot;
6978
6979 /* We can get here with the TU already read, *or* in the process of being
6980 read. Don't reassign the global entry to point to this DWO if that's
6981 the case. Also note that if the TU is already being read, it may not
6982 have come from a DWO, the program may be a mix of Fission-compiled
6983 code and non-Fission-compiled code. */
6984
6985 /* Have we already tried to read this TU?
6986 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6987 needn't exist in the global table yet). */
6988 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6989 return sig_entry;
6990
6991 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6992 dwo_unit of the TU itself. */
6993 dwo_file = cu->dwo_unit->dwo_file;
6994
6995 /* Ok, this is the first time we're reading this TU. */
6996 if (dwo_file->tus == NULL)
6997 return NULL;
6998 find_dwo_entry.signature = sig;
6999 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7000 if (dwo_entry == NULL)
7001 return NULL;
7002
7003 /* If the global table doesn't have an entry for this TU, add one. */
7004 if (sig_entry == NULL)
7005 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7006
7007 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7008 sig_entry->per_cu.tu_read = 1;
7009 return sig_entry;
7010 }
7011
7012 /* Subroutine of lookup_signatured_type.
7013 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7014 then try the DWP file. If the TU stub (skeleton) has been removed then
7015 it won't be in .gdb_index. */
7016
7017 static struct signatured_type *
7018 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7019 {
7020 struct dwarf2_per_objfile *dwarf2_per_objfile
7021 = cu->per_cu->dwarf2_per_objfile;
7022 struct objfile *objfile = dwarf2_per_objfile->objfile;
7023 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7024 struct dwo_unit *dwo_entry;
7025 struct signatured_type find_sig_entry, *sig_entry;
7026 void **slot;
7027
7028 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7029 gdb_assert (dwp_file != NULL);
7030
7031 /* If TU skeletons have been removed then we may not have read in any
7032 TUs yet. */
7033 if (dwarf2_per_objfile->signatured_types == NULL)
7034 {
7035 dwarf2_per_objfile->signatured_types
7036 = allocate_signatured_type_table (objfile);
7037 }
7038
7039 find_sig_entry.signature = sig;
7040 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7041 &find_sig_entry, INSERT);
7042 sig_entry = (struct signatured_type *) *slot;
7043
7044 /* Have we already tried to read this TU?
7045 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7046 needn't exist in the global table yet). */
7047 if (sig_entry != NULL)
7048 return sig_entry;
7049
7050 if (dwp_file->tus == NULL)
7051 return NULL;
7052 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7053 sig, 1 /* is_debug_types */);
7054 if (dwo_entry == NULL)
7055 return NULL;
7056
7057 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7058 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7059
7060 return sig_entry;
7061 }
7062
7063 /* Lookup a signature based type for DW_FORM_ref_sig8.
7064 Returns NULL if signature SIG is not present in the table.
7065 It is up to the caller to complain about this. */
7066
7067 static struct signatured_type *
7068 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7069 {
7070 struct dwarf2_per_objfile *dwarf2_per_objfile
7071 = cu->per_cu->dwarf2_per_objfile;
7072
7073 if (cu->dwo_unit
7074 && dwarf2_per_objfile->using_index)
7075 {
7076 /* We're in a DWO/DWP file, and we're using .gdb_index.
7077 These cases require special processing. */
7078 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7079 return lookup_dwo_signatured_type (cu, sig);
7080 else
7081 return lookup_dwp_signatured_type (cu, sig);
7082 }
7083 else
7084 {
7085 struct signatured_type find_entry, *entry;
7086
7087 if (dwarf2_per_objfile->signatured_types == NULL)
7088 return NULL;
7089 find_entry.signature = sig;
7090 entry = ((struct signatured_type *)
7091 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7092 return entry;
7093 }
7094 }
7095 \f
7096 /* Low level DIE reading support. */
7097
7098 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7099
7100 static void
7101 init_cu_die_reader (struct die_reader_specs *reader,
7102 struct dwarf2_cu *cu,
7103 struct dwarf2_section_info *section,
7104 struct dwo_file *dwo_file,
7105 struct abbrev_table *abbrev_table)
7106 {
7107 gdb_assert (section->readin && section->buffer != NULL);
7108 reader->abfd = get_section_bfd_owner (section);
7109 reader->cu = cu;
7110 reader->dwo_file = dwo_file;
7111 reader->die_section = section;
7112 reader->buffer = section->buffer;
7113 reader->buffer_end = section->buffer + section->size;
7114 reader->comp_dir = NULL;
7115 reader->abbrev_table = abbrev_table;
7116 }
7117
7118 /* Subroutine of init_cutu_and_read_dies to simplify it.
7119 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7120 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7121 already.
7122
7123 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7124 from it to the DIE in the DWO. If NULL we are skipping the stub.
7125 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7126 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7127 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7128 STUB_COMP_DIR may be non-NULL.
7129 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7130 are filled in with the info of the DIE from the DWO file.
7131 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7132 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7133 kept around for at least as long as *RESULT_READER.
7134
7135 The result is non-zero if a valid (non-dummy) DIE was found. */
7136
7137 static int
7138 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7139 struct dwo_unit *dwo_unit,
7140 struct die_info *stub_comp_unit_die,
7141 const char *stub_comp_dir,
7142 struct die_reader_specs *result_reader,
7143 const gdb_byte **result_info_ptr,
7144 struct die_info **result_comp_unit_die,
7145 int *result_has_children,
7146 abbrev_table_up *result_dwo_abbrev_table)
7147 {
7148 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7149 struct objfile *objfile = dwarf2_per_objfile->objfile;
7150 struct dwarf2_cu *cu = this_cu->cu;
7151 bfd *abfd;
7152 const gdb_byte *begin_info_ptr, *info_ptr;
7153 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7154 int i,num_extra_attrs;
7155 struct dwarf2_section_info *dwo_abbrev_section;
7156 struct attribute *attr;
7157 struct die_info *comp_unit_die;
7158
7159 /* At most one of these may be provided. */
7160 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7161
7162 /* These attributes aren't processed until later:
7163 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7164 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7165 referenced later. However, these attributes are found in the stub
7166 which we won't have later. In order to not impose this complication
7167 on the rest of the code, we read them here and copy them to the
7168 DWO CU/TU die. */
7169
7170 stmt_list = NULL;
7171 low_pc = NULL;
7172 high_pc = NULL;
7173 ranges = NULL;
7174 comp_dir = NULL;
7175
7176 if (stub_comp_unit_die != NULL)
7177 {
7178 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7179 DWO file. */
7180 if (! this_cu->is_debug_types)
7181 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7182 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7183 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7184 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7185 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7186
7187 /* There should be a DW_AT_addr_base attribute here (if needed).
7188 We need the value before we can process DW_FORM_GNU_addr_index
7189 or DW_FORM_addrx. */
7190 cu->addr_base = 0;
7191 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7192 if (attr)
7193 cu->addr_base = DW_UNSND (attr);
7194
7195 /* There should be a DW_AT_ranges_base attribute here (if needed).
7196 We need the value before we can process DW_AT_ranges. */
7197 cu->ranges_base = 0;
7198 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7199 if (attr)
7200 cu->ranges_base = DW_UNSND (attr);
7201 }
7202 else if (stub_comp_dir != NULL)
7203 {
7204 /* Reconstruct the comp_dir attribute to simplify the code below. */
7205 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7206 comp_dir->name = DW_AT_comp_dir;
7207 comp_dir->form = DW_FORM_string;
7208 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7209 DW_STRING (comp_dir) = stub_comp_dir;
7210 }
7211
7212 /* Set up for reading the DWO CU/TU. */
7213 cu->dwo_unit = dwo_unit;
7214 dwarf2_section_info *section = dwo_unit->section;
7215 dwarf2_read_section (objfile, section);
7216 abfd = get_section_bfd_owner (section);
7217 begin_info_ptr = info_ptr = (section->buffer
7218 + to_underlying (dwo_unit->sect_off));
7219 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7220
7221 if (this_cu->is_debug_types)
7222 {
7223 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7224
7225 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7226 &cu->header, section,
7227 dwo_abbrev_section,
7228 info_ptr, rcuh_kind::TYPE);
7229 /* This is not an assert because it can be caused by bad debug info. */
7230 if (sig_type->signature != cu->header.signature)
7231 {
7232 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7233 " TU at offset %s [in module %s]"),
7234 hex_string (sig_type->signature),
7235 hex_string (cu->header.signature),
7236 sect_offset_str (dwo_unit->sect_off),
7237 bfd_get_filename (abfd));
7238 }
7239 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7240 /* For DWOs coming from DWP files, we don't know the CU length
7241 nor the type's offset in the TU until now. */
7242 dwo_unit->length = get_cu_length (&cu->header);
7243 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7244
7245 /* Establish the type offset that can be used to lookup the type.
7246 For DWO files, we don't know it until now. */
7247 sig_type->type_offset_in_section
7248 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7249 }
7250 else
7251 {
7252 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7253 &cu->header, section,
7254 dwo_abbrev_section,
7255 info_ptr, rcuh_kind::COMPILE);
7256 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7257 /* For DWOs coming from DWP files, we don't know the CU length
7258 until now. */
7259 dwo_unit->length = get_cu_length (&cu->header);
7260 }
7261
7262 *result_dwo_abbrev_table
7263 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7264 cu->header.abbrev_sect_off);
7265 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7266 result_dwo_abbrev_table->get ());
7267
7268 /* Read in the die, but leave space to copy over the attributes
7269 from the stub. This has the benefit of simplifying the rest of
7270 the code - all the work to maintain the illusion of a single
7271 DW_TAG_{compile,type}_unit DIE is done here. */
7272 num_extra_attrs = ((stmt_list != NULL)
7273 + (low_pc != NULL)
7274 + (high_pc != NULL)
7275 + (ranges != NULL)
7276 + (comp_dir != NULL));
7277 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7278 result_has_children, num_extra_attrs);
7279
7280 /* Copy over the attributes from the stub to the DIE we just read in. */
7281 comp_unit_die = *result_comp_unit_die;
7282 i = comp_unit_die->num_attrs;
7283 if (stmt_list != NULL)
7284 comp_unit_die->attrs[i++] = *stmt_list;
7285 if (low_pc != NULL)
7286 comp_unit_die->attrs[i++] = *low_pc;
7287 if (high_pc != NULL)
7288 comp_unit_die->attrs[i++] = *high_pc;
7289 if (ranges != NULL)
7290 comp_unit_die->attrs[i++] = *ranges;
7291 if (comp_dir != NULL)
7292 comp_unit_die->attrs[i++] = *comp_dir;
7293 comp_unit_die->num_attrs += num_extra_attrs;
7294
7295 if (dwarf_die_debug)
7296 {
7297 fprintf_unfiltered (gdb_stdlog,
7298 "Read die from %s@0x%x of %s:\n",
7299 get_section_name (section),
7300 (unsigned) (begin_info_ptr - section->buffer),
7301 bfd_get_filename (abfd));
7302 dump_die (comp_unit_die, dwarf_die_debug);
7303 }
7304
7305 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7306 TUs by skipping the stub and going directly to the entry in the DWO file.
7307 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7308 to get it via circuitous means. Blech. */
7309 if (comp_dir != NULL)
7310 result_reader->comp_dir = DW_STRING (comp_dir);
7311
7312 /* Skip dummy compilation units. */
7313 if (info_ptr >= begin_info_ptr + dwo_unit->length
7314 || peek_abbrev_code (abfd, info_ptr) == 0)
7315 return 0;
7316
7317 *result_info_ptr = info_ptr;
7318 return 1;
7319 }
7320
7321 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7322 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7323 signature is part of the header. */
7324 static gdb::optional<ULONGEST>
7325 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7326 {
7327 if (cu->header.version >= 5)
7328 return cu->header.signature;
7329 struct attribute *attr;
7330 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7331 if (attr == nullptr)
7332 return gdb::optional<ULONGEST> ();
7333 return DW_UNSND (attr);
7334 }
7335
7336 /* Subroutine of init_cutu_and_read_dies to simplify it.
7337 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7338 Returns NULL if the specified DWO unit cannot be found. */
7339
7340 static struct dwo_unit *
7341 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7342 struct die_info *comp_unit_die)
7343 {
7344 struct dwarf2_cu *cu = this_cu->cu;
7345 struct dwo_unit *dwo_unit;
7346 const char *comp_dir, *dwo_name;
7347
7348 gdb_assert (cu != NULL);
7349
7350 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7351 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7352 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7353
7354 if (this_cu->is_debug_types)
7355 {
7356 struct signatured_type *sig_type;
7357
7358 /* Since this_cu is the first member of struct signatured_type,
7359 we can go from a pointer to one to a pointer to the other. */
7360 sig_type = (struct signatured_type *) this_cu;
7361 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7362 }
7363 else
7364 {
7365 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7366 if (!signature.has_value ())
7367 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7368 " [in module %s]"),
7369 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7370 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7371 *signature);
7372 }
7373
7374 return dwo_unit;
7375 }
7376
7377 /* Subroutine of init_cutu_and_read_dies to simplify it.
7378 See it for a description of the parameters.
7379 Read a TU directly from a DWO file, bypassing the stub. */
7380
7381 static void
7382 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7383 int use_existing_cu, int keep,
7384 die_reader_func_ftype *die_reader_func,
7385 void *data)
7386 {
7387 std::unique_ptr<dwarf2_cu> new_cu;
7388 struct signatured_type *sig_type;
7389 struct die_reader_specs reader;
7390 const gdb_byte *info_ptr;
7391 struct die_info *comp_unit_die;
7392 int has_children;
7393 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7394
7395 /* Verify we can do the following downcast, and that we have the
7396 data we need. */
7397 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7398 sig_type = (struct signatured_type *) this_cu;
7399 gdb_assert (sig_type->dwo_unit != NULL);
7400
7401 if (use_existing_cu && this_cu->cu != NULL)
7402 {
7403 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7404 /* There's no need to do the rereading_dwo_cu handling that
7405 init_cutu_and_read_dies does since we don't read the stub. */
7406 }
7407 else
7408 {
7409 /* If !use_existing_cu, this_cu->cu must be NULL. */
7410 gdb_assert (this_cu->cu == NULL);
7411 new_cu.reset (new dwarf2_cu (this_cu));
7412 }
7413
7414 /* A future optimization, if needed, would be to use an existing
7415 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7416 could share abbrev tables. */
7417
7418 /* The abbreviation table used by READER, this must live at least as long as
7419 READER. */
7420 abbrev_table_up dwo_abbrev_table;
7421
7422 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7423 NULL /* stub_comp_unit_die */,
7424 sig_type->dwo_unit->dwo_file->comp_dir,
7425 &reader, &info_ptr,
7426 &comp_unit_die, &has_children,
7427 &dwo_abbrev_table) == 0)
7428 {
7429 /* Dummy die. */
7430 return;
7431 }
7432
7433 /* All the "real" work is done here. */
7434 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7435
7436 /* This duplicates the code in init_cutu_and_read_dies,
7437 but the alternative is making the latter more complex.
7438 This function is only for the special case of using DWO files directly:
7439 no point in overly complicating the general case just to handle this. */
7440 if (new_cu != NULL && keep)
7441 {
7442 /* Link this CU into read_in_chain. */
7443 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7444 dwarf2_per_objfile->read_in_chain = this_cu;
7445 /* The chain owns it now. */
7446 new_cu.release ();
7447 }
7448 }
7449
7450 /* Initialize a CU (or TU) and read its DIEs.
7451 If the CU defers to a DWO file, read the DWO file as well.
7452
7453 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7454 Otherwise the table specified in the comp unit header is read in and used.
7455 This is an optimization for when we already have the abbrev table.
7456
7457 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7458 Otherwise, a new CU is allocated with xmalloc.
7459
7460 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7461 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7462
7463 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7464 linker) then DIE_READER_FUNC will not get called. */
7465
7466 static void
7467 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7468 struct abbrev_table *abbrev_table,
7469 int use_existing_cu, int keep,
7470 bool skip_partial,
7471 die_reader_func_ftype *die_reader_func,
7472 void *data)
7473 {
7474 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7475 struct objfile *objfile = dwarf2_per_objfile->objfile;
7476 struct dwarf2_section_info *section = this_cu->section;
7477 bfd *abfd = get_section_bfd_owner (section);
7478 struct dwarf2_cu *cu;
7479 const gdb_byte *begin_info_ptr, *info_ptr;
7480 struct die_reader_specs reader;
7481 struct die_info *comp_unit_die;
7482 int has_children;
7483 struct signatured_type *sig_type = NULL;
7484 struct dwarf2_section_info *abbrev_section;
7485 /* Non-zero if CU currently points to a DWO file and we need to
7486 reread it. When this happens we need to reread the skeleton die
7487 before we can reread the DWO file (this only applies to CUs, not TUs). */
7488 int rereading_dwo_cu = 0;
7489
7490 if (dwarf_die_debug)
7491 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7492 this_cu->is_debug_types ? "type" : "comp",
7493 sect_offset_str (this_cu->sect_off));
7494
7495 if (use_existing_cu)
7496 gdb_assert (keep);
7497
7498 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7499 file (instead of going through the stub), short-circuit all of this. */
7500 if (this_cu->reading_dwo_directly)
7501 {
7502 /* Narrow down the scope of possibilities to have to understand. */
7503 gdb_assert (this_cu->is_debug_types);
7504 gdb_assert (abbrev_table == NULL);
7505 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7506 die_reader_func, data);
7507 return;
7508 }
7509
7510 /* This is cheap if the section is already read in. */
7511 dwarf2_read_section (objfile, section);
7512
7513 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7514
7515 abbrev_section = get_abbrev_section_for_cu (this_cu);
7516
7517 std::unique_ptr<dwarf2_cu> new_cu;
7518 if (use_existing_cu && this_cu->cu != NULL)
7519 {
7520 cu = this_cu->cu;
7521 /* If this CU is from a DWO file we need to start over, we need to
7522 refetch the attributes from the skeleton CU.
7523 This could be optimized by retrieving those attributes from when we
7524 were here the first time: the previous comp_unit_die was stored in
7525 comp_unit_obstack. But there's no data yet that we need this
7526 optimization. */
7527 if (cu->dwo_unit != NULL)
7528 rereading_dwo_cu = 1;
7529 }
7530 else
7531 {
7532 /* If !use_existing_cu, this_cu->cu must be NULL. */
7533 gdb_assert (this_cu->cu == NULL);
7534 new_cu.reset (new dwarf2_cu (this_cu));
7535 cu = new_cu.get ();
7536 }
7537
7538 /* Get the header. */
7539 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7540 {
7541 /* We already have the header, there's no need to read it in again. */
7542 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7543 }
7544 else
7545 {
7546 if (this_cu->is_debug_types)
7547 {
7548 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7549 &cu->header, section,
7550 abbrev_section, info_ptr,
7551 rcuh_kind::TYPE);
7552
7553 /* Since per_cu is the first member of struct signatured_type,
7554 we can go from a pointer to one to a pointer to the other. */
7555 sig_type = (struct signatured_type *) this_cu;
7556 gdb_assert (sig_type->signature == cu->header.signature);
7557 gdb_assert (sig_type->type_offset_in_tu
7558 == cu->header.type_cu_offset_in_tu);
7559 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7560
7561 /* LENGTH has not been set yet for type units if we're
7562 using .gdb_index. */
7563 this_cu->length = get_cu_length (&cu->header);
7564
7565 /* Establish the type offset that can be used to lookup the type. */
7566 sig_type->type_offset_in_section =
7567 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7568
7569 this_cu->dwarf_version = cu->header.version;
7570 }
7571 else
7572 {
7573 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7574 &cu->header, section,
7575 abbrev_section,
7576 info_ptr,
7577 rcuh_kind::COMPILE);
7578
7579 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7580 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7581 this_cu->dwarf_version = cu->header.version;
7582 }
7583 }
7584
7585 /* Skip dummy compilation units. */
7586 if (info_ptr >= begin_info_ptr + this_cu->length
7587 || peek_abbrev_code (abfd, info_ptr) == 0)
7588 return;
7589
7590 /* If we don't have them yet, read the abbrevs for this compilation unit.
7591 And if we need to read them now, make sure they're freed when we're
7592 done (own the table through ABBREV_TABLE_HOLDER). */
7593 abbrev_table_up abbrev_table_holder;
7594 if (abbrev_table != NULL)
7595 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7596 else
7597 {
7598 abbrev_table_holder
7599 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7600 cu->header.abbrev_sect_off);
7601 abbrev_table = abbrev_table_holder.get ();
7602 }
7603
7604 /* Read the top level CU/TU die. */
7605 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7606 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7607
7608 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7609 return;
7610
7611 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7612 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7613 table from the DWO file and pass the ownership over to us. It will be
7614 referenced from READER, so we must make sure to free it after we're done
7615 with READER.
7616
7617 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7618 DWO CU, that this test will fail (the attribute will not be present). */
7619 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7620 abbrev_table_up dwo_abbrev_table;
7621 if (dwo_name != nullptr)
7622 {
7623 struct dwo_unit *dwo_unit;
7624 struct die_info *dwo_comp_unit_die;
7625
7626 if (has_children)
7627 {
7628 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7629 " has children (offset %s) [in module %s]"),
7630 sect_offset_str (this_cu->sect_off),
7631 bfd_get_filename (abfd));
7632 }
7633 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7634 if (dwo_unit != NULL)
7635 {
7636 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7637 comp_unit_die, NULL,
7638 &reader, &info_ptr,
7639 &dwo_comp_unit_die, &has_children,
7640 &dwo_abbrev_table) == 0)
7641 {
7642 /* Dummy die. */
7643 return;
7644 }
7645 comp_unit_die = dwo_comp_unit_die;
7646 }
7647 else
7648 {
7649 /* Yikes, we couldn't find the rest of the DIE, we only have
7650 the stub. A complaint has already been logged. There's
7651 not much more we can do except pass on the stub DIE to
7652 die_reader_func. We don't want to throw an error on bad
7653 debug info. */
7654 }
7655 }
7656
7657 /* All of the above is setup for this call. Yikes. */
7658 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7659
7660 /* Done, clean up. */
7661 if (new_cu != NULL && keep)
7662 {
7663 /* Link this CU into read_in_chain. */
7664 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7665 dwarf2_per_objfile->read_in_chain = this_cu;
7666 /* The chain owns it now. */
7667 new_cu.release ();
7668 }
7669 }
7670
7671 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7672 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7673 to have already done the lookup to find the DWO file).
7674
7675 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7676 THIS_CU->is_debug_types, but nothing else.
7677
7678 We fill in THIS_CU->length.
7679
7680 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7681 linker) then DIE_READER_FUNC will not get called.
7682
7683 THIS_CU->cu is always freed when done.
7684 This is done in order to not leave THIS_CU->cu in a state where we have
7685 to care whether it refers to the "main" CU or the DWO CU. */
7686
7687 static void
7688 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7689 struct dwo_file *dwo_file,
7690 die_reader_func_ftype *die_reader_func,
7691 void *data)
7692 {
7693 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7694 struct objfile *objfile = dwarf2_per_objfile->objfile;
7695 struct dwarf2_section_info *section = this_cu->section;
7696 bfd *abfd = get_section_bfd_owner (section);
7697 struct dwarf2_section_info *abbrev_section;
7698 const gdb_byte *begin_info_ptr, *info_ptr;
7699 struct die_reader_specs reader;
7700 struct die_info *comp_unit_die;
7701 int has_children;
7702
7703 if (dwarf_die_debug)
7704 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7705 this_cu->is_debug_types ? "type" : "comp",
7706 sect_offset_str (this_cu->sect_off));
7707
7708 gdb_assert (this_cu->cu == NULL);
7709
7710 abbrev_section = (dwo_file != NULL
7711 ? &dwo_file->sections.abbrev
7712 : get_abbrev_section_for_cu (this_cu));
7713
7714 /* This is cheap if the section is already read in. */
7715 dwarf2_read_section (objfile, section);
7716
7717 struct dwarf2_cu cu (this_cu);
7718
7719 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7720 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7721 &cu.header, section,
7722 abbrev_section, info_ptr,
7723 (this_cu->is_debug_types
7724 ? rcuh_kind::TYPE
7725 : rcuh_kind::COMPILE));
7726
7727 this_cu->length = get_cu_length (&cu.header);
7728
7729 /* Skip dummy compilation units. */
7730 if (info_ptr >= begin_info_ptr + this_cu->length
7731 || peek_abbrev_code (abfd, info_ptr) == 0)
7732 return;
7733
7734 abbrev_table_up abbrev_table
7735 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7736 cu.header.abbrev_sect_off);
7737
7738 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
7739 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7740
7741 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7742 }
7743
7744 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7745 does not lookup the specified DWO file.
7746 This cannot be used to read DWO files.
7747
7748 THIS_CU->cu is always freed when done.
7749 This is done in order to not leave THIS_CU->cu in a state where we have
7750 to care whether it refers to the "main" CU or the DWO CU.
7751 We can revisit this if the data shows there's a performance issue. */
7752
7753 static void
7754 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7755 die_reader_func_ftype *die_reader_func,
7756 void *data)
7757 {
7758 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
7759 }
7760 \f
7761 /* Type Unit Groups.
7762
7763 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7764 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7765 so that all types coming from the same compilation (.o file) are grouped
7766 together. A future step could be to put the types in the same symtab as
7767 the CU the types ultimately came from. */
7768
7769 static hashval_t
7770 hash_type_unit_group (const void *item)
7771 {
7772 const struct type_unit_group *tu_group
7773 = (const struct type_unit_group *) item;
7774
7775 return hash_stmt_list_entry (&tu_group->hash);
7776 }
7777
7778 static int
7779 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7780 {
7781 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7782 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7783
7784 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7785 }
7786
7787 /* Allocate a hash table for type unit groups. */
7788
7789 static htab_t
7790 allocate_type_unit_groups_table (struct objfile *objfile)
7791 {
7792 return htab_create_alloc_ex (3,
7793 hash_type_unit_group,
7794 eq_type_unit_group,
7795 NULL,
7796 &objfile->objfile_obstack,
7797 hashtab_obstack_allocate,
7798 dummy_obstack_deallocate);
7799 }
7800
7801 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7802 partial symtabs. We combine several TUs per psymtab to not let the size
7803 of any one psymtab grow too big. */
7804 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7805 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7806
7807 /* Helper routine for get_type_unit_group.
7808 Create the type_unit_group object used to hold one or more TUs. */
7809
7810 static struct type_unit_group *
7811 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7812 {
7813 struct dwarf2_per_objfile *dwarf2_per_objfile
7814 = cu->per_cu->dwarf2_per_objfile;
7815 struct objfile *objfile = dwarf2_per_objfile->objfile;
7816 struct dwarf2_per_cu_data *per_cu;
7817 struct type_unit_group *tu_group;
7818
7819 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7820 struct type_unit_group);
7821 per_cu = &tu_group->per_cu;
7822 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7823
7824 if (dwarf2_per_objfile->using_index)
7825 {
7826 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7827 struct dwarf2_per_cu_quick_data);
7828 }
7829 else
7830 {
7831 unsigned int line_offset = to_underlying (line_offset_struct);
7832 struct partial_symtab *pst;
7833 std::string name;
7834
7835 /* Give the symtab a useful name for debug purposes. */
7836 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7837 name = string_printf ("<type_units_%d>",
7838 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7839 else
7840 name = string_printf ("<type_units_at_0x%x>", line_offset);
7841
7842 pst = create_partial_symtab (per_cu, name.c_str ());
7843 pst->anonymous = 1;
7844 }
7845
7846 tu_group->hash.dwo_unit = cu->dwo_unit;
7847 tu_group->hash.line_sect_off = line_offset_struct;
7848
7849 return tu_group;
7850 }
7851
7852 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7853 STMT_LIST is a DW_AT_stmt_list attribute. */
7854
7855 static struct type_unit_group *
7856 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7857 {
7858 struct dwarf2_per_objfile *dwarf2_per_objfile
7859 = cu->per_cu->dwarf2_per_objfile;
7860 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7861 struct type_unit_group *tu_group;
7862 void **slot;
7863 unsigned int line_offset;
7864 struct type_unit_group type_unit_group_for_lookup;
7865
7866 if (dwarf2_per_objfile->type_unit_groups == NULL)
7867 {
7868 dwarf2_per_objfile->type_unit_groups =
7869 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7870 }
7871
7872 /* Do we need to create a new group, or can we use an existing one? */
7873
7874 if (stmt_list)
7875 {
7876 line_offset = DW_UNSND (stmt_list);
7877 ++tu_stats->nr_symtab_sharers;
7878 }
7879 else
7880 {
7881 /* Ugh, no stmt_list. Rare, but we have to handle it.
7882 We can do various things here like create one group per TU or
7883 spread them over multiple groups to split up the expansion work.
7884 To avoid worst case scenarios (too many groups or too large groups)
7885 we, umm, group them in bunches. */
7886 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7887 | (tu_stats->nr_stmt_less_type_units
7888 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7889 ++tu_stats->nr_stmt_less_type_units;
7890 }
7891
7892 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7893 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7894 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7895 &type_unit_group_for_lookup, INSERT);
7896 if (*slot != NULL)
7897 {
7898 tu_group = (struct type_unit_group *) *slot;
7899 gdb_assert (tu_group != NULL);
7900 }
7901 else
7902 {
7903 sect_offset line_offset_struct = (sect_offset) line_offset;
7904 tu_group = create_type_unit_group (cu, line_offset_struct);
7905 *slot = tu_group;
7906 ++tu_stats->nr_symtabs;
7907 }
7908
7909 return tu_group;
7910 }
7911 \f
7912 /* Partial symbol tables. */
7913
7914 /* Create a psymtab named NAME and assign it to PER_CU.
7915
7916 The caller must fill in the following details:
7917 dirname, textlow, texthigh. */
7918
7919 static struct partial_symtab *
7920 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7921 {
7922 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7923 struct partial_symtab *pst;
7924
7925 pst = start_psymtab_common (objfile, name, 0);
7926
7927 pst->psymtabs_addrmap_supported = 1;
7928
7929 /* This is the glue that links PST into GDB's symbol API. */
7930 pst->read_symtab_private = per_cu;
7931 pst->read_symtab = dwarf2_read_symtab;
7932 per_cu->v.psymtab = pst;
7933
7934 return pst;
7935 }
7936
7937 /* The DATA object passed to process_psymtab_comp_unit_reader has this
7938 type. */
7939
7940 struct process_psymtab_comp_unit_data
7941 {
7942 /* True if we are reading a DW_TAG_partial_unit. */
7943
7944 int want_partial_unit;
7945
7946 /* The "pretend" language that is used if the CU doesn't declare a
7947 language. */
7948
7949 enum language pretend_language;
7950 };
7951
7952 /* die_reader_func for process_psymtab_comp_unit. */
7953
7954 static void
7955 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7956 const gdb_byte *info_ptr,
7957 struct die_info *comp_unit_die,
7958 int has_children,
7959 void *data)
7960 {
7961 struct dwarf2_cu *cu = reader->cu;
7962 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7963 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7964 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7965 CORE_ADDR baseaddr;
7966 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7967 struct partial_symtab *pst;
7968 enum pc_bounds_kind cu_bounds_kind;
7969 const char *filename;
7970 struct process_psymtab_comp_unit_data *info
7971 = (struct process_psymtab_comp_unit_data *) data;
7972
7973 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
7974 return;
7975
7976 gdb_assert (! per_cu->is_debug_types);
7977
7978 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
7979
7980 /* Allocate a new partial symbol table structure. */
7981 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7982 if (filename == NULL)
7983 filename = "";
7984
7985 pst = create_partial_symtab (per_cu, filename);
7986
7987 /* This must be done before calling dwarf2_build_include_psymtabs. */
7988 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7989
7990 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7991
7992 dwarf2_find_base_address (comp_unit_die, cu);
7993
7994 /* Possibly set the default values of LOWPC and HIGHPC from
7995 `DW_AT_ranges'. */
7996 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7997 &best_highpc, cu, pst);
7998 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7999 {
8000 CORE_ADDR low
8001 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8002 - baseaddr);
8003 CORE_ADDR high
8004 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8005 - baseaddr - 1);
8006 /* Store the contiguous range if it is not empty; it can be
8007 empty for CUs with no code. */
8008 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8009 low, high, pst);
8010 }
8011
8012 /* Check if comp unit has_children.
8013 If so, read the rest of the partial symbols from this comp unit.
8014 If not, there's no more debug_info for this comp unit. */
8015 if (has_children)
8016 {
8017 struct partial_die_info *first_die;
8018 CORE_ADDR lowpc, highpc;
8019
8020 lowpc = ((CORE_ADDR) -1);
8021 highpc = ((CORE_ADDR) 0);
8022
8023 first_die = load_partial_dies (reader, info_ptr, 1);
8024
8025 scan_partial_symbols (first_die, &lowpc, &highpc,
8026 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8027
8028 /* If we didn't find a lowpc, set it to highpc to avoid
8029 complaints from `maint check'. */
8030 if (lowpc == ((CORE_ADDR) -1))
8031 lowpc = highpc;
8032
8033 /* If the compilation unit didn't have an explicit address range,
8034 then use the information extracted from its child dies. */
8035 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8036 {
8037 best_lowpc = lowpc;
8038 best_highpc = highpc;
8039 }
8040 }
8041 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
8042 best_lowpc + baseaddr)
8043 - baseaddr);
8044 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
8045 best_highpc + baseaddr)
8046 - baseaddr);
8047
8048 end_psymtab_common (objfile, pst);
8049
8050 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8051 {
8052 int i;
8053 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8054 struct dwarf2_per_cu_data *iter;
8055
8056 /* Fill in 'dependencies' here; we fill in 'users' in a
8057 post-pass. */
8058 pst->number_of_dependencies = len;
8059 pst->dependencies
8060 = objfile->partial_symtabs->allocate_dependencies (len);
8061 for (i = 0;
8062 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8063 i, iter);
8064 ++i)
8065 pst->dependencies[i] = iter->v.psymtab;
8066
8067 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8068 }
8069
8070 /* Get the list of files included in the current compilation unit,
8071 and build a psymtab for each of them. */
8072 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8073
8074 if (dwarf_read_debug)
8075 fprintf_unfiltered (gdb_stdlog,
8076 "Psymtab for %s unit @%s: %s - %s"
8077 ", %d global, %d static syms\n",
8078 per_cu->is_debug_types ? "type" : "comp",
8079 sect_offset_str (per_cu->sect_off),
8080 paddress (gdbarch, pst->text_low (objfile)),
8081 paddress (gdbarch, pst->text_high (objfile)),
8082 pst->n_global_syms, pst->n_static_syms);
8083 }
8084
8085 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8086 Process compilation unit THIS_CU for a psymtab. */
8087
8088 static void
8089 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8090 int want_partial_unit,
8091 enum language pretend_language)
8092 {
8093 /* If this compilation unit was already read in, free the
8094 cached copy in order to read it in again. This is
8095 necessary because we skipped some symbols when we first
8096 read in the compilation unit (see load_partial_dies).
8097 This problem could be avoided, but the benefit is unclear. */
8098 if (this_cu->cu != NULL)
8099 free_one_cached_comp_unit (this_cu);
8100
8101 if (this_cu->is_debug_types)
8102 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8103 build_type_psymtabs_reader, NULL);
8104 else
8105 {
8106 process_psymtab_comp_unit_data info;
8107 info.want_partial_unit = want_partial_unit;
8108 info.pretend_language = pretend_language;
8109 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8110 process_psymtab_comp_unit_reader, &info);
8111 }
8112
8113 /* Age out any secondary CUs. */
8114 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8115 }
8116
8117 /* Reader function for build_type_psymtabs. */
8118
8119 static void
8120 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8121 const gdb_byte *info_ptr,
8122 struct die_info *type_unit_die,
8123 int has_children,
8124 void *data)
8125 {
8126 struct dwarf2_per_objfile *dwarf2_per_objfile
8127 = reader->cu->per_cu->dwarf2_per_objfile;
8128 struct objfile *objfile = dwarf2_per_objfile->objfile;
8129 struct dwarf2_cu *cu = reader->cu;
8130 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8131 struct signatured_type *sig_type;
8132 struct type_unit_group *tu_group;
8133 struct attribute *attr;
8134 struct partial_die_info *first_die;
8135 CORE_ADDR lowpc, highpc;
8136 struct partial_symtab *pst;
8137
8138 gdb_assert (data == NULL);
8139 gdb_assert (per_cu->is_debug_types);
8140 sig_type = (struct signatured_type *) per_cu;
8141
8142 if (! has_children)
8143 return;
8144
8145 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8146 tu_group = get_type_unit_group (cu, attr);
8147
8148 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8149
8150 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8151 pst = create_partial_symtab (per_cu, "");
8152 pst->anonymous = 1;
8153
8154 first_die = load_partial_dies (reader, info_ptr, 1);
8155
8156 lowpc = (CORE_ADDR) -1;
8157 highpc = (CORE_ADDR) 0;
8158 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8159
8160 end_psymtab_common (objfile, pst);
8161 }
8162
8163 /* Struct used to sort TUs by their abbreviation table offset. */
8164
8165 struct tu_abbrev_offset
8166 {
8167 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8168 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8169 {}
8170
8171 signatured_type *sig_type;
8172 sect_offset abbrev_offset;
8173 };
8174
8175 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8176
8177 static bool
8178 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8179 const struct tu_abbrev_offset &b)
8180 {
8181 return a.abbrev_offset < b.abbrev_offset;
8182 }
8183
8184 /* Efficiently read all the type units.
8185 This does the bulk of the work for build_type_psymtabs.
8186
8187 The efficiency is because we sort TUs by the abbrev table they use and
8188 only read each abbrev table once. In one program there are 200K TUs
8189 sharing 8K abbrev tables.
8190
8191 The main purpose of this function is to support building the
8192 dwarf2_per_objfile->type_unit_groups table.
8193 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8194 can collapse the search space by grouping them by stmt_list.
8195 The savings can be significant, in the same program from above the 200K TUs
8196 share 8K stmt_list tables.
8197
8198 FUNC is expected to call get_type_unit_group, which will create the
8199 struct type_unit_group if necessary and add it to
8200 dwarf2_per_objfile->type_unit_groups. */
8201
8202 static void
8203 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8204 {
8205 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8206 abbrev_table_up abbrev_table;
8207 sect_offset abbrev_offset;
8208
8209 /* It's up to the caller to not call us multiple times. */
8210 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8211
8212 if (dwarf2_per_objfile->all_type_units.empty ())
8213 return;
8214
8215 /* TUs typically share abbrev tables, and there can be way more TUs than
8216 abbrev tables. Sort by abbrev table to reduce the number of times we
8217 read each abbrev table in.
8218 Alternatives are to punt or to maintain a cache of abbrev tables.
8219 This is simpler and efficient enough for now.
8220
8221 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8222 symtab to use). Typically TUs with the same abbrev offset have the same
8223 stmt_list value too so in practice this should work well.
8224
8225 The basic algorithm here is:
8226
8227 sort TUs by abbrev table
8228 for each TU with same abbrev table:
8229 read abbrev table if first user
8230 read TU top level DIE
8231 [IWBN if DWO skeletons had DW_AT_stmt_list]
8232 call FUNC */
8233
8234 if (dwarf_read_debug)
8235 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8236
8237 /* Sort in a separate table to maintain the order of all_type_units
8238 for .gdb_index: TU indices directly index all_type_units. */
8239 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8240 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8241
8242 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8243 sorted_by_abbrev.emplace_back
8244 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8245 sig_type->per_cu.section,
8246 sig_type->per_cu.sect_off));
8247
8248 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8249 sort_tu_by_abbrev_offset);
8250
8251 abbrev_offset = (sect_offset) ~(unsigned) 0;
8252
8253 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8254 {
8255 /* Switch to the next abbrev table if necessary. */
8256 if (abbrev_table == NULL
8257 || tu.abbrev_offset != abbrev_offset)
8258 {
8259 abbrev_offset = tu.abbrev_offset;
8260 abbrev_table =
8261 abbrev_table_read_table (dwarf2_per_objfile,
8262 &dwarf2_per_objfile->abbrev,
8263 abbrev_offset);
8264 ++tu_stats->nr_uniq_abbrev_tables;
8265 }
8266
8267 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
8268 0, 0, false, build_type_psymtabs_reader, NULL);
8269 }
8270 }
8271
8272 /* Print collected type unit statistics. */
8273
8274 static void
8275 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8276 {
8277 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8278
8279 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8280 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8281 dwarf2_per_objfile->all_type_units.size ());
8282 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8283 tu_stats->nr_uniq_abbrev_tables);
8284 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8285 tu_stats->nr_symtabs);
8286 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8287 tu_stats->nr_symtab_sharers);
8288 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8289 tu_stats->nr_stmt_less_type_units);
8290 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8291 tu_stats->nr_all_type_units_reallocs);
8292 }
8293
8294 /* Traversal function for build_type_psymtabs. */
8295
8296 static int
8297 build_type_psymtab_dependencies (void **slot, void *info)
8298 {
8299 struct dwarf2_per_objfile *dwarf2_per_objfile
8300 = (struct dwarf2_per_objfile *) info;
8301 struct objfile *objfile = dwarf2_per_objfile->objfile;
8302 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8303 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8304 struct partial_symtab *pst = per_cu->v.psymtab;
8305 int len = VEC_length (sig_type_ptr, tu_group->tus);
8306 struct signatured_type *iter;
8307 int i;
8308
8309 gdb_assert (len > 0);
8310 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8311
8312 pst->number_of_dependencies = len;
8313 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8314 for (i = 0;
8315 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8316 ++i)
8317 {
8318 gdb_assert (iter->per_cu.is_debug_types);
8319 pst->dependencies[i] = iter->per_cu.v.psymtab;
8320 iter->type_unit_group = tu_group;
8321 }
8322
8323 VEC_free (sig_type_ptr, tu_group->tus);
8324
8325 return 1;
8326 }
8327
8328 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8329 Build partial symbol tables for the .debug_types comp-units. */
8330
8331 static void
8332 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8333 {
8334 if (! create_all_type_units (dwarf2_per_objfile))
8335 return;
8336
8337 build_type_psymtabs_1 (dwarf2_per_objfile);
8338 }
8339
8340 /* Traversal function for process_skeletonless_type_unit.
8341 Read a TU in a DWO file and build partial symbols for it. */
8342
8343 static int
8344 process_skeletonless_type_unit (void **slot, void *info)
8345 {
8346 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8347 struct dwarf2_per_objfile *dwarf2_per_objfile
8348 = (struct dwarf2_per_objfile *) info;
8349 struct signatured_type find_entry, *entry;
8350
8351 /* If this TU doesn't exist in the global table, add it and read it in. */
8352
8353 if (dwarf2_per_objfile->signatured_types == NULL)
8354 {
8355 dwarf2_per_objfile->signatured_types
8356 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8357 }
8358
8359 find_entry.signature = dwo_unit->signature;
8360 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8361 INSERT);
8362 /* If we've already seen this type there's nothing to do. What's happening
8363 is we're doing our own version of comdat-folding here. */
8364 if (*slot != NULL)
8365 return 1;
8366
8367 /* This does the job that create_all_type_units would have done for
8368 this TU. */
8369 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8370 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8371 *slot = entry;
8372
8373 /* This does the job that build_type_psymtabs_1 would have done. */
8374 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
8375 build_type_psymtabs_reader, NULL);
8376
8377 return 1;
8378 }
8379
8380 /* Traversal function for process_skeletonless_type_units. */
8381
8382 static int
8383 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8384 {
8385 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8386
8387 if (dwo_file->tus != NULL)
8388 {
8389 htab_traverse_noresize (dwo_file->tus,
8390 process_skeletonless_type_unit, info);
8391 }
8392
8393 return 1;
8394 }
8395
8396 /* Scan all TUs of DWO files, verifying we've processed them.
8397 This is needed in case a TU was emitted without its skeleton.
8398 Note: This can't be done until we know what all the DWO files are. */
8399
8400 static void
8401 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8402 {
8403 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8404 if (get_dwp_file (dwarf2_per_objfile) == NULL
8405 && dwarf2_per_objfile->dwo_files != NULL)
8406 {
8407 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
8408 process_dwo_file_for_skeletonless_type_units,
8409 dwarf2_per_objfile);
8410 }
8411 }
8412
8413 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8414
8415 static void
8416 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8417 {
8418 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8419 {
8420 struct partial_symtab *pst = per_cu->v.psymtab;
8421
8422 if (pst == NULL)
8423 continue;
8424
8425 for (int j = 0; j < pst->number_of_dependencies; ++j)
8426 {
8427 /* Set the 'user' field only if it is not already set. */
8428 if (pst->dependencies[j]->user == NULL)
8429 pst->dependencies[j]->user = pst;
8430 }
8431 }
8432 }
8433
8434 /* Build the partial symbol table by doing a quick pass through the
8435 .debug_info and .debug_abbrev sections. */
8436
8437 static void
8438 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8439 {
8440 struct objfile *objfile = dwarf2_per_objfile->objfile;
8441
8442 if (dwarf_read_debug)
8443 {
8444 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8445 objfile_name (objfile));
8446 }
8447
8448 dwarf2_per_objfile->reading_partial_symbols = 1;
8449
8450 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8451
8452 /* Any cached compilation units will be linked by the per-objfile
8453 read_in_chain. Make sure to free them when we're done. */
8454 free_cached_comp_units freer (dwarf2_per_objfile);
8455
8456 build_type_psymtabs (dwarf2_per_objfile);
8457
8458 create_all_comp_units (dwarf2_per_objfile);
8459
8460 /* Create a temporary address map on a temporary obstack. We later
8461 copy this to the final obstack. */
8462 auto_obstack temp_obstack;
8463
8464 scoped_restore save_psymtabs_addrmap
8465 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8466 addrmap_create_mutable (&temp_obstack));
8467
8468 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8469 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8470
8471 /* This has to wait until we read the CUs, we need the list of DWOs. */
8472 process_skeletonless_type_units (dwarf2_per_objfile);
8473
8474 /* Now that all TUs have been processed we can fill in the dependencies. */
8475 if (dwarf2_per_objfile->type_unit_groups != NULL)
8476 {
8477 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8478 build_type_psymtab_dependencies, dwarf2_per_objfile);
8479 }
8480
8481 if (dwarf_read_debug)
8482 print_tu_stats (dwarf2_per_objfile);
8483
8484 set_partial_user (dwarf2_per_objfile);
8485
8486 objfile->partial_symtabs->psymtabs_addrmap
8487 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8488 objfile->partial_symtabs->obstack ());
8489 /* At this point we want to keep the address map. */
8490 save_psymtabs_addrmap.release ();
8491
8492 if (dwarf_read_debug)
8493 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8494 objfile_name (objfile));
8495 }
8496
8497 /* die_reader_func for load_partial_comp_unit. */
8498
8499 static void
8500 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8501 const gdb_byte *info_ptr,
8502 struct die_info *comp_unit_die,
8503 int has_children,
8504 void *data)
8505 {
8506 struct dwarf2_cu *cu = reader->cu;
8507
8508 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8509
8510 /* Check if comp unit has_children.
8511 If so, read the rest of the partial symbols from this comp unit.
8512 If not, there's no more debug_info for this comp unit. */
8513 if (has_children)
8514 load_partial_dies (reader, info_ptr, 0);
8515 }
8516
8517 /* Load the partial DIEs for a secondary CU into memory.
8518 This is also used when rereading a primary CU with load_all_dies. */
8519
8520 static void
8521 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8522 {
8523 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
8524 load_partial_comp_unit_reader, NULL);
8525 }
8526
8527 static void
8528 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8529 struct dwarf2_section_info *section,
8530 struct dwarf2_section_info *abbrev_section,
8531 unsigned int is_dwz)
8532 {
8533 const gdb_byte *info_ptr;
8534 struct objfile *objfile = dwarf2_per_objfile->objfile;
8535
8536 if (dwarf_read_debug)
8537 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8538 get_section_name (section),
8539 get_section_file_name (section));
8540
8541 dwarf2_read_section (objfile, section);
8542
8543 info_ptr = section->buffer;
8544
8545 while (info_ptr < section->buffer + section->size)
8546 {
8547 struct dwarf2_per_cu_data *this_cu;
8548
8549 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8550
8551 comp_unit_head cu_header;
8552 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8553 abbrev_section, info_ptr,
8554 rcuh_kind::COMPILE);
8555
8556 /* Save the compilation unit for later lookup. */
8557 if (cu_header.unit_type != DW_UT_type)
8558 {
8559 this_cu = XOBNEW (&objfile->objfile_obstack,
8560 struct dwarf2_per_cu_data);
8561 memset (this_cu, 0, sizeof (*this_cu));
8562 }
8563 else
8564 {
8565 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8566 struct signatured_type);
8567 memset (sig_type, 0, sizeof (*sig_type));
8568 sig_type->signature = cu_header.signature;
8569 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8570 this_cu = &sig_type->per_cu;
8571 }
8572 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8573 this_cu->sect_off = sect_off;
8574 this_cu->length = cu_header.length + cu_header.initial_length_size;
8575 this_cu->is_dwz = is_dwz;
8576 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8577 this_cu->section = section;
8578
8579 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8580
8581 info_ptr = info_ptr + this_cu->length;
8582 }
8583 }
8584
8585 /* Create a list of all compilation units in OBJFILE.
8586 This is only done for -readnow and building partial symtabs. */
8587
8588 static void
8589 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8590 {
8591 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8592 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8593 &dwarf2_per_objfile->abbrev, 0);
8594
8595 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8596 if (dwz != NULL)
8597 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8598 1);
8599 }
8600
8601 /* Process all loaded DIEs for compilation unit CU, starting at
8602 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8603 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8604 DW_AT_ranges). See the comments of add_partial_subprogram on how
8605 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8606
8607 static void
8608 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8609 CORE_ADDR *highpc, int set_addrmap,
8610 struct dwarf2_cu *cu)
8611 {
8612 struct partial_die_info *pdi;
8613
8614 /* Now, march along the PDI's, descending into ones which have
8615 interesting children but skipping the children of the other ones,
8616 until we reach the end of the compilation unit. */
8617
8618 pdi = first_die;
8619
8620 while (pdi != NULL)
8621 {
8622 pdi->fixup (cu);
8623
8624 /* Anonymous namespaces or modules have no name but have interesting
8625 children, so we need to look at them. Ditto for anonymous
8626 enums. */
8627
8628 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8629 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8630 || pdi->tag == DW_TAG_imported_unit
8631 || pdi->tag == DW_TAG_inlined_subroutine)
8632 {
8633 switch (pdi->tag)
8634 {
8635 case DW_TAG_subprogram:
8636 case DW_TAG_inlined_subroutine:
8637 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8638 break;
8639 case DW_TAG_constant:
8640 case DW_TAG_variable:
8641 case DW_TAG_typedef:
8642 case DW_TAG_union_type:
8643 if (!pdi->is_declaration)
8644 {
8645 add_partial_symbol (pdi, cu);
8646 }
8647 break;
8648 case DW_TAG_class_type:
8649 case DW_TAG_interface_type:
8650 case DW_TAG_structure_type:
8651 if (!pdi->is_declaration)
8652 {
8653 add_partial_symbol (pdi, cu);
8654 }
8655 if ((cu->language == language_rust
8656 || cu->language == language_cplus) && pdi->has_children)
8657 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8658 set_addrmap, cu);
8659 break;
8660 case DW_TAG_enumeration_type:
8661 if (!pdi->is_declaration)
8662 add_partial_enumeration (pdi, cu);
8663 break;
8664 case DW_TAG_base_type:
8665 case DW_TAG_subrange_type:
8666 /* File scope base type definitions are added to the partial
8667 symbol table. */
8668 add_partial_symbol (pdi, cu);
8669 break;
8670 case DW_TAG_namespace:
8671 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8672 break;
8673 case DW_TAG_module:
8674 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8675 break;
8676 case DW_TAG_imported_unit:
8677 {
8678 struct dwarf2_per_cu_data *per_cu;
8679
8680 /* For now we don't handle imported units in type units. */
8681 if (cu->per_cu->is_debug_types)
8682 {
8683 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8684 " supported in type units [in module %s]"),
8685 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8686 }
8687
8688 per_cu = dwarf2_find_containing_comp_unit
8689 (pdi->d.sect_off, pdi->is_dwz,
8690 cu->per_cu->dwarf2_per_objfile);
8691
8692 /* Go read the partial unit, if needed. */
8693 if (per_cu->v.psymtab == NULL)
8694 process_psymtab_comp_unit (per_cu, 1, cu->language);
8695
8696 VEC_safe_push (dwarf2_per_cu_ptr,
8697 cu->per_cu->imported_symtabs, per_cu);
8698 }
8699 break;
8700 case DW_TAG_imported_declaration:
8701 add_partial_symbol (pdi, cu);
8702 break;
8703 default:
8704 break;
8705 }
8706 }
8707
8708 /* If the die has a sibling, skip to the sibling. */
8709
8710 pdi = pdi->die_sibling;
8711 }
8712 }
8713
8714 /* Functions used to compute the fully scoped name of a partial DIE.
8715
8716 Normally, this is simple. For C++, the parent DIE's fully scoped
8717 name is concatenated with "::" and the partial DIE's name.
8718 Enumerators are an exception; they use the scope of their parent
8719 enumeration type, i.e. the name of the enumeration type is not
8720 prepended to the enumerator.
8721
8722 There are two complexities. One is DW_AT_specification; in this
8723 case "parent" means the parent of the target of the specification,
8724 instead of the direct parent of the DIE. The other is compilers
8725 which do not emit DW_TAG_namespace; in this case we try to guess
8726 the fully qualified name of structure types from their members'
8727 linkage names. This must be done using the DIE's children rather
8728 than the children of any DW_AT_specification target. We only need
8729 to do this for structures at the top level, i.e. if the target of
8730 any DW_AT_specification (if any; otherwise the DIE itself) does not
8731 have a parent. */
8732
8733 /* Compute the scope prefix associated with PDI's parent, in
8734 compilation unit CU. The result will be allocated on CU's
8735 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8736 field. NULL is returned if no prefix is necessary. */
8737 static const char *
8738 partial_die_parent_scope (struct partial_die_info *pdi,
8739 struct dwarf2_cu *cu)
8740 {
8741 const char *grandparent_scope;
8742 struct partial_die_info *parent, *real_pdi;
8743
8744 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8745 then this means the parent of the specification DIE. */
8746
8747 real_pdi = pdi;
8748 while (real_pdi->has_specification)
8749 {
8750 auto res = find_partial_die (real_pdi->spec_offset,
8751 real_pdi->spec_is_dwz, cu);
8752 real_pdi = res.pdi;
8753 cu = res.cu;
8754 }
8755
8756 parent = real_pdi->die_parent;
8757 if (parent == NULL)
8758 return NULL;
8759
8760 if (parent->scope_set)
8761 return parent->scope;
8762
8763 parent->fixup (cu);
8764
8765 grandparent_scope = partial_die_parent_scope (parent, cu);
8766
8767 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8768 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8769 Work around this problem here. */
8770 if (cu->language == language_cplus
8771 && parent->tag == DW_TAG_namespace
8772 && strcmp (parent->name, "::") == 0
8773 && grandparent_scope == NULL)
8774 {
8775 parent->scope = NULL;
8776 parent->scope_set = 1;
8777 return NULL;
8778 }
8779
8780 if (pdi->tag == DW_TAG_enumerator)
8781 /* Enumerators should not get the name of the enumeration as a prefix. */
8782 parent->scope = grandparent_scope;
8783 else if (parent->tag == DW_TAG_namespace
8784 || parent->tag == DW_TAG_module
8785 || parent->tag == DW_TAG_structure_type
8786 || parent->tag == DW_TAG_class_type
8787 || parent->tag == DW_TAG_interface_type
8788 || parent->tag == DW_TAG_union_type
8789 || parent->tag == DW_TAG_enumeration_type)
8790 {
8791 if (grandparent_scope == NULL)
8792 parent->scope = parent->name;
8793 else
8794 parent->scope = typename_concat (&cu->comp_unit_obstack,
8795 grandparent_scope,
8796 parent->name, 0, cu);
8797 }
8798 else
8799 {
8800 /* FIXME drow/2004-04-01: What should we be doing with
8801 function-local names? For partial symbols, we should probably be
8802 ignoring them. */
8803 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8804 dwarf_tag_name (parent->tag),
8805 sect_offset_str (pdi->sect_off));
8806 parent->scope = grandparent_scope;
8807 }
8808
8809 parent->scope_set = 1;
8810 return parent->scope;
8811 }
8812
8813 /* Return the fully scoped name associated with PDI, from compilation unit
8814 CU. The result will be allocated with malloc. */
8815
8816 static char *
8817 partial_die_full_name (struct partial_die_info *pdi,
8818 struct dwarf2_cu *cu)
8819 {
8820 const char *parent_scope;
8821
8822 /* If this is a template instantiation, we can not work out the
8823 template arguments from partial DIEs. So, unfortunately, we have
8824 to go through the full DIEs. At least any work we do building
8825 types here will be reused if full symbols are loaded later. */
8826 if (pdi->has_template_arguments)
8827 {
8828 pdi->fixup (cu);
8829
8830 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8831 {
8832 struct die_info *die;
8833 struct attribute attr;
8834 struct dwarf2_cu *ref_cu = cu;
8835
8836 /* DW_FORM_ref_addr is using section offset. */
8837 attr.name = (enum dwarf_attribute) 0;
8838 attr.form = DW_FORM_ref_addr;
8839 attr.u.unsnd = to_underlying (pdi->sect_off);
8840 die = follow_die_ref (NULL, &attr, &ref_cu);
8841
8842 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8843 }
8844 }
8845
8846 parent_scope = partial_die_parent_scope (pdi, cu);
8847 if (parent_scope == NULL)
8848 return NULL;
8849 else
8850 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
8851 }
8852
8853 static void
8854 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8855 {
8856 struct dwarf2_per_objfile *dwarf2_per_objfile
8857 = cu->per_cu->dwarf2_per_objfile;
8858 struct objfile *objfile = dwarf2_per_objfile->objfile;
8859 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8860 CORE_ADDR addr = 0;
8861 const char *actual_name = NULL;
8862 CORE_ADDR baseaddr;
8863 char *built_actual_name;
8864
8865 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8866
8867 built_actual_name = partial_die_full_name (pdi, cu);
8868 if (built_actual_name != NULL)
8869 actual_name = built_actual_name;
8870
8871 if (actual_name == NULL)
8872 actual_name = pdi->name;
8873
8874 switch (pdi->tag)
8875 {
8876 case DW_TAG_inlined_subroutine:
8877 case DW_TAG_subprogram:
8878 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8879 - baseaddr);
8880 if (pdi->is_external || cu->language == language_ada)
8881 {
8882 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8883 of the global scope. But in Ada, we want to be able to access
8884 nested procedures globally. So all Ada subprograms are stored
8885 in the global scope. */
8886 add_psymbol_to_list (actual_name, strlen (actual_name),
8887 built_actual_name != NULL,
8888 VAR_DOMAIN, LOC_BLOCK,
8889 SECT_OFF_TEXT (objfile),
8890 psymbol_placement::GLOBAL,
8891 addr,
8892 cu->language, objfile);
8893 }
8894 else
8895 {
8896 add_psymbol_to_list (actual_name, strlen (actual_name),
8897 built_actual_name != NULL,
8898 VAR_DOMAIN, LOC_BLOCK,
8899 SECT_OFF_TEXT (objfile),
8900 psymbol_placement::STATIC,
8901 addr, cu->language, objfile);
8902 }
8903
8904 if (pdi->main_subprogram && actual_name != NULL)
8905 set_objfile_main_name (objfile, actual_name, cu->language);
8906 break;
8907 case DW_TAG_constant:
8908 add_psymbol_to_list (actual_name, strlen (actual_name),
8909 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8910 -1, (pdi->is_external
8911 ? psymbol_placement::GLOBAL
8912 : psymbol_placement::STATIC),
8913 0, cu->language, objfile);
8914 break;
8915 case DW_TAG_variable:
8916 if (pdi->d.locdesc)
8917 addr = decode_locdesc (pdi->d.locdesc, cu);
8918
8919 if (pdi->d.locdesc
8920 && addr == 0
8921 && !dwarf2_per_objfile->has_section_at_zero)
8922 {
8923 /* A global or static variable may also have been stripped
8924 out by the linker if unused, in which case its address
8925 will be nullified; do not add such variables into partial
8926 symbol table then. */
8927 }
8928 else if (pdi->is_external)
8929 {
8930 /* Global Variable.
8931 Don't enter into the minimal symbol tables as there is
8932 a minimal symbol table entry from the ELF symbols already.
8933 Enter into partial symbol table if it has a location
8934 descriptor or a type.
8935 If the location descriptor is missing, new_symbol will create
8936 a LOC_UNRESOLVED symbol, the address of the variable will then
8937 be determined from the minimal symbol table whenever the variable
8938 is referenced.
8939 The address for the partial symbol table entry is not
8940 used by GDB, but it comes in handy for debugging partial symbol
8941 table building. */
8942
8943 if (pdi->d.locdesc || pdi->has_type)
8944 add_psymbol_to_list (actual_name, strlen (actual_name),
8945 built_actual_name != NULL,
8946 VAR_DOMAIN, LOC_STATIC,
8947 SECT_OFF_TEXT (objfile),
8948 psymbol_placement::GLOBAL,
8949 addr, cu->language, objfile);
8950 }
8951 else
8952 {
8953 int has_loc = pdi->d.locdesc != NULL;
8954
8955 /* Static Variable. Skip symbols whose value we cannot know (those
8956 without location descriptors or constant values). */
8957 if (!has_loc && !pdi->has_const_value)
8958 {
8959 xfree (built_actual_name);
8960 return;
8961 }
8962
8963 add_psymbol_to_list (actual_name, strlen (actual_name),
8964 built_actual_name != NULL,
8965 VAR_DOMAIN, LOC_STATIC,
8966 SECT_OFF_TEXT (objfile),
8967 psymbol_placement::STATIC,
8968 has_loc ? addr : 0,
8969 cu->language, objfile);
8970 }
8971 break;
8972 case DW_TAG_typedef:
8973 case DW_TAG_base_type:
8974 case DW_TAG_subrange_type:
8975 add_psymbol_to_list (actual_name, strlen (actual_name),
8976 built_actual_name != NULL,
8977 VAR_DOMAIN, LOC_TYPEDEF, -1,
8978 psymbol_placement::STATIC,
8979 0, cu->language, objfile);
8980 break;
8981 case DW_TAG_imported_declaration:
8982 case DW_TAG_namespace:
8983 add_psymbol_to_list (actual_name, strlen (actual_name),
8984 built_actual_name != NULL,
8985 VAR_DOMAIN, LOC_TYPEDEF, -1,
8986 psymbol_placement::GLOBAL,
8987 0, cu->language, objfile);
8988 break;
8989 case DW_TAG_module:
8990 /* With Fortran 77 there might be a "BLOCK DATA" module
8991 available without any name. If so, we skip the module as it
8992 doesn't bring any value. */
8993 if (actual_name != nullptr)
8994 add_psymbol_to_list (actual_name, strlen (actual_name),
8995 built_actual_name != NULL,
8996 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8997 psymbol_placement::GLOBAL,
8998 0, cu->language, objfile);
8999 break;
9000 case DW_TAG_class_type:
9001 case DW_TAG_interface_type:
9002 case DW_TAG_structure_type:
9003 case DW_TAG_union_type:
9004 case DW_TAG_enumeration_type:
9005 /* Skip external references. The DWARF standard says in the section
9006 about "Structure, Union, and Class Type Entries": "An incomplete
9007 structure, union or class type is represented by a structure,
9008 union or class entry that does not have a byte size attribute
9009 and that has a DW_AT_declaration attribute." */
9010 if (!pdi->has_byte_size && pdi->is_declaration)
9011 {
9012 xfree (built_actual_name);
9013 return;
9014 }
9015
9016 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9017 static vs. global. */
9018 add_psymbol_to_list (actual_name, strlen (actual_name),
9019 built_actual_name != NULL,
9020 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9021 cu->language == language_cplus
9022 ? psymbol_placement::GLOBAL
9023 : psymbol_placement::STATIC,
9024 0, cu->language, objfile);
9025
9026 break;
9027 case DW_TAG_enumerator:
9028 add_psymbol_to_list (actual_name, strlen (actual_name),
9029 built_actual_name != NULL,
9030 VAR_DOMAIN, LOC_CONST, -1,
9031 cu->language == language_cplus
9032 ? psymbol_placement::GLOBAL
9033 : psymbol_placement::STATIC,
9034 0, cu->language, objfile);
9035 break;
9036 default:
9037 break;
9038 }
9039
9040 xfree (built_actual_name);
9041 }
9042
9043 /* Read a partial die corresponding to a namespace; also, add a symbol
9044 corresponding to that namespace to the symbol table. NAMESPACE is
9045 the name of the enclosing namespace. */
9046
9047 static void
9048 add_partial_namespace (struct partial_die_info *pdi,
9049 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9050 int set_addrmap, struct dwarf2_cu *cu)
9051 {
9052 /* Add a symbol for the namespace. */
9053
9054 add_partial_symbol (pdi, cu);
9055
9056 /* Now scan partial symbols in that namespace. */
9057
9058 if (pdi->has_children)
9059 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9060 }
9061
9062 /* Read a partial die corresponding to a Fortran module. */
9063
9064 static void
9065 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9066 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9067 {
9068 /* Add a symbol for the namespace. */
9069
9070 add_partial_symbol (pdi, cu);
9071
9072 /* Now scan partial symbols in that module. */
9073
9074 if (pdi->has_children)
9075 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9076 }
9077
9078 /* Read a partial die corresponding to a subprogram or an inlined
9079 subprogram and create a partial symbol for that subprogram.
9080 When the CU language allows it, this routine also defines a partial
9081 symbol for each nested subprogram that this subprogram contains.
9082 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9083 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9084
9085 PDI may also be a lexical block, in which case we simply search
9086 recursively for subprograms defined inside that lexical block.
9087 Again, this is only performed when the CU language allows this
9088 type of definitions. */
9089
9090 static void
9091 add_partial_subprogram (struct partial_die_info *pdi,
9092 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9093 int set_addrmap, struct dwarf2_cu *cu)
9094 {
9095 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9096 {
9097 if (pdi->has_pc_info)
9098 {
9099 if (pdi->lowpc < *lowpc)
9100 *lowpc = pdi->lowpc;
9101 if (pdi->highpc > *highpc)
9102 *highpc = pdi->highpc;
9103 if (set_addrmap)
9104 {
9105 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9106 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9107 CORE_ADDR baseaddr;
9108 CORE_ADDR this_highpc;
9109 CORE_ADDR this_lowpc;
9110
9111 baseaddr = ANOFFSET (objfile->section_offsets,
9112 SECT_OFF_TEXT (objfile));
9113 this_lowpc
9114 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9115 pdi->lowpc + baseaddr)
9116 - baseaddr);
9117 this_highpc
9118 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9119 pdi->highpc + baseaddr)
9120 - baseaddr);
9121 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
9122 this_lowpc, this_highpc - 1,
9123 cu->per_cu->v.psymtab);
9124 }
9125 }
9126
9127 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9128 {
9129 if (!pdi->is_declaration)
9130 /* Ignore subprogram DIEs that do not have a name, they are
9131 illegal. Do not emit a complaint at this point, we will
9132 do so when we convert this psymtab into a symtab. */
9133 if (pdi->name)
9134 add_partial_symbol (pdi, cu);
9135 }
9136 }
9137
9138 if (! pdi->has_children)
9139 return;
9140
9141 if (cu->language == language_ada)
9142 {
9143 pdi = pdi->die_child;
9144 while (pdi != NULL)
9145 {
9146 pdi->fixup (cu);
9147 if (pdi->tag == DW_TAG_subprogram
9148 || pdi->tag == DW_TAG_inlined_subroutine
9149 || pdi->tag == DW_TAG_lexical_block)
9150 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9151 pdi = pdi->die_sibling;
9152 }
9153 }
9154 }
9155
9156 /* Read a partial die corresponding to an enumeration type. */
9157
9158 static void
9159 add_partial_enumeration (struct partial_die_info *enum_pdi,
9160 struct dwarf2_cu *cu)
9161 {
9162 struct partial_die_info *pdi;
9163
9164 if (enum_pdi->name != NULL)
9165 add_partial_symbol (enum_pdi, cu);
9166
9167 pdi = enum_pdi->die_child;
9168 while (pdi)
9169 {
9170 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9171 complaint (_("malformed enumerator DIE ignored"));
9172 else
9173 add_partial_symbol (pdi, cu);
9174 pdi = pdi->die_sibling;
9175 }
9176 }
9177
9178 /* Return the initial uleb128 in the die at INFO_PTR. */
9179
9180 static unsigned int
9181 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9182 {
9183 unsigned int bytes_read;
9184
9185 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9186 }
9187
9188 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9189 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9190
9191 Return the corresponding abbrev, or NULL if the number is zero (indicating
9192 an empty DIE). In either case *BYTES_READ will be set to the length of
9193 the initial number. */
9194
9195 static struct abbrev_info *
9196 peek_die_abbrev (const die_reader_specs &reader,
9197 const gdb_byte *info_ptr, unsigned int *bytes_read)
9198 {
9199 dwarf2_cu *cu = reader.cu;
9200 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9201 unsigned int abbrev_number
9202 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9203
9204 if (abbrev_number == 0)
9205 return NULL;
9206
9207 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9208 if (!abbrev)
9209 {
9210 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9211 " at offset %s [in module %s]"),
9212 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9213 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9214 }
9215
9216 return abbrev;
9217 }
9218
9219 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9220 Returns a pointer to the end of a series of DIEs, terminated by an empty
9221 DIE. Any children of the skipped DIEs will also be skipped. */
9222
9223 static const gdb_byte *
9224 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9225 {
9226 while (1)
9227 {
9228 unsigned int bytes_read;
9229 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9230
9231 if (abbrev == NULL)
9232 return info_ptr + bytes_read;
9233 else
9234 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9235 }
9236 }
9237
9238 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9239 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9240 abbrev corresponding to that skipped uleb128 should be passed in
9241 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9242 children. */
9243
9244 static const gdb_byte *
9245 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9246 struct abbrev_info *abbrev)
9247 {
9248 unsigned int bytes_read;
9249 struct attribute attr;
9250 bfd *abfd = reader->abfd;
9251 struct dwarf2_cu *cu = reader->cu;
9252 const gdb_byte *buffer = reader->buffer;
9253 const gdb_byte *buffer_end = reader->buffer_end;
9254 unsigned int form, i;
9255
9256 for (i = 0; i < abbrev->num_attrs; i++)
9257 {
9258 /* The only abbrev we care about is DW_AT_sibling. */
9259 if (abbrev->attrs[i].name == DW_AT_sibling)
9260 {
9261 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9262 if (attr.form == DW_FORM_ref_addr)
9263 complaint (_("ignoring absolute DW_AT_sibling"));
9264 else
9265 {
9266 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9267 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9268
9269 if (sibling_ptr < info_ptr)
9270 complaint (_("DW_AT_sibling points backwards"));
9271 else if (sibling_ptr > reader->buffer_end)
9272 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9273 else
9274 return sibling_ptr;
9275 }
9276 }
9277
9278 /* If it isn't DW_AT_sibling, skip this attribute. */
9279 form = abbrev->attrs[i].form;
9280 skip_attribute:
9281 switch (form)
9282 {
9283 case DW_FORM_ref_addr:
9284 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9285 and later it is offset sized. */
9286 if (cu->header.version == 2)
9287 info_ptr += cu->header.addr_size;
9288 else
9289 info_ptr += cu->header.offset_size;
9290 break;
9291 case DW_FORM_GNU_ref_alt:
9292 info_ptr += cu->header.offset_size;
9293 break;
9294 case DW_FORM_addr:
9295 info_ptr += cu->header.addr_size;
9296 break;
9297 case DW_FORM_data1:
9298 case DW_FORM_ref1:
9299 case DW_FORM_flag:
9300 info_ptr += 1;
9301 break;
9302 case DW_FORM_flag_present:
9303 case DW_FORM_implicit_const:
9304 break;
9305 case DW_FORM_data2:
9306 case DW_FORM_ref2:
9307 info_ptr += 2;
9308 break;
9309 case DW_FORM_data4:
9310 case DW_FORM_ref4:
9311 info_ptr += 4;
9312 break;
9313 case DW_FORM_data8:
9314 case DW_FORM_ref8:
9315 case DW_FORM_ref_sig8:
9316 info_ptr += 8;
9317 break;
9318 case DW_FORM_data16:
9319 info_ptr += 16;
9320 break;
9321 case DW_FORM_string:
9322 read_direct_string (abfd, info_ptr, &bytes_read);
9323 info_ptr += bytes_read;
9324 break;
9325 case DW_FORM_sec_offset:
9326 case DW_FORM_strp:
9327 case DW_FORM_GNU_strp_alt:
9328 info_ptr += cu->header.offset_size;
9329 break;
9330 case DW_FORM_exprloc:
9331 case DW_FORM_block:
9332 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9333 info_ptr += bytes_read;
9334 break;
9335 case DW_FORM_block1:
9336 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9337 break;
9338 case DW_FORM_block2:
9339 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9340 break;
9341 case DW_FORM_block4:
9342 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9343 break;
9344 case DW_FORM_addrx:
9345 case DW_FORM_strx:
9346 case DW_FORM_sdata:
9347 case DW_FORM_udata:
9348 case DW_FORM_ref_udata:
9349 case DW_FORM_GNU_addr_index:
9350 case DW_FORM_GNU_str_index:
9351 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9352 break;
9353 case DW_FORM_indirect:
9354 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9355 info_ptr += bytes_read;
9356 /* We need to continue parsing from here, so just go back to
9357 the top. */
9358 goto skip_attribute;
9359
9360 default:
9361 error (_("Dwarf Error: Cannot handle %s "
9362 "in DWARF reader [in module %s]"),
9363 dwarf_form_name (form),
9364 bfd_get_filename (abfd));
9365 }
9366 }
9367
9368 if (abbrev->has_children)
9369 return skip_children (reader, info_ptr);
9370 else
9371 return info_ptr;
9372 }
9373
9374 /* Locate ORIG_PDI's sibling.
9375 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9376
9377 static const gdb_byte *
9378 locate_pdi_sibling (const struct die_reader_specs *reader,
9379 struct partial_die_info *orig_pdi,
9380 const gdb_byte *info_ptr)
9381 {
9382 /* Do we know the sibling already? */
9383
9384 if (orig_pdi->sibling)
9385 return orig_pdi->sibling;
9386
9387 /* Are there any children to deal with? */
9388
9389 if (!orig_pdi->has_children)
9390 return info_ptr;
9391
9392 /* Skip the children the long way. */
9393
9394 return skip_children (reader, info_ptr);
9395 }
9396
9397 /* Expand this partial symbol table into a full symbol table. SELF is
9398 not NULL. */
9399
9400 static void
9401 dwarf2_read_symtab (struct partial_symtab *self,
9402 struct objfile *objfile)
9403 {
9404 struct dwarf2_per_objfile *dwarf2_per_objfile
9405 = get_dwarf2_per_objfile (objfile);
9406
9407 if (self->readin)
9408 {
9409 warning (_("bug: psymtab for %s is already read in."),
9410 self->filename);
9411 }
9412 else
9413 {
9414 if (info_verbose)
9415 {
9416 printf_filtered (_("Reading in symbols for %s..."),
9417 self->filename);
9418 gdb_flush (gdb_stdout);
9419 }
9420
9421 /* If this psymtab is constructed from a debug-only objfile, the
9422 has_section_at_zero flag will not necessarily be correct. We
9423 can get the correct value for this flag by looking at the data
9424 associated with the (presumably stripped) associated objfile. */
9425 if (objfile->separate_debug_objfile_backlink)
9426 {
9427 struct dwarf2_per_objfile *dpo_backlink
9428 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9429
9430 dwarf2_per_objfile->has_section_at_zero
9431 = dpo_backlink->has_section_at_zero;
9432 }
9433
9434 dwarf2_per_objfile->reading_partial_symbols = 0;
9435
9436 psymtab_to_symtab_1 (self);
9437
9438 /* Finish up the debug error message. */
9439 if (info_verbose)
9440 printf_filtered (_("done.\n"));
9441 }
9442
9443 process_cu_includes (dwarf2_per_objfile);
9444 }
9445 \f
9446 /* Reading in full CUs. */
9447
9448 /* Add PER_CU to the queue. */
9449
9450 static void
9451 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9452 enum language pretend_language)
9453 {
9454 struct dwarf2_queue_item *item;
9455
9456 per_cu->queued = 1;
9457 item = XNEW (struct dwarf2_queue_item);
9458 item->per_cu = per_cu;
9459 item->pretend_language = pretend_language;
9460 item->next = NULL;
9461
9462 if (dwarf2_queue == NULL)
9463 dwarf2_queue = item;
9464 else
9465 dwarf2_queue_tail->next = item;
9466
9467 dwarf2_queue_tail = item;
9468 }
9469
9470 /* If PER_CU is not yet queued, add it to the queue.
9471 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9472 dependency.
9473 The result is non-zero if PER_CU was queued, otherwise the result is zero
9474 meaning either PER_CU is already queued or it is already loaded.
9475
9476 N.B. There is an invariant here that if a CU is queued then it is loaded.
9477 The caller is required to load PER_CU if we return non-zero. */
9478
9479 static int
9480 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9481 struct dwarf2_per_cu_data *per_cu,
9482 enum language pretend_language)
9483 {
9484 /* We may arrive here during partial symbol reading, if we need full
9485 DIEs to process an unusual case (e.g. template arguments). Do
9486 not queue PER_CU, just tell our caller to load its DIEs. */
9487 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9488 {
9489 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9490 return 1;
9491 return 0;
9492 }
9493
9494 /* Mark the dependence relation so that we don't flush PER_CU
9495 too early. */
9496 if (dependent_cu != NULL)
9497 dwarf2_add_dependence (dependent_cu, per_cu);
9498
9499 /* If it's already on the queue, we have nothing to do. */
9500 if (per_cu->queued)
9501 return 0;
9502
9503 /* If the compilation unit is already loaded, just mark it as
9504 used. */
9505 if (per_cu->cu != NULL)
9506 {
9507 per_cu->cu->last_used = 0;
9508 return 0;
9509 }
9510
9511 /* Add it to the queue. */
9512 queue_comp_unit (per_cu, pretend_language);
9513
9514 return 1;
9515 }
9516
9517 /* Process the queue. */
9518
9519 static void
9520 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9521 {
9522 struct dwarf2_queue_item *item, *next_item;
9523
9524 if (dwarf_read_debug)
9525 {
9526 fprintf_unfiltered (gdb_stdlog,
9527 "Expanding one or more symtabs of objfile %s ...\n",
9528 objfile_name (dwarf2_per_objfile->objfile));
9529 }
9530
9531 /* The queue starts out with one item, but following a DIE reference
9532 may load a new CU, adding it to the end of the queue. */
9533 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9534 {
9535 if ((dwarf2_per_objfile->using_index
9536 ? !item->per_cu->v.quick->compunit_symtab
9537 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9538 /* Skip dummy CUs. */
9539 && item->per_cu->cu != NULL)
9540 {
9541 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9542 unsigned int debug_print_threshold;
9543 char buf[100];
9544
9545 if (per_cu->is_debug_types)
9546 {
9547 struct signatured_type *sig_type =
9548 (struct signatured_type *) per_cu;
9549
9550 sprintf (buf, "TU %s at offset %s",
9551 hex_string (sig_type->signature),
9552 sect_offset_str (per_cu->sect_off));
9553 /* There can be 100s of TUs.
9554 Only print them in verbose mode. */
9555 debug_print_threshold = 2;
9556 }
9557 else
9558 {
9559 sprintf (buf, "CU at offset %s",
9560 sect_offset_str (per_cu->sect_off));
9561 debug_print_threshold = 1;
9562 }
9563
9564 if (dwarf_read_debug >= debug_print_threshold)
9565 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9566
9567 if (per_cu->is_debug_types)
9568 process_full_type_unit (per_cu, item->pretend_language);
9569 else
9570 process_full_comp_unit (per_cu, item->pretend_language);
9571
9572 if (dwarf_read_debug >= debug_print_threshold)
9573 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9574 }
9575
9576 item->per_cu->queued = 0;
9577 next_item = item->next;
9578 xfree (item);
9579 }
9580
9581 dwarf2_queue_tail = NULL;
9582
9583 if (dwarf_read_debug)
9584 {
9585 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9586 objfile_name (dwarf2_per_objfile->objfile));
9587 }
9588 }
9589
9590 /* Read in full symbols for PST, and anything it depends on. */
9591
9592 static void
9593 psymtab_to_symtab_1 (struct partial_symtab *pst)
9594 {
9595 struct dwarf2_per_cu_data *per_cu;
9596 int i;
9597
9598 if (pst->readin)
9599 return;
9600
9601 for (i = 0; i < pst->number_of_dependencies; i++)
9602 if (!pst->dependencies[i]->readin
9603 && pst->dependencies[i]->user == NULL)
9604 {
9605 /* Inform about additional files that need to be read in. */
9606 if (info_verbose)
9607 {
9608 /* FIXME: i18n: Need to make this a single string. */
9609 fputs_filtered (" ", gdb_stdout);
9610 wrap_here ("");
9611 fputs_filtered ("and ", gdb_stdout);
9612 wrap_here ("");
9613 printf_filtered ("%s...", pst->dependencies[i]->filename);
9614 wrap_here (""); /* Flush output. */
9615 gdb_flush (gdb_stdout);
9616 }
9617 psymtab_to_symtab_1 (pst->dependencies[i]);
9618 }
9619
9620 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9621
9622 if (per_cu == NULL)
9623 {
9624 /* It's an include file, no symbols to read for it.
9625 Everything is in the parent symtab. */
9626 pst->readin = 1;
9627 return;
9628 }
9629
9630 dw2_do_instantiate_symtab (per_cu, false);
9631 }
9632
9633 /* Trivial hash function for die_info: the hash value of a DIE
9634 is its offset in .debug_info for this objfile. */
9635
9636 static hashval_t
9637 die_hash (const void *item)
9638 {
9639 const struct die_info *die = (const struct die_info *) item;
9640
9641 return to_underlying (die->sect_off);
9642 }
9643
9644 /* Trivial comparison function for die_info structures: two DIEs
9645 are equal if they have the same offset. */
9646
9647 static int
9648 die_eq (const void *item_lhs, const void *item_rhs)
9649 {
9650 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9651 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9652
9653 return die_lhs->sect_off == die_rhs->sect_off;
9654 }
9655
9656 /* die_reader_func for load_full_comp_unit.
9657 This is identical to read_signatured_type_reader,
9658 but is kept separate for now. */
9659
9660 static void
9661 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9662 const gdb_byte *info_ptr,
9663 struct die_info *comp_unit_die,
9664 int has_children,
9665 void *data)
9666 {
9667 struct dwarf2_cu *cu = reader->cu;
9668 enum language *language_ptr = (enum language *) data;
9669
9670 gdb_assert (cu->die_hash == NULL);
9671 cu->die_hash =
9672 htab_create_alloc_ex (cu->header.length / 12,
9673 die_hash,
9674 die_eq,
9675 NULL,
9676 &cu->comp_unit_obstack,
9677 hashtab_obstack_allocate,
9678 dummy_obstack_deallocate);
9679
9680 if (has_children)
9681 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9682 &info_ptr, comp_unit_die);
9683 cu->dies = comp_unit_die;
9684 /* comp_unit_die is not stored in die_hash, no need. */
9685
9686 /* We try not to read any attributes in this function, because not
9687 all CUs needed for references have been loaded yet, and symbol
9688 table processing isn't initialized. But we have to set the CU language,
9689 or we won't be able to build types correctly.
9690 Similarly, if we do not read the producer, we can not apply
9691 producer-specific interpretation. */
9692 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
9693 }
9694
9695 /* Load the DIEs associated with PER_CU into memory. */
9696
9697 static void
9698 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9699 bool skip_partial,
9700 enum language pretend_language)
9701 {
9702 gdb_assert (! this_cu->is_debug_types);
9703
9704 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
9705 load_full_comp_unit_reader, &pretend_language);
9706 }
9707
9708 /* Add a DIE to the delayed physname list. */
9709
9710 static void
9711 add_to_method_list (struct type *type, int fnfield_index, int index,
9712 const char *name, struct die_info *die,
9713 struct dwarf2_cu *cu)
9714 {
9715 struct delayed_method_info mi;
9716 mi.type = type;
9717 mi.fnfield_index = fnfield_index;
9718 mi.index = index;
9719 mi.name = name;
9720 mi.die = die;
9721 cu->method_list.push_back (mi);
9722 }
9723
9724 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9725 "const" / "volatile". If so, decrements LEN by the length of the
9726 modifier and return true. Otherwise return false. */
9727
9728 template<size_t N>
9729 static bool
9730 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9731 {
9732 size_t mod_len = sizeof (mod) - 1;
9733 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9734 {
9735 len -= mod_len;
9736 return true;
9737 }
9738 return false;
9739 }
9740
9741 /* Compute the physnames of any methods on the CU's method list.
9742
9743 The computation of method physnames is delayed in order to avoid the
9744 (bad) condition that one of the method's formal parameters is of an as yet
9745 incomplete type. */
9746
9747 static void
9748 compute_delayed_physnames (struct dwarf2_cu *cu)
9749 {
9750 /* Only C++ delays computing physnames. */
9751 if (cu->method_list.empty ())
9752 return;
9753 gdb_assert (cu->language == language_cplus);
9754
9755 for (const delayed_method_info &mi : cu->method_list)
9756 {
9757 const char *physname;
9758 struct fn_fieldlist *fn_flp
9759 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9760 physname = dwarf2_physname (mi.name, mi.die, cu);
9761 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9762 = physname ? physname : "";
9763
9764 /* Since there's no tag to indicate whether a method is a
9765 const/volatile overload, extract that information out of the
9766 demangled name. */
9767 if (physname != NULL)
9768 {
9769 size_t len = strlen (physname);
9770
9771 while (1)
9772 {
9773 if (physname[len] == ')') /* shortcut */
9774 break;
9775 else if (check_modifier (physname, len, " const"))
9776 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9777 else if (check_modifier (physname, len, " volatile"))
9778 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9779 else
9780 break;
9781 }
9782 }
9783 }
9784
9785 /* The list is no longer needed. */
9786 cu->method_list.clear ();
9787 }
9788
9789 /* Go objects should be embedded in a DW_TAG_module DIE,
9790 and it's not clear if/how imported objects will appear.
9791 To keep Go support simple until that's worked out,
9792 go back through what we've read and create something usable.
9793 We could do this while processing each DIE, and feels kinda cleaner,
9794 but that way is more invasive.
9795 This is to, for example, allow the user to type "p var" or "b main"
9796 without having to specify the package name, and allow lookups
9797 of module.object to work in contexts that use the expression
9798 parser. */
9799
9800 static void
9801 fixup_go_packaging (struct dwarf2_cu *cu)
9802 {
9803 char *package_name = NULL;
9804 struct pending *list;
9805 int i;
9806
9807 for (list = *cu->get_builder ()->get_global_symbols ();
9808 list != NULL;
9809 list = list->next)
9810 {
9811 for (i = 0; i < list->nsyms; ++i)
9812 {
9813 struct symbol *sym = list->symbol[i];
9814
9815 if (SYMBOL_LANGUAGE (sym) == language_go
9816 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9817 {
9818 char *this_package_name = go_symbol_package_name (sym);
9819
9820 if (this_package_name == NULL)
9821 continue;
9822 if (package_name == NULL)
9823 package_name = this_package_name;
9824 else
9825 {
9826 struct objfile *objfile
9827 = cu->per_cu->dwarf2_per_objfile->objfile;
9828 if (strcmp (package_name, this_package_name) != 0)
9829 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9830 (symbol_symtab (sym) != NULL
9831 ? symtab_to_filename_for_display
9832 (symbol_symtab (sym))
9833 : objfile_name (objfile)),
9834 this_package_name, package_name);
9835 xfree (this_package_name);
9836 }
9837 }
9838 }
9839 }
9840
9841 if (package_name != NULL)
9842 {
9843 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9844 const char *saved_package_name
9845 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name);
9846 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9847 saved_package_name);
9848 struct symbol *sym;
9849
9850 sym = allocate_symbol (objfile);
9851 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
9852 SYMBOL_SET_NAMES (sym, saved_package_name,
9853 strlen (saved_package_name), 0, objfile);
9854 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9855 e.g., "main" finds the "main" module and not C's main(). */
9856 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9857 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9858 SYMBOL_TYPE (sym) = type;
9859
9860 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9861
9862 xfree (package_name);
9863 }
9864 }
9865
9866 /* Allocate a fully-qualified name consisting of the two parts on the
9867 obstack. */
9868
9869 static const char *
9870 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9871 {
9872 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9873 }
9874
9875 /* A helper that allocates a struct discriminant_info to attach to a
9876 union type. */
9877
9878 static struct discriminant_info *
9879 alloc_discriminant_info (struct type *type, int discriminant_index,
9880 int default_index)
9881 {
9882 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9883 gdb_assert (discriminant_index == -1
9884 || (discriminant_index >= 0
9885 && discriminant_index < TYPE_NFIELDS (type)));
9886 gdb_assert (default_index == -1
9887 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9888
9889 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9890
9891 struct discriminant_info *disc
9892 = ((struct discriminant_info *)
9893 TYPE_ZALLOC (type,
9894 offsetof (struct discriminant_info, discriminants)
9895 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9896 disc->default_index = default_index;
9897 disc->discriminant_index = discriminant_index;
9898
9899 struct dynamic_prop prop;
9900 prop.kind = PROP_UNDEFINED;
9901 prop.data.baton = disc;
9902
9903 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9904
9905 return disc;
9906 }
9907
9908 /* Some versions of rustc emitted enums in an unusual way.
9909
9910 Ordinary enums were emitted as unions. The first element of each
9911 structure in the union was named "RUST$ENUM$DISR". This element
9912 held the discriminant.
9913
9914 These versions of Rust also implemented the "non-zero"
9915 optimization. When the enum had two values, and one is empty and
9916 the other holds a pointer that cannot be zero, the pointer is used
9917 as the discriminant, with a zero value meaning the empty variant.
9918 Here, the union's first member is of the form
9919 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9920 where the fieldnos are the indices of the fields that should be
9921 traversed in order to find the field (which may be several fields deep)
9922 and the variantname is the name of the variant of the case when the
9923 field is zero.
9924
9925 This function recognizes whether TYPE is of one of these forms,
9926 and, if so, smashes it to be a variant type. */
9927
9928 static void
9929 quirk_rust_enum (struct type *type, struct objfile *objfile)
9930 {
9931 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9932
9933 /* We don't need to deal with empty enums. */
9934 if (TYPE_NFIELDS (type) == 0)
9935 return;
9936
9937 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9938 if (TYPE_NFIELDS (type) == 1
9939 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9940 {
9941 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9942
9943 /* Decode the field name to find the offset of the
9944 discriminant. */
9945 ULONGEST bit_offset = 0;
9946 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9947 while (name[0] >= '0' && name[0] <= '9')
9948 {
9949 char *tail;
9950 unsigned long index = strtoul (name, &tail, 10);
9951 name = tail;
9952 if (*name != '$'
9953 || index >= TYPE_NFIELDS (field_type)
9954 || (TYPE_FIELD_LOC_KIND (field_type, index)
9955 != FIELD_LOC_KIND_BITPOS))
9956 {
9957 complaint (_("Could not parse Rust enum encoding string \"%s\""
9958 "[in module %s]"),
9959 TYPE_FIELD_NAME (type, 0),
9960 objfile_name (objfile));
9961 return;
9962 }
9963 ++name;
9964
9965 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9966 field_type = TYPE_FIELD_TYPE (field_type, index);
9967 }
9968
9969 /* Make a union to hold the variants. */
9970 struct type *union_type = alloc_type (objfile);
9971 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9972 TYPE_NFIELDS (union_type) = 3;
9973 TYPE_FIELDS (union_type)
9974 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9975 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9976 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9977
9978 /* Put the discriminant must at index 0. */
9979 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9980 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9981 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9982 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9983
9984 /* The order of fields doesn't really matter, so put the real
9985 field at index 1 and the data-less field at index 2. */
9986 struct discriminant_info *disc
9987 = alloc_discriminant_info (union_type, 0, 1);
9988 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9989 TYPE_FIELD_NAME (union_type, 1)
9990 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9991 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9992 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9993 TYPE_FIELD_NAME (union_type, 1));
9994
9995 const char *dataless_name
9996 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9997 name);
9998 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9999 dataless_name);
10000 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10001 /* NAME points into the original discriminant name, which
10002 already has the correct lifetime. */
10003 TYPE_FIELD_NAME (union_type, 2) = name;
10004 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10005 disc->discriminants[2] = 0;
10006
10007 /* Smash this type to be a structure type. We have to do this
10008 because the type has already been recorded. */
10009 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10010 TYPE_NFIELDS (type) = 1;
10011 TYPE_FIELDS (type)
10012 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10013
10014 /* Install the variant part. */
10015 TYPE_FIELD_TYPE (type, 0) = union_type;
10016 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10017 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10018 }
10019 else if (TYPE_NFIELDS (type) == 1)
10020 {
10021 /* We assume that a union with a single field is a univariant
10022 enum. */
10023 /* Smash this type to be a structure type. We have to do this
10024 because the type has already been recorded. */
10025 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10026
10027 /* Make a union to hold the variants. */
10028 struct type *union_type = alloc_type (objfile);
10029 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10030 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10031 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10032 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10033 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10034
10035 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10036 const char *variant_name
10037 = rust_last_path_segment (TYPE_NAME (field_type));
10038 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10039 TYPE_NAME (field_type)
10040 = rust_fully_qualify (&objfile->objfile_obstack,
10041 TYPE_NAME (type), variant_name);
10042
10043 /* Install the union in the outer struct type. */
10044 TYPE_NFIELDS (type) = 1;
10045 TYPE_FIELDS (type)
10046 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10047 TYPE_FIELD_TYPE (type, 0) = union_type;
10048 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10049 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10050
10051 alloc_discriminant_info (union_type, -1, 0);
10052 }
10053 else
10054 {
10055 struct type *disr_type = nullptr;
10056 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10057 {
10058 disr_type = TYPE_FIELD_TYPE (type, i);
10059
10060 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10061 {
10062 /* All fields of a true enum will be structs. */
10063 return;
10064 }
10065 else if (TYPE_NFIELDS (disr_type) == 0)
10066 {
10067 /* Could be data-less variant, so keep going. */
10068 disr_type = nullptr;
10069 }
10070 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10071 "RUST$ENUM$DISR") != 0)
10072 {
10073 /* Not a Rust enum. */
10074 return;
10075 }
10076 else
10077 {
10078 /* Found one. */
10079 break;
10080 }
10081 }
10082
10083 /* If we got here without a discriminant, then it's probably
10084 just a union. */
10085 if (disr_type == nullptr)
10086 return;
10087
10088 /* Smash this type to be a structure type. We have to do this
10089 because the type has already been recorded. */
10090 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10091
10092 /* Make a union to hold the variants. */
10093 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10094 struct type *union_type = alloc_type (objfile);
10095 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10096 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10097 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10098 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10099 TYPE_FIELDS (union_type)
10100 = (struct field *) TYPE_ZALLOC (union_type,
10101 (TYPE_NFIELDS (union_type)
10102 * sizeof (struct field)));
10103
10104 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10105 TYPE_NFIELDS (type) * sizeof (struct field));
10106
10107 /* Install the discriminant at index 0 in the union. */
10108 TYPE_FIELD (union_type, 0) = *disr_field;
10109 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10110 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10111
10112 /* Install the union in the outer struct type. */
10113 TYPE_FIELD_TYPE (type, 0) = union_type;
10114 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10115 TYPE_NFIELDS (type) = 1;
10116
10117 /* Set the size and offset of the union type. */
10118 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10119
10120 /* We need a way to find the correct discriminant given a
10121 variant name. For convenience we build a map here. */
10122 struct type *enum_type = FIELD_TYPE (*disr_field);
10123 std::unordered_map<std::string, ULONGEST> discriminant_map;
10124 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10125 {
10126 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10127 {
10128 const char *name
10129 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10130 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10131 }
10132 }
10133
10134 int n_fields = TYPE_NFIELDS (union_type);
10135 struct discriminant_info *disc
10136 = alloc_discriminant_info (union_type, 0, -1);
10137 /* Skip the discriminant here. */
10138 for (int i = 1; i < n_fields; ++i)
10139 {
10140 /* Find the final word in the name of this variant's type.
10141 That name can be used to look up the correct
10142 discriminant. */
10143 const char *variant_name
10144 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10145 i)));
10146
10147 auto iter = discriminant_map.find (variant_name);
10148 if (iter != discriminant_map.end ())
10149 disc->discriminants[i] = iter->second;
10150
10151 /* Remove the discriminant field, if it exists. */
10152 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10153 if (TYPE_NFIELDS (sub_type) > 0)
10154 {
10155 --TYPE_NFIELDS (sub_type);
10156 ++TYPE_FIELDS (sub_type);
10157 }
10158 TYPE_FIELD_NAME (union_type, i) = variant_name;
10159 TYPE_NAME (sub_type)
10160 = rust_fully_qualify (&objfile->objfile_obstack,
10161 TYPE_NAME (type), variant_name);
10162 }
10163 }
10164 }
10165
10166 /* Rewrite some Rust unions to be structures with variants parts. */
10167
10168 static void
10169 rust_union_quirks (struct dwarf2_cu *cu)
10170 {
10171 gdb_assert (cu->language == language_rust);
10172 for (type *type_ : cu->rust_unions)
10173 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
10174 /* We don't need this any more. */
10175 cu->rust_unions.clear ();
10176 }
10177
10178 /* Return the symtab for PER_CU. This works properly regardless of
10179 whether we're using the index or psymtabs. */
10180
10181 static struct compunit_symtab *
10182 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10183 {
10184 return (per_cu->dwarf2_per_objfile->using_index
10185 ? per_cu->v.quick->compunit_symtab
10186 : per_cu->v.psymtab->compunit_symtab);
10187 }
10188
10189 /* A helper function for computing the list of all symbol tables
10190 included by PER_CU. */
10191
10192 static void
10193 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
10194 htab_t all_children, htab_t all_type_symtabs,
10195 struct dwarf2_per_cu_data *per_cu,
10196 struct compunit_symtab *immediate_parent)
10197 {
10198 void **slot;
10199 int ix;
10200 struct compunit_symtab *cust;
10201 struct dwarf2_per_cu_data *iter;
10202
10203 slot = htab_find_slot (all_children, per_cu, INSERT);
10204 if (*slot != NULL)
10205 {
10206 /* This inclusion and its children have been processed. */
10207 return;
10208 }
10209
10210 *slot = per_cu;
10211 /* Only add a CU if it has a symbol table. */
10212 cust = get_compunit_symtab (per_cu);
10213 if (cust != NULL)
10214 {
10215 /* If this is a type unit only add its symbol table if we haven't
10216 seen it yet (type unit per_cu's can share symtabs). */
10217 if (per_cu->is_debug_types)
10218 {
10219 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10220 if (*slot == NULL)
10221 {
10222 *slot = cust;
10223 result->push_back (cust);
10224 if (cust->user == NULL)
10225 cust->user = immediate_parent;
10226 }
10227 }
10228 else
10229 {
10230 result->push_back (cust);
10231 if (cust->user == NULL)
10232 cust->user = immediate_parent;
10233 }
10234 }
10235
10236 for (ix = 0;
10237 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10238 ++ix)
10239 {
10240 recursively_compute_inclusions (result, all_children,
10241 all_type_symtabs, iter, cust);
10242 }
10243 }
10244
10245 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10246 PER_CU. */
10247
10248 static void
10249 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10250 {
10251 gdb_assert (! per_cu->is_debug_types);
10252
10253 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10254 {
10255 int ix, len;
10256 struct dwarf2_per_cu_data *per_cu_iter;
10257 std::vector<compunit_symtab *> result_symtabs;
10258 htab_t all_children, all_type_symtabs;
10259 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10260
10261 /* If we don't have a symtab, we can just skip this case. */
10262 if (cust == NULL)
10263 return;
10264
10265 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10266 NULL, xcalloc, xfree);
10267 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10268 NULL, xcalloc, xfree);
10269
10270 for (ix = 0;
10271 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10272 ix, per_cu_iter);
10273 ++ix)
10274 {
10275 recursively_compute_inclusions (&result_symtabs, all_children,
10276 all_type_symtabs, per_cu_iter,
10277 cust);
10278 }
10279
10280 /* Now we have a transitive closure of all the included symtabs. */
10281 len = result_symtabs.size ();
10282 cust->includes
10283 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10284 struct compunit_symtab *, len + 1);
10285 memcpy (cust->includes, result_symtabs.data (),
10286 len * sizeof (compunit_symtab *));
10287 cust->includes[len] = NULL;
10288
10289 htab_delete (all_children);
10290 htab_delete (all_type_symtabs);
10291 }
10292 }
10293
10294 /* Compute the 'includes' field for the symtabs of all the CUs we just
10295 read. */
10296
10297 static void
10298 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10299 {
10300 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
10301 {
10302 if (! iter->is_debug_types)
10303 compute_compunit_symtab_includes (iter);
10304 }
10305
10306 dwarf2_per_objfile->just_read_cus.clear ();
10307 }
10308
10309 /* Generate full symbol information for PER_CU, whose DIEs have
10310 already been loaded into memory. */
10311
10312 static void
10313 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10314 enum language pretend_language)
10315 {
10316 struct dwarf2_cu *cu = per_cu->cu;
10317 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10318 struct objfile *objfile = dwarf2_per_objfile->objfile;
10319 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10320 CORE_ADDR lowpc, highpc;
10321 struct compunit_symtab *cust;
10322 CORE_ADDR baseaddr;
10323 struct block *static_block;
10324 CORE_ADDR addr;
10325
10326 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10327
10328 /* Clear the list here in case something was left over. */
10329 cu->method_list.clear ();
10330
10331 cu->language = pretend_language;
10332 cu->language_defn = language_def (cu->language);
10333
10334 /* Do line number decoding in read_file_scope () */
10335 process_die (cu->dies, cu);
10336
10337 /* For now fudge the Go package. */
10338 if (cu->language == language_go)
10339 fixup_go_packaging (cu);
10340
10341 /* Now that we have processed all the DIEs in the CU, all the types
10342 should be complete, and it should now be safe to compute all of the
10343 physnames. */
10344 compute_delayed_physnames (cu);
10345
10346 if (cu->language == language_rust)
10347 rust_union_quirks (cu);
10348
10349 /* Some compilers don't define a DW_AT_high_pc attribute for the
10350 compilation unit. If the DW_AT_high_pc is missing, synthesize
10351 it, by scanning the DIE's below the compilation unit. */
10352 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10353
10354 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10355 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10356
10357 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10358 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10359 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10360 addrmap to help ensure it has an accurate map of pc values belonging to
10361 this comp unit. */
10362 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10363
10364 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10365 SECT_OFF_TEXT (objfile),
10366 0);
10367
10368 if (cust != NULL)
10369 {
10370 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10371
10372 /* Set symtab language to language from DW_AT_language. If the
10373 compilation is from a C file generated by language preprocessors, do
10374 not set the language if it was already deduced by start_subfile. */
10375 if (!(cu->language == language_c
10376 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10377 COMPUNIT_FILETABS (cust)->language = cu->language;
10378
10379 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10380 produce DW_AT_location with location lists but it can be possibly
10381 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10382 there were bugs in prologue debug info, fixed later in GCC-4.5
10383 by "unwind info for epilogues" patch (which is not directly related).
10384
10385 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10386 needed, it would be wrong due to missing DW_AT_producer there.
10387
10388 Still one can confuse GDB by using non-standard GCC compilation
10389 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10390 */
10391 if (cu->has_loclist && gcc_4_minor >= 5)
10392 cust->locations_valid = 1;
10393
10394 if (gcc_4_minor >= 5)
10395 cust->epilogue_unwind_valid = 1;
10396
10397 cust->call_site_htab = cu->call_site_htab;
10398 }
10399
10400 if (dwarf2_per_objfile->using_index)
10401 per_cu->v.quick->compunit_symtab = cust;
10402 else
10403 {
10404 struct partial_symtab *pst = per_cu->v.psymtab;
10405 pst->compunit_symtab = cust;
10406 pst->readin = 1;
10407 }
10408
10409 /* Push it for inclusion processing later. */
10410 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10411
10412 /* Not needed any more. */
10413 cu->reset_builder ();
10414 }
10415
10416 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10417 already been loaded into memory. */
10418
10419 static void
10420 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10421 enum language pretend_language)
10422 {
10423 struct dwarf2_cu *cu = per_cu->cu;
10424 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10425 struct objfile *objfile = dwarf2_per_objfile->objfile;
10426 struct compunit_symtab *cust;
10427 struct signatured_type *sig_type;
10428
10429 gdb_assert (per_cu->is_debug_types);
10430 sig_type = (struct signatured_type *) per_cu;
10431
10432 /* Clear the list here in case something was left over. */
10433 cu->method_list.clear ();
10434
10435 cu->language = pretend_language;
10436 cu->language_defn = language_def (cu->language);
10437
10438 /* The symbol tables are set up in read_type_unit_scope. */
10439 process_die (cu->dies, cu);
10440
10441 /* For now fudge the Go package. */
10442 if (cu->language == language_go)
10443 fixup_go_packaging (cu);
10444
10445 /* Now that we have processed all the DIEs in the CU, all the types
10446 should be complete, and it should now be safe to compute all of the
10447 physnames. */
10448 compute_delayed_physnames (cu);
10449
10450 if (cu->language == language_rust)
10451 rust_union_quirks (cu);
10452
10453 /* TUs share symbol tables.
10454 If this is the first TU to use this symtab, complete the construction
10455 of it with end_expandable_symtab. Otherwise, complete the addition of
10456 this TU's symbols to the existing symtab. */
10457 if (sig_type->type_unit_group->compunit_symtab == NULL)
10458 {
10459 buildsym_compunit *builder = cu->get_builder ();
10460 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10461 sig_type->type_unit_group->compunit_symtab = cust;
10462
10463 if (cust != NULL)
10464 {
10465 /* Set symtab language to language from DW_AT_language. If the
10466 compilation is from a C file generated by language preprocessors,
10467 do not set the language if it was already deduced by
10468 start_subfile. */
10469 if (!(cu->language == language_c
10470 && COMPUNIT_FILETABS (cust)->language != language_c))
10471 COMPUNIT_FILETABS (cust)->language = cu->language;
10472 }
10473 }
10474 else
10475 {
10476 cu->get_builder ()->augment_type_symtab ();
10477 cust = sig_type->type_unit_group->compunit_symtab;
10478 }
10479
10480 if (dwarf2_per_objfile->using_index)
10481 per_cu->v.quick->compunit_symtab = cust;
10482 else
10483 {
10484 struct partial_symtab *pst = per_cu->v.psymtab;
10485 pst->compunit_symtab = cust;
10486 pst->readin = 1;
10487 }
10488
10489 /* Not needed any more. */
10490 cu->reset_builder ();
10491 }
10492
10493 /* Process an imported unit DIE. */
10494
10495 static void
10496 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10497 {
10498 struct attribute *attr;
10499
10500 /* For now we don't handle imported units in type units. */
10501 if (cu->per_cu->is_debug_types)
10502 {
10503 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10504 " supported in type units [in module %s]"),
10505 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10506 }
10507
10508 attr = dwarf2_attr (die, DW_AT_import, cu);
10509 if (attr != NULL)
10510 {
10511 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10512 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10513 dwarf2_per_cu_data *per_cu
10514 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10515 cu->per_cu->dwarf2_per_objfile);
10516
10517 /* If necessary, add it to the queue and load its DIEs. */
10518 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10519 load_full_comp_unit (per_cu, false, cu->language);
10520
10521 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10522 per_cu);
10523 }
10524 }
10525
10526 /* RAII object that represents a process_die scope: i.e.,
10527 starts/finishes processing a DIE. */
10528 class process_die_scope
10529 {
10530 public:
10531 process_die_scope (die_info *die, dwarf2_cu *cu)
10532 : m_die (die), m_cu (cu)
10533 {
10534 /* We should only be processing DIEs not already in process. */
10535 gdb_assert (!m_die->in_process);
10536 m_die->in_process = true;
10537 }
10538
10539 ~process_die_scope ()
10540 {
10541 m_die->in_process = false;
10542
10543 /* If we're done processing the DIE for the CU that owns the line
10544 header, we don't need the line header anymore. */
10545 if (m_cu->line_header_die_owner == m_die)
10546 {
10547 delete m_cu->line_header;
10548 m_cu->line_header = NULL;
10549 m_cu->line_header_die_owner = NULL;
10550 }
10551 }
10552
10553 private:
10554 die_info *m_die;
10555 dwarf2_cu *m_cu;
10556 };
10557
10558 /* Process a die and its children. */
10559
10560 static void
10561 process_die (struct die_info *die, struct dwarf2_cu *cu)
10562 {
10563 process_die_scope scope (die, cu);
10564
10565 switch (die->tag)
10566 {
10567 case DW_TAG_padding:
10568 break;
10569 case DW_TAG_compile_unit:
10570 case DW_TAG_partial_unit:
10571 read_file_scope (die, cu);
10572 break;
10573 case DW_TAG_type_unit:
10574 read_type_unit_scope (die, cu);
10575 break;
10576 case DW_TAG_subprogram:
10577 case DW_TAG_inlined_subroutine:
10578 read_func_scope (die, cu);
10579 break;
10580 case DW_TAG_lexical_block:
10581 case DW_TAG_try_block:
10582 case DW_TAG_catch_block:
10583 read_lexical_block_scope (die, cu);
10584 break;
10585 case DW_TAG_call_site:
10586 case DW_TAG_GNU_call_site:
10587 read_call_site_scope (die, cu);
10588 break;
10589 case DW_TAG_class_type:
10590 case DW_TAG_interface_type:
10591 case DW_TAG_structure_type:
10592 case DW_TAG_union_type:
10593 process_structure_scope (die, cu);
10594 break;
10595 case DW_TAG_enumeration_type:
10596 process_enumeration_scope (die, cu);
10597 break;
10598
10599 /* These dies have a type, but processing them does not create
10600 a symbol or recurse to process the children. Therefore we can
10601 read them on-demand through read_type_die. */
10602 case DW_TAG_subroutine_type:
10603 case DW_TAG_set_type:
10604 case DW_TAG_array_type:
10605 case DW_TAG_pointer_type:
10606 case DW_TAG_ptr_to_member_type:
10607 case DW_TAG_reference_type:
10608 case DW_TAG_rvalue_reference_type:
10609 case DW_TAG_string_type:
10610 break;
10611
10612 case DW_TAG_base_type:
10613 case DW_TAG_subrange_type:
10614 case DW_TAG_typedef:
10615 /* Add a typedef symbol for the type definition, if it has a
10616 DW_AT_name. */
10617 new_symbol (die, read_type_die (die, cu), cu);
10618 break;
10619 case DW_TAG_common_block:
10620 read_common_block (die, cu);
10621 break;
10622 case DW_TAG_common_inclusion:
10623 break;
10624 case DW_TAG_namespace:
10625 cu->processing_has_namespace_info = true;
10626 read_namespace (die, cu);
10627 break;
10628 case DW_TAG_module:
10629 cu->processing_has_namespace_info = true;
10630 read_module (die, cu);
10631 break;
10632 case DW_TAG_imported_declaration:
10633 cu->processing_has_namespace_info = true;
10634 if (read_namespace_alias (die, cu))
10635 break;
10636 /* The declaration is not a global namespace alias. */
10637 /* Fall through. */
10638 case DW_TAG_imported_module:
10639 cu->processing_has_namespace_info = true;
10640 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10641 || cu->language != language_fortran))
10642 complaint (_("Tag '%s' has unexpected children"),
10643 dwarf_tag_name (die->tag));
10644 read_import_statement (die, cu);
10645 break;
10646
10647 case DW_TAG_imported_unit:
10648 process_imported_unit_die (die, cu);
10649 break;
10650
10651 case DW_TAG_variable:
10652 read_variable (die, cu);
10653 break;
10654
10655 default:
10656 new_symbol (die, NULL, cu);
10657 break;
10658 }
10659 }
10660 \f
10661 /* DWARF name computation. */
10662
10663 /* A helper function for dwarf2_compute_name which determines whether DIE
10664 needs to have the name of the scope prepended to the name listed in the
10665 die. */
10666
10667 static int
10668 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10669 {
10670 struct attribute *attr;
10671
10672 switch (die->tag)
10673 {
10674 case DW_TAG_namespace:
10675 case DW_TAG_typedef:
10676 case DW_TAG_class_type:
10677 case DW_TAG_interface_type:
10678 case DW_TAG_structure_type:
10679 case DW_TAG_union_type:
10680 case DW_TAG_enumeration_type:
10681 case DW_TAG_enumerator:
10682 case DW_TAG_subprogram:
10683 case DW_TAG_inlined_subroutine:
10684 case DW_TAG_member:
10685 case DW_TAG_imported_declaration:
10686 return 1;
10687
10688 case DW_TAG_variable:
10689 case DW_TAG_constant:
10690 /* We only need to prefix "globally" visible variables. These include
10691 any variable marked with DW_AT_external or any variable that
10692 lives in a namespace. [Variables in anonymous namespaces
10693 require prefixing, but they are not DW_AT_external.] */
10694
10695 if (dwarf2_attr (die, DW_AT_specification, cu))
10696 {
10697 struct dwarf2_cu *spec_cu = cu;
10698
10699 return die_needs_namespace (die_specification (die, &spec_cu),
10700 spec_cu);
10701 }
10702
10703 attr = dwarf2_attr (die, DW_AT_external, cu);
10704 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10705 && die->parent->tag != DW_TAG_module)
10706 return 0;
10707 /* A variable in a lexical block of some kind does not need a
10708 namespace, even though in C++ such variables may be external
10709 and have a mangled name. */
10710 if (die->parent->tag == DW_TAG_lexical_block
10711 || die->parent->tag == DW_TAG_try_block
10712 || die->parent->tag == DW_TAG_catch_block
10713 || die->parent->tag == DW_TAG_subprogram)
10714 return 0;
10715 return 1;
10716
10717 default:
10718 return 0;
10719 }
10720 }
10721
10722 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10723 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10724 defined for the given DIE. */
10725
10726 static struct attribute *
10727 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10728 {
10729 struct attribute *attr;
10730
10731 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10732 if (attr == NULL)
10733 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10734
10735 return attr;
10736 }
10737
10738 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10739 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10740 defined for the given DIE. */
10741
10742 static const char *
10743 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10744 {
10745 const char *linkage_name;
10746
10747 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10748 if (linkage_name == NULL)
10749 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10750
10751 return linkage_name;
10752 }
10753
10754 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10755 compute the physname for the object, which include a method's:
10756 - formal parameters (C++),
10757 - receiver type (Go),
10758
10759 The term "physname" is a bit confusing.
10760 For C++, for example, it is the demangled name.
10761 For Go, for example, it's the mangled name.
10762
10763 For Ada, return the DIE's linkage name rather than the fully qualified
10764 name. PHYSNAME is ignored..
10765
10766 The result is allocated on the objfile_obstack and canonicalized. */
10767
10768 static const char *
10769 dwarf2_compute_name (const char *name,
10770 struct die_info *die, struct dwarf2_cu *cu,
10771 int physname)
10772 {
10773 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10774
10775 if (name == NULL)
10776 name = dwarf2_name (die, cu);
10777
10778 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10779 but otherwise compute it by typename_concat inside GDB.
10780 FIXME: Actually this is not really true, or at least not always true.
10781 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10782 Fortran names because there is no mangling standard. So new_symbol
10783 will set the demangled name to the result of dwarf2_full_name, and it is
10784 the demangled name that GDB uses if it exists. */
10785 if (cu->language == language_ada
10786 || (cu->language == language_fortran && physname))
10787 {
10788 /* For Ada unit, we prefer the linkage name over the name, as
10789 the former contains the exported name, which the user expects
10790 to be able to reference. Ideally, we want the user to be able
10791 to reference this entity using either natural or linkage name,
10792 but we haven't started looking at this enhancement yet. */
10793 const char *linkage_name = dw2_linkage_name (die, cu);
10794
10795 if (linkage_name != NULL)
10796 return linkage_name;
10797 }
10798
10799 /* These are the only languages we know how to qualify names in. */
10800 if (name != NULL
10801 && (cu->language == language_cplus
10802 || cu->language == language_fortran || cu->language == language_d
10803 || cu->language == language_rust))
10804 {
10805 if (die_needs_namespace (die, cu))
10806 {
10807 const char *prefix;
10808 const char *canonical_name = NULL;
10809
10810 string_file buf;
10811
10812 prefix = determine_prefix (die, cu);
10813 if (*prefix != '\0')
10814 {
10815 char *prefixed_name = typename_concat (NULL, prefix, name,
10816 physname, cu);
10817
10818 buf.puts (prefixed_name);
10819 xfree (prefixed_name);
10820 }
10821 else
10822 buf.puts (name);
10823
10824 /* Template parameters may be specified in the DIE's DW_AT_name, or
10825 as children with DW_TAG_template_type_param or
10826 DW_TAG_value_type_param. If the latter, add them to the name
10827 here. If the name already has template parameters, then
10828 skip this step; some versions of GCC emit both, and
10829 it is more efficient to use the pre-computed name.
10830
10831 Something to keep in mind about this process: it is very
10832 unlikely, or in some cases downright impossible, to produce
10833 something that will match the mangled name of a function.
10834 If the definition of the function has the same debug info,
10835 we should be able to match up with it anyway. But fallbacks
10836 using the minimal symbol, for instance to find a method
10837 implemented in a stripped copy of libstdc++, will not work.
10838 If we do not have debug info for the definition, we will have to
10839 match them up some other way.
10840
10841 When we do name matching there is a related problem with function
10842 templates; two instantiated function templates are allowed to
10843 differ only by their return types, which we do not add here. */
10844
10845 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10846 {
10847 struct attribute *attr;
10848 struct die_info *child;
10849 int first = 1;
10850
10851 die->building_fullname = 1;
10852
10853 for (child = die->child; child != NULL; child = child->sibling)
10854 {
10855 struct type *type;
10856 LONGEST value;
10857 const gdb_byte *bytes;
10858 struct dwarf2_locexpr_baton *baton;
10859 struct value *v;
10860
10861 if (child->tag != DW_TAG_template_type_param
10862 && child->tag != DW_TAG_template_value_param)
10863 continue;
10864
10865 if (first)
10866 {
10867 buf.puts ("<");
10868 first = 0;
10869 }
10870 else
10871 buf.puts (", ");
10872
10873 attr = dwarf2_attr (child, DW_AT_type, cu);
10874 if (attr == NULL)
10875 {
10876 complaint (_("template parameter missing DW_AT_type"));
10877 buf.puts ("UNKNOWN_TYPE");
10878 continue;
10879 }
10880 type = die_type (child, cu);
10881
10882 if (child->tag == DW_TAG_template_type_param)
10883 {
10884 c_print_type (type, "", &buf, -1, 0, cu->language,
10885 &type_print_raw_options);
10886 continue;
10887 }
10888
10889 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10890 if (attr == NULL)
10891 {
10892 complaint (_("template parameter missing "
10893 "DW_AT_const_value"));
10894 buf.puts ("UNKNOWN_VALUE");
10895 continue;
10896 }
10897
10898 dwarf2_const_value_attr (attr, type, name,
10899 &cu->comp_unit_obstack, cu,
10900 &value, &bytes, &baton);
10901
10902 if (TYPE_NOSIGN (type))
10903 /* GDB prints characters as NUMBER 'CHAR'. If that's
10904 changed, this can use value_print instead. */
10905 c_printchar (value, type, &buf);
10906 else
10907 {
10908 struct value_print_options opts;
10909
10910 if (baton != NULL)
10911 v = dwarf2_evaluate_loc_desc (type, NULL,
10912 baton->data,
10913 baton->size,
10914 baton->per_cu);
10915 else if (bytes != NULL)
10916 {
10917 v = allocate_value (type);
10918 memcpy (value_contents_writeable (v), bytes,
10919 TYPE_LENGTH (type));
10920 }
10921 else
10922 v = value_from_longest (type, value);
10923
10924 /* Specify decimal so that we do not depend on
10925 the radix. */
10926 get_formatted_print_options (&opts, 'd');
10927 opts.raw = 1;
10928 value_print (v, &buf, &opts);
10929 release_value (v);
10930 }
10931 }
10932
10933 die->building_fullname = 0;
10934
10935 if (!first)
10936 {
10937 /* Close the argument list, with a space if necessary
10938 (nested templates). */
10939 if (!buf.empty () && buf.string ().back () == '>')
10940 buf.puts (" >");
10941 else
10942 buf.puts (">");
10943 }
10944 }
10945
10946 /* For C++ methods, append formal parameter type
10947 information, if PHYSNAME. */
10948
10949 if (physname && die->tag == DW_TAG_subprogram
10950 && cu->language == language_cplus)
10951 {
10952 struct type *type = read_type_die (die, cu);
10953
10954 c_type_print_args (type, &buf, 1, cu->language,
10955 &type_print_raw_options);
10956
10957 if (cu->language == language_cplus)
10958 {
10959 /* Assume that an artificial first parameter is
10960 "this", but do not crash if it is not. RealView
10961 marks unnamed (and thus unused) parameters as
10962 artificial; there is no way to differentiate
10963 the two cases. */
10964 if (TYPE_NFIELDS (type) > 0
10965 && TYPE_FIELD_ARTIFICIAL (type, 0)
10966 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10967 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10968 0))))
10969 buf.puts (" const");
10970 }
10971 }
10972
10973 const std::string &intermediate_name = buf.string ();
10974
10975 if (cu->language == language_cplus)
10976 canonical_name
10977 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10978 &objfile->per_bfd->storage_obstack);
10979
10980 /* If we only computed INTERMEDIATE_NAME, or if
10981 INTERMEDIATE_NAME is already canonical, then we need to
10982 copy it to the appropriate obstack. */
10983 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10984 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
10985 intermediate_name);
10986 else
10987 name = canonical_name;
10988 }
10989 }
10990
10991 return name;
10992 }
10993
10994 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10995 If scope qualifiers are appropriate they will be added. The result
10996 will be allocated on the storage_obstack, or NULL if the DIE does
10997 not have a name. NAME may either be from a previous call to
10998 dwarf2_name or NULL.
10999
11000 The output string will be canonicalized (if C++). */
11001
11002 static const char *
11003 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11004 {
11005 return dwarf2_compute_name (name, die, cu, 0);
11006 }
11007
11008 /* Construct a physname for the given DIE in CU. NAME may either be
11009 from a previous call to dwarf2_name or NULL. The result will be
11010 allocated on the objfile_objstack or NULL if the DIE does not have a
11011 name.
11012
11013 The output string will be canonicalized (if C++). */
11014
11015 static const char *
11016 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11017 {
11018 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11019 const char *retval, *mangled = NULL, *canon = NULL;
11020 int need_copy = 1;
11021
11022 /* In this case dwarf2_compute_name is just a shortcut not building anything
11023 on its own. */
11024 if (!die_needs_namespace (die, cu))
11025 return dwarf2_compute_name (name, die, cu, 1);
11026
11027 mangled = dw2_linkage_name (die, cu);
11028
11029 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11030 See https://github.com/rust-lang/rust/issues/32925. */
11031 if (cu->language == language_rust && mangled != NULL
11032 && strchr (mangled, '{') != NULL)
11033 mangled = NULL;
11034
11035 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11036 has computed. */
11037 gdb::unique_xmalloc_ptr<char> demangled;
11038 if (mangled != NULL)
11039 {
11040
11041 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11042 {
11043 /* Do nothing (do not demangle the symbol name). */
11044 }
11045 else if (cu->language == language_go)
11046 {
11047 /* This is a lie, but we already lie to the caller new_symbol.
11048 new_symbol assumes we return the mangled name.
11049 This just undoes that lie until things are cleaned up. */
11050 }
11051 else
11052 {
11053 /* Use DMGL_RET_DROP for C++ template functions to suppress
11054 their return type. It is easier for GDB users to search
11055 for such functions as `name(params)' than `long name(params)'.
11056 In such case the minimal symbol names do not match the full
11057 symbol names but for template functions there is never a need
11058 to look up their definition from their declaration so
11059 the only disadvantage remains the minimal symbol variant
11060 `long name(params)' does not have the proper inferior type. */
11061 demangled.reset (gdb_demangle (mangled,
11062 (DMGL_PARAMS | DMGL_ANSI
11063 | DMGL_RET_DROP)));
11064 }
11065 if (demangled)
11066 canon = demangled.get ();
11067 else
11068 {
11069 canon = mangled;
11070 need_copy = 0;
11071 }
11072 }
11073
11074 if (canon == NULL || check_physname)
11075 {
11076 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11077
11078 if (canon != NULL && strcmp (physname, canon) != 0)
11079 {
11080 /* It may not mean a bug in GDB. The compiler could also
11081 compute DW_AT_linkage_name incorrectly. But in such case
11082 GDB would need to be bug-to-bug compatible. */
11083
11084 complaint (_("Computed physname <%s> does not match demangled <%s> "
11085 "(from linkage <%s>) - DIE at %s [in module %s]"),
11086 physname, canon, mangled, sect_offset_str (die->sect_off),
11087 objfile_name (objfile));
11088
11089 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11090 is available here - over computed PHYSNAME. It is safer
11091 against both buggy GDB and buggy compilers. */
11092
11093 retval = canon;
11094 }
11095 else
11096 {
11097 retval = physname;
11098 need_copy = 0;
11099 }
11100 }
11101 else
11102 retval = canon;
11103
11104 if (need_copy)
11105 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
11106
11107 return retval;
11108 }
11109
11110 /* Inspect DIE in CU for a namespace alias. If one exists, record
11111 a new symbol for it.
11112
11113 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11114
11115 static int
11116 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11117 {
11118 struct attribute *attr;
11119
11120 /* If the die does not have a name, this is not a namespace
11121 alias. */
11122 attr = dwarf2_attr (die, DW_AT_name, cu);
11123 if (attr != NULL)
11124 {
11125 int num;
11126 struct die_info *d = die;
11127 struct dwarf2_cu *imported_cu = cu;
11128
11129 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11130 keep inspecting DIEs until we hit the underlying import. */
11131 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11132 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11133 {
11134 attr = dwarf2_attr (d, DW_AT_import, cu);
11135 if (attr == NULL)
11136 break;
11137
11138 d = follow_die_ref (d, attr, &imported_cu);
11139 if (d->tag != DW_TAG_imported_declaration)
11140 break;
11141 }
11142
11143 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11144 {
11145 complaint (_("DIE at %s has too many recursively imported "
11146 "declarations"), sect_offset_str (d->sect_off));
11147 return 0;
11148 }
11149
11150 if (attr != NULL)
11151 {
11152 struct type *type;
11153 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11154
11155 type = get_die_type_at_offset (sect_off, cu->per_cu);
11156 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11157 {
11158 /* This declaration is a global namespace alias. Add
11159 a symbol for it whose type is the aliased namespace. */
11160 new_symbol (die, type, cu);
11161 return 1;
11162 }
11163 }
11164 }
11165
11166 return 0;
11167 }
11168
11169 /* Return the using directives repository (global or local?) to use in the
11170 current context for CU.
11171
11172 For Ada, imported declarations can materialize renamings, which *may* be
11173 global. However it is impossible (for now?) in DWARF to distinguish
11174 "external" imported declarations and "static" ones. As all imported
11175 declarations seem to be static in all other languages, make them all CU-wide
11176 global only in Ada. */
11177
11178 static struct using_direct **
11179 using_directives (struct dwarf2_cu *cu)
11180 {
11181 if (cu->language == language_ada
11182 && cu->get_builder ()->outermost_context_p ())
11183 return cu->get_builder ()->get_global_using_directives ();
11184 else
11185 return cu->get_builder ()->get_local_using_directives ();
11186 }
11187
11188 /* Read the import statement specified by the given die and record it. */
11189
11190 static void
11191 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11192 {
11193 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11194 struct attribute *import_attr;
11195 struct die_info *imported_die, *child_die;
11196 struct dwarf2_cu *imported_cu;
11197 const char *imported_name;
11198 const char *imported_name_prefix;
11199 const char *canonical_name;
11200 const char *import_alias;
11201 const char *imported_declaration = NULL;
11202 const char *import_prefix;
11203 std::vector<const char *> excludes;
11204
11205 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11206 if (import_attr == NULL)
11207 {
11208 complaint (_("Tag '%s' has no DW_AT_import"),
11209 dwarf_tag_name (die->tag));
11210 return;
11211 }
11212
11213 imported_cu = cu;
11214 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11215 imported_name = dwarf2_name (imported_die, imported_cu);
11216 if (imported_name == NULL)
11217 {
11218 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11219
11220 The import in the following code:
11221 namespace A
11222 {
11223 typedef int B;
11224 }
11225
11226 int main ()
11227 {
11228 using A::B;
11229 B b;
11230 return b;
11231 }
11232
11233 ...
11234 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11235 <52> DW_AT_decl_file : 1
11236 <53> DW_AT_decl_line : 6
11237 <54> DW_AT_import : <0x75>
11238 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11239 <59> DW_AT_name : B
11240 <5b> DW_AT_decl_file : 1
11241 <5c> DW_AT_decl_line : 2
11242 <5d> DW_AT_type : <0x6e>
11243 ...
11244 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11245 <76> DW_AT_byte_size : 4
11246 <77> DW_AT_encoding : 5 (signed)
11247
11248 imports the wrong die ( 0x75 instead of 0x58 ).
11249 This case will be ignored until the gcc bug is fixed. */
11250 return;
11251 }
11252
11253 /* Figure out the local name after import. */
11254 import_alias = dwarf2_name (die, cu);
11255
11256 /* Figure out where the statement is being imported to. */
11257 import_prefix = determine_prefix (die, cu);
11258
11259 /* Figure out what the scope of the imported die is and prepend it
11260 to the name of the imported die. */
11261 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11262
11263 if (imported_die->tag != DW_TAG_namespace
11264 && imported_die->tag != DW_TAG_module)
11265 {
11266 imported_declaration = imported_name;
11267 canonical_name = imported_name_prefix;
11268 }
11269 else if (strlen (imported_name_prefix) > 0)
11270 canonical_name = obconcat (&objfile->objfile_obstack,
11271 imported_name_prefix,
11272 (cu->language == language_d ? "." : "::"),
11273 imported_name, (char *) NULL);
11274 else
11275 canonical_name = imported_name;
11276
11277 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11278 for (child_die = die->child; child_die && child_die->tag;
11279 child_die = sibling_die (child_die))
11280 {
11281 /* DWARF-4: A Fortran use statement with a “rename list” may be
11282 represented by an imported module entry with an import attribute
11283 referring to the module and owned entries corresponding to those
11284 entities that are renamed as part of being imported. */
11285
11286 if (child_die->tag != DW_TAG_imported_declaration)
11287 {
11288 complaint (_("child DW_TAG_imported_declaration expected "
11289 "- DIE at %s [in module %s]"),
11290 sect_offset_str (child_die->sect_off),
11291 objfile_name (objfile));
11292 continue;
11293 }
11294
11295 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11296 if (import_attr == NULL)
11297 {
11298 complaint (_("Tag '%s' has no DW_AT_import"),
11299 dwarf_tag_name (child_die->tag));
11300 continue;
11301 }
11302
11303 imported_cu = cu;
11304 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11305 &imported_cu);
11306 imported_name = dwarf2_name (imported_die, imported_cu);
11307 if (imported_name == NULL)
11308 {
11309 complaint (_("child DW_TAG_imported_declaration has unknown "
11310 "imported name - DIE at %s [in module %s]"),
11311 sect_offset_str (child_die->sect_off),
11312 objfile_name (objfile));
11313 continue;
11314 }
11315
11316 excludes.push_back (imported_name);
11317
11318 process_die (child_die, cu);
11319 }
11320
11321 add_using_directive (using_directives (cu),
11322 import_prefix,
11323 canonical_name,
11324 import_alias,
11325 imported_declaration,
11326 excludes,
11327 0,
11328 &objfile->objfile_obstack);
11329 }
11330
11331 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11332 types, but gives them a size of zero. Starting with version 14,
11333 ICC is compatible with GCC. */
11334
11335 static bool
11336 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11337 {
11338 if (!cu->checked_producer)
11339 check_producer (cu);
11340
11341 return cu->producer_is_icc_lt_14;
11342 }
11343
11344 /* ICC generates a DW_AT_type for C void functions. This was observed on
11345 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11346 which says that void functions should not have a DW_AT_type. */
11347
11348 static bool
11349 producer_is_icc (struct dwarf2_cu *cu)
11350 {
11351 if (!cu->checked_producer)
11352 check_producer (cu);
11353
11354 return cu->producer_is_icc;
11355 }
11356
11357 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11358 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11359 this, it was first present in GCC release 4.3.0. */
11360
11361 static bool
11362 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11363 {
11364 if (!cu->checked_producer)
11365 check_producer (cu);
11366
11367 return cu->producer_is_gcc_lt_4_3;
11368 }
11369
11370 static file_and_directory
11371 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11372 {
11373 file_and_directory res;
11374
11375 /* Find the filename. Do not use dwarf2_name here, since the filename
11376 is not a source language identifier. */
11377 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11378 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11379
11380 if (res.comp_dir == NULL
11381 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11382 && IS_ABSOLUTE_PATH (res.name))
11383 {
11384 res.comp_dir_storage = ldirname (res.name);
11385 if (!res.comp_dir_storage.empty ())
11386 res.comp_dir = res.comp_dir_storage.c_str ();
11387 }
11388 if (res.comp_dir != NULL)
11389 {
11390 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11391 directory, get rid of it. */
11392 const char *cp = strchr (res.comp_dir, ':');
11393
11394 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11395 res.comp_dir = cp + 1;
11396 }
11397
11398 if (res.name == NULL)
11399 res.name = "<unknown>";
11400
11401 return res;
11402 }
11403
11404 /* Handle DW_AT_stmt_list for a compilation unit.
11405 DIE is the DW_TAG_compile_unit die for CU.
11406 COMP_DIR is the compilation directory. LOWPC is passed to
11407 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11408
11409 static void
11410 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11411 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11412 {
11413 struct dwarf2_per_objfile *dwarf2_per_objfile
11414 = cu->per_cu->dwarf2_per_objfile;
11415 struct objfile *objfile = dwarf2_per_objfile->objfile;
11416 struct attribute *attr;
11417 struct line_header line_header_local;
11418 hashval_t line_header_local_hash;
11419 void **slot;
11420 int decode_mapping;
11421
11422 gdb_assert (! cu->per_cu->is_debug_types);
11423
11424 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11425 if (attr == NULL)
11426 return;
11427
11428 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11429
11430 /* The line header hash table is only created if needed (it exists to
11431 prevent redundant reading of the line table for partial_units).
11432 If we're given a partial_unit, we'll need it. If we're given a
11433 compile_unit, then use the line header hash table if it's already
11434 created, but don't create one just yet. */
11435
11436 if (dwarf2_per_objfile->line_header_hash == NULL
11437 && die->tag == DW_TAG_partial_unit)
11438 {
11439 dwarf2_per_objfile->line_header_hash
11440 = htab_create_alloc_ex (127, line_header_hash_voidp,
11441 line_header_eq_voidp,
11442 free_line_header_voidp,
11443 &objfile->objfile_obstack,
11444 hashtab_obstack_allocate,
11445 dummy_obstack_deallocate);
11446 }
11447
11448 line_header_local.sect_off = line_offset;
11449 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11450 line_header_local_hash = line_header_hash (&line_header_local);
11451 if (dwarf2_per_objfile->line_header_hash != NULL)
11452 {
11453 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11454 &line_header_local,
11455 line_header_local_hash, NO_INSERT);
11456
11457 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11458 is not present in *SLOT (since if there is something in *SLOT then
11459 it will be for a partial_unit). */
11460 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11461 {
11462 gdb_assert (*slot != NULL);
11463 cu->line_header = (struct line_header *) *slot;
11464 return;
11465 }
11466 }
11467
11468 /* dwarf_decode_line_header does not yet provide sufficient information.
11469 We always have to call also dwarf_decode_lines for it. */
11470 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11471 if (lh == NULL)
11472 return;
11473
11474 cu->line_header = lh.release ();
11475 cu->line_header_die_owner = die;
11476
11477 if (dwarf2_per_objfile->line_header_hash == NULL)
11478 slot = NULL;
11479 else
11480 {
11481 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11482 &line_header_local,
11483 line_header_local_hash, INSERT);
11484 gdb_assert (slot != NULL);
11485 }
11486 if (slot != NULL && *slot == NULL)
11487 {
11488 /* This newly decoded line number information unit will be owned
11489 by line_header_hash hash table. */
11490 *slot = cu->line_header;
11491 cu->line_header_die_owner = NULL;
11492 }
11493 else
11494 {
11495 /* We cannot free any current entry in (*slot) as that struct line_header
11496 may be already used by multiple CUs. Create only temporary decoded
11497 line_header for this CU - it may happen at most once for each line
11498 number information unit. And if we're not using line_header_hash
11499 then this is what we want as well. */
11500 gdb_assert (die->tag != DW_TAG_partial_unit);
11501 }
11502 decode_mapping = (die->tag != DW_TAG_partial_unit);
11503 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11504 decode_mapping);
11505
11506 }
11507
11508 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11509
11510 static void
11511 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11512 {
11513 struct dwarf2_per_objfile *dwarf2_per_objfile
11514 = cu->per_cu->dwarf2_per_objfile;
11515 struct objfile *objfile = dwarf2_per_objfile->objfile;
11516 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11517 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11518 CORE_ADDR highpc = ((CORE_ADDR) 0);
11519 struct attribute *attr;
11520 struct die_info *child_die;
11521 CORE_ADDR baseaddr;
11522
11523 prepare_one_comp_unit (cu, die, cu->language);
11524 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11525
11526 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11527
11528 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11529 from finish_block. */
11530 if (lowpc == ((CORE_ADDR) -1))
11531 lowpc = highpc;
11532 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11533
11534 file_and_directory fnd = find_file_and_directory (die, cu);
11535
11536 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11537 standardised yet. As a workaround for the language detection we fall
11538 back to the DW_AT_producer string. */
11539 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11540 cu->language = language_opencl;
11541
11542 /* Similar hack for Go. */
11543 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11544 set_cu_language (DW_LANG_Go, cu);
11545
11546 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11547
11548 /* Decode line number information if present. We do this before
11549 processing child DIEs, so that the line header table is available
11550 for DW_AT_decl_file. */
11551 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11552
11553 /* Process all dies in compilation unit. */
11554 if (die->child != NULL)
11555 {
11556 child_die = die->child;
11557 while (child_die && child_die->tag)
11558 {
11559 process_die (child_die, cu);
11560 child_die = sibling_die (child_die);
11561 }
11562 }
11563
11564 /* Decode macro information, if present. Dwarf 2 macro information
11565 refers to information in the line number info statement program
11566 header, so we can only read it if we've read the header
11567 successfully. */
11568 attr = dwarf2_attr (die, DW_AT_macros, cu);
11569 if (attr == NULL)
11570 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11571 if (attr && cu->line_header)
11572 {
11573 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11574 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11575
11576 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11577 }
11578 else
11579 {
11580 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11581 if (attr && cu->line_header)
11582 {
11583 unsigned int macro_offset = DW_UNSND (attr);
11584
11585 dwarf_decode_macros (cu, macro_offset, 0);
11586 }
11587 }
11588 }
11589
11590 void
11591 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11592 {
11593 struct type_unit_group *tu_group;
11594 int first_time;
11595 struct attribute *attr;
11596 unsigned int i;
11597 struct signatured_type *sig_type;
11598
11599 gdb_assert (per_cu->is_debug_types);
11600 sig_type = (struct signatured_type *) per_cu;
11601
11602 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11603
11604 /* If we're using .gdb_index (includes -readnow) then
11605 per_cu->type_unit_group may not have been set up yet. */
11606 if (sig_type->type_unit_group == NULL)
11607 sig_type->type_unit_group = get_type_unit_group (this, attr);
11608 tu_group = sig_type->type_unit_group;
11609
11610 /* If we've already processed this stmt_list there's no real need to
11611 do it again, we could fake it and just recreate the part we need
11612 (file name,index -> symtab mapping). If data shows this optimization
11613 is useful we can do it then. */
11614 first_time = tu_group->compunit_symtab == NULL;
11615
11616 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11617 debug info. */
11618 line_header_up lh;
11619 if (attr != NULL)
11620 {
11621 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11622 lh = dwarf_decode_line_header (line_offset, this);
11623 }
11624 if (lh == NULL)
11625 {
11626 if (first_time)
11627 start_symtab ("", NULL, 0);
11628 else
11629 {
11630 gdb_assert (tu_group->symtabs == NULL);
11631 gdb_assert (m_builder == nullptr);
11632 struct compunit_symtab *cust = tu_group->compunit_symtab;
11633 m_builder.reset (new struct buildsym_compunit
11634 (COMPUNIT_OBJFILE (cust), "",
11635 COMPUNIT_DIRNAME (cust),
11636 compunit_language (cust),
11637 0, cust));
11638 }
11639 return;
11640 }
11641
11642 line_header = lh.release ();
11643 line_header_die_owner = die;
11644
11645 if (first_time)
11646 {
11647 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11648
11649 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11650 still initializing it, and our caller (a few levels up)
11651 process_full_type_unit still needs to know if this is the first
11652 time. */
11653
11654 tu_group->num_symtabs = line_header->file_names.size ();
11655 tu_group->symtabs = XNEWVEC (struct symtab *,
11656 line_header->file_names.size ());
11657
11658 for (i = 0; i < line_header->file_names.size (); ++i)
11659 {
11660 file_entry &fe = line_header->file_names[i];
11661
11662 dwarf2_start_subfile (this, fe.name,
11663 fe.include_dir (line_header));
11664 buildsym_compunit *b = get_builder ();
11665 if (b->get_current_subfile ()->symtab == NULL)
11666 {
11667 /* NOTE: start_subfile will recognize when it's been
11668 passed a file it has already seen. So we can't
11669 assume there's a simple mapping from
11670 cu->line_header->file_names to subfiles, plus
11671 cu->line_header->file_names may contain dups. */
11672 b->get_current_subfile ()->symtab
11673 = allocate_symtab (cust, b->get_current_subfile ()->name);
11674 }
11675
11676 fe.symtab = b->get_current_subfile ()->symtab;
11677 tu_group->symtabs[i] = fe.symtab;
11678 }
11679 }
11680 else
11681 {
11682 gdb_assert (m_builder == nullptr);
11683 struct compunit_symtab *cust = tu_group->compunit_symtab;
11684 m_builder.reset (new struct buildsym_compunit
11685 (COMPUNIT_OBJFILE (cust), "",
11686 COMPUNIT_DIRNAME (cust),
11687 compunit_language (cust),
11688 0, cust));
11689
11690 for (i = 0; i < line_header->file_names.size (); ++i)
11691 {
11692 file_entry &fe = line_header->file_names[i];
11693
11694 fe.symtab = tu_group->symtabs[i];
11695 }
11696 }
11697
11698 /* The main symtab is allocated last. Type units don't have DW_AT_name
11699 so they don't have a "real" (so to speak) symtab anyway.
11700 There is later code that will assign the main symtab to all symbols
11701 that don't have one. We need to handle the case of a symbol with a
11702 missing symtab (DW_AT_decl_file) anyway. */
11703 }
11704
11705 /* Process DW_TAG_type_unit.
11706 For TUs we want to skip the first top level sibling if it's not the
11707 actual type being defined by this TU. In this case the first top
11708 level sibling is there to provide context only. */
11709
11710 static void
11711 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11712 {
11713 struct die_info *child_die;
11714
11715 prepare_one_comp_unit (cu, die, language_minimal);
11716
11717 /* Initialize (or reinitialize) the machinery for building symtabs.
11718 We do this before processing child DIEs, so that the line header table
11719 is available for DW_AT_decl_file. */
11720 cu->setup_type_unit_groups (die);
11721
11722 if (die->child != NULL)
11723 {
11724 child_die = die->child;
11725 while (child_die && child_die->tag)
11726 {
11727 process_die (child_die, cu);
11728 child_die = sibling_die (child_die);
11729 }
11730 }
11731 }
11732 \f
11733 /* DWO/DWP files.
11734
11735 http://gcc.gnu.org/wiki/DebugFission
11736 http://gcc.gnu.org/wiki/DebugFissionDWP
11737
11738 To simplify handling of both DWO files ("object" files with the DWARF info)
11739 and DWP files (a file with the DWOs packaged up into one file), we treat
11740 DWP files as having a collection of virtual DWO files. */
11741
11742 static hashval_t
11743 hash_dwo_file (const void *item)
11744 {
11745 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11746 hashval_t hash;
11747
11748 hash = htab_hash_string (dwo_file->dwo_name);
11749 if (dwo_file->comp_dir != NULL)
11750 hash += htab_hash_string (dwo_file->comp_dir);
11751 return hash;
11752 }
11753
11754 static int
11755 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11756 {
11757 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11758 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11759
11760 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11761 return 0;
11762 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11763 return lhs->comp_dir == rhs->comp_dir;
11764 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11765 }
11766
11767 /* Allocate a hash table for DWO files. */
11768
11769 static htab_up
11770 allocate_dwo_file_hash_table (struct objfile *objfile)
11771 {
11772 auto delete_dwo_file = [] (void *item)
11773 {
11774 struct dwo_file *dwo_file = (struct dwo_file *) item;
11775
11776 delete dwo_file;
11777 };
11778
11779 return htab_up (htab_create_alloc_ex (41,
11780 hash_dwo_file,
11781 eq_dwo_file,
11782 delete_dwo_file,
11783 &objfile->objfile_obstack,
11784 hashtab_obstack_allocate,
11785 dummy_obstack_deallocate));
11786 }
11787
11788 /* Lookup DWO file DWO_NAME. */
11789
11790 static void **
11791 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11792 const char *dwo_name,
11793 const char *comp_dir)
11794 {
11795 struct dwo_file find_entry;
11796 void **slot;
11797
11798 if (dwarf2_per_objfile->dwo_files == NULL)
11799 dwarf2_per_objfile->dwo_files
11800 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11801
11802 find_entry.dwo_name = dwo_name;
11803 find_entry.comp_dir = comp_dir;
11804 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11805 INSERT);
11806
11807 return slot;
11808 }
11809
11810 static hashval_t
11811 hash_dwo_unit (const void *item)
11812 {
11813 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11814
11815 /* This drops the top 32 bits of the id, but is ok for a hash. */
11816 return dwo_unit->signature;
11817 }
11818
11819 static int
11820 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11821 {
11822 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11823 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11824
11825 /* The signature is assumed to be unique within the DWO file.
11826 So while object file CU dwo_id's always have the value zero,
11827 that's OK, assuming each object file DWO file has only one CU,
11828 and that's the rule for now. */
11829 return lhs->signature == rhs->signature;
11830 }
11831
11832 /* Allocate a hash table for DWO CUs,TUs.
11833 There is one of these tables for each of CUs,TUs for each DWO file. */
11834
11835 static htab_t
11836 allocate_dwo_unit_table (struct objfile *objfile)
11837 {
11838 /* Start out with a pretty small number.
11839 Generally DWO files contain only one CU and maybe some TUs. */
11840 return htab_create_alloc_ex (3,
11841 hash_dwo_unit,
11842 eq_dwo_unit,
11843 NULL,
11844 &objfile->objfile_obstack,
11845 hashtab_obstack_allocate,
11846 dummy_obstack_deallocate);
11847 }
11848
11849 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11850
11851 struct create_dwo_cu_data
11852 {
11853 struct dwo_file *dwo_file;
11854 struct dwo_unit dwo_unit;
11855 };
11856
11857 /* die_reader_func for create_dwo_cu. */
11858
11859 static void
11860 create_dwo_cu_reader (const struct die_reader_specs *reader,
11861 const gdb_byte *info_ptr,
11862 struct die_info *comp_unit_die,
11863 int has_children,
11864 void *datap)
11865 {
11866 struct dwarf2_cu *cu = reader->cu;
11867 sect_offset sect_off = cu->per_cu->sect_off;
11868 struct dwarf2_section_info *section = cu->per_cu->section;
11869 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11870 struct dwo_file *dwo_file = data->dwo_file;
11871 struct dwo_unit *dwo_unit = &data->dwo_unit;
11872
11873 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11874 if (!signature.has_value ())
11875 {
11876 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11877 " its dwo_id [in module %s]"),
11878 sect_offset_str (sect_off), dwo_file->dwo_name);
11879 return;
11880 }
11881
11882 dwo_unit->dwo_file = dwo_file;
11883 dwo_unit->signature = *signature;
11884 dwo_unit->section = section;
11885 dwo_unit->sect_off = sect_off;
11886 dwo_unit->length = cu->per_cu->length;
11887
11888 if (dwarf_read_debug)
11889 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11890 sect_offset_str (sect_off),
11891 hex_string (dwo_unit->signature));
11892 }
11893
11894 /* Create the dwo_units for the CUs in a DWO_FILE.
11895 Note: This function processes DWO files only, not DWP files. */
11896
11897 static void
11898 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11899 struct dwo_file &dwo_file, dwarf2_section_info &section,
11900 htab_t &cus_htab)
11901 {
11902 struct objfile *objfile = dwarf2_per_objfile->objfile;
11903 const gdb_byte *info_ptr, *end_ptr;
11904
11905 dwarf2_read_section (objfile, &section);
11906 info_ptr = section.buffer;
11907
11908 if (info_ptr == NULL)
11909 return;
11910
11911 if (dwarf_read_debug)
11912 {
11913 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11914 get_section_name (&section),
11915 get_section_file_name (&section));
11916 }
11917
11918 end_ptr = info_ptr + section.size;
11919 while (info_ptr < end_ptr)
11920 {
11921 struct dwarf2_per_cu_data per_cu;
11922 struct create_dwo_cu_data create_dwo_cu_data;
11923 struct dwo_unit *dwo_unit;
11924 void **slot;
11925 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11926
11927 memset (&create_dwo_cu_data.dwo_unit, 0,
11928 sizeof (create_dwo_cu_data.dwo_unit));
11929 memset (&per_cu, 0, sizeof (per_cu));
11930 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11931 per_cu.is_debug_types = 0;
11932 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11933 per_cu.section = &section;
11934 create_dwo_cu_data.dwo_file = &dwo_file;
11935
11936 init_cutu_and_read_dies_no_follow (
11937 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11938 info_ptr += per_cu.length;
11939
11940 // If the unit could not be parsed, skip it.
11941 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11942 continue;
11943
11944 if (cus_htab == NULL)
11945 cus_htab = allocate_dwo_unit_table (objfile);
11946
11947 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11948 *dwo_unit = create_dwo_cu_data.dwo_unit;
11949 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11950 gdb_assert (slot != NULL);
11951 if (*slot != NULL)
11952 {
11953 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11954 sect_offset dup_sect_off = dup_cu->sect_off;
11955
11956 complaint (_("debug cu entry at offset %s is duplicate to"
11957 " the entry at offset %s, signature %s"),
11958 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11959 hex_string (dwo_unit->signature));
11960 }
11961 *slot = (void *)dwo_unit;
11962 }
11963 }
11964
11965 /* DWP file .debug_{cu,tu}_index section format:
11966 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11967
11968 DWP Version 1:
11969
11970 Both index sections have the same format, and serve to map a 64-bit
11971 signature to a set of section numbers. Each section begins with a header,
11972 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11973 indexes, and a pool of 32-bit section numbers. The index sections will be
11974 aligned at 8-byte boundaries in the file.
11975
11976 The index section header consists of:
11977
11978 V, 32 bit version number
11979 -, 32 bits unused
11980 N, 32 bit number of compilation units or type units in the index
11981 M, 32 bit number of slots in the hash table
11982
11983 Numbers are recorded using the byte order of the application binary.
11984
11985 The hash table begins at offset 16 in the section, and consists of an array
11986 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11987 order of the application binary). Unused slots in the hash table are 0.
11988 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11989
11990 The parallel table begins immediately after the hash table
11991 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11992 array of 32-bit indexes (using the byte order of the application binary),
11993 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11994 table contains a 32-bit index into the pool of section numbers. For unused
11995 hash table slots, the corresponding entry in the parallel table will be 0.
11996
11997 The pool of section numbers begins immediately following the hash table
11998 (at offset 16 + 12 * M from the beginning of the section). The pool of
11999 section numbers consists of an array of 32-bit words (using the byte order
12000 of the application binary). Each item in the array is indexed starting
12001 from 0. The hash table entry provides the index of the first section
12002 number in the set. Additional section numbers in the set follow, and the
12003 set is terminated by a 0 entry (section number 0 is not used in ELF).
12004
12005 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12006 section must be the first entry in the set, and the .debug_abbrev.dwo must
12007 be the second entry. Other members of the set may follow in any order.
12008
12009 ---
12010
12011 DWP Version 2:
12012
12013 DWP Version 2 combines all the .debug_info, etc. sections into one,
12014 and the entries in the index tables are now offsets into these sections.
12015 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12016 section.
12017
12018 Index Section Contents:
12019 Header
12020 Hash Table of Signatures dwp_hash_table.hash_table
12021 Parallel Table of Indices dwp_hash_table.unit_table
12022 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12023 Table of Section Sizes dwp_hash_table.v2.sizes
12024
12025 The index section header consists of:
12026
12027 V, 32 bit version number
12028 L, 32 bit number of columns in the table of section offsets
12029 N, 32 bit number of compilation units or type units in the index
12030 M, 32 bit number of slots in the hash table
12031
12032 Numbers are recorded using the byte order of the application binary.
12033
12034 The hash table has the same format as version 1.
12035 The parallel table of indices has the same format as version 1,
12036 except that the entries are origin-1 indices into the table of sections
12037 offsets and the table of section sizes.
12038
12039 The table of offsets begins immediately following the parallel table
12040 (at offset 16 + 12 * M from the beginning of the section). The table is
12041 a two-dimensional array of 32-bit words (using the byte order of the
12042 application binary), with L columns and N+1 rows, in row-major order.
12043 Each row in the array is indexed starting from 0. The first row provides
12044 a key to the remaining rows: each column in this row provides an identifier
12045 for a debug section, and the offsets in the same column of subsequent rows
12046 refer to that section. The section identifiers are:
12047
12048 DW_SECT_INFO 1 .debug_info.dwo
12049 DW_SECT_TYPES 2 .debug_types.dwo
12050 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12051 DW_SECT_LINE 4 .debug_line.dwo
12052 DW_SECT_LOC 5 .debug_loc.dwo
12053 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12054 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12055 DW_SECT_MACRO 8 .debug_macro.dwo
12056
12057 The offsets provided by the CU and TU index sections are the base offsets
12058 for the contributions made by each CU or TU to the corresponding section
12059 in the package file. Each CU and TU header contains an abbrev_offset
12060 field, used to find the abbreviations table for that CU or TU within the
12061 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12062 be interpreted as relative to the base offset given in the index section.
12063 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12064 should be interpreted as relative to the base offset for .debug_line.dwo,
12065 and offsets into other debug sections obtained from DWARF attributes should
12066 also be interpreted as relative to the corresponding base offset.
12067
12068 The table of sizes begins immediately following the table of offsets.
12069 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12070 with L columns and N rows, in row-major order. Each row in the array is
12071 indexed starting from 1 (row 0 is shared by the two tables).
12072
12073 ---
12074
12075 Hash table lookup is handled the same in version 1 and 2:
12076
12077 We assume that N and M will not exceed 2^32 - 1.
12078 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12079
12080 Given a 64-bit compilation unit signature or a type signature S, an entry
12081 in the hash table is located as follows:
12082
12083 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12084 the low-order k bits all set to 1.
12085
12086 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12087
12088 3) If the hash table entry at index H matches the signature, use that
12089 entry. If the hash table entry at index H is unused (all zeroes),
12090 terminate the search: the signature is not present in the table.
12091
12092 4) Let H = (H + H') modulo M. Repeat at Step 3.
12093
12094 Because M > N and H' and M are relatively prime, the search is guaranteed
12095 to stop at an unused slot or find the match. */
12096
12097 /* Create a hash table to map DWO IDs to their CU/TU entry in
12098 .debug_{info,types}.dwo in DWP_FILE.
12099 Returns NULL if there isn't one.
12100 Note: This function processes DWP files only, not DWO files. */
12101
12102 static struct dwp_hash_table *
12103 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12104 struct dwp_file *dwp_file, int is_debug_types)
12105 {
12106 struct objfile *objfile = dwarf2_per_objfile->objfile;
12107 bfd *dbfd = dwp_file->dbfd.get ();
12108 const gdb_byte *index_ptr, *index_end;
12109 struct dwarf2_section_info *index;
12110 uint32_t version, nr_columns, nr_units, nr_slots;
12111 struct dwp_hash_table *htab;
12112
12113 if (is_debug_types)
12114 index = &dwp_file->sections.tu_index;
12115 else
12116 index = &dwp_file->sections.cu_index;
12117
12118 if (dwarf2_section_empty_p (index))
12119 return NULL;
12120 dwarf2_read_section (objfile, index);
12121
12122 index_ptr = index->buffer;
12123 index_end = index_ptr + index->size;
12124
12125 version = read_4_bytes (dbfd, index_ptr);
12126 index_ptr += 4;
12127 if (version == 2)
12128 nr_columns = read_4_bytes (dbfd, index_ptr);
12129 else
12130 nr_columns = 0;
12131 index_ptr += 4;
12132 nr_units = read_4_bytes (dbfd, index_ptr);
12133 index_ptr += 4;
12134 nr_slots = read_4_bytes (dbfd, index_ptr);
12135 index_ptr += 4;
12136
12137 if (version != 1 && version != 2)
12138 {
12139 error (_("Dwarf Error: unsupported DWP file version (%s)"
12140 " [in module %s]"),
12141 pulongest (version), dwp_file->name);
12142 }
12143 if (nr_slots != (nr_slots & -nr_slots))
12144 {
12145 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12146 " is not power of 2 [in module %s]"),
12147 pulongest (nr_slots), dwp_file->name);
12148 }
12149
12150 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12151 htab->version = version;
12152 htab->nr_columns = nr_columns;
12153 htab->nr_units = nr_units;
12154 htab->nr_slots = nr_slots;
12155 htab->hash_table = index_ptr;
12156 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12157
12158 /* Exit early if the table is empty. */
12159 if (nr_slots == 0 || nr_units == 0
12160 || (version == 2 && nr_columns == 0))
12161 {
12162 /* All must be zero. */
12163 if (nr_slots != 0 || nr_units != 0
12164 || (version == 2 && nr_columns != 0))
12165 {
12166 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
12167 " all zero [in modules %s]"),
12168 dwp_file->name);
12169 }
12170 return htab;
12171 }
12172
12173 if (version == 1)
12174 {
12175 htab->section_pool.v1.indices =
12176 htab->unit_table + sizeof (uint32_t) * nr_slots;
12177 /* It's harder to decide whether the section is too small in v1.
12178 V1 is deprecated anyway so we punt. */
12179 }
12180 else
12181 {
12182 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12183 int *ids = htab->section_pool.v2.section_ids;
12184 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
12185 /* Reverse map for error checking. */
12186 int ids_seen[DW_SECT_MAX + 1];
12187 int i;
12188
12189 if (nr_columns < 2)
12190 {
12191 error (_("Dwarf Error: bad DWP hash table, too few columns"
12192 " in section table [in module %s]"),
12193 dwp_file->name);
12194 }
12195 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12196 {
12197 error (_("Dwarf Error: bad DWP hash table, too many columns"
12198 " in section table [in module %s]"),
12199 dwp_file->name);
12200 }
12201 memset (ids, 255, sizeof_ids);
12202 memset (ids_seen, 255, sizeof (ids_seen));
12203 for (i = 0; i < nr_columns; ++i)
12204 {
12205 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12206
12207 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12208 {
12209 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12210 " in section table [in module %s]"),
12211 id, dwp_file->name);
12212 }
12213 if (ids_seen[id] != -1)
12214 {
12215 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12216 " id %d in section table [in module %s]"),
12217 id, dwp_file->name);
12218 }
12219 ids_seen[id] = i;
12220 ids[i] = id;
12221 }
12222 /* Must have exactly one info or types section. */
12223 if (((ids_seen[DW_SECT_INFO] != -1)
12224 + (ids_seen[DW_SECT_TYPES] != -1))
12225 != 1)
12226 {
12227 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12228 " DWO info/types section [in module %s]"),
12229 dwp_file->name);
12230 }
12231 /* Must have an abbrev section. */
12232 if (ids_seen[DW_SECT_ABBREV] == -1)
12233 {
12234 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12235 " section [in module %s]"),
12236 dwp_file->name);
12237 }
12238 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12239 htab->section_pool.v2.sizes =
12240 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12241 * nr_units * nr_columns);
12242 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12243 * nr_units * nr_columns))
12244 > index_end)
12245 {
12246 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12247 " [in module %s]"),
12248 dwp_file->name);
12249 }
12250 }
12251
12252 return htab;
12253 }
12254
12255 /* Update SECTIONS with the data from SECTP.
12256
12257 This function is like the other "locate" section routines that are
12258 passed to bfd_map_over_sections, but in this context the sections to
12259 read comes from the DWP V1 hash table, not the full ELF section table.
12260
12261 The result is non-zero for success, or zero if an error was found. */
12262
12263 static int
12264 locate_v1_virtual_dwo_sections (asection *sectp,
12265 struct virtual_v1_dwo_sections *sections)
12266 {
12267 const struct dwop_section_names *names = &dwop_section_names;
12268
12269 if (section_is_p (sectp->name, &names->abbrev_dwo))
12270 {
12271 /* There can be only one. */
12272 if (sections->abbrev.s.section != NULL)
12273 return 0;
12274 sections->abbrev.s.section = sectp;
12275 sections->abbrev.size = bfd_get_section_size (sectp);
12276 }
12277 else if (section_is_p (sectp->name, &names->info_dwo)
12278 || section_is_p (sectp->name, &names->types_dwo))
12279 {
12280 /* There can be only one. */
12281 if (sections->info_or_types.s.section != NULL)
12282 return 0;
12283 sections->info_or_types.s.section = sectp;
12284 sections->info_or_types.size = bfd_get_section_size (sectp);
12285 }
12286 else if (section_is_p (sectp->name, &names->line_dwo))
12287 {
12288 /* There can be only one. */
12289 if (sections->line.s.section != NULL)
12290 return 0;
12291 sections->line.s.section = sectp;
12292 sections->line.size = bfd_get_section_size (sectp);
12293 }
12294 else if (section_is_p (sectp->name, &names->loc_dwo))
12295 {
12296 /* There can be only one. */
12297 if (sections->loc.s.section != NULL)
12298 return 0;
12299 sections->loc.s.section = sectp;
12300 sections->loc.size = bfd_get_section_size (sectp);
12301 }
12302 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12303 {
12304 /* There can be only one. */
12305 if (sections->macinfo.s.section != NULL)
12306 return 0;
12307 sections->macinfo.s.section = sectp;
12308 sections->macinfo.size = bfd_get_section_size (sectp);
12309 }
12310 else if (section_is_p (sectp->name, &names->macro_dwo))
12311 {
12312 /* There can be only one. */
12313 if (sections->macro.s.section != NULL)
12314 return 0;
12315 sections->macro.s.section = sectp;
12316 sections->macro.size = bfd_get_section_size (sectp);
12317 }
12318 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12319 {
12320 /* There can be only one. */
12321 if (sections->str_offsets.s.section != NULL)
12322 return 0;
12323 sections->str_offsets.s.section = sectp;
12324 sections->str_offsets.size = bfd_get_section_size (sectp);
12325 }
12326 else
12327 {
12328 /* No other kind of section is valid. */
12329 return 0;
12330 }
12331
12332 return 1;
12333 }
12334
12335 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12336 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12337 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12338 This is for DWP version 1 files. */
12339
12340 static struct dwo_unit *
12341 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12342 struct dwp_file *dwp_file,
12343 uint32_t unit_index,
12344 const char *comp_dir,
12345 ULONGEST signature, int is_debug_types)
12346 {
12347 struct objfile *objfile = dwarf2_per_objfile->objfile;
12348 const struct dwp_hash_table *dwp_htab =
12349 is_debug_types ? dwp_file->tus : dwp_file->cus;
12350 bfd *dbfd = dwp_file->dbfd.get ();
12351 const char *kind = is_debug_types ? "TU" : "CU";
12352 struct dwo_file *dwo_file;
12353 struct dwo_unit *dwo_unit;
12354 struct virtual_v1_dwo_sections sections;
12355 void **dwo_file_slot;
12356 int i;
12357
12358 gdb_assert (dwp_file->version == 1);
12359
12360 if (dwarf_read_debug)
12361 {
12362 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12363 kind,
12364 pulongest (unit_index), hex_string (signature),
12365 dwp_file->name);
12366 }
12367
12368 /* Fetch the sections of this DWO unit.
12369 Put a limit on the number of sections we look for so that bad data
12370 doesn't cause us to loop forever. */
12371
12372 #define MAX_NR_V1_DWO_SECTIONS \
12373 (1 /* .debug_info or .debug_types */ \
12374 + 1 /* .debug_abbrev */ \
12375 + 1 /* .debug_line */ \
12376 + 1 /* .debug_loc */ \
12377 + 1 /* .debug_str_offsets */ \
12378 + 1 /* .debug_macro or .debug_macinfo */ \
12379 + 1 /* trailing zero */)
12380
12381 memset (&sections, 0, sizeof (sections));
12382
12383 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12384 {
12385 asection *sectp;
12386 uint32_t section_nr =
12387 read_4_bytes (dbfd,
12388 dwp_htab->section_pool.v1.indices
12389 + (unit_index + i) * sizeof (uint32_t));
12390
12391 if (section_nr == 0)
12392 break;
12393 if (section_nr >= dwp_file->num_sections)
12394 {
12395 error (_("Dwarf Error: bad DWP hash table, section number too large"
12396 " [in module %s]"),
12397 dwp_file->name);
12398 }
12399
12400 sectp = dwp_file->elf_sections[section_nr];
12401 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12402 {
12403 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12404 " [in module %s]"),
12405 dwp_file->name);
12406 }
12407 }
12408
12409 if (i < 2
12410 || dwarf2_section_empty_p (&sections.info_or_types)
12411 || dwarf2_section_empty_p (&sections.abbrev))
12412 {
12413 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12414 " [in module %s]"),
12415 dwp_file->name);
12416 }
12417 if (i == MAX_NR_V1_DWO_SECTIONS)
12418 {
12419 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12420 " [in module %s]"),
12421 dwp_file->name);
12422 }
12423
12424 /* It's easier for the rest of the code if we fake a struct dwo_file and
12425 have dwo_unit "live" in that. At least for now.
12426
12427 The DWP file can be made up of a random collection of CUs and TUs.
12428 However, for each CU + set of TUs that came from the same original DWO
12429 file, we can combine them back into a virtual DWO file to save space
12430 (fewer struct dwo_file objects to allocate). Remember that for really
12431 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12432
12433 std::string virtual_dwo_name =
12434 string_printf ("virtual-dwo/%d-%d-%d-%d",
12435 get_section_id (&sections.abbrev),
12436 get_section_id (&sections.line),
12437 get_section_id (&sections.loc),
12438 get_section_id (&sections.str_offsets));
12439 /* Can we use an existing virtual DWO file? */
12440 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12441 virtual_dwo_name.c_str (),
12442 comp_dir);
12443 /* Create one if necessary. */
12444 if (*dwo_file_slot == NULL)
12445 {
12446 if (dwarf_read_debug)
12447 {
12448 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12449 virtual_dwo_name.c_str ());
12450 }
12451 dwo_file = new struct dwo_file;
12452 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12453 virtual_dwo_name);
12454 dwo_file->comp_dir = comp_dir;
12455 dwo_file->sections.abbrev = sections.abbrev;
12456 dwo_file->sections.line = sections.line;
12457 dwo_file->sections.loc = sections.loc;
12458 dwo_file->sections.macinfo = sections.macinfo;
12459 dwo_file->sections.macro = sections.macro;
12460 dwo_file->sections.str_offsets = sections.str_offsets;
12461 /* The "str" section is global to the entire DWP file. */
12462 dwo_file->sections.str = dwp_file->sections.str;
12463 /* The info or types section is assigned below to dwo_unit,
12464 there's no need to record it in dwo_file.
12465 Also, we can't simply record type sections in dwo_file because
12466 we record a pointer into the vector in dwo_unit. As we collect more
12467 types we'll grow the vector and eventually have to reallocate space
12468 for it, invalidating all copies of pointers into the previous
12469 contents. */
12470 *dwo_file_slot = dwo_file;
12471 }
12472 else
12473 {
12474 if (dwarf_read_debug)
12475 {
12476 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12477 virtual_dwo_name.c_str ());
12478 }
12479 dwo_file = (struct dwo_file *) *dwo_file_slot;
12480 }
12481
12482 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12483 dwo_unit->dwo_file = dwo_file;
12484 dwo_unit->signature = signature;
12485 dwo_unit->section =
12486 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12487 *dwo_unit->section = sections.info_or_types;
12488 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12489
12490 return dwo_unit;
12491 }
12492
12493 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12494 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12495 piece within that section used by a TU/CU, return a virtual section
12496 of just that piece. */
12497
12498 static struct dwarf2_section_info
12499 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12500 struct dwarf2_section_info *section,
12501 bfd_size_type offset, bfd_size_type size)
12502 {
12503 struct dwarf2_section_info result;
12504 asection *sectp;
12505
12506 gdb_assert (section != NULL);
12507 gdb_assert (!section->is_virtual);
12508
12509 memset (&result, 0, sizeof (result));
12510 result.s.containing_section = section;
12511 result.is_virtual = true;
12512
12513 if (size == 0)
12514 return result;
12515
12516 sectp = get_section_bfd_section (section);
12517
12518 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12519 bounds of the real section. This is a pretty-rare event, so just
12520 flag an error (easier) instead of a warning and trying to cope. */
12521 if (sectp == NULL
12522 || offset + size > bfd_get_section_size (sectp))
12523 {
12524 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12525 " in section %s [in module %s]"),
12526 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12527 objfile_name (dwarf2_per_objfile->objfile));
12528 }
12529
12530 result.virtual_offset = offset;
12531 result.size = size;
12532 return result;
12533 }
12534
12535 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12536 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12537 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12538 This is for DWP version 2 files. */
12539
12540 static struct dwo_unit *
12541 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12542 struct dwp_file *dwp_file,
12543 uint32_t unit_index,
12544 const char *comp_dir,
12545 ULONGEST signature, int is_debug_types)
12546 {
12547 struct objfile *objfile = dwarf2_per_objfile->objfile;
12548 const struct dwp_hash_table *dwp_htab =
12549 is_debug_types ? dwp_file->tus : dwp_file->cus;
12550 bfd *dbfd = dwp_file->dbfd.get ();
12551 const char *kind = is_debug_types ? "TU" : "CU";
12552 struct dwo_file *dwo_file;
12553 struct dwo_unit *dwo_unit;
12554 struct virtual_v2_dwo_sections sections;
12555 void **dwo_file_slot;
12556 int i;
12557
12558 gdb_assert (dwp_file->version == 2);
12559
12560 if (dwarf_read_debug)
12561 {
12562 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12563 kind,
12564 pulongest (unit_index), hex_string (signature),
12565 dwp_file->name);
12566 }
12567
12568 /* Fetch the section offsets of this DWO unit. */
12569
12570 memset (&sections, 0, sizeof (sections));
12571
12572 for (i = 0; i < dwp_htab->nr_columns; ++i)
12573 {
12574 uint32_t offset = read_4_bytes (dbfd,
12575 dwp_htab->section_pool.v2.offsets
12576 + (((unit_index - 1) * dwp_htab->nr_columns
12577 + i)
12578 * sizeof (uint32_t)));
12579 uint32_t size = read_4_bytes (dbfd,
12580 dwp_htab->section_pool.v2.sizes
12581 + (((unit_index - 1) * dwp_htab->nr_columns
12582 + i)
12583 * sizeof (uint32_t)));
12584
12585 switch (dwp_htab->section_pool.v2.section_ids[i])
12586 {
12587 case DW_SECT_INFO:
12588 case DW_SECT_TYPES:
12589 sections.info_or_types_offset = offset;
12590 sections.info_or_types_size = size;
12591 break;
12592 case DW_SECT_ABBREV:
12593 sections.abbrev_offset = offset;
12594 sections.abbrev_size = size;
12595 break;
12596 case DW_SECT_LINE:
12597 sections.line_offset = offset;
12598 sections.line_size = size;
12599 break;
12600 case DW_SECT_LOC:
12601 sections.loc_offset = offset;
12602 sections.loc_size = size;
12603 break;
12604 case DW_SECT_STR_OFFSETS:
12605 sections.str_offsets_offset = offset;
12606 sections.str_offsets_size = size;
12607 break;
12608 case DW_SECT_MACINFO:
12609 sections.macinfo_offset = offset;
12610 sections.macinfo_size = size;
12611 break;
12612 case DW_SECT_MACRO:
12613 sections.macro_offset = offset;
12614 sections.macro_size = size;
12615 break;
12616 }
12617 }
12618
12619 /* It's easier for the rest of the code if we fake a struct dwo_file and
12620 have dwo_unit "live" in that. At least for now.
12621
12622 The DWP file can be made up of a random collection of CUs and TUs.
12623 However, for each CU + set of TUs that came from the same original DWO
12624 file, we can combine them back into a virtual DWO file to save space
12625 (fewer struct dwo_file objects to allocate). Remember that for really
12626 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12627
12628 std::string virtual_dwo_name =
12629 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12630 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12631 (long) (sections.line_size ? sections.line_offset : 0),
12632 (long) (sections.loc_size ? sections.loc_offset : 0),
12633 (long) (sections.str_offsets_size
12634 ? sections.str_offsets_offset : 0));
12635 /* Can we use an existing virtual DWO file? */
12636 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12637 virtual_dwo_name.c_str (),
12638 comp_dir);
12639 /* Create one if necessary. */
12640 if (*dwo_file_slot == NULL)
12641 {
12642 if (dwarf_read_debug)
12643 {
12644 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12645 virtual_dwo_name.c_str ());
12646 }
12647 dwo_file = new struct dwo_file;
12648 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12649 virtual_dwo_name);
12650 dwo_file->comp_dir = comp_dir;
12651 dwo_file->sections.abbrev =
12652 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12653 sections.abbrev_offset, sections.abbrev_size);
12654 dwo_file->sections.line =
12655 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12656 sections.line_offset, sections.line_size);
12657 dwo_file->sections.loc =
12658 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12659 sections.loc_offset, sections.loc_size);
12660 dwo_file->sections.macinfo =
12661 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12662 sections.macinfo_offset, sections.macinfo_size);
12663 dwo_file->sections.macro =
12664 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12665 sections.macro_offset, sections.macro_size);
12666 dwo_file->sections.str_offsets =
12667 create_dwp_v2_section (dwarf2_per_objfile,
12668 &dwp_file->sections.str_offsets,
12669 sections.str_offsets_offset,
12670 sections.str_offsets_size);
12671 /* The "str" section is global to the entire DWP file. */
12672 dwo_file->sections.str = dwp_file->sections.str;
12673 /* The info or types section is assigned below to dwo_unit,
12674 there's no need to record it in dwo_file.
12675 Also, we can't simply record type sections in dwo_file because
12676 we record a pointer into the vector in dwo_unit. As we collect more
12677 types we'll grow the vector and eventually have to reallocate space
12678 for it, invalidating all copies of pointers into the previous
12679 contents. */
12680 *dwo_file_slot = dwo_file;
12681 }
12682 else
12683 {
12684 if (dwarf_read_debug)
12685 {
12686 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12687 virtual_dwo_name.c_str ());
12688 }
12689 dwo_file = (struct dwo_file *) *dwo_file_slot;
12690 }
12691
12692 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12693 dwo_unit->dwo_file = dwo_file;
12694 dwo_unit->signature = signature;
12695 dwo_unit->section =
12696 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12697 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12698 is_debug_types
12699 ? &dwp_file->sections.types
12700 : &dwp_file->sections.info,
12701 sections.info_or_types_offset,
12702 sections.info_or_types_size);
12703 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12704
12705 return dwo_unit;
12706 }
12707
12708 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12709 Returns NULL if the signature isn't found. */
12710
12711 static struct dwo_unit *
12712 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12713 struct dwp_file *dwp_file, const char *comp_dir,
12714 ULONGEST signature, int is_debug_types)
12715 {
12716 const struct dwp_hash_table *dwp_htab =
12717 is_debug_types ? dwp_file->tus : dwp_file->cus;
12718 bfd *dbfd = dwp_file->dbfd.get ();
12719 uint32_t mask = dwp_htab->nr_slots - 1;
12720 uint32_t hash = signature & mask;
12721 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12722 unsigned int i;
12723 void **slot;
12724 struct dwo_unit find_dwo_cu;
12725
12726 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12727 find_dwo_cu.signature = signature;
12728 slot = htab_find_slot (is_debug_types
12729 ? dwp_file->loaded_tus
12730 : dwp_file->loaded_cus,
12731 &find_dwo_cu, INSERT);
12732
12733 if (*slot != NULL)
12734 return (struct dwo_unit *) *slot;
12735
12736 /* Use a for loop so that we don't loop forever on bad debug info. */
12737 for (i = 0; i < dwp_htab->nr_slots; ++i)
12738 {
12739 ULONGEST signature_in_table;
12740
12741 signature_in_table =
12742 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12743 if (signature_in_table == signature)
12744 {
12745 uint32_t unit_index =
12746 read_4_bytes (dbfd,
12747 dwp_htab->unit_table + hash * sizeof (uint32_t));
12748
12749 if (dwp_file->version == 1)
12750 {
12751 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12752 dwp_file, unit_index,
12753 comp_dir, signature,
12754 is_debug_types);
12755 }
12756 else
12757 {
12758 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12759 dwp_file, unit_index,
12760 comp_dir, signature,
12761 is_debug_types);
12762 }
12763 return (struct dwo_unit *) *slot;
12764 }
12765 if (signature_in_table == 0)
12766 return NULL;
12767 hash = (hash + hash2) & mask;
12768 }
12769
12770 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12771 " [in module %s]"),
12772 dwp_file->name);
12773 }
12774
12775 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12776 Open the file specified by FILE_NAME and hand it off to BFD for
12777 preliminary analysis. Return a newly initialized bfd *, which
12778 includes a canonicalized copy of FILE_NAME.
12779 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12780 SEARCH_CWD is true if the current directory is to be searched.
12781 It will be searched before debug-file-directory.
12782 If successful, the file is added to the bfd include table of the
12783 objfile's bfd (see gdb_bfd_record_inclusion).
12784 If unable to find/open the file, return NULL.
12785 NOTE: This function is derived from symfile_bfd_open. */
12786
12787 static gdb_bfd_ref_ptr
12788 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12789 const char *file_name, int is_dwp, int search_cwd)
12790 {
12791 int desc;
12792 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12793 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12794 to debug_file_directory. */
12795 const char *search_path;
12796 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12797
12798 gdb::unique_xmalloc_ptr<char> search_path_holder;
12799 if (search_cwd)
12800 {
12801 if (*debug_file_directory != '\0')
12802 {
12803 search_path_holder.reset (concat (".", dirname_separator_string,
12804 debug_file_directory,
12805 (char *) NULL));
12806 search_path = search_path_holder.get ();
12807 }
12808 else
12809 search_path = ".";
12810 }
12811 else
12812 search_path = debug_file_directory;
12813
12814 openp_flags flags = OPF_RETURN_REALPATH;
12815 if (is_dwp)
12816 flags |= OPF_SEARCH_IN_PATH;
12817
12818 gdb::unique_xmalloc_ptr<char> absolute_name;
12819 desc = openp (search_path, flags, file_name,
12820 O_RDONLY | O_BINARY, &absolute_name);
12821 if (desc < 0)
12822 return NULL;
12823
12824 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12825 gnutarget, desc));
12826 if (sym_bfd == NULL)
12827 return NULL;
12828 bfd_set_cacheable (sym_bfd.get (), 1);
12829
12830 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12831 return NULL;
12832
12833 /* Success. Record the bfd as having been included by the objfile's bfd.
12834 This is important because things like demangled_names_hash lives in the
12835 objfile's per_bfd space and may have references to things like symbol
12836 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12837 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12838
12839 return sym_bfd;
12840 }
12841
12842 /* Try to open DWO file FILE_NAME.
12843 COMP_DIR is the DW_AT_comp_dir attribute.
12844 The result is the bfd handle of the file.
12845 If there is a problem finding or opening the file, return NULL.
12846 Upon success, the canonicalized path of the file is stored in the bfd,
12847 same as symfile_bfd_open. */
12848
12849 static gdb_bfd_ref_ptr
12850 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12851 const char *file_name, const char *comp_dir)
12852 {
12853 if (IS_ABSOLUTE_PATH (file_name))
12854 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12855 0 /*is_dwp*/, 0 /*search_cwd*/);
12856
12857 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12858
12859 if (comp_dir != NULL)
12860 {
12861 char *path_to_try = concat (comp_dir, SLASH_STRING,
12862 file_name, (char *) NULL);
12863
12864 /* NOTE: If comp_dir is a relative path, this will also try the
12865 search path, which seems useful. */
12866 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12867 path_to_try,
12868 0 /*is_dwp*/,
12869 1 /*search_cwd*/));
12870 xfree (path_to_try);
12871 if (abfd != NULL)
12872 return abfd;
12873 }
12874
12875 /* That didn't work, try debug-file-directory, which, despite its name,
12876 is a list of paths. */
12877
12878 if (*debug_file_directory == '\0')
12879 return NULL;
12880
12881 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12882 0 /*is_dwp*/, 1 /*search_cwd*/);
12883 }
12884
12885 /* This function is mapped across the sections and remembers the offset and
12886 size of each of the DWO debugging sections we are interested in. */
12887
12888 static void
12889 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12890 {
12891 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12892 const struct dwop_section_names *names = &dwop_section_names;
12893
12894 if (section_is_p (sectp->name, &names->abbrev_dwo))
12895 {
12896 dwo_sections->abbrev.s.section = sectp;
12897 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12898 }
12899 else if (section_is_p (sectp->name, &names->info_dwo))
12900 {
12901 dwo_sections->info.s.section = sectp;
12902 dwo_sections->info.size = bfd_get_section_size (sectp);
12903 }
12904 else if (section_is_p (sectp->name, &names->line_dwo))
12905 {
12906 dwo_sections->line.s.section = sectp;
12907 dwo_sections->line.size = bfd_get_section_size (sectp);
12908 }
12909 else if (section_is_p (sectp->name, &names->loc_dwo))
12910 {
12911 dwo_sections->loc.s.section = sectp;
12912 dwo_sections->loc.size = bfd_get_section_size (sectp);
12913 }
12914 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12915 {
12916 dwo_sections->macinfo.s.section = sectp;
12917 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12918 }
12919 else if (section_is_p (sectp->name, &names->macro_dwo))
12920 {
12921 dwo_sections->macro.s.section = sectp;
12922 dwo_sections->macro.size = bfd_get_section_size (sectp);
12923 }
12924 else if (section_is_p (sectp->name, &names->str_dwo))
12925 {
12926 dwo_sections->str.s.section = sectp;
12927 dwo_sections->str.size = bfd_get_section_size (sectp);
12928 }
12929 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12930 {
12931 dwo_sections->str_offsets.s.section = sectp;
12932 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12933 }
12934 else if (section_is_p (sectp->name, &names->types_dwo))
12935 {
12936 struct dwarf2_section_info type_section;
12937
12938 memset (&type_section, 0, sizeof (type_section));
12939 type_section.s.section = sectp;
12940 type_section.size = bfd_get_section_size (sectp);
12941 dwo_sections->types.push_back (type_section);
12942 }
12943 }
12944
12945 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12946 by PER_CU. This is for the non-DWP case.
12947 The result is NULL if DWO_NAME can't be found. */
12948
12949 static struct dwo_file *
12950 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12951 const char *dwo_name, const char *comp_dir)
12952 {
12953 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12954
12955 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12956 if (dbfd == NULL)
12957 {
12958 if (dwarf_read_debug)
12959 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12960 return NULL;
12961 }
12962
12963 dwo_file_up dwo_file (new struct dwo_file);
12964 dwo_file->dwo_name = dwo_name;
12965 dwo_file->comp_dir = comp_dir;
12966 dwo_file->dbfd = std::move (dbfd);
12967
12968 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12969 &dwo_file->sections);
12970
12971 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
12972 dwo_file->cus);
12973
12974 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12975 dwo_file->sections.types, dwo_file->tus);
12976
12977 if (dwarf_read_debug)
12978 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12979
12980 return dwo_file.release ();
12981 }
12982
12983 /* This function is mapped across the sections and remembers the offset and
12984 size of each of the DWP debugging sections common to version 1 and 2 that
12985 we are interested in. */
12986
12987 static void
12988 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12989 void *dwp_file_ptr)
12990 {
12991 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12992 const struct dwop_section_names *names = &dwop_section_names;
12993 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12994
12995 /* Record the ELF section number for later lookup: this is what the
12996 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12997 gdb_assert (elf_section_nr < dwp_file->num_sections);
12998 dwp_file->elf_sections[elf_section_nr] = sectp;
12999
13000 /* Look for specific sections that we need. */
13001 if (section_is_p (sectp->name, &names->str_dwo))
13002 {
13003 dwp_file->sections.str.s.section = sectp;
13004 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13005 }
13006 else if (section_is_p (sectp->name, &names->cu_index))
13007 {
13008 dwp_file->sections.cu_index.s.section = sectp;
13009 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13010 }
13011 else if (section_is_p (sectp->name, &names->tu_index))
13012 {
13013 dwp_file->sections.tu_index.s.section = sectp;
13014 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13015 }
13016 }
13017
13018 /* This function is mapped across the sections and remembers the offset and
13019 size of each of the DWP version 2 debugging sections that we are interested
13020 in. This is split into a separate function because we don't know if we
13021 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13022
13023 static void
13024 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13025 {
13026 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13027 const struct dwop_section_names *names = &dwop_section_names;
13028 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13029
13030 /* Record the ELF section number for later lookup: this is what the
13031 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13032 gdb_assert (elf_section_nr < dwp_file->num_sections);
13033 dwp_file->elf_sections[elf_section_nr] = sectp;
13034
13035 /* Look for specific sections that we need. */
13036 if (section_is_p (sectp->name, &names->abbrev_dwo))
13037 {
13038 dwp_file->sections.abbrev.s.section = sectp;
13039 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13040 }
13041 else if (section_is_p (sectp->name, &names->info_dwo))
13042 {
13043 dwp_file->sections.info.s.section = sectp;
13044 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13045 }
13046 else if (section_is_p (sectp->name, &names->line_dwo))
13047 {
13048 dwp_file->sections.line.s.section = sectp;
13049 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13050 }
13051 else if (section_is_p (sectp->name, &names->loc_dwo))
13052 {
13053 dwp_file->sections.loc.s.section = sectp;
13054 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13055 }
13056 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13057 {
13058 dwp_file->sections.macinfo.s.section = sectp;
13059 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13060 }
13061 else if (section_is_p (sectp->name, &names->macro_dwo))
13062 {
13063 dwp_file->sections.macro.s.section = sectp;
13064 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13065 }
13066 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13067 {
13068 dwp_file->sections.str_offsets.s.section = sectp;
13069 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13070 }
13071 else if (section_is_p (sectp->name, &names->types_dwo))
13072 {
13073 dwp_file->sections.types.s.section = sectp;
13074 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13075 }
13076 }
13077
13078 /* Hash function for dwp_file loaded CUs/TUs. */
13079
13080 static hashval_t
13081 hash_dwp_loaded_cutus (const void *item)
13082 {
13083 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13084
13085 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13086 return dwo_unit->signature;
13087 }
13088
13089 /* Equality function for dwp_file loaded CUs/TUs. */
13090
13091 static int
13092 eq_dwp_loaded_cutus (const void *a, const void *b)
13093 {
13094 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13095 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13096
13097 return dua->signature == dub->signature;
13098 }
13099
13100 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13101
13102 static htab_t
13103 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13104 {
13105 return htab_create_alloc_ex (3,
13106 hash_dwp_loaded_cutus,
13107 eq_dwp_loaded_cutus,
13108 NULL,
13109 &objfile->objfile_obstack,
13110 hashtab_obstack_allocate,
13111 dummy_obstack_deallocate);
13112 }
13113
13114 /* Try to open DWP file FILE_NAME.
13115 The result is the bfd handle of the file.
13116 If there is a problem finding or opening the file, return NULL.
13117 Upon success, the canonicalized path of the file is stored in the bfd,
13118 same as symfile_bfd_open. */
13119
13120 static gdb_bfd_ref_ptr
13121 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13122 const char *file_name)
13123 {
13124 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13125 1 /*is_dwp*/,
13126 1 /*search_cwd*/));
13127 if (abfd != NULL)
13128 return abfd;
13129
13130 /* Work around upstream bug 15652.
13131 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13132 [Whether that's a "bug" is debatable, but it is getting in our way.]
13133 We have no real idea where the dwp file is, because gdb's realpath-ing
13134 of the executable's path may have discarded the needed info.
13135 [IWBN if the dwp file name was recorded in the executable, akin to
13136 .gnu_debuglink, but that doesn't exist yet.]
13137 Strip the directory from FILE_NAME and search again. */
13138 if (*debug_file_directory != '\0')
13139 {
13140 /* Don't implicitly search the current directory here.
13141 If the user wants to search "." to handle this case,
13142 it must be added to debug-file-directory. */
13143 return try_open_dwop_file (dwarf2_per_objfile,
13144 lbasename (file_name), 1 /*is_dwp*/,
13145 0 /*search_cwd*/);
13146 }
13147
13148 return NULL;
13149 }
13150
13151 /* Initialize the use of the DWP file for the current objfile.
13152 By convention the name of the DWP file is ${objfile}.dwp.
13153 The result is NULL if it can't be found. */
13154
13155 static std::unique_ptr<struct dwp_file>
13156 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13157 {
13158 struct objfile *objfile = dwarf2_per_objfile->objfile;
13159
13160 /* Try to find first .dwp for the binary file before any symbolic links
13161 resolving. */
13162
13163 /* If the objfile is a debug file, find the name of the real binary
13164 file and get the name of dwp file from there. */
13165 std::string dwp_name;
13166 if (objfile->separate_debug_objfile_backlink != NULL)
13167 {
13168 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13169 const char *backlink_basename = lbasename (backlink->original_name);
13170
13171 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13172 }
13173 else
13174 dwp_name = objfile->original_name;
13175
13176 dwp_name += ".dwp";
13177
13178 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13179 if (dbfd == NULL
13180 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13181 {
13182 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13183 dwp_name = objfile_name (objfile);
13184 dwp_name += ".dwp";
13185 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13186 }
13187
13188 if (dbfd == NULL)
13189 {
13190 if (dwarf_read_debug)
13191 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13192 return std::unique_ptr<dwp_file> ();
13193 }
13194
13195 const char *name = bfd_get_filename (dbfd.get ());
13196 std::unique_ptr<struct dwp_file> dwp_file
13197 (new struct dwp_file (name, std::move (dbfd)));
13198
13199 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13200 dwp_file->elf_sections =
13201 OBSTACK_CALLOC (&objfile->objfile_obstack,
13202 dwp_file->num_sections, asection *);
13203
13204 bfd_map_over_sections (dwp_file->dbfd.get (),
13205 dwarf2_locate_common_dwp_sections,
13206 dwp_file.get ());
13207
13208 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13209 0);
13210
13211 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13212 1);
13213
13214 /* The DWP file version is stored in the hash table. Oh well. */
13215 if (dwp_file->cus && dwp_file->tus
13216 && dwp_file->cus->version != dwp_file->tus->version)
13217 {
13218 /* Technically speaking, we should try to limp along, but this is
13219 pretty bizarre. We use pulongest here because that's the established
13220 portability solution (e.g, we cannot use %u for uint32_t). */
13221 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13222 " TU version %s [in DWP file %s]"),
13223 pulongest (dwp_file->cus->version),
13224 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13225 }
13226
13227 if (dwp_file->cus)
13228 dwp_file->version = dwp_file->cus->version;
13229 else if (dwp_file->tus)
13230 dwp_file->version = dwp_file->tus->version;
13231 else
13232 dwp_file->version = 2;
13233
13234 if (dwp_file->version == 2)
13235 bfd_map_over_sections (dwp_file->dbfd.get (),
13236 dwarf2_locate_v2_dwp_sections,
13237 dwp_file.get ());
13238
13239 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13240 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13241
13242 if (dwarf_read_debug)
13243 {
13244 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13245 fprintf_unfiltered (gdb_stdlog,
13246 " %s CUs, %s TUs\n",
13247 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13248 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13249 }
13250
13251 return dwp_file;
13252 }
13253
13254 /* Wrapper around open_and_init_dwp_file, only open it once. */
13255
13256 static struct dwp_file *
13257 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13258 {
13259 if (! dwarf2_per_objfile->dwp_checked)
13260 {
13261 dwarf2_per_objfile->dwp_file
13262 = open_and_init_dwp_file (dwarf2_per_objfile);
13263 dwarf2_per_objfile->dwp_checked = 1;
13264 }
13265 return dwarf2_per_objfile->dwp_file.get ();
13266 }
13267
13268 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13269 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13270 or in the DWP file for the objfile, referenced by THIS_UNIT.
13271 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13272 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13273
13274 This is called, for example, when wanting to read a variable with a
13275 complex location. Therefore we don't want to do file i/o for every call.
13276 Therefore we don't want to look for a DWO file on every call.
13277 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13278 then we check if we've already seen DWO_NAME, and only THEN do we check
13279 for a DWO file.
13280
13281 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13282 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13283
13284 static struct dwo_unit *
13285 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13286 const char *dwo_name, const char *comp_dir,
13287 ULONGEST signature, int is_debug_types)
13288 {
13289 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13290 struct objfile *objfile = dwarf2_per_objfile->objfile;
13291 const char *kind = is_debug_types ? "TU" : "CU";
13292 void **dwo_file_slot;
13293 struct dwo_file *dwo_file;
13294 struct dwp_file *dwp_file;
13295
13296 /* First see if there's a DWP file.
13297 If we have a DWP file but didn't find the DWO inside it, don't
13298 look for the original DWO file. It makes gdb behave differently
13299 depending on whether one is debugging in the build tree. */
13300
13301 dwp_file = get_dwp_file (dwarf2_per_objfile);
13302 if (dwp_file != NULL)
13303 {
13304 const struct dwp_hash_table *dwp_htab =
13305 is_debug_types ? dwp_file->tus : dwp_file->cus;
13306
13307 if (dwp_htab != NULL)
13308 {
13309 struct dwo_unit *dwo_cutu =
13310 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13311 signature, is_debug_types);
13312
13313 if (dwo_cutu != NULL)
13314 {
13315 if (dwarf_read_debug)
13316 {
13317 fprintf_unfiltered (gdb_stdlog,
13318 "Virtual DWO %s %s found: @%s\n",
13319 kind, hex_string (signature),
13320 host_address_to_string (dwo_cutu));
13321 }
13322 return dwo_cutu;
13323 }
13324 }
13325 }
13326 else
13327 {
13328 /* No DWP file, look for the DWO file. */
13329
13330 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13331 dwo_name, comp_dir);
13332 if (*dwo_file_slot == NULL)
13333 {
13334 /* Read in the file and build a table of the CUs/TUs it contains. */
13335 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13336 }
13337 /* NOTE: This will be NULL if unable to open the file. */
13338 dwo_file = (struct dwo_file *) *dwo_file_slot;
13339
13340 if (dwo_file != NULL)
13341 {
13342 struct dwo_unit *dwo_cutu = NULL;
13343
13344 if (is_debug_types && dwo_file->tus)
13345 {
13346 struct dwo_unit find_dwo_cutu;
13347
13348 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13349 find_dwo_cutu.signature = signature;
13350 dwo_cutu
13351 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13352 }
13353 else if (!is_debug_types && dwo_file->cus)
13354 {
13355 struct dwo_unit find_dwo_cutu;
13356
13357 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13358 find_dwo_cutu.signature = signature;
13359 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13360 &find_dwo_cutu);
13361 }
13362
13363 if (dwo_cutu != NULL)
13364 {
13365 if (dwarf_read_debug)
13366 {
13367 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13368 kind, dwo_name, hex_string (signature),
13369 host_address_to_string (dwo_cutu));
13370 }
13371 return dwo_cutu;
13372 }
13373 }
13374 }
13375
13376 /* We didn't find it. This could mean a dwo_id mismatch, or
13377 someone deleted the DWO/DWP file, or the search path isn't set up
13378 correctly to find the file. */
13379
13380 if (dwarf_read_debug)
13381 {
13382 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13383 kind, dwo_name, hex_string (signature));
13384 }
13385
13386 /* This is a warning and not a complaint because it can be caused by
13387 pilot error (e.g., user accidentally deleting the DWO). */
13388 {
13389 /* Print the name of the DWP file if we looked there, helps the user
13390 better diagnose the problem. */
13391 std::string dwp_text;
13392
13393 if (dwp_file != NULL)
13394 dwp_text = string_printf (" [in DWP file %s]",
13395 lbasename (dwp_file->name));
13396
13397 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13398 " [in module %s]"),
13399 kind, dwo_name, hex_string (signature),
13400 dwp_text.c_str (),
13401 this_unit->is_debug_types ? "TU" : "CU",
13402 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13403 }
13404 return NULL;
13405 }
13406
13407 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13408 See lookup_dwo_cutu_unit for details. */
13409
13410 static struct dwo_unit *
13411 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13412 const char *dwo_name, const char *comp_dir,
13413 ULONGEST signature)
13414 {
13415 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13416 }
13417
13418 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13419 See lookup_dwo_cutu_unit for details. */
13420
13421 static struct dwo_unit *
13422 lookup_dwo_type_unit (struct signatured_type *this_tu,
13423 const char *dwo_name, const char *comp_dir)
13424 {
13425 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13426 }
13427
13428 /* Traversal function for queue_and_load_all_dwo_tus. */
13429
13430 static int
13431 queue_and_load_dwo_tu (void **slot, void *info)
13432 {
13433 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13434 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13435 ULONGEST signature = dwo_unit->signature;
13436 struct signatured_type *sig_type =
13437 lookup_dwo_signatured_type (per_cu->cu, signature);
13438
13439 if (sig_type != NULL)
13440 {
13441 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13442
13443 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13444 a real dependency of PER_CU on SIG_TYPE. That is detected later
13445 while processing PER_CU. */
13446 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13447 load_full_type_unit (sig_cu);
13448 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13449 }
13450
13451 return 1;
13452 }
13453
13454 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13455 The DWO may have the only definition of the type, though it may not be
13456 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13457 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13458
13459 static void
13460 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13461 {
13462 struct dwo_unit *dwo_unit;
13463 struct dwo_file *dwo_file;
13464
13465 gdb_assert (!per_cu->is_debug_types);
13466 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13467 gdb_assert (per_cu->cu != NULL);
13468
13469 dwo_unit = per_cu->cu->dwo_unit;
13470 gdb_assert (dwo_unit != NULL);
13471
13472 dwo_file = dwo_unit->dwo_file;
13473 if (dwo_file->tus != NULL)
13474 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13475 }
13476
13477 /* Read in various DIEs. */
13478
13479 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13480 Inherit only the children of the DW_AT_abstract_origin DIE not being
13481 already referenced by DW_AT_abstract_origin from the children of the
13482 current DIE. */
13483
13484 static void
13485 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13486 {
13487 struct die_info *child_die;
13488 sect_offset *offsetp;
13489 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13490 struct die_info *origin_die;
13491 /* Iterator of the ORIGIN_DIE children. */
13492 struct die_info *origin_child_die;
13493 struct attribute *attr;
13494 struct dwarf2_cu *origin_cu;
13495 struct pending **origin_previous_list_in_scope;
13496
13497 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13498 if (!attr)
13499 return;
13500
13501 /* Note that following die references may follow to a die in a
13502 different cu. */
13503
13504 origin_cu = cu;
13505 origin_die = follow_die_ref (die, attr, &origin_cu);
13506
13507 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13508 symbols in. */
13509 origin_previous_list_in_scope = origin_cu->list_in_scope;
13510 origin_cu->list_in_scope = cu->list_in_scope;
13511
13512 if (die->tag != origin_die->tag
13513 && !(die->tag == DW_TAG_inlined_subroutine
13514 && origin_die->tag == DW_TAG_subprogram))
13515 complaint (_("DIE %s and its abstract origin %s have different tags"),
13516 sect_offset_str (die->sect_off),
13517 sect_offset_str (origin_die->sect_off));
13518
13519 std::vector<sect_offset> offsets;
13520
13521 for (child_die = die->child;
13522 child_die && child_die->tag;
13523 child_die = sibling_die (child_die))
13524 {
13525 struct die_info *child_origin_die;
13526 struct dwarf2_cu *child_origin_cu;
13527
13528 /* We are trying to process concrete instance entries:
13529 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13530 it's not relevant to our analysis here. i.e. detecting DIEs that are
13531 present in the abstract instance but not referenced in the concrete
13532 one. */
13533 if (child_die->tag == DW_TAG_call_site
13534 || child_die->tag == DW_TAG_GNU_call_site)
13535 continue;
13536
13537 /* For each CHILD_DIE, find the corresponding child of
13538 ORIGIN_DIE. If there is more than one layer of
13539 DW_AT_abstract_origin, follow them all; there shouldn't be,
13540 but GCC versions at least through 4.4 generate this (GCC PR
13541 40573). */
13542 child_origin_die = child_die;
13543 child_origin_cu = cu;
13544 while (1)
13545 {
13546 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13547 child_origin_cu);
13548 if (attr == NULL)
13549 break;
13550 child_origin_die = follow_die_ref (child_origin_die, attr,
13551 &child_origin_cu);
13552 }
13553
13554 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13555 counterpart may exist. */
13556 if (child_origin_die != child_die)
13557 {
13558 if (child_die->tag != child_origin_die->tag
13559 && !(child_die->tag == DW_TAG_inlined_subroutine
13560 && child_origin_die->tag == DW_TAG_subprogram))
13561 complaint (_("Child DIE %s and its abstract origin %s have "
13562 "different tags"),
13563 sect_offset_str (child_die->sect_off),
13564 sect_offset_str (child_origin_die->sect_off));
13565 if (child_origin_die->parent != origin_die)
13566 complaint (_("Child DIE %s and its abstract origin %s have "
13567 "different parents"),
13568 sect_offset_str (child_die->sect_off),
13569 sect_offset_str (child_origin_die->sect_off));
13570 else
13571 offsets.push_back (child_origin_die->sect_off);
13572 }
13573 }
13574 std::sort (offsets.begin (), offsets.end ());
13575 sect_offset *offsets_end = offsets.data () + offsets.size ();
13576 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13577 if (offsetp[-1] == *offsetp)
13578 complaint (_("Multiple children of DIE %s refer "
13579 "to DIE %s as their abstract origin"),
13580 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13581
13582 offsetp = offsets.data ();
13583 origin_child_die = origin_die->child;
13584 while (origin_child_die && origin_child_die->tag)
13585 {
13586 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13587 while (offsetp < offsets_end
13588 && *offsetp < origin_child_die->sect_off)
13589 offsetp++;
13590 if (offsetp >= offsets_end
13591 || *offsetp > origin_child_die->sect_off)
13592 {
13593 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13594 Check whether we're already processing ORIGIN_CHILD_DIE.
13595 This can happen with mutually referenced abstract_origins.
13596 PR 16581. */
13597 if (!origin_child_die->in_process)
13598 process_die (origin_child_die, origin_cu);
13599 }
13600 origin_child_die = sibling_die (origin_child_die);
13601 }
13602 origin_cu->list_in_scope = origin_previous_list_in_scope;
13603 }
13604
13605 static void
13606 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13607 {
13608 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13609 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13610 struct context_stack *newobj;
13611 CORE_ADDR lowpc;
13612 CORE_ADDR highpc;
13613 struct die_info *child_die;
13614 struct attribute *attr, *call_line, *call_file;
13615 const char *name;
13616 CORE_ADDR baseaddr;
13617 struct block *block;
13618 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13619 std::vector<struct symbol *> template_args;
13620 struct template_symbol *templ_func = NULL;
13621
13622 if (inlined_func)
13623 {
13624 /* If we do not have call site information, we can't show the
13625 caller of this inlined function. That's too confusing, so
13626 only use the scope for local variables. */
13627 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13628 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13629 if (call_line == NULL || call_file == NULL)
13630 {
13631 read_lexical_block_scope (die, cu);
13632 return;
13633 }
13634 }
13635
13636 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13637
13638 name = dwarf2_name (die, cu);
13639
13640 /* Ignore functions with missing or empty names. These are actually
13641 illegal according to the DWARF standard. */
13642 if (name == NULL)
13643 {
13644 complaint (_("missing name for subprogram DIE at %s"),
13645 sect_offset_str (die->sect_off));
13646 return;
13647 }
13648
13649 /* Ignore functions with missing or invalid low and high pc attributes. */
13650 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13651 <= PC_BOUNDS_INVALID)
13652 {
13653 attr = dwarf2_attr (die, DW_AT_external, cu);
13654 if (!attr || !DW_UNSND (attr))
13655 complaint (_("cannot get low and high bounds "
13656 "for subprogram DIE at %s"),
13657 sect_offset_str (die->sect_off));
13658 return;
13659 }
13660
13661 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13662 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13663
13664 /* If we have any template arguments, then we must allocate a
13665 different sort of symbol. */
13666 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13667 {
13668 if (child_die->tag == DW_TAG_template_type_param
13669 || child_die->tag == DW_TAG_template_value_param)
13670 {
13671 templ_func = allocate_template_symbol (objfile);
13672 templ_func->subclass = SYMBOL_TEMPLATE;
13673 break;
13674 }
13675 }
13676
13677 newobj = cu->get_builder ()->push_context (0, lowpc);
13678 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13679 (struct symbol *) templ_func);
13680
13681 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13682 set_objfile_main_name (objfile, SYMBOL_LINKAGE_NAME (newobj->name),
13683 cu->language);
13684
13685 /* If there is a location expression for DW_AT_frame_base, record
13686 it. */
13687 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13688 if (attr)
13689 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13690
13691 /* If there is a location for the static link, record it. */
13692 newobj->static_link = NULL;
13693 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13694 if (attr)
13695 {
13696 newobj->static_link
13697 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13698 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13699 dwarf2_per_cu_addr_type (cu->per_cu));
13700 }
13701
13702 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13703
13704 if (die->child != NULL)
13705 {
13706 child_die = die->child;
13707 while (child_die && child_die->tag)
13708 {
13709 if (child_die->tag == DW_TAG_template_type_param
13710 || child_die->tag == DW_TAG_template_value_param)
13711 {
13712 struct symbol *arg = new_symbol (child_die, NULL, cu);
13713
13714 if (arg != NULL)
13715 template_args.push_back (arg);
13716 }
13717 else
13718 process_die (child_die, cu);
13719 child_die = sibling_die (child_die);
13720 }
13721 }
13722
13723 inherit_abstract_dies (die, cu);
13724
13725 /* If we have a DW_AT_specification, we might need to import using
13726 directives from the context of the specification DIE. See the
13727 comment in determine_prefix. */
13728 if (cu->language == language_cplus
13729 && dwarf2_attr (die, DW_AT_specification, cu))
13730 {
13731 struct dwarf2_cu *spec_cu = cu;
13732 struct die_info *spec_die = die_specification (die, &spec_cu);
13733
13734 while (spec_die)
13735 {
13736 child_die = spec_die->child;
13737 while (child_die && child_die->tag)
13738 {
13739 if (child_die->tag == DW_TAG_imported_module)
13740 process_die (child_die, spec_cu);
13741 child_die = sibling_die (child_die);
13742 }
13743
13744 /* In some cases, GCC generates specification DIEs that
13745 themselves contain DW_AT_specification attributes. */
13746 spec_die = die_specification (spec_die, &spec_cu);
13747 }
13748 }
13749
13750 struct context_stack cstk = cu->get_builder ()->pop_context ();
13751 /* Make a block for the local symbols within. */
13752 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13753 cstk.static_link, lowpc, highpc);
13754
13755 /* For C++, set the block's scope. */
13756 if ((cu->language == language_cplus
13757 || cu->language == language_fortran
13758 || cu->language == language_d
13759 || cu->language == language_rust)
13760 && cu->processing_has_namespace_info)
13761 block_set_scope (block, determine_prefix (die, cu),
13762 &objfile->objfile_obstack);
13763
13764 /* If we have address ranges, record them. */
13765 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13766
13767 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13768
13769 /* Attach template arguments to function. */
13770 if (!template_args.empty ())
13771 {
13772 gdb_assert (templ_func != NULL);
13773
13774 templ_func->n_template_arguments = template_args.size ();
13775 templ_func->template_arguments
13776 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13777 templ_func->n_template_arguments);
13778 memcpy (templ_func->template_arguments,
13779 template_args.data (),
13780 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13781
13782 /* Make sure that the symtab is set on the new symbols. Even
13783 though they don't appear in this symtab directly, other parts
13784 of gdb assume that symbols do, and this is reasonably
13785 true. */
13786 for (symbol *sym : template_args)
13787 symbol_set_symtab (sym, symbol_symtab (templ_func));
13788 }
13789
13790 /* In C++, we can have functions nested inside functions (e.g., when
13791 a function declares a class that has methods). This means that
13792 when we finish processing a function scope, we may need to go
13793 back to building a containing block's symbol lists. */
13794 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13795 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13796
13797 /* If we've finished processing a top-level function, subsequent
13798 symbols go in the file symbol list. */
13799 if (cu->get_builder ()->outermost_context_p ())
13800 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13801 }
13802
13803 /* Process all the DIES contained within a lexical block scope. Start
13804 a new scope, process the dies, and then close the scope. */
13805
13806 static void
13807 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13808 {
13809 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13810 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13811 CORE_ADDR lowpc, highpc;
13812 struct die_info *child_die;
13813 CORE_ADDR baseaddr;
13814
13815 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13816
13817 /* Ignore blocks with missing or invalid low and high pc attributes. */
13818 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13819 as multiple lexical blocks? Handling children in a sane way would
13820 be nasty. Might be easier to properly extend generic blocks to
13821 describe ranges. */
13822 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13823 {
13824 case PC_BOUNDS_NOT_PRESENT:
13825 /* DW_TAG_lexical_block has no attributes, process its children as if
13826 there was no wrapping by that DW_TAG_lexical_block.
13827 GCC does no longer produces such DWARF since GCC r224161. */
13828 for (child_die = die->child;
13829 child_die != NULL && child_die->tag;
13830 child_die = sibling_die (child_die))
13831 process_die (child_die, cu);
13832 return;
13833 case PC_BOUNDS_INVALID:
13834 return;
13835 }
13836 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13837 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13838
13839 cu->get_builder ()->push_context (0, lowpc);
13840 if (die->child != NULL)
13841 {
13842 child_die = die->child;
13843 while (child_die && child_die->tag)
13844 {
13845 process_die (child_die, cu);
13846 child_die = sibling_die (child_die);
13847 }
13848 }
13849 inherit_abstract_dies (die, cu);
13850 struct context_stack cstk = cu->get_builder ()->pop_context ();
13851
13852 if (*cu->get_builder ()->get_local_symbols () != NULL
13853 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13854 {
13855 struct block *block
13856 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13857 cstk.start_addr, highpc);
13858
13859 /* Note that recording ranges after traversing children, as we
13860 do here, means that recording a parent's ranges entails
13861 walking across all its children's ranges as they appear in
13862 the address map, which is quadratic behavior.
13863
13864 It would be nicer to record the parent's ranges before
13865 traversing its children, simply overriding whatever you find
13866 there. But since we don't even decide whether to create a
13867 block until after we've traversed its children, that's hard
13868 to do. */
13869 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13870 }
13871 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13872 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13873 }
13874
13875 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13876
13877 static void
13878 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13879 {
13880 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13881 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13882 CORE_ADDR pc, baseaddr;
13883 struct attribute *attr;
13884 struct call_site *call_site, call_site_local;
13885 void **slot;
13886 int nparams;
13887 struct die_info *child_die;
13888
13889 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13890
13891 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13892 if (attr == NULL)
13893 {
13894 /* This was a pre-DWARF-5 GNU extension alias
13895 for DW_AT_call_return_pc. */
13896 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13897 }
13898 if (!attr)
13899 {
13900 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13901 "DIE %s [in module %s]"),
13902 sect_offset_str (die->sect_off), objfile_name (objfile));
13903 return;
13904 }
13905 pc = attr_value_as_address (attr) + baseaddr;
13906 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13907
13908 if (cu->call_site_htab == NULL)
13909 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13910 NULL, &objfile->objfile_obstack,
13911 hashtab_obstack_allocate, NULL);
13912 call_site_local.pc = pc;
13913 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13914 if (*slot != NULL)
13915 {
13916 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13917 "DIE %s [in module %s]"),
13918 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13919 objfile_name (objfile));
13920 return;
13921 }
13922
13923 /* Count parameters at the caller. */
13924
13925 nparams = 0;
13926 for (child_die = die->child; child_die && child_die->tag;
13927 child_die = sibling_die (child_die))
13928 {
13929 if (child_die->tag != DW_TAG_call_site_parameter
13930 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13931 {
13932 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13933 "DW_TAG_call_site child DIE %s [in module %s]"),
13934 child_die->tag, sect_offset_str (child_die->sect_off),
13935 objfile_name (objfile));
13936 continue;
13937 }
13938
13939 nparams++;
13940 }
13941
13942 call_site
13943 = ((struct call_site *)
13944 obstack_alloc (&objfile->objfile_obstack,
13945 sizeof (*call_site)
13946 + (sizeof (*call_site->parameter) * (nparams - 1))));
13947 *slot = call_site;
13948 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13949 call_site->pc = pc;
13950
13951 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13952 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13953 {
13954 struct die_info *func_die;
13955
13956 /* Skip also over DW_TAG_inlined_subroutine. */
13957 for (func_die = die->parent;
13958 func_die && func_die->tag != DW_TAG_subprogram
13959 && func_die->tag != DW_TAG_subroutine_type;
13960 func_die = func_die->parent);
13961
13962 /* DW_AT_call_all_calls is a superset
13963 of DW_AT_call_all_tail_calls. */
13964 if (func_die
13965 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13966 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13967 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13968 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13969 {
13970 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13971 not complete. But keep CALL_SITE for look ups via call_site_htab,
13972 both the initial caller containing the real return address PC and
13973 the final callee containing the current PC of a chain of tail
13974 calls do not need to have the tail call list complete. But any
13975 function candidate for a virtual tail call frame searched via
13976 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13977 determined unambiguously. */
13978 }
13979 else
13980 {
13981 struct type *func_type = NULL;
13982
13983 if (func_die)
13984 func_type = get_die_type (func_die, cu);
13985 if (func_type != NULL)
13986 {
13987 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13988
13989 /* Enlist this call site to the function. */
13990 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13991 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13992 }
13993 else
13994 complaint (_("Cannot find function owning DW_TAG_call_site "
13995 "DIE %s [in module %s]"),
13996 sect_offset_str (die->sect_off), objfile_name (objfile));
13997 }
13998 }
13999
14000 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14001 if (attr == NULL)
14002 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14003 if (attr == NULL)
14004 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14005 if (attr == NULL)
14006 {
14007 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14008 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14009 }
14010 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14011 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14012 /* Keep NULL DWARF_BLOCK. */;
14013 else if (attr_form_is_block (attr))
14014 {
14015 struct dwarf2_locexpr_baton *dlbaton;
14016
14017 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14018 dlbaton->data = DW_BLOCK (attr)->data;
14019 dlbaton->size = DW_BLOCK (attr)->size;
14020 dlbaton->per_cu = cu->per_cu;
14021
14022 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14023 }
14024 else if (attr_form_is_ref (attr))
14025 {
14026 struct dwarf2_cu *target_cu = cu;
14027 struct die_info *target_die;
14028
14029 target_die = follow_die_ref (die, attr, &target_cu);
14030 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14031 if (die_is_declaration (target_die, target_cu))
14032 {
14033 const char *target_physname;
14034
14035 /* Prefer the mangled name; otherwise compute the demangled one. */
14036 target_physname = dw2_linkage_name (target_die, target_cu);
14037 if (target_physname == NULL)
14038 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14039 if (target_physname == NULL)
14040 complaint (_("DW_AT_call_target target DIE has invalid "
14041 "physname, for referencing DIE %s [in module %s]"),
14042 sect_offset_str (die->sect_off), objfile_name (objfile));
14043 else
14044 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14045 }
14046 else
14047 {
14048 CORE_ADDR lowpc;
14049
14050 /* DW_AT_entry_pc should be preferred. */
14051 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14052 <= PC_BOUNDS_INVALID)
14053 complaint (_("DW_AT_call_target target DIE has invalid "
14054 "low pc, for referencing DIE %s [in module %s]"),
14055 sect_offset_str (die->sect_off), objfile_name (objfile));
14056 else
14057 {
14058 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14059 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14060 }
14061 }
14062 }
14063 else
14064 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14065 "block nor reference, for DIE %s [in module %s]"),
14066 sect_offset_str (die->sect_off), objfile_name (objfile));
14067
14068 call_site->per_cu = cu->per_cu;
14069
14070 for (child_die = die->child;
14071 child_die && child_die->tag;
14072 child_die = sibling_die (child_die))
14073 {
14074 struct call_site_parameter *parameter;
14075 struct attribute *loc, *origin;
14076
14077 if (child_die->tag != DW_TAG_call_site_parameter
14078 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14079 {
14080 /* Already printed the complaint above. */
14081 continue;
14082 }
14083
14084 gdb_assert (call_site->parameter_count < nparams);
14085 parameter = &call_site->parameter[call_site->parameter_count];
14086
14087 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14088 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14089 register is contained in DW_AT_call_value. */
14090
14091 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14092 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14093 if (origin == NULL)
14094 {
14095 /* This was a pre-DWARF-5 GNU extension alias
14096 for DW_AT_call_parameter. */
14097 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14098 }
14099 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14100 {
14101 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14102
14103 sect_offset sect_off
14104 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14105 if (!offset_in_cu_p (&cu->header, sect_off))
14106 {
14107 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14108 binding can be done only inside one CU. Such referenced DIE
14109 therefore cannot be even moved to DW_TAG_partial_unit. */
14110 complaint (_("DW_AT_call_parameter offset is not in CU for "
14111 "DW_TAG_call_site child DIE %s [in module %s]"),
14112 sect_offset_str (child_die->sect_off),
14113 objfile_name (objfile));
14114 continue;
14115 }
14116 parameter->u.param_cu_off
14117 = (cu_offset) (sect_off - cu->header.sect_off);
14118 }
14119 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14120 {
14121 complaint (_("No DW_FORM_block* DW_AT_location for "
14122 "DW_TAG_call_site child DIE %s [in module %s]"),
14123 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14124 continue;
14125 }
14126 else
14127 {
14128 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14129 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14130 if (parameter->u.dwarf_reg != -1)
14131 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14132 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14133 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14134 &parameter->u.fb_offset))
14135 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14136 else
14137 {
14138 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14139 "for DW_FORM_block* DW_AT_location is supported for "
14140 "DW_TAG_call_site child DIE %s "
14141 "[in module %s]"),
14142 sect_offset_str (child_die->sect_off),
14143 objfile_name (objfile));
14144 continue;
14145 }
14146 }
14147
14148 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14149 if (attr == NULL)
14150 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14151 if (!attr_form_is_block (attr))
14152 {
14153 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14154 "DW_TAG_call_site child DIE %s [in module %s]"),
14155 sect_offset_str (child_die->sect_off),
14156 objfile_name (objfile));
14157 continue;
14158 }
14159 parameter->value = DW_BLOCK (attr)->data;
14160 parameter->value_size = DW_BLOCK (attr)->size;
14161
14162 /* Parameters are not pre-cleared by memset above. */
14163 parameter->data_value = NULL;
14164 parameter->data_value_size = 0;
14165 call_site->parameter_count++;
14166
14167 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14168 if (attr == NULL)
14169 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14170 if (attr)
14171 {
14172 if (!attr_form_is_block (attr))
14173 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14174 "DW_TAG_call_site child DIE %s [in module %s]"),
14175 sect_offset_str (child_die->sect_off),
14176 objfile_name (objfile));
14177 else
14178 {
14179 parameter->data_value = DW_BLOCK (attr)->data;
14180 parameter->data_value_size = DW_BLOCK (attr)->size;
14181 }
14182 }
14183 }
14184 }
14185
14186 /* Helper function for read_variable. If DIE represents a virtual
14187 table, then return the type of the concrete object that is
14188 associated with the virtual table. Otherwise, return NULL. */
14189
14190 static struct type *
14191 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14192 {
14193 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14194 if (attr == NULL)
14195 return NULL;
14196
14197 /* Find the type DIE. */
14198 struct die_info *type_die = NULL;
14199 struct dwarf2_cu *type_cu = cu;
14200
14201 if (attr_form_is_ref (attr))
14202 type_die = follow_die_ref (die, attr, &type_cu);
14203 if (type_die == NULL)
14204 return NULL;
14205
14206 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14207 return NULL;
14208 return die_containing_type (type_die, type_cu);
14209 }
14210
14211 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14212
14213 static void
14214 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14215 {
14216 struct rust_vtable_symbol *storage = NULL;
14217
14218 if (cu->language == language_rust)
14219 {
14220 struct type *containing_type = rust_containing_type (die, cu);
14221
14222 if (containing_type != NULL)
14223 {
14224 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14225
14226 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14227 struct rust_vtable_symbol);
14228 initialize_objfile_symbol (storage);
14229 storage->concrete_type = containing_type;
14230 storage->subclass = SYMBOL_RUST_VTABLE;
14231 }
14232 }
14233
14234 struct symbol *res = new_symbol (die, NULL, cu, storage);
14235 struct attribute *abstract_origin
14236 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14237 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14238 if (res == NULL && loc && abstract_origin)
14239 {
14240 /* We have a variable without a name, but with a location and an abstract
14241 origin. This may be a concrete instance of an abstract variable
14242 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14243 later. */
14244 struct dwarf2_cu *origin_cu = cu;
14245 struct die_info *origin_die
14246 = follow_die_ref (die, abstract_origin, &origin_cu);
14247 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
14248 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
14249 }
14250 }
14251
14252 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14253 reading .debug_rnglists.
14254 Callback's type should be:
14255 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14256 Return true if the attributes are present and valid, otherwise,
14257 return false. */
14258
14259 template <typename Callback>
14260 static bool
14261 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14262 Callback &&callback)
14263 {
14264 struct dwarf2_per_objfile *dwarf2_per_objfile
14265 = cu->per_cu->dwarf2_per_objfile;
14266 struct objfile *objfile = dwarf2_per_objfile->objfile;
14267 bfd *obfd = objfile->obfd;
14268 /* Base address selection entry. */
14269 CORE_ADDR base;
14270 int found_base;
14271 const gdb_byte *buffer;
14272 CORE_ADDR baseaddr;
14273 bool overflow = false;
14274
14275 found_base = cu->base_known;
14276 base = cu->base_address;
14277
14278 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14279 if (offset >= dwarf2_per_objfile->rnglists.size)
14280 {
14281 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14282 offset);
14283 return false;
14284 }
14285 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14286
14287 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14288
14289 while (1)
14290 {
14291 /* Initialize it due to a false compiler warning. */
14292 CORE_ADDR range_beginning = 0, range_end = 0;
14293 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14294 + dwarf2_per_objfile->rnglists.size);
14295 unsigned int bytes_read;
14296
14297 if (buffer == buf_end)
14298 {
14299 overflow = true;
14300 break;
14301 }
14302 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14303 switch (rlet)
14304 {
14305 case DW_RLE_end_of_list:
14306 break;
14307 case DW_RLE_base_address:
14308 if (buffer + cu->header.addr_size > buf_end)
14309 {
14310 overflow = true;
14311 break;
14312 }
14313 base = read_address (obfd, buffer, cu, &bytes_read);
14314 found_base = 1;
14315 buffer += bytes_read;
14316 break;
14317 case DW_RLE_start_length:
14318 if (buffer + cu->header.addr_size > buf_end)
14319 {
14320 overflow = true;
14321 break;
14322 }
14323 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14324 buffer += bytes_read;
14325 range_end = (range_beginning
14326 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14327 buffer += bytes_read;
14328 if (buffer > buf_end)
14329 {
14330 overflow = true;
14331 break;
14332 }
14333 break;
14334 case DW_RLE_offset_pair:
14335 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14336 buffer += bytes_read;
14337 if (buffer > buf_end)
14338 {
14339 overflow = true;
14340 break;
14341 }
14342 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14343 buffer += bytes_read;
14344 if (buffer > buf_end)
14345 {
14346 overflow = true;
14347 break;
14348 }
14349 break;
14350 case DW_RLE_start_end:
14351 if (buffer + 2 * cu->header.addr_size > buf_end)
14352 {
14353 overflow = true;
14354 break;
14355 }
14356 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14357 buffer += bytes_read;
14358 range_end = read_address (obfd, buffer, cu, &bytes_read);
14359 buffer += bytes_read;
14360 break;
14361 default:
14362 complaint (_("Invalid .debug_rnglists data (no base address)"));
14363 return false;
14364 }
14365 if (rlet == DW_RLE_end_of_list || overflow)
14366 break;
14367 if (rlet == DW_RLE_base_address)
14368 continue;
14369
14370 if (!found_base)
14371 {
14372 /* We have no valid base address for the ranges
14373 data. */
14374 complaint (_("Invalid .debug_rnglists data (no base address)"));
14375 return false;
14376 }
14377
14378 if (range_beginning > range_end)
14379 {
14380 /* Inverted range entries are invalid. */
14381 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14382 return false;
14383 }
14384
14385 /* Empty range entries have no effect. */
14386 if (range_beginning == range_end)
14387 continue;
14388
14389 range_beginning += base;
14390 range_end += base;
14391
14392 /* A not-uncommon case of bad debug info.
14393 Don't pollute the addrmap with bad data. */
14394 if (range_beginning + baseaddr == 0
14395 && !dwarf2_per_objfile->has_section_at_zero)
14396 {
14397 complaint (_(".debug_rnglists entry has start address of zero"
14398 " [in module %s]"), objfile_name (objfile));
14399 continue;
14400 }
14401
14402 callback (range_beginning, range_end);
14403 }
14404
14405 if (overflow)
14406 {
14407 complaint (_("Offset %d is not terminated "
14408 "for DW_AT_ranges attribute"),
14409 offset);
14410 return false;
14411 }
14412
14413 return true;
14414 }
14415
14416 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14417 Callback's type should be:
14418 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14419 Return 1 if the attributes are present and valid, otherwise, return 0. */
14420
14421 template <typename Callback>
14422 static int
14423 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14424 Callback &&callback)
14425 {
14426 struct dwarf2_per_objfile *dwarf2_per_objfile
14427 = cu->per_cu->dwarf2_per_objfile;
14428 struct objfile *objfile = dwarf2_per_objfile->objfile;
14429 struct comp_unit_head *cu_header = &cu->header;
14430 bfd *obfd = objfile->obfd;
14431 unsigned int addr_size = cu_header->addr_size;
14432 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14433 /* Base address selection entry. */
14434 CORE_ADDR base;
14435 int found_base;
14436 unsigned int dummy;
14437 const gdb_byte *buffer;
14438 CORE_ADDR baseaddr;
14439
14440 if (cu_header->version >= 5)
14441 return dwarf2_rnglists_process (offset, cu, callback);
14442
14443 found_base = cu->base_known;
14444 base = cu->base_address;
14445
14446 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14447 if (offset >= dwarf2_per_objfile->ranges.size)
14448 {
14449 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14450 offset);
14451 return 0;
14452 }
14453 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14454
14455 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14456
14457 while (1)
14458 {
14459 CORE_ADDR range_beginning, range_end;
14460
14461 range_beginning = read_address (obfd, buffer, cu, &dummy);
14462 buffer += addr_size;
14463 range_end = read_address (obfd, buffer, cu, &dummy);
14464 buffer += addr_size;
14465 offset += 2 * addr_size;
14466
14467 /* An end of list marker is a pair of zero addresses. */
14468 if (range_beginning == 0 && range_end == 0)
14469 /* Found the end of list entry. */
14470 break;
14471
14472 /* Each base address selection entry is a pair of 2 values.
14473 The first is the largest possible address, the second is
14474 the base address. Check for a base address here. */
14475 if ((range_beginning & mask) == mask)
14476 {
14477 /* If we found the largest possible address, then we already
14478 have the base address in range_end. */
14479 base = range_end;
14480 found_base = 1;
14481 continue;
14482 }
14483
14484 if (!found_base)
14485 {
14486 /* We have no valid base address for the ranges
14487 data. */
14488 complaint (_("Invalid .debug_ranges data (no base address)"));
14489 return 0;
14490 }
14491
14492 if (range_beginning > range_end)
14493 {
14494 /* Inverted range entries are invalid. */
14495 complaint (_("Invalid .debug_ranges data (inverted range)"));
14496 return 0;
14497 }
14498
14499 /* Empty range entries have no effect. */
14500 if (range_beginning == range_end)
14501 continue;
14502
14503 range_beginning += base;
14504 range_end += base;
14505
14506 /* A not-uncommon case of bad debug info.
14507 Don't pollute the addrmap with bad data. */
14508 if (range_beginning + baseaddr == 0
14509 && !dwarf2_per_objfile->has_section_at_zero)
14510 {
14511 complaint (_(".debug_ranges entry has start address of zero"
14512 " [in module %s]"), objfile_name (objfile));
14513 continue;
14514 }
14515
14516 callback (range_beginning, range_end);
14517 }
14518
14519 return 1;
14520 }
14521
14522 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14523 Return 1 if the attributes are present and valid, otherwise, return 0.
14524 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14525
14526 static int
14527 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14528 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14529 struct partial_symtab *ranges_pst)
14530 {
14531 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14532 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14533 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14534 SECT_OFF_TEXT (objfile));
14535 int low_set = 0;
14536 CORE_ADDR low = 0;
14537 CORE_ADDR high = 0;
14538 int retval;
14539
14540 retval = dwarf2_ranges_process (offset, cu,
14541 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14542 {
14543 if (ranges_pst != NULL)
14544 {
14545 CORE_ADDR lowpc;
14546 CORE_ADDR highpc;
14547
14548 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14549 range_beginning + baseaddr)
14550 - baseaddr);
14551 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14552 range_end + baseaddr)
14553 - baseaddr);
14554 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14555 lowpc, highpc - 1, ranges_pst);
14556 }
14557
14558 /* FIXME: This is recording everything as a low-high
14559 segment of consecutive addresses. We should have a
14560 data structure for discontiguous block ranges
14561 instead. */
14562 if (! low_set)
14563 {
14564 low = range_beginning;
14565 high = range_end;
14566 low_set = 1;
14567 }
14568 else
14569 {
14570 if (range_beginning < low)
14571 low = range_beginning;
14572 if (range_end > high)
14573 high = range_end;
14574 }
14575 });
14576 if (!retval)
14577 return 0;
14578
14579 if (! low_set)
14580 /* If the first entry is an end-of-list marker, the range
14581 describes an empty scope, i.e. no instructions. */
14582 return 0;
14583
14584 if (low_return)
14585 *low_return = low;
14586 if (high_return)
14587 *high_return = high;
14588 return 1;
14589 }
14590
14591 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14592 definition for the return value. *LOWPC and *HIGHPC are set iff
14593 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14594
14595 static enum pc_bounds_kind
14596 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14597 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14598 struct partial_symtab *pst)
14599 {
14600 struct dwarf2_per_objfile *dwarf2_per_objfile
14601 = cu->per_cu->dwarf2_per_objfile;
14602 struct attribute *attr;
14603 struct attribute *attr_high;
14604 CORE_ADDR low = 0;
14605 CORE_ADDR high = 0;
14606 enum pc_bounds_kind ret;
14607
14608 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14609 if (attr_high)
14610 {
14611 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14612 if (attr)
14613 {
14614 low = attr_value_as_address (attr);
14615 high = attr_value_as_address (attr_high);
14616 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14617 high += low;
14618 }
14619 else
14620 /* Found high w/o low attribute. */
14621 return PC_BOUNDS_INVALID;
14622
14623 /* Found consecutive range of addresses. */
14624 ret = PC_BOUNDS_HIGH_LOW;
14625 }
14626 else
14627 {
14628 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14629 if (attr != NULL)
14630 {
14631 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14632 We take advantage of the fact that DW_AT_ranges does not appear
14633 in DW_TAG_compile_unit of DWO files. */
14634 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14635 unsigned int ranges_offset = (DW_UNSND (attr)
14636 + (need_ranges_base
14637 ? cu->ranges_base
14638 : 0));
14639
14640 /* Value of the DW_AT_ranges attribute is the offset in the
14641 .debug_ranges section. */
14642 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14643 return PC_BOUNDS_INVALID;
14644 /* Found discontinuous range of addresses. */
14645 ret = PC_BOUNDS_RANGES;
14646 }
14647 else
14648 return PC_BOUNDS_NOT_PRESENT;
14649 }
14650
14651 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14652 if (high <= low)
14653 return PC_BOUNDS_INVALID;
14654
14655 /* When using the GNU linker, .gnu.linkonce. sections are used to
14656 eliminate duplicate copies of functions and vtables and such.
14657 The linker will arbitrarily choose one and discard the others.
14658 The AT_*_pc values for such functions refer to local labels in
14659 these sections. If the section from that file was discarded, the
14660 labels are not in the output, so the relocs get a value of 0.
14661 If this is a discarded function, mark the pc bounds as invalid,
14662 so that GDB will ignore it. */
14663 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14664 return PC_BOUNDS_INVALID;
14665
14666 *lowpc = low;
14667 if (highpc)
14668 *highpc = high;
14669 return ret;
14670 }
14671
14672 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14673 its low and high PC addresses. Do nothing if these addresses could not
14674 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14675 and HIGHPC to the high address if greater than HIGHPC. */
14676
14677 static void
14678 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14679 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14680 struct dwarf2_cu *cu)
14681 {
14682 CORE_ADDR low, high;
14683 struct die_info *child = die->child;
14684
14685 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14686 {
14687 *lowpc = std::min (*lowpc, low);
14688 *highpc = std::max (*highpc, high);
14689 }
14690
14691 /* If the language does not allow nested subprograms (either inside
14692 subprograms or lexical blocks), we're done. */
14693 if (cu->language != language_ada)
14694 return;
14695
14696 /* Check all the children of the given DIE. If it contains nested
14697 subprograms, then check their pc bounds. Likewise, we need to
14698 check lexical blocks as well, as they may also contain subprogram
14699 definitions. */
14700 while (child && child->tag)
14701 {
14702 if (child->tag == DW_TAG_subprogram
14703 || child->tag == DW_TAG_lexical_block)
14704 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14705 child = sibling_die (child);
14706 }
14707 }
14708
14709 /* Get the low and high pc's represented by the scope DIE, and store
14710 them in *LOWPC and *HIGHPC. If the correct values can't be
14711 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14712
14713 static void
14714 get_scope_pc_bounds (struct die_info *die,
14715 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14716 struct dwarf2_cu *cu)
14717 {
14718 CORE_ADDR best_low = (CORE_ADDR) -1;
14719 CORE_ADDR best_high = (CORE_ADDR) 0;
14720 CORE_ADDR current_low, current_high;
14721
14722 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14723 >= PC_BOUNDS_RANGES)
14724 {
14725 best_low = current_low;
14726 best_high = current_high;
14727 }
14728 else
14729 {
14730 struct die_info *child = die->child;
14731
14732 while (child && child->tag)
14733 {
14734 switch (child->tag) {
14735 case DW_TAG_subprogram:
14736 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14737 break;
14738 case DW_TAG_namespace:
14739 case DW_TAG_module:
14740 /* FIXME: carlton/2004-01-16: Should we do this for
14741 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14742 that current GCC's always emit the DIEs corresponding
14743 to definitions of methods of classes as children of a
14744 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14745 the DIEs giving the declarations, which could be
14746 anywhere). But I don't see any reason why the
14747 standards says that they have to be there. */
14748 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14749
14750 if (current_low != ((CORE_ADDR) -1))
14751 {
14752 best_low = std::min (best_low, current_low);
14753 best_high = std::max (best_high, current_high);
14754 }
14755 break;
14756 default:
14757 /* Ignore. */
14758 break;
14759 }
14760
14761 child = sibling_die (child);
14762 }
14763 }
14764
14765 *lowpc = best_low;
14766 *highpc = best_high;
14767 }
14768
14769 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14770 in DIE. */
14771
14772 static void
14773 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14774 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14775 {
14776 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14777 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14778 struct attribute *attr;
14779 struct attribute *attr_high;
14780
14781 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14782 if (attr_high)
14783 {
14784 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14785 if (attr)
14786 {
14787 CORE_ADDR low = attr_value_as_address (attr);
14788 CORE_ADDR high = attr_value_as_address (attr_high);
14789
14790 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14791 high += low;
14792
14793 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14794 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14795 cu->get_builder ()->record_block_range (block, low, high - 1);
14796 }
14797 }
14798
14799 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14800 if (attr)
14801 {
14802 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14803 We take advantage of the fact that DW_AT_ranges does not appear
14804 in DW_TAG_compile_unit of DWO files. */
14805 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14806
14807 /* The value of the DW_AT_ranges attribute is the offset of the
14808 address range list in the .debug_ranges section. */
14809 unsigned long offset = (DW_UNSND (attr)
14810 + (need_ranges_base ? cu->ranges_base : 0));
14811
14812 std::vector<blockrange> blockvec;
14813 dwarf2_ranges_process (offset, cu,
14814 [&] (CORE_ADDR start, CORE_ADDR end)
14815 {
14816 start += baseaddr;
14817 end += baseaddr;
14818 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14819 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14820 cu->get_builder ()->record_block_range (block, start, end - 1);
14821 blockvec.emplace_back (start, end);
14822 });
14823
14824 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14825 }
14826 }
14827
14828 /* Check whether the producer field indicates either of GCC < 4.6, or the
14829 Intel C/C++ compiler, and cache the result in CU. */
14830
14831 static void
14832 check_producer (struct dwarf2_cu *cu)
14833 {
14834 int major, minor;
14835
14836 if (cu->producer == NULL)
14837 {
14838 /* For unknown compilers expect their behavior is DWARF version
14839 compliant.
14840
14841 GCC started to support .debug_types sections by -gdwarf-4 since
14842 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14843 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14844 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14845 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14846 }
14847 else if (producer_is_gcc (cu->producer, &major, &minor))
14848 {
14849 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14850 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14851 }
14852 else if (producer_is_icc (cu->producer, &major, &minor))
14853 {
14854 cu->producer_is_icc = true;
14855 cu->producer_is_icc_lt_14 = major < 14;
14856 }
14857 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14858 cu->producer_is_codewarrior = true;
14859 else
14860 {
14861 /* For other non-GCC compilers, expect their behavior is DWARF version
14862 compliant. */
14863 }
14864
14865 cu->checked_producer = true;
14866 }
14867
14868 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14869 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14870 during 4.6.0 experimental. */
14871
14872 static bool
14873 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14874 {
14875 if (!cu->checked_producer)
14876 check_producer (cu);
14877
14878 return cu->producer_is_gxx_lt_4_6;
14879 }
14880
14881
14882 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14883 with incorrect is_stmt attributes. */
14884
14885 static bool
14886 producer_is_codewarrior (struct dwarf2_cu *cu)
14887 {
14888 if (!cu->checked_producer)
14889 check_producer (cu);
14890
14891 return cu->producer_is_codewarrior;
14892 }
14893
14894 /* Return the default accessibility type if it is not overriden by
14895 DW_AT_accessibility. */
14896
14897 static enum dwarf_access_attribute
14898 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14899 {
14900 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14901 {
14902 /* The default DWARF 2 accessibility for members is public, the default
14903 accessibility for inheritance is private. */
14904
14905 if (die->tag != DW_TAG_inheritance)
14906 return DW_ACCESS_public;
14907 else
14908 return DW_ACCESS_private;
14909 }
14910 else
14911 {
14912 /* DWARF 3+ defines the default accessibility a different way. The same
14913 rules apply now for DW_TAG_inheritance as for the members and it only
14914 depends on the container kind. */
14915
14916 if (die->parent->tag == DW_TAG_class_type)
14917 return DW_ACCESS_private;
14918 else
14919 return DW_ACCESS_public;
14920 }
14921 }
14922
14923 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14924 offset. If the attribute was not found return 0, otherwise return
14925 1. If it was found but could not properly be handled, set *OFFSET
14926 to 0. */
14927
14928 static int
14929 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14930 LONGEST *offset)
14931 {
14932 struct attribute *attr;
14933
14934 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14935 if (attr != NULL)
14936 {
14937 *offset = 0;
14938
14939 /* Note that we do not check for a section offset first here.
14940 This is because DW_AT_data_member_location is new in DWARF 4,
14941 so if we see it, we can assume that a constant form is really
14942 a constant and not a section offset. */
14943 if (attr_form_is_constant (attr))
14944 *offset = dwarf2_get_attr_constant_value (attr, 0);
14945 else if (attr_form_is_section_offset (attr))
14946 dwarf2_complex_location_expr_complaint ();
14947 else if (attr_form_is_block (attr))
14948 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14949 else
14950 dwarf2_complex_location_expr_complaint ();
14951
14952 return 1;
14953 }
14954
14955 return 0;
14956 }
14957
14958 /* Add an aggregate field to the field list. */
14959
14960 static void
14961 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14962 struct dwarf2_cu *cu)
14963 {
14964 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14965 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14966 struct nextfield *new_field;
14967 struct attribute *attr;
14968 struct field *fp;
14969 const char *fieldname = "";
14970
14971 if (die->tag == DW_TAG_inheritance)
14972 {
14973 fip->baseclasses.emplace_back ();
14974 new_field = &fip->baseclasses.back ();
14975 }
14976 else
14977 {
14978 fip->fields.emplace_back ();
14979 new_field = &fip->fields.back ();
14980 }
14981
14982 fip->nfields++;
14983
14984 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14985 if (attr)
14986 new_field->accessibility = DW_UNSND (attr);
14987 else
14988 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14989 if (new_field->accessibility != DW_ACCESS_public)
14990 fip->non_public_fields = 1;
14991
14992 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14993 if (attr)
14994 new_field->virtuality = DW_UNSND (attr);
14995 else
14996 new_field->virtuality = DW_VIRTUALITY_none;
14997
14998 fp = &new_field->field;
14999
15000 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15001 {
15002 LONGEST offset;
15003
15004 /* Data member other than a C++ static data member. */
15005
15006 /* Get type of field. */
15007 fp->type = die_type (die, cu);
15008
15009 SET_FIELD_BITPOS (*fp, 0);
15010
15011 /* Get bit size of field (zero if none). */
15012 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15013 if (attr)
15014 {
15015 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15016 }
15017 else
15018 {
15019 FIELD_BITSIZE (*fp) = 0;
15020 }
15021
15022 /* Get bit offset of field. */
15023 if (handle_data_member_location (die, cu, &offset))
15024 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15025 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15026 if (attr)
15027 {
15028 if (gdbarch_bits_big_endian (gdbarch))
15029 {
15030 /* For big endian bits, the DW_AT_bit_offset gives the
15031 additional bit offset from the MSB of the containing
15032 anonymous object to the MSB of the field. We don't
15033 have to do anything special since we don't need to
15034 know the size of the anonymous object. */
15035 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15036 }
15037 else
15038 {
15039 /* For little endian bits, compute the bit offset to the
15040 MSB of the anonymous object, subtract off the number of
15041 bits from the MSB of the field to the MSB of the
15042 object, and then subtract off the number of bits of
15043 the field itself. The result is the bit offset of
15044 the LSB of the field. */
15045 int anonymous_size;
15046 int bit_offset = DW_UNSND (attr);
15047
15048 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15049 if (attr)
15050 {
15051 /* The size of the anonymous object containing
15052 the bit field is explicit, so use the
15053 indicated size (in bytes). */
15054 anonymous_size = DW_UNSND (attr);
15055 }
15056 else
15057 {
15058 /* The size of the anonymous object containing
15059 the bit field must be inferred from the type
15060 attribute of the data member containing the
15061 bit field. */
15062 anonymous_size = TYPE_LENGTH (fp->type);
15063 }
15064 SET_FIELD_BITPOS (*fp,
15065 (FIELD_BITPOS (*fp)
15066 + anonymous_size * bits_per_byte
15067 - bit_offset - FIELD_BITSIZE (*fp)));
15068 }
15069 }
15070 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15071 if (attr != NULL)
15072 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15073 + dwarf2_get_attr_constant_value (attr, 0)));
15074
15075 /* Get name of field. */
15076 fieldname = dwarf2_name (die, cu);
15077 if (fieldname == NULL)
15078 fieldname = "";
15079
15080 /* The name is already allocated along with this objfile, so we don't
15081 need to duplicate it for the type. */
15082 fp->name = fieldname;
15083
15084 /* Change accessibility for artificial fields (e.g. virtual table
15085 pointer or virtual base class pointer) to private. */
15086 if (dwarf2_attr (die, DW_AT_artificial, cu))
15087 {
15088 FIELD_ARTIFICIAL (*fp) = 1;
15089 new_field->accessibility = DW_ACCESS_private;
15090 fip->non_public_fields = 1;
15091 }
15092 }
15093 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15094 {
15095 /* C++ static member. */
15096
15097 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15098 is a declaration, but all versions of G++ as of this writing
15099 (so through at least 3.2.1) incorrectly generate
15100 DW_TAG_variable tags. */
15101
15102 const char *physname;
15103
15104 /* Get name of field. */
15105 fieldname = dwarf2_name (die, cu);
15106 if (fieldname == NULL)
15107 return;
15108
15109 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15110 if (attr
15111 /* Only create a symbol if this is an external value.
15112 new_symbol checks this and puts the value in the global symbol
15113 table, which we want. If it is not external, new_symbol
15114 will try to put the value in cu->list_in_scope which is wrong. */
15115 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15116 {
15117 /* A static const member, not much different than an enum as far as
15118 we're concerned, except that we can support more types. */
15119 new_symbol (die, NULL, cu);
15120 }
15121
15122 /* Get physical name. */
15123 physname = dwarf2_physname (fieldname, die, cu);
15124
15125 /* The name is already allocated along with this objfile, so we don't
15126 need to duplicate it for the type. */
15127 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15128 FIELD_TYPE (*fp) = die_type (die, cu);
15129 FIELD_NAME (*fp) = fieldname;
15130 }
15131 else if (die->tag == DW_TAG_inheritance)
15132 {
15133 LONGEST offset;
15134
15135 /* C++ base class field. */
15136 if (handle_data_member_location (die, cu, &offset))
15137 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15138 FIELD_BITSIZE (*fp) = 0;
15139 FIELD_TYPE (*fp) = die_type (die, cu);
15140 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
15141 }
15142 else if (die->tag == DW_TAG_variant_part)
15143 {
15144 /* process_structure_scope will treat this DIE as a union. */
15145 process_structure_scope (die, cu);
15146
15147 /* The variant part is relative to the start of the enclosing
15148 structure. */
15149 SET_FIELD_BITPOS (*fp, 0);
15150 fp->type = get_die_type (die, cu);
15151 fp->artificial = 1;
15152 fp->name = "<<variant>>";
15153
15154 /* Normally a DW_TAG_variant_part won't have a size, but our
15155 representation requires one, so set it to the maximum of the
15156 child sizes. */
15157 if (TYPE_LENGTH (fp->type) == 0)
15158 {
15159 unsigned max = 0;
15160 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15161 if (TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)) > max)
15162 max = TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i));
15163 TYPE_LENGTH (fp->type) = max;
15164 }
15165 }
15166 else
15167 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15168 }
15169
15170 /* Can the type given by DIE define another type? */
15171
15172 static bool
15173 type_can_define_types (const struct die_info *die)
15174 {
15175 switch (die->tag)
15176 {
15177 case DW_TAG_typedef:
15178 case DW_TAG_class_type:
15179 case DW_TAG_structure_type:
15180 case DW_TAG_union_type:
15181 case DW_TAG_enumeration_type:
15182 return true;
15183
15184 default:
15185 return false;
15186 }
15187 }
15188
15189 /* Add a type definition defined in the scope of the FIP's class. */
15190
15191 static void
15192 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15193 struct dwarf2_cu *cu)
15194 {
15195 struct decl_field fp;
15196 memset (&fp, 0, sizeof (fp));
15197
15198 gdb_assert (type_can_define_types (die));
15199
15200 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15201 fp.name = dwarf2_name (die, cu);
15202 fp.type = read_type_die (die, cu);
15203
15204 /* Save accessibility. */
15205 enum dwarf_access_attribute accessibility;
15206 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15207 if (attr != NULL)
15208 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15209 else
15210 accessibility = dwarf2_default_access_attribute (die, cu);
15211 switch (accessibility)
15212 {
15213 case DW_ACCESS_public:
15214 /* The assumed value if neither private nor protected. */
15215 break;
15216 case DW_ACCESS_private:
15217 fp.is_private = 1;
15218 break;
15219 case DW_ACCESS_protected:
15220 fp.is_protected = 1;
15221 break;
15222 default:
15223 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15224 }
15225
15226 if (die->tag == DW_TAG_typedef)
15227 fip->typedef_field_list.push_back (fp);
15228 else
15229 fip->nested_types_list.push_back (fp);
15230 }
15231
15232 /* Create the vector of fields, and attach it to the type. */
15233
15234 static void
15235 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15236 struct dwarf2_cu *cu)
15237 {
15238 int nfields = fip->nfields;
15239
15240 /* Record the field count, allocate space for the array of fields,
15241 and create blank accessibility bitfields if necessary. */
15242 TYPE_NFIELDS (type) = nfields;
15243 TYPE_FIELDS (type) = (struct field *)
15244 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15245
15246 if (fip->non_public_fields && cu->language != language_ada)
15247 {
15248 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15249
15250 TYPE_FIELD_PRIVATE_BITS (type) =
15251 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15252 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15253
15254 TYPE_FIELD_PROTECTED_BITS (type) =
15255 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15256 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15257
15258 TYPE_FIELD_IGNORE_BITS (type) =
15259 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15260 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15261 }
15262
15263 /* If the type has baseclasses, allocate and clear a bit vector for
15264 TYPE_FIELD_VIRTUAL_BITS. */
15265 if (!fip->baseclasses.empty () && cu->language != language_ada)
15266 {
15267 int num_bytes = B_BYTES (fip->baseclasses.size ());
15268 unsigned char *pointer;
15269
15270 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15271 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15272 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15273 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15274 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15275 }
15276
15277 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15278 {
15279 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15280
15281 for (int index = 0; index < nfields; ++index)
15282 {
15283 struct nextfield &field = fip->fields[index];
15284
15285 if (field.variant.is_discriminant)
15286 di->discriminant_index = index;
15287 else if (field.variant.default_branch)
15288 di->default_index = index;
15289 else
15290 di->discriminants[index] = field.variant.discriminant_value;
15291 }
15292 }
15293
15294 /* Copy the saved-up fields into the field vector. */
15295 for (int i = 0; i < nfields; ++i)
15296 {
15297 struct nextfield &field
15298 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15299 : fip->fields[i - fip->baseclasses.size ()]);
15300
15301 TYPE_FIELD (type, i) = field.field;
15302 switch (field.accessibility)
15303 {
15304 case DW_ACCESS_private:
15305 if (cu->language != language_ada)
15306 SET_TYPE_FIELD_PRIVATE (type, i);
15307 break;
15308
15309 case DW_ACCESS_protected:
15310 if (cu->language != language_ada)
15311 SET_TYPE_FIELD_PROTECTED (type, i);
15312 break;
15313
15314 case DW_ACCESS_public:
15315 break;
15316
15317 default:
15318 /* Unknown accessibility. Complain and treat it as public. */
15319 {
15320 complaint (_("unsupported accessibility %d"),
15321 field.accessibility);
15322 }
15323 break;
15324 }
15325 if (i < fip->baseclasses.size ())
15326 {
15327 switch (field.virtuality)
15328 {
15329 case DW_VIRTUALITY_virtual:
15330 case DW_VIRTUALITY_pure_virtual:
15331 if (cu->language == language_ada)
15332 error (_("unexpected virtuality in component of Ada type"));
15333 SET_TYPE_FIELD_VIRTUAL (type, i);
15334 break;
15335 }
15336 }
15337 }
15338 }
15339
15340 /* Return true if this member function is a constructor, false
15341 otherwise. */
15342
15343 static int
15344 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15345 {
15346 const char *fieldname;
15347 const char *type_name;
15348 int len;
15349
15350 if (die->parent == NULL)
15351 return 0;
15352
15353 if (die->parent->tag != DW_TAG_structure_type
15354 && die->parent->tag != DW_TAG_union_type
15355 && die->parent->tag != DW_TAG_class_type)
15356 return 0;
15357
15358 fieldname = dwarf2_name (die, cu);
15359 type_name = dwarf2_name (die->parent, cu);
15360 if (fieldname == NULL || type_name == NULL)
15361 return 0;
15362
15363 len = strlen (fieldname);
15364 return (strncmp (fieldname, type_name, len) == 0
15365 && (type_name[len] == '\0' || type_name[len] == '<'));
15366 }
15367
15368 /* Add a member function to the proper fieldlist. */
15369
15370 static void
15371 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15372 struct type *type, struct dwarf2_cu *cu)
15373 {
15374 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15375 struct attribute *attr;
15376 int i;
15377 struct fnfieldlist *flp = nullptr;
15378 struct fn_field *fnp;
15379 const char *fieldname;
15380 struct type *this_type;
15381 enum dwarf_access_attribute accessibility;
15382
15383 if (cu->language == language_ada)
15384 error (_("unexpected member function in Ada type"));
15385
15386 /* Get name of member function. */
15387 fieldname = dwarf2_name (die, cu);
15388 if (fieldname == NULL)
15389 return;
15390
15391 /* Look up member function name in fieldlist. */
15392 for (i = 0; i < fip->fnfieldlists.size (); i++)
15393 {
15394 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15395 {
15396 flp = &fip->fnfieldlists[i];
15397 break;
15398 }
15399 }
15400
15401 /* Create a new fnfieldlist if necessary. */
15402 if (flp == nullptr)
15403 {
15404 fip->fnfieldlists.emplace_back ();
15405 flp = &fip->fnfieldlists.back ();
15406 flp->name = fieldname;
15407 i = fip->fnfieldlists.size () - 1;
15408 }
15409
15410 /* Create a new member function field and add it to the vector of
15411 fnfieldlists. */
15412 flp->fnfields.emplace_back ();
15413 fnp = &flp->fnfields.back ();
15414
15415 /* Delay processing of the physname until later. */
15416 if (cu->language == language_cplus)
15417 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15418 die, cu);
15419 else
15420 {
15421 const char *physname = dwarf2_physname (fieldname, die, cu);
15422 fnp->physname = physname ? physname : "";
15423 }
15424
15425 fnp->type = alloc_type (objfile);
15426 this_type = read_type_die (die, cu);
15427 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15428 {
15429 int nparams = TYPE_NFIELDS (this_type);
15430
15431 /* TYPE is the domain of this method, and THIS_TYPE is the type
15432 of the method itself (TYPE_CODE_METHOD). */
15433 smash_to_method_type (fnp->type, type,
15434 TYPE_TARGET_TYPE (this_type),
15435 TYPE_FIELDS (this_type),
15436 TYPE_NFIELDS (this_type),
15437 TYPE_VARARGS (this_type));
15438
15439 /* Handle static member functions.
15440 Dwarf2 has no clean way to discern C++ static and non-static
15441 member functions. G++ helps GDB by marking the first
15442 parameter for non-static member functions (which is the this
15443 pointer) as artificial. We obtain this information from
15444 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15445 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15446 fnp->voffset = VOFFSET_STATIC;
15447 }
15448 else
15449 complaint (_("member function type missing for '%s'"),
15450 dwarf2_full_name (fieldname, die, cu));
15451
15452 /* Get fcontext from DW_AT_containing_type if present. */
15453 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15454 fnp->fcontext = die_containing_type (die, cu);
15455
15456 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15457 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15458
15459 /* Get accessibility. */
15460 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15461 if (attr)
15462 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15463 else
15464 accessibility = dwarf2_default_access_attribute (die, cu);
15465 switch (accessibility)
15466 {
15467 case DW_ACCESS_private:
15468 fnp->is_private = 1;
15469 break;
15470 case DW_ACCESS_protected:
15471 fnp->is_protected = 1;
15472 break;
15473 }
15474
15475 /* Check for artificial methods. */
15476 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15477 if (attr && DW_UNSND (attr) != 0)
15478 fnp->is_artificial = 1;
15479
15480 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15481
15482 /* Get index in virtual function table if it is a virtual member
15483 function. For older versions of GCC, this is an offset in the
15484 appropriate virtual table, as specified by DW_AT_containing_type.
15485 For everyone else, it is an expression to be evaluated relative
15486 to the object address. */
15487
15488 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15489 if (attr)
15490 {
15491 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15492 {
15493 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15494 {
15495 /* Old-style GCC. */
15496 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15497 }
15498 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15499 || (DW_BLOCK (attr)->size > 1
15500 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15501 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15502 {
15503 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15504 if ((fnp->voffset % cu->header.addr_size) != 0)
15505 dwarf2_complex_location_expr_complaint ();
15506 else
15507 fnp->voffset /= cu->header.addr_size;
15508 fnp->voffset += 2;
15509 }
15510 else
15511 dwarf2_complex_location_expr_complaint ();
15512
15513 if (!fnp->fcontext)
15514 {
15515 /* If there is no `this' field and no DW_AT_containing_type,
15516 we cannot actually find a base class context for the
15517 vtable! */
15518 if (TYPE_NFIELDS (this_type) == 0
15519 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15520 {
15521 complaint (_("cannot determine context for virtual member "
15522 "function \"%s\" (offset %s)"),
15523 fieldname, sect_offset_str (die->sect_off));
15524 }
15525 else
15526 {
15527 fnp->fcontext
15528 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15529 }
15530 }
15531 }
15532 else if (attr_form_is_section_offset (attr))
15533 {
15534 dwarf2_complex_location_expr_complaint ();
15535 }
15536 else
15537 {
15538 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15539 fieldname);
15540 }
15541 }
15542 else
15543 {
15544 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15545 if (attr && DW_UNSND (attr))
15546 {
15547 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15548 complaint (_("Member function \"%s\" (offset %s) is virtual "
15549 "but the vtable offset is not specified"),
15550 fieldname, sect_offset_str (die->sect_off));
15551 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15552 TYPE_CPLUS_DYNAMIC (type) = 1;
15553 }
15554 }
15555 }
15556
15557 /* Create the vector of member function fields, and attach it to the type. */
15558
15559 static void
15560 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15561 struct dwarf2_cu *cu)
15562 {
15563 if (cu->language == language_ada)
15564 error (_("unexpected member functions in Ada type"));
15565
15566 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15567 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15568 TYPE_ALLOC (type,
15569 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15570
15571 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15572 {
15573 struct fnfieldlist &nf = fip->fnfieldlists[i];
15574 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15575
15576 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15577 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15578 fn_flp->fn_fields = (struct fn_field *)
15579 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15580
15581 for (int k = 0; k < nf.fnfields.size (); ++k)
15582 fn_flp->fn_fields[k] = nf.fnfields[k];
15583 }
15584
15585 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15586 }
15587
15588 /* Returns non-zero if NAME is the name of a vtable member in CU's
15589 language, zero otherwise. */
15590 static int
15591 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15592 {
15593 static const char vptr[] = "_vptr";
15594
15595 /* Look for the C++ form of the vtable. */
15596 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15597 return 1;
15598
15599 return 0;
15600 }
15601
15602 /* GCC outputs unnamed structures that are really pointers to member
15603 functions, with the ABI-specified layout. If TYPE describes
15604 such a structure, smash it into a member function type.
15605
15606 GCC shouldn't do this; it should just output pointer to member DIEs.
15607 This is GCC PR debug/28767. */
15608
15609 static void
15610 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15611 {
15612 struct type *pfn_type, *self_type, *new_type;
15613
15614 /* Check for a structure with no name and two children. */
15615 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15616 return;
15617
15618 /* Check for __pfn and __delta members. */
15619 if (TYPE_FIELD_NAME (type, 0) == NULL
15620 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15621 || TYPE_FIELD_NAME (type, 1) == NULL
15622 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15623 return;
15624
15625 /* Find the type of the method. */
15626 pfn_type = TYPE_FIELD_TYPE (type, 0);
15627 if (pfn_type == NULL
15628 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15629 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15630 return;
15631
15632 /* Look for the "this" argument. */
15633 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15634 if (TYPE_NFIELDS (pfn_type) == 0
15635 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15636 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15637 return;
15638
15639 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15640 new_type = alloc_type (objfile);
15641 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15642 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15643 TYPE_VARARGS (pfn_type));
15644 smash_to_methodptr_type (type, new_type);
15645 }
15646
15647 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15648 appropriate error checking and issuing complaints if there is a
15649 problem. */
15650
15651 static ULONGEST
15652 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15653 {
15654 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15655
15656 if (attr == nullptr)
15657 return 0;
15658
15659 if (!attr_form_is_constant (attr))
15660 {
15661 complaint (_("DW_AT_alignment must have constant form"
15662 " - DIE at %s [in module %s]"),
15663 sect_offset_str (die->sect_off),
15664 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15665 return 0;
15666 }
15667
15668 ULONGEST align;
15669 if (attr->form == DW_FORM_sdata)
15670 {
15671 LONGEST val = DW_SND (attr);
15672 if (val < 0)
15673 {
15674 complaint (_("DW_AT_alignment value must not be negative"
15675 " - DIE at %s [in module %s]"),
15676 sect_offset_str (die->sect_off),
15677 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15678 return 0;
15679 }
15680 align = val;
15681 }
15682 else
15683 align = DW_UNSND (attr);
15684
15685 if (align == 0)
15686 {
15687 complaint (_("DW_AT_alignment value must not be zero"
15688 " - DIE at %s [in module %s]"),
15689 sect_offset_str (die->sect_off),
15690 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15691 return 0;
15692 }
15693 if ((align & (align - 1)) != 0)
15694 {
15695 complaint (_("DW_AT_alignment value must be a power of 2"
15696 " - DIE at %s [in module %s]"),
15697 sect_offset_str (die->sect_off),
15698 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15699 return 0;
15700 }
15701
15702 return align;
15703 }
15704
15705 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15706 the alignment for TYPE. */
15707
15708 static void
15709 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15710 struct type *type)
15711 {
15712 if (!set_type_align (type, get_alignment (cu, die)))
15713 complaint (_("DW_AT_alignment value too large"
15714 " - DIE at %s [in module %s]"),
15715 sect_offset_str (die->sect_off),
15716 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15717 }
15718
15719 /* Called when we find the DIE that starts a structure or union scope
15720 (definition) to create a type for the structure or union. Fill in
15721 the type's name and general properties; the members will not be
15722 processed until process_structure_scope. A symbol table entry for
15723 the type will also not be done until process_structure_scope (assuming
15724 the type has a name).
15725
15726 NOTE: we need to call these functions regardless of whether or not the
15727 DIE has a DW_AT_name attribute, since it might be an anonymous
15728 structure or union. This gets the type entered into our set of
15729 user defined types. */
15730
15731 static struct type *
15732 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15733 {
15734 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15735 struct type *type;
15736 struct attribute *attr;
15737 const char *name;
15738
15739 /* If the definition of this type lives in .debug_types, read that type.
15740 Don't follow DW_AT_specification though, that will take us back up
15741 the chain and we want to go down. */
15742 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15743 if (attr)
15744 {
15745 type = get_DW_AT_signature_type (die, attr, cu);
15746
15747 /* The type's CU may not be the same as CU.
15748 Ensure TYPE is recorded with CU in die_type_hash. */
15749 return set_die_type (die, type, cu);
15750 }
15751
15752 type = alloc_type (objfile);
15753 INIT_CPLUS_SPECIFIC (type);
15754
15755 name = dwarf2_name (die, cu);
15756 if (name != NULL)
15757 {
15758 if (cu->language == language_cplus
15759 || cu->language == language_d
15760 || cu->language == language_rust)
15761 {
15762 const char *full_name = dwarf2_full_name (name, die, cu);
15763
15764 /* dwarf2_full_name might have already finished building the DIE's
15765 type. If so, there is no need to continue. */
15766 if (get_die_type (die, cu) != NULL)
15767 return get_die_type (die, cu);
15768
15769 TYPE_NAME (type) = full_name;
15770 }
15771 else
15772 {
15773 /* The name is already allocated along with this objfile, so
15774 we don't need to duplicate it for the type. */
15775 TYPE_NAME (type) = name;
15776 }
15777 }
15778
15779 if (die->tag == DW_TAG_structure_type)
15780 {
15781 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15782 }
15783 else if (die->tag == DW_TAG_union_type)
15784 {
15785 TYPE_CODE (type) = TYPE_CODE_UNION;
15786 }
15787 else if (die->tag == DW_TAG_variant_part)
15788 {
15789 TYPE_CODE (type) = TYPE_CODE_UNION;
15790 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15791 }
15792 else
15793 {
15794 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15795 }
15796
15797 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15798 TYPE_DECLARED_CLASS (type) = 1;
15799
15800 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15801 if (attr)
15802 {
15803 if (attr_form_is_constant (attr))
15804 TYPE_LENGTH (type) = DW_UNSND (attr);
15805 else
15806 {
15807 /* For the moment, dynamic type sizes are not supported
15808 by GDB's struct type. The actual size is determined
15809 on-demand when resolving the type of a given object,
15810 so set the type's length to zero for now. Otherwise,
15811 we record an expression as the length, and that expression
15812 could lead to a very large value, which could eventually
15813 lead to us trying to allocate that much memory when creating
15814 a value of that type. */
15815 TYPE_LENGTH (type) = 0;
15816 }
15817 }
15818 else
15819 {
15820 TYPE_LENGTH (type) = 0;
15821 }
15822
15823 maybe_set_alignment (cu, die, type);
15824
15825 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15826 {
15827 /* ICC<14 does not output the required DW_AT_declaration on
15828 incomplete types, but gives them a size of zero. */
15829 TYPE_STUB (type) = 1;
15830 }
15831 else
15832 TYPE_STUB_SUPPORTED (type) = 1;
15833
15834 if (die_is_declaration (die, cu))
15835 TYPE_STUB (type) = 1;
15836 else if (attr == NULL && die->child == NULL
15837 && producer_is_realview (cu->producer))
15838 /* RealView does not output the required DW_AT_declaration
15839 on incomplete types. */
15840 TYPE_STUB (type) = 1;
15841
15842 /* We need to add the type field to the die immediately so we don't
15843 infinitely recurse when dealing with pointers to the structure
15844 type within the structure itself. */
15845 set_die_type (die, type, cu);
15846
15847 /* set_die_type should be already done. */
15848 set_descriptive_type (type, die, cu);
15849
15850 return type;
15851 }
15852
15853 /* A helper for process_structure_scope that handles a single member
15854 DIE. */
15855
15856 static void
15857 handle_struct_member_die (struct die_info *child_die, struct type *type,
15858 struct field_info *fi,
15859 std::vector<struct symbol *> *template_args,
15860 struct dwarf2_cu *cu)
15861 {
15862 if (child_die->tag == DW_TAG_member
15863 || child_die->tag == DW_TAG_variable
15864 || child_die->tag == DW_TAG_variant_part)
15865 {
15866 /* NOTE: carlton/2002-11-05: A C++ static data member
15867 should be a DW_TAG_member that is a declaration, but
15868 all versions of G++ as of this writing (so through at
15869 least 3.2.1) incorrectly generate DW_TAG_variable
15870 tags for them instead. */
15871 dwarf2_add_field (fi, child_die, cu);
15872 }
15873 else if (child_die->tag == DW_TAG_subprogram)
15874 {
15875 /* Rust doesn't have member functions in the C++ sense.
15876 However, it does emit ordinary functions as children
15877 of a struct DIE. */
15878 if (cu->language == language_rust)
15879 read_func_scope (child_die, cu);
15880 else
15881 {
15882 /* C++ member function. */
15883 dwarf2_add_member_fn (fi, child_die, type, cu);
15884 }
15885 }
15886 else if (child_die->tag == DW_TAG_inheritance)
15887 {
15888 /* C++ base class field. */
15889 dwarf2_add_field (fi, child_die, cu);
15890 }
15891 else if (type_can_define_types (child_die))
15892 dwarf2_add_type_defn (fi, child_die, cu);
15893 else if (child_die->tag == DW_TAG_template_type_param
15894 || child_die->tag == DW_TAG_template_value_param)
15895 {
15896 struct symbol *arg = new_symbol (child_die, NULL, cu);
15897
15898 if (arg != NULL)
15899 template_args->push_back (arg);
15900 }
15901 else if (child_die->tag == DW_TAG_variant)
15902 {
15903 /* In a variant we want to get the discriminant and also add a
15904 field for our sole member child. */
15905 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15906
15907 for (die_info *variant_child = child_die->child;
15908 variant_child != NULL;
15909 variant_child = sibling_die (variant_child))
15910 {
15911 if (variant_child->tag == DW_TAG_member)
15912 {
15913 handle_struct_member_die (variant_child, type, fi,
15914 template_args, cu);
15915 /* Only handle the one. */
15916 break;
15917 }
15918 }
15919
15920 /* We don't handle this but we might as well report it if we see
15921 it. */
15922 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15923 complaint (_("DW_AT_discr_list is not supported yet"
15924 " - DIE at %s [in module %s]"),
15925 sect_offset_str (child_die->sect_off),
15926 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15927
15928 /* The first field was just added, so we can stash the
15929 discriminant there. */
15930 gdb_assert (!fi->fields.empty ());
15931 if (discr == NULL)
15932 fi->fields.back ().variant.default_branch = true;
15933 else
15934 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15935 }
15936 }
15937
15938 /* Finish creating a structure or union type, including filling in
15939 its members and creating a symbol for it. */
15940
15941 static void
15942 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15943 {
15944 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15945 struct die_info *child_die;
15946 struct type *type;
15947
15948 type = get_die_type (die, cu);
15949 if (type == NULL)
15950 type = read_structure_type (die, cu);
15951
15952 /* When reading a DW_TAG_variant_part, we need to notice when we
15953 read the discriminant member, so we can record it later in the
15954 discriminant_info. */
15955 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15956 sect_offset discr_offset;
15957 bool has_template_parameters = false;
15958
15959 if (is_variant_part)
15960 {
15961 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15962 if (discr == NULL)
15963 {
15964 /* Maybe it's a univariant form, an extension we support.
15965 In this case arrange not to check the offset. */
15966 is_variant_part = false;
15967 }
15968 else if (attr_form_is_ref (discr))
15969 {
15970 struct dwarf2_cu *target_cu = cu;
15971 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15972
15973 discr_offset = target_die->sect_off;
15974 }
15975 else
15976 {
15977 complaint (_("DW_AT_discr does not have DIE reference form"
15978 " - DIE at %s [in module %s]"),
15979 sect_offset_str (die->sect_off),
15980 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15981 is_variant_part = false;
15982 }
15983 }
15984
15985 if (die->child != NULL && ! die_is_declaration (die, cu))
15986 {
15987 struct field_info fi;
15988 std::vector<struct symbol *> template_args;
15989
15990 child_die = die->child;
15991
15992 while (child_die && child_die->tag)
15993 {
15994 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15995
15996 if (is_variant_part && discr_offset == child_die->sect_off)
15997 fi.fields.back ().variant.is_discriminant = true;
15998
15999 child_die = sibling_die (child_die);
16000 }
16001
16002 /* Attach template arguments to type. */
16003 if (!template_args.empty ())
16004 {
16005 has_template_parameters = true;
16006 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16007 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16008 TYPE_TEMPLATE_ARGUMENTS (type)
16009 = XOBNEWVEC (&objfile->objfile_obstack,
16010 struct symbol *,
16011 TYPE_N_TEMPLATE_ARGUMENTS (type));
16012 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16013 template_args.data (),
16014 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16015 * sizeof (struct symbol *)));
16016 }
16017
16018 /* Attach fields and member functions to the type. */
16019 if (fi.nfields)
16020 dwarf2_attach_fields_to_type (&fi, type, cu);
16021 if (!fi.fnfieldlists.empty ())
16022 {
16023 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16024
16025 /* Get the type which refers to the base class (possibly this
16026 class itself) which contains the vtable pointer for the current
16027 class from the DW_AT_containing_type attribute. This use of
16028 DW_AT_containing_type is a GNU extension. */
16029
16030 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16031 {
16032 struct type *t = die_containing_type (die, cu);
16033
16034 set_type_vptr_basetype (type, t);
16035 if (type == t)
16036 {
16037 int i;
16038
16039 /* Our own class provides vtbl ptr. */
16040 for (i = TYPE_NFIELDS (t) - 1;
16041 i >= TYPE_N_BASECLASSES (t);
16042 --i)
16043 {
16044 const char *fieldname = TYPE_FIELD_NAME (t, i);
16045
16046 if (is_vtable_name (fieldname, cu))
16047 {
16048 set_type_vptr_fieldno (type, i);
16049 break;
16050 }
16051 }
16052
16053 /* Complain if virtual function table field not found. */
16054 if (i < TYPE_N_BASECLASSES (t))
16055 complaint (_("virtual function table pointer "
16056 "not found when defining class '%s'"),
16057 TYPE_NAME (type) ? TYPE_NAME (type) : "");
16058 }
16059 else
16060 {
16061 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16062 }
16063 }
16064 else if (cu->producer
16065 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16066 {
16067 /* The IBM XLC compiler does not provide direct indication
16068 of the containing type, but the vtable pointer is
16069 always named __vfp. */
16070
16071 int i;
16072
16073 for (i = TYPE_NFIELDS (type) - 1;
16074 i >= TYPE_N_BASECLASSES (type);
16075 --i)
16076 {
16077 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16078 {
16079 set_type_vptr_fieldno (type, i);
16080 set_type_vptr_basetype (type, type);
16081 break;
16082 }
16083 }
16084 }
16085 }
16086
16087 /* Copy fi.typedef_field_list linked list elements content into the
16088 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16089 if (!fi.typedef_field_list.empty ())
16090 {
16091 int count = fi.typedef_field_list.size ();
16092
16093 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16094 TYPE_TYPEDEF_FIELD_ARRAY (type)
16095 = ((struct decl_field *)
16096 TYPE_ALLOC (type,
16097 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16098 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16099
16100 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16101 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16102 }
16103
16104 /* Copy fi.nested_types_list linked list elements content into the
16105 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16106 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16107 {
16108 int count = fi.nested_types_list.size ();
16109
16110 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16111 TYPE_NESTED_TYPES_ARRAY (type)
16112 = ((struct decl_field *)
16113 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16114 TYPE_NESTED_TYPES_COUNT (type) = count;
16115
16116 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16117 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16118 }
16119 }
16120
16121 quirk_gcc_member_function_pointer (type, objfile);
16122 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16123 cu->rust_unions.push_back (type);
16124
16125 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16126 snapshots) has been known to create a die giving a declaration
16127 for a class that has, as a child, a die giving a definition for a
16128 nested class. So we have to process our children even if the
16129 current die is a declaration. Normally, of course, a declaration
16130 won't have any children at all. */
16131
16132 child_die = die->child;
16133
16134 while (child_die != NULL && child_die->tag)
16135 {
16136 if (child_die->tag == DW_TAG_member
16137 || child_die->tag == DW_TAG_variable
16138 || child_die->tag == DW_TAG_inheritance
16139 || child_die->tag == DW_TAG_template_value_param
16140 || child_die->tag == DW_TAG_template_type_param)
16141 {
16142 /* Do nothing. */
16143 }
16144 else
16145 process_die (child_die, cu);
16146
16147 child_die = sibling_die (child_die);
16148 }
16149
16150 /* Do not consider external references. According to the DWARF standard,
16151 these DIEs are identified by the fact that they have no byte_size
16152 attribute, and a declaration attribute. */
16153 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16154 || !die_is_declaration (die, cu))
16155 {
16156 struct symbol *sym = new_symbol (die, type, cu);
16157
16158 if (has_template_parameters)
16159 {
16160 struct symtab *symtab;
16161 if (sym != nullptr)
16162 symtab = symbol_symtab (sym);
16163 else if (cu->line_header != nullptr)
16164 {
16165 /* Any related symtab will do. */
16166 symtab
16167 = cu->line_header->file_name_at (file_name_index (1))->symtab;
16168 }
16169 else
16170 {
16171 symtab = nullptr;
16172 complaint (_("could not find suitable "
16173 "symtab for template parameter"
16174 " - DIE at %s [in module %s]"),
16175 sect_offset_str (die->sect_off),
16176 objfile_name (objfile));
16177 }
16178
16179 if (symtab != nullptr)
16180 {
16181 /* Make sure that the symtab is set on the new symbols.
16182 Even though they don't appear in this symtab directly,
16183 other parts of gdb assume that symbols do, and this is
16184 reasonably true. */
16185 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16186 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16187 }
16188 }
16189 }
16190 }
16191
16192 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16193 update TYPE using some information only available in DIE's children. */
16194
16195 static void
16196 update_enumeration_type_from_children (struct die_info *die,
16197 struct type *type,
16198 struct dwarf2_cu *cu)
16199 {
16200 struct die_info *child_die;
16201 int unsigned_enum = 1;
16202 int flag_enum = 1;
16203 ULONGEST mask = 0;
16204
16205 auto_obstack obstack;
16206
16207 for (child_die = die->child;
16208 child_die != NULL && child_die->tag;
16209 child_die = sibling_die (child_die))
16210 {
16211 struct attribute *attr;
16212 LONGEST value;
16213 const gdb_byte *bytes;
16214 struct dwarf2_locexpr_baton *baton;
16215 const char *name;
16216
16217 if (child_die->tag != DW_TAG_enumerator)
16218 continue;
16219
16220 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16221 if (attr == NULL)
16222 continue;
16223
16224 name = dwarf2_name (child_die, cu);
16225 if (name == NULL)
16226 name = "<anonymous enumerator>";
16227
16228 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16229 &value, &bytes, &baton);
16230 if (value < 0)
16231 {
16232 unsigned_enum = 0;
16233 flag_enum = 0;
16234 }
16235 else if ((mask & value) != 0)
16236 flag_enum = 0;
16237 else
16238 mask |= value;
16239
16240 /* If we already know that the enum type is neither unsigned, nor
16241 a flag type, no need to look at the rest of the enumerates. */
16242 if (!unsigned_enum && !flag_enum)
16243 break;
16244 }
16245
16246 if (unsigned_enum)
16247 TYPE_UNSIGNED (type) = 1;
16248 if (flag_enum)
16249 TYPE_FLAG_ENUM (type) = 1;
16250 }
16251
16252 /* Given a DW_AT_enumeration_type die, set its type. We do not
16253 complete the type's fields yet, or create any symbols. */
16254
16255 static struct type *
16256 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16257 {
16258 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16259 struct type *type;
16260 struct attribute *attr;
16261 const char *name;
16262
16263 /* If the definition of this type lives in .debug_types, read that type.
16264 Don't follow DW_AT_specification though, that will take us back up
16265 the chain and we want to go down. */
16266 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16267 if (attr)
16268 {
16269 type = get_DW_AT_signature_type (die, attr, cu);
16270
16271 /* The type's CU may not be the same as CU.
16272 Ensure TYPE is recorded with CU in die_type_hash. */
16273 return set_die_type (die, type, cu);
16274 }
16275
16276 type = alloc_type (objfile);
16277
16278 TYPE_CODE (type) = TYPE_CODE_ENUM;
16279 name = dwarf2_full_name (NULL, die, cu);
16280 if (name != NULL)
16281 TYPE_NAME (type) = name;
16282
16283 attr = dwarf2_attr (die, DW_AT_type, cu);
16284 if (attr != NULL)
16285 {
16286 struct type *underlying_type = die_type (die, cu);
16287
16288 TYPE_TARGET_TYPE (type) = underlying_type;
16289 }
16290
16291 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16292 if (attr)
16293 {
16294 TYPE_LENGTH (type) = DW_UNSND (attr);
16295 }
16296 else
16297 {
16298 TYPE_LENGTH (type) = 0;
16299 }
16300
16301 maybe_set_alignment (cu, die, type);
16302
16303 /* The enumeration DIE can be incomplete. In Ada, any type can be
16304 declared as private in the package spec, and then defined only
16305 inside the package body. Such types are known as Taft Amendment
16306 Types. When another package uses such a type, an incomplete DIE
16307 may be generated by the compiler. */
16308 if (die_is_declaration (die, cu))
16309 TYPE_STUB (type) = 1;
16310
16311 /* Finish the creation of this type by using the enum's children.
16312 We must call this even when the underlying type has been provided
16313 so that we can determine if we're looking at a "flag" enum. */
16314 update_enumeration_type_from_children (die, type, cu);
16315
16316 /* If this type has an underlying type that is not a stub, then we
16317 may use its attributes. We always use the "unsigned" attribute
16318 in this situation, because ordinarily we guess whether the type
16319 is unsigned -- but the guess can be wrong and the underlying type
16320 can tell us the reality. However, we defer to a local size
16321 attribute if one exists, because this lets the compiler override
16322 the underlying type if needed. */
16323 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16324 {
16325 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16326 if (TYPE_LENGTH (type) == 0)
16327 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16328 if (TYPE_RAW_ALIGN (type) == 0
16329 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16330 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16331 }
16332
16333 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16334
16335 return set_die_type (die, type, cu);
16336 }
16337
16338 /* Given a pointer to a die which begins an enumeration, process all
16339 the dies that define the members of the enumeration, and create the
16340 symbol for the enumeration type.
16341
16342 NOTE: We reverse the order of the element list. */
16343
16344 static void
16345 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16346 {
16347 struct type *this_type;
16348
16349 this_type = get_die_type (die, cu);
16350 if (this_type == NULL)
16351 this_type = read_enumeration_type (die, cu);
16352
16353 if (die->child != NULL)
16354 {
16355 struct die_info *child_die;
16356 struct symbol *sym;
16357 struct field *fields = NULL;
16358 int num_fields = 0;
16359 const char *name;
16360
16361 child_die = die->child;
16362 while (child_die && child_die->tag)
16363 {
16364 if (child_die->tag != DW_TAG_enumerator)
16365 {
16366 process_die (child_die, cu);
16367 }
16368 else
16369 {
16370 name = dwarf2_name (child_die, cu);
16371 if (name)
16372 {
16373 sym = new_symbol (child_die, this_type, cu);
16374
16375 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16376 {
16377 fields = (struct field *)
16378 xrealloc (fields,
16379 (num_fields + DW_FIELD_ALLOC_CHUNK)
16380 * sizeof (struct field));
16381 }
16382
16383 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16384 FIELD_TYPE (fields[num_fields]) = NULL;
16385 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16386 FIELD_BITSIZE (fields[num_fields]) = 0;
16387
16388 num_fields++;
16389 }
16390 }
16391
16392 child_die = sibling_die (child_die);
16393 }
16394
16395 if (num_fields)
16396 {
16397 TYPE_NFIELDS (this_type) = num_fields;
16398 TYPE_FIELDS (this_type) = (struct field *)
16399 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16400 memcpy (TYPE_FIELDS (this_type), fields,
16401 sizeof (struct field) * num_fields);
16402 xfree (fields);
16403 }
16404 }
16405
16406 /* If we are reading an enum from a .debug_types unit, and the enum
16407 is a declaration, and the enum is not the signatured type in the
16408 unit, then we do not want to add a symbol for it. Adding a
16409 symbol would in some cases obscure the true definition of the
16410 enum, giving users an incomplete type when the definition is
16411 actually available. Note that we do not want to do this for all
16412 enums which are just declarations, because C++0x allows forward
16413 enum declarations. */
16414 if (cu->per_cu->is_debug_types
16415 && die_is_declaration (die, cu))
16416 {
16417 struct signatured_type *sig_type;
16418
16419 sig_type = (struct signatured_type *) cu->per_cu;
16420 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16421 if (sig_type->type_offset_in_section != die->sect_off)
16422 return;
16423 }
16424
16425 new_symbol (die, this_type, cu);
16426 }
16427
16428 /* Extract all information from a DW_TAG_array_type DIE and put it in
16429 the DIE's type field. For now, this only handles one dimensional
16430 arrays. */
16431
16432 static struct type *
16433 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16434 {
16435 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16436 struct die_info *child_die;
16437 struct type *type;
16438 struct type *element_type, *range_type, *index_type;
16439 struct attribute *attr;
16440 const char *name;
16441 struct dynamic_prop *byte_stride_prop = NULL;
16442 unsigned int bit_stride = 0;
16443
16444 element_type = die_type (die, cu);
16445
16446 /* The die_type call above may have already set the type for this DIE. */
16447 type = get_die_type (die, cu);
16448 if (type)
16449 return type;
16450
16451 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16452 if (attr != NULL)
16453 {
16454 int stride_ok;
16455 struct type *prop_type
16456 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
16457
16458 byte_stride_prop
16459 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16460 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16461 prop_type);
16462 if (!stride_ok)
16463 {
16464 complaint (_("unable to read array DW_AT_byte_stride "
16465 " - DIE at %s [in module %s]"),
16466 sect_offset_str (die->sect_off),
16467 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16468 /* Ignore this attribute. We will likely not be able to print
16469 arrays of this type correctly, but there is little we can do
16470 to help if we cannot read the attribute's value. */
16471 byte_stride_prop = NULL;
16472 }
16473 }
16474
16475 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16476 if (attr != NULL)
16477 bit_stride = DW_UNSND (attr);
16478
16479 /* Irix 6.2 native cc creates array types without children for
16480 arrays with unspecified length. */
16481 if (die->child == NULL)
16482 {
16483 index_type = objfile_type (objfile)->builtin_int;
16484 range_type = create_static_range_type (NULL, index_type, 0, -1);
16485 type = create_array_type_with_stride (NULL, element_type, range_type,
16486 byte_stride_prop, bit_stride);
16487 return set_die_type (die, type, cu);
16488 }
16489
16490 std::vector<struct type *> range_types;
16491 child_die = die->child;
16492 while (child_die && child_die->tag)
16493 {
16494 if (child_die->tag == DW_TAG_subrange_type)
16495 {
16496 struct type *child_type = read_type_die (child_die, cu);
16497
16498 if (child_type != NULL)
16499 {
16500 /* The range type was succesfully read. Save it for the
16501 array type creation. */
16502 range_types.push_back (child_type);
16503 }
16504 }
16505 child_die = sibling_die (child_die);
16506 }
16507
16508 /* Dwarf2 dimensions are output from left to right, create the
16509 necessary array types in backwards order. */
16510
16511 type = element_type;
16512
16513 if (read_array_order (die, cu) == DW_ORD_col_major)
16514 {
16515 int i = 0;
16516
16517 while (i < range_types.size ())
16518 type = create_array_type_with_stride (NULL, type, range_types[i++],
16519 byte_stride_prop, bit_stride);
16520 }
16521 else
16522 {
16523 size_t ndim = range_types.size ();
16524 while (ndim-- > 0)
16525 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16526 byte_stride_prop, bit_stride);
16527 }
16528
16529 /* Understand Dwarf2 support for vector types (like they occur on
16530 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16531 array type. This is not part of the Dwarf2/3 standard yet, but a
16532 custom vendor extension. The main difference between a regular
16533 array and the vector variant is that vectors are passed by value
16534 to functions. */
16535 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16536 if (attr)
16537 make_vector_type (type);
16538
16539 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16540 implementation may choose to implement triple vectors using this
16541 attribute. */
16542 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16543 if (attr)
16544 {
16545 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16546 TYPE_LENGTH (type) = DW_UNSND (attr);
16547 else
16548 complaint (_("DW_AT_byte_size for array type smaller "
16549 "than the total size of elements"));
16550 }
16551
16552 name = dwarf2_name (die, cu);
16553 if (name)
16554 TYPE_NAME (type) = name;
16555
16556 maybe_set_alignment (cu, die, type);
16557
16558 /* Install the type in the die. */
16559 set_die_type (die, type, cu);
16560
16561 /* set_die_type should be already done. */
16562 set_descriptive_type (type, die, cu);
16563
16564 return type;
16565 }
16566
16567 static enum dwarf_array_dim_ordering
16568 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16569 {
16570 struct attribute *attr;
16571
16572 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16573
16574 if (attr)
16575 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16576
16577 /* GNU F77 is a special case, as at 08/2004 array type info is the
16578 opposite order to the dwarf2 specification, but data is still
16579 laid out as per normal fortran.
16580
16581 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16582 version checking. */
16583
16584 if (cu->language == language_fortran
16585 && cu->producer && strstr (cu->producer, "GNU F77"))
16586 {
16587 return DW_ORD_row_major;
16588 }
16589
16590 switch (cu->language_defn->la_array_ordering)
16591 {
16592 case array_column_major:
16593 return DW_ORD_col_major;
16594 case array_row_major:
16595 default:
16596 return DW_ORD_row_major;
16597 };
16598 }
16599
16600 /* Extract all information from a DW_TAG_set_type DIE and put it in
16601 the DIE's type field. */
16602
16603 static struct type *
16604 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16605 {
16606 struct type *domain_type, *set_type;
16607 struct attribute *attr;
16608
16609 domain_type = die_type (die, cu);
16610
16611 /* The die_type call above may have already set the type for this DIE. */
16612 set_type = get_die_type (die, cu);
16613 if (set_type)
16614 return set_type;
16615
16616 set_type = create_set_type (NULL, domain_type);
16617
16618 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16619 if (attr)
16620 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16621
16622 maybe_set_alignment (cu, die, set_type);
16623
16624 return set_die_type (die, set_type, cu);
16625 }
16626
16627 /* A helper for read_common_block that creates a locexpr baton.
16628 SYM is the symbol which we are marking as computed.
16629 COMMON_DIE is the DIE for the common block.
16630 COMMON_LOC is the location expression attribute for the common
16631 block itself.
16632 MEMBER_LOC is the location expression attribute for the particular
16633 member of the common block that we are processing.
16634 CU is the CU from which the above come. */
16635
16636 static void
16637 mark_common_block_symbol_computed (struct symbol *sym,
16638 struct die_info *common_die,
16639 struct attribute *common_loc,
16640 struct attribute *member_loc,
16641 struct dwarf2_cu *cu)
16642 {
16643 struct dwarf2_per_objfile *dwarf2_per_objfile
16644 = cu->per_cu->dwarf2_per_objfile;
16645 struct objfile *objfile = dwarf2_per_objfile->objfile;
16646 struct dwarf2_locexpr_baton *baton;
16647 gdb_byte *ptr;
16648 unsigned int cu_off;
16649 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16650 LONGEST offset = 0;
16651
16652 gdb_assert (common_loc && member_loc);
16653 gdb_assert (attr_form_is_block (common_loc));
16654 gdb_assert (attr_form_is_block (member_loc)
16655 || attr_form_is_constant (member_loc));
16656
16657 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16658 baton->per_cu = cu->per_cu;
16659 gdb_assert (baton->per_cu);
16660
16661 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16662
16663 if (attr_form_is_constant (member_loc))
16664 {
16665 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16666 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16667 }
16668 else
16669 baton->size += DW_BLOCK (member_loc)->size;
16670
16671 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16672 baton->data = ptr;
16673
16674 *ptr++ = DW_OP_call4;
16675 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16676 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16677 ptr += 4;
16678
16679 if (attr_form_is_constant (member_loc))
16680 {
16681 *ptr++ = DW_OP_addr;
16682 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16683 ptr += cu->header.addr_size;
16684 }
16685 else
16686 {
16687 /* We have to copy the data here, because DW_OP_call4 will only
16688 use a DW_AT_location attribute. */
16689 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16690 ptr += DW_BLOCK (member_loc)->size;
16691 }
16692
16693 *ptr++ = DW_OP_plus;
16694 gdb_assert (ptr - baton->data == baton->size);
16695
16696 SYMBOL_LOCATION_BATON (sym) = baton;
16697 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16698 }
16699
16700 /* Create appropriate locally-scoped variables for all the
16701 DW_TAG_common_block entries. Also create a struct common_block
16702 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16703 is used to sepate the common blocks name namespace from regular
16704 variable names. */
16705
16706 static void
16707 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16708 {
16709 struct attribute *attr;
16710
16711 attr = dwarf2_attr (die, DW_AT_location, cu);
16712 if (attr)
16713 {
16714 /* Support the .debug_loc offsets. */
16715 if (attr_form_is_block (attr))
16716 {
16717 /* Ok. */
16718 }
16719 else if (attr_form_is_section_offset (attr))
16720 {
16721 dwarf2_complex_location_expr_complaint ();
16722 attr = NULL;
16723 }
16724 else
16725 {
16726 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16727 "common block member");
16728 attr = NULL;
16729 }
16730 }
16731
16732 if (die->child != NULL)
16733 {
16734 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16735 struct die_info *child_die;
16736 size_t n_entries = 0, size;
16737 struct common_block *common_block;
16738 struct symbol *sym;
16739
16740 for (child_die = die->child;
16741 child_die && child_die->tag;
16742 child_die = sibling_die (child_die))
16743 ++n_entries;
16744
16745 size = (sizeof (struct common_block)
16746 + (n_entries - 1) * sizeof (struct symbol *));
16747 common_block
16748 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16749 size);
16750 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16751 common_block->n_entries = 0;
16752
16753 for (child_die = die->child;
16754 child_die && child_die->tag;
16755 child_die = sibling_die (child_die))
16756 {
16757 /* Create the symbol in the DW_TAG_common_block block in the current
16758 symbol scope. */
16759 sym = new_symbol (child_die, NULL, cu);
16760 if (sym != NULL)
16761 {
16762 struct attribute *member_loc;
16763
16764 common_block->contents[common_block->n_entries++] = sym;
16765
16766 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16767 cu);
16768 if (member_loc)
16769 {
16770 /* GDB has handled this for a long time, but it is
16771 not specified by DWARF. It seems to have been
16772 emitted by gfortran at least as recently as:
16773 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16774 complaint (_("Variable in common block has "
16775 "DW_AT_data_member_location "
16776 "- DIE at %s [in module %s]"),
16777 sect_offset_str (child_die->sect_off),
16778 objfile_name (objfile));
16779
16780 if (attr_form_is_section_offset (member_loc))
16781 dwarf2_complex_location_expr_complaint ();
16782 else if (attr_form_is_constant (member_loc)
16783 || attr_form_is_block (member_loc))
16784 {
16785 if (attr)
16786 mark_common_block_symbol_computed (sym, die, attr,
16787 member_loc, cu);
16788 }
16789 else
16790 dwarf2_complex_location_expr_complaint ();
16791 }
16792 }
16793 }
16794
16795 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16796 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16797 }
16798 }
16799
16800 /* Create a type for a C++ namespace. */
16801
16802 static struct type *
16803 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16804 {
16805 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16806 const char *previous_prefix, *name;
16807 int is_anonymous;
16808 struct type *type;
16809
16810 /* For extensions, reuse the type of the original namespace. */
16811 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16812 {
16813 struct die_info *ext_die;
16814 struct dwarf2_cu *ext_cu = cu;
16815
16816 ext_die = dwarf2_extension (die, &ext_cu);
16817 type = read_type_die (ext_die, ext_cu);
16818
16819 /* EXT_CU may not be the same as CU.
16820 Ensure TYPE is recorded with CU in die_type_hash. */
16821 return set_die_type (die, type, cu);
16822 }
16823
16824 name = namespace_name (die, &is_anonymous, cu);
16825
16826 /* Now build the name of the current namespace. */
16827
16828 previous_prefix = determine_prefix (die, cu);
16829 if (previous_prefix[0] != '\0')
16830 name = typename_concat (&objfile->objfile_obstack,
16831 previous_prefix, name, 0, cu);
16832
16833 /* Create the type. */
16834 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16835
16836 return set_die_type (die, type, cu);
16837 }
16838
16839 /* Read a namespace scope. */
16840
16841 static void
16842 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16843 {
16844 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16845 int is_anonymous;
16846
16847 /* Add a symbol associated to this if we haven't seen the namespace
16848 before. Also, add a using directive if it's an anonymous
16849 namespace. */
16850
16851 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16852 {
16853 struct type *type;
16854
16855 type = read_type_die (die, cu);
16856 new_symbol (die, type, cu);
16857
16858 namespace_name (die, &is_anonymous, cu);
16859 if (is_anonymous)
16860 {
16861 const char *previous_prefix = determine_prefix (die, cu);
16862
16863 std::vector<const char *> excludes;
16864 add_using_directive (using_directives (cu),
16865 previous_prefix, TYPE_NAME (type), NULL,
16866 NULL, excludes, 0, &objfile->objfile_obstack);
16867 }
16868 }
16869
16870 if (die->child != NULL)
16871 {
16872 struct die_info *child_die = die->child;
16873
16874 while (child_die && child_die->tag)
16875 {
16876 process_die (child_die, cu);
16877 child_die = sibling_die (child_die);
16878 }
16879 }
16880 }
16881
16882 /* Read a Fortran module as type. This DIE can be only a declaration used for
16883 imported module. Still we need that type as local Fortran "use ... only"
16884 declaration imports depend on the created type in determine_prefix. */
16885
16886 static struct type *
16887 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16888 {
16889 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16890 const char *module_name;
16891 struct type *type;
16892
16893 module_name = dwarf2_name (die, cu);
16894 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16895
16896 return set_die_type (die, type, cu);
16897 }
16898
16899 /* Read a Fortran module. */
16900
16901 static void
16902 read_module (struct die_info *die, struct dwarf2_cu *cu)
16903 {
16904 struct die_info *child_die = die->child;
16905 struct type *type;
16906
16907 type = read_type_die (die, cu);
16908 new_symbol (die, type, cu);
16909
16910 while (child_die && child_die->tag)
16911 {
16912 process_die (child_die, cu);
16913 child_die = sibling_die (child_die);
16914 }
16915 }
16916
16917 /* Return the name of the namespace represented by DIE. Set
16918 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16919 namespace. */
16920
16921 static const char *
16922 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16923 {
16924 struct die_info *current_die;
16925 const char *name = NULL;
16926
16927 /* Loop through the extensions until we find a name. */
16928
16929 for (current_die = die;
16930 current_die != NULL;
16931 current_die = dwarf2_extension (die, &cu))
16932 {
16933 /* We don't use dwarf2_name here so that we can detect the absence
16934 of a name -> anonymous namespace. */
16935 name = dwarf2_string_attr (die, DW_AT_name, cu);
16936
16937 if (name != NULL)
16938 break;
16939 }
16940
16941 /* Is it an anonymous namespace? */
16942
16943 *is_anonymous = (name == NULL);
16944 if (*is_anonymous)
16945 name = CP_ANONYMOUS_NAMESPACE_STR;
16946
16947 return name;
16948 }
16949
16950 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16951 the user defined type vector. */
16952
16953 static struct type *
16954 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16955 {
16956 struct gdbarch *gdbarch
16957 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16958 struct comp_unit_head *cu_header = &cu->header;
16959 struct type *type;
16960 struct attribute *attr_byte_size;
16961 struct attribute *attr_address_class;
16962 int byte_size, addr_class;
16963 struct type *target_type;
16964
16965 target_type = die_type (die, cu);
16966
16967 /* The die_type call above may have already set the type for this DIE. */
16968 type = get_die_type (die, cu);
16969 if (type)
16970 return type;
16971
16972 type = lookup_pointer_type (target_type);
16973
16974 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16975 if (attr_byte_size)
16976 byte_size = DW_UNSND (attr_byte_size);
16977 else
16978 byte_size = cu_header->addr_size;
16979
16980 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16981 if (attr_address_class)
16982 addr_class = DW_UNSND (attr_address_class);
16983 else
16984 addr_class = DW_ADDR_none;
16985
16986 ULONGEST alignment = get_alignment (cu, die);
16987
16988 /* If the pointer size, alignment, or address class is different
16989 than the default, create a type variant marked as such and set
16990 the length accordingly. */
16991 if (TYPE_LENGTH (type) != byte_size
16992 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16993 && alignment != TYPE_RAW_ALIGN (type))
16994 || addr_class != DW_ADDR_none)
16995 {
16996 if (gdbarch_address_class_type_flags_p (gdbarch))
16997 {
16998 int type_flags;
16999
17000 type_flags = gdbarch_address_class_type_flags
17001 (gdbarch, byte_size, addr_class);
17002 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17003 == 0);
17004 type = make_type_with_address_space (type, type_flags);
17005 }
17006 else if (TYPE_LENGTH (type) != byte_size)
17007 {
17008 complaint (_("invalid pointer size %d"), byte_size);
17009 }
17010 else if (TYPE_RAW_ALIGN (type) != alignment)
17011 {
17012 complaint (_("Invalid DW_AT_alignment"
17013 " - DIE at %s [in module %s]"),
17014 sect_offset_str (die->sect_off),
17015 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17016 }
17017 else
17018 {
17019 /* Should we also complain about unhandled address classes? */
17020 }
17021 }
17022
17023 TYPE_LENGTH (type) = byte_size;
17024 set_type_align (type, alignment);
17025 return set_die_type (die, type, cu);
17026 }
17027
17028 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17029 the user defined type vector. */
17030
17031 static struct type *
17032 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17033 {
17034 struct type *type;
17035 struct type *to_type;
17036 struct type *domain;
17037
17038 to_type = die_type (die, cu);
17039 domain = die_containing_type (die, cu);
17040
17041 /* The calls above may have already set the type for this DIE. */
17042 type = get_die_type (die, cu);
17043 if (type)
17044 return type;
17045
17046 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17047 type = lookup_methodptr_type (to_type);
17048 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17049 {
17050 struct type *new_type
17051 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17052
17053 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17054 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17055 TYPE_VARARGS (to_type));
17056 type = lookup_methodptr_type (new_type);
17057 }
17058 else
17059 type = lookup_memberptr_type (to_type, domain);
17060
17061 return set_die_type (die, type, cu);
17062 }
17063
17064 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17065 the user defined type vector. */
17066
17067 static struct type *
17068 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17069 enum type_code refcode)
17070 {
17071 struct comp_unit_head *cu_header = &cu->header;
17072 struct type *type, *target_type;
17073 struct attribute *attr;
17074
17075 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17076
17077 target_type = die_type (die, cu);
17078
17079 /* The die_type call above may have already set the type for this DIE. */
17080 type = get_die_type (die, cu);
17081 if (type)
17082 return type;
17083
17084 type = lookup_reference_type (target_type, refcode);
17085 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17086 if (attr)
17087 {
17088 TYPE_LENGTH (type) = DW_UNSND (attr);
17089 }
17090 else
17091 {
17092 TYPE_LENGTH (type) = cu_header->addr_size;
17093 }
17094 maybe_set_alignment (cu, die, type);
17095 return set_die_type (die, type, cu);
17096 }
17097
17098 /* Add the given cv-qualifiers to the element type of the array. GCC
17099 outputs DWARF type qualifiers that apply to an array, not the
17100 element type. But GDB relies on the array element type to carry
17101 the cv-qualifiers. This mimics section 6.7.3 of the C99
17102 specification. */
17103
17104 static struct type *
17105 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17106 struct type *base_type, int cnst, int voltl)
17107 {
17108 struct type *el_type, *inner_array;
17109
17110 base_type = copy_type (base_type);
17111 inner_array = base_type;
17112
17113 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17114 {
17115 TYPE_TARGET_TYPE (inner_array) =
17116 copy_type (TYPE_TARGET_TYPE (inner_array));
17117 inner_array = TYPE_TARGET_TYPE (inner_array);
17118 }
17119
17120 el_type = TYPE_TARGET_TYPE (inner_array);
17121 cnst |= TYPE_CONST (el_type);
17122 voltl |= TYPE_VOLATILE (el_type);
17123 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17124
17125 return set_die_type (die, base_type, cu);
17126 }
17127
17128 static struct type *
17129 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17130 {
17131 struct type *base_type, *cv_type;
17132
17133 base_type = die_type (die, cu);
17134
17135 /* The die_type call above may have already set the type for this DIE. */
17136 cv_type = get_die_type (die, cu);
17137 if (cv_type)
17138 return cv_type;
17139
17140 /* In case the const qualifier is applied to an array type, the element type
17141 is so qualified, not the array type (section 6.7.3 of C99). */
17142 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17143 return add_array_cv_type (die, cu, base_type, 1, 0);
17144
17145 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17146 return set_die_type (die, cv_type, cu);
17147 }
17148
17149 static struct type *
17150 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17151 {
17152 struct type *base_type, *cv_type;
17153
17154 base_type = die_type (die, cu);
17155
17156 /* The die_type call above may have already set the type for this DIE. */
17157 cv_type = get_die_type (die, cu);
17158 if (cv_type)
17159 return cv_type;
17160
17161 /* In case the volatile qualifier is applied to an array type, the
17162 element type is so qualified, not the array type (section 6.7.3
17163 of C99). */
17164 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17165 return add_array_cv_type (die, cu, base_type, 0, 1);
17166
17167 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17168 return set_die_type (die, cv_type, cu);
17169 }
17170
17171 /* Handle DW_TAG_restrict_type. */
17172
17173 static struct type *
17174 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17175 {
17176 struct type *base_type, *cv_type;
17177
17178 base_type = die_type (die, cu);
17179
17180 /* The die_type call above may have already set the type for this DIE. */
17181 cv_type = get_die_type (die, cu);
17182 if (cv_type)
17183 return cv_type;
17184
17185 cv_type = make_restrict_type (base_type);
17186 return set_die_type (die, cv_type, cu);
17187 }
17188
17189 /* Handle DW_TAG_atomic_type. */
17190
17191 static struct type *
17192 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17193 {
17194 struct type *base_type, *cv_type;
17195
17196 base_type = die_type (die, cu);
17197
17198 /* The die_type call above may have already set the type for this DIE. */
17199 cv_type = get_die_type (die, cu);
17200 if (cv_type)
17201 return cv_type;
17202
17203 cv_type = make_atomic_type (base_type);
17204 return set_die_type (die, cv_type, cu);
17205 }
17206
17207 /* Extract all information from a DW_TAG_string_type DIE and add to
17208 the user defined type vector. It isn't really a user defined type,
17209 but it behaves like one, with other DIE's using an AT_user_def_type
17210 attribute to reference it. */
17211
17212 static struct type *
17213 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17214 {
17215 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17216 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17217 struct type *type, *range_type, *index_type, *char_type;
17218 struct attribute *attr;
17219 unsigned int length;
17220
17221 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17222 if (attr)
17223 {
17224 length = DW_UNSND (attr);
17225 }
17226 else
17227 {
17228 /* Check for the DW_AT_byte_size attribute. */
17229 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17230 if (attr)
17231 {
17232 length = DW_UNSND (attr);
17233 }
17234 else
17235 {
17236 length = 1;
17237 }
17238 }
17239
17240 index_type = objfile_type (objfile)->builtin_int;
17241 range_type = create_static_range_type (NULL, index_type, 1, length);
17242 char_type = language_string_char_type (cu->language_defn, gdbarch);
17243 type = create_string_type (NULL, char_type, range_type);
17244
17245 return set_die_type (die, type, cu);
17246 }
17247
17248 /* Assuming that DIE corresponds to a function, returns nonzero
17249 if the function is prototyped. */
17250
17251 static int
17252 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17253 {
17254 struct attribute *attr;
17255
17256 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17257 if (attr && (DW_UNSND (attr) != 0))
17258 return 1;
17259
17260 /* The DWARF standard implies that the DW_AT_prototyped attribute
17261 is only meaninful for C, but the concept also extends to other
17262 languages that allow unprototyped functions (Eg: Objective C).
17263 For all other languages, assume that functions are always
17264 prototyped. */
17265 if (cu->language != language_c
17266 && cu->language != language_objc
17267 && cu->language != language_opencl)
17268 return 1;
17269
17270 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17271 prototyped and unprototyped functions; default to prototyped,
17272 since that is more common in modern code (and RealView warns
17273 about unprototyped functions). */
17274 if (producer_is_realview (cu->producer))
17275 return 1;
17276
17277 return 0;
17278 }
17279
17280 /* Handle DIES due to C code like:
17281
17282 struct foo
17283 {
17284 int (*funcp)(int a, long l);
17285 int b;
17286 };
17287
17288 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17289
17290 static struct type *
17291 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17292 {
17293 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17294 struct type *type; /* Type that this function returns. */
17295 struct type *ftype; /* Function that returns above type. */
17296 struct attribute *attr;
17297
17298 type = die_type (die, cu);
17299
17300 /* The die_type call above may have already set the type for this DIE. */
17301 ftype = get_die_type (die, cu);
17302 if (ftype)
17303 return ftype;
17304
17305 ftype = lookup_function_type (type);
17306
17307 if (prototyped_function_p (die, cu))
17308 TYPE_PROTOTYPED (ftype) = 1;
17309
17310 /* Store the calling convention in the type if it's available in
17311 the subroutine die. Otherwise set the calling convention to
17312 the default value DW_CC_normal. */
17313 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17314 if (attr)
17315 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17316 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17317 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17318 else
17319 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17320
17321 /* Record whether the function returns normally to its caller or not
17322 if the DWARF producer set that information. */
17323 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17324 if (attr && (DW_UNSND (attr) != 0))
17325 TYPE_NO_RETURN (ftype) = 1;
17326
17327 /* We need to add the subroutine type to the die immediately so
17328 we don't infinitely recurse when dealing with parameters
17329 declared as the same subroutine type. */
17330 set_die_type (die, ftype, cu);
17331
17332 if (die->child != NULL)
17333 {
17334 struct type *void_type = objfile_type (objfile)->builtin_void;
17335 struct die_info *child_die;
17336 int nparams, iparams;
17337
17338 /* Count the number of parameters.
17339 FIXME: GDB currently ignores vararg functions, but knows about
17340 vararg member functions. */
17341 nparams = 0;
17342 child_die = die->child;
17343 while (child_die && child_die->tag)
17344 {
17345 if (child_die->tag == DW_TAG_formal_parameter)
17346 nparams++;
17347 else if (child_die->tag == DW_TAG_unspecified_parameters)
17348 TYPE_VARARGS (ftype) = 1;
17349 child_die = sibling_die (child_die);
17350 }
17351
17352 /* Allocate storage for parameters and fill them in. */
17353 TYPE_NFIELDS (ftype) = nparams;
17354 TYPE_FIELDS (ftype) = (struct field *)
17355 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17356
17357 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17358 even if we error out during the parameters reading below. */
17359 for (iparams = 0; iparams < nparams; iparams++)
17360 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17361
17362 iparams = 0;
17363 child_die = die->child;
17364 while (child_die && child_die->tag)
17365 {
17366 if (child_die->tag == DW_TAG_formal_parameter)
17367 {
17368 struct type *arg_type;
17369
17370 /* DWARF version 2 has no clean way to discern C++
17371 static and non-static member functions. G++ helps
17372 GDB by marking the first parameter for non-static
17373 member functions (which is the this pointer) as
17374 artificial. We pass this information to
17375 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17376
17377 DWARF version 3 added DW_AT_object_pointer, which GCC
17378 4.5 does not yet generate. */
17379 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17380 if (attr)
17381 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17382 else
17383 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17384 arg_type = die_type (child_die, cu);
17385
17386 /* RealView does not mark THIS as const, which the testsuite
17387 expects. GCC marks THIS as const in method definitions,
17388 but not in the class specifications (GCC PR 43053). */
17389 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17390 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17391 {
17392 int is_this = 0;
17393 struct dwarf2_cu *arg_cu = cu;
17394 const char *name = dwarf2_name (child_die, cu);
17395
17396 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17397 if (attr)
17398 {
17399 /* If the compiler emits this, use it. */
17400 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17401 is_this = 1;
17402 }
17403 else if (name && strcmp (name, "this") == 0)
17404 /* Function definitions will have the argument names. */
17405 is_this = 1;
17406 else if (name == NULL && iparams == 0)
17407 /* Declarations may not have the names, so like
17408 elsewhere in GDB, assume an artificial first
17409 argument is "this". */
17410 is_this = 1;
17411
17412 if (is_this)
17413 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17414 arg_type, 0);
17415 }
17416
17417 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17418 iparams++;
17419 }
17420 child_die = sibling_die (child_die);
17421 }
17422 }
17423
17424 return ftype;
17425 }
17426
17427 static struct type *
17428 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17429 {
17430 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17431 const char *name = NULL;
17432 struct type *this_type, *target_type;
17433
17434 name = dwarf2_full_name (NULL, die, cu);
17435 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17436 TYPE_TARGET_STUB (this_type) = 1;
17437 set_die_type (die, this_type, cu);
17438 target_type = die_type (die, cu);
17439 if (target_type != this_type)
17440 TYPE_TARGET_TYPE (this_type) = target_type;
17441 else
17442 {
17443 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17444 spec and cause infinite loops in GDB. */
17445 complaint (_("Self-referential DW_TAG_typedef "
17446 "- DIE at %s [in module %s]"),
17447 sect_offset_str (die->sect_off), objfile_name (objfile));
17448 TYPE_TARGET_TYPE (this_type) = NULL;
17449 }
17450 return this_type;
17451 }
17452
17453 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17454 (which may be different from NAME) to the architecture back-end to allow
17455 it to guess the correct format if necessary. */
17456
17457 static struct type *
17458 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17459 const char *name_hint)
17460 {
17461 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17462 const struct floatformat **format;
17463 struct type *type;
17464
17465 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17466 if (format)
17467 type = init_float_type (objfile, bits, name, format);
17468 else
17469 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17470
17471 return type;
17472 }
17473
17474 /* Allocate an integer type of size BITS and name NAME. */
17475
17476 static struct type *
17477 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17478 int bits, int unsigned_p, const char *name)
17479 {
17480 struct type *type;
17481
17482 /* Versions of Intel's C Compiler generate an integer type called "void"
17483 instead of using DW_TAG_unspecified_type. This has been seen on
17484 at least versions 14, 17, and 18. */
17485 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17486 && strcmp (name, "void") == 0)
17487 type = objfile_type (objfile)->builtin_void;
17488 else
17489 type = init_integer_type (objfile, bits, unsigned_p, name);
17490
17491 return type;
17492 }
17493
17494 /* Initialise and return a floating point type of size BITS suitable for
17495 use as a component of a complex number. The NAME_HINT is passed through
17496 when initialising the floating point type and is the name of the complex
17497 type.
17498
17499 As DWARF doesn't currently provide an explicit name for the components
17500 of a complex number, but it can be helpful to have these components
17501 named, we try to select a suitable name based on the size of the
17502 component. */
17503 static struct type *
17504 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17505 struct objfile *objfile,
17506 int bits, const char *name_hint)
17507 {
17508 gdbarch *gdbarch = get_objfile_arch (objfile);
17509 struct type *tt = nullptr;
17510
17511 /* Try to find a suitable floating point builtin type of size BITS.
17512 We're going to use the name of this type as the name for the complex
17513 target type that we are about to create. */
17514 switch (cu->language)
17515 {
17516 case language_fortran:
17517 switch (bits)
17518 {
17519 case 32:
17520 tt = builtin_f_type (gdbarch)->builtin_real;
17521 break;
17522 case 64:
17523 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17524 break;
17525 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17526 case 128:
17527 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17528 break;
17529 }
17530 break;
17531 default:
17532 switch (bits)
17533 {
17534 case 32:
17535 tt = builtin_type (gdbarch)->builtin_float;
17536 break;
17537 case 64:
17538 tt = builtin_type (gdbarch)->builtin_double;
17539 break;
17540 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17541 case 128:
17542 tt = builtin_type (gdbarch)->builtin_long_double;
17543 break;
17544 }
17545 break;
17546 }
17547
17548 /* If the type we found doesn't match the size we were looking for, then
17549 pretend we didn't find a type at all, the complex target type we
17550 create will then be nameless. */
17551 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17552 tt = nullptr;
17553
17554 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17555 return dwarf2_init_float_type (objfile, bits, name, name_hint);
17556 }
17557
17558 /* Find a representation of a given base type and install
17559 it in the TYPE field of the die. */
17560
17561 static struct type *
17562 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17563 {
17564 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17565 struct type *type;
17566 struct attribute *attr;
17567 int encoding = 0, bits = 0;
17568 const char *name;
17569
17570 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17571 if (attr)
17572 {
17573 encoding = DW_UNSND (attr);
17574 }
17575 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17576 if (attr)
17577 {
17578 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17579 }
17580 name = dwarf2_name (die, cu);
17581 if (!name)
17582 {
17583 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17584 }
17585
17586 switch (encoding)
17587 {
17588 case DW_ATE_address:
17589 /* Turn DW_ATE_address into a void * pointer. */
17590 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17591 type = init_pointer_type (objfile, bits, name, type);
17592 break;
17593 case DW_ATE_boolean:
17594 type = init_boolean_type (objfile, bits, 1, name);
17595 break;
17596 case DW_ATE_complex_float:
17597 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name);
17598 type = init_complex_type (objfile, name, type);
17599 break;
17600 case DW_ATE_decimal_float:
17601 type = init_decfloat_type (objfile, bits, name);
17602 break;
17603 case DW_ATE_float:
17604 type = dwarf2_init_float_type (objfile, bits, name, name);
17605 break;
17606 case DW_ATE_signed:
17607 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17608 break;
17609 case DW_ATE_unsigned:
17610 if (cu->language == language_fortran
17611 && name
17612 && startswith (name, "character("))
17613 type = init_character_type (objfile, bits, 1, name);
17614 else
17615 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17616 break;
17617 case DW_ATE_signed_char:
17618 if (cu->language == language_ada || cu->language == language_m2
17619 || cu->language == language_pascal
17620 || cu->language == language_fortran)
17621 type = init_character_type (objfile, bits, 0, name);
17622 else
17623 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17624 break;
17625 case DW_ATE_unsigned_char:
17626 if (cu->language == language_ada || cu->language == language_m2
17627 || cu->language == language_pascal
17628 || cu->language == language_fortran
17629 || cu->language == language_rust)
17630 type = init_character_type (objfile, bits, 1, name);
17631 else
17632 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17633 break;
17634 case DW_ATE_UTF:
17635 {
17636 gdbarch *arch = get_objfile_arch (objfile);
17637
17638 if (bits == 16)
17639 type = builtin_type (arch)->builtin_char16;
17640 else if (bits == 32)
17641 type = builtin_type (arch)->builtin_char32;
17642 else
17643 {
17644 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17645 bits);
17646 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17647 }
17648 return set_die_type (die, type, cu);
17649 }
17650 break;
17651
17652 default:
17653 complaint (_("unsupported DW_AT_encoding: '%s'"),
17654 dwarf_type_encoding_name (encoding));
17655 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17656 break;
17657 }
17658
17659 if (name && strcmp (name, "char") == 0)
17660 TYPE_NOSIGN (type) = 1;
17661
17662 maybe_set_alignment (cu, die, type);
17663
17664 return set_die_type (die, type, cu);
17665 }
17666
17667 /* Parse dwarf attribute if it's a block, reference or constant and put the
17668 resulting value of the attribute into struct bound_prop.
17669 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17670
17671 static int
17672 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17673 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17674 struct type *default_type)
17675 {
17676 struct dwarf2_property_baton *baton;
17677 struct obstack *obstack
17678 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17679
17680 gdb_assert (default_type != NULL);
17681
17682 if (attr == NULL || prop == NULL)
17683 return 0;
17684
17685 if (attr_form_is_block (attr))
17686 {
17687 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17688 baton->property_type = default_type;
17689 baton->locexpr.per_cu = cu->per_cu;
17690 baton->locexpr.size = DW_BLOCK (attr)->size;
17691 baton->locexpr.data = DW_BLOCK (attr)->data;
17692 baton->locexpr.is_reference = false;
17693 prop->data.baton = baton;
17694 prop->kind = PROP_LOCEXPR;
17695 gdb_assert (prop->data.baton != NULL);
17696 }
17697 else if (attr_form_is_ref (attr))
17698 {
17699 struct dwarf2_cu *target_cu = cu;
17700 struct die_info *target_die;
17701 struct attribute *target_attr;
17702
17703 target_die = follow_die_ref (die, attr, &target_cu);
17704 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17705 if (target_attr == NULL)
17706 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17707 target_cu);
17708 if (target_attr == NULL)
17709 return 0;
17710
17711 switch (target_attr->name)
17712 {
17713 case DW_AT_location:
17714 if (attr_form_is_section_offset (target_attr))
17715 {
17716 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17717 baton->property_type = die_type (target_die, target_cu);
17718 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17719 prop->data.baton = baton;
17720 prop->kind = PROP_LOCLIST;
17721 gdb_assert (prop->data.baton != NULL);
17722 }
17723 else if (attr_form_is_block (target_attr))
17724 {
17725 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17726 baton->property_type = die_type (target_die, target_cu);
17727 baton->locexpr.per_cu = cu->per_cu;
17728 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17729 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17730 baton->locexpr.is_reference = true;
17731 prop->data.baton = baton;
17732 prop->kind = PROP_LOCEXPR;
17733 gdb_assert (prop->data.baton != NULL);
17734 }
17735 else
17736 {
17737 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17738 "dynamic property");
17739 return 0;
17740 }
17741 break;
17742 case DW_AT_data_member_location:
17743 {
17744 LONGEST offset;
17745
17746 if (!handle_data_member_location (target_die, target_cu,
17747 &offset))
17748 return 0;
17749
17750 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17751 baton->property_type = read_type_die (target_die->parent,
17752 target_cu);
17753 baton->offset_info.offset = offset;
17754 baton->offset_info.type = die_type (target_die, target_cu);
17755 prop->data.baton = baton;
17756 prop->kind = PROP_ADDR_OFFSET;
17757 break;
17758 }
17759 }
17760 }
17761 else if (attr_form_is_constant (attr))
17762 {
17763 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17764 prop->kind = PROP_CONST;
17765 }
17766 else
17767 {
17768 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17769 dwarf2_name (die, cu));
17770 return 0;
17771 }
17772
17773 return 1;
17774 }
17775
17776 /* Find an integer type the same size as the address size given in the
17777 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
17778 is unsigned or not. */
17779
17780 static struct type *
17781 dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
17782 bool unsigned_p)
17783 {
17784 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
17785 int addr_size = dwarf2_per_cu_addr_size (per_cu);
17786 struct type *int_type;
17787
17788 /* Helper macro to examine the various builtin types. */
17789 #define TRY_TYPE(F) \
17790 int_type = (unsigned_p \
17791 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17792 : objfile_type (objfile)->builtin_ ## F); \
17793 if (int_type != NULL && TYPE_LENGTH (int_type) == addr_size) \
17794 return int_type
17795
17796 TRY_TYPE (char);
17797 TRY_TYPE (short);
17798 TRY_TYPE (int);
17799 TRY_TYPE (long);
17800 TRY_TYPE (long_long);
17801
17802 #undef TRY_TYPE
17803
17804 gdb_assert_not_reached ("unable to find suitable integer type");
17805 }
17806
17807 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17808 present (which is valid) then compute the default type based on the
17809 compilation units address size. */
17810
17811 static struct type *
17812 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17813 {
17814 struct type *index_type = die_type (die, cu);
17815
17816 /* Dwarf-2 specifications explicitly allows to create subrange types
17817 without specifying a base type.
17818 In that case, the base type must be set to the type of
17819 the lower bound, upper bound or count, in that order, if any of these
17820 three attributes references an object that has a type.
17821 If no base type is found, the Dwarf-2 specifications say that
17822 a signed integer type of size equal to the size of an address should
17823 be used.
17824 For the following C code: `extern char gdb_int [];'
17825 GCC produces an empty range DIE.
17826 FIXME: muller/2010-05-28: Possible references to object for low bound,
17827 high bound or count are not yet handled by this code. */
17828 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17829 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17830
17831 return index_type;
17832 }
17833
17834 /* Read the given DW_AT_subrange DIE. */
17835
17836 static struct type *
17837 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17838 {
17839 struct type *base_type, *orig_base_type;
17840 struct type *range_type;
17841 struct attribute *attr;
17842 struct dynamic_prop low, high;
17843 int low_default_is_valid;
17844 int high_bound_is_count = 0;
17845 const char *name;
17846 ULONGEST negative_mask;
17847
17848 orig_base_type = read_subrange_index_type (die, cu);
17849
17850 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17851 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17852 creating the range type, but we use the result of check_typedef
17853 when examining properties of the type. */
17854 base_type = check_typedef (orig_base_type);
17855
17856 /* The die_type call above may have already set the type for this DIE. */
17857 range_type = get_die_type (die, cu);
17858 if (range_type)
17859 return range_type;
17860
17861 low.kind = PROP_CONST;
17862 high.kind = PROP_CONST;
17863 high.data.const_val = 0;
17864
17865 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17866 omitting DW_AT_lower_bound. */
17867 switch (cu->language)
17868 {
17869 case language_c:
17870 case language_cplus:
17871 low.data.const_val = 0;
17872 low_default_is_valid = 1;
17873 break;
17874 case language_fortran:
17875 low.data.const_val = 1;
17876 low_default_is_valid = 1;
17877 break;
17878 case language_d:
17879 case language_objc:
17880 case language_rust:
17881 low.data.const_val = 0;
17882 low_default_is_valid = (cu->header.version >= 4);
17883 break;
17884 case language_ada:
17885 case language_m2:
17886 case language_pascal:
17887 low.data.const_val = 1;
17888 low_default_is_valid = (cu->header.version >= 4);
17889 break;
17890 default:
17891 low.data.const_val = 0;
17892 low_default_is_valid = 0;
17893 break;
17894 }
17895
17896 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17897 if (attr)
17898 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17899 else if (!low_default_is_valid)
17900 complaint (_("Missing DW_AT_lower_bound "
17901 "- DIE at %s [in module %s]"),
17902 sect_offset_str (die->sect_off),
17903 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17904
17905 struct attribute *attr_ub, *attr_count;
17906 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17907 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17908 {
17909 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17910 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17911 {
17912 /* If bounds are constant do the final calculation here. */
17913 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17914 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17915 else
17916 high_bound_is_count = 1;
17917 }
17918 else
17919 {
17920 if (attr_ub != NULL)
17921 complaint (_("Unresolved DW_AT_upper_bound "
17922 "- DIE at %s [in module %s]"),
17923 sect_offset_str (die->sect_off),
17924 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17925 if (attr_count != NULL)
17926 complaint (_("Unresolved DW_AT_count "
17927 "- DIE at %s [in module %s]"),
17928 sect_offset_str (die->sect_off),
17929 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17930 }
17931 }
17932
17933 LONGEST bias = 0;
17934 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17935 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
17936 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17937
17938 /* Normally, the DWARF producers are expected to use a signed
17939 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17940 But this is unfortunately not always the case, as witnessed
17941 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17942 is used instead. To work around that ambiguity, we treat
17943 the bounds as signed, and thus sign-extend their values, when
17944 the base type is signed. */
17945 negative_mask =
17946 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17947 if (low.kind == PROP_CONST
17948 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17949 low.data.const_val |= negative_mask;
17950 if (high.kind == PROP_CONST
17951 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17952 high.data.const_val |= negative_mask;
17953
17954 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17955
17956 if (high_bound_is_count)
17957 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17958
17959 /* Ada expects an empty array on no boundary attributes. */
17960 if (attr == NULL && cu->language != language_ada)
17961 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17962
17963 name = dwarf2_name (die, cu);
17964 if (name)
17965 TYPE_NAME (range_type) = name;
17966
17967 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17968 if (attr)
17969 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17970
17971 maybe_set_alignment (cu, die, range_type);
17972
17973 set_die_type (die, range_type, cu);
17974
17975 /* set_die_type should be already done. */
17976 set_descriptive_type (range_type, die, cu);
17977
17978 return range_type;
17979 }
17980
17981 static struct type *
17982 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17983 {
17984 struct type *type;
17985
17986 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17987 NULL);
17988 TYPE_NAME (type) = dwarf2_name (die, cu);
17989
17990 /* In Ada, an unspecified type is typically used when the description
17991 of the type is defered to a different unit. When encountering
17992 such a type, we treat it as a stub, and try to resolve it later on,
17993 when needed. */
17994 if (cu->language == language_ada)
17995 TYPE_STUB (type) = 1;
17996
17997 return set_die_type (die, type, cu);
17998 }
17999
18000 /* Read a single die and all its descendents. Set the die's sibling
18001 field to NULL; set other fields in the die correctly, and set all
18002 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18003 location of the info_ptr after reading all of those dies. PARENT
18004 is the parent of the die in question. */
18005
18006 static struct die_info *
18007 read_die_and_children (const struct die_reader_specs *reader,
18008 const gdb_byte *info_ptr,
18009 const gdb_byte **new_info_ptr,
18010 struct die_info *parent)
18011 {
18012 struct die_info *die;
18013 const gdb_byte *cur_ptr;
18014 int has_children;
18015
18016 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18017 if (die == NULL)
18018 {
18019 *new_info_ptr = cur_ptr;
18020 return NULL;
18021 }
18022 store_in_ref_table (die, reader->cu);
18023
18024 if (has_children)
18025 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18026 else
18027 {
18028 die->child = NULL;
18029 *new_info_ptr = cur_ptr;
18030 }
18031
18032 die->sibling = NULL;
18033 die->parent = parent;
18034 return die;
18035 }
18036
18037 /* Read a die, all of its descendents, and all of its siblings; set
18038 all of the fields of all of the dies correctly. Arguments are as
18039 in read_die_and_children. */
18040
18041 static struct die_info *
18042 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18043 const gdb_byte *info_ptr,
18044 const gdb_byte **new_info_ptr,
18045 struct die_info *parent)
18046 {
18047 struct die_info *first_die, *last_sibling;
18048 const gdb_byte *cur_ptr;
18049
18050 cur_ptr = info_ptr;
18051 first_die = last_sibling = NULL;
18052
18053 while (1)
18054 {
18055 struct die_info *die
18056 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18057
18058 if (die == NULL)
18059 {
18060 *new_info_ptr = cur_ptr;
18061 return first_die;
18062 }
18063
18064 if (!first_die)
18065 first_die = die;
18066 else
18067 last_sibling->sibling = die;
18068
18069 last_sibling = die;
18070 }
18071 }
18072
18073 /* Read a die, all of its descendents, and all of its siblings; set
18074 all of the fields of all of the dies correctly. Arguments are as
18075 in read_die_and_children.
18076 This the main entry point for reading a DIE and all its children. */
18077
18078 static struct die_info *
18079 read_die_and_siblings (const struct die_reader_specs *reader,
18080 const gdb_byte *info_ptr,
18081 const gdb_byte **new_info_ptr,
18082 struct die_info *parent)
18083 {
18084 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18085 new_info_ptr, parent);
18086
18087 if (dwarf_die_debug)
18088 {
18089 fprintf_unfiltered (gdb_stdlog,
18090 "Read die from %s@0x%x of %s:\n",
18091 get_section_name (reader->die_section),
18092 (unsigned) (info_ptr - reader->die_section->buffer),
18093 bfd_get_filename (reader->abfd));
18094 dump_die (die, dwarf_die_debug);
18095 }
18096
18097 return die;
18098 }
18099
18100 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18101 attributes.
18102 The caller is responsible for filling in the extra attributes
18103 and updating (*DIEP)->num_attrs.
18104 Set DIEP to point to a newly allocated die with its information,
18105 except for its child, sibling, and parent fields.
18106 Set HAS_CHILDREN to tell whether the die has children or not. */
18107
18108 static const gdb_byte *
18109 read_full_die_1 (const struct die_reader_specs *reader,
18110 struct die_info **diep, const gdb_byte *info_ptr,
18111 int *has_children, int num_extra_attrs)
18112 {
18113 unsigned int abbrev_number, bytes_read, i;
18114 struct abbrev_info *abbrev;
18115 struct die_info *die;
18116 struct dwarf2_cu *cu = reader->cu;
18117 bfd *abfd = reader->abfd;
18118
18119 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18120 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18121 info_ptr += bytes_read;
18122 if (!abbrev_number)
18123 {
18124 *diep = NULL;
18125 *has_children = 0;
18126 return info_ptr;
18127 }
18128
18129 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18130 if (!abbrev)
18131 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18132 abbrev_number,
18133 bfd_get_filename (abfd));
18134
18135 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18136 die->sect_off = sect_off;
18137 die->tag = abbrev->tag;
18138 die->abbrev = abbrev_number;
18139
18140 /* Make the result usable.
18141 The caller needs to update num_attrs after adding the extra
18142 attributes. */
18143 die->num_attrs = abbrev->num_attrs;
18144
18145 for (i = 0; i < abbrev->num_attrs; ++i)
18146 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18147 info_ptr);
18148
18149 *diep = die;
18150 *has_children = abbrev->has_children;
18151 return info_ptr;
18152 }
18153
18154 /* Read a die and all its attributes.
18155 Set DIEP to point to a newly allocated die with its information,
18156 except for its child, sibling, and parent fields.
18157 Set HAS_CHILDREN to tell whether the die has children or not. */
18158
18159 static const gdb_byte *
18160 read_full_die (const struct die_reader_specs *reader,
18161 struct die_info **diep, const gdb_byte *info_ptr,
18162 int *has_children)
18163 {
18164 const gdb_byte *result;
18165
18166 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18167
18168 if (dwarf_die_debug)
18169 {
18170 fprintf_unfiltered (gdb_stdlog,
18171 "Read die from %s@0x%x of %s:\n",
18172 get_section_name (reader->die_section),
18173 (unsigned) (info_ptr - reader->die_section->buffer),
18174 bfd_get_filename (reader->abfd));
18175 dump_die (*diep, dwarf_die_debug);
18176 }
18177
18178 return result;
18179 }
18180 \f
18181 /* Abbreviation tables.
18182
18183 In DWARF version 2, the description of the debugging information is
18184 stored in a separate .debug_abbrev section. Before we read any
18185 dies from a section we read in all abbreviations and install them
18186 in a hash table. */
18187
18188 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18189
18190 struct abbrev_info *
18191 abbrev_table::alloc_abbrev ()
18192 {
18193 struct abbrev_info *abbrev;
18194
18195 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18196 memset (abbrev, 0, sizeof (struct abbrev_info));
18197
18198 return abbrev;
18199 }
18200
18201 /* Add an abbreviation to the table. */
18202
18203 void
18204 abbrev_table::add_abbrev (unsigned int abbrev_number,
18205 struct abbrev_info *abbrev)
18206 {
18207 unsigned int hash_number;
18208
18209 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18210 abbrev->next = m_abbrevs[hash_number];
18211 m_abbrevs[hash_number] = abbrev;
18212 }
18213
18214 /* Look up an abbrev in the table.
18215 Returns NULL if the abbrev is not found. */
18216
18217 struct abbrev_info *
18218 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18219 {
18220 unsigned int hash_number;
18221 struct abbrev_info *abbrev;
18222
18223 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18224 abbrev = m_abbrevs[hash_number];
18225
18226 while (abbrev)
18227 {
18228 if (abbrev->number == abbrev_number)
18229 return abbrev;
18230 abbrev = abbrev->next;
18231 }
18232 return NULL;
18233 }
18234
18235 /* Read in an abbrev table. */
18236
18237 static abbrev_table_up
18238 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18239 struct dwarf2_section_info *section,
18240 sect_offset sect_off)
18241 {
18242 struct objfile *objfile = dwarf2_per_objfile->objfile;
18243 bfd *abfd = get_section_bfd_owner (section);
18244 const gdb_byte *abbrev_ptr;
18245 struct abbrev_info *cur_abbrev;
18246 unsigned int abbrev_number, bytes_read, abbrev_name;
18247 unsigned int abbrev_form;
18248 struct attr_abbrev *cur_attrs;
18249 unsigned int allocated_attrs;
18250
18251 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18252
18253 dwarf2_read_section (objfile, section);
18254 abbrev_ptr = section->buffer + to_underlying (sect_off);
18255 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18256 abbrev_ptr += bytes_read;
18257
18258 allocated_attrs = ATTR_ALLOC_CHUNK;
18259 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18260
18261 /* Loop until we reach an abbrev number of 0. */
18262 while (abbrev_number)
18263 {
18264 cur_abbrev = abbrev_table->alloc_abbrev ();
18265
18266 /* read in abbrev header */
18267 cur_abbrev->number = abbrev_number;
18268 cur_abbrev->tag
18269 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18270 abbrev_ptr += bytes_read;
18271 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18272 abbrev_ptr += 1;
18273
18274 /* now read in declarations */
18275 for (;;)
18276 {
18277 LONGEST implicit_const;
18278
18279 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18280 abbrev_ptr += bytes_read;
18281 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18282 abbrev_ptr += bytes_read;
18283 if (abbrev_form == DW_FORM_implicit_const)
18284 {
18285 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18286 &bytes_read);
18287 abbrev_ptr += bytes_read;
18288 }
18289 else
18290 {
18291 /* Initialize it due to a false compiler warning. */
18292 implicit_const = -1;
18293 }
18294
18295 if (abbrev_name == 0)
18296 break;
18297
18298 if (cur_abbrev->num_attrs == allocated_attrs)
18299 {
18300 allocated_attrs += ATTR_ALLOC_CHUNK;
18301 cur_attrs
18302 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18303 }
18304
18305 cur_attrs[cur_abbrev->num_attrs].name
18306 = (enum dwarf_attribute) abbrev_name;
18307 cur_attrs[cur_abbrev->num_attrs].form
18308 = (enum dwarf_form) abbrev_form;
18309 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18310 ++cur_abbrev->num_attrs;
18311 }
18312
18313 cur_abbrev->attrs =
18314 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18315 cur_abbrev->num_attrs);
18316 memcpy (cur_abbrev->attrs, cur_attrs,
18317 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18318
18319 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18320
18321 /* Get next abbreviation.
18322 Under Irix6 the abbreviations for a compilation unit are not
18323 always properly terminated with an abbrev number of 0.
18324 Exit loop if we encounter an abbreviation which we have
18325 already read (which means we are about to read the abbreviations
18326 for the next compile unit) or if the end of the abbreviation
18327 table is reached. */
18328 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18329 break;
18330 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18331 abbrev_ptr += bytes_read;
18332 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18333 break;
18334 }
18335
18336 xfree (cur_attrs);
18337 return abbrev_table;
18338 }
18339
18340 /* Returns nonzero if TAG represents a type that we might generate a partial
18341 symbol for. */
18342
18343 static int
18344 is_type_tag_for_partial (int tag)
18345 {
18346 switch (tag)
18347 {
18348 #if 0
18349 /* Some types that would be reasonable to generate partial symbols for,
18350 that we don't at present. */
18351 case DW_TAG_array_type:
18352 case DW_TAG_file_type:
18353 case DW_TAG_ptr_to_member_type:
18354 case DW_TAG_set_type:
18355 case DW_TAG_string_type:
18356 case DW_TAG_subroutine_type:
18357 #endif
18358 case DW_TAG_base_type:
18359 case DW_TAG_class_type:
18360 case DW_TAG_interface_type:
18361 case DW_TAG_enumeration_type:
18362 case DW_TAG_structure_type:
18363 case DW_TAG_subrange_type:
18364 case DW_TAG_typedef:
18365 case DW_TAG_union_type:
18366 return 1;
18367 default:
18368 return 0;
18369 }
18370 }
18371
18372 /* Load all DIEs that are interesting for partial symbols into memory. */
18373
18374 static struct partial_die_info *
18375 load_partial_dies (const struct die_reader_specs *reader,
18376 const gdb_byte *info_ptr, int building_psymtab)
18377 {
18378 struct dwarf2_cu *cu = reader->cu;
18379 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18380 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18381 unsigned int bytes_read;
18382 unsigned int load_all = 0;
18383 int nesting_level = 1;
18384
18385 parent_die = NULL;
18386 last_die = NULL;
18387
18388 gdb_assert (cu->per_cu != NULL);
18389 if (cu->per_cu->load_all_dies)
18390 load_all = 1;
18391
18392 cu->partial_dies
18393 = htab_create_alloc_ex (cu->header.length / 12,
18394 partial_die_hash,
18395 partial_die_eq,
18396 NULL,
18397 &cu->comp_unit_obstack,
18398 hashtab_obstack_allocate,
18399 dummy_obstack_deallocate);
18400
18401 while (1)
18402 {
18403 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18404
18405 /* A NULL abbrev means the end of a series of children. */
18406 if (abbrev == NULL)
18407 {
18408 if (--nesting_level == 0)
18409 return first_die;
18410
18411 info_ptr += bytes_read;
18412 last_die = parent_die;
18413 parent_die = parent_die->die_parent;
18414 continue;
18415 }
18416
18417 /* Check for template arguments. We never save these; if
18418 they're seen, we just mark the parent, and go on our way. */
18419 if (parent_die != NULL
18420 && cu->language == language_cplus
18421 && (abbrev->tag == DW_TAG_template_type_param
18422 || abbrev->tag == DW_TAG_template_value_param))
18423 {
18424 parent_die->has_template_arguments = 1;
18425
18426 if (!load_all)
18427 {
18428 /* We don't need a partial DIE for the template argument. */
18429 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18430 continue;
18431 }
18432 }
18433
18434 /* We only recurse into c++ subprograms looking for template arguments.
18435 Skip their other children. */
18436 if (!load_all
18437 && cu->language == language_cplus
18438 && parent_die != NULL
18439 && parent_die->tag == DW_TAG_subprogram)
18440 {
18441 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18442 continue;
18443 }
18444
18445 /* Check whether this DIE is interesting enough to save. Normally
18446 we would not be interested in members here, but there may be
18447 later variables referencing them via DW_AT_specification (for
18448 static members). */
18449 if (!load_all
18450 && !is_type_tag_for_partial (abbrev->tag)
18451 && abbrev->tag != DW_TAG_constant
18452 && abbrev->tag != DW_TAG_enumerator
18453 && abbrev->tag != DW_TAG_subprogram
18454 && abbrev->tag != DW_TAG_inlined_subroutine
18455 && abbrev->tag != DW_TAG_lexical_block
18456 && abbrev->tag != DW_TAG_variable
18457 && abbrev->tag != DW_TAG_namespace
18458 && abbrev->tag != DW_TAG_module
18459 && abbrev->tag != DW_TAG_member
18460 && abbrev->tag != DW_TAG_imported_unit
18461 && abbrev->tag != DW_TAG_imported_declaration)
18462 {
18463 /* Otherwise we skip to the next sibling, if any. */
18464 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18465 continue;
18466 }
18467
18468 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18469 abbrev);
18470
18471 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18472
18473 /* This two-pass algorithm for processing partial symbols has a
18474 high cost in cache pressure. Thus, handle some simple cases
18475 here which cover the majority of C partial symbols. DIEs
18476 which neither have specification tags in them, nor could have
18477 specification tags elsewhere pointing at them, can simply be
18478 processed and discarded.
18479
18480 This segment is also optional; scan_partial_symbols and
18481 add_partial_symbol will handle these DIEs if we chain
18482 them in normally. When compilers which do not emit large
18483 quantities of duplicate debug information are more common,
18484 this code can probably be removed. */
18485
18486 /* Any complete simple types at the top level (pretty much all
18487 of them, for a language without namespaces), can be processed
18488 directly. */
18489 if (parent_die == NULL
18490 && pdi.has_specification == 0
18491 && pdi.is_declaration == 0
18492 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18493 || pdi.tag == DW_TAG_base_type
18494 || pdi.tag == DW_TAG_subrange_type))
18495 {
18496 if (building_psymtab && pdi.name != NULL)
18497 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18498 VAR_DOMAIN, LOC_TYPEDEF, -1,
18499 psymbol_placement::STATIC,
18500 0, cu->language, objfile);
18501 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18502 continue;
18503 }
18504
18505 /* The exception for DW_TAG_typedef with has_children above is
18506 a workaround of GCC PR debug/47510. In the case of this complaint
18507 type_name_or_error will error on such types later.
18508
18509 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18510 it could not find the child DIEs referenced later, this is checked
18511 above. In correct DWARF DW_TAG_typedef should have no children. */
18512
18513 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18514 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18515 "- DIE at %s [in module %s]"),
18516 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18517
18518 /* If we're at the second level, and we're an enumerator, and
18519 our parent has no specification (meaning possibly lives in a
18520 namespace elsewhere), then we can add the partial symbol now
18521 instead of queueing it. */
18522 if (pdi.tag == DW_TAG_enumerator
18523 && parent_die != NULL
18524 && parent_die->die_parent == NULL
18525 && parent_die->tag == DW_TAG_enumeration_type
18526 && parent_die->has_specification == 0)
18527 {
18528 if (pdi.name == NULL)
18529 complaint (_("malformed enumerator DIE ignored"));
18530 else if (building_psymtab)
18531 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18532 VAR_DOMAIN, LOC_CONST, -1,
18533 cu->language == language_cplus
18534 ? psymbol_placement::GLOBAL
18535 : psymbol_placement::STATIC,
18536 0, cu->language, objfile);
18537
18538 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18539 continue;
18540 }
18541
18542 struct partial_die_info *part_die
18543 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18544
18545 /* We'll save this DIE so link it in. */
18546 part_die->die_parent = parent_die;
18547 part_die->die_sibling = NULL;
18548 part_die->die_child = NULL;
18549
18550 if (last_die && last_die == parent_die)
18551 last_die->die_child = part_die;
18552 else if (last_die)
18553 last_die->die_sibling = part_die;
18554
18555 last_die = part_die;
18556
18557 if (first_die == NULL)
18558 first_die = part_die;
18559
18560 /* Maybe add the DIE to the hash table. Not all DIEs that we
18561 find interesting need to be in the hash table, because we
18562 also have the parent/sibling/child chains; only those that we
18563 might refer to by offset later during partial symbol reading.
18564
18565 For now this means things that might have be the target of a
18566 DW_AT_specification, DW_AT_abstract_origin, or
18567 DW_AT_extension. DW_AT_extension will refer only to
18568 namespaces; DW_AT_abstract_origin refers to functions (and
18569 many things under the function DIE, but we do not recurse
18570 into function DIEs during partial symbol reading) and
18571 possibly variables as well; DW_AT_specification refers to
18572 declarations. Declarations ought to have the DW_AT_declaration
18573 flag. It happens that GCC forgets to put it in sometimes, but
18574 only for functions, not for types.
18575
18576 Adding more things than necessary to the hash table is harmless
18577 except for the performance cost. Adding too few will result in
18578 wasted time in find_partial_die, when we reread the compilation
18579 unit with load_all_dies set. */
18580
18581 if (load_all
18582 || abbrev->tag == DW_TAG_constant
18583 || abbrev->tag == DW_TAG_subprogram
18584 || abbrev->tag == DW_TAG_variable
18585 || abbrev->tag == DW_TAG_namespace
18586 || part_die->is_declaration)
18587 {
18588 void **slot;
18589
18590 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18591 to_underlying (part_die->sect_off),
18592 INSERT);
18593 *slot = part_die;
18594 }
18595
18596 /* For some DIEs we want to follow their children (if any). For C
18597 we have no reason to follow the children of structures; for other
18598 languages we have to, so that we can get at method physnames
18599 to infer fully qualified class names, for DW_AT_specification,
18600 and for C++ template arguments. For C++, we also look one level
18601 inside functions to find template arguments (if the name of the
18602 function does not already contain the template arguments).
18603
18604 For Ada, we need to scan the children of subprograms and lexical
18605 blocks as well because Ada allows the definition of nested
18606 entities that could be interesting for the debugger, such as
18607 nested subprograms for instance. */
18608 if (last_die->has_children
18609 && (load_all
18610 || last_die->tag == DW_TAG_namespace
18611 || last_die->tag == DW_TAG_module
18612 || last_die->tag == DW_TAG_enumeration_type
18613 || (cu->language == language_cplus
18614 && last_die->tag == DW_TAG_subprogram
18615 && (last_die->name == NULL
18616 || strchr (last_die->name, '<') == NULL))
18617 || (cu->language != language_c
18618 && (last_die->tag == DW_TAG_class_type
18619 || last_die->tag == DW_TAG_interface_type
18620 || last_die->tag == DW_TAG_structure_type
18621 || last_die->tag == DW_TAG_union_type))
18622 || (cu->language == language_ada
18623 && (last_die->tag == DW_TAG_subprogram
18624 || last_die->tag == DW_TAG_lexical_block))))
18625 {
18626 nesting_level++;
18627 parent_die = last_die;
18628 continue;
18629 }
18630
18631 /* Otherwise we skip to the next sibling, if any. */
18632 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18633
18634 /* Back to the top, do it again. */
18635 }
18636 }
18637
18638 partial_die_info::partial_die_info (sect_offset sect_off_,
18639 struct abbrev_info *abbrev)
18640 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18641 {
18642 }
18643
18644 /* Read a minimal amount of information into the minimal die structure.
18645 INFO_PTR should point just after the initial uleb128 of a DIE. */
18646
18647 const gdb_byte *
18648 partial_die_info::read (const struct die_reader_specs *reader,
18649 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18650 {
18651 struct dwarf2_cu *cu = reader->cu;
18652 struct dwarf2_per_objfile *dwarf2_per_objfile
18653 = cu->per_cu->dwarf2_per_objfile;
18654 unsigned int i;
18655 int has_low_pc_attr = 0;
18656 int has_high_pc_attr = 0;
18657 int high_pc_relative = 0;
18658
18659 for (i = 0; i < abbrev.num_attrs; ++i)
18660 {
18661 struct attribute attr;
18662
18663 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18664
18665 /* Store the data if it is of an attribute we want to keep in a
18666 partial symbol table. */
18667 switch (attr.name)
18668 {
18669 case DW_AT_name:
18670 switch (tag)
18671 {
18672 case DW_TAG_compile_unit:
18673 case DW_TAG_partial_unit:
18674 case DW_TAG_type_unit:
18675 /* Compilation units have a DW_AT_name that is a filename, not
18676 a source language identifier. */
18677 case DW_TAG_enumeration_type:
18678 case DW_TAG_enumerator:
18679 /* These tags always have simple identifiers already; no need
18680 to canonicalize them. */
18681 name = DW_STRING (&attr);
18682 break;
18683 default:
18684 {
18685 struct objfile *objfile = dwarf2_per_objfile->objfile;
18686
18687 name
18688 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18689 &objfile->per_bfd->storage_obstack);
18690 }
18691 break;
18692 }
18693 break;
18694 case DW_AT_linkage_name:
18695 case DW_AT_MIPS_linkage_name:
18696 /* Note that both forms of linkage name might appear. We
18697 assume they will be the same, and we only store the last
18698 one we see. */
18699 linkage_name = DW_STRING (&attr);
18700 break;
18701 case DW_AT_low_pc:
18702 has_low_pc_attr = 1;
18703 lowpc = attr_value_as_address (&attr);
18704 break;
18705 case DW_AT_high_pc:
18706 has_high_pc_attr = 1;
18707 highpc = attr_value_as_address (&attr);
18708 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18709 high_pc_relative = 1;
18710 break;
18711 case DW_AT_location:
18712 /* Support the .debug_loc offsets. */
18713 if (attr_form_is_block (&attr))
18714 {
18715 d.locdesc = DW_BLOCK (&attr);
18716 }
18717 else if (attr_form_is_section_offset (&attr))
18718 {
18719 dwarf2_complex_location_expr_complaint ();
18720 }
18721 else
18722 {
18723 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18724 "partial symbol information");
18725 }
18726 break;
18727 case DW_AT_external:
18728 is_external = DW_UNSND (&attr);
18729 break;
18730 case DW_AT_declaration:
18731 is_declaration = DW_UNSND (&attr);
18732 break;
18733 case DW_AT_type:
18734 has_type = 1;
18735 break;
18736 case DW_AT_abstract_origin:
18737 case DW_AT_specification:
18738 case DW_AT_extension:
18739 has_specification = 1;
18740 spec_offset = dwarf2_get_ref_die_offset (&attr);
18741 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18742 || cu->per_cu->is_dwz);
18743 break;
18744 case DW_AT_sibling:
18745 /* Ignore absolute siblings, they might point outside of
18746 the current compile unit. */
18747 if (attr.form == DW_FORM_ref_addr)
18748 complaint (_("ignoring absolute DW_AT_sibling"));
18749 else
18750 {
18751 const gdb_byte *buffer = reader->buffer;
18752 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18753 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18754
18755 if (sibling_ptr < info_ptr)
18756 complaint (_("DW_AT_sibling points backwards"));
18757 else if (sibling_ptr > reader->buffer_end)
18758 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18759 else
18760 sibling = sibling_ptr;
18761 }
18762 break;
18763 case DW_AT_byte_size:
18764 has_byte_size = 1;
18765 break;
18766 case DW_AT_const_value:
18767 has_const_value = 1;
18768 break;
18769 case DW_AT_calling_convention:
18770 /* DWARF doesn't provide a way to identify a program's source-level
18771 entry point. DW_AT_calling_convention attributes are only meant
18772 to describe functions' calling conventions.
18773
18774 However, because it's a necessary piece of information in
18775 Fortran, and before DWARF 4 DW_CC_program was the only
18776 piece of debugging information whose definition refers to
18777 a 'main program' at all, several compilers marked Fortran
18778 main programs with DW_CC_program --- even when those
18779 functions use the standard calling conventions.
18780
18781 Although DWARF now specifies a way to provide this
18782 information, we support this practice for backward
18783 compatibility. */
18784 if (DW_UNSND (&attr) == DW_CC_program
18785 && cu->language == language_fortran)
18786 main_subprogram = 1;
18787 break;
18788 case DW_AT_inline:
18789 if (DW_UNSND (&attr) == DW_INL_inlined
18790 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18791 may_be_inlined = 1;
18792 break;
18793
18794 case DW_AT_import:
18795 if (tag == DW_TAG_imported_unit)
18796 {
18797 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18798 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18799 || cu->per_cu->is_dwz);
18800 }
18801 break;
18802
18803 case DW_AT_main_subprogram:
18804 main_subprogram = DW_UNSND (&attr);
18805 break;
18806
18807 case DW_AT_ranges:
18808 {
18809 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18810 but that requires a full DIE, so instead we just
18811 reimplement it. */
18812 int need_ranges_base = tag != DW_TAG_compile_unit;
18813 unsigned int ranges_offset = (DW_UNSND (&attr)
18814 + (need_ranges_base
18815 ? cu->ranges_base
18816 : 0));
18817
18818 /* Value of the DW_AT_ranges attribute is the offset in the
18819 .debug_ranges section. */
18820 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18821 nullptr))
18822 has_pc_info = 1;
18823 }
18824 break;
18825
18826 default:
18827 break;
18828 }
18829 }
18830
18831 /* For Ada, if both the name and the linkage name appear, we prefer
18832 the latter. This lets "catch exception" work better, regardless
18833 of the order in which the name and linkage name were emitted.
18834 Really, though, this is just a workaround for the fact that gdb
18835 doesn't store both the name and the linkage name. */
18836 if (cu->language == language_ada && linkage_name != nullptr)
18837 name = linkage_name;
18838
18839 if (high_pc_relative)
18840 highpc += lowpc;
18841
18842 if (has_low_pc_attr && has_high_pc_attr)
18843 {
18844 /* When using the GNU linker, .gnu.linkonce. sections are used to
18845 eliminate duplicate copies of functions and vtables and such.
18846 The linker will arbitrarily choose one and discard the others.
18847 The AT_*_pc values for such functions refer to local labels in
18848 these sections. If the section from that file was discarded, the
18849 labels are not in the output, so the relocs get a value of 0.
18850 If this is a discarded function, mark the pc bounds as invalid,
18851 so that GDB will ignore it. */
18852 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18853 {
18854 struct objfile *objfile = dwarf2_per_objfile->objfile;
18855 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18856
18857 complaint (_("DW_AT_low_pc %s is zero "
18858 "for DIE at %s [in module %s]"),
18859 paddress (gdbarch, lowpc),
18860 sect_offset_str (sect_off),
18861 objfile_name (objfile));
18862 }
18863 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18864 else if (lowpc >= highpc)
18865 {
18866 struct objfile *objfile = dwarf2_per_objfile->objfile;
18867 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18868
18869 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18870 "for DIE at %s [in module %s]"),
18871 paddress (gdbarch, lowpc),
18872 paddress (gdbarch, highpc),
18873 sect_offset_str (sect_off),
18874 objfile_name (objfile));
18875 }
18876 else
18877 has_pc_info = 1;
18878 }
18879
18880 return info_ptr;
18881 }
18882
18883 /* Find a cached partial DIE at OFFSET in CU. */
18884
18885 struct partial_die_info *
18886 dwarf2_cu::find_partial_die (sect_offset sect_off)
18887 {
18888 struct partial_die_info *lookup_die = NULL;
18889 struct partial_die_info part_die (sect_off);
18890
18891 lookup_die = ((struct partial_die_info *)
18892 htab_find_with_hash (partial_dies, &part_die,
18893 to_underlying (sect_off)));
18894
18895 return lookup_die;
18896 }
18897
18898 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18899 except in the case of .debug_types DIEs which do not reference
18900 outside their CU (they do however referencing other types via
18901 DW_FORM_ref_sig8). */
18902
18903 static const struct cu_partial_die_info
18904 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18905 {
18906 struct dwarf2_per_objfile *dwarf2_per_objfile
18907 = cu->per_cu->dwarf2_per_objfile;
18908 struct objfile *objfile = dwarf2_per_objfile->objfile;
18909 struct dwarf2_per_cu_data *per_cu = NULL;
18910 struct partial_die_info *pd = NULL;
18911
18912 if (offset_in_dwz == cu->per_cu->is_dwz
18913 && offset_in_cu_p (&cu->header, sect_off))
18914 {
18915 pd = cu->find_partial_die (sect_off);
18916 if (pd != NULL)
18917 return { cu, pd };
18918 /* We missed recording what we needed.
18919 Load all dies and try again. */
18920 per_cu = cu->per_cu;
18921 }
18922 else
18923 {
18924 /* TUs don't reference other CUs/TUs (except via type signatures). */
18925 if (cu->per_cu->is_debug_types)
18926 {
18927 error (_("Dwarf Error: Type Unit at offset %s contains"
18928 " external reference to offset %s [in module %s].\n"),
18929 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18930 bfd_get_filename (objfile->obfd));
18931 }
18932 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18933 dwarf2_per_objfile);
18934
18935 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18936 load_partial_comp_unit (per_cu);
18937
18938 per_cu->cu->last_used = 0;
18939 pd = per_cu->cu->find_partial_die (sect_off);
18940 }
18941
18942 /* If we didn't find it, and not all dies have been loaded,
18943 load them all and try again. */
18944
18945 if (pd == NULL && per_cu->load_all_dies == 0)
18946 {
18947 per_cu->load_all_dies = 1;
18948
18949 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18950 THIS_CU->cu may already be in use. So we can't just free it and
18951 replace its DIEs with the ones we read in. Instead, we leave those
18952 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18953 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18954 set. */
18955 load_partial_comp_unit (per_cu);
18956
18957 pd = per_cu->cu->find_partial_die (sect_off);
18958 }
18959
18960 if (pd == NULL)
18961 internal_error (__FILE__, __LINE__,
18962 _("could not find partial DIE %s "
18963 "in cache [from module %s]\n"),
18964 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18965 return { per_cu->cu, pd };
18966 }
18967
18968 /* See if we can figure out if the class lives in a namespace. We do
18969 this by looking for a member function; its demangled name will
18970 contain namespace info, if there is any. */
18971
18972 static void
18973 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18974 struct dwarf2_cu *cu)
18975 {
18976 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18977 what template types look like, because the demangler
18978 frequently doesn't give the same name as the debug info. We
18979 could fix this by only using the demangled name to get the
18980 prefix (but see comment in read_structure_type). */
18981
18982 struct partial_die_info *real_pdi;
18983 struct partial_die_info *child_pdi;
18984
18985 /* If this DIE (this DIE's specification, if any) has a parent, then
18986 we should not do this. We'll prepend the parent's fully qualified
18987 name when we create the partial symbol. */
18988
18989 real_pdi = struct_pdi;
18990 while (real_pdi->has_specification)
18991 {
18992 auto res = find_partial_die (real_pdi->spec_offset,
18993 real_pdi->spec_is_dwz, cu);
18994 real_pdi = res.pdi;
18995 cu = res.cu;
18996 }
18997
18998 if (real_pdi->die_parent != NULL)
18999 return;
19000
19001 for (child_pdi = struct_pdi->die_child;
19002 child_pdi != NULL;
19003 child_pdi = child_pdi->die_sibling)
19004 {
19005 if (child_pdi->tag == DW_TAG_subprogram
19006 && child_pdi->linkage_name != NULL)
19007 {
19008 char *actual_class_name
19009 = language_class_name_from_physname (cu->language_defn,
19010 child_pdi->linkage_name);
19011 if (actual_class_name != NULL)
19012 {
19013 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19014 struct_pdi->name
19015 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19016 actual_class_name);
19017 xfree (actual_class_name);
19018 }
19019 break;
19020 }
19021 }
19022 }
19023
19024 void
19025 partial_die_info::fixup (struct dwarf2_cu *cu)
19026 {
19027 /* Once we've fixed up a die, there's no point in doing so again.
19028 This also avoids a memory leak if we were to call
19029 guess_partial_die_structure_name multiple times. */
19030 if (fixup_called)
19031 return;
19032
19033 /* If we found a reference attribute and the DIE has no name, try
19034 to find a name in the referred to DIE. */
19035
19036 if (name == NULL && has_specification)
19037 {
19038 struct partial_die_info *spec_die;
19039
19040 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19041 spec_die = res.pdi;
19042 cu = res.cu;
19043
19044 spec_die->fixup (cu);
19045
19046 if (spec_die->name)
19047 {
19048 name = spec_die->name;
19049
19050 /* Copy DW_AT_external attribute if it is set. */
19051 if (spec_die->is_external)
19052 is_external = spec_die->is_external;
19053 }
19054 }
19055
19056 /* Set default names for some unnamed DIEs. */
19057
19058 if (name == NULL && tag == DW_TAG_namespace)
19059 name = CP_ANONYMOUS_NAMESPACE_STR;
19060
19061 /* If there is no parent die to provide a namespace, and there are
19062 children, see if we can determine the namespace from their linkage
19063 name. */
19064 if (cu->language == language_cplus
19065 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
19066 && die_parent == NULL
19067 && has_children
19068 && (tag == DW_TAG_class_type
19069 || tag == DW_TAG_structure_type
19070 || tag == DW_TAG_union_type))
19071 guess_partial_die_structure_name (this, cu);
19072
19073 /* GCC might emit a nameless struct or union that has a linkage
19074 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19075 if (name == NULL
19076 && (tag == DW_TAG_class_type
19077 || tag == DW_TAG_interface_type
19078 || tag == DW_TAG_structure_type
19079 || tag == DW_TAG_union_type)
19080 && linkage_name != NULL)
19081 {
19082 char *demangled;
19083
19084 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
19085 if (demangled)
19086 {
19087 const char *base;
19088
19089 /* Strip any leading namespaces/classes, keep only the base name.
19090 DW_AT_name for named DIEs does not contain the prefixes. */
19091 base = strrchr (demangled, ':');
19092 if (base && base > demangled && base[-1] == ':')
19093 base++;
19094 else
19095 base = demangled;
19096
19097 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19098 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
19099 xfree (demangled);
19100 }
19101 }
19102
19103 fixup_called = 1;
19104 }
19105
19106 /* Read an attribute value described by an attribute form. */
19107
19108 static const gdb_byte *
19109 read_attribute_value (const struct die_reader_specs *reader,
19110 struct attribute *attr, unsigned form,
19111 LONGEST implicit_const, const gdb_byte *info_ptr)
19112 {
19113 struct dwarf2_cu *cu = reader->cu;
19114 struct dwarf2_per_objfile *dwarf2_per_objfile
19115 = cu->per_cu->dwarf2_per_objfile;
19116 struct objfile *objfile = dwarf2_per_objfile->objfile;
19117 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19118 bfd *abfd = reader->abfd;
19119 struct comp_unit_head *cu_header = &cu->header;
19120 unsigned int bytes_read;
19121 struct dwarf_block *blk;
19122
19123 attr->form = (enum dwarf_form) form;
19124 switch (form)
19125 {
19126 case DW_FORM_ref_addr:
19127 if (cu->header.version == 2)
19128 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19129 else
19130 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19131 &cu->header, &bytes_read);
19132 info_ptr += bytes_read;
19133 break;
19134 case DW_FORM_GNU_ref_alt:
19135 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19136 info_ptr += bytes_read;
19137 break;
19138 case DW_FORM_addr:
19139 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19140 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19141 info_ptr += bytes_read;
19142 break;
19143 case DW_FORM_block2:
19144 blk = dwarf_alloc_block (cu);
19145 blk->size = read_2_bytes (abfd, info_ptr);
19146 info_ptr += 2;
19147 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19148 info_ptr += blk->size;
19149 DW_BLOCK (attr) = blk;
19150 break;
19151 case DW_FORM_block4:
19152 blk = dwarf_alloc_block (cu);
19153 blk->size = read_4_bytes (abfd, info_ptr);
19154 info_ptr += 4;
19155 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19156 info_ptr += blk->size;
19157 DW_BLOCK (attr) = blk;
19158 break;
19159 case DW_FORM_data2:
19160 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19161 info_ptr += 2;
19162 break;
19163 case DW_FORM_data4:
19164 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19165 info_ptr += 4;
19166 break;
19167 case DW_FORM_data8:
19168 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19169 info_ptr += 8;
19170 break;
19171 case DW_FORM_data16:
19172 blk = dwarf_alloc_block (cu);
19173 blk->size = 16;
19174 blk->data = read_n_bytes (abfd, info_ptr, 16);
19175 info_ptr += 16;
19176 DW_BLOCK (attr) = blk;
19177 break;
19178 case DW_FORM_sec_offset:
19179 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19180 info_ptr += bytes_read;
19181 break;
19182 case DW_FORM_string:
19183 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19184 DW_STRING_IS_CANONICAL (attr) = 0;
19185 info_ptr += bytes_read;
19186 break;
19187 case DW_FORM_strp:
19188 if (!cu->per_cu->is_dwz)
19189 {
19190 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19191 abfd, info_ptr, cu_header,
19192 &bytes_read);
19193 DW_STRING_IS_CANONICAL (attr) = 0;
19194 info_ptr += bytes_read;
19195 break;
19196 }
19197 /* FALLTHROUGH */
19198 case DW_FORM_line_strp:
19199 if (!cu->per_cu->is_dwz)
19200 {
19201 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19202 abfd, info_ptr,
19203 cu_header, &bytes_read);
19204 DW_STRING_IS_CANONICAL (attr) = 0;
19205 info_ptr += bytes_read;
19206 break;
19207 }
19208 /* FALLTHROUGH */
19209 case DW_FORM_GNU_strp_alt:
19210 {
19211 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19212 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19213 &bytes_read);
19214
19215 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19216 dwz, str_offset);
19217 DW_STRING_IS_CANONICAL (attr) = 0;
19218 info_ptr += bytes_read;
19219 }
19220 break;
19221 case DW_FORM_exprloc:
19222 case DW_FORM_block:
19223 blk = dwarf_alloc_block (cu);
19224 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19225 info_ptr += bytes_read;
19226 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19227 info_ptr += blk->size;
19228 DW_BLOCK (attr) = blk;
19229 break;
19230 case DW_FORM_block1:
19231 blk = dwarf_alloc_block (cu);
19232 blk->size = read_1_byte (abfd, info_ptr);
19233 info_ptr += 1;
19234 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19235 info_ptr += blk->size;
19236 DW_BLOCK (attr) = blk;
19237 break;
19238 case DW_FORM_data1:
19239 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19240 info_ptr += 1;
19241 break;
19242 case DW_FORM_flag:
19243 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19244 info_ptr += 1;
19245 break;
19246 case DW_FORM_flag_present:
19247 DW_UNSND (attr) = 1;
19248 break;
19249 case DW_FORM_sdata:
19250 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19251 info_ptr += bytes_read;
19252 break;
19253 case DW_FORM_udata:
19254 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19255 info_ptr += bytes_read;
19256 break;
19257 case DW_FORM_ref1:
19258 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19259 + read_1_byte (abfd, info_ptr));
19260 info_ptr += 1;
19261 break;
19262 case DW_FORM_ref2:
19263 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19264 + read_2_bytes (abfd, info_ptr));
19265 info_ptr += 2;
19266 break;
19267 case DW_FORM_ref4:
19268 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19269 + read_4_bytes (abfd, info_ptr));
19270 info_ptr += 4;
19271 break;
19272 case DW_FORM_ref8:
19273 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19274 + read_8_bytes (abfd, info_ptr));
19275 info_ptr += 8;
19276 break;
19277 case DW_FORM_ref_sig8:
19278 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19279 info_ptr += 8;
19280 break;
19281 case DW_FORM_ref_udata:
19282 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19283 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19284 info_ptr += bytes_read;
19285 break;
19286 case DW_FORM_indirect:
19287 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19288 info_ptr += bytes_read;
19289 if (form == DW_FORM_implicit_const)
19290 {
19291 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19292 info_ptr += bytes_read;
19293 }
19294 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19295 info_ptr);
19296 break;
19297 case DW_FORM_implicit_const:
19298 DW_SND (attr) = implicit_const;
19299 break;
19300 case DW_FORM_addrx:
19301 case DW_FORM_GNU_addr_index:
19302 if (reader->dwo_file == NULL)
19303 {
19304 /* For now flag a hard error.
19305 Later we can turn this into a complaint. */
19306 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19307 dwarf_form_name (form),
19308 bfd_get_filename (abfd));
19309 }
19310 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19311 info_ptr += bytes_read;
19312 break;
19313 case DW_FORM_strx:
19314 case DW_FORM_strx1:
19315 case DW_FORM_strx2:
19316 case DW_FORM_strx3:
19317 case DW_FORM_strx4:
19318 case DW_FORM_GNU_str_index:
19319 if (reader->dwo_file == NULL)
19320 {
19321 /* For now flag a hard error.
19322 Later we can turn this into a complaint if warranted. */
19323 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19324 dwarf_form_name (form),
19325 bfd_get_filename (abfd));
19326 }
19327 {
19328 ULONGEST str_index;
19329 if (form == DW_FORM_strx1)
19330 {
19331 str_index = read_1_byte (abfd, info_ptr);
19332 info_ptr += 1;
19333 }
19334 else if (form == DW_FORM_strx2)
19335 {
19336 str_index = read_2_bytes (abfd, info_ptr);
19337 info_ptr += 2;
19338 }
19339 else if (form == DW_FORM_strx3)
19340 {
19341 str_index = read_3_bytes (abfd, info_ptr);
19342 info_ptr += 3;
19343 }
19344 else if (form == DW_FORM_strx4)
19345 {
19346 str_index = read_4_bytes (abfd, info_ptr);
19347 info_ptr += 4;
19348 }
19349 else
19350 {
19351 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19352 info_ptr += bytes_read;
19353 }
19354 DW_STRING (attr) = read_str_index (reader, str_index);
19355 DW_STRING_IS_CANONICAL (attr) = 0;
19356 }
19357 break;
19358 default:
19359 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19360 dwarf_form_name (form),
19361 bfd_get_filename (abfd));
19362 }
19363
19364 /* Super hack. */
19365 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19366 attr->form = DW_FORM_GNU_ref_alt;
19367
19368 /* We have seen instances where the compiler tried to emit a byte
19369 size attribute of -1 which ended up being encoded as an unsigned
19370 0xffffffff. Although 0xffffffff is technically a valid size value,
19371 an object of this size seems pretty unlikely so we can relatively
19372 safely treat these cases as if the size attribute was invalid and
19373 treat them as zero by default. */
19374 if (attr->name == DW_AT_byte_size
19375 && form == DW_FORM_data4
19376 && DW_UNSND (attr) >= 0xffffffff)
19377 {
19378 complaint
19379 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19380 hex_string (DW_UNSND (attr)));
19381 DW_UNSND (attr) = 0;
19382 }
19383
19384 return info_ptr;
19385 }
19386
19387 /* Read an attribute described by an abbreviated attribute. */
19388
19389 static const gdb_byte *
19390 read_attribute (const struct die_reader_specs *reader,
19391 struct attribute *attr, struct attr_abbrev *abbrev,
19392 const gdb_byte *info_ptr)
19393 {
19394 attr->name = abbrev->name;
19395 return read_attribute_value (reader, attr, abbrev->form,
19396 abbrev->implicit_const, info_ptr);
19397 }
19398
19399 /* Read dwarf information from a buffer. */
19400
19401 static unsigned int
19402 read_1_byte (bfd *abfd, const gdb_byte *buf)
19403 {
19404 return bfd_get_8 (abfd, buf);
19405 }
19406
19407 static int
19408 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19409 {
19410 return bfd_get_signed_8 (abfd, buf);
19411 }
19412
19413 static unsigned int
19414 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19415 {
19416 return bfd_get_16 (abfd, buf);
19417 }
19418
19419 static int
19420 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19421 {
19422 return bfd_get_signed_16 (abfd, buf);
19423 }
19424
19425 static unsigned int
19426 read_3_bytes (bfd *abfd, const gdb_byte *buf)
19427 {
19428 unsigned int result = 0;
19429 for (int i = 0; i < 3; ++i)
19430 {
19431 unsigned char byte = bfd_get_8 (abfd, buf);
19432 buf++;
19433 result |= ((unsigned int) byte << (i * 8));
19434 }
19435 return result;
19436 }
19437
19438 static unsigned int
19439 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19440 {
19441 return bfd_get_32 (abfd, buf);
19442 }
19443
19444 static int
19445 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19446 {
19447 return bfd_get_signed_32 (abfd, buf);
19448 }
19449
19450 static ULONGEST
19451 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19452 {
19453 return bfd_get_64 (abfd, buf);
19454 }
19455
19456 static CORE_ADDR
19457 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19458 unsigned int *bytes_read)
19459 {
19460 struct comp_unit_head *cu_header = &cu->header;
19461 CORE_ADDR retval = 0;
19462
19463 if (cu_header->signed_addr_p)
19464 {
19465 switch (cu_header->addr_size)
19466 {
19467 case 2:
19468 retval = bfd_get_signed_16 (abfd, buf);
19469 break;
19470 case 4:
19471 retval = bfd_get_signed_32 (abfd, buf);
19472 break;
19473 case 8:
19474 retval = bfd_get_signed_64 (abfd, buf);
19475 break;
19476 default:
19477 internal_error (__FILE__, __LINE__,
19478 _("read_address: bad switch, signed [in module %s]"),
19479 bfd_get_filename (abfd));
19480 }
19481 }
19482 else
19483 {
19484 switch (cu_header->addr_size)
19485 {
19486 case 2:
19487 retval = bfd_get_16 (abfd, buf);
19488 break;
19489 case 4:
19490 retval = bfd_get_32 (abfd, buf);
19491 break;
19492 case 8:
19493 retval = bfd_get_64 (abfd, buf);
19494 break;
19495 default:
19496 internal_error (__FILE__, __LINE__,
19497 _("read_address: bad switch, "
19498 "unsigned [in module %s]"),
19499 bfd_get_filename (abfd));
19500 }
19501 }
19502
19503 *bytes_read = cu_header->addr_size;
19504 return retval;
19505 }
19506
19507 /* Read the initial length from a section. The (draft) DWARF 3
19508 specification allows the initial length to take up either 4 bytes
19509 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19510 bytes describe the length and all offsets will be 8 bytes in length
19511 instead of 4.
19512
19513 An older, non-standard 64-bit format is also handled by this
19514 function. The older format in question stores the initial length
19515 as an 8-byte quantity without an escape value. Lengths greater
19516 than 2^32 aren't very common which means that the initial 4 bytes
19517 is almost always zero. Since a length value of zero doesn't make
19518 sense for the 32-bit format, this initial zero can be considered to
19519 be an escape value which indicates the presence of the older 64-bit
19520 format. As written, the code can't detect (old format) lengths
19521 greater than 4GB. If it becomes necessary to handle lengths
19522 somewhat larger than 4GB, we could allow other small values (such
19523 as the non-sensical values of 1, 2, and 3) to also be used as
19524 escape values indicating the presence of the old format.
19525
19526 The value returned via bytes_read should be used to increment the
19527 relevant pointer after calling read_initial_length().
19528
19529 [ Note: read_initial_length() and read_offset() are based on the
19530 document entitled "DWARF Debugging Information Format", revision
19531 3, draft 8, dated November 19, 2001. This document was obtained
19532 from:
19533
19534 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19535
19536 This document is only a draft and is subject to change. (So beware.)
19537
19538 Details regarding the older, non-standard 64-bit format were
19539 determined empirically by examining 64-bit ELF files produced by
19540 the SGI toolchain on an IRIX 6.5 machine.
19541
19542 - Kevin, July 16, 2002
19543 ] */
19544
19545 static LONGEST
19546 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19547 {
19548 LONGEST length = bfd_get_32 (abfd, buf);
19549
19550 if (length == 0xffffffff)
19551 {
19552 length = bfd_get_64 (abfd, buf + 4);
19553 *bytes_read = 12;
19554 }
19555 else if (length == 0)
19556 {
19557 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19558 length = bfd_get_64 (abfd, buf);
19559 *bytes_read = 8;
19560 }
19561 else
19562 {
19563 *bytes_read = 4;
19564 }
19565
19566 return length;
19567 }
19568
19569 /* Cover function for read_initial_length.
19570 Returns the length of the object at BUF, and stores the size of the
19571 initial length in *BYTES_READ and stores the size that offsets will be in
19572 *OFFSET_SIZE.
19573 If the initial length size is not equivalent to that specified in
19574 CU_HEADER then issue a complaint.
19575 This is useful when reading non-comp-unit headers. */
19576
19577 static LONGEST
19578 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19579 const struct comp_unit_head *cu_header,
19580 unsigned int *bytes_read,
19581 unsigned int *offset_size)
19582 {
19583 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19584
19585 gdb_assert (cu_header->initial_length_size == 4
19586 || cu_header->initial_length_size == 8
19587 || cu_header->initial_length_size == 12);
19588
19589 if (cu_header->initial_length_size != *bytes_read)
19590 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19591
19592 *offset_size = (*bytes_read == 4) ? 4 : 8;
19593 return length;
19594 }
19595
19596 /* Read an offset from the data stream. The size of the offset is
19597 given by cu_header->offset_size. */
19598
19599 static LONGEST
19600 read_offset (bfd *abfd, const gdb_byte *buf,
19601 const struct comp_unit_head *cu_header,
19602 unsigned int *bytes_read)
19603 {
19604 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19605
19606 *bytes_read = cu_header->offset_size;
19607 return offset;
19608 }
19609
19610 /* Read an offset from the data stream. */
19611
19612 static LONGEST
19613 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19614 {
19615 LONGEST retval = 0;
19616
19617 switch (offset_size)
19618 {
19619 case 4:
19620 retval = bfd_get_32 (abfd, buf);
19621 break;
19622 case 8:
19623 retval = bfd_get_64 (abfd, buf);
19624 break;
19625 default:
19626 internal_error (__FILE__, __LINE__,
19627 _("read_offset_1: bad switch [in module %s]"),
19628 bfd_get_filename (abfd));
19629 }
19630
19631 return retval;
19632 }
19633
19634 static const gdb_byte *
19635 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19636 {
19637 /* If the size of a host char is 8 bits, we can return a pointer
19638 to the buffer, otherwise we have to copy the data to a buffer
19639 allocated on the temporary obstack. */
19640 gdb_assert (HOST_CHAR_BIT == 8);
19641 return buf;
19642 }
19643
19644 static const char *
19645 read_direct_string (bfd *abfd, const gdb_byte *buf,
19646 unsigned int *bytes_read_ptr)
19647 {
19648 /* If the size of a host char is 8 bits, we can return a pointer
19649 to the string, otherwise we have to copy the string to a buffer
19650 allocated on the temporary obstack. */
19651 gdb_assert (HOST_CHAR_BIT == 8);
19652 if (*buf == '\0')
19653 {
19654 *bytes_read_ptr = 1;
19655 return NULL;
19656 }
19657 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19658 return (const char *) buf;
19659 }
19660
19661 /* Return pointer to string at section SECT offset STR_OFFSET with error
19662 reporting strings FORM_NAME and SECT_NAME. */
19663
19664 static const char *
19665 read_indirect_string_at_offset_from (struct objfile *objfile,
19666 bfd *abfd, LONGEST str_offset,
19667 struct dwarf2_section_info *sect,
19668 const char *form_name,
19669 const char *sect_name)
19670 {
19671 dwarf2_read_section (objfile, sect);
19672 if (sect->buffer == NULL)
19673 error (_("%s used without %s section [in module %s]"),
19674 form_name, sect_name, bfd_get_filename (abfd));
19675 if (str_offset >= sect->size)
19676 error (_("%s pointing outside of %s section [in module %s]"),
19677 form_name, sect_name, bfd_get_filename (abfd));
19678 gdb_assert (HOST_CHAR_BIT == 8);
19679 if (sect->buffer[str_offset] == '\0')
19680 return NULL;
19681 return (const char *) (sect->buffer + str_offset);
19682 }
19683
19684 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19685
19686 static const char *
19687 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19688 bfd *abfd, LONGEST str_offset)
19689 {
19690 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19691 abfd, str_offset,
19692 &dwarf2_per_objfile->str,
19693 "DW_FORM_strp", ".debug_str");
19694 }
19695
19696 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19697
19698 static const char *
19699 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19700 bfd *abfd, LONGEST str_offset)
19701 {
19702 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19703 abfd, str_offset,
19704 &dwarf2_per_objfile->line_str,
19705 "DW_FORM_line_strp",
19706 ".debug_line_str");
19707 }
19708
19709 /* Read a string at offset STR_OFFSET in the .debug_str section from
19710 the .dwz file DWZ. Throw an error if the offset is too large. If
19711 the string consists of a single NUL byte, return NULL; otherwise
19712 return a pointer to the string. */
19713
19714 static const char *
19715 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19716 LONGEST str_offset)
19717 {
19718 dwarf2_read_section (objfile, &dwz->str);
19719
19720 if (dwz->str.buffer == NULL)
19721 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19722 "section [in module %s]"),
19723 bfd_get_filename (dwz->dwz_bfd));
19724 if (str_offset >= dwz->str.size)
19725 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19726 ".debug_str section [in module %s]"),
19727 bfd_get_filename (dwz->dwz_bfd));
19728 gdb_assert (HOST_CHAR_BIT == 8);
19729 if (dwz->str.buffer[str_offset] == '\0')
19730 return NULL;
19731 return (const char *) (dwz->str.buffer + str_offset);
19732 }
19733
19734 /* Return pointer to string at .debug_str offset as read from BUF.
19735 BUF is assumed to be in a compilation unit described by CU_HEADER.
19736 Return *BYTES_READ_PTR count of bytes read from BUF. */
19737
19738 static const char *
19739 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19740 const gdb_byte *buf,
19741 const struct comp_unit_head *cu_header,
19742 unsigned int *bytes_read_ptr)
19743 {
19744 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19745
19746 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19747 }
19748
19749 /* Return pointer to string at .debug_line_str offset as read from BUF.
19750 BUF is assumed to be in a compilation unit described by CU_HEADER.
19751 Return *BYTES_READ_PTR count of bytes read from BUF. */
19752
19753 static const char *
19754 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19755 bfd *abfd, const gdb_byte *buf,
19756 const struct comp_unit_head *cu_header,
19757 unsigned int *bytes_read_ptr)
19758 {
19759 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19760
19761 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19762 str_offset);
19763 }
19764
19765 ULONGEST
19766 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19767 unsigned int *bytes_read_ptr)
19768 {
19769 ULONGEST result;
19770 unsigned int num_read;
19771 int shift;
19772 unsigned char byte;
19773
19774 result = 0;
19775 shift = 0;
19776 num_read = 0;
19777 while (1)
19778 {
19779 byte = bfd_get_8 (abfd, buf);
19780 buf++;
19781 num_read++;
19782 result |= ((ULONGEST) (byte & 127) << shift);
19783 if ((byte & 128) == 0)
19784 {
19785 break;
19786 }
19787 shift += 7;
19788 }
19789 *bytes_read_ptr = num_read;
19790 return result;
19791 }
19792
19793 static LONGEST
19794 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19795 unsigned int *bytes_read_ptr)
19796 {
19797 ULONGEST result;
19798 int shift, num_read;
19799 unsigned char byte;
19800
19801 result = 0;
19802 shift = 0;
19803 num_read = 0;
19804 while (1)
19805 {
19806 byte = bfd_get_8 (abfd, buf);
19807 buf++;
19808 num_read++;
19809 result |= ((ULONGEST) (byte & 127) << shift);
19810 shift += 7;
19811 if ((byte & 128) == 0)
19812 {
19813 break;
19814 }
19815 }
19816 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19817 result |= -(((ULONGEST) 1) << shift);
19818 *bytes_read_ptr = num_read;
19819 return result;
19820 }
19821
19822 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19823 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19824 ADDR_SIZE is the size of addresses from the CU header. */
19825
19826 static CORE_ADDR
19827 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19828 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19829 {
19830 struct objfile *objfile = dwarf2_per_objfile->objfile;
19831 bfd *abfd = objfile->obfd;
19832 const gdb_byte *info_ptr;
19833
19834 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19835 if (dwarf2_per_objfile->addr.buffer == NULL)
19836 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19837 objfile_name (objfile));
19838 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19839 error (_("DW_FORM_addr_index pointing outside of "
19840 ".debug_addr section [in module %s]"),
19841 objfile_name (objfile));
19842 info_ptr = (dwarf2_per_objfile->addr.buffer
19843 + addr_base + addr_index * addr_size);
19844 if (addr_size == 4)
19845 return bfd_get_32 (abfd, info_ptr);
19846 else
19847 return bfd_get_64 (abfd, info_ptr);
19848 }
19849
19850 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19851
19852 static CORE_ADDR
19853 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19854 {
19855 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19856 cu->addr_base, cu->header.addr_size);
19857 }
19858
19859 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19860
19861 static CORE_ADDR
19862 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19863 unsigned int *bytes_read)
19864 {
19865 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19866 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19867
19868 return read_addr_index (cu, addr_index);
19869 }
19870
19871 /* Data structure to pass results from dwarf2_read_addr_index_reader
19872 back to dwarf2_read_addr_index. */
19873
19874 struct dwarf2_read_addr_index_data
19875 {
19876 ULONGEST addr_base;
19877 int addr_size;
19878 };
19879
19880 /* die_reader_func for dwarf2_read_addr_index. */
19881
19882 static void
19883 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19884 const gdb_byte *info_ptr,
19885 struct die_info *comp_unit_die,
19886 int has_children,
19887 void *data)
19888 {
19889 struct dwarf2_cu *cu = reader->cu;
19890 struct dwarf2_read_addr_index_data *aidata =
19891 (struct dwarf2_read_addr_index_data *) data;
19892
19893 aidata->addr_base = cu->addr_base;
19894 aidata->addr_size = cu->header.addr_size;
19895 }
19896
19897 /* Given an index in .debug_addr, fetch the value.
19898 NOTE: This can be called during dwarf expression evaluation,
19899 long after the debug information has been read, and thus per_cu->cu
19900 may no longer exist. */
19901
19902 CORE_ADDR
19903 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19904 unsigned int addr_index)
19905 {
19906 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19907 struct dwarf2_cu *cu = per_cu->cu;
19908 ULONGEST addr_base;
19909 int addr_size;
19910
19911 /* We need addr_base and addr_size.
19912 If we don't have PER_CU->cu, we have to get it.
19913 Nasty, but the alternative is storing the needed info in PER_CU,
19914 which at this point doesn't seem justified: it's not clear how frequently
19915 it would get used and it would increase the size of every PER_CU.
19916 Entry points like dwarf2_per_cu_addr_size do a similar thing
19917 so we're not in uncharted territory here.
19918 Alas we need to be a bit more complicated as addr_base is contained
19919 in the DIE.
19920
19921 We don't need to read the entire CU(/TU).
19922 We just need the header and top level die.
19923
19924 IWBN to use the aging mechanism to let us lazily later discard the CU.
19925 For now we skip this optimization. */
19926
19927 if (cu != NULL)
19928 {
19929 addr_base = cu->addr_base;
19930 addr_size = cu->header.addr_size;
19931 }
19932 else
19933 {
19934 struct dwarf2_read_addr_index_data aidata;
19935
19936 /* Note: We can't use init_cutu_and_read_dies_simple here,
19937 we need addr_base. */
19938 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
19939 dwarf2_read_addr_index_reader, &aidata);
19940 addr_base = aidata.addr_base;
19941 addr_size = aidata.addr_size;
19942 }
19943
19944 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19945 addr_size);
19946 }
19947
19948 /* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
19949 This is only used by the Fission support. */
19950
19951 static const char *
19952 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19953 {
19954 struct dwarf2_cu *cu = reader->cu;
19955 struct dwarf2_per_objfile *dwarf2_per_objfile
19956 = cu->per_cu->dwarf2_per_objfile;
19957 struct objfile *objfile = dwarf2_per_objfile->objfile;
19958 const char *objf_name = objfile_name (objfile);
19959 bfd *abfd = objfile->obfd;
19960 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19961 struct dwarf2_section_info *str_offsets_section =
19962 &reader->dwo_file->sections.str_offsets;
19963 const gdb_byte *info_ptr;
19964 ULONGEST str_offset;
19965 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19966
19967 dwarf2_read_section (objfile, str_section);
19968 dwarf2_read_section (objfile, str_offsets_section);
19969 if (str_section->buffer == NULL)
19970 error (_("%s used without .debug_str.dwo section"
19971 " in CU at offset %s [in module %s]"),
19972 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19973 if (str_offsets_section->buffer == NULL)
19974 error (_("%s used without .debug_str_offsets.dwo section"
19975 " in CU at offset %s [in module %s]"),
19976 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19977 if (str_index * cu->header.offset_size >= str_offsets_section->size)
19978 error (_("%s pointing outside of .debug_str_offsets.dwo"
19979 " section in CU at offset %s [in module %s]"),
19980 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19981 info_ptr = (str_offsets_section->buffer
19982 + str_index * cu->header.offset_size);
19983 if (cu->header.offset_size == 4)
19984 str_offset = bfd_get_32 (abfd, info_ptr);
19985 else
19986 str_offset = bfd_get_64 (abfd, info_ptr);
19987 if (str_offset >= str_section->size)
19988 error (_("Offset from %s pointing outside of"
19989 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19990 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19991 return (const char *) (str_section->buffer + str_offset);
19992 }
19993
19994 /* Return the length of an LEB128 number in BUF. */
19995
19996 static int
19997 leb128_size (const gdb_byte *buf)
19998 {
19999 const gdb_byte *begin = buf;
20000 gdb_byte byte;
20001
20002 while (1)
20003 {
20004 byte = *buf++;
20005 if ((byte & 128) == 0)
20006 return buf - begin;
20007 }
20008 }
20009
20010 static void
20011 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20012 {
20013 switch (lang)
20014 {
20015 case DW_LANG_C89:
20016 case DW_LANG_C99:
20017 case DW_LANG_C11:
20018 case DW_LANG_C:
20019 case DW_LANG_UPC:
20020 cu->language = language_c;
20021 break;
20022 case DW_LANG_Java:
20023 case DW_LANG_C_plus_plus:
20024 case DW_LANG_C_plus_plus_11:
20025 case DW_LANG_C_plus_plus_14:
20026 cu->language = language_cplus;
20027 break;
20028 case DW_LANG_D:
20029 cu->language = language_d;
20030 break;
20031 case DW_LANG_Fortran77:
20032 case DW_LANG_Fortran90:
20033 case DW_LANG_Fortran95:
20034 case DW_LANG_Fortran03:
20035 case DW_LANG_Fortran08:
20036 cu->language = language_fortran;
20037 break;
20038 case DW_LANG_Go:
20039 cu->language = language_go;
20040 break;
20041 case DW_LANG_Mips_Assembler:
20042 cu->language = language_asm;
20043 break;
20044 case DW_LANG_Ada83:
20045 case DW_LANG_Ada95:
20046 cu->language = language_ada;
20047 break;
20048 case DW_LANG_Modula2:
20049 cu->language = language_m2;
20050 break;
20051 case DW_LANG_Pascal83:
20052 cu->language = language_pascal;
20053 break;
20054 case DW_LANG_ObjC:
20055 cu->language = language_objc;
20056 break;
20057 case DW_LANG_Rust:
20058 case DW_LANG_Rust_old:
20059 cu->language = language_rust;
20060 break;
20061 case DW_LANG_Cobol74:
20062 case DW_LANG_Cobol85:
20063 default:
20064 cu->language = language_minimal;
20065 break;
20066 }
20067 cu->language_defn = language_def (cu->language);
20068 }
20069
20070 /* Return the named attribute or NULL if not there. */
20071
20072 static struct attribute *
20073 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20074 {
20075 for (;;)
20076 {
20077 unsigned int i;
20078 struct attribute *spec = NULL;
20079
20080 for (i = 0; i < die->num_attrs; ++i)
20081 {
20082 if (die->attrs[i].name == name)
20083 return &die->attrs[i];
20084 if (die->attrs[i].name == DW_AT_specification
20085 || die->attrs[i].name == DW_AT_abstract_origin)
20086 spec = &die->attrs[i];
20087 }
20088
20089 if (!spec)
20090 break;
20091
20092 die = follow_die_ref (die, spec, &cu);
20093 }
20094
20095 return NULL;
20096 }
20097
20098 /* Return the named attribute or NULL if not there,
20099 but do not follow DW_AT_specification, etc.
20100 This is for use in contexts where we're reading .debug_types dies.
20101 Following DW_AT_specification, DW_AT_abstract_origin will take us
20102 back up the chain, and we want to go down. */
20103
20104 static struct attribute *
20105 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20106 {
20107 unsigned int i;
20108
20109 for (i = 0; i < die->num_attrs; ++i)
20110 if (die->attrs[i].name == name)
20111 return &die->attrs[i];
20112
20113 return NULL;
20114 }
20115
20116 /* Return the string associated with a string-typed attribute, or NULL if it
20117 is either not found or is of an incorrect type. */
20118
20119 static const char *
20120 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20121 {
20122 struct attribute *attr;
20123 const char *str = NULL;
20124
20125 attr = dwarf2_attr (die, name, cu);
20126
20127 if (attr != NULL)
20128 {
20129 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20130 || attr->form == DW_FORM_string
20131 || attr->form == DW_FORM_strx
20132 || attr->form == DW_FORM_GNU_str_index
20133 || attr->form == DW_FORM_GNU_strp_alt)
20134 str = DW_STRING (attr);
20135 else
20136 complaint (_("string type expected for attribute %s for "
20137 "DIE at %s in module %s"),
20138 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20139 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20140 }
20141
20142 return str;
20143 }
20144
20145 /* Return the dwo name or NULL if not present. If present, it is in either
20146 DW_AT_GNU_dwo_name or DW_AT_dwo_name atrribute. */
20147 static const char *
20148 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20149 {
20150 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20151 if (dwo_name == nullptr)
20152 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20153 return dwo_name;
20154 }
20155
20156 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20157 and holds a non-zero value. This function should only be used for
20158 DW_FORM_flag or DW_FORM_flag_present attributes. */
20159
20160 static int
20161 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20162 {
20163 struct attribute *attr = dwarf2_attr (die, name, cu);
20164
20165 return (attr && DW_UNSND (attr));
20166 }
20167
20168 static int
20169 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20170 {
20171 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20172 which value is non-zero. However, we have to be careful with
20173 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20174 (via dwarf2_flag_true_p) follows this attribute. So we may
20175 end up accidently finding a declaration attribute that belongs
20176 to a different DIE referenced by the specification attribute,
20177 even though the given DIE does not have a declaration attribute. */
20178 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20179 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20180 }
20181
20182 /* Return the die giving the specification for DIE, if there is
20183 one. *SPEC_CU is the CU containing DIE on input, and the CU
20184 containing the return value on output. If there is no
20185 specification, but there is an abstract origin, that is
20186 returned. */
20187
20188 static struct die_info *
20189 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20190 {
20191 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20192 *spec_cu);
20193
20194 if (spec_attr == NULL)
20195 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20196
20197 if (spec_attr == NULL)
20198 return NULL;
20199 else
20200 return follow_die_ref (die, spec_attr, spec_cu);
20201 }
20202
20203 /* Stub for free_line_header to match void * callback types. */
20204
20205 static void
20206 free_line_header_voidp (void *arg)
20207 {
20208 struct line_header *lh = (struct line_header *) arg;
20209
20210 delete lh;
20211 }
20212
20213 void
20214 line_header::add_include_dir (const char *include_dir)
20215 {
20216 if (dwarf_line_debug >= 2)
20217 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20218 include_dirs.size () + 1, include_dir);
20219
20220 include_dirs.push_back (include_dir);
20221 }
20222
20223 void
20224 line_header::add_file_name (const char *name,
20225 dir_index d_index,
20226 unsigned int mod_time,
20227 unsigned int length)
20228 {
20229 if (dwarf_line_debug >= 2)
20230 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
20231 (unsigned) file_names.size () + 1, name);
20232
20233 file_names.emplace_back (name, d_index, mod_time, length);
20234 }
20235
20236 /* A convenience function to find the proper .debug_line section for a CU. */
20237
20238 static struct dwarf2_section_info *
20239 get_debug_line_section (struct dwarf2_cu *cu)
20240 {
20241 struct dwarf2_section_info *section;
20242 struct dwarf2_per_objfile *dwarf2_per_objfile
20243 = cu->per_cu->dwarf2_per_objfile;
20244
20245 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20246 DWO file. */
20247 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20248 section = &cu->dwo_unit->dwo_file->sections.line;
20249 else if (cu->per_cu->is_dwz)
20250 {
20251 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20252
20253 section = &dwz->line;
20254 }
20255 else
20256 section = &dwarf2_per_objfile->line;
20257
20258 return section;
20259 }
20260
20261 /* Read directory or file name entry format, starting with byte of
20262 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20263 entries count and the entries themselves in the described entry
20264 format. */
20265
20266 static void
20267 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20268 bfd *abfd, const gdb_byte **bufp,
20269 struct line_header *lh,
20270 const struct comp_unit_head *cu_header,
20271 void (*callback) (struct line_header *lh,
20272 const char *name,
20273 dir_index d_index,
20274 unsigned int mod_time,
20275 unsigned int length))
20276 {
20277 gdb_byte format_count, formati;
20278 ULONGEST data_count, datai;
20279 const gdb_byte *buf = *bufp;
20280 const gdb_byte *format_header_data;
20281 unsigned int bytes_read;
20282
20283 format_count = read_1_byte (abfd, buf);
20284 buf += 1;
20285 format_header_data = buf;
20286 for (formati = 0; formati < format_count; formati++)
20287 {
20288 read_unsigned_leb128 (abfd, buf, &bytes_read);
20289 buf += bytes_read;
20290 read_unsigned_leb128 (abfd, buf, &bytes_read);
20291 buf += bytes_read;
20292 }
20293
20294 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20295 buf += bytes_read;
20296 for (datai = 0; datai < data_count; datai++)
20297 {
20298 const gdb_byte *format = format_header_data;
20299 struct file_entry fe;
20300
20301 for (formati = 0; formati < format_count; formati++)
20302 {
20303 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20304 format += bytes_read;
20305
20306 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20307 format += bytes_read;
20308
20309 gdb::optional<const char *> string;
20310 gdb::optional<unsigned int> uint;
20311
20312 switch (form)
20313 {
20314 case DW_FORM_string:
20315 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20316 buf += bytes_read;
20317 break;
20318
20319 case DW_FORM_line_strp:
20320 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20321 abfd, buf,
20322 cu_header,
20323 &bytes_read));
20324 buf += bytes_read;
20325 break;
20326
20327 case DW_FORM_data1:
20328 uint.emplace (read_1_byte (abfd, buf));
20329 buf += 1;
20330 break;
20331
20332 case DW_FORM_data2:
20333 uint.emplace (read_2_bytes (abfd, buf));
20334 buf += 2;
20335 break;
20336
20337 case DW_FORM_data4:
20338 uint.emplace (read_4_bytes (abfd, buf));
20339 buf += 4;
20340 break;
20341
20342 case DW_FORM_data8:
20343 uint.emplace (read_8_bytes (abfd, buf));
20344 buf += 8;
20345 break;
20346
20347 case DW_FORM_udata:
20348 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20349 buf += bytes_read;
20350 break;
20351
20352 case DW_FORM_block:
20353 /* It is valid only for DW_LNCT_timestamp which is ignored by
20354 current GDB. */
20355 break;
20356 }
20357
20358 switch (content_type)
20359 {
20360 case DW_LNCT_path:
20361 if (string.has_value ())
20362 fe.name = *string;
20363 break;
20364 case DW_LNCT_directory_index:
20365 if (uint.has_value ())
20366 fe.d_index = (dir_index) *uint;
20367 break;
20368 case DW_LNCT_timestamp:
20369 if (uint.has_value ())
20370 fe.mod_time = *uint;
20371 break;
20372 case DW_LNCT_size:
20373 if (uint.has_value ())
20374 fe.length = *uint;
20375 break;
20376 case DW_LNCT_MD5:
20377 break;
20378 default:
20379 complaint (_("Unknown format content type %s"),
20380 pulongest (content_type));
20381 }
20382 }
20383
20384 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20385 }
20386
20387 *bufp = buf;
20388 }
20389
20390 /* Read the statement program header starting at OFFSET in
20391 .debug_line, or .debug_line.dwo. Return a pointer
20392 to a struct line_header, allocated using xmalloc.
20393 Returns NULL if there is a problem reading the header, e.g., if it
20394 has a version we don't understand.
20395
20396 NOTE: the strings in the include directory and file name tables of
20397 the returned object point into the dwarf line section buffer,
20398 and must not be freed. */
20399
20400 static line_header_up
20401 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20402 {
20403 const gdb_byte *line_ptr;
20404 unsigned int bytes_read, offset_size;
20405 int i;
20406 const char *cur_dir, *cur_file;
20407 struct dwarf2_section_info *section;
20408 bfd *abfd;
20409 struct dwarf2_per_objfile *dwarf2_per_objfile
20410 = cu->per_cu->dwarf2_per_objfile;
20411
20412 section = get_debug_line_section (cu);
20413 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20414 if (section->buffer == NULL)
20415 {
20416 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20417 complaint (_("missing .debug_line.dwo section"));
20418 else
20419 complaint (_("missing .debug_line section"));
20420 return 0;
20421 }
20422
20423 /* We can't do this until we know the section is non-empty.
20424 Only then do we know we have such a section. */
20425 abfd = get_section_bfd_owner (section);
20426
20427 /* Make sure that at least there's room for the total_length field.
20428 That could be 12 bytes long, but we're just going to fudge that. */
20429 if (to_underlying (sect_off) + 4 >= section->size)
20430 {
20431 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20432 return 0;
20433 }
20434
20435 line_header_up lh (new line_header ());
20436
20437 lh->sect_off = sect_off;
20438 lh->offset_in_dwz = cu->per_cu->is_dwz;
20439
20440 line_ptr = section->buffer + to_underlying (sect_off);
20441
20442 /* Read in the header. */
20443 lh->total_length =
20444 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20445 &bytes_read, &offset_size);
20446 line_ptr += bytes_read;
20447 if (line_ptr + lh->total_length > (section->buffer + section->size))
20448 {
20449 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20450 return 0;
20451 }
20452 lh->statement_program_end = line_ptr + lh->total_length;
20453 lh->version = read_2_bytes (abfd, line_ptr);
20454 line_ptr += 2;
20455 if (lh->version > 5)
20456 {
20457 /* This is a version we don't understand. The format could have
20458 changed in ways we don't handle properly so just punt. */
20459 complaint (_("unsupported version in .debug_line section"));
20460 return NULL;
20461 }
20462 if (lh->version >= 5)
20463 {
20464 gdb_byte segment_selector_size;
20465
20466 /* Skip address size. */
20467 read_1_byte (abfd, line_ptr);
20468 line_ptr += 1;
20469
20470 segment_selector_size = read_1_byte (abfd, line_ptr);
20471 line_ptr += 1;
20472 if (segment_selector_size != 0)
20473 {
20474 complaint (_("unsupported segment selector size %u "
20475 "in .debug_line section"),
20476 segment_selector_size);
20477 return NULL;
20478 }
20479 }
20480 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20481 line_ptr += offset_size;
20482 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20483 line_ptr += 1;
20484 if (lh->version >= 4)
20485 {
20486 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20487 line_ptr += 1;
20488 }
20489 else
20490 lh->maximum_ops_per_instruction = 1;
20491
20492 if (lh->maximum_ops_per_instruction == 0)
20493 {
20494 lh->maximum_ops_per_instruction = 1;
20495 complaint (_("invalid maximum_ops_per_instruction "
20496 "in `.debug_line' section"));
20497 }
20498
20499 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20500 line_ptr += 1;
20501 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20502 line_ptr += 1;
20503 lh->line_range = read_1_byte (abfd, line_ptr);
20504 line_ptr += 1;
20505 lh->opcode_base = read_1_byte (abfd, line_ptr);
20506 line_ptr += 1;
20507 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20508
20509 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20510 for (i = 1; i < lh->opcode_base; ++i)
20511 {
20512 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20513 line_ptr += 1;
20514 }
20515
20516 if (lh->version >= 5)
20517 {
20518 /* Read directory table. */
20519 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20520 &cu->header,
20521 [] (struct line_header *header, const char *name,
20522 dir_index d_index, unsigned int mod_time,
20523 unsigned int length)
20524 {
20525 header->add_include_dir (name);
20526 });
20527
20528 /* Read file name table. */
20529 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20530 &cu->header,
20531 [] (struct line_header *header, const char *name,
20532 dir_index d_index, unsigned int mod_time,
20533 unsigned int length)
20534 {
20535 header->add_file_name (name, d_index, mod_time, length);
20536 });
20537 }
20538 else
20539 {
20540 /* Read directory table. */
20541 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20542 {
20543 line_ptr += bytes_read;
20544 lh->add_include_dir (cur_dir);
20545 }
20546 line_ptr += bytes_read;
20547
20548 /* Read file name table. */
20549 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20550 {
20551 unsigned int mod_time, length;
20552 dir_index d_index;
20553
20554 line_ptr += bytes_read;
20555 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20556 line_ptr += bytes_read;
20557 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20558 line_ptr += bytes_read;
20559 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20560 line_ptr += bytes_read;
20561
20562 lh->add_file_name (cur_file, d_index, mod_time, length);
20563 }
20564 line_ptr += bytes_read;
20565 }
20566 lh->statement_program_start = line_ptr;
20567
20568 if (line_ptr > (section->buffer + section->size))
20569 complaint (_("line number info header doesn't "
20570 "fit in `.debug_line' section"));
20571
20572 return lh;
20573 }
20574
20575 /* Subroutine of dwarf_decode_lines to simplify it.
20576 Return the file name of the psymtab for included file FILE_INDEX
20577 in line header LH of PST.
20578 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20579 If space for the result is malloc'd, *NAME_HOLDER will be set.
20580 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20581
20582 static const char *
20583 psymtab_include_file_name (const struct line_header *lh, int file_index,
20584 const struct partial_symtab *pst,
20585 const char *comp_dir,
20586 gdb::unique_xmalloc_ptr<char> *name_holder)
20587 {
20588 const file_entry &fe = lh->file_names[file_index];
20589 const char *include_name = fe.name;
20590 const char *include_name_to_compare = include_name;
20591 const char *pst_filename;
20592 int file_is_pst;
20593
20594 const char *dir_name = fe.include_dir (lh);
20595
20596 gdb::unique_xmalloc_ptr<char> hold_compare;
20597 if (!IS_ABSOLUTE_PATH (include_name)
20598 && (dir_name != NULL || comp_dir != NULL))
20599 {
20600 /* Avoid creating a duplicate psymtab for PST.
20601 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20602 Before we do the comparison, however, we need to account
20603 for DIR_NAME and COMP_DIR.
20604 First prepend dir_name (if non-NULL). If we still don't
20605 have an absolute path prepend comp_dir (if non-NULL).
20606 However, the directory we record in the include-file's
20607 psymtab does not contain COMP_DIR (to match the
20608 corresponding symtab(s)).
20609
20610 Example:
20611
20612 bash$ cd /tmp
20613 bash$ gcc -g ./hello.c
20614 include_name = "hello.c"
20615 dir_name = "."
20616 DW_AT_comp_dir = comp_dir = "/tmp"
20617 DW_AT_name = "./hello.c"
20618
20619 */
20620
20621 if (dir_name != NULL)
20622 {
20623 name_holder->reset (concat (dir_name, SLASH_STRING,
20624 include_name, (char *) NULL));
20625 include_name = name_holder->get ();
20626 include_name_to_compare = include_name;
20627 }
20628 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20629 {
20630 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20631 include_name, (char *) NULL));
20632 include_name_to_compare = hold_compare.get ();
20633 }
20634 }
20635
20636 pst_filename = pst->filename;
20637 gdb::unique_xmalloc_ptr<char> copied_name;
20638 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20639 {
20640 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20641 pst_filename, (char *) NULL));
20642 pst_filename = copied_name.get ();
20643 }
20644
20645 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20646
20647 if (file_is_pst)
20648 return NULL;
20649 return include_name;
20650 }
20651
20652 /* State machine to track the state of the line number program. */
20653
20654 class lnp_state_machine
20655 {
20656 public:
20657 /* Initialize a machine state for the start of a line number
20658 program. */
20659 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20660 bool record_lines_p);
20661
20662 file_entry *current_file ()
20663 {
20664 /* lh->file_names is 0-based, but the file name numbers in the
20665 statement program are 1-based. */
20666 return m_line_header->file_name_at (m_file);
20667 }
20668
20669 /* Record the line in the state machine. END_SEQUENCE is true if
20670 we're processing the end of a sequence. */
20671 void record_line (bool end_sequence);
20672
20673 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20674 nop-out rest of the lines in this sequence. */
20675 void check_line_address (struct dwarf2_cu *cu,
20676 const gdb_byte *line_ptr,
20677 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20678
20679 void handle_set_discriminator (unsigned int discriminator)
20680 {
20681 m_discriminator = discriminator;
20682 m_line_has_non_zero_discriminator |= discriminator != 0;
20683 }
20684
20685 /* Handle DW_LNE_set_address. */
20686 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20687 {
20688 m_op_index = 0;
20689 address += baseaddr;
20690 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20691 }
20692
20693 /* Handle DW_LNS_advance_pc. */
20694 void handle_advance_pc (CORE_ADDR adjust);
20695
20696 /* Handle a special opcode. */
20697 void handle_special_opcode (unsigned char op_code);
20698
20699 /* Handle DW_LNS_advance_line. */
20700 void handle_advance_line (int line_delta)
20701 {
20702 advance_line (line_delta);
20703 }
20704
20705 /* Handle DW_LNS_set_file. */
20706 void handle_set_file (file_name_index file);
20707
20708 /* Handle DW_LNS_negate_stmt. */
20709 void handle_negate_stmt ()
20710 {
20711 m_is_stmt = !m_is_stmt;
20712 }
20713
20714 /* Handle DW_LNS_const_add_pc. */
20715 void handle_const_add_pc ();
20716
20717 /* Handle DW_LNS_fixed_advance_pc. */
20718 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20719 {
20720 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20721 m_op_index = 0;
20722 }
20723
20724 /* Handle DW_LNS_copy. */
20725 void handle_copy ()
20726 {
20727 record_line (false);
20728 m_discriminator = 0;
20729 }
20730
20731 /* Handle DW_LNE_end_sequence. */
20732 void handle_end_sequence ()
20733 {
20734 m_currently_recording_lines = true;
20735 }
20736
20737 private:
20738 /* Advance the line by LINE_DELTA. */
20739 void advance_line (int line_delta)
20740 {
20741 m_line += line_delta;
20742
20743 if (line_delta != 0)
20744 m_line_has_non_zero_discriminator = m_discriminator != 0;
20745 }
20746
20747 struct dwarf2_cu *m_cu;
20748
20749 gdbarch *m_gdbarch;
20750
20751 /* True if we're recording lines.
20752 Otherwise we're building partial symtabs and are just interested in
20753 finding include files mentioned by the line number program. */
20754 bool m_record_lines_p;
20755
20756 /* The line number header. */
20757 line_header *m_line_header;
20758
20759 /* These are part of the standard DWARF line number state machine,
20760 and initialized according to the DWARF spec. */
20761
20762 unsigned char m_op_index = 0;
20763 /* The line table index (1-based) of the current file. */
20764 file_name_index m_file = (file_name_index) 1;
20765 unsigned int m_line = 1;
20766
20767 /* These are initialized in the constructor. */
20768
20769 CORE_ADDR m_address;
20770 bool m_is_stmt;
20771 unsigned int m_discriminator;
20772
20773 /* Additional bits of state we need to track. */
20774
20775 /* The last file that we called dwarf2_start_subfile for.
20776 This is only used for TLLs. */
20777 unsigned int m_last_file = 0;
20778 /* The last file a line number was recorded for. */
20779 struct subfile *m_last_subfile = NULL;
20780
20781 /* When true, record the lines we decode. */
20782 bool m_currently_recording_lines = false;
20783
20784 /* The last line number that was recorded, used to coalesce
20785 consecutive entries for the same line. This can happen, for
20786 example, when discriminators are present. PR 17276. */
20787 unsigned int m_last_line = 0;
20788 bool m_line_has_non_zero_discriminator = false;
20789 };
20790
20791 void
20792 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20793 {
20794 CORE_ADDR addr_adj = (((m_op_index + adjust)
20795 / m_line_header->maximum_ops_per_instruction)
20796 * m_line_header->minimum_instruction_length);
20797 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20798 m_op_index = ((m_op_index + adjust)
20799 % m_line_header->maximum_ops_per_instruction);
20800 }
20801
20802 void
20803 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20804 {
20805 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20806 CORE_ADDR addr_adj = (((m_op_index
20807 + (adj_opcode / m_line_header->line_range))
20808 / m_line_header->maximum_ops_per_instruction)
20809 * m_line_header->minimum_instruction_length);
20810 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20811 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20812 % m_line_header->maximum_ops_per_instruction);
20813
20814 int line_delta = (m_line_header->line_base
20815 + (adj_opcode % m_line_header->line_range));
20816 advance_line (line_delta);
20817 record_line (false);
20818 m_discriminator = 0;
20819 }
20820
20821 void
20822 lnp_state_machine::handle_set_file (file_name_index file)
20823 {
20824 m_file = file;
20825
20826 const file_entry *fe = current_file ();
20827 if (fe == NULL)
20828 dwarf2_debug_line_missing_file_complaint ();
20829 else if (m_record_lines_p)
20830 {
20831 const char *dir = fe->include_dir (m_line_header);
20832
20833 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20834 m_line_has_non_zero_discriminator = m_discriminator != 0;
20835 dwarf2_start_subfile (m_cu, fe->name, dir);
20836 }
20837 }
20838
20839 void
20840 lnp_state_machine::handle_const_add_pc ()
20841 {
20842 CORE_ADDR adjust
20843 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20844
20845 CORE_ADDR addr_adj
20846 = (((m_op_index + adjust)
20847 / m_line_header->maximum_ops_per_instruction)
20848 * m_line_header->minimum_instruction_length);
20849
20850 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20851 m_op_index = ((m_op_index + adjust)
20852 % m_line_header->maximum_ops_per_instruction);
20853 }
20854
20855 /* Return non-zero if we should add LINE to the line number table.
20856 LINE is the line to add, LAST_LINE is the last line that was added,
20857 LAST_SUBFILE is the subfile for LAST_LINE.
20858 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20859 had a non-zero discriminator.
20860
20861 We have to be careful in the presence of discriminators.
20862 E.g., for this line:
20863
20864 for (i = 0; i < 100000; i++);
20865
20866 clang can emit four line number entries for that one line,
20867 each with a different discriminator.
20868 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20869
20870 However, we want gdb to coalesce all four entries into one.
20871 Otherwise the user could stepi into the middle of the line and
20872 gdb would get confused about whether the pc really was in the
20873 middle of the line.
20874
20875 Things are further complicated by the fact that two consecutive
20876 line number entries for the same line is a heuristic used by gcc
20877 to denote the end of the prologue. So we can't just discard duplicate
20878 entries, we have to be selective about it. The heuristic we use is
20879 that we only collapse consecutive entries for the same line if at least
20880 one of those entries has a non-zero discriminator. PR 17276.
20881
20882 Note: Addresses in the line number state machine can never go backwards
20883 within one sequence, thus this coalescing is ok. */
20884
20885 static int
20886 dwarf_record_line_p (struct dwarf2_cu *cu,
20887 unsigned int line, unsigned int last_line,
20888 int line_has_non_zero_discriminator,
20889 struct subfile *last_subfile)
20890 {
20891 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20892 return 1;
20893 if (line != last_line)
20894 return 1;
20895 /* Same line for the same file that we've seen already.
20896 As a last check, for pr 17276, only record the line if the line
20897 has never had a non-zero discriminator. */
20898 if (!line_has_non_zero_discriminator)
20899 return 1;
20900 return 0;
20901 }
20902
20903 /* Use the CU's builder to record line number LINE beginning at
20904 address ADDRESS in the line table of subfile SUBFILE. */
20905
20906 static void
20907 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20908 unsigned int line, CORE_ADDR address,
20909 struct dwarf2_cu *cu)
20910 {
20911 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20912
20913 if (dwarf_line_debug)
20914 {
20915 fprintf_unfiltered (gdb_stdlog,
20916 "Recording line %u, file %s, address %s\n",
20917 line, lbasename (subfile->name),
20918 paddress (gdbarch, address));
20919 }
20920
20921 if (cu != nullptr)
20922 cu->get_builder ()->record_line (subfile, line, addr);
20923 }
20924
20925 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20926 Mark the end of a set of line number records.
20927 The arguments are the same as for dwarf_record_line_1.
20928 If SUBFILE is NULL the request is ignored. */
20929
20930 static void
20931 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20932 CORE_ADDR address, struct dwarf2_cu *cu)
20933 {
20934 if (subfile == NULL)
20935 return;
20936
20937 if (dwarf_line_debug)
20938 {
20939 fprintf_unfiltered (gdb_stdlog,
20940 "Finishing current line, file %s, address %s\n",
20941 lbasename (subfile->name),
20942 paddress (gdbarch, address));
20943 }
20944
20945 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
20946 }
20947
20948 void
20949 lnp_state_machine::record_line (bool end_sequence)
20950 {
20951 if (dwarf_line_debug)
20952 {
20953 fprintf_unfiltered (gdb_stdlog,
20954 "Processing actual line %u: file %u,"
20955 " address %s, is_stmt %u, discrim %u\n",
20956 m_line, to_underlying (m_file),
20957 paddress (m_gdbarch, m_address),
20958 m_is_stmt, m_discriminator);
20959 }
20960
20961 file_entry *fe = current_file ();
20962
20963 if (fe == NULL)
20964 dwarf2_debug_line_missing_file_complaint ();
20965 /* For now we ignore lines not starting on an instruction boundary.
20966 But not when processing end_sequence for compatibility with the
20967 previous version of the code. */
20968 else if (m_op_index == 0 || end_sequence)
20969 {
20970 fe->included_p = 1;
20971 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
20972 {
20973 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20974 || end_sequence)
20975 {
20976 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20977 m_currently_recording_lines ? m_cu : nullptr);
20978 }
20979
20980 if (!end_sequence)
20981 {
20982 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20983 m_line_has_non_zero_discriminator,
20984 m_last_subfile))
20985 {
20986 buildsym_compunit *builder = m_cu->get_builder ();
20987 dwarf_record_line_1 (m_gdbarch,
20988 builder->get_current_subfile (),
20989 m_line, m_address,
20990 m_currently_recording_lines ? m_cu : nullptr);
20991 }
20992 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20993 m_last_line = m_line;
20994 }
20995 }
20996 }
20997 }
20998
20999 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21000 line_header *lh, bool record_lines_p)
21001 {
21002 m_cu = cu;
21003 m_gdbarch = arch;
21004 m_record_lines_p = record_lines_p;
21005 m_line_header = lh;
21006
21007 m_currently_recording_lines = true;
21008
21009 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21010 was a line entry for it so that the backend has a chance to adjust it
21011 and also record it in case it needs it. This is currently used by MIPS
21012 code, cf. `mips_adjust_dwarf2_line'. */
21013 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21014 m_is_stmt = lh->default_is_stmt;
21015 m_discriminator = 0;
21016 }
21017
21018 void
21019 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21020 const gdb_byte *line_ptr,
21021 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21022 {
21023 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21024 the pc range of the CU. However, we restrict the test to only ADDRESS
21025 values of zero to preserve GDB's previous behaviour which is to handle
21026 the specific case of a function being GC'd by the linker. */
21027
21028 if (address == 0 && address < unrelocated_lowpc)
21029 {
21030 /* This line table is for a function which has been
21031 GCd by the linker. Ignore it. PR gdb/12528 */
21032
21033 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21034 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21035
21036 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21037 line_offset, objfile_name (objfile));
21038 m_currently_recording_lines = false;
21039 /* Note: m_currently_recording_lines is left as false until we see
21040 DW_LNE_end_sequence. */
21041 }
21042 }
21043
21044 /* Subroutine of dwarf_decode_lines to simplify it.
21045 Process the line number information in LH.
21046 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21047 program in order to set included_p for every referenced header. */
21048
21049 static void
21050 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21051 const int decode_for_pst_p, CORE_ADDR lowpc)
21052 {
21053 const gdb_byte *line_ptr, *extended_end;
21054 const gdb_byte *line_end;
21055 unsigned int bytes_read, extended_len;
21056 unsigned char op_code, extended_op;
21057 CORE_ADDR baseaddr;
21058 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21059 bfd *abfd = objfile->obfd;
21060 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21061 /* True if we're recording line info (as opposed to building partial
21062 symtabs and just interested in finding include files mentioned by
21063 the line number program). */
21064 bool record_lines_p = !decode_for_pst_p;
21065
21066 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21067
21068 line_ptr = lh->statement_program_start;
21069 line_end = lh->statement_program_end;
21070
21071 /* Read the statement sequences until there's nothing left. */
21072 while (line_ptr < line_end)
21073 {
21074 /* The DWARF line number program state machine. Reset the state
21075 machine at the start of each sequence. */
21076 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21077 bool end_sequence = false;
21078
21079 if (record_lines_p)
21080 {
21081 /* Start a subfile for the current file of the state
21082 machine. */
21083 const file_entry *fe = state_machine.current_file ();
21084
21085 if (fe != NULL)
21086 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21087 }
21088
21089 /* Decode the table. */
21090 while (line_ptr < line_end && !end_sequence)
21091 {
21092 op_code = read_1_byte (abfd, line_ptr);
21093 line_ptr += 1;
21094
21095 if (op_code >= lh->opcode_base)
21096 {
21097 /* Special opcode. */
21098 state_machine.handle_special_opcode (op_code);
21099 }
21100 else switch (op_code)
21101 {
21102 case DW_LNS_extended_op:
21103 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21104 &bytes_read);
21105 line_ptr += bytes_read;
21106 extended_end = line_ptr + extended_len;
21107 extended_op = read_1_byte (abfd, line_ptr);
21108 line_ptr += 1;
21109 switch (extended_op)
21110 {
21111 case DW_LNE_end_sequence:
21112 state_machine.handle_end_sequence ();
21113 end_sequence = true;
21114 break;
21115 case DW_LNE_set_address:
21116 {
21117 CORE_ADDR address
21118 = read_address (abfd, line_ptr, cu, &bytes_read);
21119 line_ptr += bytes_read;
21120
21121 state_machine.check_line_address (cu, line_ptr,
21122 lowpc - baseaddr, address);
21123 state_machine.handle_set_address (baseaddr, address);
21124 }
21125 break;
21126 case DW_LNE_define_file:
21127 {
21128 const char *cur_file;
21129 unsigned int mod_time, length;
21130 dir_index dindex;
21131
21132 cur_file = read_direct_string (abfd, line_ptr,
21133 &bytes_read);
21134 line_ptr += bytes_read;
21135 dindex = (dir_index)
21136 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21137 line_ptr += bytes_read;
21138 mod_time =
21139 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21140 line_ptr += bytes_read;
21141 length =
21142 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21143 line_ptr += bytes_read;
21144 lh->add_file_name (cur_file, dindex, mod_time, length);
21145 }
21146 break;
21147 case DW_LNE_set_discriminator:
21148 {
21149 /* The discriminator is not interesting to the
21150 debugger; just ignore it. We still need to
21151 check its value though:
21152 if there are consecutive entries for the same
21153 (non-prologue) line we want to coalesce them.
21154 PR 17276. */
21155 unsigned int discr
21156 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21157 line_ptr += bytes_read;
21158
21159 state_machine.handle_set_discriminator (discr);
21160 }
21161 break;
21162 default:
21163 complaint (_("mangled .debug_line section"));
21164 return;
21165 }
21166 /* Make sure that we parsed the extended op correctly. If e.g.
21167 we expected a different address size than the producer used,
21168 we may have read the wrong number of bytes. */
21169 if (line_ptr != extended_end)
21170 {
21171 complaint (_("mangled .debug_line section"));
21172 return;
21173 }
21174 break;
21175 case DW_LNS_copy:
21176 state_machine.handle_copy ();
21177 break;
21178 case DW_LNS_advance_pc:
21179 {
21180 CORE_ADDR adjust
21181 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21182 line_ptr += bytes_read;
21183
21184 state_machine.handle_advance_pc (adjust);
21185 }
21186 break;
21187 case DW_LNS_advance_line:
21188 {
21189 int line_delta
21190 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21191 line_ptr += bytes_read;
21192
21193 state_machine.handle_advance_line (line_delta);
21194 }
21195 break;
21196 case DW_LNS_set_file:
21197 {
21198 file_name_index file
21199 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21200 &bytes_read);
21201 line_ptr += bytes_read;
21202
21203 state_machine.handle_set_file (file);
21204 }
21205 break;
21206 case DW_LNS_set_column:
21207 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21208 line_ptr += bytes_read;
21209 break;
21210 case DW_LNS_negate_stmt:
21211 state_machine.handle_negate_stmt ();
21212 break;
21213 case DW_LNS_set_basic_block:
21214 break;
21215 /* Add to the address register of the state machine the
21216 address increment value corresponding to special opcode
21217 255. I.e., this value is scaled by the minimum
21218 instruction length since special opcode 255 would have
21219 scaled the increment. */
21220 case DW_LNS_const_add_pc:
21221 state_machine.handle_const_add_pc ();
21222 break;
21223 case DW_LNS_fixed_advance_pc:
21224 {
21225 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21226 line_ptr += 2;
21227
21228 state_machine.handle_fixed_advance_pc (addr_adj);
21229 }
21230 break;
21231 default:
21232 {
21233 /* Unknown standard opcode, ignore it. */
21234 int i;
21235
21236 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21237 {
21238 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21239 line_ptr += bytes_read;
21240 }
21241 }
21242 }
21243 }
21244
21245 if (!end_sequence)
21246 dwarf2_debug_line_missing_end_sequence_complaint ();
21247
21248 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21249 in which case we still finish recording the last line). */
21250 state_machine.record_line (true);
21251 }
21252 }
21253
21254 /* Decode the Line Number Program (LNP) for the given line_header
21255 structure and CU. The actual information extracted and the type
21256 of structures created from the LNP depends on the value of PST.
21257
21258 1. If PST is NULL, then this procedure uses the data from the program
21259 to create all necessary symbol tables, and their linetables.
21260
21261 2. If PST is not NULL, this procedure reads the program to determine
21262 the list of files included by the unit represented by PST, and
21263 builds all the associated partial symbol tables.
21264
21265 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21266 It is used for relative paths in the line table.
21267 NOTE: When processing partial symtabs (pst != NULL),
21268 comp_dir == pst->dirname.
21269
21270 NOTE: It is important that psymtabs have the same file name (via strcmp)
21271 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21272 symtab we don't use it in the name of the psymtabs we create.
21273 E.g. expand_line_sal requires this when finding psymtabs to expand.
21274 A good testcase for this is mb-inline.exp.
21275
21276 LOWPC is the lowest address in CU (or 0 if not known).
21277
21278 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21279 for its PC<->lines mapping information. Otherwise only the filename
21280 table is read in. */
21281
21282 static void
21283 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21284 struct dwarf2_cu *cu, struct partial_symtab *pst,
21285 CORE_ADDR lowpc, int decode_mapping)
21286 {
21287 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21288 const int decode_for_pst_p = (pst != NULL);
21289
21290 if (decode_mapping)
21291 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21292
21293 if (decode_for_pst_p)
21294 {
21295 int file_index;
21296
21297 /* Now that we're done scanning the Line Header Program, we can
21298 create the psymtab of each included file. */
21299 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21300 if (lh->file_names[file_index].included_p == 1)
21301 {
21302 gdb::unique_xmalloc_ptr<char> name_holder;
21303 const char *include_name =
21304 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21305 &name_holder);
21306 if (include_name != NULL)
21307 dwarf2_create_include_psymtab (include_name, pst, objfile);
21308 }
21309 }
21310 else
21311 {
21312 /* Make sure a symtab is created for every file, even files
21313 which contain only variables (i.e. no code with associated
21314 line numbers). */
21315 buildsym_compunit *builder = cu->get_builder ();
21316 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21317 int i;
21318
21319 for (i = 0; i < lh->file_names.size (); i++)
21320 {
21321 file_entry &fe = lh->file_names[i];
21322
21323 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21324
21325 if (builder->get_current_subfile ()->symtab == NULL)
21326 {
21327 builder->get_current_subfile ()->symtab
21328 = allocate_symtab (cust,
21329 builder->get_current_subfile ()->name);
21330 }
21331 fe.symtab = builder->get_current_subfile ()->symtab;
21332 }
21333 }
21334 }
21335
21336 /* Start a subfile for DWARF. FILENAME is the name of the file and
21337 DIRNAME the name of the source directory which contains FILENAME
21338 or NULL if not known.
21339 This routine tries to keep line numbers from identical absolute and
21340 relative file names in a common subfile.
21341
21342 Using the `list' example from the GDB testsuite, which resides in
21343 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21344 of /srcdir/list0.c yields the following debugging information for list0.c:
21345
21346 DW_AT_name: /srcdir/list0.c
21347 DW_AT_comp_dir: /compdir
21348 files.files[0].name: list0.h
21349 files.files[0].dir: /srcdir
21350 files.files[1].name: list0.c
21351 files.files[1].dir: /srcdir
21352
21353 The line number information for list0.c has to end up in a single
21354 subfile, so that `break /srcdir/list0.c:1' works as expected.
21355 start_subfile will ensure that this happens provided that we pass the
21356 concatenation of files.files[1].dir and files.files[1].name as the
21357 subfile's name. */
21358
21359 static void
21360 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21361 const char *dirname)
21362 {
21363 char *copy = NULL;
21364
21365 /* In order not to lose the line information directory,
21366 we concatenate it to the filename when it makes sense.
21367 Note that the Dwarf3 standard says (speaking of filenames in line
21368 information): ``The directory index is ignored for file names
21369 that represent full path names''. Thus ignoring dirname in the
21370 `else' branch below isn't an issue. */
21371
21372 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21373 {
21374 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21375 filename = copy;
21376 }
21377
21378 cu->get_builder ()->start_subfile (filename);
21379
21380 if (copy != NULL)
21381 xfree (copy);
21382 }
21383
21384 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21385 buildsym_compunit constructor. */
21386
21387 struct compunit_symtab *
21388 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21389 CORE_ADDR low_pc)
21390 {
21391 gdb_assert (m_builder == nullptr);
21392
21393 m_builder.reset (new struct buildsym_compunit
21394 (per_cu->dwarf2_per_objfile->objfile,
21395 name, comp_dir, language, low_pc));
21396
21397 list_in_scope = get_builder ()->get_file_symbols ();
21398
21399 get_builder ()->record_debugformat ("DWARF 2");
21400 get_builder ()->record_producer (producer);
21401
21402 processing_has_namespace_info = false;
21403
21404 return get_builder ()->get_compunit_symtab ();
21405 }
21406
21407 static void
21408 var_decode_location (struct attribute *attr, struct symbol *sym,
21409 struct dwarf2_cu *cu)
21410 {
21411 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21412 struct comp_unit_head *cu_header = &cu->header;
21413
21414 /* NOTE drow/2003-01-30: There used to be a comment and some special
21415 code here to turn a symbol with DW_AT_external and a
21416 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21417 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21418 with some versions of binutils) where shared libraries could have
21419 relocations against symbols in their debug information - the
21420 minimal symbol would have the right address, but the debug info
21421 would not. It's no longer necessary, because we will explicitly
21422 apply relocations when we read in the debug information now. */
21423
21424 /* A DW_AT_location attribute with no contents indicates that a
21425 variable has been optimized away. */
21426 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21427 {
21428 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21429 return;
21430 }
21431
21432 /* Handle one degenerate form of location expression specially, to
21433 preserve GDB's previous behavior when section offsets are
21434 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21435 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21436
21437 if (attr_form_is_block (attr)
21438 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21439 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21440 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21441 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21442 && (DW_BLOCK (attr)->size
21443 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21444 {
21445 unsigned int dummy;
21446
21447 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21448 SYMBOL_VALUE_ADDRESS (sym) =
21449 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21450 else
21451 SYMBOL_VALUE_ADDRESS (sym) =
21452 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21453 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21454 fixup_symbol_section (sym, objfile);
21455 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21456 SYMBOL_SECTION (sym));
21457 return;
21458 }
21459
21460 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21461 expression evaluator, and use LOC_COMPUTED only when necessary
21462 (i.e. when the value of a register or memory location is
21463 referenced, or a thread-local block, etc.). Then again, it might
21464 not be worthwhile. I'm assuming that it isn't unless performance
21465 or memory numbers show me otherwise. */
21466
21467 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21468
21469 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21470 cu->has_loclist = true;
21471 }
21472
21473 /* Given a pointer to a DWARF information entry, figure out if we need
21474 to make a symbol table entry for it, and if so, create a new entry
21475 and return a pointer to it.
21476 If TYPE is NULL, determine symbol type from the die, otherwise
21477 used the passed type.
21478 If SPACE is not NULL, use it to hold the new symbol. If it is
21479 NULL, allocate a new symbol on the objfile's obstack. */
21480
21481 static struct symbol *
21482 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21483 struct symbol *space)
21484 {
21485 struct dwarf2_per_objfile *dwarf2_per_objfile
21486 = cu->per_cu->dwarf2_per_objfile;
21487 struct objfile *objfile = dwarf2_per_objfile->objfile;
21488 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21489 struct symbol *sym = NULL;
21490 const char *name;
21491 struct attribute *attr = NULL;
21492 struct attribute *attr2 = NULL;
21493 CORE_ADDR baseaddr;
21494 struct pending **list_to_add = NULL;
21495
21496 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21497
21498 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21499
21500 name = dwarf2_name (die, cu);
21501 if (name)
21502 {
21503 const char *linkagename;
21504 int suppress_add = 0;
21505
21506 if (space)
21507 sym = space;
21508 else
21509 sym = allocate_symbol (objfile);
21510 OBJSTAT (objfile, n_syms++);
21511
21512 /* Cache this symbol's name and the name's demangled form (if any). */
21513 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21514 linkagename = dwarf2_physname (name, die, cu);
21515 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21516
21517 /* Fortran does not have mangling standard and the mangling does differ
21518 between gfortran, iFort etc. */
21519 if (cu->language == language_fortran
21520 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21521 symbol_set_demangled_name (&(sym->ginfo),
21522 dwarf2_full_name (name, die, cu),
21523 NULL);
21524
21525 /* Default assumptions.
21526 Use the passed type or decode it from the die. */
21527 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21528 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21529 if (type != NULL)
21530 SYMBOL_TYPE (sym) = type;
21531 else
21532 SYMBOL_TYPE (sym) = die_type (die, cu);
21533 attr = dwarf2_attr (die,
21534 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21535 cu);
21536 if (attr)
21537 {
21538 SYMBOL_LINE (sym) = DW_UNSND (attr);
21539 }
21540
21541 attr = dwarf2_attr (die,
21542 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21543 cu);
21544 if (attr)
21545 {
21546 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21547 struct file_entry *fe;
21548
21549 if (cu->line_header != NULL)
21550 fe = cu->line_header->file_name_at (file_index);
21551 else
21552 fe = NULL;
21553
21554 if (fe == NULL)
21555 complaint (_("file index out of range"));
21556 else
21557 symbol_set_symtab (sym, fe->symtab);
21558 }
21559
21560 switch (die->tag)
21561 {
21562 case DW_TAG_label:
21563 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21564 if (attr)
21565 {
21566 CORE_ADDR addr;
21567
21568 addr = attr_value_as_address (attr);
21569 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21570 SYMBOL_VALUE_ADDRESS (sym) = addr;
21571 }
21572 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21573 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21574 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21575 add_symbol_to_list (sym, cu->list_in_scope);
21576 break;
21577 case DW_TAG_subprogram:
21578 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21579 finish_block. */
21580 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21581 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21582 if ((attr2 && (DW_UNSND (attr2) != 0))
21583 || cu->language == language_ada)
21584 {
21585 /* Subprograms marked external are stored as a global symbol.
21586 Ada subprograms, whether marked external or not, are always
21587 stored as a global symbol, because we want to be able to
21588 access them globally. For instance, we want to be able
21589 to break on a nested subprogram without having to
21590 specify the context. */
21591 list_to_add = cu->get_builder ()->get_global_symbols ();
21592 }
21593 else
21594 {
21595 list_to_add = cu->list_in_scope;
21596 }
21597 break;
21598 case DW_TAG_inlined_subroutine:
21599 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21600 finish_block. */
21601 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21602 SYMBOL_INLINED (sym) = 1;
21603 list_to_add = cu->list_in_scope;
21604 break;
21605 case DW_TAG_template_value_param:
21606 suppress_add = 1;
21607 /* Fall through. */
21608 case DW_TAG_constant:
21609 case DW_TAG_variable:
21610 case DW_TAG_member:
21611 /* Compilation with minimal debug info may result in
21612 variables with missing type entries. Change the
21613 misleading `void' type to something sensible. */
21614 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21615 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21616
21617 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21618 /* In the case of DW_TAG_member, we should only be called for
21619 static const members. */
21620 if (die->tag == DW_TAG_member)
21621 {
21622 /* dwarf2_add_field uses die_is_declaration,
21623 so we do the same. */
21624 gdb_assert (die_is_declaration (die, cu));
21625 gdb_assert (attr);
21626 }
21627 if (attr)
21628 {
21629 dwarf2_const_value (attr, sym, cu);
21630 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21631 if (!suppress_add)
21632 {
21633 if (attr2 && (DW_UNSND (attr2) != 0))
21634 list_to_add = cu->get_builder ()->get_global_symbols ();
21635 else
21636 list_to_add = cu->list_in_scope;
21637 }
21638 break;
21639 }
21640 attr = dwarf2_attr (die, DW_AT_location, cu);
21641 if (attr)
21642 {
21643 var_decode_location (attr, sym, cu);
21644 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21645
21646 /* Fortran explicitly imports any global symbols to the local
21647 scope by DW_TAG_common_block. */
21648 if (cu->language == language_fortran && die->parent
21649 && die->parent->tag == DW_TAG_common_block)
21650 attr2 = NULL;
21651
21652 if (SYMBOL_CLASS (sym) == LOC_STATIC
21653 && SYMBOL_VALUE_ADDRESS (sym) == 0
21654 && !dwarf2_per_objfile->has_section_at_zero)
21655 {
21656 /* When a static variable is eliminated by the linker,
21657 the corresponding debug information is not stripped
21658 out, but the variable address is set to null;
21659 do not add such variables into symbol table. */
21660 }
21661 else if (attr2 && (DW_UNSND (attr2) != 0))
21662 {
21663 /* Workaround gfortran PR debug/40040 - it uses
21664 DW_AT_location for variables in -fPIC libraries which may
21665 get overriden by other libraries/executable and get
21666 a different address. Resolve it by the minimal symbol
21667 which may come from inferior's executable using copy
21668 relocation. Make this workaround only for gfortran as for
21669 other compilers GDB cannot guess the minimal symbol
21670 Fortran mangling kind. */
21671 if (cu->language == language_fortran && die->parent
21672 && die->parent->tag == DW_TAG_module
21673 && cu->producer
21674 && startswith (cu->producer, "GNU Fortran"))
21675 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21676
21677 /* A variable with DW_AT_external is never static,
21678 but it may be block-scoped. */
21679 list_to_add
21680 = ((cu->list_in_scope
21681 == cu->get_builder ()->get_file_symbols ())
21682 ? cu->get_builder ()->get_global_symbols ()
21683 : cu->list_in_scope);
21684 }
21685 else
21686 list_to_add = cu->list_in_scope;
21687 }
21688 else
21689 {
21690 /* We do not know the address of this symbol.
21691 If it is an external symbol and we have type information
21692 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21693 The address of the variable will then be determined from
21694 the minimal symbol table whenever the variable is
21695 referenced. */
21696 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21697
21698 /* Fortran explicitly imports any global symbols to the local
21699 scope by DW_TAG_common_block. */
21700 if (cu->language == language_fortran && die->parent
21701 && die->parent->tag == DW_TAG_common_block)
21702 {
21703 /* SYMBOL_CLASS doesn't matter here because
21704 read_common_block is going to reset it. */
21705 if (!suppress_add)
21706 list_to_add = cu->list_in_scope;
21707 }
21708 else if (attr2 && (DW_UNSND (attr2) != 0)
21709 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21710 {
21711 /* A variable with DW_AT_external is never static, but it
21712 may be block-scoped. */
21713 list_to_add
21714 = ((cu->list_in_scope
21715 == cu->get_builder ()->get_file_symbols ())
21716 ? cu->get_builder ()->get_global_symbols ()
21717 : cu->list_in_scope);
21718
21719 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21720 }
21721 else if (!die_is_declaration (die, cu))
21722 {
21723 /* Use the default LOC_OPTIMIZED_OUT class. */
21724 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21725 if (!suppress_add)
21726 list_to_add = cu->list_in_scope;
21727 }
21728 }
21729 break;
21730 case DW_TAG_formal_parameter:
21731 {
21732 /* If we are inside a function, mark this as an argument. If
21733 not, we might be looking at an argument to an inlined function
21734 when we do not have enough information to show inlined frames;
21735 pretend it's a local variable in that case so that the user can
21736 still see it. */
21737 struct context_stack *curr
21738 = cu->get_builder ()->get_current_context_stack ();
21739 if (curr != nullptr && curr->name != nullptr)
21740 SYMBOL_IS_ARGUMENT (sym) = 1;
21741 attr = dwarf2_attr (die, DW_AT_location, cu);
21742 if (attr)
21743 {
21744 var_decode_location (attr, sym, cu);
21745 }
21746 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21747 if (attr)
21748 {
21749 dwarf2_const_value (attr, sym, cu);
21750 }
21751
21752 list_to_add = cu->list_in_scope;
21753 }
21754 break;
21755 case DW_TAG_unspecified_parameters:
21756 /* From varargs functions; gdb doesn't seem to have any
21757 interest in this information, so just ignore it for now.
21758 (FIXME?) */
21759 break;
21760 case DW_TAG_template_type_param:
21761 suppress_add = 1;
21762 /* Fall through. */
21763 case DW_TAG_class_type:
21764 case DW_TAG_interface_type:
21765 case DW_TAG_structure_type:
21766 case DW_TAG_union_type:
21767 case DW_TAG_set_type:
21768 case DW_TAG_enumeration_type:
21769 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21770 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21771
21772 {
21773 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21774 really ever be static objects: otherwise, if you try
21775 to, say, break of a class's method and you're in a file
21776 which doesn't mention that class, it won't work unless
21777 the check for all static symbols in lookup_symbol_aux
21778 saves you. See the OtherFileClass tests in
21779 gdb.c++/namespace.exp. */
21780
21781 if (!suppress_add)
21782 {
21783 buildsym_compunit *builder = cu->get_builder ();
21784 list_to_add
21785 = (cu->list_in_scope == builder->get_file_symbols ()
21786 && cu->language == language_cplus
21787 ? builder->get_global_symbols ()
21788 : cu->list_in_scope);
21789
21790 /* The semantics of C++ state that "struct foo {
21791 ... }" also defines a typedef for "foo". */
21792 if (cu->language == language_cplus
21793 || cu->language == language_ada
21794 || cu->language == language_d
21795 || cu->language == language_rust)
21796 {
21797 /* The symbol's name is already allocated along
21798 with this objfile, so we don't need to
21799 duplicate it for the type. */
21800 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21801 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21802 }
21803 }
21804 }
21805 break;
21806 case DW_TAG_typedef:
21807 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21808 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21809 list_to_add = cu->list_in_scope;
21810 break;
21811 case DW_TAG_base_type:
21812 case DW_TAG_subrange_type:
21813 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21814 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21815 list_to_add = cu->list_in_scope;
21816 break;
21817 case DW_TAG_enumerator:
21818 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21819 if (attr)
21820 {
21821 dwarf2_const_value (attr, sym, cu);
21822 }
21823 {
21824 /* NOTE: carlton/2003-11-10: See comment above in the
21825 DW_TAG_class_type, etc. block. */
21826
21827 list_to_add
21828 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21829 && cu->language == language_cplus
21830 ? cu->get_builder ()->get_global_symbols ()
21831 : cu->list_in_scope);
21832 }
21833 break;
21834 case DW_TAG_imported_declaration:
21835 case DW_TAG_namespace:
21836 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21837 list_to_add = cu->get_builder ()->get_global_symbols ();
21838 break;
21839 case DW_TAG_module:
21840 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21841 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21842 list_to_add = cu->get_builder ()->get_global_symbols ();
21843 break;
21844 case DW_TAG_common_block:
21845 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21846 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21847 add_symbol_to_list (sym, cu->list_in_scope);
21848 break;
21849 default:
21850 /* Not a tag we recognize. Hopefully we aren't processing
21851 trash data, but since we must specifically ignore things
21852 we don't recognize, there is nothing else we should do at
21853 this point. */
21854 complaint (_("unsupported tag: '%s'"),
21855 dwarf_tag_name (die->tag));
21856 break;
21857 }
21858
21859 if (suppress_add)
21860 {
21861 sym->hash_next = objfile->template_symbols;
21862 objfile->template_symbols = sym;
21863 list_to_add = NULL;
21864 }
21865
21866 if (list_to_add != NULL)
21867 add_symbol_to_list (sym, list_to_add);
21868
21869 /* For the benefit of old versions of GCC, check for anonymous
21870 namespaces based on the demangled name. */
21871 if (!cu->processing_has_namespace_info
21872 && cu->language == language_cplus)
21873 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21874 }
21875 return (sym);
21876 }
21877
21878 /* Given an attr with a DW_FORM_dataN value in host byte order,
21879 zero-extend it as appropriate for the symbol's type. The DWARF
21880 standard (v4) is not entirely clear about the meaning of using
21881 DW_FORM_dataN for a constant with a signed type, where the type is
21882 wider than the data. The conclusion of a discussion on the DWARF
21883 list was that this is unspecified. We choose to always zero-extend
21884 because that is the interpretation long in use by GCC. */
21885
21886 static gdb_byte *
21887 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21888 struct dwarf2_cu *cu, LONGEST *value, int bits)
21889 {
21890 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21891 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21892 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21893 LONGEST l = DW_UNSND (attr);
21894
21895 if (bits < sizeof (*value) * 8)
21896 {
21897 l &= ((LONGEST) 1 << bits) - 1;
21898 *value = l;
21899 }
21900 else if (bits == sizeof (*value) * 8)
21901 *value = l;
21902 else
21903 {
21904 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21905 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21906 return bytes;
21907 }
21908
21909 return NULL;
21910 }
21911
21912 /* Read a constant value from an attribute. Either set *VALUE, or if
21913 the value does not fit in *VALUE, set *BYTES - either already
21914 allocated on the objfile obstack, or newly allocated on OBSTACK,
21915 or, set *BATON, if we translated the constant to a location
21916 expression. */
21917
21918 static void
21919 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21920 const char *name, struct obstack *obstack,
21921 struct dwarf2_cu *cu,
21922 LONGEST *value, const gdb_byte **bytes,
21923 struct dwarf2_locexpr_baton **baton)
21924 {
21925 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21926 struct comp_unit_head *cu_header = &cu->header;
21927 struct dwarf_block *blk;
21928 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21929 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21930
21931 *value = 0;
21932 *bytes = NULL;
21933 *baton = NULL;
21934
21935 switch (attr->form)
21936 {
21937 case DW_FORM_addr:
21938 case DW_FORM_addrx:
21939 case DW_FORM_GNU_addr_index:
21940 {
21941 gdb_byte *data;
21942
21943 if (TYPE_LENGTH (type) != cu_header->addr_size)
21944 dwarf2_const_value_length_mismatch_complaint (name,
21945 cu_header->addr_size,
21946 TYPE_LENGTH (type));
21947 /* Symbols of this form are reasonably rare, so we just
21948 piggyback on the existing location code rather than writing
21949 a new implementation of symbol_computed_ops. */
21950 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21951 (*baton)->per_cu = cu->per_cu;
21952 gdb_assert ((*baton)->per_cu);
21953
21954 (*baton)->size = 2 + cu_header->addr_size;
21955 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21956 (*baton)->data = data;
21957
21958 data[0] = DW_OP_addr;
21959 store_unsigned_integer (&data[1], cu_header->addr_size,
21960 byte_order, DW_ADDR (attr));
21961 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21962 }
21963 break;
21964 case DW_FORM_string:
21965 case DW_FORM_strp:
21966 case DW_FORM_strx:
21967 case DW_FORM_GNU_str_index:
21968 case DW_FORM_GNU_strp_alt:
21969 /* DW_STRING is already allocated on the objfile obstack, point
21970 directly to it. */
21971 *bytes = (const gdb_byte *) DW_STRING (attr);
21972 break;
21973 case DW_FORM_block1:
21974 case DW_FORM_block2:
21975 case DW_FORM_block4:
21976 case DW_FORM_block:
21977 case DW_FORM_exprloc:
21978 case DW_FORM_data16:
21979 blk = DW_BLOCK (attr);
21980 if (TYPE_LENGTH (type) != blk->size)
21981 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21982 TYPE_LENGTH (type));
21983 *bytes = blk->data;
21984 break;
21985
21986 /* The DW_AT_const_value attributes are supposed to carry the
21987 symbol's value "represented as it would be on the target
21988 architecture." By the time we get here, it's already been
21989 converted to host endianness, so we just need to sign- or
21990 zero-extend it as appropriate. */
21991 case DW_FORM_data1:
21992 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21993 break;
21994 case DW_FORM_data2:
21995 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21996 break;
21997 case DW_FORM_data4:
21998 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21999 break;
22000 case DW_FORM_data8:
22001 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22002 break;
22003
22004 case DW_FORM_sdata:
22005 case DW_FORM_implicit_const:
22006 *value = DW_SND (attr);
22007 break;
22008
22009 case DW_FORM_udata:
22010 *value = DW_UNSND (attr);
22011 break;
22012
22013 default:
22014 complaint (_("unsupported const value attribute form: '%s'"),
22015 dwarf_form_name (attr->form));
22016 *value = 0;
22017 break;
22018 }
22019 }
22020
22021
22022 /* Copy constant value from an attribute to a symbol. */
22023
22024 static void
22025 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22026 struct dwarf2_cu *cu)
22027 {
22028 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22029 LONGEST value;
22030 const gdb_byte *bytes;
22031 struct dwarf2_locexpr_baton *baton;
22032
22033 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22034 SYMBOL_PRINT_NAME (sym),
22035 &objfile->objfile_obstack, cu,
22036 &value, &bytes, &baton);
22037
22038 if (baton != NULL)
22039 {
22040 SYMBOL_LOCATION_BATON (sym) = baton;
22041 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22042 }
22043 else if (bytes != NULL)
22044 {
22045 SYMBOL_VALUE_BYTES (sym) = bytes;
22046 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22047 }
22048 else
22049 {
22050 SYMBOL_VALUE (sym) = value;
22051 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22052 }
22053 }
22054
22055 /* Return the type of the die in question using its DW_AT_type attribute. */
22056
22057 static struct type *
22058 die_type (struct die_info *die, struct dwarf2_cu *cu)
22059 {
22060 struct attribute *type_attr;
22061
22062 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22063 if (!type_attr)
22064 {
22065 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22066 /* A missing DW_AT_type represents a void type. */
22067 return objfile_type (objfile)->builtin_void;
22068 }
22069
22070 return lookup_die_type (die, type_attr, cu);
22071 }
22072
22073 /* True iff CU's producer generates GNAT Ada auxiliary information
22074 that allows to find parallel types through that information instead
22075 of having to do expensive parallel lookups by type name. */
22076
22077 static int
22078 need_gnat_info (struct dwarf2_cu *cu)
22079 {
22080 /* Assume that the Ada compiler was GNAT, which always produces
22081 the auxiliary information. */
22082 return (cu->language == language_ada);
22083 }
22084
22085 /* Return the auxiliary type of the die in question using its
22086 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22087 attribute is not present. */
22088
22089 static struct type *
22090 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22091 {
22092 struct attribute *type_attr;
22093
22094 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22095 if (!type_attr)
22096 return NULL;
22097
22098 return lookup_die_type (die, type_attr, cu);
22099 }
22100
22101 /* If DIE has a descriptive_type attribute, then set the TYPE's
22102 descriptive type accordingly. */
22103
22104 static void
22105 set_descriptive_type (struct type *type, struct die_info *die,
22106 struct dwarf2_cu *cu)
22107 {
22108 struct type *descriptive_type = die_descriptive_type (die, cu);
22109
22110 if (descriptive_type)
22111 {
22112 ALLOCATE_GNAT_AUX_TYPE (type);
22113 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22114 }
22115 }
22116
22117 /* Return the containing type of the die in question using its
22118 DW_AT_containing_type attribute. */
22119
22120 static struct type *
22121 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22122 {
22123 struct attribute *type_attr;
22124 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22125
22126 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22127 if (!type_attr)
22128 error (_("Dwarf Error: Problem turning containing type into gdb type "
22129 "[in module %s]"), objfile_name (objfile));
22130
22131 return lookup_die_type (die, type_attr, cu);
22132 }
22133
22134 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22135
22136 static struct type *
22137 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22138 {
22139 struct dwarf2_per_objfile *dwarf2_per_objfile
22140 = cu->per_cu->dwarf2_per_objfile;
22141 struct objfile *objfile = dwarf2_per_objfile->objfile;
22142 char *saved;
22143
22144 std::string message
22145 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22146 objfile_name (objfile),
22147 sect_offset_str (cu->header.sect_off),
22148 sect_offset_str (die->sect_off));
22149 saved = obstack_strdup (&objfile->objfile_obstack, message);
22150
22151 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22152 }
22153
22154 /* Look up the type of DIE in CU using its type attribute ATTR.
22155 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22156 DW_AT_containing_type.
22157 If there is no type substitute an error marker. */
22158
22159 static struct type *
22160 lookup_die_type (struct die_info *die, const struct attribute *attr,
22161 struct dwarf2_cu *cu)
22162 {
22163 struct dwarf2_per_objfile *dwarf2_per_objfile
22164 = cu->per_cu->dwarf2_per_objfile;
22165 struct objfile *objfile = dwarf2_per_objfile->objfile;
22166 struct type *this_type;
22167
22168 gdb_assert (attr->name == DW_AT_type
22169 || attr->name == DW_AT_GNAT_descriptive_type
22170 || attr->name == DW_AT_containing_type);
22171
22172 /* First see if we have it cached. */
22173
22174 if (attr->form == DW_FORM_GNU_ref_alt)
22175 {
22176 struct dwarf2_per_cu_data *per_cu;
22177 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22178
22179 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22180 dwarf2_per_objfile);
22181 this_type = get_die_type_at_offset (sect_off, per_cu);
22182 }
22183 else if (attr_form_is_ref (attr))
22184 {
22185 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22186
22187 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22188 }
22189 else if (attr->form == DW_FORM_ref_sig8)
22190 {
22191 ULONGEST signature = DW_SIGNATURE (attr);
22192
22193 return get_signatured_type (die, signature, cu);
22194 }
22195 else
22196 {
22197 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22198 " at %s [in module %s]"),
22199 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22200 objfile_name (objfile));
22201 return build_error_marker_type (cu, die);
22202 }
22203
22204 /* If not cached we need to read it in. */
22205
22206 if (this_type == NULL)
22207 {
22208 struct die_info *type_die = NULL;
22209 struct dwarf2_cu *type_cu = cu;
22210
22211 if (attr_form_is_ref (attr))
22212 type_die = follow_die_ref (die, attr, &type_cu);
22213 if (type_die == NULL)
22214 return build_error_marker_type (cu, die);
22215 /* If we find the type now, it's probably because the type came
22216 from an inter-CU reference and the type's CU got expanded before
22217 ours. */
22218 this_type = read_type_die (type_die, type_cu);
22219 }
22220
22221 /* If we still don't have a type use an error marker. */
22222
22223 if (this_type == NULL)
22224 return build_error_marker_type (cu, die);
22225
22226 return this_type;
22227 }
22228
22229 /* Return the type in DIE, CU.
22230 Returns NULL for invalid types.
22231
22232 This first does a lookup in die_type_hash,
22233 and only reads the die in if necessary.
22234
22235 NOTE: This can be called when reading in partial or full symbols. */
22236
22237 static struct type *
22238 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22239 {
22240 struct type *this_type;
22241
22242 this_type = get_die_type (die, cu);
22243 if (this_type)
22244 return this_type;
22245
22246 return read_type_die_1 (die, cu);
22247 }
22248
22249 /* Read the type in DIE, CU.
22250 Returns NULL for invalid types. */
22251
22252 static struct type *
22253 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22254 {
22255 struct type *this_type = NULL;
22256
22257 switch (die->tag)
22258 {
22259 case DW_TAG_class_type:
22260 case DW_TAG_interface_type:
22261 case DW_TAG_structure_type:
22262 case DW_TAG_union_type:
22263 this_type = read_structure_type (die, cu);
22264 break;
22265 case DW_TAG_enumeration_type:
22266 this_type = read_enumeration_type (die, cu);
22267 break;
22268 case DW_TAG_subprogram:
22269 case DW_TAG_subroutine_type:
22270 case DW_TAG_inlined_subroutine:
22271 this_type = read_subroutine_type (die, cu);
22272 break;
22273 case DW_TAG_array_type:
22274 this_type = read_array_type (die, cu);
22275 break;
22276 case DW_TAG_set_type:
22277 this_type = read_set_type (die, cu);
22278 break;
22279 case DW_TAG_pointer_type:
22280 this_type = read_tag_pointer_type (die, cu);
22281 break;
22282 case DW_TAG_ptr_to_member_type:
22283 this_type = read_tag_ptr_to_member_type (die, cu);
22284 break;
22285 case DW_TAG_reference_type:
22286 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22287 break;
22288 case DW_TAG_rvalue_reference_type:
22289 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22290 break;
22291 case DW_TAG_const_type:
22292 this_type = read_tag_const_type (die, cu);
22293 break;
22294 case DW_TAG_volatile_type:
22295 this_type = read_tag_volatile_type (die, cu);
22296 break;
22297 case DW_TAG_restrict_type:
22298 this_type = read_tag_restrict_type (die, cu);
22299 break;
22300 case DW_TAG_string_type:
22301 this_type = read_tag_string_type (die, cu);
22302 break;
22303 case DW_TAG_typedef:
22304 this_type = read_typedef (die, cu);
22305 break;
22306 case DW_TAG_subrange_type:
22307 this_type = read_subrange_type (die, cu);
22308 break;
22309 case DW_TAG_base_type:
22310 this_type = read_base_type (die, cu);
22311 break;
22312 case DW_TAG_unspecified_type:
22313 this_type = read_unspecified_type (die, cu);
22314 break;
22315 case DW_TAG_namespace:
22316 this_type = read_namespace_type (die, cu);
22317 break;
22318 case DW_TAG_module:
22319 this_type = read_module_type (die, cu);
22320 break;
22321 case DW_TAG_atomic_type:
22322 this_type = read_tag_atomic_type (die, cu);
22323 break;
22324 default:
22325 complaint (_("unexpected tag in read_type_die: '%s'"),
22326 dwarf_tag_name (die->tag));
22327 break;
22328 }
22329
22330 return this_type;
22331 }
22332
22333 /* See if we can figure out if the class lives in a namespace. We do
22334 this by looking for a member function; its demangled name will
22335 contain namespace info, if there is any.
22336 Return the computed name or NULL.
22337 Space for the result is allocated on the objfile's obstack.
22338 This is the full-die version of guess_partial_die_structure_name.
22339 In this case we know DIE has no useful parent. */
22340
22341 static char *
22342 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22343 {
22344 struct die_info *spec_die;
22345 struct dwarf2_cu *spec_cu;
22346 struct die_info *child;
22347 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22348
22349 spec_cu = cu;
22350 spec_die = die_specification (die, &spec_cu);
22351 if (spec_die != NULL)
22352 {
22353 die = spec_die;
22354 cu = spec_cu;
22355 }
22356
22357 for (child = die->child;
22358 child != NULL;
22359 child = child->sibling)
22360 {
22361 if (child->tag == DW_TAG_subprogram)
22362 {
22363 const char *linkage_name = dw2_linkage_name (child, cu);
22364
22365 if (linkage_name != NULL)
22366 {
22367 char *actual_name
22368 = language_class_name_from_physname (cu->language_defn,
22369 linkage_name);
22370 char *name = NULL;
22371
22372 if (actual_name != NULL)
22373 {
22374 const char *die_name = dwarf2_name (die, cu);
22375
22376 if (die_name != NULL
22377 && strcmp (die_name, actual_name) != 0)
22378 {
22379 /* Strip off the class name from the full name.
22380 We want the prefix. */
22381 int die_name_len = strlen (die_name);
22382 int actual_name_len = strlen (actual_name);
22383
22384 /* Test for '::' as a sanity check. */
22385 if (actual_name_len > die_name_len + 2
22386 && actual_name[actual_name_len
22387 - die_name_len - 1] == ':')
22388 name = obstack_strndup (
22389 &objfile->per_bfd->storage_obstack,
22390 actual_name, actual_name_len - die_name_len - 2);
22391 }
22392 }
22393 xfree (actual_name);
22394 return name;
22395 }
22396 }
22397 }
22398
22399 return NULL;
22400 }
22401
22402 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22403 prefix part in such case. See
22404 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22405
22406 static const char *
22407 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22408 {
22409 struct attribute *attr;
22410 const char *base;
22411
22412 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22413 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22414 return NULL;
22415
22416 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22417 return NULL;
22418
22419 attr = dw2_linkage_name_attr (die, cu);
22420 if (attr == NULL || DW_STRING (attr) == NULL)
22421 return NULL;
22422
22423 /* dwarf2_name had to be already called. */
22424 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22425
22426 /* Strip the base name, keep any leading namespaces/classes. */
22427 base = strrchr (DW_STRING (attr), ':');
22428 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22429 return "";
22430
22431 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22432 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22433 DW_STRING (attr),
22434 &base[-1] - DW_STRING (attr));
22435 }
22436
22437 /* Return the name of the namespace/class that DIE is defined within,
22438 or "" if we can't tell. The caller should not xfree the result.
22439
22440 For example, if we're within the method foo() in the following
22441 code:
22442
22443 namespace N {
22444 class C {
22445 void foo () {
22446 }
22447 };
22448 }
22449
22450 then determine_prefix on foo's die will return "N::C". */
22451
22452 static const char *
22453 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22454 {
22455 struct dwarf2_per_objfile *dwarf2_per_objfile
22456 = cu->per_cu->dwarf2_per_objfile;
22457 struct die_info *parent, *spec_die;
22458 struct dwarf2_cu *spec_cu;
22459 struct type *parent_type;
22460 const char *retval;
22461
22462 if (cu->language != language_cplus
22463 && cu->language != language_fortran && cu->language != language_d
22464 && cu->language != language_rust)
22465 return "";
22466
22467 retval = anonymous_struct_prefix (die, cu);
22468 if (retval)
22469 return retval;
22470
22471 /* We have to be careful in the presence of DW_AT_specification.
22472 For example, with GCC 3.4, given the code
22473
22474 namespace N {
22475 void foo() {
22476 // Definition of N::foo.
22477 }
22478 }
22479
22480 then we'll have a tree of DIEs like this:
22481
22482 1: DW_TAG_compile_unit
22483 2: DW_TAG_namespace // N
22484 3: DW_TAG_subprogram // declaration of N::foo
22485 4: DW_TAG_subprogram // definition of N::foo
22486 DW_AT_specification // refers to die #3
22487
22488 Thus, when processing die #4, we have to pretend that we're in
22489 the context of its DW_AT_specification, namely the contex of die
22490 #3. */
22491 spec_cu = cu;
22492 spec_die = die_specification (die, &spec_cu);
22493 if (spec_die == NULL)
22494 parent = die->parent;
22495 else
22496 {
22497 parent = spec_die->parent;
22498 cu = spec_cu;
22499 }
22500
22501 if (parent == NULL)
22502 return "";
22503 else if (parent->building_fullname)
22504 {
22505 const char *name;
22506 const char *parent_name;
22507
22508 /* It has been seen on RealView 2.2 built binaries,
22509 DW_TAG_template_type_param types actually _defined_ as
22510 children of the parent class:
22511
22512 enum E {};
22513 template class <class Enum> Class{};
22514 Class<enum E> class_e;
22515
22516 1: DW_TAG_class_type (Class)
22517 2: DW_TAG_enumeration_type (E)
22518 3: DW_TAG_enumerator (enum1:0)
22519 3: DW_TAG_enumerator (enum2:1)
22520 ...
22521 2: DW_TAG_template_type_param
22522 DW_AT_type DW_FORM_ref_udata (E)
22523
22524 Besides being broken debug info, it can put GDB into an
22525 infinite loop. Consider:
22526
22527 When we're building the full name for Class<E>, we'll start
22528 at Class, and go look over its template type parameters,
22529 finding E. We'll then try to build the full name of E, and
22530 reach here. We're now trying to build the full name of E,
22531 and look over the parent DIE for containing scope. In the
22532 broken case, if we followed the parent DIE of E, we'd again
22533 find Class, and once again go look at its template type
22534 arguments, etc., etc. Simply don't consider such parent die
22535 as source-level parent of this die (it can't be, the language
22536 doesn't allow it), and break the loop here. */
22537 name = dwarf2_name (die, cu);
22538 parent_name = dwarf2_name (parent, cu);
22539 complaint (_("template param type '%s' defined within parent '%s'"),
22540 name ? name : "<unknown>",
22541 parent_name ? parent_name : "<unknown>");
22542 return "";
22543 }
22544 else
22545 switch (parent->tag)
22546 {
22547 case DW_TAG_namespace:
22548 parent_type = read_type_die (parent, cu);
22549 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22550 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22551 Work around this problem here. */
22552 if (cu->language == language_cplus
22553 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22554 return "";
22555 /* We give a name to even anonymous namespaces. */
22556 return TYPE_NAME (parent_type);
22557 case DW_TAG_class_type:
22558 case DW_TAG_interface_type:
22559 case DW_TAG_structure_type:
22560 case DW_TAG_union_type:
22561 case DW_TAG_module:
22562 parent_type = read_type_die (parent, cu);
22563 if (TYPE_NAME (parent_type) != NULL)
22564 return TYPE_NAME (parent_type);
22565 else
22566 /* An anonymous structure is only allowed non-static data
22567 members; no typedefs, no member functions, et cetera.
22568 So it does not need a prefix. */
22569 return "";
22570 case DW_TAG_compile_unit:
22571 case DW_TAG_partial_unit:
22572 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22573 if (cu->language == language_cplus
22574 && !dwarf2_per_objfile->types.empty ()
22575 && die->child != NULL
22576 && (die->tag == DW_TAG_class_type
22577 || die->tag == DW_TAG_structure_type
22578 || die->tag == DW_TAG_union_type))
22579 {
22580 char *name = guess_full_die_structure_name (die, cu);
22581 if (name != NULL)
22582 return name;
22583 }
22584 return "";
22585 case DW_TAG_enumeration_type:
22586 parent_type = read_type_die (parent, cu);
22587 if (TYPE_DECLARED_CLASS (parent_type))
22588 {
22589 if (TYPE_NAME (parent_type) != NULL)
22590 return TYPE_NAME (parent_type);
22591 return "";
22592 }
22593 /* Fall through. */
22594 default:
22595 return determine_prefix (parent, cu);
22596 }
22597 }
22598
22599 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22600 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22601 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22602 an obconcat, otherwise allocate storage for the result. The CU argument is
22603 used to determine the language and hence, the appropriate separator. */
22604
22605 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22606
22607 static char *
22608 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22609 int physname, struct dwarf2_cu *cu)
22610 {
22611 const char *lead = "";
22612 const char *sep;
22613
22614 if (suffix == NULL || suffix[0] == '\0'
22615 || prefix == NULL || prefix[0] == '\0')
22616 sep = "";
22617 else if (cu->language == language_d)
22618 {
22619 /* For D, the 'main' function could be defined in any module, but it
22620 should never be prefixed. */
22621 if (strcmp (suffix, "D main") == 0)
22622 {
22623 prefix = "";
22624 sep = "";
22625 }
22626 else
22627 sep = ".";
22628 }
22629 else if (cu->language == language_fortran && physname)
22630 {
22631 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22632 DW_AT_MIPS_linkage_name is preferred and used instead. */
22633
22634 lead = "__";
22635 sep = "_MOD_";
22636 }
22637 else
22638 sep = "::";
22639
22640 if (prefix == NULL)
22641 prefix = "";
22642 if (suffix == NULL)
22643 suffix = "";
22644
22645 if (obs == NULL)
22646 {
22647 char *retval
22648 = ((char *)
22649 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22650
22651 strcpy (retval, lead);
22652 strcat (retval, prefix);
22653 strcat (retval, sep);
22654 strcat (retval, suffix);
22655 return retval;
22656 }
22657 else
22658 {
22659 /* We have an obstack. */
22660 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22661 }
22662 }
22663
22664 /* Return sibling of die, NULL if no sibling. */
22665
22666 static struct die_info *
22667 sibling_die (struct die_info *die)
22668 {
22669 return die->sibling;
22670 }
22671
22672 /* Get name of a die, return NULL if not found. */
22673
22674 static const char *
22675 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22676 struct obstack *obstack)
22677 {
22678 if (name && cu->language == language_cplus)
22679 {
22680 std::string canon_name = cp_canonicalize_string (name);
22681
22682 if (!canon_name.empty ())
22683 {
22684 if (canon_name != name)
22685 name = obstack_strdup (obstack, canon_name);
22686 }
22687 }
22688
22689 return name;
22690 }
22691
22692 /* Get name of a die, return NULL if not found.
22693 Anonymous namespaces are converted to their magic string. */
22694
22695 static const char *
22696 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22697 {
22698 struct attribute *attr;
22699 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22700
22701 attr = dwarf2_attr (die, DW_AT_name, cu);
22702 if ((!attr || !DW_STRING (attr))
22703 && die->tag != DW_TAG_namespace
22704 && die->tag != DW_TAG_class_type
22705 && die->tag != DW_TAG_interface_type
22706 && die->tag != DW_TAG_structure_type
22707 && die->tag != DW_TAG_union_type)
22708 return NULL;
22709
22710 switch (die->tag)
22711 {
22712 case DW_TAG_compile_unit:
22713 case DW_TAG_partial_unit:
22714 /* Compilation units have a DW_AT_name that is a filename, not
22715 a source language identifier. */
22716 case DW_TAG_enumeration_type:
22717 case DW_TAG_enumerator:
22718 /* These tags always have simple identifiers already; no need
22719 to canonicalize them. */
22720 return DW_STRING (attr);
22721
22722 case DW_TAG_namespace:
22723 if (attr != NULL && DW_STRING (attr) != NULL)
22724 return DW_STRING (attr);
22725 return CP_ANONYMOUS_NAMESPACE_STR;
22726
22727 case DW_TAG_class_type:
22728 case DW_TAG_interface_type:
22729 case DW_TAG_structure_type:
22730 case DW_TAG_union_type:
22731 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22732 structures or unions. These were of the form "._%d" in GCC 4.1,
22733 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22734 and GCC 4.4. We work around this problem by ignoring these. */
22735 if (attr && DW_STRING (attr)
22736 && (startswith (DW_STRING (attr), "._")
22737 || startswith (DW_STRING (attr), "<anonymous")))
22738 return NULL;
22739
22740 /* GCC might emit a nameless typedef that has a linkage name. See
22741 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22742 if (!attr || DW_STRING (attr) == NULL)
22743 {
22744 char *demangled = NULL;
22745
22746 attr = dw2_linkage_name_attr (die, cu);
22747 if (attr == NULL || DW_STRING (attr) == NULL)
22748 return NULL;
22749
22750 /* Avoid demangling DW_STRING (attr) the second time on a second
22751 call for the same DIE. */
22752 if (!DW_STRING_IS_CANONICAL (attr))
22753 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22754
22755 if (demangled)
22756 {
22757 const char *base;
22758
22759 /* FIXME: we already did this for the partial symbol... */
22760 DW_STRING (attr)
22761 = obstack_strdup (&objfile->per_bfd->storage_obstack,
22762 demangled);
22763 DW_STRING_IS_CANONICAL (attr) = 1;
22764 xfree (demangled);
22765
22766 /* Strip any leading namespaces/classes, keep only the base name.
22767 DW_AT_name for named DIEs does not contain the prefixes. */
22768 base = strrchr (DW_STRING (attr), ':');
22769 if (base && base > DW_STRING (attr) && base[-1] == ':')
22770 return &base[1];
22771 else
22772 return DW_STRING (attr);
22773 }
22774 }
22775 break;
22776
22777 default:
22778 break;
22779 }
22780
22781 if (!DW_STRING_IS_CANONICAL (attr))
22782 {
22783 DW_STRING (attr)
22784 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22785 &objfile->per_bfd->storage_obstack);
22786 DW_STRING_IS_CANONICAL (attr) = 1;
22787 }
22788 return DW_STRING (attr);
22789 }
22790
22791 /* Return the die that this die in an extension of, or NULL if there
22792 is none. *EXT_CU is the CU containing DIE on input, and the CU
22793 containing the return value on output. */
22794
22795 static struct die_info *
22796 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22797 {
22798 struct attribute *attr;
22799
22800 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22801 if (attr == NULL)
22802 return NULL;
22803
22804 return follow_die_ref (die, attr, ext_cu);
22805 }
22806
22807 /* A convenience function that returns an "unknown" DWARF name,
22808 including the value of V. STR is the name of the entity being
22809 printed, e.g., "TAG". */
22810
22811 static const char *
22812 dwarf_unknown (const char *str, unsigned v)
22813 {
22814 char *cell = get_print_cell ();
22815 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
22816 return cell;
22817 }
22818
22819 /* Convert a DIE tag into its string name. */
22820
22821 static const char *
22822 dwarf_tag_name (unsigned tag)
22823 {
22824 const char *name = get_DW_TAG_name (tag);
22825
22826 if (name == NULL)
22827 return dwarf_unknown ("TAG", tag);
22828
22829 return name;
22830 }
22831
22832 /* Convert a DWARF attribute code into its string name. */
22833
22834 static const char *
22835 dwarf_attr_name (unsigned attr)
22836 {
22837 const char *name;
22838
22839 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22840 if (attr == DW_AT_MIPS_fde)
22841 return "DW_AT_MIPS_fde";
22842 #else
22843 if (attr == DW_AT_HP_block_index)
22844 return "DW_AT_HP_block_index";
22845 #endif
22846
22847 name = get_DW_AT_name (attr);
22848
22849 if (name == NULL)
22850 return dwarf_unknown ("AT", attr);
22851
22852 return name;
22853 }
22854
22855 /* Convert a unit type to corresponding DW_UT name. */
22856
22857 static const char *
22858 dwarf_unit_type_name (int unit_type) {
22859 switch (unit_type)
22860 {
22861 case 0x01:
22862 return "DW_UT_compile (0x01)";
22863 case 0x02:
22864 return "DW_UT_type (0x02)";
22865 case 0x03:
22866 return "DW_UT_partial (0x03)";
22867 case 0x04:
22868 return "DW_UT_skeleton (0x04)";
22869 case 0x05:
22870 return "DW_UT_split_compile (0x05)";
22871 case 0x06:
22872 return "DW_UT_split_type (0x06)";
22873 case 0x80:
22874 return "DW_UT_lo_user (0x80)";
22875 case 0xff:
22876 return "DW_UT_hi_user (0xff)";
22877 default:
22878 return nullptr;
22879 }
22880 }
22881
22882 /* Convert a DWARF value form code into its string name. */
22883
22884 static const char *
22885 dwarf_form_name (unsigned form)
22886 {
22887 const char *name = get_DW_FORM_name (form);
22888
22889 if (name == NULL)
22890 return dwarf_unknown ("FORM", form);
22891
22892 return name;
22893 }
22894
22895 static const char *
22896 dwarf_bool_name (unsigned mybool)
22897 {
22898 if (mybool)
22899 return "TRUE";
22900 else
22901 return "FALSE";
22902 }
22903
22904 /* Convert a DWARF type code into its string name. */
22905
22906 static const char *
22907 dwarf_type_encoding_name (unsigned enc)
22908 {
22909 const char *name = get_DW_ATE_name (enc);
22910
22911 if (name == NULL)
22912 return dwarf_unknown ("ATE", enc);
22913
22914 return name;
22915 }
22916
22917 static void
22918 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22919 {
22920 unsigned int i;
22921
22922 print_spaces (indent, f);
22923 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22924 dwarf_tag_name (die->tag), die->abbrev,
22925 sect_offset_str (die->sect_off));
22926
22927 if (die->parent != NULL)
22928 {
22929 print_spaces (indent, f);
22930 fprintf_unfiltered (f, " parent at offset: %s\n",
22931 sect_offset_str (die->parent->sect_off));
22932 }
22933
22934 print_spaces (indent, f);
22935 fprintf_unfiltered (f, " has children: %s\n",
22936 dwarf_bool_name (die->child != NULL));
22937
22938 print_spaces (indent, f);
22939 fprintf_unfiltered (f, " attributes:\n");
22940
22941 for (i = 0; i < die->num_attrs; ++i)
22942 {
22943 print_spaces (indent, f);
22944 fprintf_unfiltered (f, " %s (%s) ",
22945 dwarf_attr_name (die->attrs[i].name),
22946 dwarf_form_name (die->attrs[i].form));
22947
22948 switch (die->attrs[i].form)
22949 {
22950 case DW_FORM_addr:
22951 case DW_FORM_addrx:
22952 case DW_FORM_GNU_addr_index:
22953 fprintf_unfiltered (f, "address: ");
22954 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22955 break;
22956 case DW_FORM_block2:
22957 case DW_FORM_block4:
22958 case DW_FORM_block:
22959 case DW_FORM_block1:
22960 fprintf_unfiltered (f, "block: size %s",
22961 pulongest (DW_BLOCK (&die->attrs[i])->size));
22962 break;
22963 case DW_FORM_exprloc:
22964 fprintf_unfiltered (f, "expression: size %s",
22965 pulongest (DW_BLOCK (&die->attrs[i])->size));
22966 break;
22967 case DW_FORM_data16:
22968 fprintf_unfiltered (f, "constant of 16 bytes");
22969 break;
22970 case DW_FORM_ref_addr:
22971 fprintf_unfiltered (f, "ref address: ");
22972 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22973 break;
22974 case DW_FORM_GNU_ref_alt:
22975 fprintf_unfiltered (f, "alt ref address: ");
22976 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22977 break;
22978 case DW_FORM_ref1:
22979 case DW_FORM_ref2:
22980 case DW_FORM_ref4:
22981 case DW_FORM_ref8:
22982 case DW_FORM_ref_udata:
22983 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22984 (long) (DW_UNSND (&die->attrs[i])));
22985 break;
22986 case DW_FORM_data1:
22987 case DW_FORM_data2:
22988 case DW_FORM_data4:
22989 case DW_FORM_data8:
22990 case DW_FORM_udata:
22991 case DW_FORM_sdata:
22992 fprintf_unfiltered (f, "constant: %s",
22993 pulongest (DW_UNSND (&die->attrs[i])));
22994 break;
22995 case DW_FORM_sec_offset:
22996 fprintf_unfiltered (f, "section offset: %s",
22997 pulongest (DW_UNSND (&die->attrs[i])));
22998 break;
22999 case DW_FORM_ref_sig8:
23000 fprintf_unfiltered (f, "signature: %s",
23001 hex_string (DW_SIGNATURE (&die->attrs[i])));
23002 break;
23003 case DW_FORM_string:
23004 case DW_FORM_strp:
23005 case DW_FORM_line_strp:
23006 case DW_FORM_strx:
23007 case DW_FORM_GNU_str_index:
23008 case DW_FORM_GNU_strp_alt:
23009 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23010 DW_STRING (&die->attrs[i])
23011 ? DW_STRING (&die->attrs[i]) : "",
23012 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
23013 break;
23014 case DW_FORM_flag:
23015 if (DW_UNSND (&die->attrs[i]))
23016 fprintf_unfiltered (f, "flag: TRUE");
23017 else
23018 fprintf_unfiltered (f, "flag: FALSE");
23019 break;
23020 case DW_FORM_flag_present:
23021 fprintf_unfiltered (f, "flag: TRUE");
23022 break;
23023 case DW_FORM_indirect:
23024 /* The reader will have reduced the indirect form to
23025 the "base form" so this form should not occur. */
23026 fprintf_unfiltered (f,
23027 "unexpected attribute form: DW_FORM_indirect");
23028 break;
23029 case DW_FORM_implicit_const:
23030 fprintf_unfiltered (f, "constant: %s",
23031 plongest (DW_SND (&die->attrs[i])));
23032 break;
23033 default:
23034 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23035 die->attrs[i].form);
23036 break;
23037 }
23038 fprintf_unfiltered (f, "\n");
23039 }
23040 }
23041
23042 static void
23043 dump_die_for_error (struct die_info *die)
23044 {
23045 dump_die_shallow (gdb_stderr, 0, die);
23046 }
23047
23048 static void
23049 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23050 {
23051 int indent = level * 4;
23052
23053 gdb_assert (die != NULL);
23054
23055 if (level >= max_level)
23056 return;
23057
23058 dump_die_shallow (f, indent, die);
23059
23060 if (die->child != NULL)
23061 {
23062 print_spaces (indent, f);
23063 fprintf_unfiltered (f, " Children:");
23064 if (level + 1 < max_level)
23065 {
23066 fprintf_unfiltered (f, "\n");
23067 dump_die_1 (f, level + 1, max_level, die->child);
23068 }
23069 else
23070 {
23071 fprintf_unfiltered (f,
23072 " [not printed, max nesting level reached]\n");
23073 }
23074 }
23075
23076 if (die->sibling != NULL && level > 0)
23077 {
23078 dump_die_1 (f, level, max_level, die->sibling);
23079 }
23080 }
23081
23082 /* This is called from the pdie macro in gdbinit.in.
23083 It's not static so gcc will keep a copy callable from gdb. */
23084
23085 void
23086 dump_die (struct die_info *die, int max_level)
23087 {
23088 dump_die_1 (gdb_stdlog, 0, max_level, die);
23089 }
23090
23091 static void
23092 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23093 {
23094 void **slot;
23095
23096 slot = htab_find_slot_with_hash (cu->die_hash, die,
23097 to_underlying (die->sect_off),
23098 INSERT);
23099
23100 *slot = die;
23101 }
23102
23103 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23104 required kind. */
23105
23106 static sect_offset
23107 dwarf2_get_ref_die_offset (const struct attribute *attr)
23108 {
23109 if (attr_form_is_ref (attr))
23110 return (sect_offset) DW_UNSND (attr);
23111
23112 complaint (_("unsupported die ref attribute form: '%s'"),
23113 dwarf_form_name (attr->form));
23114 return {};
23115 }
23116
23117 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23118 * the value held by the attribute is not constant. */
23119
23120 static LONGEST
23121 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23122 {
23123 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23124 return DW_SND (attr);
23125 else if (attr->form == DW_FORM_udata
23126 || attr->form == DW_FORM_data1
23127 || attr->form == DW_FORM_data2
23128 || attr->form == DW_FORM_data4
23129 || attr->form == DW_FORM_data8)
23130 return DW_UNSND (attr);
23131 else
23132 {
23133 /* For DW_FORM_data16 see attr_form_is_constant. */
23134 complaint (_("Attribute value is not a constant (%s)"),
23135 dwarf_form_name (attr->form));
23136 return default_value;
23137 }
23138 }
23139
23140 /* Follow reference or signature attribute ATTR of SRC_DIE.
23141 On entry *REF_CU is the CU of SRC_DIE.
23142 On exit *REF_CU is the CU of the result. */
23143
23144 static struct die_info *
23145 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23146 struct dwarf2_cu **ref_cu)
23147 {
23148 struct die_info *die;
23149
23150 if (attr_form_is_ref (attr))
23151 die = follow_die_ref (src_die, attr, ref_cu);
23152 else if (attr->form == DW_FORM_ref_sig8)
23153 die = follow_die_sig (src_die, attr, ref_cu);
23154 else
23155 {
23156 dump_die_for_error (src_die);
23157 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23158 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23159 }
23160
23161 return die;
23162 }
23163
23164 /* Follow reference OFFSET.
23165 On entry *REF_CU is the CU of the source die referencing OFFSET.
23166 On exit *REF_CU is the CU of the result.
23167 Returns NULL if OFFSET is invalid. */
23168
23169 static struct die_info *
23170 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23171 struct dwarf2_cu **ref_cu)
23172 {
23173 struct die_info temp_die;
23174 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23175 struct dwarf2_per_objfile *dwarf2_per_objfile
23176 = cu->per_cu->dwarf2_per_objfile;
23177
23178 gdb_assert (cu->per_cu != NULL);
23179
23180 target_cu = cu;
23181
23182 if (cu->per_cu->is_debug_types)
23183 {
23184 /* .debug_types CUs cannot reference anything outside their CU.
23185 If they need to, they have to reference a signatured type via
23186 DW_FORM_ref_sig8. */
23187 if (!offset_in_cu_p (&cu->header, sect_off))
23188 return NULL;
23189 }
23190 else if (offset_in_dwz != cu->per_cu->is_dwz
23191 || !offset_in_cu_p (&cu->header, sect_off))
23192 {
23193 struct dwarf2_per_cu_data *per_cu;
23194
23195 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23196 dwarf2_per_objfile);
23197
23198 /* If necessary, add it to the queue and load its DIEs. */
23199 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23200 load_full_comp_unit (per_cu, false, cu->language);
23201
23202 target_cu = per_cu->cu;
23203 }
23204 else if (cu->dies == NULL)
23205 {
23206 /* We're loading full DIEs during partial symbol reading. */
23207 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23208 load_full_comp_unit (cu->per_cu, false, language_minimal);
23209 }
23210
23211 *ref_cu = target_cu;
23212 temp_die.sect_off = sect_off;
23213
23214 if (target_cu != cu)
23215 target_cu->ancestor = cu;
23216
23217 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23218 &temp_die,
23219 to_underlying (sect_off));
23220 }
23221
23222 /* Follow reference attribute ATTR of SRC_DIE.
23223 On entry *REF_CU is the CU of SRC_DIE.
23224 On exit *REF_CU is the CU of the result. */
23225
23226 static struct die_info *
23227 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23228 struct dwarf2_cu **ref_cu)
23229 {
23230 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23231 struct dwarf2_cu *cu = *ref_cu;
23232 struct die_info *die;
23233
23234 die = follow_die_offset (sect_off,
23235 (attr->form == DW_FORM_GNU_ref_alt
23236 || cu->per_cu->is_dwz),
23237 ref_cu);
23238 if (!die)
23239 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23240 "at %s [in module %s]"),
23241 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23242 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23243
23244 return die;
23245 }
23246
23247 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23248 Returned value is intended for DW_OP_call*. Returned
23249 dwarf2_locexpr_baton->data has lifetime of
23250 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23251
23252 struct dwarf2_locexpr_baton
23253 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23254 struct dwarf2_per_cu_data *per_cu,
23255 CORE_ADDR (*get_frame_pc) (void *baton),
23256 void *baton, bool resolve_abstract_p)
23257 {
23258 struct dwarf2_cu *cu;
23259 struct die_info *die;
23260 struct attribute *attr;
23261 struct dwarf2_locexpr_baton retval;
23262 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23263 struct objfile *objfile = dwarf2_per_objfile->objfile;
23264
23265 if (per_cu->cu == NULL)
23266 load_cu (per_cu, false);
23267 cu = per_cu->cu;
23268 if (cu == NULL)
23269 {
23270 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23271 Instead just throw an error, not much else we can do. */
23272 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23273 sect_offset_str (sect_off), objfile_name (objfile));
23274 }
23275
23276 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23277 if (!die)
23278 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23279 sect_offset_str (sect_off), objfile_name (objfile));
23280
23281 attr = dwarf2_attr (die, DW_AT_location, cu);
23282 if (!attr && resolve_abstract_p
23283 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
23284 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23285 {
23286 CORE_ADDR pc = (*get_frame_pc) (baton);
23287 CORE_ADDR baseaddr
23288 = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23289 struct gdbarch *gdbarch = get_objfile_arch (objfile);
23290
23291 for (const auto &cand_off
23292 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
23293 {
23294 struct dwarf2_cu *cand_cu = cu;
23295 struct die_info *cand
23296 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23297 if (!cand
23298 || !cand->parent
23299 || cand->parent->tag != DW_TAG_subprogram)
23300 continue;
23301
23302 CORE_ADDR pc_low, pc_high;
23303 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23304 if (pc_low == ((CORE_ADDR) -1))
23305 continue;
23306 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23307 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23308 if (!(pc_low <= pc && pc < pc_high))
23309 continue;
23310
23311 die = cand;
23312 attr = dwarf2_attr (die, DW_AT_location, cu);
23313 break;
23314 }
23315 }
23316
23317 if (!attr)
23318 {
23319 /* DWARF: "If there is no such attribute, then there is no effect.".
23320 DATA is ignored if SIZE is 0. */
23321
23322 retval.data = NULL;
23323 retval.size = 0;
23324 }
23325 else if (attr_form_is_section_offset (attr))
23326 {
23327 struct dwarf2_loclist_baton loclist_baton;
23328 CORE_ADDR pc = (*get_frame_pc) (baton);
23329 size_t size;
23330
23331 fill_in_loclist_baton (cu, &loclist_baton, attr);
23332
23333 retval.data = dwarf2_find_location_expression (&loclist_baton,
23334 &size, pc);
23335 retval.size = size;
23336 }
23337 else
23338 {
23339 if (!attr_form_is_block (attr))
23340 error (_("Dwarf Error: DIE at %s referenced in module %s "
23341 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23342 sect_offset_str (sect_off), objfile_name (objfile));
23343
23344 retval.data = DW_BLOCK (attr)->data;
23345 retval.size = DW_BLOCK (attr)->size;
23346 }
23347 retval.per_cu = cu->per_cu;
23348
23349 age_cached_comp_units (dwarf2_per_objfile);
23350
23351 return retval;
23352 }
23353
23354 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23355 offset. */
23356
23357 struct dwarf2_locexpr_baton
23358 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23359 struct dwarf2_per_cu_data *per_cu,
23360 CORE_ADDR (*get_frame_pc) (void *baton),
23361 void *baton)
23362 {
23363 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23364
23365 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23366 }
23367
23368 /* Write a constant of a given type as target-ordered bytes into
23369 OBSTACK. */
23370
23371 static const gdb_byte *
23372 write_constant_as_bytes (struct obstack *obstack,
23373 enum bfd_endian byte_order,
23374 struct type *type,
23375 ULONGEST value,
23376 LONGEST *len)
23377 {
23378 gdb_byte *result;
23379
23380 *len = TYPE_LENGTH (type);
23381 result = (gdb_byte *) obstack_alloc (obstack, *len);
23382 store_unsigned_integer (result, *len, byte_order, value);
23383
23384 return result;
23385 }
23386
23387 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23388 pointer to the constant bytes and set LEN to the length of the
23389 data. If memory is needed, allocate it on OBSTACK. If the DIE
23390 does not have a DW_AT_const_value, return NULL. */
23391
23392 const gdb_byte *
23393 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23394 struct dwarf2_per_cu_data *per_cu,
23395 struct obstack *obstack,
23396 LONGEST *len)
23397 {
23398 struct dwarf2_cu *cu;
23399 struct die_info *die;
23400 struct attribute *attr;
23401 const gdb_byte *result = NULL;
23402 struct type *type;
23403 LONGEST value;
23404 enum bfd_endian byte_order;
23405 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23406
23407 if (per_cu->cu == NULL)
23408 load_cu (per_cu, false);
23409 cu = per_cu->cu;
23410 if (cu == NULL)
23411 {
23412 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23413 Instead just throw an error, not much else we can do. */
23414 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23415 sect_offset_str (sect_off), objfile_name (objfile));
23416 }
23417
23418 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23419 if (!die)
23420 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23421 sect_offset_str (sect_off), objfile_name (objfile));
23422
23423 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23424 if (attr == NULL)
23425 return NULL;
23426
23427 byte_order = (bfd_big_endian (objfile->obfd)
23428 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23429
23430 switch (attr->form)
23431 {
23432 case DW_FORM_addr:
23433 case DW_FORM_addrx:
23434 case DW_FORM_GNU_addr_index:
23435 {
23436 gdb_byte *tem;
23437
23438 *len = cu->header.addr_size;
23439 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23440 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23441 result = tem;
23442 }
23443 break;
23444 case DW_FORM_string:
23445 case DW_FORM_strp:
23446 case DW_FORM_strx:
23447 case DW_FORM_GNU_str_index:
23448 case DW_FORM_GNU_strp_alt:
23449 /* DW_STRING is already allocated on the objfile obstack, point
23450 directly to it. */
23451 result = (const gdb_byte *) DW_STRING (attr);
23452 *len = strlen (DW_STRING (attr));
23453 break;
23454 case DW_FORM_block1:
23455 case DW_FORM_block2:
23456 case DW_FORM_block4:
23457 case DW_FORM_block:
23458 case DW_FORM_exprloc:
23459 case DW_FORM_data16:
23460 result = DW_BLOCK (attr)->data;
23461 *len = DW_BLOCK (attr)->size;
23462 break;
23463
23464 /* The DW_AT_const_value attributes are supposed to carry the
23465 symbol's value "represented as it would be on the target
23466 architecture." By the time we get here, it's already been
23467 converted to host endianness, so we just need to sign- or
23468 zero-extend it as appropriate. */
23469 case DW_FORM_data1:
23470 type = die_type (die, cu);
23471 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23472 if (result == NULL)
23473 result = write_constant_as_bytes (obstack, byte_order,
23474 type, value, len);
23475 break;
23476 case DW_FORM_data2:
23477 type = die_type (die, cu);
23478 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23479 if (result == NULL)
23480 result = write_constant_as_bytes (obstack, byte_order,
23481 type, value, len);
23482 break;
23483 case DW_FORM_data4:
23484 type = die_type (die, cu);
23485 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23486 if (result == NULL)
23487 result = write_constant_as_bytes (obstack, byte_order,
23488 type, value, len);
23489 break;
23490 case DW_FORM_data8:
23491 type = die_type (die, cu);
23492 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23493 if (result == NULL)
23494 result = write_constant_as_bytes (obstack, byte_order,
23495 type, value, len);
23496 break;
23497
23498 case DW_FORM_sdata:
23499 case DW_FORM_implicit_const:
23500 type = die_type (die, cu);
23501 result = write_constant_as_bytes (obstack, byte_order,
23502 type, DW_SND (attr), len);
23503 break;
23504
23505 case DW_FORM_udata:
23506 type = die_type (die, cu);
23507 result = write_constant_as_bytes (obstack, byte_order,
23508 type, DW_UNSND (attr), len);
23509 break;
23510
23511 default:
23512 complaint (_("unsupported const value attribute form: '%s'"),
23513 dwarf_form_name (attr->form));
23514 break;
23515 }
23516
23517 return result;
23518 }
23519
23520 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23521 valid type for this die is found. */
23522
23523 struct type *
23524 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23525 struct dwarf2_per_cu_data *per_cu)
23526 {
23527 struct dwarf2_cu *cu;
23528 struct die_info *die;
23529
23530 if (per_cu->cu == NULL)
23531 load_cu (per_cu, false);
23532 cu = per_cu->cu;
23533 if (!cu)
23534 return NULL;
23535
23536 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23537 if (!die)
23538 return NULL;
23539
23540 return die_type (die, cu);
23541 }
23542
23543 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23544 PER_CU. */
23545
23546 struct type *
23547 dwarf2_get_die_type (cu_offset die_offset,
23548 struct dwarf2_per_cu_data *per_cu)
23549 {
23550 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23551 return get_die_type_at_offset (die_offset_sect, per_cu);
23552 }
23553
23554 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23555 On entry *REF_CU is the CU of SRC_DIE.
23556 On exit *REF_CU is the CU of the result.
23557 Returns NULL if the referenced DIE isn't found. */
23558
23559 static struct die_info *
23560 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23561 struct dwarf2_cu **ref_cu)
23562 {
23563 struct die_info temp_die;
23564 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23565 struct die_info *die;
23566
23567 /* While it might be nice to assert sig_type->type == NULL here,
23568 we can get here for DW_AT_imported_declaration where we need
23569 the DIE not the type. */
23570
23571 /* If necessary, add it to the queue and load its DIEs. */
23572
23573 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23574 read_signatured_type (sig_type);
23575
23576 sig_cu = sig_type->per_cu.cu;
23577 gdb_assert (sig_cu != NULL);
23578 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23579 temp_die.sect_off = sig_type->type_offset_in_section;
23580 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23581 to_underlying (temp_die.sect_off));
23582 if (die)
23583 {
23584 struct dwarf2_per_objfile *dwarf2_per_objfile
23585 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23586
23587 /* For .gdb_index version 7 keep track of included TUs.
23588 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23589 if (dwarf2_per_objfile->index_table != NULL
23590 && dwarf2_per_objfile->index_table->version <= 7)
23591 {
23592 VEC_safe_push (dwarf2_per_cu_ptr,
23593 (*ref_cu)->per_cu->imported_symtabs,
23594 sig_cu->per_cu);
23595 }
23596
23597 *ref_cu = sig_cu;
23598 if (sig_cu != cu)
23599 sig_cu->ancestor = cu;
23600
23601 return die;
23602 }
23603
23604 return NULL;
23605 }
23606
23607 /* Follow signatured type referenced by ATTR in SRC_DIE.
23608 On entry *REF_CU is the CU of SRC_DIE.
23609 On exit *REF_CU is the CU of the result.
23610 The result is the DIE of the type.
23611 If the referenced type cannot be found an error is thrown. */
23612
23613 static struct die_info *
23614 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23615 struct dwarf2_cu **ref_cu)
23616 {
23617 ULONGEST signature = DW_SIGNATURE (attr);
23618 struct signatured_type *sig_type;
23619 struct die_info *die;
23620
23621 gdb_assert (attr->form == DW_FORM_ref_sig8);
23622
23623 sig_type = lookup_signatured_type (*ref_cu, signature);
23624 /* sig_type will be NULL if the signatured type is missing from
23625 the debug info. */
23626 if (sig_type == NULL)
23627 {
23628 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23629 " from DIE at %s [in module %s]"),
23630 hex_string (signature), sect_offset_str (src_die->sect_off),
23631 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23632 }
23633
23634 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23635 if (die == NULL)
23636 {
23637 dump_die_for_error (src_die);
23638 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23639 " from DIE at %s [in module %s]"),
23640 hex_string (signature), sect_offset_str (src_die->sect_off),
23641 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23642 }
23643
23644 return die;
23645 }
23646
23647 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23648 reading in and processing the type unit if necessary. */
23649
23650 static struct type *
23651 get_signatured_type (struct die_info *die, ULONGEST signature,
23652 struct dwarf2_cu *cu)
23653 {
23654 struct dwarf2_per_objfile *dwarf2_per_objfile
23655 = cu->per_cu->dwarf2_per_objfile;
23656 struct signatured_type *sig_type;
23657 struct dwarf2_cu *type_cu;
23658 struct die_info *type_die;
23659 struct type *type;
23660
23661 sig_type = lookup_signatured_type (cu, signature);
23662 /* sig_type will be NULL if the signatured type is missing from
23663 the debug info. */
23664 if (sig_type == NULL)
23665 {
23666 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23667 " from DIE at %s [in module %s]"),
23668 hex_string (signature), sect_offset_str (die->sect_off),
23669 objfile_name (dwarf2_per_objfile->objfile));
23670 return build_error_marker_type (cu, die);
23671 }
23672
23673 /* If we already know the type we're done. */
23674 if (sig_type->type != NULL)
23675 return sig_type->type;
23676
23677 type_cu = cu;
23678 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23679 if (type_die != NULL)
23680 {
23681 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23682 is created. This is important, for example, because for c++ classes
23683 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23684 type = read_type_die (type_die, type_cu);
23685 if (type == NULL)
23686 {
23687 complaint (_("Dwarf Error: Cannot build signatured type %s"
23688 " referenced from DIE at %s [in module %s]"),
23689 hex_string (signature), sect_offset_str (die->sect_off),
23690 objfile_name (dwarf2_per_objfile->objfile));
23691 type = build_error_marker_type (cu, die);
23692 }
23693 }
23694 else
23695 {
23696 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23697 " from DIE at %s [in module %s]"),
23698 hex_string (signature), sect_offset_str (die->sect_off),
23699 objfile_name (dwarf2_per_objfile->objfile));
23700 type = build_error_marker_type (cu, die);
23701 }
23702 sig_type->type = type;
23703
23704 return type;
23705 }
23706
23707 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23708 reading in and processing the type unit if necessary. */
23709
23710 static struct type *
23711 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23712 struct dwarf2_cu *cu) /* ARI: editCase function */
23713 {
23714 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23715 if (attr_form_is_ref (attr))
23716 {
23717 struct dwarf2_cu *type_cu = cu;
23718 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23719
23720 return read_type_die (type_die, type_cu);
23721 }
23722 else if (attr->form == DW_FORM_ref_sig8)
23723 {
23724 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23725 }
23726 else
23727 {
23728 struct dwarf2_per_objfile *dwarf2_per_objfile
23729 = cu->per_cu->dwarf2_per_objfile;
23730
23731 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23732 " at %s [in module %s]"),
23733 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23734 objfile_name (dwarf2_per_objfile->objfile));
23735 return build_error_marker_type (cu, die);
23736 }
23737 }
23738
23739 /* Load the DIEs associated with type unit PER_CU into memory. */
23740
23741 static void
23742 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23743 {
23744 struct signatured_type *sig_type;
23745
23746 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23747 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23748
23749 /* We have the per_cu, but we need the signatured_type.
23750 Fortunately this is an easy translation. */
23751 gdb_assert (per_cu->is_debug_types);
23752 sig_type = (struct signatured_type *) per_cu;
23753
23754 gdb_assert (per_cu->cu == NULL);
23755
23756 read_signatured_type (sig_type);
23757
23758 gdb_assert (per_cu->cu != NULL);
23759 }
23760
23761 /* die_reader_func for read_signatured_type.
23762 This is identical to load_full_comp_unit_reader,
23763 but is kept separate for now. */
23764
23765 static void
23766 read_signatured_type_reader (const struct die_reader_specs *reader,
23767 const gdb_byte *info_ptr,
23768 struct die_info *comp_unit_die,
23769 int has_children,
23770 void *data)
23771 {
23772 struct dwarf2_cu *cu = reader->cu;
23773
23774 gdb_assert (cu->die_hash == NULL);
23775 cu->die_hash =
23776 htab_create_alloc_ex (cu->header.length / 12,
23777 die_hash,
23778 die_eq,
23779 NULL,
23780 &cu->comp_unit_obstack,
23781 hashtab_obstack_allocate,
23782 dummy_obstack_deallocate);
23783
23784 if (has_children)
23785 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23786 &info_ptr, comp_unit_die);
23787 cu->dies = comp_unit_die;
23788 /* comp_unit_die is not stored in die_hash, no need. */
23789
23790 /* We try not to read any attributes in this function, because not
23791 all CUs needed for references have been loaded yet, and symbol
23792 table processing isn't initialized. But we have to set the CU language,
23793 or we won't be able to build types correctly.
23794 Similarly, if we do not read the producer, we can not apply
23795 producer-specific interpretation. */
23796 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23797 }
23798
23799 /* Read in a signatured type and build its CU and DIEs.
23800 If the type is a stub for the real type in a DWO file,
23801 read in the real type from the DWO file as well. */
23802
23803 static void
23804 read_signatured_type (struct signatured_type *sig_type)
23805 {
23806 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23807
23808 gdb_assert (per_cu->is_debug_types);
23809 gdb_assert (per_cu->cu == NULL);
23810
23811 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
23812 read_signatured_type_reader, NULL);
23813 sig_type->per_cu.tu_read = 1;
23814 }
23815
23816 /* Decode simple location descriptions.
23817 Given a pointer to a dwarf block that defines a location, compute
23818 the location and return the value.
23819
23820 NOTE drow/2003-11-18: This function is called in two situations
23821 now: for the address of static or global variables (partial symbols
23822 only) and for offsets into structures which are expected to be
23823 (more or less) constant. The partial symbol case should go away,
23824 and only the constant case should remain. That will let this
23825 function complain more accurately. A few special modes are allowed
23826 without complaint for global variables (for instance, global
23827 register values and thread-local values).
23828
23829 A location description containing no operations indicates that the
23830 object is optimized out. The return value is 0 for that case.
23831 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23832 callers will only want a very basic result and this can become a
23833 complaint.
23834
23835 Note that stack[0] is unused except as a default error return. */
23836
23837 static CORE_ADDR
23838 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23839 {
23840 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23841 size_t i;
23842 size_t size = blk->size;
23843 const gdb_byte *data = blk->data;
23844 CORE_ADDR stack[64];
23845 int stacki;
23846 unsigned int bytes_read, unsnd;
23847 gdb_byte op;
23848
23849 i = 0;
23850 stacki = 0;
23851 stack[stacki] = 0;
23852 stack[++stacki] = 0;
23853
23854 while (i < size)
23855 {
23856 op = data[i++];
23857 switch (op)
23858 {
23859 case DW_OP_lit0:
23860 case DW_OP_lit1:
23861 case DW_OP_lit2:
23862 case DW_OP_lit3:
23863 case DW_OP_lit4:
23864 case DW_OP_lit5:
23865 case DW_OP_lit6:
23866 case DW_OP_lit7:
23867 case DW_OP_lit8:
23868 case DW_OP_lit9:
23869 case DW_OP_lit10:
23870 case DW_OP_lit11:
23871 case DW_OP_lit12:
23872 case DW_OP_lit13:
23873 case DW_OP_lit14:
23874 case DW_OP_lit15:
23875 case DW_OP_lit16:
23876 case DW_OP_lit17:
23877 case DW_OP_lit18:
23878 case DW_OP_lit19:
23879 case DW_OP_lit20:
23880 case DW_OP_lit21:
23881 case DW_OP_lit22:
23882 case DW_OP_lit23:
23883 case DW_OP_lit24:
23884 case DW_OP_lit25:
23885 case DW_OP_lit26:
23886 case DW_OP_lit27:
23887 case DW_OP_lit28:
23888 case DW_OP_lit29:
23889 case DW_OP_lit30:
23890 case DW_OP_lit31:
23891 stack[++stacki] = op - DW_OP_lit0;
23892 break;
23893
23894 case DW_OP_reg0:
23895 case DW_OP_reg1:
23896 case DW_OP_reg2:
23897 case DW_OP_reg3:
23898 case DW_OP_reg4:
23899 case DW_OP_reg5:
23900 case DW_OP_reg6:
23901 case DW_OP_reg7:
23902 case DW_OP_reg8:
23903 case DW_OP_reg9:
23904 case DW_OP_reg10:
23905 case DW_OP_reg11:
23906 case DW_OP_reg12:
23907 case DW_OP_reg13:
23908 case DW_OP_reg14:
23909 case DW_OP_reg15:
23910 case DW_OP_reg16:
23911 case DW_OP_reg17:
23912 case DW_OP_reg18:
23913 case DW_OP_reg19:
23914 case DW_OP_reg20:
23915 case DW_OP_reg21:
23916 case DW_OP_reg22:
23917 case DW_OP_reg23:
23918 case DW_OP_reg24:
23919 case DW_OP_reg25:
23920 case DW_OP_reg26:
23921 case DW_OP_reg27:
23922 case DW_OP_reg28:
23923 case DW_OP_reg29:
23924 case DW_OP_reg30:
23925 case DW_OP_reg31:
23926 stack[++stacki] = op - DW_OP_reg0;
23927 if (i < size)
23928 dwarf2_complex_location_expr_complaint ();
23929 break;
23930
23931 case DW_OP_regx:
23932 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23933 i += bytes_read;
23934 stack[++stacki] = unsnd;
23935 if (i < size)
23936 dwarf2_complex_location_expr_complaint ();
23937 break;
23938
23939 case DW_OP_addr:
23940 stack[++stacki] = read_address (objfile->obfd, &data[i],
23941 cu, &bytes_read);
23942 i += bytes_read;
23943 break;
23944
23945 case DW_OP_const1u:
23946 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23947 i += 1;
23948 break;
23949
23950 case DW_OP_const1s:
23951 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23952 i += 1;
23953 break;
23954
23955 case DW_OP_const2u:
23956 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23957 i += 2;
23958 break;
23959
23960 case DW_OP_const2s:
23961 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23962 i += 2;
23963 break;
23964
23965 case DW_OP_const4u:
23966 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23967 i += 4;
23968 break;
23969
23970 case DW_OP_const4s:
23971 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23972 i += 4;
23973 break;
23974
23975 case DW_OP_const8u:
23976 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23977 i += 8;
23978 break;
23979
23980 case DW_OP_constu:
23981 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23982 &bytes_read);
23983 i += bytes_read;
23984 break;
23985
23986 case DW_OP_consts:
23987 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23988 i += bytes_read;
23989 break;
23990
23991 case DW_OP_dup:
23992 stack[stacki + 1] = stack[stacki];
23993 stacki++;
23994 break;
23995
23996 case DW_OP_plus:
23997 stack[stacki - 1] += stack[stacki];
23998 stacki--;
23999 break;
24000
24001 case DW_OP_plus_uconst:
24002 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24003 &bytes_read);
24004 i += bytes_read;
24005 break;
24006
24007 case DW_OP_minus:
24008 stack[stacki - 1] -= stack[stacki];
24009 stacki--;
24010 break;
24011
24012 case DW_OP_deref:
24013 /* If we're not the last op, then we definitely can't encode
24014 this using GDB's address_class enum. This is valid for partial
24015 global symbols, although the variable's address will be bogus
24016 in the psymtab. */
24017 if (i < size)
24018 dwarf2_complex_location_expr_complaint ();
24019 break;
24020
24021 case DW_OP_GNU_push_tls_address:
24022 case DW_OP_form_tls_address:
24023 /* The top of the stack has the offset from the beginning
24024 of the thread control block at which the variable is located. */
24025 /* Nothing should follow this operator, so the top of stack would
24026 be returned. */
24027 /* This is valid for partial global symbols, but the variable's
24028 address will be bogus in the psymtab. Make it always at least
24029 non-zero to not look as a variable garbage collected by linker
24030 which have DW_OP_addr 0. */
24031 if (i < size)
24032 dwarf2_complex_location_expr_complaint ();
24033 stack[stacki]++;
24034 break;
24035
24036 case DW_OP_GNU_uninit:
24037 break;
24038
24039 case DW_OP_addrx:
24040 case DW_OP_GNU_addr_index:
24041 case DW_OP_GNU_const_index:
24042 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24043 &bytes_read);
24044 i += bytes_read;
24045 break;
24046
24047 default:
24048 {
24049 const char *name = get_DW_OP_name (op);
24050
24051 if (name)
24052 complaint (_("unsupported stack op: '%s'"),
24053 name);
24054 else
24055 complaint (_("unsupported stack op: '%02x'"),
24056 op);
24057 }
24058
24059 return (stack[stacki]);
24060 }
24061
24062 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24063 outside of the allocated space. Also enforce minimum>0. */
24064 if (stacki >= ARRAY_SIZE (stack) - 1)
24065 {
24066 complaint (_("location description stack overflow"));
24067 return 0;
24068 }
24069
24070 if (stacki <= 0)
24071 {
24072 complaint (_("location description stack underflow"));
24073 return 0;
24074 }
24075 }
24076 return (stack[stacki]);
24077 }
24078
24079 /* memory allocation interface */
24080
24081 static struct dwarf_block *
24082 dwarf_alloc_block (struct dwarf2_cu *cu)
24083 {
24084 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24085 }
24086
24087 static struct die_info *
24088 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24089 {
24090 struct die_info *die;
24091 size_t size = sizeof (struct die_info);
24092
24093 if (num_attrs > 1)
24094 size += (num_attrs - 1) * sizeof (struct attribute);
24095
24096 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24097 memset (die, 0, sizeof (struct die_info));
24098 return (die);
24099 }
24100
24101 \f
24102 /* Macro support. */
24103
24104 /* Return file name relative to the compilation directory of file number I in
24105 *LH's file name table. The result is allocated using xmalloc; the caller is
24106 responsible for freeing it. */
24107
24108 static char *
24109 file_file_name (int file, struct line_header *lh)
24110 {
24111 /* Is the file number a valid index into the line header's file name
24112 table? Remember that file numbers start with one, not zero. */
24113 if (1 <= file && file <= lh->file_names.size ())
24114 {
24115 const file_entry &fe = lh->file_names[file - 1];
24116
24117 if (!IS_ABSOLUTE_PATH (fe.name))
24118 {
24119 const char *dir = fe.include_dir (lh);
24120 if (dir != NULL)
24121 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
24122 }
24123 return xstrdup (fe.name);
24124 }
24125 else
24126 {
24127 /* The compiler produced a bogus file number. We can at least
24128 record the macro definitions made in the file, even if we
24129 won't be able to find the file by name. */
24130 char fake_name[80];
24131
24132 xsnprintf (fake_name, sizeof (fake_name),
24133 "<bad macro file number %d>", file);
24134
24135 complaint (_("bad file number in macro information (%d)"),
24136 file);
24137
24138 return xstrdup (fake_name);
24139 }
24140 }
24141
24142 /* Return the full name of file number I in *LH's file name table.
24143 Use COMP_DIR as the name of the current directory of the
24144 compilation. The result is allocated using xmalloc; the caller is
24145 responsible for freeing it. */
24146 static char *
24147 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24148 {
24149 /* Is the file number a valid index into the line header's file name
24150 table? Remember that file numbers start with one, not zero. */
24151 if (1 <= file && file <= lh->file_names.size ())
24152 {
24153 char *relative = file_file_name (file, lh);
24154
24155 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24156 return relative;
24157 return reconcat (relative, comp_dir, SLASH_STRING,
24158 relative, (char *) NULL);
24159 }
24160 else
24161 return file_file_name (file, lh);
24162 }
24163
24164
24165 static struct macro_source_file *
24166 macro_start_file (struct dwarf2_cu *cu,
24167 int file, int line,
24168 struct macro_source_file *current_file,
24169 struct line_header *lh)
24170 {
24171 /* File name relative to the compilation directory of this source file. */
24172 char *file_name = file_file_name (file, lh);
24173
24174 if (! current_file)
24175 {
24176 /* Note: We don't create a macro table for this compilation unit
24177 at all until we actually get a filename. */
24178 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
24179
24180 /* If we have no current file, then this must be the start_file
24181 directive for the compilation unit's main source file. */
24182 current_file = macro_set_main (macro_table, file_name);
24183 macro_define_special (macro_table);
24184 }
24185 else
24186 current_file = macro_include (current_file, line, file_name);
24187
24188 xfree (file_name);
24189
24190 return current_file;
24191 }
24192
24193 static const char *
24194 consume_improper_spaces (const char *p, const char *body)
24195 {
24196 if (*p == ' ')
24197 {
24198 complaint (_("macro definition contains spaces "
24199 "in formal argument list:\n`%s'"),
24200 body);
24201
24202 while (*p == ' ')
24203 p++;
24204 }
24205
24206 return p;
24207 }
24208
24209
24210 static void
24211 parse_macro_definition (struct macro_source_file *file, int line,
24212 const char *body)
24213 {
24214 const char *p;
24215
24216 /* The body string takes one of two forms. For object-like macro
24217 definitions, it should be:
24218
24219 <macro name> " " <definition>
24220
24221 For function-like macro definitions, it should be:
24222
24223 <macro name> "() " <definition>
24224 or
24225 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24226
24227 Spaces may appear only where explicitly indicated, and in the
24228 <definition>.
24229
24230 The Dwarf 2 spec says that an object-like macro's name is always
24231 followed by a space, but versions of GCC around March 2002 omit
24232 the space when the macro's definition is the empty string.
24233
24234 The Dwarf 2 spec says that there should be no spaces between the
24235 formal arguments in a function-like macro's formal argument list,
24236 but versions of GCC around March 2002 include spaces after the
24237 commas. */
24238
24239
24240 /* Find the extent of the macro name. The macro name is terminated
24241 by either a space or null character (for an object-like macro) or
24242 an opening paren (for a function-like macro). */
24243 for (p = body; *p; p++)
24244 if (*p == ' ' || *p == '(')
24245 break;
24246
24247 if (*p == ' ' || *p == '\0')
24248 {
24249 /* It's an object-like macro. */
24250 int name_len = p - body;
24251 char *name = savestring (body, name_len);
24252 const char *replacement;
24253
24254 if (*p == ' ')
24255 replacement = body + name_len + 1;
24256 else
24257 {
24258 dwarf2_macro_malformed_definition_complaint (body);
24259 replacement = body + name_len;
24260 }
24261
24262 macro_define_object (file, line, name, replacement);
24263
24264 xfree (name);
24265 }
24266 else if (*p == '(')
24267 {
24268 /* It's a function-like macro. */
24269 char *name = savestring (body, p - body);
24270 int argc = 0;
24271 int argv_size = 1;
24272 char **argv = XNEWVEC (char *, argv_size);
24273
24274 p++;
24275
24276 p = consume_improper_spaces (p, body);
24277
24278 /* Parse the formal argument list. */
24279 while (*p && *p != ')')
24280 {
24281 /* Find the extent of the current argument name. */
24282 const char *arg_start = p;
24283
24284 while (*p && *p != ',' && *p != ')' && *p != ' ')
24285 p++;
24286
24287 if (! *p || p == arg_start)
24288 dwarf2_macro_malformed_definition_complaint (body);
24289 else
24290 {
24291 /* Make sure argv has room for the new argument. */
24292 if (argc >= argv_size)
24293 {
24294 argv_size *= 2;
24295 argv = XRESIZEVEC (char *, argv, argv_size);
24296 }
24297
24298 argv[argc++] = savestring (arg_start, p - arg_start);
24299 }
24300
24301 p = consume_improper_spaces (p, body);
24302
24303 /* Consume the comma, if present. */
24304 if (*p == ',')
24305 {
24306 p++;
24307
24308 p = consume_improper_spaces (p, body);
24309 }
24310 }
24311
24312 if (*p == ')')
24313 {
24314 p++;
24315
24316 if (*p == ' ')
24317 /* Perfectly formed definition, no complaints. */
24318 macro_define_function (file, line, name,
24319 argc, (const char **) argv,
24320 p + 1);
24321 else if (*p == '\0')
24322 {
24323 /* Complain, but do define it. */
24324 dwarf2_macro_malformed_definition_complaint (body);
24325 macro_define_function (file, line, name,
24326 argc, (const char **) argv,
24327 p);
24328 }
24329 else
24330 /* Just complain. */
24331 dwarf2_macro_malformed_definition_complaint (body);
24332 }
24333 else
24334 /* Just complain. */
24335 dwarf2_macro_malformed_definition_complaint (body);
24336
24337 xfree (name);
24338 {
24339 int i;
24340
24341 for (i = 0; i < argc; i++)
24342 xfree (argv[i]);
24343 }
24344 xfree (argv);
24345 }
24346 else
24347 dwarf2_macro_malformed_definition_complaint (body);
24348 }
24349
24350 /* Skip some bytes from BYTES according to the form given in FORM.
24351 Returns the new pointer. */
24352
24353 static const gdb_byte *
24354 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24355 enum dwarf_form form,
24356 unsigned int offset_size,
24357 struct dwarf2_section_info *section)
24358 {
24359 unsigned int bytes_read;
24360
24361 switch (form)
24362 {
24363 case DW_FORM_data1:
24364 case DW_FORM_flag:
24365 ++bytes;
24366 break;
24367
24368 case DW_FORM_data2:
24369 bytes += 2;
24370 break;
24371
24372 case DW_FORM_data4:
24373 bytes += 4;
24374 break;
24375
24376 case DW_FORM_data8:
24377 bytes += 8;
24378 break;
24379
24380 case DW_FORM_data16:
24381 bytes += 16;
24382 break;
24383
24384 case DW_FORM_string:
24385 read_direct_string (abfd, bytes, &bytes_read);
24386 bytes += bytes_read;
24387 break;
24388
24389 case DW_FORM_sec_offset:
24390 case DW_FORM_strp:
24391 case DW_FORM_GNU_strp_alt:
24392 bytes += offset_size;
24393 break;
24394
24395 case DW_FORM_block:
24396 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24397 bytes += bytes_read;
24398 break;
24399
24400 case DW_FORM_block1:
24401 bytes += 1 + read_1_byte (abfd, bytes);
24402 break;
24403 case DW_FORM_block2:
24404 bytes += 2 + read_2_bytes (abfd, bytes);
24405 break;
24406 case DW_FORM_block4:
24407 bytes += 4 + read_4_bytes (abfd, bytes);
24408 break;
24409
24410 case DW_FORM_addrx:
24411 case DW_FORM_sdata:
24412 case DW_FORM_strx:
24413 case DW_FORM_udata:
24414 case DW_FORM_GNU_addr_index:
24415 case DW_FORM_GNU_str_index:
24416 bytes = gdb_skip_leb128 (bytes, buffer_end);
24417 if (bytes == NULL)
24418 {
24419 dwarf2_section_buffer_overflow_complaint (section);
24420 return NULL;
24421 }
24422 break;
24423
24424 case DW_FORM_implicit_const:
24425 break;
24426
24427 default:
24428 {
24429 complaint (_("invalid form 0x%x in `%s'"),
24430 form, get_section_name (section));
24431 return NULL;
24432 }
24433 }
24434
24435 return bytes;
24436 }
24437
24438 /* A helper for dwarf_decode_macros that handles skipping an unknown
24439 opcode. Returns an updated pointer to the macro data buffer; or,
24440 on error, issues a complaint and returns NULL. */
24441
24442 static const gdb_byte *
24443 skip_unknown_opcode (unsigned int opcode,
24444 const gdb_byte **opcode_definitions,
24445 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24446 bfd *abfd,
24447 unsigned int offset_size,
24448 struct dwarf2_section_info *section)
24449 {
24450 unsigned int bytes_read, i;
24451 unsigned long arg;
24452 const gdb_byte *defn;
24453
24454 if (opcode_definitions[opcode] == NULL)
24455 {
24456 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24457 opcode);
24458 return NULL;
24459 }
24460
24461 defn = opcode_definitions[opcode];
24462 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24463 defn += bytes_read;
24464
24465 for (i = 0; i < arg; ++i)
24466 {
24467 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24468 (enum dwarf_form) defn[i], offset_size,
24469 section);
24470 if (mac_ptr == NULL)
24471 {
24472 /* skip_form_bytes already issued the complaint. */
24473 return NULL;
24474 }
24475 }
24476
24477 return mac_ptr;
24478 }
24479
24480 /* A helper function which parses the header of a macro section.
24481 If the macro section is the extended (for now called "GNU") type,
24482 then this updates *OFFSET_SIZE. Returns a pointer to just after
24483 the header, or issues a complaint and returns NULL on error. */
24484
24485 static const gdb_byte *
24486 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24487 bfd *abfd,
24488 const gdb_byte *mac_ptr,
24489 unsigned int *offset_size,
24490 int section_is_gnu)
24491 {
24492 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24493
24494 if (section_is_gnu)
24495 {
24496 unsigned int version, flags;
24497
24498 version = read_2_bytes (abfd, mac_ptr);
24499 if (version != 4 && version != 5)
24500 {
24501 complaint (_("unrecognized version `%d' in .debug_macro section"),
24502 version);
24503 return NULL;
24504 }
24505 mac_ptr += 2;
24506
24507 flags = read_1_byte (abfd, mac_ptr);
24508 ++mac_ptr;
24509 *offset_size = (flags & 1) ? 8 : 4;
24510
24511 if ((flags & 2) != 0)
24512 /* We don't need the line table offset. */
24513 mac_ptr += *offset_size;
24514
24515 /* Vendor opcode descriptions. */
24516 if ((flags & 4) != 0)
24517 {
24518 unsigned int i, count;
24519
24520 count = read_1_byte (abfd, mac_ptr);
24521 ++mac_ptr;
24522 for (i = 0; i < count; ++i)
24523 {
24524 unsigned int opcode, bytes_read;
24525 unsigned long arg;
24526
24527 opcode = read_1_byte (abfd, mac_ptr);
24528 ++mac_ptr;
24529 opcode_definitions[opcode] = mac_ptr;
24530 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24531 mac_ptr += bytes_read;
24532 mac_ptr += arg;
24533 }
24534 }
24535 }
24536
24537 return mac_ptr;
24538 }
24539
24540 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24541 including DW_MACRO_import. */
24542
24543 static void
24544 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24545 bfd *abfd,
24546 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24547 struct macro_source_file *current_file,
24548 struct line_header *lh,
24549 struct dwarf2_section_info *section,
24550 int section_is_gnu, int section_is_dwz,
24551 unsigned int offset_size,
24552 htab_t include_hash)
24553 {
24554 struct dwarf2_per_objfile *dwarf2_per_objfile
24555 = cu->per_cu->dwarf2_per_objfile;
24556 struct objfile *objfile = dwarf2_per_objfile->objfile;
24557 enum dwarf_macro_record_type macinfo_type;
24558 int at_commandline;
24559 const gdb_byte *opcode_definitions[256];
24560
24561 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24562 &offset_size, section_is_gnu);
24563 if (mac_ptr == NULL)
24564 {
24565 /* We already issued a complaint. */
24566 return;
24567 }
24568
24569 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24570 GDB is still reading the definitions from command line. First
24571 DW_MACINFO_start_file will need to be ignored as it was already executed
24572 to create CURRENT_FILE for the main source holding also the command line
24573 definitions. On first met DW_MACINFO_start_file this flag is reset to
24574 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24575
24576 at_commandline = 1;
24577
24578 do
24579 {
24580 /* Do we at least have room for a macinfo type byte? */
24581 if (mac_ptr >= mac_end)
24582 {
24583 dwarf2_section_buffer_overflow_complaint (section);
24584 break;
24585 }
24586
24587 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24588 mac_ptr++;
24589
24590 /* Note that we rely on the fact that the corresponding GNU and
24591 DWARF constants are the same. */
24592 DIAGNOSTIC_PUSH
24593 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24594 switch (macinfo_type)
24595 {
24596 /* A zero macinfo type indicates the end of the macro
24597 information. */
24598 case 0:
24599 break;
24600
24601 case DW_MACRO_define:
24602 case DW_MACRO_undef:
24603 case DW_MACRO_define_strp:
24604 case DW_MACRO_undef_strp:
24605 case DW_MACRO_define_sup:
24606 case DW_MACRO_undef_sup:
24607 {
24608 unsigned int bytes_read;
24609 int line;
24610 const char *body;
24611 int is_define;
24612
24613 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24614 mac_ptr += bytes_read;
24615
24616 if (macinfo_type == DW_MACRO_define
24617 || macinfo_type == DW_MACRO_undef)
24618 {
24619 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24620 mac_ptr += bytes_read;
24621 }
24622 else
24623 {
24624 LONGEST str_offset;
24625
24626 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24627 mac_ptr += offset_size;
24628
24629 if (macinfo_type == DW_MACRO_define_sup
24630 || macinfo_type == DW_MACRO_undef_sup
24631 || section_is_dwz)
24632 {
24633 struct dwz_file *dwz
24634 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24635
24636 body = read_indirect_string_from_dwz (objfile,
24637 dwz, str_offset);
24638 }
24639 else
24640 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24641 abfd, str_offset);
24642 }
24643
24644 is_define = (macinfo_type == DW_MACRO_define
24645 || macinfo_type == DW_MACRO_define_strp
24646 || macinfo_type == DW_MACRO_define_sup);
24647 if (! current_file)
24648 {
24649 /* DWARF violation as no main source is present. */
24650 complaint (_("debug info with no main source gives macro %s "
24651 "on line %d: %s"),
24652 is_define ? _("definition") : _("undefinition"),
24653 line, body);
24654 break;
24655 }
24656 if ((line == 0 && !at_commandline)
24657 || (line != 0 && at_commandline))
24658 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
24659 at_commandline ? _("command-line") : _("in-file"),
24660 is_define ? _("definition") : _("undefinition"),
24661 line == 0 ? _("zero") : _("non-zero"), line, body);
24662
24663 if (body == NULL)
24664 {
24665 /* Fedora's rpm-build's "debugedit" binary
24666 corrupted .debug_macro sections.
24667
24668 For more info, see
24669 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24670 complaint (_("debug info gives %s invalid macro %s "
24671 "without body (corrupted?) at line %d "
24672 "on file %s"),
24673 at_commandline ? _("command-line") : _("in-file"),
24674 is_define ? _("definition") : _("undefinition"),
24675 line, current_file->filename);
24676 }
24677 else if (is_define)
24678 parse_macro_definition (current_file, line, body);
24679 else
24680 {
24681 gdb_assert (macinfo_type == DW_MACRO_undef
24682 || macinfo_type == DW_MACRO_undef_strp
24683 || macinfo_type == DW_MACRO_undef_sup);
24684 macro_undef (current_file, line, body);
24685 }
24686 }
24687 break;
24688
24689 case DW_MACRO_start_file:
24690 {
24691 unsigned int bytes_read;
24692 int line, file;
24693
24694 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24695 mac_ptr += bytes_read;
24696 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24697 mac_ptr += bytes_read;
24698
24699 if ((line == 0 && !at_commandline)
24700 || (line != 0 && at_commandline))
24701 complaint (_("debug info gives source %d included "
24702 "from %s at %s line %d"),
24703 file, at_commandline ? _("command-line") : _("file"),
24704 line == 0 ? _("zero") : _("non-zero"), line);
24705
24706 if (at_commandline)
24707 {
24708 /* This DW_MACRO_start_file was executed in the
24709 pass one. */
24710 at_commandline = 0;
24711 }
24712 else
24713 current_file = macro_start_file (cu, file, line, current_file,
24714 lh);
24715 }
24716 break;
24717
24718 case DW_MACRO_end_file:
24719 if (! current_file)
24720 complaint (_("macro debug info has an unmatched "
24721 "`close_file' directive"));
24722 else
24723 {
24724 current_file = current_file->included_by;
24725 if (! current_file)
24726 {
24727 enum dwarf_macro_record_type next_type;
24728
24729 /* GCC circa March 2002 doesn't produce the zero
24730 type byte marking the end of the compilation
24731 unit. Complain if it's not there, but exit no
24732 matter what. */
24733
24734 /* Do we at least have room for a macinfo type byte? */
24735 if (mac_ptr >= mac_end)
24736 {
24737 dwarf2_section_buffer_overflow_complaint (section);
24738 return;
24739 }
24740
24741 /* We don't increment mac_ptr here, so this is just
24742 a look-ahead. */
24743 next_type
24744 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24745 mac_ptr);
24746 if (next_type != 0)
24747 complaint (_("no terminating 0-type entry for "
24748 "macros in `.debug_macinfo' section"));
24749
24750 return;
24751 }
24752 }
24753 break;
24754
24755 case DW_MACRO_import:
24756 case DW_MACRO_import_sup:
24757 {
24758 LONGEST offset;
24759 void **slot;
24760 bfd *include_bfd = abfd;
24761 struct dwarf2_section_info *include_section = section;
24762 const gdb_byte *include_mac_end = mac_end;
24763 int is_dwz = section_is_dwz;
24764 const gdb_byte *new_mac_ptr;
24765
24766 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24767 mac_ptr += offset_size;
24768
24769 if (macinfo_type == DW_MACRO_import_sup)
24770 {
24771 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24772
24773 dwarf2_read_section (objfile, &dwz->macro);
24774
24775 include_section = &dwz->macro;
24776 include_bfd = get_section_bfd_owner (include_section);
24777 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24778 is_dwz = 1;
24779 }
24780
24781 new_mac_ptr = include_section->buffer + offset;
24782 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24783
24784 if (*slot != NULL)
24785 {
24786 /* This has actually happened; see
24787 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24788 complaint (_("recursive DW_MACRO_import in "
24789 ".debug_macro section"));
24790 }
24791 else
24792 {
24793 *slot = (void *) new_mac_ptr;
24794
24795 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
24796 include_mac_end, current_file, lh,
24797 section, section_is_gnu, is_dwz,
24798 offset_size, include_hash);
24799
24800 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24801 }
24802 }
24803 break;
24804
24805 case DW_MACINFO_vendor_ext:
24806 if (!section_is_gnu)
24807 {
24808 unsigned int bytes_read;
24809
24810 /* This reads the constant, but since we don't recognize
24811 any vendor extensions, we ignore it. */
24812 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24813 mac_ptr += bytes_read;
24814 read_direct_string (abfd, mac_ptr, &bytes_read);
24815 mac_ptr += bytes_read;
24816
24817 /* We don't recognize any vendor extensions. */
24818 break;
24819 }
24820 /* FALLTHROUGH */
24821
24822 default:
24823 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24824 mac_ptr, mac_end, abfd, offset_size,
24825 section);
24826 if (mac_ptr == NULL)
24827 return;
24828 break;
24829 }
24830 DIAGNOSTIC_POP
24831 } while (macinfo_type != 0);
24832 }
24833
24834 static void
24835 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24836 int section_is_gnu)
24837 {
24838 struct dwarf2_per_objfile *dwarf2_per_objfile
24839 = cu->per_cu->dwarf2_per_objfile;
24840 struct objfile *objfile = dwarf2_per_objfile->objfile;
24841 struct line_header *lh = cu->line_header;
24842 bfd *abfd;
24843 const gdb_byte *mac_ptr, *mac_end;
24844 struct macro_source_file *current_file = 0;
24845 enum dwarf_macro_record_type macinfo_type;
24846 unsigned int offset_size = cu->header.offset_size;
24847 const gdb_byte *opcode_definitions[256];
24848 void **slot;
24849 struct dwarf2_section_info *section;
24850 const char *section_name;
24851
24852 if (cu->dwo_unit != NULL)
24853 {
24854 if (section_is_gnu)
24855 {
24856 section = &cu->dwo_unit->dwo_file->sections.macro;
24857 section_name = ".debug_macro.dwo";
24858 }
24859 else
24860 {
24861 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24862 section_name = ".debug_macinfo.dwo";
24863 }
24864 }
24865 else
24866 {
24867 if (section_is_gnu)
24868 {
24869 section = &dwarf2_per_objfile->macro;
24870 section_name = ".debug_macro";
24871 }
24872 else
24873 {
24874 section = &dwarf2_per_objfile->macinfo;
24875 section_name = ".debug_macinfo";
24876 }
24877 }
24878
24879 dwarf2_read_section (objfile, section);
24880 if (section->buffer == NULL)
24881 {
24882 complaint (_("missing %s section"), section_name);
24883 return;
24884 }
24885 abfd = get_section_bfd_owner (section);
24886
24887 /* First pass: Find the name of the base filename.
24888 This filename is needed in order to process all macros whose definition
24889 (or undefinition) comes from the command line. These macros are defined
24890 before the first DW_MACINFO_start_file entry, and yet still need to be
24891 associated to the base file.
24892
24893 To determine the base file name, we scan the macro definitions until we
24894 reach the first DW_MACINFO_start_file entry. We then initialize
24895 CURRENT_FILE accordingly so that any macro definition found before the
24896 first DW_MACINFO_start_file can still be associated to the base file. */
24897
24898 mac_ptr = section->buffer + offset;
24899 mac_end = section->buffer + section->size;
24900
24901 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24902 &offset_size, section_is_gnu);
24903 if (mac_ptr == NULL)
24904 {
24905 /* We already issued a complaint. */
24906 return;
24907 }
24908
24909 do
24910 {
24911 /* Do we at least have room for a macinfo type byte? */
24912 if (mac_ptr >= mac_end)
24913 {
24914 /* Complaint is printed during the second pass as GDB will probably
24915 stop the first pass earlier upon finding
24916 DW_MACINFO_start_file. */
24917 break;
24918 }
24919
24920 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24921 mac_ptr++;
24922
24923 /* Note that we rely on the fact that the corresponding GNU and
24924 DWARF constants are the same. */
24925 DIAGNOSTIC_PUSH
24926 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24927 switch (macinfo_type)
24928 {
24929 /* A zero macinfo type indicates the end of the macro
24930 information. */
24931 case 0:
24932 break;
24933
24934 case DW_MACRO_define:
24935 case DW_MACRO_undef:
24936 /* Only skip the data by MAC_PTR. */
24937 {
24938 unsigned int bytes_read;
24939
24940 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24941 mac_ptr += bytes_read;
24942 read_direct_string (abfd, mac_ptr, &bytes_read);
24943 mac_ptr += bytes_read;
24944 }
24945 break;
24946
24947 case DW_MACRO_start_file:
24948 {
24949 unsigned int bytes_read;
24950 int line, file;
24951
24952 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24953 mac_ptr += bytes_read;
24954 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24955 mac_ptr += bytes_read;
24956
24957 current_file = macro_start_file (cu, file, line, current_file, lh);
24958 }
24959 break;
24960
24961 case DW_MACRO_end_file:
24962 /* No data to skip by MAC_PTR. */
24963 break;
24964
24965 case DW_MACRO_define_strp:
24966 case DW_MACRO_undef_strp:
24967 case DW_MACRO_define_sup:
24968 case DW_MACRO_undef_sup:
24969 {
24970 unsigned int bytes_read;
24971
24972 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24973 mac_ptr += bytes_read;
24974 mac_ptr += offset_size;
24975 }
24976 break;
24977
24978 case DW_MACRO_import:
24979 case DW_MACRO_import_sup:
24980 /* Note that, according to the spec, a transparent include
24981 chain cannot call DW_MACRO_start_file. So, we can just
24982 skip this opcode. */
24983 mac_ptr += offset_size;
24984 break;
24985
24986 case DW_MACINFO_vendor_ext:
24987 /* Only skip the data by MAC_PTR. */
24988 if (!section_is_gnu)
24989 {
24990 unsigned int bytes_read;
24991
24992 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24993 mac_ptr += bytes_read;
24994 read_direct_string (abfd, mac_ptr, &bytes_read);
24995 mac_ptr += bytes_read;
24996 }
24997 /* FALLTHROUGH */
24998
24999 default:
25000 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25001 mac_ptr, mac_end, abfd, offset_size,
25002 section);
25003 if (mac_ptr == NULL)
25004 return;
25005 break;
25006 }
25007 DIAGNOSTIC_POP
25008 } while (macinfo_type != 0 && current_file == NULL);
25009
25010 /* Second pass: Process all entries.
25011
25012 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25013 command-line macro definitions/undefinitions. This flag is unset when we
25014 reach the first DW_MACINFO_start_file entry. */
25015
25016 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25017 htab_eq_pointer,
25018 NULL, xcalloc, xfree));
25019 mac_ptr = section->buffer + offset;
25020 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
25021 *slot = (void *) mac_ptr;
25022 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
25023 current_file, lh, section,
25024 section_is_gnu, 0, offset_size,
25025 include_hash.get ());
25026 }
25027
25028 /* Check if the attribute's form is a DW_FORM_block*
25029 if so return true else false. */
25030
25031 static int
25032 attr_form_is_block (const struct attribute *attr)
25033 {
25034 return (attr == NULL ? 0 :
25035 attr->form == DW_FORM_block1
25036 || attr->form == DW_FORM_block2
25037 || attr->form == DW_FORM_block4
25038 || attr->form == DW_FORM_block
25039 || attr->form == DW_FORM_exprloc);
25040 }
25041
25042 /* Return non-zero if ATTR's value is a section offset --- classes
25043 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25044 You may use DW_UNSND (attr) to retrieve such offsets.
25045
25046 Section 7.5.4, "Attribute Encodings", explains that no attribute
25047 may have a value that belongs to more than one of these classes; it
25048 would be ambiguous if we did, because we use the same forms for all
25049 of them. */
25050
25051 static int
25052 attr_form_is_section_offset (const struct attribute *attr)
25053 {
25054 return (attr->form == DW_FORM_data4
25055 || attr->form == DW_FORM_data8
25056 || attr->form == DW_FORM_sec_offset);
25057 }
25058
25059 /* Return non-zero if ATTR's value falls in the 'constant' class, or
25060 zero otherwise. When this function returns true, you can apply
25061 dwarf2_get_attr_constant_value to it.
25062
25063 However, note that for some attributes you must check
25064 attr_form_is_section_offset before using this test. DW_FORM_data4
25065 and DW_FORM_data8 are members of both the constant class, and of
25066 the classes that contain offsets into other debug sections
25067 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25068 that, if an attribute's can be either a constant or one of the
25069 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25070 taken as section offsets, not constants.
25071
25072 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25073 cannot handle that. */
25074
25075 static int
25076 attr_form_is_constant (const struct attribute *attr)
25077 {
25078 switch (attr->form)
25079 {
25080 case DW_FORM_sdata:
25081 case DW_FORM_udata:
25082 case DW_FORM_data1:
25083 case DW_FORM_data2:
25084 case DW_FORM_data4:
25085 case DW_FORM_data8:
25086 case DW_FORM_implicit_const:
25087 return 1;
25088 default:
25089 return 0;
25090 }
25091 }
25092
25093
25094 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25095 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25096
25097 static int
25098 attr_form_is_ref (const struct attribute *attr)
25099 {
25100 switch (attr->form)
25101 {
25102 case DW_FORM_ref_addr:
25103 case DW_FORM_ref1:
25104 case DW_FORM_ref2:
25105 case DW_FORM_ref4:
25106 case DW_FORM_ref8:
25107 case DW_FORM_ref_udata:
25108 case DW_FORM_GNU_ref_alt:
25109 return 1;
25110 default:
25111 return 0;
25112 }
25113 }
25114
25115 /* Return the .debug_loc section to use for CU.
25116 For DWO files use .debug_loc.dwo. */
25117
25118 static struct dwarf2_section_info *
25119 cu_debug_loc_section (struct dwarf2_cu *cu)
25120 {
25121 struct dwarf2_per_objfile *dwarf2_per_objfile
25122 = cu->per_cu->dwarf2_per_objfile;
25123
25124 if (cu->dwo_unit)
25125 {
25126 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25127
25128 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25129 }
25130 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25131 : &dwarf2_per_objfile->loc);
25132 }
25133
25134 /* A helper function that fills in a dwarf2_loclist_baton. */
25135
25136 static void
25137 fill_in_loclist_baton (struct dwarf2_cu *cu,
25138 struct dwarf2_loclist_baton *baton,
25139 const struct attribute *attr)
25140 {
25141 struct dwarf2_per_objfile *dwarf2_per_objfile
25142 = cu->per_cu->dwarf2_per_objfile;
25143 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25144
25145 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25146
25147 baton->per_cu = cu->per_cu;
25148 gdb_assert (baton->per_cu);
25149 /* We don't know how long the location list is, but make sure we
25150 don't run off the edge of the section. */
25151 baton->size = section->size - DW_UNSND (attr);
25152 baton->data = section->buffer + DW_UNSND (attr);
25153 baton->base_address = cu->base_address;
25154 baton->from_dwo = cu->dwo_unit != NULL;
25155 }
25156
25157 static void
25158 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25159 struct dwarf2_cu *cu, int is_block)
25160 {
25161 struct dwarf2_per_objfile *dwarf2_per_objfile
25162 = cu->per_cu->dwarf2_per_objfile;
25163 struct objfile *objfile = dwarf2_per_objfile->objfile;
25164 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25165
25166 if (attr_form_is_section_offset (attr)
25167 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25168 the section. If so, fall through to the complaint in the
25169 other branch. */
25170 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25171 {
25172 struct dwarf2_loclist_baton *baton;
25173
25174 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25175
25176 fill_in_loclist_baton (cu, baton, attr);
25177
25178 if (cu->base_known == 0)
25179 complaint (_("Location list used without "
25180 "specifying the CU base address."));
25181
25182 SYMBOL_ACLASS_INDEX (sym) = (is_block
25183 ? dwarf2_loclist_block_index
25184 : dwarf2_loclist_index);
25185 SYMBOL_LOCATION_BATON (sym) = baton;
25186 }
25187 else
25188 {
25189 struct dwarf2_locexpr_baton *baton;
25190
25191 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25192 baton->per_cu = cu->per_cu;
25193 gdb_assert (baton->per_cu);
25194
25195 if (attr_form_is_block (attr))
25196 {
25197 /* Note that we're just copying the block's data pointer
25198 here, not the actual data. We're still pointing into the
25199 info_buffer for SYM's objfile; right now we never release
25200 that buffer, but when we do clean up properly this may
25201 need to change. */
25202 baton->size = DW_BLOCK (attr)->size;
25203 baton->data = DW_BLOCK (attr)->data;
25204 }
25205 else
25206 {
25207 dwarf2_invalid_attrib_class_complaint ("location description",
25208 SYMBOL_NATURAL_NAME (sym));
25209 baton->size = 0;
25210 }
25211
25212 SYMBOL_ACLASS_INDEX (sym) = (is_block
25213 ? dwarf2_locexpr_block_index
25214 : dwarf2_locexpr_index);
25215 SYMBOL_LOCATION_BATON (sym) = baton;
25216 }
25217 }
25218
25219 /* Return the OBJFILE associated with the compilation unit CU. If CU
25220 came from a separate debuginfo file, then the master objfile is
25221 returned. */
25222
25223 struct objfile *
25224 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25225 {
25226 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25227
25228 /* Return the master objfile, so that we can report and look up the
25229 correct file containing this variable. */
25230 if (objfile->separate_debug_objfile_backlink)
25231 objfile = objfile->separate_debug_objfile_backlink;
25232
25233 return objfile;
25234 }
25235
25236 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25237 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25238 CU_HEADERP first. */
25239
25240 static const struct comp_unit_head *
25241 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25242 struct dwarf2_per_cu_data *per_cu)
25243 {
25244 const gdb_byte *info_ptr;
25245
25246 if (per_cu->cu)
25247 return &per_cu->cu->header;
25248
25249 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25250
25251 memset (cu_headerp, 0, sizeof (*cu_headerp));
25252 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25253 rcuh_kind::COMPILE);
25254
25255 return cu_headerp;
25256 }
25257
25258 /* Return the address size given in the compilation unit header for CU. */
25259
25260 int
25261 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25262 {
25263 struct comp_unit_head cu_header_local;
25264 const struct comp_unit_head *cu_headerp;
25265
25266 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25267
25268 return cu_headerp->addr_size;
25269 }
25270
25271 /* Return the offset size given in the compilation unit header for CU. */
25272
25273 int
25274 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25275 {
25276 struct comp_unit_head cu_header_local;
25277 const struct comp_unit_head *cu_headerp;
25278
25279 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25280
25281 return cu_headerp->offset_size;
25282 }
25283
25284 /* See its dwarf2loc.h declaration. */
25285
25286 int
25287 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25288 {
25289 struct comp_unit_head cu_header_local;
25290 const struct comp_unit_head *cu_headerp;
25291
25292 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25293
25294 if (cu_headerp->version == 2)
25295 return cu_headerp->addr_size;
25296 else
25297 return cu_headerp->offset_size;
25298 }
25299
25300 /* Return the text offset of the CU. The returned offset comes from
25301 this CU's objfile. If this objfile came from a separate debuginfo
25302 file, then the offset may be different from the corresponding
25303 offset in the parent objfile. */
25304
25305 CORE_ADDR
25306 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25307 {
25308 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25309
25310 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25311 }
25312
25313 /* Return a type that is a generic pointer type, the size of which matches
25314 the address size given in the compilation unit header for PER_CU. */
25315 static struct type *
25316 dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25317 {
25318 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25319 struct type *void_type = objfile_type (objfile)->builtin_void;
25320 struct type *addr_type = lookup_pointer_type (void_type);
25321 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25322
25323 if (TYPE_LENGTH (addr_type) == addr_size)
25324 return addr_type;
25325
25326 addr_type
25327 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25328 return addr_type;
25329 }
25330
25331 /* Return DWARF version number of PER_CU. */
25332
25333 short
25334 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25335 {
25336 return per_cu->dwarf_version;
25337 }
25338
25339 /* Locate the .debug_info compilation unit from CU's objfile which contains
25340 the DIE at OFFSET. Raises an error on failure. */
25341
25342 static struct dwarf2_per_cu_data *
25343 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25344 unsigned int offset_in_dwz,
25345 struct dwarf2_per_objfile *dwarf2_per_objfile)
25346 {
25347 struct dwarf2_per_cu_data *this_cu;
25348 int low, high;
25349
25350 low = 0;
25351 high = dwarf2_per_objfile->all_comp_units.size () - 1;
25352 while (high > low)
25353 {
25354 struct dwarf2_per_cu_data *mid_cu;
25355 int mid = low + (high - low) / 2;
25356
25357 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25358 if (mid_cu->is_dwz > offset_in_dwz
25359 || (mid_cu->is_dwz == offset_in_dwz
25360 && mid_cu->sect_off + mid_cu->length >= sect_off))
25361 high = mid;
25362 else
25363 low = mid + 1;
25364 }
25365 gdb_assert (low == high);
25366 this_cu = dwarf2_per_objfile->all_comp_units[low];
25367 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25368 {
25369 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25370 error (_("Dwarf Error: could not find partial DIE containing "
25371 "offset %s [in module %s]"),
25372 sect_offset_str (sect_off),
25373 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25374
25375 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25376 <= sect_off);
25377 return dwarf2_per_objfile->all_comp_units[low-1];
25378 }
25379 else
25380 {
25381 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25382 && sect_off >= this_cu->sect_off + this_cu->length)
25383 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25384 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25385 return this_cu;
25386 }
25387 }
25388
25389 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25390
25391 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25392 : per_cu (per_cu_),
25393 mark (false),
25394 has_loclist (false),
25395 checked_producer (false),
25396 producer_is_gxx_lt_4_6 (false),
25397 producer_is_gcc_lt_4_3 (false),
25398 producer_is_icc (false),
25399 producer_is_icc_lt_14 (false),
25400 producer_is_codewarrior (false),
25401 processing_has_namespace_info (false)
25402 {
25403 per_cu->cu = this;
25404 }
25405
25406 /* Destroy a dwarf2_cu. */
25407
25408 dwarf2_cu::~dwarf2_cu ()
25409 {
25410 per_cu->cu = NULL;
25411 }
25412
25413 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25414
25415 static void
25416 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25417 enum language pretend_language)
25418 {
25419 struct attribute *attr;
25420
25421 /* Set the language we're debugging. */
25422 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25423 if (attr)
25424 set_cu_language (DW_UNSND (attr), cu);
25425 else
25426 {
25427 cu->language = pretend_language;
25428 cu->language_defn = language_def (cu->language);
25429 }
25430
25431 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25432 }
25433
25434 /* Increase the age counter on each cached compilation unit, and free
25435 any that are too old. */
25436
25437 static void
25438 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25439 {
25440 struct dwarf2_per_cu_data *per_cu, **last_chain;
25441
25442 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25443 per_cu = dwarf2_per_objfile->read_in_chain;
25444 while (per_cu != NULL)
25445 {
25446 per_cu->cu->last_used ++;
25447 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25448 dwarf2_mark (per_cu->cu);
25449 per_cu = per_cu->cu->read_in_chain;
25450 }
25451
25452 per_cu = dwarf2_per_objfile->read_in_chain;
25453 last_chain = &dwarf2_per_objfile->read_in_chain;
25454 while (per_cu != NULL)
25455 {
25456 struct dwarf2_per_cu_data *next_cu;
25457
25458 next_cu = per_cu->cu->read_in_chain;
25459
25460 if (!per_cu->cu->mark)
25461 {
25462 delete per_cu->cu;
25463 *last_chain = next_cu;
25464 }
25465 else
25466 last_chain = &per_cu->cu->read_in_chain;
25467
25468 per_cu = next_cu;
25469 }
25470 }
25471
25472 /* Remove a single compilation unit from the cache. */
25473
25474 static void
25475 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25476 {
25477 struct dwarf2_per_cu_data *per_cu, **last_chain;
25478 struct dwarf2_per_objfile *dwarf2_per_objfile
25479 = target_per_cu->dwarf2_per_objfile;
25480
25481 per_cu = dwarf2_per_objfile->read_in_chain;
25482 last_chain = &dwarf2_per_objfile->read_in_chain;
25483 while (per_cu != NULL)
25484 {
25485 struct dwarf2_per_cu_data *next_cu;
25486
25487 next_cu = per_cu->cu->read_in_chain;
25488
25489 if (per_cu == target_per_cu)
25490 {
25491 delete per_cu->cu;
25492 per_cu->cu = NULL;
25493 *last_chain = next_cu;
25494 break;
25495 }
25496 else
25497 last_chain = &per_cu->cu->read_in_chain;
25498
25499 per_cu = next_cu;
25500 }
25501 }
25502
25503 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25504 We store these in a hash table separate from the DIEs, and preserve them
25505 when the DIEs are flushed out of cache.
25506
25507 The CU "per_cu" pointer is needed because offset alone is not enough to
25508 uniquely identify the type. A file may have multiple .debug_types sections,
25509 or the type may come from a DWO file. Furthermore, while it's more logical
25510 to use per_cu->section+offset, with Fission the section with the data is in
25511 the DWO file but we don't know that section at the point we need it.
25512 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25513 because we can enter the lookup routine, get_die_type_at_offset, from
25514 outside this file, and thus won't necessarily have PER_CU->cu.
25515 Fortunately, PER_CU is stable for the life of the objfile. */
25516
25517 struct dwarf2_per_cu_offset_and_type
25518 {
25519 const struct dwarf2_per_cu_data *per_cu;
25520 sect_offset sect_off;
25521 struct type *type;
25522 };
25523
25524 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25525
25526 static hashval_t
25527 per_cu_offset_and_type_hash (const void *item)
25528 {
25529 const struct dwarf2_per_cu_offset_and_type *ofs
25530 = (const struct dwarf2_per_cu_offset_and_type *) item;
25531
25532 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25533 }
25534
25535 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25536
25537 static int
25538 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25539 {
25540 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25541 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25542 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25543 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25544
25545 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25546 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25547 }
25548
25549 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25550 table if necessary. For convenience, return TYPE.
25551
25552 The DIEs reading must have careful ordering to:
25553 * Not cause infite loops trying to read in DIEs as a prerequisite for
25554 reading current DIE.
25555 * Not trying to dereference contents of still incompletely read in types
25556 while reading in other DIEs.
25557 * Enable referencing still incompletely read in types just by a pointer to
25558 the type without accessing its fields.
25559
25560 Therefore caller should follow these rules:
25561 * Try to fetch any prerequisite types we may need to build this DIE type
25562 before building the type and calling set_die_type.
25563 * After building type call set_die_type for current DIE as soon as
25564 possible before fetching more types to complete the current type.
25565 * Make the type as complete as possible before fetching more types. */
25566
25567 static struct type *
25568 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25569 {
25570 struct dwarf2_per_objfile *dwarf2_per_objfile
25571 = cu->per_cu->dwarf2_per_objfile;
25572 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25573 struct objfile *objfile = dwarf2_per_objfile->objfile;
25574 struct attribute *attr;
25575 struct dynamic_prop prop;
25576
25577 /* For Ada types, make sure that the gnat-specific data is always
25578 initialized (if not already set). There are a few types where
25579 we should not be doing so, because the type-specific area is
25580 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25581 where the type-specific area is used to store the floatformat).
25582 But this is not a problem, because the gnat-specific information
25583 is actually not needed for these types. */
25584 if (need_gnat_info (cu)
25585 && TYPE_CODE (type) != TYPE_CODE_FUNC
25586 && TYPE_CODE (type) != TYPE_CODE_FLT
25587 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25588 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25589 && TYPE_CODE (type) != TYPE_CODE_METHOD
25590 && !HAVE_GNAT_AUX_INFO (type))
25591 INIT_GNAT_SPECIFIC (type);
25592
25593 /* Read DW_AT_allocated and set in type. */
25594 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25595 if (attr_form_is_block (attr))
25596 {
25597 struct type *prop_type
25598 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25599 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25600 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25601 }
25602 else if (attr != NULL)
25603 {
25604 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25605 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25606 sect_offset_str (die->sect_off));
25607 }
25608
25609 /* Read DW_AT_associated and set in type. */
25610 attr = dwarf2_attr (die, DW_AT_associated, cu);
25611 if (attr_form_is_block (attr))
25612 {
25613 struct type *prop_type
25614 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25615 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25616 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25617 }
25618 else if (attr != NULL)
25619 {
25620 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25621 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25622 sect_offset_str (die->sect_off));
25623 }
25624
25625 /* Read DW_AT_data_location and set in type. */
25626 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25627 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25628 dwarf2_per_cu_addr_type (cu->per_cu)))
25629 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25630
25631 if (dwarf2_per_objfile->die_type_hash == NULL)
25632 {
25633 dwarf2_per_objfile->die_type_hash =
25634 htab_create_alloc_ex (127,
25635 per_cu_offset_and_type_hash,
25636 per_cu_offset_and_type_eq,
25637 NULL,
25638 &objfile->objfile_obstack,
25639 hashtab_obstack_allocate,
25640 dummy_obstack_deallocate);
25641 }
25642
25643 ofs.per_cu = cu->per_cu;
25644 ofs.sect_off = die->sect_off;
25645 ofs.type = type;
25646 slot = (struct dwarf2_per_cu_offset_and_type **)
25647 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25648 if (*slot)
25649 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25650 sect_offset_str (die->sect_off));
25651 *slot = XOBNEW (&objfile->objfile_obstack,
25652 struct dwarf2_per_cu_offset_and_type);
25653 **slot = ofs;
25654 return type;
25655 }
25656
25657 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25658 or return NULL if the die does not have a saved type. */
25659
25660 static struct type *
25661 get_die_type_at_offset (sect_offset sect_off,
25662 struct dwarf2_per_cu_data *per_cu)
25663 {
25664 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25665 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25666
25667 if (dwarf2_per_objfile->die_type_hash == NULL)
25668 return NULL;
25669
25670 ofs.per_cu = per_cu;
25671 ofs.sect_off = sect_off;
25672 slot = ((struct dwarf2_per_cu_offset_and_type *)
25673 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25674 if (slot)
25675 return slot->type;
25676 else
25677 return NULL;
25678 }
25679
25680 /* Look up the type for DIE in CU in die_type_hash,
25681 or return NULL if DIE does not have a saved type. */
25682
25683 static struct type *
25684 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25685 {
25686 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25687 }
25688
25689 /* Add a dependence relationship from CU to REF_PER_CU. */
25690
25691 static void
25692 dwarf2_add_dependence (struct dwarf2_cu *cu,
25693 struct dwarf2_per_cu_data *ref_per_cu)
25694 {
25695 void **slot;
25696
25697 if (cu->dependencies == NULL)
25698 cu->dependencies
25699 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25700 NULL, &cu->comp_unit_obstack,
25701 hashtab_obstack_allocate,
25702 dummy_obstack_deallocate);
25703
25704 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25705 if (*slot == NULL)
25706 *slot = ref_per_cu;
25707 }
25708
25709 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25710 Set the mark field in every compilation unit in the
25711 cache that we must keep because we are keeping CU. */
25712
25713 static int
25714 dwarf2_mark_helper (void **slot, void *data)
25715 {
25716 struct dwarf2_per_cu_data *per_cu;
25717
25718 per_cu = (struct dwarf2_per_cu_data *) *slot;
25719
25720 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25721 reading of the chain. As such dependencies remain valid it is not much
25722 useful to track and undo them during QUIT cleanups. */
25723 if (per_cu->cu == NULL)
25724 return 1;
25725
25726 if (per_cu->cu->mark)
25727 return 1;
25728 per_cu->cu->mark = true;
25729
25730 if (per_cu->cu->dependencies != NULL)
25731 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25732
25733 return 1;
25734 }
25735
25736 /* Set the mark field in CU and in every other compilation unit in the
25737 cache that we must keep because we are keeping CU. */
25738
25739 static void
25740 dwarf2_mark (struct dwarf2_cu *cu)
25741 {
25742 if (cu->mark)
25743 return;
25744 cu->mark = true;
25745 if (cu->dependencies != NULL)
25746 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25747 }
25748
25749 static void
25750 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25751 {
25752 while (per_cu)
25753 {
25754 per_cu->cu->mark = false;
25755 per_cu = per_cu->cu->read_in_chain;
25756 }
25757 }
25758
25759 /* Trivial hash function for partial_die_info: the hash value of a DIE
25760 is its offset in .debug_info for this objfile. */
25761
25762 static hashval_t
25763 partial_die_hash (const void *item)
25764 {
25765 const struct partial_die_info *part_die
25766 = (const struct partial_die_info *) item;
25767
25768 return to_underlying (part_die->sect_off);
25769 }
25770
25771 /* Trivial comparison function for partial_die_info structures: two DIEs
25772 are equal if they have the same offset. */
25773
25774 static int
25775 partial_die_eq (const void *item_lhs, const void *item_rhs)
25776 {
25777 const struct partial_die_info *part_die_lhs
25778 = (const struct partial_die_info *) item_lhs;
25779 const struct partial_die_info *part_die_rhs
25780 = (const struct partial_die_info *) item_rhs;
25781
25782 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25783 }
25784
25785 struct cmd_list_element *set_dwarf_cmdlist;
25786 struct cmd_list_element *show_dwarf_cmdlist;
25787
25788 static void
25789 set_dwarf_cmd (const char *args, int from_tty)
25790 {
25791 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25792 gdb_stdout);
25793 }
25794
25795 static void
25796 show_dwarf_cmd (const char *args, int from_tty)
25797 {
25798 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25799 }
25800
25801 int dwarf_always_disassemble;
25802
25803 static void
25804 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25805 struct cmd_list_element *c, const char *value)
25806 {
25807 fprintf_filtered (file,
25808 _("Whether to always disassemble "
25809 "DWARF expressions is %s.\n"),
25810 value);
25811 }
25812
25813 static void
25814 show_check_physname (struct ui_file *file, int from_tty,
25815 struct cmd_list_element *c, const char *value)
25816 {
25817 fprintf_filtered (file,
25818 _("Whether to check \"physname\" is %s.\n"),
25819 value);
25820 }
25821
25822 void
25823 _initialize_dwarf2_read (void)
25824 {
25825 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25826 Set DWARF specific variables.\n\
25827 Configure DWARF variables such as the cache size."),
25828 &set_dwarf_cmdlist, "maintenance set dwarf ",
25829 0/*allow-unknown*/, &maintenance_set_cmdlist);
25830
25831 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25832 Show DWARF specific variables.\n\
25833 Show DWARF variables such as the cache size."),
25834 &show_dwarf_cmdlist, "maintenance show dwarf ",
25835 0/*allow-unknown*/, &maintenance_show_cmdlist);
25836
25837 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25838 &dwarf_max_cache_age, _("\
25839 Set the upper bound on the age of cached DWARF compilation units."), _("\
25840 Show the upper bound on the age of cached DWARF compilation units."), _("\
25841 A higher limit means that cached compilation units will be stored\n\
25842 in memory longer, and more total memory will be used. Zero disables\n\
25843 caching, which can slow down startup."),
25844 NULL,
25845 show_dwarf_max_cache_age,
25846 &set_dwarf_cmdlist,
25847 &show_dwarf_cmdlist);
25848
25849 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25850 &dwarf_always_disassemble, _("\
25851 Set whether `info address' always disassembles DWARF expressions."), _("\
25852 Show whether `info address' always disassembles DWARF expressions."), _("\
25853 When enabled, DWARF expressions are always printed in an assembly-like\n\
25854 syntax. When disabled, expressions will be printed in a more\n\
25855 conversational style, when possible."),
25856 NULL,
25857 show_dwarf_always_disassemble,
25858 &set_dwarf_cmdlist,
25859 &show_dwarf_cmdlist);
25860
25861 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25862 Set debugging of the DWARF reader."), _("\
25863 Show debugging of the DWARF reader."), _("\
25864 When enabled (non-zero), debugging messages are printed during DWARF\n\
25865 reading and symtab expansion. A value of 1 (one) provides basic\n\
25866 information. A value greater than 1 provides more verbose information."),
25867 NULL,
25868 NULL,
25869 &setdebuglist, &showdebuglist);
25870
25871 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25872 Set debugging of the DWARF DIE reader."), _("\
25873 Show debugging of the DWARF DIE reader."), _("\
25874 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25875 The value is the maximum depth to print."),
25876 NULL,
25877 NULL,
25878 &setdebuglist, &showdebuglist);
25879
25880 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25881 Set debugging of the dwarf line reader."), _("\
25882 Show debugging of the dwarf line reader."), _("\
25883 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25884 A value of 1 (one) provides basic information.\n\
25885 A value greater than 1 provides more verbose information."),
25886 NULL,
25887 NULL,
25888 &setdebuglist, &showdebuglist);
25889
25890 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25891 Set cross-checking of \"physname\" code against demangler."), _("\
25892 Show cross-checking of \"physname\" code against demangler."), _("\
25893 When enabled, GDB's internal \"physname\" code is checked against\n\
25894 the demangler."),
25895 NULL, show_check_physname,
25896 &setdebuglist, &showdebuglist);
25897
25898 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25899 no_class, &use_deprecated_index_sections, _("\
25900 Set whether to use deprecated gdb_index sections."), _("\
25901 Show whether to use deprecated gdb_index sections."), _("\
25902 When enabled, deprecated .gdb_index sections are used anyway.\n\
25903 Normally they are ignored either because of a missing feature or\n\
25904 performance issue.\n\
25905 Warning: This option must be enabled before gdb reads the file."),
25906 NULL,
25907 NULL,
25908 &setlist, &showlist);
25909
25910 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25911 &dwarf2_locexpr_funcs);
25912 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25913 &dwarf2_loclist_funcs);
25914
25915 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25916 &dwarf2_block_frame_base_locexpr_funcs);
25917 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25918 &dwarf2_block_frame_base_loclist_funcs);
25919
25920 #if GDB_SELF_TEST
25921 selftests::register_test ("dw2_expand_symtabs_matching",
25922 selftests::dw2_expand_symtabs_matching::run_test);
25923 #endif
25924 }
This page took 0.549866 seconds and 5 git commands to generate.