Handle copy relocations
[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 true, cross-check physname against demangler. */
106 static bool check_physname = false;
107
108 /* When true, do not reject deprecated .gdb_index sections. */
109 static bool use_deprecated_index_sections = false;
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,
183 enum language lang) const;
184
185 /* Prevent deleting/destroying via a base class pointer. */
186 protected:
187 ~mapped_index_base() = default;
188 };
189
190 /* A description of the mapped index. The file format is described in
191 a comment by the code that writes the index. */
192 struct mapped_index final : public mapped_index_base
193 {
194 /* A slot/bucket in the symbol table hash. */
195 struct symbol_table_slot
196 {
197 const offset_type name;
198 const offset_type vec;
199 };
200
201 /* Index data format version. */
202 int version = 0;
203
204 /* The address table data. */
205 gdb::array_view<const gdb_byte> address_table;
206
207 /* The symbol table, implemented as a hash table. */
208 gdb::array_view<symbol_table_slot> symbol_table;
209
210 /* A pointer to the constant pool. */
211 const char *constant_pool = nullptr;
212
213 bool symbol_name_slot_invalid (offset_type idx) const override
214 {
215 const auto &bucket = this->symbol_table[idx];
216 return bucket.name == 0 && bucket.vec == 0;
217 }
218
219 /* Convenience method to get at the name of the symbol at IDX in the
220 symbol table. */
221 const char *symbol_name_at (offset_type idx) const override
222 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
223
224 size_t symbol_name_count () const override
225 { return this->symbol_table.size (); }
226 };
227
228 /* A description of the mapped .debug_names.
229 Uninitialized map has CU_COUNT 0. */
230 struct mapped_debug_names final : public mapped_index_base
231 {
232 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
233 : dwarf2_per_objfile (dwarf2_per_objfile_)
234 {}
235
236 struct dwarf2_per_objfile *dwarf2_per_objfile;
237 bfd_endian dwarf5_byte_order;
238 bool dwarf5_is_dwarf64;
239 bool augmentation_is_gdb;
240 uint8_t offset_size;
241 uint32_t cu_count = 0;
242 uint32_t tu_count, bucket_count, name_count;
243 const gdb_byte *cu_table_reordered, *tu_table_reordered;
244 const uint32_t *bucket_table_reordered, *hash_table_reordered;
245 const gdb_byte *name_table_string_offs_reordered;
246 const gdb_byte *name_table_entry_offs_reordered;
247 const gdb_byte *entry_pool;
248
249 struct index_val
250 {
251 ULONGEST dwarf_tag;
252 struct attr
253 {
254 /* Attribute name DW_IDX_*. */
255 ULONGEST dw_idx;
256
257 /* Attribute form DW_FORM_*. */
258 ULONGEST form;
259
260 /* Value if FORM is DW_FORM_implicit_const. */
261 LONGEST implicit_const;
262 };
263 std::vector<attr> attr_vec;
264 };
265
266 std::unordered_map<ULONGEST, index_val> abbrev_map;
267
268 const char *namei_to_name (uint32_t namei) const;
269
270 /* Implementation of the mapped_index_base virtual interface, for
271 the name_components cache. */
272
273 const char *symbol_name_at (offset_type idx) const override
274 { return namei_to_name (idx); }
275
276 size_t symbol_name_count () const override
277 { return this->name_count; }
278 };
279
280 /* See dwarf2read.h. */
281
282 dwarf2_per_objfile *
283 get_dwarf2_per_objfile (struct objfile *objfile)
284 {
285 return dwarf2_objfile_data_key.get (objfile);
286 }
287
288 /* Default names of the debugging sections. */
289
290 /* Note that if the debugging section has been compressed, it might
291 have a name like .zdebug_info. */
292
293 static const struct dwarf2_debug_sections dwarf2_elf_names =
294 {
295 { ".debug_info", ".zdebug_info" },
296 { ".debug_abbrev", ".zdebug_abbrev" },
297 { ".debug_line", ".zdebug_line" },
298 { ".debug_loc", ".zdebug_loc" },
299 { ".debug_loclists", ".zdebug_loclists" },
300 { ".debug_macinfo", ".zdebug_macinfo" },
301 { ".debug_macro", ".zdebug_macro" },
302 { ".debug_str", ".zdebug_str" },
303 { ".debug_line_str", ".zdebug_line_str" },
304 { ".debug_ranges", ".zdebug_ranges" },
305 { ".debug_rnglists", ".zdebug_rnglists" },
306 { ".debug_types", ".zdebug_types" },
307 { ".debug_addr", ".zdebug_addr" },
308 { ".debug_frame", ".zdebug_frame" },
309 { ".eh_frame", NULL },
310 { ".gdb_index", ".zgdb_index" },
311 { ".debug_names", ".zdebug_names" },
312 { ".debug_aranges", ".zdebug_aranges" },
313 23
314 };
315
316 /* List of DWO/DWP sections. */
317
318 static const struct dwop_section_names
319 {
320 struct dwarf2_section_names abbrev_dwo;
321 struct dwarf2_section_names info_dwo;
322 struct dwarf2_section_names line_dwo;
323 struct dwarf2_section_names loc_dwo;
324 struct dwarf2_section_names loclists_dwo;
325 struct dwarf2_section_names macinfo_dwo;
326 struct dwarf2_section_names macro_dwo;
327 struct dwarf2_section_names str_dwo;
328 struct dwarf2_section_names str_offsets_dwo;
329 struct dwarf2_section_names types_dwo;
330 struct dwarf2_section_names cu_index;
331 struct dwarf2_section_names tu_index;
332 }
333 dwop_section_names =
334 {
335 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
336 { ".debug_info.dwo", ".zdebug_info.dwo" },
337 { ".debug_line.dwo", ".zdebug_line.dwo" },
338 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
339 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
340 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
341 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
342 { ".debug_str.dwo", ".zdebug_str.dwo" },
343 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
344 { ".debug_types.dwo", ".zdebug_types.dwo" },
345 { ".debug_cu_index", ".zdebug_cu_index" },
346 { ".debug_tu_index", ".zdebug_tu_index" },
347 };
348
349 /* local data types */
350
351 /* The data in a compilation unit header, after target2host
352 translation, looks like this. */
353 struct comp_unit_head
354 {
355 unsigned int length;
356 short version;
357 unsigned char addr_size;
358 unsigned char signed_addr_p;
359 sect_offset abbrev_sect_off;
360
361 /* Size of file offsets; either 4 or 8. */
362 unsigned int offset_size;
363
364 /* Size of the length field; either 4 or 12. */
365 unsigned int initial_length_size;
366
367 enum dwarf_unit_type unit_type;
368
369 /* Offset to the first byte of this compilation unit header in the
370 .debug_info section, for resolving relative reference dies. */
371 sect_offset sect_off;
372
373 /* Offset to first die in this cu from the start of the cu.
374 This will be the first byte following the compilation unit header. */
375 cu_offset first_die_cu_offset;
376
377
378 /* 64-bit signature of this unit. For type units, it denotes the signature of
379 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
380 Also used in DWARF 5, to denote the dwo id when the unit type is
381 DW_UT_skeleton or DW_UT_split_compile. */
382 ULONGEST signature;
383
384 /* For types, offset in the type's DIE of the type defined by this TU. */
385 cu_offset type_cu_offset_in_tu;
386 };
387
388 /* Type used for delaying computation of method physnames.
389 See comments for compute_delayed_physnames. */
390 struct delayed_method_info
391 {
392 /* The type to which the method is attached, i.e., its parent class. */
393 struct type *type;
394
395 /* The index of the method in the type's function fieldlists. */
396 int fnfield_index;
397
398 /* The index of the method in the fieldlist. */
399 int index;
400
401 /* The name of the DIE. */
402 const char *name;
403
404 /* The DIE associated with this method. */
405 struct die_info *die;
406 };
407
408 /* Internal state when decoding a particular compilation unit. */
409 struct dwarf2_cu
410 {
411 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
412 ~dwarf2_cu ();
413
414 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
415
416 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
417 Create the set of symtabs used by this TU, or if this TU is sharing
418 symtabs with another TU and the symtabs have already been created
419 then restore those symtabs in the line header.
420 We don't need the pc/line-number mapping for type units. */
421 void setup_type_unit_groups (struct die_info *die);
422
423 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
424 buildsym_compunit constructor. */
425 struct compunit_symtab *start_symtab (const char *name,
426 const char *comp_dir,
427 CORE_ADDR low_pc);
428
429 /* Reset the builder. */
430 void reset_builder () { m_builder.reset (); }
431
432 /* The header of the compilation unit. */
433 struct comp_unit_head header {};
434
435 /* Base address of this compilation unit. */
436 CORE_ADDR base_address = 0;
437
438 /* Non-zero if base_address has been set. */
439 int base_known = 0;
440
441 /* The language we are debugging. */
442 enum language language = language_unknown;
443 const struct language_defn *language_defn = nullptr;
444
445 const char *producer = nullptr;
446
447 private:
448 /* The symtab builder for this CU. This is only non-NULL when full
449 symbols are being read. */
450 std::unique_ptr<buildsym_compunit> m_builder;
451
452 public:
453 /* The generic symbol table building routines have separate lists for
454 file scope symbols and all all other scopes (local scopes). So
455 we need to select the right one to pass to add_symbol_to_list().
456 We do it by keeping a pointer to the correct list in list_in_scope.
457
458 FIXME: The original dwarf code just treated the file scope as the
459 first local scope, and all other local scopes as nested local
460 scopes, and worked fine. Check to see if we really need to
461 distinguish these in buildsym.c. */
462 struct pending **list_in_scope = nullptr;
463
464 /* Hash table holding all the loaded partial DIEs
465 with partial_die->offset.SECT_OFF as hash. */
466 htab_t partial_dies = nullptr;
467
468 /* Storage for things with the same lifetime as this read-in compilation
469 unit, including partial DIEs. */
470 auto_obstack comp_unit_obstack;
471
472 /* When multiple dwarf2_cu structures are living in memory, this field
473 chains them all together, so that they can be released efficiently.
474 We will probably also want a generation counter so that most-recently-used
475 compilation units are cached... */
476 struct dwarf2_per_cu_data *read_in_chain = nullptr;
477
478 /* Backlink to our per_cu entry. */
479 struct dwarf2_per_cu_data *per_cu;
480
481 /* How many compilation units ago was this CU last referenced? */
482 int last_used = 0;
483
484 /* A hash table of DIE cu_offset for following references with
485 die_info->offset.sect_off as hash. */
486 htab_t die_hash = nullptr;
487
488 /* Full DIEs if read in. */
489 struct die_info *dies = nullptr;
490
491 /* A set of pointers to dwarf2_per_cu_data objects for compilation
492 units referenced by this one. Only set during full symbol processing;
493 partial symbol tables do not have dependencies. */
494 htab_t dependencies = nullptr;
495
496 /* Header data from the line table, during full symbol processing. */
497 struct line_header *line_header = nullptr;
498 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
499 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
500 this is the DW_TAG_compile_unit die for this CU. We'll hold on
501 to the line header as long as this DIE is being processed. See
502 process_die_scope. */
503 die_info *line_header_die_owner = nullptr;
504
505 /* A list of methods which need to have physnames computed
506 after all type information has been read. */
507 std::vector<delayed_method_info> method_list;
508
509 /* To be copied to symtab->call_site_htab. */
510 htab_t call_site_htab = nullptr;
511
512 /* Non-NULL if this CU came from a DWO file.
513 There is an invariant here that is important to remember:
514 Except for attributes copied from the top level DIE in the "main"
515 (or "stub") file in preparation for reading the DWO file
516 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
517 Either there isn't a DWO file (in which case this is NULL and the point
518 is moot), or there is and either we're not going to read it (in which
519 case this is NULL) or there is and we are reading it (in which case this
520 is non-NULL). */
521 struct dwo_unit *dwo_unit = nullptr;
522
523 /* The DW_AT_addr_base attribute if present, zero otherwise
524 (zero is a valid value though).
525 Note this value comes from the Fission stub CU/TU's DIE. */
526 ULONGEST addr_base = 0;
527
528 /* The DW_AT_ranges_base attribute if present, zero otherwise
529 (zero is a valid value though).
530 Note this value comes from the Fission stub CU/TU's DIE.
531 Also note that the value is zero in the non-DWO case so this value can
532 be used without needing to know whether DWO files are in use or not.
533 N.B. This does not apply to DW_AT_ranges appearing in
534 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
535 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
536 DW_AT_ranges_base *would* have to be applied, and we'd have to care
537 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
538 ULONGEST ranges_base = 0;
539
540 /* When reading debug info generated by older versions of rustc, we
541 have to rewrite some union types to be struct types with a
542 variant part. This rewriting must be done after the CU is fully
543 read in, because otherwise at the point of rewriting some struct
544 type might not have been fully processed. So, we keep a list of
545 all such types here and process them after expansion. */
546 std::vector<struct type *> rust_unions;
547
548 /* Mark used when releasing cached dies. */
549 bool mark : 1;
550
551 /* This CU references .debug_loc. See the symtab->locations_valid field.
552 This test is imperfect as there may exist optimized debug code not using
553 any location list and still facing inlining issues if handled as
554 unoptimized code. For a future better test see GCC PR other/32998. */
555 bool has_loclist : 1;
556
557 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
558 if all the producer_is_* fields are valid. This information is cached
559 because profiling CU expansion showed excessive time spent in
560 producer_is_gxx_lt_4_6. */
561 bool checked_producer : 1;
562 bool producer_is_gxx_lt_4_6 : 1;
563 bool producer_is_gcc_lt_4_3 : 1;
564 bool producer_is_icc : 1;
565 bool producer_is_icc_lt_14 : 1;
566 bool producer_is_codewarrior : 1;
567
568 /* When true, the file that we're processing is known to have
569 debugging info for C++ namespaces. GCC 3.3.x did not produce
570 this information, but later versions do. */
571
572 bool processing_has_namespace_info : 1;
573
574 struct partial_die_info *find_partial_die (sect_offset sect_off);
575
576 /* If this CU was inherited by another CU (via specification,
577 abstract_origin, etc), this is the ancestor CU. */
578 dwarf2_cu *ancestor;
579
580 /* Get the buildsym_compunit for this CU. */
581 buildsym_compunit *get_builder ()
582 {
583 /* If this CU has a builder associated with it, use that. */
584 if (m_builder != nullptr)
585 return m_builder.get ();
586
587 /* Otherwise, search ancestors for a valid builder. */
588 if (ancestor != nullptr)
589 return ancestor->get_builder ();
590
591 return nullptr;
592 }
593 };
594
595 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
596 This includes type_unit_group and quick_file_names. */
597
598 struct stmt_list_hash
599 {
600 /* The DWO unit this table is from or NULL if there is none. */
601 struct dwo_unit *dwo_unit;
602
603 /* Offset in .debug_line or .debug_line.dwo. */
604 sect_offset line_sect_off;
605 };
606
607 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
608 an object of this type. */
609
610 struct type_unit_group
611 {
612 /* dwarf2read.c's main "handle" on a TU symtab.
613 To simplify things we create an artificial CU that "includes" all the
614 type units using this stmt_list so that the rest of the code still has
615 a "per_cu" handle on the symtab.
616 This PER_CU is recognized by having no section. */
617 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
618 struct dwarf2_per_cu_data per_cu;
619
620 /* The TUs that share this DW_AT_stmt_list entry.
621 This is added to while parsing type units to build partial symtabs,
622 and is deleted afterwards and not used again. */
623 std::vector <signatured_type *> *tus;
624
625 /* The compunit symtab.
626 Type units in a group needn't all be defined in the same source file,
627 so we create an essentially anonymous symtab as the compunit symtab. */
628 struct compunit_symtab *compunit_symtab;
629
630 /* The data used to construct the hash key. */
631 struct stmt_list_hash hash;
632
633 /* The number of symtabs from the line header.
634 The value here must match line_header.num_file_names. */
635 unsigned int num_symtabs;
636
637 /* The symbol tables for this TU (obtained from the files listed in
638 DW_AT_stmt_list).
639 WARNING: The order of entries here must match the order of entries
640 in the line header. After the first TU using this type_unit_group, the
641 line header for the subsequent TUs is recreated from this. This is done
642 because we need to use the same symtabs for each TU using the same
643 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
644 there's no guarantee the line header doesn't have duplicate entries. */
645 struct symtab **symtabs;
646 };
647
648 /* These sections are what may appear in a (real or virtual) DWO file. */
649
650 struct dwo_sections
651 {
652 struct dwarf2_section_info abbrev;
653 struct dwarf2_section_info line;
654 struct dwarf2_section_info loc;
655 struct dwarf2_section_info loclists;
656 struct dwarf2_section_info macinfo;
657 struct dwarf2_section_info macro;
658 struct dwarf2_section_info str;
659 struct dwarf2_section_info str_offsets;
660 /* In the case of a virtual DWO file, these two are unused. */
661 struct dwarf2_section_info info;
662 std::vector<dwarf2_section_info> types;
663 };
664
665 /* CUs/TUs in DWP/DWO files. */
666
667 struct dwo_unit
668 {
669 /* Backlink to the containing struct dwo_file. */
670 struct dwo_file *dwo_file;
671
672 /* The "id" that distinguishes this CU/TU.
673 .debug_info calls this "dwo_id", .debug_types calls this "signature".
674 Since signatures came first, we stick with it for consistency. */
675 ULONGEST signature;
676
677 /* The section this CU/TU lives in, in the DWO file. */
678 struct dwarf2_section_info *section;
679
680 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
681 sect_offset sect_off;
682 unsigned int length;
683
684 /* For types, offset in the type's DIE of the type defined by this TU. */
685 cu_offset type_offset_in_tu;
686 };
687
688 /* include/dwarf2.h defines the DWP section codes.
689 It defines a max value but it doesn't define a min value, which we
690 use for error checking, so provide one. */
691
692 enum dwp_v2_section_ids
693 {
694 DW_SECT_MIN = 1
695 };
696
697 /* Data for one DWO file.
698
699 This includes virtual DWO files (a virtual DWO file is a DWO file as it
700 appears in a DWP file). DWP files don't really have DWO files per se -
701 comdat folding of types "loses" the DWO file they came from, and from
702 a high level view DWP files appear to contain a mass of random types.
703 However, to maintain consistency with the non-DWP case we pretend DWP
704 files contain virtual DWO files, and we assign each TU with one virtual
705 DWO file (generally based on the line and abbrev section offsets -
706 a heuristic that seems to work in practice). */
707
708 struct dwo_file
709 {
710 dwo_file () = default;
711 DISABLE_COPY_AND_ASSIGN (dwo_file);
712
713 /* The DW_AT_GNU_dwo_name attribute.
714 For virtual DWO files the name is constructed from the section offsets
715 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
716 from related CU+TUs. */
717 const char *dwo_name = nullptr;
718
719 /* The DW_AT_comp_dir attribute. */
720 const char *comp_dir = nullptr;
721
722 /* The bfd, when the file is open. Otherwise this is NULL.
723 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
724 gdb_bfd_ref_ptr dbfd;
725
726 /* The sections that make up this DWO file.
727 Remember that for virtual DWO files in DWP V2, these are virtual
728 sections (for lack of a better name). */
729 struct dwo_sections sections {};
730
731 /* The CUs in the file.
732 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
733 an extension to handle LLVM's Link Time Optimization output (where
734 multiple source files may be compiled into a single object/dwo pair). */
735 htab_t cus {};
736
737 /* Table of TUs in the file.
738 Each element is a struct dwo_unit. */
739 htab_t tus {};
740 };
741
742 /* These sections are what may appear in a DWP file. */
743
744 struct dwp_sections
745 {
746 /* These are used by both DWP version 1 and 2. */
747 struct dwarf2_section_info str;
748 struct dwarf2_section_info cu_index;
749 struct dwarf2_section_info tu_index;
750
751 /* These are only used by DWP version 2 files.
752 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
753 sections are referenced by section number, and are not recorded here.
754 In DWP version 2 there is at most one copy of all these sections, each
755 section being (effectively) comprised of the concatenation of all of the
756 individual sections that exist in the version 1 format.
757 To keep the code simple we treat each of these concatenated pieces as a
758 section itself (a virtual section?). */
759 struct dwarf2_section_info abbrev;
760 struct dwarf2_section_info info;
761 struct dwarf2_section_info line;
762 struct dwarf2_section_info loc;
763 struct dwarf2_section_info macinfo;
764 struct dwarf2_section_info macro;
765 struct dwarf2_section_info str_offsets;
766 struct dwarf2_section_info types;
767 };
768
769 /* These sections are what may appear in a virtual DWO file in DWP version 1.
770 A virtual DWO file is a DWO file as it appears in a DWP file. */
771
772 struct virtual_v1_dwo_sections
773 {
774 struct dwarf2_section_info abbrev;
775 struct dwarf2_section_info line;
776 struct dwarf2_section_info loc;
777 struct dwarf2_section_info macinfo;
778 struct dwarf2_section_info macro;
779 struct dwarf2_section_info str_offsets;
780 /* Each DWP hash table entry records one CU or one TU.
781 That is recorded here, and copied to dwo_unit.section. */
782 struct dwarf2_section_info info_or_types;
783 };
784
785 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
786 In version 2, the sections of the DWO files are concatenated together
787 and stored in one section of that name. Thus each ELF section contains
788 several "virtual" sections. */
789
790 struct virtual_v2_dwo_sections
791 {
792 bfd_size_type abbrev_offset;
793 bfd_size_type abbrev_size;
794
795 bfd_size_type line_offset;
796 bfd_size_type line_size;
797
798 bfd_size_type loc_offset;
799 bfd_size_type loc_size;
800
801 bfd_size_type macinfo_offset;
802 bfd_size_type macinfo_size;
803
804 bfd_size_type macro_offset;
805 bfd_size_type macro_size;
806
807 bfd_size_type str_offsets_offset;
808 bfd_size_type str_offsets_size;
809
810 /* Each DWP hash table entry records one CU or one TU.
811 That is recorded here, and copied to dwo_unit.section. */
812 bfd_size_type info_or_types_offset;
813 bfd_size_type info_or_types_size;
814 };
815
816 /* Contents of DWP hash tables. */
817
818 struct dwp_hash_table
819 {
820 uint32_t version, nr_columns;
821 uint32_t nr_units, nr_slots;
822 const gdb_byte *hash_table, *unit_table;
823 union
824 {
825 struct
826 {
827 const gdb_byte *indices;
828 } v1;
829 struct
830 {
831 /* This is indexed by column number and gives the id of the section
832 in that column. */
833 #define MAX_NR_V2_DWO_SECTIONS \
834 (1 /* .debug_info or .debug_types */ \
835 + 1 /* .debug_abbrev */ \
836 + 1 /* .debug_line */ \
837 + 1 /* .debug_loc */ \
838 + 1 /* .debug_str_offsets */ \
839 + 1 /* .debug_macro or .debug_macinfo */)
840 int section_ids[MAX_NR_V2_DWO_SECTIONS];
841 const gdb_byte *offsets;
842 const gdb_byte *sizes;
843 } v2;
844 } section_pool;
845 };
846
847 /* Data for one DWP file. */
848
849 struct dwp_file
850 {
851 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
852 : name (name_),
853 dbfd (std::move (abfd))
854 {
855 }
856
857 /* Name of the file. */
858 const char *name;
859
860 /* File format version. */
861 int version = 0;
862
863 /* The bfd. */
864 gdb_bfd_ref_ptr dbfd;
865
866 /* Section info for this file. */
867 struct dwp_sections sections {};
868
869 /* Table of CUs in the file. */
870 const struct dwp_hash_table *cus = nullptr;
871
872 /* Table of TUs in the file. */
873 const struct dwp_hash_table *tus = nullptr;
874
875 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
876 htab_t loaded_cus {};
877 htab_t loaded_tus {};
878
879 /* Table to map ELF section numbers to their sections.
880 This is only needed for the DWP V1 file format. */
881 unsigned int num_sections = 0;
882 asection **elf_sections = nullptr;
883 };
884
885 /* Struct used to pass misc. parameters to read_die_and_children, et
886 al. which are used for both .debug_info and .debug_types dies.
887 All parameters here are unchanging for the life of the call. This
888 struct exists to abstract away the constant parameters of die reading. */
889
890 struct die_reader_specs
891 {
892 /* The bfd of die_section. */
893 bfd* abfd;
894
895 /* The CU of the DIE we are parsing. */
896 struct dwarf2_cu *cu;
897
898 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
899 struct dwo_file *dwo_file;
900
901 /* The section the die comes from.
902 This is either .debug_info or .debug_types, or the .dwo variants. */
903 struct dwarf2_section_info *die_section;
904
905 /* die_section->buffer. */
906 const gdb_byte *buffer;
907
908 /* The end of the buffer. */
909 const gdb_byte *buffer_end;
910
911 /* The value of the DW_AT_comp_dir attribute. */
912 const char *comp_dir;
913
914 /* The abbreviation table to use when reading the DIEs. */
915 struct abbrev_table *abbrev_table;
916 };
917
918 /* Type of function passed to init_cutu_and_read_dies, et.al. */
919 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
920 const gdb_byte *info_ptr,
921 struct die_info *comp_unit_die,
922 int has_children,
923 void *data);
924
925 /* A 1-based directory index. This is a strong typedef to prevent
926 accidentally using a directory index as a 0-based index into an
927 array/vector. */
928 enum class dir_index : unsigned int {};
929
930 /* Likewise, a 1-based file name index. */
931 enum class file_name_index : unsigned int {};
932
933 struct file_entry
934 {
935 file_entry () = default;
936
937 file_entry (const char *name_, dir_index d_index_,
938 unsigned int mod_time_, unsigned int length_)
939 : name (name_),
940 d_index (d_index_),
941 mod_time (mod_time_),
942 length (length_)
943 {}
944
945 /* Return the include directory at D_INDEX stored in LH. Returns
946 NULL if D_INDEX is out of bounds. */
947 const char *include_dir (const line_header *lh) const;
948
949 /* The file name. Note this is an observing pointer. The memory is
950 owned by debug_line_buffer. */
951 const char *name {};
952
953 /* The directory index (1-based). */
954 dir_index d_index {};
955
956 unsigned int mod_time {};
957
958 unsigned int length {};
959
960 /* True if referenced by the Line Number Program. */
961 bool included_p {};
962
963 /* The associated symbol table, if any. */
964 struct symtab *symtab {};
965 };
966
967 /* The line number information for a compilation unit (found in the
968 .debug_line section) begins with a "statement program header",
969 which contains the following information. */
970 struct line_header
971 {
972 line_header ()
973 : offset_in_dwz {}
974 {}
975
976 /* Add an entry to the include directory table. */
977 void add_include_dir (const char *include_dir);
978
979 /* Add an entry to the file name table. */
980 void add_file_name (const char *name, dir_index d_index,
981 unsigned int mod_time, unsigned int length);
982
983 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
984 is out of bounds. */
985 const char *include_dir_at (dir_index index) const
986 {
987 /* Convert directory index number (1-based) to vector index
988 (0-based). */
989 size_t vec_index = to_underlying (index) - 1;
990
991 if (vec_index >= include_dirs.size ())
992 return NULL;
993 return include_dirs[vec_index];
994 }
995
996 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
997 is out of bounds. */
998 file_entry *file_name_at (file_name_index index)
999 {
1000 /* Convert file name index number (1-based) to vector index
1001 (0-based). */
1002 size_t vec_index = to_underlying (index) - 1;
1003
1004 if (vec_index >= file_names.size ())
1005 return NULL;
1006 return &file_names[vec_index];
1007 }
1008
1009 /* Offset of line number information in .debug_line section. */
1010 sect_offset sect_off {};
1011
1012 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1013 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1014
1015 unsigned int total_length {};
1016 unsigned short version {};
1017 unsigned int header_length {};
1018 unsigned char minimum_instruction_length {};
1019 unsigned char maximum_ops_per_instruction {};
1020 unsigned char default_is_stmt {};
1021 int line_base {};
1022 unsigned char line_range {};
1023 unsigned char opcode_base {};
1024
1025 /* standard_opcode_lengths[i] is the number of operands for the
1026 standard opcode whose value is i. This means that
1027 standard_opcode_lengths[0] is unused, and the last meaningful
1028 element is standard_opcode_lengths[opcode_base - 1]. */
1029 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1030
1031 /* The include_directories table. Note these are observing
1032 pointers. The memory is owned by debug_line_buffer. */
1033 std::vector<const char *> include_dirs;
1034
1035 /* The file_names table. */
1036 std::vector<file_entry> file_names;
1037
1038 /* The start and end of the statement program following this
1039 header. These point into dwarf2_per_objfile->line_buffer. */
1040 const gdb_byte *statement_program_start {}, *statement_program_end {};
1041 };
1042
1043 typedef std::unique_ptr<line_header> line_header_up;
1044
1045 const char *
1046 file_entry::include_dir (const line_header *lh) const
1047 {
1048 return lh->include_dir_at (d_index);
1049 }
1050
1051 /* When we construct a partial symbol table entry we only
1052 need this much information. */
1053 struct partial_die_info : public allocate_on_obstack
1054 {
1055 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1056
1057 /* Disable assign but still keep copy ctor, which is needed
1058 load_partial_dies. */
1059 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1060
1061 /* Adjust the partial die before generating a symbol for it. This
1062 function may set the is_external flag or change the DIE's
1063 name. */
1064 void fixup (struct dwarf2_cu *cu);
1065
1066 /* Read a minimal amount of information into the minimal die
1067 structure. */
1068 const gdb_byte *read (const struct die_reader_specs *reader,
1069 const struct abbrev_info &abbrev,
1070 const gdb_byte *info_ptr);
1071
1072 /* Offset of this DIE. */
1073 const sect_offset sect_off;
1074
1075 /* DWARF-2 tag for this DIE. */
1076 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1077
1078 /* Assorted flags describing the data found in this DIE. */
1079 const unsigned int has_children : 1;
1080
1081 unsigned int is_external : 1;
1082 unsigned int is_declaration : 1;
1083 unsigned int has_type : 1;
1084 unsigned int has_specification : 1;
1085 unsigned int has_pc_info : 1;
1086 unsigned int may_be_inlined : 1;
1087
1088 /* This DIE has been marked DW_AT_main_subprogram. */
1089 unsigned int main_subprogram : 1;
1090
1091 /* Flag set if the SCOPE field of this structure has been
1092 computed. */
1093 unsigned int scope_set : 1;
1094
1095 /* Flag set if the DIE has a byte_size attribute. */
1096 unsigned int has_byte_size : 1;
1097
1098 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1099 unsigned int has_const_value : 1;
1100
1101 /* Flag set if any of the DIE's children are template arguments. */
1102 unsigned int has_template_arguments : 1;
1103
1104 /* Flag set if fixup has been called on this die. */
1105 unsigned int fixup_called : 1;
1106
1107 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1108 unsigned int is_dwz : 1;
1109
1110 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1111 unsigned int spec_is_dwz : 1;
1112
1113 /* The name of this DIE. Normally the value of DW_AT_name, but
1114 sometimes a default name for unnamed DIEs. */
1115 const char *name = nullptr;
1116
1117 /* The linkage name, if present. */
1118 const char *linkage_name = nullptr;
1119
1120 /* The scope to prepend to our children. This is generally
1121 allocated on the comp_unit_obstack, so will disappear
1122 when this compilation unit leaves the cache. */
1123 const char *scope = nullptr;
1124
1125 /* Some data associated with the partial DIE. The tag determines
1126 which field is live. */
1127 union
1128 {
1129 /* The location description associated with this DIE, if any. */
1130 struct dwarf_block *locdesc;
1131 /* The offset of an import, for DW_TAG_imported_unit. */
1132 sect_offset sect_off;
1133 } d {};
1134
1135 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1136 CORE_ADDR lowpc = 0;
1137 CORE_ADDR highpc = 0;
1138
1139 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1140 DW_AT_sibling, if any. */
1141 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1142 could return DW_AT_sibling values to its caller load_partial_dies. */
1143 const gdb_byte *sibling = nullptr;
1144
1145 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1146 DW_AT_specification (or DW_AT_abstract_origin or
1147 DW_AT_extension). */
1148 sect_offset spec_offset {};
1149
1150 /* Pointers to this DIE's parent, first child, and next sibling,
1151 if any. */
1152 struct partial_die_info *die_parent = nullptr;
1153 struct partial_die_info *die_child = nullptr;
1154 struct partial_die_info *die_sibling = nullptr;
1155
1156 friend struct partial_die_info *
1157 dwarf2_cu::find_partial_die (sect_offset sect_off);
1158
1159 private:
1160 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1161 partial_die_info (sect_offset sect_off)
1162 : partial_die_info (sect_off, DW_TAG_padding, 0)
1163 {
1164 }
1165
1166 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1167 int has_children_)
1168 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1169 {
1170 is_external = 0;
1171 is_declaration = 0;
1172 has_type = 0;
1173 has_specification = 0;
1174 has_pc_info = 0;
1175 may_be_inlined = 0;
1176 main_subprogram = 0;
1177 scope_set = 0;
1178 has_byte_size = 0;
1179 has_const_value = 0;
1180 has_template_arguments = 0;
1181 fixup_called = 0;
1182 is_dwz = 0;
1183 spec_is_dwz = 0;
1184 }
1185 };
1186
1187 /* This data structure holds the information of an abbrev. */
1188 struct abbrev_info
1189 {
1190 unsigned int number; /* number identifying abbrev */
1191 enum dwarf_tag tag; /* dwarf tag */
1192 unsigned short has_children; /* boolean */
1193 unsigned short num_attrs; /* number of attributes */
1194 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1195 struct abbrev_info *next; /* next in chain */
1196 };
1197
1198 struct attr_abbrev
1199 {
1200 ENUM_BITFIELD(dwarf_attribute) name : 16;
1201 ENUM_BITFIELD(dwarf_form) form : 16;
1202
1203 /* It is valid only if FORM is DW_FORM_implicit_const. */
1204 LONGEST implicit_const;
1205 };
1206
1207 /* Size of abbrev_table.abbrev_hash_table. */
1208 #define ABBREV_HASH_SIZE 121
1209
1210 /* Top level data structure to contain an abbreviation table. */
1211
1212 struct abbrev_table
1213 {
1214 explicit abbrev_table (sect_offset off)
1215 : sect_off (off)
1216 {
1217 m_abbrevs =
1218 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1219 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1220 }
1221
1222 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1223
1224 /* Allocate space for a struct abbrev_info object in
1225 ABBREV_TABLE. */
1226 struct abbrev_info *alloc_abbrev ();
1227
1228 /* Add an abbreviation to the table. */
1229 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1230
1231 /* Look up an abbrev in the table.
1232 Returns NULL if the abbrev is not found. */
1233
1234 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1235
1236
1237 /* Where the abbrev table came from.
1238 This is used as a sanity check when the table is used. */
1239 const sect_offset sect_off;
1240
1241 /* Storage for the abbrev table. */
1242 auto_obstack abbrev_obstack;
1243
1244 private:
1245
1246 /* Hash table of abbrevs.
1247 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1248 It could be statically allocated, but the previous code didn't so we
1249 don't either. */
1250 struct abbrev_info **m_abbrevs;
1251 };
1252
1253 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1254
1255 /* Attributes have a name and a value. */
1256 struct attribute
1257 {
1258 ENUM_BITFIELD(dwarf_attribute) name : 16;
1259 ENUM_BITFIELD(dwarf_form) form : 15;
1260
1261 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1262 field should be in u.str (existing only for DW_STRING) but it is kept
1263 here for better struct attribute alignment. */
1264 unsigned int string_is_canonical : 1;
1265
1266 union
1267 {
1268 const char *str;
1269 struct dwarf_block *blk;
1270 ULONGEST unsnd;
1271 LONGEST snd;
1272 CORE_ADDR addr;
1273 ULONGEST signature;
1274 }
1275 u;
1276 };
1277
1278 /* This data structure holds a complete die structure. */
1279 struct die_info
1280 {
1281 /* DWARF-2 tag for this DIE. */
1282 ENUM_BITFIELD(dwarf_tag) tag : 16;
1283
1284 /* Number of attributes */
1285 unsigned char num_attrs;
1286
1287 /* True if we're presently building the full type name for the
1288 type derived from this DIE. */
1289 unsigned char building_fullname : 1;
1290
1291 /* True if this die is in process. PR 16581. */
1292 unsigned char in_process : 1;
1293
1294 /* Abbrev number */
1295 unsigned int abbrev;
1296
1297 /* Offset in .debug_info or .debug_types section. */
1298 sect_offset sect_off;
1299
1300 /* The dies in a compilation unit form an n-ary tree. PARENT
1301 points to this die's parent; CHILD points to the first child of
1302 this node; and all the children of a given node are chained
1303 together via their SIBLING fields. */
1304 struct die_info *child; /* Its first child, if any. */
1305 struct die_info *sibling; /* Its next sibling, if any. */
1306 struct die_info *parent; /* Its parent, if any. */
1307
1308 /* An array of attributes, with NUM_ATTRS elements. There may be
1309 zero, but it's not common and zero-sized arrays are not
1310 sufficiently portable C. */
1311 struct attribute attrs[1];
1312 };
1313
1314 /* Get at parts of an attribute structure. */
1315
1316 #define DW_STRING(attr) ((attr)->u.str)
1317 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1318 #define DW_UNSND(attr) ((attr)->u.unsnd)
1319 #define DW_BLOCK(attr) ((attr)->u.blk)
1320 #define DW_SND(attr) ((attr)->u.snd)
1321 #define DW_ADDR(attr) ((attr)->u.addr)
1322 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1323
1324 /* Blocks are a bunch of untyped bytes. */
1325 struct dwarf_block
1326 {
1327 size_t size;
1328
1329 /* Valid only if SIZE is not zero. */
1330 const gdb_byte *data;
1331 };
1332
1333 #ifndef ATTR_ALLOC_CHUNK
1334 #define ATTR_ALLOC_CHUNK 4
1335 #endif
1336
1337 /* Allocate fields for structs, unions and enums in this size. */
1338 #ifndef DW_FIELD_ALLOC_CHUNK
1339 #define DW_FIELD_ALLOC_CHUNK 4
1340 #endif
1341
1342 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1343 but this would require a corresponding change in unpack_field_as_long
1344 and friends. */
1345 static int bits_per_byte = 8;
1346
1347 /* When reading a variant or variant part, we track a bit more
1348 information about the field, and store it in an object of this
1349 type. */
1350
1351 struct variant_field
1352 {
1353 /* If we see a DW_TAG_variant, then this will be the discriminant
1354 value. */
1355 ULONGEST discriminant_value;
1356 /* If we see a DW_TAG_variant, then this will be set if this is the
1357 default branch. */
1358 bool default_branch;
1359 /* While reading a DW_TAG_variant_part, this will be set if this
1360 field is the discriminant. */
1361 bool is_discriminant;
1362 };
1363
1364 struct nextfield
1365 {
1366 int accessibility = 0;
1367 int virtuality = 0;
1368 /* Extra information to describe a variant or variant part. */
1369 struct variant_field variant {};
1370 struct field field {};
1371 };
1372
1373 struct fnfieldlist
1374 {
1375 const char *name = nullptr;
1376 std::vector<struct fn_field> fnfields;
1377 };
1378
1379 /* The routines that read and process dies for a C struct or C++ class
1380 pass lists of data member fields and lists of member function fields
1381 in an instance of a field_info structure, as defined below. */
1382 struct field_info
1383 {
1384 /* List of data member and baseclasses fields. */
1385 std::vector<struct nextfield> fields;
1386 std::vector<struct nextfield> baseclasses;
1387
1388 /* Number of fields (including baseclasses). */
1389 int nfields = 0;
1390
1391 /* Set if the accesibility of one of the fields is not public. */
1392 int non_public_fields = 0;
1393
1394 /* Member function fieldlist array, contains name of possibly overloaded
1395 member function, number of overloaded member functions and a pointer
1396 to the head of the member function field chain. */
1397 std::vector<struct fnfieldlist> fnfieldlists;
1398
1399 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1400 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1401 std::vector<struct decl_field> typedef_field_list;
1402
1403 /* Nested types defined by this class and the number of elements in this
1404 list. */
1405 std::vector<struct decl_field> nested_types_list;
1406 };
1407
1408 /* One item on the queue of compilation units to read in full symbols
1409 for. */
1410 struct dwarf2_queue_item
1411 {
1412 struct dwarf2_per_cu_data *per_cu;
1413 enum language pretend_language;
1414 struct dwarf2_queue_item *next;
1415 };
1416
1417 /* The current queue. */
1418 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1419
1420 /* Loaded secondary compilation units are kept in memory until they
1421 have not been referenced for the processing of this many
1422 compilation units. Set this to zero to disable caching. Cache
1423 sizes of up to at least twenty will improve startup time for
1424 typical inter-CU-reference binaries, at an obvious memory cost. */
1425 static int dwarf_max_cache_age = 5;
1426 static void
1427 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1428 struct cmd_list_element *c, const char *value)
1429 {
1430 fprintf_filtered (file, _("The upper bound on the age of cached "
1431 "DWARF compilation units is %s.\n"),
1432 value);
1433 }
1434 \f
1435 /* local function prototypes */
1436
1437 static const char *get_section_name (const struct dwarf2_section_info *);
1438
1439 static const char *get_section_file_name (const struct dwarf2_section_info *);
1440
1441 static void dwarf2_find_base_address (struct die_info *die,
1442 struct dwarf2_cu *cu);
1443
1444 static struct partial_symtab *create_partial_symtab
1445 (struct dwarf2_per_cu_data *per_cu, const char *name);
1446
1447 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1448 const gdb_byte *info_ptr,
1449 struct die_info *type_unit_die,
1450 int has_children, void *data);
1451
1452 static void dwarf2_build_psymtabs_hard
1453 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1454
1455 static void scan_partial_symbols (struct partial_die_info *,
1456 CORE_ADDR *, CORE_ADDR *,
1457 int, struct dwarf2_cu *);
1458
1459 static void add_partial_symbol (struct partial_die_info *,
1460 struct dwarf2_cu *);
1461
1462 static void add_partial_namespace (struct partial_die_info *pdi,
1463 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1464 int set_addrmap, struct dwarf2_cu *cu);
1465
1466 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1467 CORE_ADDR *highpc, int set_addrmap,
1468 struct dwarf2_cu *cu);
1469
1470 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1471 struct dwarf2_cu *cu);
1472
1473 static void add_partial_subprogram (struct partial_die_info *pdi,
1474 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1475 int need_pc, struct dwarf2_cu *cu);
1476
1477 static void dwarf2_read_symtab (struct partial_symtab *,
1478 struct objfile *);
1479
1480 static void psymtab_to_symtab_1 (struct partial_symtab *);
1481
1482 static abbrev_table_up abbrev_table_read_table
1483 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1484 sect_offset);
1485
1486 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1487
1488 static struct partial_die_info *load_partial_dies
1489 (const struct die_reader_specs *, const gdb_byte *, int);
1490
1491 /* A pair of partial_die_info and compilation unit. */
1492 struct cu_partial_die_info
1493 {
1494 /* The compilation unit of the partial_die_info. */
1495 struct dwarf2_cu *cu;
1496 /* A partial_die_info. */
1497 struct partial_die_info *pdi;
1498
1499 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1500 : cu (cu),
1501 pdi (pdi)
1502 { /* Nothhing. */ }
1503
1504 private:
1505 cu_partial_die_info () = delete;
1506 };
1507
1508 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1509 struct dwarf2_cu *);
1510
1511 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1512 struct attribute *, struct attr_abbrev *,
1513 const gdb_byte *);
1514
1515 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1516
1517 static int read_1_signed_byte (bfd *, const gdb_byte *);
1518
1519 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1520
1521 /* Read the next three bytes (little-endian order) as an unsigned integer. */
1522 static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1523
1524 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1525
1526 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1527
1528 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1529 unsigned int *);
1530
1531 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1532
1533 static LONGEST read_checked_initial_length_and_offset
1534 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1535 unsigned int *, unsigned int *);
1536
1537 static LONGEST read_offset (bfd *, const gdb_byte *,
1538 const struct comp_unit_head *,
1539 unsigned int *);
1540
1541 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1542
1543 static sect_offset read_abbrev_offset
1544 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1545 struct dwarf2_section_info *, sect_offset);
1546
1547 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1548
1549 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1550
1551 static const char *read_indirect_string
1552 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1553 const struct comp_unit_head *, unsigned int *);
1554
1555 static const char *read_indirect_line_string
1556 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1557 const struct comp_unit_head *, unsigned int *);
1558
1559 static const char *read_indirect_string_at_offset
1560 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1561 LONGEST str_offset);
1562
1563 static const char *read_indirect_string_from_dwz
1564 (struct objfile *objfile, struct dwz_file *, LONGEST);
1565
1566 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1567
1568 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1569 const gdb_byte *,
1570 unsigned int *);
1571
1572 static const char *read_str_index (const struct die_reader_specs *reader,
1573 ULONGEST str_index);
1574
1575 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1576
1577 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1578 struct dwarf2_cu *);
1579
1580 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1581 unsigned int);
1582
1583 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1584 struct dwarf2_cu *cu);
1585
1586 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1587
1588 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1589 struct dwarf2_cu *cu);
1590
1591 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1592
1593 static struct die_info *die_specification (struct die_info *die,
1594 struct dwarf2_cu **);
1595
1596 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1597 struct dwarf2_cu *cu);
1598
1599 static void dwarf_decode_lines (struct line_header *, const char *,
1600 struct dwarf2_cu *, struct partial_symtab *,
1601 CORE_ADDR, int decode_mapping);
1602
1603 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1604 const char *);
1605
1606 static struct symbol *new_symbol (struct die_info *, struct type *,
1607 struct dwarf2_cu *, struct symbol * = NULL);
1608
1609 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1610 struct dwarf2_cu *);
1611
1612 static void dwarf2_const_value_attr (const struct attribute *attr,
1613 struct type *type,
1614 const char *name,
1615 struct obstack *obstack,
1616 struct dwarf2_cu *cu, LONGEST *value,
1617 const gdb_byte **bytes,
1618 struct dwarf2_locexpr_baton **baton);
1619
1620 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1621
1622 static int need_gnat_info (struct dwarf2_cu *);
1623
1624 static struct type *die_descriptive_type (struct die_info *,
1625 struct dwarf2_cu *);
1626
1627 static void set_descriptive_type (struct type *, struct die_info *,
1628 struct dwarf2_cu *);
1629
1630 static struct type *die_containing_type (struct die_info *,
1631 struct dwarf2_cu *);
1632
1633 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1634 struct dwarf2_cu *);
1635
1636 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1637
1638 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1639
1640 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1641
1642 static char *typename_concat (struct obstack *obs, const char *prefix,
1643 const char *suffix, int physname,
1644 struct dwarf2_cu *cu);
1645
1646 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1647
1648 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1649
1650 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1651
1652 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1653
1654 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1655
1656 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1657
1658 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1659 struct dwarf2_cu *, struct partial_symtab *);
1660
1661 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1662 values. Keep the items ordered with increasing constraints compliance. */
1663 enum pc_bounds_kind
1664 {
1665 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1666 PC_BOUNDS_NOT_PRESENT,
1667
1668 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1669 were present but they do not form a valid range of PC addresses. */
1670 PC_BOUNDS_INVALID,
1671
1672 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1673 PC_BOUNDS_RANGES,
1674
1675 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1676 PC_BOUNDS_HIGH_LOW,
1677 };
1678
1679 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1680 CORE_ADDR *, CORE_ADDR *,
1681 struct dwarf2_cu *,
1682 struct partial_symtab *);
1683
1684 static void get_scope_pc_bounds (struct die_info *,
1685 CORE_ADDR *, CORE_ADDR *,
1686 struct dwarf2_cu *);
1687
1688 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1689 CORE_ADDR, struct dwarf2_cu *);
1690
1691 static void dwarf2_add_field (struct field_info *, struct die_info *,
1692 struct dwarf2_cu *);
1693
1694 static void dwarf2_attach_fields_to_type (struct field_info *,
1695 struct type *, struct dwarf2_cu *);
1696
1697 static void dwarf2_add_member_fn (struct field_info *,
1698 struct die_info *, struct type *,
1699 struct dwarf2_cu *);
1700
1701 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1702 struct type *,
1703 struct dwarf2_cu *);
1704
1705 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1706
1707 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1708
1709 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1710
1711 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1712
1713 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1714
1715 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1716
1717 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1718
1719 static struct type *read_module_type (struct die_info *die,
1720 struct dwarf2_cu *cu);
1721
1722 static const char *namespace_name (struct die_info *die,
1723 int *is_anonymous, struct dwarf2_cu *);
1724
1725 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1726
1727 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1728
1729 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1730 struct dwarf2_cu *);
1731
1732 static struct die_info *read_die_and_siblings_1
1733 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1734 struct die_info *);
1735
1736 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1737 const gdb_byte *info_ptr,
1738 const gdb_byte **new_info_ptr,
1739 struct die_info *parent);
1740
1741 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1742 struct die_info **, const gdb_byte *,
1743 int *, int);
1744
1745 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1746 struct die_info **, const gdb_byte *,
1747 int *);
1748
1749 static void process_die (struct die_info *, struct dwarf2_cu *);
1750
1751 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1752 struct obstack *);
1753
1754 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1755
1756 static const char *dwarf2_full_name (const char *name,
1757 struct die_info *die,
1758 struct dwarf2_cu *cu);
1759
1760 static const char *dwarf2_physname (const char *name, struct die_info *die,
1761 struct dwarf2_cu *cu);
1762
1763 static struct die_info *dwarf2_extension (struct die_info *die,
1764 struct dwarf2_cu **);
1765
1766 static const char *dwarf_tag_name (unsigned int);
1767
1768 static const char *dwarf_attr_name (unsigned int);
1769
1770 static const char *dwarf_unit_type_name (int unit_type);
1771
1772 static const char *dwarf_form_name (unsigned int);
1773
1774 static const char *dwarf_bool_name (unsigned int);
1775
1776 static const char *dwarf_type_encoding_name (unsigned int);
1777
1778 static struct die_info *sibling_die (struct die_info *);
1779
1780 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1781
1782 static void dump_die_for_error (struct die_info *);
1783
1784 static void dump_die_1 (struct ui_file *, int level, int max_level,
1785 struct die_info *);
1786
1787 /*static*/ void dump_die (struct die_info *, int max_level);
1788
1789 static void store_in_ref_table (struct die_info *,
1790 struct dwarf2_cu *);
1791
1792 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1793
1794 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1795
1796 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1797 const struct attribute *,
1798 struct dwarf2_cu **);
1799
1800 static struct die_info *follow_die_ref (struct die_info *,
1801 const struct attribute *,
1802 struct dwarf2_cu **);
1803
1804 static struct die_info *follow_die_sig (struct die_info *,
1805 const struct attribute *,
1806 struct dwarf2_cu **);
1807
1808 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1809 struct dwarf2_cu *);
1810
1811 static struct type *get_DW_AT_signature_type (struct die_info *,
1812 const struct attribute *,
1813 struct dwarf2_cu *);
1814
1815 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1816
1817 static void read_signatured_type (struct signatured_type *);
1818
1819 static int attr_to_dynamic_prop (const struct attribute *attr,
1820 struct die_info *die, struct dwarf2_cu *cu,
1821 struct dynamic_prop *prop, struct type *type);
1822
1823 /* memory allocation interface */
1824
1825 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1826
1827 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1828
1829 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1830
1831 static int attr_form_is_block (const struct attribute *);
1832
1833 static int attr_form_is_section_offset (const struct attribute *);
1834
1835 static int attr_form_is_constant (const struct attribute *);
1836
1837 static int attr_form_is_ref (const struct attribute *);
1838
1839 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1840 struct dwarf2_loclist_baton *baton,
1841 const struct attribute *attr);
1842
1843 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1844 struct symbol *sym,
1845 struct dwarf2_cu *cu,
1846 int is_block);
1847
1848 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1849 const gdb_byte *info_ptr,
1850 struct abbrev_info *abbrev);
1851
1852 static hashval_t partial_die_hash (const void *item);
1853
1854 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1855
1856 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1857 (sect_offset sect_off, unsigned int offset_in_dwz,
1858 struct dwarf2_per_objfile *dwarf2_per_objfile);
1859
1860 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1861 struct die_info *comp_unit_die,
1862 enum language pretend_language);
1863
1864 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1865
1866 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1867
1868 static struct type *set_die_type (struct die_info *, struct type *,
1869 struct dwarf2_cu *);
1870
1871 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1872
1873 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1874
1875 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1876 enum language);
1877
1878 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1879 enum language);
1880
1881 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1882 enum language);
1883
1884 static void dwarf2_add_dependence (struct dwarf2_cu *,
1885 struct dwarf2_per_cu_data *);
1886
1887 static void dwarf2_mark (struct dwarf2_cu *);
1888
1889 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1890
1891 static struct type *get_die_type_at_offset (sect_offset,
1892 struct dwarf2_per_cu_data *);
1893
1894 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1895
1896 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1897 enum language pretend_language);
1898
1899 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1900
1901 static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1902 static struct type *dwarf2_per_cu_addr_sized_int_type
1903 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1904
1905 /* Class, the destructor of which frees all allocated queue entries. This
1906 will only have work to do if an error was thrown while processing the
1907 dwarf. If no error was thrown then the queue entries should have all
1908 been processed, and freed, as we went along. */
1909
1910 class dwarf2_queue_guard
1911 {
1912 public:
1913 dwarf2_queue_guard () = default;
1914
1915 /* Free any entries remaining on the queue. There should only be
1916 entries left if we hit an error while processing the dwarf. */
1917 ~dwarf2_queue_guard ()
1918 {
1919 struct dwarf2_queue_item *item, *last;
1920
1921 item = dwarf2_queue;
1922 while (item)
1923 {
1924 /* Anything still marked queued is likely to be in an
1925 inconsistent state, so discard it. */
1926 if (item->per_cu->queued)
1927 {
1928 if (item->per_cu->cu != NULL)
1929 free_one_cached_comp_unit (item->per_cu);
1930 item->per_cu->queued = 0;
1931 }
1932
1933 last = item;
1934 item = item->next;
1935 xfree (last);
1936 }
1937
1938 dwarf2_queue = dwarf2_queue_tail = NULL;
1939 }
1940 };
1941
1942 /* The return type of find_file_and_directory. Note, the enclosed
1943 string pointers are only valid while this object is valid. */
1944
1945 struct file_and_directory
1946 {
1947 /* The filename. This is never NULL. */
1948 const char *name;
1949
1950 /* The compilation directory. NULL if not known. If we needed to
1951 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1952 points directly to the DW_AT_comp_dir string attribute owned by
1953 the obstack that owns the DIE. */
1954 const char *comp_dir;
1955
1956 /* If we needed to build a new string for comp_dir, this is what
1957 owns the storage. */
1958 std::string comp_dir_storage;
1959 };
1960
1961 static file_and_directory find_file_and_directory (struct die_info *die,
1962 struct dwarf2_cu *cu);
1963
1964 static char *file_full_name (int file, struct line_header *lh,
1965 const char *comp_dir);
1966
1967 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1968 enum class rcuh_kind { COMPILE, TYPE };
1969
1970 static const gdb_byte *read_and_check_comp_unit_head
1971 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1972 struct comp_unit_head *header,
1973 struct dwarf2_section_info *section,
1974 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1975 rcuh_kind section_kind);
1976
1977 static void init_cutu_and_read_dies
1978 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1979 int use_existing_cu, int keep, bool skip_partial,
1980 die_reader_func_ftype *die_reader_func, void *data);
1981
1982 static void init_cutu_and_read_dies_simple
1983 (struct dwarf2_per_cu_data *this_cu,
1984 die_reader_func_ftype *die_reader_func, void *data);
1985
1986 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1987
1988 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1989
1990 static struct dwo_unit *lookup_dwo_unit_in_dwp
1991 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1992 struct dwp_file *dwp_file, const char *comp_dir,
1993 ULONGEST signature, int is_debug_types);
1994
1995 static struct dwp_file *get_dwp_file
1996 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1997
1998 static struct dwo_unit *lookup_dwo_comp_unit
1999 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2000
2001 static struct dwo_unit *lookup_dwo_type_unit
2002 (struct signatured_type *, const char *, const char *);
2003
2004 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2005
2006 /* A unique pointer to a dwo_file. */
2007
2008 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
2009
2010 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2011
2012 static void check_producer (struct dwarf2_cu *cu);
2013
2014 static void free_line_header_voidp (void *arg);
2015 \f
2016 /* Various complaints about symbol reading that don't abort the process. */
2017
2018 static void
2019 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2020 {
2021 complaint (_("statement list doesn't fit in .debug_line section"));
2022 }
2023
2024 static void
2025 dwarf2_debug_line_missing_file_complaint (void)
2026 {
2027 complaint (_(".debug_line section has line data without a file"));
2028 }
2029
2030 static void
2031 dwarf2_debug_line_missing_end_sequence_complaint (void)
2032 {
2033 complaint (_(".debug_line section has line "
2034 "program sequence without an end"));
2035 }
2036
2037 static void
2038 dwarf2_complex_location_expr_complaint (void)
2039 {
2040 complaint (_("location expression too complex"));
2041 }
2042
2043 static void
2044 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2045 int arg3)
2046 {
2047 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
2048 arg1, arg2, arg3);
2049 }
2050
2051 static void
2052 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2053 {
2054 complaint (_("debug info runs off end of %s section"
2055 " [in module %s]"),
2056 get_section_name (section),
2057 get_section_file_name (section));
2058 }
2059
2060 static void
2061 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2062 {
2063 complaint (_("macro debug info contains a "
2064 "malformed macro definition:\n`%s'"),
2065 arg1);
2066 }
2067
2068 static void
2069 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2070 {
2071 complaint (_("invalid attribute class or form for '%s' in '%s'"),
2072 arg1, arg2);
2073 }
2074
2075 /* Hash function for line_header_hash. */
2076
2077 static hashval_t
2078 line_header_hash (const struct line_header *ofs)
2079 {
2080 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2081 }
2082
2083 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2084
2085 static hashval_t
2086 line_header_hash_voidp (const void *item)
2087 {
2088 const struct line_header *ofs = (const struct line_header *) item;
2089
2090 return line_header_hash (ofs);
2091 }
2092
2093 /* Equality function for line_header_hash. */
2094
2095 static int
2096 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2097 {
2098 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2099 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2100
2101 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2102 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2103 }
2104
2105 \f
2106
2107 /* Read the given attribute value as an address, taking the attribute's
2108 form into account. */
2109
2110 static CORE_ADDR
2111 attr_value_as_address (struct attribute *attr)
2112 {
2113 CORE_ADDR addr;
2114
2115 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2116 && attr->form != DW_FORM_GNU_addr_index)
2117 {
2118 /* Aside from a few clearly defined exceptions, attributes that
2119 contain an address must always be in DW_FORM_addr form.
2120 Unfortunately, some compilers happen to be violating this
2121 requirement by encoding addresses using other forms, such
2122 as DW_FORM_data4 for example. For those broken compilers,
2123 we try to do our best, without any guarantee of success,
2124 to interpret the address correctly. It would also be nice
2125 to generate a complaint, but that would require us to maintain
2126 a list of legitimate cases where a non-address form is allowed,
2127 as well as update callers to pass in at least the CU's DWARF
2128 version. This is more overhead than what we're willing to
2129 expand for a pretty rare case. */
2130 addr = DW_UNSND (attr);
2131 }
2132 else
2133 addr = DW_ADDR (attr);
2134
2135 return addr;
2136 }
2137
2138 /* See declaration. */
2139
2140 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2141 const dwarf2_debug_sections *names,
2142 bool can_copy_)
2143 : objfile (objfile_),
2144 can_copy (can_copy_)
2145 {
2146 if (names == NULL)
2147 names = &dwarf2_elf_names;
2148
2149 bfd *obfd = objfile->obfd;
2150
2151 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2152 locate_sections (obfd, sec, *names);
2153 }
2154
2155 dwarf2_per_objfile::~dwarf2_per_objfile ()
2156 {
2157 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2158 free_cached_comp_units ();
2159
2160 if (quick_file_names_table)
2161 htab_delete (quick_file_names_table);
2162
2163 if (line_header_hash)
2164 htab_delete (line_header_hash);
2165
2166 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2167 VEC_free (dwarf2_per_cu_ptr, per_cu->imported_symtabs);
2168
2169 for (signatured_type *sig_type : all_type_units)
2170 VEC_free (dwarf2_per_cu_ptr, sig_type->per_cu.imported_symtabs);
2171
2172 /* Everything else should be on the objfile obstack. */
2173 }
2174
2175 /* See declaration. */
2176
2177 void
2178 dwarf2_per_objfile::free_cached_comp_units ()
2179 {
2180 dwarf2_per_cu_data *per_cu = read_in_chain;
2181 dwarf2_per_cu_data **last_chain = &read_in_chain;
2182 while (per_cu != NULL)
2183 {
2184 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2185
2186 delete per_cu->cu;
2187 *last_chain = next_cu;
2188 per_cu = next_cu;
2189 }
2190 }
2191
2192 /* A helper class that calls free_cached_comp_units on
2193 destruction. */
2194
2195 class free_cached_comp_units
2196 {
2197 public:
2198
2199 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2200 : m_per_objfile (per_objfile)
2201 {
2202 }
2203
2204 ~free_cached_comp_units ()
2205 {
2206 m_per_objfile->free_cached_comp_units ();
2207 }
2208
2209 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2210
2211 private:
2212
2213 dwarf2_per_objfile *m_per_objfile;
2214 };
2215
2216 /* Try to locate the sections we need for DWARF 2 debugging
2217 information and return true if we have enough to do something.
2218 NAMES points to the dwarf2 section names, or is NULL if the standard
2219 ELF names are used. CAN_COPY is true for formats where symbol
2220 interposition is possible and so symbol values must follow copy
2221 relocation rules. */
2222
2223 int
2224 dwarf2_has_info (struct objfile *objfile,
2225 const struct dwarf2_debug_sections *names,
2226 bool can_copy)
2227 {
2228 if (objfile->flags & OBJF_READNEVER)
2229 return 0;
2230
2231 struct dwarf2_per_objfile *dwarf2_per_objfile
2232 = get_dwarf2_per_objfile (objfile);
2233
2234 if (dwarf2_per_objfile == NULL)
2235 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2236 names,
2237 can_copy);
2238
2239 return (!dwarf2_per_objfile->info.is_virtual
2240 && dwarf2_per_objfile->info.s.section != NULL
2241 && !dwarf2_per_objfile->abbrev.is_virtual
2242 && dwarf2_per_objfile->abbrev.s.section != NULL);
2243 }
2244
2245 /* Return the containing section of virtual section SECTION. */
2246
2247 static struct dwarf2_section_info *
2248 get_containing_section (const struct dwarf2_section_info *section)
2249 {
2250 gdb_assert (section->is_virtual);
2251 return section->s.containing_section;
2252 }
2253
2254 /* Return the bfd owner of SECTION. */
2255
2256 static struct bfd *
2257 get_section_bfd_owner (const struct dwarf2_section_info *section)
2258 {
2259 if (section->is_virtual)
2260 {
2261 section = get_containing_section (section);
2262 gdb_assert (!section->is_virtual);
2263 }
2264 return section->s.section->owner;
2265 }
2266
2267 /* Return the bfd section of SECTION.
2268 Returns NULL if the section is not present. */
2269
2270 static asection *
2271 get_section_bfd_section (const struct dwarf2_section_info *section)
2272 {
2273 if (section->is_virtual)
2274 {
2275 section = get_containing_section (section);
2276 gdb_assert (!section->is_virtual);
2277 }
2278 return section->s.section;
2279 }
2280
2281 /* Return the name of SECTION. */
2282
2283 static const char *
2284 get_section_name (const struct dwarf2_section_info *section)
2285 {
2286 asection *sectp = get_section_bfd_section (section);
2287
2288 gdb_assert (sectp != NULL);
2289 return bfd_section_name (sectp);
2290 }
2291
2292 /* Return the name of the file SECTION is in. */
2293
2294 static const char *
2295 get_section_file_name (const struct dwarf2_section_info *section)
2296 {
2297 bfd *abfd = get_section_bfd_owner (section);
2298
2299 return bfd_get_filename (abfd);
2300 }
2301
2302 /* Return the id of SECTION.
2303 Returns 0 if SECTION doesn't exist. */
2304
2305 static int
2306 get_section_id (const struct dwarf2_section_info *section)
2307 {
2308 asection *sectp = get_section_bfd_section (section);
2309
2310 if (sectp == NULL)
2311 return 0;
2312 return sectp->id;
2313 }
2314
2315 /* Return the flags of SECTION.
2316 SECTION (or containing section if this is a virtual section) must exist. */
2317
2318 static int
2319 get_section_flags (const struct dwarf2_section_info *section)
2320 {
2321 asection *sectp = get_section_bfd_section (section);
2322
2323 gdb_assert (sectp != NULL);
2324 return bfd_section_flags (sectp);
2325 }
2326
2327 /* When loading sections, we look either for uncompressed section or for
2328 compressed section names. */
2329
2330 static int
2331 section_is_p (const char *section_name,
2332 const struct dwarf2_section_names *names)
2333 {
2334 if (names->normal != NULL
2335 && strcmp (section_name, names->normal) == 0)
2336 return 1;
2337 if (names->compressed != NULL
2338 && strcmp (section_name, names->compressed) == 0)
2339 return 1;
2340 return 0;
2341 }
2342
2343 /* See declaration. */
2344
2345 void
2346 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2347 const dwarf2_debug_sections &names)
2348 {
2349 flagword aflag = bfd_section_flags (sectp);
2350
2351 if ((aflag & SEC_HAS_CONTENTS) == 0)
2352 {
2353 }
2354 else if (section_is_p (sectp->name, &names.info))
2355 {
2356 this->info.s.section = sectp;
2357 this->info.size = bfd_section_size (sectp);
2358 }
2359 else if (section_is_p (sectp->name, &names.abbrev))
2360 {
2361 this->abbrev.s.section = sectp;
2362 this->abbrev.size = bfd_section_size (sectp);
2363 }
2364 else if (section_is_p (sectp->name, &names.line))
2365 {
2366 this->line.s.section = sectp;
2367 this->line.size = bfd_section_size (sectp);
2368 }
2369 else if (section_is_p (sectp->name, &names.loc))
2370 {
2371 this->loc.s.section = sectp;
2372 this->loc.size = bfd_section_size (sectp);
2373 }
2374 else if (section_is_p (sectp->name, &names.loclists))
2375 {
2376 this->loclists.s.section = sectp;
2377 this->loclists.size = bfd_section_size (sectp);
2378 }
2379 else if (section_is_p (sectp->name, &names.macinfo))
2380 {
2381 this->macinfo.s.section = sectp;
2382 this->macinfo.size = bfd_section_size (sectp);
2383 }
2384 else if (section_is_p (sectp->name, &names.macro))
2385 {
2386 this->macro.s.section = sectp;
2387 this->macro.size = bfd_section_size (sectp);
2388 }
2389 else if (section_is_p (sectp->name, &names.str))
2390 {
2391 this->str.s.section = sectp;
2392 this->str.size = bfd_section_size (sectp);
2393 }
2394 else if (section_is_p (sectp->name, &names.line_str))
2395 {
2396 this->line_str.s.section = sectp;
2397 this->line_str.size = bfd_section_size (sectp);
2398 }
2399 else if (section_is_p (sectp->name, &names.addr))
2400 {
2401 this->addr.s.section = sectp;
2402 this->addr.size = bfd_section_size (sectp);
2403 }
2404 else if (section_is_p (sectp->name, &names.frame))
2405 {
2406 this->frame.s.section = sectp;
2407 this->frame.size = bfd_section_size (sectp);
2408 }
2409 else if (section_is_p (sectp->name, &names.eh_frame))
2410 {
2411 this->eh_frame.s.section = sectp;
2412 this->eh_frame.size = bfd_section_size (sectp);
2413 }
2414 else if (section_is_p (sectp->name, &names.ranges))
2415 {
2416 this->ranges.s.section = sectp;
2417 this->ranges.size = bfd_section_size (sectp);
2418 }
2419 else if (section_is_p (sectp->name, &names.rnglists))
2420 {
2421 this->rnglists.s.section = sectp;
2422 this->rnglists.size = bfd_section_size (sectp);
2423 }
2424 else if (section_is_p (sectp->name, &names.types))
2425 {
2426 struct dwarf2_section_info type_section;
2427
2428 memset (&type_section, 0, sizeof (type_section));
2429 type_section.s.section = sectp;
2430 type_section.size = bfd_section_size (sectp);
2431
2432 this->types.push_back (type_section);
2433 }
2434 else if (section_is_p (sectp->name, &names.gdb_index))
2435 {
2436 this->gdb_index.s.section = sectp;
2437 this->gdb_index.size = bfd_section_size (sectp);
2438 }
2439 else if (section_is_p (sectp->name, &names.debug_names))
2440 {
2441 this->debug_names.s.section = sectp;
2442 this->debug_names.size = bfd_section_size (sectp);
2443 }
2444 else if (section_is_p (sectp->name, &names.debug_aranges))
2445 {
2446 this->debug_aranges.s.section = sectp;
2447 this->debug_aranges.size = bfd_section_size (sectp);
2448 }
2449
2450 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2451 && bfd_section_vma (sectp) == 0)
2452 this->has_section_at_zero = true;
2453 }
2454
2455 /* A helper function that decides whether a section is empty,
2456 or not present. */
2457
2458 static int
2459 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2460 {
2461 if (section->is_virtual)
2462 return section->size == 0;
2463 return section->s.section == NULL || section->size == 0;
2464 }
2465
2466 /* See dwarf2read.h. */
2467
2468 void
2469 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2470 {
2471 asection *sectp;
2472 bfd *abfd;
2473 gdb_byte *buf, *retbuf;
2474
2475 if (info->readin)
2476 return;
2477 info->buffer = NULL;
2478 info->readin = true;
2479
2480 if (dwarf2_section_empty_p (info))
2481 return;
2482
2483 sectp = get_section_bfd_section (info);
2484
2485 /* If this is a virtual section we need to read in the real one first. */
2486 if (info->is_virtual)
2487 {
2488 struct dwarf2_section_info *containing_section =
2489 get_containing_section (info);
2490
2491 gdb_assert (sectp != NULL);
2492 if ((sectp->flags & SEC_RELOC) != 0)
2493 {
2494 error (_("Dwarf Error: DWP format V2 with relocations is not"
2495 " supported in section %s [in module %s]"),
2496 get_section_name (info), get_section_file_name (info));
2497 }
2498 dwarf2_read_section (objfile, containing_section);
2499 /* Other code should have already caught virtual sections that don't
2500 fit. */
2501 gdb_assert (info->virtual_offset + info->size
2502 <= containing_section->size);
2503 /* If the real section is empty or there was a problem reading the
2504 section we shouldn't get here. */
2505 gdb_assert (containing_section->buffer != NULL);
2506 info->buffer = containing_section->buffer + info->virtual_offset;
2507 return;
2508 }
2509
2510 /* If the section has relocations, we must read it ourselves.
2511 Otherwise we attach it to the BFD. */
2512 if ((sectp->flags & SEC_RELOC) == 0)
2513 {
2514 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2515 return;
2516 }
2517
2518 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2519 info->buffer = buf;
2520
2521 /* When debugging .o files, we may need to apply relocations; see
2522 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2523 We never compress sections in .o files, so we only need to
2524 try this when the section is not compressed. */
2525 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2526 if (retbuf != NULL)
2527 {
2528 info->buffer = retbuf;
2529 return;
2530 }
2531
2532 abfd = get_section_bfd_owner (info);
2533 gdb_assert (abfd != NULL);
2534
2535 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2536 || bfd_bread (buf, info->size, abfd) != info->size)
2537 {
2538 error (_("Dwarf Error: Can't read DWARF data"
2539 " in section %s [in module %s]"),
2540 bfd_section_name (sectp), bfd_get_filename (abfd));
2541 }
2542 }
2543
2544 /* A helper function that returns the size of a section in a safe way.
2545 If you are positive that the section has been read before using the
2546 size, then it is safe to refer to the dwarf2_section_info object's
2547 "size" field directly. In other cases, you must call this
2548 function, because for compressed sections the size field is not set
2549 correctly until the section has been read. */
2550
2551 static bfd_size_type
2552 dwarf2_section_size (struct objfile *objfile,
2553 struct dwarf2_section_info *info)
2554 {
2555 if (!info->readin)
2556 dwarf2_read_section (objfile, info);
2557 return info->size;
2558 }
2559
2560 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2561 SECTION_NAME. */
2562
2563 void
2564 dwarf2_get_section_info (struct objfile *objfile,
2565 enum dwarf2_section_enum sect,
2566 asection **sectp, const gdb_byte **bufp,
2567 bfd_size_type *sizep)
2568 {
2569 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2570 struct dwarf2_section_info *info;
2571
2572 /* We may see an objfile without any DWARF, in which case we just
2573 return nothing. */
2574 if (data == NULL)
2575 {
2576 *sectp = NULL;
2577 *bufp = NULL;
2578 *sizep = 0;
2579 return;
2580 }
2581 switch (sect)
2582 {
2583 case DWARF2_DEBUG_FRAME:
2584 info = &data->frame;
2585 break;
2586 case DWARF2_EH_FRAME:
2587 info = &data->eh_frame;
2588 break;
2589 default:
2590 gdb_assert_not_reached ("unexpected section");
2591 }
2592
2593 dwarf2_read_section (objfile, info);
2594
2595 *sectp = get_section_bfd_section (info);
2596 *bufp = info->buffer;
2597 *sizep = info->size;
2598 }
2599
2600 /* A helper function to find the sections for a .dwz file. */
2601
2602 static void
2603 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2604 {
2605 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2606
2607 /* Note that we only support the standard ELF names, because .dwz
2608 is ELF-only (at the time of writing). */
2609 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2610 {
2611 dwz_file->abbrev.s.section = sectp;
2612 dwz_file->abbrev.size = bfd_section_size (sectp);
2613 }
2614 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2615 {
2616 dwz_file->info.s.section = sectp;
2617 dwz_file->info.size = bfd_section_size (sectp);
2618 }
2619 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2620 {
2621 dwz_file->str.s.section = sectp;
2622 dwz_file->str.size = bfd_section_size (sectp);
2623 }
2624 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2625 {
2626 dwz_file->line.s.section = sectp;
2627 dwz_file->line.size = bfd_section_size (sectp);
2628 }
2629 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2630 {
2631 dwz_file->macro.s.section = sectp;
2632 dwz_file->macro.size = bfd_section_size (sectp);
2633 }
2634 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2635 {
2636 dwz_file->gdb_index.s.section = sectp;
2637 dwz_file->gdb_index.size = bfd_section_size (sectp);
2638 }
2639 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2640 {
2641 dwz_file->debug_names.s.section = sectp;
2642 dwz_file->debug_names.size = bfd_section_size (sectp);
2643 }
2644 }
2645
2646 /* See dwarf2read.h. */
2647
2648 struct dwz_file *
2649 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2650 {
2651 const char *filename;
2652 bfd_size_type buildid_len_arg;
2653 size_t buildid_len;
2654 bfd_byte *buildid;
2655
2656 if (dwarf2_per_objfile->dwz_file != NULL)
2657 return dwarf2_per_objfile->dwz_file.get ();
2658
2659 bfd_set_error (bfd_error_no_error);
2660 gdb::unique_xmalloc_ptr<char> data
2661 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2662 &buildid_len_arg, &buildid));
2663 if (data == NULL)
2664 {
2665 if (bfd_get_error () == bfd_error_no_error)
2666 return NULL;
2667 error (_("could not read '.gnu_debugaltlink' section: %s"),
2668 bfd_errmsg (bfd_get_error ()));
2669 }
2670
2671 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2672
2673 buildid_len = (size_t) buildid_len_arg;
2674
2675 filename = data.get ();
2676
2677 std::string abs_storage;
2678 if (!IS_ABSOLUTE_PATH (filename))
2679 {
2680 gdb::unique_xmalloc_ptr<char> abs
2681 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2682
2683 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2684 filename = abs_storage.c_str ();
2685 }
2686
2687 /* First try the file name given in the section. If that doesn't
2688 work, try to use the build-id instead. */
2689 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2690 if (dwz_bfd != NULL)
2691 {
2692 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2693 dwz_bfd.reset (nullptr);
2694 }
2695
2696 if (dwz_bfd == NULL)
2697 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2698
2699 if (dwz_bfd == NULL)
2700 error (_("could not find '.gnu_debugaltlink' file for %s"),
2701 objfile_name (dwarf2_per_objfile->objfile));
2702
2703 std::unique_ptr<struct dwz_file> result
2704 (new struct dwz_file (std::move (dwz_bfd)));
2705
2706 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2707 result.get ());
2708
2709 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2710 result->dwz_bfd.get ());
2711 dwarf2_per_objfile->dwz_file = std::move (result);
2712 return dwarf2_per_objfile->dwz_file.get ();
2713 }
2714 \f
2715 /* DWARF quick_symbols_functions support. */
2716
2717 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2718 unique line tables, so we maintain a separate table of all .debug_line
2719 derived entries to support the sharing.
2720 All the quick functions need is the list of file names. We discard the
2721 line_header when we're done and don't need to record it here. */
2722 struct quick_file_names
2723 {
2724 /* The data used to construct the hash key. */
2725 struct stmt_list_hash hash;
2726
2727 /* The number of entries in file_names, real_names. */
2728 unsigned int num_file_names;
2729
2730 /* The file names from the line table, after being run through
2731 file_full_name. */
2732 const char **file_names;
2733
2734 /* The file names from the line table after being run through
2735 gdb_realpath. These are computed lazily. */
2736 const char **real_names;
2737 };
2738
2739 /* When using the index (and thus not using psymtabs), each CU has an
2740 object of this type. This is used to hold information needed by
2741 the various "quick" methods. */
2742 struct dwarf2_per_cu_quick_data
2743 {
2744 /* The file table. This can be NULL if there was no file table
2745 or it's currently not read in.
2746 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2747 struct quick_file_names *file_names;
2748
2749 /* The corresponding symbol table. This is NULL if symbols for this
2750 CU have not yet been read. */
2751 struct compunit_symtab *compunit_symtab;
2752
2753 /* A temporary mark bit used when iterating over all CUs in
2754 expand_symtabs_matching. */
2755 unsigned int mark : 1;
2756
2757 /* True if we've tried to read the file table and found there isn't one.
2758 There will be no point in trying to read it again next time. */
2759 unsigned int no_file_data : 1;
2760 };
2761
2762 /* Utility hash function for a stmt_list_hash. */
2763
2764 static hashval_t
2765 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2766 {
2767 hashval_t v = 0;
2768
2769 if (stmt_list_hash->dwo_unit != NULL)
2770 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2771 v += to_underlying (stmt_list_hash->line_sect_off);
2772 return v;
2773 }
2774
2775 /* Utility equality function for a stmt_list_hash. */
2776
2777 static int
2778 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2779 const struct stmt_list_hash *rhs)
2780 {
2781 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2782 return 0;
2783 if (lhs->dwo_unit != NULL
2784 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2785 return 0;
2786
2787 return lhs->line_sect_off == rhs->line_sect_off;
2788 }
2789
2790 /* Hash function for a quick_file_names. */
2791
2792 static hashval_t
2793 hash_file_name_entry (const void *e)
2794 {
2795 const struct quick_file_names *file_data
2796 = (const struct quick_file_names *) e;
2797
2798 return hash_stmt_list_entry (&file_data->hash);
2799 }
2800
2801 /* Equality function for a quick_file_names. */
2802
2803 static int
2804 eq_file_name_entry (const void *a, const void *b)
2805 {
2806 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2807 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2808
2809 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2810 }
2811
2812 /* Delete function for a quick_file_names. */
2813
2814 static void
2815 delete_file_name_entry (void *e)
2816 {
2817 struct quick_file_names *file_data = (struct quick_file_names *) e;
2818 int i;
2819
2820 for (i = 0; i < file_data->num_file_names; ++i)
2821 {
2822 xfree ((void*) file_data->file_names[i]);
2823 if (file_data->real_names)
2824 xfree ((void*) file_data->real_names[i]);
2825 }
2826
2827 /* The space for the struct itself lives on objfile_obstack,
2828 so we don't free it here. */
2829 }
2830
2831 /* Create a quick_file_names hash table. */
2832
2833 static htab_t
2834 create_quick_file_names_table (unsigned int nr_initial_entries)
2835 {
2836 return htab_create_alloc (nr_initial_entries,
2837 hash_file_name_entry, eq_file_name_entry,
2838 delete_file_name_entry, xcalloc, xfree);
2839 }
2840
2841 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2842 have to be created afterwards. You should call age_cached_comp_units after
2843 processing PER_CU->CU. dw2_setup must have been already called. */
2844
2845 static void
2846 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2847 {
2848 if (per_cu->is_debug_types)
2849 load_full_type_unit (per_cu);
2850 else
2851 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2852
2853 if (per_cu->cu == NULL)
2854 return; /* Dummy CU. */
2855
2856 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2857 }
2858
2859 /* Read in the symbols for PER_CU. */
2860
2861 static void
2862 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2863 {
2864 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2865
2866 /* Skip type_unit_groups, reading the type units they contain
2867 is handled elsewhere. */
2868 if (IS_TYPE_UNIT_GROUP (per_cu))
2869 return;
2870
2871 /* The destructor of dwarf2_queue_guard frees any entries left on
2872 the queue. After this point we're guaranteed to leave this function
2873 with the dwarf queue empty. */
2874 dwarf2_queue_guard q_guard;
2875
2876 if (dwarf2_per_objfile->using_index
2877 ? per_cu->v.quick->compunit_symtab == NULL
2878 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2879 {
2880 queue_comp_unit (per_cu, language_minimal);
2881 load_cu (per_cu, skip_partial);
2882
2883 /* If we just loaded a CU from a DWO, and we're working with an index
2884 that may badly handle TUs, load all the TUs in that DWO as well.
2885 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2886 if (!per_cu->is_debug_types
2887 && per_cu->cu != NULL
2888 && per_cu->cu->dwo_unit != NULL
2889 && dwarf2_per_objfile->index_table != NULL
2890 && dwarf2_per_objfile->index_table->version <= 7
2891 /* DWP files aren't supported yet. */
2892 && get_dwp_file (dwarf2_per_objfile) == NULL)
2893 queue_and_load_all_dwo_tus (per_cu);
2894 }
2895
2896 process_queue (dwarf2_per_objfile);
2897
2898 /* Age the cache, releasing compilation units that have not
2899 been used recently. */
2900 age_cached_comp_units (dwarf2_per_objfile);
2901 }
2902
2903 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2904 the objfile from which this CU came. Returns the resulting symbol
2905 table. */
2906
2907 static struct compunit_symtab *
2908 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2909 {
2910 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2911
2912 gdb_assert (dwarf2_per_objfile->using_index);
2913 if (!per_cu->v.quick->compunit_symtab)
2914 {
2915 free_cached_comp_units freer (dwarf2_per_objfile);
2916 scoped_restore decrementer = increment_reading_symtab ();
2917 dw2_do_instantiate_symtab (per_cu, skip_partial);
2918 process_cu_includes (dwarf2_per_objfile);
2919 }
2920
2921 return per_cu->v.quick->compunit_symtab;
2922 }
2923
2924 /* See declaration. */
2925
2926 dwarf2_per_cu_data *
2927 dwarf2_per_objfile::get_cutu (int index)
2928 {
2929 if (index >= this->all_comp_units.size ())
2930 {
2931 index -= this->all_comp_units.size ();
2932 gdb_assert (index < this->all_type_units.size ());
2933 return &this->all_type_units[index]->per_cu;
2934 }
2935
2936 return this->all_comp_units[index];
2937 }
2938
2939 /* See declaration. */
2940
2941 dwarf2_per_cu_data *
2942 dwarf2_per_objfile::get_cu (int index)
2943 {
2944 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2945
2946 return this->all_comp_units[index];
2947 }
2948
2949 /* See declaration. */
2950
2951 signatured_type *
2952 dwarf2_per_objfile::get_tu (int index)
2953 {
2954 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2955
2956 return this->all_type_units[index];
2957 }
2958
2959 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2960 objfile_obstack, and constructed with the specified field
2961 values. */
2962
2963 static dwarf2_per_cu_data *
2964 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2965 struct dwarf2_section_info *section,
2966 int is_dwz,
2967 sect_offset sect_off, ULONGEST length)
2968 {
2969 struct objfile *objfile = dwarf2_per_objfile->objfile;
2970 dwarf2_per_cu_data *the_cu
2971 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2972 struct dwarf2_per_cu_data);
2973 the_cu->sect_off = sect_off;
2974 the_cu->length = length;
2975 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2976 the_cu->section = section;
2977 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2978 struct dwarf2_per_cu_quick_data);
2979 the_cu->is_dwz = is_dwz;
2980 return the_cu;
2981 }
2982
2983 /* A helper for create_cus_from_index that handles a given list of
2984 CUs. */
2985
2986 static void
2987 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2988 const gdb_byte *cu_list, offset_type n_elements,
2989 struct dwarf2_section_info *section,
2990 int is_dwz)
2991 {
2992 for (offset_type i = 0; i < n_elements; i += 2)
2993 {
2994 gdb_static_assert (sizeof (ULONGEST) >= 8);
2995
2996 sect_offset sect_off
2997 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2998 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2999 cu_list += 2 * 8;
3000
3001 dwarf2_per_cu_data *per_cu
3002 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3003 sect_off, length);
3004 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
3005 }
3006 }
3007
3008 /* Read the CU list from the mapped index, and use it to create all
3009 the CU objects for this objfile. */
3010
3011 static void
3012 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3013 const gdb_byte *cu_list, offset_type cu_list_elements,
3014 const gdb_byte *dwz_list, offset_type dwz_elements)
3015 {
3016 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3017 dwarf2_per_objfile->all_comp_units.reserve
3018 ((cu_list_elements + dwz_elements) / 2);
3019
3020 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3021 &dwarf2_per_objfile->info, 0);
3022
3023 if (dwz_elements == 0)
3024 return;
3025
3026 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3027 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3028 &dwz->info, 1);
3029 }
3030
3031 /* Create the signatured type hash table from the index. */
3032
3033 static void
3034 create_signatured_type_table_from_index
3035 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3036 struct dwarf2_section_info *section,
3037 const gdb_byte *bytes,
3038 offset_type elements)
3039 {
3040 struct objfile *objfile = dwarf2_per_objfile->objfile;
3041
3042 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3043 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
3044
3045 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3046
3047 for (offset_type i = 0; i < elements; i += 3)
3048 {
3049 struct signatured_type *sig_type;
3050 ULONGEST signature;
3051 void **slot;
3052 cu_offset type_offset_in_tu;
3053
3054 gdb_static_assert (sizeof (ULONGEST) >= 8);
3055 sect_offset sect_off
3056 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3057 type_offset_in_tu
3058 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3059 BFD_ENDIAN_LITTLE);
3060 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3061 bytes += 3 * 8;
3062
3063 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3064 struct signatured_type);
3065 sig_type->signature = signature;
3066 sig_type->type_offset_in_tu = type_offset_in_tu;
3067 sig_type->per_cu.is_debug_types = 1;
3068 sig_type->per_cu.section = section;
3069 sig_type->per_cu.sect_off = sect_off;
3070 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3071 sig_type->per_cu.v.quick
3072 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3073 struct dwarf2_per_cu_quick_data);
3074
3075 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3076 *slot = sig_type;
3077
3078 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3079 }
3080
3081 dwarf2_per_objfile->signatured_types = sig_types_hash;
3082 }
3083
3084 /* Create the signatured type hash table from .debug_names. */
3085
3086 static void
3087 create_signatured_type_table_from_debug_names
3088 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3089 const mapped_debug_names &map,
3090 struct dwarf2_section_info *section,
3091 struct dwarf2_section_info *abbrev_section)
3092 {
3093 struct objfile *objfile = dwarf2_per_objfile->objfile;
3094
3095 dwarf2_read_section (objfile, section);
3096 dwarf2_read_section (objfile, abbrev_section);
3097
3098 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3099 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
3100
3101 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3102
3103 for (uint32_t i = 0; i < map.tu_count; ++i)
3104 {
3105 struct signatured_type *sig_type;
3106 void **slot;
3107
3108 sect_offset sect_off
3109 = (sect_offset) (extract_unsigned_integer
3110 (map.tu_table_reordered + i * map.offset_size,
3111 map.offset_size,
3112 map.dwarf5_byte_order));
3113
3114 comp_unit_head cu_header;
3115 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3116 abbrev_section,
3117 section->buffer + to_underlying (sect_off),
3118 rcuh_kind::TYPE);
3119
3120 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3121 struct signatured_type);
3122 sig_type->signature = cu_header.signature;
3123 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3124 sig_type->per_cu.is_debug_types = 1;
3125 sig_type->per_cu.section = section;
3126 sig_type->per_cu.sect_off = sect_off;
3127 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3128 sig_type->per_cu.v.quick
3129 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3130 struct dwarf2_per_cu_quick_data);
3131
3132 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3133 *slot = sig_type;
3134
3135 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3136 }
3137
3138 dwarf2_per_objfile->signatured_types = sig_types_hash;
3139 }
3140
3141 /* Read the address map data from the mapped index, and use it to
3142 populate the objfile's psymtabs_addrmap. */
3143
3144 static void
3145 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3146 struct mapped_index *index)
3147 {
3148 struct objfile *objfile = dwarf2_per_objfile->objfile;
3149 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3150 const gdb_byte *iter, *end;
3151 struct addrmap *mutable_map;
3152 CORE_ADDR baseaddr;
3153
3154 auto_obstack temp_obstack;
3155
3156 mutable_map = addrmap_create_mutable (&temp_obstack);
3157
3158 iter = index->address_table.data ();
3159 end = iter + index->address_table.size ();
3160
3161 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3162
3163 while (iter < end)
3164 {
3165 ULONGEST hi, lo, cu_index;
3166 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3167 iter += 8;
3168 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3169 iter += 8;
3170 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3171 iter += 4;
3172
3173 if (lo > hi)
3174 {
3175 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
3176 hex_string (lo), hex_string (hi));
3177 continue;
3178 }
3179
3180 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
3181 {
3182 complaint (_(".gdb_index address table has invalid CU number %u"),
3183 (unsigned) cu_index);
3184 continue;
3185 }
3186
3187 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3188 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
3189 addrmap_set_empty (mutable_map, lo, hi - 1,
3190 dwarf2_per_objfile->get_cu (cu_index));
3191 }
3192
3193 objfile->partial_symtabs->psymtabs_addrmap
3194 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3195 }
3196
3197 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3198 populate the objfile's psymtabs_addrmap. */
3199
3200 static void
3201 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3202 struct dwarf2_section_info *section)
3203 {
3204 struct objfile *objfile = dwarf2_per_objfile->objfile;
3205 bfd *abfd = objfile->obfd;
3206 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3207 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3208 SECT_OFF_TEXT (objfile));
3209
3210 auto_obstack temp_obstack;
3211 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3212
3213 std::unordered_map<sect_offset,
3214 dwarf2_per_cu_data *,
3215 gdb::hash_enum<sect_offset>>
3216 debug_info_offset_to_per_cu;
3217 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3218 {
3219 const auto insertpair
3220 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3221 if (!insertpair.second)
3222 {
3223 warning (_("Section .debug_aranges in %s has duplicate "
3224 "debug_info_offset %s, ignoring .debug_aranges."),
3225 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3226 return;
3227 }
3228 }
3229
3230 dwarf2_read_section (objfile, section);
3231
3232 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3233
3234 const gdb_byte *addr = section->buffer;
3235
3236 while (addr < section->buffer + section->size)
3237 {
3238 const gdb_byte *const entry_addr = addr;
3239 unsigned int bytes_read;
3240
3241 const LONGEST entry_length = read_initial_length (abfd, addr,
3242 &bytes_read);
3243 addr += bytes_read;
3244
3245 const gdb_byte *const entry_end = addr + entry_length;
3246 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3247 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3248 if (addr + entry_length > section->buffer + section->size)
3249 {
3250 warning (_("Section .debug_aranges in %s entry at offset %s "
3251 "length %s exceeds section length %s, "
3252 "ignoring .debug_aranges."),
3253 objfile_name (objfile),
3254 plongest (entry_addr - section->buffer),
3255 plongest (bytes_read + entry_length),
3256 pulongest (section->size));
3257 return;
3258 }
3259
3260 /* The version number. */
3261 const uint16_t version = read_2_bytes (abfd, addr);
3262 addr += 2;
3263 if (version != 2)
3264 {
3265 warning (_("Section .debug_aranges in %s entry at offset %s "
3266 "has unsupported version %d, ignoring .debug_aranges."),
3267 objfile_name (objfile),
3268 plongest (entry_addr - section->buffer), version);
3269 return;
3270 }
3271
3272 const uint64_t debug_info_offset
3273 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3274 addr += offset_size;
3275 const auto per_cu_it
3276 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3277 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3278 {
3279 warning (_("Section .debug_aranges in %s entry at offset %s "
3280 "debug_info_offset %s does not exists, "
3281 "ignoring .debug_aranges."),
3282 objfile_name (objfile),
3283 plongest (entry_addr - section->buffer),
3284 pulongest (debug_info_offset));
3285 return;
3286 }
3287 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3288
3289 const uint8_t address_size = *addr++;
3290 if (address_size < 1 || address_size > 8)
3291 {
3292 warning (_("Section .debug_aranges in %s entry at offset %s "
3293 "address_size %u is invalid, ignoring .debug_aranges."),
3294 objfile_name (objfile),
3295 plongest (entry_addr - section->buffer), address_size);
3296 return;
3297 }
3298
3299 const uint8_t segment_selector_size = *addr++;
3300 if (segment_selector_size != 0)
3301 {
3302 warning (_("Section .debug_aranges in %s entry at offset %s "
3303 "segment_selector_size %u is not supported, "
3304 "ignoring .debug_aranges."),
3305 objfile_name (objfile),
3306 plongest (entry_addr - section->buffer),
3307 segment_selector_size);
3308 return;
3309 }
3310
3311 /* Must pad to an alignment boundary that is twice the address
3312 size. It is undocumented by the DWARF standard but GCC does
3313 use it. */
3314 for (size_t padding = ((-(addr - section->buffer))
3315 & (2 * address_size - 1));
3316 padding > 0; padding--)
3317 if (*addr++ != 0)
3318 {
3319 warning (_("Section .debug_aranges in %s entry at offset %s "
3320 "padding is not zero, ignoring .debug_aranges."),
3321 objfile_name (objfile),
3322 plongest (entry_addr - section->buffer));
3323 return;
3324 }
3325
3326 for (;;)
3327 {
3328 if (addr + 2 * address_size > entry_end)
3329 {
3330 warning (_("Section .debug_aranges in %s entry at offset %s "
3331 "address list is not properly terminated, "
3332 "ignoring .debug_aranges."),
3333 objfile_name (objfile),
3334 plongest (entry_addr - section->buffer));
3335 return;
3336 }
3337 ULONGEST start = extract_unsigned_integer (addr, address_size,
3338 dwarf5_byte_order);
3339 addr += address_size;
3340 ULONGEST length = extract_unsigned_integer (addr, address_size,
3341 dwarf5_byte_order);
3342 addr += address_size;
3343 if (start == 0 && length == 0)
3344 break;
3345 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3346 {
3347 /* Symbol was eliminated due to a COMDAT group. */
3348 continue;
3349 }
3350 ULONGEST end = start + length;
3351 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3352 - baseaddr);
3353 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3354 - baseaddr);
3355 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3356 }
3357 }
3358
3359 objfile->partial_symtabs->psymtabs_addrmap
3360 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3361 }
3362
3363 /* Find a slot in the mapped index INDEX for the object named NAME.
3364 If NAME is found, set *VEC_OUT to point to the CU vector in the
3365 constant pool and return true. If NAME cannot be found, return
3366 false. */
3367
3368 static bool
3369 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3370 offset_type **vec_out)
3371 {
3372 offset_type hash;
3373 offset_type slot, step;
3374 int (*cmp) (const char *, const char *);
3375
3376 gdb::unique_xmalloc_ptr<char> without_params;
3377 if (current_language->la_language == language_cplus
3378 || current_language->la_language == language_fortran
3379 || current_language->la_language == language_d)
3380 {
3381 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3382 not contain any. */
3383
3384 if (strchr (name, '(') != NULL)
3385 {
3386 without_params = cp_remove_params (name);
3387
3388 if (without_params != NULL)
3389 name = without_params.get ();
3390 }
3391 }
3392
3393 /* Index version 4 did not support case insensitive searches. But the
3394 indices for case insensitive languages are built in lowercase, therefore
3395 simulate our NAME being searched is also lowercased. */
3396 hash = mapped_index_string_hash ((index->version == 4
3397 && case_sensitivity == case_sensitive_off
3398 ? 5 : index->version),
3399 name);
3400
3401 slot = hash & (index->symbol_table.size () - 1);
3402 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3403 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3404
3405 for (;;)
3406 {
3407 const char *str;
3408
3409 const auto &bucket = index->symbol_table[slot];
3410 if (bucket.name == 0 && bucket.vec == 0)
3411 return false;
3412
3413 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3414 if (!cmp (name, str))
3415 {
3416 *vec_out = (offset_type *) (index->constant_pool
3417 + MAYBE_SWAP (bucket.vec));
3418 return true;
3419 }
3420
3421 slot = (slot + step) & (index->symbol_table.size () - 1);
3422 }
3423 }
3424
3425 /* A helper function that reads the .gdb_index from BUFFER and fills
3426 in MAP. FILENAME is the name of the file containing the data;
3427 it is used for error reporting. DEPRECATED_OK is true if it is
3428 ok to use deprecated sections.
3429
3430 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3431 out parameters that are filled in with information about the CU and
3432 TU lists in the section.
3433
3434 Returns true if all went well, false otherwise. */
3435
3436 static bool
3437 read_gdb_index_from_buffer (struct objfile *objfile,
3438 const char *filename,
3439 bool deprecated_ok,
3440 gdb::array_view<const gdb_byte> buffer,
3441 struct mapped_index *map,
3442 const gdb_byte **cu_list,
3443 offset_type *cu_list_elements,
3444 const gdb_byte **types_list,
3445 offset_type *types_list_elements)
3446 {
3447 const gdb_byte *addr = &buffer[0];
3448
3449 /* Version check. */
3450 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3451 /* Versions earlier than 3 emitted every copy of a psymbol. This
3452 causes the index to behave very poorly for certain requests. Version 3
3453 contained incomplete addrmap. So, it seems better to just ignore such
3454 indices. */
3455 if (version < 4)
3456 {
3457 static int warning_printed = 0;
3458 if (!warning_printed)
3459 {
3460 warning (_("Skipping obsolete .gdb_index section in %s."),
3461 filename);
3462 warning_printed = 1;
3463 }
3464 return 0;
3465 }
3466 /* Index version 4 uses a different hash function than index version
3467 5 and later.
3468
3469 Versions earlier than 6 did not emit psymbols for inlined
3470 functions. Using these files will cause GDB not to be able to
3471 set breakpoints on inlined functions by name, so we ignore these
3472 indices unless the user has done
3473 "set use-deprecated-index-sections on". */
3474 if (version < 6 && !deprecated_ok)
3475 {
3476 static int warning_printed = 0;
3477 if (!warning_printed)
3478 {
3479 warning (_("\
3480 Skipping deprecated .gdb_index section in %s.\n\
3481 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3482 to use the section anyway."),
3483 filename);
3484 warning_printed = 1;
3485 }
3486 return 0;
3487 }
3488 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3489 of the TU (for symbols coming from TUs),
3490 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3491 Plus gold-generated indices can have duplicate entries for global symbols,
3492 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3493 These are just performance bugs, and we can't distinguish gdb-generated
3494 indices from gold-generated ones, so issue no warning here. */
3495
3496 /* Indexes with higher version than the one supported by GDB may be no
3497 longer backward compatible. */
3498 if (version > 8)
3499 return 0;
3500
3501 map->version = version;
3502
3503 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3504
3505 int i = 0;
3506 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3507 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3508 / 8);
3509 ++i;
3510
3511 *types_list = addr + MAYBE_SWAP (metadata[i]);
3512 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3513 - MAYBE_SWAP (metadata[i]))
3514 / 8);
3515 ++i;
3516
3517 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3518 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3519 map->address_table
3520 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3521 ++i;
3522
3523 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3524 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3525 map->symbol_table
3526 = gdb::array_view<mapped_index::symbol_table_slot>
3527 ((mapped_index::symbol_table_slot *) symbol_table,
3528 (mapped_index::symbol_table_slot *) symbol_table_end);
3529
3530 ++i;
3531 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3532
3533 return 1;
3534 }
3535
3536 /* Callback types for dwarf2_read_gdb_index. */
3537
3538 typedef gdb::function_view
3539 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3540 get_gdb_index_contents_ftype;
3541 typedef gdb::function_view
3542 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3543 get_gdb_index_contents_dwz_ftype;
3544
3545 /* Read .gdb_index. If everything went ok, initialize the "quick"
3546 elements of all the CUs and return 1. Otherwise, return 0. */
3547
3548 static int
3549 dwarf2_read_gdb_index
3550 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3551 get_gdb_index_contents_ftype get_gdb_index_contents,
3552 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3553 {
3554 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3555 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3556 struct dwz_file *dwz;
3557 struct objfile *objfile = dwarf2_per_objfile->objfile;
3558
3559 gdb::array_view<const gdb_byte> main_index_contents
3560 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3561
3562 if (main_index_contents.empty ())
3563 return 0;
3564
3565 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3566 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3567 use_deprecated_index_sections,
3568 main_index_contents, map.get (), &cu_list,
3569 &cu_list_elements, &types_list,
3570 &types_list_elements))
3571 return 0;
3572
3573 /* Don't use the index if it's empty. */
3574 if (map->symbol_table.empty ())
3575 return 0;
3576
3577 /* If there is a .dwz file, read it so we can get its CU list as
3578 well. */
3579 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3580 if (dwz != NULL)
3581 {
3582 struct mapped_index dwz_map;
3583 const gdb_byte *dwz_types_ignore;
3584 offset_type dwz_types_elements_ignore;
3585
3586 gdb::array_view<const gdb_byte> dwz_index_content
3587 = get_gdb_index_contents_dwz (objfile, dwz);
3588
3589 if (dwz_index_content.empty ())
3590 return 0;
3591
3592 if (!read_gdb_index_from_buffer (objfile,
3593 bfd_get_filename (dwz->dwz_bfd.get ()),
3594 1, dwz_index_content, &dwz_map,
3595 &dwz_list, &dwz_list_elements,
3596 &dwz_types_ignore,
3597 &dwz_types_elements_ignore))
3598 {
3599 warning (_("could not read '.gdb_index' section from %s; skipping"),
3600 bfd_get_filename (dwz->dwz_bfd.get ()));
3601 return 0;
3602 }
3603 }
3604
3605 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3606 dwz_list, dwz_list_elements);
3607
3608 if (types_list_elements)
3609 {
3610 /* We can only handle a single .debug_types when we have an
3611 index. */
3612 if (dwarf2_per_objfile->types.size () != 1)
3613 return 0;
3614
3615 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3616
3617 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3618 types_list, types_list_elements);
3619 }
3620
3621 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3622
3623 dwarf2_per_objfile->index_table = std::move (map);
3624 dwarf2_per_objfile->using_index = 1;
3625 dwarf2_per_objfile->quick_file_names_table =
3626 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3627
3628 return 1;
3629 }
3630
3631 /* die_reader_func for dw2_get_file_names. */
3632
3633 static void
3634 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3635 const gdb_byte *info_ptr,
3636 struct die_info *comp_unit_die,
3637 int has_children,
3638 void *data)
3639 {
3640 struct dwarf2_cu *cu = reader->cu;
3641 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3642 struct dwarf2_per_objfile *dwarf2_per_objfile
3643 = cu->per_cu->dwarf2_per_objfile;
3644 struct objfile *objfile = dwarf2_per_objfile->objfile;
3645 struct dwarf2_per_cu_data *lh_cu;
3646 struct attribute *attr;
3647 int i;
3648 void **slot;
3649 struct quick_file_names *qfn;
3650
3651 gdb_assert (! this_cu->is_debug_types);
3652
3653 /* Our callers never want to match partial units -- instead they
3654 will match the enclosing full CU. */
3655 if (comp_unit_die->tag == DW_TAG_partial_unit)
3656 {
3657 this_cu->v.quick->no_file_data = 1;
3658 return;
3659 }
3660
3661 lh_cu = this_cu;
3662 slot = NULL;
3663
3664 line_header_up lh;
3665 sect_offset line_offset {};
3666
3667 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3668 if (attr)
3669 {
3670 struct quick_file_names find_entry;
3671
3672 line_offset = (sect_offset) DW_UNSND (attr);
3673
3674 /* We may have already read in this line header (TU line header sharing).
3675 If we have we're done. */
3676 find_entry.hash.dwo_unit = cu->dwo_unit;
3677 find_entry.hash.line_sect_off = line_offset;
3678 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3679 &find_entry, INSERT);
3680 if (*slot != NULL)
3681 {
3682 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3683 return;
3684 }
3685
3686 lh = dwarf_decode_line_header (line_offset, cu);
3687 }
3688 if (lh == NULL)
3689 {
3690 lh_cu->v.quick->no_file_data = 1;
3691 return;
3692 }
3693
3694 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3695 qfn->hash.dwo_unit = cu->dwo_unit;
3696 qfn->hash.line_sect_off = line_offset;
3697 gdb_assert (slot != NULL);
3698 *slot = qfn;
3699
3700 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3701
3702 int offset = 0;
3703 if (strcmp (fnd.name, "<unknown>") != 0)
3704 ++offset;
3705
3706 qfn->num_file_names = offset + lh->file_names.size ();
3707 qfn->file_names =
3708 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3709 if (offset != 0)
3710 qfn->file_names[0] = xstrdup (fnd.name);
3711 for (i = 0; i < lh->file_names.size (); ++i)
3712 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3713 qfn->real_names = NULL;
3714
3715 lh_cu->v.quick->file_names = qfn;
3716 }
3717
3718 /* A helper for the "quick" functions which attempts to read the line
3719 table for THIS_CU. */
3720
3721 static struct quick_file_names *
3722 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3723 {
3724 /* This should never be called for TUs. */
3725 gdb_assert (! this_cu->is_debug_types);
3726 /* Nor type unit groups. */
3727 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3728
3729 if (this_cu->v.quick->file_names != NULL)
3730 return this_cu->v.quick->file_names;
3731 /* If we know there is no line data, no point in looking again. */
3732 if (this_cu->v.quick->no_file_data)
3733 return NULL;
3734
3735 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3736
3737 if (this_cu->v.quick->no_file_data)
3738 return NULL;
3739 return this_cu->v.quick->file_names;
3740 }
3741
3742 /* A helper for the "quick" functions which computes and caches the
3743 real path for a given file name from the line table. */
3744
3745 static const char *
3746 dw2_get_real_path (struct objfile *objfile,
3747 struct quick_file_names *qfn, int index)
3748 {
3749 if (qfn->real_names == NULL)
3750 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3751 qfn->num_file_names, const char *);
3752
3753 if (qfn->real_names[index] == NULL)
3754 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3755
3756 return qfn->real_names[index];
3757 }
3758
3759 static struct symtab *
3760 dw2_find_last_source_symtab (struct objfile *objfile)
3761 {
3762 struct dwarf2_per_objfile *dwarf2_per_objfile
3763 = get_dwarf2_per_objfile (objfile);
3764 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3765 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3766
3767 if (cust == NULL)
3768 return NULL;
3769
3770 return compunit_primary_filetab (cust);
3771 }
3772
3773 /* Traversal function for dw2_forget_cached_source_info. */
3774
3775 static int
3776 dw2_free_cached_file_names (void **slot, void *info)
3777 {
3778 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3779
3780 if (file_data->real_names)
3781 {
3782 int i;
3783
3784 for (i = 0; i < file_data->num_file_names; ++i)
3785 {
3786 xfree ((void*) file_data->real_names[i]);
3787 file_data->real_names[i] = NULL;
3788 }
3789 }
3790
3791 return 1;
3792 }
3793
3794 static void
3795 dw2_forget_cached_source_info (struct objfile *objfile)
3796 {
3797 struct dwarf2_per_objfile *dwarf2_per_objfile
3798 = get_dwarf2_per_objfile (objfile);
3799
3800 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3801 dw2_free_cached_file_names, NULL);
3802 }
3803
3804 /* Helper function for dw2_map_symtabs_matching_filename that expands
3805 the symtabs and calls the iterator. */
3806
3807 static int
3808 dw2_map_expand_apply (struct objfile *objfile,
3809 struct dwarf2_per_cu_data *per_cu,
3810 const char *name, const char *real_path,
3811 gdb::function_view<bool (symtab *)> callback)
3812 {
3813 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3814
3815 /* Don't visit already-expanded CUs. */
3816 if (per_cu->v.quick->compunit_symtab)
3817 return 0;
3818
3819 /* This may expand more than one symtab, and we want to iterate over
3820 all of them. */
3821 dw2_instantiate_symtab (per_cu, false);
3822
3823 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3824 last_made, callback);
3825 }
3826
3827 /* Implementation of the map_symtabs_matching_filename method. */
3828
3829 static bool
3830 dw2_map_symtabs_matching_filename
3831 (struct objfile *objfile, const char *name, const char *real_path,
3832 gdb::function_view<bool (symtab *)> callback)
3833 {
3834 const char *name_basename = lbasename (name);
3835 struct dwarf2_per_objfile *dwarf2_per_objfile
3836 = get_dwarf2_per_objfile (objfile);
3837
3838 /* The rule is CUs specify all the files, including those used by
3839 any TU, so there's no need to scan TUs here. */
3840
3841 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3842 {
3843 /* We only need to look at symtabs not already expanded. */
3844 if (per_cu->v.quick->compunit_symtab)
3845 continue;
3846
3847 quick_file_names *file_data = dw2_get_file_names (per_cu);
3848 if (file_data == NULL)
3849 continue;
3850
3851 for (int j = 0; j < file_data->num_file_names; ++j)
3852 {
3853 const char *this_name = file_data->file_names[j];
3854 const char *this_real_name;
3855
3856 if (compare_filenames_for_search (this_name, name))
3857 {
3858 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3859 callback))
3860 return true;
3861 continue;
3862 }
3863
3864 /* Before we invoke realpath, which can get expensive when many
3865 files are involved, do a quick comparison of the basenames. */
3866 if (! basenames_may_differ
3867 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3868 continue;
3869
3870 this_real_name = dw2_get_real_path (objfile, file_data, j);
3871 if (compare_filenames_for_search (this_real_name, name))
3872 {
3873 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3874 callback))
3875 return true;
3876 continue;
3877 }
3878
3879 if (real_path != NULL)
3880 {
3881 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3882 gdb_assert (IS_ABSOLUTE_PATH (name));
3883 if (this_real_name != NULL
3884 && FILENAME_CMP (real_path, this_real_name) == 0)
3885 {
3886 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3887 callback))
3888 return true;
3889 continue;
3890 }
3891 }
3892 }
3893 }
3894
3895 return false;
3896 }
3897
3898 /* Struct used to manage iterating over all CUs looking for a symbol. */
3899
3900 struct dw2_symtab_iterator
3901 {
3902 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3903 struct dwarf2_per_objfile *dwarf2_per_objfile;
3904 /* If set, only look for symbols that match that block. Valid values are
3905 GLOBAL_BLOCK and STATIC_BLOCK. */
3906 gdb::optional<block_enum> block_index;
3907 /* The kind of symbol we're looking for. */
3908 domain_enum domain;
3909 /* The list of CUs from the index entry of the symbol,
3910 or NULL if not found. */
3911 offset_type *vec;
3912 /* The next element in VEC to look at. */
3913 int next;
3914 /* The number of elements in VEC, or zero if there is no match. */
3915 int length;
3916 /* Have we seen a global version of the symbol?
3917 If so we can ignore all further global instances.
3918 This is to work around gold/15646, inefficient gold-generated
3919 indices. */
3920 int global_seen;
3921 };
3922
3923 /* Initialize the index symtab iterator ITER. */
3924
3925 static void
3926 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3927 struct dwarf2_per_objfile *dwarf2_per_objfile,
3928 gdb::optional<block_enum> block_index,
3929 domain_enum domain,
3930 const char *name)
3931 {
3932 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3933 iter->block_index = block_index;
3934 iter->domain = domain;
3935 iter->next = 0;
3936 iter->global_seen = 0;
3937
3938 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3939
3940 /* index is NULL if OBJF_READNOW. */
3941 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3942 iter->length = MAYBE_SWAP (*iter->vec);
3943 else
3944 {
3945 iter->vec = NULL;
3946 iter->length = 0;
3947 }
3948 }
3949
3950 /* Return the next matching CU or NULL if there are no more. */
3951
3952 static struct dwarf2_per_cu_data *
3953 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3954 {
3955 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3956
3957 for ( ; iter->next < iter->length; ++iter->next)
3958 {
3959 offset_type cu_index_and_attrs =
3960 MAYBE_SWAP (iter->vec[iter->next + 1]);
3961 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3962 gdb_index_symbol_kind symbol_kind =
3963 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3964 /* Only check the symbol attributes if they're present.
3965 Indices prior to version 7 don't record them,
3966 and indices >= 7 may elide them for certain symbols
3967 (gold does this). */
3968 int attrs_valid =
3969 (dwarf2_per_objfile->index_table->version >= 7
3970 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3971
3972 /* Don't crash on bad data. */
3973 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3974 + dwarf2_per_objfile->all_type_units.size ()))
3975 {
3976 complaint (_(".gdb_index entry has bad CU index"
3977 " [in module %s]"),
3978 objfile_name (dwarf2_per_objfile->objfile));
3979 continue;
3980 }
3981
3982 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3983
3984 /* Skip if already read in. */
3985 if (per_cu->v.quick->compunit_symtab)
3986 continue;
3987
3988 /* Check static vs global. */
3989 if (attrs_valid)
3990 {
3991 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3992
3993 if (iter->block_index.has_value ())
3994 {
3995 bool want_static = *iter->block_index == STATIC_BLOCK;
3996
3997 if (is_static != want_static)
3998 continue;
3999 }
4000
4001 /* Work around gold/15646. */
4002 if (!is_static && iter->global_seen)
4003 continue;
4004 if (!is_static)
4005 iter->global_seen = 1;
4006 }
4007
4008 /* Only check the symbol's kind if it has one. */
4009 if (attrs_valid)
4010 {
4011 switch (iter->domain)
4012 {
4013 case VAR_DOMAIN:
4014 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4015 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4016 /* Some types are also in VAR_DOMAIN. */
4017 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4018 continue;
4019 break;
4020 case STRUCT_DOMAIN:
4021 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4022 continue;
4023 break;
4024 case LABEL_DOMAIN:
4025 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4026 continue;
4027 break;
4028 default:
4029 break;
4030 }
4031 }
4032
4033 ++iter->next;
4034 return per_cu;
4035 }
4036
4037 return NULL;
4038 }
4039
4040 static struct compunit_symtab *
4041 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
4042 const char *name, domain_enum domain)
4043 {
4044 struct compunit_symtab *stab_best = NULL;
4045 struct dwarf2_per_objfile *dwarf2_per_objfile
4046 = get_dwarf2_per_objfile (objfile);
4047
4048 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4049
4050 struct dw2_symtab_iterator iter;
4051 struct dwarf2_per_cu_data *per_cu;
4052
4053 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
4054
4055 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4056 {
4057 struct symbol *sym, *with_opaque = NULL;
4058 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
4059 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4060 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4061
4062 sym = block_find_symbol (block, name, domain,
4063 block_find_non_opaque_type_preferred,
4064 &with_opaque);
4065
4066 /* Some caution must be observed with overloaded functions
4067 and methods, since the index will not contain any overload
4068 information (but NAME might contain it). */
4069
4070 if (sym != NULL
4071 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4072 return stab;
4073 if (with_opaque != NULL
4074 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4075 stab_best = stab;
4076
4077 /* Keep looking through other CUs. */
4078 }
4079
4080 return stab_best;
4081 }
4082
4083 static void
4084 dw2_print_stats (struct objfile *objfile)
4085 {
4086 struct dwarf2_per_objfile *dwarf2_per_objfile
4087 = get_dwarf2_per_objfile (objfile);
4088 int total = (dwarf2_per_objfile->all_comp_units.size ()
4089 + dwarf2_per_objfile->all_type_units.size ());
4090 int count = 0;
4091
4092 for (int i = 0; i < total; ++i)
4093 {
4094 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4095
4096 if (!per_cu->v.quick->compunit_symtab)
4097 ++count;
4098 }
4099 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4100 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4101 }
4102
4103 /* This dumps minimal information about the index.
4104 It is called via "mt print objfiles".
4105 One use is to verify .gdb_index has been loaded by the
4106 gdb.dwarf2/gdb-index.exp testcase. */
4107
4108 static void
4109 dw2_dump (struct objfile *objfile)
4110 {
4111 struct dwarf2_per_objfile *dwarf2_per_objfile
4112 = get_dwarf2_per_objfile (objfile);
4113
4114 gdb_assert (dwarf2_per_objfile->using_index);
4115 printf_filtered (".gdb_index:");
4116 if (dwarf2_per_objfile->index_table != NULL)
4117 {
4118 printf_filtered (" version %d\n",
4119 dwarf2_per_objfile->index_table->version);
4120 }
4121 else
4122 printf_filtered (" faked for \"readnow\"\n");
4123 printf_filtered ("\n");
4124 }
4125
4126 static void
4127 dw2_expand_symtabs_for_function (struct objfile *objfile,
4128 const char *func_name)
4129 {
4130 struct dwarf2_per_objfile *dwarf2_per_objfile
4131 = get_dwarf2_per_objfile (objfile);
4132
4133 struct dw2_symtab_iterator iter;
4134 struct dwarf2_per_cu_data *per_cu;
4135
4136 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
4137
4138 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4139 dw2_instantiate_symtab (per_cu, false);
4140
4141 }
4142
4143 static void
4144 dw2_expand_all_symtabs (struct objfile *objfile)
4145 {
4146 struct dwarf2_per_objfile *dwarf2_per_objfile
4147 = get_dwarf2_per_objfile (objfile);
4148 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4149 + dwarf2_per_objfile->all_type_units.size ());
4150
4151 for (int i = 0; i < total_units; ++i)
4152 {
4153 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4154
4155 /* We don't want to directly expand a partial CU, because if we
4156 read it with the wrong language, then assertion failures can
4157 be triggered later on. See PR symtab/23010. So, tell
4158 dw2_instantiate_symtab to skip partial CUs -- any important
4159 partial CU will be read via DW_TAG_imported_unit anyway. */
4160 dw2_instantiate_symtab (per_cu, true);
4161 }
4162 }
4163
4164 static void
4165 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4166 const char *fullname)
4167 {
4168 struct dwarf2_per_objfile *dwarf2_per_objfile
4169 = get_dwarf2_per_objfile (objfile);
4170
4171 /* We don't need to consider type units here.
4172 This is only called for examining code, e.g. expand_line_sal.
4173 There can be an order of magnitude (or more) more type units
4174 than comp units, and we avoid them if we can. */
4175
4176 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4177 {
4178 /* We only need to look at symtabs not already expanded. */
4179 if (per_cu->v.quick->compunit_symtab)
4180 continue;
4181
4182 quick_file_names *file_data = dw2_get_file_names (per_cu);
4183 if (file_data == NULL)
4184 continue;
4185
4186 for (int j = 0; j < file_data->num_file_names; ++j)
4187 {
4188 const char *this_fullname = file_data->file_names[j];
4189
4190 if (filename_cmp (this_fullname, fullname) == 0)
4191 {
4192 dw2_instantiate_symtab (per_cu, false);
4193 break;
4194 }
4195 }
4196 }
4197 }
4198
4199 static void
4200 dw2_map_matching_symbols
4201 (struct objfile *objfile,
4202 const lookup_name_info &name, domain_enum domain,
4203 int global,
4204 gdb::function_view<symbol_found_callback_ftype> callback,
4205 symbol_compare_ftype *ordered_compare)
4206 {
4207 /* Currently unimplemented; used for Ada. The function can be called if the
4208 current language is Ada for a non-Ada objfile using GNU index. As Ada
4209 does not look for non-Ada symbols this function should just return. */
4210 }
4211
4212 /* Starting from a search name, return the string that finds the upper
4213 bound of all strings that start with SEARCH_NAME in a sorted name
4214 list. Returns the empty string to indicate that the upper bound is
4215 the end of the list. */
4216
4217 static std::string
4218 make_sort_after_prefix_name (const char *search_name)
4219 {
4220 /* When looking to complete "func", we find the upper bound of all
4221 symbols that start with "func" by looking for where we'd insert
4222 the closest string that would follow "func" in lexicographical
4223 order. Usually, that's "func"-with-last-character-incremented,
4224 i.e. "fund". Mind non-ASCII characters, though. Usually those
4225 will be UTF-8 multi-byte sequences, but we can't be certain.
4226 Especially mind the 0xff character, which is a valid character in
4227 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4228 rule out compilers allowing it in identifiers. Note that
4229 conveniently, strcmp/strcasecmp are specified to compare
4230 characters interpreted as unsigned char. So what we do is treat
4231 the whole string as a base 256 number composed of a sequence of
4232 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4233 to 0, and carries 1 to the following more-significant position.
4234 If the very first character in SEARCH_NAME ends up incremented
4235 and carries/overflows, then the upper bound is the end of the
4236 list. The string after the empty string is also the empty
4237 string.
4238
4239 Some examples of this operation:
4240
4241 SEARCH_NAME => "+1" RESULT
4242
4243 "abc" => "abd"
4244 "ab\xff" => "ac"
4245 "\xff" "a" "\xff" => "\xff" "b"
4246 "\xff" => ""
4247 "\xff\xff" => ""
4248 "" => ""
4249
4250 Then, with these symbols for example:
4251
4252 func
4253 func1
4254 fund
4255
4256 completing "func" looks for symbols between "func" and
4257 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4258 which finds "func" and "func1", but not "fund".
4259
4260 And with:
4261
4262 funcÿ (Latin1 'ÿ' [0xff])
4263 funcÿ1
4264 fund
4265
4266 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4267 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4268
4269 And with:
4270
4271 ÿÿ (Latin1 'ÿ' [0xff])
4272 ÿÿ1
4273
4274 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4275 the end of the list.
4276 */
4277 std::string after = search_name;
4278 while (!after.empty () && (unsigned char) after.back () == 0xff)
4279 after.pop_back ();
4280 if (!after.empty ())
4281 after.back () = (unsigned char) after.back () + 1;
4282 return after;
4283 }
4284
4285 /* See declaration. */
4286
4287 std::pair<std::vector<name_component>::const_iterator,
4288 std::vector<name_component>::const_iterator>
4289 mapped_index_base::find_name_components_bounds
4290 (const lookup_name_info &lookup_name_without_params, language lang) const
4291 {
4292 auto *name_cmp
4293 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4294
4295 const char *lang_name
4296 = lookup_name_without_params.language_lookup_name (lang).c_str ();
4297
4298 /* Comparison function object for lower_bound that matches against a
4299 given symbol name. */
4300 auto lookup_compare_lower = [&] (const name_component &elem,
4301 const char *name)
4302 {
4303 const char *elem_qualified = this->symbol_name_at (elem.idx);
4304 const char *elem_name = elem_qualified + elem.name_offset;
4305 return name_cmp (elem_name, name) < 0;
4306 };
4307
4308 /* Comparison function object for upper_bound that matches against a
4309 given symbol name. */
4310 auto lookup_compare_upper = [&] (const char *name,
4311 const name_component &elem)
4312 {
4313 const char *elem_qualified = this->symbol_name_at (elem.idx);
4314 const char *elem_name = elem_qualified + elem.name_offset;
4315 return name_cmp (name, elem_name) < 0;
4316 };
4317
4318 auto begin = this->name_components.begin ();
4319 auto end = this->name_components.end ();
4320
4321 /* Find the lower bound. */
4322 auto lower = [&] ()
4323 {
4324 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4325 return begin;
4326 else
4327 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4328 } ();
4329
4330 /* Find the upper bound. */
4331 auto upper = [&] ()
4332 {
4333 if (lookup_name_without_params.completion_mode ())
4334 {
4335 /* In completion mode, we want UPPER to point past all
4336 symbols names that have the same prefix. I.e., with
4337 these symbols, and completing "func":
4338
4339 function << lower bound
4340 function1
4341 other_function << upper bound
4342
4343 We find the upper bound by looking for the insertion
4344 point of "func"-with-last-character-incremented,
4345 i.e. "fund". */
4346 std::string after = make_sort_after_prefix_name (lang_name);
4347 if (after.empty ())
4348 return end;
4349 return std::lower_bound (lower, end, after.c_str (),
4350 lookup_compare_lower);
4351 }
4352 else
4353 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4354 } ();
4355
4356 return {lower, upper};
4357 }
4358
4359 /* See declaration. */
4360
4361 void
4362 mapped_index_base::build_name_components ()
4363 {
4364 if (!this->name_components.empty ())
4365 return;
4366
4367 this->name_components_casing = case_sensitivity;
4368 auto *name_cmp
4369 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4370
4371 /* The code below only knows how to break apart components of C++
4372 symbol names (and other languages that use '::' as
4373 namespace/module separator) and Ada symbol names. */
4374 auto count = this->symbol_name_count ();
4375 for (offset_type idx = 0; idx < count; idx++)
4376 {
4377 if (this->symbol_name_slot_invalid (idx))
4378 continue;
4379
4380 const char *name = this->symbol_name_at (idx);
4381
4382 /* Add each name component to the name component table. */
4383 unsigned int previous_len = 0;
4384
4385 if (strstr (name, "::") != nullptr)
4386 {
4387 for (unsigned int current_len = cp_find_first_component (name);
4388 name[current_len] != '\0';
4389 current_len += cp_find_first_component (name + current_len))
4390 {
4391 gdb_assert (name[current_len] == ':');
4392 this->name_components.push_back ({previous_len, idx});
4393 /* Skip the '::'. */
4394 current_len += 2;
4395 previous_len = current_len;
4396 }
4397 }
4398 else
4399 {
4400 /* Handle the Ada encoded (aka mangled) form here. */
4401 for (const char *iter = strstr (name, "__");
4402 iter != nullptr;
4403 iter = strstr (iter, "__"))
4404 {
4405 this->name_components.push_back ({previous_len, idx});
4406 iter += 2;
4407 previous_len = iter - name;
4408 }
4409 }
4410
4411 this->name_components.push_back ({previous_len, idx});
4412 }
4413
4414 /* Sort name_components elements by name. */
4415 auto name_comp_compare = [&] (const name_component &left,
4416 const name_component &right)
4417 {
4418 const char *left_qualified = this->symbol_name_at (left.idx);
4419 const char *right_qualified = this->symbol_name_at (right.idx);
4420
4421 const char *left_name = left_qualified + left.name_offset;
4422 const char *right_name = right_qualified + right.name_offset;
4423
4424 return name_cmp (left_name, right_name) < 0;
4425 };
4426
4427 std::sort (this->name_components.begin (),
4428 this->name_components.end (),
4429 name_comp_compare);
4430 }
4431
4432 /* Helper for dw2_expand_symtabs_matching that works with a
4433 mapped_index_base instead of the containing objfile. This is split
4434 to a separate function in order to be able to unit test the
4435 name_components matching using a mock mapped_index_base. For each
4436 symbol name that matches, calls MATCH_CALLBACK, passing it the
4437 symbol's index in the mapped_index_base symbol table. */
4438
4439 static void
4440 dw2_expand_symtabs_matching_symbol
4441 (mapped_index_base &index,
4442 const lookup_name_info &lookup_name_in,
4443 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4444 enum search_domain kind,
4445 gdb::function_view<bool (offset_type)> match_callback)
4446 {
4447 lookup_name_info lookup_name_without_params
4448 = lookup_name_in.make_ignore_params ();
4449
4450 /* Build the symbol name component sorted vector, if we haven't
4451 yet. */
4452 index.build_name_components ();
4453
4454 /* The same symbol may appear more than once in the range though.
4455 E.g., if we're looking for symbols that complete "w", and we have
4456 a symbol named "w1::w2", we'll find the two name components for
4457 that same symbol in the range. To be sure we only call the
4458 callback once per symbol, we first collect the symbol name
4459 indexes that matched in a temporary vector and ignore
4460 duplicates. */
4461 std::vector<offset_type> matches;
4462
4463 struct name_and_matcher
4464 {
4465 symbol_name_matcher_ftype *matcher;
4466 const std::string &name;
4467
4468 bool operator== (const name_and_matcher &other) const
4469 {
4470 return matcher == other.matcher && name == other.name;
4471 }
4472 };
4473
4474 /* A vector holding all the different symbol name matchers, for all
4475 languages. */
4476 std::vector<name_and_matcher> matchers;
4477
4478 for (int i = 0; i < nr_languages; i++)
4479 {
4480 enum language lang_e = (enum language) i;
4481
4482 const language_defn *lang = language_def (lang_e);
4483 symbol_name_matcher_ftype *name_matcher
4484 = get_symbol_name_matcher (lang, lookup_name_without_params);
4485
4486 name_and_matcher key {
4487 name_matcher,
4488 lookup_name_without_params.language_lookup_name (lang_e)
4489 };
4490
4491 /* Don't insert the same comparison routine more than once.
4492 Note that we do this linear walk. This is not a problem in
4493 practice because the number of supported languages is
4494 low. */
4495 if (std::find (matchers.begin (), matchers.end (), key)
4496 != matchers.end ())
4497 continue;
4498 matchers.push_back (std::move (key));
4499
4500 auto bounds
4501 = index.find_name_components_bounds (lookup_name_without_params,
4502 lang_e);
4503
4504 /* Now for each symbol name in range, check to see if we have a name
4505 match, and if so, call the MATCH_CALLBACK callback. */
4506
4507 for (; bounds.first != bounds.second; ++bounds.first)
4508 {
4509 const char *qualified = index.symbol_name_at (bounds.first->idx);
4510
4511 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4512 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4513 continue;
4514
4515 matches.push_back (bounds.first->idx);
4516 }
4517 }
4518
4519 std::sort (matches.begin (), matches.end ());
4520
4521 /* Finally call the callback, once per match. */
4522 ULONGEST prev = -1;
4523 for (offset_type idx : matches)
4524 {
4525 if (prev != idx)
4526 {
4527 if (!match_callback (idx))
4528 break;
4529 prev = idx;
4530 }
4531 }
4532
4533 /* Above we use a type wider than idx's for 'prev', since 0 and
4534 (offset_type)-1 are both possible values. */
4535 static_assert (sizeof (prev) > sizeof (offset_type), "");
4536 }
4537
4538 #if GDB_SELF_TEST
4539
4540 namespace selftests { namespace dw2_expand_symtabs_matching {
4541
4542 /* A mock .gdb_index/.debug_names-like name index table, enough to
4543 exercise dw2_expand_symtabs_matching_symbol, which works with the
4544 mapped_index_base interface. Builds an index from the symbol list
4545 passed as parameter to the constructor. */
4546 class mock_mapped_index : public mapped_index_base
4547 {
4548 public:
4549 mock_mapped_index (gdb::array_view<const char *> symbols)
4550 : m_symbol_table (symbols)
4551 {}
4552
4553 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4554
4555 /* Return the number of names in the symbol table. */
4556 size_t symbol_name_count () const override
4557 {
4558 return m_symbol_table.size ();
4559 }
4560
4561 /* Get the name of the symbol at IDX in the symbol table. */
4562 const char *symbol_name_at (offset_type idx) const override
4563 {
4564 return m_symbol_table[idx];
4565 }
4566
4567 private:
4568 gdb::array_view<const char *> m_symbol_table;
4569 };
4570
4571 /* Convenience function that converts a NULL pointer to a "<null>"
4572 string, to pass to print routines. */
4573
4574 static const char *
4575 string_or_null (const char *str)
4576 {
4577 return str != NULL ? str : "<null>";
4578 }
4579
4580 /* Check if a lookup_name_info built from
4581 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4582 index. EXPECTED_LIST is the list of expected matches, in expected
4583 matching order. If no match expected, then an empty list is
4584 specified. Returns true on success. On failure prints a warning
4585 indicating the file:line that failed, and returns false. */
4586
4587 static bool
4588 check_match (const char *file, int line,
4589 mock_mapped_index &mock_index,
4590 const char *name, symbol_name_match_type match_type,
4591 bool completion_mode,
4592 std::initializer_list<const char *> expected_list)
4593 {
4594 lookup_name_info lookup_name (name, match_type, completion_mode);
4595
4596 bool matched = true;
4597
4598 auto mismatch = [&] (const char *expected_str,
4599 const char *got)
4600 {
4601 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4602 "expected=\"%s\", got=\"%s\"\n"),
4603 file, line,
4604 (match_type == symbol_name_match_type::FULL
4605 ? "FULL" : "WILD"),
4606 name, string_or_null (expected_str), string_or_null (got));
4607 matched = false;
4608 };
4609
4610 auto expected_it = expected_list.begin ();
4611 auto expected_end = expected_list.end ();
4612
4613 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4614 NULL, ALL_DOMAIN,
4615 [&] (offset_type idx)
4616 {
4617 const char *matched_name = mock_index.symbol_name_at (idx);
4618 const char *expected_str
4619 = expected_it == expected_end ? NULL : *expected_it++;
4620
4621 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4622 mismatch (expected_str, matched_name);
4623 return true;
4624 });
4625
4626 const char *expected_str
4627 = expected_it == expected_end ? NULL : *expected_it++;
4628 if (expected_str != NULL)
4629 mismatch (expected_str, NULL);
4630
4631 return matched;
4632 }
4633
4634 /* The symbols added to the mock mapped_index for testing (in
4635 canonical form). */
4636 static const char *test_symbols[] = {
4637 "function",
4638 "std::bar",
4639 "std::zfunction",
4640 "std::zfunction2",
4641 "w1::w2",
4642 "ns::foo<char*>",
4643 "ns::foo<int>",
4644 "ns::foo<long>",
4645 "ns2::tmpl<int>::foo2",
4646 "(anonymous namespace)::A::B::C",
4647
4648 /* These are used to check that the increment-last-char in the
4649 matching algorithm for completion doesn't match "t1_fund" when
4650 completing "t1_func". */
4651 "t1_func",
4652 "t1_func1",
4653 "t1_fund",
4654 "t1_fund1",
4655
4656 /* A UTF-8 name with multi-byte sequences to make sure that
4657 cp-name-parser understands this as a single identifier ("função"
4658 is "function" in PT). */
4659 u8"u8função",
4660
4661 /* \377 (0xff) is Latin1 'ÿ'. */
4662 "yfunc\377",
4663
4664 /* \377 (0xff) is Latin1 'ÿ'. */
4665 "\377",
4666 "\377\377123",
4667
4668 /* A name with all sorts of complications. Starts with "z" to make
4669 it easier for the completion tests below. */
4670 #define Z_SYM_NAME \
4671 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4672 "::tuple<(anonymous namespace)::ui*, " \
4673 "std::default_delete<(anonymous namespace)::ui>, void>"
4674
4675 Z_SYM_NAME
4676 };
4677
4678 /* Returns true if the mapped_index_base::find_name_component_bounds
4679 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4680 in completion mode. */
4681
4682 static bool
4683 check_find_bounds_finds (mapped_index_base &index,
4684 const char *search_name,
4685 gdb::array_view<const char *> expected_syms)
4686 {
4687 lookup_name_info lookup_name (search_name,
4688 symbol_name_match_type::FULL, true);
4689
4690 auto bounds = index.find_name_components_bounds (lookup_name,
4691 language_cplus);
4692
4693 size_t distance = std::distance (bounds.first, bounds.second);
4694 if (distance != expected_syms.size ())
4695 return false;
4696
4697 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4698 {
4699 auto nc_elem = bounds.first + exp_elem;
4700 const char *qualified = index.symbol_name_at (nc_elem->idx);
4701 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4702 return false;
4703 }
4704
4705 return true;
4706 }
4707
4708 /* Test the lower-level mapped_index::find_name_component_bounds
4709 method. */
4710
4711 static void
4712 test_mapped_index_find_name_component_bounds ()
4713 {
4714 mock_mapped_index mock_index (test_symbols);
4715
4716 mock_index.build_name_components ();
4717
4718 /* Test the lower-level mapped_index::find_name_component_bounds
4719 method in completion mode. */
4720 {
4721 static const char *expected_syms[] = {
4722 "t1_func",
4723 "t1_func1",
4724 };
4725
4726 SELF_CHECK (check_find_bounds_finds (mock_index,
4727 "t1_func", expected_syms));
4728 }
4729
4730 /* Check that the increment-last-char in the name matching algorithm
4731 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4732 {
4733 static const char *expected_syms1[] = {
4734 "\377",
4735 "\377\377123",
4736 };
4737 SELF_CHECK (check_find_bounds_finds (mock_index,
4738 "\377", expected_syms1));
4739
4740 static const char *expected_syms2[] = {
4741 "\377\377123",
4742 };
4743 SELF_CHECK (check_find_bounds_finds (mock_index,
4744 "\377\377", expected_syms2));
4745 }
4746 }
4747
4748 /* Test dw2_expand_symtabs_matching_symbol. */
4749
4750 static void
4751 test_dw2_expand_symtabs_matching_symbol ()
4752 {
4753 mock_mapped_index mock_index (test_symbols);
4754
4755 /* We let all tests run until the end even if some fails, for debug
4756 convenience. */
4757 bool any_mismatch = false;
4758
4759 /* Create the expected symbols list (an initializer_list). Needed
4760 because lists have commas, and we need to pass them to CHECK,
4761 which is a macro. */
4762 #define EXPECT(...) { __VA_ARGS__ }
4763
4764 /* Wrapper for check_match that passes down the current
4765 __FILE__/__LINE__. */
4766 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4767 any_mismatch |= !check_match (__FILE__, __LINE__, \
4768 mock_index, \
4769 NAME, MATCH_TYPE, COMPLETION_MODE, \
4770 EXPECTED_LIST)
4771
4772 /* Identity checks. */
4773 for (const char *sym : test_symbols)
4774 {
4775 /* Should be able to match all existing symbols. */
4776 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4777 EXPECT (sym));
4778
4779 /* Should be able to match all existing symbols with
4780 parameters. */
4781 std::string with_params = std::string (sym) + "(int)";
4782 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4783 EXPECT (sym));
4784
4785 /* Should be able to match all existing symbols with
4786 parameters and qualifiers. */
4787 with_params = std::string (sym) + " ( int ) const";
4788 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4789 EXPECT (sym));
4790
4791 /* This should really find sym, but cp-name-parser.y doesn't
4792 know about lvalue/rvalue qualifiers yet. */
4793 with_params = std::string (sym) + " ( int ) &&";
4794 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4795 {});
4796 }
4797
4798 /* Check that the name matching algorithm for completion doesn't get
4799 confused with Latin1 'ÿ' / 0xff. */
4800 {
4801 static const char str[] = "\377";
4802 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4803 EXPECT ("\377", "\377\377123"));
4804 }
4805
4806 /* Check that the increment-last-char in the matching algorithm for
4807 completion doesn't match "t1_fund" when completing "t1_func". */
4808 {
4809 static const char str[] = "t1_func";
4810 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4811 EXPECT ("t1_func", "t1_func1"));
4812 }
4813
4814 /* Check that completion mode works at each prefix of the expected
4815 symbol name. */
4816 {
4817 static const char str[] = "function(int)";
4818 size_t len = strlen (str);
4819 std::string lookup;
4820
4821 for (size_t i = 1; i < len; i++)
4822 {
4823 lookup.assign (str, i);
4824 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4825 EXPECT ("function"));
4826 }
4827 }
4828
4829 /* While "w" is a prefix of both components, the match function
4830 should still only be called once. */
4831 {
4832 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4833 EXPECT ("w1::w2"));
4834 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4835 EXPECT ("w1::w2"));
4836 }
4837
4838 /* Same, with a "complicated" symbol. */
4839 {
4840 static const char str[] = Z_SYM_NAME;
4841 size_t len = strlen (str);
4842 std::string lookup;
4843
4844 for (size_t i = 1; i < len; i++)
4845 {
4846 lookup.assign (str, i);
4847 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4848 EXPECT (Z_SYM_NAME));
4849 }
4850 }
4851
4852 /* In FULL mode, an incomplete symbol doesn't match. */
4853 {
4854 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4855 {});
4856 }
4857
4858 /* A complete symbol with parameters matches any overload, since the
4859 index has no overload info. */
4860 {
4861 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4862 EXPECT ("std::zfunction", "std::zfunction2"));
4863 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4864 EXPECT ("std::zfunction", "std::zfunction2"));
4865 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4866 EXPECT ("std::zfunction", "std::zfunction2"));
4867 }
4868
4869 /* Check that whitespace is ignored appropriately. A symbol with a
4870 template argument list. */
4871 {
4872 static const char expected[] = "ns::foo<int>";
4873 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4874 EXPECT (expected));
4875 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4876 EXPECT (expected));
4877 }
4878
4879 /* Check that whitespace is ignored appropriately. A symbol with a
4880 template argument list that includes a pointer. */
4881 {
4882 static const char expected[] = "ns::foo<char*>";
4883 /* Try both completion and non-completion modes. */
4884 static const bool completion_mode[2] = {false, true};
4885 for (size_t i = 0; i < 2; i++)
4886 {
4887 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4888 completion_mode[i], EXPECT (expected));
4889 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4890 completion_mode[i], EXPECT (expected));
4891
4892 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4893 completion_mode[i], EXPECT (expected));
4894 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4895 completion_mode[i], EXPECT (expected));
4896 }
4897 }
4898
4899 {
4900 /* Check method qualifiers are ignored. */
4901 static const char expected[] = "ns::foo<char*>";
4902 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4903 symbol_name_match_type::FULL, true, EXPECT (expected));
4904 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4905 symbol_name_match_type::FULL, true, EXPECT (expected));
4906 CHECK_MATCH ("foo < char * > ( int ) const",
4907 symbol_name_match_type::WILD, true, EXPECT (expected));
4908 CHECK_MATCH ("foo < char * > ( int ) &&",
4909 symbol_name_match_type::WILD, true, EXPECT (expected));
4910 }
4911
4912 /* Test lookup names that don't match anything. */
4913 {
4914 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4915 {});
4916
4917 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4918 {});
4919 }
4920
4921 /* Some wild matching tests, exercising "(anonymous namespace)",
4922 which should not be confused with a parameter list. */
4923 {
4924 static const char *syms[] = {
4925 "A::B::C",
4926 "B::C",
4927 "C",
4928 "A :: B :: C ( int )",
4929 "B :: C ( int )",
4930 "C ( int )",
4931 };
4932
4933 for (const char *s : syms)
4934 {
4935 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4936 EXPECT ("(anonymous namespace)::A::B::C"));
4937 }
4938 }
4939
4940 {
4941 static const char expected[] = "ns2::tmpl<int>::foo2";
4942 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4943 EXPECT (expected));
4944 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4945 EXPECT (expected));
4946 }
4947
4948 SELF_CHECK (!any_mismatch);
4949
4950 #undef EXPECT
4951 #undef CHECK_MATCH
4952 }
4953
4954 static void
4955 run_test ()
4956 {
4957 test_mapped_index_find_name_component_bounds ();
4958 test_dw2_expand_symtabs_matching_symbol ();
4959 }
4960
4961 }} // namespace selftests::dw2_expand_symtabs_matching
4962
4963 #endif /* GDB_SELF_TEST */
4964
4965 /* If FILE_MATCHER is NULL or if PER_CU has
4966 dwarf2_per_cu_quick_data::MARK set (see
4967 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4968 EXPANSION_NOTIFY on it. */
4969
4970 static void
4971 dw2_expand_symtabs_matching_one
4972 (struct dwarf2_per_cu_data *per_cu,
4973 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4974 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4975 {
4976 if (file_matcher == NULL || per_cu->v.quick->mark)
4977 {
4978 bool symtab_was_null
4979 = (per_cu->v.quick->compunit_symtab == NULL);
4980
4981 dw2_instantiate_symtab (per_cu, false);
4982
4983 if (expansion_notify != NULL
4984 && symtab_was_null
4985 && per_cu->v.quick->compunit_symtab != NULL)
4986 expansion_notify (per_cu->v.quick->compunit_symtab);
4987 }
4988 }
4989
4990 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4991 matched, to expand corresponding CUs that were marked. IDX is the
4992 index of the symbol name that matched. */
4993
4994 static void
4995 dw2_expand_marked_cus
4996 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4997 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4998 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4999 search_domain kind)
5000 {
5001 offset_type *vec, vec_len, vec_idx;
5002 bool global_seen = false;
5003 mapped_index &index = *dwarf2_per_objfile->index_table;
5004
5005 vec = (offset_type *) (index.constant_pool
5006 + MAYBE_SWAP (index.symbol_table[idx].vec));
5007 vec_len = MAYBE_SWAP (vec[0]);
5008 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5009 {
5010 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5011 /* This value is only valid for index versions >= 7. */
5012 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5013 gdb_index_symbol_kind symbol_kind =
5014 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5015 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5016 /* Only check the symbol attributes if they're present.
5017 Indices prior to version 7 don't record them,
5018 and indices >= 7 may elide them for certain symbols
5019 (gold does this). */
5020 int attrs_valid =
5021 (index.version >= 7
5022 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5023
5024 /* Work around gold/15646. */
5025 if (attrs_valid)
5026 {
5027 if (!is_static && global_seen)
5028 continue;
5029 if (!is_static)
5030 global_seen = true;
5031 }
5032
5033 /* Only check the symbol's kind if it has one. */
5034 if (attrs_valid)
5035 {
5036 switch (kind)
5037 {
5038 case VARIABLES_DOMAIN:
5039 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5040 continue;
5041 break;
5042 case FUNCTIONS_DOMAIN:
5043 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5044 continue;
5045 break;
5046 case TYPES_DOMAIN:
5047 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5048 continue;
5049 break;
5050 default:
5051 break;
5052 }
5053 }
5054
5055 /* Don't crash on bad data. */
5056 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5057 + dwarf2_per_objfile->all_type_units.size ()))
5058 {
5059 complaint (_(".gdb_index entry has bad CU index"
5060 " [in module %s]"),
5061 objfile_name (dwarf2_per_objfile->objfile));
5062 continue;
5063 }
5064
5065 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5066 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5067 expansion_notify);
5068 }
5069 }
5070
5071 /* If FILE_MATCHER is non-NULL, set all the
5072 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5073 that match FILE_MATCHER. */
5074
5075 static void
5076 dw_expand_symtabs_matching_file_matcher
5077 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5078 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5079 {
5080 if (file_matcher == NULL)
5081 return;
5082
5083 objfile *const objfile = dwarf2_per_objfile->objfile;
5084
5085 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5086 htab_eq_pointer,
5087 NULL, xcalloc, xfree));
5088 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5089 htab_eq_pointer,
5090 NULL, xcalloc, xfree));
5091
5092 /* The rule is CUs specify all the files, including those used by
5093 any TU, so there's no need to scan TUs here. */
5094
5095 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5096 {
5097 QUIT;
5098
5099 per_cu->v.quick->mark = 0;
5100
5101 /* We only need to look at symtabs not already expanded. */
5102 if (per_cu->v.quick->compunit_symtab)
5103 continue;
5104
5105 quick_file_names *file_data = dw2_get_file_names (per_cu);
5106 if (file_data == NULL)
5107 continue;
5108
5109 if (htab_find (visited_not_found.get (), file_data) != NULL)
5110 continue;
5111 else if (htab_find (visited_found.get (), file_data) != NULL)
5112 {
5113 per_cu->v.quick->mark = 1;
5114 continue;
5115 }
5116
5117 for (int j = 0; j < file_data->num_file_names; ++j)
5118 {
5119 const char *this_real_name;
5120
5121 if (file_matcher (file_data->file_names[j], false))
5122 {
5123 per_cu->v.quick->mark = 1;
5124 break;
5125 }
5126
5127 /* Before we invoke realpath, which can get expensive when many
5128 files are involved, do a quick comparison of the basenames. */
5129 if (!basenames_may_differ
5130 && !file_matcher (lbasename (file_data->file_names[j]),
5131 true))
5132 continue;
5133
5134 this_real_name = dw2_get_real_path (objfile, file_data, j);
5135 if (file_matcher (this_real_name, false))
5136 {
5137 per_cu->v.quick->mark = 1;
5138 break;
5139 }
5140 }
5141
5142 void **slot = htab_find_slot (per_cu->v.quick->mark
5143 ? visited_found.get ()
5144 : visited_not_found.get (),
5145 file_data, INSERT);
5146 *slot = file_data;
5147 }
5148 }
5149
5150 static void
5151 dw2_expand_symtabs_matching
5152 (struct objfile *objfile,
5153 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5154 const lookup_name_info &lookup_name,
5155 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5156 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5157 enum search_domain kind)
5158 {
5159 struct dwarf2_per_objfile *dwarf2_per_objfile
5160 = get_dwarf2_per_objfile (objfile);
5161
5162 /* index_table is NULL if OBJF_READNOW. */
5163 if (!dwarf2_per_objfile->index_table)
5164 return;
5165
5166 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5167
5168 mapped_index &index = *dwarf2_per_objfile->index_table;
5169
5170 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5171 symbol_matcher,
5172 kind, [&] (offset_type idx)
5173 {
5174 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5175 expansion_notify, kind);
5176 return true;
5177 });
5178 }
5179
5180 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5181 symtab. */
5182
5183 static struct compunit_symtab *
5184 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5185 CORE_ADDR pc)
5186 {
5187 int i;
5188
5189 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5190 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5191 return cust;
5192
5193 if (cust->includes == NULL)
5194 return NULL;
5195
5196 for (i = 0; cust->includes[i]; ++i)
5197 {
5198 struct compunit_symtab *s = cust->includes[i];
5199
5200 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5201 if (s != NULL)
5202 return s;
5203 }
5204
5205 return NULL;
5206 }
5207
5208 static struct compunit_symtab *
5209 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5210 struct bound_minimal_symbol msymbol,
5211 CORE_ADDR pc,
5212 struct obj_section *section,
5213 int warn_if_readin)
5214 {
5215 struct dwarf2_per_cu_data *data;
5216 struct compunit_symtab *result;
5217
5218 if (!objfile->partial_symtabs->psymtabs_addrmap)
5219 return NULL;
5220
5221 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5222 SECT_OFF_TEXT (objfile));
5223 data = (struct dwarf2_per_cu_data *) addrmap_find
5224 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
5225 if (!data)
5226 return NULL;
5227
5228 if (warn_if_readin && data->v.quick->compunit_symtab)
5229 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5230 paddress (get_objfile_arch (objfile), pc));
5231
5232 result
5233 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5234 false),
5235 pc);
5236 gdb_assert (result != NULL);
5237 return result;
5238 }
5239
5240 static void
5241 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5242 void *data, int need_fullname)
5243 {
5244 struct dwarf2_per_objfile *dwarf2_per_objfile
5245 = get_dwarf2_per_objfile (objfile);
5246
5247 if (!dwarf2_per_objfile->filenames_cache)
5248 {
5249 dwarf2_per_objfile->filenames_cache.emplace ();
5250
5251 htab_up visited (htab_create_alloc (10,
5252 htab_hash_pointer, htab_eq_pointer,
5253 NULL, xcalloc, xfree));
5254
5255 /* The rule is CUs specify all the files, including those used
5256 by any TU, so there's no need to scan TUs here. We can
5257 ignore file names coming from already-expanded CUs. */
5258
5259 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5260 {
5261 if (per_cu->v.quick->compunit_symtab)
5262 {
5263 void **slot = htab_find_slot (visited.get (),
5264 per_cu->v.quick->file_names,
5265 INSERT);
5266
5267 *slot = per_cu->v.quick->file_names;
5268 }
5269 }
5270
5271 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5272 {
5273 /* We only need to look at symtabs not already expanded. */
5274 if (per_cu->v.quick->compunit_symtab)
5275 continue;
5276
5277 quick_file_names *file_data = dw2_get_file_names (per_cu);
5278 if (file_data == NULL)
5279 continue;
5280
5281 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5282 if (*slot)
5283 {
5284 /* Already visited. */
5285 continue;
5286 }
5287 *slot = file_data;
5288
5289 for (int j = 0; j < file_data->num_file_names; ++j)
5290 {
5291 const char *filename = file_data->file_names[j];
5292 dwarf2_per_objfile->filenames_cache->seen (filename);
5293 }
5294 }
5295 }
5296
5297 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5298 {
5299 gdb::unique_xmalloc_ptr<char> this_real_name;
5300
5301 if (need_fullname)
5302 this_real_name = gdb_realpath (filename);
5303 (*fun) (filename, this_real_name.get (), data);
5304 });
5305 }
5306
5307 static int
5308 dw2_has_symbols (struct objfile *objfile)
5309 {
5310 return 1;
5311 }
5312
5313 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5314 {
5315 dw2_has_symbols,
5316 dw2_find_last_source_symtab,
5317 dw2_forget_cached_source_info,
5318 dw2_map_symtabs_matching_filename,
5319 dw2_lookup_symbol,
5320 dw2_print_stats,
5321 dw2_dump,
5322 dw2_expand_symtabs_for_function,
5323 dw2_expand_all_symtabs,
5324 dw2_expand_symtabs_with_fullname,
5325 dw2_map_matching_symbols,
5326 dw2_expand_symtabs_matching,
5327 dw2_find_pc_sect_compunit_symtab,
5328 NULL,
5329 dw2_map_symbol_filenames
5330 };
5331
5332 /* DWARF-5 debug_names reader. */
5333
5334 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5335 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5336
5337 /* A helper function that reads the .debug_names section in SECTION
5338 and fills in MAP. FILENAME is the name of the file containing the
5339 section; it is used for error reporting.
5340
5341 Returns true if all went well, false otherwise. */
5342
5343 static bool
5344 read_debug_names_from_section (struct objfile *objfile,
5345 const char *filename,
5346 struct dwarf2_section_info *section,
5347 mapped_debug_names &map)
5348 {
5349 if (dwarf2_section_empty_p (section))
5350 return false;
5351
5352 /* Older elfutils strip versions could keep the section in the main
5353 executable while splitting it for the separate debug info file. */
5354 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5355 return false;
5356
5357 dwarf2_read_section (objfile, section);
5358
5359 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5360
5361 const gdb_byte *addr = section->buffer;
5362
5363 bfd *const abfd = get_section_bfd_owner (section);
5364
5365 unsigned int bytes_read;
5366 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5367 addr += bytes_read;
5368
5369 map.dwarf5_is_dwarf64 = bytes_read != 4;
5370 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5371 if (bytes_read + length != section->size)
5372 {
5373 /* There may be multiple per-CU indices. */
5374 warning (_("Section .debug_names in %s length %s does not match "
5375 "section length %s, ignoring .debug_names."),
5376 filename, plongest (bytes_read + length),
5377 pulongest (section->size));
5378 return false;
5379 }
5380
5381 /* The version number. */
5382 uint16_t version = read_2_bytes (abfd, addr);
5383 addr += 2;
5384 if (version != 5)
5385 {
5386 warning (_("Section .debug_names in %s has unsupported version %d, "
5387 "ignoring .debug_names."),
5388 filename, version);
5389 return false;
5390 }
5391
5392 /* Padding. */
5393 uint16_t padding = read_2_bytes (abfd, addr);
5394 addr += 2;
5395 if (padding != 0)
5396 {
5397 warning (_("Section .debug_names in %s has unsupported padding %d, "
5398 "ignoring .debug_names."),
5399 filename, padding);
5400 return false;
5401 }
5402
5403 /* comp_unit_count - The number of CUs in the CU list. */
5404 map.cu_count = read_4_bytes (abfd, addr);
5405 addr += 4;
5406
5407 /* local_type_unit_count - The number of TUs in the local TU
5408 list. */
5409 map.tu_count = read_4_bytes (abfd, addr);
5410 addr += 4;
5411
5412 /* foreign_type_unit_count - The number of TUs in the foreign TU
5413 list. */
5414 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5415 addr += 4;
5416 if (foreign_tu_count != 0)
5417 {
5418 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5419 "ignoring .debug_names."),
5420 filename, static_cast<unsigned long> (foreign_tu_count));
5421 return false;
5422 }
5423
5424 /* bucket_count - The number of hash buckets in the hash lookup
5425 table. */
5426 map.bucket_count = read_4_bytes (abfd, addr);
5427 addr += 4;
5428
5429 /* name_count - The number of unique names in the index. */
5430 map.name_count = read_4_bytes (abfd, addr);
5431 addr += 4;
5432
5433 /* abbrev_table_size - The size in bytes of the abbreviations
5434 table. */
5435 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5436 addr += 4;
5437
5438 /* augmentation_string_size - The size in bytes of the augmentation
5439 string. This value is rounded up to a multiple of 4. */
5440 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5441 addr += 4;
5442 map.augmentation_is_gdb = ((augmentation_string_size
5443 == sizeof (dwarf5_augmentation))
5444 && memcmp (addr, dwarf5_augmentation,
5445 sizeof (dwarf5_augmentation)) == 0);
5446 augmentation_string_size += (-augmentation_string_size) & 3;
5447 addr += augmentation_string_size;
5448
5449 /* List of CUs */
5450 map.cu_table_reordered = addr;
5451 addr += map.cu_count * map.offset_size;
5452
5453 /* List of Local TUs */
5454 map.tu_table_reordered = addr;
5455 addr += map.tu_count * map.offset_size;
5456
5457 /* Hash Lookup Table */
5458 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5459 addr += map.bucket_count * 4;
5460 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5461 addr += map.name_count * 4;
5462
5463 /* Name Table */
5464 map.name_table_string_offs_reordered = addr;
5465 addr += map.name_count * map.offset_size;
5466 map.name_table_entry_offs_reordered = addr;
5467 addr += map.name_count * map.offset_size;
5468
5469 const gdb_byte *abbrev_table_start = addr;
5470 for (;;)
5471 {
5472 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5473 addr += bytes_read;
5474 if (index_num == 0)
5475 break;
5476
5477 const auto insertpair
5478 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5479 if (!insertpair.second)
5480 {
5481 warning (_("Section .debug_names in %s has duplicate index %s, "
5482 "ignoring .debug_names."),
5483 filename, pulongest (index_num));
5484 return false;
5485 }
5486 mapped_debug_names::index_val &indexval = insertpair.first->second;
5487 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5488 addr += bytes_read;
5489
5490 for (;;)
5491 {
5492 mapped_debug_names::index_val::attr attr;
5493 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5494 addr += bytes_read;
5495 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5496 addr += bytes_read;
5497 if (attr.form == DW_FORM_implicit_const)
5498 {
5499 attr.implicit_const = read_signed_leb128 (abfd, addr,
5500 &bytes_read);
5501 addr += bytes_read;
5502 }
5503 if (attr.dw_idx == 0 && attr.form == 0)
5504 break;
5505 indexval.attr_vec.push_back (std::move (attr));
5506 }
5507 }
5508 if (addr != abbrev_table_start + abbrev_table_size)
5509 {
5510 warning (_("Section .debug_names in %s has abbreviation_table "
5511 "of size %s vs. written as %u, ignoring .debug_names."),
5512 filename, plongest (addr - abbrev_table_start),
5513 abbrev_table_size);
5514 return false;
5515 }
5516 map.entry_pool = addr;
5517
5518 return true;
5519 }
5520
5521 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5522 list. */
5523
5524 static void
5525 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5526 const mapped_debug_names &map,
5527 dwarf2_section_info &section,
5528 bool is_dwz)
5529 {
5530 sect_offset sect_off_prev;
5531 for (uint32_t i = 0; i <= map.cu_count; ++i)
5532 {
5533 sect_offset sect_off_next;
5534 if (i < map.cu_count)
5535 {
5536 sect_off_next
5537 = (sect_offset) (extract_unsigned_integer
5538 (map.cu_table_reordered + i * map.offset_size,
5539 map.offset_size,
5540 map.dwarf5_byte_order));
5541 }
5542 else
5543 sect_off_next = (sect_offset) section.size;
5544 if (i >= 1)
5545 {
5546 const ULONGEST length = sect_off_next - sect_off_prev;
5547 dwarf2_per_cu_data *per_cu
5548 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5549 sect_off_prev, length);
5550 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5551 }
5552 sect_off_prev = sect_off_next;
5553 }
5554 }
5555
5556 /* Read the CU list from the mapped index, and use it to create all
5557 the CU objects for this dwarf2_per_objfile. */
5558
5559 static void
5560 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5561 const mapped_debug_names &map,
5562 const mapped_debug_names &dwz_map)
5563 {
5564 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5565 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5566
5567 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5568 dwarf2_per_objfile->info,
5569 false /* is_dwz */);
5570
5571 if (dwz_map.cu_count == 0)
5572 return;
5573
5574 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5575 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5576 true /* is_dwz */);
5577 }
5578
5579 /* Read .debug_names. If everything went ok, initialize the "quick"
5580 elements of all the CUs and return true. Otherwise, return false. */
5581
5582 static bool
5583 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5584 {
5585 std::unique_ptr<mapped_debug_names> map
5586 (new mapped_debug_names (dwarf2_per_objfile));
5587 mapped_debug_names dwz_map (dwarf2_per_objfile);
5588 struct objfile *objfile = dwarf2_per_objfile->objfile;
5589
5590 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5591 &dwarf2_per_objfile->debug_names,
5592 *map))
5593 return false;
5594
5595 /* Don't use the index if it's empty. */
5596 if (map->name_count == 0)
5597 return false;
5598
5599 /* If there is a .dwz file, read it so we can get its CU list as
5600 well. */
5601 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5602 if (dwz != NULL)
5603 {
5604 if (!read_debug_names_from_section (objfile,
5605 bfd_get_filename (dwz->dwz_bfd.get ()),
5606 &dwz->debug_names, dwz_map))
5607 {
5608 warning (_("could not read '.debug_names' section from %s; skipping"),
5609 bfd_get_filename (dwz->dwz_bfd.get ()));
5610 return false;
5611 }
5612 }
5613
5614 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5615
5616 if (map->tu_count != 0)
5617 {
5618 /* We can only handle a single .debug_types when we have an
5619 index. */
5620 if (dwarf2_per_objfile->types.size () != 1)
5621 return false;
5622
5623 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5624
5625 create_signatured_type_table_from_debug_names
5626 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5627 }
5628
5629 create_addrmap_from_aranges (dwarf2_per_objfile,
5630 &dwarf2_per_objfile->debug_aranges);
5631
5632 dwarf2_per_objfile->debug_names_table = std::move (map);
5633 dwarf2_per_objfile->using_index = 1;
5634 dwarf2_per_objfile->quick_file_names_table =
5635 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5636
5637 return true;
5638 }
5639
5640 /* Type used to manage iterating over all CUs looking for a symbol for
5641 .debug_names. */
5642
5643 class dw2_debug_names_iterator
5644 {
5645 public:
5646 dw2_debug_names_iterator (const mapped_debug_names &map,
5647 gdb::optional<block_enum> block_index,
5648 domain_enum domain,
5649 const char *name)
5650 : m_map (map), m_block_index (block_index), m_domain (domain),
5651 m_addr (find_vec_in_debug_names (map, name))
5652 {}
5653
5654 dw2_debug_names_iterator (const mapped_debug_names &map,
5655 search_domain search, uint32_t namei)
5656 : m_map (map),
5657 m_search (search),
5658 m_addr (find_vec_in_debug_names (map, namei))
5659 {}
5660
5661 dw2_debug_names_iterator (const mapped_debug_names &map,
5662 block_enum block_index, domain_enum domain,
5663 uint32_t namei)
5664 : m_map (map), m_block_index (block_index), m_domain (domain),
5665 m_addr (find_vec_in_debug_names (map, namei))
5666 {}
5667
5668 /* Return the next matching CU or NULL if there are no more. */
5669 dwarf2_per_cu_data *next ();
5670
5671 private:
5672 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5673 const char *name);
5674 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5675 uint32_t namei);
5676
5677 /* The internalized form of .debug_names. */
5678 const mapped_debug_names &m_map;
5679
5680 /* If set, only look for symbols that match that block. Valid values are
5681 GLOBAL_BLOCK and STATIC_BLOCK. */
5682 const gdb::optional<block_enum> m_block_index;
5683
5684 /* The kind of symbol we're looking for. */
5685 const domain_enum m_domain = UNDEF_DOMAIN;
5686 const search_domain m_search = ALL_DOMAIN;
5687
5688 /* The list of CUs from the index entry of the symbol, or NULL if
5689 not found. */
5690 const gdb_byte *m_addr;
5691 };
5692
5693 const char *
5694 mapped_debug_names::namei_to_name (uint32_t namei) const
5695 {
5696 const ULONGEST namei_string_offs
5697 = extract_unsigned_integer ((name_table_string_offs_reordered
5698 + namei * offset_size),
5699 offset_size,
5700 dwarf5_byte_order);
5701 return read_indirect_string_at_offset
5702 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5703 }
5704
5705 /* Find a slot in .debug_names for the object named NAME. If NAME is
5706 found, return pointer to its pool data. If NAME cannot be found,
5707 return NULL. */
5708
5709 const gdb_byte *
5710 dw2_debug_names_iterator::find_vec_in_debug_names
5711 (const mapped_debug_names &map, const char *name)
5712 {
5713 int (*cmp) (const char *, const char *);
5714
5715 gdb::unique_xmalloc_ptr<char> without_params;
5716 if (current_language->la_language == language_cplus
5717 || current_language->la_language == language_fortran
5718 || current_language->la_language == language_d)
5719 {
5720 /* NAME is already canonical. Drop any qualifiers as
5721 .debug_names does not contain any. */
5722
5723 if (strchr (name, '(') != NULL)
5724 {
5725 without_params = cp_remove_params (name);
5726 if (without_params != NULL)
5727 name = without_params.get ();
5728 }
5729 }
5730
5731 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5732
5733 const uint32_t full_hash = dwarf5_djb_hash (name);
5734 uint32_t namei
5735 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5736 (map.bucket_table_reordered
5737 + (full_hash % map.bucket_count)), 4,
5738 map.dwarf5_byte_order);
5739 if (namei == 0)
5740 return NULL;
5741 --namei;
5742 if (namei >= map.name_count)
5743 {
5744 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5745 "[in module %s]"),
5746 namei, map.name_count,
5747 objfile_name (map.dwarf2_per_objfile->objfile));
5748 return NULL;
5749 }
5750
5751 for (;;)
5752 {
5753 const uint32_t namei_full_hash
5754 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5755 (map.hash_table_reordered + namei), 4,
5756 map.dwarf5_byte_order);
5757 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5758 return NULL;
5759
5760 if (full_hash == namei_full_hash)
5761 {
5762 const char *const namei_string = map.namei_to_name (namei);
5763
5764 #if 0 /* An expensive sanity check. */
5765 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5766 {
5767 complaint (_("Wrong .debug_names hash for string at index %u "
5768 "[in module %s]"),
5769 namei, objfile_name (dwarf2_per_objfile->objfile));
5770 return NULL;
5771 }
5772 #endif
5773
5774 if (cmp (namei_string, name) == 0)
5775 {
5776 const ULONGEST namei_entry_offs
5777 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5778 + namei * map.offset_size),
5779 map.offset_size, map.dwarf5_byte_order);
5780 return map.entry_pool + namei_entry_offs;
5781 }
5782 }
5783
5784 ++namei;
5785 if (namei >= map.name_count)
5786 return NULL;
5787 }
5788 }
5789
5790 const gdb_byte *
5791 dw2_debug_names_iterator::find_vec_in_debug_names
5792 (const mapped_debug_names &map, uint32_t namei)
5793 {
5794 if (namei >= map.name_count)
5795 {
5796 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5797 "[in module %s]"),
5798 namei, map.name_count,
5799 objfile_name (map.dwarf2_per_objfile->objfile));
5800 return NULL;
5801 }
5802
5803 const ULONGEST namei_entry_offs
5804 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5805 + namei * map.offset_size),
5806 map.offset_size, map.dwarf5_byte_order);
5807 return map.entry_pool + namei_entry_offs;
5808 }
5809
5810 /* See dw2_debug_names_iterator. */
5811
5812 dwarf2_per_cu_data *
5813 dw2_debug_names_iterator::next ()
5814 {
5815 if (m_addr == NULL)
5816 return NULL;
5817
5818 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5819 struct objfile *objfile = dwarf2_per_objfile->objfile;
5820 bfd *const abfd = objfile->obfd;
5821
5822 again:
5823
5824 unsigned int bytes_read;
5825 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5826 m_addr += bytes_read;
5827 if (abbrev == 0)
5828 return NULL;
5829
5830 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5831 if (indexval_it == m_map.abbrev_map.cend ())
5832 {
5833 complaint (_("Wrong .debug_names undefined abbrev code %s "
5834 "[in module %s]"),
5835 pulongest (abbrev), objfile_name (objfile));
5836 return NULL;
5837 }
5838 const mapped_debug_names::index_val &indexval = indexval_it->second;
5839 enum class symbol_linkage {
5840 unknown,
5841 static_,
5842 extern_,
5843 } symbol_linkage_ = symbol_linkage::unknown;
5844 dwarf2_per_cu_data *per_cu = NULL;
5845 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5846 {
5847 ULONGEST ull;
5848 switch (attr.form)
5849 {
5850 case DW_FORM_implicit_const:
5851 ull = attr.implicit_const;
5852 break;
5853 case DW_FORM_flag_present:
5854 ull = 1;
5855 break;
5856 case DW_FORM_udata:
5857 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5858 m_addr += bytes_read;
5859 break;
5860 default:
5861 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5862 dwarf_form_name (attr.form),
5863 objfile_name (objfile));
5864 return NULL;
5865 }
5866 switch (attr.dw_idx)
5867 {
5868 case DW_IDX_compile_unit:
5869 /* Don't crash on bad data. */
5870 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5871 {
5872 complaint (_(".debug_names entry has bad CU index %s"
5873 " [in module %s]"),
5874 pulongest (ull),
5875 objfile_name (dwarf2_per_objfile->objfile));
5876 continue;
5877 }
5878 per_cu = dwarf2_per_objfile->get_cutu (ull);
5879 break;
5880 case DW_IDX_type_unit:
5881 /* Don't crash on bad data. */
5882 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5883 {
5884 complaint (_(".debug_names entry has bad TU index %s"
5885 " [in module %s]"),
5886 pulongest (ull),
5887 objfile_name (dwarf2_per_objfile->objfile));
5888 continue;
5889 }
5890 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5891 break;
5892 case DW_IDX_GNU_internal:
5893 if (!m_map.augmentation_is_gdb)
5894 break;
5895 symbol_linkage_ = symbol_linkage::static_;
5896 break;
5897 case DW_IDX_GNU_external:
5898 if (!m_map.augmentation_is_gdb)
5899 break;
5900 symbol_linkage_ = symbol_linkage::extern_;
5901 break;
5902 }
5903 }
5904
5905 /* Skip if already read in. */
5906 if (per_cu->v.quick->compunit_symtab)
5907 goto again;
5908
5909 /* Check static vs global. */
5910 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5911 {
5912 const bool want_static = *m_block_index == STATIC_BLOCK;
5913 const bool symbol_is_static =
5914 symbol_linkage_ == symbol_linkage::static_;
5915 if (want_static != symbol_is_static)
5916 goto again;
5917 }
5918
5919 /* Match dw2_symtab_iter_next, symbol_kind
5920 and debug_names::psymbol_tag. */
5921 switch (m_domain)
5922 {
5923 case VAR_DOMAIN:
5924 switch (indexval.dwarf_tag)
5925 {
5926 case DW_TAG_variable:
5927 case DW_TAG_subprogram:
5928 /* Some types are also in VAR_DOMAIN. */
5929 case DW_TAG_typedef:
5930 case DW_TAG_structure_type:
5931 break;
5932 default:
5933 goto again;
5934 }
5935 break;
5936 case STRUCT_DOMAIN:
5937 switch (indexval.dwarf_tag)
5938 {
5939 case DW_TAG_typedef:
5940 case DW_TAG_structure_type:
5941 break;
5942 default:
5943 goto again;
5944 }
5945 break;
5946 case LABEL_DOMAIN:
5947 switch (indexval.dwarf_tag)
5948 {
5949 case 0:
5950 case DW_TAG_variable:
5951 break;
5952 default:
5953 goto again;
5954 }
5955 break;
5956 default:
5957 break;
5958 }
5959
5960 /* Match dw2_expand_symtabs_matching, symbol_kind and
5961 debug_names::psymbol_tag. */
5962 switch (m_search)
5963 {
5964 case VARIABLES_DOMAIN:
5965 switch (indexval.dwarf_tag)
5966 {
5967 case DW_TAG_variable:
5968 break;
5969 default:
5970 goto again;
5971 }
5972 break;
5973 case FUNCTIONS_DOMAIN:
5974 switch (indexval.dwarf_tag)
5975 {
5976 case DW_TAG_subprogram:
5977 break;
5978 default:
5979 goto again;
5980 }
5981 break;
5982 case TYPES_DOMAIN:
5983 switch (indexval.dwarf_tag)
5984 {
5985 case DW_TAG_typedef:
5986 case DW_TAG_structure_type:
5987 break;
5988 default:
5989 goto again;
5990 }
5991 break;
5992 default:
5993 break;
5994 }
5995
5996 return per_cu;
5997 }
5998
5999 static struct compunit_symtab *
6000 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
6001 const char *name, domain_enum domain)
6002 {
6003 struct dwarf2_per_objfile *dwarf2_per_objfile
6004 = get_dwarf2_per_objfile (objfile);
6005
6006 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6007 if (!mapp)
6008 {
6009 /* index is NULL if OBJF_READNOW. */
6010 return NULL;
6011 }
6012 const auto &map = *mapp;
6013
6014 dw2_debug_names_iterator iter (map, block_index, domain, name);
6015
6016 struct compunit_symtab *stab_best = NULL;
6017 struct dwarf2_per_cu_data *per_cu;
6018 while ((per_cu = iter.next ()) != NULL)
6019 {
6020 struct symbol *sym, *with_opaque = NULL;
6021 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
6022 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6023 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6024
6025 sym = block_find_symbol (block, name, domain,
6026 block_find_non_opaque_type_preferred,
6027 &with_opaque);
6028
6029 /* Some caution must be observed with overloaded functions and
6030 methods, since the index will not contain any overload
6031 information (but NAME might contain it). */
6032
6033 if (sym != NULL
6034 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6035 return stab;
6036 if (with_opaque != NULL
6037 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6038 stab_best = stab;
6039
6040 /* Keep looking through other CUs. */
6041 }
6042
6043 return stab_best;
6044 }
6045
6046 /* This dumps minimal information about .debug_names. It is called
6047 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6048 uses this to verify that .debug_names has been loaded. */
6049
6050 static void
6051 dw2_debug_names_dump (struct objfile *objfile)
6052 {
6053 struct dwarf2_per_objfile *dwarf2_per_objfile
6054 = get_dwarf2_per_objfile (objfile);
6055
6056 gdb_assert (dwarf2_per_objfile->using_index);
6057 printf_filtered (".debug_names:");
6058 if (dwarf2_per_objfile->debug_names_table)
6059 printf_filtered (" exists\n");
6060 else
6061 printf_filtered (" faked for \"readnow\"\n");
6062 printf_filtered ("\n");
6063 }
6064
6065 static void
6066 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6067 const char *func_name)
6068 {
6069 struct dwarf2_per_objfile *dwarf2_per_objfile
6070 = get_dwarf2_per_objfile (objfile);
6071
6072 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6073 if (dwarf2_per_objfile->debug_names_table)
6074 {
6075 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6076
6077 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
6078
6079 struct dwarf2_per_cu_data *per_cu;
6080 while ((per_cu = iter.next ()) != NULL)
6081 dw2_instantiate_symtab (per_cu, false);
6082 }
6083 }
6084
6085 static void
6086 dw2_debug_names_map_matching_symbols
6087 (struct objfile *objfile,
6088 const lookup_name_info &name, domain_enum domain,
6089 int global,
6090 gdb::function_view<symbol_found_callback_ftype> callback,
6091 symbol_compare_ftype *ordered_compare)
6092 {
6093 struct dwarf2_per_objfile *dwarf2_per_objfile
6094 = get_dwarf2_per_objfile (objfile);
6095
6096 /* debug_names_table is NULL if OBJF_READNOW. */
6097 if (!dwarf2_per_objfile->debug_names_table)
6098 return;
6099
6100 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6101 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6102
6103 const char *match_name = name.ada ().lookup_name ().c_str ();
6104 auto matcher = [&] (const char *symname)
6105 {
6106 if (ordered_compare == nullptr)
6107 return true;
6108 return ordered_compare (symname, match_name) == 0;
6109 };
6110
6111 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6112 [&] (offset_type namei)
6113 {
6114 /* The name was matched, now expand corresponding CUs that were
6115 marked. */
6116 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6117
6118 struct dwarf2_per_cu_data *per_cu;
6119 while ((per_cu = iter.next ()) != NULL)
6120 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6121 return true;
6122 });
6123
6124 /* It's a shame we couldn't do this inside the
6125 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6126 that have already been expanded. Instead, this loop matches what
6127 the psymtab code does. */
6128 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6129 {
6130 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6131 if (cust != nullptr)
6132 {
6133 const struct block *block
6134 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6135 if (!iterate_over_symbols_terminated (block, name,
6136 domain, callback))
6137 break;
6138 }
6139 }
6140 }
6141
6142 static void
6143 dw2_debug_names_expand_symtabs_matching
6144 (struct objfile *objfile,
6145 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6146 const lookup_name_info &lookup_name,
6147 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6148 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6149 enum search_domain kind)
6150 {
6151 struct dwarf2_per_objfile *dwarf2_per_objfile
6152 = get_dwarf2_per_objfile (objfile);
6153
6154 /* debug_names_table is NULL if OBJF_READNOW. */
6155 if (!dwarf2_per_objfile->debug_names_table)
6156 return;
6157
6158 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6159
6160 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6161
6162 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6163 symbol_matcher,
6164 kind, [&] (offset_type namei)
6165 {
6166 /* The name was matched, now expand corresponding CUs that were
6167 marked. */
6168 dw2_debug_names_iterator iter (map, kind, namei);
6169
6170 struct dwarf2_per_cu_data *per_cu;
6171 while ((per_cu = iter.next ()) != NULL)
6172 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6173 expansion_notify);
6174 return true;
6175 });
6176 }
6177
6178 const struct quick_symbol_functions dwarf2_debug_names_functions =
6179 {
6180 dw2_has_symbols,
6181 dw2_find_last_source_symtab,
6182 dw2_forget_cached_source_info,
6183 dw2_map_symtabs_matching_filename,
6184 dw2_debug_names_lookup_symbol,
6185 dw2_print_stats,
6186 dw2_debug_names_dump,
6187 dw2_debug_names_expand_symtabs_for_function,
6188 dw2_expand_all_symtabs,
6189 dw2_expand_symtabs_with_fullname,
6190 dw2_debug_names_map_matching_symbols,
6191 dw2_debug_names_expand_symtabs_matching,
6192 dw2_find_pc_sect_compunit_symtab,
6193 NULL,
6194 dw2_map_symbol_filenames
6195 };
6196
6197 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6198 to either a dwarf2_per_objfile or dwz_file object. */
6199
6200 template <typename T>
6201 static gdb::array_view<const gdb_byte>
6202 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6203 {
6204 dwarf2_section_info *section = &section_owner->gdb_index;
6205
6206 if (dwarf2_section_empty_p (section))
6207 return {};
6208
6209 /* Older elfutils strip versions could keep the section in the main
6210 executable while splitting it for the separate debug info file. */
6211 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6212 return {};
6213
6214 dwarf2_read_section (obj, section);
6215
6216 /* dwarf2_section_info::size is a bfd_size_type, while
6217 gdb::array_view works with size_t. On 32-bit hosts, with
6218 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6219 is 32-bit. So we need an explicit narrowing conversion here.
6220 This is fine, because it's impossible to allocate or mmap an
6221 array/buffer larger than what size_t can represent. */
6222 return gdb::make_array_view (section->buffer, section->size);
6223 }
6224
6225 /* Lookup the index cache for the contents of the index associated to
6226 DWARF2_OBJ. */
6227
6228 static gdb::array_view<const gdb_byte>
6229 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6230 {
6231 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6232 if (build_id == nullptr)
6233 return {};
6234
6235 return global_index_cache.lookup_gdb_index (build_id,
6236 &dwarf2_obj->index_cache_res);
6237 }
6238
6239 /* Same as the above, but for DWZ. */
6240
6241 static gdb::array_view<const gdb_byte>
6242 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6243 {
6244 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6245 if (build_id == nullptr)
6246 return {};
6247
6248 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6249 }
6250
6251 /* See symfile.h. */
6252
6253 bool
6254 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6255 {
6256 struct dwarf2_per_objfile *dwarf2_per_objfile
6257 = get_dwarf2_per_objfile (objfile);
6258
6259 /* If we're about to read full symbols, don't bother with the
6260 indices. In this case we also don't care if some other debug
6261 format is making psymtabs, because they are all about to be
6262 expanded anyway. */
6263 if ((objfile->flags & OBJF_READNOW))
6264 {
6265 dwarf2_per_objfile->using_index = 1;
6266 create_all_comp_units (dwarf2_per_objfile);
6267 create_all_type_units (dwarf2_per_objfile);
6268 dwarf2_per_objfile->quick_file_names_table
6269 = create_quick_file_names_table
6270 (dwarf2_per_objfile->all_comp_units.size ());
6271
6272 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6273 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6274 {
6275 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6276
6277 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6278 struct dwarf2_per_cu_quick_data);
6279 }
6280
6281 /* Return 1 so that gdb sees the "quick" functions. However,
6282 these functions will be no-ops because we will have expanded
6283 all symtabs. */
6284 *index_kind = dw_index_kind::GDB_INDEX;
6285 return true;
6286 }
6287
6288 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6289 {
6290 *index_kind = dw_index_kind::DEBUG_NAMES;
6291 return true;
6292 }
6293
6294 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6295 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6296 get_gdb_index_contents_from_section<dwz_file>))
6297 {
6298 *index_kind = dw_index_kind::GDB_INDEX;
6299 return true;
6300 }
6301
6302 /* ... otherwise, try to find the index in the index cache. */
6303 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6304 get_gdb_index_contents_from_cache,
6305 get_gdb_index_contents_from_cache_dwz))
6306 {
6307 global_index_cache.hit ();
6308 *index_kind = dw_index_kind::GDB_INDEX;
6309 return true;
6310 }
6311
6312 global_index_cache.miss ();
6313 return false;
6314 }
6315
6316 \f
6317
6318 /* Build a partial symbol table. */
6319
6320 void
6321 dwarf2_build_psymtabs (struct objfile *objfile)
6322 {
6323 struct dwarf2_per_objfile *dwarf2_per_objfile
6324 = get_dwarf2_per_objfile (objfile);
6325
6326 init_psymbol_list (objfile, 1024);
6327
6328 try
6329 {
6330 /* This isn't really ideal: all the data we allocate on the
6331 objfile's obstack is still uselessly kept around. However,
6332 freeing it seems unsafe. */
6333 psymtab_discarder psymtabs (objfile);
6334 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6335 psymtabs.keep ();
6336
6337 /* (maybe) store an index in the cache. */
6338 global_index_cache.store (dwarf2_per_objfile);
6339 }
6340 catch (const gdb_exception_error &except)
6341 {
6342 exception_print (gdb_stderr, except);
6343 }
6344 }
6345
6346 /* Return the total length of the CU described by HEADER. */
6347
6348 static unsigned int
6349 get_cu_length (const struct comp_unit_head *header)
6350 {
6351 return header->initial_length_size + header->length;
6352 }
6353
6354 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6355
6356 static inline bool
6357 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6358 {
6359 sect_offset bottom = cu_header->sect_off;
6360 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6361
6362 return sect_off >= bottom && sect_off < top;
6363 }
6364
6365 /* Find the base address of the compilation unit for range lists and
6366 location lists. It will normally be specified by DW_AT_low_pc.
6367 In DWARF-3 draft 4, the base address could be overridden by
6368 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6369 compilation units with discontinuous ranges. */
6370
6371 static void
6372 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6373 {
6374 struct attribute *attr;
6375
6376 cu->base_known = 0;
6377 cu->base_address = 0;
6378
6379 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6380 if (attr)
6381 {
6382 cu->base_address = attr_value_as_address (attr);
6383 cu->base_known = 1;
6384 }
6385 else
6386 {
6387 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6388 if (attr)
6389 {
6390 cu->base_address = attr_value_as_address (attr);
6391 cu->base_known = 1;
6392 }
6393 }
6394 }
6395
6396 /* Read in the comp unit header information from the debug_info at info_ptr.
6397 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6398 NOTE: This leaves members offset, first_die_offset to be filled in
6399 by the caller. */
6400
6401 static const gdb_byte *
6402 read_comp_unit_head (struct comp_unit_head *cu_header,
6403 const gdb_byte *info_ptr,
6404 struct dwarf2_section_info *section,
6405 rcuh_kind section_kind)
6406 {
6407 int signed_addr;
6408 unsigned int bytes_read;
6409 const char *filename = get_section_file_name (section);
6410 bfd *abfd = get_section_bfd_owner (section);
6411
6412 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6413 cu_header->initial_length_size = bytes_read;
6414 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6415 info_ptr += bytes_read;
6416 cu_header->version = read_2_bytes (abfd, info_ptr);
6417 if (cu_header->version < 2 || cu_header->version > 5)
6418 error (_("Dwarf Error: wrong version in compilation unit header "
6419 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6420 cu_header->version, filename);
6421 info_ptr += 2;
6422 if (cu_header->version < 5)
6423 switch (section_kind)
6424 {
6425 case rcuh_kind::COMPILE:
6426 cu_header->unit_type = DW_UT_compile;
6427 break;
6428 case rcuh_kind::TYPE:
6429 cu_header->unit_type = DW_UT_type;
6430 break;
6431 default:
6432 internal_error (__FILE__, __LINE__,
6433 _("read_comp_unit_head: invalid section_kind"));
6434 }
6435 else
6436 {
6437 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6438 (read_1_byte (abfd, info_ptr));
6439 info_ptr += 1;
6440 switch (cu_header->unit_type)
6441 {
6442 case DW_UT_compile:
6443 case DW_UT_partial:
6444 case DW_UT_skeleton:
6445 case DW_UT_split_compile:
6446 if (section_kind != rcuh_kind::COMPILE)
6447 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6448 "(is %s, should be %s) [in module %s]"),
6449 dwarf_unit_type_name (cu_header->unit_type),
6450 dwarf_unit_type_name (DW_UT_type), filename);
6451 break;
6452 case DW_UT_type:
6453 case DW_UT_split_type:
6454 section_kind = rcuh_kind::TYPE;
6455 break;
6456 default:
6457 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6458 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6459 "[in module %s]"), cu_header->unit_type,
6460 dwarf_unit_type_name (DW_UT_compile),
6461 dwarf_unit_type_name (DW_UT_skeleton),
6462 dwarf_unit_type_name (DW_UT_split_compile),
6463 dwarf_unit_type_name (DW_UT_type),
6464 dwarf_unit_type_name (DW_UT_split_type), filename);
6465 }
6466
6467 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6468 info_ptr += 1;
6469 }
6470 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6471 cu_header,
6472 &bytes_read);
6473 info_ptr += bytes_read;
6474 if (cu_header->version < 5)
6475 {
6476 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6477 info_ptr += 1;
6478 }
6479 signed_addr = bfd_get_sign_extend_vma (abfd);
6480 if (signed_addr < 0)
6481 internal_error (__FILE__, __LINE__,
6482 _("read_comp_unit_head: dwarf from non elf file"));
6483 cu_header->signed_addr_p = signed_addr;
6484
6485 bool header_has_signature = section_kind == rcuh_kind::TYPE
6486 || cu_header->unit_type == DW_UT_skeleton
6487 || cu_header->unit_type == DW_UT_split_compile;
6488
6489 if (header_has_signature)
6490 {
6491 cu_header->signature = read_8_bytes (abfd, info_ptr);
6492 info_ptr += 8;
6493 }
6494
6495 if (section_kind == rcuh_kind::TYPE)
6496 {
6497 LONGEST type_offset;
6498 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6499 info_ptr += bytes_read;
6500 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6501 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6502 error (_("Dwarf Error: Too big type_offset in compilation unit "
6503 "header (is %s) [in module %s]"), plongest (type_offset),
6504 filename);
6505 }
6506
6507 return info_ptr;
6508 }
6509
6510 /* Helper function that returns the proper abbrev section for
6511 THIS_CU. */
6512
6513 static struct dwarf2_section_info *
6514 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6515 {
6516 struct dwarf2_section_info *abbrev;
6517 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6518
6519 if (this_cu->is_dwz)
6520 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6521 else
6522 abbrev = &dwarf2_per_objfile->abbrev;
6523
6524 return abbrev;
6525 }
6526
6527 /* Subroutine of read_and_check_comp_unit_head and
6528 read_and_check_type_unit_head to simplify them.
6529 Perform various error checking on the header. */
6530
6531 static void
6532 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6533 struct comp_unit_head *header,
6534 struct dwarf2_section_info *section,
6535 struct dwarf2_section_info *abbrev_section)
6536 {
6537 const char *filename = get_section_file_name (section);
6538
6539 if (to_underlying (header->abbrev_sect_off)
6540 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6541 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6542 "(offset %s + 6) [in module %s]"),
6543 sect_offset_str (header->abbrev_sect_off),
6544 sect_offset_str (header->sect_off),
6545 filename);
6546
6547 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6548 avoid potential 32-bit overflow. */
6549 if (((ULONGEST) header->sect_off + get_cu_length (header))
6550 > section->size)
6551 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6552 "(offset %s + 0) [in module %s]"),
6553 header->length, sect_offset_str (header->sect_off),
6554 filename);
6555 }
6556
6557 /* Read in a CU/TU header and perform some basic error checking.
6558 The contents of the header are stored in HEADER.
6559 The result is a pointer to the start of the first DIE. */
6560
6561 static const gdb_byte *
6562 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6563 struct comp_unit_head *header,
6564 struct dwarf2_section_info *section,
6565 struct dwarf2_section_info *abbrev_section,
6566 const gdb_byte *info_ptr,
6567 rcuh_kind section_kind)
6568 {
6569 const gdb_byte *beg_of_comp_unit = info_ptr;
6570
6571 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6572
6573 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6574
6575 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6576
6577 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6578 abbrev_section);
6579
6580 return info_ptr;
6581 }
6582
6583 /* Fetch the abbreviation table offset from a comp or type unit header. */
6584
6585 static sect_offset
6586 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6587 struct dwarf2_section_info *section,
6588 sect_offset sect_off)
6589 {
6590 bfd *abfd = get_section_bfd_owner (section);
6591 const gdb_byte *info_ptr;
6592 unsigned int initial_length_size, offset_size;
6593 uint16_t version;
6594
6595 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6596 info_ptr = section->buffer + to_underlying (sect_off);
6597 read_initial_length (abfd, info_ptr, &initial_length_size);
6598 offset_size = initial_length_size == 4 ? 4 : 8;
6599 info_ptr += initial_length_size;
6600
6601 version = read_2_bytes (abfd, info_ptr);
6602 info_ptr += 2;
6603 if (version >= 5)
6604 {
6605 /* Skip unit type and address size. */
6606 info_ptr += 2;
6607 }
6608
6609 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6610 }
6611
6612 /* Allocate a new partial symtab for file named NAME and mark this new
6613 partial symtab as being an include of PST. */
6614
6615 static void
6616 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6617 struct objfile *objfile)
6618 {
6619 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6620
6621 if (!IS_ABSOLUTE_PATH (subpst->filename))
6622 {
6623 /* It shares objfile->objfile_obstack. */
6624 subpst->dirname = pst->dirname;
6625 }
6626
6627 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6628 subpst->dependencies[0] = pst;
6629 subpst->number_of_dependencies = 1;
6630
6631 subpst->read_symtab = pst->read_symtab;
6632
6633 /* No private part is necessary for include psymtabs. This property
6634 can be used to differentiate between such include psymtabs and
6635 the regular ones. */
6636 subpst->read_symtab_private = NULL;
6637 }
6638
6639 /* Read the Line Number Program data and extract the list of files
6640 included by the source file represented by PST. Build an include
6641 partial symtab for each of these included files. */
6642
6643 static void
6644 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6645 struct die_info *die,
6646 struct partial_symtab *pst)
6647 {
6648 line_header_up lh;
6649 struct attribute *attr;
6650
6651 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6652 if (attr)
6653 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6654 if (lh == NULL)
6655 return; /* No linetable, so no includes. */
6656
6657 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6658 that we pass in the raw text_low here; that is ok because we're
6659 only decoding the line table to make include partial symtabs, and
6660 so the addresses aren't really used. */
6661 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6662 pst->raw_text_low (), 1);
6663 }
6664
6665 static hashval_t
6666 hash_signatured_type (const void *item)
6667 {
6668 const struct signatured_type *sig_type
6669 = (const struct signatured_type *) item;
6670
6671 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6672 return sig_type->signature;
6673 }
6674
6675 static int
6676 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6677 {
6678 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6679 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6680
6681 return lhs->signature == rhs->signature;
6682 }
6683
6684 /* Allocate a hash table for signatured types. */
6685
6686 static htab_t
6687 allocate_signatured_type_table (struct objfile *objfile)
6688 {
6689 return htab_create_alloc_ex (41,
6690 hash_signatured_type,
6691 eq_signatured_type,
6692 NULL,
6693 &objfile->objfile_obstack,
6694 hashtab_obstack_allocate,
6695 dummy_obstack_deallocate);
6696 }
6697
6698 /* A helper function to add a signatured type CU to a table. */
6699
6700 static int
6701 add_signatured_type_cu_to_table (void **slot, void *datum)
6702 {
6703 struct signatured_type *sigt = (struct signatured_type *) *slot;
6704 std::vector<signatured_type *> *all_type_units
6705 = (std::vector<signatured_type *> *) datum;
6706
6707 all_type_units->push_back (sigt);
6708
6709 return 1;
6710 }
6711
6712 /* A helper for create_debug_types_hash_table. Read types from SECTION
6713 and fill them into TYPES_HTAB. It will process only type units,
6714 therefore DW_UT_type. */
6715
6716 static void
6717 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6718 struct dwo_file *dwo_file,
6719 dwarf2_section_info *section, htab_t &types_htab,
6720 rcuh_kind section_kind)
6721 {
6722 struct objfile *objfile = dwarf2_per_objfile->objfile;
6723 struct dwarf2_section_info *abbrev_section;
6724 bfd *abfd;
6725 const gdb_byte *info_ptr, *end_ptr;
6726
6727 abbrev_section = (dwo_file != NULL
6728 ? &dwo_file->sections.abbrev
6729 : &dwarf2_per_objfile->abbrev);
6730
6731 if (dwarf_read_debug)
6732 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6733 get_section_name (section),
6734 get_section_file_name (abbrev_section));
6735
6736 dwarf2_read_section (objfile, section);
6737 info_ptr = section->buffer;
6738
6739 if (info_ptr == NULL)
6740 return;
6741
6742 /* We can't set abfd until now because the section may be empty or
6743 not present, in which case the bfd is unknown. */
6744 abfd = get_section_bfd_owner (section);
6745
6746 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6747 because we don't need to read any dies: the signature is in the
6748 header. */
6749
6750 end_ptr = info_ptr + section->size;
6751 while (info_ptr < end_ptr)
6752 {
6753 struct signatured_type *sig_type;
6754 struct dwo_unit *dwo_tu;
6755 void **slot;
6756 const gdb_byte *ptr = info_ptr;
6757 struct comp_unit_head header;
6758 unsigned int length;
6759
6760 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6761
6762 /* Initialize it due to a false compiler warning. */
6763 header.signature = -1;
6764 header.type_cu_offset_in_tu = (cu_offset) -1;
6765
6766 /* We need to read the type's signature in order to build the hash
6767 table, but we don't need anything else just yet. */
6768
6769 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6770 abbrev_section, ptr, section_kind);
6771
6772 length = get_cu_length (&header);
6773
6774 /* Skip dummy type units. */
6775 if (ptr >= info_ptr + length
6776 || peek_abbrev_code (abfd, ptr) == 0
6777 || header.unit_type != DW_UT_type)
6778 {
6779 info_ptr += length;
6780 continue;
6781 }
6782
6783 if (types_htab == NULL)
6784 {
6785 if (dwo_file)
6786 types_htab = allocate_dwo_unit_table (objfile);
6787 else
6788 types_htab = allocate_signatured_type_table (objfile);
6789 }
6790
6791 if (dwo_file)
6792 {
6793 sig_type = NULL;
6794 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6795 struct dwo_unit);
6796 dwo_tu->dwo_file = dwo_file;
6797 dwo_tu->signature = header.signature;
6798 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6799 dwo_tu->section = section;
6800 dwo_tu->sect_off = sect_off;
6801 dwo_tu->length = length;
6802 }
6803 else
6804 {
6805 /* N.B.: type_offset is not usable if this type uses a DWO file.
6806 The real type_offset is in the DWO file. */
6807 dwo_tu = NULL;
6808 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6809 struct signatured_type);
6810 sig_type->signature = header.signature;
6811 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6812 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6813 sig_type->per_cu.is_debug_types = 1;
6814 sig_type->per_cu.section = section;
6815 sig_type->per_cu.sect_off = sect_off;
6816 sig_type->per_cu.length = length;
6817 }
6818
6819 slot = htab_find_slot (types_htab,
6820 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6821 INSERT);
6822 gdb_assert (slot != NULL);
6823 if (*slot != NULL)
6824 {
6825 sect_offset dup_sect_off;
6826
6827 if (dwo_file)
6828 {
6829 const struct dwo_unit *dup_tu
6830 = (const struct dwo_unit *) *slot;
6831
6832 dup_sect_off = dup_tu->sect_off;
6833 }
6834 else
6835 {
6836 const struct signatured_type *dup_tu
6837 = (const struct signatured_type *) *slot;
6838
6839 dup_sect_off = dup_tu->per_cu.sect_off;
6840 }
6841
6842 complaint (_("debug type entry at offset %s is duplicate to"
6843 " the entry at offset %s, signature %s"),
6844 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6845 hex_string (header.signature));
6846 }
6847 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6848
6849 if (dwarf_read_debug > 1)
6850 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6851 sect_offset_str (sect_off),
6852 hex_string (header.signature));
6853
6854 info_ptr += length;
6855 }
6856 }
6857
6858 /* Create the hash table of all entries in the .debug_types
6859 (or .debug_types.dwo) section(s).
6860 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6861 otherwise it is NULL.
6862
6863 The result is a pointer to the hash table or NULL if there are no types.
6864
6865 Note: This function processes DWO files only, not DWP files. */
6866
6867 static void
6868 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6869 struct dwo_file *dwo_file,
6870 gdb::array_view<dwarf2_section_info> type_sections,
6871 htab_t &types_htab)
6872 {
6873 for (dwarf2_section_info &section : type_sections)
6874 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6875 types_htab, rcuh_kind::TYPE);
6876 }
6877
6878 /* Create the hash table of all entries in the .debug_types section,
6879 and initialize all_type_units.
6880 The result is zero if there is an error (e.g. missing .debug_types section),
6881 otherwise non-zero. */
6882
6883 static int
6884 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6885 {
6886 htab_t types_htab = NULL;
6887
6888 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6889 &dwarf2_per_objfile->info, types_htab,
6890 rcuh_kind::COMPILE);
6891 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6892 dwarf2_per_objfile->types, types_htab);
6893 if (types_htab == NULL)
6894 {
6895 dwarf2_per_objfile->signatured_types = NULL;
6896 return 0;
6897 }
6898
6899 dwarf2_per_objfile->signatured_types = types_htab;
6900
6901 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6902 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6903
6904 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6905 &dwarf2_per_objfile->all_type_units);
6906
6907 return 1;
6908 }
6909
6910 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6911 If SLOT is non-NULL, it is the entry to use in the hash table.
6912 Otherwise we find one. */
6913
6914 static struct signatured_type *
6915 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6916 void **slot)
6917 {
6918 struct objfile *objfile = dwarf2_per_objfile->objfile;
6919
6920 if (dwarf2_per_objfile->all_type_units.size ()
6921 == dwarf2_per_objfile->all_type_units.capacity ())
6922 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6923
6924 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6925 struct signatured_type);
6926
6927 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6928 sig_type->signature = sig;
6929 sig_type->per_cu.is_debug_types = 1;
6930 if (dwarf2_per_objfile->using_index)
6931 {
6932 sig_type->per_cu.v.quick =
6933 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6934 struct dwarf2_per_cu_quick_data);
6935 }
6936
6937 if (slot == NULL)
6938 {
6939 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6940 sig_type, INSERT);
6941 }
6942 gdb_assert (*slot == NULL);
6943 *slot = sig_type;
6944 /* The rest of sig_type must be filled in by the caller. */
6945 return sig_type;
6946 }
6947
6948 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6949 Fill in SIG_ENTRY with DWO_ENTRY. */
6950
6951 static void
6952 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6953 struct signatured_type *sig_entry,
6954 struct dwo_unit *dwo_entry)
6955 {
6956 /* Make sure we're not clobbering something we don't expect to. */
6957 gdb_assert (! sig_entry->per_cu.queued);
6958 gdb_assert (sig_entry->per_cu.cu == NULL);
6959 if (dwarf2_per_objfile->using_index)
6960 {
6961 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6962 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6963 }
6964 else
6965 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6966 gdb_assert (sig_entry->signature == dwo_entry->signature);
6967 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6968 gdb_assert (sig_entry->type_unit_group == NULL);
6969 gdb_assert (sig_entry->dwo_unit == NULL);
6970
6971 sig_entry->per_cu.section = dwo_entry->section;
6972 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6973 sig_entry->per_cu.length = dwo_entry->length;
6974 sig_entry->per_cu.reading_dwo_directly = 1;
6975 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6976 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6977 sig_entry->dwo_unit = dwo_entry;
6978 }
6979
6980 /* Subroutine of lookup_signatured_type.
6981 If we haven't read the TU yet, create the signatured_type data structure
6982 for a TU to be read in directly from a DWO file, bypassing the stub.
6983 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6984 using .gdb_index, then when reading a CU we want to stay in the DWO file
6985 containing that CU. Otherwise we could end up reading several other DWO
6986 files (due to comdat folding) to process the transitive closure of all the
6987 mentioned TUs, and that can be slow. The current DWO file will have every
6988 type signature that it needs.
6989 We only do this for .gdb_index because in the psymtab case we already have
6990 to read all the DWOs to build the type unit groups. */
6991
6992 static struct signatured_type *
6993 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6994 {
6995 struct dwarf2_per_objfile *dwarf2_per_objfile
6996 = cu->per_cu->dwarf2_per_objfile;
6997 struct objfile *objfile = dwarf2_per_objfile->objfile;
6998 struct dwo_file *dwo_file;
6999 struct dwo_unit find_dwo_entry, *dwo_entry;
7000 struct signatured_type find_sig_entry, *sig_entry;
7001 void **slot;
7002
7003 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7004
7005 /* If TU skeletons have been removed then we may not have read in any
7006 TUs yet. */
7007 if (dwarf2_per_objfile->signatured_types == NULL)
7008 {
7009 dwarf2_per_objfile->signatured_types
7010 = allocate_signatured_type_table (objfile);
7011 }
7012
7013 /* We only ever need to read in one copy of a signatured type.
7014 Use the global signatured_types array to do our own comdat-folding
7015 of types. If this is the first time we're reading this TU, and
7016 the TU has an entry in .gdb_index, replace the recorded data from
7017 .gdb_index with this TU. */
7018
7019 find_sig_entry.signature = sig;
7020 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7021 &find_sig_entry, INSERT);
7022 sig_entry = (struct signatured_type *) *slot;
7023
7024 /* We can get here with the TU already read, *or* in the process of being
7025 read. Don't reassign the global entry to point to this DWO if that's
7026 the case. Also note that if the TU is already being read, it may not
7027 have come from a DWO, the program may be a mix of Fission-compiled
7028 code and non-Fission-compiled code. */
7029
7030 /* Have we already tried to read this TU?
7031 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7032 needn't exist in the global table yet). */
7033 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7034 return sig_entry;
7035
7036 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7037 dwo_unit of the TU itself. */
7038 dwo_file = cu->dwo_unit->dwo_file;
7039
7040 /* Ok, this is the first time we're reading this TU. */
7041 if (dwo_file->tus == NULL)
7042 return NULL;
7043 find_dwo_entry.signature = sig;
7044 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7045 if (dwo_entry == NULL)
7046 return NULL;
7047
7048 /* If the global table doesn't have an entry for this TU, add one. */
7049 if (sig_entry == NULL)
7050 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7051
7052 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7053 sig_entry->per_cu.tu_read = 1;
7054 return sig_entry;
7055 }
7056
7057 /* Subroutine of lookup_signatured_type.
7058 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7059 then try the DWP file. If the TU stub (skeleton) has been removed then
7060 it won't be in .gdb_index. */
7061
7062 static struct signatured_type *
7063 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7064 {
7065 struct dwarf2_per_objfile *dwarf2_per_objfile
7066 = cu->per_cu->dwarf2_per_objfile;
7067 struct objfile *objfile = dwarf2_per_objfile->objfile;
7068 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7069 struct dwo_unit *dwo_entry;
7070 struct signatured_type find_sig_entry, *sig_entry;
7071 void **slot;
7072
7073 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7074 gdb_assert (dwp_file != NULL);
7075
7076 /* If TU skeletons have been removed then we may not have read in any
7077 TUs yet. */
7078 if (dwarf2_per_objfile->signatured_types == NULL)
7079 {
7080 dwarf2_per_objfile->signatured_types
7081 = allocate_signatured_type_table (objfile);
7082 }
7083
7084 find_sig_entry.signature = sig;
7085 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7086 &find_sig_entry, INSERT);
7087 sig_entry = (struct signatured_type *) *slot;
7088
7089 /* Have we already tried to read this TU?
7090 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7091 needn't exist in the global table yet). */
7092 if (sig_entry != NULL)
7093 return sig_entry;
7094
7095 if (dwp_file->tus == NULL)
7096 return NULL;
7097 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7098 sig, 1 /* is_debug_types */);
7099 if (dwo_entry == NULL)
7100 return NULL;
7101
7102 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7103 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7104
7105 return sig_entry;
7106 }
7107
7108 /* Lookup a signature based type for DW_FORM_ref_sig8.
7109 Returns NULL if signature SIG is not present in the table.
7110 It is up to the caller to complain about this. */
7111
7112 static struct signatured_type *
7113 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7114 {
7115 struct dwarf2_per_objfile *dwarf2_per_objfile
7116 = cu->per_cu->dwarf2_per_objfile;
7117
7118 if (cu->dwo_unit
7119 && dwarf2_per_objfile->using_index)
7120 {
7121 /* We're in a DWO/DWP file, and we're using .gdb_index.
7122 These cases require special processing. */
7123 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7124 return lookup_dwo_signatured_type (cu, sig);
7125 else
7126 return lookup_dwp_signatured_type (cu, sig);
7127 }
7128 else
7129 {
7130 struct signatured_type find_entry, *entry;
7131
7132 if (dwarf2_per_objfile->signatured_types == NULL)
7133 return NULL;
7134 find_entry.signature = sig;
7135 entry = ((struct signatured_type *)
7136 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7137 return entry;
7138 }
7139 }
7140 \f
7141 /* Low level DIE reading support. */
7142
7143 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7144
7145 static void
7146 init_cu_die_reader (struct die_reader_specs *reader,
7147 struct dwarf2_cu *cu,
7148 struct dwarf2_section_info *section,
7149 struct dwo_file *dwo_file,
7150 struct abbrev_table *abbrev_table)
7151 {
7152 gdb_assert (section->readin && section->buffer != NULL);
7153 reader->abfd = get_section_bfd_owner (section);
7154 reader->cu = cu;
7155 reader->dwo_file = dwo_file;
7156 reader->die_section = section;
7157 reader->buffer = section->buffer;
7158 reader->buffer_end = section->buffer + section->size;
7159 reader->comp_dir = NULL;
7160 reader->abbrev_table = abbrev_table;
7161 }
7162
7163 /* Subroutine of init_cutu_and_read_dies to simplify it.
7164 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7165 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7166 already.
7167
7168 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7169 from it to the DIE in the DWO. If NULL we are skipping the stub.
7170 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7171 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7172 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7173 STUB_COMP_DIR may be non-NULL.
7174 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7175 are filled in with the info of the DIE from the DWO file.
7176 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7177 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7178 kept around for at least as long as *RESULT_READER.
7179
7180 The result is non-zero if a valid (non-dummy) DIE was found. */
7181
7182 static int
7183 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7184 struct dwo_unit *dwo_unit,
7185 struct die_info *stub_comp_unit_die,
7186 const char *stub_comp_dir,
7187 struct die_reader_specs *result_reader,
7188 const gdb_byte **result_info_ptr,
7189 struct die_info **result_comp_unit_die,
7190 int *result_has_children,
7191 abbrev_table_up *result_dwo_abbrev_table)
7192 {
7193 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7194 struct objfile *objfile = dwarf2_per_objfile->objfile;
7195 struct dwarf2_cu *cu = this_cu->cu;
7196 bfd *abfd;
7197 const gdb_byte *begin_info_ptr, *info_ptr;
7198 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7199 int i,num_extra_attrs;
7200 struct dwarf2_section_info *dwo_abbrev_section;
7201 struct attribute *attr;
7202 struct die_info *comp_unit_die;
7203
7204 /* At most one of these may be provided. */
7205 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7206
7207 /* These attributes aren't processed until later:
7208 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7209 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7210 referenced later. However, these attributes are found in the stub
7211 which we won't have later. In order to not impose this complication
7212 on the rest of the code, we read them here and copy them to the
7213 DWO CU/TU die. */
7214
7215 stmt_list = NULL;
7216 low_pc = NULL;
7217 high_pc = NULL;
7218 ranges = NULL;
7219 comp_dir = NULL;
7220
7221 if (stub_comp_unit_die != NULL)
7222 {
7223 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7224 DWO file. */
7225 if (! this_cu->is_debug_types)
7226 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7227 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7228 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7229 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7230 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7231
7232 /* There should be a DW_AT_addr_base attribute here (if needed).
7233 We need the value before we can process DW_FORM_GNU_addr_index
7234 or DW_FORM_addrx. */
7235 cu->addr_base = 0;
7236 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7237 if (attr)
7238 cu->addr_base = DW_UNSND (attr);
7239
7240 /* There should be a DW_AT_ranges_base attribute here (if needed).
7241 We need the value before we can process DW_AT_ranges. */
7242 cu->ranges_base = 0;
7243 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7244 if (attr)
7245 cu->ranges_base = DW_UNSND (attr);
7246 }
7247 else if (stub_comp_dir != NULL)
7248 {
7249 /* Reconstruct the comp_dir attribute to simplify the code below. */
7250 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7251 comp_dir->name = DW_AT_comp_dir;
7252 comp_dir->form = DW_FORM_string;
7253 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7254 DW_STRING (comp_dir) = stub_comp_dir;
7255 }
7256
7257 /* Set up for reading the DWO CU/TU. */
7258 cu->dwo_unit = dwo_unit;
7259 dwarf2_section_info *section = dwo_unit->section;
7260 dwarf2_read_section (objfile, section);
7261 abfd = get_section_bfd_owner (section);
7262 begin_info_ptr = info_ptr = (section->buffer
7263 + to_underlying (dwo_unit->sect_off));
7264 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7265
7266 if (this_cu->is_debug_types)
7267 {
7268 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7269
7270 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7271 &cu->header, section,
7272 dwo_abbrev_section,
7273 info_ptr, rcuh_kind::TYPE);
7274 /* This is not an assert because it can be caused by bad debug info. */
7275 if (sig_type->signature != cu->header.signature)
7276 {
7277 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7278 " TU at offset %s [in module %s]"),
7279 hex_string (sig_type->signature),
7280 hex_string (cu->header.signature),
7281 sect_offset_str (dwo_unit->sect_off),
7282 bfd_get_filename (abfd));
7283 }
7284 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7285 /* For DWOs coming from DWP files, we don't know the CU length
7286 nor the type's offset in the TU until now. */
7287 dwo_unit->length = get_cu_length (&cu->header);
7288 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7289
7290 /* Establish the type offset that can be used to lookup the type.
7291 For DWO files, we don't know it until now. */
7292 sig_type->type_offset_in_section
7293 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7294 }
7295 else
7296 {
7297 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7298 &cu->header, section,
7299 dwo_abbrev_section,
7300 info_ptr, rcuh_kind::COMPILE);
7301 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7302 /* For DWOs coming from DWP files, we don't know the CU length
7303 until now. */
7304 dwo_unit->length = get_cu_length (&cu->header);
7305 }
7306
7307 *result_dwo_abbrev_table
7308 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7309 cu->header.abbrev_sect_off);
7310 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7311 result_dwo_abbrev_table->get ());
7312
7313 /* Read in the die, but leave space to copy over the attributes
7314 from the stub. This has the benefit of simplifying the rest of
7315 the code - all the work to maintain the illusion of a single
7316 DW_TAG_{compile,type}_unit DIE is done here. */
7317 num_extra_attrs = ((stmt_list != NULL)
7318 + (low_pc != NULL)
7319 + (high_pc != NULL)
7320 + (ranges != NULL)
7321 + (comp_dir != NULL));
7322 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7323 result_has_children, num_extra_attrs);
7324
7325 /* Copy over the attributes from the stub to the DIE we just read in. */
7326 comp_unit_die = *result_comp_unit_die;
7327 i = comp_unit_die->num_attrs;
7328 if (stmt_list != NULL)
7329 comp_unit_die->attrs[i++] = *stmt_list;
7330 if (low_pc != NULL)
7331 comp_unit_die->attrs[i++] = *low_pc;
7332 if (high_pc != NULL)
7333 comp_unit_die->attrs[i++] = *high_pc;
7334 if (ranges != NULL)
7335 comp_unit_die->attrs[i++] = *ranges;
7336 if (comp_dir != NULL)
7337 comp_unit_die->attrs[i++] = *comp_dir;
7338 comp_unit_die->num_attrs += num_extra_attrs;
7339
7340 if (dwarf_die_debug)
7341 {
7342 fprintf_unfiltered (gdb_stdlog,
7343 "Read die from %s@0x%x of %s:\n",
7344 get_section_name (section),
7345 (unsigned) (begin_info_ptr - section->buffer),
7346 bfd_get_filename (abfd));
7347 dump_die (comp_unit_die, dwarf_die_debug);
7348 }
7349
7350 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7351 TUs by skipping the stub and going directly to the entry in the DWO file.
7352 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7353 to get it via circuitous means. Blech. */
7354 if (comp_dir != NULL)
7355 result_reader->comp_dir = DW_STRING (comp_dir);
7356
7357 /* Skip dummy compilation units. */
7358 if (info_ptr >= begin_info_ptr + dwo_unit->length
7359 || peek_abbrev_code (abfd, info_ptr) == 0)
7360 return 0;
7361
7362 *result_info_ptr = info_ptr;
7363 return 1;
7364 }
7365
7366 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7367 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7368 signature is part of the header. */
7369 static gdb::optional<ULONGEST>
7370 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7371 {
7372 if (cu->header.version >= 5)
7373 return cu->header.signature;
7374 struct attribute *attr;
7375 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7376 if (attr == nullptr)
7377 return gdb::optional<ULONGEST> ();
7378 return DW_UNSND (attr);
7379 }
7380
7381 /* Subroutine of init_cutu_and_read_dies to simplify it.
7382 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7383 Returns NULL if the specified DWO unit cannot be found. */
7384
7385 static struct dwo_unit *
7386 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7387 struct die_info *comp_unit_die)
7388 {
7389 struct dwarf2_cu *cu = this_cu->cu;
7390 struct dwo_unit *dwo_unit;
7391 const char *comp_dir, *dwo_name;
7392
7393 gdb_assert (cu != NULL);
7394
7395 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7396 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7397 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7398
7399 if (this_cu->is_debug_types)
7400 {
7401 struct signatured_type *sig_type;
7402
7403 /* Since this_cu is the first member of struct signatured_type,
7404 we can go from a pointer to one to a pointer to the other. */
7405 sig_type = (struct signatured_type *) this_cu;
7406 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7407 }
7408 else
7409 {
7410 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7411 if (!signature.has_value ())
7412 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7413 " [in module %s]"),
7414 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7415 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7416 *signature);
7417 }
7418
7419 return dwo_unit;
7420 }
7421
7422 /* Subroutine of init_cutu_and_read_dies to simplify it.
7423 See it for a description of the parameters.
7424 Read a TU directly from a DWO file, bypassing the stub. */
7425
7426 static void
7427 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7428 int use_existing_cu, int keep,
7429 die_reader_func_ftype *die_reader_func,
7430 void *data)
7431 {
7432 std::unique_ptr<dwarf2_cu> new_cu;
7433 struct signatured_type *sig_type;
7434 struct die_reader_specs reader;
7435 const gdb_byte *info_ptr;
7436 struct die_info *comp_unit_die;
7437 int has_children;
7438 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7439
7440 /* Verify we can do the following downcast, and that we have the
7441 data we need. */
7442 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7443 sig_type = (struct signatured_type *) this_cu;
7444 gdb_assert (sig_type->dwo_unit != NULL);
7445
7446 if (use_existing_cu && this_cu->cu != NULL)
7447 {
7448 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7449 /* There's no need to do the rereading_dwo_cu handling that
7450 init_cutu_and_read_dies does since we don't read the stub. */
7451 }
7452 else
7453 {
7454 /* If !use_existing_cu, this_cu->cu must be NULL. */
7455 gdb_assert (this_cu->cu == NULL);
7456 new_cu.reset (new dwarf2_cu (this_cu));
7457 }
7458
7459 /* A future optimization, if needed, would be to use an existing
7460 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7461 could share abbrev tables. */
7462
7463 /* The abbreviation table used by READER, this must live at least as long as
7464 READER. */
7465 abbrev_table_up dwo_abbrev_table;
7466
7467 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7468 NULL /* stub_comp_unit_die */,
7469 sig_type->dwo_unit->dwo_file->comp_dir,
7470 &reader, &info_ptr,
7471 &comp_unit_die, &has_children,
7472 &dwo_abbrev_table) == 0)
7473 {
7474 /* Dummy die. */
7475 return;
7476 }
7477
7478 /* All the "real" work is done here. */
7479 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7480
7481 /* This duplicates the code in init_cutu_and_read_dies,
7482 but the alternative is making the latter more complex.
7483 This function is only for the special case of using DWO files directly:
7484 no point in overly complicating the general case just to handle this. */
7485 if (new_cu != NULL && keep)
7486 {
7487 /* Link this CU into read_in_chain. */
7488 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7489 dwarf2_per_objfile->read_in_chain = this_cu;
7490 /* The chain owns it now. */
7491 new_cu.release ();
7492 }
7493 }
7494
7495 /* Initialize a CU (or TU) and read its DIEs.
7496 If the CU defers to a DWO file, read the DWO file as well.
7497
7498 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7499 Otherwise the table specified in the comp unit header is read in and used.
7500 This is an optimization for when we already have the abbrev table.
7501
7502 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7503 Otherwise, a new CU is allocated with xmalloc.
7504
7505 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7506 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7507
7508 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7509 linker) then DIE_READER_FUNC will not get called. */
7510
7511 static void
7512 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7513 struct abbrev_table *abbrev_table,
7514 int use_existing_cu, int keep,
7515 bool skip_partial,
7516 die_reader_func_ftype *die_reader_func,
7517 void *data)
7518 {
7519 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7520 struct objfile *objfile = dwarf2_per_objfile->objfile;
7521 struct dwarf2_section_info *section = this_cu->section;
7522 bfd *abfd = get_section_bfd_owner (section);
7523 struct dwarf2_cu *cu;
7524 const gdb_byte *begin_info_ptr, *info_ptr;
7525 struct die_reader_specs reader;
7526 struct die_info *comp_unit_die;
7527 int has_children;
7528 struct signatured_type *sig_type = NULL;
7529 struct dwarf2_section_info *abbrev_section;
7530 /* Non-zero if CU currently points to a DWO file and we need to
7531 reread it. When this happens we need to reread the skeleton die
7532 before we can reread the DWO file (this only applies to CUs, not TUs). */
7533 int rereading_dwo_cu = 0;
7534
7535 if (dwarf_die_debug)
7536 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7537 this_cu->is_debug_types ? "type" : "comp",
7538 sect_offset_str (this_cu->sect_off));
7539
7540 if (use_existing_cu)
7541 gdb_assert (keep);
7542
7543 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7544 file (instead of going through the stub), short-circuit all of this. */
7545 if (this_cu->reading_dwo_directly)
7546 {
7547 /* Narrow down the scope of possibilities to have to understand. */
7548 gdb_assert (this_cu->is_debug_types);
7549 gdb_assert (abbrev_table == NULL);
7550 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7551 die_reader_func, data);
7552 return;
7553 }
7554
7555 /* This is cheap if the section is already read in. */
7556 dwarf2_read_section (objfile, section);
7557
7558 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7559
7560 abbrev_section = get_abbrev_section_for_cu (this_cu);
7561
7562 std::unique_ptr<dwarf2_cu> new_cu;
7563 if (use_existing_cu && this_cu->cu != NULL)
7564 {
7565 cu = this_cu->cu;
7566 /* If this CU is from a DWO file we need to start over, we need to
7567 refetch the attributes from the skeleton CU.
7568 This could be optimized by retrieving those attributes from when we
7569 were here the first time: the previous comp_unit_die was stored in
7570 comp_unit_obstack. But there's no data yet that we need this
7571 optimization. */
7572 if (cu->dwo_unit != NULL)
7573 rereading_dwo_cu = 1;
7574 }
7575 else
7576 {
7577 /* If !use_existing_cu, this_cu->cu must be NULL. */
7578 gdb_assert (this_cu->cu == NULL);
7579 new_cu.reset (new dwarf2_cu (this_cu));
7580 cu = new_cu.get ();
7581 }
7582
7583 /* Get the header. */
7584 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7585 {
7586 /* We already have the header, there's no need to read it in again. */
7587 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7588 }
7589 else
7590 {
7591 if (this_cu->is_debug_types)
7592 {
7593 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7594 &cu->header, section,
7595 abbrev_section, info_ptr,
7596 rcuh_kind::TYPE);
7597
7598 /* Since per_cu is the first member of struct signatured_type,
7599 we can go from a pointer to one to a pointer to the other. */
7600 sig_type = (struct signatured_type *) this_cu;
7601 gdb_assert (sig_type->signature == cu->header.signature);
7602 gdb_assert (sig_type->type_offset_in_tu
7603 == cu->header.type_cu_offset_in_tu);
7604 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7605
7606 /* LENGTH has not been set yet for type units if we're
7607 using .gdb_index. */
7608 this_cu->length = get_cu_length (&cu->header);
7609
7610 /* Establish the type offset that can be used to lookup the type. */
7611 sig_type->type_offset_in_section =
7612 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7613
7614 this_cu->dwarf_version = cu->header.version;
7615 }
7616 else
7617 {
7618 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7619 &cu->header, section,
7620 abbrev_section,
7621 info_ptr,
7622 rcuh_kind::COMPILE);
7623
7624 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7625 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7626 this_cu->dwarf_version = cu->header.version;
7627 }
7628 }
7629
7630 /* Skip dummy compilation units. */
7631 if (info_ptr >= begin_info_ptr + this_cu->length
7632 || peek_abbrev_code (abfd, info_ptr) == 0)
7633 return;
7634
7635 /* If we don't have them yet, read the abbrevs for this compilation unit.
7636 And if we need to read them now, make sure they're freed when we're
7637 done (own the table through ABBREV_TABLE_HOLDER). */
7638 abbrev_table_up abbrev_table_holder;
7639 if (abbrev_table != NULL)
7640 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7641 else
7642 {
7643 abbrev_table_holder
7644 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7645 cu->header.abbrev_sect_off);
7646 abbrev_table = abbrev_table_holder.get ();
7647 }
7648
7649 /* Read the top level CU/TU die. */
7650 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7651 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7652
7653 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7654 return;
7655
7656 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7657 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7658 table from the DWO file and pass the ownership over to us. It will be
7659 referenced from READER, so we must make sure to free it after we're done
7660 with READER.
7661
7662 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7663 DWO CU, that this test will fail (the attribute will not be present). */
7664 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7665 abbrev_table_up dwo_abbrev_table;
7666 if (dwo_name != nullptr)
7667 {
7668 struct dwo_unit *dwo_unit;
7669 struct die_info *dwo_comp_unit_die;
7670
7671 if (has_children)
7672 {
7673 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7674 " has children (offset %s) [in module %s]"),
7675 sect_offset_str (this_cu->sect_off),
7676 bfd_get_filename (abfd));
7677 }
7678 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7679 if (dwo_unit != NULL)
7680 {
7681 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7682 comp_unit_die, NULL,
7683 &reader, &info_ptr,
7684 &dwo_comp_unit_die, &has_children,
7685 &dwo_abbrev_table) == 0)
7686 {
7687 /* Dummy die. */
7688 return;
7689 }
7690 comp_unit_die = dwo_comp_unit_die;
7691 }
7692 else
7693 {
7694 /* Yikes, we couldn't find the rest of the DIE, we only have
7695 the stub. A complaint has already been logged. There's
7696 not much more we can do except pass on the stub DIE to
7697 die_reader_func. We don't want to throw an error on bad
7698 debug info. */
7699 }
7700 }
7701
7702 /* All of the above is setup for this call. Yikes. */
7703 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7704
7705 /* Done, clean up. */
7706 if (new_cu != NULL && keep)
7707 {
7708 /* Link this CU into read_in_chain. */
7709 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7710 dwarf2_per_objfile->read_in_chain = this_cu;
7711 /* The chain owns it now. */
7712 new_cu.release ();
7713 }
7714 }
7715
7716 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7717 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7718 to have already done the lookup to find the DWO file).
7719
7720 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7721 THIS_CU->is_debug_types, but nothing else.
7722
7723 We fill in THIS_CU->length.
7724
7725 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7726 linker) then DIE_READER_FUNC will not get called.
7727
7728 THIS_CU->cu is always freed when done.
7729 This is done in order to not leave THIS_CU->cu in a state where we have
7730 to care whether it refers to the "main" CU or the DWO CU. */
7731
7732 static void
7733 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7734 struct dwo_file *dwo_file,
7735 die_reader_func_ftype *die_reader_func,
7736 void *data)
7737 {
7738 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7739 struct objfile *objfile = dwarf2_per_objfile->objfile;
7740 struct dwarf2_section_info *section = this_cu->section;
7741 bfd *abfd = get_section_bfd_owner (section);
7742 struct dwarf2_section_info *abbrev_section;
7743 const gdb_byte *begin_info_ptr, *info_ptr;
7744 struct die_reader_specs reader;
7745 struct die_info *comp_unit_die;
7746 int has_children;
7747
7748 if (dwarf_die_debug)
7749 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7750 this_cu->is_debug_types ? "type" : "comp",
7751 sect_offset_str (this_cu->sect_off));
7752
7753 gdb_assert (this_cu->cu == NULL);
7754
7755 abbrev_section = (dwo_file != NULL
7756 ? &dwo_file->sections.abbrev
7757 : get_abbrev_section_for_cu (this_cu));
7758
7759 /* This is cheap if the section is already read in. */
7760 dwarf2_read_section (objfile, section);
7761
7762 struct dwarf2_cu cu (this_cu);
7763
7764 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7765 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7766 &cu.header, section,
7767 abbrev_section, info_ptr,
7768 (this_cu->is_debug_types
7769 ? rcuh_kind::TYPE
7770 : rcuh_kind::COMPILE));
7771
7772 this_cu->length = get_cu_length (&cu.header);
7773
7774 /* Skip dummy compilation units. */
7775 if (info_ptr >= begin_info_ptr + this_cu->length
7776 || peek_abbrev_code (abfd, info_ptr) == 0)
7777 return;
7778
7779 abbrev_table_up abbrev_table
7780 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7781 cu.header.abbrev_sect_off);
7782
7783 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
7784 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7785
7786 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7787 }
7788
7789 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7790 does not lookup the specified DWO file.
7791 This cannot be used to read DWO files.
7792
7793 THIS_CU->cu is always freed when done.
7794 This is done in order to not leave THIS_CU->cu in a state where we have
7795 to care whether it refers to the "main" CU or the DWO CU.
7796 We can revisit this if the data shows there's a performance issue. */
7797
7798 static void
7799 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7800 die_reader_func_ftype *die_reader_func,
7801 void *data)
7802 {
7803 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
7804 }
7805 \f
7806 /* Type Unit Groups.
7807
7808 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7809 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7810 so that all types coming from the same compilation (.o file) are grouped
7811 together. A future step could be to put the types in the same symtab as
7812 the CU the types ultimately came from. */
7813
7814 static hashval_t
7815 hash_type_unit_group (const void *item)
7816 {
7817 const struct type_unit_group *tu_group
7818 = (const struct type_unit_group *) item;
7819
7820 return hash_stmt_list_entry (&tu_group->hash);
7821 }
7822
7823 static int
7824 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7825 {
7826 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7827 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7828
7829 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7830 }
7831
7832 /* Allocate a hash table for type unit groups. */
7833
7834 static htab_t
7835 allocate_type_unit_groups_table (struct objfile *objfile)
7836 {
7837 return htab_create_alloc_ex (3,
7838 hash_type_unit_group,
7839 eq_type_unit_group,
7840 NULL,
7841 &objfile->objfile_obstack,
7842 hashtab_obstack_allocate,
7843 dummy_obstack_deallocate);
7844 }
7845
7846 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7847 partial symtabs. We combine several TUs per psymtab to not let the size
7848 of any one psymtab grow too big. */
7849 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7850 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7851
7852 /* Helper routine for get_type_unit_group.
7853 Create the type_unit_group object used to hold one or more TUs. */
7854
7855 static struct type_unit_group *
7856 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7857 {
7858 struct dwarf2_per_objfile *dwarf2_per_objfile
7859 = cu->per_cu->dwarf2_per_objfile;
7860 struct objfile *objfile = dwarf2_per_objfile->objfile;
7861 struct dwarf2_per_cu_data *per_cu;
7862 struct type_unit_group *tu_group;
7863
7864 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7865 struct type_unit_group);
7866 per_cu = &tu_group->per_cu;
7867 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7868
7869 if (dwarf2_per_objfile->using_index)
7870 {
7871 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7872 struct dwarf2_per_cu_quick_data);
7873 }
7874 else
7875 {
7876 unsigned int line_offset = to_underlying (line_offset_struct);
7877 struct partial_symtab *pst;
7878 std::string name;
7879
7880 /* Give the symtab a useful name for debug purposes. */
7881 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7882 name = string_printf ("<type_units_%d>",
7883 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7884 else
7885 name = string_printf ("<type_units_at_0x%x>", line_offset);
7886
7887 pst = create_partial_symtab (per_cu, name.c_str ());
7888 pst->anonymous = 1;
7889 }
7890
7891 tu_group->hash.dwo_unit = cu->dwo_unit;
7892 tu_group->hash.line_sect_off = line_offset_struct;
7893
7894 return tu_group;
7895 }
7896
7897 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7898 STMT_LIST is a DW_AT_stmt_list attribute. */
7899
7900 static struct type_unit_group *
7901 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7902 {
7903 struct dwarf2_per_objfile *dwarf2_per_objfile
7904 = cu->per_cu->dwarf2_per_objfile;
7905 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7906 struct type_unit_group *tu_group;
7907 void **slot;
7908 unsigned int line_offset;
7909 struct type_unit_group type_unit_group_for_lookup;
7910
7911 if (dwarf2_per_objfile->type_unit_groups == NULL)
7912 {
7913 dwarf2_per_objfile->type_unit_groups =
7914 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7915 }
7916
7917 /* Do we need to create a new group, or can we use an existing one? */
7918
7919 if (stmt_list)
7920 {
7921 line_offset = DW_UNSND (stmt_list);
7922 ++tu_stats->nr_symtab_sharers;
7923 }
7924 else
7925 {
7926 /* Ugh, no stmt_list. Rare, but we have to handle it.
7927 We can do various things here like create one group per TU or
7928 spread them over multiple groups to split up the expansion work.
7929 To avoid worst case scenarios (too many groups or too large groups)
7930 we, umm, group them in bunches. */
7931 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7932 | (tu_stats->nr_stmt_less_type_units
7933 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7934 ++tu_stats->nr_stmt_less_type_units;
7935 }
7936
7937 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7938 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7939 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7940 &type_unit_group_for_lookup, INSERT);
7941 if (*slot != NULL)
7942 {
7943 tu_group = (struct type_unit_group *) *slot;
7944 gdb_assert (tu_group != NULL);
7945 }
7946 else
7947 {
7948 sect_offset line_offset_struct = (sect_offset) line_offset;
7949 tu_group = create_type_unit_group (cu, line_offset_struct);
7950 *slot = tu_group;
7951 ++tu_stats->nr_symtabs;
7952 }
7953
7954 return tu_group;
7955 }
7956 \f
7957 /* Partial symbol tables. */
7958
7959 /* Create a psymtab named NAME and assign it to PER_CU.
7960
7961 The caller must fill in the following details:
7962 dirname, textlow, texthigh. */
7963
7964 static struct partial_symtab *
7965 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7966 {
7967 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7968 struct partial_symtab *pst;
7969
7970 pst = start_psymtab_common (objfile, name, 0);
7971
7972 pst->psymtabs_addrmap_supported = 1;
7973
7974 /* This is the glue that links PST into GDB's symbol API. */
7975 pst->read_symtab_private = per_cu;
7976 pst->read_symtab = dwarf2_read_symtab;
7977 per_cu->v.psymtab = pst;
7978
7979 return pst;
7980 }
7981
7982 /* The DATA object passed to process_psymtab_comp_unit_reader has this
7983 type. */
7984
7985 struct process_psymtab_comp_unit_data
7986 {
7987 /* True if we are reading a DW_TAG_partial_unit. */
7988
7989 int want_partial_unit;
7990
7991 /* The "pretend" language that is used if the CU doesn't declare a
7992 language. */
7993
7994 enum language pretend_language;
7995 };
7996
7997 /* die_reader_func for process_psymtab_comp_unit. */
7998
7999 static void
8000 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8001 const gdb_byte *info_ptr,
8002 struct die_info *comp_unit_die,
8003 int has_children,
8004 void *data)
8005 {
8006 struct dwarf2_cu *cu = reader->cu;
8007 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8008 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8009 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8010 CORE_ADDR baseaddr;
8011 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8012 struct partial_symtab *pst;
8013 enum pc_bounds_kind cu_bounds_kind;
8014 const char *filename;
8015 struct process_psymtab_comp_unit_data *info
8016 = (struct process_psymtab_comp_unit_data *) data;
8017
8018 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8019 return;
8020
8021 gdb_assert (! per_cu->is_debug_types);
8022
8023 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8024
8025 /* Allocate a new partial symbol table structure. */
8026 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8027 if (filename == NULL)
8028 filename = "";
8029
8030 pst = create_partial_symtab (per_cu, filename);
8031
8032 /* This must be done before calling dwarf2_build_include_psymtabs. */
8033 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8034
8035 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8036
8037 dwarf2_find_base_address (comp_unit_die, cu);
8038
8039 /* Possibly set the default values of LOWPC and HIGHPC from
8040 `DW_AT_ranges'. */
8041 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8042 &best_highpc, cu, pst);
8043 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8044 {
8045 CORE_ADDR low
8046 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8047 - baseaddr);
8048 CORE_ADDR high
8049 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8050 - baseaddr - 1);
8051 /* Store the contiguous range if it is not empty; it can be
8052 empty for CUs with no code. */
8053 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8054 low, high, pst);
8055 }
8056
8057 /* Check if comp unit has_children.
8058 If so, read the rest of the partial symbols from this comp unit.
8059 If not, there's no more debug_info for this comp unit. */
8060 if (has_children)
8061 {
8062 struct partial_die_info *first_die;
8063 CORE_ADDR lowpc, highpc;
8064
8065 lowpc = ((CORE_ADDR) -1);
8066 highpc = ((CORE_ADDR) 0);
8067
8068 first_die = load_partial_dies (reader, info_ptr, 1);
8069
8070 scan_partial_symbols (first_die, &lowpc, &highpc,
8071 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8072
8073 /* If we didn't find a lowpc, set it to highpc to avoid
8074 complaints from `maint check'. */
8075 if (lowpc == ((CORE_ADDR) -1))
8076 lowpc = highpc;
8077
8078 /* If the compilation unit didn't have an explicit address range,
8079 then use the information extracted from its child dies. */
8080 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8081 {
8082 best_lowpc = lowpc;
8083 best_highpc = highpc;
8084 }
8085 }
8086 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
8087 best_lowpc + baseaddr)
8088 - baseaddr);
8089 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
8090 best_highpc + baseaddr)
8091 - baseaddr);
8092
8093 end_psymtab_common (objfile, pst);
8094
8095 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8096 {
8097 int i;
8098 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8099 struct dwarf2_per_cu_data *iter;
8100
8101 /* Fill in 'dependencies' here; we fill in 'users' in a
8102 post-pass. */
8103 pst->number_of_dependencies = len;
8104 pst->dependencies
8105 = objfile->partial_symtabs->allocate_dependencies (len);
8106 for (i = 0;
8107 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8108 i, iter);
8109 ++i)
8110 pst->dependencies[i] = iter->v.psymtab;
8111
8112 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8113 }
8114
8115 /* Get the list of files included in the current compilation unit,
8116 and build a psymtab for each of them. */
8117 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8118
8119 if (dwarf_read_debug)
8120 fprintf_unfiltered (gdb_stdlog,
8121 "Psymtab for %s unit @%s: %s - %s"
8122 ", %d global, %d static syms\n",
8123 per_cu->is_debug_types ? "type" : "comp",
8124 sect_offset_str (per_cu->sect_off),
8125 paddress (gdbarch, pst->text_low (objfile)),
8126 paddress (gdbarch, pst->text_high (objfile)),
8127 pst->n_global_syms, pst->n_static_syms);
8128 }
8129
8130 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8131 Process compilation unit THIS_CU for a psymtab. */
8132
8133 static void
8134 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8135 int want_partial_unit,
8136 enum language pretend_language)
8137 {
8138 /* If this compilation unit was already read in, free the
8139 cached copy in order to read it in again. This is
8140 necessary because we skipped some symbols when we first
8141 read in the compilation unit (see load_partial_dies).
8142 This problem could be avoided, but the benefit is unclear. */
8143 if (this_cu->cu != NULL)
8144 free_one_cached_comp_unit (this_cu);
8145
8146 if (this_cu->is_debug_types)
8147 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8148 build_type_psymtabs_reader, NULL);
8149 else
8150 {
8151 process_psymtab_comp_unit_data info;
8152 info.want_partial_unit = want_partial_unit;
8153 info.pretend_language = pretend_language;
8154 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8155 process_psymtab_comp_unit_reader, &info);
8156 }
8157
8158 /* Age out any secondary CUs. */
8159 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8160 }
8161
8162 /* Reader function for build_type_psymtabs. */
8163
8164 static void
8165 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8166 const gdb_byte *info_ptr,
8167 struct die_info *type_unit_die,
8168 int has_children,
8169 void *data)
8170 {
8171 struct dwarf2_per_objfile *dwarf2_per_objfile
8172 = reader->cu->per_cu->dwarf2_per_objfile;
8173 struct objfile *objfile = dwarf2_per_objfile->objfile;
8174 struct dwarf2_cu *cu = reader->cu;
8175 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8176 struct signatured_type *sig_type;
8177 struct type_unit_group *tu_group;
8178 struct attribute *attr;
8179 struct partial_die_info *first_die;
8180 CORE_ADDR lowpc, highpc;
8181 struct partial_symtab *pst;
8182
8183 gdb_assert (data == NULL);
8184 gdb_assert (per_cu->is_debug_types);
8185 sig_type = (struct signatured_type *) per_cu;
8186
8187 if (! has_children)
8188 return;
8189
8190 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8191 tu_group = get_type_unit_group (cu, attr);
8192
8193 if (tu_group->tus == nullptr)
8194 tu_group->tus = new std::vector <signatured_type *>;
8195 tu_group->tus->push_back (sig_type);
8196
8197 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8198 pst = create_partial_symtab (per_cu, "");
8199 pst->anonymous = 1;
8200
8201 first_die = load_partial_dies (reader, info_ptr, 1);
8202
8203 lowpc = (CORE_ADDR) -1;
8204 highpc = (CORE_ADDR) 0;
8205 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8206
8207 end_psymtab_common (objfile, pst);
8208 }
8209
8210 /* Struct used to sort TUs by their abbreviation table offset. */
8211
8212 struct tu_abbrev_offset
8213 {
8214 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8215 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8216 {}
8217
8218 signatured_type *sig_type;
8219 sect_offset abbrev_offset;
8220 };
8221
8222 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8223
8224 static bool
8225 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8226 const struct tu_abbrev_offset &b)
8227 {
8228 return a.abbrev_offset < b.abbrev_offset;
8229 }
8230
8231 /* Efficiently read all the type units.
8232 This does the bulk of the work for build_type_psymtabs.
8233
8234 The efficiency is because we sort TUs by the abbrev table they use and
8235 only read each abbrev table once. In one program there are 200K TUs
8236 sharing 8K abbrev tables.
8237
8238 The main purpose of this function is to support building the
8239 dwarf2_per_objfile->type_unit_groups table.
8240 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8241 can collapse the search space by grouping them by stmt_list.
8242 The savings can be significant, in the same program from above the 200K TUs
8243 share 8K stmt_list tables.
8244
8245 FUNC is expected to call get_type_unit_group, which will create the
8246 struct type_unit_group if necessary and add it to
8247 dwarf2_per_objfile->type_unit_groups. */
8248
8249 static void
8250 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8251 {
8252 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8253 abbrev_table_up abbrev_table;
8254 sect_offset abbrev_offset;
8255
8256 /* It's up to the caller to not call us multiple times. */
8257 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8258
8259 if (dwarf2_per_objfile->all_type_units.empty ())
8260 return;
8261
8262 /* TUs typically share abbrev tables, and there can be way more TUs than
8263 abbrev tables. Sort by abbrev table to reduce the number of times we
8264 read each abbrev table in.
8265 Alternatives are to punt or to maintain a cache of abbrev tables.
8266 This is simpler and efficient enough for now.
8267
8268 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8269 symtab to use). Typically TUs with the same abbrev offset have the same
8270 stmt_list value too so in practice this should work well.
8271
8272 The basic algorithm here is:
8273
8274 sort TUs by abbrev table
8275 for each TU with same abbrev table:
8276 read abbrev table if first user
8277 read TU top level DIE
8278 [IWBN if DWO skeletons had DW_AT_stmt_list]
8279 call FUNC */
8280
8281 if (dwarf_read_debug)
8282 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8283
8284 /* Sort in a separate table to maintain the order of all_type_units
8285 for .gdb_index: TU indices directly index all_type_units. */
8286 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8287 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8288
8289 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8290 sorted_by_abbrev.emplace_back
8291 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8292 sig_type->per_cu.section,
8293 sig_type->per_cu.sect_off));
8294
8295 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8296 sort_tu_by_abbrev_offset);
8297
8298 abbrev_offset = (sect_offset) ~(unsigned) 0;
8299
8300 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8301 {
8302 /* Switch to the next abbrev table if necessary. */
8303 if (abbrev_table == NULL
8304 || tu.abbrev_offset != abbrev_offset)
8305 {
8306 abbrev_offset = tu.abbrev_offset;
8307 abbrev_table =
8308 abbrev_table_read_table (dwarf2_per_objfile,
8309 &dwarf2_per_objfile->abbrev,
8310 abbrev_offset);
8311 ++tu_stats->nr_uniq_abbrev_tables;
8312 }
8313
8314 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
8315 0, 0, false, build_type_psymtabs_reader, NULL);
8316 }
8317 }
8318
8319 /* Print collected type unit statistics. */
8320
8321 static void
8322 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8323 {
8324 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8325
8326 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8327 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8328 dwarf2_per_objfile->all_type_units.size ());
8329 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8330 tu_stats->nr_uniq_abbrev_tables);
8331 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8332 tu_stats->nr_symtabs);
8333 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8334 tu_stats->nr_symtab_sharers);
8335 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8336 tu_stats->nr_stmt_less_type_units);
8337 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8338 tu_stats->nr_all_type_units_reallocs);
8339 }
8340
8341 /* Traversal function for build_type_psymtabs. */
8342
8343 static int
8344 build_type_psymtab_dependencies (void **slot, void *info)
8345 {
8346 struct dwarf2_per_objfile *dwarf2_per_objfile
8347 = (struct dwarf2_per_objfile *) info;
8348 struct objfile *objfile = dwarf2_per_objfile->objfile;
8349 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8350 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8351 struct partial_symtab *pst = per_cu->v.psymtab;
8352 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8353 int i;
8354
8355 gdb_assert (len > 0);
8356 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8357
8358 pst->number_of_dependencies = len;
8359 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8360 for (i = 0; i < len; ++i)
8361 {
8362 struct signatured_type *iter = tu_group->tus->at (i);
8363 gdb_assert (iter->per_cu.is_debug_types);
8364 pst->dependencies[i] = iter->per_cu.v.psymtab;
8365 iter->type_unit_group = tu_group;
8366 }
8367
8368 delete tu_group->tus;
8369 tu_group->tus = nullptr;
8370
8371 return 1;
8372 }
8373
8374 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8375 Build partial symbol tables for the .debug_types comp-units. */
8376
8377 static void
8378 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8379 {
8380 if (! create_all_type_units (dwarf2_per_objfile))
8381 return;
8382
8383 build_type_psymtabs_1 (dwarf2_per_objfile);
8384 }
8385
8386 /* Traversal function for process_skeletonless_type_unit.
8387 Read a TU in a DWO file and build partial symbols for it. */
8388
8389 static int
8390 process_skeletonless_type_unit (void **slot, void *info)
8391 {
8392 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8393 struct dwarf2_per_objfile *dwarf2_per_objfile
8394 = (struct dwarf2_per_objfile *) info;
8395 struct signatured_type find_entry, *entry;
8396
8397 /* If this TU doesn't exist in the global table, add it and read it in. */
8398
8399 if (dwarf2_per_objfile->signatured_types == NULL)
8400 {
8401 dwarf2_per_objfile->signatured_types
8402 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8403 }
8404
8405 find_entry.signature = dwo_unit->signature;
8406 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8407 INSERT);
8408 /* If we've already seen this type there's nothing to do. What's happening
8409 is we're doing our own version of comdat-folding here. */
8410 if (*slot != NULL)
8411 return 1;
8412
8413 /* This does the job that create_all_type_units would have done for
8414 this TU. */
8415 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8416 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8417 *slot = entry;
8418
8419 /* This does the job that build_type_psymtabs_1 would have done. */
8420 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
8421 build_type_psymtabs_reader, NULL);
8422
8423 return 1;
8424 }
8425
8426 /* Traversal function for process_skeletonless_type_units. */
8427
8428 static int
8429 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8430 {
8431 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8432
8433 if (dwo_file->tus != NULL)
8434 {
8435 htab_traverse_noresize (dwo_file->tus,
8436 process_skeletonless_type_unit, info);
8437 }
8438
8439 return 1;
8440 }
8441
8442 /* Scan all TUs of DWO files, verifying we've processed them.
8443 This is needed in case a TU was emitted without its skeleton.
8444 Note: This can't be done until we know what all the DWO files are. */
8445
8446 static void
8447 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8448 {
8449 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8450 if (get_dwp_file (dwarf2_per_objfile) == NULL
8451 && dwarf2_per_objfile->dwo_files != NULL)
8452 {
8453 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
8454 process_dwo_file_for_skeletonless_type_units,
8455 dwarf2_per_objfile);
8456 }
8457 }
8458
8459 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8460
8461 static void
8462 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8463 {
8464 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8465 {
8466 struct partial_symtab *pst = per_cu->v.psymtab;
8467
8468 if (pst == NULL)
8469 continue;
8470
8471 for (int j = 0; j < pst->number_of_dependencies; ++j)
8472 {
8473 /* Set the 'user' field only if it is not already set. */
8474 if (pst->dependencies[j]->user == NULL)
8475 pst->dependencies[j]->user = pst;
8476 }
8477 }
8478 }
8479
8480 /* Build the partial symbol table by doing a quick pass through the
8481 .debug_info and .debug_abbrev sections. */
8482
8483 static void
8484 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8485 {
8486 struct objfile *objfile = dwarf2_per_objfile->objfile;
8487
8488 if (dwarf_read_debug)
8489 {
8490 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8491 objfile_name (objfile));
8492 }
8493
8494 dwarf2_per_objfile->reading_partial_symbols = 1;
8495
8496 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8497
8498 /* Any cached compilation units will be linked by the per-objfile
8499 read_in_chain. Make sure to free them when we're done. */
8500 free_cached_comp_units freer (dwarf2_per_objfile);
8501
8502 build_type_psymtabs (dwarf2_per_objfile);
8503
8504 create_all_comp_units (dwarf2_per_objfile);
8505
8506 /* Create a temporary address map on a temporary obstack. We later
8507 copy this to the final obstack. */
8508 auto_obstack temp_obstack;
8509
8510 scoped_restore save_psymtabs_addrmap
8511 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8512 addrmap_create_mutable (&temp_obstack));
8513
8514 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8515 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8516
8517 /* This has to wait until we read the CUs, we need the list of DWOs. */
8518 process_skeletonless_type_units (dwarf2_per_objfile);
8519
8520 /* Now that all TUs have been processed we can fill in the dependencies. */
8521 if (dwarf2_per_objfile->type_unit_groups != NULL)
8522 {
8523 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8524 build_type_psymtab_dependencies, dwarf2_per_objfile);
8525 }
8526
8527 if (dwarf_read_debug)
8528 print_tu_stats (dwarf2_per_objfile);
8529
8530 set_partial_user (dwarf2_per_objfile);
8531
8532 objfile->partial_symtabs->psymtabs_addrmap
8533 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8534 objfile->partial_symtabs->obstack ());
8535 /* At this point we want to keep the address map. */
8536 save_psymtabs_addrmap.release ();
8537
8538 if (dwarf_read_debug)
8539 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8540 objfile_name (objfile));
8541 }
8542
8543 /* die_reader_func for load_partial_comp_unit. */
8544
8545 static void
8546 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8547 const gdb_byte *info_ptr,
8548 struct die_info *comp_unit_die,
8549 int has_children,
8550 void *data)
8551 {
8552 struct dwarf2_cu *cu = reader->cu;
8553
8554 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8555
8556 /* Check if comp unit has_children.
8557 If so, read the rest of the partial symbols from this comp unit.
8558 If not, there's no more debug_info for this comp unit. */
8559 if (has_children)
8560 load_partial_dies (reader, info_ptr, 0);
8561 }
8562
8563 /* Load the partial DIEs for a secondary CU into memory.
8564 This is also used when rereading a primary CU with load_all_dies. */
8565
8566 static void
8567 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8568 {
8569 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
8570 load_partial_comp_unit_reader, NULL);
8571 }
8572
8573 static void
8574 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8575 struct dwarf2_section_info *section,
8576 struct dwarf2_section_info *abbrev_section,
8577 unsigned int is_dwz)
8578 {
8579 const gdb_byte *info_ptr;
8580 struct objfile *objfile = dwarf2_per_objfile->objfile;
8581
8582 if (dwarf_read_debug)
8583 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8584 get_section_name (section),
8585 get_section_file_name (section));
8586
8587 dwarf2_read_section (objfile, section);
8588
8589 info_ptr = section->buffer;
8590
8591 while (info_ptr < section->buffer + section->size)
8592 {
8593 struct dwarf2_per_cu_data *this_cu;
8594
8595 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8596
8597 comp_unit_head cu_header;
8598 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8599 abbrev_section, info_ptr,
8600 rcuh_kind::COMPILE);
8601
8602 /* Save the compilation unit for later lookup. */
8603 if (cu_header.unit_type != DW_UT_type)
8604 {
8605 this_cu = XOBNEW (&objfile->objfile_obstack,
8606 struct dwarf2_per_cu_data);
8607 memset (this_cu, 0, sizeof (*this_cu));
8608 }
8609 else
8610 {
8611 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8612 struct signatured_type);
8613 memset (sig_type, 0, sizeof (*sig_type));
8614 sig_type->signature = cu_header.signature;
8615 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8616 this_cu = &sig_type->per_cu;
8617 }
8618 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8619 this_cu->sect_off = sect_off;
8620 this_cu->length = cu_header.length + cu_header.initial_length_size;
8621 this_cu->is_dwz = is_dwz;
8622 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8623 this_cu->section = section;
8624
8625 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8626
8627 info_ptr = info_ptr + this_cu->length;
8628 }
8629 }
8630
8631 /* Create a list of all compilation units in OBJFILE.
8632 This is only done for -readnow and building partial symtabs. */
8633
8634 static void
8635 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8636 {
8637 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8638 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8639 &dwarf2_per_objfile->abbrev, 0);
8640
8641 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8642 if (dwz != NULL)
8643 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8644 1);
8645 }
8646
8647 /* Process all loaded DIEs for compilation unit CU, starting at
8648 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8649 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8650 DW_AT_ranges). See the comments of add_partial_subprogram on how
8651 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8652
8653 static void
8654 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8655 CORE_ADDR *highpc, int set_addrmap,
8656 struct dwarf2_cu *cu)
8657 {
8658 struct partial_die_info *pdi;
8659
8660 /* Now, march along the PDI's, descending into ones which have
8661 interesting children but skipping the children of the other ones,
8662 until we reach the end of the compilation unit. */
8663
8664 pdi = first_die;
8665
8666 while (pdi != NULL)
8667 {
8668 pdi->fixup (cu);
8669
8670 /* Anonymous namespaces or modules have no name but have interesting
8671 children, so we need to look at them. Ditto for anonymous
8672 enums. */
8673
8674 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8675 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8676 || pdi->tag == DW_TAG_imported_unit
8677 || pdi->tag == DW_TAG_inlined_subroutine)
8678 {
8679 switch (pdi->tag)
8680 {
8681 case DW_TAG_subprogram:
8682 case DW_TAG_inlined_subroutine:
8683 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8684 break;
8685 case DW_TAG_constant:
8686 case DW_TAG_variable:
8687 case DW_TAG_typedef:
8688 case DW_TAG_union_type:
8689 if (!pdi->is_declaration)
8690 {
8691 add_partial_symbol (pdi, cu);
8692 }
8693 break;
8694 case DW_TAG_class_type:
8695 case DW_TAG_interface_type:
8696 case DW_TAG_structure_type:
8697 if (!pdi->is_declaration)
8698 {
8699 add_partial_symbol (pdi, cu);
8700 }
8701 if ((cu->language == language_rust
8702 || cu->language == language_cplus) && pdi->has_children)
8703 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8704 set_addrmap, cu);
8705 break;
8706 case DW_TAG_enumeration_type:
8707 if (!pdi->is_declaration)
8708 add_partial_enumeration (pdi, cu);
8709 break;
8710 case DW_TAG_base_type:
8711 case DW_TAG_subrange_type:
8712 /* File scope base type definitions are added to the partial
8713 symbol table. */
8714 add_partial_symbol (pdi, cu);
8715 break;
8716 case DW_TAG_namespace:
8717 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8718 break;
8719 case DW_TAG_module:
8720 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8721 break;
8722 case DW_TAG_imported_unit:
8723 {
8724 struct dwarf2_per_cu_data *per_cu;
8725
8726 /* For now we don't handle imported units in type units. */
8727 if (cu->per_cu->is_debug_types)
8728 {
8729 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8730 " supported in type units [in module %s]"),
8731 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8732 }
8733
8734 per_cu = dwarf2_find_containing_comp_unit
8735 (pdi->d.sect_off, pdi->is_dwz,
8736 cu->per_cu->dwarf2_per_objfile);
8737
8738 /* Go read the partial unit, if needed. */
8739 if (per_cu->v.psymtab == NULL)
8740 process_psymtab_comp_unit (per_cu, 1, cu->language);
8741
8742 VEC_safe_push (dwarf2_per_cu_ptr,
8743 cu->per_cu->imported_symtabs, per_cu);
8744 }
8745 break;
8746 case DW_TAG_imported_declaration:
8747 add_partial_symbol (pdi, cu);
8748 break;
8749 default:
8750 break;
8751 }
8752 }
8753
8754 /* If the die has a sibling, skip to the sibling. */
8755
8756 pdi = pdi->die_sibling;
8757 }
8758 }
8759
8760 /* Functions used to compute the fully scoped name of a partial DIE.
8761
8762 Normally, this is simple. For C++, the parent DIE's fully scoped
8763 name is concatenated with "::" and the partial DIE's name.
8764 Enumerators are an exception; they use the scope of their parent
8765 enumeration type, i.e. the name of the enumeration type is not
8766 prepended to the enumerator.
8767
8768 There are two complexities. One is DW_AT_specification; in this
8769 case "parent" means the parent of the target of the specification,
8770 instead of the direct parent of the DIE. The other is compilers
8771 which do not emit DW_TAG_namespace; in this case we try to guess
8772 the fully qualified name of structure types from their members'
8773 linkage names. This must be done using the DIE's children rather
8774 than the children of any DW_AT_specification target. We only need
8775 to do this for structures at the top level, i.e. if the target of
8776 any DW_AT_specification (if any; otherwise the DIE itself) does not
8777 have a parent. */
8778
8779 /* Compute the scope prefix associated with PDI's parent, in
8780 compilation unit CU. The result will be allocated on CU's
8781 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8782 field. NULL is returned if no prefix is necessary. */
8783 static const char *
8784 partial_die_parent_scope (struct partial_die_info *pdi,
8785 struct dwarf2_cu *cu)
8786 {
8787 const char *grandparent_scope;
8788 struct partial_die_info *parent, *real_pdi;
8789
8790 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8791 then this means the parent of the specification DIE. */
8792
8793 real_pdi = pdi;
8794 while (real_pdi->has_specification)
8795 {
8796 auto res = find_partial_die (real_pdi->spec_offset,
8797 real_pdi->spec_is_dwz, cu);
8798 real_pdi = res.pdi;
8799 cu = res.cu;
8800 }
8801
8802 parent = real_pdi->die_parent;
8803 if (parent == NULL)
8804 return NULL;
8805
8806 if (parent->scope_set)
8807 return parent->scope;
8808
8809 parent->fixup (cu);
8810
8811 grandparent_scope = partial_die_parent_scope (parent, cu);
8812
8813 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8814 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8815 Work around this problem here. */
8816 if (cu->language == language_cplus
8817 && parent->tag == DW_TAG_namespace
8818 && strcmp (parent->name, "::") == 0
8819 && grandparent_scope == NULL)
8820 {
8821 parent->scope = NULL;
8822 parent->scope_set = 1;
8823 return NULL;
8824 }
8825
8826 if (pdi->tag == DW_TAG_enumerator)
8827 /* Enumerators should not get the name of the enumeration as a prefix. */
8828 parent->scope = grandparent_scope;
8829 else if (parent->tag == DW_TAG_namespace
8830 || parent->tag == DW_TAG_module
8831 || parent->tag == DW_TAG_structure_type
8832 || parent->tag == DW_TAG_class_type
8833 || parent->tag == DW_TAG_interface_type
8834 || parent->tag == DW_TAG_union_type
8835 || parent->tag == DW_TAG_enumeration_type)
8836 {
8837 if (grandparent_scope == NULL)
8838 parent->scope = parent->name;
8839 else
8840 parent->scope = typename_concat (&cu->comp_unit_obstack,
8841 grandparent_scope,
8842 parent->name, 0, cu);
8843 }
8844 else
8845 {
8846 /* FIXME drow/2004-04-01: What should we be doing with
8847 function-local names? For partial symbols, we should probably be
8848 ignoring them. */
8849 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8850 dwarf_tag_name (parent->tag),
8851 sect_offset_str (pdi->sect_off));
8852 parent->scope = grandparent_scope;
8853 }
8854
8855 parent->scope_set = 1;
8856 return parent->scope;
8857 }
8858
8859 /* Return the fully scoped name associated with PDI, from compilation unit
8860 CU. The result will be allocated with malloc. */
8861
8862 static char *
8863 partial_die_full_name (struct partial_die_info *pdi,
8864 struct dwarf2_cu *cu)
8865 {
8866 const char *parent_scope;
8867
8868 /* If this is a template instantiation, we can not work out the
8869 template arguments from partial DIEs. So, unfortunately, we have
8870 to go through the full DIEs. At least any work we do building
8871 types here will be reused if full symbols are loaded later. */
8872 if (pdi->has_template_arguments)
8873 {
8874 pdi->fixup (cu);
8875
8876 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8877 {
8878 struct die_info *die;
8879 struct attribute attr;
8880 struct dwarf2_cu *ref_cu = cu;
8881
8882 /* DW_FORM_ref_addr is using section offset. */
8883 attr.name = (enum dwarf_attribute) 0;
8884 attr.form = DW_FORM_ref_addr;
8885 attr.u.unsnd = to_underlying (pdi->sect_off);
8886 die = follow_die_ref (NULL, &attr, &ref_cu);
8887
8888 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8889 }
8890 }
8891
8892 parent_scope = partial_die_parent_scope (pdi, cu);
8893 if (parent_scope == NULL)
8894 return NULL;
8895 else
8896 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
8897 }
8898
8899 static void
8900 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8901 {
8902 struct dwarf2_per_objfile *dwarf2_per_objfile
8903 = cu->per_cu->dwarf2_per_objfile;
8904 struct objfile *objfile = dwarf2_per_objfile->objfile;
8905 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8906 CORE_ADDR addr = 0;
8907 const char *actual_name = NULL;
8908 CORE_ADDR baseaddr;
8909 char *built_actual_name;
8910
8911 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8912
8913 built_actual_name = partial_die_full_name (pdi, cu);
8914 if (built_actual_name != NULL)
8915 actual_name = built_actual_name;
8916
8917 if (actual_name == NULL)
8918 actual_name = pdi->name;
8919
8920 switch (pdi->tag)
8921 {
8922 case DW_TAG_inlined_subroutine:
8923 case DW_TAG_subprogram:
8924 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8925 - baseaddr);
8926 if (pdi->is_external || cu->language == language_ada)
8927 {
8928 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8929 of the global scope. But in Ada, we want to be able to access
8930 nested procedures globally. So all Ada subprograms are stored
8931 in the global scope. */
8932 add_psymbol_to_list (actual_name, strlen (actual_name),
8933 built_actual_name != NULL,
8934 VAR_DOMAIN, LOC_BLOCK,
8935 SECT_OFF_TEXT (objfile),
8936 psymbol_placement::GLOBAL,
8937 addr,
8938 cu->language, objfile);
8939 }
8940 else
8941 {
8942 add_psymbol_to_list (actual_name, strlen (actual_name),
8943 built_actual_name != NULL,
8944 VAR_DOMAIN, LOC_BLOCK,
8945 SECT_OFF_TEXT (objfile),
8946 psymbol_placement::STATIC,
8947 addr, cu->language, objfile);
8948 }
8949
8950 if (pdi->main_subprogram && actual_name != NULL)
8951 set_objfile_main_name (objfile, actual_name, cu->language);
8952 break;
8953 case DW_TAG_constant:
8954 add_psymbol_to_list (actual_name, strlen (actual_name),
8955 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8956 -1, (pdi->is_external
8957 ? psymbol_placement::GLOBAL
8958 : psymbol_placement::STATIC),
8959 0, cu->language, objfile);
8960 break;
8961 case DW_TAG_variable:
8962 if (pdi->d.locdesc)
8963 addr = decode_locdesc (pdi->d.locdesc, cu);
8964
8965 if (pdi->d.locdesc
8966 && addr == 0
8967 && !dwarf2_per_objfile->has_section_at_zero)
8968 {
8969 /* A global or static variable may also have been stripped
8970 out by the linker if unused, in which case its address
8971 will be nullified; do not add such variables into partial
8972 symbol table then. */
8973 }
8974 else if (pdi->is_external)
8975 {
8976 /* Global Variable.
8977 Don't enter into the minimal symbol tables as there is
8978 a minimal symbol table entry from the ELF symbols already.
8979 Enter into partial symbol table if it has a location
8980 descriptor or a type.
8981 If the location descriptor is missing, new_symbol will create
8982 a LOC_UNRESOLVED symbol, the address of the variable will then
8983 be determined from the minimal symbol table whenever the variable
8984 is referenced.
8985 The address for the partial symbol table entry is not
8986 used by GDB, but it comes in handy for debugging partial symbol
8987 table building. */
8988
8989 if (pdi->d.locdesc || pdi->has_type)
8990 add_psymbol_to_list (actual_name, strlen (actual_name),
8991 built_actual_name != NULL,
8992 VAR_DOMAIN, LOC_STATIC,
8993 SECT_OFF_TEXT (objfile),
8994 psymbol_placement::GLOBAL,
8995 addr, cu->language, objfile);
8996 }
8997 else
8998 {
8999 int has_loc = pdi->d.locdesc != NULL;
9000
9001 /* Static Variable. Skip symbols whose value we cannot know (those
9002 without location descriptors or constant values). */
9003 if (!has_loc && !pdi->has_const_value)
9004 {
9005 xfree (built_actual_name);
9006 return;
9007 }
9008
9009 add_psymbol_to_list (actual_name, strlen (actual_name),
9010 built_actual_name != NULL,
9011 VAR_DOMAIN, LOC_STATIC,
9012 SECT_OFF_TEXT (objfile),
9013 psymbol_placement::STATIC,
9014 has_loc ? addr : 0,
9015 cu->language, objfile);
9016 }
9017 break;
9018 case DW_TAG_typedef:
9019 case DW_TAG_base_type:
9020 case DW_TAG_subrange_type:
9021 add_psymbol_to_list (actual_name, strlen (actual_name),
9022 built_actual_name != NULL,
9023 VAR_DOMAIN, LOC_TYPEDEF, -1,
9024 psymbol_placement::STATIC,
9025 0, cu->language, objfile);
9026 break;
9027 case DW_TAG_imported_declaration:
9028 case DW_TAG_namespace:
9029 add_psymbol_to_list (actual_name, strlen (actual_name),
9030 built_actual_name != NULL,
9031 VAR_DOMAIN, LOC_TYPEDEF, -1,
9032 psymbol_placement::GLOBAL,
9033 0, cu->language, objfile);
9034 break;
9035 case DW_TAG_module:
9036 /* With Fortran 77 there might be a "BLOCK DATA" module
9037 available without any name. If so, we skip the module as it
9038 doesn't bring any value. */
9039 if (actual_name != nullptr)
9040 add_psymbol_to_list (actual_name, strlen (actual_name),
9041 built_actual_name != NULL,
9042 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9043 psymbol_placement::GLOBAL,
9044 0, cu->language, objfile);
9045 break;
9046 case DW_TAG_class_type:
9047 case DW_TAG_interface_type:
9048 case DW_TAG_structure_type:
9049 case DW_TAG_union_type:
9050 case DW_TAG_enumeration_type:
9051 /* Skip external references. The DWARF standard says in the section
9052 about "Structure, Union, and Class Type Entries": "An incomplete
9053 structure, union or class type is represented by a structure,
9054 union or class entry that does not have a byte size attribute
9055 and that has a DW_AT_declaration attribute." */
9056 if (!pdi->has_byte_size && pdi->is_declaration)
9057 {
9058 xfree (built_actual_name);
9059 return;
9060 }
9061
9062 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9063 static vs. global. */
9064 add_psymbol_to_list (actual_name, strlen (actual_name),
9065 built_actual_name != NULL,
9066 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9067 cu->language == language_cplus
9068 ? psymbol_placement::GLOBAL
9069 : psymbol_placement::STATIC,
9070 0, cu->language, objfile);
9071
9072 break;
9073 case DW_TAG_enumerator:
9074 add_psymbol_to_list (actual_name, strlen (actual_name),
9075 built_actual_name != NULL,
9076 VAR_DOMAIN, LOC_CONST, -1,
9077 cu->language == language_cplus
9078 ? psymbol_placement::GLOBAL
9079 : psymbol_placement::STATIC,
9080 0, cu->language, objfile);
9081 break;
9082 default:
9083 break;
9084 }
9085
9086 xfree (built_actual_name);
9087 }
9088
9089 /* Read a partial die corresponding to a namespace; also, add a symbol
9090 corresponding to that namespace to the symbol table. NAMESPACE is
9091 the name of the enclosing namespace. */
9092
9093 static void
9094 add_partial_namespace (struct partial_die_info *pdi,
9095 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9096 int set_addrmap, struct dwarf2_cu *cu)
9097 {
9098 /* Add a symbol for the namespace. */
9099
9100 add_partial_symbol (pdi, cu);
9101
9102 /* Now scan partial symbols in that namespace. */
9103
9104 if (pdi->has_children)
9105 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9106 }
9107
9108 /* Read a partial die corresponding to a Fortran module. */
9109
9110 static void
9111 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9112 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9113 {
9114 /* Add a symbol for the namespace. */
9115
9116 add_partial_symbol (pdi, cu);
9117
9118 /* Now scan partial symbols in that module. */
9119
9120 if (pdi->has_children)
9121 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9122 }
9123
9124 /* Read a partial die corresponding to a subprogram or an inlined
9125 subprogram and create a partial symbol for that subprogram.
9126 When the CU language allows it, this routine also defines a partial
9127 symbol for each nested subprogram that this subprogram contains.
9128 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9129 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9130
9131 PDI may also be a lexical block, in which case we simply search
9132 recursively for subprograms defined inside that lexical block.
9133 Again, this is only performed when the CU language allows this
9134 type of definitions. */
9135
9136 static void
9137 add_partial_subprogram (struct partial_die_info *pdi,
9138 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9139 int set_addrmap, struct dwarf2_cu *cu)
9140 {
9141 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9142 {
9143 if (pdi->has_pc_info)
9144 {
9145 if (pdi->lowpc < *lowpc)
9146 *lowpc = pdi->lowpc;
9147 if (pdi->highpc > *highpc)
9148 *highpc = pdi->highpc;
9149 if (set_addrmap)
9150 {
9151 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9152 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9153 CORE_ADDR baseaddr;
9154 CORE_ADDR this_highpc;
9155 CORE_ADDR this_lowpc;
9156
9157 baseaddr = ANOFFSET (objfile->section_offsets,
9158 SECT_OFF_TEXT (objfile));
9159 this_lowpc
9160 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9161 pdi->lowpc + baseaddr)
9162 - baseaddr);
9163 this_highpc
9164 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9165 pdi->highpc + baseaddr)
9166 - baseaddr);
9167 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
9168 this_lowpc, this_highpc - 1,
9169 cu->per_cu->v.psymtab);
9170 }
9171 }
9172
9173 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9174 {
9175 if (!pdi->is_declaration)
9176 /* Ignore subprogram DIEs that do not have a name, they are
9177 illegal. Do not emit a complaint at this point, we will
9178 do so when we convert this psymtab into a symtab. */
9179 if (pdi->name)
9180 add_partial_symbol (pdi, cu);
9181 }
9182 }
9183
9184 if (! pdi->has_children)
9185 return;
9186
9187 if (cu->language == language_ada)
9188 {
9189 pdi = pdi->die_child;
9190 while (pdi != NULL)
9191 {
9192 pdi->fixup (cu);
9193 if (pdi->tag == DW_TAG_subprogram
9194 || pdi->tag == DW_TAG_inlined_subroutine
9195 || pdi->tag == DW_TAG_lexical_block)
9196 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9197 pdi = pdi->die_sibling;
9198 }
9199 }
9200 }
9201
9202 /* Read a partial die corresponding to an enumeration type. */
9203
9204 static void
9205 add_partial_enumeration (struct partial_die_info *enum_pdi,
9206 struct dwarf2_cu *cu)
9207 {
9208 struct partial_die_info *pdi;
9209
9210 if (enum_pdi->name != NULL)
9211 add_partial_symbol (enum_pdi, cu);
9212
9213 pdi = enum_pdi->die_child;
9214 while (pdi)
9215 {
9216 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9217 complaint (_("malformed enumerator DIE ignored"));
9218 else
9219 add_partial_symbol (pdi, cu);
9220 pdi = pdi->die_sibling;
9221 }
9222 }
9223
9224 /* Return the initial uleb128 in the die at INFO_PTR. */
9225
9226 static unsigned int
9227 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9228 {
9229 unsigned int bytes_read;
9230
9231 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9232 }
9233
9234 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9235 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9236
9237 Return the corresponding abbrev, or NULL if the number is zero (indicating
9238 an empty DIE). In either case *BYTES_READ will be set to the length of
9239 the initial number. */
9240
9241 static struct abbrev_info *
9242 peek_die_abbrev (const die_reader_specs &reader,
9243 const gdb_byte *info_ptr, unsigned int *bytes_read)
9244 {
9245 dwarf2_cu *cu = reader.cu;
9246 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9247 unsigned int abbrev_number
9248 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9249
9250 if (abbrev_number == 0)
9251 return NULL;
9252
9253 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9254 if (!abbrev)
9255 {
9256 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9257 " at offset %s [in module %s]"),
9258 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9259 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9260 }
9261
9262 return abbrev;
9263 }
9264
9265 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9266 Returns a pointer to the end of a series of DIEs, terminated by an empty
9267 DIE. Any children of the skipped DIEs will also be skipped. */
9268
9269 static const gdb_byte *
9270 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9271 {
9272 while (1)
9273 {
9274 unsigned int bytes_read;
9275 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9276
9277 if (abbrev == NULL)
9278 return info_ptr + bytes_read;
9279 else
9280 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9281 }
9282 }
9283
9284 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9285 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9286 abbrev corresponding to that skipped uleb128 should be passed in
9287 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9288 children. */
9289
9290 static const gdb_byte *
9291 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9292 struct abbrev_info *abbrev)
9293 {
9294 unsigned int bytes_read;
9295 struct attribute attr;
9296 bfd *abfd = reader->abfd;
9297 struct dwarf2_cu *cu = reader->cu;
9298 const gdb_byte *buffer = reader->buffer;
9299 const gdb_byte *buffer_end = reader->buffer_end;
9300 unsigned int form, i;
9301
9302 for (i = 0; i < abbrev->num_attrs; i++)
9303 {
9304 /* The only abbrev we care about is DW_AT_sibling. */
9305 if (abbrev->attrs[i].name == DW_AT_sibling)
9306 {
9307 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9308 if (attr.form == DW_FORM_ref_addr)
9309 complaint (_("ignoring absolute DW_AT_sibling"));
9310 else
9311 {
9312 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9313 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9314
9315 if (sibling_ptr < info_ptr)
9316 complaint (_("DW_AT_sibling points backwards"));
9317 else if (sibling_ptr > reader->buffer_end)
9318 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9319 else
9320 return sibling_ptr;
9321 }
9322 }
9323
9324 /* If it isn't DW_AT_sibling, skip this attribute. */
9325 form = abbrev->attrs[i].form;
9326 skip_attribute:
9327 switch (form)
9328 {
9329 case DW_FORM_ref_addr:
9330 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9331 and later it is offset sized. */
9332 if (cu->header.version == 2)
9333 info_ptr += cu->header.addr_size;
9334 else
9335 info_ptr += cu->header.offset_size;
9336 break;
9337 case DW_FORM_GNU_ref_alt:
9338 info_ptr += cu->header.offset_size;
9339 break;
9340 case DW_FORM_addr:
9341 info_ptr += cu->header.addr_size;
9342 break;
9343 case DW_FORM_data1:
9344 case DW_FORM_ref1:
9345 case DW_FORM_flag:
9346 case DW_FORM_strx1:
9347 info_ptr += 1;
9348 break;
9349 case DW_FORM_flag_present:
9350 case DW_FORM_implicit_const:
9351 break;
9352 case DW_FORM_data2:
9353 case DW_FORM_ref2:
9354 case DW_FORM_strx2:
9355 info_ptr += 2;
9356 break;
9357 case DW_FORM_strx3:
9358 info_ptr += 3;
9359 break;
9360 case DW_FORM_data4:
9361 case DW_FORM_ref4:
9362 case DW_FORM_strx4:
9363 info_ptr += 4;
9364 break;
9365 case DW_FORM_data8:
9366 case DW_FORM_ref8:
9367 case DW_FORM_ref_sig8:
9368 info_ptr += 8;
9369 break;
9370 case DW_FORM_data16:
9371 info_ptr += 16;
9372 break;
9373 case DW_FORM_string:
9374 read_direct_string (abfd, info_ptr, &bytes_read);
9375 info_ptr += bytes_read;
9376 break;
9377 case DW_FORM_sec_offset:
9378 case DW_FORM_strp:
9379 case DW_FORM_GNU_strp_alt:
9380 info_ptr += cu->header.offset_size;
9381 break;
9382 case DW_FORM_exprloc:
9383 case DW_FORM_block:
9384 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9385 info_ptr += bytes_read;
9386 break;
9387 case DW_FORM_block1:
9388 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9389 break;
9390 case DW_FORM_block2:
9391 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9392 break;
9393 case DW_FORM_block4:
9394 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9395 break;
9396 case DW_FORM_addrx:
9397 case DW_FORM_strx:
9398 case DW_FORM_sdata:
9399 case DW_FORM_udata:
9400 case DW_FORM_ref_udata:
9401 case DW_FORM_GNU_addr_index:
9402 case DW_FORM_GNU_str_index:
9403 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9404 break;
9405 case DW_FORM_indirect:
9406 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9407 info_ptr += bytes_read;
9408 /* We need to continue parsing from here, so just go back to
9409 the top. */
9410 goto skip_attribute;
9411
9412 default:
9413 error (_("Dwarf Error: Cannot handle %s "
9414 "in DWARF reader [in module %s]"),
9415 dwarf_form_name (form),
9416 bfd_get_filename (abfd));
9417 }
9418 }
9419
9420 if (abbrev->has_children)
9421 return skip_children (reader, info_ptr);
9422 else
9423 return info_ptr;
9424 }
9425
9426 /* Locate ORIG_PDI's sibling.
9427 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9428
9429 static const gdb_byte *
9430 locate_pdi_sibling (const struct die_reader_specs *reader,
9431 struct partial_die_info *orig_pdi,
9432 const gdb_byte *info_ptr)
9433 {
9434 /* Do we know the sibling already? */
9435
9436 if (orig_pdi->sibling)
9437 return orig_pdi->sibling;
9438
9439 /* Are there any children to deal with? */
9440
9441 if (!orig_pdi->has_children)
9442 return info_ptr;
9443
9444 /* Skip the children the long way. */
9445
9446 return skip_children (reader, info_ptr);
9447 }
9448
9449 /* Expand this partial symbol table into a full symbol table. SELF is
9450 not NULL. */
9451
9452 static void
9453 dwarf2_read_symtab (struct partial_symtab *self,
9454 struct objfile *objfile)
9455 {
9456 struct dwarf2_per_objfile *dwarf2_per_objfile
9457 = get_dwarf2_per_objfile (objfile);
9458
9459 if (self->readin)
9460 {
9461 warning (_("bug: psymtab for %s is already read in."),
9462 self->filename);
9463 }
9464 else
9465 {
9466 if (info_verbose)
9467 {
9468 printf_filtered (_("Reading in symbols for %s..."),
9469 self->filename);
9470 gdb_flush (gdb_stdout);
9471 }
9472
9473 /* If this psymtab is constructed from a debug-only objfile, the
9474 has_section_at_zero flag will not necessarily be correct. We
9475 can get the correct value for this flag by looking at the data
9476 associated with the (presumably stripped) associated objfile. */
9477 if (objfile->separate_debug_objfile_backlink)
9478 {
9479 struct dwarf2_per_objfile *dpo_backlink
9480 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9481
9482 dwarf2_per_objfile->has_section_at_zero
9483 = dpo_backlink->has_section_at_zero;
9484 }
9485
9486 dwarf2_per_objfile->reading_partial_symbols = 0;
9487
9488 psymtab_to_symtab_1 (self);
9489
9490 /* Finish up the debug error message. */
9491 if (info_verbose)
9492 printf_filtered (_("done.\n"));
9493 }
9494
9495 process_cu_includes (dwarf2_per_objfile);
9496 }
9497 \f
9498 /* Reading in full CUs. */
9499
9500 /* Add PER_CU to the queue. */
9501
9502 static void
9503 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9504 enum language pretend_language)
9505 {
9506 struct dwarf2_queue_item *item;
9507
9508 per_cu->queued = 1;
9509 item = XNEW (struct dwarf2_queue_item);
9510 item->per_cu = per_cu;
9511 item->pretend_language = pretend_language;
9512 item->next = NULL;
9513
9514 if (dwarf2_queue == NULL)
9515 dwarf2_queue = item;
9516 else
9517 dwarf2_queue_tail->next = item;
9518
9519 dwarf2_queue_tail = item;
9520 }
9521
9522 /* If PER_CU is not yet queued, add it to the queue.
9523 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9524 dependency.
9525 The result is non-zero if PER_CU was queued, otherwise the result is zero
9526 meaning either PER_CU is already queued or it is already loaded.
9527
9528 N.B. There is an invariant here that if a CU is queued then it is loaded.
9529 The caller is required to load PER_CU if we return non-zero. */
9530
9531 static int
9532 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9533 struct dwarf2_per_cu_data *per_cu,
9534 enum language pretend_language)
9535 {
9536 /* We may arrive here during partial symbol reading, if we need full
9537 DIEs to process an unusual case (e.g. template arguments). Do
9538 not queue PER_CU, just tell our caller to load its DIEs. */
9539 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9540 {
9541 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9542 return 1;
9543 return 0;
9544 }
9545
9546 /* Mark the dependence relation so that we don't flush PER_CU
9547 too early. */
9548 if (dependent_cu != NULL)
9549 dwarf2_add_dependence (dependent_cu, per_cu);
9550
9551 /* If it's already on the queue, we have nothing to do. */
9552 if (per_cu->queued)
9553 return 0;
9554
9555 /* If the compilation unit is already loaded, just mark it as
9556 used. */
9557 if (per_cu->cu != NULL)
9558 {
9559 per_cu->cu->last_used = 0;
9560 return 0;
9561 }
9562
9563 /* Add it to the queue. */
9564 queue_comp_unit (per_cu, pretend_language);
9565
9566 return 1;
9567 }
9568
9569 /* Process the queue. */
9570
9571 static void
9572 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9573 {
9574 struct dwarf2_queue_item *item, *next_item;
9575
9576 if (dwarf_read_debug)
9577 {
9578 fprintf_unfiltered (gdb_stdlog,
9579 "Expanding one or more symtabs of objfile %s ...\n",
9580 objfile_name (dwarf2_per_objfile->objfile));
9581 }
9582
9583 /* The queue starts out with one item, but following a DIE reference
9584 may load a new CU, adding it to the end of the queue. */
9585 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9586 {
9587 if ((dwarf2_per_objfile->using_index
9588 ? !item->per_cu->v.quick->compunit_symtab
9589 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9590 /* Skip dummy CUs. */
9591 && item->per_cu->cu != NULL)
9592 {
9593 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9594 unsigned int debug_print_threshold;
9595 char buf[100];
9596
9597 if (per_cu->is_debug_types)
9598 {
9599 struct signatured_type *sig_type =
9600 (struct signatured_type *) per_cu;
9601
9602 sprintf (buf, "TU %s at offset %s",
9603 hex_string (sig_type->signature),
9604 sect_offset_str (per_cu->sect_off));
9605 /* There can be 100s of TUs.
9606 Only print them in verbose mode. */
9607 debug_print_threshold = 2;
9608 }
9609 else
9610 {
9611 sprintf (buf, "CU at offset %s",
9612 sect_offset_str (per_cu->sect_off));
9613 debug_print_threshold = 1;
9614 }
9615
9616 if (dwarf_read_debug >= debug_print_threshold)
9617 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9618
9619 if (per_cu->is_debug_types)
9620 process_full_type_unit (per_cu, item->pretend_language);
9621 else
9622 process_full_comp_unit (per_cu, item->pretend_language);
9623
9624 if (dwarf_read_debug >= debug_print_threshold)
9625 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9626 }
9627
9628 item->per_cu->queued = 0;
9629 next_item = item->next;
9630 xfree (item);
9631 }
9632
9633 dwarf2_queue_tail = NULL;
9634
9635 if (dwarf_read_debug)
9636 {
9637 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9638 objfile_name (dwarf2_per_objfile->objfile));
9639 }
9640 }
9641
9642 /* Read in full symbols for PST, and anything it depends on. */
9643
9644 static void
9645 psymtab_to_symtab_1 (struct partial_symtab *pst)
9646 {
9647 struct dwarf2_per_cu_data *per_cu;
9648 int i;
9649
9650 if (pst->readin)
9651 return;
9652
9653 for (i = 0; i < pst->number_of_dependencies; i++)
9654 if (!pst->dependencies[i]->readin
9655 && pst->dependencies[i]->user == NULL)
9656 {
9657 /* Inform about additional files that need to be read in. */
9658 if (info_verbose)
9659 {
9660 /* FIXME: i18n: Need to make this a single string. */
9661 fputs_filtered (" ", gdb_stdout);
9662 wrap_here ("");
9663 fputs_filtered ("and ", gdb_stdout);
9664 wrap_here ("");
9665 printf_filtered ("%s...", pst->dependencies[i]->filename);
9666 wrap_here (""); /* Flush output. */
9667 gdb_flush (gdb_stdout);
9668 }
9669 psymtab_to_symtab_1 (pst->dependencies[i]);
9670 }
9671
9672 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9673
9674 if (per_cu == NULL)
9675 {
9676 /* It's an include file, no symbols to read for it.
9677 Everything is in the parent symtab. */
9678 pst->readin = 1;
9679 return;
9680 }
9681
9682 dw2_do_instantiate_symtab (per_cu, false);
9683 }
9684
9685 /* Trivial hash function for die_info: the hash value of a DIE
9686 is its offset in .debug_info for this objfile. */
9687
9688 static hashval_t
9689 die_hash (const void *item)
9690 {
9691 const struct die_info *die = (const struct die_info *) item;
9692
9693 return to_underlying (die->sect_off);
9694 }
9695
9696 /* Trivial comparison function for die_info structures: two DIEs
9697 are equal if they have the same offset. */
9698
9699 static int
9700 die_eq (const void *item_lhs, const void *item_rhs)
9701 {
9702 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9703 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9704
9705 return die_lhs->sect_off == die_rhs->sect_off;
9706 }
9707
9708 /* die_reader_func for load_full_comp_unit.
9709 This is identical to read_signatured_type_reader,
9710 but is kept separate for now. */
9711
9712 static void
9713 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9714 const gdb_byte *info_ptr,
9715 struct die_info *comp_unit_die,
9716 int has_children,
9717 void *data)
9718 {
9719 struct dwarf2_cu *cu = reader->cu;
9720 enum language *language_ptr = (enum language *) data;
9721
9722 gdb_assert (cu->die_hash == NULL);
9723 cu->die_hash =
9724 htab_create_alloc_ex (cu->header.length / 12,
9725 die_hash,
9726 die_eq,
9727 NULL,
9728 &cu->comp_unit_obstack,
9729 hashtab_obstack_allocate,
9730 dummy_obstack_deallocate);
9731
9732 if (has_children)
9733 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9734 &info_ptr, comp_unit_die);
9735 cu->dies = comp_unit_die;
9736 /* comp_unit_die is not stored in die_hash, no need. */
9737
9738 /* We try not to read any attributes in this function, because not
9739 all CUs needed for references have been loaded yet, and symbol
9740 table processing isn't initialized. But we have to set the CU language,
9741 or we won't be able to build types correctly.
9742 Similarly, if we do not read the producer, we can not apply
9743 producer-specific interpretation. */
9744 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
9745 }
9746
9747 /* Load the DIEs associated with PER_CU into memory. */
9748
9749 static void
9750 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9751 bool skip_partial,
9752 enum language pretend_language)
9753 {
9754 gdb_assert (! this_cu->is_debug_types);
9755
9756 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
9757 load_full_comp_unit_reader, &pretend_language);
9758 }
9759
9760 /* Add a DIE to the delayed physname list. */
9761
9762 static void
9763 add_to_method_list (struct type *type, int fnfield_index, int index,
9764 const char *name, struct die_info *die,
9765 struct dwarf2_cu *cu)
9766 {
9767 struct delayed_method_info mi;
9768 mi.type = type;
9769 mi.fnfield_index = fnfield_index;
9770 mi.index = index;
9771 mi.name = name;
9772 mi.die = die;
9773 cu->method_list.push_back (mi);
9774 }
9775
9776 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9777 "const" / "volatile". If so, decrements LEN by the length of the
9778 modifier and return true. Otherwise return false. */
9779
9780 template<size_t N>
9781 static bool
9782 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9783 {
9784 size_t mod_len = sizeof (mod) - 1;
9785 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9786 {
9787 len -= mod_len;
9788 return true;
9789 }
9790 return false;
9791 }
9792
9793 /* Compute the physnames of any methods on the CU's method list.
9794
9795 The computation of method physnames is delayed in order to avoid the
9796 (bad) condition that one of the method's formal parameters is of an as yet
9797 incomplete type. */
9798
9799 static void
9800 compute_delayed_physnames (struct dwarf2_cu *cu)
9801 {
9802 /* Only C++ delays computing physnames. */
9803 if (cu->method_list.empty ())
9804 return;
9805 gdb_assert (cu->language == language_cplus);
9806
9807 for (const delayed_method_info &mi : cu->method_list)
9808 {
9809 const char *physname;
9810 struct fn_fieldlist *fn_flp
9811 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9812 physname = dwarf2_physname (mi.name, mi.die, cu);
9813 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9814 = physname ? physname : "";
9815
9816 /* Since there's no tag to indicate whether a method is a
9817 const/volatile overload, extract that information out of the
9818 demangled name. */
9819 if (physname != NULL)
9820 {
9821 size_t len = strlen (physname);
9822
9823 while (1)
9824 {
9825 if (physname[len] == ')') /* shortcut */
9826 break;
9827 else if (check_modifier (physname, len, " const"))
9828 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9829 else if (check_modifier (physname, len, " volatile"))
9830 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9831 else
9832 break;
9833 }
9834 }
9835 }
9836
9837 /* The list is no longer needed. */
9838 cu->method_list.clear ();
9839 }
9840
9841 /* Go objects should be embedded in a DW_TAG_module DIE,
9842 and it's not clear if/how imported objects will appear.
9843 To keep Go support simple until that's worked out,
9844 go back through what we've read and create something usable.
9845 We could do this while processing each DIE, and feels kinda cleaner,
9846 but that way is more invasive.
9847 This is to, for example, allow the user to type "p var" or "b main"
9848 without having to specify the package name, and allow lookups
9849 of module.object to work in contexts that use the expression
9850 parser. */
9851
9852 static void
9853 fixup_go_packaging (struct dwarf2_cu *cu)
9854 {
9855 char *package_name = NULL;
9856 struct pending *list;
9857 int i;
9858
9859 for (list = *cu->get_builder ()->get_global_symbols ();
9860 list != NULL;
9861 list = list->next)
9862 {
9863 for (i = 0; i < list->nsyms; ++i)
9864 {
9865 struct symbol *sym = list->symbol[i];
9866
9867 if (SYMBOL_LANGUAGE (sym) == language_go
9868 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9869 {
9870 char *this_package_name = go_symbol_package_name (sym);
9871
9872 if (this_package_name == NULL)
9873 continue;
9874 if (package_name == NULL)
9875 package_name = this_package_name;
9876 else
9877 {
9878 struct objfile *objfile
9879 = cu->per_cu->dwarf2_per_objfile->objfile;
9880 if (strcmp (package_name, this_package_name) != 0)
9881 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9882 (symbol_symtab (sym) != NULL
9883 ? symtab_to_filename_for_display
9884 (symbol_symtab (sym))
9885 : objfile_name (objfile)),
9886 this_package_name, package_name);
9887 xfree (this_package_name);
9888 }
9889 }
9890 }
9891 }
9892
9893 if (package_name != NULL)
9894 {
9895 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9896 const char *saved_package_name
9897 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name);
9898 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9899 saved_package_name);
9900 struct symbol *sym;
9901
9902 sym = allocate_symbol (objfile);
9903 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
9904 SYMBOL_SET_NAMES (sym, saved_package_name,
9905 strlen (saved_package_name), 0, objfile);
9906 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9907 e.g., "main" finds the "main" module and not C's main(). */
9908 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9909 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9910 SYMBOL_TYPE (sym) = type;
9911
9912 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9913
9914 xfree (package_name);
9915 }
9916 }
9917
9918 /* Allocate a fully-qualified name consisting of the two parts on the
9919 obstack. */
9920
9921 static const char *
9922 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9923 {
9924 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9925 }
9926
9927 /* A helper that allocates a struct discriminant_info to attach to a
9928 union type. */
9929
9930 static struct discriminant_info *
9931 alloc_discriminant_info (struct type *type, int discriminant_index,
9932 int default_index)
9933 {
9934 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9935 gdb_assert (discriminant_index == -1
9936 || (discriminant_index >= 0
9937 && discriminant_index < TYPE_NFIELDS (type)));
9938 gdb_assert (default_index == -1
9939 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9940
9941 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9942
9943 struct discriminant_info *disc
9944 = ((struct discriminant_info *)
9945 TYPE_ZALLOC (type,
9946 offsetof (struct discriminant_info, discriminants)
9947 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9948 disc->default_index = default_index;
9949 disc->discriminant_index = discriminant_index;
9950
9951 struct dynamic_prop prop;
9952 prop.kind = PROP_UNDEFINED;
9953 prop.data.baton = disc;
9954
9955 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9956
9957 return disc;
9958 }
9959
9960 /* Some versions of rustc emitted enums in an unusual way.
9961
9962 Ordinary enums were emitted as unions. The first element of each
9963 structure in the union was named "RUST$ENUM$DISR". This element
9964 held the discriminant.
9965
9966 These versions of Rust also implemented the "non-zero"
9967 optimization. When the enum had two values, and one is empty and
9968 the other holds a pointer that cannot be zero, the pointer is used
9969 as the discriminant, with a zero value meaning the empty variant.
9970 Here, the union's first member is of the form
9971 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9972 where the fieldnos are the indices of the fields that should be
9973 traversed in order to find the field (which may be several fields deep)
9974 and the variantname is the name of the variant of the case when the
9975 field is zero.
9976
9977 This function recognizes whether TYPE is of one of these forms,
9978 and, if so, smashes it to be a variant type. */
9979
9980 static void
9981 quirk_rust_enum (struct type *type, struct objfile *objfile)
9982 {
9983 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9984
9985 /* We don't need to deal with empty enums. */
9986 if (TYPE_NFIELDS (type) == 0)
9987 return;
9988
9989 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9990 if (TYPE_NFIELDS (type) == 1
9991 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9992 {
9993 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9994
9995 /* Decode the field name to find the offset of the
9996 discriminant. */
9997 ULONGEST bit_offset = 0;
9998 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9999 while (name[0] >= '0' && name[0] <= '9')
10000 {
10001 char *tail;
10002 unsigned long index = strtoul (name, &tail, 10);
10003 name = tail;
10004 if (*name != '$'
10005 || index >= TYPE_NFIELDS (field_type)
10006 || (TYPE_FIELD_LOC_KIND (field_type, index)
10007 != FIELD_LOC_KIND_BITPOS))
10008 {
10009 complaint (_("Could not parse Rust enum encoding string \"%s\""
10010 "[in module %s]"),
10011 TYPE_FIELD_NAME (type, 0),
10012 objfile_name (objfile));
10013 return;
10014 }
10015 ++name;
10016
10017 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10018 field_type = TYPE_FIELD_TYPE (field_type, index);
10019 }
10020
10021 /* Make a union to hold the variants. */
10022 struct type *union_type = alloc_type (objfile);
10023 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10024 TYPE_NFIELDS (union_type) = 3;
10025 TYPE_FIELDS (union_type)
10026 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10027 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10028 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10029
10030 /* Put the discriminant must at index 0. */
10031 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10032 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10033 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10034 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10035
10036 /* The order of fields doesn't really matter, so put the real
10037 field at index 1 and the data-less field at index 2. */
10038 struct discriminant_info *disc
10039 = alloc_discriminant_info (union_type, 0, 1);
10040 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10041 TYPE_FIELD_NAME (union_type, 1)
10042 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10043 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10044 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10045 TYPE_FIELD_NAME (union_type, 1));
10046
10047 const char *dataless_name
10048 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10049 name);
10050 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10051 dataless_name);
10052 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10053 /* NAME points into the original discriminant name, which
10054 already has the correct lifetime. */
10055 TYPE_FIELD_NAME (union_type, 2) = name;
10056 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10057 disc->discriminants[2] = 0;
10058
10059 /* Smash this type to be a structure type. We have to do this
10060 because the type has already been recorded. */
10061 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10062 TYPE_NFIELDS (type) = 1;
10063 TYPE_FIELDS (type)
10064 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10065
10066 /* Install the variant part. */
10067 TYPE_FIELD_TYPE (type, 0) = union_type;
10068 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10069 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10070 }
10071 else if (TYPE_NFIELDS (type) == 1)
10072 {
10073 /* We assume that a union with a single field is a univariant
10074 enum. */
10075 /* Smash this type to be a structure type. We have to do this
10076 because the type has already been recorded. */
10077 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10078
10079 /* Make a union to hold the variants. */
10080 struct type *union_type = alloc_type (objfile);
10081 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10082 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10083 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10084 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10085 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10086
10087 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10088 const char *variant_name
10089 = rust_last_path_segment (TYPE_NAME (field_type));
10090 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10091 TYPE_NAME (field_type)
10092 = rust_fully_qualify (&objfile->objfile_obstack,
10093 TYPE_NAME (type), variant_name);
10094
10095 /* Install the union in the outer struct type. */
10096 TYPE_NFIELDS (type) = 1;
10097 TYPE_FIELDS (type)
10098 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10099 TYPE_FIELD_TYPE (type, 0) = union_type;
10100 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10101 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10102
10103 alloc_discriminant_info (union_type, -1, 0);
10104 }
10105 else
10106 {
10107 struct type *disr_type = nullptr;
10108 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10109 {
10110 disr_type = TYPE_FIELD_TYPE (type, i);
10111
10112 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10113 {
10114 /* All fields of a true enum will be structs. */
10115 return;
10116 }
10117 else if (TYPE_NFIELDS (disr_type) == 0)
10118 {
10119 /* Could be data-less variant, so keep going. */
10120 disr_type = nullptr;
10121 }
10122 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10123 "RUST$ENUM$DISR") != 0)
10124 {
10125 /* Not a Rust enum. */
10126 return;
10127 }
10128 else
10129 {
10130 /* Found one. */
10131 break;
10132 }
10133 }
10134
10135 /* If we got here without a discriminant, then it's probably
10136 just a union. */
10137 if (disr_type == nullptr)
10138 return;
10139
10140 /* Smash this type to be a structure type. We have to do this
10141 because the type has already been recorded. */
10142 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10143
10144 /* Make a union to hold the variants. */
10145 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10146 struct type *union_type = alloc_type (objfile);
10147 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10148 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10149 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10150 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10151 TYPE_FIELDS (union_type)
10152 = (struct field *) TYPE_ZALLOC (union_type,
10153 (TYPE_NFIELDS (union_type)
10154 * sizeof (struct field)));
10155
10156 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10157 TYPE_NFIELDS (type) * sizeof (struct field));
10158
10159 /* Install the discriminant at index 0 in the union. */
10160 TYPE_FIELD (union_type, 0) = *disr_field;
10161 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10162 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10163
10164 /* Install the union in the outer struct type. */
10165 TYPE_FIELD_TYPE (type, 0) = union_type;
10166 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10167 TYPE_NFIELDS (type) = 1;
10168
10169 /* Set the size and offset of the union type. */
10170 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10171
10172 /* We need a way to find the correct discriminant given a
10173 variant name. For convenience we build a map here. */
10174 struct type *enum_type = FIELD_TYPE (*disr_field);
10175 std::unordered_map<std::string, ULONGEST> discriminant_map;
10176 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10177 {
10178 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10179 {
10180 const char *name
10181 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10182 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10183 }
10184 }
10185
10186 int n_fields = TYPE_NFIELDS (union_type);
10187 struct discriminant_info *disc
10188 = alloc_discriminant_info (union_type, 0, -1);
10189 /* Skip the discriminant here. */
10190 for (int i = 1; i < n_fields; ++i)
10191 {
10192 /* Find the final word in the name of this variant's type.
10193 That name can be used to look up the correct
10194 discriminant. */
10195 const char *variant_name
10196 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10197 i)));
10198
10199 auto iter = discriminant_map.find (variant_name);
10200 if (iter != discriminant_map.end ())
10201 disc->discriminants[i] = iter->second;
10202
10203 /* Remove the discriminant field, if it exists. */
10204 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10205 if (TYPE_NFIELDS (sub_type) > 0)
10206 {
10207 --TYPE_NFIELDS (sub_type);
10208 ++TYPE_FIELDS (sub_type);
10209 }
10210 TYPE_FIELD_NAME (union_type, i) = variant_name;
10211 TYPE_NAME (sub_type)
10212 = rust_fully_qualify (&objfile->objfile_obstack,
10213 TYPE_NAME (type), variant_name);
10214 }
10215 }
10216 }
10217
10218 /* Rewrite some Rust unions to be structures with variants parts. */
10219
10220 static void
10221 rust_union_quirks (struct dwarf2_cu *cu)
10222 {
10223 gdb_assert (cu->language == language_rust);
10224 for (type *type_ : cu->rust_unions)
10225 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
10226 /* We don't need this any more. */
10227 cu->rust_unions.clear ();
10228 }
10229
10230 /* Return the symtab for PER_CU. This works properly regardless of
10231 whether we're using the index or psymtabs. */
10232
10233 static struct compunit_symtab *
10234 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10235 {
10236 return (per_cu->dwarf2_per_objfile->using_index
10237 ? per_cu->v.quick->compunit_symtab
10238 : per_cu->v.psymtab->compunit_symtab);
10239 }
10240
10241 /* A helper function for computing the list of all symbol tables
10242 included by PER_CU. */
10243
10244 static void
10245 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
10246 htab_t all_children, htab_t all_type_symtabs,
10247 struct dwarf2_per_cu_data *per_cu,
10248 struct compunit_symtab *immediate_parent)
10249 {
10250 void **slot;
10251 int ix;
10252 struct compunit_symtab *cust;
10253 struct dwarf2_per_cu_data *iter;
10254
10255 slot = htab_find_slot (all_children, per_cu, INSERT);
10256 if (*slot != NULL)
10257 {
10258 /* This inclusion and its children have been processed. */
10259 return;
10260 }
10261
10262 *slot = per_cu;
10263 /* Only add a CU if it has a symbol table. */
10264 cust = get_compunit_symtab (per_cu);
10265 if (cust != NULL)
10266 {
10267 /* If this is a type unit only add its symbol table if we haven't
10268 seen it yet (type unit per_cu's can share symtabs). */
10269 if (per_cu->is_debug_types)
10270 {
10271 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10272 if (*slot == NULL)
10273 {
10274 *slot = cust;
10275 result->push_back (cust);
10276 if (cust->user == NULL)
10277 cust->user = immediate_parent;
10278 }
10279 }
10280 else
10281 {
10282 result->push_back (cust);
10283 if (cust->user == NULL)
10284 cust->user = immediate_parent;
10285 }
10286 }
10287
10288 for (ix = 0;
10289 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10290 ++ix)
10291 {
10292 recursively_compute_inclusions (result, all_children,
10293 all_type_symtabs, iter, cust);
10294 }
10295 }
10296
10297 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10298 PER_CU. */
10299
10300 static void
10301 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10302 {
10303 gdb_assert (! per_cu->is_debug_types);
10304
10305 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10306 {
10307 int ix, len;
10308 struct dwarf2_per_cu_data *per_cu_iter;
10309 std::vector<compunit_symtab *> result_symtabs;
10310 htab_t all_children, all_type_symtabs;
10311 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10312
10313 /* If we don't have a symtab, we can just skip this case. */
10314 if (cust == NULL)
10315 return;
10316
10317 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10318 NULL, xcalloc, xfree);
10319 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10320 NULL, xcalloc, xfree);
10321
10322 for (ix = 0;
10323 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10324 ix, per_cu_iter);
10325 ++ix)
10326 {
10327 recursively_compute_inclusions (&result_symtabs, all_children,
10328 all_type_symtabs, per_cu_iter,
10329 cust);
10330 }
10331
10332 /* Now we have a transitive closure of all the included symtabs. */
10333 len = result_symtabs.size ();
10334 cust->includes
10335 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10336 struct compunit_symtab *, len + 1);
10337 memcpy (cust->includes, result_symtabs.data (),
10338 len * sizeof (compunit_symtab *));
10339 cust->includes[len] = NULL;
10340
10341 htab_delete (all_children);
10342 htab_delete (all_type_symtabs);
10343 }
10344 }
10345
10346 /* Compute the 'includes' field for the symtabs of all the CUs we just
10347 read. */
10348
10349 static void
10350 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10351 {
10352 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
10353 {
10354 if (! iter->is_debug_types)
10355 compute_compunit_symtab_includes (iter);
10356 }
10357
10358 dwarf2_per_objfile->just_read_cus.clear ();
10359 }
10360
10361 /* Generate full symbol information for PER_CU, whose DIEs have
10362 already been loaded into memory. */
10363
10364 static void
10365 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10366 enum language pretend_language)
10367 {
10368 struct dwarf2_cu *cu = per_cu->cu;
10369 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10370 struct objfile *objfile = dwarf2_per_objfile->objfile;
10371 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10372 CORE_ADDR lowpc, highpc;
10373 struct compunit_symtab *cust;
10374 CORE_ADDR baseaddr;
10375 struct block *static_block;
10376 CORE_ADDR addr;
10377
10378 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10379
10380 /* Clear the list here in case something was left over. */
10381 cu->method_list.clear ();
10382
10383 cu->language = pretend_language;
10384 cu->language_defn = language_def (cu->language);
10385
10386 /* Do line number decoding in read_file_scope () */
10387 process_die (cu->dies, cu);
10388
10389 /* For now fudge the Go package. */
10390 if (cu->language == language_go)
10391 fixup_go_packaging (cu);
10392
10393 /* Now that we have processed all the DIEs in the CU, all the types
10394 should be complete, and it should now be safe to compute all of the
10395 physnames. */
10396 compute_delayed_physnames (cu);
10397
10398 if (cu->language == language_rust)
10399 rust_union_quirks (cu);
10400
10401 /* Some compilers don't define a DW_AT_high_pc attribute for the
10402 compilation unit. If the DW_AT_high_pc is missing, synthesize
10403 it, by scanning the DIE's below the compilation unit. */
10404 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10405
10406 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10407 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10408
10409 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10410 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10411 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10412 addrmap to help ensure it has an accurate map of pc values belonging to
10413 this comp unit. */
10414 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10415
10416 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10417 SECT_OFF_TEXT (objfile),
10418 0);
10419
10420 if (cust != NULL)
10421 {
10422 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10423
10424 /* Set symtab language to language from DW_AT_language. If the
10425 compilation is from a C file generated by language preprocessors, do
10426 not set the language if it was already deduced by start_subfile. */
10427 if (!(cu->language == language_c
10428 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10429 COMPUNIT_FILETABS (cust)->language = cu->language;
10430
10431 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10432 produce DW_AT_location with location lists but it can be possibly
10433 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10434 there were bugs in prologue debug info, fixed later in GCC-4.5
10435 by "unwind info for epilogues" patch (which is not directly related).
10436
10437 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10438 needed, it would be wrong due to missing DW_AT_producer there.
10439
10440 Still one can confuse GDB by using non-standard GCC compilation
10441 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10442 */
10443 if (cu->has_loclist && gcc_4_minor >= 5)
10444 cust->locations_valid = 1;
10445
10446 if (gcc_4_minor >= 5)
10447 cust->epilogue_unwind_valid = 1;
10448
10449 cust->call_site_htab = cu->call_site_htab;
10450 }
10451
10452 if (dwarf2_per_objfile->using_index)
10453 per_cu->v.quick->compunit_symtab = cust;
10454 else
10455 {
10456 struct partial_symtab *pst = per_cu->v.psymtab;
10457 pst->compunit_symtab = cust;
10458 pst->readin = 1;
10459 }
10460
10461 /* Push it for inclusion processing later. */
10462 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10463
10464 /* Not needed any more. */
10465 cu->reset_builder ();
10466 }
10467
10468 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10469 already been loaded into memory. */
10470
10471 static void
10472 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10473 enum language pretend_language)
10474 {
10475 struct dwarf2_cu *cu = per_cu->cu;
10476 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10477 struct objfile *objfile = dwarf2_per_objfile->objfile;
10478 struct compunit_symtab *cust;
10479 struct signatured_type *sig_type;
10480
10481 gdb_assert (per_cu->is_debug_types);
10482 sig_type = (struct signatured_type *) per_cu;
10483
10484 /* Clear the list here in case something was left over. */
10485 cu->method_list.clear ();
10486
10487 cu->language = pretend_language;
10488 cu->language_defn = language_def (cu->language);
10489
10490 /* The symbol tables are set up in read_type_unit_scope. */
10491 process_die (cu->dies, cu);
10492
10493 /* For now fudge the Go package. */
10494 if (cu->language == language_go)
10495 fixup_go_packaging (cu);
10496
10497 /* Now that we have processed all the DIEs in the CU, all the types
10498 should be complete, and it should now be safe to compute all of the
10499 physnames. */
10500 compute_delayed_physnames (cu);
10501
10502 if (cu->language == language_rust)
10503 rust_union_quirks (cu);
10504
10505 /* TUs share symbol tables.
10506 If this is the first TU to use this symtab, complete the construction
10507 of it with end_expandable_symtab. Otherwise, complete the addition of
10508 this TU's symbols to the existing symtab. */
10509 if (sig_type->type_unit_group->compunit_symtab == NULL)
10510 {
10511 buildsym_compunit *builder = cu->get_builder ();
10512 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10513 sig_type->type_unit_group->compunit_symtab = cust;
10514
10515 if (cust != NULL)
10516 {
10517 /* Set symtab language to language from DW_AT_language. If the
10518 compilation is from a C file generated by language preprocessors,
10519 do not set the language if it was already deduced by
10520 start_subfile. */
10521 if (!(cu->language == language_c
10522 && COMPUNIT_FILETABS (cust)->language != language_c))
10523 COMPUNIT_FILETABS (cust)->language = cu->language;
10524 }
10525 }
10526 else
10527 {
10528 cu->get_builder ()->augment_type_symtab ();
10529 cust = sig_type->type_unit_group->compunit_symtab;
10530 }
10531
10532 if (dwarf2_per_objfile->using_index)
10533 per_cu->v.quick->compunit_symtab = cust;
10534 else
10535 {
10536 struct partial_symtab *pst = per_cu->v.psymtab;
10537 pst->compunit_symtab = cust;
10538 pst->readin = 1;
10539 }
10540
10541 /* Not needed any more. */
10542 cu->reset_builder ();
10543 }
10544
10545 /* Process an imported unit DIE. */
10546
10547 static void
10548 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10549 {
10550 struct attribute *attr;
10551
10552 /* For now we don't handle imported units in type units. */
10553 if (cu->per_cu->is_debug_types)
10554 {
10555 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10556 " supported in type units [in module %s]"),
10557 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10558 }
10559
10560 attr = dwarf2_attr (die, DW_AT_import, cu);
10561 if (attr != NULL)
10562 {
10563 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10564 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10565 dwarf2_per_cu_data *per_cu
10566 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10567 cu->per_cu->dwarf2_per_objfile);
10568
10569 /* If necessary, add it to the queue and load its DIEs. */
10570 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10571 load_full_comp_unit (per_cu, false, cu->language);
10572
10573 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10574 per_cu);
10575 }
10576 }
10577
10578 /* RAII object that represents a process_die scope: i.e.,
10579 starts/finishes processing a DIE. */
10580 class process_die_scope
10581 {
10582 public:
10583 process_die_scope (die_info *die, dwarf2_cu *cu)
10584 : m_die (die), m_cu (cu)
10585 {
10586 /* We should only be processing DIEs not already in process. */
10587 gdb_assert (!m_die->in_process);
10588 m_die->in_process = true;
10589 }
10590
10591 ~process_die_scope ()
10592 {
10593 m_die->in_process = false;
10594
10595 /* If we're done processing the DIE for the CU that owns the line
10596 header, we don't need the line header anymore. */
10597 if (m_cu->line_header_die_owner == m_die)
10598 {
10599 delete m_cu->line_header;
10600 m_cu->line_header = NULL;
10601 m_cu->line_header_die_owner = NULL;
10602 }
10603 }
10604
10605 private:
10606 die_info *m_die;
10607 dwarf2_cu *m_cu;
10608 };
10609
10610 /* Process a die and its children. */
10611
10612 static void
10613 process_die (struct die_info *die, struct dwarf2_cu *cu)
10614 {
10615 process_die_scope scope (die, cu);
10616
10617 switch (die->tag)
10618 {
10619 case DW_TAG_padding:
10620 break;
10621 case DW_TAG_compile_unit:
10622 case DW_TAG_partial_unit:
10623 read_file_scope (die, cu);
10624 break;
10625 case DW_TAG_type_unit:
10626 read_type_unit_scope (die, cu);
10627 break;
10628 case DW_TAG_subprogram:
10629 case DW_TAG_inlined_subroutine:
10630 read_func_scope (die, cu);
10631 break;
10632 case DW_TAG_lexical_block:
10633 case DW_TAG_try_block:
10634 case DW_TAG_catch_block:
10635 read_lexical_block_scope (die, cu);
10636 break;
10637 case DW_TAG_call_site:
10638 case DW_TAG_GNU_call_site:
10639 read_call_site_scope (die, cu);
10640 break;
10641 case DW_TAG_class_type:
10642 case DW_TAG_interface_type:
10643 case DW_TAG_structure_type:
10644 case DW_TAG_union_type:
10645 process_structure_scope (die, cu);
10646 break;
10647 case DW_TAG_enumeration_type:
10648 process_enumeration_scope (die, cu);
10649 break;
10650
10651 /* These dies have a type, but processing them does not create
10652 a symbol or recurse to process the children. Therefore we can
10653 read them on-demand through read_type_die. */
10654 case DW_TAG_subroutine_type:
10655 case DW_TAG_set_type:
10656 case DW_TAG_array_type:
10657 case DW_TAG_pointer_type:
10658 case DW_TAG_ptr_to_member_type:
10659 case DW_TAG_reference_type:
10660 case DW_TAG_rvalue_reference_type:
10661 case DW_TAG_string_type:
10662 break;
10663
10664 case DW_TAG_base_type:
10665 case DW_TAG_subrange_type:
10666 case DW_TAG_typedef:
10667 /* Add a typedef symbol for the type definition, if it has a
10668 DW_AT_name. */
10669 new_symbol (die, read_type_die (die, cu), cu);
10670 break;
10671 case DW_TAG_common_block:
10672 read_common_block (die, cu);
10673 break;
10674 case DW_TAG_common_inclusion:
10675 break;
10676 case DW_TAG_namespace:
10677 cu->processing_has_namespace_info = true;
10678 read_namespace (die, cu);
10679 break;
10680 case DW_TAG_module:
10681 cu->processing_has_namespace_info = true;
10682 read_module (die, cu);
10683 break;
10684 case DW_TAG_imported_declaration:
10685 cu->processing_has_namespace_info = true;
10686 if (read_namespace_alias (die, cu))
10687 break;
10688 /* The declaration is not a global namespace alias. */
10689 /* Fall through. */
10690 case DW_TAG_imported_module:
10691 cu->processing_has_namespace_info = true;
10692 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10693 || cu->language != language_fortran))
10694 complaint (_("Tag '%s' has unexpected children"),
10695 dwarf_tag_name (die->tag));
10696 read_import_statement (die, cu);
10697 break;
10698
10699 case DW_TAG_imported_unit:
10700 process_imported_unit_die (die, cu);
10701 break;
10702
10703 case DW_TAG_variable:
10704 read_variable (die, cu);
10705 break;
10706
10707 default:
10708 new_symbol (die, NULL, cu);
10709 break;
10710 }
10711 }
10712 \f
10713 /* DWARF name computation. */
10714
10715 /* A helper function for dwarf2_compute_name which determines whether DIE
10716 needs to have the name of the scope prepended to the name listed in the
10717 die. */
10718
10719 static int
10720 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10721 {
10722 struct attribute *attr;
10723
10724 switch (die->tag)
10725 {
10726 case DW_TAG_namespace:
10727 case DW_TAG_typedef:
10728 case DW_TAG_class_type:
10729 case DW_TAG_interface_type:
10730 case DW_TAG_structure_type:
10731 case DW_TAG_union_type:
10732 case DW_TAG_enumeration_type:
10733 case DW_TAG_enumerator:
10734 case DW_TAG_subprogram:
10735 case DW_TAG_inlined_subroutine:
10736 case DW_TAG_member:
10737 case DW_TAG_imported_declaration:
10738 return 1;
10739
10740 case DW_TAG_variable:
10741 case DW_TAG_constant:
10742 /* We only need to prefix "globally" visible variables. These include
10743 any variable marked with DW_AT_external or any variable that
10744 lives in a namespace. [Variables in anonymous namespaces
10745 require prefixing, but they are not DW_AT_external.] */
10746
10747 if (dwarf2_attr (die, DW_AT_specification, cu))
10748 {
10749 struct dwarf2_cu *spec_cu = cu;
10750
10751 return die_needs_namespace (die_specification (die, &spec_cu),
10752 spec_cu);
10753 }
10754
10755 attr = dwarf2_attr (die, DW_AT_external, cu);
10756 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10757 && die->parent->tag != DW_TAG_module)
10758 return 0;
10759 /* A variable in a lexical block of some kind does not need a
10760 namespace, even though in C++ such variables may be external
10761 and have a mangled name. */
10762 if (die->parent->tag == DW_TAG_lexical_block
10763 || die->parent->tag == DW_TAG_try_block
10764 || die->parent->tag == DW_TAG_catch_block
10765 || die->parent->tag == DW_TAG_subprogram)
10766 return 0;
10767 return 1;
10768
10769 default:
10770 return 0;
10771 }
10772 }
10773
10774 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10775 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10776 defined for the given DIE. */
10777
10778 static struct attribute *
10779 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10780 {
10781 struct attribute *attr;
10782
10783 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10784 if (attr == NULL)
10785 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10786
10787 return attr;
10788 }
10789
10790 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10791 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10792 defined for the given DIE. */
10793
10794 static const char *
10795 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10796 {
10797 const char *linkage_name;
10798
10799 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10800 if (linkage_name == NULL)
10801 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10802
10803 return linkage_name;
10804 }
10805
10806 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10807 compute the physname for the object, which include a method's:
10808 - formal parameters (C++),
10809 - receiver type (Go),
10810
10811 The term "physname" is a bit confusing.
10812 For C++, for example, it is the demangled name.
10813 For Go, for example, it's the mangled name.
10814
10815 For Ada, return the DIE's linkage name rather than the fully qualified
10816 name. PHYSNAME is ignored..
10817
10818 The result is allocated on the objfile_obstack and canonicalized. */
10819
10820 static const char *
10821 dwarf2_compute_name (const char *name,
10822 struct die_info *die, struct dwarf2_cu *cu,
10823 int physname)
10824 {
10825 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10826
10827 if (name == NULL)
10828 name = dwarf2_name (die, cu);
10829
10830 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10831 but otherwise compute it by typename_concat inside GDB.
10832 FIXME: Actually this is not really true, or at least not always true.
10833 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10834 Fortran names because there is no mangling standard. So new_symbol
10835 will set the demangled name to the result of dwarf2_full_name, and it is
10836 the demangled name that GDB uses if it exists. */
10837 if (cu->language == language_ada
10838 || (cu->language == language_fortran && physname))
10839 {
10840 /* For Ada unit, we prefer the linkage name over the name, as
10841 the former contains the exported name, which the user expects
10842 to be able to reference. Ideally, we want the user to be able
10843 to reference this entity using either natural or linkage name,
10844 but we haven't started looking at this enhancement yet. */
10845 const char *linkage_name = dw2_linkage_name (die, cu);
10846
10847 if (linkage_name != NULL)
10848 return linkage_name;
10849 }
10850
10851 /* These are the only languages we know how to qualify names in. */
10852 if (name != NULL
10853 && (cu->language == language_cplus
10854 || cu->language == language_fortran || cu->language == language_d
10855 || cu->language == language_rust))
10856 {
10857 if (die_needs_namespace (die, cu))
10858 {
10859 const char *prefix;
10860 const char *canonical_name = NULL;
10861
10862 string_file buf;
10863
10864 prefix = determine_prefix (die, cu);
10865 if (*prefix != '\0')
10866 {
10867 char *prefixed_name = typename_concat (NULL, prefix, name,
10868 physname, cu);
10869
10870 buf.puts (prefixed_name);
10871 xfree (prefixed_name);
10872 }
10873 else
10874 buf.puts (name);
10875
10876 /* Template parameters may be specified in the DIE's DW_AT_name, or
10877 as children with DW_TAG_template_type_param or
10878 DW_TAG_value_type_param. If the latter, add them to the name
10879 here. If the name already has template parameters, then
10880 skip this step; some versions of GCC emit both, and
10881 it is more efficient to use the pre-computed name.
10882
10883 Something to keep in mind about this process: it is very
10884 unlikely, or in some cases downright impossible, to produce
10885 something that will match the mangled name of a function.
10886 If the definition of the function has the same debug info,
10887 we should be able to match up with it anyway. But fallbacks
10888 using the minimal symbol, for instance to find a method
10889 implemented in a stripped copy of libstdc++, will not work.
10890 If we do not have debug info for the definition, we will have to
10891 match them up some other way.
10892
10893 When we do name matching there is a related problem with function
10894 templates; two instantiated function templates are allowed to
10895 differ only by their return types, which we do not add here. */
10896
10897 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10898 {
10899 struct attribute *attr;
10900 struct die_info *child;
10901 int first = 1;
10902
10903 die->building_fullname = 1;
10904
10905 for (child = die->child; child != NULL; child = child->sibling)
10906 {
10907 struct type *type;
10908 LONGEST value;
10909 const gdb_byte *bytes;
10910 struct dwarf2_locexpr_baton *baton;
10911 struct value *v;
10912
10913 if (child->tag != DW_TAG_template_type_param
10914 && child->tag != DW_TAG_template_value_param)
10915 continue;
10916
10917 if (first)
10918 {
10919 buf.puts ("<");
10920 first = 0;
10921 }
10922 else
10923 buf.puts (", ");
10924
10925 attr = dwarf2_attr (child, DW_AT_type, cu);
10926 if (attr == NULL)
10927 {
10928 complaint (_("template parameter missing DW_AT_type"));
10929 buf.puts ("UNKNOWN_TYPE");
10930 continue;
10931 }
10932 type = die_type (child, cu);
10933
10934 if (child->tag == DW_TAG_template_type_param)
10935 {
10936 c_print_type (type, "", &buf, -1, 0, cu->language,
10937 &type_print_raw_options);
10938 continue;
10939 }
10940
10941 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10942 if (attr == NULL)
10943 {
10944 complaint (_("template parameter missing "
10945 "DW_AT_const_value"));
10946 buf.puts ("UNKNOWN_VALUE");
10947 continue;
10948 }
10949
10950 dwarf2_const_value_attr (attr, type, name,
10951 &cu->comp_unit_obstack, cu,
10952 &value, &bytes, &baton);
10953
10954 if (TYPE_NOSIGN (type))
10955 /* GDB prints characters as NUMBER 'CHAR'. If that's
10956 changed, this can use value_print instead. */
10957 c_printchar (value, type, &buf);
10958 else
10959 {
10960 struct value_print_options opts;
10961
10962 if (baton != NULL)
10963 v = dwarf2_evaluate_loc_desc (type, NULL,
10964 baton->data,
10965 baton->size,
10966 baton->per_cu);
10967 else if (bytes != NULL)
10968 {
10969 v = allocate_value (type);
10970 memcpy (value_contents_writeable (v), bytes,
10971 TYPE_LENGTH (type));
10972 }
10973 else
10974 v = value_from_longest (type, value);
10975
10976 /* Specify decimal so that we do not depend on
10977 the radix. */
10978 get_formatted_print_options (&opts, 'd');
10979 opts.raw = 1;
10980 value_print (v, &buf, &opts);
10981 release_value (v);
10982 }
10983 }
10984
10985 die->building_fullname = 0;
10986
10987 if (!first)
10988 {
10989 /* Close the argument list, with a space if necessary
10990 (nested templates). */
10991 if (!buf.empty () && buf.string ().back () == '>')
10992 buf.puts (" >");
10993 else
10994 buf.puts (">");
10995 }
10996 }
10997
10998 /* For C++ methods, append formal parameter type
10999 information, if PHYSNAME. */
11000
11001 if (physname && die->tag == DW_TAG_subprogram
11002 && cu->language == language_cplus)
11003 {
11004 struct type *type = read_type_die (die, cu);
11005
11006 c_type_print_args (type, &buf, 1, cu->language,
11007 &type_print_raw_options);
11008
11009 if (cu->language == language_cplus)
11010 {
11011 /* Assume that an artificial first parameter is
11012 "this", but do not crash if it is not. RealView
11013 marks unnamed (and thus unused) parameters as
11014 artificial; there is no way to differentiate
11015 the two cases. */
11016 if (TYPE_NFIELDS (type) > 0
11017 && TYPE_FIELD_ARTIFICIAL (type, 0)
11018 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11019 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11020 0))))
11021 buf.puts (" const");
11022 }
11023 }
11024
11025 const std::string &intermediate_name = buf.string ();
11026
11027 if (cu->language == language_cplus)
11028 canonical_name
11029 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11030 &objfile->per_bfd->storage_obstack);
11031
11032 /* If we only computed INTERMEDIATE_NAME, or if
11033 INTERMEDIATE_NAME is already canonical, then we need to
11034 copy it to the appropriate obstack. */
11035 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11036 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11037 intermediate_name);
11038 else
11039 name = canonical_name;
11040 }
11041 }
11042
11043 return name;
11044 }
11045
11046 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11047 If scope qualifiers are appropriate they will be added. The result
11048 will be allocated on the storage_obstack, or NULL if the DIE does
11049 not have a name. NAME may either be from a previous call to
11050 dwarf2_name or NULL.
11051
11052 The output string will be canonicalized (if C++). */
11053
11054 static const char *
11055 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11056 {
11057 return dwarf2_compute_name (name, die, cu, 0);
11058 }
11059
11060 /* Construct a physname for the given DIE in CU. NAME may either be
11061 from a previous call to dwarf2_name or NULL. The result will be
11062 allocated on the objfile_objstack or NULL if the DIE does not have a
11063 name.
11064
11065 The output string will be canonicalized (if C++). */
11066
11067 static const char *
11068 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11069 {
11070 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11071 const char *retval, *mangled = NULL, *canon = NULL;
11072 int need_copy = 1;
11073
11074 /* In this case dwarf2_compute_name is just a shortcut not building anything
11075 on its own. */
11076 if (!die_needs_namespace (die, cu))
11077 return dwarf2_compute_name (name, die, cu, 1);
11078
11079 mangled = dw2_linkage_name (die, cu);
11080
11081 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11082 See https://github.com/rust-lang/rust/issues/32925. */
11083 if (cu->language == language_rust && mangled != NULL
11084 && strchr (mangled, '{') != NULL)
11085 mangled = NULL;
11086
11087 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11088 has computed. */
11089 gdb::unique_xmalloc_ptr<char> demangled;
11090 if (mangled != NULL)
11091 {
11092
11093 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11094 {
11095 /* Do nothing (do not demangle the symbol name). */
11096 }
11097 else if (cu->language == language_go)
11098 {
11099 /* This is a lie, but we already lie to the caller new_symbol.
11100 new_symbol assumes we return the mangled name.
11101 This just undoes that lie until things are cleaned up. */
11102 }
11103 else
11104 {
11105 /* Use DMGL_RET_DROP for C++ template functions to suppress
11106 their return type. It is easier for GDB users to search
11107 for such functions as `name(params)' than `long name(params)'.
11108 In such case the minimal symbol names do not match the full
11109 symbol names but for template functions there is never a need
11110 to look up their definition from their declaration so
11111 the only disadvantage remains the minimal symbol variant
11112 `long name(params)' does not have the proper inferior type. */
11113 demangled.reset (gdb_demangle (mangled,
11114 (DMGL_PARAMS | DMGL_ANSI
11115 | DMGL_RET_DROP)));
11116 }
11117 if (demangled)
11118 canon = demangled.get ();
11119 else
11120 {
11121 canon = mangled;
11122 need_copy = 0;
11123 }
11124 }
11125
11126 if (canon == NULL || check_physname)
11127 {
11128 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11129
11130 if (canon != NULL && strcmp (physname, canon) != 0)
11131 {
11132 /* It may not mean a bug in GDB. The compiler could also
11133 compute DW_AT_linkage_name incorrectly. But in such case
11134 GDB would need to be bug-to-bug compatible. */
11135
11136 complaint (_("Computed physname <%s> does not match demangled <%s> "
11137 "(from linkage <%s>) - DIE at %s [in module %s]"),
11138 physname, canon, mangled, sect_offset_str (die->sect_off),
11139 objfile_name (objfile));
11140
11141 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11142 is available here - over computed PHYSNAME. It is safer
11143 against both buggy GDB and buggy compilers. */
11144
11145 retval = canon;
11146 }
11147 else
11148 {
11149 retval = physname;
11150 need_copy = 0;
11151 }
11152 }
11153 else
11154 retval = canon;
11155
11156 if (need_copy)
11157 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
11158
11159 return retval;
11160 }
11161
11162 /* Inspect DIE in CU for a namespace alias. If one exists, record
11163 a new symbol for it.
11164
11165 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11166
11167 static int
11168 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11169 {
11170 struct attribute *attr;
11171
11172 /* If the die does not have a name, this is not a namespace
11173 alias. */
11174 attr = dwarf2_attr (die, DW_AT_name, cu);
11175 if (attr != NULL)
11176 {
11177 int num;
11178 struct die_info *d = die;
11179 struct dwarf2_cu *imported_cu = cu;
11180
11181 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11182 keep inspecting DIEs until we hit the underlying import. */
11183 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11184 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11185 {
11186 attr = dwarf2_attr (d, DW_AT_import, cu);
11187 if (attr == NULL)
11188 break;
11189
11190 d = follow_die_ref (d, attr, &imported_cu);
11191 if (d->tag != DW_TAG_imported_declaration)
11192 break;
11193 }
11194
11195 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11196 {
11197 complaint (_("DIE at %s has too many recursively imported "
11198 "declarations"), sect_offset_str (d->sect_off));
11199 return 0;
11200 }
11201
11202 if (attr != NULL)
11203 {
11204 struct type *type;
11205 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11206
11207 type = get_die_type_at_offset (sect_off, cu->per_cu);
11208 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11209 {
11210 /* This declaration is a global namespace alias. Add
11211 a symbol for it whose type is the aliased namespace. */
11212 new_symbol (die, type, cu);
11213 return 1;
11214 }
11215 }
11216 }
11217
11218 return 0;
11219 }
11220
11221 /* Return the using directives repository (global or local?) to use in the
11222 current context for CU.
11223
11224 For Ada, imported declarations can materialize renamings, which *may* be
11225 global. However it is impossible (for now?) in DWARF to distinguish
11226 "external" imported declarations and "static" ones. As all imported
11227 declarations seem to be static in all other languages, make them all CU-wide
11228 global only in Ada. */
11229
11230 static struct using_direct **
11231 using_directives (struct dwarf2_cu *cu)
11232 {
11233 if (cu->language == language_ada
11234 && cu->get_builder ()->outermost_context_p ())
11235 return cu->get_builder ()->get_global_using_directives ();
11236 else
11237 return cu->get_builder ()->get_local_using_directives ();
11238 }
11239
11240 /* Read the import statement specified by the given die and record it. */
11241
11242 static void
11243 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11244 {
11245 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11246 struct attribute *import_attr;
11247 struct die_info *imported_die, *child_die;
11248 struct dwarf2_cu *imported_cu;
11249 const char *imported_name;
11250 const char *imported_name_prefix;
11251 const char *canonical_name;
11252 const char *import_alias;
11253 const char *imported_declaration = NULL;
11254 const char *import_prefix;
11255 std::vector<const char *> excludes;
11256
11257 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11258 if (import_attr == NULL)
11259 {
11260 complaint (_("Tag '%s' has no DW_AT_import"),
11261 dwarf_tag_name (die->tag));
11262 return;
11263 }
11264
11265 imported_cu = cu;
11266 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11267 imported_name = dwarf2_name (imported_die, imported_cu);
11268 if (imported_name == NULL)
11269 {
11270 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11271
11272 The import in the following code:
11273 namespace A
11274 {
11275 typedef int B;
11276 }
11277
11278 int main ()
11279 {
11280 using A::B;
11281 B b;
11282 return b;
11283 }
11284
11285 ...
11286 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11287 <52> DW_AT_decl_file : 1
11288 <53> DW_AT_decl_line : 6
11289 <54> DW_AT_import : <0x75>
11290 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11291 <59> DW_AT_name : B
11292 <5b> DW_AT_decl_file : 1
11293 <5c> DW_AT_decl_line : 2
11294 <5d> DW_AT_type : <0x6e>
11295 ...
11296 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11297 <76> DW_AT_byte_size : 4
11298 <77> DW_AT_encoding : 5 (signed)
11299
11300 imports the wrong die ( 0x75 instead of 0x58 ).
11301 This case will be ignored until the gcc bug is fixed. */
11302 return;
11303 }
11304
11305 /* Figure out the local name after import. */
11306 import_alias = dwarf2_name (die, cu);
11307
11308 /* Figure out where the statement is being imported to. */
11309 import_prefix = determine_prefix (die, cu);
11310
11311 /* Figure out what the scope of the imported die is and prepend it
11312 to the name of the imported die. */
11313 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11314
11315 if (imported_die->tag != DW_TAG_namespace
11316 && imported_die->tag != DW_TAG_module)
11317 {
11318 imported_declaration = imported_name;
11319 canonical_name = imported_name_prefix;
11320 }
11321 else if (strlen (imported_name_prefix) > 0)
11322 canonical_name = obconcat (&objfile->objfile_obstack,
11323 imported_name_prefix,
11324 (cu->language == language_d ? "." : "::"),
11325 imported_name, (char *) NULL);
11326 else
11327 canonical_name = imported_name;
11328
11329 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11330 for (child_die = die->child; child_die && child_die->tag;
11331 child_die = sibling_die (child_die))
11332 {
11333 /* DWARF-4: A Fortran use statement with a “rename list” may be
11334 represented by an imported module entry with an import attribute
11335 referring to the module and owned entries corresponding to those
11336 entities that are renamed as part of being imported. */
11337
11338 if (child_die->tag != DW_TAG_imported_declaration)
11339 {
11340 complaint (_("child DW_TAG_imported_declaration expected "
11341 "- DIE at %s [in module %s]"),
11342 sect_offset_str (child_die->sect_off),
11343 objfile_name (objfile));
11344 continue;
11345 }
11346
11347 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11348 if (import_attr == NULL)
11349 {
11350 complaint (_("Tag '%s' has no DW_AT_import"),
11351 dwarf_tag_name (child_die->tag));
11352 continue;
11353 }
11354
11355 imported_cu = cu;
11356 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11357 &imported_cu);
11358 imported_name = dwarf2_name (imported_die, imported_cu);
11359 if (imported_name == NULL)
11360 {
11361 complaint (_("child DW_TAG_imported_declaration has unknown "
11362 "imported name - DIE at %s [in module %s]"),
11363 sect_offset_str (child_die->sect_off),
11364 objfile_name (objfile));
11365 continue;
11366 }
11367
11368 excludes.push_back (imported_name);
11369
11370 process_die (child_die, cu);
11371 }
11372
11373 add_using_directive (using_directives (cu),
11374 import_prefix,
11375 canonical_name,
11376 import_alias,
11377 imported_declaration,
11378 excludes,
11379 0,
11380 &objfile->objfile_obstack);
11381 }
11382
11383 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11384 types, but gives them a size of zero. Starting with version 14,
11385 ICC is compatible with GCC. */
11386
11387 static bool
11388 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11389 {
11390 if (!cu->checked_producer)
11391 check_producer (cu);
11392
11393 return cu->producer_is_icc_lt_14;
11394 }
11395
11396 /* ICC generates a DW_AT_type for C void functions. This was observed on
11397 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11398 which says that void functions should not have a DW_AT_type. */
11399
11400 static bool
11401 producer_is_icc (struct dwarf2_cu *cu)
11402 {
11403 if (!cu->checked_producer)
11404 check_producer (cu);
11405
11406 return cu->producer_is_icc;
11407 }
11408
11409 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11410 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11411 this, it was first present in GCC release 4.3.0. */
11412
11413 static bool
11414 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11415 {
11416 if (!cu->checked_producer)
11417 check_producer (cu);
11418
11419 return cu->producer_is_gcc_lt_4_3;
11420 }
11421
11422 static file_and_directory
11423 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11424 {
11425 file_and_directory res;
11426
11427 /* Find the filename. Do not use dwarf2_name here, since the filename
11428 is not a source language identifier. */
11429 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11430 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11431
11432 if (res.comp_dir == NULL
11433 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11434 && IS_ABSOLUTE_PATH (res.name))
11435 {
11436 res.comp_dir_storage = ldirname (res.name);
11437 if (!res.comp_dir_storage.empty ())
11438 res.comp_dir = res.comp_dir_storage.c_str ();
11439 }
11440 if (res.comp_dir != NULL)
11441 {
11442 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11443 directory, get rid of it. */
11444 const char *cp = strchr (res.comp_dir, ':');
11445
11446 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11447 res.comp_dir = cp + 1;
11448 }
11449
11450 if (res.name == NULL)
11451 res.name = "<unknown>";
11452
11453 return res;
11454 }
11455
11456 /* Handle DW_AT_stmt_list for a compilation unit.
11457 DIE is the DW_TAG_compile_unit die for CU.
11458 COMP_DIR is the compilation directory. LOWPC is passed to
11459 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11460
11461 static void
11462 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11463 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11464 {
11465 struct dwarf2_per_objfile *dwarf2_per_objfile
11466 = cu->per_cu->dwarf2_per_objfile;
11467 struct objfile *objfile = dwarf2_per_objfile->objfile;
11468 struct attribute *attr;
11469 struct line_header line_header_local;
11470 hashval_t line_header_local_hash;
11471 void **slot;
11472 int decode_mapping;
11473
11474 gdb_assert (! cu->per_cu->is_debug_types);
11475
11476 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11477 if (attr == NULL)
11478 return;
11479
11480 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11481
11482 /* The line header hash table is only created if needed (it exists to
11483 prevent redundant reading of the line table for partial_units).
11484 If we're given a partial_unit, we'll need it. If we're given a
11485 compile_unit, then use the line header hash table if it's already
11486 created, but don't create one just yet. */
11487
11488 if (dwarf2_per_objfile->line_header_hash == NULL
11489 && die->tag == DW_TAG_partial_unit)
11490 {
11491 dwarf2_per_objfile->line_header_hash
11492 = htab_create_alloc_ex (127, line_header_hash_voidp,
11493 line_header_eq_voidp,
11494 free_line_header_voidp,
11495 &objfile->objfile_obstack,
11496 hashtab_obstack_allocate,
11497 dummy_obstack_deallocate);
11498 }
11499
11500 line_header_local.sect_off = line_offset;
11501 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11502 line_header_local_hash = line_header_hash (&line_header_local);
11503 if (dwarf2_per_objfile->line_header_hash != NULL)
11504 {
11505 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11506 &line_header_local,
11507 line_header_local_hash, NO_INSERT);
11508
11509 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11510 is not present in *SLOT (since if there is something in *SLOT then
11511 it will be for a partial_unit). */
11512 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11513 {
11514 gdb_assert (*slot != NULL);
11515 cu->line_header = (struct line_header *) *slot;
11516 return;
11517 }
11518 }
11519
11520 /* dwarf_decode_line_header does not yet provide sufficient information.
11521 We always have to call also dwarf_decode_lines for it. */
11522 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11523 if (lh == NULL)
11524 return;
11525
11526 cu->line_header = lh.release ();
11527 cu->line_header_die_owner = die;
11528
11529 if (dwarf2_per_objfile->line_header_hash == NULL)
11530 slot = NULL;
11531 else
11532 {
11533 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11534 &line_header_local,
11535 line_header_local_hash, INSERT);
11536 gdb_assert (slot != NULL);
11537 }
11538 if (slot != NULL && *slot == NULL)
11539 {
11540 /* This newly decoded line number information unit will be owned
11541 by line_header_hash hash table. */
11542 *slot = cu->line_header;
11543 cu->line_header_die_owner = NULL;
11544 }
11545 else
11546 {
11547 /* We cannot free any current entry in (*slot) as that struct line_header
11548 may be already used by multiple CUs. Create only temporary decoded
11549 line_header for this CU - it may happen at most once for each line
11550 number information unit. And if we're not using line_header_hash
11551 then this is what we want as well. */
11552 gdb_assert (die->tag != DW_TAG_partial_unit);
11553 }
11554 decode_mapping = (die->tag != DW_TAG_partial_unit);
11555 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11556 decode_mapping);
11557
11558 }
11559
11560 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11561
11562 static void
11563 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11564 {
11565 struct dwarf2_per_objfile *dwarf2_per_objfile
11566 = cu->per_cu->dwarf2_per_objfile;
11567 struct objfile *objfile = dwarf2_per_objfile->objfile;
11568 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11569 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11570 CORE_ADDR highpc = ((CORE_ADDR) 0);
11571 struct attribute *attr;
11572 struct die_info *child_die;
11573 CORE_ADDR baseaddr;
11574
11575 prepare_one_comp_unit (cu, die, cu->language);
11576 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11577
11578 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11579
11580 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11581 from finish_block. */
11582 if (lowpc == ((CORE_ADDR) -1))
11583 lowpc = highpc;
11584 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11585
11586 file_and_directory fnd = find_file_and_directory (die, cu);
11587
11588 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11589 standardised yet. As a workaround for the language detection we fall
11590 back to the DW_AT_producer string. */
11591 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11592 cu->language = language_opencl;
11593
11594 /* Similar hack for Go. */
11595 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11596 set_cu_language (DW_LANG_Go, cu);
11597
11598 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11599
11600 /* Decode line number information if present. We do this before
11601 processing child DIEs, so that the line header table is available
11602 for DW_AT_decl_file. */
11603 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11604
11605 /* Process all dies in compilation unit. */
11606 if (die->child != NULL)
11607 {
11608 child_die = die->child;
11609 while (child_die && child_die->tag)
11610 {
11611 process_die (child_die, cu);
11612 child_die = sibling_die (child_die);
11613 }
11614 }
11615
11616 /* Decode macro information, if present. Dwarf 2 macro information
11617 refers to information in the line number info statement program
11618 header, so we can only read it if we've read the header
11619 successfully. */
11620 attr = dwarf2_attr (die, DW_AT_macros, cu);
11621 if (attr == NULL)
11622 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11623 if (attr && cu->line_header)
11624 {
11625 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11626 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11627
11628 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11629 }
11630 else
11631 {
11632 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11633 if (attr && cu->line_header)
11634 {
11635 unsigned int macro_offset = DW_UNSND (attr);
11636
11637 dwarf_decode_macros (cu, macro_offset, 0);
11638 }
11639 }
11640 }
11641
11642 void
11643 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11644 {
11645 struct type_unit_group *tu_group;
11646 int first_time;
11647 struct attribute *attr;
11648 unsigned int i;
11649 struct signatured_type *sig_type;
11650
11651 gdb_assert (per_cu->is_debug_types);
11652 sig_type = (struct signatured_type *) per_cu;
11653
11654 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11655
11656 /* If we're using .gdb_index (includes -readnow) then
11657 per_cu->type_unit_group may not have been set up yet. */
11658 if (sig_type->type_unit_group == NULL)
11659 sig_type->type_unit_group = get_type_unit_group (this, attr);
11660 tu_group = sig_type->type_unit_group;
11661
11662 /* If we've already processed this stmt_list there's no real need to
11663 do it again, we could fake it and just recreate the part we need
11664 (file name,index -> symtab mapping). If data shows this optimization
11665 is useful we can do it then. */
11666 first_time = tu_group->compunit_symtab == NULL;
11667
11668 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11669 debug info. */
11670 line_header_up lh;
11671 if (attr != NULL)
11672 {
11673 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11674 lh = dwarf_decode_line_header (line_offset, this);
11675 }
11676 if (lh == NULL)
11677 {
11678 if (first_time)
11679 start_symtab ("", NULL, 0);
11680 else
11681 {
11682 gdb_assert (tu_group->symtabs == NULL);
11683 gdb_assert (m_builder == nullptr);
11684 struct compunit_symtab *cust = tu_group->compunit_symtab;
11685 m_builder.reset (new struct buildsym_compunit
11686 (COMPUNIT_OBJFILE (cust), "",
11687 COMPUNIT_DIRNAME (cust),
11688 compunit_language (cust),
11689 0, cust));
11690 }
11691 return;
11692 }
11693
11694 line_header = lh.release ();
11695 line_header_die_owner = die;
11696
11697 if (first_time)
11698 {
11699 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11700
11701 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11702 still initializing it, and our caller (a few levels up)
11703 process_full_type_unit still needs to know if this is the first
11704 time. */
11705
11706 tu_group->num_symtabs = line_header->file_names.size ();
11707 tu_group->symtabs = XNEWVEC (struct symtab *,
11708 line_header->file_names.size ());
11709
11710 for (i = 0; i < line_header->file_names.size (); ++i)
11711 {
11712 file_entry &fe = line_header->file_names[i];
11713
11714 dwarf2_start_subfile (this, fe.name,
11715 fe.include_dir (line_header));
11716 buildsym_compunit *b = get_builder ();
11717 if (b->get_current_subfile ()->symtab == NULL)
11718 {
11719 /* NOTE: start_subfile will recognize when it's been
11720 passed a file it has already seen. So we can't
11721 assume there's a simple mapping from
11722 cu->line_header->file_names to subfiles, plus
11723 cu->line_header->file_names may contain dups. */
11724 b->get_current_subfile ()->symtab
11725 = allocate_symtab (cust, b->get_current_subfile ()->name);
11726 }
11727
11728 fe.symtab = b->get_current_subfile ()->symtab;
11729 tu_group->symtabs[i] = fe.symtab;
11730 }
11731 }
11732 else
11733 {
11734 gdb_assert (m_builder == nullptr);
11735 struct compunit_symtab *cust = tu_group->compunit_symtab;
11736 m_builder.reset (new struct buildsym_compunit
11737 (COMPUNIT_OBJFILE (cust), "",
11738 COMPUNIT_DIRNAME (cust),
11739 compunit_language (cust),
11740 0, cust));
11741
11742 for (i = 0; i < line_header->file_names.size (); ++i)
11743 {
11744 file_entry &fe = line_header->file_names[i];
11745
11746 fe.symtab = tu_group->symtabs[i];
11747 }
11748 }
11749
11750 /* The main symtab is allocated last. Type units don't have DW_AT_name
11751 so they don't have a "real" (so to speak) symtab anyway.
11752 There is later code that will assign the main symtab to all symbols
11753 that don't have one. We need to handle the case of a symbol with a
11754 missing symtab (DW_AT_decl_file) anyway. */
11755 }
11756
11757 /* Process DW_TAG_type_unit.
11758 For TUs we want to skip the first top level sibling if it's not the
11759 actual type being defined by this TU. In this case the first top
11760 level sibling is there to provide context only. */
11761
11762 static void
11763 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11764 {
11765 struct die_info *child_die;
11766
11767 prepare_one_comp_unit (cu, die, language_minimal);
11768
11769 /* Initialize (or reinitialize) the machinery for building symtabs.
11770 We do this before processing child DIEs, so that the line header table
11771 is available for DW_AT_decl_file. */
11772 cu->setup_type_unit_groups (die);
11773
11774 if (die->child != NULL)
11775 {
11776 child_die = die->child;
11777 while (child_die && child_die->tag)
11778 {
11779 process_die (child_die, cu);
11780 child_die = sibling_die (child_die);
11781 }
11782 }
11783 }
11784 \f
11785 /* DWO/DWP files.
11786
11787 http://gcc.gnu.org/wiki/DebugFission
11788 http://gcc.gnu.org/wiki/DebugFissionDWP
11789
11790 To simplify handling of both DWO files ("object" files with the DWARF info)
11791 and DWP files (a file with the DWOs packaged up into one file), we treat
11792 DWP files as having a collection of virtual DWO files. */
11793
11794 static hashval_t
11795 hash_dwo_file (const void *item)
11796 {
11797 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11798 hashval_t hash;
11799
11800 hash = htab_hash_string (dwo_file->dwo_name);
11801 if (dwo_file->comp_dir != NULL)
11802 hash += htab_hash_string (dwo_file->comp_dir);
11803 return hash;
11804 }
11805
11806 static int
11807 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11808 {
11809 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11810 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11811
11812 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11813 return 0;
11814 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11815 return lhs->comp_dir == rhs->comp_dir;
11816 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11817 }
11818
11819 /* Allocate a hash table for DWO files. */
11820
11821 static htab_up
11822 allocate_dwo_file_hash_table (struct objfile *objfile)
11823 {
11824 auto delete_dwo_file = [] (void *item)
11825 {
11826 struct dwo_file *dwo_file = (struct dwo_file *) item;
11827
11828 delete dwo_file;
11829 };
11830
11831 return htab_up (htab_create_alloc_ex (41,
11832 hash_dwo_file,
11833 eq_dwo_file,
11834 delete_dwo_file,
11835 &objfile->objfile_obstack,
11836 hashtab_obstack_allocate,
11837 dummy_obstack_deallocate));
11838 }
11839
11840 /* Lookup DWO file DWO_NAME. */
11841
11842 static void **
11843 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11844 const char *dwo_name,
11845 const char *comp_dir)
11846 {
11847 struct dwo_file find_entry;
11848 void **slot;
11849
11850 if (dwarf2_per_objfile->dwo_files == NULL)
11851 dwarf2_per_objfile->dwo_files
11852 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11853
11854 find_entry.dwo_name = dwo_name;
11855 find_entry.comp_dir = comp_dir;
11856 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11857 INSERT);
11858
11859 return slot;
11860 }
11861
11862 static hashval_t
11863 hash_dwo_unit (const void *item)
11864 {
11865 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11866
11867 /* This drops the top 32 bits of the id, but is ok for a hash. */
11868 return dwo_unit->signature;
11869 }
11870
11871 static int
11872 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11873 {
11874 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11875 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11876
11877 /* The signature is assumed to be unique within the DWO file.
11878 So while object file CU dwo_id's always have the value zero,
11879 that's OK, assuming each object file DWO file has only one CU,
11880 and that's the rule for now. */
11881 return lhs->signature == rhs->signature;
11882 }
11883
11884 /* Allocate a hash table for DWO CUs,TUs.
11885 There is one of these tables for each of CUs,TUs for each DWO file. */
11886
11887 static htab_t
11888 allocate_dwo_unit_table (struct objfile *objfile)
11889 {
11890 /* Start out with a pretty small number.
11891 Generally DWO files contain only one CU and maybe some TUs. */
11892 return htab_create_alloc_ex (3,
11893 hash_dwo_unit,
11894 eq_dwo_unit,
11895 NULL,
11896 &objfile->objfile_obstack,
11897 hashtab_obstack_allocate,
11898 dummy_obstack_deallocate);
11899 }
11900
11901 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11902
11903 struct create_dwo_cu_data
11904 {
11905 struct dwo_file *dwo_file;
11906 struct dwo_unit dwo_unit;
11907 };
11908
11909 /* die_reader_func for create_dwo_cu. */
11910
11911 static void
11912 create_dwo_cu_reader (const struct die_reader_specs *reader,
11913 const gdb_byte *info_ptr,
11914 struct die_info *comp_unit_die,
11915 int has_children,
11916 void *datap)
11917 {
11918 struct dwarf2_cu *cu = reader->cu;
11919 sect_offset sect_off = cu->per_cu->sect_off;
11920 struct dwarf2_section_info *section = cu->per_cu->section;
11921 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11922 struct dwo_file *dwo_file = data->dwo_file;
11923 struct dwo_unit *dwo_unit = &data->dwo_unit;
11924
11925 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11926 if (!signature.has_value ())
11927 {
11928 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11929 " its dwo_id [in module %s]"),
11930 sect_offset_str (sect_off), dwo_file->dwo_name);
11931 return;
11932 }
11933
11934 dwo_unit->dwo_file = dwo_file;
11935 dwo_unit->signature = *signature;
11936 dwo_unit->section = section;
11937 dwo_unit->sect_off = sect_off;
11938 dwo_unit->length = cu->per_cu->length;
11939
11940 if (dwarf_read_debug)
11941 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11942 sect_offset_str (sect_off),
11943 hex_string (dwo_unit->signature));
11944 }
11945
11946 /* Create the dwo_units for the CUs in a DWO_FILE.
11947 Note: This function processes DWO files only, not DWP files. */
11948
11949 static void
11950 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11951 struct dwo_file &dwo_file, dwarf2_section_info &section,
11952 htab_t &cus_htab)
11953 {
11954 struct objfile *objfile = dwarf2_per_objfile->objfile;
11955 const gdb_byte *info_ptr, *end_ptr;
11956
11957 dwarf2_read_section (objfile, &section);
11958 info_ptr = section.buffer;
11959
11960 if (info_ptr == NULL)
11961 return;
11962
11963 if (dwarf_read_debug)
11964 {
11965 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11966 get_section_name (&section),
11967 get_section_file_name (&section));
11968 }
11969
11970 end_ptr = info_ptr + section.size;
11971 while (info_ptr < end_ptr)
11972 {
11973 struct dwarf2_per_cu_data per_cu;
11974 struct create_dwo_cu_data create_dwo_cu_data;
11975 struct dwo_unit *dwo_unit;
11976 void **slot;
11977 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11978
11979 memset (&create_dwo_cu_data.dwo_unit, 0,
11980 sizeof (create_dwo_cu_data.dwo_unit));
11981 memset (&per_cu, 0, sizeof (per_cu));
11982 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11983 per_cu.is_debug_types = 0;
11984 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11985 per_cu.section = &section;
11986 create_dwo_cu_data.dwo_file = &dwo_file;
11987
11988 init_cutu_and_read_dies_no_follow (
11989 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11990 info_ptr += per_cu.length;
11991
11992 // If the unit could not be parsed, skip it.
11993 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11994 continue;
11995
11996 if (cus_htab == NULL)
11997 cus_htab = allocate_dwo_unit_table (objfile);
11998
11999 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12000 *dwo_unit = create_dwo_cu_data.dwo_unit;
12001 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12002 gdb_assert (slot != NULL);
12003 if (*slot != NULL)
12004 {
12005 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12006 sect_offset dup_sect_off = dup_cu->sect_off;
12007
12008 complaint (_("debug cu entry at offset %s is duplicate to"
12009 " the entry at offset %s, signature %s"),
12010 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
12011 hex_string (dwo_unit->signature));
12012 }
12013 *slot = (void *)dwo_unit;
12014 }
12015 }
12016
12017 /* DWP file .debug_{cu,tu}_index section format:
12018 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12019
12020 DWP Version 1:
12021
12022 Both index sections have the same format, and serve to map a 64-bit
12023 signature to a set of section numbers. Each section begins with a header,
12024 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12025 indexes, and a pool of 32-bit section numbers. The index sections will be
12026 aligned at 8-byte boundaries in the file.
12027
12028 The index section header consists of:
12029
12030 V, 32 bit version number
12031 -, 32 bits unused
12032 N, 32 bit number of compilation units or type units in the index
12033 M, 32 bit number of slots in the hash table
12034
12035 Numbers are recorded using the byte order of the application binary.
12036
12037 The hash table begins at offset 16 in the section, and consists of an array
12038 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12039 order of the application binary). Unused slots in the hash table are 0.
12040 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12041
12042 The parallel table begins immediately after the hash table
12043 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12044 array of 32-bit indexes (using the byte order of the application binary),
12045 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12046 table contains a 32-bit index into the pool of section numbers. For unused
12047 hash table slots, the corresponding entry in the parallel table will be 0.
12048
12049 The pool of section numbers begins immediately following the hash table
12050 (at offset 16 + 12 * M from the beginning of the section). The pool of
12051 section numbers consists of an array of 32-bit words (using the byte order
12052 of the application binary). Each item in the array is indexed starting
12053 from 0. The hash table entry provides the index of the first section
12054 number in the set. Additional section numbers in the set follow, and the
12055 set is terminated by a 0 entry (section number 0 is not used in ELF).
12056
12057 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12058 section must be the first entry in the set, and the .debug_abbrev.dwo must
12059 be the second entry. Other members of the set may follow in any order.
12060
12061 ---
12062
12063 DWP Version 2:
12064
12065 DWP Version 2 combines all the .debug_info, etc. sections into one,
12066 and the entries in the index tables are now offsets into these sections.
12067 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12068 section.
12069
12070 Index Section Contents:
12071 Header
12072 Hash Table of Signatures dwp_hash_table.hash_table
12073 Parallel Table of Indices dwp_hash_table.unit_table
12074 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12075 Table of Section Sizes dwp_hash_table.v2.sizes
12076
12077 The index section header consists of:
12078
12079 V, 32 bit version number
12080 L, 32 bit number of columns in the table of section offsets
12081 N, 32 bit number of compilation units or type units in the index
12082 M, 32 bit number of slots in the hash table
12083
12084 Numbers are recorded using the byte order of the application binary.
12085
12086 The hash table has the same format as version 1.
12087 The parallel table of indices has the same format as version 1,
12088 except that the entries are origin-1 indices into the table of sections
12089 offsets and the table of section sizes.
12090
12091 The table of offsets begins immediately following the parallel table
12092 (at offset 16 + 12 * M from the beginning of the section). The table is
12093 a two-dimensional array of 32-bit words (using the byte order of the
12094 application binary), with L columns and N+1 rows, in row-major order.
12095 Each row in the array is indexed starting from 0. The first row provides
12096 a key to the remaining rows: each column in this row provides an identifier
12097 for a debug section, and the offsets in the same column of subsequent rows
12098 refer to that section. The section identifiers are:
12099
12100 DW_SECT_INFO 1 .debug_info.dwo
12101 DW_SECT_TYPES 2 .debug_types.dwo
12102 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12103 DW_SECT_LINE 4 .debug_line.dwo
12104 DW_SECT_LOC 5 .debug_loc.dwo
12105 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12106 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12107 DW_SECT_MACRO 8 .debug_macro.dwo
12108
12109 The offsets provided by the CU and TU index sections are the base offsets
12110 for the contributions made by each CU or TU to the corresponding section
12111 in the package file. Each CU and TU header contains an abbrev_offset
12112 field, used to find the abbreviations table for that CU or TU within the
12113 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12114 be interpreted as relative to the base offset given in the index section.
12115 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12116 should be interpreted as relative to the base offset for .debug_line.dwo,
12117 and offsets into other debug sections obtained from DWARF attributes should
12118 also be interpreted as relative to the corresponding base offset.
12119
12120 The table of sizes begins immediately following the table of offsets.
12121 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12122 with L columns and N rows, in row-major order. Each row in the array is
12123 indexed starting from 1 (row 0 is shared by the two tables).
12124
12125 ---
12126
12127 Hash table lookup is handled the same in version 1 and 2:
12128
12129 We assume that N and M will not exceed 2^32 - 1.
12130 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12131
12132 Given a 64-bit compilation unit signature or a type signature S, an entry
12133 in the hash table is located as follows:
12134
12135 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12136 the low-order k bits all set to 1.
12137
12138 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12139
12140 3) If the hash table entry at index H matches the signature, use that
12141 entry. If the hash table entry at index H is unused (all zeroes),
12142 terminate the search: the signature is not present in the table.
12143
12144 4) Let H = (H + H') modulo M. Repeat at Step 3.
12145
12146 Because M > N and H' and M are relatively prime, the search is guaranteed
12147 to stop at an unused slot or find the match. */
12148
12149 /* Create a hash table to map DWO IDs to their CU/TU entry in
12150 .debug_{info,types}.dwo in DWP_FILE.
12151 Returns NULL if there isn't one.
12152 Note: This function processes DWP files only, not DWO files. */
12153
12154 static struct dwp_hash_table *
12155 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12156 struct dwp_file *dwp_file, int is_debug_types)
12157 {
12158 struct objfile *objfile = dwarf2_per_objfile->objfile;
12159 bfd *dbfd = dwp_file->dbfd.get ();
12160 const gdb_byte *index_ptr, *index_end;
12161 struct dwarf2_section_info *index;
12162 uint32_t version, nr_columns, nr_units, nr_slots;
12163 struct dwp_hash_table *htab;
12164
12165 if (is_debug_types)
12166 index = &dwp_file->sections.tu_index;
12167 else
12168 index = &dwp_file->sections.cu_index;
12169
12170 if (dwarf2_section_empty_p (index))
12171 return NULL;
12172 dwarf2_read_section (objfile, index);
12173
12174 index_ptr = index->buffer;
12175 index_end = index_ptr + index->size;
12176
12177 version = read_4_bytes (dbfd, index_ptr);
12178 index_ptr += 4;
12179 if (version == 2)
12180 nr_columns = read_4_bytes (dbfd, index_ptr);
12181 else
12182 nr_columns = 0;
12183 index_ptr += 4;
12184 nr_units = read_4_bytes (dbfd, index_ptr);
12185 index_ptr += 4;
12186 nr_slots = read_4_bytes (dbfd, index_ptr);
12187 index_ptr += 4;
12188
12189 if (version != 1 && version != 2)
12190 {
12191 error (_("Dwarf Error: unsupported DWP file version (%s)"
12192 " [in module %s]"),
12193 pulongest (version), dwp_file->name);
12194 }
12195 if (nr_slots != (nr_slots & -nr_slots))
12196 {
12197 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12198 " is not power of 2 [in module %s]"),
12199 pulongest (nr_slots), dwp_file->name);
12200 }
12201
12202 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12203 htab->version = version;
12204 htab->nr_columns = nr_columns;
12205 htab->nr_units = nr_units;
12206 htab->nr_slots = nr_slots;
12207 htab->hash_table = index_ptr;
12208 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12209
12210 /* Exit early if the table is empty. */
12211 if (nr_slots == 0 || nr_units == 0
12212 || (version == 2 && nr_columns == 0))
12213 {
12214 /* All must be zero. */
12215 if (nr_slots != 0 || nr_units != 0
12216 || (version == 2 && nr_columns != 0))
12217 {
12218 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
12219 " all zero [in modules %s]"),
12220 dwp_file->name);
12221 }
12222 return htab;
12223 }
12224
12225 if (version == 1)
12226 {
12227 htab->section_pool.v1.indices =
12228 htab->unit_table + sizeof (uint32_t) * nr_slots;
12229 /* It's harder to decide whether the section is too small in v1.
12230 V1 is deprecated anyway so we punt. */
12231 }
12232 else
12233 {
12234 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12235 int *ids = htab->section_pool.v2.section_ids;
12236 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
12237 /* Reverse map for error checking. */
12238 int ids_seen[DW_SECT_MAX + 1];
12239 int i;
12240
12241 if (nr_columns < 2)
12242 {
12243 error (_("Dwarf Error: bad DWP hash table, too few columns"
12244 " in section table [in module %s]"),
12245 dwp_file->name);
12246 }
12247 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12248 {
12249 error (_("Dwarf Error: bad DWP hash table, too many columns"
12250 " in section table [in module %s]"),
12251 dwp_file->name);
12252 }
12253 memset (ids, 255, sizeof_ids);
12254 memset (ids_seen, 255, sizeof (ids_seen));
12255 for (i = 0; i < nr_columns; ++i)
12256 {
12257 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12258
12259 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12260 {
12261 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12262 " in section table [in module %s]"),
12263 id, dwp_file->name);
12264 }
12265 if (ids_seen[id] != -1)
12266 {
12267 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12268 " id %d in section table [in module %s]"),
12269 id, dwp_file->name);
12270 }
12271 ids_seen[id] = i;
12272 ids[i] = id;
12273 }
12274 /* Must have exactly one info or types section. */
12275 if (((ids_seen[DW_SECT_INFO] != -1)
12276 + (ids_seen[DW_SECT_TYPES] != -1))
12277 != 1)
12278 {
12279 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12280 " DWO info/types section [in module %s]"),
12281 dwp_file->name);
12282 }
12283 /* Must have an abbrev section. */
12284 if (ids_seen[DW_SECT_ABBREV] == -1)
12285 {
12286 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12287 " section [in module %s]"),
12288 dwp_file->name);
12289 }
12290 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12291 htab->section_pool.v2.sizes =
12292 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12293 * nr_units * nr_columns);
12294 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12295 * nr_units * nr_columns))
12296 > index_end)
12297 {
12298 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12299 " [in module %s]"),
12300 dwp_file->name);
12301 }
12302 }
12303
12304 return htab;
12305 }
12306
12307 /* Update SECTIONS with the data from SECTP.
12308
12309 This function is like the other "locate" section routines that are
12310 passed to bfd_map_over_sections, but in this context the sections to
12311 read comes from the DWP V1 hash table, not the full ELF section table.
12312
12313 The result is non-zero for success, or zero if an error was found. */
12314
12315 static int
12316 locate_v1_virtual_dwo_sections (asection *sectp,
12317 struct virtual_v1_dwo_sections *sections)
12318 {
12319 const struct dwop_section_names *names = &dwop_section_names;
12320
12321 if (section_is_p (sectp->name, &names->abbrev_dwo))
12322 {
12323 /* There can be only one. */
12324 if (sections->abbrev.s.section != NULL)
12325 return 0;
12326 sections->abbrev.s.section = sectp;
12327 sections->abbrev.size = bfd_section_size (sectp);
12328 }
12329 else if (section_is_p (sectp->name, &names->info_dwo)
12330 || section_is_p (sectp->name, &names->types_dwo))
12331 {
12332 /* There can be only one. */
12333 if (sections->info_or_types.s.section != NULL)
12334 return 0;
12335 sections->info_or_types.s.section = sectp;
12336 sections->info_or_types.size = bfd_section_size (sectp);
12337 }
12338 else if (section_is_p (sectp->name, &names->line_dwo))
12339 {
12340 /* There can be only one. */
12341 if (sections->line.s.section != NULL)
12342 return 0;
12343 sections->line.s.section = sectp;
12344 sections->line.size = bfd_section_size (sectp);
12345 }
12346 else if (section_is_p (sectp->name, &names->loc_dwo))
12347 {
12348 /* There can be only one. */
12349 if (sections->loc.s.section != NULL)
12350 return 0;
12351 sections->loc.s.section = sectp;
12352 sections->loc.size = bfd_section_size (sectp);
12353 }
12354 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12355 {
12356 /* There can be only one. */
12357 if (sections->macinfo.s.section != NULL)
12358 return 0;
12359 sections->macinfo.s.section = sectp;
12360 sections->macinfo.size = bfd_section_size (sectp);
12361 }
12362 else if (section_is_p (sectp->name, &names->macro_dwo))
12363 {
12364 /* There can be only one. */
12365 if (sections->macro.s.section != NULL)
12366 return 0;
12367 sections->macro.s.section = sectp;
12368 sections->macro.size = bfd_section_size (sectp);
12369 }
12370 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12371 {
12372 /* There can be only one. */
12373 if (sections->str_offsets.s.section != NULL)
12374 return 0;
12375 sections->str_offsets.s.section = sectp;
12376 sections->str_offsets.size = bfd_section_size (sectp);
12377 }
12378 else
12379 {
12380 /* No other kind of section is valid. */
12381 return 0;
12382 }
12383
12384 return 1;
12385 }
12386
12387 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12388 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12389 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12390 This is for DWP version 1 files. */
12391
12392 static struct dwo_unit *
12393 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12394 struct dwp_file *dwp_file,
12395 uint32_t unit_index,
12396 const char *comp_dir,
12397 ULONGEST signature, int is_debug_types)
12398 {
12399 struct objfile *objfile = dwarf2_per_objfile->objfile;
12400 const struct dwp_hash_table *dwp_htab =
12401 is_debug_types ? dwp_file->tus : dwp_file->cus;
12402 bfd *dbfd = dwp_file->dbfd.get ();
12403 const char *kind = is_debug_types ? "TU" : "CU";
12404 struct dwo_file *dwo_file;
12405 struct dwo_unit *dwo_unit;
12406 struct virtual_v1_dwo_sections sections;
12407 void **dwo_file_slot;
12408 int i;
12409
12410 gdb_assert (dwp_file->version == 1);
12411
12412 if (dwarf_read_debug)
12413 {
12414 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12415 kind,
12416 pulongest (unit_index), hex_string (signature),
12417 dwp_file->name);
12418 }
12419
12420 /* Fetch the sections of this DWO unit.
12421 Put a limit on the number of sections we look for so that bad data
12422 doesn't cause us to loop forever. */
12423
12424 #define MAX_NR_V1_DWO_SECTIONS \
12425 (1 /* .debug_info or .debug_types */ \
12426 + 1 /* .debug_abbrev */ \
12427 + 1 /* .debug_line */ \
12428 + 1 /* .debug_loc */ \
12429 + 1 /* .debug_str_offsets */ \
12430 + 1 /* .debug_macro or .debug_macinfo */ \
12431 + 1 /* trailing zero */)
12432
12433 memset (&sections, 0, sizeof (sections));
12434
12435 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12436 {
12437 asection *sectp;
12438 uint32_t section_nr =
12439 read_4_bytes (dbfd,
12440 dwp_htab->section_pool.v1.indices
12441 + (unit_index + i) * sizeof (uint32_t));
12442
12443 if (section_nr == 0)
12444 break;
12445 if (section_nr >= dwp_file->num_sections)
12446 {
12447 error (_("Dwarf Error: bad DWP hash table, section number too large"
12448 " [in module %s]"),
12449 dwp_file->name);
12450 }
12451
12452 sectp = dwp_file->elf_sections[section_nr];
12453 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12454 {
12455 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12456 " [in module %s]"),
12457 dwp_file->name);
12458 }
12459 }
12460
12461 if (i < 2
12462 || dwarf2_section_empty_p (&sections.info_or_types)
12463 || dwarf2_section_empty_p (&sections.abbrev))
12464 {
12465 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12466 " [in module %s]"),
12467 dwp_file->name);
12468 }
12469 if (i == MAX_NR_V1_DWO_SECTIONS)
12470 {
12471 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12472 " [in module %s]"),
12473 dwp_file->name);
12474 }
12475
12476 /* It's easier for the rest of the code if we fake a struct dwo_file and
12477 have dwo_unit "live" in that. At least for now.
12478
12479 The DWP file can be made up of a random collection of CUs and TUs.
12480 However, for each CU + set of TUs that came from the same original DWO
12481 file, we can combine them back into a virtual DWO file to save space
12482 (fewer struct dwo_file objects to allocate). Remember that for really
12483 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12484
12485 std::string virtual_dwo_name =
12486 string_printf ("virtual-dwo/%d-%d-%d-%d",
12487 get_section_id (&sections.abbrev),
12488 get_section_id (&sections.line),
12489 get_section_id (&sections.loc),
12490 get_section_id (&sections.str_offsets));
12491 /* Can we use an existing virtual DWO file? */
12492 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12493 virtual_dwo_name.c_str (),
12494 comp_dir);
12495 /* Create one if necessary. */
12496 if (*dwo_file_slot == NULL)
12497 {
12498 if (dwarf_read_debug)
12499 {
12500 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12501 virtual_dwo_name.c_str ());
12502 }
12503 dwo_file = new struct dwo_file;
12504 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12505 virtual_dwo_name);
12506 dwo_file->comp_dir = comp_dir;
12507 dwo_file->sections.abbrev = sections.abbrev;
12508 dwo_file->sections.line = sections.line;
12509 dwo_file->sections.loc = sections.loc;
12510 dwo_file->sections.macinfo = sections.macinfo;
12511 dwo_file->sections.macro = sections.macro;
12512 dwo_file->sections.str_offsets = sections.str_offsets;
12513 /* The "str" section is global to the entire DWP file. */
12514 dwo_file->sections.str = dwp_file->sections.str;
12515 /* The info or types section is assigned below to dwo_unit,
12516 there's no need to record it in dwo_file.
12517 Also, we can't simply record type sections in dwo_file because
12518 we record a pointer into the vector in dwo_unit. As we collect more
12519 types we'll grow the vector and eventually have to reallocate space
12520 for it, invalidating all copies of pointers into the previous
12521 contents. */
12522 *dwo_file_slot = dwo_file;
12523 }
12524 else
12525 {
12526 if (dwarf_read_debug)
12527 {
12528 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12529 virtual_dwo_name.c_str ());
12530 }
12531 dwo_file = (struct dwo_file *) *dwo_file_slot;
12532 }
12533
12534 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12535 dwo_unit->dwo_file = dwo_file;
12536 dwo_unit->signature = signature;
12537 dwo_unit->section =
12538 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12539 *dwo_unit->section = sections.info_or_types;
12540 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12541
12542 return dwo_unit;
12543 }
12544
12545 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12546 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12547 piece within that section used by a TU/CU, return a virtual section
12548 of just that piece. */
12549
12550 static struct dwarf2_section_info
12551 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12552 struct dwarf2_section_info *section,
12553 bfd_size_type offset, bfd_size_type size)
12554 {
12555 struct dwarf2_section_info result;
12556 asection *sectp;
12557
12558 gdb_assert (section != NULL);
12559 gdb_assert (!section->is_virtual);
12560
12561 memset (&result, 0, sizeof (result));
12562 result.s.containing_section = section;
12563 result.is_virtual = true;
12564
12565 if (size == 0)
12566 return result;
12567
12568 sectp = get_section_bfd_section (section);
12569
12570 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12571 bounds of the real section. This is a pretty-rare event, so just
12572 flag an error (easier) instead of a warning and trying to cope. */
12573 if (sectp == NULL
12574 || offset + size > bfd_section_size (sectp))
12575 {
12576 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12577 " in section %s [in module %s]"),
12578 sectp ? bfd_section_name (sectp) : "<unknown>",
12579 objfile_name (dwarf2_per_objfile->objfile));
12580 }
12581
12582 result.virtual_offset = offset;
12583 result.size = size;
12584 return result;
12585 }
12586
12587 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12588 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12589 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12590 This is for DWP version 2 files. */
12591
12592 static struct dwo_unit *
12593 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12594 struct dwp_file *dwp_file,
12595 uint32_t unit_index,
12596 const char *comp_dir,
12597 ULONGEST signature, int is_debug_types)
12598 {
12599 struct objfile *objfile = dwarf2_per_objfile->objfile;
12600 const struct dwp_hash_table *dwp_htab =
12601 is_debug_types ? dwp_file->tus : dwp_file->cus;
12602 bfd *dbfd = dwp_file->dbfd.get ();
12603 const char *kind = is_debug_types ? "TU" : "CU";
12604 struct dwo_file *dwo_file;
12605 struct dwo_unit *dwo_unit;
12606 struct virtual_v2_dwo_sections sections;
12607 void **dwo_file_slot;
12608 int i;
12609
12610 gdb_assert (dwp_file->version == 2);
12611
12612 if (dwarf_read_debug)
12613 {
12614 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12615 kind,
12616 pulongest (unit_index), hex_string (signature),
12617 dwp_file->name);
12618 }
12619
12620 /* Fetch the section offsets of this DWO unit. */
12621
12622 memset (&sections, 0, sizeof (sections));
12623
12624 for (i = 0; i < dwp_htab->nr_columns; ++i)
12625 {
12626 uint32_t offset = read_4_bytes (dbfd,
12627 dwp_htab->section_pool.v2.offsets
12628 + (((unit_index - 1) * dwp_htab->nr_columns
12629 + i)
12630 * sizeof (uint32_t)));
12631 uint32_t size = read_4_bytes (dbfd,
12632 dwp_htab->section_pool.v2.sizes
12633 + (((unit_index - 1) * dwp_htab->nr_columns
12634 + i)
12635 * sizeof (uint32_t)));
12636
12637 switch (dwp_htab->section_pool.v2.section_ids[i])
12638 {
12639 case DW_SECT_INFO:
12640 case DW_SECT_TYPES:
12641 sections.info_or_types_offset = offset;
12642 sections.info_or_types_size = size;
12643 break;
12644 case DW_SECT_ABBREV:
12645 sections.abbrev_offset = offset;
12646 sections.abbrev_size = size;
12647 break;
12648 case DW_SECT_LINE:
12649 sections.line_offset = offset;
12650 sections.line_size = size;
12651 break;
12652 case DW_SECT_LOC:
12653 sections.loc_offset = offset;
12654 sections.loc_size = size;
12655 break;
12656 case DW_SECT_STR_OFFSETS:
12657 sections.str_offsets_offset = offset;
12658 sections.str_offsets_size = size;
12659 break;
12660 case DW_SECT_MACINFO:
12661 sections.macinfo_offset = offset;
12662 sections.macinfo_size = size;
12663 break;
12664 case DW_SECT_MACRO:
12665 sections.macro_offset = offset;
12666 sections.macro_size = size;
12667 break;
12668 }
12669 }
12670
12671 /* It's easier for the rest of the code if we fake a struct dwo_file and
12672 have dwo_unit "live" in that. At least for now.
12673
12674 The DWP file can be made up of a random collection of CUs and TUs.
12675 However, for each CU + set of TUs that came from the same original DWO
12676 file, we can combine them back into a virtual DWO file to save space
12677 (fewer struct dwo_file objects to allocate). Remember that for really
12678 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12679
12680 std::string virtual_dwo_name =
12681 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12682 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12683 (long) (sections.line_size ? sections.line_offset : 0),
12684 (long) (sections.loc_size ? sections.loc_offset : 0),
12685 (long) (sections.str_offsets_size
12686 ? sections.str_offsets_offset : 0));
12687 /* Can we use an existing virtual DWO file? */
12688 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12689 virtual_dwo_name.c_str (),
12690 comp_dir);
12691 /* Create one if necessary. */
12692 if (*dwo_file_slot == NULL)
12693 {
12694 if (dwarf_read_debug)
12695 {
12696 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12697 virtual_dwo_name.c_str ());
12698 }
12699 dwo_file = new struct dwo_file;
12700 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12701 virtual_dwo_name);
12702 dwo_file->comp_dir = comp_dir;
12703 dwo_file->sections.abbrev =
12704 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12705 sections.abbrev_offset, sections.abbrev_size);
12706 dwo_file->sections.line =
12707 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12708 sections.line_offset, sections.line_size);
12709 dwo_file->sections.loc =
12710 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12711 sections.loc_offset, sections.loc_size);
12712 dwo_file->sections.macinfo =
12713 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12714 sections.macinfo_offset, sections.macinfo_size);
12715 dwo_file->sections.macro =
12716 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12717 sections.macro_offset, sections.macro_size);
12718 dwo_file->sections.str_offsets =
12719 create_dwp_v2_section (dwarf2_per_objfile,
12720 &dwp_file->sections.str_offsets,
12721 sections.str_offsets_offset,
12722 sections.str_offsets_size);
12723 /* The "str" section is global to the entire DWP file. */
12724 dwo_file->sections.str = dwp_file->sections.str;
12725 /* The info or types section is assigned below to dwo_unit,
12726 there's no need to record it in dwo_file.
12727 Also, we can't simply record type sections in dwo_file because
12728 we record a pointer into the vector in dwo_unit. As we collect more
12729 types we'll grow the vector and eventually have to reallocate space
12730 for it, invalidating all copies of pointers into the previous
12731 contents. */
12732 *dwo_file_slot = dwo_file;
12733 }
12734 else
12735 {
12736 if (dwarf_read_debug)
12737 {
12738 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12739 virtual_dwo_name.c_str ());
12740 }
12741 dwo_file = (struct dwo_file *) *dwo_file_slot;
12742 }
12743
12744 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12745 dwo_unit->dwo_file = dwo_file;
12746 dwo_unit->signature = signature;
12747 dwo_unit->section =
12748 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12749 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12750 is_debug_types
12751 ? &dwp_file->sections.types
12752 : &dwp_file->sections.info,
12753 sections.info_or_types_offset,
12754 sections.info_or_types_size);
12755 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12756
12757 return dwo_unit;
12758 }
12759
12760 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12761 Returns NULL if the signature isn't found. */
12762
12763 static struct dwo_unit *
12764 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12765 struct dwp_file *dwp_file, const char *comp_dir,
12766 ULONGEST signature, int is_debug_types)
12767 {
12768 const struct dwp_hash_table *dwp_htab =
12769 is_debug_types ? dwp_file->tus : dwp_file->cus;
12770 bfd *dbfd = dwp_file->dbfd.get ();
12771 uint32_t mask = dwp_htab->nr_slots - 1;
12772 uint32_t hash = signature & mask;
12773 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12774 unsigned int i;
12775 void **slot;
12776 struct dwo_unit find_dwo_cu;
12777
12778 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12779 find_dwo_cu.signature = signature;
12780 slot = htab_find_slot (is_debug_types
12781 ? dwp_file->loaded_tus
12782 : dwp_file->loaded_cus,
12783 &find_dwo_cu, INSERT);
12784
12785 if (*slot != NULL)
12786 return (struct dwo_unit *) *slot;
12787
12788 /* Use a for loop so that we don't loop forever on bad debug info. */
12789 for (i = 0; i < dwp_htab->nr_slots; ++i)
12790 {
12791 ULONGEST signature_in_table;
12792
12793 signature_in_table =
12794 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12795 if (signature_in_table == signature)
12796 {
12797 uint32_t unit_index =
12798 read_4_bytes (dbfd,
12799 dwp_htab->unit_table + hash * sizeof (uint32_t));
12800
12801 if (dwp_file->version == 1)
12802 {
12803 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12804 dwp_file, unit_index,
12805 comp_dir, signature,
12806 is_debug_types);
12807 }
12808 else
12809 {
12810 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12811 dwp_file, unit_index,
12812 comp_dir, signature,
12813 is_debug_types);
12814 }
12815 return (struct dwo_unit *) *slot;
12816 }
12817 if (signature_in_table == 0)
12818 return NULL;
12819 hash = (hash + hash2) & mask;
12820 }
12821
12822 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12823 " [in module %s]"),
12824 dwp_file->name);
12825 }
12826
12827 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12828 Open the file specified by FILE_NAME and hand it off to BFD for
12829 preliminary analysis. Return a newly initialized bfd *, which
12830 includes a canonicalized copy of FILE_NAME.
12831 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12832 SEARCH_CWD is true if the current directory is to be searched.
12833 It will be searched before debug-file-directory.
12834 If successful, the file is added to the bfd include table of the
12835 objfile's bfd (see gdb_bfd_record_inclusion).
12836 If unable to find/open the file, return NULL.
12837 NOTE: This function is derived from symfile_bfd_open. */
12838
12839 static gdb_bfd_ref_ptr
12840 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12841 const char *file_name, int is_dwp, int search_cwd)
12842 {
12843 int desc;
12844 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12845 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12846 to debug_file_directory. */
12847 const char *search_path;
12848 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12849
12850 gdb::unique_xmalloc_ptr<char> search_path_holder;
12851 if (search_cwd)
12852 {
12853 if (*debug_file_directory != '\0')
12854 {
12855 search_path_holder.reset (concat (".", dirname_separator_string,
12856 debug_file_directory,
12857 (char *) NULL));
12858 search_path = search_path_holder.get ();
12859 }
12860 else
12861 search_path = ".";
12862 }
12863 else
12864 search_path = debug_file_directory;
12865
12866 openp_flags flags = OPF_RETURN_REALPATH;
12867 if (is_dwp)
12868 flags |= OPF_SEARCH_IN_PATH;
12869
12870 gdb::unique_xmalloc_ptr<char> absolute_name;
12871 desc = openp (search_path, flags, file_name,
12872 O_RDONLY | O_BINARY, &absolute_name);
12873 if (desc < 0)
12874 return NULL;
12875
12876 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12877 gnutarget, desc));
12878 if (sym_bfd == NULL)
12879 return NULL;
12880 bfd_set_cacheable (sym_bfd.get (), 1);
12881
12882 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12883 return NULL;
12884
12885 /* Success. Record the bfd as having been included by the objfile's bfd.
12886 This is important because things like demangled_names_hash lives in the
12887 objfile's per_bfd space and may have references to things like symbol
12888 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12889 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12890
12891 return sym_bfd;
12892 }
12893
12894 /* Try to open DWO file FILE_NAME.
12895 COMP_DIR is the DW_AT_comp_dir attribute.
12896 The result is the bfd handle of the file.
12897 If there is a problem finding or opening the file, return NULL.
12898 Upon success, the canonicalized path of the file is stored in the bfd,
12899 same as symfile_bfd_open. */
12900
12901 static gdb_bfd_ref_ptr
12902 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12903 const char *file_name, const char *comp_dir)
12904 {
12905 if (IS_ABSOLUTE_PATH (file_name))
12906 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12907 0 /*is_dwp*/, 0 /*search_cwd*/);
12908
12909 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12910
12911 if (comp_dir != NULL)
12912 {
12913 char *path_to_try = concat (comp_dir, SLASH_STRING,
12914 file_name, (char *) NULL);
12915
12916 /* NOTE: If comp_dir is a relative path, this will also try the
12917 search path, which seems useful. */
12918 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12919 path_to_try,
12920 0 /*is_dwp*/,
12921 1 /*search_cwd*/));
12922 xfree (path_to_try);
12923 if (abfd != NULL)
12924 return abfd;
12925 }
12926
12927 /* That didn't work, try debug-file-directory, which, despite its name,
12928 is a list of paths. */
12929
12930 if (*debug_file_directory == '\0')
12931 return NULL;
12932
12933 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12934 0 /*is_dwp*/, 1 /*search_cwd*/);
12935 }
12936
12937 /* This function is mapped across the sections and remembers the offset and
12938 size of each of the DWO debugging sections we are interested in. */
12939
12940 static void
12941 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12942 {
12943 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12944 const struct dwop_section_names *names = &dwop_section_names;
12945
12946 if (section_is_p (sectp->name, &names->abbrev_dwo))
12947 {
12948 dwo_sections->abbrev.s.section = sectp;
12949 dwo_sections->abbrev.size = bfd_section_size (sectp);
12950 }
12951 else if (section_is_p (sectp->name, &names->info_dwo))
12952 {
12953 dwo_sections->info.s.section = sectp;
12954 dwo_sections->info.size = bfd_section_size (sectp);
12955 }
12956 else if (section_is_p (sectp->name, &names->line_dwo))
12957 {
12958 dwo_sections->line.s.section = sectp;
12959 dwo_sections->line.size = bfd_section_size (sectp);
12960 }
12961 else if (section_is_p (sectp->name, &names->loc_dwo))
12962 {
12963 dwo_sections->loc.s.section = sectp;
12964 dwo_sections->loc.size = bfd_section_size (sectp);
12965 }
12966 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12967 {
12968 dwo_sections->macinfo.s.section = sectp;
12969 dwo_sections->macinfo.size = bfd_section_size (sectp);
12970 }
12971 else if (section_is_p (sectp->name, &names->macro_dwo))
12972 {
12973 dwo_sections->macro.s.section = sectp;
12974 dwo_sections->macro.size = bfd_section_size (sectp);
12975 }
12976 else if (section_is_p (sectp->name, &names->str_dwo))
12977 {
12978 dwo_sections->str.s.section = sectp;
12979 dwo_sections->str.size = bfd_section_size (sectp);
12980 }
12981 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12982 {
12983 dwo_sections->str_offsets.s.section = sectp;
12984 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12985 }
12986 else if (section_is_p (sectp->name, &names->types_dwo))
12987 {
12988 struct dwarf2_section_info type_section;
12989
12990 memset (&type_section, 0, sizeof (type_section));
12991 type_section.s.section = sectp;
12992 type_section.size = bfd_section_size (sectp);
12993 dwo_sections->types.push_back (type_section);
12994 }
12995 }
12996
12997 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12998 by PER_CU. This is for the non-DWP case.
12999 The result is NULL if DWO_NAME can't be found. */
13000
13001 static struct dwo_file *
13002 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13003 const char *dwo_name, const char *comp_dir)
13004 {
13005 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13006
13007 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
13008 if (dbfd == NULL)
13009 {
13010 if (dwarf_read_debug)
13011 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13012 return NULL;
13013 }
13014
13015 dwo_file_up dwo_file (new struct dwo_file);
13016 dwo_file->dwo_name = dwo_name;
13017 dwo_file->comp_dir = comp_dir;
13018 dwo_file->dbfd = std::move (dbfd);
13019
13020 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
13021 &dwo_file->sections);
13022
13023 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13024 dwo_file->cus);
13025
13026 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
13027 dwo_file->sections.types, dwo_file->tus);
13028
13029 if (dwarf_read_debug)
13030 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13031
13032 return dwo_file.release ();
13033 }
13034
13035 /* This function is mapped across the sections and remembers the offset and
13036 size of each of the DWP debugging sections common to version 1 and 2 that
13037 we are interested in. */
13038
13039 static void
13040 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13041 void *dwp_file_ptr)
13042 {
13043 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13044 const struct dwop_section_names *names = &dwop_section_names;
13045 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13046
13047 /* Record the ELF section number for later lookup: this is what the
13048 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13049 gdb_assert (elf_section_nr < dwp_file->num_sections);
13050 dwp_file->elf_sections[elf_section_nr] = sectp;
13051
13052 /* Look for specific sections that we need. */
13053 if (section_is_p (sectp->name, &names->str_dwo))
13054 {
13055 dwp_file->sections.str.s.section = sectp;
13056 dwp_file->sections.str.size = bfd_section_size (sectp);
13057 }
13058 else if (section_is_p (sectp->name, &names->cu_index))
13059 {
13060 dwp_file->sections.cu_index.s.section = sectp;
13061 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
13062 }
13063 else if (section_is_p (sectp->name, &names->tu_index))
13064 {
13065 dwp_file->sections.tu_index.s.section = sectp;
13066 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
13067 }
13068 }
13069
13070 /* This function is mapped across the sections and remembers the offset and
13071 size of each of the DWP version 2 debugging sections that we are interested
13072 in. This is split into a separate function because we don't know if we
13073 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13074
13075 static void
13076 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13077 {
13078 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13079 const struct dwop_section_names *names = &dwop_section_names;
13080 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13081
13082 /* Record the ELF section number for later lookup: this is what the
13083 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13084 gdb_assert (elf_section_nr < dwp_file->num_sections);
13085 dwp_file->elf_sections[elf_section_nr] = sectp;
13086
13087 /* Look for specific sections that we need. */
13088 if (section_is_p (sectp->name, &names->abbrev_dwo))
13089 {
13090 dwp_file->sections.abbrev.s.section = sectp;
13091 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13092 }
13093 else if (section_is_p (sectp->name, &names->info_dwo))
13094 {
13095 dwp_file->sections.info.s.section = sectp;
13096 dwp_file->sections.info.size = bfd_section_size (sectp);
13097 }
13098 else if (section_is_p (sectp->name, &names->line_dwo))
13099 {
13100 dwp_file->sections.line.s.section = sectp;
13101 dwp_file->sections.line.size = bfd_section_size (sectp);
13102 }
13103 else if (section_is_p (sectp->name, &names->loc_dwo))
13104 {
13105 dwp_file->sections.loc.s.section = sectp;
13106 dwp_file->sections.loc.size = bfd_section_size (sectp);
13107 }
13108 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13109 {
13110 dwp_file->sections.macinfo.s.section = sectp;
13111 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
13112 }
13113 else if (section_is_p (sectp->name, &names->macro_dwo))
13114 {
13115 dwp_file->sections.macro.s.section = sectp;
13116 dwp_file->sections.macro.size = bfd_section_size (sectp);
13117 }
13118 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13119 {
13120 dwp_file->sections.str_offsets.s.section = sectp;
13121 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13122 }
13123 else if (section_is_p (sectp->name, &names->types_dwo))
13124 {
13125 dwp_file->sections.types.s.section = sectp;
13126 dwp_file->sections.types.size = bfd_section_size (sectp);
13127 }
13128 }
13129
13130 /* Hash function for dwp_file loaded CUs/TUs. */
13131
13132 static hashval_t
13133 hash_dwp_loaded_cutus (const void *item)
13134 {
13135 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13136
13137 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13138 return dwo_unit->signature;
13139 }
13140
13141 /* Equality function for dwp_file loaded CUs/TUs. */
13142
13143 static int
13144 eq_dwp_loaded_cutus (const void *a, const void *b)
13145 {
13146 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13147 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13148
13149 return dua->signature == dub->signature;
13150 }
13151
13152 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13153
13154 static htab_t
13155 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13156 {
13157 return htab_create_alloc_ex (3,
13158 hash_dwp_loaded_cutus,
13159 eq_dwp_loaded_cutus,
13160 NULL,
13161 &objfile->objfile_obstack,
13162 hashtab_obstack_allocate,
13163 dummy_obstack_deallocate);
13164 }
13165
13166 /* Try to open DWP file FILE_NAME.
13167 The result is the bfd handle of the file.
13168 If there is a problem finding or opening the file, return NULL.
13169 Upon success, the canonicalized path of the file is stored in the bfd,
13170 same as symfile_bfd_open. */
13171
13172 static gdb_bfd_ref_ptr
13173 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13174 const char *file_name)
13175 {
13176 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13177 1 /*is_dwp*/,
13178 1 /*search_cwd*/));
13179 if (abfd != NULL)
13180 return abfd;
13181
13182 /* Work around upstream bug 15652.
13183 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13184 [Whether that's a "bug" is debatable, but it is getting in our way.]
13185 We have no real idea where the dwp file is, because gdb's realpath-ing
13186 of the executable's path may have discarded the needed info.
13187 [IWBN if the dwp file name was recorded in the executable, akin to
13188 .gnu_debuglink, but that doesn't exist yet.]
13189 Strip the directory from FILE_NAME and search again. */
13190 if (*debug_file_directory != '\0')
13191 {
13192 /* Don't implicitly search the current directory here.
13193 If the user wants to search "." to handle this case,
13194 it must be added to debug-file-directory. */
13195 return try_open_dwop_file (dwarf2_per_objfile,
13196 lbasename (file_name), 1 /*is_dwp*/,
13197 0 /*search_cwd*/);
13198 }
13199
13200 return NULL;
13201 }
13202
13203 /* Initialize the use of the DWP file for the current objfile.
13204 By convention the name of the DWP file is ${objfile}.dwp.
13205 The result is NULL if it can't be found. */
13206
13207 static std::unique_ptr<struct dwp_file>
13208 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13209 {
13210 struct objfile *objfile = dwarf2_per_objfile->objfile;
13211
13212 /* Try to find first .dwp for the binary file before any symbolic links
13213 resolving. */
13214
13215 /* If the objfile is a debug file, find the name of the real binary
13216 file and get the name of dwp file from there. */
13217 std::string dwp_name;
13218 if (objfile->separate_debug_objfile_backlink != NULL)
13219 {
13220 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13221 const char *backlink_basename = lbasename (backlink->original_name);
13222
13223 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13224 }
13225 else
13226 dwp_name = objfile->original_name;
13227
13228 dwp_name += ".dwp";
13229
13230 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13231 if (dbfd == NULL
13232 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13233 {
13234 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13235 dwp_name = objfile_name (objfile);
13236 dwp_name += ".dwp";
13237 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13238 }
13239
13240 if (dbfd == NULL)
13241 {
13242 if (dwarf_read_debug)
13243 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13244 return std::unique_ptr<dwp_file> ();
13245 }
13246
13247 const char *name = bfd_get_filename (dbfd.get ());
13248 std::unique_ptr<struct dwp_file> dwp_file
13249 (new struct dwp_file (name, std::move (dbfd)));
13250
13251 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13252 dwp_file->elf_sections =
13253 OBSTACK_CALLOC (&objfile->objfile_obstack,
13254 dwp_file->num_sections, asection *);
13255
13256 bfd_map_over_sections (dwp_file->dbfd.get (),
13257 dwarf2_locate_common_dwp_sections,
13258 dwp_file.get ());
13259
13260 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13261 0);
13262
13263 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13264 1);
13265
13266 /* The DWP file version is stored in the hash table. Oh well. */
13267 if (dwp_file->cus && dwp_file->tus
13268 && dwp_file->cus->version != dwp_file->tus->version)
13269 {
13270 /* Technically speaking, we should try to limp along, but this is
13271 pretty bizarre. We use pulongest here because that's the established
13272 portability solution (e.g, we cannot use %u for uint32_t). */
13273 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13274 " TU version %s [in DWP file %s]"),
13275 pulongest (dwp_file->cus->version),
13276 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13277 }
13278
13279 if (dwp_file->cus)
13280 dwp_file->version = dwp_file->cus->version;
13281 else if (dwp_file->tus)
13282 dwp_file->version = dwp_file->tus->version;
13283 else
13284 dwp_file->version = 2;
13285
13286 if (dwp_file->version == 2)
13287 bfd_map_over_sections (dwp_file->dbfd.get (),
13288 dwarf2_locate_v2_dwp_sections,
13289 dwp_file.get ());
13290
13291 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13292 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13293
13294 if (dwarf_read_debug)
13295 {
13296 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13297 fprintf_unfiltered (gdb_stdlog,
13298 " %s CUs, %s TUs\n",
13299 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13300 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13301 }
13302
13303 return dwp_file;
13304 }
13305
13306 /* Wrapper around open_and_init_dwp_file, only open it once. */
13307
13308 static struct dwp_file *
13309 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13310 {
13311 if (! dwarf2_per_objfile->dwp_checked)
13312 {
13313 dwarf2_per_objfile->dwp_file
13314 = open_and_init_dwp_file (dwarf2_per_objfile);
13315 dwarf2_per_objfile->dwp_checked = 1;
13316 }
13317 return dwarf2_per_objfile->dwp_file.get ();
13318 }
13319
13320 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13321 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13322 or in the DWP file for the objfile, referenced by THIS_UNIT.
13323 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13324 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13325
13326 This is called, for example, when wanting to read a variable with a
13327 complex location. Therefore we don't want to do file i/o for every call.
13328 Therefore we don't want to look for a DWO file on every call.
13329 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13330 then we check if we've already seen DWO_NAME, and only THEN do we check
13331 for a DWO file.
13332
13333 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13334 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13335
13336 static struct dwo_unit *
13337 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13338 const char *dwo_name, const char *comp_dir,
13339 ULONGEST signature, int is_debug_types)
13340 {
13341 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13342 struct objfile *objfile = dwarf2_per_objfile->objfile;
13343 const char *kind = is_debug_types ? "TU" : "CU";
13344 void **dwo_file_slot;
13345 struct dwo_file *dwo_file;
13346 struct dwp_file *dwp_file;
13347
13348 /* First see if there's a DWP file.
13349 If we have a DWP file but didn't find the DWO inside it, don't
13350 look for the original DWO file. It makes gdb behave differently
13351 depending on whether one is debugging in the build tree. */
13352
13353 dwp_file = get_dwp_file (dwarf2_per_objfile);
13354 if (dwp_file != NULL)
13355 {
13356 const struct dwp_hash_table *dwp_htab =
13357 is_debug_types ? dwp_file->tus : dwp_file->cus;
13358
13359 if (dwp_htab != NULL)
13360 {
13361 struct dwo_unit *dwo_cutu =
13362 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13363 signature, is_debug_types);
13364
13365 if (dwo_cutu != NULL)
13366 {
13367 if (dwarf_read_debug)
13368 {
13369 fprintf_unfiltered (gdb_stdlog,
13370 "Virtual DWO %s %s found: @%s\n",
13371 kind, hex_string (signature),
13372 host_address_to_string (dwo_cutu));
13373 }
13374 return dwo_cutu;
13375 }
13376 }
13377 }
13378 else
13379 {
13380 /* No DWP file, look for the DWO file. */
13381
13382 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13383 dwo_name, comp_dir);
13384 if (*dwo_file_slot == NULL)
13385 {
13386 /* Read in the file and build a table of the CUs/TUs it contains. */
13387 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13388 }
13389 /* NOTE: This will be NULL if unable to open the file. */
13390 dwo_file = (struct dwo_file *) *dwo_file_slot;
13391
13392 if (dwo_file != NULL)
13393 {
13394 struct dwo_unit *dwo_cutu = NULL;
13395
13396 if (is_debug_types && dwo_file->tus)
13397 {
13398 struct dwo_unit find_dwo_cutu;
13399
13400 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13401 find_dwo_cutu.signature = signature;
13402 dwo_cutu
13403 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13404 }
13405 else if (!is_debug_types && dwo_file->cus)
13406 {
13407 struct dwo_unit find_dwo_cutu;
13408
13409 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13410 find_dwo_cutu.signature = signature;
13411 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13412 &find_dwo_cutu);
13413 }
13414
13415 if (dwo_cutu != NULL)
13416 {
13417 if (dwarf_read_debug)
13418 {
13419 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13420 kind, dwo_name, hex_string (signature),
13421 host_address_to_string (dwo_cutu));
13422 }
13423 return dwo_cutu;
13424 }
13425 }
13426 }
13427
13428 /* We didn't find it. This could mean a dwo_id mismatch, or
13429 someone deleted the DWO/DWP file, or the search path isn't set up
13430 correctly to find the file. */
13431
13432 if (dwarf_read_debug)
13433 {
13434 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13435 kind, dwo_name, hex_string (signature));
13436 }
13437
13438 /* This is a warning and not a complaint because it can be caused by
13439 pilot error (e.g., user accidentally deleting the DWO). */
13440 {
13441 /* Print the name of the DWP file if we looked there, helps the user
13442 better diagnose the problem. */
13443 std::string dwp_text;
13444
13445 if (dwp_file != NULL)
13446 dwp_text = string_printf (" [in DWP file %s]",
13447 lbasename (dwp_file->name));
13448
13449 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13450 " [in module %s]"),
13451 kind, dwo_name, hex_string (signature),
13452 dwp_text.c_str (),
13453 this_unit->is_debug_types ? "TU" : "CU",
13454 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13455 }
13456 return NULL;
13457 }
13458
13459 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13460 See lookup_dwo_cutu_unit for details. */
13461
13462 static struct dwo_unit *
13463 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13464 const char *dwo_name, const char *comp_dir,
13465 ULONGEST signature)
13466 {
13467 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13468 }
13469
13470 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13471 See lookup_dwo_cutu_unit for details. */
13472
13473 static struct dwo_unit *
13474 lookup_dwo_type_unit (struct signatured_type *this_tu,
13475 const char *dwo_name, const char *comp_dir)
13476 {
13477 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13478 }
13479
13480 /* Traversal function for queue_and_load_all_dwo_tus. */
13481
13482 static int
13483 queue_and_load_dwo_tu (void **slot, void *info)
13484 {
13485 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13486 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13487 ULONGEST signature = dwo_unit->signature;
13488 struct signatured_type *sig_type =
13489 lookup_dwo_signatured_type (per_cu->cu, signature);
13490
13491 if (sig_type != NULL)
13492 {
13493 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13494
13495 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13496 a real dependency of PER_CU on SIG_TYPE. That is detected later
13497 while processing PER_CU. */
13498 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13499 load_full_type_unit (sig_cu);
13500 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13501 }
13502
13503 return 1;
13504 }
13505
13506 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13507 The DWO may have the only definition of the type, though it may not be
13508 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13509 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13510
13511 static void
13512 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13513 {
13514 struct dwo_unit *dwo_unit;
13515 struct dwo_file *dwo_file;
13516
13517 gdb_assert (!per_cu->is_debug_types);
13518 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13519 gdb_assert (per_cu->cu != NULL);
13520
13521 dwo_unit = per_cu->cu->dwo_unit;
13522 gdb_assert (dwo_unit != NULL);
13523
13524 dwo_file = dwo_unit->dwo_file;
13525 if (dwo_file->tus != NULL)
13526 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13527 }
13528
13529 /* Read in various DIEs. */
13530
13531 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13532 Inherit only the children of the DW_AT_abstract_origin DIE not being
13533 already referenced by DW_AT_abstract_origin from the children of the
13534 current DIE. */
13535
13536 static void
13537 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13538 {
13539 struct die_info *child_die;
13540 sect_offset *offsetp;
13541 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13542 struct die_info *origin_die;
13543 /* Iterator of the ORIGIN_DIE children. */
13544 struct die_info *origin_child_die;
13545 struct attribute *attr;
13546 struct dwarf2_cu *origin_cu;
13547 struct pending **origin_previous_list_in_scope;
13548
13549 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13550 if (!attr)
13551 return;
13552
13553 /* Note that following die references may follow to a die in a
13554 different cu. */
13555
13556 origin_cu = cu;
13557 origin_die = follow_die_ref (die, attr, &origin_cu);
13558
13559 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13560 symbols in. */
13561 origin_previous_list_in_scope = origin_cu->list_in_scope;
13562 origin_cu->list_in_scope = cu->list_in_scope;
13563
13564 if (die->tag != origin_die->tag
13565 && !(die->tag == DW_TAG_inlined_subroutine
13566 && origin_die->tag == DW_TAG_subprogram))
13567 complaint (_("DIE %s and its abstract origin %s have different tags"),
13568 sect_offset_str (die->sect_off),
13569 sect_offset_str (origin_die->sect_off));
13570
13571 std::vector<sect_offset> offsets;
13572
13573 for (child_die = die->child;
13574 child_die && child_die->tag;
13575 child_die = sibling_die (child_die))
13576 {
13577 struct die_info *child_origin_die;
13578 struct dwarf2_cu *child_origin_cu;
13579
13580 /* We are trying to process concrete instance entries:
13581 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13582 it's not relevant to our analysis here. i.e. detecting DIEs that are
13583 present in the abstract instance but not referenced in the concrete
13584 one. */
13585 if (child_die->tag == DW_TAG_call_site
13586 || child_die->tag == DW_TAG_GNU_call_site)
13587 continue;
13588
13589 /* For each CHILD_DIE, find the corresponding child of
13590 ORIGIN_DIE. If there is more than one layer of
13591 DW_AT_abstract_origin, follow them all; there shouldn't be,
13592 but GCC versions at least through 4.4 generate this (GCC PR
13593 40573). */
13594 child_origin_die = child_die;
13595 child_origin_cu = cu;
13596 while (1)
13597 {
13598 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13599 child_origin_cu);
13600 if (attr == NULL)
13601 break;
13602 child_origin_die = follow_die_ref (child_origin_die, attr,
13603 &child_origin_cu);
13604 }
13605
13606 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13607 counterpart may exist. */
13608 if (child_origin_die != child_die)
13609 {
13610 if (child_die->tag != child_origin_die->tag
13611 && !(child_die->tag == DW_TAG_inlined_subroutine
13612 && child_origin_die->tag == DW_TAG_subprogram))
13613 complaint (_("Child DIE %s and its abstract origin %s have "
13614 "different tags"),
13615 sect_offset_str (child_die->sect_off),
13616 sect_offset_str (child_origin_die->sect_off));
13617 if (child_origin_die->parent != origin_die)
13618 complaint (_("Child DIE %s and its abstract origin %s have "
13619 "different parents"),
13620 sect_offset_str (child_die->sect_off),
13621 sect_offset_str (child_origin_die->sect_off));
13622 else
13623 offsets.push_back (child_origin_die->sect_off);
13624 }
13625 }
13626 std::sort (offsets.begin (), offsets.end ());
13627 sect_offset *offsets_end = offsets.data () + offsets.size ();
13628 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13629 if (offsetp[-1] == *offsetp)
13630 complaint (_("Multiple children of DIE %s refer "
13631 "to DIE %s as their abstract origin"),
13632 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13633
13634 offsetp = offsets.data ();
13635 origin_child_die = origin_die->child;
13636 while (origin_child_die && origin_child_die->tag)
13637 {
13638 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13639 while (offsetp < offsets_end
13640 && *offsetp < origin_child_die->sect_off)
13641 offsetp++;
13642 if (offsetp >= offsets_end
13643 || *offsetp > origin_child_die->sect_off)
13644 {
13645 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13646 Check whether we're already processing ORIGIN_CHILD_DIE.
13647 This can happen with mutually referenced abstract_origins.
13648 PR 16581. */
13649 if (!origin_child_die->in_process)
13650 process_die (origin_child_die, origin_cu);
13651 }
13652 origin_child_die = sibling_die (origin_child_die);
13653 }
13654 origin_cu->list_in_scope = origin_previous_list_in_scope;
13655 }
13656
13657 static void
13658 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13659 {
13660 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13661 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13662 struct context_stack *newobj;
13663 CORE_ADDR lowpc;
13664 CORE_ADDR highpc;
13665 struct die_info *child_die;
13666 struct attribute *attr, *call_line, *call_file;
13667 const char *name;
13668 CORE_ADDR baseaddr;
13669 struct block *block;
13670 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13671 std::vector<struct symbol *> template_args;
13672 struct template_symbol *templ_func = NULL;
13673
13674 if (inlined_func)
13675 {
13676 /* If we do not have call site information, we can't show the
13677 caller of this inlined function. That's too confusing, so
13678 only use the scope for local variables. */
13679 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13680 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13681 if (call_line == NULL || call_file == NULL)
13682 {
13683 read_lexical_block_scope (die, cu);
13684 return;
13685 }
13686 }
13687
13688 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13689
13690 name = dwarf2_name (die, cu);
13691
13692 /* Ignore functions with missing or empty names. These are actually
13693 illegal according to the DWARF standard. */
13694 if (name == NULL)
13695 {
13696 complaint (_("missing name for subprogram DIE at %s"),
13697 sect_offset_str (die->sect_off));
13698 return;
13699 }
13700
13701 /* Ignore functions with missing or invalid low and high pc attributes. */
13702 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13703 <= PC_BOUNDS_INVALID)
13704 {
13705 attr = dwarf2_attr (die, DW_AT_external, cu);
13706 if (!attr || !DW_UNSND (attr))
13707 complaint (_("cannot get low and high bounds "
13708 "for subprogram DIE at %s"),
13709 sect_offset_str (die->sect_off));
13710 return;
13711 }
13712
13713 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13714 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13715
13716 /* If we have any template arguments, then we must allocate a
13717 different sort of symbol. */
13718 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13719 {
13720 if (child_die->tag == DW_TAG_template_type_param
13721 || child_die->tag == DW_TAG_template_value_param)
13722 {
13723 templ_func = allocate_template_symbol (objfile);
13724 templ_func->subclass = SYMBOL_TEMPLATE;
13725 break;
13726 }
13727 }
13728
13729 newobj = cu->get_builder ()->push_context (0, lowpc);
13730 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13731 (struct symbol *) templ_func);
13732
13733 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13734 set_objfile_main_name (objfile, SYMBOL_LINKAGE_NAME (newobj->name),
13735 cu->language);
13736
13737 /* If there is a location expression for DW_AT_frame_base, record
13738 it. */
13739 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13740 if (attr)
13741 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13742
13743 /* If there is a location for the static link, record it. */
13744 newobj->static_link = NULL;
13745 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13746 if (attr)
13747 {
13748 newobj->static_link
13749 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13750 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13751 dwarf2_per_cu_addr_type (cu->per_cu));
13752 }
13753
13754 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13755
13756 if (die->child != NULL)
13757 {
13758 child_die = die->child;
13759 while (child_die && child_die->tag)
13760 {
13761 if (child_die->tag == DW_TAG_template_type_param
13762 || child_die->tag == DW_TAG_template_value_param)
13763 {
13764 struct symbol *arg = new_symbol (child_die, NULL, cu);
13765
13766 if (arg != NULL)
13767 template_args.push_back (arg);
13768 }
13769 else
13770 process_die (child_die, cu);
13771 child_die = sibling_die (child_die);
13772 }
13773 }
13774
13775 inherit_abstract_dies (die, cu);
13776
13777 /* If we have a DW_AT_specification, we might need to import using
13778 directives from the context of the specification DIE. See the
13779 comment in determine_prefix. */
13780 if (cu->language == language_cplus
13781 && dwarf2_attr (die, DW_AT_specification, cu))
13782 {
13783 struct dwarf2_cu *spec_cu = cu;
13784 struct die_info *spec_die = die_specification (die, &spec_cu);
13785
13786 while (spec_die)
13787 {
13788 child_die = spec_die->child;
13789 while (child_die && child_die->tag)
13790 {
13791 if (child_die->tag == DW_TAG_imported_module)
13792 process_die (child_die, spec_cu);
13793 child_die = sibling_die (child_die);
13794 }
13795
13796 /* In some cases, GCC generates specification DIEs that
13797 themselves contain DW_AT_specification attributes. */
13798 spec_die = die_specification (spec_die, &spec_cu);
13799 }
13800 }
13801
13802 struct context_stack cstk = cu->get_builder ()->pop_context ();
13803 /* Make a block for the local symbols within. */
13804 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13805 cstk.static_link, lowpc, highpc);
13806
13807 /* For C++, set the block's scope. */
13808 if ((cu->language == language_cplus
13809 || cu->language == language_fortran
13810 || cu->language == language_d
13811 || cu->language == language_rust)
13812 && cu->processing_has_namespace_info)
13813 block_set_scope (block, determine_prefix (die, cu),
13814 &objfile->objfile_obstack);
13815
13816 /* If we have address ranges, record them. */
13817 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13818
13819 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13820
13821 /* Attach template arguments to function. */
13822 if (!template_args.empty ())
13823 {
13824 gdb_assert (templ_func != NULL);
13825
13826 templ_func->n_template_arguments = template_args.size ();
13827 templ_func->template_arguments
13828 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13829 templ_func->n_template_arguments);
13830 memcpy (templ_func->template_arguments,
13831 template_args.data (),
13832 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13833
13834 /* Make sure that the symtab is set on the new symbols. Even
13835 though they don't appear in this symtab directly, other parts
13836 of gdb assume that symbols do, and this is reasonably
13837 true. */
13838 for (symbol *sym : template_args)
13839 symbol_set_symtab (sym, symbol_symtab (templ_func));
13840 }
13841
13842 /* In C++, we can have functions nested inside functions (e.g., when
13843 a function declares a class that has methods). This means that
13844 when we finish processing a function scope, we may need to go
13845 back to building a containing block's symbol lists. */
13846 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13847 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13848
13849 /* If we've finished processing a top-level function, subsequent
13850 symbols go in the file symbol list. */
13851 if (cu->get_builder ()->outermost_context_p ())
13852 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13853 }
13854
13855 /* Process all the DIES contained within a lexical block scope. Start
13856 a new scope, process the dies, and then close the scope. */
13857
13858 static void
13859 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13860 {
13861 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13862 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13863 CORE_ADDR lowpc, highpc;
13864 struct die_info *child_die;
13865 CORE_ADDR baseaddr;
13866
13867 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13868
13869 /* Ignore blocks with missing or invalid low and high pc attributes. */
13870 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13871 as multiple lexical blocks? Handling children in a sane way would
13872 be nasty. Might be easier to properly extend generic blocks to
13873 describe ranges. */
13874 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13875 {
13876 case PC_BOUNDS_NOT_PRESENT:
13877 /* DW_TAG_lexical_block has no attributes, process its children as if
13878 there was no wrapping by that DW_TAG_lexical_block.
13879 GCC does no longer produces such DWARF since GCC r224161. */
13880 for (child_die = die->child;
13881 child_die != NULL && child_die->tag;
13882 child_die = sibling_die (child_die))
13883 process_die (child_die, cu);
13884 return;
13885 case PC_BOUNDS_INVALID:
13886 return;
13887 }
13888 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13889 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13890
13891 cu->get_builder ()->push_context (0, lowpc);
13892 if (die->child != NULL)
13893 {
13894 child_die = die->child;
13895 while (child_die && child_die->tag)
13896 {
13897 process_die (child_die, cu);
13898 child_die = sibling_die (child_die);
13899 }
13900 }
13901 inherit_abstract_dies (die, cu);
13902 struct context_stack cstk = cu->get_builder ()->pop_context ();
13903
13904 if (*cu->get_builder ()->get_local_symbols () != NULL
13905 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13906 {
13907 struct block *block
13908 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13909 cstk.start_addr, highpc);
13910
13911 /* Note that recording ranges after traversing children, as we
13912 do here, means that recording a parent's ranges entails
13913 walking across all its children's ranges as they appear in
13914 the address map, which is quadratic behavior.
13915
13916 It would be nicer to record the parent's ranges before
13917 traversing its children, simply overriding whatever you find
13918 there. But since we don't even decide whether to create a
13919 block until after we've traversed its children, that's hard
13920 to do. */
13921 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13922 }
13923 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13924 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13925 }
13926
13927 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13928
13929 static void
13930 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13931 {
13932 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13933 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13934 CORE_ADDR pc, baseaddr;
13935 struct attribute *attr;
13936 struct call_site *call_site, call_site_local;
13937 void **slot;
13938 int nparams;
13939 struct die_info *child_die;
13940
13941 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13942
13943 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13944 if (attr == NULL)
13945 {
13946 /* This was a pre-DWARF-5 GNU extension alias
13947 for DW_AT_call_return_pc. */
13948 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13949 }
13950 if (!attr)
13951 {
13952 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13953 "DIE %s [in module %s]"),
13954 sect_offset_str (die->sect_off), objfile_name (objfile));
13955 return;
13956 }
13957 pc = attr_value_as_address (attr) + baseaddr;
13958 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13959
13960 if (cu->call_site_htab == NULL)
13961 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13962 NULL, &objfile->objfile_obstack,
13963 hashtab_obstack_allocate, NULL);
13964 call_site_local.pc = pc;
13965 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13966 if (*slot != NULL)
13967 {
13968 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13969 "DIE %s [in module %s]"),
13970 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13971 objfile_name (objfile));
13972 return;
13973 }
13974
13975 /* Count parameters at the caller. */
13976
13977 nparams = 0;
13978 for (child_die = die->child; child_die && child_die->tag;
13979 child_die = sibling_die (child_die))
13980 {
13981 if (child_die->tag != DW_TAG_call_site_parameter
13982 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13983 {
13984 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13985 "DW_TAG_call_site child DIE %s [in module %s]"),
13986 child_die->tag, sect_offset_str (child_die->sect_off),
13987 objfile_name (objfile));
13988 continue;
13989 }
13990
13991 nparams++;
13992 }
13993
13994 call_site
13995 = ((struct call_site *)
13996 obstack_alloc (&objfile->objfile_obstack,
13997 sizeof (*call_site)
13998 + (sizeof (*call_site->parameter) * (nparams - 1))));
13999 *slot = call_site;
14000 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14001 call_site->pc = pc;
14002
14003 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14004 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14005 {
14006 struct die_info *func_die;
14007
14008 /* Skip also over DW_TAG_inlined_subroutine. */
14009 for (func_die = die->parent;
14010 func_die && func_die->tag != DW_TAG_subprogram
14011 && func_die->tag != DW_TAG_subroutine_type;
14012 func_die = func_die->parent);
14013
14014 /* DW_AT_call_all_calls is a superset
14015 of DW_AT_call_all_tail_calls. */
14016 if (func_die
14017 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14018 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14019 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14020 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14021 {
14022 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14023 not complete. But keep CALL_SITE for look ups via call_site_htab,
14024 both the initial caller containing the real return address PC and
14025 the final callee containing the current PC of a chain of tail
14026 calls do not need to have the tail call list complete. But any
14027 function candidate for a virtual tail call frame searched via
14028 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14029 determined unambiguously. */
14030 }
14031 else
14032 {
14033 struct type *func_type = NULL;
14034
14035 if (func_die)
14036 func_type = get_die_type (func_die, cu);
14037 if (func_type != NULL)
14038 {
14039 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14040
14041 /* Enlist this call site to the function. */
14042 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14043 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14044 }
14045 else
14046 complaint (_("Cannot find function owning DW_TAG_call_site "
14047 "DIE %s [in module %s]"),
14048 sect_offset_str (die->sect_off), objfile_name (objfile));
14049 }
14050 }
14051
14052 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14053 if (attr == NULL)
14054 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14055 if (attr == NULL)
14056 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14057 if (attr == NULL)
14058 {
14059 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14060 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14061 }
14062 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14063 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14064 /* Keep NULL DWARF_BLOCK. */;
14065 else if (attr_form_is_block (attr))
14066 {
14067 struct dwarf2_locexpr_baton *dlbaton;
14068
14069 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14070 dlbaton->data = DW_BLOCK (attr)->data;
14071 dlbaton->size = DW_BLOCK (attr)->size;
14072 dlbaton->per_cu = cu->per_cu;
14073
14074 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14075 }
14076 else if (attr_form_is_ref (attr))
14077 {
14078 struct dwarf2_cu *target_cu = cu;
14079 struct die_info *target_die;
14080
14081 target_die = follow_die_ref (die, attr, &target_cu);
14082 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14083 if (die_is_declaration (target_die, target_cu))
14084 {
14085 const char *target_physname;
14086
14087 /* Prefer the mangled name; otherwise compute the demangled one. */
14088 target_physname = dw2_linkage_name (target_die, target_cu);
14089 if (target_physname == NULL)
14090 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14091 if (target_physname == NULL)
14092 complaint (_("DW_AT_call_target target DIE has invalid "
14093 "physname, for referencing DIE %s [in module %s]"),
14094 sect_offset_str (die->sect_off), objfile_name (objfile));
14095 else
14096 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14097 }
14098 else
14099 {
14100 CORE_ADDR lowpc;
14101
14102 /* DW_AT_entry_pc should be preferred. */
14103 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14104 <= PC_BOUNDS_INVALID)
14105 complaint (_("DW_AT_call_target target DIE has invalid "
14106 "low pc, for referencing DIE %s [in module %s]"),
14107 sect_offset_str (die->sect_off), objfile_name (objfile));
14108 else
14109 {
14110 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14111 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14112 }
14113 }
14114 }
14115 else
14116 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14117 "block nor reference, for DIE %s [in module %s]"),
14118 sect_offset_str (die->sect_off), objfile_name (objfile));
14119
14120 call_site->per_cu = cu->per_cu;
14121
14122 for (child_die = die->child;
14123 child_die && child_die->tag;
14124 child_die = sibling_die (child_die))
14125 {
14126 struct call_site_parameter *parameter;
14127 struct attribute *loc, *origin;
14128
14129 if (child_die->tag != DW_TAG_call_site_parameter
14130 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14131 {
14132 /* Already printed the complaint above. */
14133 continue;
14134 }
14135
14136 gdb_assert (call_site->parameter_count < nparams);
14137 parameter = &call_site->parameter[call_site->parameter_count];
14138
14139 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14140 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14141 register is contained in DW_AT_call_value. */
14142
14143 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14144 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14145 if (origin == NULL)
14146 {
14147 /* This was a pre-DWARF-5 GNU extension alias
14148 for DW_AT_call_parameter. */
14149 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14150 }
14151 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14152 {
14153 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14154
14155 sect_offset sect_off
14156 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14157 if (!offset_in_cu_p (&cu->header, sect_off))
14158 {
14159 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14160 binding can be done only inside one CU. Such referenced DIE
14161 therefore cannot be even moved to DW_TAG_partial_unit. */
14162 complaint (_("DW_AT_call_parameter offset is not in CU for "
14163 "DW_TAG_call_site child DIE %s [in module %s]"),
14164 sect_offset_str (child_die->sect_off),
14165 objfile_name (objfile));
14166 continue;
14167 }
14168 parameter->u.param_cu_off
14169 = (cu_offset) (sect_off - cu->header.sect_off);
14170 }
14171 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14172 {
14173 complaint (_("No DW_FORM_block* DW_AT_location for "
14174 "DW_TAG_call_site child DIE %s [in module %s]"),
14175 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14176 continue;
14177 }
14178 else
14179 {
14180 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14181 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14182 if (parameter->u.dwarf_reg != -1)
14183 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14184 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14185 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14186 &parameter->u.fb_offset))
14187 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14188 else
14189 {
14190 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14191 "for DW_FORM_block* DW_AT_location is supported for "
14192 "DW_TAG_call_site child DIE %s "
14193 "[in module %s]"),
14194 sect_offset_str (child_die->sect_off),
14195 objfile_name (objfile));
14196 continue;
14197 }
14198 }
14199
14200 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14201 if (attr == NULL)
14202 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14203 if (!attr_form_is_block (attr))
14204 {
14205 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14206 "DW_TAG_call_site child DIE %s [in module %s]"),
14207 sect_offset_str (child_die->sect_off),
14208 objfile_name (objfile));
14209 continue;
14210 }
14211 parameter->value = DW_BLOCK (attr)->data;
14212 parameter->value_size = DW_BLOCK (attr)->size;
14213
14214 /* Parameters are not pre-cleared by memset above. */
14215 parameter->data_value = NULL;
14216 parameter->data_value_size = 0;
14217 call_site->parameter_count++;
14218
14219 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14220 if (attr == NULL)
14221 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14222 if (attr)
14223 {
14224 if (!attr_form_is_block (attr))
14225 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14226 "DW_TAG_call_site child DIE %s [in module %s]"),
14227 sect_offset_str (child_die->sect_off),
14228 objfile_name (objfile));
14229 else
14230 {
14231 parameter->data_value = DW_BLOCK (attr)->data;
14232 parameter->data_value_size = DW_BLOCK (attr)->size;
14233 }
14234 }
14235 }
14236 }
14237
14238 /* Helper function for read_variable. If DIE represents a virtual
14239 table, then return the type of the concrete object that is
14240 associated with the virtual table. Otherwise, return NULL. */
14241
14242 static struct type *
14243 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14244 {
14245 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14246 if (attr == NULL)
14247 return NULL;
14248
14249 /* Find the type DIE. */
14250 struct die_info *type_die = NULL;
14251 struct dwarf2_cu *type_cu = cu;
14252
14253 if (attr_form_is_ref (attr))
14254 type_die = follow_die_ref (die, attr, &type_cu);
14255 if (type_die == NULL)
14256 return NULL;
14257
14258 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14259 return NULL;
14260 return die_containing_type (type_die, type_cu);
14261 }
14262
14263 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14264
14265 static void
14266 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14267 {
14268 struct rust_vtable_symbol *storage = NULL;
14269
14270 if (cu->language == language_rust)
14271 {
14272 struct type *containing_type = rust_containing_type (die, cu);
14273
14274 if (containing_type != NULL)
14275 {
14276 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14277
14278 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14279 struct rust_vtable_symbol);
14280 initialize_objfile_symbol (storage);
14281 storage->concrete_type = containing_type;
14282 storage->subclass = SYMBOL_RUST_VTABLE;
14283 }
14284 }
14285
14286 struct symbol *res = new_symbol (die, NULL, cu, storage);
14287 struct attribute *abstract_origin
14288 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14289 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14290 if (res == NULL && loc && abstract_origin)
14291 {
14292 /* We have a variable without a name, but with a location and an abstract
14293 origin. This may be a concrete instance of an abstract variable
14294 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14295 later. */
14296 struct dwarf2_cu *origin_cu = cu;
14297 struct die_info *origin_die
14298 = follow_die_ref (die, abstract_origin, &origin_cu);
14299 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
14300 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
14301 }
14302 }
14303
14304 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14305 reading .debug_rnglists.
14306 Callback's type should be:
14307 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14308 Return true if the attributes are present and valid, otherwise,
14309 return false. */
14310
14311 template <typename Callback>
14312 static bool
14313 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14314 Callback &&callback)
14315 {
14316 struct dwarf2_per_objfile *dwarf2_per_objfile
14317 = cu->per_cu->dwarf2_per_objfile;
14318 struct objfile *objfile = dwarf2_per_objfile->objfile;
14319 bfd *obfd = objfile->obfd;
14320 /* Base address selection entry. */
14321 CORE_ADDR base;
14322 int found_base;
14323 const gdb_byte *buffer;
14324 CORE_ADDR baseaddr;
14325 bool overflow = false;
14326
14327 found_base = cu->base_known;
14328 base = cu->base_address;
14329
14330 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14331 if (offset >= dwarf2_per_objfile->rnglists.size)
14332 {
14333 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14334 offset);
14335 return false;
14336 }
14337 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14338
14339 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14340
14341 while (1)
14342 {
14343 /* Initialize it due to a false compiler warning. */
14344 CORE_ADDR range_beginning = 0, range_end = 0;
14345 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14346 + dwarf2_per_objfile->rnglists.size);
14347 unsigned int bytes_read;
14348
14349 if (buffer == buf_end)
14350 {
14351 overflow = true;
14352 break;
14353 }
14354 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14355 switch (rlet)
14356 {
14357 case DW_RLE_end_of_list:
14358 break;
14359 case DW_RLE_base_address:
14360 if (buffer + cu->header.addr_size > buf_end)
14361 {
14362 overflow = true;
14363 break;
14364 }
14365 base = read_address (obfd, buffer, cu, &bytes_read);
14366 found_base = 1;
14367 buffer += bytes_read;
14368 break;
14369 case DW_RLE_start_length:
14370 if (buffer + cu->header.addr_size > buf_end)
14371 {
14372 overflow = true;
14373 break;
14374 }
14375 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14376 buffer += bytes_read;
14377 range_end = (range_beginning
14378 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14379 buffer += bytes_read;
14380 if (buffer > buf_end)
14381 {
14382 overflow = true;
14383 break;
14384 }
14385 break;
14386 case DW_RLE_offset_pair:
14387 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14388 buffer += bytes_read;
14389 if (buffer > buf_end)
14390 {
14391 overflow = true;
14392 break;
14393 }
14394 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14395 buffer += bytes_read;
14396 if (buffer > buf_end)
14397 {
14398 overflow = true;
14399 break;
14400 }
14401 break;
14402 case DW_RLE_start_end:
14403 if (buffer + 2 * cu->header.addr_size > buf_end)
14404 {
14405 overflow = true;
14406 break;
14407 }
14408 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14409 buffer += bytes_read;
14410 range_end = read_address (obfd, buffer, cu, &bytes_read);
14411 buffer += bytes_read;
14412 break;
14413 default:
14414 complaint (_("Invalid .debug_rnglists data (no base address)"));
14415 return false;
14416 }
14417 if (rlet == DW_RLE_end_of_list || overflow)
14418 break;
14419 if (rlet == DW_RLE_base_address)
14420 continue;
14421
14422 if (!found_base)
14423 {
14424 /* We have no valid base address for the ranges
14425 data. */
14426 complaint (_("Invalid .debug_rnglists data (no base address)"));
14427 return false;
14428 }
14429
14430 if (range_beginning > range_end)
14431 {
14432 /* Inverted range entries are invalid. */
14433 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14434 return false;
14435 }
14436
14437 /* Empty range entries have no effect. */
14438 if (range_beginning == range_end)
14439 continue;
14440
14441 range_beginning += base;
14442 range_end += base;
14443
14444 /* A not-uncommon case of bad debug info.
14445 Don't pollute the addrmap with bad data. */
14446 if (range_beginning + baseaddr == 0
14447 && !dwarf2_per_objfile->has_section_at_zero)
14448 {
14449 complaint (_(".debug_rnglists entry has start address of zero"
14450 " [in module %s]"), objfile_name (objfile));
14451 continue;
14452 }
14453
14454 callback (range_beginning, range_end);
14455 }
14456
14457 if (overflow)
14458 {
14459 complaint (_("Offset %d is not terminated "
14460 "for DW_AT_ranges attribute"),
14461 offset);
14462 return false;
14463 }
14464
14465 return true;
14466 }
14467
14468 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14469 Callback's type should be:
14470 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14471 Return 1 if the attributes are present and valid, otherwise, return 0. */
14472
14473 template <typename Callback>
14474 static int
14475 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14476 Callback &&callback)
14477 {
14478 struct dwarf2_per_objfile *dwarf2_per_objfile
14479 = cu->per_cu->dwarf2_per_objfile;
14480 struct objfile *objfile = dwarf2_per_objfile->objfile;
14481 struct comp_unit_head *cu_header = &cu->header;
14482 bfd *obfd = objfile->obfd;
14483 unsigned int addr_size = cu_header->addr_size;
14484 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14485 /* Base address selection entry. */
14486 CORE_ADDR base;
14487 int found_base;
14488 unsigned int dummy;
14489 const gdb_byte *buffer;
14490 CORE_ADDR baseaddr;
14491
14492 if (cu_header->version >= 5)
14493 return dwarf2_rnglists_process (offset, cu, callback);
14494
14495 found_base = cu->base_known;
14496 base = cu->base_address;
14497
14498 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14499 if (offset >= dwarf2_per_objfile->ranges.size)
14500 {
14501 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14502 offset);
14503 return 0;
14504 }
14505 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14506
14507 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14508
14509 while (1)
14510 {
14511 CORE_ADDR range_beginning, range_end;
14512
14513 range_beginning = read_address (obfd, buffer, cu, &dummy);
14514 buffer += addr_size;
14515 range_end = read_address (obfd, buffer, cu, &dummy);
14516 buffer += addr_size;
14517 offset += 2 * addr_size;
14518
14519 /* An end of list marker is a pair of zero addresses. */
14520 if (range_beginning == 0 && range_end == 0)
14521 /* Found the end of list entry. */
14522 break;
14523
14524 /* Each base address selection entry is a pair of 2 values.
14525 The first is the largest possible address, the second is
14526 the base address. Check for a base address here. */
14527 if ((range_beginning & mask) == mask)
14528 {
14529 /* If we found the largest possible address, then we already
14530 have the base address in range_end. */
14531 base = range_end;
14532 found_base = 1;
14533 continue;
14534 }
14535
14536 if (!found_base)
14537 {
14538 /* We have no valid base address for the ranges
14539 data. */
14540 complaint (_("Invalid .debug_ranges data (no base address)"));
14541 return 0;
14542 }
14543
14544 if (range_beginning > range_end)
14545 {
14546 /* Inverted range entries are invalid. */
14547 complaint (_("Invalid .debug_ranges data (inverted range)"));
14548 return 0;
14549 }
14550
14551 /* Empty range entries have no effect. */
14552 if (range_beginning == range_end)
14553 continue;
14554
14555 range_beginning += base;
14556 range_end += base;
14557
14558 /* A not-uncommon case of bad debug info.
14559 Don't pollute the addrmap with bad data. */
14560 if (range_beginning + baseaddr == 0
14561 && !dwarf2_per_objfile->has_section_at_zero)
14562 {
14563 complaint (_(".debug_ranges entry has start address of zero"
14564 " [in module %s]"), objfile_name (objfile));
14565 continue;
14566 }
14567
14568 callback (range_beginning, range_end);
14569 }
14570
14571 return 1;
14572 }
14573
14574 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14575 Return 1 if the attributes are present and valid, otherwise, return 0.
14576 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14577
14578 static int
14579 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14580 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14581 struct partial_symtab *ranges_pst)
14582 {
14583 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14584 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14585 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14586 SECT_OFF_TEXT (objfile));
14587 int low_set = 0;
14588 CORE_ADDR low = 0;
14589 CORE_ADDR high = 0;
14590 int retval;
14591
14592 retval = dwarf2_ranges_process (offset, cu,
14593 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14594 {
14595 if (ranges_pst != NULL)
14596 {
14597 CORE_ADDR lowpc;
14598 CORE_ADDR highpc;
14599
14600 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14601 range_beginning + baseaddr)
14602 - baseaddr);
14603 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14604 range_end + baseaddr)
14605 - baseaddr);
14606 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14607 lowpc, highpc - 1, ranges_pst);
14608 }
14609
14610 /* FIXME: This is recording everything as a low-high
14611 segment of consecutive addresses. We should have a
14612 data structure for discontiguous block ranges
14613 instead. */
14614 if (! low_set)
14615 {
14616 low = range_beginning;
14617 high = range_end;
14618 low_set = 1;
14619 }
14620 else
14621 {
14622 if (range_beginning < low)
14623 low = range_beginning;
14624 if (range_end > high)
14625 high = range_end;
14626 }
14627 });
14628 if (!retval)
14629 return 0;
14630
14631 if (! low_set)
14632 /* If the first entry is an end-of-list marker, the range
14633 describes an empty scope, i.e. no instructions. */
14634 return 0;
14635
14636 if (low_return)
14637 *low_return = low;
14638 if (high_return)
14639 *high_return = high;
14640 return 1;
14641 }
14642
14643 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14644 definition for the return value. *LOWPC and *HIGHPC are set iff
14645 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14646
14647 static enum pc_bounds_kind
14648 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14649 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14650 struct partial_symtab *pst)
14651 {
14652 struct dwarf2_per_objfile *dwarf2_per_objfile
14653 = cu->per_cu->dwarf2_per_objfile;
14654 struct attribute *attr;
14655 struct attribute *attr_high;
14656 CORE_ADDR low = 0;
14657 CORE_ADDR high = 0;
14658 enum pc_bounds_kind ret;
14659
14660 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14661 if (attr_high)
14662 {
14663 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14664 if (attr)
14665 {
14666 low = attr_value_as_address (attr);
14667 high = attr_value_as_address (attr_high);
14668 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14669 high += low;
14670 }
14671 else
14672 /* Found high w/o low attribute. */
14673 return PC_BOUNDS_INVALID;
14674
14675 /* Found consecutive range of addresses. */
14676 ret = PC_BOUNDS_HIGH_LOW;
14677 }
14678 else
14679 {
14680 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14681 if (attr != NULL)
14682 {
14683 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14684 We take advantage of the fact that DW_AT_ranges does not appear
14685 in DW_TAG_compile_unit of DWO files. */
14686 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14687 unsigned int ranges_offset = (DW_UNSND (attr)
14688 + (need_ranges_base
14689 ? cu->ranges_base
14690 : 0));
14691
14692 /* Value of the DW_AT_ranges attribute is the offset in the
14693 .debug_ranges section. */
14694 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14695 return PC_BOUNDS_INVALID;
14696 /* Found discontinuous range of addresses. */
14697 ret = PC_BOUNDS_RANGES;
14698 }
14699 else
14700 return PC_BOUNDS_NOT_PRESENT;
14701 }
14702
14703 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14704 if (high <= low)
14705 return PC_BOUNDS_INVALID;
14706
14707 /* When using the GNU linker, .gnu.linkonce. sections are used to
14708 eliminate duplicate copies of functions and vtables and such.
14709 The linker will arbitrarily choose one and discard the others.
14710 The AT_*_pc values for such functions refer to local labels in
14711 these sections. If the section from that file was discarded, the
14712 labels are not in the output, so the relocs get a value of 0.
14713 If this is a discarded function, mark the pc bounds as invalid,
14714 so that GDB will ignore it. */
14715 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14716 return PC_BOUNDS_INVALID;
14717
14718 *lowpc = low;
14719 if (highpc)
14720 *highpc = high;
14721 return ret;
14722 }
14723
14724 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14725 its low and high PC addresses. Do nothing if these addresses could not
14726 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14727 and HIGHPC to the high address if greater than HIGHPC. */
14728
14729 static void
14730 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14731 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14732 struct dwarf2_cu *cu)
14733 {
14734 CORE_ADDR low, high;
14735 struct die_info *child = die->child;
14736
14737 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14738 {
14739 *lowpc = std::min (*lowpc, low);
14740 *highpc = std::max (*highpc, high);
14741 }
14742
14743 /* If the language does not allow nested subprograms (either inside
14744 subprograms or lexical blocks), we're done. */
14745 if (cu->language != language_ada)
14746 return;
14747
14748 /* Check all the children of the given DIE. If it contains nested
14749 subprograms, then check their pc bounds. Likewise, we need to
14750 check lexical blocks as well, as they may also contain subprogram
14751 definitions. */
14752 while (child && child->tag)
14753 {
14754 if (child->tag == DW_TAG_subprogram
14755 || child->tag == DW_TAG_lexical_block)
14756 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14757 child = sibling_die (child);
14758 }
14759 }
14760
14761 /* Get the low and high pc's represented by the scope DIE, and store
14762 them in *LOWPC and *HIGHPC. If the correct values can't be
14763 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14764
14765 static void
14766 get_scope_pc_bounds (struct die_info *die,
14767 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14768 struct dwarf2_cu *cu)
14769 {
14770 CORE_ADDR best_low = (CORE_ADDR) -1;
14771 CORE_ADDR best_high = (CORE_ADDR) 0;
14772 CORE_ADDR current_low, current_high;
14773
14774 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14775 >= PC_BOUNDS_RANGES)
14776 {
14777 best_low = current_low;
14778 best_high = current_high;
14779 }
14780 else
14781 {
14782 struct die_info *child = die->child;
14783
14784 while (child && child->tag)
14785 {
14786 switch (child->tag) {
14787 case DW_TAG_subprogram:
14788 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14789 break;
14790 case DW_TAG_namespace:
14791 case DW_TAG_module:
14792 /* FIXME: carlton/2004-01-16: Should we do this for
14793 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14794 that current GCC's always emit the DIEs corresponding
14795 to definitions of methods of classes as children of a
14796 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14797 the DIEs giving the declarations, which could be
14798 anywhere). But I don't see any reason why the
14799 standards says that they have to be there. */
14800 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14801
14802 if (current_low != ((CORE_ADDR) -1))
14803 {
14804 best_low = std::min (best_low, current_low);
14805 best_high = std::max (best_high, current_high);
14806 }
14807 break;
14808 default:
14809 /* Ignore. */
14810 break;
14811 }
14812
14813 child = sibling_die (child);
14814 }
14815 }
14816
14817 *lowpc = best_low;
14818 *highpc = best_high;
14819 }
14820
14821 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14822 in DIE. */
14823
14824 static void
14825 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14826 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14827 {
14828 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14829 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14830 struct attribute *attr;
14831 struct attribute *attr_high;
14832
14833 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14834 if (attr_high)
14835 {
14836 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14837 if (attr)
14838 {
14839 CORE_ADDR low = attr_value_as_address (attr);
14840 CORE_ADDR high = attr_value_as_address (attr_high);
14841
14842 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14843 high += low;
14844
14845 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14846 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14847 cu->get_builder ()->record_block_range (block, low, high - 1);
14848 }
14849 }
14850
14851 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14852 if (attr)
14853 {
14854 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14855 We take advantage of the fact that DW_AT_ranges does not appear
14856 in DW_TAG_compile_unit of DWO files. */
14857 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14858
14859 /* The value of the DW_AT_ranges attribute is the offset of the
14860 address range list in the .debug_ranges section. */
14861 unsigned long offset = (DW_UNSND (attr)
14862 + (need_ranges_base ? cu->ranges_base : 0));
14863
14864 std::vector<blockrange> blockvec;
14865 dwarf2_ranges_process (offset, cu,
14866 [&] (CORE_ADDR start, CORE_ADDR end)
14867 {
14868 start += baseaddr;
14869 end += baseaddr;
14870 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14871 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14872 cu->get_builder ()->record_block_range (block, start, end - 1);
14873 blockvec.emplace_back (start, end);
14874 });
14875
14876 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14877 }
14878 }
14879
14880 /* Check whether the producer field indicates either of GCC < 4.6, or the
14881 Intel C/C++ compiler, and cache the result in CU. */
14882
14883 static void
14884 check_producer (struct dwarf2_cu *cu)
14885 {
14886 int major, minor;
14887
14888 if (cu->producer == NULL)
14889 {
14890 /* For unknown compilers expect their behavior is DWARF version
14891 compliant.
14892
14893 GCC started to support .debug_types sections by -gdwarf-4 since
14894 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14895 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14896 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14897 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14898 }
14899 else if (producer_is_gcc (cu->producer, &major, &minor))
14900 {
14901 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14902 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14903 }
14904 else if (producer_is_icc (cu->producer, &major, &minor))
14905 {
14906 cu->producer_is_icc = true;
14907 cu->producer_is_icc_lt_14 = major < 14;
14908 }
14909 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14910 cu->producer_is_codewarrior = true;
14911 else
14912 {
14913 /* For other non-GCC compilers, expect their behavior is DWARF version
14914 compliant. */
14915 }
14916
14917 cu->checked_producer = true;
14918 }
14919
14920 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14921 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14922 during 4.6.0 experimental. */
14923
14924 static bool
14925 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14926 {
14927 if (!cu->checked_producer)
14928 check_producer (cu);
14929
14930 return cu->producer_is_gxx_lt_4_6;
14931 }
14932
14933
14934 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14935 with incorrect is_stmt attributes. */
14936
14937 static bool
14938 producer_is_codewarrior (struct dwarf2_cu *cu)
14939 {
14940 if (!cu->checked_producer)
14941 check_producer (cu);
14942
14943 return cu->producer_is_codewarrior;
14944 }
14945
14946 /* Return the default accessibility type if it is not overriden by
14947 DW_AT_accessibility. */
14948
14949 static enum dwarf_access_attribute
14950 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14951 {
14952 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14953 {
14954 /* The default DWARF 2 accessibility for members is public, the default
14955 accessibility for inheritance is private. */
14956
14957 if (die->tag != DW_TAG_inheritance)
14958 return DW_ACCESS_public;
14959 else
14960 return DW_ACCESS_private;
14961 }
14962 else
14963 {
14964 /* DWARF 3+ defines the default accessibility a different way. The same
14965 rules apply now for DW_TAG_inheritance as for the members and it only
14966 depends on the container kind. */
14967
14968 if (die->parent->tag == DW_TAG_class_type)
14969 return DW_ACCESS_private;
14970 else
14971 return DW_ACCESS_public;
14972 }
14973 }
14974
14975 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14976 offset. If the attribute was not found return 0, otherwise return
14977 1. If it was found but could not properly be handled, set *OFFSET
14978 to 0. */
14979
14980 static int
14981 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14982 LONGEST *offset)
14983 {
14984 struct attribute *attr;
14985
14986 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14987 if (attr != NULL)
14988 {
14989 *offset = 0;
14990
14991 /* Note that we do not check for a section offset first here.
14992 This is because DW_AT_data_member_location is new in DWARF 4,
14993 so if we see it, we can assume that a constant form is really
14994 a constant and not a section offset. */
14995 if (attr_form_is_constant (attr))
14996 *offset = dwarf2_get_attr_constant_value (attr, 0);
14997 else if (attr_form_is_section_offset (attr))
14998 dwarf2_complex_location_expr_complaint ();
14999 else if (attr_form_is_block (attr))
15000 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15001 else
15002 dwarf2_complex_location_expr_complaint ();
15003
15004 return 1;
15005 }
15006
15007 return 0;
15008 }
15009
15010 /* Add an aggregate field to the field list. */
15011
15012 static void
15013 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15014 struct dwarf2_cu *cu)
15015 {
15016 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15017 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15018 struct nextfield *new_field;
15019 struct attribute *attr;
15020 struct field *fp;
15021 const char *fieldname = "";
15022
15023 if (die->tag == DW_TAG_inheritance)
15024 {
15025 fip->baseclasses.emplace_back ();
15026 new_field = &fip->baseclasses.back ();
15027 }
15028 else
15029 {
15030 fip->fields.emplace_back ();
15031 new_field = &fip->fields.back ();
15032 }
15033
15034 fip->nfields++;
15035
15036 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15037 if (attr)
15038 new_field->accessibility = DW_UNSND (attr);
15039 else
15040 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15041 if (new_field->accessibility != DW_ACCESS_public)
15042 fip->non_public_fields = 1;
15043
15044 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15045 if (attr)
15046 new_field->virtuality = DW_UNSND (attr);
15047 else
15048 new_field->virtuality = DW_VIRTUALITY_none;
15049
15050 fp = &new_field->field;
15051
15052 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15053 {
15054 LONGEST offset;
15055
15056 /* Data member other than a C++ static data member. */
15057
15058 /* Get type of field. */
15059 fp->type = die_type (die, cu);
15060
15061 SET_FIELD_BITPOS (*fp, 0);
15062
15063 /* Get bit size of field (zero if none). */
15064 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15065 if (attr)
15066 {
15067 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15068 }
15069 else
15070 {
15071 FIELD_BITSIZE (*fp) = 0;
15072 }
15073
15074 /* Get bit offset of field. */
15075 if (handle_data_member_location (die, cu, &offset))
15076 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15077 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15078 if (attr)
15079 {
15080 if (gdbarch_bits_big_endian (gdbarch))
15081 {
15082 /* For big endian bits, the DW_AT_bit_offset gives the
15083 additional bit offset from the MSB of the containing
15084 anonymous object to the MSB of the field. We don't
15085 have to do anything special since we don't need to
15086 know the size of the anonymous object. */
15087 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15088 }
15089 else
15090 {
15091 /* For little endian bits, compute the bit offset to the
15092 MSB of the anonymous object, subtract off the number of
15093 bits from the MSB of the field to the MSB of the
15094 object, and then subtract off the number of bits of
15095 the field itself. The result is the bit offset of
15096 the LSB of the field. */
15097 int anonymous_size;
15098 int bit_offset = DW_UNSND (attr);
15099
15100 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15101 if (attr)
15102 {
15103 /* The size of the anonymous object containing
15104 the bit field is explicit, so use the
15105 indicated size (in bytes). */
15106 anonymous_size = DW_UNSND (attr);
15107 }
15108 else
15109 {
15110 /* The size of the anonymous object containing
15111 the bit field must be inferred from the type
15112 attribute of the data member containing the
15113 bit field. */
15114 anonymous_size = TYPE_LENGTH (fp->type);
15115 }
15116 SET_FIELD_BITPOS (*fp,
15117 (FIELD_BITPOS (*fp)
15118 + anonymous_size * bits_per_byte
15119 - bit_offset - FIELD_BITSIZE (*fp)));
15120 }
15121 }
15122 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15123 if (attr != NULL)
15124 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15125 + dwarf2_get_attr_constant_value (attr, 0)));
15126
15127 /* Get name of field. */
15128 fieldname = dwarf2_name (die, cu);
15129 if (fieldname == NULL)
15130 fieldname = "";
15131
15132 /* The name is already allocated along with this objfile, so we don't
15133 need to duplicate it for the type. */
15134 fp->name = fieldname;
15135
15136 /* Change accessibility for artificial fields (e.g. virtual table
15137 pointer or virtual base class pointer) to private. */
15138 if (dwarf2_attr (die, DW_AT_artificial, cu))
15139 {
15140 FIELD_ARTIFICIAL (*fp) = 1;
15141 new_field->accessibility = DW_ACCESS_private;
15142 fip->non_public_fields = 1;
15143 }
15144 }
15145 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15146 {
15147 /* C++ static member. */
15148
15149 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15150 is a declaration, but all versions of G++ as of this writing
15151 (so through at least 3.2.1) incorrectly generate
15152 DW_TAG_variable tags. */
15153
15154 const char *physname;
15155
15156 /* Get name of field. */
15157 fieldname = dwarf2_name (die, cu);
15158 if (fieldname == NULL)
15159 return;
15160
15161 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15162 if (attr
15163 /* Only create a symbol if this is an external value.
15164 new_symbol checks this and puts the value in the global symbol
15165 table, which we want. If it is not external, new_symbol
15166 will try to put the value in cu->list_in_scope which is wrong. */
15167 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15168 {
15169 /* A static const member, not much different than an enum as far as
15170 we're concerned, except that we can support more types. */
15171 new_symbol (die, NULL, cu);
15172 }
15173
15174 /* Get physical name. */
15175 physname = dwarf2_physname (fieldname, die, cu);
15176
15177 /* The name is already allocated along with this objfile, so we don't
15178 need to duplicate it for the type. */
15179 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15180 FIELD_TYPE (*fp) = die_type (die, cu);
15181 FIELD_NAME (*fp) = fieldname;
15182 }
15183 else if (die->tag == DW_TAG_inheritance)
15184 {
15185 LONGEST offset;
15186
15187 /* C++ base class field. */
15188 if (handle_data_member_location (die, cu, &offset))
15189 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15190 FIELD_BITSIZE (*fp) = 0;
15191 FIELD_TYPE (*fp) = die_type (die, cu);
15192 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
15193 }
15194 else if (die->tag == DW_TAG_variant_part)
15195 {
15196 /* process_structure_scope will treat this DIE as a union. */
15197 process_structure_scope (die, cu);
15198
15199 /* The variant part is relative to the start of the enclosing
15200 structure. */
15201 SET_FIELD_BITPOS (*fp, 0);
15202 fp->type = get_die_type (die, cu);
15203 fp->artificial = 1;
15204 fp->name = "<<variant>>";
15205
15206 /* Normally a DW_TAG_variant_part won't have a size, but our
15207 representation requires one, so set it to the maximum of the
15208 child sizes. */
15209 if (TYPE_LENGTH (fp->type) == 0)
15210 {
15211 unsigned max = 0;
15212 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15213 if (TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)) > max)
15214 max = TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i));
15215 TYPE_LENGTH (fp->type) = max;
15216 }
15217 }
15218 else
15219 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15220 }
15221
15222 /* Can the type given by DIE define another type? */
15223
15224 static bool
15225 type_can_define_types (const struct die_info *die)
15226 {
15227 switch (die->tag)
15228 {
15229 case DW_TAG_typedef:
15230 case DW_TAG_class_type:
15231 case DW_TAG_structure_type:
15232 case DW_TAG_union_type:
15233 case DW_TAG_enumeration_type:
15234 return true;
15235
15236 default:
15237 return false;
15238 }
15239 }
15240
15241 /* Add a type definition defined in the scope of the FIP's class. */
15242
15243 static void
15244 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15245 struct dwarf2_cu *cu)
15246 {
15247 struct decl_field fp;
15248 memset (&fp, 0, sizeof (fp));
15249
15250 gdb_assert (type_can_define_types (die));
15251
15252 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15253 fp.name = dwarf2_name (die, cu);
15254 fp.type = read_type_die (die, cu);
15255
15256 /* Save accessibility. */
15257 enum dwarf_access_attribute accessibility;
15258 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15259 if (attr != NULL)
15260 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15261 else
15262 accessibility = dwarf2_default_access_attribute (die, cu);
15263 switch (accessibility)
15264 {
15265 case DW_ACCESS_public:
15266 /* The assumed value if neither private nor protected. */
15267 break;
15268 case DW_ACCESS_private:
15269 fp.is_private = 1;
15270 break;
15271 case DW_ACCESS_protected:
15272 fp.is_protected = 1;
15273 break;
15274 default:
15275 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15276 }
15277
15278 if (die->tag == DW_TAG_typedef)
15279 fip->typedef_field_list.push_back (fp);
15280 else
15281 fip->nested_types_list.push_back (fp);
15282 }
15283
15284 /* Create the vector of fields, and attach it to the type. */
15285
15286 static void
15287 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15288 struct dwarf2_cu *cu)
15289 {
15290 int nfields = fip->nfields;
15291
15292 /* Record the field count, allocate space for the array of fields,
15293 and create blank accessibility bitfields if necessary. */
15294 TYPE_NFIELDS (type) = nfields;
15295 TYPE_FIELDS (type) = (struct field *)
15296 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15297
15298 if (fip->non_public_fields && cu->language != language_ada)
15299 {
15300 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15301
15302 TYPE_FIELD_PRIVATE_BITS (type) =
15303 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15304 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15305
15306 TYPE_FIELD_PROTECTED_BITS (type) =
15307 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15308 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15309
15310 TYPE_FIELD_IGNORE_BITS (type) =
15311 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15312 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15313 }
15314
15315 /* If the type has baseclasses, allocate and clear a bit vector for
15316 TYPE_FIELD_VIRTUAL_BITS. */
15317 if (!fip->baseclasses.empty () && cu->language != language_ada)
15318 {
15319 int num_bytes = B_BYTES (fip->baseclasses.size ());
15320 unsigned char *pointer;
15321
15322 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15323 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15324 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15325 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15326 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15327 }
15328
15329 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15330 {
15331 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15332
15333 for (int index = 0; index < nfields; ++index)
15334 {
15335 struct nextfield &field = fip->fields[index];
15336
15337 if (field.variant.is_discriminant)
15338 di->discriminant_index = index;
15339 else if (field.variant.default_branch)
15340 di->default_index = index;
15341 else
15342 di->discriminants[index] = field.variant.discriminant_value;
15343 }
15344 }
15345
15346 /* Copy the saved-up fields into the field vector. */
15347 for (int i = 0; i < nfields; ++i)
15348 {
15349 struct nextfield &field
15350 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15351 : fip->fields[i - fip->baseclasses.size ()]);
15352
15353 TYPE_FIELD (type, i) = field.field;
15354 switch (field.accessibility)
15355 {
15356 case DW_ACCESS_private:
15357 if (cu->language != language_ada)
15358 SET_TYPE_FIELD_PRIVATE (type, i);
15359 break;
15360
15361 case DW_ACCESS_protected:
15362 if (cu->language != language_ada)
15363 SET_TYPE_FIELD_PROTECTED (type, i);
15364 break;
15365
15366 case DW_ACCESS_public:
15367 break;
15368
15369 default:
15370 /* Unknown accessibility. Complain and treat it as public. */
15371 {
15372 complaint (_("unsupported accessibility %d"),
15373 field.accessibility);
15374 }
15375 break;
15376 }
15377 if (i < fip->baseclasses.size ())
15378 {
15379 switch (field.virtuality)
15380 {
15381 case DW_VIRTUALITY_virtual:
15382 case DW_VIRTUALITY_pure_virtual:
15383 if (cu->language == language_ada)
15384 error (_("unexpected virtuality in component of Ada type"));
15385 SET_TYPE_FIELD_VIRTUAL (type, i);
15386 break;
15387 }
15388 }
15389 }
15390 }
15391
15392 /* Return true if this member function is a constructor, false
15393 otherwise. */
15394
15395 static int
15396 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15397 {
15398 const char *fieldname;
15399 const char *type_name;
15400 int len;
15401
15402 if (die->parent == NULL)
15403 return 0;
15404
15405 if (die->parent->tag != DW_TAG_structure_type
15406 && die->parent->tag != DW_TAG_union_type
15407 && die->parent->tag != DW_TAG_class_type)
15408 return 0;
15409
15410 fieldname = dwarf2_name (die, cu);
15411 type_name = dwarf2_name (die->parent, cu);
15412 if (fieldname == NULL || type_name == NULL)
15413 return 0;
15414
15415 len = strlen (fieldname);
15416 return (strncmp (fieldname, type_name, len) == 0
15417 && (type_name[len] == '\0' || type_name[len] == '<'));
15418 }
15419
15420 /* Add a member function to the proper fieldlist. */
15421
15422 static void
15423 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15424 struct type *type, struct dwarf2_cu *cu)
15425 {
15426 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15427 struct attribute *attr;
15428 int i;
15429 struct fnfieldlist *flp = nullptr;
15430 struct fn_field *fnp;
15431 const char *fieldname;
15432 struct type *this_type;
15433 enum dwarf_access_attribute accessibility;
15434
15435 if (cu->language == language_ada)
15436 error (_("unexpected member function in Ada type"));
15437
15438 /* Get name of member function. */
15439 fieldname = dwarf2_name (die, cu);
15440 if (fieldname == NULL)
15441 return;
15442
15443 /* Look up member function name in fieldlist. */
15444 for (i = 0; i < fip->fnfieldlists.size (); i++)
15445 {
15446 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15447 {
15448 flp = &fip->fnfieldlists[i];
15449 break;
15450 }
15451 }
15452
15453 /* Create a new fnfieldlist if necessary. */
15454 if (flp == nullptr)
15455 {
15456 fip->fnfieldlists.emplace_back ();
15457 flp = &fip->fnfieldlists.back ();
15458 flp->name = fieldname;
15459 i = fip->fnfieldlists.size () - 1;
15460 }
15461
15462 /* Create a new member function field and add it to the vector of
15463 fnfieldlists. */
15464 flp->fnfields.emplace_back ();
15465 fnp = &flp->fnfields.back ();
15466
15467 /* Delay processing of the physname until later. */
15468 if (cu->language == language_cplus)
15469 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15470 die, cu);
15471 else
15472 {
15473 const char *physname = dwarf2_physname (fieldname, die, cu);
15474 fnp->physname = physname ? physname : "";
15475 }
15476
15477 fnp->type = alloc_type (objfile);
15478 this_type = read_type_die (die, cu);
15479 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15480 {
15481 int nparams = TYPE_NFIELDS (this_type);
15482
15483 /* TYPE is the domain of this method, and THIS_TYPE is the type
15484 of the method itself (TYPE_CODE_METHOD). */
15485 smash_to_method_type (fnp->type, type,
15486 TYPE_TARGET_TYPE (this_type),
15487 TYPE_FIELDS (this_type),
15488 TYPE_NFIELDS (this_type),
15489 TYPE_VARARGS (this_type));
15490
15491 /* Handle static member functions.
15492 Dwarf2 has no clean way to discern C++ static and non-static
15493 member functions. G++ helps GDB by marking the first
15494 parameter for non-static member functions (which is the this
15495 pointer) as artificial. We obtain this information from
15496 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15497 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15498 fnp->voffset = VOFFSET_STATIC;
15499 }
15500 else
15501 complaint (_("member function type missing for '%s'"),
15502 dwarf2_full_name (fieldname, die, cu));
15503
15504 /* Get fcontext from DW_AT_containing_type if present. */
15505 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15506 fnp->fcontext = die_containing_type (die, cu);
15507
15508 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15509 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15510
15511 /* Get accessibility. */
15512 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15513 if (attr)
15514 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15515 else
15516 accessibility = dwarf2_default_access_attribute (die, cu);
15517 switch (accessibility)
15518 {
15519 case DW_ACCESS_private:
15520 fnp->is_private = 1;
15521 break;
15522 case DW_ACCESS_protected:
15523 fnp->is_protected = 1;
15524 break;
15525 }
15526
15527 /* Check for artificial methods. */
15528 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15529 if (attr && DW_UNSND (attr) != 0)
15530 fnp->is_artificial = 1;
15531
15532 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15533
15534 /* Get index in virtual function table if it is a virtual member
15535 function. For older versions of GCC, this is an offset in the
15536 appropriate virtual table, as specified by DW_AT_containing_type.
15537 For everyone else, it is an expression to be evaluated relative
15538 to the object address. */
15539
15540 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15541 if (attr)
15542 {
15543 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15544 {
15545 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15546 {
15547 /* Old-style GCC. */
15548 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15549 }
15550 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15551 || (DW_BLOCK (attr)->size > 1
15552 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15553 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15554 {
15555 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15556 if ((fnp->voffset % cu->header.addr_size) != 0)
15557 dwarf2_complex_location_expr_complaint ();
15558 else
15559 fnp->voffset /= cu->header.addr_size;
15560 fnp->voffset += 2;
15561 }
15562 else
15563 dwarf2_complex_location_expr_complaint ();
15564
15565 if (!fnp->fcontext)
15566 {
15567 /* If there is no `this' field and no DW_AT_containing_type,
15568 we cannot actually find a base class context for the
15569 vtable! */
15570 if (TYPE_NFIELDS (this_type) == 0
15571 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15572 {
15573 complaint (_("cannot determine context for virtual member "
15574 "function \"%s\" (offset %s)"),
15575 fieldname, sect_offset_str (die->sect_off));
15576 }
15577 else
15578 {
15579 fnp->fcontext
15580 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15581 }
15582 }
15583 }
15584 else if (attr_form_is_section_offset (attr))
15585 {
15586 dwarf2_complex_location_expr_complaint ();
15587 }
15588 else
15589 {
15590 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15591 fieldname);
15592 }
15593 }
15594 else
15595 {
15596 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15597 if (attr && DW_UNSND (attr))
15598 {
15599 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15600 complaint (_("Member function \"%s\" (offset %s) is virtual "
15601 "but the vtable offset is not specified"),
15602 fieldname, sect_offset_str (die->sect_off));
15603 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15604 TYPE_CPLUS_DYNAMIC (type) = 1;
15605 }
15606 }
15607 }
15608
15609 /* Create the vector of member function fields, and attach it to the type. */
15610
15611 static void
15612 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15613 struct dwarf2_cu *cu)
15614 {
15615 if (cu->language == language_ada)
15616 error (_("unexpected member functions in Ada type"));
15617
15618 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15619 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15620 TYPE_ALLOC (type,
15621 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15622
15623 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15624 {
15625 struct fnfieldlist &nf = fip->fnfieldlists[i];
15626 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15627
15628 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15629 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15630 fn_flp->fn_fields = (struct fn_field *)
15631 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15632
15633 for (int k = 0; k < nf.fnfields.size (); ++k)
15634 fn_flp->fn_fields[k] = nf.fnfields[k];
15635 }
15636
15637 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15638 }
15639
15640 /* Returns non-zero if NAME is the name of a vtable member in CU's
15641 language, zero otherwise. */
15642 static int
15643 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15644 {
15645 static const char vptr[] = "_vptr";
15646
15647 /* Look for the C++ form of the vtable. */
15648 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15649 return 1;
15650
15651 return 0;
15652 }
15653
15654 /* GCC outputs unnamed structures that are really pointers to member
15655 functions, with the ABI-specified layout. If TYPE describes
15656 such a structure, smash it into a member function type.
15657
15658 GCC shouldn't do this; it should just output pointer to member DIEs.
15659 This is GCC PR debug/28767. */
15660
15661 static void
15662 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15663 {
15664 struct type *pfn_type, *self_type, *new_type;
15665
15666 /* Check for a structure with no name and two children. */
15667 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15668 return;
15669
15670 /* Check for __pfn and __delta members. */
15671 if (TYPE_FIELD_NAME (type, 0) == NULL
15672 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15673 || TYPE_FIELD_NAME (type, 1) == NULL
15674 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15675 return;
15676
15677 /* Find the type of the method. */
15678 pfn_type = TYPE_FIELD_TYPE (type, 0);
15679 if (pfn_type == NULL
15680 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15681 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15682 return;
15683
15684 /* Look for the "this" argument. */
15685 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15686 if (TYPE_NFIELDS (pfn_type) == 0
15687 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15688 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15689 return;
15690
15691 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15692 new_type = alloc_type (objfile);
15693 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15694 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15695 TYPE_VARARGS (pfn_type));
15696 smash_to_methodptr_type (type, new_type);
15697 }
15698
15699 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15700 appropriate error checking and issuing complaints if there is a
15701 problem. */
15702
15703 static ULONGEST
15704 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15705 {
15706 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15707
15708 if (attr == nullptr)
15709 return 0;
15710
15711 if (!attr_form_is_constant (attr))
15712 {
15713 complaint (_("DW_AT_alignment must have constant form"
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 return 0;
15718 }
15719
15720 ULONGEST align;
15721 if (attr->form == DW_FORM_sdata)
15722 {
15723 LONGEST val = DW_SND (attr);
15724 if (val < 0)
15725 {
15726 complaint (_("DW_AT_alignment value must not be negative"
15727 " - DIE at %s [in module %s]"),
15728 sect_offset_str (die->sect_off),
15729 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15730 return 0;
15731 }
15732 align = val;
15733 }
15734 else
15735 align = DW_UNSND (attr);
15736
15737 if (align == 0)
15738 {
15739 complaint (_("DW_AT_alignment value must not be zero"
15740 " - DIE at %s [in module %s]"),
15741 sect_offset_str (die->sect_off),
15742 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15743 return 0;
15744 }
15745 if ((align & (align - 1)) != 0)
15746 {
15747 complaint (_("DW_AT_alignment value must be a power of 2"
15748 " - DIE at %s [in module %s]"),
15749 sect_offset_str (die->sect_off),
15750 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15751 return 0;
15752 }
15753
15754 return align;
15755 }
15756
15757 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15758 the alignment for TYPE. */
15759
15760 static void
15761 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15762 struct type *type)
15763 {
15764 if (!set_type_align (type, get_alignment (cu, die)))
15765 complaint (_("DW_AT_alignment value too large"
15766 " - DIE at %s [in module %s]"),
15767 sect_offset_str (die->sect_off),
15768 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15769 }
15770
15771 /* Called when we find the DIE that starts a structure or union scope
15772 (definition) to create a type for the structure or union. Fill in
15773 the type's name and general properties; the members will not be
15774 processed until process_structure_scope. A symbol table entry for
15775 the type will also not be done until process_structure_scope (assuming
15776 the type has a name).
15777
15778 NOTE: we need to call these functions regardless of whether or not the
15779 DIE has a DW_AT_name attribute, since it might be an anonymous
15780 structure or union. This gets the type entered into our set of
15781 user defined types. */
15782
15783 static struct type *
15784 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15785 {
15786 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15787 struct type *type;
15788 struct attribute *attr;
15789 const char *name;
15790
15791 /* If the definition of this type lives in .debug_types, read that type.
15792 Don't follow DW_AT_specification though, that will take us back up
15793 the chain and we want to go down. */
15794 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15795 if (attr)
15796 {
15797 type = get_DW_AT_signature_type (die, attr, cu);
15798
15799 /* The type's CU may not be the same as CU.
15800 Ensure TYPE is recorded with CU in die_type_hash. */
15801 return set_die_type (die, type, cu);
15802 }
15803
15804 type = alloc_type (objfile);
15805 INIT_CPLUS_SPECIFIC (type);
15806
15807 name = dwarf2_name (die, cu);
15808 if (name != NULL)
15809 {
15810 if (cu->language == language_cplus
15811 || cu->language == language_d
15812 || cu->language == language_rust)
15813 {
15814 const char *full_name = dwarf2_full_name (name, die, cu);
15815
15816 /* dwarf2_full_name might have already finished building the DIE's
15817 type. If so, there is no need to continue. */
15818 if (get_die_type (die, cu) != NULL)
15819 return get_die_type (die, cu);
15820
15821 TYPE_NAME (type) = full_name;
15822 }
15823 else
15824 {
15825 /* The name is already allocated along with this objfile, so
15826 we don't need to duplicate it for the type. */
15827 TYPE_NAME (type) = name;
15828 }
15829 }
15830
15831 if (die->tag == DW_TAG_structure_type)
15832 {
15833 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15834 }
15835 else if (die->tag == DW_TAG_union_type)
15836 {
15837 TYPE_CODE (type) = TYPE_CODE_UNION;
15838 }
15839 else if (die->tag == DW_TAG_variant_part)
15840 {
15841 TYPE_CODE (type) = TYPE_CODE_UNION;
15842 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15843 }
15844 else
15845 {
15846 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15847 }
15848
15849 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15850 TYPE_DECLARED_CLASS (type) = 1;
15851
15852 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15853 if (attr)
15854 {
15855 if (attr_form_is_constant (attr))
15856 TYPE_LENGTH (type) = DW_UNSND (attr);
15857 else
15858 {
15859 /* For the moment, dynamic type sizes are not supported
15860 by GDB's struct type. The actual size is determined
15861 on-demand when resolving the type of a given object,
15862 so set the type's length to zero for now. Otherwise,
15863 we record an expression as the length, and that expression
15864 could lead to a very large value, which could eventually
15865 lead to us trying to allocate that much memory when creating
15866 a value of that type. */
15867 TYPE_LENGTH (type) = 0;
15868 }
15869 }
15870 else
15871 {
15872 TYPE_LENGTH (type) = 0;
15873 }
15874
15875 maybe_set_alignment (cu, die, type);
15876
15877 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15878 {
15879 /* ICC<14 does not output the required DW_AT_declaration on
15880 incomplete types, but gives them a size of zero. */
15881 TYPE_STUB (type) = 1;
15882 }
15883 else
15884 TYPE_STUB_SUPPORTED (type) = 1;
15885
15886 if (die_is_declaration (die, cu))
15887 TYPE_STUB (type) = 1;
15888 else if (attr == NULL && die->child == NULL
15889 && producer_is_realview (cu->producer))
15890 /* RealView does not output the required DW_AT_declaration
15891 on incomplete types. */
15892 TYPE_STUB (type) = 1;
15893
15894 /* We need to add the type field to the die immediately so we don't
15895 infinitely recurse when dealing with pointers to the structure
15896 type within the structure itself. */
15897 set_die_type (die, type, cu);
15898
15899 /* set_die_type should be already done. */
15900 set_descriptive_type (type, die, cu);
15901
15902 return type;
15903 }
15904
15905 /* A helper for process_structure_scope that handles a single member
15906 DIE. */
15907
15908 static void
15909 handle_struct_member_die (struct die_info *child_die, struct type *type,
15910 struct field_info *fi,
15911 std::vector<struct symbol *> *template_args,
15912 struct dwarf2_cu *cu)
15913 {
15914 if (child_die->tag == DW_TAG_member
15915 || child_die->tag == DW_TAG_variable
15916 || child_die->tag == DW_TAG_variant_part)
15917 {
15918 /* NOTE: carlton/2002-11-05: A C++ static data member
15919 should be a DW_TAG_member that is a declaration, but
15920 all versions of G++ as of this writing (so through at
15921 least 3.2.1) incorrectly generate DW_TAG_variable
15922 tags for them instead. */
15923 dwarf2_add_field (fi, child_die, cu);
15924 }
15925 else if (child_die->tag == DW_TAG_subprogram)
15926 {
15927 /* Rust doesn't have member functions in the C++ sense.
15928 However, it does emit ordinary functions as children
15929 of a struct DIE. */
15930 if (cu->language == language_rust)
15931 read_func_scope (child_die, cu);
15932 else
15933 {
15934 /* C++ member function. */
15935 dwarf2_add_member_fn (fi, child_die, type, cu);
15936 }
15937 }
15938 else if (child_die->tag == DW_TAG_inheritance)
15939 {
15940 /* C++ base class field. */
15941 dwarf2_add_field (fi, child_die, cu);
15942 }
15943 else if (type_can_define_types (child_die))
15944 dwarf2_add_type_defn (fi, child_die, cu);
15945 else if (child_die->tag == DW_TAG_template_type_param
15946 || child_die->tag == DW_TAG_template_value_param)
15947 {
15948 struct symbol *arg = new_symbol (child_die, NULL, cu);
15949
15950 if (arg != NULL)
15951 template_args->push_back (arg);
15952 }
15953 else if (child_die->tag == DW_TAG_variant)
15954 {
15955 /* In a variant we want to get the discriminant and also add a
15956 field for our sole member child. */
15957 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15958
15959 for (die_info *variant_child = child_die->child;
15960 variant_child != NULL;
15961 variant_child = sibling_die (variant_child))
15962 {
15963 if (variant_child->tag == DW_TAG_member)
15964 {
15965 handle_struct_member_die (variant_child, type, fi,
15966 template_args, cu);
15967 /* Only handle the one. */
15968 break;
15969 }
15970 }
15971
15972 /* We don't handle this but we might as well report it if we see
15973 it. */
15974 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15975 complaint (_("DW_AT_discr_list is not supported yet"
15976 " - DIE at %s [in module %s]"),
15977 sect_offset_str (child_die->sect_off),
15978 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15979
15980 /* The first field was just added, so we can stash the
15981 discriminant there. */
15982 gdb_assert (!fi->fields.empty ());
15983 if (discr == NULL)
15984 fi->fields.back ().variant.default_branch = true;
15985 else
15986 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15987 }
15988 }
15989
15990 /* Finish creating a structure or union type, including filling in
15991 its members and creating a symbol for it. */
15992
15993 static void
15994 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15995 {
15996 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15997 struct die_info *child_die;
15998 struct type *type;
15999
16000 type = get_die_type (die, cu);
16001 if (type == NULL)
16002 type = read_structure_type (die, cu);
16003
16004 /* When reading a DW_TAG_variant_part, we need to notice when we
16005 read the discriminant member, so we can record it later in the
16006 discriminant_info. */
16007 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16008 sect_offset discr_offset;
16009 bool has_template_parameters = false;
16010
16011 if (is_variant_part)
16012 {
16013 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16014 if (discr == NULL)
16015 {
16016 /* Maybe it's a univariant form, an extension we support.
16017 In this case arrange not to check the offset. */
16018 is_variant_part = false;
16019 }
16020 else if (attr_form_is_ref (discr))
16021 {
16022 struct dwarf2_cu *target_cu = cu;
16023 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16024
16025 discr_offset = target_die->sect_off;
16026 }
16027 else
16028 {
16029 complaint (_("DW_AT_discr does not have DIE reference form"
16030 " - DIE at %s [in module %s]"),
16031 sect_offset_str (die->sect_off),
16032 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16033 is_variant_part = false;
16034 }
16035 }
16036
16037 if (die->child != NULL && ! die_is_declaration (die, cu))
16038 {
16039 struct field_info fi;
16040 std::vector<struct symbol *> template_args;
16041
16042 child_die = die->child;
16043
16044 while (child_die && child_die->tag)
16045 {
16046 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16047
16048 if (is_variant_part && discr_offset == child_die->sect_off)
16049 fi.fields.back ().variant.is_discriminant = true;
16050
16051 child_die = sibling_die (child_die);
16052 }
16053
16054 /* Attach template arguments to type. */
16055 if (!template_args.empty ())
16056 {
16057 has_template_parameters = true;
16058 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16059 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16060 TYPE_TEMPLATE_ARGUMENTS (type)
16061 = XOBNEWVEC (&objfile->objfile_obstack,
16062 struct symbol *,
16063 TYPE_N_TEMPLATE_ARGUMENTS (type));
16064 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16065 template_args.data (),
16066 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16067 * sizeof (struct symbol *)));
16068 }
16069
16070 /* Attach fields and member functions to the type. */
16071 if (fi.nfields)
16072 dwarf2_attach_fields_to_type (&fi, type, cu);
16073 if (!fi.fnfieldlists.empty ())
16074 {
16075 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16076
16077 /* Get the type which refers to the base class (possibly this
16078 class itself) which contains the vtable pointer for the current
16079 class from the DW_AT_containing_type attribute. This use of
16080 DW_AT_containing_type is a GNU extension. */
16081
16082 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16083 {
16084 struct type *t = die_containing_type (die, cu);
16085
16086 set_type_vptr_basetype (type, t);
16087 if (type == t)
16088 {
16089 int i;
16090
16091 /* Our own class provides vtbl ptr. */
16092 for (i = TYPE_NFIELDS (t) - 1;
16093 i >= TYPE_N_BASECLASSES (t);
16094 --i)
16095 {
16096 const char *fieldname = TYPE_FIELD_NAME (t, i);
16097
16098 if (is_vtable_name (fieldname, cu))
16099 {
16100 set_type_vptr_fieldno (type, i);
16101 break;
16102 }
16103 }
16104
16105 /* Complain if virtual function table field not found. */
16106 if (i < TYPE_N_BASECLASSES (t))
16107 complaint (_("virtual function table pointer "
16108 "not found when defining class '%s'"),
16109 TYPE_NAME (type) ? TYPE_NAME (type) : "");
16110 }
16111 else
16112 {
16113 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16114 }
16115 }
16116 else if (cu->producer
16117 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16118 {
16119 /* The IBM XLC compiler does not provide direct indication
16120 of the containing type, but the vtable pointer is
16121 always named __vfp. */
16122
16123 int i;
16124
16125 for (i = TYPE_NFIELDS (type) - 1;
16126 i >= TYPE_N_BASECLASSES (type);
16127 --i)
16128 {
16129 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16130 {
16131 set_type_vptr_fieldno (type, i);
16132 set_type_vptr_basetype (type, type);
16133 break;
16134 }
16135 }
16136 }
16137 }
16138
16139 /* Copy fi.typedef_field_list linked list elements content into the
16140 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16141 if (!fi.typedef_field_list.empty ())
16142 {
16143 int count = fi.typedef_field_list.size ();
16144
16145 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16146 TYPE_TYPEDEF_FIELD_ARRAY (type)
16147 = ((struct decl_field *)
16148 TYPE_ALLOC (type,
16149 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16150 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16151
16152 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16153 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16154 }
16155
16156 /* Copy fi.nested_types_list linked list elements content into the
16157 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16158 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16159 {
16160 int count = fi.nested_types_list.size ();
16161
16162 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16163 TYPE_NESTED_TYPES_ARRAY (type)
16164 = ((struct decl_field *)
16165 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16166 TYPE_NESTED_TYPES_COUNT (type) = count;
16167
16168 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16169 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16170 }
16171 }
16172
16173 quirk_gcc_member_function_pointer (type, objfile);
16174 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16175 cu->rust_unions.push_back (type);
16176
16177 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16178 snapshots) has been known to create a die giving a declaration
16179 for a class that has, as a child, a die giving a definition for a
16180 nested class. So we have to process our children even if the
16181 current die is a declaration. Normally, of course, a declaration
16182 won't have any children at all. */
16183
16184 child_die = die->child;
16185
16186 while (child_die != NULL && child_die->tag)
16187 {
16188 if (child_die->tag == DW_TAG_member
16189 || child_die->tag == DW_TAG_variable
16190 || child_die->tag == DW_TAG_inheritance
16191 || child_die->tag == DW_TAG_template_value_param
16192 || child_die->tag == DW_TAG_template_type_param)
16193 {
16194 /* Do nothing. */
16195 }
16196 else
16197 process_die (child_die, cu);
16198
16199 child_die = sibling_die (child_die);
16200 }
16201
16202 /* Do not consider external references. According to the DWARF standard,
16203 these DIEs are identified by the fact that they have no byte_size
16204 attribute, and a declaration attribute. */
16205 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16206 || !die_is_declaration (die, cu))
16207 {
16208 struct symbol *sym = new_symbol (die, type, cu);
16209
16210 if (has_template_parameters)
16211 {
16212 struct symtab *symtab;
16213 if (sym != nullptr)
16214 symtab = symbol_symtab (sym);
16215 else if (cu->line_header != nullptr)
16216 {
16217 /* Any related symtab will do. */
16218 symtab
16219 = cu->line_header->file_name_at (file_name_index (1))->symtab;
16220 }
16221 else
16222 {
16223 symtab = nullptr;
16224 complaint (_("could not find suitable "
16225 "symtab for template parameter"
16226 " - DIE at %s [in module %s]"),
16227 sect_offset_str (die->sect_off),
16228 objfile_name (objfile));
16229 }
16230
16231 if (symtab != nullptr)
16232 {
16233 /* Make sure that the symtab is set on the new symbols.
16234 Even though they don't appear in this symtab directly,
16235 other parts of gdb assume that symbols do, and this is
16236 reasonably true. */
16237 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16238 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16239 }
16240 }
16241 }
16242 }
16243
16244 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16245 update TYPE using some information only available in DIE's children. */
16246
16247 static void
16248 update_enumeration_type_from_children (struct die_info *die,
16249 struct type *type,
16250 struct dwarf2_cu *cu)
16251 {
16252 struct die_info *child_die;
16253 int unsigned_enum = 1;
16254 int flag_enum = 1;
16255 ULONGEST mask = 0;
16256
16257 auto_obstack obstack;
16258
16259 for (child_die = die->child;
16260 child_die != NULL && child_die->tag;
16261 child_die = sibling_die (child_die))
16262 {
16263 struct attribute *attr;
16264 LONGEST value;
16265 const gdb_byte *bytes;
16266 struct dwarf2_locexpr_baton *baton;
16267 const char *name;
16268
16269 if (child_die->tag != DW_TAG_enumerator)
16270 continue;
16271
16272 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16273 if (attr == NULL)
16274 continue;
16275
16276 name = dwarf2_name (child_die, cu);
16277 if (name == NULL)
16278 name = "<anonymous enumerator>";
16279
16280 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16281 &value, &bytes, &baton);
16282 if (value < 0)
16283 {
16284 unsigned_enum = 0;
16285 flag_enum = 0;
16286 }
16287 else if ((mask & value) != 0)
16288 flag_enum = 0;
16289 else
16290 mask |= value;
16291
16292 /* If we already know that the enum type is neither unsigned, nor
16293 a flag type, no need to look at the rest of the enumerates. */
16294 if (!unsigned_enum && !flag_enum)
16295 break;
16296 }
16297
16298 if (unsigned_enum)
16299 TYPE_UNSIGNED (type) = 1;
16300 if (flag_enum)
16301 TYPE_FLAG_ENUM (type) = 1;
16302 }
16303
16304 /* Given a DW_AT_enumeration_type die, set its type. We do not
16305 complete the type's fields yet, or create any symbols. */
16306
16307 static struct type *
16308 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16309 {
16310 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16311 struct type *type;
16312 struct attribute *attr;
16313 const char *name;
16314
16315 /* If the definition of this type lives in .debug_types, read that type.
16316 Don't follow DW_AT_specification though, that will take us back up
16317 the chain and we want to go down. */
16318 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16319 if (attr)
16320 {
16321 type = get_DW_AT_signature_type (die, attr, cu);
16322
16323 /* The type's CU may not be the same as CU.
16324 Ensure TYPE is recorded with CU in die_type_hash. */
16325 return set_die_type (die, type, cu);
16326 }
16327
16328 type = alloc_type (objfile);
16329
16330 TYPE_CODE (type) = TYPE_CODE_ENUM;
16331 name = dwarf2_full_name (NULL, die, cu);
16332 if (name != NULL)
16333 TYPE_NAME (type) = name;
16334
16335 attr = dwarf2_attr (die, DW_AT_type, cu);
16336 if (attr != NULL)
16337 {
16338 struct type *underlying_type = die_type (die, cu);
16339
16340 TYPE_TARGET_TYPE (type) = underlying_type;
16341 }
16342
16343 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16344 if (attr)
16345 {
16346 TYPE_LENGTH (type) = DW_UNSND (attr);
16347 }
16348 else
16349 {
16350 TYPE_LENGTH (type) = 0;
16351 }
16352
16353 maybe_set_alignment (cu, die, type);
16354
16355 /* The enumeration DIE can be incomplete. In Ada, any type can be
16356 declared as private in the package spec, and then defined only
16357 inside the package body. Such types are known as Taft Amendment
16358 Types. When another package uses such a type, an incomplete DIE
16359 may be generated by the compiler. */
16360 if (die_is_declaration (die, cu))
16361 TYPE_STUB (type) = 1;
16362
16363 /* Finish the creation of this type by using the enum's children.
16364 We must call this even when the underlying type has been provided
16365 so that we can determine if we're looking at a "flag" enum. */
16366 update_enumeration_type_from_children (die, type, cu);
16367
16368 /* If this type has an underlying type that is not a stub, then we
16369 may use its attributes. We always use the "unsigned" attribute
16370 in this situation, because ordinarily we guess whether the type
16371 is unsigned -- but the guess can be wrong and the underlying type
16372 can tell us the reality. However, we defer to a local size
16373 attribute if one exists, because this lets the compiler override
16374 the underlying type if needed. */
16375 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16376 {
16377 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16378 if (TYPE_LENGTH (type) == 0)
16379 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16380 if (TYPE_RAW_ALIGN (type) == 0
16381 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16382 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16383 }
16384
16385 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16386
16387 return set_die_type (die, type, cu);
16388 }
16389
16390 /* Given a pointer to a die which begins an enumeration, process all
16391 the dies that define the members of the enumeration, and create the
16392 symbol for the enumeration type.
16393
16394 NOTE: We reverse the order of the element list. */
16395
16396 static void
16397 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16398 {
16399 struct type *this_type;
16400
16401 this_type = get_die_type (die, cu);
16402 if (this_type == NULL)
16403 this_type = read_enumeration_type (die, cu);
16404
16405 if (die->child != NULL)
16406 {
16407 struct die_info *child_die;
16408 struct symbol *sym;
16409 struct field *fields = NULL;
16410 int num_fields = 0;
16411 const char *name;
16412
16413 child_die = die->child;
16414 while (child_die && child_die->tag)
16415 {
16416 if (child_die->tag != DW_TAG_enumerator)
16417 {
16418 process_die (child_die, cu);
16419 }
16420 else
16421 {
16422 name = dwarf2_name (child_die, cu);
16423 if (name)
16424 {
16425 sym = new_symbol (child_die, this_type, cu);
16426
16427 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16428 {
16429 fields = (struct field *)
16430 xrealloc (fields,
16431 (num_fields + DW_FIELD_ALLOC_CHUNK)
16432 * sizeof (struct field));
16433 }
16434
16435 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16436 FIELD_TYPE (fields[num_fields]) = NULL;
16437 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16438 FIELD_BITSIZE (fields[num_fields]) = 0;
16439
16440 num_fields++;
16441 }
16442 }
16443
16444 child_die = sibling_die (child_die);
16445 }
16446
16447 if (num_fields)
16448 {
16449 TYPE_NFIELDS (this_type) = num_fields;
16450 TYPE_FIELDS (this_type) = (struct field *)
16451 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16452 memcpy (TYPE_FIELDS (this_type), fields,
16453 sizeof (struct field) * num_fields);
16454 xfree (fields);
16455 }
16456 }
16457
16458 /* If we are reading an enum from a .debug_types unit, and the enum
16459 is a declaration, and the enum is not the signatured type in the
16460 unit, then we do not want to add a symbol for it. Adding a
16461 symbol would in some cases obscure the true definition of the
16462 enum, giving users an incomplete type when the definition is
16463 actually available. Note that we do not want to do this for all
16464 enums which are just declarations, because C++0x allows forward
16465 enum declarations. */
16466 if (cu->per_cu->is_debug_types
16467 && die_is_declaration (die, cu))
16468 {
16469 struct signatured_type *sig_type;
16470
16471 sig_type = (struct signatured_type *) cu->per_cu;
16472 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16473 if (sig_type->type_offset_in_section != die->sect_off)
16474 return;
16475 }
16476
16477 new_symbol (die, this_type, cu);
16478 }
16479
16480 /* Extract all information from a DW_TAG_array_type DIE and put it in
16481 the DIE's type field. For now, this only handles one dimensional
16482 arrays. */
16483
16484 static struct type *
16485 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16486 {
16487 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16488 struct die_info *child_die;
16489 struct type *type;
16490 struct type *element_type, *range_type, *index_type;
16491 struct attribute *attr;
16492 const char *name;
16493 struct dynamic_prop *byte_stride_prop = NULL;
16494 unsigned int bit_stride = 0;
16495
16496 element_type = die_type (die, cu);
16497
16498 /* The die_type call above may have already set the type for this DIE. */
16499 type = get_die_type (die, cu);
16500 if (type)
16501 return type;
16502
16503 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16504 if (attr != NULL)
16505 {
16506 int stride_ok;
16507 struct type *prop_type
16508 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
16509
16510 byte_stride_prop
16511 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16512 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16513 prop_type);
16514 if (!stride_ok)
16515 {
16516 complaint (_("unable to read array DW_AT_byte_stride "
16517 " - DIE at %s [in module %s]"),
16518 sect_offset_str (die->sect_off),
16519 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16520 /* Ignore this attribute. We will likely not be able to print
16521 arrays of this type correctly, but there is little we can do
16522 to help if we cannot read the attribute's value. */
16523 byte_stride_prop = NULL;
16524 }
16525 }
16526
16527 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16528 if (attr != NULL)
16529 bit_stride = DW_UNSND (attr);
16530
16531 /* Irix 6.2 native cc creates array types without children for
16532 arrays with unspecified length. */
16533 if (die->child == NULL)
16534 {
16535 index_type = objfile_type (objfile)->builtin_int;
16536 range_type = create_static_range_type (NULL, index_type, 0, -1);
16537 type = create_array_type_with_stride (NULL, element_type, range_type,
16538 byte_stride_prop, bit_stride);
16539 return set_die_type (die, type, cu);
16540 }
16541
16542 std::vector<struct type *> range_types;
16543 child_die = die->child;
16544 while (child_die && child_die->tag)
16545 {
16546 if (child_die->tag == DW_TAG_subrange_type)
16547 {
16548 struct type *child_type = read_type_die (child_die, cu);
16549
16550 if (child_type != NULL)
16551 {
16552 /* The range type was succesfully read. Save it for the
16553 array type creation. */
16554 range_types.push_back (child_type);
16555 }
16556 }
16557 child_die = sibling_die (child_die);
16558 }
16559
16560 /* Dwarf2 dimensions are output from left to right, create the
16561 necessary array types in backwards order. */
16562
16563 type = element_type;
16564
16565 if (read_array_order (die, cu) == DW_ORD_col_major)
16566 {
16567 int i = 0;
16568
16569 while (i < range_types.size ())
16570 type = create_array_type_with_stride (NULL, type, range_types[i++],
16571 byte_stride_prop, bit_stride);
16572 }
16573 else
16574 {
16575 size_t ndim = range_types.size ();
16576 while (ndim-- > 0)
16577 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16578 byte_stride_prop, bit_stride);
16579 }
16580
16581 /* Understand Dwarf2 support for vector types (like they occur on
16582 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16583 array type. This is not part of the Dwarf2/3 standard yet, but a
16584 custom vendor extension. The main difference between a regular
16585 array and the vector variant is that vectors are passed by value
16586 to functions. */
16587 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16588 if (attr)
16589 make_vector_type (type);
16590
16591 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16592 implementation may choose to implement triple vectors using this
16593 attribute. */
16594 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16595 if (attr)
16596 {
16597 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16598 TYPE_LENGTH (type) = DW_UNSND (attr);
16599 else
16600 complaint (_("DW_AT_byte_size for array type smaller "
16601 "than the total size of elements"));
16602 }
16603
16604 name = dwarf2_name (die, cu);
16605 if (name)
16606 TYPE_NAME (type) = name;
16607
16608 maybe_set_alignment (cu, die, type);
16609
16610 /* Install the type in the die. */
16611 set_die_type (die, type, cu);
16612
16613 /* set_die_type should be already done. */
16614 set_descriptive_type (type, die, cu);
16615
16616 return type;
16617 }
16618
16619 static enum dwarf_array_dim_ordering
16620 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16621 {
16622 struct attribute *attr;
16623
16624 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16625
16626 if (attr)
16627 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16628
16629 /* GNU F77 is a special case, as at 08/2004 array type info is the
16630 opposite order to the dwarf2 specification, but data is still
16631 laid out as per normal fortran.
16632
16633 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16634 version checking. */
16635
16636 if (cu->language == language_fortran
16637 && cu->producer && strstr (cu->producer, "GNU F77"))
16638 {
16639 return DW_ORD_row_major;
16640 }
16641
16642 switch (cu->language_defn->la_array_ordering)
16643 {
16644 case array_column_major:
16645 return DW_ORD_col_major;
16646 case array_row_major:
16647 default:
16648 return DW_ORD_row_major;
16649 };
16650 }
16651
16652 /* Extract all information from a DW_TAG_set_type DIE and put it in
16653 the DIE's type field. */
16654
16655 static struct type *
16656 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16657 {
16658 struct type *domain_type, *set_type;
16659 struct attribute *attr;
16660
16661 domain_type = die_type (die, cu);
16662
16663 /* The die_type call above may have already set the type for this DIE. */
16664 set_type = get_die_type (die, cu);
16665 if (set_type)
16666 return set_type;
16667
16668 set_type = create_set_type (NULL, domain_type);
16669
16670 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16671 if (attr)
16672 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16673
16674 maybe_set_alignment (cu, die, set_type);
16675
16676 return set_die_type (die, set_type, cu);
16677 }
16678
16679 /* A helper for read_common_block that creates a locexpr baton.
16680 SYM is the symbol which we are marking as computed.
16681 COMMON_DIE is the DIE for the common block.
16682 COMMON_LOC is the location expression attribute for the common
16683 block itself.
16684 MEMBER_LOC is the location expression attribute for the particular
16685 member of the common block that we are processing.
16686 CU is the CU from which the above come. */
16687
16688 static void
16689 mark_common_block_symbol_computed (struct symbol *sym,
16690 struct die_info *common_die,
16691 struct attribute *common_loc,
16692 struct attribute *member_loc,
16693 struct dwarf2_cu *cu)
16694 {
16695 struct dwarf2_per_objfile *dwarf2_per_objfile
16696 = cu->per_cu->dwarf2_per_objfile;
16697 struct objfile *objfile = dwarf2_per_objfile->objfile;
16698 struct dwarf2_locexpr_baton *baton;
16699 gdb_byte *ptr;
16700 unsigned int cu_off;
16701 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16702 LONGEST offset = 0;
16703
16704 gdb_assert (common_loc && member_loc);
16705 gdb_assert (attr_form_is_block (common_loc));
16706 gdb_assert (attr_form_is_block (member_loc)
16707 || attr_form_is_constant (member_loc));
16708
16709 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16710 baton->per_cu = cu->per_cu;
16711 gdb_assert (baton->per_cu);
16712
16713 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16714
16715 if (attr_form_is_constant (member_loc))
16716 {
16717 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16718 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16719 }
16720 else
16721 baton->size += DW_BLOCK (member_loc)->size;
16722
16723 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16724 baton->data = ptr;
16725
16726 *ptr++ = DW_OP_call4;
16727 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16728 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16729 ptr += 4;
16730
16731 if (attr_form_is_constant (member_loc))
16732 {
16733 *ptr++ = DW_OP_addr;
16734 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16735 ptr += cu->header.addr_size;
16736 }
16737 else
16738 {
16739 /* We have to copy the data here, because DW_OP_call4 will only
16740 use a DW_AT_location attribute. */
16741 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16742 ptr += DW_BLOCK (member_loc)->size;
16743 }
16744
16745 *ptr++ = DW_OP_plus;
16746 gdb_assert (ptr - baton->data == baton->size);
16747
16748 SYMBOL_LOCATION_BATON (sym) = baton;
16749 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16750 }
16751
16752 /* Create appropriate locally-scoped variables for all the
16753 DW_TAG_common_block entries. Also create a struct common_block
16754 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16755 is used to sepate the common blocks name namespace from regular
16756 variable names. */
16757
16758 static void
16759 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16760 {
16761 struct attribute *attr;
16762
16763 attr = dwarf2_attr (die, DW_AT_location, cu);
16764 if (attr)
16765 {
16766 /* Support the .debug_loc offsets. */
16767 if (attr_form_is_block (attr))
16768 {
16769 /* Ok. */
16770 }
16771 else if (attr_form_is_section_offset (attr))
16772 {
16773 dwarf2_complex_location_expr_complaint ();
16774 attr = NULL;
16775 }
16776 else
16777 {
16778 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16779 "common block member");
16780 attr = NULL;
16781 }
16782 }
16783
16784 if (die->child != NULL)
16785 {
16786 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16787 struct die_info *child_die;
16788 size_t n_entries = 0, size;
16789 struct common_block *common_block;
16790 struct symbol *sym;
16791
16792 for (child_die = die->child;
16793 child_die && child_die->tag;
16794 child_die = sibling_die (child_die))
16795 ++n_entries;
16796
16797 size = (sizeof (struct common_block)
16798 + (n_entries - 1) * sizeof (struct symbol *));
16799 common_block
16800 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16801 size);
16802 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16803 common_block->n_entries = 0;
16804
16805 for (child_die = die->child;
16806 child_die && child_die->tag;
16807 child_die = sibling_die (child_die))
16808 {
16809 /* Create the symbol in the DW_TAG_common_block block in the current
16810 symbol scope. */
16811 sym = new_symbol (child_die, NULL, cu);
16812 if (sym != NULL)
16813 {
16814 struct attribute *member_loc;
16815
16816 common_block->contents[common_block->n_entries++] = sym;
16817
16818 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16819 cu);
16820 if (member_loc)
16821 {
16822 /* GDB has handled this for a long time, but it is
16823 not specified by DWARF. It seems to have been
16824 emitted by gfortran at least as recently as:
16825 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16826 complaint (_("Variable in common block has "
16827 "DW_AT_data_member_location "
16828 "- DIE at %s [in module %s]"),
16829 sect_offset_str (child_die->sect_off),
16830 objfile_name (objfile));
16831
16832 if (attr_form_is_section_offset (member_loc))
16833 dwarf2_complex_location_expr_complaint ();
16834 else if (attr_form_is_constant (member_loc)
16835 || attr_form_is_block (member_loc))
16836 {
16837 if (attr)
16838 mark_common_block_symbol_computed (sym, die, attr,
16839 member_loc, cu);
16840 }
16841 else
16842 dwarf2_complex_location_expr_complaint ();
16843 }
16844 }
16845 }
16846
16847 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16848 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16849 }
16850 }
16851
16852 /* Create a type for a C++ namespace. */
16853
16854 static struct type *
16855 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16856 {
16857 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16858 const char *previous_prefix, *name;
16859 int is_anonymous;
16860 struct type *type;
16861
16862 /* For extensions, reuse the type of the original namespace. */
16863 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16864 {
16865 struct die_info *ext_die;
16866 struct dwarf2_cu *ext_cu = cu;
16867
16868 ext_die = dwarf2_extension (die, &ext_cu);
16869 type = read_type_die (ext_die, ext_cu);
16870
16871 /* EXT_CU may not be the same as CU.
16872 Ensure TYPE is recorded with CU in die_type_hash. */
16873 return set_die_type (die, type, cu);
16874 }
16875
16876 name = namespace_name (die, &is_anonymous, cu);
16877
16878 /* Now build the name of the current namespace. */
16879
16880 previous_prefix = determine_prefix (die, cu);
16881 if (previous_prefix[0] != '\0')
16882 name = typename_concat (&objfile->objfile_obstack,
16883 previous_prefix, name, 0, cu);
16884
16885 /* Create the type. */
16886 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16887
16888 return set_die_type (die, type, cu);
16889 }
16890
16891 /* Read a namespace scope. */
16892
16893 static void
16894 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16895 {
16896 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16897 int is_anonymous;
16898
16899 /* Add a symbol associated to this if we haven't seen the namespace
16900 before. Also, add a using directive if it's an anonymous
16901 namespace. */
16902
16903 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16904 {
16905 struct type *type;
16906
16907 type = read_type_die (die, cu);
16908 new_symbol (die, type, cu);
16909
16910 namespace_name (die, &is_anonymous, cu);
16911 if (is_anonymous)
16912 {
16913 const char *previous_prefix = determine_prefix (die, cu);
16914
16915 std::vector<const char *> excludes;
16916 add_using_directive (using_directives (cu),
16917 previous_prefix, TYPE_NAME (type), NULL,
16918 NULL, excludes, 0, &objfile->objfile_obstack);
16919 }
16920 }
16921
16922 if (die->child != NULL)
16923 {
16924 struct die_info *child_die = die->child;
16925
16926 while (child_die && child_die->tag)
16927 {
16928 process_die (child_die, cu);
16929 child_die = sibling_die (child_die);
16930 }
16931 }
16932 }
16933
16934 /* Read a Fortran module as type. This DIE can be only a declaration used for
16935 imported module. Still we need that type as local Fortran "use ... only"
16936 declaration imports depend on the created type in determine_prefix. */
16937
16938 static struct type *
16939 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16940 {
16941 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16942 const char *module_name;
16943 struct type *type;
16944
16945 module_name = dwarf2_name (die, cu);
16946 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16947
16948 return set_die_type (die, type, cu);
16949 }
16950
16951 /* Read a Fortran module. */
16952
16953 static void
16954 read_module (struct die_info *die, struct dwarf2_cu *cu)
16955 {
16956 struct die_info *child_die = die->child;
16957 struct type *type;
16958
16959 type = read_type_die (die, cu);
16960 new_symbol (die, type, cu);
16961
16962 while (child_die && child_die->tag)
16963 {
16964 process_die (child_die, cu);
16965 child_die = sibling_die (child_die);
16966 }
16967 }
16968
16969 /* Return the name of the namespace represented by DIE. Set
16970 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16971 namespace. */
16972
16973 static const char *
16974 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16975 {
16976 struct die_info *current_die;
16977 const char *name = NULL;
16978
16979 /* Loop through the extensions until we find a name. */
16980
16981 for (current_die = die;
16982 current_die != NULL;
16983 current_die = dwarf2_extension (die, &cu))
16984 {
16985 /* We don't use dwarf2_name here so that we can detect the absence
16986 of a name -> anonymous namespace. */
16987 name = dwarf2_string_attr (die, DW_AT_name, cu);
16988
16989 if (name != NULL)
16990 break;
16991 }
16992
16993 /* Is it an anonymous namespace? */
16994
16995 *is_anonymous = (name == NULL);
16996 if (*is_anonymous)
16997 name = CP_ANONYMOUS_NAMESPACE_STR;
16998
16999 return name;
17000 }
17001
17002 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17003 the user defined type vector. */
17004
17005 static struct type *
17006 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17007 {
17008 struct gdbarch *gdbarch
17009 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
17010 struct comp_unit_head *cu_header = &cu->header;
17011 struct type *type;
17012 struct attribute *attr_byte_size;
17013 struct attribute *attr_address_class;
17014 int byte_size, addr_class;
17015 struct type *target_type;
17016
17017 target_type = die_type (die, cu);
17018
17019 /* The die_type call above may have already set the type for this DIE. */
17020 type = get_die_type (die, cu);
17021 if (type)
17022 return type;
17023
17024 type = lookup_pointer_type (target_type);
17025
17026 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17027 if (attr_byte_size)
17028 byte_size = DW_UNSND (attr_byte_size);
17029 else
17030 byte_size = cu_header->addr_size;
17031
17032 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17033 if (attr_address_class)
17034 addr_class = DW_UNSND (attr_address_class);
17035 else
17036 addr_class = DW_ADDR_none;
17037
17038 ULONGEST alignment = get_alignment (cu, die);
17039
17040 /* If the pointer size, alignment, or address class is different
17041 than the default, create a type variant marked as such and set
17042 the length accordingly. */
17043 if (TYPE_LENGTH (type) != byte_size
17044 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17045 && alignment != TYPE_RAW_ALIGN (type))
17046 || addr_class != DW_ADDR_none)
17047 {
17048 if (gdbarch_address_class_type_flags_p (gdbarch))
17049 {
17050 int type_flags;
17051
17052 type_flags = gdbarch_address_class_type_flags
17053 (gdbarch, byte_size, addr_class);
17054 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17055 == 0);
17056 type = make_type_with_address_space (type, type_flags);
17057 }
17058 else if (TYPE_LENGTH (type) != byte_size)
17059 {
17060 complaint (_("invalid pointer size %d"), byte_size);
17061 }
17062 else if (TYPE_RAW_ALIGN (type) != alignment)
17063 {
17064 complaint (_("Invalid DW_AT_alignment"
17065 " - DIE at %s [in module %s]"),
17066 sect_offset_str (die->sect_off),
17067 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17068 }
17069 else
17070 {
17071 /* Should we also complain about unhandled address classes? */
17072 }
17073 }
17074
17075 TYPE_LENGTH (type) = byte_size;
17076 set_type_align (type, alignment);
17077 return set_die_type (die, type, cu);
17078 }
17079
17080 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17081 the user defined type vector. */
17082
17083 static struct type *
17084 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17085 {
17086 struct type *type;
17087 struct type *to_type;
17088 struct type *domain;
17089
17090 to_type = die_type (die, cu);
17091 domain = die_containing_type (die, cu);
17092
17093 /* The calls above may have already set the type for this DIE. */
17094 type = get_die_type (die, cu);
17095 if (type)
17096 return type;
17097
17098 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17099 type = lookup_methodptr_type (to_type);
17100 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17101 {
17102 struct type *new_type
17103 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17104
17105 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17106 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17107 TYPE_VARARGS (to_type));
17108 type = lookup_methodptr_type (new_type);
17109 }
17110 else
17111 type = lookup_memberptr_type (to_type, domain);
17112
17113 return set_die_type (die, type, cu);
17114 }
17115
17116 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17117 the user defined type vector. */
17118
17119 static struct type *
17120 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17121 enum type_code refcode)
17122 {
17123 struct comp_unit_head *cu_header = &cu->header;
17124 struct type *type, *target_type;
17125 struct attribute *attr;
17126
17127 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17128
17129 target_type = die_type (die, cu);
17130
17131 /* The die_type call above may have already set the type for this DIE. */
17132 type = get_die_type (die, cu);
17133 if (type)
17134 return type;
17135
17136 type = lookup_reference_type (target_type, refcode);
17137 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17138 if (attr)
17139 {
17140 TYPE_LENGTH (type) = DW_UNSND (attr);
17141 }
17142 else
17143 {
17144 TYPE_LENGTH (type) = cu_header->addr_size;
17145 }
17146 maybe_set_alignment (cu, die, type);
17147 return set_die_type (die, type, cu);
17148 }
17149
17150 /* Add the given cv-qualifiers to the element type of the array. GCC
17151 outputs DWARF type qualifiers that apply to an array, not the
17152 element type. But GDB relies on the array element type to carry
17153 the cv-qualifiers. This mimics section 6.7.3 of the C99
17154 specification. */
17155
17156 static struct type *
17157 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17158 struct type *base_type, int cnst, int voltl)
17159 {
17160 struct type *el_type, *inner_array;
17161
17162 base_type = copy_type (base_type);
17163 inner_array = base_type;
17164
17165 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17166 {
17167 TYPE_TARGET_TYPE (inner_array) =
17168 copy_type (TYPE_TARGET_TYPE (inner_array));
17169 inner_array = TYPE_TARGET_TYPE (inner_array);
17170 }
17171
17172 el_type = TYPE_TARGET_TYPE (inner_array);
17173 cnst |= TYPE_CONST (el_type);
17174 voltl |= TYPE_VOLATILE (el_type);
17175 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17176
17177 return set_die_type (die, base_type, cu);
17178 }
17179
17180 static struct type *
17181 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17182 {
17183 struct type *base_type, *cv_type;
17184
17185 base_type = die_type (die, cu);
17186
17187 /* The die_type call above may have already set the type for this DIE. */
17188 cv_type = get_die_type (die, cu);
17189 if (cv_type)
17190 return cv_type;
17191
17192 /* In case the const qualifier is applied to an array type, the element type
17193 is so qualified, not the array type (section 6.7.3 of C99). */
17194 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17195 return add_array_cv_type (die, cu, base_type, 1, 0);
17196
17197 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17198 return set_die_type (die, cv_type, cu);
17199 }
17200
17201 static struct type *
17202 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17203 {
17204 struct type *base_type, *cv_type;
17205
17206 base_type = die_type (die, cu);
17207
17208 /* The die_type call above may have already set the type for this DIE. */
17209 cv_type = get_die_type (die, cu);
17210 if (cv_type)
17211 return cv_type;
17212
17213 /* In case the volatile qualifier is applied to an array type, the
17214 element type is so qualified, not the array type (section 6.7.3
17215 of C99). */
17216 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17217 return add_array_cv_type (die, cu, base_type, 0, 1);
17218
17219 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17220 return set_die_type (die, cv_type, cu);
17221 }
17222
17223 /* Handle DW_TAG_restrict_type. */
17224
17225 static struct type *
17226 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17227 {
17228 struct type *base_type, *cv_type;
17229
17230 base_type = die_type (die, cu);
17231
17232 /* The die_type call above may have already set the type for this DIE. */
17233 cv_type = get_die_type (die, cu);
17234 if (cv_type)
17235 return cv_type;
17236
17237 cv_type = make_restrict_type (base_type);
17238 return set_die_type (die, cv_type, cu);
17239 }
17240
17241 /* Handle DW_TAG_atomic_type. */
17242
17243 static struct type *
17244 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17245 {
17246 struct type *base_type, *cv_type;
17247
17248 base_type = die_type (die, cu);
17249
17250 /* The die_type call above may have already set the type for this DIE. */
17251 cv_type = get_die_type (die, cu);
17252 if (cv_type)
17253 return cv_type;
17254
17255 cv_type = make_atomic_type (base_type);
17256 return set_die_type (die, cv_type, cu);
17257 }
17258
17259 /* Extract all information from a DW_TAG_string_type DIE and add to
17260 the user defined type vector. It isn't really a user defined type,
17261 but it behaves like one, with other DIE's using an AT_user_def_type
17262 attribute to reference it. */
17263
17264 static struct type *
17265 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17266 {
17267 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17268 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17269 struct type *type, *range_type, *index_type, *char_type;
17270 struct attribute *attr;
17271 unsigned int length;
17272
17273 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17274 if (attr)
17275 {
17276 length = DW_UNSND (attr);
17277 }
17278 else
17279 {
17280 /* Check for the DW_AT_byte_size attribute. */
17281 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17282 if (attr)
17283 {
17284 length = DW_UNSND (attr);
17285 }
17286 else
17287 {
17288 length = 1;
17289 }
17290 }
17291
17292 index_type = objfile_type (objfile)->builtin_int;
17293 range_type = create_static_range_type (NULL, index_type, 1, length);
17294 char_type = language_string_char_type (cu->language_defn, gdbarch);
17295 type = create_string_type (NULL, char_type, range_type);
17296
17297 return set_die_type (die, type, cu);
17298 }
17299
17300 /* Assuming that DIE corresponds to a function, returns nonzero
17301 if the function is prototyped. */
17302
17303 static int
17304 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17305 {
17306 struct attribute *attr;
17307
17308 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17309 if (attr && (DW_UNSND (attr) != 0))
17310 return 1;
17311
17312 /* The DWARF standard implies that the DW_AT_prototyped attribute
17313 is only meaninful for C, but the concept also extends to other
17314 languages that allow unprototyped functions (Eg: Objective C).
17315 For all other languages, assume that functions are always
17316 prototyped. */
17317 if (cu->language != language_c
17318 && cu->language != language_objc
17319 && cu->language != language_opencl)
17320 return 1;
17321
17322 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17323 prototyped and unprototyped functions; default to prototyped,
17324 since that is more common in modern code (and RealView warns
17325 about unprototyped functions). */
17326 if (producer_is_realview (cu->producer))
17327 return 1;
17328
17329 return 0;
17330 }
17331
17332 /* Handle DIES due to C code like:
17333
17334 struct foo
17335 {
17336 int (*funcp)(int a, long l);
17337 int b;
17338 };
17339
17340 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17341
17342 static struct type *
17343 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17344 {
17345 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17346 struct type *type; /* Type that this function returns. */
17347 struct type *ftype; /* Function that returns above type. */
17348 struct attribute *attr;
17349
17350 type = die_type (die, cu);
17351
17352 /* The die_type call above may have already set the type for this DIE. */
17353 ftype = get_die_type (die, cu);
17354 if (ftype)
17355 return ftype;
17356
17357 ftype = lookup_function_type (type);
17358
17359 if (prototyped_function_p (die, cu))
17360 TYPE_PROTOTYPED (ftype) = 1;
17361
17362 /* Store the calling convention in the type if it's available in
17363 the subroutine die. Otherwise set the calling convention to
17364 the default value DW_CC_normal. */
17365 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17366 if (attr)
17367 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17368 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17369 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17370 else
17371 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17372
17373 /* Record whether the function returns normally to its caller or not
17374 if the DWARF producer set that information. */
17375 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17376 if (attr && (DW_UNSND (attr) != 0))
17377 TYPE_NO_RETURN (ftype) = 1;
17378
17379 /* We need to add the subroutine type to the die immediately so
17380 we don't infinitely recurse when dealing with parameters
17381 declared as the same subroutine type. */
17382 set_die_type (die, ftype, cu);
17383
17384 if (die->child != NULL)
17385 {
17386 struct type *void_type = objfile_type (objfile)->builtin_void;
17387 struct die_info *child_die;
17388 int nparams, iparams;
17389
17390 /* Count the number of parameters.
17391 FIXME: GDB currently ignores vararg functions, but knows about
17392 vararg member functions. */
17393 nparams = 0;
17394 child_die = die->child;
17395 while (child_die && child_die->tag)
17396 {
17397 if (child_die->tag == DW_TAG_formal_parameter)
17398 nparams++;
17399 else if (child_die->tag == DW_TAG_unspecified_parameters)
17400 TYPE_VARARGS (ftype) = 1;
17401 child_die = sibling_die (child_die);
17402 }
17403
17404 /* Allocate storage for parameters and fill them in. */
17405 TYPE_NFIELDS (ftype) = nparams;
17406 TYPE_FIELDS (ftype) = (struct field *)
17407 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17408
17409 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17410 even if we error out during the parameters reading below. */
17411 for (iparams = 0; iparams < nparams; iparams++)
17412 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17413
17414 iparams = 0;
17415 child_die = die->child;
17416 while (child_die && child_die->tag)
17417 {
17418 if (child_die->tag == DW_TAG_formal_parameter)
17419 {
17420 struct type *arg_type;
17421
17422 /* DWARF version 2 has no clean way to discern C++
17423 static and non-static member functions. G++ helps
17424 GDB by marking the first parameter for non-static
17425 member functions (which is the this pointer) as
17426 artificial. We pass this information to
17427 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17428
17429 DWARF version 3 added DW_AT_object_pointer, which GCC
17430 4.5 does not yet generate. */
17431 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17432 if (attr)
17433 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17434 else
17435 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17436 arg_type = die_type (child_die, cu);
17437
17438 /* RealView does not mark THIS as const, which the testsuite
17439 expects. GCC marks THIS as const in method definitions,
17440 but not in the class specifications (GCC PR 43053). */
17441 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17442 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17443 {
17444 int is_this = 0;
17445 struct dwarf2_cu *arg_cu = cu;
17446 const char *name = dwarf2_name (child_die, cu);
17447
17448 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17449 if (attr)
17450 {
17451 /* If the compiler emits this, use it. */
17452 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17453 is_this = 1;
17454 }
17455 else if (name && strcmp (name, "this") == 0)
17456 /* Function definitions will have the argument names. */
17457 is_this = 1;
17458 else if (name == NULL && iparams == 0)
17459 /* Declarations may not have the names, so like
17460 elsewhere in GDB, assume an artificial first
17461 argument is "this". */
17462 is_this = 1;
17463
17464 if (is_this)
17465 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17466 arg_type, 0);
17467 }
17468
17469 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17470 iparams++;
17471 }
17472 child_die = sibling_die (child_die);
17473 }
17474 }
17475
17476 return ftype;
17477 }
17478
17479 static struct type *
17480 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17481 {
17482 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17483 const char *name = NULL;
17484 struct type *this_type, *target_type;
17485
17486 name = dwarf2_full_name (NULL, die, cu);
17487 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17488 TYPE_TARGET_STUB (this_type) = 1;
17489 set_die_type (die, this_type, cu);
17490 target_type = die_type (die, cu);
17491 if (target_type != this_type)
17492 TYPE_TARGET_TYPE (this_type) = target_type;
17493 else
17494 {
17495 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17496 spec and cause infinite loops in GDB. */
17497 complaint (_("Self-referential DW_TAG_typedef "
17498 "- DIE at %s [in module %s]"),
17499 sect_offset_str (die->sect_off), objfile_name (objfile));
17500 TYPE_TARGET_TYPE (this_type) = NULL;
17501 }
17502 return this_type;
17503 }
17504
17505 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17506 (which may be different from NAME) to the architecture back-end to allow
17507 it to guess the correct format if necessary. */
17508
17509 static struct type *
17510 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17511 const char *name_hint)
17512 {
17513 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17514 const struct floatformat **format;
17515 struct type *type;
17516
17517 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17518 if (format)
17519 type = init_float_type (objfile, bits, name, format);
17520 else
17521 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17522
17523 return type;
17524 }
17525
17526 /* Allocate an integer type of size BITS and name NAME. */
17527
17528 static struct type *
17529 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17530 int bits, int unsigned_p, const char *name)
17531 {
17532 struct type *type;
17533
17534 /* Versions of Intel's C Compiler generate an integer type called "void"
17535 instead of using DW_TAG_unspecified_type. This has been seen on
17536 at least versions 14, 17, and 18. */
17537 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17538 && strcmp (name, "void") == 0)
17539 type = objfile_type (objfile)->builtin_void;
17540 else
17541 type = init_integer_type (objfile, bits, unsigned_p, name);
17542
17543 return type;
17544 }
17545
17546 /* Initialise and return a floating point type of size BITS suitable for
17547 use as a component of a complex number. The NAME_HINT is passed through
17548 when initialising the floating point type and is the name of the complex
17549 type.
17550
17551 As DWARF doesn't currently provide an explicit name for the components
17552 of a complex number, but it can be helpful to have these components
17553 named, we try to select a suitable name based on the size of the
17554 component. */
17555 static struct type *
17556 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17557 struct objfile *objfile,
17558 int bits, const char *name_hint)
17559 {
17560 gdbarch *gdbarch = get_objfile_arch (objfile);
17561 struct type *tt = nullptr;
17562
17563 /* Try to find a suitable floating point builtin type of size BITS.
17564 We're going to use the name of this type as the name for the complex
17565 target type that we are about to create. */
17566 switch (cu->language)
17567 {
17568 case language_fortran:
17569 switch (bits)
17570 {
17571 case 32:
17572 tt = builtin_f_type (gdbarch)->builtin_real;
17573 break;
17574 case 64:
17575 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17576 break;
17577 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17578 case 128:
17579 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17580 break;
17581 }
17582 break;
17583 default:
17584 switch (bits)
17585 {
17586 case 32:
17587 tt = builtin_type (gdbarch)->builtin_float;
17588 break;
17589 case 64:
17590 tt = builtin_type (gdbarch)->builtin_double;
17591 break;
17592 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17593 case 128:
17594 tt = builtin_type (gdbarch)->builtin_long_double;
17595 break;
17596 }
17597 break;
17598 }
17599
17600 /* If the type we found doesn't match the size we were looking for, then
17601 pretend we didn't find a type at all, the complex target type we
17602 create will then be nameless. */
17603 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17604 tt = nullptr;
17605
17606 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17607 return dwarf2_init_float_type (objfile, bits, name, name_hint);
17608 }
17609
17610 /* Find a representation of a given base type and install
17611 it in the TYPE field of the die. */
17612
17613 static struct type *
17614 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17615 {
17616 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17617 struct type *type;
17618 struct attribute *attr;
17619 int encoding = 0, bits = 0;
17620 const char *name;
17621
17622 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17623 if (attr)
17624 {
17625 encoding = DW_UNSND (attr);
17626 }
17627 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17628 if (attr)
17629 {
17630 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17631 }
17632 name = dwarf2_name (die, cu);
17633 if (!name)
17634 {
17635 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17636 }
17637
17638 switch (encoding)
17639 {
17640 case DW_ATE_address:
17641 /* Turn DW_ATE_address into a void * pointer. */
17642 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17643 type = init_pointer_type (objfile, bits, name, type);
17644 break;
17645 case DW_ATE_boolean:
17646 type = init_boolean_type (objfile, bits, 1, name);
17647 break;
17648 case DW_ATE_complex_float:
17649 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name);
17650 type = init_complex_type (objfile, name, type);
17651 break;
17652 case DW_ATE_decimal_float:
17653 type = init_decfloat_type (objfile, bits, name);
17654 break;
17655 case DW_ATE_float:
17656 type = dwarf2_init_float_type (objfile, bits, name, name);
17657 break;
17658 case DW_ATE_signed:
17659 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17660 break;
17661 case DW_ATE_unsigned:
17662 if (cu->language == language_fortran
17663 && name
17664 && startswith (name, "character("))
17665 type = init_character_type (objfile, bits, 1, name);
17666 else
17667 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17668 break;
17669 case DW_ATE_signed_char:
17670 if (cu->language == language_ada || cu->language == language_m2
17671 || cu->language == language_pascal
17672 || cu->language == language_fortran)
17673 type = init_character_type (objfile, bits, 0, name);
17674 else
17675 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17676 break;
17677 case DW_ATE_unsigned_char:
17678 if (cu->language == language_ada || cu->language == language_m2
17679 || cu->language == language_pascal
17680 || cu->language == language_fortran
17681 || cu->language == language_rust)
17682 type = init_character_type (objfile, bits, 1, name);
17683 else
17684 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17685 break;
17686 case DW_ATE_UTF:
17687 {
17688 gdbarch *arch = get_objfile_arch (objfile);
17689
17690 if (bits == 16)
17691 type = builtin_type (arch)->builtin_char16;
17692 else if (bits == 32)
17693 type = builtin_type (arch)->builtin_char32;
17694 else
17695 {
17696 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17697 bits);
17698 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17699 }
17700 return set_die_type (die, type, cu);
17701 }
17702 break;
17703
17704 default:
17705 complaint (_("unsupported DW_AT_encoding: '%s'"),
17706 dwarf_type_encoding_name (encoding));
17707 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17708 break;
17709 }
17710
17711 if (name && strcmp (name, "char") == 0)
17712 TYPE_NOSIGN (type) = 1;
17713
17714 maybe_set_alignment (cu, die, type);
17715
17716 return set_die_type (die, type, cu);
17717 }
17718
17719 /* Parse dwarf attribute if it's a block, reference or constant and put the
17720 resulting value of the attribute into struct bound_prop.
17721 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17722
17723 static int
17724 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17725 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17726 struct type *default_type)
17727 {
17728 struct dwarf2_property_baton *baton;
17729 struct obstack *obstack
17730 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17731
17732 gdb_assert (default_type != NULL);
17733
17734 if (attr == NULL || prop == NULL)
17735 return 0;
17736
17737 if (attr_form_is_block (attr))
17738 {
17739 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17740 baton->property_type = default_type;
17741 baton->locexpr.per_cu = cu->per_cu;
17742 baton->locexpr.size = DW_BLOCK (attr)->size;
17743 baton->locexpr.data = DW_BLOCK (attr)->data;
17744 baton->locexpr.is_reference = false;
17745 prop->data.baton = baton;
17746 prop->kind = PROP_LOCEXPR;
17747 gdb_assert (prop->data.baton != NULL);
17748 }
17749 else if (attr_form_is_ref (attr))
17750 {
17751 struct dwarf2_cu *target_cu = cu;
17752 struct die_info *target_die;
17753 struct attribute *target_attr;
17754
17755 target_die = follow_die_ref (die, attr, &target_cu);
17756 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17757 if (target_attr == NULL)
17758 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17759 target_cu);
17760 if (target_attr == NULL)
17761 return 0;
17762
17763 switch (target_attr->name)
17764 {
17765 case DW_AT_location:
17766 if (attr_form_is_section_offset (target_attr))
17767 {
17768 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17769 baton->property_type = die_type (target_die, target_cu);
17770 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17771 prop->data.baton = baton;
17772 prop->kind = PROP_LOCLIST;
17773 gdb_assert (prop->data.baton != NULL);
17774 }
17775 else if (attr_form_is_block (target_attr))
17776 {
17777 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17778 baton->property_type = die_type (target_die, target_cu);
17779 baton->locexpr.per_cu = cu->per_cu;
17780 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17781 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17782 baton->locexpr.is_reference = true;
17783 prop->data.baton = baton;
17784 prop->kind = PROP_LOCEXPR;
17785 gdb_assert (prop->data.baton != NULL);
17786 }
17787 else
17788 {
17789 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17790 "dynamic property");
17791 return 0;
17792 }
17793 break;
17794 case DW_AT_data_member_location:
17795 {
17796 LONGEST offset;
17797
17798 if (!handle_data_member_location (target_die, target_cu,
17799 &offset))
17800 return 0;
17801
17802 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17803 baton->property_type = read_type_die (target_die->parent,
17804 target_cu);
17805 baton->offset_info.offset = offset;
17806 baton->offset_info.type = die_type (target_die, target_cu);
17807 prop->data.baton = baton;
17808 prop->kind = PROP_ADDR_OFFSET;
17809 break;
17810 }
17811 }
17812 }
17813 else if (attr_form_is_constant (attr))
17814 {
17815 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17816 prop->kind = PROP_CONST;
17817 }
17818 else
17819 {
17820 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17821 dwarf2_name (die, cu));
17822 return 0;
17823 }
17824
17825 return 1;
17826 }
17827
17828 /* Find an integer type the same size as the address size given in the
17829 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
17830 is unsigned or not. */
17831
17832 static struct type *
17833 dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
17834 bool unsigned_p)
17835 {
17836 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
17837 int addr_size = dwarf2_per_cu_addr_size (per_cu);
17838 struct type *int_type;
17839
17840 /* Helper macro to examine the various builtin types. */
17841 #define TRY_TYPE(F) \
17842 int_type = (unsigned_p \
17843 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17844 : objfile_type (objfile)->builtin_ ## F); \
17845 if (int_type != NULL && TYPE_LENGTH (int_type) == addr_size) \
17846 return int_type
17847
17848 TRY_TYPE (char);
17849 TRY_TYPE (short);
17850 TRY_TYPE (int);
17851 TRY_TYPE (long);
17852 TRY_TYPE (long_long);
17853
17854 #undef TRY_TYPE
17855
17856 gdb_assert_not_reached ("unable to find suitable integer type");
17857 }
17858
17859 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17860 present (which is valid) then compute the default type based on the
17861 compilation units address size. */
17862
17863 static struct type *
17864 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17865 {
17866 struct type *index_type = die_type (die, cu);
17867
17868 /* Dwarf-2 specifications explicitly allows to create subrange types
17869 without specifying a base type.
17870 In that case, the base type must be set to the type of
17871 the lower bound, upper bound or count, in that order, if any of these
17872 three attributes references an object that has a type.
17873 If no base type is found, the Dwarf-2 specifications say that
17874 a signed integer type of size equal to the size of an address should
17875 be used.
17876 For the following C code: `extern char gdb_int [];'
17877 GCC produces an empty range DIE.
17878 FIXME: muller/2010-05-28: Possible references to object for low bound,
17879 high bound or count are not yet handled by this code. */
17880 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17881 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17882
17883 return index_type;
17884 }
17885
17886 /* Read the given DW_AT_subrange DIE. */
17887
17888 static struct type *
17889 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17890 {
17891 struct type *base_type, *orig_base_type;
17892 struct type *range_type;
17893 struct attribute *attr;
17894 struct dynamic_prop low, high;
17895 int low_default_is_valid;
17896 int high_bound_is_count = 0;
17897 const char *name;
17898 ULONGEST negative_mask;
17899
17900 orig_base_type = read_subrange_index_type (die, cu);
17901
17902 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17903 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17904 creating the range type, but we use the result of check_typedef
17905 when examining properties of the type. */
17906 base_type = check_typedef (orig_base_type);
17907
17908 /* The die_type call above may have already set the type for this DIE. */
17909 range_type = get_die_type (die, cu);
17910 if (range_type)
17911 return range_type;
17912
17913 low.kind = PROP_CONST;
17914 high.kind = PROP_CONST;
17915 high.data.const_val = 0;
17916
17917 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17918 omitting DW_AT_lower_bound. */
17919 switch (cu->language)
17920 {
17921 case language_c:
17922 case language_cplus:
17923 low.data.const_val = 0;
17924 low_default_is_valid = 1;
17925 break;
17926 case language_fortran:
17927 low.data.const_val = 1;
17928 low_default_is_valid = 1;
17929 break;
17930 case language_d:
17931 case language_objc:
17932 case language_rust:
17933 low.data.const_val = 0;
17934 low_default_is_valid = (cu->header.version >= 4);
17935 break;
17936 case language_ada:
17937 case language_m2:
17938 case language_pascal:
17939 low.data.const_val = 1;
17940 low_default_is_valid = (cu->header.version >= 4);
17941 break;
17942 default:
17943 low.data.const_val = 0;
17944 low_default_is_valid = 0;
17945 break;
17946 }
17947
17948 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17949 if (attr)
17950 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17951 else if (!low_default_is_valid)
17952 complaint (_("Missing DW_AT_lower_bound "
17953 "- DIE at %s [in module %s]"),
17954 sect_offset_str (die->sect_off),
17955 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17956
17957 struct attribute *attr_ub, *attr_count;
17958 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17959 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17960 {
17961 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17962 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17963 {
17964 /* If bounds are constant do the final calculation here. */
17965 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17966 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17967 else
17968 high_bound_is_count = 1;
17969 }
17970 else
17971 {
17972 if (attr_ub != NULL)
17973 complaint (_("Unresolved DW_AT_upper_bound "
17974 "- DIE at %s [in module %s]"),
17975 sect_offset_str (die->sect_off),
17976 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17977 if (attr_count != NULL)
17978 complaint (_("Unresolved DW_AT_count "
17979 "- DIE at %s [in module %s]"),
17980 sect_offset_str (die->sect_off),
17981 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17982 }
17983 }
17984
17985 LONGEST bias = 0;
17986 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17987 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
17988 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17989
17990 /* Normally, the DWARF producers are expected to use a signed
17991 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17992 But this is unfortunately not always the case, as witnessed
17993 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17994 is used instead. To work around that ambiguity, we treat
17995 the bounds as signed, and thus sign-extend their values, when
17996 the base type is signed. */
17997 negative_mask =
17998 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17999 if (low.kind == PROP_CONST
18000 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18001 low.data.const_val |= negative_mask;
18002 if (high.kind == PROP_CONST
18003 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18004 high.data.const_val |= negative_mask;
18005
18006 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18007
18008 if (high_bound_is_count)
18009 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18010
18011 /* Ada expects an empty array on no boundary attributes. */
18012 if (attr == NULL && cu->language != language_ada)
18013 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
18014
18015 name = dwarf2_name (die, cu);
18016 if (name)
18017 TYPE_NAME (range_type) = name;
18018
18019 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18020 if (attr)
18021 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18022
18023 maybe_set_alignment (cu, die, range_type);
18024
18025 set_die_type (die, range_type, cu);
18026
18027 /* set_die_type should be already done. */
18028 set_descriptive_type (range_type, die, cu);
18029
18030 return range_type;
18031 }
18032
18033 static struct type *
18034 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18035 {
18036 struct type *type;
18037
18038 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18039 NULL);
18040 TYPE_NAME (type) = dwarf2_name (die, cu);
18041
18042 /* In Ada, an unspecified type is typically used when the description
18043 of the type is defered to a different unit. When encountering
18044 such a type, we treat it as a stub, and try to resolve it later on,
18045 when needed. */
18046 if (cu->language == language_ada)
18047 TYPE_STUB (type) = 1;
18048
18049 return set_die_type (die, type, cu);
18050 }
18051
18052 /* Read a single die and all its descendents. Set the die's sibling
18053 field to NULL; set other fields in the die correctly, and set all
18054 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18055 location of the info_ptr after reading all of those dies. PARENT
18056 is the parent of the die in question. */
18057
18058 static struct die_info *
18059 read_die_and_children (const struct die_reader_specs *reader,
18060 const gdb_byte *info_ptr,
18061 const gdb_byte **new_info_ptr,
18062 struct die_info *parent)
18063 {
18064 struct die_info *die;
18065 const gdb_byte *cur_ptr;
18066 int has_children;
18067
18068 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18069 if (die == NULL)
18070 {
18071 *new_info_ptr = cur_ptr;
18072 return NULL;
18073 }
18074 store_in_ref_table (die, reader->cu);
18075
18076 if (has_children)
18077 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18078 else
18079 {
18080 die->child = NULL;
18081 *new_info_ptr = cur_ptr;
18082 }
18083
18084 die->sibling = NULL;
18085 die->parent = parent;
18086 return die;
18087 }
18088
18089 /* Read a die, all of its descendents, and all of its siblings; set
18090 all of the fields of all of the dies correctly. Arguments are as
18091 in read_die_and_children. */
18092
18093 static struct die_info *
18094 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18095 const gdb_byte *info_ptr,
18096 const gdb_byte **new_info_ptr,
18097 struct die_info *parent)
18098 {
18099 struct die_info *first_die, *last_sibling;
18100 const gdb_byte *cur_ptr;
18101
18102 cur_ptr = info_ptr;
18103 first_die = last_sibling = NULL;
18104
18105 while (1)
18106 {
18107 struct die_info *die
18108 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18109
18110 if (die == NULL)
18111 {
18112 *new_info_ptr = cur_ptr;
18113 return first_die;
18114 }
18115
18116 if (!first_die)
18117 first_die = die;
18118 else
18119 last_sibling->sibling = die;
18120
18121 last_sibling = die;
18122 }
18123 }
18124
18125 /* Read a die, all of its descendents, and all of its siblings; set
18126 all of the fields of all of the dies correctly. Arguments are as
18127 in read_die_and_children.
18128 This the main entry point for reading a DIE and all its children. */
18129
18130 static struct die_info *
18131 read_die_and_siblings (const struct die_reader_specs *reader,
18132 const gdb_byte *info_ptr,
18133 const gdb_byte **new_info_ptr,
18134 struct die_info *parent)
18135 {
18136 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18137 new_info_ptr, parent);
18138
18139 if (dwarf_die_debug)
18140 {
18141 fprintf_unfiltered (gdb_stdlog,
18142 "Read die from %s@0x%x of %s:\n",
18143 get_section_name (reader->die_section),
18144 (unsigned) (info_ptr - reader->die_section->buffer),
18145 bfd_get_filename (reader->abfd));
18146 dump_die (die, dwarf_die_debug);
18147 }
18148
18149 return die;
18150 }
18151
18152 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18153 attributes.
18154 The caller is responsible for filling in the extra attributes
18155 and updating (*DIEP)->num_attrs.
18156 Set DIEP to point to a newly allocated die with its information,
18157 except for its child, sibling, and parent fields.
18158 Set HAS_CHILDREN to tell whether the die has children or not. */
18159
18160 static const gdb_byte *
18161 read_full_die_1 (const struct die_reader_specs *reader,
18162 struct die_info **diep, const gdb_byte *info_ptr,
18163 int *has_children, int num_extra_attrs)
18164 {
18165 unsigned int abbrev_number, bytes_read, i;
18166 struct abbrev_info *abbrev;
18167 struct die_info *die;
18168 struct dwarf2_cu *cu = reader->cu;
18169 bfd *abfd = reader->abfd;
18170
18171 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18172 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18173 info_ptr += bytes_read;
18174 if (!abbrev_number)
18175 {
18176 *diep = NULL;
18177 *has_children = 0;
18178 return info_ptr;
18179 }
18180
18181 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18182 if (!abbrev)
18183 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18184 abbrev_number,
18185 bfd_get_filename (abfd));
18186
18187 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18188 die->sect_off = sect_off;
18189 die->tag = abbrev->tag;
18190 die->abbrev = abbrev_number;
18191
18192 /* Make the result usable.
18193 The caller needs to update num_attrs after adding the extra
18194 attributes. */
18195 die->num_attrs = abbrev->num_attrs;
18196
18197 for (i = 0; i < abbrev->num_attrs; ++i)
18198 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18199 info_ptr);
18200
18201 *diep = die;
18202 *has_children = abbrev->has_children;
18203 return info_ptr;
18204 }
18205
18206 /* Read a die and all its attributes.
18207 Set DIEP to point to a newly allocated die with its information,
18208 except for its child, sibling, and parent fields.
18209 Set HAS_CHILDREN to tell whether the die has children or not. */
18210
18211 static const gdb_byte *
18212 read_full_die (const struct die_reader_specs *reader,
18213 struct die_info **diep, const gdb_byte *info_ptr,
18214 int *has_children)
18215 {
18216 const gdb_byte *result;
18217
18218 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18219
18220 if (dwarf_die_debug)
18221 {
18222 fprintf_unfiltered (gdb_stdlog,
18223 "Read die from %s@0x%x of %s:\n",
18224 get_section_name (reader->die_section),
18225 (unsigned) (info_ptr - reader->die_section->buffer),
18226 bfd_get_filename (reader->abfd));
18227 dump_die (*diep, dwarf_die_debug);
18228 }
18229
18230 return result;
18231 }
18232 \f
18233 /* Abbreviation tables.
18234
18235 In DWARF version 2, the description of the debugging information is
18236 stored in a separate .debug_abbrev section. Before we read any
18237 dies from a section we read in all abbreviations and install them
18238 in a hash table. */
18239
18240 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18241
18242 struct abbrev_info *
18243 abbrev_table::alloc_abbrev ()
18244 {
18245 struct abbrev_info *abbrev;
18246
18247 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18248 memset (abbrev, 0, sizeof (struct abbrev_info));
18249
18250 return abbrev;
18251 }
18252
18253 /* Add an abbreviation to the table. */
18254
18255 void
18256 abbrev_table::add_abbrev (unsigned int abbrev_number,
18257 struct abbrev_info *abbrev)
18258 {
18259 unsigned int hash_number;
18260
18261 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18262 abbrev->next = m_abbrevs[hash_number];
18263 m_abbrevs[hash_number] = abbrev;
18264 }
18265
18266 /* Look up an abbrev in the table.
18267 Returns NULL if the abbrev is not found. */
18268
18269 struct abbrev_info *
18270 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18271 {
18272 unsigned int hash_number;
18273 struct abbrev_info *abbrev;
18274
18275 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18276 abbrev = m_abbrevs[hash_number];
18277
18278 while (abbrev)
18279 {
18280 if (abbrev->number == abbrev_number)
18281 return abbrev;
18282 abbrev = abbrev->next;
18283 }
18284 return NULL;
18285 }
18286
18287 /* Read in an abbrev table. */
18288
18289 static abbrev_table_up
18290 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18291 struct dwarf2_section_info *section,
18292 sect_offset sect_off)
18293 {
18294 struct objfile *objfile = dwarf2_per_objfile->objfile;
18295 bfd *abfd = get_section_bfd_owner (section);
18296 const gdb_byte *abbrev_ptr;
18297 struct abbrev_info *cur_abbrev;
18298 unsigned int abbrev_number, bytes_read, abbrev_name;
18299 unsigned int abbrev_form;
18300 struct attr_abbrev *cur_attrs;
18301 unsigned int allocated_attrs;
18302
18303 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18304
18305 dwarf2_read_section (objfile, section);
18306 abbrev_ptr = section->buffer + to_underlying (sect_off);
18307 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18308 abbrev_ptr += bytes_read;
18309
18310 allocated_attrs = ATTR_ALLOC_CHUNK;
18311 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18312
18313 /* Loop until we reach an abbrev number of 0. */
18314 while (abbrev_number)
18315 {
18316 cur_abbrev = abbrev_table->alloc_abbrev ();
18317
18318 /* read in abbrev header */
18319 cur_abbrev->number = abbrev_number;
18320 cur_abbrev->tag
18321 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18322 abbrev_ptr += bytes_read;
18323 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18324 abbrev_ptr += 1;
18325
18326 /* now read in declarations */
18327 for (;;)
18328 {
18329 LONGEST implicit_const;
18330
18331 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18332 abbrev_ptr += bytes_read;
18333 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18334 abbrev_ptr += bytes_read;
18335 if (abbrev_form == DW_FORM_implicit_const)
18336 {
18337 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18338 &bytes_read);
18339 abbrev_ptr += bytes_read;
18340 }
18341 else
18342 {
18343 /* Initialize it due to a false compiler warning. */
18344 implicit_const = -1;
18345 }
18346
18347 if (abbrev_name == 0)
18348 break;
18349
18350 if (cur_abbrev->num_attrs == allocated_attrs)
18351 {
18352 allocated_attrs += ATTR_ALLOC_CHUNK;
18353 cur_attrs
18354 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18355 }
18356
18357 cur_attrs[cur_abbrev->num_attrs].name
18358 = (enum dwarf_attribute) abbrev_name;
18359 cur_attrs[cur_abbrev->num_attrs].form
18360 = (enum dwarf_form) abbrev_form;
18361 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18362 ++cur_abbrev->num_attrs;
18363 }
18364
18365 cur_abbrev->attrs =
18366 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18367 cur_abbrev->num_attrs);
18368 memcpy (cur_abbrev->attrs, cur_attrs,
18369 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18370
18371 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18372
18373 /* Get next abbreviation.
18374 Under Irix6 the abbreviations for a compilation unit are not
18375 always properly terminated with an abbrev number of 0.
18376 Exit loop if we encounter an abbreviation which we have
18377 already read (which means we are about to read the abbreviations
18378 for the next compile unit) or if the end of the abbreviation
18379 table is reached. */
18380 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18381 break;
18382 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18383 abbrev_ptr += bytes_read;
18384 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18385 break;
18386 }
18387
18388 xfree (cur_attrs);
18389 return abbrev_table;
18390 }
18391
18392 /* Returns nonzero if TAG represents a type that we might generate a partial
18393 symbol for. */
18394
18395 static int
18396 is_type_tag_for_partial (int tag)
18397 {
18398 switch (tag)
18399 {
18400 #if 0
18401 /* Some types that would be reasonable to generate partial symbols for,
18402 that we don't at present. */
18403 case DW_TAG_array_type:
18404 case DW_TAG_file_type:
18405 case DW_TAG_ptr_to_member_type:
18406 case DW_TAG_set_type:
18407 case DW_TAG_string_type:
18408 case DW_TAG_subroutine_type:
18409 #endif
18410 case DW_TAG_base_type:
18411 case DW_TAG_class_type:
18412 case DW_TAG_interface_type:
18413 case DW_TAG_enumeration_type:
18414 case DW_TAG_structure_type:
18415 case DW_TAG_subrange_type:
18416 case DW_TAG_typedef:
18417 case DW_TAG_union_type:
18418 return 1;
18419 default:
18420 return 0;
18421 }
18422 }
18423
18424 /* Load all DIEs that are interesting for partial symbols into memory. */
18425
18426 static struct partial_die_info *
18427 load_partial_dies (const struct die_reader_specs *reader,
18428 const gdb_byte *info_ptr, int building_psymtab)
18429 {
18430 struct dwarf2_cu *cu = reader->cu;
18431 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18432 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18433 unsigned int bytes_read;
18434 unsigned int load_all = 0;
18435 int nesting_level = 1;
18436
18437 parent_die = NULL;
18438 last_die = NULL;
18439
18440 gdb_assert (cu->per_cu != NULL);
18441 if (cu->per_cu->load_all_dies)
18442 load_all = 1;
18443
18444 cu->partial_dies
18445 = htab_create_alloc_ex (cu->header.length / 12,
18446 partial_die_hash,
18447 partial_die_eq,
18448 NULL,
18449 &cu->comp_unit_obstack,
18450 hashtab_obstack_allocate,
18451 dummy_obstack_deallocate);
18452
18453 while (1)
18454 {
18455 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18456
18457 /* A NULL abbrev means the end of a series of children. */
18458 if (abbrev == NULL)
18459 {
18460 if (--nesting_level == 0)
18461 return first_die;
18462
18463 info_ptr += bytes_read;
18464 last_die = parent_die;
18465 parent_die = parent_die->die_parent;
18466 continue;
18467 }
18468
18469 /* Check for template arguments. We never save these; if
18470 they're seen, we just mark the parent, and go on our way. */
18471 if (parent_die != NULL
18472 && cu->language == language_cplus
18473 && (abbrev->tag == DW_TAG_template_type_param
18474 || abbrev->tag == DW_TAG_template_value_param))
18475 {
18476 parent_die->has_template_arguments = 1;
18477
18478 if (!load_all)
18479 {
18480 /* We don't need a partial DIE for the template argument. */
18481 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18482 continue;
18483 }
18484 }
18485
18486 /* We only recurse into c++ subprograms looking for template arguments.
18487 Skip their other children. */
18488 if (!load_all
18489 && cu->language == language_cplus
18490 && parent_die != NULL
18491 && parent_die->tag == DW_TAG_subprogram)
18492 {
18493 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18494 continue;
18495 }
18496
18497 /* Check whether this DIE is interesting enough to save. Normally
18498 we would not be interested in members here, but there may be
18499 later variables referencing them via DW_AT_specification (for
18500 static members). */
18501 if (!load_all
18502 && !is_type_tag_for_partial (abbrev->tag)
18503 && abbrev->tag != DW_TAG_constant
18504 && abbrev->tag != DW_TAG_enumerator
18505 && abbrev->tag != DW_TAG_subprogram
18506 && abbrev->tag != DW_TAG_inlined_subroutine
18507 && abbrev->tag != DW_TAG_lexical_block
18508 && abbrev->tag != DW_TAG_variable
18509 && abbrev->tag != DW_TAG_namespace
18510 && abbrev->tag != DW_TAG_module
18511 && abbrev->tag != DW_TAG_member
18512 && abbrev->tag != DW_TAG_imported_unit
18513 && abbrev->tag != DW_TAG_imported_declaration)
18514 {
18515 /* Otherwise we skip to the next sibling, if any. */
18516 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18517 continue;
18518 }
18519
18520 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18521 abbrev);
18522
18523 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18524
18525 /* This two-pass algorithm for processing partial symbols has a
18526 high cost in cache pressure. Thus, handle some simple cases
18527 here which cover the majority of C partial symbols. DIEs
18528 which neither have specification tags in them, nor could have
18529 specification tags elsewhere pointing at them, can simply be
18530 processed and discarded.
18531
18532 This segment is also optional; scan_partial_symbols and
18533 add_partial_symbol will handle these DIEs if we chain
18534 them in normally. When compilers which do not emit large
18535 quantities of duplicate debug information are more common,
18536 this code can probably be removed. */
18537
18538 /* Any complete simple types at the top level (pretty much all
18539 of them, for a language without namespaces), can be processed
18540 directly. */
18541 if (parent_die == NULL
18542 && pdi.has_specification == 0
18543 && pdi.is_declaration == 0
18544 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18545 || pdi.tag == DW_TAG_base_type
18546 || pdi.tag == DW_TAG_subrange_type))
18547 {
18548 if (building_psymtab && pdi.name != NULL)
18549 add_psymbol_to_list (pdi.name, strlen (pdi.name), false,
18550 VAR_DOMAIN, LOC_TYPEDEF, -1,
18551 psymbol_placement::STATIC,
18552 0, cu->language, objfile);
18553 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18554 continue;
18555 }
18556
18557 /* The exception for DW_TAG_typedef with has_children above is
18558 a workaround of GCC PR debug/47510. In the case of this complaint
18559 type_name_or_error will error on such types later.
18560
18561 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18562 it could not find the child DIEs referenced later, this is checked
18563 above. In correct DWARF DW_TAG_typedef should have no children. */
18564
18565 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18566 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18567 "- DIE at %s [in module %s]"),
18568 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18569
18570 /* If we're at the second level, and we're an enumerator, and
18571 our parent has no specification (meaning possibly lives in a
18572 namespace elsewhere), then we can add the partial symbol now
18573 instead of queueing it. */
18574 if (pdi.tag == DW_TAG_enumerator
18575 && parent_die != NULL
18576 && parent_die->die_parent == NULL
18577 && parent_die->tag == DW_TAG_enumeration_type
18578 && parent_die->has_specification == 0)
18579 {
18580 if (pdi.name == NULL)
18581 complaint (_("malformed enumerator DIE ignored"));
18582 else if (building_psymtab)
18583 add_psymbol_to_list (pdi.name, strlen (pdi.name), false,
18584 VAR_DOMAIN, LOC_CONST, -1,
18585 cu->language == language_cplus
18586 ? psymbol_placement::GLOBAL
18587 : psymbol_placement::STATIC,
18588 0, cu->language, objfile);
18589
18590 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18591 continue;
18592 }
18593
18594 struct partial_die_info *part_die
18595 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18596
18597 /* We'll save this DIE so link it in. */
18598 part_die->die_parent = parent_die;
18599 part_die->die_sibling = NULL;
18600 part_die->die_child = NULL;
18601
18602 if (last_die && last_die == parent_die)
18603 last_die->die_child = part_die;
18604 else if (last_die)
18605 last_die->die_sibling = part_die;
18606
18607 last_die = part_die;
18608
18609 if (first_die == NULL)
18610 first_die = part_die;
18611
18612 /* Maybe add the DIE to the hash table. Not all DIEs that we
18613 find interesting need to be in the hash table, because we
18614 also have the parent/sibling/child chains; only those that we
18615 might refer to by offset later during partial symbol reading.
18616
18617 For now this means things that might have be the target of a
18618 DW_AT_specification, DW_AT_abstract_origin, or
18619 DW_AT_extension. DW_AT_extension will refer only to
18620 namespaces; DW_AT_abstract_origin refers to functions (and
18621 many things under the function DIE, but we do not recurse
18622 into function DIEs during partial symbol reading) and
18623 possibly variables as well; DW_AT_specification refers to
18624 declarations. Declarations ought to have the DW_AT_declaration
18625 flag. It happens that GCC forgets to put it in sometimes, but
18626 only for functions, not for types.
18627
18628 Adding more things than necessary to the hash table is harmless
18629 except for the performance cost. Adding too few will result in
18630 wasted time in find_partial_die, when we reread the compilation
18631 unit with load_all_dies set. */
18632
18633 if (load_all
18634 || abbrev->tag == DW_TAG_constant
18635 || abbrev->tag == DW_TAG_subprogram
18636 || abbrev->tag == DW_TAG_variable
18637 || abbrev->tag == DW_TAG_namespace
18638 || part_die->is_declaration)
18639 {
18640 void **slot;
18641
18642 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18643 to_underlying (part_die->sect_off),
18644 INSERT);
18645 *slot = part_die;
18646 }
18647
18648 /* For some DIEs we want to follow their children (if any). For C
18649 we have no reason to follow the children of structures; for other
18650 languages we have to, so that we can get at method physnames
18651 to infer fully qualified class names, for DW_AT_specification,
18652 and for C++ template arguments. For C++, we also look one level
18653 inside functions to find template arguments (if the name of the
18654 function does not already contain the template arguments).
18655
18656 For Ada, we need to scan the children of subprograms and lexical
18657 blocks as well because Ada allows the definition of nested
18658 entities that could be interesting for the debugger, such as
18659 nested subprograms for instance. */
18660 if (last_die->has_children
18661 && (load_all
18662 || last_die->tag == DW_TAG_namespace
18663 || last_die->tag == DW_TAG_module
18664 || last_die->tag == DW_TAG_enumeration_type
18665 || (cu->language == language_cplus
18666 && last_die->tag == DW_TAG_subprogram
18667 && (last_die->name == NULL
18668 || strchr (last_die->name, '<') == NULL))
18669 || (cu->language != language_c
18670 && (last_die->tag == DW_TAG_class_type
18671 || last_die->tag == DW_TAG_interface_type
18672 || last_die->tag == DW_TAG_structure_type
18673 || last_die->tag == DW_TAG_union_type))
18674 || (cu->language == language_ada
18675 && (last_die->tag == DW_TAG_subprogram
18676 || last_die->tag == DW_TAG_lexical_block))))
18677 {
18678 nesting_level++;
18679 parent_die = last_die;
18680 continue;
18681 }
18682
18683 /* Otherwise we skip to the next sibling, if any. */
18684 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18685
18686 /* Back to the top, do it again. */
18687 }
18688 }
18689
18690 partial_die_info::partial_die_info (sect_offset sect_off_,
18691 struct abbrev_info *abbrev)
18692 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18693 {
18694 }
18695
18696 /* Read a minimal amount of information into the minimal die structure.
18697 INFO_PTR should point just after the initial uleb128 of a DIE. */
18698
18699 const gdb_byte *
18700 partial_die_info::read (const struct die_reader_specs *reader,
18701 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18702 {
18703 struct dwarf2_cu *cu = reader->cu;
18704 struct dwarf2_per_objfile *dwarf2_per_objfile
18705 = cu->per_cu->dwarf2_per_objfile;
18706 unsigned int i;
18707 int has_low_pc_attr = 0;
18708 int has_high_pc_attr = 0;
18709 int high_pc_relative = 0;
18710
18711 for (i = 0; i < abbrev.num_attrs; ++i)
18712 {
18713 struct attribute attr;
18714
18715 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18716
18717 /* Store the data if it is of an attribute we want to keep in a
18718 partial symbol table. */
18719 switch (attr.name)
18720 {
18721 case DW_AT_name:
18722 switch (tag)
18723 {
18724 case DW_TAG_compile_unit:
18725 case DW_TAG_partial_unit:
18726 case DW_TAG_type_unit:
18727 /* Compilation units have a DW_AT_name that is a filename, not
18728 a source language identifier. */
18729 case DW_TAG_enumeration_type:
18730 case DW_TAG_enumerator:
18731 /* These tags always have simple identifiers already; no need
18732 to canonicalize them. */
18733 name = DW_STRING (&attr);
18734 break;
18735 default:
18736 {
18737 struct objfile *objfile = dwarf2_per_objfile->objfile;
18738
18739 name
18740 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18741 &objfile->per_bfd->storage_obstack);
18742 }
18743 break;
18744 }
18745 break;
18746 case DW_AT_linkage_name:
18747 case DW_AT_MIPS_linkage_name:
18748 /* Note that both forms of linkage name might appear. We
18749 assume they will be the same, and we only store the last
18750 one we see. */
18751 linkage_name = DW_STRING (&attr);
18752 break;
18753 case DW_AT_low_pc:
18754 has_low_pc_attr = 1;
18755 lowpc = attr_value_as_address (&attr);
18756 break;
18757 case DW_AT_high_pc:
18758 has_high_pc_attr = 1;
18759 highpc = attr_value_as_address (&attr);
18760 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18761 high_pc_relative = 1;
18762 break;
18763 case DW_AT_location:
18764 /* Support the .debug_loc offsets. */
18765 if (attr_form_is_block (&attr))
18766 {
18767 d.locdesc = DW_BLOCK (&attr);
18768 }
18769 else if (attr_form_is_section_offset (&attr))
18770 {
18771 dwarf2_complex_location_expr_complaint ();
18772 }
18773 else
18774 {
18775 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18776 "partial symbol information");
18777 }
18778 break;
18779 case DW_AT_external:
18780 is_external = DW_UNSND (&attr);
18781 break;
18782 case DW_AT_declaration:
18783 is_declaration = DW_UNSND (&attr);
18784 break;
18785 case DW_AT_type:
18786 has_type = 1;
18787 break;
18788 case DW_AT_abstract_origin:
18789 case DW_AT_specification:
18790 case DW_AT_extension:
18791 has_specification = 1;
18792 spec_offset = dwarf2_get_ref_die_offset (&attr);
18793 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18794 || cu->per_cu->is_dwz);
18795 break;
18796 case DW_AT_sibling:
18797 /* Ignore absolute siblings, they might point outside of
18798 the current compile unit. */
18799 if (attr.form == DW_FORM_ref_addr)
18800 complaint (_("ignoring absolute DW_AT_sibling"));
18801 else
18802 {
18803 const gdb_byte *buffer = reader->buffer;
18804 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18805 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18806
18807 if (sibling_ptr < info_ptr)
18808 complaint (_("DW_AT_sibling points backwards"));
18809 else if (sibling_ptr > reader->buffer_end)
18810 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18811 else
18812 sibling = sibling_ptr;
18813 }
18814 break;
18815 case DW_AT_byte_size:
18816 has_byte_size = 1;
18817 break;
18818 case DW_AT_const_value:
18819 has_const_value = 1;
18820 break;
18821 case DW_AT_calling_convention:
18822 /* DWARF doesn't provide a way to identify a program's source-level
18823 entry point. DW_AT_calling_convention attributes are only meant
18824 to describe functions' calling conventions.
18825
18826 However, because it's a necessary piece of information in
18827 Fortran, and before DWARF 4 DW_CC_program was the only
18828 piece of debugging information whose definition refers to
18829 a 'main program' at all, several compilers marked Fortran
18830 main programs with DW_CC_program --- even when those
18831 functions use the standard calling conventions.
18832
18833 Although DWARF now specifies a way to provide this
18834 information, we support this practice for backward
18835 compatibility. */
18836 if (DW_UNSND (&attr) == DW_CC_program
18837 && cu->language == language_fortran)
18838 main_subprogram = 1;
18839 break;
18840 case DW_AT_inline:
18841 if (DW_UNSND (&attr) == DW_INL_inlined
18842 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18843 may_be_inlined = 1;
18844 break;
18845
18846 case DW_AT_import:
18847 if (tag == DW_TAG_imported_unit)
18848 {
18849 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18850 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18851 || cu->per_cu->is_dwz);
18852 }
18853 break;
18854
18855 case DW_AT_main_subprogram:
18856 main_subprogram = DW_UNSND (&attr);
18857 break;
18858
18859 case DW_AT_ranges:
18860 {
18861 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18862 but that requires a full DIE, so instead we just
18863 reimplement it. */
18864 int need_ranges_base = tag != DW_TAG_compile_unit;
18865 unsigned int ranges_offset = (DW_UNSND (&attr)
18866 + (need_ranges_base
18867 ? cu->ranges_base
18868 : 0));
18869
18870 /* Value of the DW_AT_ranges attribute is the offset in the
18871 .debug_ranges section. */
18872 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18873 nullptr))
18874 has_pc_info = 1;
18875 }
18876 break;
18877
18878 default:
18879 break;
18880 }
18881 }
18882
18883 /* For Ada, if both the name and the linkage name appear, we prefer
18884 the latter. This lets "catch exception" work better, regardless
18885 of the order in which the name and linkage name were emitted.
18886 Really, though, this is just a workaround for the fact that gdb
18887 doesn't store both the name and the linkage name. */
18888 if (cu->language == language_ada && linkage_name != nullptr)
18889 name = linkage_name;
18890
18891 if (high_pc_relative)
18892 highpc += lowpc;
18893
18894 if (has_low_pc_attr && has_high_pc_attr)
18895 {
18896 /* When using the GNU linker, .gnu.linkonce. sections are used to
18897 eliminate duplicate copies of functions and vtables and such.
18898 The linker will arbitrarily choose one and discard the others.
18899 The AT_*_pc values for such functions refer to local labels in
18900 these sections. If the section from that file was discarded, the
18901 labels are not in the output, so the relocs get a value of 0.
18902 If this is a discarded function, mark the pc bounds as invalid,
18903 so that GDB will ignore it. */
18904 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18905 {
18906 struct objfile *objfile = dwarf2_per_objfile->objfile;
18907 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18908
18909 complaint (_("DW_AT_low_pc %s is zero "
18910 "for DIE at %s [in module %s]"),
18911 paddress (gdbarch, lowpc),
18912 sect_offset_str (sect_off),
18913 objfile_name (objfile));
18914 }
18915 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18916 else if (lowpc >= highpc)
18917 {
18918 struct objfile *objfile = dwarf2_per_objfile->objfile;
18919 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18920
18921 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18922 "for DIE at %s [in module %s]"),
18923 paddress (gdbarch, lowpc),
18924 paddress (gdbarch, highpc),
18925 sect_offset_str (sect_off),
18926 objfile_name (objfile));
18927 }
18928 else
18929 has_pc_info = 1;
18930 }
18931
18932 return info_ptr;
18933 }
18934
18935 /* Find a cached partial DIE at OFFSET in CU. */
18936
18937 struct partial_die_info *
18938 dwarf2_cu::find_partial_die (sect_offset sect_off)
18939 {
18940 struct partial_die_info *lookup_die = NULL;
18941 struct partial_die_info part_die (sect_off);
18942
18943 lookup_die = ((struct partial_die_info *)
18944 htab_find_with_hash (partial_dies, &part_die,
18945 to_underlying (sect_off)));
18946
18947 return lookup_die;
18948 }
18949
18950 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18951 except in the case of .debug_types DIEs which do not reference
18952 outside their CU (they do however referencing other types via
18953 DW_FORM_ref_sig8). */
18954
18955 static const struct cu_partial_die_info
18956 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18957 {
18958 struct dwarf2_per_objfile *dwarf2_per_objfile
18959 = cu->per_cu->dwarf2_per_objfile;
18960 struct objfile *objfile = dwarf2_per_objfile->objfile;
18961 struct dwarf2_per_cu_data *per_cu = NULL;
18962 struct partial_die_info *pd = NULL;
18963
18964 if (offset_in_dwz == cu->per_cu->is_dwz
18965 && offset_in_cu_p (&cu->header, sect_off))
18966 {
18967 pd = cu->find_partial_die (sect_off);
18968 if (pd != NULL)
18969 return { cu, pd };
18970 /* We missed recording what we needed.
18971 Load all dies and try again. */
18972 per_cu = cu->per_cu;
18973 }
18974 else
18975 {
18976 /* TUs don't reference other CUs/TUs (except via type signatures). */
18977 if (cu->per_cu->is_debug_types)
18978 {
18979 error (_("Dwarf Error: Type Unit at offset %s contains"
18980 " external reference to offset %s [in module %s].\n"),
18981 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18982 bfd_get_filename (objfile->obfd));
18983 }
18984 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18985 dwarf2_per_objfile);
18986
18987 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18988 load_partial_comp_unit (per_cu);
18989
18990 per_cu->cu->last_used = 0;
18991 pd = per_cu->cu->find_partial_die (sect_off);
18992 }
18993
18994 /* If we didn't find it, and not all dies have been loaded,
18995 load them all and try again. */
18996
18997 if (pd == NULL && per_cu->load_all_dies == 0)
18998 {
18999 per_cu->load_all_dies = 1;
19000
19001 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19002 THIS_CU->cu may already be in use. So we can't just free it and
19003 replace its DIEs with the ones we read in. Instead, we leave those
19004 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19005 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19006 set. */
19007 load_partial_comp_unit (per_cu);
19008
19009 pd = per_cu->cu->find_partial_die (sect_off);
19010 }
19011
19012 if (pd == NULL)
19013 internal_error (__FILE__, __LINE__,
19014 _("could not find partial DIE %s "
19015 "in cache [from module %s]\n"),
19016 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19017 return { per_cu->cu, pd };
19018 }
19019
19020 /* See if we can figure out if the class lives in a namespace. We do
19021 this by looking for a member function; its demangled name will
19022 contain namespace info, if there is any. */
19023
19024 static void
19025 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19026 struct dwarf2_cu *cu)
19027 {
19028 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19029 what template types look like, because the demangler
19030 frequently doesn't give the same name as the debug info. We
19031 could fix this by only using the demangled name to get the
19032 prefix (but see comment in read_structure_type). */
19033
19034 struct partial_die_info *real_pdi;
19035 struct partial_die_info *child_pdi;
19036
19037 /* If this DIE (this DIE's specification, if any) has a parent, then
19038 we should not do this. We'll prepend the parent's fully qualified
19039 name when we create the partial symbol. */
19040
19041 real_pdi = struct_pdi;
19042 while (real_pdi->has_specification)
19043 {
19044 auto res = find_partial_die (real_pdi->spec_offset,
19045 real_pdi->spec_is_dwz, cu);
19046 real_pdi = res.pdi;
19047 cu = res.cu;
19048 }
19049
19050 if (real_pdi->die_parent != NULL)
19051 return;
19052
19053 for (child_pdi = struct_pdi->die_child;
19054 child_pdi != NULL;
19055 child_pdi = child_pdi->die_sibling)
19056 {
19057 if (child_pdi->tag == DW_TAG_subprogram
19058 && child_pdi->linkage_name != NULL)
19059 {
19060 char *actual_class_name
19061 = language_class_name_from_physname (cu->language_defn,
19062 child_pdi->linkage_name);
19063 if (actual_class_name != NULL)
19064 {
19065 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19066 struct_pdi->name
19067 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19068 actual_class_name);
19069 xfree (actual_class_name);
19070 }
19071 break;
19072 }
19073 }
19074 }
19075
19076 void
19077 partial_die_info::fixup (struct dwarf2_cu *cu)
19078 {
19079 /* Once we've fixed up a die, there's no point in doing so again.
19080 This also avoids a memory leak if we were to call
19081 guess_partial_die_structure_name multiple times. */
19082 if (fixup_called)
19083 return;
19084
19085 /* If we found a reference attribute and the DIE has no name, try
19086 to find a name in the referred to DIE. */
19087
19088 if (name == NULL && has_specification)
19089 {
19090 struct partial_die_info *spec_die;
19091
19092 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19093 spec_die = res.pdi;
19094 cu = res.cu;
19095
19096 spec_die->fixup (cu);
19097
19098 if (spec_die->name)
19099 {
19100 name = spec_die->name;
19101
19102 /* Copy DW_AT_external attribute if it is set. */
19103 if (spec_die->is_external)
19104 is_external = spec_die->is_external;
19105 }
19106 }
19107
19108 /* Set default names for some unnamed DIEs. */
19109
19110 if (name == NULL && tag == DW_TAG_namespace)
19111 name = CP_ANONYMOUS_NAMESPACE_STR;
19112
19113 /* If there is no parent die to provide a namespace, and there are
19114 children, see if we can determine the namespace from their linkage
19115 name. */
19116 if (cu->language == language_cplus
19117 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
19118 && die_parent == NULL
19119 && has_children
19120 && (tag == DW_TAG_class_type
19121 || tag == DW_TAG_structure_type
19122 || tag == DW_TAG_union_type))
19123 guess_partial_die_structure_name (this, cu);
19124
19125 /* GCC might emit a nameless struct or union that has a linkage
19126 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19127 if (name == NULL
19128 && (tag == DW_TAG_class_type
19129 || tag == DW_TAG_interface_type
19130 || tag == DW_TAG_structure_type
19131 || tag == DW_TAG_union_type)
19132 && linkage_name != NULL)
19133 {
19134 char *demangled;
19135
19136 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
19137 if (demangled)
19138 {
19139 const char *base;
19140
19141 /* Strip any leading namespaces/classes, keep only the base name.
19142 DW_AT_name for named DIEs does not contain the prefixes. */
19143 base = strrchr (demangled, ':');
19144 if (base && base > demangled && base[-1] == ':')
19145 base++;
19146 else
19147 base = demangled;
19148
19149 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19150 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
19151 xfree (demangled);
19152 }
19153 }
19154
19155 fixup_called = 1;
19156 }
19157
19158 /* Read an attribute value described by an attribute form. */
19159
19160 static const gdb_byte *
19161 read_attribute_value (const struct die_reader_specs *reader,
19162 struct attribute *attr, unsigned form,
19163 LONGEST implicit_const, const gdb_byte *info_ptr)
19164 {
19165 struct dwarf2_cu *cu = reader->cu;
19166 struct dwarf2_per_objfile *dwarf2_per_objfile
19167 = cu->per_cu->dwarf2_per_objfile;
19168 struct objfile *objfile = dwarf2_per_objfile->objfile;
19169 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19170 bfd *abfd = reader->abfd;
19171 struct comp_unit_head *cu_header = &cu->header;
19172 unsigned int bytes_read;
19173 struct dwarf_block *blk;
19174
19175 attr->form = (enum dwarf_form) form;
19176 switch (form)
19177 {
19178 case DW_FORM_ref_addr:
19179 if (cu->header.version == 2)
19180 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19181 else
19182 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19183 &cu->header, &bytes_read);
19184 info_ptr += bytes_read;
19185 break;
19186 case DW_FORM_GNU_ref_alt:
19187 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19188 info_ptr += bytes_read;
19189 break;
19190 case DW_FORM_addr:
19191 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19192 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19193 info_ptr += bytes_read;
19194 break;
19195 case DW_FORM_block2:
19196 blk = dwarf_alloc_block (cu);
19197 blk->size = read_2_bytes (abfd, info_ptr);
19198 info_ptr += 2;
19199 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19200 info_ptr += blk->size;
19201 DW_BLOCK (attr) = blk;
19202 break;
19203 case DW_FORM_block4:
19204 blk = dwarf_alloc_block (cu);
19205 blk->size = read_4_bytes (abfd, info_ptr);
19206 info_ptr += 4;
19207 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19208 info_ptr += blk->size;
19209 DW_BLOCK (attr) = blk;
19210 break;
19211 case DW_FORM_data2:
19212 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19213 info_ptr += 2;
19214 break;
19215 case DW_FORM_data4:
19216 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19217 info_ptr += 4;
19218 break;
19219 case DW_FORM_data8:
19220 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19221 info_ptr += 8;
19222 break;
19223 case DW_FORM_data16:
19224 blk = dwarf_alloc_block (cu);
19225 blk->size = 16;
19226 blk->data = read_n_bytes (abfd, info_ptr, 16);
19227 info_ptr += 16;
19228 DW_BLOCK (attr) = blk;
19229 break;
19230 case DW_FORM_sec_offset:
19231 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19232 info_ptr += bytes_read;
19233 break;
19234 case DW_FORM_string:
19235 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19236 DW_STRING_IS_CANONICAL (attr) = 0;
19237 info_ptr += bytes_read;
19238 break;
19239 case DW_FORM_strp:
19240 if (!cu->per_cu->is_dwz)
19241 {
19242 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19243 abfd, info_ptr, cu_header,
19244 &bytes_read);
19245 DW_STRING_IS_CANONICAL (attr) = 0;
19246 info_ptr += bytes_read;
19247 break;
19248 }
19249 /* FALLTHROUGH */
19250 case DW_FORM_line_strp:
19251 if (!cu->per_cu->is_dwz)
19252 {
19253 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19254 abfd, info_ptr,
19255 cu_header, &bytes_read);
19256 DW_STRING_IS_CANONICAL (attr) = 0;
19257 info_ptr += bytes_read;
19258 break;
19259 }
19260 /* FALLTHROUGH */
19261 case DW_FORM_GNU_strp_alt:
19262 {
19263 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19264 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19265 &bytes_read);
19266
19267 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19268 dwz, str_offset);
19269 DW_STRING_IS_CANONICAL (attr) = 0;
19270 info_ptr += bytes_read;
19271 }
19272 break;
19273 case DW_FORM_exprloc:
19274 case DW_FORM_block:
19275 blk = dwarf_alloc_block (cu);
19276 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19277 info_ptr += bytes_read;
19278 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19279 info_ptr += blk->size;
19280 DW_BLOCK (attr) = blk;
19281 break;
19282 case DW_FORM_block1:
19283 blk = dwarf_alloc_block (cu);
19284 blk->size = read_1_byte (abfd, info_ptr);
19285 info_ptr += 1;
19286 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19287 info_ptr += blk->size;
19288 DW_BLOCK (attr) = blk;
19289 break;
19290 case DW_FORM_data1:
19291 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19292 info_ptr += 1;
19293 break;
19294 case DW_FORM_flag:
19295 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19296 info_ptr += 1;
19297 break;
19298 case DW_FORM_flag_present:
19299 DW_UNSND (attr) = 1;
19300 break;
19301 case DW_FORM_sdata:
19302 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19303 info_ptr += bytes_read;
19304 break;
19305 case DW_FORM_udata:
19306 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19307 info_ptr += bytes_read;
19308 break;
19309 case DW_FORM_ref1:
19310 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19311 + read_1_byte (abfd, info_ptr));
19312 info_ptr += 1;
19313 break;
19314 case DW_FORM_ref2:
19315 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19316 + read_2_bytes (abfd, info_ptr));
19317 info_ptr += 2;
19318 break;
19319 case DW_FORM_ref4:
19320 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19321 + read_4_bytes (abfd, info_ptr));
19322 info_ptr += 4;
19323 break;
19324 case DW_FORM_ref8:
19325 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19326 + read_8_bytes (abfd, info_ptr));
19327 info_ptr += 8;
19328 break;
19329 case DW_FORM_ref_sig8:
19330 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19331 info_ptr += 8;
19332 break;
19333 case DW_FORM_ref_udata:
19334 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19335 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19336 info_ptr += bytes_read;
19337 break;
19338 case DW_FORM_indirect:
19339 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19340 info_ptr += bytes_read;
19341 if (form == DW_FORM_implicit_const)
19342 {
19343 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19344 info_ptr += bytes_read;
19345 }
19346 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19347 info_ptr);
19348 break;
19349 case DW_FORM_implicit_const:
19350 DW_SND (attr) = implicit_const;
19351 break;
19352 case DW_FORM_addrx:
19353 case DW_FORM_GNU_addr_index:
19354 if (reader->dwo_file == NULL)
19355 {
19356 /* For now flag a hard error.
19357 Later we can turn this into a complaint. */
19358 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19359 dwarf_form_name (form),
19360 bfd_get_filename (abfd));
19361 }
19362 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19363 info_ptr += bytes_read;
19364 break;
19365 case DW_FORM_strx:
19366 case DW_FORM_strx1:
19367 case DW_FORM_strx2:
19368 case DW_FORM_strx3:
19369 case DW_FORM_strx4:
19370 case DW_FORM_GNU_str_index:
19371 if (reader->dwo_file == NULL)
19372 {
19373 /* For now flag a hard error.
19374 Later we can turn this into a complaint if warranted. */
19375 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19376 dwarf_form_name (form),
19377 bfd_get_filename (abfd));
19378 }
19379 {
19380 ULONGEST str_index;
19381 if (form == DW_FORM_strx1)
19382 {
19383 str_index = read_1_byte (abfd, info_ptr);
19384 info_ptr += 1;
19385 }
19386 else if (form == DW_FORM_strx2)
19387 {
19388 str_index = read_2_bytes (abfd, info_ptr);
19389 info_ptr += 2;
19390 }
19391 else if (form == DW_FORM_strx3)
19392 {
19393 str_index = read_3_bytes (abfd, info_ptr);
19394 info_ptr += 3;
19395 }
19396 else if (form == DW_FORM_strx4)
19397 {
19398 str_index = read_4_bytes (abfd, info_ptr);
19399 info_ptr += 4;
19400 }
19401 else
19402 {
19403 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19404 info_ptr += bytes_read;
19405 }
19406 DW_STRING (attr) = read_str_index (reader, str_index);
19407 DW_STRING_IS_CANONICAL (attr) = 0;
19408 }
19409 break;
19410 default:
19411 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19412 dwarf_form_name (form),
19413 bfd_get_filename (abfd));
19414 }
19415
19416 /* Super hack. */
19417 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19418 attr->form = DW_FORM_GNU_ref_alt;
19419
19420 /* We have seen instances where the compiler tried to emit a byte
19421 size attribute of -1 which ended up being encoded as an unsigned
19422 0xffffffff. Although 0xffffffff is technically a valid size value,
19423 an object of this size seems pretty unlikely so we can relatively
19424 safely treat these cases as if the size attribute was invalid and
19425 treat them as zero by default. */
19426 if (attr->name == DW_AT_byte_size
19427 && form == DW_FORM_data4
19428 && DW_UNSND (attr) >= 0xffffffff)
19429 {
19430 complaint
19431 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19432 hex_string (DW_UNSND (attr)));
19433 DW_UNSND (attr) = 0;
19434 }
19435
19436 return info_ptr;
19437 }
19438
19439 /* Read an attribute described by an abbreviated attribute. */
19440
19441 static const gdb_byte *
19442 read_attribute (const struct die_reader_specs *reader,
19443 struct attribute *attr, struct attr_abbrev *abbrev,
19444 const gdb_byte *info_ptr)
19445 {
19446 attr->name = abbrev->name;
19447 return read_attribute_value (reader, attr, abbrev->form,
19448 abbrev->implicit_const, info_ptr);
19449 }
19450
19451 /* Read dwarf information from a buffer. */
19452
19453 static unsigned int
19454 read_1_byte (bfd *abfd, const gdb_byte *buf)
19455 {
19456 return bfd_get_8 (abfd, buf);
19457 }
19458
19459 static int
19460 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19461 {
19462 return bfd_get_signed_8 (abfd, buf);
19463 }
19464
19465 static unsigned int
19466 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19467 {
19468 return bfd_get_16 (abfd, buf);
19469 }
19470
19471 static int
19472 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19473 {
19474 return bfd_get_signed_16 (abfd, buf);
19475 }
19476
19477 static unsigned int
19478 read_3_bytes (bfd *abfd, const gdb_byte *buf)
19479 {
19480 unsigned int result = 0;
19481 for (int i = 0; i < 3; ++i)
19482 {
19483 unsigned char byte = bfd_get_8 (abfd, buf);
19484 buf++;
19485 result |= ((unsigned int) byte << (i * 8));
19486 }
19487 return result;
19488 }
19489
19490 static unsigned int
19491 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19492 {
19493 return bfd_get_32 (abfd, buf);
19494 }
19495
19496 static int
19497 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19498 {
19499 return bfd_get_signed_32 (abfd, buf);
19500 }
19501
19502 static ULONGEST
19503 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19504 {
19505 return bfd_get_64 (abfd, buf);
19506 }
19507
19508 static CORE_ADDR
19509 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19510 unsigned int *bytes_read)
19511 {
19512 struct comp_unit_head *cu_header = &cu->header;
19513 CORE_ADDR retval = 0;
19514
19515 if (cu_header->signed_addr_p)
19516 {
19517 switch (cu_header->addr_size)
19518 {
19519 case 2:
19520 retval = bfd_get_signed_16 (abfd, buf);
19521 break;
19522 case 4:
19523 retval = bfd_get_signed_32 (abfd, buf);
19524 break;
19525 case 8:
19526 retval = bfd_get_signed_64 (abfd, buf);
19527 break;
19528 default:
19529 internal_error (__FILE__, __LINE__,
19530 _("read_address: bad switch, signed [in module %s]"),
19531 bfd_get_filename (abfd));
19532 }
19533 }
19534 else
19535 {
19536 switch (cu_header->addr_size)
19537 {
19538 case 2:
19539 retval = bfd_get_16 (abfd, buf);
19540 break;
19541 case 4:
19542 retval = bfd_get_32 (abfd, buf);
19543 break;
19544 case 8:
19545 retval = bfd_get_64 (abfd, buf);
19546 break;
19547 default:
19548 internal_error (__FILE__, __LINE__,
19549 _("read_address: bad switch, "
19550 "unsigned [in module %s]"),
19551 bfd_get_filename (abfd));
19552 }
19553 }
19554
19555 *bytes_read = cu_header->addr_size;
19556 return retval;
19557 }
19558
19559 /* Read the initial length from a section. The (draft) DWARF 3
19560 specification allows the initial length to take up either 4 bytes
19561 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19562 bytes describe the length and all offsets will be 8 bytes in length
19563 instead of 4.
19564
19565 An older, non-standard 64-bit format is also handled by this
19566 function. The older format in question stores the initial length
19567 as an 8-byte quantity without an escape value. Lengths greater
19568 than 2^32 aren't very common which means that the initial 4 bytes
19569 is almost always zero. Since a length value of zero doesn't make
19570 sense for the 32-bit format, this initial zero can be considered to
19571 be an escape value which indicates the presence of the older 64-bit
19572 format. As written, the code can't detect (old format) lengths
19573 greater than 4GB. If it becomes necessary to handle lengths
19574 somewhat larger than 4GB, we could allow other small values (such
19575 as the non-sensical values of 1, 2, and 3) to also be used as
19576 escape values indicating the presence of the old format.
19577
19578 The value returned via bytes_read should be used to increment the
19579 relevant pointer after calling read_initial_length().
19580
19581 [ Note: read_initial_length() and read_offset() are based on the
19582 document entitled "DWARF Debugging Information Format", revision
19583 3, draft 8, dated November 19, 2001. This document was obtained
19584 from:
19585
19586 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19587
19588 This document is only a draft and is subject to change. (So beware.)
19589
19590 Details regarding the older, non-standard 64-bit format were
19591 determined empirically by examining 64-bit ELF files produced by
19592 the SGI toolchain on an IRIX 6.5 machine.
19593
19594 - Kevin, July 16, 2002
19595 ] */
19596
19597 static LONGEST
19598 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19599 {
19600 LONGEST length = bfd_get_32 (abfd, buf);
19601
19602 if (length == 0xffffffff)
19603 {
19604 length = bfd_get_64 (abfd, buf + 4);
19605 *bytes_read = 12;
19606 }
19607 else if (length == 0)
19608 {
19609 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19610 length = bfd_get_64 (abfd, buf);
19611 *bytes_read = 8;
19612 }
19613 else
19614 {
19615 *bytes_read = 4;
19616 }
19617
19618 return length;
19619 }
19620
19621 /* Cover function for read_initial_length.
19622 Returns the length of the object at BUF, and stores the size of the
19623 initial length in *BYTES_READ and stores the size that offsets will be in
19624 *OFFSET_SIZE.
19625 If the initial length size is not equivalent to that specified in
19626 CU_HEADER then issue a complaint.
19627 This is useful when reading non-comp-unit headers. */
19628
19629 static LONGEST
19630 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19631 const struct comp_unit_head *cu_header,
19632 unsigned int *bytes_read,
19633 unsigned int *offset_size)
19634 {
19635 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19636
19637 gdb_assert (cu_header->initial_length_size == 4
19638 || cu_header->initial_length_size == 8
19639 || cu_header->initial_length_size == 12);
19640
19641 if (cu_header->initial_length_size != *bytes_read)
19642 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19643
19644 *offset_size = (*bytes_read == 4) ? 4 : 8;
19645 return length;
19646 }
19647
19648 /* Read an offset from the data stream. The size of the offset is
19649 given by cu_header->offset_size. */
19650
19651 static LONGEST
19652 read_offset (bfd *abfd, const gdb_byte *buf,
19653 const struct comp_unit_head *cu_header,
19654 unsigned int *bytes_read)
19655 {
19656 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19657
19658 *bytes_read = cu_header->offset_size;
19659 return offset;
19660 }
19661
19662 /* Read an offset from the data stream. */
19663
19664 static LONGEST
19665 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19666 {
19667 LONGEST retval = 0;
19668
19669 switch (offset_size)
19670 {
19671 case 4:
19672 retval = bfd_get_32 (abfd, buf);
19673 break;
19674 case 8:
19675 retval = bfd_get_64 (abfd, buf);
19676 break;
19677 default:
19678 internal_error (__FILE__, __LINE__,
19679 _("read_offset_1: bad switch [in module %s]"),
19680 bfd_get_filename (abfd));
19681 }
19682
19683 return retval;
19684 }
19685
19686 static const gdb_byte *
19687 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19688 {
19689 /* If the size of a host char is 8 bits, we can return a pointer
19690 to the buffer, otherwise we have to copy the data to a buffer
19691 allocated on the temporary obstack. */
19692 gdb_assert (HOST_CHAR_BIT == 8);
19693 return buf;
19694 }
19695
19696 static const char *
19697 read_direct_string (bfd *abfd, const gdb_byte *buf,
19698 unsigned int *bytes_read_ptr)
19699 {
19700 /* If the size of a host char is 8 bits, we can return a pointer
19701 to the string, otherwise we have to copy the string to a buffer
19702 allocated on the temporary obstack. */
19703 gdb_assert (HOST_CHAR_BIT == 8);
19704 if (*buf == '\0')
19705 {
19706 *bytes_read_ptr = 1;
19707 return NULL;
19708 }
19709 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19710 return (const char *) buf;
19711 }
19712
19713 /* Return pointer to string at section SECT offset STR_OFFSET with error
19714 reporting strings FORM_NAME and SECT_NAME. */
19715
19716 static const char *
19717 read_indirect_string_at_offset_from (struct objfile *objfile,
19718 bfd *abfd, LONGEST str_offset,
19719 struct dwarf2_section_info *sect,
19720 const char *form_name,
19721 const char *sect_name)
19722 {
19723 dwarf2_read_section (objfile, sect);
19724 if (sect->buffer == NULL)
19725 error (_("%s used without %s section [in module %s]"),
19726 form_name, sect_name, bfd_get_filename (abfd));
19727 if (str_offset >= sect->size)
19728 error (_("%s pointing outside of %s section [in module %s]"),
19729 form_name, sect_name, bfd_get_filename (abfd));
19730 gdb_assert (HOST_CHAR_BIT == 8);
19731 if (sect->buffer[str_offset] == '\0')
19732 return NULL;
19733 return (const char *) (sect->buffer + str_offset);
19734 }
19735
19736 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19737
19738 static const char *
19739 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19740 bfd *abfd, LONGEST str_offset)
19741 {
19742 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19743 abfd, str_offset,
19744 &dwarf2_per_objfile->str,
19745 "DW_FORM_strp", ".debug_str");
19746 }
19747
19748 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19749
19750 static const char *
19751 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19752 bfd *abfd, LONGEST str_offset)
19753 {
19754 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19755 abfd, str_offset,
19756 &dwarf2_per_objfile->line_str,
19757 "DW_FORM_line_strp",
19758 ".debug_line_str");
19759 }
19760
19761 /* Read a string at offset STR_OFFSET in the .debug_str section from
19762 the .dwz file DWZ. Throw an error if the offset is too large. If
19763 the string consists of a single NUL byte, return NULL; otherwise
19764 return a pointer to the string. */
19765
19766 static const char *
19767 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19768 LONGEST str_offset)
19769 {
19770 dwarf2_read_section (objfile, &dwz->str);
19771
19772 if (dwz->str.buffer == NULL)
19773 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19774 "section [in module %s]"),
19775 bfd_get_filename (dwz->dwz_bfd.get ()));
19776 if (str_offset >= dwz->str.size)
19777 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19778 ".debug_str section [in module %s]"),
19779 bfd_get_filename (dwz->dwz_bfd.get ()));
19780 gdb_assert (HOST_CHAR_BIT == 8);
19781 if (dwz->str.buffer[str_offset] == '\0')
19782 return NULL;
19783 return (const char *) (dwz->str.buffer + str_offset);
19784 }
19785
19786 /* Return pointer to string at .debug_str offset as read from BUF.
19787 BUF is assumed to be in a compilation unit described by CU_HEADER.
19788 Return *BYTES_READ_PTR count of bytes read from BUF. */
19789
19790 static const char *
19791 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19792 const gdb_byte *buf,
19793 const struct comp_unit_head *cu_header,
19794 unsigned int *bytes_read_ptr)
19795 {
19796 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19797
19798 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19799 }
19800
19801 /* Return pointer to string at .debug_line_str offset as read from BUF.
19802 BUF is assumed to be in a compilation unit described by CU_HEADER.
19803 Return *BYTES_READ_PTR count of bytes read from BUF. */
19804
19805 static const char *
19806 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19807 bfd *abfd, const gdb_byte *buf,
19808 const struct comp_unit_head *cu_header,
19809 unsigned int *bytes_read_ptr)
19810 {
19811 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19812
19813 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19814 str_offset);
19815 }
19816
19817 ULONGEST
19818 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19819 unsigned int *bytes_read_ptr)
19820 {
19821 ULONGEST result;
19822 unsigned int num_read;
19823 int shift;
19824 unsigned char byte;
19825
19826 result = 0;
19827 shift = 0;
19828 num_read = 0;
19829 while (1)
19830 {
19831 byte = bfd_get_8 (abfd, buf);
19832 buf++;
19833 num_read++;
19834 result |= ((ULONGEST) (byte & 127) << shift);
19835 if ((byte & 128) == 0)
19836 {
19837 break;
19838 }
19839 shift += 7;
19840 }
19841 *bytes_read_ptr = num_read;
19842 return result;
19843 }
19844
19845 static LONGEST
19846 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19847 unsigned int *bytes_read_ptr)
19848 {
19849 ULONGEST result;
19850 int shift, num_read;
19851 unsigned char byte;
19852
19853 result = 0;
19854 shift = 0;
19855 num_read = 0;
19856 while (1)
19857 {
19858 byte = bfd_get_8 (abfd, buf);
19859 buf++;
19860 num_read++;
19861 result |= ((ULONGEST) (byte & 127) << shift);
19862 shift += 7;
19863 if ((byte & 128) == 0)
19864 {
19865 break;
19866 }
19867 }
19868 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19869 result |= -(((ULONGEST) 1) << shift);
19870 *bytes_read_ptr = num_read;
19871 return result;
19872 }
19873
19874 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19875 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19876 ADDR_SIZE is the size of addresses from the CU header. */
19877
19878 static CORE_ADDR
19879 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19880 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19881 {
19882 struct objfile *objfile = dwarf2_per_objfile->objfile;
19883 bfd *abfd = objfile->obfd;
19884 const gdb_byte *info_ptr;
19885
19886 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19887 if (dwarf2_per_objfile->addr.buffer == NULL)
19888 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19889 objfile_name (objfile));
19890 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19891 error (_("DW_FORM_addr_index pointing outside of "
19892 ".debug_addr section [in module %s]"),
19893 objfile_name (objfile));
19894 info_ptr = (dwarf2_per_objfile->addr.buffer
19895 + addr_base + addr_index * addr_size);
19896 if (addr_size == 4)
19897 return bfd_get_32 (abfd, info_ptr);
19898 else
19899 return bfd_get_64 (abfd, info_ptr);
19900 }
19901
19902 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19903
19904 static CORE_ADDR
19905 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19906 {
19907 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19908 cu->addr_base, cu->header.addr_size);
19909 }
19910
19911 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19912
19913 static CORE_ADDR
19914 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19915 unsigned int *bytes_read)
19916 {
19917 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19918 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19919
19920 return read_addr_index (cu, addr_index);
19921 }
19922
19923 /* Data structure to pass results from dwarf2_read_addr_index_reader
19924 back to dwarf2_read_addr_index. */
19925
19926 struct dwarf2_read_addr_index_data
19927 {
19928 ULONGEST addr_base;
19929 int addr_size;
19930 };
19931
19932 /* die_reader_func for dwarf2_read_addr_index. */
19933
19934 static void
19935 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19936 const gdb_byte *info_ptr,
19937 struct die_info *comp_unit_die,
19938 int has_children,
19939 void *data)
19940 {
19941 struct dwarf2_cu *cu = reader->cu;
19942 struct dwarf2_read_addr_index_data *aidata =
19943 (struct dwarf2_read_addr_index_data *) data;
19944
19945 aidata->addr_base = cu->addr_base;
19946 aidata->addr_size = cu->header.addr_size;
19947 }
19948
19949 /* Given an index in .debug_addr, fetch the value.
19950 NOTE: This can be called during dwarf expression evaluation,
19951 long after the debug information has been read, and thus per_cu->cu
19952 may no longer exist. */
19953
19954 CORE_ADDR
19955 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19956 unsigned int addr_index)
19957 {
19958 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19959 struct dwarf2_cu *cu = per_cu->cu;
19960 ULONGEST addr_base;
19961 int addr_size;
19962
19963 /* We need addr_base and addr_size.
19964 If we don't have PER_CU->cu, we have to get it.
19965 Nasty, but the alternative is storing the needed info in PER_CU,
19966 which at this point doesn't seem justified: it's not clear how frequently
19967 it would get used and it would increase the size of every PER_CU.
19968 Entry points like dwarf2_per_cu_addr_size do a similar thing
19969 so we're not in uncharted territory here.
19970 Alas we need to be a bit more complicated as addr_base is contained
19971 in the DIE.
19972
19973 We don't need to read the entire CU(/TU).
19974 We just need the header and top level die.
19975
19976 IWBN to use the aging mechanism to let us lazily later discard the CU.
19977 For now we skip this optimization. */
19978
19979 if (cu != NULL)
19980 {
19981 addr_base = cu->addr_base;
19982 addr_size = cu->header.addr_size;
19983 }
19984 else
19985 {
19986 struct dwarf2_read_addr_index_data aidata;
19987
19988 /* Note: We can't use init_cutu_and_read_dies_simple here,
19989 we need addr_base. */
19990 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
19991 dwarf2_read_addr_index_reader, &aidata);
19992 addr_base = aidata.addr_base;
19993 addr_size = aidata.addr_size;
19994 }
19995
19996 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19997 addr_size);
19998 }
19999
20000 /* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
20001 This is only used by the Fission support. */
20002
20003 static const char *
20004 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20005 {
20006 struct dwarf2_cu *cu = reader->cu;
20007 struct dwarf2_per_objfile *dwarf2_per_objfile
20008 = cu->per_cu->dwarf2_per_objfile;
20009 struct objfile *objfile = dwarf2_per_objfile->objfile;
20010 const char *objf_name = objfile_name (objfile);
20011 bfd *abfd = objfile->obfd;
20012 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20013 struct dwarf2_section_info *str_offsets_section =
20014 &reader->dwo_file->sections.str_offsets;
20015 const gdb_byte *info_ptr;
20016 ULONGEST str_offset;
20017 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20018
20019 dwarf2_read_section (objfile, str_section);
20020 dwarf2_read_section (objfile, str_offsets_section);
20021 if (str_section->buffer == NULL)
20022 error (_("%s used without .debug_str.dwo section"
20023 " in CU at offset %s [in module %s]"),
20024 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20025 if (str_offsets_section->buffer == NULL)
20026 error (_("%s used without .debug_str_offsets.dwo section"
20027 " in CU at offset %s [in module %s]"),
20028 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20029 if (str_index * cu->header.offset_size >= str_offsets_section->size)
20030 error (_("%s pointing outside of .debug_str_offsets.dwo"
20031 " section in CU at offset %s [in module %s]"),
20032 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20033 info_ptr = (str_offsets_section->buffer
20034 + str_index * cu->header.offset_size);
20035 if (cu->header.offset_size == 4)
20036 str_offset = bfd_get_32 (abfd, info_ptr);
20037 else
20038 str_offset = bfd_get_64 (abfd, info_ptr);
20039 if (str_offset >= str_section->size)
20040 error (_("Offset from %s pointing outside of"
20041 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20042 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20043 return (const char *) (str_section->buffer + str_offset);
20044 }
20045
20046 /* Return the length of an LEB128 number in BUF. */
20047
20048 static int
20049 leb128_size (const gdb_byte *buf)
20050 {
20051 const gdb_byte *begin = buf;
20052 gdb_byte byte;
20053
20054 while (1)
20055 {
20056 byte = *buf++;
20057 if ((byte & 128) == 0)
20058 return buf - begin;
20059 }
20060 }
20061
20062 static void
20063 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20064 {
20065 switch (lang)
20066 {
20067 case DW_LANG_C89:
20068 case DW_LANG_C99:
20069 case DW_LANG_C11:
20070 case DW_LANG_C:
20071 case DW_LANG_UPC:
20072 cu->language = language_c;
20073 break;
20074 case DW_LANG_Java:
20075 case DW_LANG_C_plus_plus:
20076 case DW_LANG_C_plus_plus_11:
20077 case DW_LANG_C_plus_plus_14:
20078 cu->language = language_cplus;
20079 break;
20080 case DW_LANG_D:
20081 cu->language = language_d;
20082 break;
20083 case DW_LANG_Fortran77:
20084 case DW_LANG_Fortran90:
20085 case DW_LANG_Fortran95:
20086 case DW_LANG_Fortran03:
20087 case DW_LANG_Fortran08:
20088 cu->language = language_fortran;
20089 break;
20090 case DW_LANG_Go:
20091 cu->language = language_go;
20092 break;
20093 case DW_LANG_Mips_Assembler:
20094 cu->language = language_asm;
20095 break;
20096 case DW_LANG_Ada83:
20097 case DW_LANG_Ada95:
20098 cu->language = language_ada;
20099 break;
20100 case DW_LANG_Modula2:
20101 cu->language = language_m2;
20102 break;
20103 case DW_LANG_Pascal83:
20104 cu->language = language_pascal;
20105 break;
20106 case DW_LANG_ObjC:
20107 cu->language = language_objc;
20108 break;
20109 case DW_LANG_Rust:
20110 case DW_LANG_Rust_old:
20111 cu->language = language_rust;
20112 break;
20113 case DW_LANG_Cobol74:
20114 case DW_LANG_Cobol85:
20115 default:
20116 cu->language = language_minimal;
20117 break;
20118 }
20119 cu->language_defn = language_def (cu->language);
20120 }
20121
20122 /* Return the named attribute or NULL if not there. */
20123
20124 static struct attribute *
20125 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20126 {
20127 for (;;)
20128 {
20129 unsigned int i;
20130 struct attribute *spec = NULL;
20131
20132 for (i = 0; i < die->num_attrs; ++i)
20133 {
20134 if (die->attrs[i].name == name)
20135 return &die->attrs[i];
20136 if (die->attrs[i].name == DW_AT_specification
20137 || die->attrs[i].name == DW_AT_abstract_origin)
20138 spec = &die->attrs[i];
20139 }
20140
20141 if (!spec)
20142 break;
20143
20144 die = follow_die_ref (die, spec, &cu);
20145 }
20146
20147 return NULL;
20148 }
20149
20150 /* Return the named attribute or NULL if not there,
20151 but do not follow DW_AT_specification, etc.
20152 This is for use in contexts where we're reading .debug_types dies.
20153 Following DW_AT_specification, DW_AT_abstract_origin will take us
20154 back up the chain, and we want to go down. */
20155
20156 static struct attribute *
20157 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20158 {
20159 unsigned int i;
20160
20161 for (i = 0; i < die->num_attrs; ++i)
20162 if (die->attrs[i].name == name)
20163 return &die->attrs[i];
20164
20165 return NULL;
20166 }
20167
20168 /* Return the string associated with a string-typed attribute, or NULL if it
20169 is either not found or is of an incorrect type. */
20170
20171 static const char *
20172 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20173 {
20174 struct attribute *attr;
20175 const char *str = NULL;
20176
20177 attr = dwarf2_attr (die, name, cu);
20178
20179 if (attr != NULL)
20180 {
20181 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20182 || attr->form == DW_FORM_string
20183 || attr->form == DW_FORM_strx
20184 || attr->form == DW_FORM_strx1
20185 || attr->form == DW_FORM_strx2
20186 || attr->form == DW_FORM_strx3
20187 || attr->form == DW_FORM_strx4
20188 || attr->form == DW_FORM_GNU_str_index
20189 || attr->form == DW_FORM_GNU_strp_alt)
20190 str = DW_STRING (attr);
20191 else
20192 complaint (_("string type expected for attribute %s for "
20193 "DIE at %s in module %s"),
20194 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20195 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20196 }
20197
20198 return str;
20199 }
20200
20201 /* Return the dwo name or NULL if not present. If present, it is in either
20202 DW_AT_GNU_dwo_name or DW_AT_dwo_name atrribute. */
20203 static const char *
20204 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20205 {
20206 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20207 if (dwo_name == nullptr)
20208 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20209 return dwo_name;
20210 }
20211
20212 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20213 and holds a non-zero value. This function should only be used for
20214 DW_FORM_flag or DW_FORM_flag_present attributes. */
20215
20216 static int
20217 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20218 {
20219 struct attribute *attr = dwarf2_attr (die, name, cu);
20220
20221 return (attr && DW_UNSND (attr));
20222 }
20223
20224 static int
20225 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20226 {
20227 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20228 which value is non-zero. However, we have to be careful with
20229 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20230 (via dwarf2_flag_true_p) follows this attribute. So we may
20231 end up accidently finding a declaration attribute that belongs
20232 to a different DIE referenced by the specification attribute,
20233 even though the given DIE does not have a declaration attribute. */
20234 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20235 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20236 }
20237
20238 /* Return the die giving the specification for DIE, if there is
20239 one. *SPEC_CU is the CU containing DIE on input, and the CU
20240 containing the return value on output. If there is no
20241 specification, but there is an abstract origin, that is
20242 returned. */
20243
20244 static struct die_info *
20245 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20246 {
20247 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20248 *spec_cu);
20249
20250 if (spec_attr == NULL)
20251 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20252
20253 if (spec_attr == NULL)
20254 return NULL;
20255 else
20256 return follow_die_ref (die, spec_attr, spec_cu);
20257 }
20258
20259 /* Stub for free_line_header to match void * callback types. */
20260
20261 static void
20262 free_line_header_voidp (void *arg)
20263 {
20264 struct line_header *lh = (struct line_header *) arg;
20265
20266 delete lh;
20267 }
20268
20269 void
20270 line_header::add_include_dir (const char *include_dir)
20271 {
20272 if (dwarf_line_debug >= 2)
20273 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20274 include_dirs.size () + 1, include_dir);
20275
20276 include_dirs.push_back (include_dir);
20277 }
20278
20279 void
20280 line_header::add_file_name (const char *name,
20281 dir_index d_index,
20282 unsigned int mod_time,
20283 unsigned int length)
20284 {
20285 if (dwarf_line_debug >= 2)
20286 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
20287 (unsigned) file_names.size () + 1, name);
20288
20289 file_names.emplace_back (name, d_index, mod_time, length);
20290 }
20291
20292 /* A convenience function to find the proper .debug_line section for a CU. */
20293
20294 static struct dwarf2_section_info *
20295 get_debug_line_section (struct dwarf2_cu *cu)
20296 {
20297 struct dwarf2_section_info *section;
20298 struct dwarf2_per_objfile *dwarf2_per_objfile
20299 = cu->per_cu->dwarf2_per_objfile;
20300
20301 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20302 DWO file. */
20303 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20304 section = &cu->dwo_unit->dwo_file->sections.line;
20305 else if (cu->per_cu->is_dwz)
20306 {
20307 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20308
20309 section = &dwz->line;
20310 }
20311 else
20312 section = &dwarf2_per_objfile->line;
20313
20314 return section;
20315 }
20316
20317 /* Read directory or file name entry format, starting with byte of
20318 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20319 entries count and the entries themselves in the described entry
20320 format. */
20321
20322 static void
20323 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20324 bfd *abfd, const gdb_byte **bufp,
20325 struct line_header *lh,
20326 const struct comp_unit_head *cu_header,
20327 void (*callback) (struct line_header *lh,
20328 const char *name,
20329 dir_index d_index,
20330 unsigned int mod_time,
20331 unsigned int length))
20332 {
20333 gdb_byte format_count, formati;
20334 ULONGEST data_count, datai;
20335 const gdb_byte *buf = *bufp;
20336 const gdb_byte *format_header_data;
20337 unsigned int bytes_read;
20338
20339 format_count = read_1_byte (abfd, buf);
20340 buf += 1;
20341 format_header_data = buf;
20342 for (formati = 0; formati < format_count; formati++)
20343 {
20344 read_unsigned_leb128 (abfd, buf, &bytes_read);
20345 buf += bytes_read;
20346 read_unsigned_leb128 (abfd, buf, &bytes_read);
20347 buf += bytes_read;
20348 }
20349
20350 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20351 buf += bytes_read;
20352 for (datai = 0; datai < data_count; datai++)
20353 {
20354 const gdb_byte *format = format_header_data;
20355 struct file_entry fe;
20356
20357 for (formati = 0; formati < format_count; formati++)
20358 {
20359 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20360 format += bytes_read;
20361
20362 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20363 format += bytes_read;
20364
20365 gdb::optional<const char *> string;
20366 gdb::optional<unsigned int> uint;
20367
20368 switch (form)
20369 {
20370 case DW_FORM_string:
20371 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20372 buf += bytes_read;
20373 break;
20374
20375 case DW_FORM_line_strp:
20376 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20377 abfd, buf,
20378 cu_header,
20379 &bytes_read));
20380 buf += bytes_read;
20381 break;
20382
20383 case DW_FORM_data1:
20384 uint.emplace (read_1_byte (abfd, buf));
20385 buf += 1;
20386 break;
20387
20388 case DW_FORM_data2:
20389 uint.emplace (read_2_bytes (abfd, buf));
20390 buf += 2;
20391 break;
20392
20393 case DW_FORM_data4:
20394 uint.emplace (read_4_bytes (abfd, buf));
20395 buf += 4;
20396 break;
20397
20398 case DW_FORM_data8:
20399 uint.emplace (read_8_bytes (abfd, buf));
20400 buf += 8;
20401 break;
20402
20403 case DW_FORM_udata:
20404 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20405 buf += bytes_read;
20406 break;
20407
20408 case DW_FORM_block:
20409 /* It is valid only for DW_LNCT_timestamp which is ignored by
20410 current GDB. */
20411 break;
20412 }
20413
20414 switch (content_type)
20415 {
20416 case DW_LNCT_path:
20417 if (string.has_value ())
20418 fe.name = *string;
20419 break;
20420 case DW_LNCT_directory_index:
20421 if (uint.has_value ())
20422 fe.d_index = (dir_index) *uint;
20423 break;
20424 case DW_LNCT_timestamp:
20425 if (uint.has_value ())
20426 fe.mod_time = *uint;
20427 break;
20428 case DW_LNCT_size:
20429 if (uint.has_value ())
20430 fe.length = *uint;
20431 break;
20432 case DW_LNCT_MD5:
20433 break;
20434 default:
20435 complaint (_("Unknown format content type %s"),
20436 pulongest (content_type));
20437 }
20438 }
20439
20440 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20441 }
20442
20443 *bufp = buf;
20444 }
20445
20446 /* Read the statement program header starting at OFFSET in
20447 .debug_line, or .debug_line.dwo. Return a pointer
20448 to a struct line_header, allocated using xmalloc.
20449 Returns NULL if there is a problem reading the header, e.g., if it
20450 has a version we don't understand.
20451
20452 NOTE: the strings in the include directory and file name tables of
20453 the returned object point into the dwarf line section buffer,
20454 and must not be freed. */
20455
20456 static line_header_up
20457 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20458 {
20459 const gdb_byte *line_ptr;
20460 unsigned int bytes_read, offset_size;
20461 int i;
20462 const char *cur_dir, *cur_file;
20463 struct dwarf2_section_info *section;
20464 bfd *abfd;
20465 struct dwarf2_per_objfile *dwarf2_per_objfile
20466 = cu->per_cu->dwarf2_per_objfile;
20467
20468 section = get_debug_line_section (cu);
20469 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20470 if (section->buffer == NULL)
20471 {
20472 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20473 complaint (_("missing .debug_line.dwo section"));
20474 else
20475 complaint (_("missing .debug_line section"));
20476 return 0;
20477 }
20478
20479 /* We can't do this until we know the section is non-empty.
20480 Only then do we know we have such a section. */
20481 abfd = get_section_bfd_owner (section);
20482
20483 /* Make sure that at least there's room for the total_length field.
20484 That could be 12 bytes long, but we're just going to fudge that. */
20485 if (to_underlying (sect_off) + 4 >= section->size)
20486 {
20487 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20488 return 0;
20489 }
20490
20491 line_header_up lh (new line_header ());
20492
20493 lh->sect_off = sect_off;
20494 lh->offset_in_dwz = cu->per_cu->is_dwz;
20495
20496 line_ptr = section->buffer + to_underlying (sect_off);
20497
20498 /* Read in the header. */
20499 lh->total_length =
20500 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20501 &bytes_read, &offset_size);
20502 line_ptr += bytes_read;
20503 if (line_ptr + lh->total_length > (section->buffer + section->size))
20504 {
20505 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20506 return 0;
20507 }
20508 lh->statement_program_end = line_ptr + lh->total_length;
20509 lh->version = read_2_bytes (abfd, line_ptr);
20510 line_ptr += 2;
20511 if (lh->version > 5)
20512 {
20513 /* This is a version we don't understand. The format could have
20514 changed in ways we don't handle properly so just punt. */
20515 complaint (_("unsupported version in .debug_line section"));
20516 return NULL;
20517 }
20518 if (lh->version >= 5)
20519 {
20520 gdb_byte segment_selector_size;
20521
20522 /* Skip address size. */
20523 read_1_byte (abfd, line_ptr);
20524 line_ptr += 1;
20525
20526 segment_selector_size = read_1_byte (abfd, line_ptr);
20527 line_ptr += 1;
20528 if (segment_selector_size != 0)
20529 {
20530 complaint (_("unsupported segment selector size %u "
20531 "in .debug_line section"),
20532 segment_selector_size);
20533 return NULL;
20534 }
20535 }
20536 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20537 line_ptr += offset_size;
20538 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20539 line_ptr += 1;
20540 if (lh->version >= 4)
20541 {
20542 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20543 line_ptr += 1;
20544 }
20545 else
20546 lh->maximum_ops_per_instruction = 1;
20547
20548 if (lh->maximum_ops_per_instruction == 0)
20549 {
20550 lh->maximum_ops_per_instruction = 1;
20551 complaint (_("invalid maximum_ops_per_instruction "
20552 "in `.debug_line' section"));
20553 }
20554
20555 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20556 line_ptr += 1;
20557 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20558 line_ptr += 1;
20559 lh->line_range = read_1_byte (abfd, line_ptr);
20560 line_ptr += 1;
20561 lh->opcode_base = read_1_byte (abfd, line_ptr);
20562 line_ptr += 1;
20563 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20564
20565 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20566 for (i = 1; i < lh->opcode_base; ++i)
20567 {
20568 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20569 line_ptr += 1;
20570 }
20571
20572 if (lh->version >= 5)
20573 {
20574 /* Read directory table. */
20575 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20576 &cu->header,
20577 [] (struct line_header *header, const char *name,
20578 dir_index d_index, unsigned int mod_time,
20579 unsigned int length)
20580 {
20581 header->add_include_dir (name);
20582 });
20583
20584 /* Read file name table. */
20585 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20586 &cu->header,
20587 [] (struct line_header *header, const char *name,
20588 dir_index d_index, unsigned int mod_time,
20589 unsigned int length)
20590 {
20591 header->add_file_name (name, d_index, mod_time, length);
20592 });
20593 }
20594 else
20595 {
20596 /* Read directory table. */
20597 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20598 {
20599 line_ptr += bytes_read;
20600 lh->add_include_dir (cur_dir);
20601 }
20602 line_ptr += bytes_read;
20603
20604 /* Read file name table. */
20605 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20606 {
20607 unsigned int mod_time, length;
20608 dir_index d_index;
20609
20610 line_ptr += bytes_read;
20611 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20612 line_ptr += bytes_read;
20613 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20614 line_ptr += bytes_read;
20615 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20616 line_ptr += bytes_read;
20617
20618 lh->add_file_name (cur_file, d_index, mod_time, length);
20619 }
20620 line_ptr += bytes_read;
20621 }
20622 lh->statement_program_start = line_ptr;
20623
20624 if (line_ptr > (section->buffer + section->size))
20625 complaint (_("line number info header doesn't "
20626 "fit in `.debug_line' section"));
20627
20628 return lh;
20629 }
20630
20631 /* Subroutine of dwarf_decode_lines to simplify it.
20632 Return the file name of the psymtab for included file FILE_INDEX
20633 in line header LH of PST.
20634 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20635 If space for the result is malloc'd, *NAME_HOLDER will be set.
20636 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20637
20638 static const char *
20639 psymtab_include_file_name (const struct line_header *lh, int file_index,
20640 const struct partial_symtab *pst,
20641 const char *comp_dir,
20642 gdb::unique_xmalloc_ptr<char> *name_holder)
20643 {
20644 const file_entry &fe = lh->file_names[file_index];
20645 const char *include_name = fe.name;
20646 const char *include_name_to_compare = include_name;
20647 const char *pst_filename;
20648 int file_is_pst;
20649
20650 const char *dir_name = fe.include_dir (lh);
20651
20652 gdb::unique_xmalloc_ptr<char> hold_compare;
20653 if (!IS_ABSOLUTE_PATH (include_name)
20654 && (dir_name != NULL || comp_dir != NULL))
20655 {
20656 /* Avoid creating a duplicate psymtab for PST.
20657 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20658 Before we do the comparison, however, we need to account
20659 for DIR_NAME and COMP_DIR.
20660 First prepend dir_name (if non-NULL). If we still don't
20661 have an absolute path prepend comp_dir (if non-NULL).
20662 However, the directory we record in the include-file's
20663 psymtab does not contain COMP_DIR (to match the
20664 corresponding symtab(s)).
20665
20666 Example:
20667
20668 bash$ cd /tmp
20669 bash$ gcc -g ./hello.c
20670 include_name = "hello.c"
20671 dir_name = "."
20672 DW_AT_comp_dir = comp_dir = "/tmp"
20673 DW_AT_name = "./hello.c"
20674
20675 */
20676
20677 if (dir_name != NULL)
20678 {
20679 name_holder->reset (concat (dir_name, SLASH_STRING,
20680 include_name, (char *) NULL));
20681 include_name = name_holder->get ();
20682 include_name_to_compare = include_name;
20683 }
20684 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20685 {
20686 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20687 include_name, (char *) NULL));
20688 include_name_to_compare = hold_compare.get ();
20689 }
20690 }
20691
20692 pst_filename = pst->filename;
20693 gdb::unique_xmalloc_ptr<char> copied_name;
20694 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20695 {
20696 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20697 pst_filename, (char *) NULL));
20698 pst_filename = copied_name.get ();
20699 }
20700
20701 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20702
20703 if (file_is_pst)
20704 return NULL;
20705 return include_name;
20706 }
20707
20708 /* State machine to track the state of the line number program. */
20709
20710 class lnp_state_machine
20711 {
20712 public:
20713 /* Initialize a machine state for the start of a line number
20714 program. */
20715 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20716 bool record_lines_p);
20717
20718 file_entry *current_file ()
20719 {
20720 /* lh->file_names is 0-based, but the file name numbers in the
20721 statement program are 1-based. */
20722 return m_line_header->file_name_at (m_file);
20723 }
20724
20725 /* Record the line in the state machine. END_SEQUENCE is true if
20726 we're processing the end of a sequence. */
20727 void record_line (bool end_sequence);
20728
20729 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20730 nop-out rest of the lines in this sequence. */
20731 void check_line_address (struct dwarf2_cu *cu,
20732 const gdb_byte *line_ptr,
20733 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20734
20735 void handle_set_discriminator (unsigned int discriminator)
20736 {
20737 m_discriminator = discriminator;
20738 m_line_has_non_zero_discriminator |= discriminator != 0;
20739 }
20740
20741 /* Handle DW_LNE_set_address. */
20742 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20743 {
20744 m_op_index = 0;
20745 address += baseaddr;
20746 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20747 }
20748
20749 /* Handle DW_LNS_advance_pc. */
20750 void handle_advance_pc (CORE_ADDR adjust);
20751
20752 /* Handle a special opcode. */
20753 void handle_special_opcode (unsigned char op_code);
20754
20755 /* Handle DW_LNS_advance_line. */
20756 void handle_advance_line (int line_delta)
20757 {
20758 advance_line (line_delta);
20759 }
20760
20761 /* Handle DW_LNS_set_file. */
20762 void handle_set_file (file_name_index file);
20763
20764 /* Handle DW_LNS_negate_stmt. */
20765 void handle_negate_stmt ()
20766 {
20767 m_is_stmt = !m_is_stmt;
20768 }
20769
20770 /* Handle DW_LNS_const_add_pc. */
20771 void handle_const_add_pc ();
20772
20773 /* Handle DW_LNS_fixed_advance_pc. */
20774 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20775 {
20776 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20777 m_op_index = 0;
20778 }
20779
20780 /* Handle DW_LNS_copy. */
20781 void handle_copy ()
20782 {
20783 record_line (false);
20784 m_discriminator = 0;
20785 }
20786
20787 /* Handle DW_LNE_end_sequence. */
20788 void handle_end_sequence ()
20789 {
20790 m_currently_recording_lines = true;
20791 }
20792
20793 private:
20794 /* Advance the line by LINE_DELTA. */
20795 void advance_line (int line_delta)
20796 {
20797 m_line += line_delta;
20798
20799 if (line_delta != 0)
20800 m_line_has_non_zero_discriminator = m_discriminator != 0;
20801 }
20802
20803 struct dwarf2_cu *m_cu;
20804
20805 gdbarch *m_gdbarch;
20806
20807 /* True if we're recording lines.
20808 Otherwise we're building partial symtabs and are just interested in
20809 finding include files mentioned by the line number program. */
20810 bool m_record_lines_p;
20811
20812 /* The line number header. */
20813 line_header *m_line_header;
20814
20815 /* These are part of the standard DWARF line number state machine,
20816 and initialized according to the DWARF spec. */
20817
20818 unsigned char m_op_index = 0;
20819 /* The line table index (1-based) of the current file. */
20820 file_name_index m_file = (file_name_index) 1;
20821 unsigned int m_line = 1;
20822
20823 /* These are initialized in the constructor. */
20824
20825 CORE_ADDR m_address;
20826 bool m_is_stmt;
20827 unsigned int m_discriminator;
20828
20829 /* Additional bits of state we need to track. */
20830
20831 /* The last file that we called dwarf2_start_subfile for.
20832 This is only used for TLLs. */
20833 unsigned int m_last_file = 0;
20834 /* The last file a line number was recorded for. */
20835 struct subfile *m_last_subfile = NULL;
20836
20837 /* When true, record the lines we decode. */
20838 bool m_currently_recording_lines = false;
20839
20840 /* The last line number that was recorded, used to coalesce
20841 consecutive entries for the same line. This can happen, for
20842 example, when discriminators are present. PR 17276. */
20843 unsigned int m_last_line = 0;
20844 bool m_line_has_non_zero_discriminator = false;
20845 };
20846
20847 void
20848 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20849 {
20850 CORE_ADDR addr_adj = (((m_op_index + adjust)
20851 / m_line_header->maximum_ops_per_instruction)
20852 * m_line_header->minimum_instruction_length);
20853 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20854 m_op_index = ((m_op_index + adjust)
20855 % m_line_header->maximum_ops_per_instruction);
20856 }
20857
20858 void
20859 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20860 {
20861 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20862 CORE_ADDR addr_adj = (((m_op_index
20863 + (adj_opcode / m_line_header->line_range))
20864 / m_line_header->maximum_ops_per_instruction)
20865 * m_line_header->minimum_instruction_length);
20866 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20867 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20868 % m_line_header->maximum_ops_per_instruction);
20869
20870 int line_delta = (m_line_header->line_base
20871 + (adj_opcode % m_line_header->line_range));
20872 advance_line (line_delta);
20873 record_line (false);
20874 m_discriminator = 0;
20875 }
20876
20877 void
20878 lnp_state_machine::handle_set_file (file_name_index file)
20879 {
20880 m_file = file;
20881
20882 const file_entry *fe = current_file ();
20883 if (fe == NULL)
20884 dwarf2_debug_line_missing_file_complaint ();
20885 else if (m_record_lines_p)
20886 {
20887 const char *dir = fe->include_dir (m_line_header);
20888
20889 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20890 m_line_has_non_zero_discriminator = m_discriminator != 0;
20891 dwarf2_start_subfile (m_cu, fe->name, dir);
20892 }
20893 }
20894
20895 void
20896 lnp_state_machine::handle_const_add_pc ()
20897 {
20898 CORE_ADDR adjust
20899 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20900
20901 CORE_ADDR addr_adj
20902 = (((m_op_index + adjust)
20903 / m_line_header->maximum_ops_per_instruction)
20904 * m_line_header->minimum_instruction_length);
20905
20906 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20907 m_op_index = ((m_op_index + adjust)
20908 % m_line_header->maximum_ops_per_instruction);
20909 }
20910
20911 /* Return non-zero if we should add LINE to the line number table.
20912 LINE is the line to add, LAST_LINE is the last line that was added,
20913 LAST_SUBFILE is the subfile for LAST_LINE.
20914 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20915 had a non-zero discriminator.
20916
20917 We have to be careful in the presence of discriminators.
20918 E.g., for this line:
20919
20920 for (i = 0; i < 100000; i++);
20921
20922 clang can emit four line number entries for that one line,
20923 each with a different discriminator.
20924 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20925
20926 However, we want gdb to coalesce all four entries into one.
20927 Otherwise the user could stepi into the middle of the line and
20928 gdb would get confused about whether the pc really was in the
20929 middle of the line.
20930
20931 Things are further complicated by the fact that two consecutive
20932 line number entries for the same line is a heuristic used by gcc
20933 to denote the end of the prologue. So we can't just discard duplicate
20934 entries, we have to be selective about it. The heuristic we use is
20935 that we only collapse consecutive entries for the same line if at least
20936 one of those entries has a non-zero discriminator. PR 17276.
20937
20938 Note: Addresses in the line number state machine can never go backwards
20939 within one sequence, thus this coalescing is ok. */
20940
20941 static int
20942 dwarf_record_line_p (struct dwarf2_cu *cu,
20943 unsigned int line, unsigned int last_line,
20944 int line_has_non_zero_discriminator,
20945 struct subfile *last_subfile)
20946 {
20947 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20948 return 1;
20949 if (line != last_line)
20950 return 1;
20951 /* Same line for the same file that we've seen already.
20952 As a last check, for pr 17276, only record the line if the line
20953 has never had a non-zero discriminator. */
20954 if (!line_has_non_zero_discriminator)
20955 return 1;
20956 return 0;
20957 }
20958
20959 /* Use the CU's builder to record line number LINE beginning at
20960 address ADDRESS in the line table of subfile SUBFILE. */
20961
20962 static void
20963 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20964 unsigned int line, CORE_ADDR address,
20965 struct dwarf2_cu *cu)
20966 {
20967 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20968
20969 if (dwarf_line_debug)
20970 {
20971 fprintf_unfiltered (gdb_stdlog,
20972 "Recording line %u, file %s, address %s\n",
20973 line, lbasename (subfile->name),
20974 paddress (gdbarch, address));
20975 }
20976
20977 if (cu != nullptr)
20978 cu->get_builder ()->record_line (subfile, line, addr);
20979 }
20980
20981 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20982 Mark the end of a set of line number records.
20983 The arguments are the same as for dwarf_record_line_1.
20984 If SUBFILE is NULL the request is ignored. */
20985
20986 static void
20987 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20988 CORE_ADDR address, struct dwarf2_cu *cu)
20989 {
20990 if (subfile == NULL)
20991 return;
20992
20993 if (dwarf_line_debug)
20994 {
20995 fprintf_unfiltered (gdb_stdlog,
20996 "Finishing current line, file %s, address %s\n",
20997 lbasename (subfile->name),
20998 paddress (gdbarch, address));
20999 }
21000
21001 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
21002 }
21003
21004 void
21005 lnp_state_machine::record_line (bool end_sequence)
21006 {
21007 if (dwarf_line_debug)
21008 {
21009 fprintf_unfiltered (gdb_stdlog,
21010 "Processing actual line %u: file %u,"
21011 " address %s, is_stmt %u, discrim %u\n",
21012 m_line, to_underlying (m_file),
21013 paddress (m_gdbarch, m_address),
21014 m_is_stmt, m_discriminator);
21015 }
21016
21017 file_entry *fe = current_file ();
21018
21019 if (fe == NULL)
21020 dwarf2_debug_line_missing_file_complaint ();
21021 /* For now we ignore lines not starting on an instruction boundary.
21022 But not when processing end_sequence for compatibility with the
21023 previous version of the code. */
21024 else if (m_op_index == 0 || end_sequence)
21025 {
21026 fe->included_p = 1;
21027 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
21028 {
21029 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
21030 || end_sequence)
21031 {
21032 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21033 m_currently_recording_lines ? m_cu : nullptr);
21034 }
21035
21036 if (!end_sequence)
21037 {
21038 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21039 m_line_has_non_zero_discriminator,
21040 m_last_subfile))
21041 {
21042 buildsym_compunit *builder = m_cu->get_builder ();
21043 dwarf_record_line_1 (m_gdbarch,
21044 builder->get_current_subfile (),
21045 m_line, m_address,
21046 m_currently_recording_lines ? m_cu : nullptr);
21047 }
21048 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21049 m_last_line = m_line;
21050 }
21051 }
21052 }
21053 }
21054
21055 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21056 line_header *lh, bool record_lines_p)
21057 {
21058 m_cu = cu;
21059 m_gdbarch = arch;
21060 m_record_lines_p = record_lines_p;
21061 m_line_header = lh;
21062
21063 m_currently_recording_lines = true;
21064
21065 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21066 was a line entry for it so that the backend has a chance to adjust it
21067 and also record it in case it needs it. This is currently used by MIPS
21068 code, cf. `mips_adjust_dwarf2_line'. */
21069 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21070 m_is_stmt = lh->default_is_stmt;
21071 m_discriminator = 0;
21072 }
21073
21074 void
21075 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21076 const gdb_byte *line_ptr,
21077 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21078 {
21079 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21080 the pc range of the CU. However, we restrict the test to only ADDRESS
21081 values of zero to preserve GDB's previous behaviour which is to handle
21082 the specific case of a function being GC'd by the linker. */
21083
21084 if (address == 0 && address < unrelocated_lowpc)
21085 {
21086 /* This line table is for a function which has been
21087 GCd by the linker. Ignore it. PR gdb/12528 */
21088
21089 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21090 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21091
21092 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21093 line_offset, objfile_name (objfile));
21094 m_currently_recording_lines = false;
21095 /* Note: m_currently_recording_lines is left as false until we see
21096 DW_LNE_end_sequence. */
21097 }
21098 }
21099
21100 /* Subroutine of dwarf_decode_lines to simplify it.
21101 Process the line number information in LH.
21102 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21103 program in order to set included_p for every referenced header. */
21104
21105 static void
21106 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21107 const int decode_for_pst_p, CORE_ADDR lowpc)
21108 {
21109 const gdb_byte *line_ptr, *extended_end;
21110 const gdb_byte *line_end;
21111 unsigned int bytes_read, extended_len;
21112 unsigned char op_code, extended_op;
21113 CORE_ADDR baseaddr;
21114 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21115 bfd *abfd = objfile->obfd;
21116 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21117 /* True if we're recording line info (as opposed to building partial
21118 symtabs and just interested in finding include files mentioned by
21119 the line number program). */
21120 bool record_lines_p = !decode_for_pst_p;
21121
21122 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21123
21124 line_ptr = lh->statement_program_start;
21125 line_end = lh->statement_program_end;
21126
21127 /* Read the statement sequences until there's nothing left. */
21128 while (line_ptr < line_end)
21129 {
21130 /* The DWARF line number program state machine. Reset the state
21131 machine at the start of each sequence. */
21132 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21133 bool end_sequence = false;
21134
21135 if (record_lines_p)
21136 {
21137 /* Start a subfile for the current file of the state
21138 machine. */
21139 const file_entry *fe = state_machine.current_file ();
21140
21141 if (fe != NULL)
21142 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21143 }
21144
21145 /* Decode the table. */
21146 while (line_ptr < line_end && !end_sequence)
21147 {
21148 op_code = read_1_byte (abfd, line_ptr);
21149 line_ptr += 1;
21150
21151 if (op_code >= lh->opcode_base)
21152 {
21153 /* Special opcode. */
21154 state_machine.handle_special_opcode (op_code);
21155 }
21156 else switch (op_code)
21157 {
21158 case DW_LNS_extended_op:
21159 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21160 &bytes_read);
21161 line_ptr += bytes_read;
21162 extended_end = line_ptr + extended_len;
21163 extended_op = read_1_byte (abfd, line_ptr);
21164 line_ptr += 1;
21165 switch (extended_op)
21166 {
21167 case DW_LNE_end_sequence:
21168 state_machine.handle_end_sequence ();
21169 end_sequence = true;
21170 break;
21171 case DW_LNE_set_address:
21172 {
21173 CORE_ADDR address
21174 = read_address (abfd, line_ptr, cu, &bytes_read);
21175 line_ptr += bytes_read;
21176
21177 state_machine.check_line_address (cu, line_ptr,
21178 lowpc - baseaddr, address);
21179 state_machine.handle_set_address (baseaddr, address);
21180 }
21181 break;
21182 case DW_LNE_define_file:
21183 {
21184 const char *cur_file;
21185 unsigned int mod_time, length;
21186 dir_index dindex;
21187
21188 cur_file = read_direct_string (abfd, line_ptr,
21189 &bytes_read);
21190 line_ptr += bytes_read;
21191 dindex = (dir_index)
21192 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21193 line_ptr += bytes_read;
21194 mod_time =
21195 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21196 line_ptr += bytes_read;
21197 length =
21198 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21199 line_ptr += bytes_read;
21200 lh->add_file_name (cur_file, dindex, mod_time, length);
21201 }
21202 break;
21203 case DW_LNE_set_discriminator:
21204 {
21205 /* The discriminator is not interesting to the
21206 debugger; just ignore it. We still need to
21207 check its value though:
21208 if there are consecutive entries for the same
21209 (non-prologue) line we want to coalesce them.
21210 PR 17276. */
21211 unsigned int discr
21212 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21213 line_ptr += bytes_read;
21214
21215 state_machine.handle_set_discriminator (discr);
21216 }
21217 break;
21218 default:
21219 complaint (_("mangled .debug_line section"));
21220 return;
21221 }
21222 /* Make sure that we parsed the extended op correctly. If e.g.
21223 we expected a different address size than the producer used,
21224 we may have read the wrong number of bytes. */
21225 if (line_ptr != extended_end)
21226 {
21227 complaint (_("mangled .debug_line section"));
21228 return;
21229 }
21230 break;
21231 case DW_LNS_copy:
21232 state_machine.handle_copy ();
21233 break;
21234 case DW_LNS_advance_pc:
21235 {
21236 CORE_ADDR adjust
21237 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21238 line_ptr += bytes_read;
21239
21240 state_machine.handle_advance_pc (adjust);
21241 }
21242 break;
21243 case DW_LNS_advance_line:
21244 {
21245 int line_delta
21246 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21247 line_ptr += bytes_read;
21248
21249 state_machine.handle_advance_line (line_delta);
21250 }
21251 break;
21252 case DW_LNS_set_file:
21253 {
21254 file_name_index file
21255 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21256 &bytes_read);
21257 line_ptr += bytes_read;
21258
21259 state_machine.handle_set_file (file);
21260 }
21261 break;
21262 case DW_LNS_set_column:
21263 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21264 line_ptr += bytes_read;
21265 break;
21266 case DW_LNS_negate_stmt:
21267 state_machine.handle_negate_stmt ();
21268 break;
21269 case DW_LNS_set_basic_block:
21270 break;
21271 /* Add to the address register of the state machine the
21272 address increment value corresponding to special opcode
21273 255. I.e., this value is scaled by the minimum
21274 instruction length since special opcode 255 would have
21275 scaled the increment. */
21276 case DW_LNS_const_add_pc:
21277 state_machine.handle_const_add_pc ();
21278 break;
21279 case DW_LNS_fixed_advance_pc:
21280 {
21281 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21282 line_ptr += 2;
21283
21284 state_machine.handle_fixed_advance_pc (addr_adj);
21285 }
21286 break;
21287 default:
21288 {
21289 /* Unknown standard opcode, ignore it. */
21290 int i;
21291
21292 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21293 {
21294 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21295 line_ptr += bytes_read;
21296 }
21297 }
21298 }
21299 }
21300
21301 if (!end_sequence)
21302 dwarf2_debug_line_missing_end_sequence_complaint ();
21303
21304 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21305 in which case we still finish recording the last line). */
21306 state_machine.record_line (true);
21307 }
21308 }
21309
21310 /* Decode the Line Number Program (LNP) for the given line_header
21311 structure and CU. The actual information extracted and the type
21312 of structures created from the LNP depends on the value of PST.
21313
21314 1. If PST is NULL, then this procedure uses the data from the program
21315 to create all necessary symbol tables, and their linetables.
21316
21317 2. If PST is not NULL, this procedure reads the program to determine
21318 the list of files included by the unit represented by PST, and
21319 builds all the associated partial symbol tables.
21320
21321 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21322 It is used for relative paths in the line table.
21323 NOTE: When processing partial symtabs (pst != NULL),
21324 comp_dir == pst->dirname.
21325
21326 NOTE: It is important that psymtabs have the same file name (via strcmp)
21327 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21328 symtab we don't use it in the name of the psymtabs we create.
21329 E.g. expand_line_sal requires this when finding psymtabs to expand.
21330 A good testcase for this is mb-inline.exp.
21331
21332 LOWPC is the lowest address in CU (or 0 if not known).
21333
21334 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21335 for its PC<->lines mapping information. Otherwise only the filename
21336 table is read in. */
21337
21338 static void
21339 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21340 struct dwarf2_cu *cu, struct partial_symtab *pst,
21341 CORE_ADDR lowpc, int decode_mapping)
21342 {
21343 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21344 const int decode_for_pst_p = (pst != NULL);
21345
21346 if (decode_mapping)
21347 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21348
21349 if (decode_for_pst_p)
21350 {
21351 int file_index;
21352
21353 /* Now that we're done scanning the Line Header Program, we can
21354 create the psymtab of each included file. */
21355 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21356 if (lh->file_names[file_index].included_p == 1)
21357 {
21358 gdb::unique_xmalloc_ptr<char> name_holder;
21359 const char *include_name =
21360 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21361 &name_holder);
21362 if (include_name != NULL)
21363 dwarf2_create_include_psymtab (include_name, pst, objfile);
21364 }
21365 }
21366 else
21367 {
21368 /* Make sure a symtab is created for every file, even files
21369 which contain only variables (i.e. no code with associated
21370 line numbers). */
21371 buildsym_compunit *builder = cu->get_builder ();
21372 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21373 int i;
21374
21375 for (i = 0; i < lh->file_names.size (); i++)
21376 {
21377 file_entry &fe = lh->file_names[i];
21378
21379 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21380
21381 if (builder->get_current_subfile ()->symtab == NULL)
21382 {
21383 builder->get_current_subfile ()->symtab
21384 = allocate_symtab (cust,
21385 builder->get_current_subfile ()->name);
21386 }
21387 fe.symtab = builder->get_current_subfile ()->symtab;
21388 }
21389 }
21390 }
21391
21392 /* Start a subfile for DWARF. FILENAME is the name of the file and
21393 DIRNAME the name of the source directory which contains FILENAME
21394 or NULL if not known.
21395 This routine tries to keep line numbers from identical absolute and
21396 relative file names in a common subfile.
21397
21398 Using the `list' example from the GDB testsuite, which resides in
21399 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21400 of /srcdir/list0.c yields the following debugging information for list0.c:
21401
21402 DW_AT_name: /srcdir/list0.c
21403 DW_AT_comp_dir: /compdir
21404 files.files[0].name: list0.h
21405 files.files[0].dir: /srcdir
21406 files.files[1].name: list0.c
21407 files.files[1].dir: /srcdir
21408
21409 The line number information for list0.c has to end up in a single
21410 subfile, so that `break /srcdir/list0.c:1' works as expected.
21411 start_subfile will ensure that this happens provided that we pass the
21412 concatenation of files.files[1].dir and files.files[1].name as the
21413 subfile's name. */
21414
21415 static void
21416 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21417 const char *dirname)
21418 {
21419 char *copy = NULL;
21420
21421 /* In order not to lose the line information directory,
21422 we concatenate it to the filename when it makes sense.
21423 Note that the Dwarf3 standard says (speaking of filenames in line
21424 information): ``The directory index is ignored for file names
21425 that represent full path names''. Thus ignoring dirname in the
21426 `else' branch below isn't an issue. */
21427
21428 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21429 {
21430 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21431 filename = copy;
21432 }
21433
21434 cu->get_builder ()->start_subfile (filename);
21435
21436 if (copy != NULL)
21437 xfree (copy);
21438 }
21439
21440 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21441 buildsym_compunit constructor. */
21442
21443 struct compunit_symtab *
21444 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21445 CORE_ADDR low_pc)
21446 {
21447 gdb_assert (m_builder == nullptr);
21448
21449 m_builder.reset (new struct buildsym_compunit
21450 (per_cu->dwarf2_per_objfile->objfile,
21451 name, comp_dir, language, low_pc));
21452
21453 list_in_scope = get_builder ()->get_file_symbols ();
21454
21455 get_builder ()->record_debugformat ("DWARF 2");
21456 get_builder ()->record_producer (producer);
21457
21458 processing_has_namespace_info = false;
21459
21460 return get_builder ()->get_compunit_symtab ();
21461 }
21462
21463 static void
21464 var_decode_location (struct attribute *attr, struct symbol *sym,
21465 struct dwarf2_cu *cu)
21466 {
21467 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21468 struct comp_unit_head *cu_header = &cu->header;
21469
21470 /* NOTE drow/2003-01-30: There used to be a comment and some special
21471 code here to turn a symbol with DW_AT_external and a
21472 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21473 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21474 with some versions of binutils) where shared libraries could have
21475 relocations against symbols in their debug information - the
21476 minimal symbol would have the right address, but the debug info
21477 would not. It's no longer necessary, because we will explicitly
21478 apply relocations when we read in the debug information now. */
21479
21480 /* A DW_AT_location attribute with no contents indicates that a
21481 variable has been optimized away. */
21482 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21483 {
21484 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21485 return;
21486 }
21487
21488 /* Handle one degenerate form of location expression specially, to
21489 preserve GDB's previous behavior when section offsets are
21490 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21491 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21492
21493 if (attr_form_is_block (attr)
21494 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21495 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21496 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21497 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21498 && (DW_BLOCK (attr)->size
21499 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21500 {
21501 unsigned int dummy;
21502
21503 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21504 SET_SYMBOL_VALUE_ADDRESS (sym,
21505 read_address (objfile->obfd,
21506 DW_BLOCK (attr)->data + 1,
21507 cu, &dummy));
21508 else
21509 SET_SYMBOL_VALUE_ADDRESS
21510 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21511 &dummy));
21512 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21513 fixup_symbol_section (sym, objfile);
21514 SET_SYMBOL_VALUE_ADDRESS (sym,
21515 SYMBOL_VALUE_ADDRESS (sym)
21516 + ANOFFSET (objfile->section_offsets,
21517 SYMBOL_SECTION (sym)));
21518 return;
21519 }
21520
21521 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21522 expression evaluator, and use LOC_COMPUTED only when necessary
21523 (i.e. when the value of a register or memory location is
21524 referenced, or a thread-local block, etc.). Then again, it might
21525 not be worthwhile. I'm assuming that it isn't unless performance
21526 or memory numbers show me otherwise. */
21527
21528 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21529
21530 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21531 cu->has_loclist = true;
21532 }
21533
21534 /* Given a pointer to a DWARF information entry, figure out if we need
21535 to make a symbol table entry for it, and if so, create a new entry
21536 and return a pointer to it.
21537 If TYPE is NULL, determine symbol type from the die, otherwise
21538 used the passed type.
21539 If SPACE is not NULL, use it to hold the new symbol. If it is
21540 NULL, allocate a new symbol on the objfile's obstack. */
21541
21542 static struct symbol *
21543 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21544 struct symbol *space)
21545 {
21546 struct dwarf2_per_objfile *dwarf2_per_objfile
21547 = cu->per_cu->dwarf2_per_objfile;
21548 struct objfile *objfile = dwarf2_per_objfile->objfile;
21549 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21550 struct symbol *sym = NULL;
21551 const char *name;
21552 struct attribute *attr = NULL;
21553 struct attribute *attr2 = NULL;
21554 CORE_ADDR baseaddr;
21555 struct pending **list_to_add = NULL;
21556
21557 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21558
21559 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21560
21561 name = dwarf2_name (die, cu);
21562 if (name)
21563 {
21564 const char *linkagename;
21565 int suppress_add = 0;
21566
21567 if (space)
21568 sym = space;
21569 else
21570 sym = allocate_symbol (objfile);
21571 OBJSTAT (objfile, n_syms++);
21572
21573 /* Cache this symbol's name and the name's demangled form (if any). */
21574 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21575 linkagename = dwarf2_physname (name, die, cu);
21576 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21577
21578 /* Fortran does not have mangling standard and the mangling does differ
21579 between gfortran, iFort etc. */
21580 if (cu->language == language_fortran
21581 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21582 symbol_set_demangled_name (&(sym->ginfo),
21583 dwarf2_full_name (name, die, cu),
21584 NULL);
21585
21586 /* Default assumptions.
21587 Use the passed type or decode it from the die. */
21588 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21589 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21590 if (type != NULL)
21591 SYMBOL_TYPE (sym) = type;
21592 else
21593 SYMBOL_TYPE (sym) = die_type (die, cu);
21594 attr = dwarf2_attr (die,
21595 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21596 cu);
21597 if (attr)
21598 {
21599 SYMBOL_LINE (sym) = DW_UNSND (attr);
21600 }
21601
21602 attr = dwarf2_attr (die,
21603 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21604 cu);
21605 if (attr)
21606 {
21607 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21608 struct file_entry *fe;
21609
21610 if (cu->line_header != NULL)
21611 fe = cu->line_header->file_name_at (file_index);
21612 else
21613 fe = NULL;
21614
21615 if (fe == NULL)
21616 complaint (_("file index out of range"));
21617 else
21618 symbol_set_symtab (sym, fe->symtab);
21619 }
21620
21621 switch (die->tag)
21622 {
21623 case DW_TAG_label:
21624 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21625 if (attr)
21626 {
21627 CORE_ADDR addr;
21628
21629 addr = attr_value_as_address (attr);
21630 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21631 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21632 }
21633 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21634 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21635 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21636 add_symbol_to_list (sym, cu->list_in_scope);
21637 break;
21638 case DW_TAG_subprogram:
21639 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21640 finish_block. */
21641 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21642 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21643 if ((attr2 && (DW_UNSND (attr2) != 0))
21644 || cu->language == language_ada)
21645 {
21646 /* Subprograms marked external are stored as a global symbol.
21647 Ada subprograms, whether marked external or not, are always
21648 stored as a global symbol, because we want to be able to
21649 access them globally. For instance, we want to be able
21650 to break on a nested subprogram without having to
21651 specify the context. */
21652 list_to_add = cu->get_builder ()->get_global_symbols ();
21653 }
21654 else
21655 {
21656 list_to_add = cu->list_in_scope;
21657 }
21658 break;
21659 case DW_TAG_inlined_subroutine:
21660 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21661 finish_block. */
21662 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21663 SYMBOL_INLINED (sym) = 1;
21664 list_to_add = cu->list_in_scope;
21665 break;
21666 case DW_TAG_template_value_param:
21667 suppress_add = 1;
21668 /* Fall through. */
21669 case DW_TAG_constant:
21670 case DW_TAG_variable:
21671 case DW_TAG_member:
21672 /* Compilation with minimal debug info may result in
21673 variables with missing type entries. Change the
21674 misleading `void' type to something sensible. */
21675 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21676 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21677
21678 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21679 /* In the case of DW_TAG_member, we should only be called for
21680 static const members. */
21681 if (die->tag == DW_TAG_member)
21682 {
21683 /* dwarf2_add_field uses die_is_declaration,
21684 so we do the same. */
21685 gdb_assert (die_is_declaration (die, cu));
21686 gdb_assert (attr);
21687 }
21688 if (attr)
21689 {
21690 dwarf2_const_value (attr, sym, cu);
21691 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21692 if (!suppress_add)
21693 {
21694 if (attr2 && (DW_UNSND (attr2) != 0))
21695 list_to_add = cu->get_builder ()->get_global_symbols ();
21696 else
21697 list_to_add = cu->list_in_scope;
21698 }
21699 break;
21700 }
21701 attr = dwarf2_attr (die, DW_AT_location, cu);
21702 if (attr)
21703 {
21704 var_decode_location (attr, sym, cu);
21705 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21706
21707 /* Fortran explicitly imports any global symbols to the local
21708 scope by DW_TAG_common_block. */
21709 if (cu->language == language_fortran && die->parent
21710 && die->parent->tag == DW_TAG_common_block)
21711 attr2 = NULL;
21712
21713 if (SYMBOL_CLASS (sym) == LOC_STATIC
21714 && SYMBOL_VALUE_ADDRESS (sym) == 0
21715 && !dwarf2_per_objfile->has_section_at_zero)
21716 {
21717 /* When a static variable is eliminated by the linker,
21718 the corresponding debug information is not stripped
21719 out, but the variable address is set to null;
21720 do not add such variables into symbol table. */
21721 }
21722 else if (attr2 && (DW_UNSND (attr2) != 0))
21723 {
21724 if (SYMBOL_CLASS (sym) == LOC_STATIC
21725 && (objfile->flags & OBJF_MAINLINE) == 0
21726 && dwarf2_per_objfile->can_copy)
21727 {
21728 /* A global static variable might be subject to
21729 copy relocation. We first check for a local
21730 minsym, though, because maybe the symbol was
21731 marked hidden, in which case this would not
21732 apply. */
21733 bound_minimal_symbol found
21734 = (lookup_minimal_symbol_linkage
21735 (SYMBOL_LINKAGE_NAME (sym), objfile));
21736 if (found.minsym != nullptr)
21737 sym->maybe_copied = 1;
21738 }
21739
21740 /* A variable with DW_AT_external is never static,
21741 but it may be block-scoped. */
21742 list_to_add
21743 = ((cu->list_in_scope
21744 == cu->get_builder ()->get_file_symbols ())
21745 ? cu->get_builder ()->get_global_symbols ()
21746 : cu->list_in_scope);
21747 }
21748 else
21749 list_to_add = cu->list_in_scope;
21750 }
21751 else
21752 {
21753 /* We do not know the address of this symbol.
21754 If it is an external symbol and we have type information
21755 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21756 The address of the variable will then be determined from
21757 the minimal symbol table whenever the variable is
21758 referenced. */
21759 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21760
21761 /* Fortran explicitly imports any global symbols to the local
21762 scope by DW_TAG_common_block. */
21763 if (cu->language == language_fortran && die->parent
21764 && die->parent->tag == DW_TAG_common_block)
21765 {
21766 /* SYMBOL_CLASS doesn't matter here because
21767 read_common_block is going to reset it. */
21768 if (!suppress_add)
21769 list_to_add = cu->list_in_scope;
21770 }
21771 else if (attr2 && (DW_UNSND (attr2) != 0)
21772 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21773 {
21774 /* A variable with DW_AT_external is never static, but it
21775 may be block-scoped. */
21776 list_to_add
21777 = ((cu->list_in_scope
21778 == cu->get_builder ()->get_file_symbols ())
21779 ? cu->get_builder ()->get_global_symbols ()
21780 : cu->list_in_scope);
21781
21782 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21783 }
21784 else if (!die_is_declaration (die, cu))
21785 {
21786 /* Use the default LOC_OPTIMIZED_OUT class. */
21787 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21788 if (!suppress_add)
21789 list_to_add = cu->list_in_scope;
21790 }
21791 }
21792 break;
21793 case DW_TAG_formal_parameter:
21794 {
21795 /* If we are inside a function, mark this as an argument. If
21796 not, we might be looking at an argument to an inlined function
21797 when we do not have enough information to show inlined frames;
21798 pretend it's a local variable in that case so that the user can
21799 still see it. */
21800 struct context_stack *curr
21801 = cu->get_builder ()->get_current_context_stack ();
21802 if (curr != nullptr && curr->name != nullptr)
21803 SYMBOL_IS_ARGUMENT (sym) = 1;
21804 attr = dwarf2_attr (die, DW_AT_location, cu);
21805 if (attr)
21806 {
21807 var_decode_location (attr, sym, cu);
21808 }
21809 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21810 if (attr)
21811 {
21812 dwarf2_const_value (attr, sym, cu);
21813 }
21814
21815 list_to_add = cu->list_in_scope;
21816 }
21817 break;
21818 case DW_TAG_unspecified_parameters:
21819 /* From varargs functions; gdb doesn't seem to have any
21820 interest in this information, so just ignore it for now.
21821 (FIXME?) */
21822 break;
21823 case DW_TAG_template_type_param:
21824 suppress_add = 1;
21825 /* Fall through. */
21826 case DW_TAG_class_type:
21827 case DW_TAG_interface_type:
21828 case DW_TAG_structure_type:
21829 case DW_TAG_union_type:
21830 case DW_TAG_set_type:
21831 case DW_TAG_enumeration_type:
21832 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21833 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21834
21835 {
21836 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21837 really ever be static objects: otherwise, if you try
21838 to, say, break of a class's method and you're in a file
21839 which doesn't mention that class, it won't work unless
21840 the check for all static symbols in lookup_symbol_aux
21841 saves you. See the OtherFileClass tests in
21842 gdb.c++/namespace.exp. */
21843
21844 if (!suppress_add)
21845 {
21846 buildsym_compunit *builder = cu->get_builder ();
21847 list_to_add
21848 = (cu->list_in_scope == builder->get_file_symbols ()
21849 && cu->language == language_cplus
21850 ? builder->get_global_symbols ()
21851 : cu->list_in_scope);
21852
21853 /* The semantics of C++ state that "struct foo {
21854 ... }" also defines a typedef for "foo". */
21855 if (cu->language == language_cplus
21856 || cu->language == language_ada
21857 || cu->language == language_d
21858 || cu->language == language_rust)
21859 {
21860 /* The symbol's name is already allocated along
21861 with this objfile, so we don't need to
21862 duplicate it for the type. */
21863 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21864 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21865 }
21866 }
21867 }
21868 break;
21869 case DW_TAG_typedef:
21870 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21871 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21872 list_to_add = cu->list_in_scope;
21873 break;
21874 case DW_TAG_base_type:
21875 case DW_TAG_subrange_type:
21876 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21877 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21878 list_to_add = cu->list_in_scope;
21879 break;
21880 case DW_TAG_enumerator:
21881 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21882 if (attr)
21883 {
21884 dwarf2_const_value (attr, sym, cu);
21885 }
21886 {
21887 /* NOTE: carlton/2003-11-10: See comment above in the
21888 DW_TAG_class_type, etc. block. */
21889
21890 list_to_add
21891 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21892 && cu->language == language_cplus
21893 ? cu->get_builder ()->get_global_symbols ()
21894 : cu->list_in_scope);
21895 }
21896 break;
21897 case DW_TAG_imported_declaration:
21898 case DW_TAG_namespace:
21899 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21900 list_to_add = cu->get_builder ()->get_global_symbols ();
21901 break;
21902 case DW_TAG_module:
21903 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21904 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21905 list_to_add = cu->get_builder ()->get_global_symbols ();
21906 break;
21907 case DW_TAG_common_block:
21908 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21909 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21910 add_symbol_to_list (sym, cu->list_in_scope);
21911 break;
21912 default:
21913 /* Not a tag we recognize. Hopefully we aren't processing
21914 trash data, but since we must specifically ignore things
21915 we don't recognize, there is nothing else we should do at
21916 this point. */
21917 complaint (_("unsupported tag: '%s'"),
21918 dwarf_tag_name (die->tag));
21919 break;
21920 }
21921
21922 if (suppress_add)
21923 {
21924 sym->hash_next = objfile->template_symbols;
21925 objfile->template_symbols = sym;
21926 list_to_add = NULL;
21927 }
21928
21929 if (list_to_add != NULL)
21930 add_symbol_to_list (sym, list_to_add);
21931
21932 /* For the benefit of old versions of GCC, check for anonymous
21933 namespaces based on the demangled name. */
21934 if (!cu->processing_has_namespace_info
21935 && cu->language == language_cplus)
21936 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21937 }
21938 return (sym);
21939 }
21940
21941 /* Given an attr with a DW_FORM_dataN value in host byte order,
21942 zero-extend it as appropriate for the symbol's type. The DWARF
21943 standard (v4) is not entirely clear about the meaning of using
21944 DW_FORM_dataN for a constant with a signed type, where the type is
21945 wider than the data. The conclusion of a discussion on the DWARF
21946 list was that this is unspecified. We choose to always zero-extend
21947 because that is the interpretation long in use by GCC. */
21948
21949 static gdb_byte *
21950 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21951 struct dwarf2_cu *cu, LONGEST *value, int bits)
21952 {
21953 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21954 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21955 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21956 LONGEST l = DW_UNSND (attr);
21957
21958 if (bits < sizeof (*value) * 8)
21959 {
21960 l &= ((LONGEST) 1 << bits) - 1;
21961 *value = l;
21962 }
21963 else if (bits == sizeof (*value) * 8)
21964 *value = l;
21965 else
21966 {
21967 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21968 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21969 return bytes;
21970 }
21971
21972 return NULL;
21973 }
21974
21975 /* Read a constant value from an attribute. Either set *VALUE, or if
21976 the value does not fit in *VALUE, set *BYTES - either already
21977 allocated on the objfile obstack, or newly allocated on OBSTACK,
21978 or, set *BATON, if we translated the constant to a location
21979 expression. */
21980
21981 static void
21982 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21983 const char *name, struct obstack *obstack,
21984 struct dwarf2_cu *cu,
21985 LONGEST *value, const gdb_byte **bytes,
21986 struct dwarf2_locexpr_baton **baton)
21987 {
21988 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21989 struct comp_unit_head *cu_header = &cu->header;
21990 struct dwarf_block *blk;
21991 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21992 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21993
21994 *value = 0;
21995 *bytes = NULL;
21996 *baton = NULL;
21997
21998 switch (attr->form)
21999 {
22000 case DW_FORM_addr:
22001 case DW_FORM_addrx:
22002 case DW_FORM_GNU_addr_index:
22003 {
22004 gdb_byte *data;
22005
22006 if (TYPE_LENGTH (type) != cu_header->addr_size)
22007 dwarf2_const_value_length_mismatch_complaint (name,
22008 cu_header->addr_size,
22009 TYPE_LENGTH (type));
22010 /* Symbols of this form are reasonably rare, so we just
22011 piggyback on the existing location code rather than writing
22012 a new implementation of symbol_computed_ops. */
22013 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22014 (*baton)->per_cu = cu->per_cu;
22015 gdb_assert ((*baton)->per_cu);
22016
22017 (*baton)->size = 2 + cu_header->addr_size;
22018 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22019 (*baton)->data = data;
22020
22021 data[0] = DW_OP_addr;
22022 store_unsigned_integer (&data[1], cu_header->addr_size,
22023 byte_order, DW_ADDR (attr));
22024 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22025 }
22026 break;
22027 case DW_FORM_string:
22028 case DW_FORM_strp:
22029 case DW_FORM_strx:
22030 case DW_FORM_GNU_str_index:
22031 case DW_FORM_GNU_strp_alt:
22032 /* DW_STRING is already allocated on the objfile obstack, point
22033 directly to it. */
22034 *bytes = (const gdb_byte *) DW_STRING (attr);
22035 break;
22036 case DW_FORM_block1:
22037 case DW_FORM_block2:
22038 case DW_FORM_block4:
22039 case DW_FORM_block:
22040 case DW_FORM_exprloc:
22041 case DW_FORM_data16:
22042 blk = DW_BLOCK (attr);
22043 if (TYPE_LENGTH (type) != blk->size)
22044 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22045 TYPE_LENGTH (type));
22046 *bytes = blk->data;
22047 break;
22048
22049 /* The DW_AT_const_value attributes are supposed to carry the
22050 symbol's value "represented as it would be on the target
22051 architecture." By the time we get here, it's already been
22052 converted to host endianness, so we just need to sign- or
22053 zero-extend it as appropriate. */
22054 case DW_FORM_data1:
22055 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22056 break;
22057 case DW_FORM_data2:
22058 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22059 break;
22060 case DW_FORM_data4:
22061 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22062 break;
22063 case DW_FORM_data8:
22064 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22065 break;
22066
22067 case DW_FORM_sdata:
22068 case DW_FORM_implicit_const:
22069 *value = DW_SND (attr);
22070 break;
22071
22072 case DW_FORM_udata:
22073 *value = DW_UNSND (attr);
22074 break;
22075
22076 default:
22077 complaint (_("unsupported const value attribute form: '%s'"),
22078 dwarf_form_name (attr->form));
22079 *value = 0;
22080 break;
22081 }
22082 }
22083
22084
22085 /* Copy constant value from an attribute to a symbol. */
22086
22087 static void
22088 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22089 struct dwarf2_cu *cu)
22090 {
22091 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22092 LONGEST value;
22093 const gdb_byte *bytes;
22094 struct dwarf2_locexpr_baton *baton;
22095
22096 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22097 SYMBOL_PRINT_NAME (sym),
22098 &objfile->objfile_obstack, cu,
22099 &value, &bytes, &baton);
22100
22101 if (baton != NULL)
22102 {
22103 SYMBOL_LOCATION_BATON (sym) = baton;
22104 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22105 }
22106 else if (bytes != NULL)
22107 {
22108 SYMBOL_VALUE_BYTES (sym) = bytes;
22109 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22110 }
22111 else
22112 {
22113 SYMBOL_VALUE (sym) = value;
22114 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22115 }
22116 }
22117
22118 /* Return the type of the die in question using its DW_AT_type attribute. */
22119
22120 static struct type *
22121 die_type (struct die_info *die, struct dwarf2_cu *cu)
22122 {
22123 struct attribute *type_attr;
22124
22125 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22126 if (!type_attr)
22127 {
22128 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22129 /* A missing DW_AT_type represents a void type. */
22130 return objfile_type (objfile)->builtin_void;
22131 }
22132
22133 return lookup_die_type (die, type_attr, cu);
22134 }
22135
22136 /* True iff CU's producer generates GNAT Ada auxiliary information
22137 that allows to find parallel types through that information instead
22138 of having to do expensive parallel lookups by type name. */
22139
22140 static int
22141 need_gnat_info (struct dwarf2_cu *cu)
22142 {
22143 /* Assume that the Ada compiler was GNAT, which always produces
22144 the auxiliary information. */
22145 return (cu->language == language_ada);
22146 }
22147
22148 /* Return the auxiliary type of the die in question using its
22149 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22150 attribute is not present. */
22151
22152 static struct type *
22153 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22154 {
22155 struct attribute *type_attr;
22156
22157 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22158 if (!type_attr)
22159 return NULL;
22160
22161 return lookup_die_type (die, type_attr, cu);
22162 }
22163
22164 /* If DIE has a descriptive_type attribute, then set the TYPE's
22165 descriptive type accordingly. */
22166
22167 static void
22168 set_descriptive_type (struct type *type, struct die_info *die,
22169 struct dwarf2_cu *cu)
22170 {
22171 struct type *descriptive_type = die_descriptive_type (die, cu);
22172
22173 if (descriptive_type)
22174 {
22175 ALLOCATE_GNAT_AUX_TYPE (type);
22176 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22177 }
22178 }
22179
22180 /* Return the containing type of the die in question using its
22181 DW_AT_containing_type attribute. */
22182
22183 static struct type *
22184 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22185 {
22186 struct attribute *type_attr;
22187 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22188
22189 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22190 if (!type_attr)
22191 error (_("Dwarf Error: Problem turning containing type into gdb type "
22192 "[in module %s]"), objfile_name (objfile));
22193
22194 return lookup_die_type (die, type_attr, cu);
22195 }
22196
22197 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22198
22199 static struct type *
22200 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22201 {
22202 struct dwarf2_per_objfile *dwarf2_per_objfile
22203 = cu->per_cu->dwarf2_per_objfile;
22204 struct objfile *objfile = dwarf2_per_objfile->objfile;
22205 char *saved;
22206
22207 std::string message
22208 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22209 objfile_name (objfile),
22210 sect_offset_str (cu->header.sect_off),
22211 sect_offset_str (die->sect_off));
22212 saved = obstack_strdup (&objfile->objfile_obstack, message);
22213
22214 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22215 }
22216
22217 /* Look up the type of DIE in CU using its type attribute ATTR.
22218 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22219 DW_AT_containing_type.
22220 If there is no type substitute an error marker. */
22221
22222 static struct type *
22223 lookup_die_type (struct die_info *die, const struct attribute *attr,
22224 struct dwarf2_cu *cu)
22225 {
22226 struct dwarf2_per_objfile *dwarf2_per_objfile
22227 = cu->per_cu->dwarf2_per_objfile;
22228 struct objfile *objfile = dwarf2_per_objfile->objfile;
22229 struct type *this_type;
22230
22231 gdb_assert (attr->name == DW_AT_type
22232 || attr->name == DW_AT_GNAT_descriptive_type
22233 || attr->name == DW_AT_containing_type);
22234
22235 /* First see if we have it cached. */
22236
22237 if (attr->form == DW_FORM_GNU_ref_alt)
22238 {
22239 struct dwarf2_per_cu_data *per_cu;
22240 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22241
22242 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22243 dwarf2_per_objfile);
22244 this_type = get_die_type_at_offset (sect_off, per_cu);
22245 }
22246 else if (attr_form_is_ref (attr))
22247 {
22248 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22249
22250 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22251 }
22252 else if (attr->form == DW_FORM_ref_sig8)
22253 {
22254 ULONGEST signature = DW_SIGNATURE (attr);
22255
22256 return get_signatured_type (die, signature, cu);
22257 }
22258 else
22259 {
22260 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22261 " at %s [in module %s]"),
22262 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22263 objfile_name (objfile));
22264 return build_error_marker_type (cu, die);
22265 }
22266
22267 /* If not cached we need to read it in. */
22268
22269 if (this_type == NULL)
22270 {
22271 struct die_info *type_die = NULL;
22272 struct dwarf2_cu *type_cu = cu;
22273
22274 if (attr_form_is_ref (attr))
22275 type_die = follow_die_ref (die, attr, &type_cu);
22276 if (type_die == NULL)
22277 return build_error_marker_type (cu, die);
22278 /* If we find the type now, it's probably because the type came
22279 from an inter-CU reference and the type's CU got expanded before
22280 ours. */
22281 this_type = read_type_die (type_die, type_cu);
22282 }
22283
22284 /* If we still don't have a type use an error marker. */
22285
22286 if (this_type == NULL)
22287 return build_error_marker_type (cu, die);
22288
22289 return this_type;
22290 }
22291
22292 /* Return the type in DIE, CU.
22293 Returns NULL for invalid types.
22294
22295 This first does a lookup in die_type_hash,
22296 and only reads the die in if necessary.
22297
22298 NOTE: This can be called when reading in partial or full symbols. */
22299
22300 static struct type *
22301 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22302 {
22303 struct type *this_type;
22304
22305 this_type = get_die_type (die, cu);
22306 if (this_type)
22307 return this_type;
22308
22309 return read_type_die_1 (die, cu);
22310 }
22311
22312 /* Read the type in DIE, CU.
22313 Returns NULL for invalid types. */
22314
22315 static struct type *
22316 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22317 {
22318 struct type *this_type = NULL;
22319
22320 switch (die->tag)
22321 {
22322 case DW_TAG_class_type:
22323 case DW_TAG_interface_type:
22324 case DW_TAG_structure_type:
22325 case DW_TAG_union_type:
22326 this_type = read_structure_type (die, cu);
22327 break;
22328 case DW_TAG_enumeration_type:
22329 this_type = read_enumeration_type (die, cu);
22330 break;
22331 case DW_TAG_subprogram:
22332 case DW_TAG_subroutine_type:
22333 case DW_TAG_inlined_subroutine:
22334 this_type = read_subroutine_type (die, cu);
22335 break;
22336 case DW_TAG_array_type:
22337 this_type = read_array_type (die, cu);
22338 break;
22339 case DW_TAG_set_type:
22340 this_type = read_set_type (die, cu);
22341 break;
22342 case DW_TAG_pointer_type:
22343 this_type = read_tag_pointer_type (die, cu);
22344 break;
22345 case DW_TAG_ptr_to_member_type:
22346 this_type = read_tag_ptr_to_member_type (die, cu);
22347 break;
22348 case DW_TAG_reference_type:
22349 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22350 break;
22351 case DW_TAG_rvalue_reference_type:
22352 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22353 break;
22354 case DW_TAG_const_type:
22355 this_type = read_tag_const_type (die, cu);
22356 break;
22357 case DW_TAG_volatile_type:
22358 this_type = read_tag_volatile_type (die, cu);
22359 break;
22360 case DW_TAG_restrict_type:
22361 this_type = read_tag_restrict_type (die, cu);
22362 break;
22363 case DW_TAG_string_type:
22364 this_type = read_tag_string_type (die, cu);
22365 break;
22366 case DW_TAG_typedef:
22367 this_type = read_typedef (die, cu);
22368 break;
22369 case DW_TAG_subrange_type:
22370 this_type = read_subrange_type (die, cu);
22371 break;
22372 case DW_TAG_base_type:
22373 this_type = read_base_type (die, cu);
22374 break;
22375 case DW_TAG_unspecified_type:
22376 this_type = read_unspecified_type (die, cu);
22377 break;
22378 case DW_TAG_namespace:
22379 this_type = read_namespace_type (die, cu);
22380 break;
22381 case DW_TAG_module:
22382 this_type = read_module_type (die, cu);
22383 break;
22384 case DW_TAG_atomic_type:
22385 this_type = read_tag_atomic_type (die, cu);
22386 break;
22387 default:
22388 complaint (_("unexpected tag in read_type_die: '%s'"),
22389 dwarf_tag_name (die->tag));
22390 break;
22391 }
22392
22393 return this_type;
22394 }
22395
22396 /* See if we can figure out if the class lives in a namespace. We do
22397 this by looking for a member function; its demangled name will
22398 contain namespace info, if there is any.
22399 Return the computed name or NULL.
22400 Space for the result is allocated on the objfile's obstack.
22401 This is the full-die version of guess_partial_die_structure_name.
22402 In this case we know DIE has no useful parent. */
22403
22404 static char *
22405 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22406 {
22407 struct die_info *spec_die;
22408 struct dwarf2_cu *spec_cu;
22409 struct die_info *child;
22410 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22411
22412 spec_cu = cu;
22413 spec_die = die_specification (die, &spec_cu);
22414 if (spec_die != NULL)
22415 {
22416 die = spec_die;
22417 cu = spec_cu;
22418 }
22419
22420 for (child = die->child;
22421 child != NULL;
22422 child = child->sibling)
22423 {
22424 if (child->tag == DW_TAG_subprogram)
22425 {
22426 const char *linkage_name = dw2_linkage_name (child, cu);
22427
22428 if (linkage_name != NULL)
22429 {
22430 char *actual_name
22431 = language_class_name_from_physname (cu->language_defn,
22432 linkage_name);
22433 char *name = NULL;
22434
22435 if (actual_name != NULL)
22436 {
22437 const char *die_name = dwarf2_name (die, cu);
22438
22439 if (die_name != NULL
22440 && strcmp (die_name, actual_name) != 0)
22441 {
22442 /* Strip off the class name from the full name.
22443 We want the prefix. */
22444 int die_name_len = strlen (die_name);
22445 int actual_name_len = strlen (actual_name);
22446
22447 /* Test for '::' as a sanity check. */
22448 if (actual_name_len > die_name_len + 2
22449 && actual_name[actual_name_len
22450 - die_name_len - 1] == ':')
22451 name = obstack_strndup (
22452 &objfile->per_bfd->storage_obstack,
22453 actual_name, actual_name_len - die_name_len - 2);
22454 }
22455 }
22456 xfree (actual_name);
22457 return name;
22458 }
22459 }
22460 }
22461
22462 return NULL;
22463 }
22464
22465 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22466 prefix part in such case. See
22467 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22468
22469 static const char *
22470 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22471 {
22472 struct attribute *attr;
22473 const char *base;
22474
22475 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22476 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22477 return NULL;
22478
22479 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22480 return NULL;
22481
22482 attr = dw2_linkage_name_attr (die, cu);
22483 if (attr == NULL || DW_STRING (attr) == NULL)
22484 return NULL;
22485
22486 /* dwarf2_name had to be already called. */
22487 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22488
22489 /* Strip the base name, keep any leading namespaces/classes. */
22490 base = strrchr (DW_STRING (attr), ':');
22491 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22492 return "";
22493
22494 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22495 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22496 DW_STRING (attr),
22497 &base[-1] - DW_STRING (attr));
22498 }
22499
22500 /* Return the name of the namespace/class that DIE is defined within,
22501 or "" if we can't tell. The caller should not xfree the result.
22502
22503 For example, if we're within the method foo() in the following
22504 code:
22505
22506 namespace N {
22507 class C {
22508 void foo () {
22509 }
22510 };
22511 }
22512
22513 then determine_prefix on foo's die will return "N::C". */
22514
22515 static const char *
22516 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22517 {
22518 struct dwarf2_per_objfile *dwarf2_per_objfile
22519 = cu->per_cu->dwarf2_per_objfile;
22520 struct die_info *parent, *spec_die;
22521 struct dwarf2_cu *spec_cu;
22522 struct type *parent_type;
22523 const char *retval;
22524
22525 if (cu->language != language_cplus
22526 && cu->language != language_fortran && cu->language != language_d
22527 && cu->language != language_rust)
22528 return "";
22529
22530 retval = anonymous_struct_prefix (die, cu);
22531 if (retval)
22532 return retval;
22533
22534 /* We have to be careful in the presence of DW_AT_specification.
22535 For example, with GCC 3.4, given the code
22536
22537 namespace N {
22538 void foo() {
22539 // Definition of N::foo.
22540 }
22541 }
22542
22543 then we'll have a tree of DIEs like this:
22544
22545 1: DW_TAG_compile_unit
22546 2: DW_TAG_namespace // N
22547 3: DW_TAG_subprogram // declaration of N::foo
22548 4: DW_TAG_subprogram // definition of N::foo
22549 DW_AT_specification // refers to die #3
22550
22551 Thus, when processing die #4, we have to pretend that we're in
22552 the context of its DW_AT_specification, namely the contex of die
22553 #3. */
22554 spec_cu = cu;
22555 spec_die = die_specification (die, &spec_cu);
22556 if (spec_die == NULL)
22557 parent = die->parent;
22558 else
22559 {
22560 parent = spec_die->parent;
22561 cu = spec_cu;
22562 }
22563
22564 if (parent == NULL)
22565 return "";
22566 else if (parent->building_fullname)
22567 {
22568 const char *name;
22569 const char *parent_name;
22570
22571 /* It has been seen on RealView 2.2 built binaries,
22572 DW_TAG_template_type_param types actually _defined_ as
22573 children of the parent class:
22574
22575 enum E {};
22576 template class <class Enum> Class{};
22577 Class<enum E> class_e;
22578
22579 1: DW_TAG_class_type (Class)
22580 2: DW_TAG_enumeration_type (E)
22581 3: DW_TAG_enumerator (enum1:0)
22582 3: DW_TAG_enumerator (enum2:1)
22583 ...
22584 2: DW_TAG_template_type_param
22585 DW_AT_type DW_FORM_ref_udata (E)
22586
22587 Besides being broken debug info, it can put GDB into an
22588 infinite loop. Consider:
22589
22590 When we're building the full name for Class<E>, we'll start
22591 at Class, and go look over its template type parameters,
22592 finding E. We'll then try to build the full name of E, and
22593 reach here. We're now trying to build the full name of E,
22594 and look over the parent DIE for containing scope. In the
22595 broken case, if we followed the parent DIE of E, we'd again
22596 find Class, and once again go look at its template type
22597 arguments, etc., etc. Simply don't consider such parent die
22598 as source-level parent of this die (it can't be, the language
22599 doesn't allow it), and break the loop here. */
22600 name = dwarf2_name (die, cu);
22601 parent_name = dwarf2_name (parent, cu);
22602 complaint (_("template param type '%s' defined within parent '%s'"),
22603 name ? name : "<unknown>",
22604 parent_name ? parent_name : "<unknown>");
22605 return "";
22606 }
22607 else
22608 switch (parent->tag)
22609 {
22610 case DW_TAG_namespace:
22611 parent_type = read_type_die (parent, cu);
22612 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22613 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22614 Work around this problem here. */
22615 if (cu->language == language_cplus
22616 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22617 return "";
22618 /* We give a name to even anonymous namespaces. */
22619 return TYPE_NAME (parent_type);
22620 case DW_TAG_class_type:
22621 case DW_TAG_interface_type:
22622 case DW_TAG_structure_type:
22623 case DW_TAG_union_type:
22624 case DW_TAG_module:
22625 parent_type = read_type_die (parent, cu);
22626 if (TYPE_NAME (parent_type) != NULL)
22627 return TYPE_NAME (parent_type);
22628 else
22629 /* An anonymous structure is only allowed non-static data
22630 members; no typedefs, no member functions, et cetera.
22631 So it does not need a prefix. */
22632 return "";
22633 case DW_TAG_compile_unit:
22634 case DW_TAG_partial_unit:
22635 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22636 if (cu->language == language_cplus
22637 && !dwarf2_per_objfile->types.empty ()
22638 && die->child != NULL
22639 && (die->tag == DW_TAG_class_type
22640 || die->tag == DW_TAG_structure_type
22641 || die->tag == DW_TAG_union_type))
22642 {
22643 char *name = guess_full_die_structure_name (die, cu);
22644 if (name != NULL)
22645 return name;
22646 }
22647 return "";
22648 case DW_TAG_enumeration_type:
22649 parent_type = read_type_die (parent, cu);
22650 if (TYPE_DECLARED_CLASS (parent_type))
22651 {
22652 if (TYPE_NAME (parent_type) != NULL)
22653 return TYPE_NAME (parent_type);
22654 return "";
22655 }
22656 /* Fall through. */
22657 default:
22658 return determine_prefix (parent, cu);
22659 }
22660 }
22661
22662 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22663 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22664 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22665 an obconcat, otherwise allocate storage for the result. The CU argument is
22666 used to determine the language and hence, the appropriate separator. */
22667
22668 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22669
22670 static char *
22671 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22672 int physname, struct dwarf2_cu *cu)
22673 {
22674 const char *lead = "";
22675 const char *sep;
22676
22677 if (suffix == NULL || suffix[0] == '\0'
22678 || prefix == NULL || prefix[0] == '\0')
22679 sep = "";
22680 else if (cu->language == language_d)
22681 {
22682 /* For D, the 'main' function could be defined in any module, but it
22683 should never be prefixed. */
22684 if (strcmp (suffix, "D main") == 0)
22685 {
22686 prefix = "";
22687 sep = "";
22688 }
22689 else
22690 sep = ".";
22691 }
22692 else if (cu->language == language_fortran && physname)
22693 {
22694 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22695 DW_AT_MIPS_linkage_name is preferred and used instead. */
22696
22697 lead = "__";
22698 sep = "_MOD_";
22699 }
22700 else
22701 sep = "::";
22702
22703 if (prefix == NULL)
22704 prefix = "";
22705 if (suffix == NULL)
22706 suffix = "";
22707
22708 if (obs == NULL)
22709 {
22710 char *retval
22711 = ((char *)
22712 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22713
22714 strcpy (retval, lead);
22715 strcat (retval, prefix);
22716 strcat (retval, sep);
22717 strcat (retval, suffix);
22718 return retval;
22719 }
22720 else
22721 {
22722 /* We have an obstack. */
22723 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22724 }
22725 }
22726
22727 /* Return sibling of die, NULL if no sibling. */
22728
22729 static struct die_info *
22730 sibling_die (struct die_info *die)
22731 {
22732 return die->sibling;
22733 }
22734
22735 /* Get name of a die, return NULL if not found. */
22736
22737 static const char *
22738 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22739 struct obstack *obstack)
22740 {
22741 if (name && cu->language == language_cplus)
22742 {
22743 std::string canon_name = cp_canonicalize_string (name);
22744
22745 if (!canon_name.empty ())
22746 {
22747 if (canon_name != name)
22748 name = obstack_strdup (obstack, canon_name);
22749 }
22750 }
22751
22752 return name;
22753 }
22754
22755 /* Get name of a die, return NULL if not found.
22756 Anonymous namespaces are converted to their magic string. */
22757
22758 static const char *
22759 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22760 {
22761 struct attribute *attr;
22762 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22763
22764 attr = dwarf2_attr (die, DW_AT_name, cu);
22765 if ((!attr || !DW_STRING (attr))
22766 && die->tag != DW_TAG_namespace
22767 && die->tag != DW_TAG_class_type
22768 && die->tag != DW_TAG_interface_type
22769 && die->tag != DW_TAG_structure_type
22770 && die->tag != DW_TAG_union_type)
22771 return NULL;
22772
22773 switch (die->tag)
22774 {
22775 case DW_TAG_compile_unit:
22776 case DW_TAG_partial_unit:
22777 /* Compilation units have a DW_AT_name that is a filename, not
22778 a source language identifier. */
22779 case DW_TAG_enumeration_type:
22780 case DW_TAG_enumerator:
22781 /* These tags always have simple identifiers already; no need
22782 to canonicalize them. */
22783 return DW_STRING (attr);
22784
22785 case DW_TAG_namespace:
22786 if (attr != NULL && DW_STRING (attr) != NULL)
22787 return DW_STRING (attr);
22788 return CP_ANONYMOUS_NAMESPACE_STR;
22789
22790 case DW_TAG_class_type:
22791 case DW_TAG_interface_type:
22792 case DW_TAG_structure_type:
22793 case DW_TAG_union_type:
22794 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22795 structures or unions. These were of the form "._%d" in GCC 4.1,
22796 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22797 and GCC 4.4. We work around this problem by ignoring these. */
22798 if (attr && DW_STRING (attr)
22799 && (startswith (DW_STRING (attr), "._")
22800 || startswith (DW_STRING (attr), "<anonymous")))
22801 return NULL;
22802
22803 /* GCC might emit a nameless typedef that has a linkage name. See
22804 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22805 if (!attr || DW_STRING (attr) == NULL)
22806 {
22807 char *demangled = NULL;
22808
22809 attr = dw2_linkage_name_attr (die, cu);
22810 if (attr == NULL || DW_STRING (attr) == NULL)
22811 return NULL;
22812
22813 /* Avoid demangling DW_STRING (attr) the second time on a second
22814 call for the same DIE. */
22815 if (!DW_STRING_IS_CANONICAL (attr))
22816 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22817
22818 if (demangled)
22819 {
22820 const char *base;
22821
22822 /* FIXME: we already did this for the partial symbol... */
22823 DW_STRING (attr)
22824 = obstack_strdup (&objfile->per_bfd->storage_obstack,
22825 demangled);
22826 DW_STRING_IS_CANONICAL (attr) = 1;
22827 xfree (demangled);
22828
22829 /* Strip any leading namespaces/classes, keep only the base name.
22830 DW_AT_name for named DIEs does not contain the prefixes. */
22831 base = strrchr (DW_STRING (attr), ':');
22832 if (base && base > DW_STRING (attr) && base[-1] == ':')
22833 return &base[1];
22834 else
22835 return DW_STRING (attr);
22836 }
22837 }
22838 break;
22839
22840 default:
22841 break;
22842 }
22843
22844 if (!DW_STRING_IS_CANONICAL (attr))
22845 {
22846 DW_STRING (attr)
22847 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22848 &objfile->per_bfd->storage_obstack);
22849 DW_STRING_IS_CANONICAL (attr) = 1;
22850 }
22851 return DW_STRING (attr);
22852 }
22853
22854 /* Return the die that this die in an extension of, or NULL if there
22855 is none. *EXT_CU is the CU containing DIE on input, and the CU
22856 containing the return value on output. */
22857
22858 static struct die_info *
22859 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22860 {
22861 struct attribute *attr;
22862
22863 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22864 if (attr == NULL)
22865 return NULL;
22866
22867 return follow_die_ref (die, attr, ext_cu);
22868 }
22869
22870 /* A convenience function that returns an "unknown" DWARF name,
22871 including the value of V. STR is the name of the entity being
22872 printed, e.g., "TAG". */
22873
22874 static const char *
22875 dwarf_unknown (const char *str, unsigned v)
22876 {
22877 char *cell = get_print_cell ();
22878 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
22879 return cell;
22880 }
22881
22882 /* Convert a DIE tag into its string name. */
22883
22884 static const char *
22885 dwarf_tag_name (unsigned tag)
22886 {
22887 const char *name = get_DW_TAG_name (tag);
22888
22889 if (name == NULL)
22890 return dwarf_unknown ("TAG", tag);
22891
22892 return name;
22893 }
22894
22895 /* Convert a DWARF attribute code into its string name. */
22896
22897 static const char *
22898 dwarf_attr_name (unsigned attr)
22899 {
22900 const char *name;
22901
22902 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22903 if (attr == DW_AT_MIPS_fde)
22904 return "DW_AT_MIPS_fde";
22905 #else
22906 if (attr == DW_AT_HP_block_index)
22907 return "DW_AT_HP_block_index";
22908 #endif
22909
22910 name = get_DW_AT_name (attr);
22911
22912 if (name == NULL)
22913 return dwarf_unknown ("AT", attr);
22914
22915 return name;
22916 }
22917
22918 /* Convert a unit type to corresponding DW_UT name. */
22919
22920 static const char *
22921 dwarf_unit_type_name (int unit_type) {
22922 switch (unit_type)
22923 {
22924 case 0x01:
22925 return "DW_UT_compile (0x01)";
22926 case 0x02:
22927 return "DW_UT_type (0x02)";
22928 case 0x03:
22929 return "DW_UT_partial (0x03)";
22930 case 0x04:
22931 return "DW_UT_skeleton (0x04)";
22932 case 0x05:
22933 return "DW_UT_split_compile (0x05)";
22934 case 0x06:
22935 return "DW_UT_split_type (0x06)";
22936 case 0x80:
22937 return "DW_UT_lo_user (0x80)";
22938 case 0xff:
22939 return "DW_UT_hi_user (0xff)";
22940 default:
22941 return nullptr;
22942 }
22943 }
22944
22945 /* Convert a DWARF value form code into its string name. */
22946
22947 static const char *
22948 dwarf_form_name (unsigned form)
22949 {
22950 const char *name = get_DW_FORM_name (form);
22951
22952 if (name == NULL)
22953 return dwarf_unknown ("FORM", form);
22954
22955 return name;
22956 }
22957
22958 static const char *
22959 dwarf_bool_name (unsigned mybool)
22960 {
22961 if (mybool)
22962 return "TRUE";
22963 else
22964 return "FALSE";
22965 }
22966
22967 /* Convert a DWARF type code into its string name. */
22968
22969 static const char *
22970 dwarf_type_encoding_name (unsigned enc)
22971 {
22972 const char *name = get_DW_ATE_name (enc);
22973
22974 if (name == NULL)
22975 return dwarf_unknown ("ATE", enc);
22976
22977 return name;
22978 }
22979
22980 static void
22981 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22982 {
22983 unsigned int i;
22984
22985 print_spaces (indent, f);
22986 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22987 dwarf_tag_name (die->tag), die->abbrev,
22988 sect_offset_str (die->sect_off));
22989
22990 if (die->parent != NULL)
22991 {
22992 print_spaces (indent, f);
22993 fprintf_unfiltered (f, " parent at offset: %s\n",
22994 sect_offset_str (die->parent->sect_off));
22995 }
22996
22997 print_spaces (indent, f);
22998 fprintf_unfiltered (f, " has children: %s\n",
22999 dwarf_bool_name (die->child != NULL));
23000
23001 print_spaces (indent, f);
23002 fprintf_unfiltered (f, " attributes:\n");
23003
23004 for (i = 0; i < die->num_attrs; ++i)
23005 {
23006 print_spaces (indent, f);
23007 fprintf_unfiltered (f, " %s (%s) ",
23008 dwarf_attr_name (die->attrs[i].name),
23009 dwarf_form_name (die->attrs[i].form));
23010
23011 switch (die->attrs[i].form)
23012 {
23013 case DW_FORM_addr:
23014 case DW_FORM_addrx:
23015 case DW_FORM_GNU_addr_index:
23016 fprintf_unfiltered (f, "address: ");
23017 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
23018 break;
23019 case DW_FORM_block2:
23020 case DW_FORM_block4:
23021 case DW_FORM_block:
23022 case DW_FORM_block1:
23023 fprintf_unfiltered (f, "block: size %s",
23024 pulongest (DW_BLOCK (&die->attrs[i])->size));
23025 break;
23026 case DW_FORM_exprloc:
23027 fprintf_unfiltered (f, "expression: size %s",
23028 pulongest (DW_BLOCK (&die->attrs[i])->size));
23029 break;
23030 case DW_FORM_data16:
23031 fprintf_unfiltered (f, "constant of 16 bytes");
23032 break;
23033 case DW_FORM_ref_addr:
23034 fprintf_unfiltered (f, "ref address: ");
23035 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23036 break;
23037 case DW_FORM_GNU_ref_alt:
23038 fprintf_unfiltered (f, "alt ref address: ");
23039 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23040 break;
23041 case DW_FORM_ref1:
23042 case DW_FORM_ref2:
23043 case DW_FORM_ref4:
23044 case DW_FORM_ref8:
23045 case DW_FORM_ref_udata:
23046 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23047 (long) (DW_UNSND (&die->attrs[i])));
23048 break;
23049 case DW_FORM_data1:
23050 case DW_FORM_data2:
23051 case DW_FORM_data4:
23052 case DW_FORM_data8:
23053 case DW_FORM_udata:
23054 case DW_FORM_sdata:
23055 fprintf_unfiltered (f, "constant: %s",
23056 pulongest (DW_UNSND (&die->attrs[i])));
23057 break;
23058 case DW_FORM_sec_offset:
23059 fprintf_unfiltered (f, "section offset: %s",
23060 pulongest (DW_UNSND (&die->attrs[i])));
23061 break;
23062 case DW_FORM_ref_sig8:
23063 fprintf_unfiltered (f, "signature: %s",
23064 hex_string (DW_SIGNATURE (&die->attrs[i])));
23065 break;
23066 case DW_FORM_string:
23067 case DW_FORM_strp:
23068 case DW_FORM_line_strp:
23069 case DW_FORM_strx:
23070 case DW_FORM_GNU_str_index:
23071 case DW_FORM_GNU_strp_alt:
23072 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23073 DW_STRING (&die->attrs[i])
23074 ? DW_STRING (&die->attrs[i]) : "",
23075 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
23076 break;
23077 case DW_FORM_flag:
23078 if (DW_UNSND (&die->attrs[i]))
23079 fprintf_unfiltered (f, "flag: TRUE");
23080 else
23081 fprintf_unfiltered (f, "flag: FALSE");
23082 break;
23083 case DW_FORM_flag_present:
23084 fprintf_unfiltered (f, "flag: TRUE");
23085 break;
23086 case DW_FORM_indirect:
23087 /* The reader will have reduced the indirect form to
23088 the "base form" so this form should not occur. */
23089 fprintf_unfiltered (f,
23090 "unexpected attribute form: DW_FORM_indirect");
23091 break;
23092 case DW_FORM_implicit_const:
23093 fprintf_unfiltered (f, "constant: %s",
23094 plongest (DW_SND (&die->attrs[i])));
23095 break;
23096 default:
23097 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23098 die->attrs[i].form);
23099 break;
23100 }
23101 fprintf_unfiltered (f, "\n");
23102 }
23103 }
23104
23105 static void
23106 dump_die_for_error (struct die_info *die)
23107 {
23108 dump_die_shallow (gdb_stderr, 0, die);
23109 }
23110
23111 static void
23112 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23113 {
23114 int indent = level * 4;
23115
23116 gdb_assert (die != NULL);
23117
23118 if (level >= max_level)
23119 return;
23120
23121 dump_die_shallow (f, indent, die);
23122
23123 if (die->child != NULL)
23124 {
23125 print_spaces (indent, f);
23126 fprintf_unfiltered (f, " Children:");
23127 if (level + 1 < max_level)
23128 {
23129 fprintf_unfiltered (f, "\n");
23130 dump_die_1 (f, level + 1, max_level, die->child);
23131 }
23132 else
23133 {
23134 fprintf_unfiltered (f,
23135 " [not printed, max nesting level reached]\n");
23136 }
23137 }
23138
23139 if (die->sibling != NULL && level > 0)
23140 {
23141 dump_die_1 (f, level, max_level, die->sibling);
23142 }
23143 }
23144
23145 /* This is called from the pdie macro in gdbinit.in.
23146 It's not static so gcc will keep a copy callable from gdb. */
23147
23148 void
23149 dump_die (struct die_info *die, int max_level)
23150 {
23151 dump_die_1 (gdb_stdlog, 0, max_level, die);
23152 }
23153
23154 static void
23155 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23156 {
23157 void **slot;
23158
23159 slot = htab_find_slot_with_hash (cu->die_hash, die,
23160 to_underlying (die->sect_off),
23161 INSERT);
23162
23163 *slot = die;
23164 }
23165
23166 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23167 required kind. */
23168
23169 static sect_offset
23170 dwarf2_get_ref_die_offset (const struct attribute *attr)
23171 {
23172 if (attr_form_is_ref (attr))
23173 return (sect_offset) DW_UNSND (attr);
23174
23175 complaint (_("unsupported die ref attribute form: '%s'"),
23176 dwarf_form_name (attr->form));
23177 return {};
23178 }
23179
23180 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23181 * the value held by the attribute is not constant. */
23182
23183 static LONGEST
23184 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23185 {
23186 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23187 return DW_SND (attr);
23188 else if (attr->form == DW_FORM_udata
23189 || attr->form == DW_FORM_data1
23190 || attr->form == DW_FORM_data2
23191 || attr->form == DW_FORM_data4
23192 || attr->form == DW_FORM_data8)
23193 return DW_UNSND (attr);
23194 else
23195 {
23196 /* For DW_FORM_data16 see attr_form_is_constant. */
23197 complaint (_("Attribute value is not a constant (%s)"),
23198 dwarf_form_name (attr->form));
23199 return default_value;
23200 }
23201 }
23202
23203 /* Follow reference or signature attribute ATTR of SRC_DIE.
23204 On entry *REF_CU is the CU of SRC_DIE.
23205 On exit *REF_CU is the CU of the result. */
23206
23207 static struct die_info *
23208 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23209 struct dwarf2_cu **ref_cu)
23210 {
23211 struct die_info *die;
23212
23213 if (attr_form_is_ref (attr))
23214 die = follow_die_ref (src_die, attr, ref_cu);
23215 else if (attr->form == DW_FORM_ref_sig8)
23216 die = follow_die_sig (src_die, attr, ref_cu);
23217 else
23218 {
23219 dump_die_for_error (src_die);
23220 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23221 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23222 }
23223
23224 return die;
23225 }
23226
23227 /* Follow reference OFFSET.
23228 On entry *REF_CU is the CU of the source die referencing OFFSET.
23229 On exit *REF_CU is the CU of the result.
23230 Returns NULL if OFFSET is invalid. */
23231
23232 static struct die_info *
23233 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23234 struct dwarf2_cu **ref_cu)
23235 {
23236 struct die_info temp_die;
23237 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23238 struct dwarf2_per_objfile *dwarf2_per_objfile
23239 = cu->per_cu->dwarf2_per_objfile;
23240
23241 gdb_assert (cu->per_cu != NULL);
23242
23243 target_cu = cu;
23244
23245 if (cu->per_cu->is_debug_types)
23246 {
23247 /* .debug_types CUs cannot reference anything outside their CU.
23248 If they need to, they have to reference a signatured type via
23249 DW_FORM_ref_sig8. */
23250 if (!offset_in_cu_p (&cu->header, sect_off))
23251 return NULL;
23252 }
23253 else if (offset_in_dwz != cu->per_cu->is_dwz
23254 || !offset_in_cu_p (&cu->header, sect_off))
23255 {
23256 struct dwarf2_per_cu_data *per_cu;
23257
23258 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23259 dwarf2_per_objfile);
23260
23261 /* If necessary, add it to the queue and load its DIEs. */
23262 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23263 load_full_comp_unit (per_cu, false, cu->language);
23264
23265 target_cu = per_cu->cu;
23266 }
23267 else if (cu->dies == NULL)
23268 {
23269 /* We're loading full DIEs during partial symbol reading. */
23270 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23271 load_full_comp_unit (cu->per_cu, false, language_minimal);
23272 }
23273
23274 *ref_cu = target_cu;
23275 temp_die.sect_off = sect_off;
23276
23277 if (target_cu != cu)
23278 target_cu->ancestor = cu;
23279
23280 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23281 &temp_die,
23282 to_underlying (sect_off));
23283 }
23284
23285 /* Follow reference attribute ATTR of SRC_DIE.
23286 On entry *REF_CU is the CU of SRC_DIE.
23287 On exit *REF_CU is the CU of the result. */
23288
23289 static struct die_info *
23290 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23291 struct dwarf2_cu **ref_cu)
23292 {
23293 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23294 struct dwarf2_cu *cu = *ref_cu;
23295 struct die_info *die;
23296
23297 die = follow_die_offset (sect_off,
23298 (attr->form == DW_FORM_GNU_ref_alt
23299 || cu->per_cu->is_dwz),
23300 ref_cu);
23301 if (!die)
23302 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23303 "at %s [in module %s]"),
23304 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23305 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23306
23307 return die;
23308 }
23309
23310 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23311 Returned value is intended for DW_OP_call*. Returned
23312 dwarf2_locexpr_baton->data has lifetime of
23313 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23314
23315 struct dwarf2_locexpr_baton
23316 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23317 struct dwarf2_per_cu_data *per_cu,
23318 CORE_ADDR (*get_frame_pc) (void *baton),
23319 void *baton, bool resolve_abstract_p)
23320 {
23321 struct dwarf2_cu *cu;
23322 struct die_info *die;
23323 struct attribute *attr;
23324 struct dwarf2_locexpr_baton retval;
23325 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23326 struct objfile *objfile = dwarf2_per_objfile->objfile;
23327
23328 if (per_cu->cu == NULL)
23329 load_cu (per_cu, false);
23330 cu = per_cu->cu;
23331 if (cu == NULL)
23332 {
23333 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23334 Instead just throw an error, not much else we can do. */
23335 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23336 sect_offset_str (sect_off), objfile_name (objfile));
23337 }
23338
23339 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23340 if (!die)
23341 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23342 sect_offset_str (sect_off), objfile_name (objfile));
23343
23344 attr = dwarf2_attr (die, DW_AT_location, cu);
23345 if (!attr && resolve_abstract_p
23346 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
23347 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23348 {
23349 CORE_ADDR pc = (*get_frame_pc) (baton);
23350 CORE_ADDR baseaddr
23351 = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23352 struct gdbarch *gdbarch = get_objfile_arch (objfile);
23353
23354 for (const auto &cand_off
23355 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
23356 {
23357 struct dwarf2_cu *cand_cu = cu;
23358 struct die_info *cand
23359 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23360 if (!cand
23361 || !cand->parent
23362 || cand->parent->tag != DW_TAG_subprogram)
23363 continue;
23364
23365 CORE_ADDR pc_low, pc_high;
23366 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23367 if (pc_low == ((CORE_ADDR) -1))
23368 continue;
23369 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23370 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23371 if (!(pc_low <= pc && pc < pc_high))
23372 continue;
23373
23374 die = cand;
23375 attr = dwarf2_attr (die, DW_AT_location, cu);
23376 break;
23377 }
23378 }
23379
23380 if (!attr)
23381 {
23382 /* DWARF: "If there is no such attribute, then there is no effect.".
23383 DATA is ignored if SIZE is 0. */
23384
23385 retval.data = NULL;
23386 retval.size = 0;
23387 }
23388 else if (attr_form_is_section_offset (attr))
23389 {
23390 struct dwarf2_loclist_baton loclist_baton;
23391 CORE_ADDR pc = (*get_frame_pc) (baton);
23392 size_t size;
23393
23394 fill_in_loclist_baton (cu, &loclist_baton, attr);
23395
23396 retval.data = dwarf2_find_location_expression (&loclist_baton,
23397 &size, pc);
23398 retval.size = size;
23399 }
23400 else
23401 {
23402 if (!attr_form_is_block (attr))
23403 error (_("Dwarf Error: DIE at %s referenced in module %s "
23404 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23405 sect_offset_str (sect_off), objfile_name (objfile));
23406
23407 retval.data = DW_BLOCK (attr)->data;
23408 retval.size = DW_BLOCK (attr)->size;
23409 }
23410 retval.per_cu = cu->per_cu;
23411
23412 age_cached_comp_units (dwarf2_per_objfile);
23413
23414 return retval;
23415 }
23416
23417 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23418 offset. */
23419
23420 struct dwarf2_locexpr_baton
23421 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23422 struct dwarf2_per_cu_data *per_cu,
23423 CORE_ADDR (*get_frame_pc) (void *baton),
23424 void *baton)
23425 {
23426 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23427
23428 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23429 }
23430
23431 /* Write a constant of a given type as target-ordered bytes into
23432 OBSTACK. */
23433
23434 static const gdb_byte *
23435 write_constant_as_bytes (struct obstack *obstack,
23436 enum bfd_endian byte_order,
23437 struct type *type,
23438 ULONGEST value,
23439 LONGEST *len)
23440 {
23441 gdb_byte *result;
23442
23443 *len = TYPE_LENGTH (type);
23444 result = (gdb_byte *) obstack_alloc (obstack, *len);
23445 store_unsigned_integer (result, *len, byte_order, value);
23446
23447 return result;
23448 }
23449
23450 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23451 pointer to the constant bytes and set LEN to the length of the
23452 data. If memory is needed, allocate it on OBSTACK. If the DIE
23453 does not have a DW_AT_const_value, return NULL. */
23454
23455 const gdb_byte *
23456 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23457 struct dwarf2_per_cu_data *per_cu,
23458 struct obstack *obstack,
23459 LONGEST *len)
23460 {
23461 struct dwarf2_cu *cu;
23462 struct die_info *die;
23463 struct attribute *attr;
23464 const gdb_byte *result = NULL;
23465 struct type *type;
23466 LONGEST value;
23467 enum bfd_endian byte_order;
23468 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23469
23470 if (per_cu->cu == NULL)
23471 load_cu (per_cu, false);
23472 cu = per_cu->cu;
23473 if (cu == NULL)
23474 {
23475 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23476 Instead just throw an error, not much else we can do. */
23477 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23478 sect_offset_str (sect_off), objfile_name (objfile));
23479 }
23480
23481 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23482 if (!die)
23483 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23484 sect_offset_str (sect_off), objfile_name (objfile));
23485
23486 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23487 if (attr == NULL)
23488 return NULL;
23489
23490 byte_order = (bfd_big_endian (objfile->obfd)
23491 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23492
23493 switch (attr->form)
23494 {
23495 case DW_FORM_addr:
23496 case DW_FORM_addrx:
23497 case DW_FORM_GNU_addr_index:
23498 {
23499 gdb_byte *tem;
23500
23501 *len = cu->header.addr_size;
23502 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23503 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23504 result = tem;
23505 }
23506 break;
23507 case DW_FORM_string:
23508 case DW_FORM_strp:
23509 case DW_FORM_strx:
23510 case DW_FORM_GNU_str_index:
23511 case DW_FORM_GNU_strp_alt:
23512 /* DW_STRING is already allocated on the objfile obstack, point
23513 directly to it. */
23514 result = (const gdb_byte *) DW_STRING (attr);
23515 *len = strlen (DW_STRING (attr));
23516 break;
23517 case DW_FORM_block1:
23518 case DW_FORM_block2:
23519 case DW_FORM_block4:
23520 case DW_FORM_block:
23521 case DW_FORM_exprloc:
23522 case DW_FORM_data16:
23523 result = DW_BLOCK (attr)->data;
23524 *len = DW_BLOCK (attr)->size;
23525 break;
23526
23527 /* The DW_AT_const_value attributes are supposed to carry the
23528 symbol's value "represented as it would be on the target
23529 architecture." By the time we get here, it's already been
23530 converted to host endianness, so we just need to sign- or
23531 zero-extend it as appropriate. */
23532 case DW_FORM_data1:
23533 type = die_type (die, cu);
23534 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23535 if (result == NULL)
23536 result = write_constant_as_bytes (obstack, byte_order,
23537 type, value, len);
23538 break;
23539 case DW_FORM_data2:
23540 type = die_type (die, cu);
23541 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23542 if (result == NULL)
23543 result = write_constant_as_bytes (obstack, byte_order,
23544 type, value, len);
23545 break;
23546 case DW_FORM_data4:
23547 type = die_type (die, cu);
23548 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23549 if (result == NULL)
23550 result = write_constant_as_bytes (obstack, byte_order,
23551 type, value, len);
23552 break;
23553 case DW_FORM_data8:
23554 type = die_type (die, cu);
23555 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23556 if (result == NULL)
23557 result = write_constant_as_bytes (obstack, byte_order,
23558 type, value, len);
23559 break;
23560
23561 case DW_FORM_sdata:
23562 case DW_FORM_implicit_const:
23563 type = die_type (die, cu);
23564 result = write_constant_as_bytes (obstack, byte_order,
23565 type, DW_SND (attr), len);
23566 break;
23567
23568 case DW_FORM_udata:
23569 type = die_type (die, cu);
23570 result = write_constant_as_bytes (obstack, byte_order,
23571 type, DW_UNSND (attr), len);
23572 break;
23573
23574 default:
23575 complaint (_("unsupported const value attribute form: '%s'"),
23576 dwarf_form_name (attr->form));
23577 break;
23578 }
23579
23580 return result;
23581 }
23582
23583 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23584 valid type for this die is found. */
23585
23586 struct type *
23587 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23588 struct dwarf2_per_cu_data *per_cu)
23589 {
23590 struct dwarf2_cu *cu;
23591 struct die_info *die;
23592
23593 if (per_cu->cu == NULL)
23594 load_cu (per_cu, false);
23595 cu = per_cu->cu;
23596 if (!cu)
23597 return NULL;
23598
23599 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23600 if (!die)
23601 return NULL;
23602
23603 return die_type (die, cu);
23604 }
23605
23606 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23607 PER_CU. */
23608
23609 struct type *
23610 dwarf2_get_die_type (cu_offset die_offset,
23611 struct dwarf2_per_cu_data *per_cu)
23612 {
23613 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23614 return get_die_type_at_offset (die_offset_sect, per_cu);
23615 }
23616
23617 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23618 On entry *REF_CU is the CU of SRC_DIE.
23619 On exit *REF_CU is the CU of the result.
23620 Returns NULL if the referenced DIE isn't found. */
23621
23622 static struct die_info *
23623 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23624 struct dwarf2_cu **ref_cu)
23625 {
23626 struct die_info temp_die;
23627 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23628 struct die_info *die;
23629
23630 /* While it might be nice to assert sig_type->type == NULL here,
23631 we can get here for DW_AT_imported_declaration where we need
23632 the DIE not the type. */
23633
23634 /* If necessary, add it to the queue and load its DIEs. */
23635
23636 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23637 read_signatured_type (sig_type);
23638
23639 sig_cu = sig_type->per_cu.cu;
23640 gdb_assert (sig_cu != NULL);
23641 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23642 temp_die.sect_off = sig_type->type_offset_in_section;
23643 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23644 to_underlying (temp_die.sect_off));
23645 if (die)
23646 {
23647 struct dwarf2_per_objfile *dwarf2_per_objfile
23648 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23649
23650 /* For .gdb_index version 7 keep track of included TUs.
23651 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23652 if (dwarf2_per_objfile->index_table != NULL
23653 && dwarf2_per_objfile->index_table->version <= 7)
23654 {
23655 VEC_safe_push (dwarf2_per_cu_ptr,
23656 (*ref_cu)->per_cu->imported_symtabs,
23657 sig_cu->per_cu);
23658 }
23659
23660 *ref_cu = sig_cu;
23661 if (sig_cu != cu)
23662 sig_cu->ancestor = cu;
23663
23664 return die;
23665 }
23666
23667 return NULL;
23668 }
23669
23670 /* Follow signatured type referenced by ATTR in SRC_DIE.
23671 On entry *REF_CU is the CU of SRC_DIE.
23672 On exit *REF_CU is the CU of the result.
23673 The result is the DIE of the type.
23674 If the referenced type cannot be found an error is thrown. */
23675
23676 static struct die_info *
23677 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23678 struct dwarf2_cu **ref_cu)
23679 {
23680 ULONGEST signature = DW_SIGNATURE (attr);
23681 struct signatured_type *sig_type;
23682 struct die_info *die;
23683
23684 gdb_assert (attr->form == DW_FORM_ref_sig8);
23685
23686 sig_type = lookup_signatured_type (*ref_cu, signature);
23687 /* sig_type will be NULL if the signatured type is missing from
23688 the debug info. */
23689 if (sig_type == NULL)
23690 {
23691 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23692 " from DIE at %s [in module %s]"),
23693 hex_string (signature), sect_offset_str (src_die->sect_off),
23694 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23695 }
23696
23697 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23698 if (die == NULL)
23699 {
23700 dump_die_for_error (src_die);
23701 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23702 " from DIE at %s [in module %s]"),
23703 hex_string (signature), sect_offset_str (src_die->sect_off),
23704 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23705 }
23706
23707 return die;
23708 }
23709
23710 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23711 reading in and processing the type unit if necessary. */
23712
23713 static struct type *
23714 get_signatured_type (struct die_info *die, ULONGEST signature,
23715 struct dwarf2_cu *cu)
23716 {
23717 struct dwarf2_per_objfile *dwarf2_per_objfile
23718 = cu->per_cu->dwarf2_per_objfile;
23719 struct signatured_type *sig_type;
23720 struct dwarf2_cu *type_cu;
23721 struct die_info *type_die;
23722 struct type *type;
23723
23724 sig_type = lookup_signatured_type (cu, signature);
23725 /* sig_type will be NULL if the signatured type is missing from
23726 the debug info. */
23727 if (sig_type == NULL)
23728 {
23729 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23730 " from DIE at %s [in module %s]"),
23731 hex_string (signature), sect_offset_str (die->sect_off),
23732 objfile_name (dwarf2_per_objfile->objfile));
23733 return build_error_marker_type (cu, die);
23734 }
23735
23736 /* If we already know the type we're done. */
23737 if (sig_type->type != NULL)
23738 return sig_type->type;
23739
23740 type_cu = cu;
23741 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23742 if (type_die != NULL)
23743 {
23744 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23745 is created. This is important, for example, because for c++ classes
23746 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23747 type = read_type_die (type_die, type_cu);
23748 if (type == NULL)
23749 {
23750 complaint (_("Dwarf Error: Cannot build signatured type %s"
23751 " referenced from DIE at %s [in module %s]"),
23752 hex_string (signature), sect_offset_str (die->sect_off),
23753 objfile_name (dwarf2_per_objfile->objfile));
23754 type = build_error_marker_type (cu, die);
23755 }
23756 }
23757 else
23758 {
23759 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23760 " from DIE at %s [in module %s]"),
23761 hex_string (signature), sect_offset_str (die->sect_off),
23762 objfile_name (dwarf2_per_objfile->objfile));
23763 type = build_error_marker_type (cu, die);
23764 }
23765 sig_type->type = type;
23766
23767 return type;
23768 }
23769
23770 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23771 reading in and processing the type unit if necessary. */
23772
23773 static struct type *
23774 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23775 struct dwarf2_cu *cu) /* ARI: editCase function */
23776 {
23777 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23778 if (attr_form_is_ref (attr))
23779 {
23780 struct dwarf2_cu *type_cu = cu;
23781 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23782
23783 return read_type_die (type_die, type_cu);
23784 }
23785 else if (attr->form == DW_FORM_ref_sig8)
23786 {
23787 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23788 }
23789 else
23790 {
23791 struct dwarf2_per_objfile *dwarf2_per_objfile
23792 = cu->per_cu->dwarf2_per_objfile;
23793
23794 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23795 " at %s [in module %s]"),
23796 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23797 objfile_name (dwarf2_per_objfile->objfile));
23798 return build_error_marker_type (cu, die);
23799 }
23800 }
23801
23802 /* Load the DIEs associated with type unit PER_CU into memory. */
23803
23804 static void
23805 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23806 {
23807 struct signatured_type *sig_type;
23808
23809 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23810 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23811
23812 /* We have the per_cu, but we need the signatured_type.
23813 Fortunately this is an easy translation. */
23814 gdb_assert (per_cu->is_debug_types);
23815 sig_type = (struct signatured_type *) per_cu;
23816
23817 gdb_assert (per_cu->cu == NULL);
23818
23819 read_signatured_type (sig_type);
23820
23821 gdb_assert (per_cu->cu != NULL);
23822 }
23823
23824 /* die_reader_func for read_signatured_type.
23825 This is identical to load_full_comp_unit_reader,
23826 but is kept separate for now. */
23827
23828 static void
23829 read_signatured_type_reader (const struct die_reader_specs *reader,
23830 const gdb_byte *info_ptr,
23831 struct die_info *comp_unit_die,
23832 int has_children,
23833 void *data)
23834 {
23835 struct dwarf2_cu *cu = reader->cu;
23836
23837 gdb_assert (cu->die_hash == NULL);
23838 cu->die_hash =
23839 htab_create_alloc_ex (cu->header.length / 12,
23840 die_hash,
23841 die_eq,
23842 NULL,
23843 &cu->comp_unit_obstack,
23844 hashtab_obstack_allocate,
23845 dummy_obstack_deallocate);
23846
23847 if (has_children)
23848 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23849 &info_ptr, comp_unit_die);
23850 cu->dies = comp_unit_die;
23851 /* comp_unit_die is not stored in die_hash, no need. */
23852
23853 /* We try not to read any attributes in this function, because not
23854 all CUs needed for references have been loaded yet, and symbol
23855 table processing isn't initialized. But we have to set the CU language,
23856 or we won't be able to build types correctly.
23857 Similarly, if we do not read the producer, we can not apply
23858 producer-specific interpretation. */
23859 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23860 }
23861
23862 /* Read in a signatured type and build its CU and DIEs.
23863 If the type is a stub for the real type in a DWO file,
23864 read in the real type from the DWO file as well. */
23865
23866 static void
23867 read_signatured_type (struct signatured_type *sig_type)
23868 {
23869 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23870
23871 gdb_assert (per_cu->is_debug_types);
23872 gdb_assert (per_cu->cu == NULL);
23873
23874 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
23875 read_signatured_type_reader, NULL);
23876 sig_type->per_cu.tu_read = 1;
23877 }
23878
23879 /* Decode simple location descriptions.
23880 Given a pointer to a dwarf block that defines a location, compute
23881 the location and return the value.
23882
23883 NOTE drow/2003-11-18: This function is called in two situations
23884 now: for the address of static or global variables (partial symbols
23885 only) and for offsets into structures which are expected to be
23886 (more or less) constant. The partial symbol case should go away,
23887 and only the constant case should remain. That will let this
23888 function complain more accurately. A few special modes are allowed
23889 without complaint for global variables (for instance, global
23890 register values and thread-local values).
23891
23892 A location description containing no operations indicates that the
23893 object is optimized out. The return value is 0 for that case.
23894 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23895 callers will only want a very basic result and this can become a
23896 complaint.
23897
23898 Note that stack[0] is unused except as a default error return. */
23899
23900 static CORE_ADDR
23901 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23902 {
23903 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23904 size_t i;
23905 size_t size = blk->size;
23906 const gdb_byte *data = blk->data;
23907 CORE_ADDR stack[64];
23908 int stacki;
23909 unsigned int bytes_read, unsnd;
23910 gdb_byte op;
23911
23912 i = 0;
23913 stacki = 0;
23914 stack[stacki] = 0;
23915 stack[++stacki] = 0;
23916
23917 while (i < size)
23918 {
23919 op = data[i++];
23920 switch (op)
23921 {
23922 case DW_OP_lit0:
23923 case DW_OP_lit1:
23924 case DW_OP_lit2:
23925 case DW_OP_lit3:
23926 case DW_OP_lit4:
23927 case DW_OP_lit5:
23928 case DW_OP_lit6:
23929 case DW_OP_lit7:
23930 case DW_OP_lit8:
23931 case DW_OP_lit9:
23932 case DW_OP_lit10:
23933 case DW_OP_lit11:
23934 case DW_OP_lit12:
23935 case DW_OP_lit13:
23936 case DW_OP_lit14:
23937 case DW_OP_lit15:
23938 case DW_OP_lit16:
23939 case DW_OP_lit17:
23940 case DW_OP_lit18:
23941 case DW_OP_lit19:
23942 case DW_OP_lit20:
23943 case DW_OP_lit21:
23944 case DW_OP_lit22:
23945 case DW_OP_lit23:
23946 case DW_OP_lit24:
23947 case DW_OP_lit25:
23948 case DW_OP_lit26:
23949 case DW_OP_lit27:
23950 case DW_OP_lit28:
23951 case DW_OP_lit29:
23952 case DW_OP_lit30:
23953 case DW_OP_lit31:
23954 stack[++stacki] = op - DW_OP_lit0;
23955 break;
23956
23957 case DW_OP_reg0:
23958 case DW_OP_reg1:
23959 case DW_OP_reg2:
23960 case DW_OP_reg3:
23961 case DW_OP_reg4:
23962 case DW_OP_reg5:
23963 case DW_OP_reg6:
23964 case DW_OP_reg7:
23965 case DW_OP_reg8:
23966 case DW_OP_reg9:
23967 case DW_OP_reg10:
23968 case DW_OP_reg11:
23969 case DW_OP_reg12:
23970 case DW_OP_reg13:
23971 case DW_OP_reg14:
23972 case DW_OP_reg15:
23973 case DW_OP_reg16:
23974 case DW_OP_reg17:
23975 case DW_OP_reg18:
23976 case DW_OP_reg19:
23977 case DW_OP_reg20:
23978 case DW_OP_reg21:
23979 case DW_OP_reg22:
23980 case DW_OP_reg23:
23981 case DW_OP_reg24:
23982 case DW_OP_reg25:
23983 case DW_OP_reg26:
23984 case DW_OP_reg27:
23985 case DW_OP_reg28:
23986 case DW_OP_reg29:
23987 case DW_OP_reg30:
23988 case DW_OP_reg31:
23989 stack[++stacki] = op - DW_OP_reg0;
23990 if (i < size)
23991 dwarf2_complex_location_expr_complaint ();
23992 break;
23993
23994 case DW_OP_regx:
23995 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23996 i += bytes_read;
23997 stack[++stacki] = unsnd;
23998 if (i < size)
23999 dwarf2_complex_location_expr_complaint ();
24000 break;
24001
24002 case DW_OP_addr:
24003 stack[++stacki] = read_address (objfile->obfd, &data[i],
24004 cu, &bytes_read);
24005 i += bytes_read;
24006 break;
24007
24008 case DW_OP_const1u:
24009 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24010 i += 1;
24011 break;
24012
24013 case DW_OP_const1s:
24014 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24015 i += 1;
24016 break;
24017
24018 case DW_OP_const2u:
24019 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24020 i += 2;
24021 break;
24022
24023 case DW_OP_const2s:
24024 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24025 i += 2;
24026 break;
24027
24028 case DW_OP_const4u:
24029 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24030 i += 4;
24031 break;
24032
24033 case DW_OP_const4s:
24034 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24035 i += 4;
24036 break;
24037
24038 case DW_OP_const8u:
24039 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24040 i += 8;
24041 break;
24042
24043 case DW_OP_constu:
24044 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24045 &bytes_read);
24046 i += bytes_read;
24047 break;
24048
24049 case DW_OP_consts:
24050 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24051 i += bytes_read;
24052 break;
24053
24054 case DW_OP_dup:
24055 stack[stacki + 1] = stack[stacki];
24056 stacki++;
24057 break;
24058
24059 case DW_OP_plus:
24060 stack[stacki - 1] += stack[stacki];
24061 stacki--;
24062 break;
24063
24064 case DW_OP_plus_uconst:
24065 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24066 &bytes_read);
24067 i += bytes_read;
24068 break;
24069
24070 case DW_OP_minus:
24071 stack[stacki - 1] -= stack[stacki];
24072 stacki--;
24073 break;
24074
24075 case DW_OP_deref:
24076 /* If we're not the last op, then we definitely can't encode
24077 this using GDB's address_class enum. This is valid for partial
24078 global symbols, although the variable's address will be bogus
24079 in the psymtab. */
24080 if (i < size)
24081 dwarf2_complex_location_expr_complaint ();
24082 break;
24083
24084 case DW_OP_GNU_push_tls_address:
24085 case DW_OP_form_tls_address:
24086 /* The top of the stack has the offset from the beginning
24087 of the thread control block at which the variable is located. */
24088 /* Nothing should follow this operator, so the top of stack would
24089 be returned. */
24090 /* This is valid for partial global symbols, but the variable's
24091 address will be bogus in the psymtab. Make it always at least
24092 non-zero to not look as a variable garbage collected by linker
24093 which have DW_OP_addr 0. */
24094 if (i < size)
24095 dwarf2_complex_location_expr_complaint ();
24096 stack[stacki]++;
24097 break;
24098
24099 case DW_OP_GNU_uninit:
24100 break;
24101
24102 case DW_OP_addrx:
24103 case DW_OP_GNU_addr_index:
24104 case DW_OP_GNU_const_index:
24105 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24106 &bytes_read);
24107 i += bytes_read;
24108 break;
24109
24110 default:
24111 {
24112 const char *name = get_DW_OP_name (op);
24113
24114 if (name)
24115 complaint (_("unsupported stack op: '%s'"),
24116 name);
24117 else
24118 complaint (_("unsupported stack op: '%02x'"),
24119 op);
24120 }
24121
24122 return (stack[stacki]);
24123 }
24124
24125 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24126 outside of the allocated space. Also enforce minimum>0. */
24127 if (stacki >= ARRAY_SIZE (stack) - 1)
24128 {
24129 complaint (_("location description stack overflow"));
24130 return 0;
24131 }
24132
24133 if (stacki <= 0)
24134 {
24135 complaint (_("location description stack underflow"));
24136 return 0;
24137 }
24138 }
24139 return (stack[stacki]);
24140 }
24141
24142 /* memory allocation interface */
24143
24144 static struct dwarf_block *
24145 dwarf_alloc_block (struct dwarf2_cu *cu)
24146 {
24147 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24148 }
24149
24150 static struct die_info *
24151 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24152 {
24153 struct die_info *die;
24154 size_t size = sizeof (struct die_info);
24155
24156 if (num_attrs > 1)
24157 size += (num_attrs - 1) * sizeof (struct attribute);
24158
24159 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24160 memset (die, 0, sizeof (struct die_info));
24161 return (die);
24162 }
24163
24164 \f
24165 /* Macro support. */
24166
24167 /* Return file name relative to the compilation directory of file number I in
24168 *LH's file name table. The result is allocated using xmalloc; the caller is
24169 responsible for freeing it. */
24170
24171 static char *
24172 file_file_name (int file, struct line_header *lh)
24173 {
24174 /* Is the file number a valid index into the line header's file name
24175 table? Remember that file numbers start with one, not zero. */
24176 if (1 <= file && file <= lh->file_names.size ())
24177 {
24178 const file_entry &fe = lh->file_names[file - 1];
24179
24180 if (!IS_ABSOLUTE_PATH (fe.name))
24181 {
24182 const char *dir = fe.include_dir (lh);
24183 if (dir != NULL)
24184 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
24185 }
24186 return xstrdup (fe.name);
24187 }
24188 else
24189 {
24190 /* The compiler produced a bogus file number. We can at least
24191 record the macro definitions made in the file, even if we
24192 won't be able to find the file by name. */
24193 char fake_name[80];
24194
24195 xsnprintf (fake_name, sizeof (fake_name),
24196 "<bad macro file number %d>", file);
24197
24198 complaint (_("bad file number in macro information (%d)"),
24199 file);
24200
24201 return xstrdup (fake_name);
24202 }
24203 }
24204
24205 /* Return the full name of file number I in *LH's file name table.
24206 Use COMP_DIR as the name of the current directory of the
24207 compilation. The result is allocated using xmalloc; the caller is
24208 responsible for freeing it. */
24209 static char *
24210 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24211 {
24212 /* Is the file number a valid index into the line header's file name
24213 table? Remember that file numbers start with one, not zero. */
24214 if (1 <= file && file <= lh->file_names.size ())
24215 {
24216 char *relative = file_file_name (file, lh);
24217
24218 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24219 return relative;
24220 return reconcat (relative, comp_dir, SLASH_STRING,
24221 relative, (char *) NULL);
24222 }
24223 else
24224 return file_file_name (file, lh);
24225 }
24226
24227
24228 static struct macro_source_file *
24229 macro_start_file (struct dwarf2_cu *cu,
24230 int file, int line,
24231 struct macro_source_file *current_file,
24232 struct line_header *lh)
24233 {
24234 /* File name relative to the compilation directory of this source file. */
24235 char *file_name = file_file_name (file, lh);
24236
24237 if (! current_file)
24238 {
24239 /* Note: We don't create a macro table for this compilation unit
24240 at all until we actually get a filename. */
24241 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
24242
24243 /* If we have no current file, then this must be the start_file
24244 directive for the compilation unit's main source file. */
24245 current_file = macro_set_main (macro_table, file_name);
24246 macro_define_special (macro_table);
24247 }
24248 else
24249 current_file = macro_include (current_file, line, file_name);
24250
24251 xfree (file_name);
24252
24253 return current_file;
24254 }
24255
24256 static const char *
24257 consume_improper_spaces (const char *p, const char *body)
24258 {
24259 if (*p == ' ')
24260 {
24261 complaint (_("macro definition contains spaces "
24262 "in formal argument list:\n`%s'"),
24263 body);
24264
24265 while (*p == ' ')
24266 p++;
24267 }
24268
24269 return p;
24270 }
24271
24272
24273 static void
24274 parse_macro_definition (struct macro_source_file *file, int line,
24275 const char *body)
24276 {
24277 const char *p;
24278
24279 /* The body string takes one of two forms. For object-like macro
24280 definitions, it should be:
24281
24282 <macro name> " " <definition>
24283
24284 For function-like macro definitions, it should be:
24285
24286 <macro name> "() " <definition>
24287 or
24288 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24289
24290 Spaces may appear only where explicitly indicated, and in the
24291 <definition>.
24292
24293 The Dwarf 2 spec says that an object-like macro's name is always
24294 followed by a space, but versions of GCC around March 2002 omit
24295 the space when the macro's definition is the empty string.
24296
24297 The Dwarf 2 spec says that there should be no spaces between the
24298 formal arguments in a function-like macro's formal argument list,
24299 but versions of GCC around March 2002 include spaces after the
24300 commas. */
24301
24302
24303 /* Find the extent of the macro name. The macro name is terminated
24304 by either a space or null character (for an object-like macro) or
24305 an opening paren (for a function-like macro). */
24306 for (p = body; *p; p++)
24307 if (*p == ' ' || *p == '(')
24308 break;
24309
24310 if (*p == ' ' || *p == '\0')
24311 {
24312 /* It's an object-like macro. */
24313 int name_len = p - body;
24314 char *name = savestring (body, name_len);
24315 const char *replacement;
24316
24317 if (*p == ' ')
24318 replacement = body + name_len + 1;
24319 else
24320 {
24321 dwarf2_macro_malformed_definition_complaint (body);
24322 replacement = body + name_len;
24323 }
24324
24325 macro_define_object (file, line, name, replacement);
24326
24327 xfree (name);
24328 }
24329 else if (*p == '(')
24330 {
24331 /* It's a function-like macro. */
24332 char *name = savestring (body, p - body);
24333 int argc = 0;
24334 int argv_size = 1;
24335 char **argv = XNEWVEC (char *, argv_size);
24336
24337 p++;
24338
24339 p = consume_improper_spaces (p, body);
24340
24341 /* Parse the formal argument list. */
24342 while (*p && *p != ')')
24343 {
24344 /* Find the extent of the current argument name. */
24345 const char *arg_start = p;
24346
24347 while (*p && *p != ',' && *p != ')' && *p != ' ')
24348 p++;
24349
24350 if (! *p || p == arg_start)
24351 dwarf2_macro_malformed_definition_complaint (body);
24352 else
24353 {
24354 /* Make sure argv has room for the new argument. */
24355 if (argc >= argv_size)
24356 {
24357 argv_size *= 2;
24358 argv = XRESIZEVEC (char *, argv, argv_size);
24359 }
24360
24361 argv[argc++] = savestring (arg_start, p - arg_start);
24362 }
24363
24364 p = consume_improper_spaces (p, body);
24365
24366 /* Consume the comma, if present. */
24367 if (*p == ',')
24368 {
24369 p++;
24370
24371 p = consume_improper_spaces (p, body);
24372 }
24373 }
24374
24375 if (*p == ')')
24376 {
24377 p++;
24378
24379 if (*p == ' ')
24380 /* Perfectly formed definition, no complaints. */
24381 macro_define_function (file, line, name,
24382 argc, (const char **) argv,
24383 p + 1);
24384 else if (*p == '\0')
24385 {
24386 /* Complain, but do define it. */
24387 dwarf2_macro_malformed_definition_complaint (body);
24388 macro_define_function (file, line, name,
24389 argc, (const char **) argv,
24390 p);
24391 }
24392 else
24393 /* Just complain. */
24394 dwarf2_macro_malformed_definition_complaint (body);
24395 }
24396 else
24397 /* Just complain. */
24398 dwarf2_macro_malformed_definition_complaint (body);
24399
24400 xfree (name);
24401 {
24402 int i;
24403
24404 for (i = 0; i < argc; i++)
24405 xfree (argv[i]);
24406 }
24407 xfree (argv);
24408 }
24409 else
24410 dwarf2_macro_malformed_definition_complaint (body);
24411 }
24412
24413 /* Skip some bytes from BYTES according to the form given in FORM.
24414 Returns the new pointer. */
24415
24416 static const gdb_byte *
24417 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24418 enum dwarf_form form,
24419 unsigned int offset_size,
24420 struct dwarf2_section_info *section)
24421 {
24422 unsigned int bytes_read;
24423
24424 switch (form)
24425 {
24426 case DW_FORM_data1:
24427 case DW_FORM_flag:
24428 ++bytes;
24429 break;
24430
24431 case DW_FORM_data2:
24432 bytes += 2;
24433 break;
24434
24435 case DW_FORM_data4:
24436 bytes += 4;
24437 break;
24438
24439 case DW_FORM_data8:
24440 bytes += 8;
24441 break;
24442
24443 case DW_FORM_data16:
24444 bytes += 16;
24445 break;
24446
24447 case DW_FORM_string:
24448 read_direct_string (abfd, bytes, &bytes_read);
24449 bytes += bytes_read;
24450 break;
24451
24452 case DW_FORM_sec_offset:
24453 case DW_FORM_strp:
24454 case DW_FORM_GNU_strp_alt:
24455 bytes += offset_size;
24456 break;
24457
24458 case DW_FORM_block:
24459 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24460 bytes += bytes_read;
24461 break;
24462
24463 case DW_FORM_block1:
24464 bytes += 1 + read_1_byte (abfd, bytes);
24465 break;
24466 case DW_FORM_block2:
24467 bytes += 2 + read_2_bytes (abfd, bytes);
24468 break;
24469 case DW_FORM_block4:
24470 bytes += 4 + read_4_bytes (abfd, bytes);
24471 break;
24472
24473 case DW_FORM_addrx:
24474 case DW_FORM_sdata:
24475 case DW_FORM_strx:
24476 case DW_FORM_udata:
24477 case DW_FORM_GNU_addr_index:
24478 case DW_FORM_GNU_str_index:
24479 bytes = gdb_skip_leb128 (bytes, buffer_end);
24480 if (bytes == NULL)
24481 {
24482 dwarf2_section_buffer_overflow_complaint (section);
24483 return NULL;
24484 }
24485 break;
24486
24487 case DW_FORM_implicit_const:
24488 break;
24489
24490 default:
24491 {
24492 complaint (_("invalid form 0x%x in `%s'"),
24493 form, get_section_name (section));
24494 return NULL;
24495 }
24496 }
24497
24498 return bytes;
24499 }
24500
24501 /* A helper for dwarf_decode_macros that handles skipping an unknown
24502 opcode. Returns an updated pointer to the macro data buffer; or,
24503 on error, issues a complaint and returns NULL. */
24504
24505 static const gdb_byte *
24506 skip_unknown_opcode (unsigned int opcode,
24507 const gdb_byte **opcode_definitions,
24508 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24509 bfd *abfd,
24510 unsigned int offset_size,
24511 struct dwarf2_section_info *section)
24512 {
24513 unsigned int bytes_read, i;
24514 unsigned long arg;
24515 const gdb_byte *defn;
24516
24517 if (opcode_definitions[opcode] == NULL)
24518 {
24519 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24520 opcode);
24521 return NULL;
24522 }
24523
24524 defn = opcode_definitions[opcode];
24525 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24526 defn += bytes_read;
24527
24528 for (i = 0; i < arg; ++i)
24529 {
24530 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24531 (enum dwarf_form) defn[i], offset_size,
24532 section);
24533 if (mac_ptr == NULL)
24534 {
24535 /* skip_form_bytes already issued the complaint. */
24536 return NULL;
24537 }
24538 }
24539
24540 return mac_ptr;
24541 }
24542
24543 /* A helper function which parses the header of a macro section.
24544 If the macro section is the extended (for now called "GNU") type,
24545 then this updates *OFFSET_SIZE. Returns a pointer to just after
24546 the header, or issues a complaint and returns NULL on error. */
24547
24548 static const gdb_byte *
24549 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24550 bfd *abfd,
24551 const gdb_byte *mac_ptr,
24552 unsigned int *offset_size,
24553 int section_is_gnu)
24554 {
24555 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24556
24557 if (section_is_gnu)
24558 {
24559 unsigned int version, flags;
24560
24561 version = read_2_bytes (abfd, mac_ptr);
24562 if (version != 4 && version != 5)
24563 {
24564 complaint (_("unrecognized version `%d' in .debug_macro section"),
24565 version);
24566 return NULL;
24567 }
24568 mac_ptr += 2;
24569
24570 flags = read_1_byte (abfd, mac_ptr);
24571 ++mac_ptr;
24572 *offset_size = (flags & 1) ? 8 : 4;
24573
24574 if ((flags & 2) != 0)
24575 /* We don't need the line table offset. */
24576 mac_ptr += *offset_size;
24577
24578 /* Vendor opcode descriptions. */
24579 if ((flags & 4) != 0)
24580 {
24581 unsigned int i, count;
24582
24583 count = read_1_byte (abfd, mac_ptr);
24584 ++mac_ptr;
24585 for (i = 0; i < count; ++i)
24586 {
24587 unsigned int opcode, bytes_read;
24588 unsigned long arg;
24589
24590 opcode = read_1_byte (abfd, mac_ptr);
24591 ++mac_ptr;
24592 opcode_definitions[opcode] = mac_ptr;
24593 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24594 mac_ptr += bytes_read;
24595 mac_ptr += arg;
24596 }
24597 }
24598 }
24599
24600 return mac_ptr;
24601 }
24602
24603 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24604 including DW_MACRO_import. */
24605
24606 static void
24607 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24608 bfd *abfd,
24609 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24610 struct macro_source_file *current_file,
24611 struct line_header *lh,
24612 struct dwarf2_section_info *section,
24613 int section_is_gnu, int section_is_dwz,
24614 unsigned int offset_size,
24615 htab_t include_hash)
24616 {
24617 struct dwarf2_per_objfile *dwarf2_per_objfile
24618 = cu->per_cu->dwarf2_per_objfile;
24619 struct objfile *objfile = dwarf2_per_objfile->objfile;
24620 enum dwarf_macro_record_type macinfo_type;
24621 int at_commandline;
24622 const gdb_byte *opcode_definitions[256];
24623
24624 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24625 &offset_size, section_is_gnu);
24626 if (mac_ptr == NULL)
24627 {
24628 /* We already issued a complaint. */
24629 return;
24630 }
24631
24632 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24633 GDB is still reading the definitions from command line. First
24634 DW_MACINFO_start_file will need to be ignored as it was already executed
24635 to create CURRENT_FILE for the main source holding also the command line
24636 definitions. On first met DW_MACINFO_start_file this flag is reset to
24637 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24638
24639 at_commandline = 1;
24640
24641 do
24642 {
24643 /* Do we at least have room for a macinfo type byte? */
24644 if (mac_ptr >= mac_end)
24645 {
24646 dwarf2_section_buffer_overflow_complaint (section);
24647 break;
24648 }
24649
24650 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24651 mac_ptr++;
24652
24653 /* Note that we rely on the fact that the corresponding GNU and
24654 DWARF constants are the same. */
24655 DIAGNOSTIC_PUSH
24656 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24657 switch (macinfo_type)
24658 {
24659 /* A zero macinfo type indicates the end of the macro
24660 information. */
24661 case 0:
24662 break;
24663
24664 case DW_MACRO_define:
24665 case DW_MACRO_undef:
24666 case DW_MACRO_define_strp:
24667 case DW_MACRO_undef_strp:
24668 case DW_MACRO_define_sup:
24669 case DW_MACRO_undef_sup:
24670 {
24671 unsigned int bytes_read;
24672 int line;
24673 const char *body;
24674 int is_define;
24675
24676 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24677 mac_ptr += bytes_read;
24678
24679 if (macinfo_type == DW_MACRO_define
24680 || macinfo_type == DW_MACRO_undef)
24681 {
24682 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24683 mac_ptr += bytes_read;
24684 }
24685 else
24686 {
24687 LONGEST str_offset;
24688
24689 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24690 mac_ptr += offset_size;
24691
24692 if (macinfo_type == DW_MACRO_define_sup
24693 || macinfo_type == DW_MACRO_undef_sup
24694 || section_is_dwz)
24695 {
24696 struct dwz_file *dwz
24697 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24698
24699 body = read_indirect_string_from_dwz (objfile,
24700 dwz, str_offset);
24701 }
24702 else
24703 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24704 abfd, str_offset);
24705 }
24706
24707 is_define = (macinfo_type == DW_MACRO_define
24708 || macinfo_type == DW_MACRO_define_strp
24709 || macinfo_type == DW_MACRO_define_sup);
24710 if (! current_file)
24711 {
24712 /* DWARF violation as no main source is present. */
24713 complaint (_("debug info with no main source gives macro %s "
24714 "on line %d: %s"),
24715 is_define ? _("definition") : _("undefinition"),
24716 line, body);
24717 break;
24718 }
24719 if ((line == 0 && !at_commandline)
24720 || (line != 0 && at_commandline))
24721 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
24722 at_commandline ? _("command-line") : _("in-file"),
24723 is_define ? _("definition") : _("undefinition"),
24724 line == 0 ? _("zero") : _("non-zero"), line, body);
24725
24726 if (body == NULL)
24727 {
24728 /* Fedora's rpm-build's "debugedit" binary
24729 corrupted .debug_macro sections.
24730
24731 For more info, see
24732 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24733 complaint (_("debug info gives %s invalid macro %s "
24734 "without body (corrupted?) at line %d "
24735 "on file %s"),
24736 at_commandline ? _("command-line") : _("in-file"),
24737 is_define ? _("definition") : _("undefinition"),
24738 line, current_file->filename);
24739 }
24740 else if (is_define)
24741 parse_macro_definition (current_file, line, body);
24742 else
24743 {
24744 gdb_assert (macinfo_type == DW_MACRO_undef
24745 || macinfo_type == DW_MACRO_undef_strp
24746 || macinfo_type == DW_MACRO_undef_sup);
24747 macro_undef (current_file, line, body);
24748 }
24749 }
24750 break;
24751
24752 case DW_MACRO_start_file:
24753 {
24754 unsigned int bytes_read;
24755 int line, file;
24756
24757 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24758 mac_ptr += bytes_read;
24759 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24760 mac_ptr += bytes_read;
24761
24762 if ((line == 0 && !at_commandline)
24763 || (line != 0 && at_commandline))
24764 complaint (_("debug info gives source %d included "
24765 "from %s at %s line %d"),
24766 file, at_commandline ? _("command-line") : _("file"),
24767 line == 0 ? _("zero") : _("non-zero"), line);
24768
24769 if (at_commandline)
24770 {
24771 /* This DW_MACRO_start_file was executed in the
24772 pass one. */
24773 at_commandline = 0;
24774 }
24775 else
24776 current_file = macro_start_file (cu, file, line, current_file,
24777 lh);
24778 }
24779 break;
24780
24781 case DW_MACRO_end_file:
24782 if (! current_file)
24783 complaint (_("macro debug info has an unmatched "
24784 "`close_file' directive"));
24785 else
24786 {
24787 current_file = current_file->included_by;
24788 if (! current_file)
24789 {
24790 enum dwarf_macro_record_type next_type;
24791
24792 /* GCC circa March 2002 doesn't produce the zero
24793 type byte marking the end of the compilation
24794 unit. Complain if it's not there, but exit no
24795 matter what. */
24796
24797 /* Do we at least have room for a macinfo type byte? */
24798 if (mac_ptr >= mac_end)
24799 {
24800 dwarf2_section_buffer_overflow_complaint (section);
24801 return;
24802 }
24803
24804 /* We don't increment mac_ptr here, so this is just
24805 a look-ahead. */
24806 next_type
24807 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24808 mac_ptr);
24809 if (next_type != 0)
24810 complaint (_("no terminating 0-type entry for "
24811 "macros in `.debug_macinfo' section"));
24812
24813 return;
24814 }
24815 }
24816 break;
24817
24818 case DW_MACRO_import:
24819 case DW_MACRO_import_sup:
24820 {
24821 LONGEST offset;
24822 void **slot;
24823 bfd *include_bfd = abfd;
24824 struct dwarf2_section_info *include_section = section;
24825 const gdb_byte *include_mac_end = mac_end;
24826 int is_dwz = section_is_dwz;
24827 const gdb_byte *new_mac_ptr;
24828
24829 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24830 mac_ptr += offset_size;
24831
24832 if (macinfo_type == DW_MACRO_import_sup)
24833 {
24834 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24835
24836 dwarf2_read_section (objfile, &dwz->macro);
24837
24838 include_section = &dwz->macro;
24839 include_bfd = get_section_bfd_owner (include_section);
24840 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24841 is_dwz = 1;
24842 }
24843
24844 new_mac_ptr = include_section->buffer + offset;
24845 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24846
24847 if (*slot != NULL)
24848 {
24849 /* This has actually happened; see
24850 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24851 complaint (_("recursive DW_MACRO_import in "
24852 ".debug_macro section"));
24853 }
24854 else
24855 {
24856 *slot = (void *) new_mac_ptr;
24857
24858 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
24859 include_mac_end, current_file, lh,
24860 section, section_is_gnu, is_dwz,
24861 offset_size, include_hash);
24862
24863 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24864 }
24865 }
24866 break;
24867
24868 case DW_MACINFO_vendor_ext:
24869 if (!section_is_gnu)
24870 {
24871 unsigned int bytes_read;
24872
24873 /* This reads the constant, but since we don't recognize
24874 any vendor extensions, we ignore it. */
24875 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24876 mac_ptr += bytes_read;
24877 read_direct_string (abfd, mac_ptr, &bytes_read);
24878 mac_ptr += bytes_read;
24879
24880 /* We don't recognize any vendor extensions. */
24881 break;
24882 }
24883 /* FALLTHROUGH */
24884
24885 default:
24886 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24887 mac_ptr, mac_end, abfd, offset_size,
24888 section);
24889 if (mac_ptr == NULL)
24890 return;
24891 break;
24892 }
24893 DIAGNOSTIC_POP
24894 } while (macinfo_type != 0);
24895 }
24896
24897 static void
24898 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24899 int section_is_gnu)
24900 {
24901 struct dwarf2_per_objfile *dwarf2_per_objfile
24902 = cu->per_cu->dwarf2_per_objfile;
24903 struct objfile *objfile = dwarf2_per_objfile->objfile;
24904 struct line_header *lh = cu->line_header;
24905 bfd *abfd;
24906 const gdb_byte *mac_ptr, *mac_end;
24907 struct macro_source_file *current_file = 0;
24908 enum dwarf_macro_record_type macinfo_type;
24909 unsigned int offset_size = cu->header.offset_size;
24910 const gdb_byte *opcode_definitions[256];
24911 void **slot;
24912 struct dwarf2_section_info *section;
24913 const char *section_name;
24914
24915 if (cu->dwo_unit != NULL)
24916 {
24917 if (section_is_gnu)
24918 {
24919 section = &cu->dwo_unit->dwo_file->sections.macro;
24920 section_name = ".debug_macro.dwo";
24921 }
24922 else
24923 {
24924 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24925 section_name = ".debug_macinfo.dwo";
24926 }
24927 }
24928 else
24929 {
24930 if (section_is_gnu)
24931 {
24932 section = &dwarf2_per_objfile->macro;
24933 section_name = ".debug_macro";
24934 }
24935 else
24936 {
24937 section = &dwarf2_per_objfile->macinfo;
24938 section_name = ".debug_macinfo";
24939 }
24940 }
24941
24942 dwarf2_read_section (objfile, section);
24943 if (section->buffer == NULL)
24944 {
24945 complaint (_("missing %s section"), section_name);
24946 return;
24947 }
24948 abfd = get_section_bfd_owner (section);
24949
24950 /* First pass: Find the name of the base filename.
24951 This filename is needed in order to process all macros whose definition
24952 (or undefinition) comes from the command line. These macros are defined
24953 before the first DW_MACINFO_start_file entry, and yet still need to be
24954 associated to the base file.
24955
24956 To determine the base file name, we scan the macro definitions until we
24957 reach the first DW_MACINFO_start_file entry. We then initialize
24958 CURRENT_FILE accordingly so that any macro definition found before the
24959 first DW_MACINFO_start_file can still be associated to the base file. */
24960
24961 mac_ptr = section->buffer + offset;
24962 mac_end = section->buffer + section->size;
24963
24964 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24965 &offset_size, section_is_gnu);
24966 if (mac_ptr == NULL)
24967 {
24968 /* We already issued a complaint. */
24969 return;
24970 }
24971
24972 do
24973 {
24974 /* Do we at least have room for a macinfo type byte? */
24975 if (mac_ptr >= mac_end)
24976 {
24977 /* Complaint is printed during the second pass as GDB will probably
24978 stop the first pass earlier upon finding
24979 DW_MACINFO_start_file. */
24980 break;
24981 }
24982
24983 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24984 mac_ptr++;
24985
24986 /* Note that we rely on the fact that the corresponding GNU and
24987 DWARF constants are the same. */
24988 DIAGNOSTIC_PUSH
24989 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24990 switch (macinfo_type)
24991 {
24992 /* A zero macinfo type indicates the end of the macro
24993 information. */
24994 case 0:
24995 break;
24996
24997 case DW_MACRO_define:
24998 case DW_MACRO_undef:
24999 /* Only skip the data by MAC_PTR. */
25000 {
25001 unsigned int bytes_read;
25002
25003 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25004 mac_ptr += bytes_read;
25005 read_direct_string (abfd, mac_ptr, &bytes_read);
25006 mac_ptr += bytes_read;
25007 }
25008 break;
25009
25010 case DW_MACRO_start_file:
25011 {
25012 unsigned int bytes_read;
25013 int line, file;
25014
25015 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25016 mac_ptr += bytes_read;
25017 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25018 mac_ptr += bytes_read;
25019
25020 current_file = macro_start_file (cu, file, line, current_file, lh);
25021 }
25022 break;
25023
25024 case DW_MACRO_end_file:
25025 /* No data to skip by MAC_PTR. */
25026 break;
25027
25028 case DW_MACRO_define_strp:
25029 case DW_MACRO_undef_strp:
25030 case DW_MACRO_define_sup:
25031 case DW_MACRO_undef_sup:
25032 {
25033 unsigned int bytes_read;
25034
25035 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25036 mac_ptr += bytes_read;
25037 mac_ptr += offset_size;
25038 }
25039 break;
25040
25041 case DW_MACRO_import:
25042 case DW_MACRO_import_sup:
25043 /* Note that, according to the spec, a transparent include
25044 chain cannot call DW_MACRO_start_file. So, we can just
25045 skip this opcode. */
25046 mac_ptr += offset_size;
25047 break;
25048
25049 case DW_MACINFO_vendor_ext:
25050 /* Only skip the data by MAC_PTR. */
25051 if (!section_is_gnu)
25052 {
25053 unsigned int bytes_read;
25054
25055 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25056 mac_ptr += bytes_read;
25057 read_direct_string (abfd, mac_ptr, &bytes_read);
25058 mac_ptr += bytes_read;
25059 }
25060 /* FALLTHROUGH */
25061
25062 default:
25063 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25064 mac_ptr, mac_end, abfd, offset_size,
25065 section);
25066 if (mac_ptr == NULL)
25067 return;
25068 break;
25069 }
25070 DIAGNOSTIC_POP
25071 } while (macinfo_type != 0 && current_file == NULL);
25072
25073 /* Second pass: Process all entries.
25074
25075 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25076 command-line macro definitions/undefinitions. This flag is unset when we
25077 reach the first DW_MACINFO_start_file entry. */
25078
25079 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25080 htab_eq_pointer,
25081 NULL, xcalloc, xfree));
25082 mac_ptr = section->buffer + offset;
25083 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
25084 *slot = (void *) mac_ptr;
25085 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
25086 current_file, lh, section,
25087 section_is_gnu, 0, offset_size,
25088 include_hash.get ());
25089 }
25090
25091 /* Check if the attribute's form is a DW_FORM_block*
25092 if so return true else false. */
25093
25094 static int
25095 attr_form_is_block (const struct attribute *attr)
25096 {
25097 return (attr == NULL ? 0 :
25098 attr->form == DW_FORM_block1
25099 || attr->form == DW_FORM_block2
25100 || attr->form == DW_FORM_block4
25101 || attr->form == DW_FORM_block
25102 || attr->form == DW_FORM_exprloc);
25103 }
25104
25105 /* Return non-zero if ATTR's value is a section offset --- classes
25106 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25107 You may use DW_UNSND (attr) to retrieve such offsets.
25108
25109 Section 7.5.4, "Attribute Encodings", explains that no attribute
25110 may have a value that belongs to more than one of these classes; it
25111 would be ambiguous if we did, because we use the same forms for all
25112 of them. */
25113
25114 static int
25115 attr_form_is_section_offset (const struct attribute *attr)
25116 {
25117 return (attr->form == DW_FORM_data4
25118 || attr->form == DW_FORM_data8
25119 || attr->form == DW_FORM_sec_offset);
25120 }
25121
25122 /* Return non-zero if ATTR's value falls in the 'constant' class, or
25123 zero otherwise. When this function returns true, you can apply
25124 dwarf2_get_attr_constant_value to it.
25125
25126 However, note that for some attributes you must check
25127 attr_form_is_section_offset before using this test. DW_FORM_data4
25128 and DW_FORM_data8 are members of both the constant class, and of
25129 the classes that contain offsets into other debug sections
25130 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25131 that, if an attribute's can be either a constant or one of the
25132 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25133 taken as section offsets, not constants.
25134
25135 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25136 cannot handle that. */
25137
25138 static int
25139 attr_form_is_constant (const struct attribute *attr)
25140 {
25141 switch (attr->form)
25142 {
25143 case DW_FORM_sdata:
25144 case DW_FORM_udata:
25145 case DW_FORM_data1:
25146 case DW_FORM_data2:
25147 case DW_FORM_data4:
25148 case DW_FORM_data8:
25149 case DW_FORM_implicit_const:
25150 return 1;
25151 default:
25152 return 0;
25153 }
25154 }
25155
25156
25157 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25158 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25159
25160 static int
25161 attr_form_is_ref (const struct attribute *attr)
25162 {
25163 switch (attr->form)
25164 {
25165 case DW_FORM_ref_addr:
25166 case DW_FORM_ref1:
25167 case DW_FORM_ref2:
25168 case DW_FORM_ref4:
25169 case DW_FORM_ref8:
25170 case DW_FORM_ref_udata:
25171 case DW_FORM_GNU_ref_alt:
25172 return 1;
25173 default:
25174 return 0;
25175 }
25176 }
25177
25178 /* Return the .debug_loc section to use for CU.
25179 For DWO files use .debug_loc.dwo. */
25180
25181 static struct dwarf2_section_info *
25182 cu_debug_loc_section (struct dwarf2_cu *cu)
25183 {
25184 struct dwarf2_per_objfile *dwarf2_per_objfile
25185 = cu->per_cu->dwarf2_per_objfile;
25186
25187 if (cu->dwo_unit)
25188 {
25189 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25190
25191 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25192 }
25193 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25194 : &dwarf2_per_objfile->loc);
25195 }
25196
25197 /* A helper function that fills in a dwarf2_loclist_baton. */
25198
25199 static void
25200 fill_in_loclist_baton (struct dwarf2_cu *cu,
25201 struct dwarf2_loclist_baton *baton,
25202 const struct attribute *attr)
25203 {
25204 struct dwarf2_per_objfile *dwarf2_per_objfile
25205 = cu->per_cu->dwarf2_per_objfile;
25206 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25207
25208 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25209
25210 baton->per_cu = cu->per_cu;
25211 gdb_assert (baton->per_cu);
25212 /* We don't know how long the location list is, but make sure we
25213 don't run off the edge of the section. */
25214 baton->size = section->size - DW_UNSND (attr);
25215 baton->data = section->buffer + DW_UNSND (attr);
25216 baton->base_address = cu->base_address;
25217 baton->from_dwo = cu->dwo_unit != NULL;
25218 }
25219
25220 static void
25221 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25222 struct dwarf2_cu *cu, int is_block)
25223 {
25224 struct dwarf2_per_objfile *dwarf2_per_objfile
25225 = cu->per_cu->dwarf2_per_objfile;
25226 struct objfile *objfile = dwarf2_per_objfile->objfile;
25227 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25228
25229 if (attr_form_is_section_offset (attr)
25230 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25231 the section. If so, fall through to the complaint in the
25232 other branch. */
25233 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25234 {
25235 struct dwarf2_loclist_baton *baton;
25236
25237 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25238
25239 fill_in_loclist_baton (cu, baton, attr);
25240
25241 if (cu->base_known == 0)
25242 complaint (_("Location list used without "
25243 "specifying the CU base address."));
25244
25245 SYMBOL_ACLASS_INDEX (sym) = (is_block
25246 ? dwarf2_loclist_block_index
25247 : dwarf2_loclist_index);
25248 SYMBOL_LOCATION_BATON (sym) = baton;
25249 }
25250 else
25251 {
25252 struct dwarf2_locexpr_baton *baton;
25253
25254 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25255 baton->per_cu = cu->per_cu;
25256 gdb_assert (baton->per_cu);
25257
25258 if (attr_form_is_block (attr))
25259 {
25260 /* Note that we're just copying the block's data pointer
25261 here, not the actual data. We're still pointing into the
25262 info_buffer for SYM's objfile; right now we never release
25263 that buffer, but when we do clean up properly this may
25264 need to change. */
25265 baton->size = DW_BLOCK (attr)->size;
25266 baton->data = DW_BLOCK (attr)->data;
25267 }
25268 else
25269 {
25270 dwarf2_invalid_attrib_class_complaint ("location description",
25271 SYMBOL_NATURAL_NAME (sym));
25272 baton->size = 0;
25273 }
25274
25275 SYMBOL_ACLASS_INDEX (sym) = (is_block
25276 ? dwarf2_locexpr_block_index
25277 : dwarf2_locexpr_index);
25278 SYMBOL_LOCATION_BATON (sym) = baton;
25279 }
25280 }
25281
25282 /* Return the OBJFILE associated with the compilation unit CU. If CU
25283 came from a separate debuginfo file, then the master objfile is
25284 returned. */
25285
25286 struct objfile *
25287 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25288 {
25289 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25290
25291 /* Return the master objfile, so that we can report and look up the
25292 correct file containing this variable. */
25293 if (objfile->separate_debug_objfile_backlink)
25294 objfile = objfile->separate_debug_objfile_backlink;
25295
25296 return objfile;
25297 }
25298
25299 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25300 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25301 CU_HEADERP first. */
25302
25303 static const struct comp_unit_head *
25304 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25305 struct dwarf2_per_cu_data *per_cu)
25306 {
25307 const gdb_byte *info_ptr;
25308
25309 if (per_cu->cu)
25310 return &per_cu->cu->header;
25311
25312 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25313
25314 memset (cu_headerp, 0, sizeof (*cu_headerp));
25315 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25316 rcuh_kind::COMPILE);
25317
25318 return cu_headerp;
25319 }
25320
25321 /* Return the address size given in the compilation unit header for CU. */
25322
25323 int
25324 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25325 {
25326 struct comp_unit_head cu_header_local;
25327 const struct comp_unit_head *cu_headerp;
25328
25329 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25330
25331 return cu_headerp->addr_size;
25332 }
25333
25334 /* Return the offset size given in the compilation unit header for CU. */
25335
25336 int
25337 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25338 {
25339 struct comp_unit_head cu_header_local;
25340 const struct comp_unit_head *cu_headerp;
25341
25342 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25343
25344 return cu_headerp->offset_size;
25345 }
25346
25347 /* See its dwarf2loc.h declaration. */
25348
25349 int
25350 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25351 {
25352 struct comp_unit_head cu_header_local;
25353 const struct comp_unit_head *cu_headerp;
25354
25355 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25356
25357 if (cu_headerp->version == 2)
25358 return cu_headerp->addr_size;
25359 else
25360 return cu_headerp->offset_size;
25361 }
25362
25363 /* Return the text offset of the CU. The returned offset comes from
25364 this CU's objfile. If this objfile came from a separate debuginfo
25365 file, then the offset may be different from the corresponding
25366 offset in the parent objfile. */
25367
25368 CORE_ADDR
25369 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25370 {
25371 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25372
25373 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25374 }
25375
25376 /* Return a type that is a generic pointer type, the size of which matches
25377 the address size given in the compilation unit header for PER_CU. */
25378 static struct type *
25379 dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25380 {
25381 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25382 struct type *void_type = objfile_type (objfile)->builtin_void;
25383 struct type *addr_type = lookup_pointer_type (void_type);
25384 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25385
25386 if (TYPE_LENGTH (addr_type) == addr_size)
25387 return addr_type;
25388
25389 addr_type
25390 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25391 return addr_type;
25392 }
25393
25394 /* Return DWARF version number of PER_CU. */
25395
25396 short
25397 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25398 {
25399 return per_cu->dwarf_version;
25400 }
25401
25402 /* Locate the .debug_info compilation unit from CU's objfile which contains
25403 the DIE at OFFSET. Raises an error on failure. */
25404
25405 static struct dwarf2_per_cu_data *
25406 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25407 unsigned int offset_in_dwz,
25408 struct dwarf2_per_objfile *dwarf2_per_objfile)
25409 {
25410 struct dwarf2_per_cu_data *this_cu;
25411 int low, high;
25412
25413 low = 0;
25414 high = dwarf2_per_objfile->all_comp_units.size () - 1;
25415 while (high > low)
25416 {
25417 struct dwarf2_per_cu_data *mid_cu;
25418 int mid = low + (high - low) / 2;
25419
25420 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25421 if (mid_cu->is_dwz > offset_in_dwz
25422 || (mid_cu->is_dwz == offset_in_dwz
25423 && mid_cu->sect_off + mid_cu->length >= sect_off))
25424 high = mid;
25425 else
25426 low = mid + 1;
25427 }
25428 gdb_assert (low == high);
25429 this_cu = dwarf2_per_objfile->all_comp_units[low];
25430 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25431 {
25432 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25433 error (_("Dwarf Error: could not find partial DIE containing "
25434 "offset %s [in module %s]"),
25435 sect_offset_str (sect_off),
25436 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25437
25438 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25439 <= sect_off);
25440 return dwarf2_per_objfile->all_comp_units[low-1];
25441 }
25442 else
25443 {
25444 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25445 && sect_off >= this_cu->sect_off + this_cu->length)
25446 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25447 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25448 return this_cu;
25449 }
25450 }
25451
25452 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25453
25454 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25455 : per_cu (per_cu_),
25456 mark (false),
25457 has_loclist (false),
25458 checked_producer (false),
25459 producer_is_gxx_lt_4_6 (false),
25460 producer_is_gcc_lt_4_3 (false),
25461 producer_is_icc (false),
25462 producer_is_icc_lt_14 (false),
25463 producer_is_codewarrior (false),
25464 processing_has_namespace_info (false)
25465 {
25466 per_cu->cu = this;
25467 }
25468
25469 /* Destroy a dwarf2_cu. */
25470
25471 dwarf2_cu::~dwarf2_cu ()
25472 {
25473 per_cu->cu = NULL;
25474 }
25475
25476 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25477
25478 static void
25479 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25480 enum language pretend_language)
25481 {
25482 struct attribute *attr;
25483
25484 /* Set the language we're debugging. */
25485 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25486 if (attr)
25487 set_cu_language (DW_UNSND (attr), cu);
25488 else
25489 {
25490 cu->language = pretend_language;
25491 cu->language_defn = language_def (cu->language);
25492 }
25493
25494 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25495 }
25496
25497 /* Increase the age counter on each cached compilation unit, and free
25498 any that are too old. */
25499
25500 static void
25501 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25502 {
25503 struct dwarf2_per_cu_data *per_cu, **last_chain;
25504
25505 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25506 per_cu = dwarf2_per_objfile->read_in_chain;
25507 while (per_cu != NULL)
25508 {
25509 per_cu->cu->last_used ++;
25510 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25511 dwarf2_mark (per_cu->cu);
25512 per_cu = per_cu->cu->read_in_chain;
25513 }
25514
25515 per_cu = dwarf2_per_objfile->read_in_chain;
25516 last_chain = &dwarf2_per_objfile->read_in_chain;
25517 while (per_cu != NULL)
25518 {
25519 struct dwarf2_per_cu_data *next_cu;
25520
25521 next_cu = per_cu->cu->read_in_chain;
25522
25523 if (!per_cu->cu->mark)
25524 {
25525 delete per_cu->cu;
25526 *last_chain = next_cu;
25527 }
25528 else
25529 last_chain = &per_cu->cu->read_in_chain;
25530
25531 per_cu = next_cu;
25532 }
25533 }
25534
25535 /* Remove a single compilation unit from the cache. */
25536
25537 static void
25538 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25539 {
25540 struct dwarf2_per_cu_data *per_cu, **last_chain;
25541 struct dwarf2_per_objfile *dwarf2_per_objfile
25542 = target_per_cu->dwarf2_per_objfile;
25543
25544 per_cu = dwarf2_per_objfile->read_in_chain;
25545 last_chain = &dwarf2_per_objfile->read_in_chain;
25546 while (per_cu != NULL)
25547 {
25548 struct dwarf2_per_cu_data *next_cu;
25549
25550 next_cu = per_cu->cu->read_in_chain;
25551
25552 if (per_cu == target_per_cu)
25553 {
25554 delete per_cu->cu;
25555 per_cu->cu = NULL;
25556 *last_chain = next_cu;
25557 break;
25558 }
25559 else
25560 last_chain = &per_cu->cu->read_in_chain;
25561
25562 per_cu = next_cu;
25563 }
25564 }
25565
25566 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25567 We store these in a hash table separate from the DIEs, and preserve them
25568 when the DIEs are flushed out of cache.
25569
25570 The CU "per_cu" pointer is needed because offset alone is not enough to
25571 uniquely identify the type. A file may have multiple .debug_types sections,
25572 or the type may come from a DWO file. Furthermore, while it's more logical
25573 to use per_cu->section+offset, with Fission the section with the data is in
25574 the DWO file but we don't know that section at the point we need it.
25575 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25576 because we can enter the lookup routine, get_die_type_at_offset, from
25577 outside this file, and thus won't necessarily have PER_CU->cu.
25578 Fortunately, PER_CU is stable for the life of the objfile. */
25579
25580 struct dwarf2_per_cu_offset_and_type
25581 {
25582 const struct dwarf2_per_cu_data *per_cu;
25583 sect_offset sect_off;
25584 struct type *type;
25585 };
25586
25587 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25588
25589 static hashval_t
25590 per_cu_offset_and_type_hash (const void *item)
25591 {
25592 const struct dwarf2_per_cu_offset_and_type *ofs
25593 = (const struct dwarf2_per_cu_offset_and_type *) item;
25594
25595 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25596 }
25597
25598 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25599
25600 static int
25601 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25602 {
25603 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25604 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25605 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25606 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25607
25608 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25609 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25610 }
25611
25612 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25613 table if necessary. For convenience, return TYPE.
25614
25615 The DIEs reading must have careful ordering to:
25616 * Not cause infite loops trying to read in DIEs as a prerequisite for
25617 reading current DIE.
25618 * Not trying to dereference contents of still incompletely read in types
25619 while reading in other DIEs.
25620 * Enable referencing still incompletely read in types just by a pointer to
25621 the type without accessing its fields.
25622
25623 Therefore caller should follow these rules:
25624 * Try to fetch any prerequisite types we may need to build this DIE type
25625 before building the type and calling set_die_type.
25626 * After building type call set_die_type for current DIE as soon as
25627 possible before fetching more types to complete the current type.
25628 * Make the type as complete as possible before fetching more types. */
25629
25630 static struct type *
25631 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25632 {
25633 struct dwarf2_per_objfile *dwarf2_per_objfile
25634 = cu->per_cu->dwarf2_per_objfile;
25635 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25636 struct objfile *objfile = dwarf2_per_objfile->objfile;
25637 struct attribute *attr;
25638 struct dynamic_prop prop;
25639
25640 /* For Ada types, make sure that the gnat-specific data is always
25641 initialized (if not already set). There are a few types where
25642 we should not be doing so, because the type-specific area is
25643 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25644 where the type-specific area is used to store the floatformat).
25645 But this is not a problem, because the gnat-specific information
25646 is actually not needed for these types. */
25647 if (need_gnat_info (cu)
25648 && TYPE_CODE (type) != TYPE_CODE_FUNC
25649 && TYPE_CODE (type) != TYPE_CODE_FLT
25650 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25651 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25652 && TYPE_CODE (type) != TYPE_CODE_METHOD
25653 && !HAVE_GNAT_AUX_INFO (type))
25654 INIT_GNAT_SPECIFIC (type);
25655
25656 /* Read DW_AT_allocated and set in type. */
25657 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25658 if (attr_form_is_block (attr))
25659 {
25660 struct type *prop_type
25661 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25662 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25663 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25664 }
25665 else if (attr != NULL)
25666 {
25667 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25668 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25669 sect_offset_str (die->sect_off));
25670 }
25671
25672 /* Read DW_AT_associated and set in type. */
25673 attr = dwarf2_attr (die, DW_AT_associated, cu);
25674 if (attr_form_is_block (attr))
25675 {
25676 struct type *prop_type
25677 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25678 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25679 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25680 }
25681 else if (attr != NULL)
25682 {
25683 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25684 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25685 sect_offset_str (die->sect_off));
25686 }
25687
25688 /* Read DW_AT_data_location and set in type. */
25689 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25690 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25691 dwarf2_per_cu_addr_type (cu->per_cu)))
25692 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25693
25694 if (dwarf2_per_objfile->die_type_hash == NULL)
25695 {
25696 dwarf2_per_objfile->die_type_hash =
25697 htab_create_alloc_ex (127,
25698 per_cu_offset_and_type_hash,
25699 per_cu_offset_and_type_eq,
25700 NULL,
25701 &objfile->objfile_obstack,
25702 hashtab_obstack_allocate,
25703 dummy_obstack_deallocate);
25704 }
25705
25706 ofs.per_cu = cu->per_cu;
25707 ofs.sect_off = die->sect_off;
25708 ofs.type = type;
25709 slot = (struct dwarf2_per_cu_offset_and_type **)
25710 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25711 if (*slot)
25712 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25713 sect_offset_str (die->sect_off));
25714 *slot = XOBNEW (&objfile->objfile_obstack,
25715 struct dwarf2_per_cu_offset_and_type);
25716 **slot = ofs;
25717 return type;
25718 }
25719
25720 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25721 or return NULL if the die does not have a saved type. */
25722
25723 static struct type *
25724 get_die_type_at_offset (sect_offset sect_off,
25725 struct dwarf2_per_cu_data *per_cu)
25726 {
25727 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25728 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25729
25730 if (dwarf2_per_objfile->die_type_hash == NULL)
25731 return NULL;
25732
25733 ofs.per_cu = per_cu;
25734 ofs.sect_off = sect_off;
25735 slot = ((struct dwarf2_per_cu_offset_and_type *)
25736 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25737 if (slot)
25738 return slot->type;
25739 else
25740 return NULL;
25741 }
25742
25743 /* Look up the type for DIE in CU in die_type_hash,
25744 or return NULL if DIE does not have a saved type. */
25745
25746 static struct type *
25747 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25748 {
25749 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25750 }
25751
25752 /* Add a dependence relationship from CU to REF_PER_CU. */
25753
25754 static void
25755 dwarf2_add_dependence (struct dwarf2_cu *cu,
25756 struct dwarf2_per_cu_data *ref_per_cu)
25757 {
25758 void **slot;
25759
25760 if (cu->dependencies == NULL)
25761 cu->dependencies
25762 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25763 NULL, &cu->comp_unit_obstack,
25764 hashtab_obstack_allocate,
25765 dummy_obstack_deallocate);
25766
25767 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25768 if (*slot == NULL)
25769 *slot = ref_per_cu;
25770 }
25771
25772 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25773 Set the mark field in every compilation unit in the
25774 cache that we must keep because we are keeping CU. */
25775
25776 static int
25777 dwarf2_mark_helper (void **slot, void *data)
25778 {
25779 struct dwarf2_per_cu_data *per_cu;
25780
25781 per_cu = (struct dwarf2_per_cu_data *) *slot;
25782
25783 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25784 reading of the chain. As such dependencies remain valid it is not much
25785 useful to track and undo them during QUIT cleanups. */
25786 if (per_cu->cu == NULL)
25787 return 1;
25788
25789 if (per_cu->cu->mark)
25790 return 1;
25791 per_cu->cu->mark = true;
25792
25793 if (per_cu->cu->dependencies != NULL)
25794 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25795
25796 return 1;
25797 }
25798
25799 /* Set the mark field in CU and in every other compilation unit in the
25800 cache that we must keep because we are keeping CU. */
25801
25802 static void
25803 dwarf2_mark (struct dwarf2_cu *cu)
25804 {
25805 if (cu->mark)
25806 return;
25807 cu->mark = true;
25808 if (cu->dependencies != NULL)
25809 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25810 }
25811
25812 static void
25813 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25814 {
25815 while (per_cu)
25816 {
25817 per_cu->cu->mark = false;
25818 per_cu = per_cu->cu->read_in_chain;
25819 }
25820 }
25821
25822 /* Trivial hash function for partial_die_info: the hash value of a DIE
25823 is its offset in .debug_info for this objfile. */
25824
25825 static hashval_t
25826 partial_die_hash (const void *item)
25827 {
25828 const struct partial_die_info *part_die
25829 = (const struct partial_die_info *) item;
25830
25831 return to_underlying (part_die->sect_off);
25832 }
25833
25834 /* Trivial comparison function for partial_die_info structures: two DIEs
25835 are equal if they have the same offset. */
25836
25837 static int
25838 partial_die_eq (const void *item_lhs, const void *item_rhs)
25839 {
25840 const struct partial_die_info *part_die_lhs
25841 = (const struct partial_die_info *) item_lhs;
25842 const struct partial_die_info *part_die_rhs
25843 = (const struct partial_die_info *) item_rhs;
25844
25845 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25846 }
25847
25848 struct cmd_list_element *set_dwarf_cmdlist;
25849 struct cmd_list_element *show_dwarf_cmdlist;
25850
25851 static void
25852 set_dwarf_cmd (const char *args, int from_tty)
25853 {
25854 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25855 gdb_stdout);
25856 }
25857
25858 static void
25859 show_dwarf_cmd (const char *args, int from_tty)
25860 {
25861 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25862 }
25863
25864 bool dwarf_always_disassemble;
25865
25866 static void
25867 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25868 struct cmd_list_element *c, const char *value)
25869 {
25870 fprintf_filtered (file,
25871 _("Whether to always disassemble "
25872 "DWARF expressions is %s.\n"),
25873 value);
25874 }
25875
25876 static void
25877 show_check_physname (struct ui_file *file, int from_tty,
25878 struct cmd_list_element *c, const char *value)
25879 {
25880 fprintf_filtered (file,
25881 _("Whether to check \"physname\" is %s.\n"),
25882 value);
25883 }
25884
25885 void
25886 _initialize_dwarf2_read (void)
25887 {
25888 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25889 Set DWARF specific variables.\n\
25890 Configure DWARF variables such as the cache size."),
25891 &set_dwarf_cmdlist, "maintenance set dwarf ",
25892 0/*allow-unknown*/, &maintenance_set_cmdlist);
25893
25894 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25895 Show DWARF specific variables.\n\
25896 Show DWARF variables such as the cache size."),
25897 &show_dwarf_cmdlist, "maintenance show dwarf ",
25898 0/*allow-unknown*/, &maintenance_show_cmdlist);
25899
25900 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25901 &dwarf_max_cache_age, _("\
25902 Set the upper bound on the age of cached DWARF compilation units."), _("\
25903 Show the upper bound on the age of cached DWARF compilation units."), _("\
25904 A higher limit means that cached compilation units will be stored\n\
25905 in memory longer, and more total memory will be used. Zero disables\n\
25906 caching, which can slow down startup."),
25907 NULL,
25908 show_dwarf_max_cache_age,
25909 &set_dwarf_cmdlist,
25910 &show_dwarf_cmdlist);
25911
25912 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25913 &dwarf_always_disassemble, _("\
25914 Set whether `info address' always disassembles DWARF expressions."), _("\
25915 Show whether `info address' always disassembles DWARF expressions."), _("\
25916 When enabled, DWARF expressions are always printed in an assembly-like\n\
25917 syntax. When disabled, expressions will be printed in a more\n\
25918 conversational style, when possible."),
25919 NULL,
25920 show_dwarf_always_disassemble,
25921 &set_dwarf_cmdlist,
25922 &show_dwarf_cmdlist);
25923
25924 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25925 Set debugging of the DWARF reader."), _("\
25926 Show debugging of the DWARF reader."), _("\
25927 When enabled (non-zero), debugging messages are printed during DWARF\n\
25928 reading and symtab expansion. A value of 1 (one) provides basic\n\
25929 information. A value greater than 1 provides more verbose information."),
25930 NULL,
25931 NULL,
25932 &setdebuglist, &showdebuglist);
25933
25934 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25935 Set debugging of the DWARF DIE reader."), _("\
25936 Show debugging of the DWARF DIE reader."), _("\
25937 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25938 The value is the maximum depth to print."),
25939 NULL,
25940 NULL,
25941 &setdebuglist, &showdebuglist);
25942
25943 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25944 Set debugging of the dwarf line reader."), _("\
25945 Show debugging of the dwarf line reader."), _("\
25946 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25947 A value of 1 (one) provides basic information.\n\
25948 A value greater than 1 provides more verbose information."),
25949 NULL,
25950 NULL,
25951 &setdebuglist, &showdebuglist);
25952
25953 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25954 Set cross-checking of \"physname\" code against demangler."), _("\
25955 Show cross-checking of \"physname\" code against demangler."), _("\
25956 When enabled, GDB's internal \"physname\" code is checked against\n\
25957 the demangler."),
25958 NULL, show_check_physname,
25959 &setdebuglist, &showdebuglist);
25960
25961 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25962 no_class, &use_deprecated_index_sections, _("\
25963 Set whether to use deprecated gdb_index sections."), _("\
25964 Show whether to use deprecated gdb_index sections."), _("\
25965 When enabled, deprecated .gdb_index sections are used anyway.\n\
25966 Normally they are ignored either because of a missing feature or\n\
25967 performance issue.\n\
25968 Warning: This option must be enabled before gdb reads the file."),
25969 NULL,
25970 NULL,
25971 &setlist, &showlist);
25972
25973 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25974 &dwarf2_locexpr_funcs);
25975 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25976 &dwarf2_loclist_funcs);
25977
25978 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25979 &dwarf2_block_frame_base_locexpr_funcs);
25980 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25981 &dwarf2_block_frame_base_loclist_funcs);
25982
25983 #if GDB_SELF_TEST
25984 selftests::register_test ("dw2_expand_symtabs_matching",
25985 selftests::dw2_expand_symtabs_matching::run_test);
25986 #endif
25987 }
This page took 0.572396 seconds and 4 git commands to generate.