Create dwarf2/leb.[ch]
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2read.h"
33 #include "dwarf-index-cache.h"
34 #include "dwarf-index-common.h"
35 #include "dwarf2/leb.h"
36 #include "bfd.h"
37 #include "elf-bfd.h"
38 #include "symtab.h"
39 #include "gdbtypes.h"
40 #include "objfiles.h"
41 #include "dwarf2.h"
42 #include "buildsym.h"
43 #include "demangle.h"
44 #include "gdb-demangle.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 "c-lang.h"
60 #include "go-lang.h"
61 #include "valprint.h"
62 #include "gdbcore.h" /* for gnutarget */
63 #include "gdb/gdb-index.h"
64 #include "gdb_bfd.h"
65 #include "f-lang.h"
66 #include "source.h"
67 #include "build-id.h"
68 #include "namespace.h"
69 #include "gdbsupport/function-view.h"
70 #include "gdbsupport/gdb_optional.h"
71 #include "gdbsupport/underlying.h"
72 #include "gdbsupport/hash_enum.h"
73 #include "filename-seen-cache.h"
74 #include "producer.h"
75 #include <fcntl.h>
76 #include <algorithm>
77 #include <unordered_map>
78 #include "gdbsupport/selftest.h"
79 #include "rust-lang.h"
80 #include "gdbsupport/pathstuff.h"
81
82 /* When == 1, print basic high level tracing messages.
83 When > 1, be more verbose.
84 This is in contrast to the low level DIE reading of dwarf_die_debug. */
85 static unsigned int dwarf_read_debug = 0;
86
87 /* When non-zero, dump DIEs after they are read in. */
88 static unsigned int dwarf_die_debug = 0;
89
90 /* When non-zero, dump line number entries as they are read in. */
91 static unsigned int dwarf_line_debug = 0;
92
93 /* When true, cross-check physname against demangler. */
94 static bool check_physname = false;
95
96 /* When true, do not reject deprecated .gdb_index sections. */
97 static bool use_deprecated_index_sections = false;
98
99 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
100
101 /* The "aclass" indices for various kinds of computed DWARF symbols. */
102
103 static int dwarf2_locexpr_index;
104 static int dwarf2_loclist_index;
105 static int dwarf2_locexpr_block_index;
106 static int dwarf2_loclist_block_index;
107
108 /* An index into a (C++) symbol name component in a symbol name as
109 recorded in the mapped_index's symbol table. For each C++ symbol
110 in the symbol table, we record one entry for the start of each
111 component in the symbol in a table of name components, and then
112 sort the table, in order to be able to binary search symbol names,
113 ignoring leading namespaces, both completion and regular look up.
114 For example, for symbol "A::B::C", we'll have an entry that points
115 to "A::B::C", another that points to "B::C", and another for "C".
116 Note that function symbols in GDB index have no parameter
117 information, just the function/method names. You can convert a
118 name_component to a "const char *" using the
119 'mapped_index::symbol_name_at(offset_type)' method. */
120
121 struct name_component
122 {
123 /* Offset in the symbol name where the component starts. Stored as
124 a (32-bit) offset instead of a pointer to save memory and improve
125 locality on 64-bit architectures. */
126 offset_type name_offset;
127
128 /* The symbol's index in the symbol and constant pool tables of a
129 mapped_index. */
130 offset_type idx;
131 };
132
133 /* Base class containing bits shared by both .gdb_index and
134 .debug_name indexes. */
135
136 struct mapped_index_base
137 {
138 mapped_index_base () = default;
139 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
140
141 /* The name_component table (a sorted vector). See name_component's
142 description above. */
143 std::vector<name_component> name_components;
144
145 /* How NAME_COMPONENTS is sorted. */
146 enum case_sensitivity name_components_casing;
147
148 /* Return the number of names in the symbol table. */
149 virtual size_t symbol_name_count () const = 0;
150
151 /* Get the name of the symbol at IDX in the symbol table. */
152 virtual const char *symbol_name_at (offset_type idx) const = 0;
153
154 /* Return whether the name at IDX in the symbol table should be
155 ignored. */
156 virtual bool symbol_name_slot_invalid (offset_type idx) const
157 {
158 return false;
159 }
160
161 /* Build the symbol name component sorted vector, if we haven't
162 yet. */
163 void build_name_components ();
164
165 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
166 possible matches for LN_NO_PARAMS in the name component
167 vector. */
168 std::pair<std::vector<name_component>::const_iterator,
169 std::vector<name_component>::const_iterator>
170 find_name_components_bounds (const lookup_name_info &ln_no_params,
171 enum language lang) const;
172
173 /* Prevent deleting/destroying via a base class pointer. */
174 protected:
175 ~mapped_index_base() = default;
176 };
177
178 /* A description of the mapped index. The file format is described in
179 a comment by the code that writes the index. */
180 struct mapped_index final : public mapped_index_base
181 {
182 /* A slot/bucket in the symbol table hash. */
183 struct symbol_table_slot
184 {
185 const offset_type name;
186 const offset_type vec;
187 };
188
189 /* Index data format version. */
190 int version = 0;
191
192 /* The address table data. */
193 gdb::array_view<const gdb_byte> address_table;
194
195 /* The symbol table, implemented as a hash table. */
196 gdb::array_view<symbol_table_slot> symbol_table;
197
198 /* A pointer to the constant pool. */
199 const char *constant_pool = nullptr;
200
201 bool symbol_name_slot_invalid (offset_type idx) const override
202 {
203 const auto &bucket = this->symbol_table[idx];
204 return bucket.name == 0 && bucket.vec == 0;
205 }
206
207 /* Convenience method to get at the name of the symbol at IDX in the
208 symbol table. */
209 const char *symbol_name_at (offset_type idx) const override
210 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
211
212 size_t symbol_name_count () const override
213 { return this->symbol_table.size (); }
214 };
215
216 /* A description of the mapped .debug_names.
217 Uninitialized map has CU_COUNT 0. */
218 struct mapped_debug_names final : public mapped_index_base
219 {
220 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
221 : dwarf2_per_objfile (dwarf2_per_objfile_)
222 {}
223
224 struct dwarf2_per_objfile *dwarf2_per_objfile;
225 bfd_endian dwarf5_byte_order;
226 bool dwarf5_is_dwarf64;
227 bool augmentation_is_gdb;
228 uint8_t offset_size;
229 uint32_t cu_count = 0;
230 uint32_t tu_count, bucket_count, name_count;
231 const gdb_byte *cu_table_reordered, *tu_table_reordered;
232 const uint32_t *bucket_table_reordered, *hash_table_reordered;
233 const gdb_byte *name_table_string_offs_reordered;
234 const gdb_byte *name_table_entry_offs_reordered;
235 const gdb_byte *entry_pool;
236
237 struct index_val
238 {
239 ULONGEST dwarf_tag;
240 struct attr
241 {
242 /* Attribute name DW_IDX_*. */
243 ULONGEST dw_idx;
244
245 /* Attribute form DW_FORM_*. */
246 ULONGEST form;
247
248 /* Value if FORM is DW_FORM_implicit_const. */
249 LONGEST implicit_const;
250 };
251 std::vector<attr> attr_vec;
252 };
253
254 std::unordered_map<ULONGEST, index_val> abbrev_map;
255
256 const char *namei_to_name (uint32_t namei) const;
257
258 /* Implementation of the mapped_index_base virtual interface, for
259 the name_components cache. */
260
261 const char *symbol_name_at (offset_type idx) const override
262 { return namei_to_name (idx); }
263
264 size_t symbol_name_count () const override
265 { return this->name_count; }
266 };
267
268 /* See dwarf2read.h. */
269
270 dwarf2_per_objfile *
271 get_dwarf2_per_objfile (struct objfile *objfile)
272 {
273 return dwarf2_objfile_data_key.get (objfile);
274 }
275
276 /* Default names of the debugging sections. */
277
278 /* Note that if the debugging section has been compressed, it might
279 have a name like .zdebug_info. */
280
281 static const struct dwarf2_debug_sections dwarf2_elf_names =
282 {
283 { ".debug_info", ".zdebug_info" },
284 { ".debug_abbrev", ".zdebug_abbrev" },
285 { ".debug_line", ".zdebug_line" },
286 { ".debug_loc", ".zdebug_loc" },
287 { ".debug_loclists", ".zdebug_loclists" },
288 { ".debug_macinfo", ".zdebug_macinfo" },
289 { ".debug_macro", ".zdebug_macro" },
290 { ".debug_str", ".zdebug_str" },
291 { ".debug_str_offsets", ".zdebug_str_offsets" },
292 { ".debug_line_str", ".zdebug_line_str" },
293 { ".debug_ranges", ".zdebug_ranges" },
294 { ".debug_rnglists", ".zdebug_rnglists" },
295 { ".debug_types", ".zdebug_types" },
296 { ".debug_addr", ".zdebug_addr" },
297 { ".debug_frame", ".zdebug_frame" },
298 { ".eh_frame", NULL },
299 { ".gdb_index", ".zgdb_index" },
300 { ".debug_names", ".zdebug_names" },
301 { ".debug_aranges", ".zdebug_aranges" },
302 23
303 };
304
305 /* List of DWO/DWP sections. */
306
307 static const struct dwop_section_names
308 {
309 struct dwarf2_section_names abbrev_dwo;
310 struct dwarf2_section_names info_dwo;
311 struct dwarf2_section_names line_dwo;
312 struct dwarf2_section_names loc_dwo;
313 struct dwarf2_section_names loclists_dwo;
314 struct dwarf2_section_names macinfo_dwo;
315 struct dwarf2_section_names macro_dwo;
316 struct dwarf2_section_names str_dwo;
317 struct dwarf2_section_names str_offsets_dwo;
318 struct dwarf2_section_names types_dwo;
319 struct dwarf2_section_names cu_index;
320 struct dwarf2_section_names tu_index;
321 }
322 dwop_section_names =
323 {
324 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
325 { ".debug_info.dwo", ".zdebug_info.dwo" },
326 { ".debug_line.dwo", ".zdebug_line.dwo" },
327 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
328 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
329 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
330 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
331 { ".debug_str.dwo", ".zdebug_str.dwo" },
332 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
333 { ".debug_types.dwo", ".zdebug_types.dwo" },
334 { ".debug_cu_index", ".zdebug_cu_index" },
335 { ".debug_tu_index", ".zdebug_tu_index" },
336 };
337
338 /* local data types */
339
340 /* The data in a compilation unit header, after target2host
341 translation, looks like this. */
342 struct comp_unit_head
343 {
344 unsigned int length;
345 short version;
346 unsigned char addr_size;
347 unsigned char signed_addr_p;
348 sect_offset abbrev_sect_off;
349
350 /* Size of file offsets; either 4 or 8. */
351 unsigned int offset_size;
352
353 /* Size of the length field; either 4 or 12. */
354 unsigned int initial_length_size;
355
356 enum dwarf_unit_type unit_type;
357
358 /* Offset to the first byte of this compilation unit header in the
359 .debug_info section, for resolving relative reference dies. */
360 sect_offset sect_off;
361
362 /* Offset to first die in this cu from the start of the cu.
363 This will be the first byte following the compilation unit header. */
364 cu_offset first_die_cu_offset;
365
366
367 /* 64-bit signature of this unit. For type units, it denotes the signature of
368 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
369 Also used in DWARF 5, to denote the dwo id when the unit type is
370 DW_UT_skeleton or DW_UT_split_compile. */
371 ULONGEST signature;
372
373 /* For types, offset in the type's DIE of the type defined by this TU. */
374 cu_offset type_cu_offset_in_tu;
375 };
376
377 /* Type used for delaying computation of method physnames.
378 See comments for compute_delayed_physnames. */
379 struct delayed_method_info
380 {
381 /* The type to which the method is attached, i.e., its parent class. */
382 struct type *type;
383
384 /* The index of the method in the type's function fieldlists. */
385 int fnfield_index;
386
387 /* The index of the method in the fieldlist. */
388 int index;
389
390 /* The name of the DIE. */
391 const char *name;
392
393 /* The DIE associated with this method. */
394 struct die_info *die;
395 };
396
397 /* Internal state when decoding a particular compilation unit. */
398 struct dwarf2_cu
399 {
400 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
401 ~dwarf2_cu ();
402
403 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
404
405 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
406 Create the set of symtabs used by this TU, or if this TU is sharing
407 symtabs with another TU and the symtabs have already been created
408 then restore those symtabs in the line header.
409 We don't need the pc/line-number mapping for type units. */
410 void setup_type_unit_groups (struct die_info *die);
411
412 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
413 buildsym_compunit constructor. */
414 struct compunit_symtab *start_symtab (const char *name,
415 const char *comp_dir,
416 CORE_ADDR low_pc);
417
418 /* Reset the builder. */
419 void reset_builder () { m_builder.reset (); }
420
421 /* The header of the compilation unit. */
422 struct comp_unit_head header {};
423
424 /* Base address of this compilation unit. */
425 CORE_ADDR base_address = 0;
426
427 /* Non-zero if base_address has been set. */
428 int base_known = 0;
429
430 /* The language we are debugging. */
431 enum language language = language_unknown;
432 const struct language_defn *language_defn = nullptr;
433
434 const char *producer = nullptr;
435
436 private:
437 /* The symtab builder for this CU. This is only non-NULL when full
438 symbols are being read. */
439 std::unique_ptr<buildsym_compunit> m_builder;
440
441 public:
442 /* The generic symbol table building routines have separate lists for
443 file scope symbols and all all other scopes (local scopes). So
444 we need to select the right one to pass to add_symbol_to_list().
445 We do it by keeping a pointer to the correct list in list_in_scope.
446
447 FIXME: The original dwarf code just treated the file scope as the
448 first local scope, and all other local scopes as nested local
449 scopes, and worked fine. Check to see if we really need to
450 distinguish these in buildsym.c. */
451 struct pending **list_in_scope = nullptr;
452
453 /* Hash table holding all the loaded partial DIEs
454 with partial_die->offset.SECT_OFF as hash. */
455 htab_t partial_dies = nullptr;
456
457 /* Storage for things with the same lifetime as this read-in compilation
458 unit, including partial DIEs. */
459 auto_obstack comp_unit_obstack;
460
461 /* When multiple dwarf2_cu structures are living in memory, this field
462 chains them all together, so that they can be released efficiently.
463 We will probably also want a generation counter so that most-recently-used
464 compilation units are cached... */
465 struct dwarf2_per_cu_data *read_in_chain = nullptr;
466
467 /* Backlink to our per_cu entry. */
468 struct dwarf2_per_cu_data *per_cu;
469
470 /* How many compilation units ago was this CU last referenced? */
471 int last_used = 0;
472
473 /* A hash table of DIE cu_offset for following references with
474 die_info->offset.sect_off as hash. */
475 htab_t die_hash = nullptr;
476
477 /* Full DIEs if read in. */
478 struct die_info *dies = nullptr;
479
480 /* A set of pointers to dwarf2_per_cu_data objects for compilation
481 units referenced by this one. Only set during full symbol processing;
482 partial symbol tables do not have dependencies. */
483 htab_t dependencies = nullptr;
484
485 /* Header data from the line table, during full symbol processing. */
486 struct line_header *line_header = nullptr;
487 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
488 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
489 this is the DW_TAG_compile_unit die for this CU. We'll hold on
490 to the line header as long as this DIE is being processed. See
491 process_die_scope. */
492 die_info *line_header_die_owner = nullptr;
493
494 /* A list of methods which need to have physnames computed
495 after all type information has been read. */
496 std::vector<delayed_method_info> method_list;
497
498 /* To be copied to symtab->call_site_htab. */
499 htab_t call_site_htab = nullptr;
500
501 /* Non-NULL if this CU came from a DWO file.
502 There is an invariant here that is important to remember:
503 Except for attributes copied from the top level DIE in the "main"
504 (or "stub") file in preparation for reading the DWO file
505 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
506 Either there isn't a DWO file (in which case this is NULL and the point
507 is moot), or there is and either we're not going to read it (in which
508 case this is NULL) or there is and we are reading it (in which case this
509 is non-NULL). */
510 struct dwo_unit *dwo_unit = nullptr;
511
512 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
513 Note this value comes from the Fission stub CU/TU's DIE. */
514 gdb::optional<ULONGEST> addr_base;
515
516 /* The DW_AT_rnglists_base attribute if present.
517 Note this value comes from the Fission stub CU/TU's DIE.
518 Also note that the value is zero in the non-DWO case so this value can
519 be used without needing to know whether DWO files are in use or not.
520 N.B. This does not apply to DW_AT_ranges appearing in
521 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
522 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
523 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
524 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
525 ULONGEST ranges_base = 0;
526
527 /* When reading debug info generated by older versions of rustc, we
528 have to rewrite some union types to be struct types with a
529 variant part. This rewriting must be done after the CU is fully
530 read in, because otherwise at the point of rewriting some struct
531 type might not have been fully processed. So, we keep a list of
532 all such types here and process them after expansion. */
533 std::vector<struct type *> rust_unions;
534
535 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
536 files, the value is implicitly zero. For DWARF 5 version DWO files, the
537 value is often implicit and is the size of the header of
538 .debug_str_offsets section (8 or 4, depending on the address size). */
539 gdb::optional<ULONGEST> str_offsets_base;
540
541 /* Mark used when releasing cached dies. */
542 bool mark : 1;
543
544 /* This CU references .debug_loc. See the symtab->locations_valid field.
545 This test is imperfect as there may exist optimized debug code not using
546 any location list and still facing inlining issues if handled as
547 unoptimized code. For a future better test see GCC PR other/32998. */
548 bool has_loclist : 1;
549
550 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
551 if all the producer_is_* fields are valid. This information is cached
552 because profiling CU expansion showed excessive time spent in
553 producer_is_gxx_lt_4_6. */
554 bool checked_producer : 1;
555 bool producer_is_gxx_lt_4_6 : 1;
556 bool producer_is_gcc_lt_4_3 : 1;
557 bool producer_is_icc : 1;
558 bool producer_is_icc_lt_14 : 1;
559 bool producer_is_codewarrior : 1;
560
561 /* When true, the file that we're processing is known to have
562 debugging info for C++ namespaces. GCC 3.3.x did not produce
563 this information, but later versions do. */
564
565 bool processing_has_namespace_info : 1;
566
567 struct partial_die_info *find_partial_die (sect_offset sect_off);
568
569 /* If this CU was inherited by another CU (via specification,
570 abstract_origin, etc), this is the ancestor CU. */
571 dwarf2_cu *ancestor;
572
573 /* Get the buildsym_compunit for this CU. */
574 buildsym_compunit *get_builder ()
575 {
576 /* If this CU has a builder associated with it, use that. */
577 if (m_builder != nullptr)
578 return m_builder.get ();
579
580 /* Otherwise, search ancestors for a valid builder. */
581 if (ancestor != nullptr)
582 return ancestor->get_builder ();
583
584 return nullptr;
585 }
586 };
587
588 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
589 This includes type_unit_group and quick_file_names. */
590
591 struct stmt_list_hash
592 {
593 /* The DWO unit this table is from or NULL if there is none. */
594 struct dwo_unit *dwo_unit;
595
596 /* Offset in .debug_line or .debug_line.dwo. */
597 sect_offset line_sect_off;
598 };
599
600 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
601 an object of this type. */
602
603 struct type_unit_group
604 {
605 /* dwarf2read.c's main "handle" on a TU symtab.
606 To simplify things we create an artificial CU that "includes" all the
607 type units using this stmt_list so that the rest of the code still has
608 a "per_cu" handle on the symtab.
609 This PER_CU is recognized by having no section. */
610 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
611 struct dwarf2_per_cu_data per_cu;
612
613 /* The TUs that share this DW_AT_stmt_list entry.
614 This is added to while parsing type units to build partial symtabs,
615 and is deleted afterwards and not used again. */
616 std::vector<signatured_type *> *tus;
617
618 /* The compunit symtab.
619 Type units in a group needn't all be defined in the same source file,
620 so we create an essentially anonymous symtab as the compunit symtab. */
621 struct compunit_symtab *compunit_symtab;
622
623 /* The data used to construct the hash key. */
624 struct stmt_list_hash hash;
625
626 /* The number of symtabs from the line header.
627 The value here must match line_header.num_file_names. */
628 unsigned int num_symtabs;
629
630 /* The symbol tables for this TU (obtained from the files listed in
631 DW_AT_stmt_list).
632 WARNING: The order of entries here must match the order of entries
633 in the line header. After the first TU using this type_unit_group, the
634 line header for the subsequent TUs is recreated from this. This is done
635 because we need to use the same symtabs for each TU using the same
636 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
637 there's no guarantee the line header doesn't have duplicate entries. */
638 struct symtab **symtabs;
639 };
640
641 /* These sections are what may appear in a (real or virtual) DWO file. */
642
643 struct dwo_sections
644 {
645 struct dwarf2_section_info abbrev;
646 struct dwarf2_section_info line;
647 struct dwarf2_section_info loc;
648 struct dwarf2_section_info loclists;
649 struct dwarf2_section_info macinfo;
650 struct dwarf2_section_info macro;
651 struct dwarf2_section_info str;
652 struct dwarf2_section_info str_offsets;
653 /* In the case of a virtual DWO file, these two are unused. */
654 struct dwarf2_section_info info;
655 std::vector<dwarf2_section_info> types;
656 };
657
658 /* CUs/TUs in DWP/DWO files. */
659
660 struct dwo_unit
661 {
662 /* Backlink to the containing struct dwo_file. */
663 struct dwo_file *dwo_file;
664
665 /* The "id" that distinguishes this CU/TU.
666 .debug_info calls this "dwo_id", .debug_types calls this "signature".
667 Since signatures came first, we stick with it for consistency. */
668 ULONGEST signature;
669
670 /* The section this CU/TU lives in, in the DWO file. */
671 struct dwarf2_section_info *section;
672
673 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
674 sect_offset sect_off;
675 unsigned int length;
676
677 /* For types, offset in the type's DIE of the type defined by this TU. */
678 cu_offset type_offset_in_tu;
679 };
680
681 /* include/dwarf2.h defines the DWP section codes.
682 It defines a max value but it doesn't define a min value, which we
683 use for error checking, so provide one. */
684
685 enum dwp_v2_section_ids
686 {
687 DW_SECT_MIN = 1
688 };
689
690 /* Data for one DWO file.
691
692 This includes virtual DWO files (a virtual DWO file is a DWO file as it
693 appears in a DWP file). DWP files don't really have DWO files per se -
694 comdat folding of types "loses" the DWO file they came from, and from
695 a high level view DWP files appear to contain a mass of random types.
696 However, to maintain consistency with the non-DWP case we pretend DWP
697 files contain virtual DWO files, and we assign each TU with one virtual
698 DWO file (generally based on the line and abbrev section offsets -
699 a heuristic that seems to work in practice). */
700
701 struct dwo_file
702 {
703 dwo_file () = default;
704 DISABLE_COPY_AND_ASSIGN (dwo_file);
705
706 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
707 For virtual DWO files the name is constructed from the section offsets
708 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
709 from related CU+TUs. */
710 const char *dwo_name = nullptr;
711
712 /* The DW_AT_comp_dir attribute. */
713 const char *comp_dir = nullptr;
714
715 /* The bfd, when the file is open. Otherwise this is NULL.
716 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
717 gdb_bfd_ref_ptr dbfd;
718
719 /* The sections that make up this DWO file.
720 Remember that for virtual DWO files in DWP V2, these are virtual
721 sections (for lack of a better name). */
722 struct dwo_sections sections {};
723
724 /* The CUs in the file.
725 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
726 an extension to handle LLVM's Link Time Optimization output (where
727 multiple source files may be compiled into a single object/dwo pair). */
728 htab_t cus {};
729
730 /* Table of TUs in the file.
731 Each element is a struct dwo_unit. */
732 htab_t tus {};
733 };
734
735 /* These sections are what may appear in a DWP file. */
736
737 struct dwp_sections
738 {
739 /* These are used by both DWP version 1 and 2. */
740 struct dwarf2_section_info str;
741 struct dwarf2_section_info cu_index;
742 struct dwarf2_section_info tu_index;
743
744 /* These are only used by DWP version 2 files.
745 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
746 sections are referenced by section number, and are not recorded here.
747 In DWP version 2 there is at most one copy of all these sections, each
748 section being (effectively) comprised of the concatenation of all of the
749 individual sections that exist in the version 1 format.
750 To keep the code simple we treat each of these concatenated pieces as a
751 section itself (a virtual section?). */
752 struct dwarf2_section_info abbrev;
753 struct dwarf2_section_info info;
754 struct dwarf2_section_info line;
755 struct dwarf2_section_info loc;
756 struct dwarf2_section_info macinfo;
757 struct dwarf2_section_info macro;
758 struct dwarf2_section_info str_offsets;
759 struct dwarf2_section_info types;
760 };
761
762 /* These sections are what may appear in a virtual DWO file in DWP version 1.
763 A virtual DWO file is a DWO file as it appears in a DWP file. */
764
765 struct virtual_v1_dwo_sections
766 {
767 struct dwarf2_section_info abbrev;
768 struct dwarf2_section_info line;
769 struct dwarf2_section_info loc;
770 struct dwarf2_section_info macinfo;
771 struct dwarf2_section_info macro;
772 struct dwarf2_section_info str_offsets;
773 /* Each DWP hash table entry records one CU or one TU.
774 That is recorded here, and copied to dwo_unit.section. */
775 struct dwarf2_section_info info_or_types;
776 };
777
778 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
779 In version 2, the sections of the DWO files are concatenated together
780 and stored in one section of that name. Thus each ELF section contains
781 several "virtual" sections. */
782
783 struct virtual_v2_dwo_sections
784 {
785 bfd_size_type abbrev_offset;
786 bfd_size_type abbrev_size;
787
788 bfd_size_type line_offset;
789 bfd_size_type line_size;
790
791 bfd_size_type loc_offset;
792 bfd_size_type loc_size;
793
794 bfd_size_type macinfo_offset;
795 bfd_size_type macinfo_size;
796
797 bfd_size_type macro_offset;
798 bfd_size_type macro_size;
799
800 bfd_size_type str_offsets_offset;
801 bfd_size_type str_offsets_size;
802
803 /* Each DWP hash table entry records one CU or one TU.
804 That is recorded here, and copied to dwo_unit.section. */
805 bfd_size_type info_or_types_offset;
806 bfd_size_type info_or_types_size;
807 };
808
809 /* Contents of DWP hash tables. */
810
811 struct dwp_hash_table
812 {
813 uint32_t version, nr_columns;
814 uint32_t nr_units, nr_slots;
815 const gdb_byte *hash_table, *unit_table;
816 union
817 {
818 struct
819 {
820 const gdb_byte *indices;
821 } v1;
822 struct
823 {
824 /* This is indexed by column number and gives the id of the section
825 in that column. */
826 #define MAX_NR_V2_DWO_SECTIONS \
827 (1 /* .debug_info or .debug_types */ \
828 + 1 /* .debug_abbrev */ \
829 + 1 /* .debug_line */ \
830 + 1 /* .debug_loc */ \
831 + 1 /* .debug_str_offsets */ \
832 + 1 /* .debug_macro or .debug_macinfo */)
833 int section_ids[MAX_NR_V2_DWO_SECTIONS];
834 const gdb_byte *offsets;
835 const gdb_byte *sizes;
836 } v2;
837 } section_pool;
838 };
839
840 /* Data for one DWP file. */
841
842 struct dwp_file
843 {
844 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
845 : name (name_),
846 dbfd (std::move (abfd))
847 {
848 }
849
850 /* Name of the file. */
851 const char *name;
852
853 /* File format version. */
854 int version = 0;
855
856 /* The bfd. */
857 gdb_bfd_ref_ptr dbfd;
858
859 /* Section info for this file. */
860 struct dwp_sections sections {};
861
862 /* Table of CUs in the file. */
863 const struct dwp_hash_table *cus = nullptr;
864
865 /* Table of TUs in the file. */
866 const struct dwp_hash_table *tus = nullptr;
867
868 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
869 htab_t loaded_cus {};
870 htab_t loaded_tus {};
871
872 /* Table to map ELF section numbers to their sections.
873 This is only needed for the DWP V1 file format. */
874 unsigned int num_sections = 0;
875 asection **elf_sections = nullptr;
876 };
877
878 struct abbrev_table;
879 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
880
881 /* Struct used to pass misc. parameters to read_die_and_children, et
882 al. which are used for both .debug_info and .debug_types dies.
883 All parameters here are unchanging for the life of the call. This
884 struct exists to abstract away the constant parameters of die reading. */
885
886 struct die_reader_specs
887 {
888 /* The bfd of die_section. */
889 bfd* abfd;
890
891 /* The CU of the DIE we are parsing. */
892 struct dwarf2_cu *cu;
893
894 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
895 struct dwo_file *dwo_file;
896
897 /* The section the die comes from.
898 This is either .debug_info or .debug_types, or the .dwo variants. */
899 struct dwarf2_section_info *die_section;
900
901 /* die_section->buffer. */
902 const gdb_byte *buffer;
903
904 /* The end of the buffer. */
905 const gdb_byte *buffer_end;
906
907 /* The value of the DW_AT_comp_dir attribute. */
908 const char *comp_dir;
909
910 /* The abbreviation table to use when reading the DIEs. */
911 struct abbrev_table *abbrev_table;
912 };
913
914 /* A subclass of die_reader_specs that holds storage and has complex
915 constructor and destructor behavior. */
916
917 class cutu_reader : public die_reader_specs
918 {
919 public:
920
921 cutu_reader (struct dwarf2_per_cu_data *this_cu,
922 struct abbrev_table *abbrev_table,
923 int use_existing_cu, int keep,
924 bool skip_partial);
925
926 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
927 struct dwarf2_cu *parent_cu = nullptr,
928 struct dwo_file *dwo_file = nullptr);
929
930 ~cutu_reader ();
931
932 DISABLE_COPY_AND_ASSIGN (cutu_reader);
933
934 const gdb_byte *info_ptr = nullptr;
935 struct die_info *comp_unit_die = nullptr;
936 int has_children = 0;
937 bool dummy_p = false;
938
939 private:
940 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
941 int use_existing_cu, int keep);
942
943 struct dwarf2_per_cu_data *m_this_cu;
944 int m_keep = 0;
945 std::unique_ptr<dwarf2_cu> m_new_cu;
946
947 /* The ordinary abbreviation table. */
948 abbrev_table_up m_abbrev_table_holder;
949
950 /* The DWO abbreviation table. */
951 abbrev_table_up m_dwo_abbrev_table;
952 };
953
954 /* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
955 later. */
956 typedef int dir_index;
957
958 /* file_name_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5
959 and later. */
960 typedef int file_name_index;
961
962 struct file_entry
963 {
964 file_entry () = default;
965
966 file_entry (const char *name_, dir_index d_index_,
967 unsigned int mod_time_, unsigned int length_)
968 : name (name_),
969 d_index (d_index_),
970 mod_time (mod_time_),
971 length (length_)
972 {}
973
974 /* Return the include directory at D_INDEX stored in LH. Returns
975 NULL if D_INDEX is out of bounds. */
976 const char *include_dir (const line_header *lh) const;
977
978 /* The file name. Note this is an observing pointer. The memory is
979 owned by debug_line_buffer. */
980 const char *name {};
981
982 /* The directory index (1-based). */
983 dir_index d_index {};
984
985 unsigned int mod_time {};
986
987 unsigned int length {};
988
989 /* True if referenced by the Line Number Program. */
990 bool included_p {};
991
992 /* The associated symbol table, if any. */
993 struct symtab *symtab {};
994 };
995
996 /* The line number information for a compilation unit (found in the
997 .debug_line section) begins with a "statement program header",
998 which contains the following information. */
999 struct line_header
1000 {
1001 line_header ()
1002 : offset_in_dwz {}
1003 {}
1004
1005 /* Add an entry to the include directory table. */
1006 void add_include_dir (const char *include_dir);
1007
1008 /* Add an entry to the file name table. */
1009 void add_file_name (const char *name, dir_index d_index,
1010 unsigned int mod_time, unsigned int length);
1011
1012 /* Return the include dir at INDEX (0-based in DWARF 5 and 1-based before).
1013 Returns NULL if INDEX is out of bounds. */
1014 const char *include_dir_at (dir_index index) const
1015 {
1016 int vec_index;
1017 if (version >= 5)
1018 vec_index = index;
1019 else
1020 vec_index = index - 1;
1021 if (vec_index < 0 || vec_index >= m_include_dirs.size ())
1022 return NULL;
1023 return m_include_dirs[vec_index];
1024 }
1025
1026 bool is_valid_file_index (int file_index)
1027 {
1028 if (version >= 5)
1029 return 0 <= file_index && file_index < file_names_size ();
1030 return 1 <= file_index && file_index <= file_names_size ();
1031 }
1032
1033 /* Return the file name at INDEX (0-based in DWARF 5 and 1-based before).
1034 Returns NULL if INDEX is out of bounds. */
1035 file_entry *file_name_at (file_name_index index)
1036 {
1037 int vec_index;
1038 if (version >= 5)
1039 vec_index = index;
1040 else
1041 vec_index = index - 1;
1042 if (vec_index < 0 || vec_index >= m_file_names.size ())
1043 return NULL;
1044 return &m_file_names[vec_index];
1045 }
1046
1047 /* The indexes are 0-based in DWARF 5 and 1-based in DWARF 4. Therefore,
1048 this method should only be used to iterate through all file entries in an
1049 index-agnostic manner. */
1050 std::vector<file_entry> &file_names ()
1051 { return m_file_names; }
1052
1053 /* Offset of line number information in .debug_line section. */
1054 sect_offset sect_off {};
1055
1056 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1057 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1058
1059 unsigned int total_length {};
1060 unsigned short version {};
1061 unsigned int header_length {};
1062 unsigned char minimum_instruction_length {};
1063 unsigned char maximum_ops_per_instruction {};
1064 unsigned char default_is_stmt {};
1065 int line_base {};
1066 unsigned char line_range {};
1067 unsigned char opcode_base {};
1068
1069 /* standard_opcode_lengths[i] is the number of operands for the
1070 standard opcode whose value is i. This means that
1071 standard_opcode_lengths[0] is unused, and the last meaningful
1072 element is standard_opcode_lengths[opcode_base - 1]. */
1073 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1074
1075 int file_names_size ()
1076 { return m_file_names.size(); }
1077
1078 /* The start and end of the statement program following this
1079 header. These point into dwarf2_per_objfile->line_buffer. */
1080 const gdb_byte *statement_program_start {}, *statement_program_end {};
1081
1082 private:
1083 /* The include_directories table. Note these are observing
1084 pointers. The memory is owned by debug_line_buffer. */
1085 std::vector<const char *> m_include_dirs;
1086
1087 /* The file_names table. This is private because the meaning of indexes
1088 differs among DWARF versions (The first valid index is 1 in DWARF 4 and
1089 before, and is 0 in DWARF 5 and later). So the client should use
1090 file_name_at method for access. */
1091 std::vector<file_entry> m_file_names;
1092 };
1093
1094 typedef std::unique_ptr<line_header> line_header_up;
1095
1096 const char *
1097 file_entry::include_dir (const line_header *lh) const
1098 {
1099 return lh->include_dir_at (d_index);
1100 }
1101
1102 /* When we construct a partial symbol table entry we only
1103 need this much information. */
1104 struct partial_die_info : public allocate_on_obstack
1105 {
1106 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1107
1108 /* Disable assign but still keep copy ctor, which is needed
1109 load_partial_dies. */
1110 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1111
1112 /* Adjust the partial die before generating a symbol for it. This
1113 function may set the is_external flag or change the DIE's
1114 name. */
1115 void fixup (struct dwarf2_cu *cu);
1116
1117 /* Read a minimal amount of information into the minimal die
1118 structure. */
1119 const gdb_byte *read (const struct die_reader_specs *reader,
1120 const struct abbrev_info &abbrev,
1121 const gdb_byte *info_ptr);
1122
1123 /* Offset of this DIE. */
1124 const sect_offset sect_off;
1125
1126 /* DWARF-2 tag for this DIE. */
1127 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1128
1129 /* Assorted flags describing the data found in this DIE. */
1130 const unsigned int has_children : 1;
1131
1132 unsigned int is_external : 1;
1133 unsigned int is_declaration : 1;
1134 unsigned int has_type : 1;
1135 unsigned int has_specification : 1;
1136 unsigned int has_pc_info : 1;
1137 unsigned int may_be_inlined : 1;
1138
1139 /* This DIE has been marked DW_AT_main_subprogram. */
1140 unsigned int main_subprogram : 1;
1141
1142 /* Flag set if the SCOPE field of this structure has been
1143 computed. */
1144 unsigned int scope_set : 1;
1145
1146 /* Flag set if the DIE has a byte_size attribute. */
1147 unsigned int has_byte_size : 1;
1148
1149 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1150 unsigned int has_const_value : 1;
1151
1152 /* Flag set if any of the DIE's children are template arguments. */
1153 unsigned int has_template_arguments : 1;
1154
1155 /* Flag set if fixup has been called on this die. */
1156 unsigned int fixup_called : 1;
1157
1158 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1159 unsigned int is_dwz : 1;
1160
1161 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1162 unsigned int spec_is_dwz : 1;
1163
1164 /* The name of this DIE. Normally the value of DW_AT_name, but
1165 sometimes a default name for unnamed DIEs. */
1166 const char *name = nullptr;
1167
1168 /* The linkage name, if present. */
1169 const char *linkage_name = nullptr;
1170
1171 /* The scope to prepend to our children. This is generally
1172 allocated on the comp_unit_obstack, so will disappear
1173 when this compilation unit leaves the cache. */
1174 const char *scope = nullptr;
1175
1176 /* Some data associated with the partial DIE. The tag determines
1177 which field is live. */
1178 union
1179 {
1180 /* The location description associated with this DIE, if any. */
1181 struct dwarf_block *locdesc;
1182 /* The offset of an import, for DW_TAG_imported_unit. */
1183 sect_offset sect_off;
1184 } d {};
1185
1186 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1187 CORE_ADDR lowpc = 0;
1188 CORE_ADDR highpc = 0;
1189
1190 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1191 DW_AT_sibling, if any. */
1192 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1193 could return DW_AT_sibling values to its caller load_partial_dies. */
1194 const gdb_byte *sibling = nullptr;
1195
1196 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1197 DW_AT_specification (or DW_AT_abstract_origin or
1198 DW_AT_extension). */
1199 sect_offset spec_offset {};
1200
1201 /* Pointers to this DIE's parent, first child, and next sibling,
1202 if any. */
1203 struct partial_die_info *die_parent = nullptr;
1204 struct partial_die_info *die_child = nullptr;
1205 struct partial_die_info *die_sibling = nullptr;
1206
1207 friend struct partial_die_info *
1208 dwarf2_cu::find_partial_die (sect_offset sect_off);
1209
1210 private:
1211 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1212 partial_die_info (sect_offset sect_off)
1213 : partial_die_info (sect_off, DW_TAG_padding, 0)
1214 {
1215 }
1216
1217 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1218 int has_children_)
1219 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1220 {
1221 is_external = 0;
1222 is_declaration = 0;
1223 has_type = 0;
1224 has_specification = 0;
1225 has_pc_info = 0;
1226 may_be_inlined = 0;
1227 main_subprogram = 0;
1228 scope_set = 0;
1229 has_byte_size = 0;
1230 has_const_value = 0;
1231 has_template_arguments = 0;
1232 fixup_called = 0;
1233 is_dwz = 0;
1234 spec_is_dwz = 0;
1235 }
1236 };
1237
1238 /* This data structure holds the information of an abbrev. */
1239 struct abbrev_info
1240 {
1241 unsigned int number; /* number identifying abbrev */
1242 enum dwarf_tag tag; /* dwarf tag */
1243 unsigned short has_children; /* boolean */
1244 unsigned short num_attrs; /* number of attributes */
1245 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1246 struct abbrev_info *next; /* next in chain */
1247 };
1248
1249 struct attr_abbrev
1250 {
1251 ENUM_BITFIELD(dwarf_attribute) name : 16;
1252 ENUM_BITFIELD(dwarf_form) form : 16;
1253
1254 /* It is valid only if FORM is DW_FORM_implicit_const. */
1255 LONGEST implicit_const;
1256 };
1257
1258 /* Size of abbrev_table.abbrev_hash_table. */
1259 #define ABBREV_HASH_SIZE 121
1260
1261 /* Top level data structure to contain an abbreviation table. */
1262
1263 struct abbrev_table
1264 {
1265 explicit abbrev_table (sect_offset off)
1266 : sect_off (off)
1267 {
1268 m_abbrevs =
1269 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1270 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1271 }
1272
1273 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1274
1275 /* Allocate space for a struct abbrev_info object in
1276 ABBREV_TABLE. */
1277 struct abbrev_info *alloc_abbrev ();
1278
1279 /* Add an abbreviation to the table. */
1280 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1281
1282 /* Look up an abbrev in the table.
1283 Returns NULL if the abbrev is not found. */
1284
1285 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1286
1287
1288 /* Where the abbrev table came from.
1289 This is used as a sanity check when the table is used. */
1290 const sect_offset sect_off;
1291
1292 /* Storage for the abbrev table. */
1293 auto_obstack abbrev_obstack;
1294
1295 private:
1296
1297 /* Hash table of abbrevs.
1298 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1299 It could be statically allocated, but the previous code didn't so we
1300 don't either. */
1301 struct abbrev_info **m_abbrevs;
1302 };
1303
1304 /* Attributes have a name and a value. */
1305 struct attribute
1306 {
1307 ENUM_BITFIELD(dwarf_attribute) name : 16;
1308 ENUM_BITFIELD(dwarf_form) form : 15;
1309
1310 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1311 field should be in u.str (existing only for DW_STRING) but it is kept
1312 here for better struct attribute alignment. */
1313 unsigned int string_is_canonical : 1;
1314
1315 union
1316 {
1317 const char *str;
1318 struct dwarf_block *blk;
1319 ULONGEST unsnd;
1320 LONGEST snd;
1321 CORE_ADDR addr;
1322 ULONGEST signature;
1323 }
1324 u;
1325 };
1326
1327 /* This data structure holds a complete die structure. */
1328 struct die_info
1329 {
1330 /* DWARF-2 tag for this DIE. */
1331 ENUM_BITFIELD(dwarf_tag) tag : 16;
1332
1333 /* Number of attributes */
1334 unsigned char num_attrs;
1335
1336 /* True if we're presently building the full type name for the
1337 type derived from this DIE. */
1338 unsigned char building_fullname : 1;
1339
1340 /* True if this die is in process. PR 16581. */
1341 unsigned char in_process : 1;
1342
1343 /* Abbrev number */
1344 unsigned int abbrev;
1345
1346 /* Offset in .debug_info or .debug_types section. */
1347 sect_offset sect_off;
1348
1349 /* The dies in a compilation unit form an n-ary tree. PARENT
1350 points to this die's parent; CHILD points to the first child of
1351 this node; and all the children of a given node are chained
1352 together via their SIBLING fields. */
1353 struct die_info *child; /* Its first child, if any. */
1354 struct die_info *sibling; /* Its next sibling, if any. */
1355 struct die_info *parent; /* Its parent, if any. */
1356
1357 /* An array of attributes, with NUM_ATTRS elements. There may be
1358 zero, but it's not common and zero-sized arrays are not
1359 sufficiently portable C. */
1360 struct attribute attrs[1];
1361 };
1362
1363 /* Get at parts of an attribute structure. */
1364
1365 #define DW_STRING(attr) ((attr)->u.str)
1366 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1367 #define DW_UNSND(attr) ((attr)->u.unsnd)
1368 #define DW_BLOCK(attr) ((attr)->u.blk)
1369 #define DW_SND(attr) ((attr)->u.snd)
1370 #define DW_ADDR(attr) ((attr)->u.addr)
1371 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1372
1373 /* Blocks are a bunch of untyped bytes. */
1374 struct dwarf_block
1375 {
1376 size_t size;
1377
1378 /* Valid only if SIZE is not zero. */
1379 const gdb_byte *data;
1380 };
1381
1382 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1383 but this would require a corresponding change in unpack_field_as_long
1384 and friends. */
1385 static int bits_per_byte = 8;
1386
1387 /* When reading a variant or variant part, we track a bit more
1388 information about the field, and store it in an object of this
1389 type. */
1390
1391 struct variant_field
1392 {
1393 /* If we see a DW_TAG_variant, then this will be the discriminant
1394 value. */
1395 ULONGEST discriminant_value;
1396 /* If we see a DW_TAG_variant, then this will be set if this is the
1397 default branch. */
1398 bool default_branch;
1399 /* While reading a DW_TAG_variant_part, this will be set if this
1400 field is the discriminant. */
1401 bool is_discriminant;
1402 };
1403
1404 struct nextfield
1405 {
1406 int accessibility = 0;
1407 int virtuality = 0;
1408 /* Extra information to describe a variant or variant part. */
1409 struct variant_field variant {};
1410 struct field field {};
1411 };
1412
1413 struct fnfieldlist
1414 {
1415 const char *name = nullptr;
1416 std::vector<struct fn_field> fnfields;
1417 };
1418
1419 /* The routines that read and process dies for a C struct or C++ class
1420 pass lists of data member fields and lists of member function fields
1421 in an instance of a field_info structure, as defined below. */
1422 struct field_info
1423 {
1424 /* List of data member and baseclasses fields. */
1425 std::vector<struct nextfield> fields;
1426 std::vector<struct nextfield> baseclasses;
1427
1428 /* Number of fields (including baseclasses). */
1429 int nfields = 0;
1430
1431 /* Set if the accessibility of one of the fields is not public. */
1432 int non_public_fields = 0;
1433
1434 /* Member function fieldlist array, contains name of possibly overloaded
1435 member function, number of overloaded member functions and a pointer
1436 to the head of the member function field chain. */
1437 std::vector<struct fnfieldlist> fnfieldlists;
1438
1439 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1440 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1441 std::vector<struct decl_field> typedef_field_list;
1442
1443 /* Nested types defined by this class and the number of elements in this
1444 list. */
1445 std::vector<struct decl_field> nested_types_list;
1446 };
1447
1448 /* One item on the queue of compilation units to read in full symbols
1449 for. */
1450 struct dwarf2_queue_item
1451 {
1452 struct dwarf2_per_cu_data *per_cu;
1453 enum language pretend_language;
1454 struct dwarf2_queue_item *next;
1455 };
1456
1457 /* The current queue. */
1458 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1459
1460 /* Loaded secondary compilation units are kept in memory until they
1461 have not been referenced for the processing of this many
1462 compilation units. Set this to zero to disable caching. Cache
1463 sizes of up to at least twenty will improve startup time for
1464 typical inter-CU-reference binaries, at an obvious memory cost. */
1465 static int dwarf_max_cache_age = 5;
1466 static void
1467 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1468 struct cmd_list_element *c, const char *value)
1469 {
1470 fprintf_filtered (file, _("The upper bound on the age of cached "
1471 "DWARF compilation units is %s.\n"),
1472 value);
1473 }
1474 \f
1475 /* local function prototypes */
1476
1477 static const char *get_section_name (const struct dwarf2_section_info *);
1478
1479 static const char *get_section_file_name (const struct dwarf2_section_info *);
1480
1481 static void dwarf2_find_base_address (struct die_info *die,
1482 struct dwarf2_cu *cu);
1483
1484 static dwarf2_psymtab *create_partial_symtab
1485 (struct dwarf2_per_cu_data *per_cu, const char *name);
1486
1487 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1488 const gdb_byte *info_ptr,
1489 struct die_info *type_unit_die,
1490 int has_children);
1491
1492 static void dwarf2_build_psymtabs_hard
1493 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1494
1495 static void scan_partial_symbols (struct partial_die_info *,
1496 CORE_ADDR *, CORE_ADDR *,
1497 int, struct dwarf2_cu *);
1498
1499 static void add_partial_symbol (struct partial_die_info *,
1500 struct dwarf2_cu *);
1501
1502 static void add_partial_namespace (struct partial_die_info *pdi,
1503 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1504 int set_addrmap, struct dwarf2_cu *cu);
1505
1506 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1507 CORE_ADDR *highpc, int set_addrmap,
1508 struct dwarf2_cu *cu);
1509
1510 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1511 struct dwarf2_cu *cu);
1512
1513 static void add_partial_subprogram (struct partial_die_info *pdi,
1514 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1515 int need_pc, struct dwarf2_cu *cu);
1516
1517 static abbrev_table_up abbrev_table_read_table
1518 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1519 sect_offset);
1520
1521 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1522
1523 static struct partial_die_info *load_partial_dies
1524 (const struct die_reader_specs *, const gdb_byte *, int);
1525
1526 /* A pair of partial_die_info and compilation unit. */
1527 struct cu_partial_die_info
1528 {
1529 /* The compilation unit of the partial_die_info. */
1530 struct dwarf2_cu *cu;
1531 /* A partial_die_info. */
1532 struct partial_die_info *pdi;
1533
1534 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1535 : cu (cu),
1536 pdi (pdi)
1537 { /* Nothing. */ }
1538
1539 private:
1540 cu_partial_die_info () = delete;
1541 };
1542
1543 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1544 struct dwarf2_cu *);
1545
1546 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1547 struct attribute *, struct attr_abbrev *,
1548 const gdb_byte *, bool *need_reprocess);
1549
1550 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1551 struct attribute *attr);
1552
1553 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1554
1555 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1556 unsigned int *);
1557
1558 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1559
1560 static LONGEST read_checked_initial_length_and_offset
1561 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1562 unsigned int *, unsigned int *);
1563
1564 static LONGEST read_offset (bfd *, const gdb_byte *,
1565 const struct comp_unit_head *,
1566 unsigned int *);
1567
1568 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1569
1570 static sect_offset read_abbrev_offset
1571 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1572 struct dwarf2_section_info *, sect_offset);
1573
1574 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1575
1576 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1577
1578 static const char *read_indirect_string
1579 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1580 const struct comp_unit_head *, unsigned int *);
1581
1582 static const char *read_indirect_line_string
1583 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1584 const struct comp_unit_head *, unsigned int *);
1585
1586 static const char *read_indirect_string_at_offset
1587 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1588 LONGEST str_offset);
1589
1590 static const char *read_indirect_string_from_dwz
1591 (struct objfile *objfile, struct dwz_file *, LONGEST);
1592
1593 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1594 const gdb_byte *,
1595 unsigned int *);
1596
1597 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1598 ULONGEST str_index);
1599
1600 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1601 ULONGEST str_index);
1602
1603 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1604
1605 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1606 struct dwarf2_cu *);
1607
1608 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1609 unsigned int);
1610
1611 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1612 struct dwarf2_cu *cu);
1613
1614 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1615
1616 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1617 struct dwarf2_cu *cu);
1618
1619 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1620
1621 static struct die_info *die_specification (struct die_info *die,
1622 struct dwarf2_cu **);
1623
1624 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1625 struct dwarf2_cu *cu);
1626
1627 static void dwarf_decode_lines (struct line_header *, const char *,
1628 struct dwarf2_cu *, dwarf2_psymtab *,
1629 CORE_ADDR, int decode_mapping);
1630
1631 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1632 const char *);
1633
1634 static struct symbol *new_symbol (struct die_info *, struct type *,
1635 struct dwarf2_cu *, struct symbol * = NULL);
1636
1637 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1638 struct dwarf2_cu *);
1639
1640 static void dwarf2_const_value_attr (const struct attribute *attr,
1641 struct type *type,
1642 const char *name,
1643 struct obstack *obstack,
1644 struct dwarf2_cu *cu, LONGEST *value,
1645 const gdb_byte **bytes,
1646 struct dwarf2_locexpr_baton **baton);
1647
1648 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1649
1650 static int need_gnat_info (struct dwarf2_cu *);
1651
1652 static struct type *die_descriptive_type (struct die_info *,
1653 struct dwarf2_cu *);
1654
1655 static void set_descriptive_type (struct type *, struct die_info *,
1656 struct dwarf2_cu *);
1657
1658 static struct type *die_containing_type (struct die_info *,
1659 struct dwarf2_cu *);
1660
1661 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1662 struct dwarf2_cu *);
1663
1664 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1665
1666 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1667
1668 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1669
1670 static char *typename_concat (struct obstack *obs, const char *prefix,
1671 const char *suffix, int physname,
1672 struct dwarf2_cu *cu);
1673
1674 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1675
1676 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1677
1678 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1679
1680 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1681
1682 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1683
1684 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1685
1686 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1687 struct dwarf2_cu *, dwarf2_psymtab *);
1688
1689 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1690 values. Keep the items ordered with increasing constraints compliance. */
1691 enum pc_bounds_kind
1692 {
1693 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1694 PC_BOUNDS_NOT_PRESENT,
1695
1696 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1697 were present but they do not form a valid range of PC addresses. */
1698 PC_BOUNDS_INVALID,
1699
1700 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1701 PC_BOUNDS_RANGES,
1702
1703 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1704 PC_BOUNDS_HIGH_LOW,
1705 };
1706
1707 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1708 CORE_ADDR *, CORE_ADDR *,
1709 struct dwarf2_cu *,
1710 dwarf2_psymtab *);
1711
1712 static void get_scope_pc_bounds (struct die_info *,
1713 CORE_ADDR *, CORE_ADDR *,
1714 struct dwarf2_cu *);
1715
1716 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1717 CORE_ADDR, struct dwarf2_cu *);
1718
1719 static void dwarf2_add_field (struct field_info *, struct die_info *,
1720 struct dwarf2_cu *);
1721
1722 static void dwarf2_attach_fields_to_type (struct field_info *,
1723 struct type *, struct dwarf2_cu *);
1724
1725 static void dwarf2_add_member_fn (struct field_info *,
1726 struct die_info *, struct type *,
1727 struct dwarf2_cu *);
1728
1729 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1730 struct type *,
1731 struct dwarf2_cu *);
1732
1733 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1734
1735 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1736
1737 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1738
1739 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1740
1741 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1742
1743 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1744
1745 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1746
1747 static struct type *read_module_type (struct die_info *die,
1748 struct dwarf2_cu *cu);
1749
1750 static const char *namespace_name (struct die_info *die,
1751 int *is_anonymous, struct dwarf2_cu *);
1752
1753 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1754
1755 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1756
1757 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1758 struct dwarf2_cu *);
1759
1760 static struct die_info *read_die_and_siblings_1
1761 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1762 struct die_info *);
1763
1764 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1765 const gdb_byte *info_ptr,
1766 const gdb_byte **new_info_ptr,
1767 struct die_info *parent);
1768
1769 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1770 struct die_info **, const gdb_byte *,
1771 int *, int);
1772
1773 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1774 struct die_info **, const gdb_byte *,
1775 int *);
1776
1777 static void process_die (struct die_info *, struct dwarf2_cu *);
1778
1779 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1780 struct obstack *);
1781
1782 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1783
1784 static const char *dwarf2_full_name (const char *name,
1785 struct die_info *die,
1786 struct dwarf2_cu *cu);
1787
1788 static const char *dwarf2_physname (const char *name, struct die_info *die,
1789 struct dwarf2_cu *cu);
1790
1791 static struct die_info *dwarf2_extension (struct die_info *die,
1792 struct dwarf2_cu **);
1793
1794 static const char *dwarf_tag_name (unsigned int);
1795
1796 static const char *dwarf_attr_name (unsigned int);
1797
1798 static const char *dwarf_unit_type_name (int unit_type);
1799
1800 static const char *dwarf_form_name (unsigned int);
1801
1802 static const char *dwarf_bool_name (unsigned int);
1803
1804 static const char *dwarf_type_encoding_name (unsigned int);
1805
1806 static struct die_info *sibling_die (struct die_info *);
1807
1808 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1809
1810 static void dump_die_for_error (struct die_info *);
1811
1812 static void dump_die_1 (struct ui_file *, int level, int max_level,
1813 struct die_info *);
1814
1815 /*static*/ void dump_die (struct die_info *, int max_level);
1816
1817 static void store_in_ref_table (struct die_info *,
1818 struct dwarf2_cu *);
1819
1820 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1821
1822 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1823
1824 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1825 const struct attribute *,
1826 struct dwarf2_cu **);
1827
1828 static struct die_info *follow_die_ref (struct die_info *,
1829 const struct attribute *,
1830 struct dwarf2_cu **);
1831
1832 static struct die_info *follow_die_sig (struct die_info *,
1833 const struct attribute *,
1834 struct dwarf2_cu **);
1835
1836 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1837 struct dwarf2_cu *);
1838
1839 static struct type *get_DW_AT_signature_type (struct die_info *,
1840 const struct attribute *,
1841 struct dwarf2_cu *);
1842
1843 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1844
1845 static void read_signatured_type (struct signatured_type *);
1846
1847 static int attr_to_dynamic_prop (const struct attribute *attr,
1848 struct die_info *die, struct dwarf2_cu *cu,
1849 struct dynamic_prop *prop, struct type *type);
1850
1851 /* memory allocation interface */
1852
1853 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1854
1855 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1856
1857 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1858
1859 static int attr_form_is_block (const struct attribute *);
1860
1861 static int attr_form_is_section_offset (const struct attribute *);
1862
1863 static int attr_form_is_constant (const struct attribute *);
1864
1865 static int attr_form_is_ref (const struct attribute *);
1866
1867 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1868 struct dwarf2_loclist_baton *baton,
1869 const struct attribute *attr);
1870
1871 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1872 struct symbol *sym,
1873 struct dwarf2_cu *cu,
1874 int is_block);
1875
1876 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1877 const gdb_byte *info_ptr,
1878 struct abbrev_info *abbrev);
1879
1880 static hashval_t partial_die_hash (const void *item);
1881
1882 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1883
1884 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1885 (sect_offset sect_off, unsigned int offset_in_dwz,
1886 struct dwarf2_per_objfile *dwarf2_per_objfile);
1887
1888 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1889 struct die_info *comp_unit_die,
1890 enum language pretend_language);
1891
1892 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1893
1894 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1895
1896 static struct type *set_die_type (struct die_info *, struct type *,
1897 struct dwarf2_cu *);
1898
1899 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1900
1901 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1902
1903 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1904 enum language);
1905
1906 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1907 enum language);
1908
1909 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1910 enum language);
1911
1912 static void dwarf2_add_dependence (struct dwarf2_cu *,
1913 struct dwarf2_per_cu_data *);
1914
1915 static void dwarf2_mark (struct dwarf2_cu *);
1916
1917 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1918
1919 static struct type *get_die_type_at_offset (sect_offset,
1920 struct dwarf2_per_cu_data *);
1921
1922 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1923
1924 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1925 enum language pretend_language);
1926
1927 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1928
1929 static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1930 static struct type *dwarf2_per_cu_addr_sized_int_type
1931 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1932 static struct type *dwarf2_per_cu_int_type
1933 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1934 bool unsigned_p);
1935
1936 /* Class, the destructor of which frees all allocated queue entries. This
1937 will only have work to do if an error was thrown while processing the
1938 dwarf. If no error was thrown then the queue entries should have all
1939 been processed, and freed, as we went along. */
1940
1941 class dwarf2_queue_guard
1942 {
1943 public:
1944 dwarf2_queue_guard () = default;
1945
1946 /* Free any entries remaining on the queue. There should only be
1947 entries left if we hit an error while processing the dwarf. */
1948 ~dwarf2_queue_guard ()
1949 {
1950 struct dwarf2_queue_item *item, *last;
1951
1952 item = dwarf2_queue;
1953 while (item)
1954 {
1955 /* Anything still marked queued is likely to be in an
1956 inconsistent state, so discard it. */
1957 if (item->per_cu->queued)
1958 {
1959 if (item->per_cu->cu != NULL)
1960 free_one_cached_comp_unit (item->per_cu);
1961 item->per_cu->queued = 0;
1962 }
1963
1964 last = item;
1965 item = item->next;
1966 xfree (last);
1967 }
1968
1969 dwarf2_queue = dwarf2_queue_tail = NULL;
1970 }
1971 };
1972
1973 /* The return type of find_file_and_directory. Note, the enclosed
1974 string pointers are only valid while this object is valid. */
1975
1976 struct file_and_directory
1977 {
1978 /* The filename. This is never NULL. */
1979 const char *name;
1980
1981 /* The compilation directory. NULL if not known. If we needed to
1982 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1983 points directly to the DW_AT_comp_dir string attribute owned by
1984 the obstack that owns the DIE. */
1985 const char *comp_dir;
1986
1987 /* If we needed to build a new string for comp_dir, this is what
1988 owns the storage. */
1989 std::string comp_dir_storage;
1990 };
1991
1992 static file_and_directory find_file_and_directory (struct die_info *die,
1993 struct dwarf2_cu *cu);
1994
1995 static char *file_full_name (int file, struct line_header *lh,
1996 const char *comp_dir);
1997
1998 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1999 enum class rcuh_kind { COMPILE, TYPE };
2000
2001 static const gdb_byte *read_and_check_comp_unit_head
2002 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2003 struct comp_unit_head *header,
2004 struct dwarf2_section_info *section,
2005 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2006 rcuh_kind section_kind);
2007
2008 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2009
2010 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2011
2012 static struct dwo_unit *lookup_dwo_unit_in_dwp
2013 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2014 struct dwp_file *dwp_file, const char *comp_dir,
2015 ULONGEST signature, int is_debug_types);
2016
2017 static struct dwp_file *get_dwp_file
2018 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2019
2020 static struct dwo_unit *lookup_dwo_comp_unit
2021 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2022
2023 static struct dwo_unit *lookup_dwo_type_unit
2024 (struct signatured_type *, const char *, const char *);
2025
2026 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2027
2028 /* A unique pointer to a dwo_file. */
2029
2030 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
2031
2032 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2033
2034 static void check_producer (struct dwarf2_cu *cu);
2035
2036 static void free_line_header_voidp (void *arg);
2037 \f
2038 /* Various complaints about symbol reading that don't abort the process. */
2039
2040 static void
2041 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2042 {
2043 complaint (_("statement list doesn't fit in .debug_line section"));
2044 }
2045
2046 static void
2047 dwarf2_debug_line_missing_file_complaint (void)
2048 {
2049 complaint (_(".debug_line section has line data without a file"));
2050 }
2051
2052 static void
2053 dwarf2_debug_line_missing_end_sequence_complaint (void)
2054 {
2055 complaint (_(".debug_line section has line "
2056 "program sequence without an end"));
2057 }
2058
2059 static void
2060 dwarf2_complex_location_expr_complaint (void)
2061 {
2062 complaint (_("location expression too complex"));
2063 }
2064
2065 static void
2066 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2067 int arg3)
2068 {
2069 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
2070 arg1, arg2, arg3);
2071 }
2072
2073 static void
2074 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2075 {
2076 complaint (_("debug info runs off end of %s section"
2077 " [in module %s]"),
2078 get_section_name (section),
2079 get_section_file_name (section));
2080 }
2081
2082 static void
2083 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2084 {
2085 complaint (_("macro debug info contains a "
2086 "malformed macro definition:\n`%s'"),
2087 arg1);
2088 }
2089
2090 static void
2091 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2092 {
2093 complaint (_("invalid attribute class or form for '%s' in '%s'"),
2094 arg1, arg2);
2095 }
2096
2097 /* Hash function for line_header_hash. */
2098
2099 static hashval_t
2100 line_header_hash (const struct line_header *ofs)
2101 {
2102 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2103 }
2104
2105 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2106
2107 static hashval_t
2108 line_header_hash_voidp (const void *item)
2109 {
2110 const struct line_header *ofs = (const struct line_header *) item;
2111
2112 return line_header_hash (ofs);
2113 }
2114
2115 /* Equality function for line_header_hash. */
2116
2117 static int
2118 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2119 {
2120 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2121 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2122
2123 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2124 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2125 }
2126
2127 \f
2128
2129 /* Read the given attribute value as an address, taking the attribute's
2130 form into account. */
2131
2132 static CORE_ADDR
2133 attr_value_as_address (struct attribute *attr)
2134 {
2135 CORE_ADDR addr;
2136
2137 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2138 && attr->form != DW_FORM_GNU_addr_index)
2139 {
2140 /* Aside from a few clearly defined exceptions, attributes that
2141 contain an address must always be in DW_FORM_addr form.
2142 Unfortunately, some compilers happen to be violating this
2143 requirement by encoding addresses using other forms, such
2144 as DW_FORM_data4 for example. For those broken compilers,
2145 we try to do our best, without any guarantee of success,
2146 to interpret the address correctly. It would also be nice
2147 to generate a complaint, but that would require us to maintain
2148 a list of legitimate cases where a non-address form is allowed,
2149 as well as update callers to pass in at least the CU's DWARF
2150 version. This is more overhead than what we're willing to
2151 expand for a pretty rare case. */
2152 addr = DW_UNSND (attr);
2153 }
2154 else
2155 addr = DW_ADDR (attr);
2156
2157 return addr;
2158 }
2159
2160 /* See declaration. */
2161
2162 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2163 const dwarf2_debug_sections *names,
2164 bool can_copy_)
2165 : objfile (objfile_),
2166 can_copy (can_copy_)
2167 {
2168 if (names == NULL)
2169 names = &dwarf2_elf_names;
2170
2171 bfd *obfd = objfile->obfd;
2172
2173 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2174 locate_sections (obfd, sec, *names);
2175 }
2176
2177 dwarf2_per_objfile::~dwarf2_per_objfile ()
2178 {
2179 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2180 free_cached_comp_units ();
2181
2182 if (quick_file_names_table)
2183 htab_delete (quick_file_names_table);
2184
2185 if (line_header_hash)
2186 htab_delete (line_header_hash);
2187
2188 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2189 per_cu->imported_symtabs_free ();
2190
2191 for (signatured_type *sig_type : all_type_units)
2192 sig_type->per_cu.imported_symtabs_free ();
2193
2194 /* Everything else should be on the objfile obstack. */
2195 }
2196
2197 /* See declaration. */
2198
2199 void
2200 dwarf2_per_objfile::free_cached_comp_units ()
2201 {
2202 dwarf2_per_cu_data *per_cu = read_in_chain;
2203 dwarf2_per_cu_data **last_chain = &read_in_chain;
2204 while (per_cu != NULL)
2205 {
2206 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2207
2208 delete per_cu->cu;
2209 *last_chain = next_cu;
2210 per_cu = next_cu;
2211 }
2212 }
2213
2214 /* A helper class that calls free_cached_comp_units on
2215 destruction. */
2216
2217 class free_cached_comp_units
2218 {
2219 public:
2220
2221 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2222 : m_per_objfile (per_objfile)
2223 {
2224 }
2225
2226 ~free_cached_comp_units ()
2227 {
2228 m_per_objfile->free_cached_comp_units ();
2229 }
2230
2231 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2232
2233 private:
2234
2235 dwarf2_per_objfile *m_per_objfile;
2236 };
2237
2238 /* Try to locate the sections we need for DWARF 2 debugging
2239 information and return true if we have enough to do something.
2240 NAMES points to the dwarf2 section names, or is NULL if the standard
2241 ELF names are used. CAN_COPY is true for formats where symbol
2242 interposition is possible and so symbol values must follow copy
2243 relocation rules. */
2244
2245 int
2246 dwarf2_has_info (struct objfile *objfile,
2247 const struct dwarf2_debug_sections *names,
2248 bool can_copy)
2249 {
2250 if (objfile->flags & OBJF_READNEVER)
2251 return 0;
2252
2253 struct dwarf2_per_objfile *dwarf2_per_objfile
2254 = get_dwarf2_per_objfile (objfile);
2255
2256 if (dwarf2_per_objfile == NULL)
2257 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2258 names,
2259 can_copy);
2260
2261 return (!dwarf2_per_objfile->info.is_virtual
2262 && dwarf2_per_objfile->info.s.section != NULL
2263 && !dwarf2_per_objfile->abbrev.is_virtual
2264 && dwarf2_per_objfile->abbrev.s.section != NULL);
2265 }
2266
2267 /* Return the containing section of virtual section SECTION. */
2268
2269 static struct dwarf2_section_info *
2270 get_containing_section (const struct dwarf2_section_info *section)
2271 {
2272 gdb_assert (section->is_virtual);
2273 return section->s.containing_section;
2274 }
2275
2276 /* Return the bfd owner of SECTION. */
2277
2278 static struct bfd *
2279 get_section_bfd_owner (const struct dwarf2_section_info *section)
2280 {
2281 if (section->is_virtual)
2282 {
2283 section = get_containing_section (section);
2284 gdb_assert (!section->is_virtual);
2285 }
2286 return section->s.section->owner;
2287 }
2288
2289 /* Return the bfd section of SECTION.
2290 Returns NULL if the section is not present. */
2291
2292 static asection *
2293 get_section_bfd_section (const struct dwarf2_section_info *section)
2294 {
2295 if (section->is_virtual)
2296 {
2297 section = get_containing_section (section);
2298 gdb_assert (!section->is_virtual);
2299 }
2300 return section->s.section;
2301 }
2302
2303 /* Return the name of SECTION. */
2304
2305 static const char *
2306 get_section_name (const struct dwarf2_section_info *section)
2307 {
2308 asection *sectp = get_section_bfd_section (section);
2309
2310 gdb_assert (sectp != NULL);
2311 return bfd_section_name (sectp);
2312 }
2313
2314 /* Return the name of the file SECTION is in. */
2315
2316 static const char *
2317 get_section_file_name (const struct dwarf2_section_info *section)
2318 {
2319 bfd *abfd = get_section_bfd_owner (section);
2320
2321 return bfd_get_filename (abfd);
2322 }
2323
2324 /* Return the id of SECTION.
2325 Returns 0 if SECTION doesn't exist. */
2326
2327 static int
2328 get_section_id (const struct dwarf2_section_info *section)
2329 {
2330 asection *sectp = get_section_bfd_section (section);
2331
2332 if (sectp == NULL)
2333 return 0;
2334 return sectp->id;
2335 }
2336
2337 /* Return the flags of SECTION.
2338 SECTION (or containing section if this is a virtual section) must exist. */
2339
2340 static int
2341 get_section_flags (const struct dwarf2_section_info *section)
2342 {
2343 asection *sectp = get_section_bfd_section (section);
2344
2345 gdb_assert (sectp != NULL);
2346 return bfd_section_flags (sectp);
2347 }
2348
2349 /* When loading sections, we look either for uncompressed section or for
2350 compressed section names. */
2351
2352 static int
2353 section_is_p (const char *section_name,
2354 const struct dwarf2_section_names *names)
2355 {
2356 if (names->normal != NULL
2357 && strcmp (section_name, names->normal) == 0)
2358 return 1;
2359 if (names->compressed != NULL
2360 && strcmp (section_name, names->compressed) == 0)
2361 return 1;
2362 return 0;
2363 }
2364
2365 /* See declaration. */
2366
2367 void
2368 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2369 const dwarf2_debug_sections &names)
2370 {
2371 flagword aflag = bfd_section_flags (sectp);
2372
2373 if ((aflag & SEC_HAS_CONTENTS) == 0)
2374 {
2375 }
2376 else if (elf_section_data (sectp)->this_hdr.sh_size
2377 > bfd_get_file_size (abfd))
2378 {
2379 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2380 warning (_("Discarding section %s which has a section size (%s"
2381 ") larger than the file size [in module %s]"),
2382 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2383 bfd_get_filename (abfd));
2384 }
2385 else if (section_is_p (sectp->name, &names.info))
2386 {
2387 this->info.s.section = sectp;
2388 this->info.size = bfd_section_size (sectp);
2389 }
2390 else if (section_is_p (sectp->name, &names.abbrev))
2391 {
2392 this->abbrev.s.section = sectp;
2393 this->abbrev.size = bfd_section_size (sectp);
2394 }
2395 else if (section_is_p (sectp->name, &names.line))
2396 {
2397 this->line.s.section = sectp;
2398 this->line.size = bfd_section_size (sectp);
2399 }
2400 else if (section_is_p (sectp->name, &names.loc))
2401 {
2402 this->loc.s.section = sectp;
2403 this->loc.size = bfd_section_size (sectp);
2404 }
2405 else if (section_is_p (sectp->name, &names.loclists))
2406 {
2407 this->loclists.s.section = sectp;
2408 this->loclists.size = bfd_section_size (sectp);
2409 }
2410 else if (section_is_p (sectp->name, &names.macinfo))
2411 {
2412 this->macinfo.s.section = sectp;
2413 this->macinfo.size = bfd_section_size (sectp);
2414 }
2415 else if (section_is_p (sectp->name, &names.macro))
2416 {
2417 this->macro.s.section = sectp;
2418 this->macro.size = bfd_section_size (sectp);
2419 }
2420 else if (section_is_p (sectp->name, &names.str))
2421 {
2422 this->str.s.section = sectp;
2423 this->str.size = bfd_section_size (sectp);
2424 }
2425 else if (section_is_p (sectp->name, &names.str_offsets))
2426 {
2427 this->str_offsets.s.section = sectp;
2428 this->str_offsets.size = bfd_section_size (sectp);
2429 }
2430 else if (section_is_p (sectp->name, &names.line_str))
2431 {
2432 this->line_str.s.section = sectp;
2433 this->line_str.size = bfd_section_size (sectp);
2434 }
2435 else if (section_is_p (sectp->name, &names.addr))
2436 {
2437 this->addr.s.section = sectp;
2438 this->addr.size = bfd_section_size (sectp);
2439 }
2440 else if (section_is_p (sectp->name, &names.frame))
2441 {
2442 this->frame.s.section = sectp;
2443 this->frame.size = bfd_section_size (sectp);
2444 }
2445 else if (section_is_p (sectp->name, &names.eh_frame))
2446 {
2447 this->eh_frame.s.section = sectp;
2448 this->eh_frame.size = bfd_section_size (sectp);
2449 }
2450 else if (section_is_p (sectp->name, &names.ranges))
2451 {
2452 this->ranges.s.section = sectp;
2453 this->ranges.size = bfd_section_size (sectp);
2454 }
2455 else if (section_is_p (sectp->name, &names.rnglists))
2456 {
2457 this->rnglists.s.section = sectp;
2458 this->rnglists.size = bfd_section_size (sectp);
2459 }
2460 else if (section_is_p (sectp->name, &names.types))
2461 {
2462 struct dwarf2_section_info type_section;
2463
2464 memset (&type_section, 0, sizeof (type_section));
2465 type_section.s.section = sectp;
2466 type_section.size = bfd_section_size (sectp);
2467
2468 this->types.push_back (type_section);
2469 }
2470 else if (section_is_p (sectp->name, &names.gdb_index))
2471 {
2472 this->gdb_index.s.section = sectp;
2473 this->gdb_index.size = bfd_section_size (sectp);
2474 }
2475 else if (section_is_p (sectp->name, &names.debug_names))
2476 {
2477 this->debug_names.s.section = sectp;
2478 this->debug_names.size = bfd_section_size (sectp);
2479 }
2480 else if (section_is_p (sectp->name, &names.debug_aranges))
2481 {
2482 this->debug_aranges.s.section = sectp;
2483 this->debug_aranges.size = bfd_section_size (sectp);
2484 }
2485
2486 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2487 && bfd_section_vma (sectp) == 0)
2488 this->has_section_at_zero = true;
2489 }
2490
2491 /* A helper function that decides whether a section is empty,
2492 or not present. */
2493
2494 static int
2495 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2496 {
2497 if (section->is_virtual)
2498 return section->size == 0;
2499 return section->s.section == NULL || section->size == 0;
2500 }
2501
2502 /* See dwarf2read.h. */
2503
2504 void
2505 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2506 {
2507 asection *sectp;
2508 bfd *abfd;
2509 gdb_byte *buf, *retbuf;
2510
2511 if (info->readin)
2512 return;
2513 info->buffer = NULL;
2514 info->readin = true;
2515
2516 if (dwarf2_section_empty_p (info))
2517 return;
2518
2519 sectp = get_section_bfd_section (info);
2520
2521 /* If this is a virtual section we need to read in the real one first. */
2522 if (info->is_virtual)
2523 {
2524 struct dwarf2_section_info *containing_section =
2525 get_containing_section (info);
2526
2527 gdb_assert (sectp != NULL);
2528 if ((sectp->flags & SEC_RELOC) != 0)
2529 {
2530 error (_("Dwarf Error: DWP format V2 with relocations is not"
2531 " supported in section %s [in module %s]"),
2532 get_section_name (info), get_section_file_name (info));
2533 }
2534 dwarf2_read_section (objfile, containing_section);
2535 /* Other code should have already caught virtual sections that don't
2536 fit. */
2537 gdb_assert (info->virtual_offset + info->size
2538 <= containing_section->size);
2539 /* If the real section is empty or there was a problem reading the
2540 section we shouldn't get here. */
2541 gdb_assert (containing_section->buffer != NULL);
2542 info->buffer = containing_section->buffer + info->virtual_offset;
2543 return;
2544 }
2545
2546 /* If the section has relocations, we must read it ourselves.
2547 Otherwise we attach it to the BFD. */
2548 if ((sectp->flags & SEC_RELOC) == 0)
2549 {
2550 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2551 return;
2552 }
2553
2554 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2555 info->buffer = buf;
2556
2557 /* When debugging .o files, we may need to apply relocations; see
2558 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2559 We never compress sections in .o files, so we only need to
2560 try this when the section is not compressed. */
2561 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2562 if (retbuf != NULL)
2563 {
2564 info->buffer = retbuf;
2565 return;
2566 }
2567
2568 abfd = get_section_bfd_owner (info);
2569 gdb_assert (abfd != NULL);
2570
2571 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2572 || bfd_bread (buf, info->size, abfd) != info->size)
2573 {
2574 error (_("Dwarf Error: Can't read DWARF data"
2575 " in section %s [in module %s]"),
2576 bfd_section_name (sectp), bfd_get_filename (abfd));
2577 }
2578 }
2579
2580 /* A helper function that returns the size of a section in a safe way.
2581 If you are positive that the section has been read before using the
2582 size, then it is safe to refer to the dwarf2_section_info object's
2583 "size" field directly. In other cases, you must call this
2584 function, because for compressed sections the size field is not set
2585 correctly until the section has been read. */
2586
2587 static bfd_size_type
2588 dwarf2_section_size (struct objfile *objfile,
2589 struct dwarf2_section_info *info)
2590 {
2591 if (!info->readin)
2592 dwarf2_read_section (objfile, info);
2593 return info->size;
2594 }
2595
2596 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2597 SECTION_NAME. */
2598
2599 void
2600 dwarf2_get_section_info (struct objfile *objfile,
2601 enum dwarf2_section_enum sect,
2602 asection **sectp, const gdb_byte **bufp,
2603 bfd_size_type *sizep)
2604 {
2605 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2606 struct dwarf2_section_info *info;
2607
2608 /* We may see an objfile without any DWARF, in which case we just
2609 return nothing. */
2610 if (data == NULL)
2611 {
2612 *sectp = NULL;
2613 *bufp = NULL;
2614 *sizep = 0;
2615 return;
2616 }
2617 switch (sect)
2618 {
2619 case DWARF2_DEBUG_FRAME:
2620 info = &data->frame;
2621 break;
2622 case DWARF2_EH_FRAME:
2623 info = &data->eh_frame;
2624 break;
2625 default:
2626 gdb_assert_not_reached ("unexpected section");
2627 }
2628
2629 dwarf2_read_section (objfile, info);
2630
2631 *sectp = get_section_bfd_section (info);
2632 *bufp = info->buffer;
2633 *sizep = info->size;
2634 }
2635
2636 /* A helper function to find the sections for a .dwz file. */
2637
2638 static void
2639 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2640 {
2641 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2642
2643 /* Note that we only support the standard ELF names, because .dwz
2644 is ELF-only (at the time of writing). */
2645 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2646 {
2647 dwz_file->abbrev.s.section = sectp;
2648 dwz_file->abbrev.size = bfd_section_size (sectp);
2649 }
2650 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2651 {
2652 dwz_file->info.s.section = sectp;
2653 dwz_file->info.size = bfd_section_size (sectp);
2654 }
2655 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2656 {
2657 dwz_file->str.s.section = sectp;
2658 dwz_file->str.size = bfd_section_size (sectp);
2659 }
2660 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2661 {
2662 dwz_file->line.s.section = sectp;
2663 dwz_file->line.size = bfd_section_size (sectp);
2664 }
2665 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2666 {
2667 dwz_file->macro.s.section = sectp;
2668 dwz_file->macro.size = bfd_section_size (sectp);
2669 }
2670 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2671 {
2672 dwz_file->gdb_index.s.section = sectp;
2673 dwz_file->gdb_index.size = bfd_section_size (sectp);
2674 }
2675 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2676 {
2677 dwz_file->debug_names.s.section = sectp;
2678 dwz_file->debug_names.size = bfd_section_size (sectp);
2679 }
2680 }
2681
2682 /* See dwarf2read.h. */
2683
2684 struct dwz_file *
2685 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2686 {
2687 const char *filename;
2688 bfd_size_type buildid_len_arg;
2689 size_t buildid_len;
2690 bfd_byte *buildid;
2691
2692 if (dwarf2_per_objfile->dwz_file != NULL)
2693 return dwarf2_per_objfile->dwz_file.get ();
2694
2695 bfd_set_error (bfd_error_no_error);
2696 gdb::unique_xmalloc_ptr<char> data
2697 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2698 &buildid_len_arg, &buildid));
2699 if (data == NULL)
2700 {
2701 if (bfd_get_error () == bfd_error_no_error)
2702 return NULL;
2703 error (_("could not read '.gnu_debugaltlink' section: %s"),
2704 bfd_errmsg (bfd_get_error ()));
2705 }
2706
2707 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2708
2709 buildid_len = (size_t) buildid_len_arg;
2710
2711 filename = data.get ();
2712
2713 std::string abs_storage;
2714 if (!IS_ABSOLUTE_PATH (filename))
2715 {
2716 gdb::unique_xmalloc_ptr<char> abs
2717 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2718
2719 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2720 filename = abs_storage.c_str ();
2721 }
2722
2723 /* First try the file name given in the section. If that doesn't
2724 work, try to use the build-id instead. */
2725 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2726 if (dwz_bfd != NULL)
2727 {
2728 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2729 dwz_bfd.reset (nullptr);
2730 }
2731
2732 if (dwz_bfd == NULL)
2733 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2734
2735 if (dwz_bfd == NULL)
2736 error (_("could not find '.gnu_debugaltlink' file for %s"),
2737 objfile_name (dwarf2_per_objfile->objfile));
2738
2739 std::unique_ptr<struct dwz_file> result
2740 (new struct dwz_file (std::move (dwz_bfd)));
2741
2742 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2743 result.get ());
2744
2745 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2746 result->dwz_bfd.get ());
2747 dwarf2_per_objfile->dwz_file = std::move (result);
2748 return dwarf2_per_objfile->dwz_file.get ();
2749 }
2750 \f
2751 /* DWARF quick_symbols_functions support. */
2752
2753 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2754 unique line tables, so we maintain a separate table of all .debug_line
2755 derived entries to support the sharing.
2756 All the quick functions need is the list of file names. We discard the
2757 line_header when we're done and don't need to record it here. */
2758 struct quick_file_names
2759 {
2760 /* The data used to construct the hash key. */
2761 struct stmt_list_hash hash;
2762
2763 /* The number of entries in file_names, real_names. */
2764 unsigned int num_file_names;
2765
2766 /* The file names from the line table, after being run through
2767 file_full_name. */
2768 const char **file_names;
2769
2770 /* The file names from the line table after being run through
2771 gdb_realpath. These are computed lazily. */
2772 const char **real_names;
2773 };
2774
2775 /* When using the index (and thus not using psymtabs), each CU has an
2776 object of this type. This is used to hold information needed by
2777 the various "quick" methods. */
2778 struct dwarf2_per_cu_quick_data
2779 {
2780 /* The file table. This can be NULL if there was no file table
2781 or it's currently not read in.
2782 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2783 struct quick_file_names *file_names;
2784
2785 /* The corresponding symbol table. This is NULL if symbols for this
2786 CU have not yet been read. */
2787 struct compunit_symtab *compunit_symtab;
2788
2789 /* A temporary mark bit used when iterating over all CUs in
2790 expand_symtabs_matching. */
2791 unsigned int mark : 1;
2792
2793 /* True if we've tried to read the file table and found there isn't one.
2794 There will be no point in trying to read it again next time. */
2795 unsigned int no_file_data : 1;
2796 };
2797
2798 /* Utility hash function for a stmt_list_hash. */
2799
2800 static hashval_t
2801 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2802 {
2803 hashval_t v = 0;
2804
2805 if (stmt_list_hash->dwo_unit != NULL)
2806 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2807 v += to_underlying (stmt_list_hash->line_sect_off);
2808 return v;
2809 }
2810
2811 /* Utility equality function for a stmt_list_hash. */
2812
2813 static int
2814 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2815 const struct stmt_list_hash *rhs)
2816 {
2817 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2818 return 0;
2819 if (lhs->dwo_unit != NULL
2820 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2821 return 0;
2822
2823 return lhs->line_sect_off == rhs->line_sect_off;
2824 }
2825
2826 /* Hash function for a quick_file_names. */
2827
2828 static hashval_t
2829 hash_file_name_entry (const void *e)
2830 {
2831 const struct quick_file_names *file_data
2832 = (const struct quick_file_names *) e;
2833
2834 return hash_stmt_list_entry (&file_data->hash);
2835 }
2836
2837 /* Equality function for a quick_file_names. */
2838
2839 static int
2840 eq_file_name_entry (const void *a, const void *b)
2841 {
2842 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2843 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2844
2845 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2846 }
2847
2848 /* Delete function for a quick_file_names. */
2849
2850 static void
2851 delete_file_name_entry (void *e)
2852 {
2853 struct quick_file_names *file_data = (struct quick_file_names *) e;
2854 int i;
2855
2856 for (i = 0; i < file_data->num_file_names; ++i)
2857 {
2858 xfree ((void*) file_data->file_names[i]);
2859 if (file_data->real_names)
2860 xfree ((void*) file_data->real_names[i]);
2861 }
2862
2863 /* The space for the struct itself lives on objfile_obstack,
2864 so we don't free it here. */
2865 }
2866
2867 /* Create a quick_file_names hash table. */
2868
2869 static htab_t
2870 create_quick_file_names_table (unsigned int nr_initial_entries)
2871 {
2872 return htab_create_alloc (nr_initial_entries,
2873 hash_file_name_entry, eq_file_name_entry,
2874 delete_file_name_entry, xcalloc, xfree);
2875 }
2876
2877 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2878 have to be created afterwards. You should call age_cached_comp_units after
2879 processing PER_CU->CU. dw2_setup must have been already called. */
2880
2881 static void
2882 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2883 {
2884 if (per_cu->is_debug_types)
2885 load_full_type_unit (per_cu);
2886 else
2887 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2888
2889 if (per_cu->cu == NULL)
2890 return; /* Dummy CU. */
2891
2892 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2893 }
2894
2895 /* Read in the symbols for PER_CU. */
2896
2897 static void
2898 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2899 {
2900 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2901
2902 /* Skip type_unit_groups, reading the type units they contain
2903 is handled elsewhere. */
2904 if (IS_TYPE_UNIT_GROUP (per_cu))
2905 return;
2906
2907 /* The destructor of dwarf2_queue_guard frees any entries left on
2908 the queue. After this point we're guaranteed to leave this function
2909 with the dwarf queue empty. */
2910 dwarf2_queue_guard q_guard;
2911
2912 if (dwarf2_per_objfile->using_index
2913 ? per_cu->v.quick->compunit_symtab == NULL
2914 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2915 {
2916 queue_comp_unit (per_cu, language_minimal);
2917 load_cu (per_cu, skip_partial);
2918
2919 /* If we just loaded a CU from a DWO, and we're working with an index
2920 that may badly handle TUs, load all the TUs in that DWO as well.
2921 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2922 if (!per_cu->is_debug_types
2923 && per_cu->cu != NULL
2924 && per_cu->cu->dwo_unit != NULL
2925 && dwarf2_per_objfile->index_table != NULL
2926 && dwarf2_per_objfile->index_table->version <= 7
2927 /* DWP files aren't supported yet. */
2928 && get_dwp_file (dwarf2_per_objfile) == NULL)
2929 queue_and_load_all_dwo_tus (per_cu);
2930 }
2931
2932 process_queue (dwarf2_per_objfile);
2933
2934 /* Age the cache, releasing compilation units that have not
2935 been used recently. */
2936 age_cached_comp_units (dwarf2_per_objfile);
2937 }
2938
2939 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2940 the objfile from which this CU came. Returns the resulting symbol
2941 table. */
2942
2943 static struct compunit_symtab *
2944 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2945 {
2946 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2947
2948 gdb_assert (dwarf2_per_objfile->using_index);
2949 if (!per_cu->v.quick->compunit_symtab)
2950 {
2951 free_cached_comp_units freer (dwarf2_per_objfile);
2952 scoped_restore decrementer = increment_reading_symtab ();
2953 dw2_do_instantiate_symtab (per_cu, skip_partial);
2954 process_cu_includes (dwarf2_per_objfile);
2955 }
2956
2957 return per_cu->v.quick->compunit_symtab;
2958 }
2959
2960 /* See declaration. */
2961
2962 dwarf2_per_cu_data *
2963 dwarf2_per_objfile::get_cutu (int index)
2964 {
2965 if (index >= this->all_comp_units.size ())
2966 {
2967 index -= this->all_comp_units.size ();
2968 gdb_assert (index < this->all_type_units.size ());
2969 return &this->all_type_units[index]->per_cu;
2970 }
2971
2972 return this->all_comp_units[index];
2973 }
2974
2975 /* See declaration. */
2976
2977 dwarf2_per_cu_data *
2978 dwarf2_per_objfile::get_cu (int index)
2979 {
2980 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2981
2982 return this->all_comp_units[index];
2983 }
2984
2985 /* See declaration. */
2986
2987 signatured_type *
2988 dwarf2_per_objfile::get_tu (int index)
2989 {
2990 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2991
2992 return this->all_type_units[index];
2993 }
2994
2995 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2996 objfile_obstack, and constructed with the specified field
2997 values. */
2998
2999 static dwarf2_per_cu_data *
3000 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3001 struct dwarf2_section_info *section,
3002 int is_dwz,
3003 sect_offset sect_off, ULONGEST length)
3004 {
3005 struct objfile *objfile = dwarf2_per_objfile->objfile;
3006 dwarf2_per_cu_data *the_cu
3007 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3008 struct dwarf2_per_cu_data);
3009 the_cu->sect_off = sect_off;
3010 the_cu->length = length;
3011 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
3012 the_cu->section = section;
3013 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3014 struct dwarf2_per_cu_quick_data);
3015 the_cu->is_dwz = is_dwz;
3016 return the_cu;
3017 }
3018
3019 /* A helper for create_cus_from_index that handles a given list of
3020 CUs. */
3021
3022 static void
3023 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3024 const gdb_byte *cu_list, offset_type n_elements,
3025 struct dwarf2_section_info *section,
3026 int is_dwz)
3027 {
3028 for (offset_type i = 0; i < n_elements; i += 2)
3029 {
3030 gdb_static_assert (sizeof (ULONGEST) >= 8);
3031
3032 sect_offset sect_off
3033 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3034 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3035 cu_list += 2 * 8;
3036
3037 dwarf2_per_cu_data *per_cu
3038 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3039 sect_off, length);
3040 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
3041 }
3042 }
3043
3044 /* Read the CU list from the mapped index, and use it to create all
3045 the CU objects for this objfile. */
3046
3047 static void
3048 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3049 const gdb_byte *cu_list, offset_type cu_list_elements,
3050 const gdb_byte *dwz_list, offset_type dwz_elements)
3051 {
3052 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3053 dwarf2_per_objfile->all_comp_units.reserve
3054 ((cu_list_elements + dwz_elements) / 2);
3055
3056 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3057 &dwarf2_per_objfile->info, 0);
3058
3059 if (dwz_elements == 0)
3060 return;
3061
3062 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3063 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3064 &dwz->info, 1);
3065 }
3066
3067 /* Create the signatured type hash table from the index. */
3068
3069 static void
3070 create_signatured_type_table_from_index
3071 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3072 struct dwarf2_section_info *section,
3073 const gdb_byte *bytes,
3074 offset_type elements)
3075 {
3076 struct objfile *objfile = dwarf2_per_objfile->objfile;
3077
3078 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3079 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
3080
3081 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3082
3083 for (offset_type i = 0; i < elements; i += 3)
3084 {
3085 struct signatured_type *sig_type;
3086 ULONGEST signature;
3087 void **slot;
3088 cu_offset type_offset_in_tu;
3089
3090 gdb_static_assert (sizeof (ULONGEST) >= 8);
3091 sect_offset sect_off
3092 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3093 type_offset_in_tu
3094 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3095 BFD_ENDIAN_LITTLE);
3096 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3097 bytes += 3 * 8;
3098
3099 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3100 struct signatured_type);
3101 sig_type->signature = signature;
3102 sig_type->type_offset_in_tu = type_offset_in_tu;
3103 sig_type->per_cu.is_debug_types = 1;
3104 sig_type->per_cu.section = section;
3105 sig_type->per_cu.sect_off = sect_off;
3106 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3107 sig_type->per_cu.v.quick
3108 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3109 struct dwarf2_per_cu_quick_data);
3110
3111 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3112 *slot = sig_type;
3113
3114 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3115 }
3116
3117 dwarf2_per_objfile->signatured_types = sig_types_hash;
3118 }
3119
3120 /* Create the signatured type hash table from .debug_names. */
3121
3122 static void
3123 create_signatured_type_table_from_debug_names
3124 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3125 const mapped_debug_names &map,
3126 struct dwarf2_section_info *section,
3127 struct dwarf2_section_info *abbrev_section)
3128 {
3129 struct objfile *objfile = dwarf2_per_objfile->objfile;
3130
3131 dwarf2_read_section (objfile, section);
3132 dwarf2_read_section (objfile, abbrev_section);
3133
3134 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3135 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
3136
3137 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3138
3139 for (uint32_t i = 0; i < map.tu_count; ++i)
3140 {
3141 struct signatured_type *sig_type;
3142 void **slot;
3143
3144 sect_offset sect_off
3145 = (sect_offset) (extract_unsigned_integer
3146 (map.tu_table_reordered + i * map.offset_size,
3147 map.offset_size,
3148 map.dwarf5_byte_order));
3149
3150 comp_unit_head cu_header;
3151 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3152 abbrev_section,
3153 section->buffer + to_underlying (sect_off),
3154 rcuh_kind::TYPE);
3155
3156 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3157 struct signatured_type);
3158 sig_type->signature = cu_header.signature;
3159 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3160 sig_type->per_cu.is_debug_types = 1;
3161 sig_type->per_cu.section = section;
3162 sig_type->per_cu.sect_off = sect_off;
3163 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3164 sig_type->per_cu.v.quick
3165 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3166 struct dwarf2_per_cu_quick_data);
3167
3168 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3169 *slot = sig_type;
3170
3171 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3172 }
3173
3174 dwarf2_per_objfile->signatured_types = sig_types_hash;
3175 }
3176
3177 /* Read the address map data from the mapped index, and use it to
3178 populate the objfile's psymtabs_addrmap. */
3179
3180 static void
3181 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3182 struct mapped_index *index)
3183 {
3184 struct objfile *objfile = dwarf2_per_objfile->objfile;
3185 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3186 const gdb_byte *iter, *end;
3187 struct addrmap *mutable_map;
3188 CORE_ADDR baseaddr;
3189
3190 auto_obstack temp_obstack;
3191
3192 mutable_map = addrmap_create_mutable (&temp_obstack);
3193
3194 iter = index->address_table.data ();
3195 end = iter + index->address_table.size ();
3196
3197 baseaddr = objfile->text_section_offset ();
3198
3199 while (iter < end)
3200 {
3201 ULONGEST hi, lo, cu_index;
3202 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3203 iter += 8;
3204 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3205 iter += 8;
3206 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3207 iter += 4;
3208
3209 if (lo > hi)
3210 {
3211 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
3212 hex_string (lo), hex_string (hi));
3213 continue;
3214 }
3215
3216 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
3217 {
3218 complaint (_(".gdb_index address table has invalid CU number %u"),
3219 (unsigned) cu_index);
3220 continue;
3221 }
3222
3223 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3224 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
3225 addrmap_set_empty (mutable_map, lo, hi - 1,
3226 dwarf2_per_objfile->get_cu (cu_index));
3227 }
3228
3229 objfile->partial_symtabs->psymtabs_addrmap
3230 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3231 }
3232
3233 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3234 populate the objfile's psymtabs_addrmap. */
3235
3236 static void
3237 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3238 struct dwarf2_section_info *section)
3239 {
3240 struct objfile *objfile = dwarf2_per_objfile->objfile;
3241 bfd *abfd = objfile->obfd;
3242 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3243 const CORE_ADDR baseaddr = objfile->text_section_offset ();
3244
3245 auto_obstack temp_obstack;
3246 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3247
3248 std::unordered_map<sect_offset,
3249 dwarf2_per_cu_data *,
3250 gdb::hash_enum<sect_offset>>
3251 debug_info_offset_to_per_cu;
3252 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3253 {
3254 const auto insertpair
3255 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3256 if (!insertpair.second)
3257 {
3258 warning (_("Section .debug_aranges in %s has duplicate "
3259 "debug_info_offset %s, ignoring .debug_aranges."),
3260 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3261 return;
3262 }
3263 }
3264
3265 dwarf2_read_section (objfile, section);
3266
3267 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3268
3269 const gdb_byte *addr = section->buffer;
3270
3271 while (addr < section->buffer + section->size)
3272 {
3273 const gdb_byte *const entry_addr = addr;
3274 unsigned int bytes_read;
3275
3276 const LONGEST entry_length = read_initial_length (abfd, addr,
3277 &bytes_read);
3278 addr += bytes_read;
3279
3280 const gdb_byte *const entry_end = addr + entry_length;
3281 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3282 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3283 if (addr + entry_length > section->buffer + section->size)
3284 {
3285 warning (_("Section .debug_aranges in %s entry at offset %s "
3286 "length %s exceeds section length %s, "
3287 "ignoring .debug_aranges."),
3288 objfile_name (objfile),
3289 plongest (entry_addr - section->buffer),
3290 plongest (bytes_read + entry_length),
3291 pulongest (section->size));
3292 return;
3293 }
3294
3295 /* The version number. */
3296 const uint16_t version = read_2_bytes (abfd, addr);
3297 addr += 2;
3298 if (version != 2)
3299 {
3300 warning (_("Section .debug_aranges in %s entry at offset %s "
3301 "has unsupported version %d, ignoring .debug_aranges."),
3302 objfile_name (objfile),
3303 plongest (entry_addr - section->buffer), version);
3304 return;
3305 }
3306
3307 const uint64_t debug_info_offset
3308 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3309 addr += offset_size;
3310 const auto per_cu_it
3311 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3312 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3313 {
3314 warning (_("Section .debug_aranges in %s entry at offset %s "
3315 "debug_info_offset %s does not exists, "
3316 "ignoring .debug_aranges."),
3317 objfile_name (objfile),
3318 plongest (entry_addr - section->buffer),
3319 pulongest (debug_info_offset));
3320 return;
3321 }
3322 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3323
3324 const uint8_t address_size = *addr++;
3325 if (address_size < 1 || address_size > 8)
3326 {
3327 warning (_("Section .debug_aranges in %s entry at offset %s "
3328 "address_size %u is invalid, ignoring .debug_aranges."),
3329 objfile_name (objfile),
3330 plongest (entry_addr - section->buffer), address_size);
3331 return;
3332 }
3333
3334 const uint8_t segment_selector_size = *addr++;
3335 if (segment_selector_size != 0)
3336 {
3337 warning (_("Section .debug_aranges in %s entry at offset %s "
3338 "segment_selector_size %u is not supported, "
3339 "ignoring .debug_aranges."),
3340 objfile_name (objfile),
3341 plongest (entry_addr - section->buffer),
3342 segment_selector_size);
3343 return;
3344 }
3345
3346 /* Must pad to an alignment boundary that is twice the address
3347 size. It is undocumented by the DWARF standard but GCC does
3348 use it. */
3349 for (size_t padding = ((-(addr - section->buffer))
3350 & (2 * address_size - 1));
3351 padding > 0; padding--)
3352 if (*addr++ != 0)
3353 {
3354 warning (_("Section .debug_aranges in %s entry at offset %s "
3355 "padding is not zero, ignoring .debug_aranges."),
3356 objfile_name (objfile),
3357 plongest (entry_addr - section->buffer));
3358 return;
3359 }
3360
3361 for (;;)
3362 {
3363 if (addr + 2 * address_size > entry_end)
3364 {
3365 warning (_("Section .debug_aranges in %s entry at offset %s "
3366 "address list is not properly terminated, "
3367 "ignoring .debug_aranges."),
3368 objfile_name (objfile),
3369 plongest (entry_addr - section->buffer));
3370 return;
3371 }
3372 ULONGEST start = extract_unsigned_integer (addr, address_size,
3373 dwarf5_byte_order);
3374 addr += address_size;
3375 ULONGEST length = extract_unsigned_integer (addr, address_size,
3376 dwarf5_byte_order);
3377 addr += address_size;
3378 if (start == 0 && length == 0)
3379 break;
3380 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3381 {
3382 /* Symbol was eliminated due to a COMDAT group. */
3383 continue;
3384 }
3385 ULONGEST end = start + length;
3386 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3387 - baseaddr);
3388 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3389 - baseaddr);
3390 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3391 }
3392 }
3393
3394 objfile->partial_symtabs->psymtabs_addrmap
3395 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3396 }
3397
3398 /* Find a slot in the mapped index INDEX for the object named NAME.
3399 If NAME is found, set *VEC_OUT to point to the CU vector in the
3400 constant pool and return true. If NAME cannot be found, return
3401 false. */
3402
3403 static bool
3404 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3405 offset_type **vec_out)
3406 {
3407 offset_type hash;
3408 offset_type slot, step;
3409 int (*cmp) (const char *, const char *);
3410
3411 gdb::unique_xmalloc_ptr<char> without_params;
3412 if (current_language->la_language == language_cplus
3413 || current_language->la_language == language_fortran
3414 || current_language->la_language == language_d)
3415 {
3416 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3417 not contain any. */
3418
3419 if (strchr (name, '(') != NULL)
3420 {
3421 without_params = cp_remove_params (name);
3422
3423 if (without_params != NULL)
3424 name = without_params.get ();
3425 }
3426 }
3427
3428 /* Index version 4 did not support case insensitive searches. But the
3429 indices for case insensitive languages are built in lowercase, therefore
3430 simulate our NAME being searched is also lowercased. */
3431 hash = mapped_index_string_hash ((index->version == 4
3432 && case_sensitivity == case_sensitive_off
3433 ? 5 : index->version),
3434 name);
3435
3436 slot = hash & (index->symbol_table.size () - 1);
3437 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3438 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3439
3440 for (;;)
3441 {
3442 const char *str;
3443
3444 const auto &bucket = index->symbol_table[slot];
3445 if (bucket.name == 0 && bucket.vec == 0)
3446 return false;
3447
3448 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3449 if (!cmp (name, str))
3450 {
3451 *vec_out = (offset_type *) (index->constant_pool
3452 + MAYBE_SWAP (bucket.vec));
3453 return true;
3454 }
3455
3456 slot = (slot + step) & (index->symbol_table.size () - 1);
3457 }
3458 }
3459
3460 /* A helper function that reads the .gdb_index from BUFFER and fills
3461 in MAP. FILENAME is the name of the file containing the data;
3462 it is used for error reporting. DEPRECATED_OK is true if it is
3463 ok to use deprecated sections.
3464
3465 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3466 out parameters that are filled in with information about the CU and
3467 TU lists in the section.
3468
3469 Returns true if all went well, false otherwise. */
3470
3471 static bool
3472 read_gdb_index_from_buffer (struct objfile *objfile,
3473 const char *filename,
3474 bool deprecated_ok,
3475 gdb::array_view<const gdb_byte> buffer,
3476 struct mapped_index *map,
3477 const gdb_byte **cu_list,
3478 offset_type *cu_list_elements,
3479 const gdb_byte **types_list,
3480 offset_type *types_list_elements)
3481 {
3482 const gdb_byte *addr = &buffer[0];
3483
3484 /* Version check. */
3485 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3486 /* Versions earlier than 3 emitted every copy of a psymbol. This
3487 causes the index to behave very poorly for certain requests. Version 3
3488 contained incomplete addrmap. So, it seems better to just ignore such
3489 indices. */
3490 if (version < 4)
3491 {
3492 static int warning_printed = 0;
3493 if (!warning_printed)
3494 {
3495 warning (_("Skipping obsolete .gdb_index section in %s."),
3496 filename);
3497 warning_printed = 1;
3498 }
3499 return 0;
3500 }
3501 /* Index version 4 uses a different hash function than index version
3502 5 and later.
3503
3504 Versions earlier than 6 did not emit psymbols for inlined
3505 functions. Using these files will cause GDB not to be able to
3506 set breakpoints on inlined functions by name, so we ignore these
3507 indices unless the user has done
3508 "set use-deprecated-index-sections on". */
3509 if (version < 6 && !deprecated_ok)
3510 {
3511 static int warning_printed = 0;
3512 if (!warning_printed)
3513 {
3514 warning (_("\
3515 Skipping deprecated .gdb_index section in %s.\n\
3516 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3517 to use the section anyway."),
3518 filename);
3519 warning_printed = 1;
3520 }
3521 return 0;
3522 }
3523 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3524 of the TU (for symbols coming from TUs),
3525 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3526 Plus gold-generated indices can have duplicate entries for global symbols,
3527 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3528 These are just performance bugs, and we can't distinguish gdb-generated
3529 indices from gold-generated ones, so issue no warning here. */
3530
3531 /* Indexes with higher version than the one supported by GDB may be no
3532 longer backward compatible. */
3533 if (version > 8)
3534 return 0;
3535
3536 map->version = version;
3537
3538 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3539
3540 int i = 0;
3541 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3542 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3543 / 8);
3544 ++i;
3545
3546 *types_list = addr + MAYBE_SWAP (metadata[i]);
3547 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3548 - MAYBE_SWAP (metadata[i]))
3549 / 8);
3550 ++i;
3551
3552 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3553 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3554 map->address_table
3555 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3556 ++i;
3557
3558 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3559 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3560 map->symbol_table
3561 = gdb::array_view<mapped_index::symbol_table_slot>
3562 ((mapped_index::symbol_table_slot *) symbol_table,
3563 (mapped_index::symbol_table_slot *) symbol_table_end);
3564
3565 ++i;
3566 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3567
3568 return 1;
3569 }
3570
3571 /* Callback types for dwarf2_read_gdb_index. */
3572
3573 typedef gdb::function_view
3574 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3575 get_gdb_index_contents_ftype;
3576 typedef gdb::function_view
3577 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3578 get_gdb_index_contents_dwz_ftype;
3579
3580 /* Read .gdb_index. If everything went ok, initialize the "quick"
3581 elements of all the CUs and return 1. Otherwise, return 0. */
3582
3583 static int
3584 dwarf2_read_gdb_index
3585 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3586 get_gdb_index_contents_ftype get_gdb_index_contents,
3587 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3588 {
3589 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3590 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3591 struct dwz_file *dwz;
3592 struct objfile *objfile = dwarf2_per_objfile->objfile;
3593
3594 gdb::array_view<const gdb_byte> main_index_contents
3595 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3596
3597 if (main_index_contents.empty ())
3598 return 0;
3599
3600 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3601 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3602 use_deprecated_index_sections,
3603 main_index_contents, map.get (), &cu_list,
3604 &cu_list_elements, &types_list,
3605 &types_list_elements))
3606 return 0;
3607
3608 /* Don't use the index if it's empty. */
3609 if (map->symbol_table.empty ())
3610 return 0;
3611
3612 /* If there is a .dwz file, read it so we can get its CU list as
3613 well. */
3614 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3615 if (dwz != NULL)
3616 {
3617 struct mapped_index dwz_map;
3618 const gdb_byte *dwz_types_ignore;
3619 offset_type dwz_types_elements_ignore;
3620
3621 gdb::array_view<const gdb_byte> dwz_index_content
3622 = get_gdb_index_contents_dwz (objfile, dwz);
3623
3624 if (dwz_index_content.empty ())
3625 return 0;
3626
3627 if (!read_gdb_index_from_buffer (objfile,
3628 bfd_get_filename (dwz->dwz_bfd.get ()),
3629 1, dwz_index_content, &dwz_map,
3630 &dwz_list, &dwz_list_elements,
3631 &dwz_types_ignore,
3632 &dwz_types_elements_ignore))
3633 {
3634 warning (_("could not read '.gdb_index' section from %s; skipping"),
3635 bfd_get_filename (dwz->dwz_bfd.get ()));
3636 return 0;
3637 }
3638 }
3639
3640 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3641 dwz_list, dwz_list_elements);
3642
3643 if (types_list_elements)
3644 {
3645 /* We can only handle a single .debug_types when we have an
3646 index. */
3647 if (dwarf2_per_objfile->types.size () != 1)
3648 return 0;
3649
3650 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3651
3652 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3653 types_list, types_list_elements);
3654 }
3655
3656 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3657
3658 dwarf2_per_objfile->index_table = std::move (map);
3659 dwarf2_per_objfile->using_index = 1;
3660 dwarf2_per_objfile->quick_file_names_table =
3661 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3662
3663 return 1;
3664 }
3665
3666 /* die_reader_func for dw2_get_file_names. */
3667
3668 static void
3669 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3670 const gdb_byte *info_ptr,
3671 struct die_info *comp_unit_die,
3672 int has_children)
3673 {
3674 struct dwarf2_cu *cu = reader->cu;
3675 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3676 struct dwarf2_per_objfile *dwarf2_per_objfile
3677 = cu->per_cu->dwarf2_per_objfile;
3678 struct objfile *objfile = dwarf2_per_objfile->objfile;
3679 struct dwarf2_per_cu_data *lh_cu;
3680 struct attribute *attr;
3681 void **slot;
3682 struct quick_file_names *qfn;
3683
3684 gdb_assert (! this_cu->is_debug_types);
3685
3686 /* Our callers never want to match partial units -- instead they
3687 will match the enclosing full CU. */
3688 if (comp_unit_die->tag == DW_TAG_partial_unit)
3689 {
3690 this_cu->v.quick->no_file_data = 1;
3691 return;
3692 }
3693
3694 lh_cu = this_cu;
3695 slot = NULL;
3696
3697 line_header_up lh;
3698 sect_offset line_offset {};
3699
3700 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3701 if (attr != nullptr)
3702 {
3703 struct quick_file_names find_entry;
3704
3705 line_offset = (sect_offset) DW_UNSND (attr);
3706
3707 /* We may have already read in this line header (TU line header sharing).
3708 If we have we're done. */
3709 find_entry.hash.dwo_unit = cu->dwo_unit;
3710 find_entry.hash.line_sect_off = line_offset;
3711 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3712 &find_entry, INSERT);
3713 if (*slot != NULL)
3714 {
3715 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3716 return;
3717 }
3718
3719 lh = dwarf_decode_line_header (line_offset, cu);
3720 }
3721 if (lh == NULL)
3722 {
3723 lh_cu->v.quick->no_file_data = 1;
3724 return;
3725 }
3726
3727 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3728 qfn->hash.dwo_unit = cu->dwo_unit;
3729 qfn->hash.line_sect_off = line_offset;
3730 gdb_assert (slot != NULL);
3731 *slot = qfn;
3732
3733 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3734
3735 int offset = 0;
3736 if (strcmp (fnd.name, "<unknown>") != 0)
3737 ++offset;
3738
3739 qfn->num_file_names = offset + lh->file_names_size ();
3740 qfn->file_names =
3741 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3742 if (offset != 0)
3743 qfn->file_names[0] = xstrdup (fnd.name);
3744 for (int i = 0; i < lh->file_names_size (); ++i)
3745 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3746 qfn->real_names = NULL;
3747
3748 lh_cu->v.quick->file_names = qfn;
3749 }
3750
3751 /* A helper for the "quick" functions which attempts to read the line
3752 table for THIS_CU. */
3753
3754 static struct quick_file_names *
3755 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3756 {
3757 /* This should never be called for TUs. */
3758 gdb_assert (! this_cu->is_debug_types);
3759 /* Nor type unit groups. */
3760 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3761
3762 if (this_cu->v.quick->file_names != NULL)
3763 return this_cu->v.quick->file_names;
3764 /* If we know there is no line data, no point in looking again. */
3765 if (this_cu->v.quick->no_file_data)
3766 return NULL;
3767
3768 cutu_reader reader (this_cu);
3769 if (!reader.dummy_p)
3770 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3771 reader.has_children);
3772
3773 if (this_cu->v.quick->no_file_data)
3774 return NULL;
3775 return this_cu->v.quick->file_names;
3776 }
3777
3778 /* A helper for the "quick" functions which computes and caches the
3779 real path for a given file name from the line table. */
3780
3781 static const char *
3782 dw2_get_real_path (struct objfile *objfile,
3783 struct quick_file_names *qfn, int index)
3784 {
3785 if (qfn->real_names == NULL)
3786 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3787 qfn->num_file_names, const char *);
3788
3789 if (qfn->real_names[index] == NULL)
3790 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3791
3792 return qfn->real_names[index];
3793 }
3794
3795 static struct symtab *
3796 dw2_find_last_source_symtab (struct objfile *objfile)
3797 {
3798 struct dwarf2_per_objfile *dwarf2_per_objfile
3799 = get_dwarf2_per_objfile (objfile);
3800 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3801 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3802
3803 if (cust == NULL)
3804 return NULL;
3805
3806 return compunit_primary_filetab (cust);
3807 }
3808
3809 /* Traversal function for dw2_forget_cached_source_info. */
3810
3811 static int
3812 dw2_free_cached_file_names (void **slot, void *info)
3813 {
3814 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3815
3816 if (file_data->real_names)
3817 {
3818 int i;
3819
3820 for (i = 0; i < file_data->num_file_names; ++i)
3821 {
3822 xfree ((void*) file_data->real_names[i]);
3823 file_data->real_names[i] = NULL;
3824 }
3825 }
3826
3827 return 1;
3828 }
3829
3830 static void
3831 dw2_forget_cached_source_info (struct objfile *objfile)
3832 {
3833 struct dwarf2_per_objfile *dwarf2_per_objfile
3834 = get_dwarf2_per_objfile (objfile);
3835
3836 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3837 dw2_free_cached_file_names, NULL);
3838 }
3839
3840 /* Helper function for dw2_map_symtabs_matching_filename that expands
3841 the symtabs and calls the iterator. */
3842
3843 static int
3844 dw2_map_expand_apply (struct objfile *objfile,
3845 struct dwarf2_per_cu_data *per_cu,
3846 const char *name, const char *real_path,
3847 gdb::function_view<bool (symtab *)> callback)
3848 {
3849 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3850
3851 /* Don't visit already-expanded CUs. */
3852 if (per_cu->v.quick->compunit_symtab)
3853 return 0;
3854
3855 /* This may expand more than one symtab, and we want to iterate over
3856 all of them. */
3857 dw2_instantiate_symtab (per_cu, false);
3858
3859 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3860 last_made, callback);
3861 }
3862
3863 /* Implementation of the map_symtabs_matching_filename method. */
3864
3865 static bool
3866 dw2_map_symtabs_matching_filename
3867 (struct objfile *objfile, const char *name, const char *real_path,
3868 gdb::function_view<bool (symtab *)> callback)
3869 {
3870 const char *name_basename = lbasename (name);
3871 struct dwarf2_per_objfile *dwarf2_per_objfile
3872 = get_dwarf2_per_objfile (objfile);
3873
3874 /* The rule is CUs specify all the files, including those used by
3875 any TU, so there's no need to scan TUs here. */
3876
3877 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3878 {
3879 /* We only need to look at symtabs not already expanded. */
3880 if (per_cu->v.quick->compunit_symtab)
3881 continue;
3882
3883 quick_file_names *file_data = dw2_get_file_names (per_cu);
3884 if (file_data == NULL)
3885 continue;
3886
3887 for (int j = 0; j < file_data->num_file_names; ++j)
3888 {
3889 const char *this_name = file_data->file_names[j];
3890 const char *this_real_name;
3891
3892 if (compare_filenames_for_search (this_name, name))
3893 {
3894 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3895 callback))
3896 return true;
3897 continue;
3898 }
3899
3900 /* Before we invoke realpath, which can get expensive when many
3901 files are involved, do a quick comparison of the basenames. */
3902 if (! basenames_may_differ
3903 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3904 continue;
3905
3906 this_real_name = dw2_get_real_path (objfile, file_data, j);
3907 if (compare_filenames_for_search (this_real_name, name))
3908 {
3909 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3910 callback))
3911 return true;
3912 continue;
3913 }
3914
3915 if (real_path != NULL)
3916 {
3917 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3918 gdb_assert (IS_ABSOLUTE_PATH (name));
3919 if (this_real_name != NULL
3920 && FILENAME_CMP (real_path, this_real_name) == 0)
3921 {
3922 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3923 callback))
3924 return true;
3925 continue;
3926 }
3927 }
3928 }
3929 }
3930
3931 return false;
3932 }
3933
3934 /* Struct used to manage iterating over all CUs looking for a symbol. */
3935
3936 struct dw2_symtab_iterator
3937 {
3938 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3939 struct dwarf2_per_objfile *dwarf2_per_objfile;
3940 /* If set, only look for symbols that match that block. Valid values are
3941 GLOBAL_BLOCK and STATIC_BLOCK. */
3942 gdb::optional<block_enum> block_index;
3943 /* The kind of symbol we're looking for. */
3944 domain_enum domain;
3945 /* The list of CUs from the index entry of the symbol,
3946 or NULL if not found. */
3947 offset_type *vec;
3948 /* The next element in VEC to look at. */
3949 int next;
3950 /* The number of elements in VEC, or zero if there is no match. */
3951 int length;
3952 /* Have we seen a global version of the symbol?
3953 If so we can ignore all further global instances.
3954 This is to work around gold/15646, inefficient gold-generated
3955 indices. */
3956 int global_seen;
3957 };
3958
3959 /* Initialize the index symtab iterator ITER. */
3960
3961 static void
3962 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3963 struct dwarf2_per_objfile *dwarf2_per_objfile,
3964 gdb::optional<block_enum> block_index,
3965 domain_enum domain,
3966 const char *name)
3967 {
3968 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3969 iter->block_index = block_index;
3970 iter->domain = domain;
3971 iter->next = 0;
3972 iter->global_seen = 0;
3973
3974 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3975
3976 /* index is NULL if OBJF_READNOW. */
3977 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3978 iter->length = MAYBE_SWAP (*iter->vec);
3979 else
3980 {
3981 iter->vec = NULL;
3982 iter->length = 0;
3983 }
3984 }
3985
3986 /* Return the next matching CU or NULL if there are no more. */
3987
3988 static struct dwarf2_per_cu_data *
3989 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3990 {
3991 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3992
3993 for ( ; iter->next < iter->length; ++iter->next)
3994 {
3995 offset_type cu_index_and_attrs =
3996 MAYBE_SWAP (iter->vec[iter->next + 1]);
3997 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3998 gdb_index_symbol_kind symbol_kind =
3999 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4000 /* Only check the symbol attributes if they're present.
4001 Indices prior to version 7 don't record them,
4002 and indices >= 7 may elide them for certain symbols
4003 (gold does this). */
4004 int attrs_valid =
4005 (dwarf2_per_objfile->index_table->version >= 7
4006 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4007
4008 /* Don't crash on bad data. */
4009 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4010 + dwarf2_per_objfile->all_type_units.size ()))
4011 {
4012 complaint (_(".gdb_index entry has bad CU index"
4013 " [in module %s]"),
4014 objfile_name (dwarf2_per_objfile->objfile));
4015 continue;
4016 }
4017
4018 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4019
4020 /* Skip if already read in. */
4021 if (per_cu->v.quick->compunit_symtab)
4022 continue;
4023
4024 /* Check static vs global. */
4025 if (attrs_valid)
4026 {
4027 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4028
4029 if (iter->block_index.has_value ())
4030 {
4031 bool want_static = *iter->block_index == STATIC_BLOCK;
4032
4033 if (is_static != want_static)
4034 continue;
4035 }
4036
4037 /* Work around gold/15646. */
4038 if (!is_static && iter->global_seen)
4039 continue;
4040 if (!is_static)
4041 iter->global_seen = 1;
4042 }
4043
4044 /* Only check the symbol's kind if it has one. */
4045 if (attrs_valid)
4046 {
4047 switch (iter->domain)
4048 {
4049 case VAR_DOMAIN:
4050 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4051 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4052 /* Some types are also in VAR_DOMAIN. */
4053 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4054 continue;
4055 break;
4056 case STRUCT_DOMAIN:
4057 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4058 continue;
4059 break;
4060 case LABEL_DOMAIN:
4061 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4062 continue;
4063 break;
4064 case MODULE_DOMAIN:
4065 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4066 continue;
4067 break;
4068 default:
4069 break;
4070 }
4071 }
4072
4073 ++iter->next;
4074 return per_cu;
4075 }
4076
4077 return NULL;
4078 }
4079
4080 static struct compunit_symtab *
4081 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
4082 const char *name, domain_enum domain)
4083 {
4084 struct compunit_symtab *stab_best = NULL;
4085 struct dwarf2_per_objfile *dwarf2_per_objfile
4086 = get_dwarf2_per_objfile (objfile);
4087
4088 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4089
4090 struct dw2_symtab_iterator iter;
4091 struct dwarf2_per_cu_data *per_cu;
4092
4093 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
4094
4095 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4096 {
4097 struct symbol *sym, *with_opaque = NULL;
4098 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
4099 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4100 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4101
4102 sym = block_find_symbol (block, name, domain,
4103 block_find_non_opaque_type_preferred,
4104 &with_opaque);
4105
4106 /* Some caution must be observed with overloaded functions
4107 and methods, since the index will not contain any overload
4108 information (but NAME might contain it). */
4109
4110 if (sym != NULL
4111 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4112 return stab;
4113 if (with_opaque != NULL
4114 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4115 stab_best = stab;
4116
4117 /* Keep looking through other CUs. */
4118 }
4119
4120 return stab_best;
4121 }
4122
4123 static void
4124 dw2_print_stats (struct objfile *objfile)
4125 {
4126 struct dwarf2_per_objfile *dwarf2_per_objfile
4127 = get_dwarf2_per_objfile (objfile);
4128 int total = (dwarf2_per_objfile->all_comp_units.size ()
4129 + dwarf2_per_objfile->all_type_units.size ());
4130 int count = 0;
4131
4132 for (int i = 0; i < total; ++i)
4133 {
4134 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4135
4136 if (!per_cu->v.quick->compunit_symtab)
4137 ++count;
4138 }
4139 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4140 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4141 }
4142
4143 /* This dumps minimal information about the index.
4144 It is called via "mt print objfiles".
4145 One use is to verify .gdb_index has been loaded by the
4146 gdb.dwarf2/gdb-index.exp testcase. */
4147
4148 static void
4149 dw2_dump (struct objfile *objfile)
4150 {
4151 struct dwarf2_per_objfile *dwarf2_per_objfile
4152 = get_dwarf2_per_objfile (objfile);
4153
4154 gdb_assert (dwarf2_per_objfile->using_index);
4155 printf_filtered (".gdb_index:");
4156 if (dwarf2_per_objfile->index_table != NULL)
4157 {
4158 printf_filtered (" version %d\n",
4159 dwarf2_per_objfile->index_table->version);
4160 }
4161 else
4162 printf_filtered (" faked for \"readnow\"\n");
4163 printf_filtered ("\n");
4164 }
4165
4166 static void
4167 dw2_expand_symtabs_for_function (struct objfile *objfile,
4168 const char *func_name)
4169 {
4170 struct dwarf2_per_objfile *dwarf2_per_objfile
4171 = get_dwarf2_per_objfile (objfile);
4172
4173 struct dw2_symtab_iterator iter;
4174 struct dwarf2_per_cu_data *per_cu;
4175
4176 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
4177
4178 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4179 dw2_instantiate_symtab (per_cu, false);
4180
4181 }
4182
4183 static void
4184 dw2_expand_all_symtabs (struct objfile *objfile)
4185 {
4186 struct dwarf2_per_objfile *dwarf2_per_objfile
4187 = get_dwarf2_per_objfile (objfile);
4188 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4189 + dwarf2_per_objfile->all_type_units.size ());
4190
4191 for (int i = 0; i < total_units; ++i)
4192 {
4193 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4194
4195 /* We don't want to directly expand a partial CU, because if we
4196 read it with the wrong language, then assertion failures can
4197 be triggered later on. See PR symtab/23010. So, tell
4198 dw2_instantiate_symtab to skip partial CUs -- any important
4199 partial CU will be read via DW_TAG_imported_unit anyway. */
4200 dw2_instantiate_symtab (per_cu, true);
4201 }
4202 }
4203
4204 static void
4205 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4206 const char *fullname)
4207 {
4208 struct dwarf2_per_objfile *dwarf2_per_objfile
4209 = get_dwarf2_per_objfile (objfile);
4210
4211 /* We don't need to consider type units here.
4212 This is only called for examining code, e.g. expand_line_sal.
4213 There can be an order of magnitude (or more) more type units
4214 than comp units, and we avoid them if we can. */
4215
4216 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4217 {
4218 /* We only need to look at symtabs not already expanded. */
4219 if (per_cu->v.quick->compunit_symtab)
4220 continue;
4221
4222 quick_file_names *file_data = dw2_get_file_names (per_cu);
4223 if (file_data == NULL)
4224 continue;
4225
4226 for (int j = 0; j < file_data->num_file_names; ++j)
4227 {
4228 const char *this_fullname = file_data->file_names[j];
4229
4230 if (filename_cmp (this_fullname, fullname) == 0)
4231 {
4232 dw2_instantiate_symtab (per_cu, false);
4233 break;
4234 }
4235 }
4236 }
4237 }
4238
4239 static void
4240 dw2_map_matching_symbols
4241 (struct objfile *objfile,
4242 const lookup_name_info &name, domain_enum domain,
4243 int global,
4244 gdb::function_view<symbol_found_callback_ftype> callback,
4245 symbol_compare_ftype *ordered_compare)
4246 {
4247 /* Currently unimplemented; used for Ada. The function can be called if the
4248 current language is Ada for a non-Ada objfile using GNU index. As Ada
4249 does not look for non-Ada symbols this function should just return. */
4250 }
4251
4252 /* Starting from a search name, return the string that finds the upper
4253 bound of all strings that start with SEARCH_NAME in a sorted name
4254 list. Returns the empty string to indicate that the upper bound is
4255 the end of the list. */
4256
4257 static std::string
4258 make_sort_after_prefix_name (const char *search_name)
4259 {
4260 /* When looking to complete "func", we find the upper bound of all
4261 symbols that start with "func" by looking for where we'd insert
4262 the closest string that would follow "func" in lexicographical
4263 order. Usually, that's "func"-with-last-character-incremented,
4264 i.e. "fund". Mind non-ASCII characters, though. Usually those
4265 will be UTF-8 multi-byte sequences, but we can't be certain.
4266 Especially mind the 0xff character, which is a valid character in
4267 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4268 rule out compilers allowing it in identifiers. Note that
4269 conveniently, strcmp/strcasecmp are specified to compare
4270 characters interpreted as unsigned char. So what we do is treat
4271 the whole string as a base 256 number composed of a sequence of
4272 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4273 to 0, and carries 1 to the following more-significant position.
4274 If the very first character in SEARCH_NAME ends up incremented
4275 and carries/overflows, then the upper bound is the end of the
4276 list. The string after the empty string is also the empty
4277 string.
4278
4279 Some examples of this operation:
4280
4281 SEARCH_NAME => "+1" RESULT
4282
4283 "abc" => "abd"
4284 "ab\xff" => "ac"
4285 "\xff" "a" "\xff" => "\xff" "b"
4286 "\xff" => ""
4287 "\xff\xff" => ""
4288 "" => ""
4289
4290 Then, with these symbols for example:
4291
4292 func
4293 func1
4294 fund
4295
4296 completing "func" looks for symbols between "func" and
4297 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4298 which finds "func" and "func1", but not "fund".
4299
4300 And with:
4301
4302 funcÿ (Latin1 'ÿ' [0xff])
4303 funcÿ1
4304 fund
4305
4306 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4307 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4308
4309 And with:
4310
4311 ÿÿ (Latin1 'ÿ' [0xff])
4312 ÿÿ1
4313
4314 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4315 the end of the list.
4316 */
4317 std::string after = search_name;
4318 while (!after.empty () && (unsigned char) after.back () == 0xff)
4319 after.pop_back ();
4320 if (!after.empty ())
4321 after.back () = (unsigned char) after.back () + 1;
4322 return after;
4323 }
4324
4325 /* See declaration. */
4326
4327 std::pair<std::vector<name_component>::const_iterator,
4328 std::vector<name_component>::const_iterator>
4329 mapped_index_base::find_name_components_bounds
4330 (const lookup_name_info &lookup_name_without_params, language lang) const
4331 {
4332 auto *name_cmp
4333 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4334
4335 const char *lang_name
4336 = lookup_name_without_params.language_lookup_name (lang).c_str ();
4337
4338 /* Comparison function object for lower_bound that matches against a
4339 given symbol name. */
4340 auto lookup_compare_lower = [&] (const name_component &elem,
4341 const char *name)
4342 {
4343 const char *elem_qualified = this->symbol_name_at (elem.idx);
4344 const char *elem_name = elem_qualified + elem.name_offset;
4345 return name_cmp (elem_name, name) < 0;
4346 };
4347
4348 /* Comparison function object for upper_bound that matches against a
4349 given symbol name. */
4350 auto lookup_compare_upper = [&] (const char *name,
4351 const name_component &elem)
4352 {
4353 const char *elem_qualified = this->symbol_name_at (elem.idx);
4354 const char *elem_name = elem_qualified + elem.name_offset;
4355 return name_cmp (name, elem_name) < 0;
4356 };
4357
4358 auto begin = this->name_components.begin ();
4359 auto end = this->name_components.end ();
4360
4361 /* Find the lower bound. */
4362 auto lower = [&] ()
4363 {
4364 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4365 return begin;
4366 else
4367 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4368 } ();
4369
4370 /* Find the upper bound. */
4371 auto upper = [&] ()
4372 {
4373 if (lookup_name_without_params.completion_mode ())
4374 {
4375 /* In completion mode, we want UPPER to point past all
4376 symbols names that have the same prefix. I.e., with
4377 these symbols, and completing "func":
4378
4379 function << lower bound
4380 function1
4381 other_function << upper bound
4382
4383 We find the upper bound by looking for the insertion
4384 point of "func"-with-last-character-incremented,
4385 i.e. "fund". */
4386 std::string after = make_sort_after_prefix_name (lang_name);
4387 if (after.empty ())
4388 return end;
4389 return std::lower_bound (lower, end, after.c_str (),
4390 lookup_compare_lower);
4391 }
4392 else
4393 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4394 } ();
4395
4396 return {lower, upper};
4397 }
4398
4399 /* See declaration. */
4400
4401 void
4402 mapped_index_base::build_name_components ()
4403 {
4404 if (!this->name_components.empty ())
4405 return;
4406
4407 this->name_components_casing = case_sensitivity;
4408 auto *name_cmp
4409 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4410
4411 /* The code below only knows how to break apart components of C++
4412 symbol names (and other languages that use '::' as
4413 namespace/module separator) and Ada symbol names. */
4414 auto count = this->symbol_name_count ();
4415 for (offset_type idx = 0; idx < count; idx++)
4416 {
4417 if (this->symbol_name_slot_invalid (idx))
4418 continue;
4419
4420 const char *name = this->symbol_name_at (idx);
4421
4422 /* Add each name component to the name component table. */
4423 unsigned int previous_len = 0;
4424
4425 if (strstr (name, "::") != nullptr)
4426 {
4427 for (unsigned int current_len = cp_find_first_component (name);
4428 name[current_len] != '\0';
4429 current_len += cp_find_first_component (name + current_len))
4430 {
4431 gdb_assert (name[current_len] == ':');
4432 this->name_components.push_back ({previous_len, idx});
4433 /* Skip the '::'. */
4434 current_len += 2;
4435 previous_len = current_len;
4436 }
4437 }
4438 else
4439 {
4440 /* Handle the Ada encoded (aka mangled) form here. */
4441 for (const char *iter = strstr (name, "__");
4442 iter != nullptr;
4443 iter = strstr (iter, "__"))
4444 {
4445 this->name_components.push_back ({previous_len, idx});
4446 iter += 2;
4447 previous_len = iter - name;
4448 }
4449 }
4450
4451 this->name_components.push_back ({previous_len, idx});
4452 }
4453
4454 /* Sort name_components elements by name. */
4455 auto name_comp_compare = [&] (const name_component &left,
4456 const name_component &right)
4457 {
4458 const char *left_qualified = this->symbol_name_at (left.idx);
4459 const char *right_qualified = this->symbol_name_at (right.idx);
4460
4461 const char *left_name = left_qualified + left.name_offset;
4462 const char *right_name = right_qualified + right.name_offset;
4463
4464 return name_cmp (left_name, right_name) < 0;
4465 };
4466
4467 std::sort (this->name_components.begin (),
4468 this->name_components.end (),
4469 name_comp_compare);
4470 }
4471
4472 /* Helper for dw2_expand_symtabs_matching that works with a
4473 mapped_index_base instead of the containing objfile. This is split
4474 to a separate function in order to be able to unit test the
4475 name_components matching using a mock mapped_index_base. For each
4476 symbol name that matches, calls MATCH_CALLBACK, passing it the
4477 symbol's index in the mapped_index_base symbol table. */
4478
4479 static void
4480 dw2_expand_symtabs_matching_symbol
4481 (mapped_index_base &index,
4482 const lookup_name_info &lookup_name_in,
4483 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4484 enum search_domain kind,
4485 gdb::function_view<bool (offset_type)> match_callback)
4486 {
4487 lookup_name_info lookup_name_without_params
4488 = lookup_name_in.make_ignore_params ();
4489
4490 /* Build the symbol name component sorted vector, if we haven't
4491 yet. */
4492 index.build_name_components ();
4493
4494 /* The same symbol may appear more than once in the range though.
4495 E.g., if we're looking for symbols that complete "w", and we have
4496 a symbol named "w1::w2", we'll find the two name components for
4497 that same symbol in the range. To be sure we only call the
4498 callback once per symbol, we first collect the symbol name
4499 indexes that matched in a temporary vector and ignore
4500 duplicates. */
4501 std::vector<offset_type> matches;
4502
4503 struct name_and_matcher
4504 {
4505 symbol_name_matcher_ftype *matcher;
4506 const std::string &name;
4507
4508 bool operator== (const name_and_matcher &other) const
4509 {
4510 return matcher == other.matcher && name == other.name;
4511 }
4512 };
4513
4514 /* A vector holding all the different symbol name matchers, for all
4515 languages. */
4516 std::vector<name_and_matcher> matchers;
4517
4518 for (int i = 0; i < nr_languages; i++)
4519 {
4520 enum language lang_e = (enum language) i;
4521
4522 const language_defn *lang = language_def (lang_e);
4523 symbol_name_matcher_ftype *name_matcher
4524 = get_symbol_name_matcher (lang, lookup_name_without_params);
4525
4526 name_and_matcher key {
4527 name_matcher,
4528 lookup_name_without_params.language_lookup_name (lang_e)
4529 };
4530
4531 /* Don't insert the same comparison routine more than once.
4532 Note that we do this linear walk. This is not a problem in
4533 practice because the number of supported languages is
4534 low. */
4535 if (std::find (matchers.begin (), matchers.end (), key)
4536 != matchers.end ())
4537 continue;
4538 matchers.push_back (std::move (key));
4539
4540 auto bounds
4541 = index.find_name_components_bounds (lookup_name_without_params,
4542 lang_e);
4543
4544 /* Now for each symbol name in range, check to see if we have a name
4545 match, and if so, call the MATCH_CALLBACK callback. */
4546
4547 for (; bounds.first != bounds.second; ++bounds.first)
4548 {
4549 const char *qualified = index.symbol_name_at (bounds.first->idx);
4550
4551 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4552 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4553 continue;
4554
4555 matches.push_back (bounds.first->idx);
4556 }
4557 }
4558
4559 std::sort (matches.begin (), matches.end ());
4560
4561 /* Finally call the callback, once per match. */
4562 ULONGEST prev = -1;
4563 for (offset_type idx : matches)
4564 {
4565 if (prev != idx)
4566 {
4567 if (!match_callback (idx))
4568 break;
4569 prev = idx;
4570 }
4571 }
4572
4573 /* Above we use a type wider than idx's for 'prev', since 0 and
4574 (offset_type)-1 are both possible values. */
4575 static_assert (sizeof (prev) > sizeof (offset_type), "");
4576 }
4577
4578 #if GDB_SELF_TEST
4579
4580 namespace selftests { namespace dw2_expand_symtabs_matching {
4581
4582 /* A mock .gdb_index/.debug_names-like name index table, enough to
4583 exercise dw2_expand_symtabs_matching_symbol, which works with the
4584 mapped_index_base interface. Builds an index from the symbol list
4585 passed as parameter to the constructor. */
4586 class mock_mapped_index : public mapped_index_base
4587 {
4588 public:
4589 mock_mapped_index (gdb::array_view<const char *> symbols)
4590 : m_symbol_table (symbols)
4591 {}
4592
4593 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4594
4595 /* Return the number of names in the symbol table. */
4596 size_t symbol_name_count () const override
4597 {
4598 return m_symbol_table.size ();
4599 }
4600
4601 /* Get the name of the symbol at IDX in the symbol table. */
4602 const char *symbol_name_at (offset_type idx) const override
4603 {
4604 return m_symbol_table[idx];
4605 }
4606
4607 private:
4608 gdb::array_view<const char *> m_symbol_table;
4609 };
4610
4611 /* Convenience function that converts a NULL pointer to a "<null>"
4612 string, to pass to print routines. */
4613
4614 static const char *
4615 string_or_null (const char *str)
4616 {
4617 return str != NULL ? str : "<null>";
4618 }
4619
4620 /* Check if a lookup_name_info built from
4621 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4622 index. EXPECTED_LIST is the list of expected matches, in expected
4623 matching order. If no match expected, then an empty list is
4624 specified. Returns true on success. On failure prints a warning
4625 indicating the file:line that failed, and returns false. */
4626
4627 static bool
4628 check_match (const char *file, int line,
4629 mock_mapped_index &mock_index,
4630 const char *name, symbol_name_match_type match_type,
4631 bool completion_mode,
4632 std::initializer_list<const char *> expected_list)
4633 {
4634 lookup_name_info lookup_name (name, match_type, completion_mode);
4635
4636 bool matched = true;
4637
4638 auto mismatch = [&] (const char *expected_str,
4639 const char *got)
4640 {
4641 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4642 "expected=\"%s\", got=\"%s\"\n"),
4643 file, line,
4644 (match_type == symbol_name_match_type::FULL
4645 ? "FULL" : "WILD"),
4646 name, string_or_null (expected_str), string_or_null (got));
4647 matched = false;
4648 };
4649
4650 auto expected_it = expected_list.begin ();
4651 auto expected_end = expected_list.end ();
4652
4653 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4654 NULL, ALL_DOMAIN,
4655 [&] (offset_type idx)
4656 {
4657 const char *matched_name = mock_index.symbol_name_at (idx);
4658 const char *expected_str
4659 = expected_it == expected_end ? NULL : *expected_it++;
4660
4661 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4662 mismatch (expected_str, matched_name);
4663 return true;
4664 });
4665
4666 const char *expected_str
4667 = expected_it == expected_end ? NULL : *expected_it++;
4668 if (expected_str != NULL)
4669 mismatch (expected_str, NULL);
4670
4671 return matched;
4672 }
4673
4674 /* The symbols added to the mock mapped_index for testing (in
4675 canonical form). */
4676 static const char *test_symbols[] = {
4677 "function",
4678 "std::bar",
4679 "std::zfunction",
4680 "std::zfunction2",
4681 "w1::w2",
4682 "ns::foo<char*>",
4683 "ns::foo<int>",
4684 "ns::foo<long>",
4685 "ns2::tmpl<int>::foo2",
4686 "(anonymous namespace)::A::B::C",
4687
4688 /* These are used to check that the increment-last-char in the
4689 matching algorithm for completion doesn't match "t1_fund" when
4690 completing "t1_func". */
4691 "t1_func",
4692 "t1_func1",
4693 "t1_fund",
4694 "t1_fund1",
4695
4696 /* A UTF-8 name with multi-byte sequences to make sure that
4697 cp-name-parser understands this as a single identifier ("função"
4698 is "function" in PT). */
4699 u8"u8função",
4700
4701 /* \377 (0xff) is Latin1 'ÿ'. */
4702 "yfunc\377",
4703
4704 /* \377 (0xff) is Latin1 'ÿ'. */
4705 "\377",
4706 "\377\377123",
4707
4708 /* A name with all sorts of complications. Starts with "z" to make
4709 it easier for the completion tests below. */
4710 #define Z_SYM_NAME \
4711 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4712 "::tuple<(anonymous namespace)::ui*, " \
4713 "std::default_delete<(anonymous namespace)::ui>, void>"
4714
4715 Z_SYM_NAME
4716 };
4717
4718 /* Returns true if the mapped_index_base::find_name_component_bounds
4719 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4720 in completion mode. */
4721
4722 static bool
4723 check_find_bounds_finds (mapped_index_base &index,
4724 const char *search_name,
4725 gdb::array_view<const char *> expected_syms)
4726 {
4727 lookup_name_info lookup_name (search_name,
4728 symbol_name_match_type::FULL, true);
4729
4730 auto bounds = index.find_name_components_bounds (lookup_name,
4731 language_cplus);
4732
4733 size_t distance = std::distance (bounds.first, bounds.second);
4734 if (distance != expected_syms.size ())
4735 return false;
4736
4737 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4738 {
4739 auto nc_elem = bounds.first + exp_elem;
4740 const char *qualified = index.symbol_name_at (nc_elem->idx);
4741 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4742 return false;
4743 }
4744
4745 return true;
4746 }
4747
4748 /* Test the lower-level mapped_index::find_name_component_bounds
4749 method. */
4750
4751 static void
4752 test_mapped_index_find_name_component_bounds ()
4753 {
4754 mock_mapped_index mock_index (test_symbols);
4755
4756 mock_index.build_name_components ();
4757
4758 /* Test the lower-level mapped_index::find_name_component_bounds
4759 method in completion mode. */
4760 {
4761 static const char *expected_syms[] = {
4762 "t1_func",
4763 "t1_func1",
4764 };
4765
4766 SELF_CHECK (check_find_bounds_finds (mock_index,
4767 "t1_func", expected_syms));
4768 }
4769
4770 /* Check that the increment-last-char in the name matching algorithm
4771 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4772 {
4773 static const char *expected_syms1[] = {
4774 "\377",
4775 "\377\377123",
4776 };
4777 SELF_CHECK (check_find_bounds_finds (mock_index,
4778 "\377", expected_syms1));
4779
4780 static const char *expected_syms2[] = {
4781 "\377\377123",
4782 };
4783 SELF_CHECK (check_find_bounds_finds (mock_index,
4784 "\377\377", expected_syms2));
4785 }
4786 }
4787
4788 /* Test dw2_expand_symtabs_matching_symbol. */
4789
4790 static void
4791 test_dw2_expand_symtabs_matching_symbol ()
4792 {
4793 mock_mapped_index mock_index (test_symbols);
4794
4795 /* We let all tests run until the end even if some fails, for debug
4796 convenience. */
4797 bool any_mismatch = false;
4798
4799 /* Create the expected symbols list (an initializer_list). Needed
4800 because lists have commas, and we need to pass them to CHECK,
4801 which is a macro. */
4802 #define EXPECT(...) { __VA_ARGS__ }
4803
4804 /* Wrapper for check_match that passes down the current
4805 __FILE__/__LINE__. */
4806 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4807 any_mismatch |= !check_match (__FILE__, __LINE__, \
4808 mock_index, \
4809 NAME, MATCH_TYPE, COMPLETION_MODE, \
4810 EXPECTED_LIST)
4811
4812 /* Identity checks. */
4813 for (const char *sym : test_symbols)
4814 {
4815 /* Should be able to match all existing symbols. */
4816 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4817 EXPECT (sym));
4818
4819 /* Should be able to match all existing symbols with
4820 parameters. */
4821 std::string with_params = std::string (sym) + "(int)";
4822 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4823 EXPECT (sym));
4824
4825 /* Should be able to match all existing symbols with
4826 parameters and qualifiers. */
4827 with_params = std::string (sym) + " ( int ) const";
4828 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4829 EXPECT (sym));
4830
4831 /* This should really find sym, but cp-name-parser.y doesn't
4832 know about lvalue/rvalue qualifiers yet. */
4833 with_params = std::string (sym) + " ( int ) &&";
4834 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4835 {});
4836 }
4837
4838 /* Check that the name matching algorithm for completion doesn't get
4839 confused with Latin1 'ÿ' / 0xff. */
4840 {
4841 static const char str[] = "\377";
4842 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4843 EXPECT ("\377", "\377\377123"));
4844 }
4845
4846 /* Check that the increment-last-char in the matching algorithm for
4847 completion doesn't match "t1_fund" when completing "t1_func". */
4848 {
4849 static const char str[] = "t1_func";
4850 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4851 EXPECT ("t1_func", "t1_func1"));
4852 }
4853
4854 /* Check that completion mode works at each prefix of the expected
4855 symbol name. */
4856 {
4857 static const char str[] = "function(int)";
4858 size_t len = strlen (str);
4859 std::string lookup;
4860
4861 for (size_t i = 1; i < len; i++)
4862 {
4863 lookup.assign (str, i);
4864 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4865 EXPECT ("function"));
4866 }
4867 }
4868
4869 /* While "w" is a prefix of both components, the match function
4870 should still only be called once. */
4871 {
4872 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4873 EXPECT ("w1::w2"));
4874 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4875 EXPECT ("w1::w2"));
4876 }
4877
4878 /* Same, with a "complicated" symbol. */
4879 {
4880 static const char str[] = Z_SYM_NAME;
4881 size_t len = strlen (str);
4882 std::string lookup;
4883
4884 for (size_t i = 1; i < len; i++)
4885 {
4886 lookup.assign (str, i);
4887 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4888 EXPECT (Z_SYM_NAME));
4889 }
4890 }
4891
4892 /* In FULL mode, an incomplete symbol doesn't match. */
4893 {
4894 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4895 {});
4896 }
4897
4898 /* A complete symbol with parameters matches any overload, since the
4899 index has no overload info. */
4900 {
4901 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4902 EXPECT ("std::zfunction", "std::zfunction2"));
4903 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4904 EXPECT ("std::zfunction", "std::zfunction2"));
4905 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4906 EXPECT ("std::zfunction", "std::zfunction2"));
4907 }
4908
4909 /* Check that whitespace is ignored appropriately. A symbol with a
4910 template argument list. */
4911 {
4912 static const char expected[] = "ns::foo<int>";
4913 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4914 EXPECT (expected));
4915 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4916 EXPECT (expected));
4917 }
4918
4919 /* Check that whitespace is ignored appropriately. A symbol with a
4920 template argument list that includes a pointer. */
4921 {
4922 static const char expected[] = "ns::foo<char*>";
4923 /* Try both completion and non-completion modes. */
4924 static const bool completion_mode[2] = {false, true};
4925 for (size_t i = 0; i < 2; i++)
4926 {
4927 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4928 completion_mode[i], EXPECT (expected));
4929 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4930 completion_mode[i], EXPECT (expected));
4931
4932 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4933 completion_mode[i], EXPECT (expected));
4934 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4935 completion_mode[i], EXPECT (expected));
4936 }
4937 }
4938
4939 {
4940 /* Check method qualifiers are ignored. */
4941 static const char expected[] = "ns::foo<char*>";
4942 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4943 symbol_name_match_type::FULL, true, EXPECT (expected));
4944 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4945 symbol_name_match_type::FULL, true, EXPECT (expected));
4946 CHECK_MATCH ("foo < char * > ( int ) const",
4947 symbol_name_match_type::WILD, true, EXPECT (expected));
4948 CHECK_MATCH ("foo < char * > ( int ) &&",
4949 symbol_name_match_type::WILD, true, EXPECT (expected));
4950 }
4951
4952 /* Test lookup names that don't match anything. */
4953 {
4954 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4955 {});
4956
4957 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4958 {});
4959 }
4960
4961 /* Some wild matching tests, exercising "(anonymous namespace)",
4962 which should not be confused with a parameter list. */
4963 {
4964 static const char *syms[] = {
4965 "A::B::C",
4966 "B::C",
4967 "C",
4968 "A :: B :: C ( int )",
4969 "B :: C ( int )",
4970 "C ( int )",
4971 };
4972
4973 for (const char *s : syms)
4974 {
4975 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4976 EXPECT ("(anonymous namespace)::A::B::C"));
4977 }
4978 }
4979
4980 {
4981 static const char expected[] = "ns2::tmpl<int>::foo2";
4982 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4983 EXPECT (expected));
4984 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4985 EXPECT (expected));
4986 }
4987
4988 SELF_CHECK (!any_mismatch);
4989
4990 #undef EXPECT
4991 #undef CHECK_MATCH
4992 }
4993
4994 static void
4995 run_test ()
4996 {
4997 test_mapped_index_find_name_component_bounds ();
4998 test_dw2_expand_symtabs_matching_symbol ();
4999 }
5000
5001 }} // namespace selftests::dw2_expand_symtabs_matching
5002
5003 #endif /* GDB_SELF_TEST */
5004
5005 /* If FILE_MATCHER is NULL or if PER_CU has
5006 dwarf2_per_cu_quick_data::MARK set (see
5007 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5008 EXPANSION_NOTIFY on it. */
5009
5010 static void
5011 dw2_expand_symtabs_matching_one
5012 (struct dwarf2_per_cu_data *per_cu,
5013 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5014 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5015 {
5016 if (file_matcher == NULL || per_cu->v.quick->mark)
5017 {
5018 bool symtab_was_null
5019 = (per_cu->v.quick->compunit_symtab == NULL);
5020
5021 dw2_instantiate_symtab (per_cu, false);
5022
5023 if (expansion_notify != NULL
5024 && symtab_was_null
5025 && per_cu->v.quick->compunit_symtab != NULL)
5026 expansion_notify (per_cu->v.quick->compunit_symtab);
5027 }
5028 }
5029
5030 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5031 matched, to expand corresponding CUs that were marked. IDX is the
5032 index of the symbol name that matched. */
5033
5034 static void
5035 dw2_expand_marked_cus
5036 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5037 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5038 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5039 search_domain kind)
5040 {
5041 offset_type *vec, vec_len, vec_idx;
5042 bool global_seen = false;
5043 mapped_index &index = *dwarf2_per_objfile->index_table;
5044
5045 vec = (offset_type *) (index.constant_pool
5046 + MAYBE_SWAP (index.symbol_table[idx].vec));
5047 vec_len = MAYBE_SWAP (vec[0]);
5048 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5049 {
5050 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5051 /* This value is only valid for index versions >= 7. */
5052 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5053 gdb_index_symbol_kind symbol_kind =
5054 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5055 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5056 /* Only check the symbol attributes if they're present.
5057 Indices prior to version 7 don't record them,
5058 and indices >= 7 may elide them for certain symbols
5059 (gold does this). */
5060 int attrs_valid =
5061 (index.version >= 7
5062 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5063
5064 /* Work around gold/15646. */
5065 if (attrs_valid)
5066 {
5067 if (!is_static && global_seen)
5068 continue;
5069 if (!is_static)
5070 global_seen = true;
5071 }
5072
5073 /* Only check the symbol's kind if it has one. */
5074 if (attrs_valid)
5075 {
5076 switch (kind)
5077 {
5078 case VARIABLES_DOMAIN:
5079 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5080 continue;
5081 break;
5082 case FUNCTIONS_DOMAIN:
5083 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5084 continue;
5085 break;
5086 case TYPES_DOMAIN:
5087 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5088 continue;
5089 break;
5090 case MODULES_DOMAIN:
5091 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
5092 continue;
5093 break;
5094 default:
5095 break;
5096 }
5097 }
5098
5099 /* Don't crash on bad data. */
5100 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5101 + dwarf2_per_objfile->all_type_units.size ()))
5102 {
5103 complaint (_(".gdb_index entry has bad CU index"
5104 " [in module %s]"),
5105 objfile_name (dwarf2_per_objfile->objfile));
5106 continue;
5107 }
5108
5109 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5110 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5111 expansion_notify);
5112 }
5113 }
5114
5115 /* If FILE_MATCHER is non-NULL, set all the
5116 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5117 that match FILE_MATCHER. */
5118
5119 static void
5120 dw_expand_symtabs_matching_file_matcher
5121 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5122 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5123 {
5124 if (file_matcher == NULL)
5125 return;
5126
5127 objfile *const objfile = dwarf2_per_objfile->objfile;
5128
5129 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5130 htab_eq_pointer,
5131 NULL, xcalloc, xfree));
5132 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5133 htab_eq_pointer,
5134 NULL, xcalloc, xfree));
5135
5136 /* The rule is CUs specify all the files, including those used by
5137 any TU, so there's no need to scan TUs here. */
5138
5139 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5140 {
5141 QUIT;
5142
5143 per_cu->v.quick->mark = 0;
5144
5145 /* We only need to look at symtabs not already expanded. */
5146 if (per_cu->v.quick->compunit_symtab)
5147 continue;
5148
5149 quick_file_names *file_data = dw2_get_file_names (per_cu);
5150 if (file_data == NULL)
5151 continue;
5152
5153 if (htab_find (visited_not_found.get (), file_data) != NULL)
5154 continue;
5155 else if (htab_find (visited_found.get (), file_data) != NULL)
5156 {
5157 per_cu->v.quick->mark = 1;
5158 continue;
5159 }
5160
5161 for (int j = 0; j < file_data->num_file_names; ++j)
5162 {
5163 const char *this_real_name;
5164
5165 if (file_matcher (file_data->file_names[j], false))
5166 {
5167 per_cu->v.quick->mark = 1;
5168 break;
5169 }
5170
5171 /* Before we invoke realpath, which can get expensive when many
5172 files are involved, do a quick comparison of the basenames. */
5173 if (!basenames_may_differ
5174 && !file_matcher (lbasename (file_data->file_names[j]),
5175 true))
5176 continue;
5177
5178 this_real_name = dw2_get_real_path (objfile, file_data, j);
5179 if (file_matcher (this_real_name, false))
5180 {
5181 per_cu->v.quick->mark = 1;
5182 break;
5183 }
5184 }
5185
5186 void **slot = htab_find_slot (per_cu->v.quick->mark
5187 ? visited_found.get ()
5188 : visited_not_found.get (),
5189 file_data, INSERT);
5190 *slot = file_data;
5191 }
5192 }
5193
5194 static void
5195 dw2_expand_symtabs_matching
5196 (struct objfile *objfile,
5197 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5198 const lookup_name_info &lookup_name,
5199 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5200 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5201 enum search_domain kind)
5202 {
5203 struct dwarf2_per_objfile *dwarf2_per_objfile
5204 = get_dwarf2_per_objfile (objfile);
5205
5206 /* index_table is NULL if OBJF_READNOW. */
5207 if (!dwarf2_per_objfile->index_table)
5208 return;
5209
5210 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5211
5212 mapped_index &index = *dwarf2_per_objfile->index_table;
5213
5214 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5215 symbol_matcher,
5216 kind, [&] (offset_type idx)
5217 {
5218 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5219 expansion_notify, kind);
5220 return true;
5221 });
5222 }
5223
5224 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5225 symtab. */
5226
5227 static struct compunit_symtab *
5228 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5229 CORE_ADDR pc)
5230 {
5231 int i;
5232
5233 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5234 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5235 return cust;
5236
5237 if (cust->includes == NULL)
5238 return NULL;
5239
5240 for (i = 0; cust->includes[i]; ++i)
5241 {
5242 struct compunit_symtab *s = cust->includes[i];
5243
5244 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5245 if (s != NULL)
5246 return s;
5247 }
5248
5249 return NULL;
5250 }
5251
5252 static struct compunit_symtab *
5253 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5254 struct bound_minimal_symbol msymbol,
5255 CORE_ADDR pc,
5256 struct obj_section *section,
5257 int warn_if_readin)
5258 {
5259 struct dwarf2_per_cu_data *data;
5260 struct compunit_symtab *result;
5261
5262 if (!objfile->partial_symtabs->psymtabs_addrmap)
5263 return NULL;
5264
5265 CORE_ADDR baseaddr = objfile->text_section_offset ();
5266 data = (struct dwarf2_per_cu_data *) addrmap_find
5267 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
5268 if (!data)
5269 return NULL;
5270
5271 if (warn_if_readin && data->v.quick->compunit_symtab)
5272 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5273 paddress (get_objfile_arch (objfile), pc));
5274
5275 result
5276 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5277 false),
5278 pc);
5279 gdb_assert (result != NULL);
5280 return result;
5281 }
5282
5283 static void
5284 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5285 void *data, int need_fullname)
5286 {
5287 struct dwarf2_per_objfile *dwarf2_per_objfile
5288 = get_dwarf2_per_objfile (objfile);
5289
5290 if (!dwarf2_per_objfile->filenames_cache)
5291 {
5292 dwarf2_per_objfile->filenames_cache.emplace ();
5293
5294 htab_up visited (htab_create_alloc (10,
5295 htab_hash_pointer, htab_eq_pointer,
5296 NULL, xcalloc, xfree));
5297
5298 /* The rule is CUs specify all the files, including those used
5299 by any TU, so there's no need to scan TUs here. We can
5300 ignore file names coming from already-expanded CUs. */
5301
5302 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5303 {
5304 if (per_cu->v.quick->compunit_symtab)
5305 {
5306 void **slot = htab_find_slot (visited.get (),
5307 per_cu->v.quick->file_names,
5308 INSERT);
5309
5310 *slot = per_cu->v.quick->file_names;
5311 }
5312 }
5313
5314 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5315 {
5316 /* We only need to look at symtabs not already expanded. */
5317 if (per_cu->v.quick->compunit_symtab)
5318 continue;
5319
5320 quick_file_names *file_data = dw2_get_file_names (per_cu);
5321 if (file_data == NULL)
5322 continue;
5323
5324 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5325 if (*slot)
5326 {
5327 /* Already visited. */
5328 continue;
5329 }
5330 *slot = file_data;
5331
5332 for (int j = 0; j < file_data->num_file_names; ++j)
5333 {
5334 const char *filename = file_data->file_names[j];
5335 dwarf2_per_objfile->filenames_cache->seen (filename);
5336 }
5337 }
5338 }
5339
5340 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5341 {
5342 gdb::unique_xmalloc_ptr<char> this_real_name;
5343
5344 if (need_fullname)
5345 this_real_name = gdb_realpath (filename);
5346 (*fun) (filename, this_real_name.get (), data);
5347 });
5348 }
5349
5350 static int
5351 dw2_has_symbols (struct objfile *objfile)
5352 {
5353 return 1;
5354 }
5355
5356 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5357 {
5358 dw2_has_symbols,
5359 dw2_find_last_source_symtab,
5360 dw2_forget_cached_source_info,
5361 dw2_map_symtabs_matching_filename,
5362 dw2_lookup_symbol,
5363 dw2_print_stats,
5364 dw2_dump,
5365 dw2_expand_symtabs_for_function,
5366 dw2_expand_all_symtabs,
5367 dw2_expand_symtabs_with_fullname,
5368 dw2_map_matching_symbols,
5369 dw2_expand_symtabs_matching,
5370 dw2_find_pc_sect_compunit_symtab,
5371 NULL,
5372 dw2_map_symbol_filenames
5373 };
5374
5375 /* DWARF-5 debug_names reader. */
5376
5377 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5378 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5379
5380 /* A helper function that reads the .debug_names section in SECTION
5381 and fills in MAP. FILENAME is the name of the file containing the
5382 section; it is used for error reporting.
5383
5384 Returns true if all went well, false otherwise. */
5385
5386 static bool
5387 read_debug_names_from_section (struct objfile *objfile,
5388 const char *filename,
5389 struct dwarf2_section_info *section,
5390 mapped_debug_names &map)
5391 {
5392 if (dwarf2_section_empty_p (section))
5393 return false;
5394
5395 /* Older elfutils strip versions could keep the section in the main
5396 executable while splitting it for the separate debug info file. */
5397 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5398 return false;
5399
5400 dwarf2_read_section (objfile, section);
5401
5402 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5403
5404 const gdb_byte *addr = section->buffer;
5405
5406 bfd *const abfd = get_section_bfd_owner (section);
5407
5408 unsigned int bytes_read;
5409 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5410 addr += bytes_read;
5411
5412 map.dwarf5_is_dwarf64 = bytes_read != 4;
5413 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5414 if (bytes_read + length != section->size)
5415 {
5416 /* There may be multiple per-CU indices. */
5417 warning (_("Section .debug_names in %s length %s does not match "
5418 "section length %s, ignoring .debug_names."),
5419 filename, plongest (bytes_read + length),
5420 pulongest (section->size));
5421 return false;
5422 }
5423
5424 /* The version number. */
5425 uint16_t version = read_2_bytes (abfd, addr);
5426 addr += 2;
5427 if (version != 5)
5428 {
5429 warning (_("Section .debug_names in %s has unsupported version %d, "
5430 "ignoring .debug_names."),
5431 filename, version);
5432 return false;
5433 }
5434
5435 /* Padding. */
5436 uint16_t padding = read_2_bytes (abfd, addr);
5437 addr += 2;
5438 if (padding != 0)
5439 {
5440 warning (_("Section .debug_names in %s has unsupported padding %d, "
5441 "ignoring .debug_names."),
5442 filename, padding);
5443 return false;
5444 }
5445
5446 /* comp_unit_count - The number of CUs in the CU list. */
5447 map.cu_count = read_4_bytes (abfd, addr);
5448 addr += 4;
5449
5450 /* local_type_unit_count - The number of TUs in the local TU
5451 list. */
5452 map.tu_count = read_4_bytes (abfd, addr);
5453 addr += 4;
5454
5455 /* foreign_type_unit_count - The number of TUs in the foreign TU
5456 list. */
5457 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5458 addr += 4;
5459 if (foreign_tu_count != 0)
5460 {
5461 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5462 "ignoring .debug_names."),
5463 filename, static_cast<unsigned long> (foreign_tu_count));
5464 return false;
5465 }
5466
5467 /* bucket_count - The number of hash buckets in the hash lookup
5468 table. */
5469 map.bucket_count = read_4_bytes (abfd, addr);
5470 addr += 4;
5471
5472 /* name_count - The number of unique names in the index. */
5473 map.name_count = read_4_bytes (abfd, addr);
5474 addr += 4;
5475
5476 /* abbrev_table_size - The size in bytes of the abbreviations
5477 table. */
5478 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5479 addr += 4;
5480
5481 /* augmentation_string_size - The size in bytes of the augmentation
5482 string. This value is rounded up to a multiple of 4. */
5483 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5484 addr += 4;
5485 map.augmentation_is_gdb = ((augmentation_string_size
5486 == sizeof (dwarf5_augmentation))
5487 && memcmp (addr, dwarf5_augmentation,
5488 sizeof (dwarf5_augmentation)) == 0);
5489 augmentation_string_size += (-augmentation_string_size) & 3;
5490 addr += augmentation_string_size;
5491
5492 /* List of CUs */
5493 map.cu_table_reordered = addr;
5494 addr += map.cu_count * map.offset_size;
5495
5496 /* List of Local TUs */
5497 map.tu_table_reordered = addr;
5498 addr += map.tu_count * map.offset_size;
5499
5500 /* Hash Lookup Table */
5501 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5502 addr += map.bucket_count * 4;
5503 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5504 addr += map.name_count * 4;
5505
5506 /* Name Table */
5507 map.name_table_string_offs_reordered = addr;
5508 addr += map.name_count * map.offset_size;
5509 map.name_table_entry_offs_reordered = addr;
5510 addr += map.name_count * map.offset_size;
5511
5512 const gdb_byte *abbrev_table_start = addr;
5513 for (;;)
5514 {
5515 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5516 addr += bytes_read;
5517 if (index_num == 0)
5518 break;
5519
5520 const auto insertpair
5521 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5522 if (!insertpair.second)
5523 {
5524 warning (_("Section .debug_names in %s has duplicate index %s, "
5525 "ignoring .debug_names."),
5526 filename, pulongest (index_num));
5527 return false;
5528 }
5529 mapped_debug_names::index_val &indexval = insertpair.first->second;
5530 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5531 addr += bytes_read;
5532
5533 for (;;)
5534 {
5535 mapped_debug_names::index_val::attr attr;
5536 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5537 addr += bytes_read;
5538 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5539 addr += bytes_read;
5540 if (attr.form == DW_FORM_implicit_const)
5541 {
5542 attr.implicit_const = read_signed_leb128 (abfd, addr,
5543 &bytes_read);
5544 addr += bytes_read;
5545 }
5546 if (attr.dw_idx == 0 && attr.form == 0)
5547 break;
5548 indexval.attr_vec.push_back (std::move (attr));
5549 }
5550 }
5551 if (addr != abbrev_table_start + abbrev_table_size)
5552 {
5553 warning (_("Section .debug_names in %s has abbreviation_table "
5554 "of size %s vs. written as %u, ignoring .debug_names."),
5555 filename, plongest (addr - abbrev_table_start),
5556 abbrev_table_size);
5557 return false;
5558 }
5559 map.entry_pool = addr;
5560
5561 return true;
5562 }
5563
5564 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5565 list. */
5566
5567 static void
5568 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5569 const mapped_debug_names &map,
5570 dwarf2_section_info &section,
5571 bool is_dwz)
5572 {
5573 sect_offset sect_off_prev;
5574 for (uint32_t i = 0; i <= map.cu_count; ++i)
5575 {
5576 sect_offset sect_off_next;
5577 if (i < map.cu_count)
5578 {
5579 sect_off_next
5580 = (sect_offset) (extract_unsigned_integer
5581 (map.cu_table_reordered + i * map.offset_size,
5582 map.offset_size,
5583 map.dwarf5_byte_order));
5584 }
5585 else
5586 sect_off_next = (sect_offset) section.size;
5587 if (i >= 1)
5588 {
5589 const ULONGEST length = sect_off_next - sect_off_prev;
5590 dwarf2_per_cu_data *per_cu
5591 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5592 sect_off_prev, length);
5593 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5594 }
5595 sect_off_prev = sect_off_next;
5596 }
5597 }
5598
5599 /* Read the CU list from the mapped index, and use it to create all
5600 the CU objects for this dwarf2_per_objfile. */
5601
5602 static void
5603 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5604 const mapped_debug_names &map,
5605 const mapped_debug_names &dwz_map)
5606 {
5607 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5608 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5609
5610 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5611 dwarf2_per_objfile->info,
5612 false /* is_dwz */);
5613
5614 if (dwz_map.cu_count == 0)
5615 return;
5616
5617 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5618 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5619 true /* is_dwz */);
5620 }
5621
5622 /* Read .debug_names. If everything went ok, initialize the "quick"
5623 elements of all the CUs and return true. Otherwise, return false. */
5624
5625 static bool
5626 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5627 {
5628 std::unique_ptr<mapped_debug_names> map
5629 (new mapped_debug_names (dwarf2_per_objfile));
5630 mapped_debug_names dwz_map (dwarf2_per_objfile);
5631 struct objfile *objfile = dwarf2_per_objfile->objfile;
5632
5633 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5634 &dwarf2_per_objfile->debug_names,
5635 *map))
5636 return false;
5637
5638 /* Don't use the index if it's empty. */
5639 if (map->name_count == 0)
5640 return false;
5641
5642 /* If there is a .dwz file, read it so we can get its CU list as
5643 well. */
5644 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5645 if (dwz != NULL)
5646 {
5647 if (!read_debug_names_from_section (objfile,
5648 bfd_get_filename (dwz->dwz_bfd.get ()),
5649 &dwz->debug_names, dwz_map))
5650 {
5651 warning (_("could not read '.debug_names' section from %s; skipping"),
5652 bfd_get_filename (dwz->dwz_bfd.get ()));
5653 return false;
5654 }
5655 }
5656
5657 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5658
5659 if (map->tu_count != 0)
5660 {
5661 /* We can only handle a single .debug_types when we have an
5662 index. */
5663 if (dwarf2_per_objfile->types.size () != 1)
5664 return false;
5665
5666 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5667
5668 create_signatured_type_table_from_debug_names
5669 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5670 }
5671
5672 create_addrmap_from_aranges (dwarf2_per_objfile,
5673 &dwarf2_per_objfile->debug_aranges);
5674
5675 dwarf2_per_objfile->debug_names_table = std::move (map);
5676 dwarf2_per_objfile->using_index = 1;
5677 dwarf2_per_objfile->quick_file_names_table =
5678 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5679
5680 return true;
5681 }
5682
5683 /* Type used to manage iterating over all CUs looking for a symbol for
5684 .debug_names. */
5685
5686 class dw2_debug_names_iterator
5687 {
5688 public:
5689 dw2_debug_names_iterator (const mapped_debug_names &map,
5690 gdb::optional<block_enum> block_index,
5691 domain_enum domain,
5692 const char *name)
5693 : m_map (map), m_block_index (block_index), m_domain (domain),
5694 m_addr (find_vec_in_debug_names (map, name))
5695 {}
5696
5697 dw2_debug_names_iterator (const mapped_debug_names &map,
5698 search_domain search, uint32_t namei)
5699 : m_map (map),
5700 m_search (search),
5701 m_addr (find_vec_in_debug_names (map, namei))
5702 {}
5703
5704 dw2_debug_names_iterator (const mapped_debug_names &map,
5705 block_enum block_index, domain_enum domain,
5706 uint32_t namei)
5707 : m_map (map), m_block_index (block_index), m_domain (domain),
5708 m_addr (find_vec_in_debug_names (map, namei))
5709 {}
5710
5711 /* Return the next matching CU or NULL if there are no more. */
5712 dwarf2_per_cu_data *next ();
5713
5714 private:
5715 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5716 const char *name);
5717 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5718 uint32_t namei);
5719
5720 /* The internalized form of .debug_names. */
5721 const mapped_debug_names &m_map;
5722
5723 /* If set, only look for symbols that match that block. Valid values are
5724 GLOBAL_BLOCK and STATIC_BLOCK. */
5725 const gdb::optional<block_enum> m_block_index;
5726
5727 /* The kind of symbol we're looking for. */
5728 const domain_enum m_domain = UNDEF_DOMAIN;
5729 const search_domain m_search = ALL_DOMAIN;
5730
5731 /* The list of CUs from the index entry of the symbol, or NULL if
5732 not found. */
5733 const gdb_byte *m_addr;
5734 };
5735
5736 const char *
5737 mapped_debug_names::namei_to_name (uint32_t namei) const
5738 {
5739 const ULONGEST namei_string_offs
5740 = extract_unsigned_integer ((name_table_string_offs_reordered
5741 + namei * offset_size),
5742 offset_size,
5743 dwarf5_byte_order);
5744 return read_indirect_string_at_offset
5745 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5746 }
5747
5748 /* Find a slot in .debug_names for the object named NAME. If NAME is
5749 found, return pointer to its pool data. If NAME cannot be found,
5750 return NULL. */
5751
5752 const gdb_byte *
5753 dw2_debug_names_iterator::find_vec_in_debug_names
5754 (const mapped_debug_names &map, const char *name)
5755 {
5756 int (*cmp) (const char *, const char *);
5757
5758 gdb::unique_xmalloc_ptr<char> without_params;
5759 if (current_language->la_language == language_cplus
5760 || current_language->la_language == language_fortran
5761 || current_language->la_language == language_d)
5762 {
5763 /* NAME is already canonical. Drop any qualifiers as
5764 .debug_names does not contain any. */
5765
5766 if (strchr (name, '(') != NULL)
5767 {
5768 without_params = cp_remove_params (name);
5769 if (without_params != NULL)
5770 name = without_params.get ();
5771 }
5772 }
5773
5774 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5775
5776 const uint32_t full_hash = dwarf5_djb_hash (name);
5777 uint32_t namei
5778 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5779 (map.bucket_table_reordered
5780 + (full_hash % map.bucket_count)), 4,
5781 map.dwarf5_byte_order);
5782 if (namei == 0)
5783 return NULL;
5784 --namei;
5785 if (namei >= map.name_count)
5786 {
5787 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5788 "[in module %s]"),
5789 namei, map.name_count,
5790 objfile_name (map.dwarf2_per_objfile->objfile));
5791 return NULL;
5792 }
5793
5794 for (;;)
5795 {
5796 const uint32_t namei_full_hash
5797 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5798 (map.hash_table_reordered + namei), 4,
5799 map.dwarf5_byte_order);
5800 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5801 return NULL;
5802
5803 if (full_hash == namei_full_hash)
5804 {
5805 const char *const namei_string = map.namei_to_name (namei);
5806
5807 #if 0 /* An expensive sanity check. */
5808 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5809 {
5810 complaint (_("Wrong .debug_names hash for string at index %u "
5811 "[in module %s]"),
5812 namei, objfile_name (dwarf2_per_objfile->objfile));
5813 return NULL;
5814 }
5815 #endif
5816
5817 if (cmp (namei_string, name) == 0)
5818 {
5819 const ULONGEST namei_entry_offs
5820 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5821 + namei * map.offset_size),
5822 map.offset_size, map.dwarf5_byte_order);
5823 return map.entry_pool + namei_entry_offs;
5824 }
5825 }
5826
5827 ++namei;
5828 if (namei >= map.name_count)
5829 return NULL;
5830 }
5831 }
5832
5833 const gdb_byte *
5834 dw2_debug_names_iterator::find_vec_in_debug_names
5835 (const mapped_debug_names &map, uint32_t namei)
5836 {
5837 if (namei >= map.name_count)
5838 {
5839 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5840 "[in module %s]"),
5841 namei, map.name_count,
5842 objfile_name (map.dwarf2_per_objfile->objfile));
5843 return NULL;
5844 }
5845
5846 const ULONGEST namei_entry_offs
5847 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5848 + namei * map.offset_size),
5849 map.offset_size, map.dwarf5_byte_order);
5850 return map.entry_pool + namei_entry_offs;
5851 }
5852
5853 /* See dw2_debug_names_iterator. */
5854
5855 dwarf2_per_cu_data *
5856 dw2_debug_names_iterator::next ()
5857 {
5858 if (m_addr == NULL)
5859 return NULL;
5860
5861 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5862 struct objfile *objfile = dwarf2_per_objfile->objfile;
5863 bfd *const abfd = objfile->obfd;
5864
5865 again:
5866
5867 unsigned int bytes_read;
5868 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5869 m_addr += bytes_read;
5870 if (abbrev == 0)
5871 return NULL;
5872
5873 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5874 if (indexval_it == m_map.abbrev_map.cend ())
5875 {
5876 complaint (_("Wrong .debug_names undefined abbrev code %s "
5877 "[in module %s]"),
5878 pulongest (abbrev), objfile_name (objfile));
5879 return NULL;
5880 }
5881 const mapped_debug_names::index_val &indexval = indexval_it->second;
5882 enum class symbol_linkage {
5883 unknown,
5884 static_,
5885 extern_,
5886 } symbol_linkage_ = symbol_linkage::unknown;
5887 dwarf2_per_cu_data *per_cu = NULL;
5888 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5889 {
5890 ULONGEST ull;
5891 switch (attr.form)
5892 {
5893 case DW_FORM_implicit_const:
5894 ull = attr.implicit_const;
5895 break;
5896 case DW_FORM_flag_present:
5897 ull = 1;
5898 break;
5899 case DW_FORM_udata:
5900 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5901 m_addr += bytes_read;
5902 break;
5903 default:
5904 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5905 dwarf_form_name (attr.form),
5906 objfile_name (objfile));
5907 return NULL;
5908 }
5909 switch (attr.dw_idx)
5910 {
5911 case DW_IDX_compile_unit:
5912 /* Don't crash on bad data. */
5913 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5914 {
5915 complaint (_(".debug_names entry has bad CU index %s"
5916 " [in module %s]"),
5917 pulongest (ull),
5918 objfile_name (dwarf2_per_objfile->objfile));
5919 continue;
5920 }
5921 per_cu = dwarf2_per_objfile->get_cutu (ull);
5922 break;
5923 case DW_IDX_type_unit:
5924 /* Don't crash on bad data. */
5925 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5926 {
5927 complaint (_(".debug_names entry has bad TU index %s"
5928 " [in module %s]"),
5929 pulongest (ull),
5930 objfile_name (dwarf2_per_objfile->objfile));
5931 continue;
5932 }
5933 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5934 break;
5935 case DW_IDX_GNU_internal:
5936 if (!m_map.augmentation_is_gdb)
5937 break;
5938 symbol_linkage_ = symbol_linkage::static_;
5939 break;
5940 case DW_IDX_GNU_external:
5941 if (!m_map.augmentation_is_gdb)
5942 break;
5943 symbol_linkage_ = symbol_linkage::extern_;
5944 break;
5945 }
5946 }
5947
5948 /* Skip if already read in. */
5949 if (per_cu->v.quick->compunit_symtab)
5950 goto again;
5951
5952 /* Check static vs global. */
5953 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5954 {
5955 const bool want_static = *m_block_index == STATIC_BLOCK;
5956 const bool symbol_is_static =
5957 symbol_linkage_ == symbol_linkage::static_;
5958 if (want_static != symbol_is_static)
5959 goto again;
5960 }
5961
5962 /* Match dw2_symtab_iter_next, symbol_kind
5963 and debug_names::psymbol_tag. */
5964 switch (m_domain)
5965 {
5966 case VAR_DOMAIN:
5967 switch (indexval.dwarf_tag)
5968 {
5969 case DW_TAG_variable:
5970 case DW_TAG_subprogram:
5971 /* Some types are also in VAR_DOMAIN. */
5972 case DW_TAG_typedef:
5973 case DW_TAG_structure_type:
5974 break;
5975 default:
5976 goto again;
5977 }
5978 break;
5979 case STRUCT_DOMAIN:
5980 switch (indexval.dwarf_tag)
5981 {
5982 case DW_TAG_typedef:
5983 case DW_TAG_structure_type:
5984 break;
5985 default:
5986 goto again;
5987 }
5988 break;
5989 case LABEL_DOMAIN:
5990 switch (indexval.dwarf_tag)
5991 {
5992 case 0:
5993 case DW_TAG_variable:
5994 break;
5995 default:
5996 goto again;
5997 }
5998 break;
5999 case MODULE_DOMAIN:
6000 switch (indexval.dwarf_tag)
6001 {
6002 case DW_TAG_module:
6003 break;
6004 default:
6005 goto again;
6006 }
6007 break;
6008 default:
6009 break;
6010 }
6011
6012 /* Match dw2_expand_symtabs_matching, symbol_kind and
6013 debug_names::psymbol_tag. */
6014 switch (m_search)
6015 {
6016 case VARIABLES_DOMAIN:
6017 switch (indexval.dwarf_tag)
6018 {
6019 case DW_TAG_variable:
6020 break;
6021 default:
6022 goto again;
6023 }
6024 break;
6025 case FUNCTIONS_DOMAIN:
6026 switch (indexval.dwarf_tag)
6027 {
6028 case DW_TAG_subprogram:
6029 break;
6030 default:
6031 goto again;
6032 }
6033 break;
6034 case TYPES_DOMAIN:
6035 switch (indexval.dwarf_tag)
6036 {
6037 case DW_TAG_typedef:
6038 case DW_TAG_structure_type:
6039 break;
6040 default:
6041 goto again;
6042 }
6043 break;
6044 case MODULES_DOMAIN:
6045 switch (indexval.dwarf_tag)
6046 {
6047 case DW_TAG_module:
6048 break;
6049 default:
6050 goto again;
6051 }
6052 default:
6053 break;
6054 }
6055
6056 return per_cu;
6057 }
6058
6059 static struct compunit_symtab *
6060 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
6061 const char *name, domain_enum domain)
6062 {
6063 struct dwarf2_per_objfile *dwarf2_per_objfile
6064 = get_dwarf2_per_objfile (objfile);
6065
6066 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6067 if (!mapp)
6068 {
6069 /* index is NULL if OBJF_READNOW. */
6070 return NULL;
6071 }
6072 const auto &map = *mapp;
6073
6074 dw2_debug_names_iterator iter (map, block_index, domain, name);
6075
6076 struct compunit_symtab *stab_best = NULL;
6077 struct dwarf2_per_cu_data *per_cu;
6078 while ((per_cu = iter.next ()) != NULL)
6079 {
6080 struct symbol *sym, *with_opaque = NULL;
6081 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
6082 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6083 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6084
6085 sym = block_find_symbol (block, name, domain,
6086 block_find_non_opaque_type_preferred,
6087 &with_opaque);
6088
6089 /* Some caution must be observed with overloaded functions and
6090 methods, since the index will not contain any overload
6091 information (but NAME might contain it). */
6092
6093 if (sym != NULL
6094 && strcmp_iw (sym->search_name (), name) == 0)
6095 return stab;
6096 if (with_opaque != NULL
6097 && strcmp_iw (with_opaque->search_name (), name) == 0)
6098 stab_best = stab;
6099
6100 /* Keep looking through other CUs. */
6101 }
6102
6103 return stab_best;
6104 }
6105
6106 /* This dumps minimal information about .debug_names. It is called
6107 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6108 uses this to verify that .debug_names has been loaded. */
6109
6110 static void
6111 dw2_debug_names_dump (struct objfile *objfile)
6112 {
6113 struct dwarf2_per_objfile *dwarf2_per_objfile
6114 = get_dwarf2_per_objfile (objfile);
6115
6116 gdb_assert (dwarf2_per_objfile->using_index);
6117 printf_filtered (".debug_names:");
6118 if (dwarf2_per_objfile->debug_names_table)
6119 printf_filtered (" exists\n");
6120 else
6121 printf_filtered (" faked for \"readnow\"\n");
6122 printf_filtered ("\n");
6123 }
6124
6125 static void
6126 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6127 const char *func_name)
6128 {
6129 struct dwarf2_per_objfile *dwarf2_per_objfile
6130 = get_dwarf2_per_objfile (objfile);
6131
6132 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6133 if (dwarf2_per_objfile->debug_names_table)
6134 {
6135 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6136
6137 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
6138
6139 struct dwarf2_per_cu_data *per_cu;
6140 while ((per_cu = iter.next ()) != NULL)
6141 dw2_instantiate_symtab (per_cu, false);
6142 }
6143 }
6144
6145 static void
6146 dw2_debug_names_map_matching_symbols
6147 (struct objfile *objfile,
6148 const lookup_name_info &name, domain_enum domain,
6149 int global,
6150 gdb::function_view<symbol_found_callback_ftype> callback,
6151 symbol_compare_ftype *ordered_compare)
6152 {
6153 struct dwarf2_per_objfile *dwarf2_per_objfile
6154 = get_dwarf2_per_objfile (objfile);
6155
6156 /* debug_names_table is NULL if OBJF_READNOW. */
6157 if (!dwarf2_per_objfile->debug_names_table)
6158 return;
6159
6160 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6161 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6162
6163 const char *match_name = name.ada ().lookup_name ().c_str ();
6164 auto matcher = [&] (const char *symname)
6165 {
6166 if (ordered_compare == nullptr)
6167 return true;
6168 return ordered_compare (symname, match_name) == 0;
6169 };
6170
6171 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6172 [&] (offset_type namei)
6173 {
6174 /* The name was matched, now expand corresponding CUs that were
6175 marked. */
6176 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6177
6178 struct dwarf2_per_cu_data *per_cu;
6179 while ((per_cu = iter.next ()) != NULL)
6180 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6181 return true;
6182 });
6183
6184 /* It's a shame we couldn't do this inside the
6185 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6186 that have already been expanded. Instead, this loop matches what
6187 the psymtab code does. */
6188 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6189 {
6190 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6191 if (cust != nullptr)
6192 {
6193 const struct block *block
6194 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6195 if (!iterate_over_symbols_terminated (block, name,
6196 domain, callback))
6197 break;
6198 }
6199 }
6200 }
6201
6202 static void
6203 dw2_debug_names_expand_symtabs_matching
6204 (struct objfile *objfile,
6205 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6206 const lookup_name_info &lookup_name,
6207 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6208 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6209 enum search_domain kind)
6210 {
6211 struct dwarf2_per_objfile *dwarf2_per_objfile
6212 = get_dwarf2_per_objfile (objfile);
6213
6214 /* debug_names_table is NULL if OBJF_READNOW. */
6215 if (!dwarf2_per_objfile->debug_names_table)
6216 return;
6217
6218 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6219
6220 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6221
6222 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6223 symbol_matcher,
6224 kind, [&] (offset_type namei)
6225 {
6226 /* The name was matched, now expand corresponding CUs that were
6227 marked. */
6228 dw2_debug_names_iterator iter (map, kind, namei);
6229
6230 struct dwarf2_per_cu_data *per_cu;
6231 while ((per_cu = iter.next ()) != NULL)
6232 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6233 expansion_notify);
6234 return true;
6235 });
6236 }
6237
6238 const struct quick_symbol_functions dwarf2_debug_names_functions =
6239 {
6240 dw2_has_symbols,
6241 dw2_find_last_source_symtab,
6242 dw2_forget_cached_source_info,
6243 dw2_map_symtabs_matching_filename,
6244 dw2_debug_names_lookup_symbol,
6245 dw2_print_stats,
6246 dw2_debug_names_dump,
6247 dw2_debug_names_expand_symtabs_for_function,
6248 dw2_expand_all_symtabs,
6249 dw2_expand_symtabs_with_fullname,
6250 dw2_debug_names_map_matching_symbols,
6251 dw2_debug_names_expand_symtabs_matching,
6252 dw2_find_pc_sect_compunit_symtab,
6253 NULL,
6254 dw2_map_symbol_filenames
6255 };
6256
6257 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6258 to either a dwarf2_per_objfile or dwz_file object. */
6259
6260 template <typename T>
6261 static gdb::array_view<const gdb_byte>
6262 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6263 {
6264 dwarf2_section_info *section = &section_owner->gdb_index;
6265
6266 if (dwarf2_section_empty_p (section))
6267 return {};
6268
6269 /* Older elfutils strip versions could keep the section in the main
6270 executable while splitting it for the separate debug info file. */
6271 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6272 return {};
6273
6274 dwarf2_read_section (obj, section);
6275
6276 /* dwarf2_section_info::size is a bfd_size_type, while
6277 gdb::array_view works with size_t. On 32-bit hosts, with
6278 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6279 is 32-bit. So we need an explicit narrowing conversion here.
6280 This is fine, because it's impossible to allocate or mmap an
6281 array/buffer larger than what size_t can represent. */
6282 return gdb::make_array_view (section->buffer, section->size);
6283 }
6284
6285 /* Lookup the index cache for the contents of the index associated to
6286 DWARF2_OBJ. */
6287
6288 static gdb::array_view<const gdb_byte>
6289 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6290 {
6291 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6292 if (build_id == nullptr)
6293 return {};
6294
6295 return global_index_cache.lookup_gdb_index (build_id,
6296 &dwarf2_obj->index_cache_res);
6297 }
6298
6299 /* Same as the above, but for DWZ. */
6300
6301 static gdb::array_view<const gdb_byte>
6302 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6303 {
6304 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6305 if (build_id == nullptr)
6306 return {};
6307
6308 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6309 }
6310
6311 /* See symfile.h. */
6312
6313 bool
6314 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6315 {
6316 struct dwarf2_per_objfile *dwarf2_per_objfile
6317 = get_dwarf2_per_objfile (objfile);
6318
6319 /* If we're about to read full symbols, don't bother with the
6320 indices. In this case we also don't care if some other debug
6321 format is making psymtabs, because they are all about to be
6322 expanded anyway. */
6323 if ((objfile->flags & OBJF_READNOW))
6324 {
6325 dwarf2_per_objfile->using_index = 1;
6326 create_all_comp_units (dwarf2_per_objfile);
6327 create_all_type_units (dwarf2_per_objfile);
6328 dwarf2_per_objfile->quick_file_names_table
6329 = create_quick_file_names_table
6330 (dwarf2_per_objfile->all_comp_units.size ());
6331
6332 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6333 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6334 {
6335 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6336
6337 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6338 struct dwarf2_per_cu_quick_data);
6339 }
6340
6341 /* Return 1 so that gdb sees the "quick" functions. However,
6342 these functions will be no-ops because we will have expanded
6343 all symtabs. */
6344 *index_kind = dw_index_kind::GDB_INDEX;
6345 return true;
6346 }
6347
6348 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6349 {
6350 *index_kind = dw_index_kind::DEBUG_NAMES;
6351 return true;
6352 }
6353
6354 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6355 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6356 get_gdb_index_contents_from_section<dwz_file>))
6357 {
6358 *index_kind = dw_index_kind::GDB_INDEX;
6359 return true;
6360 }
6361
6362 /* ... otherwise, try to find the index in the index cache. */
6363 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6364 get_gdb_index_contents_from_cache,
6365 get_gdb_index_contents_from_cache_dwz))
6366 {
6367 global_index_cache.hit ();
6368 *index_kind = dw_index_kind::GDB_INDEX;
6369 return true;
6370 }
6371
6372 global_index_cache.miss ();
6373 return false;
6374 }
6375
6376 \f
6377
6378 /* Build a partial symbol table. */
6379
6380 void
6381 dwarf2_build_psymtabs (struct objfile *objfile)
6382 {
6383 struct dwarf2_per_objfile *dwarf2_per_objfile
6384 = get_dwarf2_per_objfile (objfile);
6385
6386 init_psymbol_list (objfile, 1024);
6387
6388 try
6389 {
6390 /* This isn't really ideal: all the data we allocate on the
6391 objfile's obstack is still uselessly kept around. However,
6392 freeing it seems unsafe. */
6393 psymtab_discarder psymtabs (objfile);
6394 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6395 psymtabs.keep ();
6396
6397 /* (maybe) store an index in the cache. */
6398 global_index_cache.store (dwarf2_per_objfile);
6399 }
6400 catch (const gdb_exception_error &except)
6401 {
6402 exception_print (gdb_stderr, except);
6403 }
6404 }
6405
6406 /* Return the total length of the CU described by HEADER. */
6407
6408 static unsigned int
6409 get_cu_length (const struct comp_unit_head *header)
6410 {
6411 return header->initial_length_size + header->length;
6412 }
6413
6414 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6415
6416 static inline bool
6417 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6418 {
6419 sect_offset bottom = cu_header->sect_off;
6420 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6421
6422 return sect_off >= bottom && sect_off < top;
6423 }
6424
6425 /* Find the base address of the compilation unit for range lists and
6426 location lists. It will normally be specified by DW_AT_low_pc.
6427 In DWARF-3 draft 4, the base address could be overridden by
6428 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6429 compilation units with discontinuous ranges. */
6430
6431 static void
6432 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6433 {
6434 struct attribute *attr;
6435
6436 cu->base_known = 0;
6437 cu->base_address = 0;
6438
6439 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6440 if (attr != nullptr)
6441 {
6442 cu->base_address = attr_value_as_address (attr);
6443 cu->base_known = 1;
6444 }
6445 else
6446 {
6447 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6448 if (attr != nullptr)
6449 {
6450 cu->base_address = attr_value_as_address (attr);
6451 cu->base_known = 1;
6452 }
6453 }
6454 }
6455
6456 /* Read in the comp unit header information from the debug_info at info_ptr.
6457 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6458 NOTE: This leaves members offset, first_die_offset to be filled in
6459 by the caller. */
6460
6461 static const gdb_byte *
6462 read_comp_unit_head (struct comp_unit_head *cu_header,
6463 const gdb_byte *info_ptr,
6464 struct dwarf2_section_info *section,
6465 rcuh_kind section_kind)
6466 {
6467 int signed_addr;
6468 unsigned int bytes_read;
6469 const char *filename = get_section_file_name (section);
6470 bfd *abfd = get_section_bfd_owner (section);
6471
6472 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6473 cu_header->initial_length_size = bytes_read;
6474 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6475 info_ptr += bytes_read;
6476 cu_header->version = read_2_bytes (abfd, info_ptr);
6477 if (cu_header->version < 2 || cu_header->version > 5)
6478 error (_("Dwarf Error: wrong version in compilation unit header "
6479 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6480 cu_header->version, filename);
6481 info_ptr += 2;
6482 if (cu_header->version < 5)
6483 switch (section_kind)
6484 {
6485 case rcuh_kind::COMPILE:
6486 cu_header->unit_type = DW_UT_compile;
6487 break;
6488 case rcuh_kind::TYPE:
6489 cu_header->unit_type = DW_UT_type;
6490 break;
6491 default:
6492 internal_error (__FILE__, __LINE__,
6493 _("read_comp_unit_head: invalid section_kind"));
6494 }
6495 else
6496 {
6497 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6498 (read_1_byte (abfd, info_ptr));
6499 info_ptr += 1;
6500 switch (cu_header->unit_type)
6501 {
6502 case DW_UT_compile:
6503 case DW_UT_partial:
6504 case DW_UT_skeleton:
6505 case DW_UT_split_compile:
6506 if (section_kind != rcuh_kind::COMPILE)
6507 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6508 "(is %s, should be %s) [in module %s]"),
6509 dwarf_unit_type_name (cu_header->unit_type),
6510 dwarf_unit_type_name (DW_UT_type), filename);
6511 break;
6512 case DW_UT_type:
6513 case DW_UT_split_type:
6514 section_kind = rcuh_kind::TYPE;
6515 break;
6516 default:
6517 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6518 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6519 "[in module %s]"), cu_header->unit_type,
6520 dwarf_unit_type_name (DW_UT_compile),
6521 dwarf_unit_type_name (DW_UT_skeleton),
6522 dwarf_unit_type_name (DW_UT_split_compile),
6523 dwarf_unit_type_name (DW_UT_type),
6524 dwarf_unit_type_name (DW_UT_split_type), filename);
6525 }
6526
6527 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6528 info_ptr += 1;
6529 }
6530 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6531 cu_header,
6532 &bytes_read);
6533 info_ptr += bytes_read;
6534 if (cu_header->version < 5)
6535 {
6536 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6537 info_ptr += 1;
6538 }
6539 signed_addr = bfd_get_sign_extend_vma (abfd);
6540 if (signed_addr < 0)
6541 internal_error (__FILE__, __LINE__,
6542 _("read_comp_unit_head: dwarf from non elf file"));
6543 cu_header->signed_addr_p = signed_addr;
6544
6545 bool header_has_signature = section_kind == rcuh_kind::TYPE
6546 || cu_header->unit_type == DW_UT_skeleton
6547 || cu_header->unit_type == DW_UT_split_compile;
6548
6549 if (header_has_signature)
6550 {
6551 cu_header->signature = read_8_bytes (abfd, info_ptr);
6552 info_ptr += 8;
6553 }
6554
6555 if (section_kind == rcuh_kind::TYPE)
6556 {
6557 LONGEST type_offset;
6558 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6559 info_ptr += bytes_read;
6560 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6561 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6562 error (_("Dwarf Error: Too big type_offset in compilation unit "
6563 "header (is %s) [in module %s]"), plongest (type_offset),
6564 filename);
6565 }
6566
6567 return info_ptr;
6568 }
6569
6570 /* Helper function that returns the proper abbrev section for
6571 THIS_CU. */
6572
6573 static struct dwarf2_section_info *
6574 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6575 {
6576 struct dwarf2_section_info *abbrev;
6577 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6578
6579 if (this_cu->is_dwz)
6580 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6581 else
6582 abbrev = &dwarf2_per_objfile->abbrev;
6583
6584 return abbrev;
6585 }
6586
6587 /* Subroutine of read_and_check_comp_unit_head and
6588 read_and_check_type_unit_head to simplify them.
6589 Perform various error checking on the header. */
6590
6591 static void
6592 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6593 struct comp_unit_head *header,
6594 struct dwarf2_section_info *section,
6595 struct dwarf2_section_info *abbrev_section)
6596 {
6597 const char *filename = get_section_file_name (section);
6598
6599 if (to_underlying (header->abbrev_sect_off)
6600 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6601 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6602 "(offset %s + 6) [in module %s]"),
6603 sect_offset_str (header->abbrev_sect_off),
6604 sect_offset_str (header->sect_off),
6605 filename);
6606
6607 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6608 avoid potential 32-bit overflow. */
6609 if (((ULONGEST) header->sect_off + get_cu_length (header))
6610 > section->size)
6611 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6612 "(offset %s + 0) [in module %s]"),
6613 header->length, sect_offset_str (header->sect_off),
6614 filename);
6615 }
6616
6617 /* Read in a CU/TU header and perform some basic error checking.
6618 The contents of the header are stored in HEADER.
6619 The result is a pointer to the start of the first DIE. */
6620
6621 static const gdb_byte *
6622 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6623 struct comp_unit_head *header,
6624 struct dwarf2_section_info *section,
6625 struct dwarf2_section_info *abbrev_section,
6626 const gdb_byte *info_ptr,
6627 rcuh_kind section_kind)
6628 {
6629 const gdb_byte *beg_of_comp_unit = info_ptr;
6630
6631 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6632
6633 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6634
6635 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6636
6637 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6638 abbrev_section);
6639
6640 return info_ptr;
6641 }
6642
6643 /* Fetch the abbreviation table offset from a comp or type unit header. */
6644
6645 static sect_offset
6646 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6647 struct dwarf2_section_info *section,
6648 sect_offset sect_off)
6649 {
6650 bfd *abfd = get_section_bfd_owner (section);
6651 const gdb_byte *info_ptr;
6652 unsigned int initial_length_size, offset_size;
6653 uint16_t version;
6654
6655 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6656 info_ptr = section->buffer + to_underlying (sect_off);
6657 read_initial_length (abfd, info_ptr, &initial_length_size);
6658 offset_size = initial_length_size == 4 ? 4 : 8;
6659 info_ptr += initial_length_size;
6660
6661 version = read_2_bytes (abfd, info_ptr);
6662 info_ptr += 2;
6663 if (version >= 5)
6664 {
6665 /* Skip unit type and address size. */
6666 info_ptr += 2;
6667 }
6668
6669 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6670 }
6671
6672 /* Allocate a new partial symtab for file named NAME and mark this new
6673 partial symtab as being an include of PST. */
6674
6675 static void
6676 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6677 struct objfile *objfile)
6678 {
6679 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
6680
6681 if (!IS_ABSOLUTE_PATH (subpst->filename))
6682 {
6683 /* It shares objfile->objfile_obstack. */
6684 subpst->dirname = pst->dirname;
6685 }
6686
6687 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6688 subpst->dependencies[0] = pst;
6689 subpst->number_of_dependencies = 1;
6690
6691 /* No private part is necessary for include psymtabs. This property
6692 can be used to differentiate between such include psymtabs and
6693 the regular ones. */
6694 subpst->per_cu_data = nullptr;
6695 }
6696
6697 /* Read the Line Number Program data and extract the list of files
6698 included by the source file represented by PST. Build an include
6699 partial symtab for each of these included files. */
6700
6701 static void
6702 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6703 struct die_info *die,
6704 dwarf2_psymtab *pst)
6705 {
6706 line_header_up lh;
6707 struct attribute *attr;
6708
6709 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6710 if (attr != nullptr)
6711 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6712 if (lh == NULL)
6713 return; /* No linetable, so no includes. */
6714
6715 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6716 that we pass in the raw text_low here; that is ok because we're
6717 only decoding the line table to make include partial symtabs, and
6718 so the addresses aren't really used. */
6719 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6720 pst->raw_text_low (), 1);
6721 }
6722
6723 static hashval_t
6724 hash_signatured_type (const void *item)
6725 {
6726 const struct signatured_type *sig_type
6727 = (const struct signatured_type *) item;
6728
6729 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6730 return sig_type->signature;
6731 }
6732
6733 static int
6734 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6735 {
6736 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6737 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6738
6739 return lhs->signature == rhs->signature;
6740 }
6741
6742 /* Allocate a hash table for signatured types. */
6743
6744 static htab_t
6745 allocate_signatured_type_table (struct objfile *objfile)
6746 {
6747 return htab_create_alloc_ex (41,
6748 hash_signatured_type,
6749 eq_signatured_type,
6750 NULL,
6751 &objfile->objfile_obstack,
6752 hashtab_obstack_allocate,
6753 dummy_obstack_deallocate);
6754 }
6755
6756 /* A helper function to add a signatured type CU to a table. */
6757
6758 static int
6759 add_signatured_type_cu_to_table (void **slot, void *datum)
6760 {
6761 struct signatured_type *sigt = (struct signatured_type *) *slot;
6762 std::vector<signatured_type *> *all_type_units
6763 = (std::vector<signatured_type *> *) datum;
6764
6765 all_type_units->push_back (sigt);
6766
6767 return 1;
6768 }
6769
6770 /* A helper for create_debug_types_hash_table. Read types from SECTION
6771 and fill them into TYPES_HTAB. It will process only type units,
6772 therefore DW_UT_type. */
6773
6774 static void
6775 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6776 struct dwo_file *dwo_file,
6777 dwarf2_section_info *section, htab_t &types_htab,
6778 rcuh_kind section_kind)
6779 {
6780 struct objfile *objfile = dwarf2_per_objfile->objfile;
6781 struct dwarf2_section_info *abbrev_section;
6782 bfd *abfd;
6783 const gdb_byte *info_ptr, *end_ptr;
6784
6785 abbrev_section = (dwo_file != NULL
6786 ? &dwo_file->sections.abbrev
6787 : &dwarf2_per_objfile->abbrev);
6788
6789 if (dwarf_read_debug)
6790 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6791 get_section_name (section),
6792 get_section_file_name (abbrev_section));
6793
6794 dwarf2_read_section (objfile, section);
6795 info_ptr = section->buffer;
6796
6797 if (info_ptr == NULL)
6798 return;
6799
6800 /* We can't set abfd until now because the section may be empty or
6801 not present, in which case the bfd is unknown. */
6802 abfd = get_section_bfd_owner (section);
6803
6804 /* We don't use cutu_reader here because we don't need to read
6805 any dies: the signature is in the header. */
6806
6807 end_ptr = info_ptr + section->size;
6808 while (info_ptr < end_ptr)
6809 {
6810 struct signatured_type *sig_type;
6811 struct dwo_unit *dwo_tu;
6812 void **slot;
6813 const gdb_byte *ptr = info_ptr;
6814 struct comp_unit_head header;
6815 unsigned int length;
6816
6817 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6818
6819 /* Initialize it due to a false compiler warning. */
6820 header.signature = -1;
6821 header.type_cu_offset_in_tu = (cu_offset) -1;
6822
6823 /* We need to read the type's signature in order to build the hash
6824 table, but we don't need anything else just yet. */
6825
6826 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6827 abbrev_section, ptr, section_kind);
6828
6829 length = get_cu_length (&header);
6830
6831 /* Skip dummy type units. */
6832 if (ptr >= info_ptr + length
6833 || peek_abbrev_code (abfd, ptr) == 0
6834 || header.unit_type != DW_UT_type)
6835 {
6836 info_ptr += length;
6837 continue;
6838 }
6839
6840 if (types_htab == NULL)
6841 {
6842 if (dwo_file)
6843 types_htab = allocate_dwo_unit_table (objfile);
6844 else
6845 types_htab = allocate_signatured_type_table (objfile);
6846 }
6847
6848 if (dwo_file)
6849 {
6850 sig_type = NULL;
6851 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6852 struct dwo_unit);
6853 dwo_tu->dwo_file = dwo_file;
6854 dwo_tu->signature = header.signature;
6855 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6856 dwo_tu->section = section;
6857 dwo_tu->sect_off = sect_off;
6858 dwo_tu->length = length;
6859 }
6860 else
6861 {
6862 /* N.B.: type_offset is not usable if this type uses a DWO file.
6863 The real type_offset is in the DWO file. */
6864 dwo_tu = NULL;
6865 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6866 struct signatured_type);
6867 sig_type->signature = header.signature;
6868 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6869 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6870 sig_type->per_cu.is_debug_types = 1;
6871 sig_type->per_cu.section = section;
6872 sig_type->per_cu.sect_off = sect_off;
6873 sig_type->per_cu.length = length;
6874 }
6875
6876 slot = htab_find_slot (types_htab,
6877 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6878 INSERT);
6879 gdb_assert (slot != NULL);
6880 if (*slot != NULL)
6881 {
6882 sect_offset dup_sect_off;
6883
6884 if (dwo_file)
6885 {
6886 const struct dwo_unit *dup_tu
6887 = (const struct dwo_unit *) *slot;
6888
6889 dup_sect_off = dup_tu->sect_off;
6890 }
6891 else
6892 {
6893 const struct signatured_type *dup_tu
6894 = (const struct signatured_type *) *slot;
6895
6896 dup_sect_off = dup_tu->per_cu.sect_off;
6897 }
6898
6899 complaint (_("debug type entry at offset %s is duplicate to"
6900 " the entry at offset %s, signature %s"),
6901 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6902 hex_string (header.signature));
6903 }
6904 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6905
6906 if (dwarf_read_debug > 1)
6907 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6908 sect_offset_str (sect_off),
6909 hex_string (header.signature));
6910
6911 info_ptr += length;
6912 }
6913 }
6914
6915 /* Create the hash table of all entries in the .debug_types
6916 (or .debug_types.dwo) section(s).
6917 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6918 otherwise it is NULL.
6919
6920 The result is a pointer to the hash table or NULL if there are no types.
6921
6922 Note: This function processes DWO files only, not DWP files. */
6923
6924 static void
6925 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6926 struct dwo_file *dwo_file,
6927 gdb::array_view<dwarf2_section_info> type_sections,
6928 htab_t &types_htab)
6929 {
6930 for (dwarf2_section_info &section : type_sections)
6931 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6932 types_htab, rcuh_kind::TYPE);
6933 }
6934
6935 /* Create the hash table of all entries in the .debug_types section,
6936 and initialize all_type_units.
6937 The result is zero if there is an error (e.g. missing .debug_types section),
6938 otherwise non-zero. */
6939
6940 static int
6941 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6942 {
6943 htab_t types_htab = NULL;
6944
6945 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6946 &dwarf2_per_objfile->info, types_htab,
6947 rcuh_kind::COMPILE);
6948 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6949 dwarf2_per_objfile->types, types_htab);
6950 if (types_htab == NULL)
6951 {
6952 dwarf2_per_objfile->signatured_types = NULL;
6953 return 0;
6954 }
6955
6956 dwarf2_per_objfile->signatured_types = types_htab;
6957
6958 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6959 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6960
6961 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6962 &dwarf2_per_objfile->all_type_units);
6963
6964 return 1;
6965 }
6966
6967 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6968 If SLOT is non-NULL, it is the entry to use in the hash table.
6969 Otherwise we find one. */
6970
6971 static struct signatured_type *
6972 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6973 void **slot)
6974 {
6975 struct objfile *objfile = dwarf2_per_objfile->objfile;
6976
6977 if (dwarf2_per_objfile->all_type_units.size ()
6978 == dwarf2_per_objfile->all_type_units.capacity ())
6979 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6980
6981 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6982 struct signatured_type);
6983
6984 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6985 sig_type->signature = sig;
6986 sig_type->per_cu.is_debug_types = 1;
6987 if (dwarf2_per_objfile->using_index)
6988 {
6989 sig_type->per_cu.v.quick =
6990 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6991 struct dwarf2_per_cu_quick_data);
6992 }
6993
6994 if (slot == NULL)
6995 {
6996 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6997 sig_type, INSERT);
6998 }
6999 gdb_assert (*slot == NULL);
7000 *slot = sig_type;
7001 /* The rest of sig_type must be filled in by the caller. */
7002 return sig_type;
7003 }
7004
7005 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7006 Fill in SIG_ENTRY with DWO_ENTRY. */
7007
7008 static void
7009 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
7010 struct signatured_type *sig_entry,
7011 struct dwo_unit *dwo_entry)
7012 {
7013 /* Make sure we're not clobbering something we don't expect to. */
7014 gdb_assert (! sig_entry->per_cu.queued);
7015 gdb_assert (sig_entry->per_cu.cu == NULL);
7016 if (dwarf2_per_objfile->using_index)
7017 {
7018 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7019 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7020 }
7021 else
7022 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7023 gdb_assert (sig_entry->signature == dwo_entry->signature);
7024 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7025 gdb_assert (sig_entry->type_unit_group == NULL);
7026 gdb_assert (sig_entry->dwo_unit == NULL);
7027
7028 sig_entry->per_cu.section = dwo_entry->section;
7029 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7030 sig_entry->per_cu.length = dwo_entry->length;
7031 sig_entry->per_cu.reading_dwo_directly = 1;
7032 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7033 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7034 sig_entry->dwo_unit = dwo_entry;
7035 }
7036
7037 /* Subroutine of lookup_signatured_type.
7038 If we haven't read the TU yet, create the signatured_type data structure
7039 for a TU to be read in directly from a DWO file, bypassing the stub.
7040 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7041 using .gdb_index, then when reading a CU we want to stay in the DWO file
7042 containing that CU. Otherwise we could end up reading several other DWO
7043 files (due to comdat folding) to process the transitive closure of all the
7044 mentioned TUs, and that can be slow. The current DWO file will have every
7045 type signature that it needs.
7046 We only do this for .gdb_index because in the psymtab case we already have
7047 to read all the DWOs to build the type unit groups. */
7048
7049 static struct signatured_type *
7050 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7051 {
7052 struct dwarf2_per_objfile *dwarf2_per_objfile
7053 = cu->per_cu->dwarf2_per_objfile;
7054 struct objfile *objfile = dwarf2_per_objfile->objfile;
7055 struct dwo_file *dwo_file;
7056 struct dwo_unit find_dwo_entry, *dwo_entry;
7057 struct signatured_type find_sig_entry, *sig_entry;
7058 void **slot;
7059
7060 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7061
7062 /* If TU skeletons have been removed then we may not have read in any
7063 TUs yet. */
7064 if (dwarf2_per_objfile->signatured_types == NULL)
7065 {
7066 dwarf2_per_objfile->signatured_types
7067 = allocate_signatured_type_table (objfile);
7068 }
7069
7070 /* We only ever need to read in one copy of a signatured type.
7071 Use the global signatured_types array to do our own comdat-folding
7072 of types. If this is the first time we're reading this TU, and
7073 the TU has an entry in .gdb_index, replace the recorded data from
7074 .gdb_index with this TU. */
7075
7076 find_sig_entry.signature = sig;
7077 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7078 &find_sig_entry, INSERT);
7079 sig_entry = (struct signatured_type *) *slot;
7080
7081 /* We can get here with the TU already read, *or* in the process of being
7082 read. Don't reassign the global entry to point to this DWO if that's
7083 the case. Also note that if the TU is already being read, it may not
7084 have come from a DWO, the program may be a mix of Fission-compiled
7085 code and non-Fission-compiled code. */
7086
7087 /* Have we already tried to read this TU?
7088 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7089 needn't exist in the global table yet). */
7090 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7091 return sig_entry;
7092
7093 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7094 dwo_unit of the TU itself. */
7095 dwo_file = cu->dwo_unit->dwo_file;
7096
7097 /* Ok, this is the first time we're reading this TU. */
7098 if (dwo_file->tus == NULL)
7099 return NULL;
7100 find_dwo_entry.signature = sig;
7101 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7102 if (dwo_entry == NULL)
7103 return NULL;
7104
7105 /* If the global table doesn't have an entry for this TU, add one. */
7106 if (sig_entry == NULL)
7107 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7108
7109 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7110 sig_entry->per_cu.tu_read = 1;
7111 return sig_entry;
7112 }
7113
7114 /* Subroutine of lookup_signatured_type.
7115 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7116 then try the DWP file. If the TU stub (skeleton) has been removed then
7117 it won't be in .gdb_index. */
7118
7119 static struct signatured_type *
7120 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7121 {
7122 struct dwarf2_per_objfile *dwarf2_per_objfile
7123 = cu->per_cu->dwarf2_per_objfile;
7124 struct objfile *objfile = dwarf2_per_objfile->objfile;
7125 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7126 struct dwo_unit *dwo_entry;
7127 struct signatured_type find_sig_entry, *sig_entry;
7128 void **slot;
7129
7130 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7131 gdb_assert (dwp_file != NULL);
7132
7133 /* If TU skeletons have been removed then we may not have read in any
7134 TUs yet. */
7135 if (dwarf2_per_objfile->signatured_types == NULL)
7136 {
7137 dwarf2_per_objfile->signatured_types
7138 = allocate_signatured_type_table (objfile);
7139 }
7140
7141 find_sig_entry.signature = sig;
7142 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7143 &find_sig_entry, INSERT);
7144 sig_entry = (struct signatured_type *) *slot;
7145
7146 /* Have we already tried to read this TU?
7147 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7148 needn't exist in the global table yet). */
7149 if (sig_entry != NULL)
7150 return sig_entry;
7151
7152 if (dwp_file->tus == NULL)
7153 return NULL;
7154 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7155 sig, 1 /* is_debug_types */);
7156 if (dwo_entry == NULL)
7157 return NULL;
7158
7159 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7160 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7161
7162 return sig_entry;
7163 }
7164
7165 /* Lookup a signature based type for DW_FORM_ref_sig8.
7166 Returns NULL if signature SIG is not present in the table.
7167 It is up to the caller to complain about this. */
7168
7169 static struct signatured_type *
7170 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7171 {
7172 struct dwarf2_per_objfile *dwarf2_per_objfile
7173 = cu->per_cu->dwarf2_per_objfile;
7174
7175 if (cu->dwo_unit
7176 && dwarf2_per_objfile->using_index)
7177 {
7178 /* We're in a DWO/DWP file, and we're using .gdb_index.
7179 These cases require special processing. */
7180 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7181 return lookup_dwo_signatured_type (cu, sig);
7182 else
7183 return lookup_dwp_signatured_type (cu, sig);
7184 }
7185 else
7186 {
7187 struct signatured_type find_entry, *entry;
7188
7189 if (dwarf2_per_objfile->signatured_types == NULL)
7190 return NULL;
7191 find_entry.signature = sig;
7192 entry = ((struct signatured_type *)
7193 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7194 return entry;
7195 }
7196 }
7197
7198 /* Return the address base of the compile unit, which, if exists, is stored
7199 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
7200 static gdb::optional<ULONGEST>
7201 lookup_addr_base (struct die_info *comp_unit_die)
7202 {
7203 struct attribute *attr;
7204 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
7205 if (attr == nullptr)
7206 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
7207 if (attr == nullptr)
7208 return gdb::optional<ULONGEST> ();
7209 return DW_UNSND (attr);
7210 }
7211
7212 /* Return range lists base of the compile unit, which, if exists, is stored
7213 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
7214 static ULONGEST
7215 lookup_ranges_base (struct die_info *comp_unit_die)
7216 {
7217 struct attribute *attr;
7218 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
7219 if (attr == nullptr)
7220 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
7221 if (attr == nullptr)
7222 return 0;
7223 return DW_UNSND (attr);
7224 }
7225
7226 /* Low level DIE reading support. */
7227
7228 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7229
7230 static void
7231 init_cu_die_reader (struct die_reader_specs *reader,
7232 struct dwarf2_cu *cu,
7233 struct dwarf2_section_info *section,
7234 struct dwo_file *dwo_file,
7235 struct abbrev_table *abbrev_table)
7236 {
7237 gdb_assert (section->readin && section->buffer != NULL);
7238 reader->abfd = get_section_bfd_owner (section);
7239 reader->cu = cu;
7240 reader->dwo_file = dwo_file;
7241 reader->die_section = section;
7242 reader->buffer = section->buffer;
7243 reader->buffer_end = section->buffer + section->size;
7244 reader->comp_dir = NULL;
7245 reader->abbrev_table = abbrev_table;
7246 }
7247
7248 /* Subroutine of cutu_reader to simplify it.
7249 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7250 There's just a lot of work to do, and cutu_reader is big enough
7251 already.
7252
7253 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7254 from it to the DIE in the DWO. If NULL we are skipping the stub.
7255 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7256 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7257 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7258 STUB_COMP_DIR may be non-NULL.
7259 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7260 are filled in with the info of the DIE from the DWO file.
7261 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7262 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7263 kept around for at least as long as *RESULT_READER.
7264
7265 The result is non-zero if a valid (non-dummy) DIE was found. */
7266
7267 static int
7268 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7269 struct dwo_unit *dwo_unit,
7270 struct die_info *stub_comp_unit_die,
7271 const char *stub_comp_dir,
7272 struct die_reader_specs *result_reader,
7273 const gdb_byte **result_info_ptr,
7274 struct die_info **result_comp_unit_die,
7275 int *result_has_children,
7276 abbrev_table_up *result_dwo_abbrev_table)
7277 {
7278 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7279 struct objfile *objfile = dwarf2_per_objfile->objfile;
7280 struct dwarf2_cu *cu = this_cu->cu;
7281 bfd *abfd;
7282 const gdb_byte *begin_info_ptr, *info_ptr;
7283 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7284 int i,num_extra_attrs;
7285 struct dwarf2_section_info *dwo_abbrev_section;
7286 struct die_info *comp_unit_die;
7287
7288 /* At most one of these may be provided. */
7289 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7290
7291 /* These attributes aren't processed until later:
7292 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7293 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7294 referenced later. However, these attributes are found in the stub
7295 which we won't have later. In order to not impose this complication
7296 on the rest of the code, we read them here and copy them to the
7297 DWO CU/TU die. */
7298
7299 stmt_list = NULL;
7300 low_pc = NULL;
7301 high_pc = NULL;
7302 ranges = NULL;
7303 comp_dir = NULL;
7304
7305 if (stub_comp_unit_die != NULL)
7306 {
7307 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7308 DWO file. */
7309 if (! this_cu->is_debug_types)
7310 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7311 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7312 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7313 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7314 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7315
7316 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
7317
7318 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
7319 here (if needed). We need the value before we can process
7320 DW_AT_ranges. */
7321 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
7322 }
7323 else if (stub_comp_dir != NULL)
7324 {
7325 /* Reconstruct the comp_dir attribute to simplify the code below. */
7326 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7327 comp_dir->name = DW_AT_comp_dir;
7328 comp_dir->form = DW_FORM_string;
7329 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7330 DW_STRING (comp_dir) = stub_comp_dir;
7331 }
7332
7333 /* Set up for reading the DWO CU/TU. */
7334 cu->dwo_unit = dwo_unit;
7335 dwarf2_section_info *section = dwo_unit->section;
7336 dwarf2_read_section (objfile, section);
7337 abfd = get_section_bfd_owner (section);
7338 begin_info_ptr = info_ptr = (section->buffer
7339 + to_underlying (dwo_unit->sect_off));
7340 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7341
7342 if (this_cu->is_debug_types)
7343 {
7344 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7345
7346 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7347 &cu->header, section,
7348 dwo_abbrev_section,
7349 info_ptr, rcuh_kind::TYPE);
7350 /* This is not an assert because it can be caused by bad debug info. */
7351 if (sig_type->signature != cu->header.signature)
7352 {
7353 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7354 " TU at offset %s [in module %s]"),
7355 hex_string (sig_type->signature),
7356 hex_string (cu->header.signature),
7357 sect_offset_str (dwo_unit->sect_off),
7358 bfd_get_filename (abfd));
7359 }
7360 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7361 /* For DWOs coming from DWP files, we don't know the CU length
7362 nor the type's offset in the TU until now. */
7363 dwo_unit->length = get_cu_length (&cu->header);
7364 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7365
7366 /* Establish the type offset that can be used to lookup the type.
7367 For DWO files, we don't know it until now. */
7368 sig_type->type_offset_in_section
7369 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7370 }
7371 else
7372 {
7373 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7374 &cu->header, section,
7375 dwo_abbrev_section,
7376 info_ptr, rcuh_kind::COMPILE);
7377 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7378 /* For DWOs coming from DWP files, we don't know the CU length
7379 until now. */
7380 dwo_unit->length = get_cu_length (&cu->header);
7381 }
7382
7383 *result_dwo_abbrev_table
7384 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7385 cu->header.abbrev_sect_off);
7386 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7387 result_dwo_abbrev_table->get ());
7388
7389 /* Read in the die, but leave space to copy over the attributes
7390 from the stub. This has the benefit of simplifying the rest of
7391 the code - all the work to maintain the illusion of a single
7392 DW_TAG_{compile,type}_unit DIE is done here. */
7393 num_extra_attrs = ((stmt_list != NULL)
7394 + (low_pc != NULL)
7395 + (high_pc != NULL)
7396 + (ranges != NULL)
7397 + (comp_dir != NULL));
7398 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7399 result_has_children, num_extra_attrs);
7400
7401 /* Copy over the attributes from the stub to the DIE we just read in. */
7402 comp_unit_die = *result_comp_unit_die;
7403 i = comp_unit_die->num_attrs;
7404 if (stmt_list != NULL)
7405 comp_unit_die->attrs[i++] = *stmt_list;
7406 if (low_pc != NULL)
7407 comp_unit_die->attrs[i++] = *low_pc;
7408 if (high_pc != NULL)
7409 comp_unit_die->attrs[i++] = *high_pc;
7410 if (ranges != NULL)
7411 comp_unit_die->attrs[i++] = *ranges;
7412 if (comp_dir != NULL)
7413 comp_unit_die->attrs[i++] = *comp_dir;
7414 comp_unit_die->num_attrs += num_extra_attrs;
7415
7416 if (dwarf_die_debug)
7417 {
7418 fprintf_unfiltered (gdb_stdlog,
7419 "Read die from %s@0x%x of %s:\n",
7420 get_section_name (section),
7421 (unsigned) (begin_info_ptr - section->buffer),
7422 bfd_get_filename (abfd));
7423 dump_die (comp_unit_die, dwarf_die_debug);
7424 }
7425
7426 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7427 TUs by skipping the stub and going directly to the entry in the DWO file.
7428 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7429 to get it via circuitous means. Blech. */
7430 if (comp_dir != NULL)
7431 result_reader->comp_dir = DW_STRING (comp_dir);
7432
7433 /* Skip dummy compilation units. */
7434 if (info_ptr >= begin_info_ptr + dwo_unit->length
7435 || peek_abbrev_code (abfd, info_ptr) == 0)
7436 return 0;
7437
7438 *result_info_ptr = info_ptr;
7439 return 1;
7440 }
7441
7442 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7443 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7444 signature is part of the header. */
7445 static gdb::optional<ULONGEST>
7446 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7447 {
7448 if (cu->header.version >= 5)
7449 return cu->header.signature;
7450 struct attribute *attr;
7451 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7452 if (attr == nullptr)
7453 return gdb::optional<ULONGEST> ();
7454 return DW_UNSND (attr);
7455 }
7456
7457 /* Subroutine of cutu_reader to simplify it.
7458 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7459 Returns NULL if the specified DWO unit cannot be found. */
7460
7461 static struct dwo_unit *
7462 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7463 struct die_info *comp_unit_die,
7464 const char *dwo_name)
7465 {
7466 struct dwarf2_cu *cu = this_cu->cu;
7467 struct dwo_unit *dwo_unit;
7468 const char *comp_dir;
7469
7470 gdb_assert (cu != NULL);
7471
7472 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7473 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7474 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7475
7476 if (this_cu->is_debug_types)
7477 {
7478 struct signatured_type *sig_type;
7479
7480 /* Since this_cu is the first member of struct signatured_type,
7481 we can go from a pointer to one to a pointer to the other. */
7482 sig_type = (struct signatured_type *) this_cu;
7483 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7484 }
7485 else
7486 {
7487 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7488 if (!signature.has_value ())
7489 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7490 " [in module %s]"),
7491 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7492 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7493 *signature);
7494 }
7495
7496 return dwo_unit;
7497 }
7498
7499 /* Subroutine of cutu_reader to simplify it.
7500 See it for a description of the parameters.
7501 Read a TU directly from a DWO file, bypassing the stub. */
7502
7503 void
7504 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7505 int use_existing_cu, int keep)
7506 {
7507 struct signatured_type *sig_type;
7508 struct die_reader_specs reader;
7509
7510 /* Verify we can do the following downcast, and that we have the
7511 data we need. */
7512 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7513 sig_type = (struct signatured_type *) this_cu;
7514 gdb_assert (sig_type->dwo_unit != NULL);
7515
7516 if (use_existing_cu && this_cu->cu != NULL)
7517 {
7518 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7519 /* There's no need to do the rereading_dwo_cu handling that
7520 cutu_reader does since we don't read the stub. */
7521 }
7522 else
7523 {
7524 /* If !use_existing_cu, this_cu->cu must be NULL. */
7525 gdb_assert (this_cu->cu == NULL);
7526 m_new_cu.reset (new dwarf2_cu (this_cu));
7527 }
7528
7529 /* A future optimization, if needed, would be to use an existing
7530 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7531 could share abbrev tables. */
7532
7533 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7534 NULL /* stub_comp_unit_die */,
7535 sig_type->dwo_unit->dwo_file->comp_dir,
7536 &reader, &info_ptr,
7537 &comp_unit_die, &has_children,
7538 &m_dwo_abbrev_table) == 0)
7539 {
7540 /* Dummy die. */
7541 dummy_p = true;
7542 }
7543 }
7544
7545 /* Initialize a CU (or TU) and read its DIEs.
7546 If the CU defers to a DWO file, read the DWO file as well.
7547
7548 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7549 Otherwise the table specified in the comp unit header is read in and used.
7550 This is an optimization for when we already have the abbrev table.
7551
7552 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7553 Otherwise, a new CU is allocated with xmalloc.
7554
7555 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7556 read_in_chain. Otherwise the dwarf2_cu data is freed at the
7557 end. */
7558
7559 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7560 struct abbrev_table *abbrev_table,
7561 int use_existing_cu, int keep,
7562 bool skip_partial)
7563 : die_reader_specs {},
7564 m_this_cu (this_cu),
7565 m_keep (keep)
7566 {
7567 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7568 struct objfile *objfile = dwarf2_per_objfile->objfile;
7569 struct dwarf2_section_info *section = this_cu->section;
7570 bfd *abfd = get_section_bfd_owner (section);
7571 struct dwarf2_cu *cu;
7572 const gdb_byte *begin_info_ptr;
7573 struct signatured_type *sig_type = NULL;
7574 struct dwarf2_section_info *abbrev_section;
7575 /* Non-zero if CU currently points to a DWO file and we need to
7576 reread it. When this happens we need to reread the skeleton die
7577 before we can reread the DWO file (this only applies to CUs, not TUs). */
7578 int rereading_dwo_cu = 0;
7579
7580 if (dwarf_die_debug)
7581 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7582 this_cu->is_debug_types ? "type" : "comp",
7583 sect_offset_str (this_cu->sect_off));
7584
7585 if (use_existing_cu)
7586 gdb_assert (keep);
7587
7588 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7589 file (instead of going through the stub), short-circuit all of this. */
7590 if (this_cu->reading_dwo_directly)
7591 {
7592 /* Narrow down the scope of possibilities to have to understand. */
7593 gdb_assert (this_cu->is_debug_types);
7594 gdb_assert (abbrev_table == NULL);
7595 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep);
7596 return;
7597 }
7598
7599 /* This is cheap if the section is already read in. */
7600 dwarf2_read_section (objfile, section);
7601
7602 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7603
7604 abbrev_section = get_abbrev_section_for_cu (this_cu);
7605
7606 if (use_existing_cu && this_cu->cu != NULL)
7607 {
7608 cu = this_cu->cu;
7609 /* If this CU is from a DWO file we need to start over, we need to
7610 refetch the attributes from the skeleton CU.
7611 This could be optimized by retrieving those attributes from when we
7612 were here the first time: the previous comp_unit_die was stored in
7613 comp_unit_obstack. But there's no data yet that we need this
7614 optimization. */
7615 if (cu->dwo_unit != NULL)
7616 rereading_dwo_cu = 1;
7617 }
7618 else
7619 {
7620 /* If !use_existing_cu, this_cu->cu must be NULL. */
7621 gdb_assert (this_cu->cu == NULL);
7622 m_new_cu.reset (new dwarf2_cu (this_cu));
7623 cu = m_new_cu.get ();
7624 }
7625
7626 /* Get the header. */
7627 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7628 {
7629 /* We already have the header, there's no need to read it in again. */
7630 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7631 }
7632 else
7633 {
7634 if (this_cu->is_debug_types)
7635 {
7636 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7637 &cu->header, section,
7638 abbrev_section, info_ptr,
7639 rcuh_kind::TYPE);
7640
7641 /* Since per_cu is the first member of struct signatured_type,
7642 we can go from a pointer to one to a pointer to the other. */
7643 sig_type = (struct signatured_type *) this_cu;
7644 gdb_assert (sig_type->signature == cu->header.signature);
7645 gdb_assert (sig_type->type_offset_in_tu
7646 == cu->header.type_cu_offset_in_tu);
7647 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7648
7649 /* LENGTH has not been set yet for type units if we're
7650 using .gdb_index. */
7651 this_cu->length = get_cu_length (&cu->header);
7652
7653 /* Establish the type offset that can be used to lookup the type. */
7654 sig_type->type_offset_in_section =
7655 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7656
7657 this_cu->dwarf_version = cu->header.version;
7658 }
7659 else
7660 {
7661 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7662 &cu->header, section,
7663 abbrev_section,
7664 info_ptr,
7665 rcuh_kind::COMPILE);
7666
7667 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7668 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7669 this_cu->dwarf_version = cu->header.version;
7670 }
7671 }
7672
7673 /* Skip dummy compilation units. */
7674 if (info_ptr >= begin_info_ptr + this_cu->length
7675 || peek_abbrev_code (abfd, info_ptr) == 0)
7676 {
7677 dummy_p = true;
7678 return;
7679 }
7680
7681 /* If we don't have them yet, read the abbrevs for this compilation unit.
7682 And if we need to read them now, make sure they're freed when we're
7683 done. */
7684 if (abbrev_table != NULL)
7685 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7686 else
7687 {
7688 m_abbrev_table_holder
7689 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7690 cu->header.abbrev_sect_off);
7691 abbrev_table = m_abbrev_table_holder.get ();
7692 }
7693
7694 /* Read the top level CU/TU die. */
7695 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7696 info_ptr = read_full_die (this, &comp_unit_die, info_ptr, &has_children);
7697
7698 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7699 {
7700 dummy_p = true;
7701 return;
7702 }
7703
7704 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7705 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7706 table from the DWO file and pass the ownership over to us. It will be
7707 referenced from READER, so we must make sure to free it after we're done
7708 with READER.
7709
7710 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7711 DWO CU, that this test will fail (the attribute will not be present). */
7712 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7713 if (dwo_name != nullptr)
7714 {
7715 struct dwo_unit *dwo_unit;
7716 struct die_info *dwo_comp_unit_die;
7717
7718 if (has_children)
7719 {
7720 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7721 " has children (offset %s) [in module %s]"),
7722 sect_offset_str (this_cu->sect_off),
7723 bfd_get_filename (abfd));
7724 }
7725 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
7726 if (dwo_unit != NULL)
7727 {
7728 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7729 comp_unit_die, NULL,
7730 this, &info_ptr,
7731 &dwo_comp_unit_die, &has_children,
7732 &m_dwo_abbrev_table) == 0)
7733 {
7734 /* Dummy die. */
7735 dummy_p = true;
7736 return;
7737 }
7738 comp_unit_die = dwo_comp_unit_die;
7739 }
7740 else
7741 {
7742 /* Yikes, we couldn't find the rest of the DIE, we only have
7743 the stub. A complaint has already been logged. There's
7744 not much more we can do except pass on the stub DIE to
7745 die_reader_func. We don't want to throw an error on bad
7746 debug info. */
7747 }
7748 }
7749 }
7750
7751 cutu_reader::~cutu_reader ()
7752 {
7753 /* Done, clean up. */
7754 if (m_new_cu != NULL && m_keep && !dummy_p)
7755 {
7756 struct dwarf2_per_objfile *dwarf2_per_objfile
7757 = m_this_cu->dwarf2_per_objfile;
7758 /* Link this CU into read_in_chain. */
7759 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7760 dwarf2_per_objfile->read_in_chain = m_this_cu;
7761 /* The chain owns it now. */
7762 m_new_cu.release ();
7763 }
7764 }
7765
7766 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7767 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7768 assumed to have already done the lookup to find the DWO file).
7769
7770 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7771 THIS_CU->is_debug_types, but nothing else.
7772
7773 We fill in THIS_CU->length.
7774
7775 THIS_CU->cu is always freed when done.
7776 This is done in order to not leave THIS_CU->cu in a state where we have
7777 to care whether it refers to the "main" CU or the DWO CU.
7778
7779 When parent_cu is passed, it is used to provide a default value for
7780 str_offsets_base and addr_base from the parent. */
7781
7782 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7783 struct dwarf2_cu *parent_cu,
7784 struct dwo_file *dwo_file)
7785 : die_reader_specs {},
7786 m_this_cu (this_cu)
7787 {
7788 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7789 struct objfile *objfile = dwarf2_per_objfile->objfile;
7790 struct dwarf2_section_info *section = this_cu->section;
7791 bfd *abfd = get_section_bfd_owner (section);
7792 struct dwarf2_section_info *abbrev_section;
7793 const gdb_byte *begin_info_ptr, *info_ptr;
7794 int has_children;
7795
7796 if (dwarf_die_debug)
7797 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7798 this_cu->is_debug_types ? "type" : "comp",
7799 sect_offset_str (this_cu->sect_off));
7800
7801 gdb_assert (this_cu->cu == NULL);
7802
7803 abbrev_section = (dwo_file != NULL
7804 ? &dwo_file->sections.abbrev
7805 : get_abbrev_section_for_cu (this_cu));
7806
7807 /* This is cheap if the section is already read in. */
7808 dwarf2_read_section (objfile, section);
7809
7810 m_new_cu.reset (new dwarf2_cu (this_cu));
7811
7812 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7813 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7814 &m_new_cu->header, section,
7815 abbrev_section, info_ptr,
7816 (this_cu->is_debug_types
7817 ? rcuh_kind::TYPE
7818 : rcuh_kind::COMPILE));
7819
7820 if (parent_cu != nullptr)
7821 {
7822 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7823 m_new_cu->addr_base = parent_cu->addr_base;
7824 }
7825 this_cu->length = get_cu_length (&m_new_cu->header);
7826
7827 /* Skip dummy compilation units. */
7828 if (info_ptr >= begin_info_ptr + this_cu->length
7829 || peek_abbrev_code (abfd, info_ptr) == 0)
7830 {
7831 dummy_p = true;
7832 return;
7833 }
7834
7835 m_abbrev_table_holder
7836 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7837 m_new_cu->header.abbrev_sect_off);
7838
7839 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7840 m_abbrev_table_holder.get ());
7841 info_ptr = read_full_die (this, &comp_unit_die, info_ptr, &has_children);
7842 }
7843
7844 \f
7845 /* Type Unit Groups.
7846
7847 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7848 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7849 so that all types coming from the same compilation (.o file) are grouped
7850 together. A future step could be to put the types in the same symtab as
7851 the CU the types ultimately came from. */
7852
7853 static hashval_t
7854 hash_type_unit_group (const void *item)
7855 {
7856 const struct type_unit_group *tu_group
7857 = (const struct type_unit_group *) item;
7858
7859 return hash_stmt_list_entry (&tu_group->hash);
7860 }
7861
7862 static int
7863 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7864 {
7865 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7866 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7867
7868 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7869 }
7870
7871 /* Allocate a hash table for type unit groups. */
7872
7873 static htab_t
7874 allocate_type_unit_groups_table (struct objfile *objfile)
7875 {
7876 return htab_create_alloc_ex (3,
7877 hash_type_unit_group,
7878 eq_type_unit_group,
7879 NULL,
7880 &objfile->objfile_obstack,
7881 hashtab_obstack_allocate,
7882 dummy_obstack_deallocate);
7883 }
7884
7885 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7886 partial symtabs. We combine several TUs per psymtab to not let the size
7887 of any one psymtab grow too big. */
7888 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7889 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7890
7891 /* Helper routine for get_type_unit_group.
7892 Create the type_unit_group object used to hold one or more TUs. */
7893
7894 static struct type_unit_group *
7895 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7896 {
7897 struct dwarf2_per_objfile *dwarf2_per_objfile
7898 = cu->per_cu->dwarf2_per_objfile;
7899 struct objfile *objfile = dwarf2_per_objfile->objfile;
7900 struct dwarf2_per_cu_data *per_cu;
7901 struct type_unit_group *tu_group;
7902
7903 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7904 struct type_unit_group);
7905 per_cu = &tu_group->per_cu;
7906 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7907
7908 if (dwarf2_per_objfile->using_index)
7909 {
7910 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7911 struct dwarf2_per_cu_quick_data);
7912 }
7913 else
7914 {
7915 unsigned int line_offset = to_underlying (line_offset_struct);
7916 dwarf2_psymtab *pst;
7917 std::string name;
7918
7919 /* Give the symtab a useful name for debug purposes. */
7920 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7921 name = string_printf ("<type_units_%d>",
7922 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7923 else
7924 name = string_printf ("<type_units_at_0x%x>", line_offset);
7925
7926 pst = create_partial_symtab (per_cu, name.c_str ());
7927 pst->anonymous = true;
7928 }
7929
7930 tu_group->hash.dwo_unit = cu->dwo_unit;
7931 tu_group->hash.line_sect_off = line_offset_struct;
7932
7933 return tu_group;
7934 }
7935
7936 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7937 STMT_LIST is a DW_AT_stmt_list attribute. */
7938
7939 static struct type_unit_group *
7940 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7941 {
7942 struct dwarf2_per_objfile *dwarf2_per_objfile
7943 = cu->per_cu->dwarf2_per_objfile;
7944 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7945 struct type_unit_group *tu_group;
7946 void **slot;
7947 unsigned int line_offset;
7948 struct type_unit_group type_unit_group_for_lookup;
7949
7950 if (dwarf2_per_objfile->type_unit_groups == NULL)
7951 {
7952 dwarf2_per_objfile->type_unit_groups =
7953 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7954 }
7955
7956 /* Do we need to create a new group, or can we use an existing one? */
7957
7958 if (stmt_list)
7959 {
7960 line_offset = DW_UNSND (stmt_list);
7961 ++tu_stats->nr_symtab_sharers;
7962 }
7963 else
7964 {
7965 /* Ugh, no stmt_list. Rare, but we have to handle it.
7966 We can do various things here like create one group per TU or
7967 spread them over multiple groups to split up the expansion work.
7968 To avoid worst case scenarios (too many groups or too large groups)
7969 we, umm, group them in bunches. */
7970 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7971 | (tu_stats->nr_stmt_less_type_units
7972 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7973 ++tu_stats->nr_stmt_less_type_units;
7974 }
7975
7976 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7977 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7978 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7979 &type_unit_group_for_lookup, INSERT);
7980 if (*slot != NULL)
7981 {
7982 tu_group = (struct type_unit_group *) *slot;
7983 gdb_assert (tu_group != NULL);
7984 }
7985 else
7986 {
7987 sect_offset line_offset_struct = (sect_offset) line_offset;
7988 tu_group = create_type_unit_group (cu, line_offset_struct);
7989 *slot = tu_group;
7990 ++tu_stats->nr_symtabs;
7991 }
7992
7993 return tu_group;
7994 }
7995 \f
7996 /* Partial symbol tables. */
7997
7998 /* Create a psymtab named NAME and assign it to PER_CU.
7999
8000 The caller must fill in the following details:
8001 dirname, textlow, texthigh. */
8002
8003 static dwarf2_psymtab *
8004 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8005 {
8006 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
8007 dwarf2_psymtab *pst;
8008
8009 pst = new dwarf2_psymtab (name, objfile, 0);
8010
8011 pst->psymtabs_addrmap_supported = true;
8012
8013 /* This is the glue that links PST into GDB's symbol API. */
8014 pst->per_cu_data = per_cu;
8015 per_cu->v.psymtab = pst;
8016
8017 return pst;
8018 }
8019
8020 /* DIE reader function for process_psymtab_comp_unit. */
8021
8022 static void
8023 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8024 const gdb_byte *info_ptr,
8025 struct die_info *comp_unit_die,
8026 int has_children,
8027 int want_partial_unit,
8028 enum language pretend_language)
8029 {
8030 struct dwarf2_cu *cu = reader->cu;
8031 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8032 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8033 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8034 CORE_ADDR baseaddr;
8035 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8036 dwarf2_psymtab *pst;
8037 enum pc_bounds_kind cu_bounds_kind;
8038 const char *filename;
8039
8040 if (comp_unit_die->tag == DW_TAG_partial_unit && !want_partial_unit)
8041 return;
8042
8043 gdb_assert (! per_cu->is_debug_types);
8044
8045 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
8046
8047 /* Allocate a new partial symbol table structure. */
8048 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8049 if (filename == NULL)
8050 filename = "";
8051
8052 pst = create_partial_symtab (per_cu, filename);
8053
8054 /* This must be done before calling dwarf2_build_include_psymtabs. */
8055 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8056
8057 baseaddr = objfile->text_section_offset ();
8058
8059 dwarf2_find_base_address (comp_unit_die, cu);
8060
8061 /* Possibly set the default values of LOWPC and HIGHPC from
8062 `DW_AT_ranges'. */
8063 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8064 &best_highpc, cu, pst);
8065 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8066 {
8067 CORE_ADDR low
8068 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8069 - baseaddr);
8070 CORE_ADDR high
8071 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8072 - baseaddr - 1);
8073 /* Store the contiguous range if it is not empty; it can be
8074 empty for CUs with no code. */
8075 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8076 low, high, pst);
8077 }
8078
8079 /* Check if comp unit has_children.
8080 If so, read the rest of the partial symbols from this comp unit.
8081 If not, there's no more debug_info for this comp unit. */
8082 if (has_children)
8083 {
8084 struct partial_die_info *first_die;
8085 CORE_ADDR lowpc, highpc;
8086
8087 lowpc = ((CORE_ADDR) -1);
8088 highpc = ((CORE_ADDR) 0);
8089
8090 first_die = load_partial_dies (reader, info_ptr, 1);
8091
8092 scan_partial_symbols (first_die, &lowpc, &highpc,
8093 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8094
8095 /* If we didn't find a lowpc, set it to highpc to avoid
8096 complaints from `maint check'. */
8097 if (lowpc == ((CORE_ADDR) -1))
8098 lowpc = highpc;
8099
8100 /* If the compilation unit didn't have an explicit address range,
8101 then use the information extracted from its child dies. */
8102 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8103 {
8104 best_lowpc = lowpc;
8105 best_highpc = highpc;
8106 }
8107 }
8108 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
8109 best_lowpc + baseaddr)
8110 - baseaddr);
8111 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
8112 best_highpc + baseaddr)
8113 - baseaddr);
8114
8115 end_psymtab_common (objfile, pst);
8116
8117 if (!cu->per_cu->imported_symtabs_empty ())
8118 {
8119 int i;
8120 int len = cu->per_cu->imported_symtabs_size ();
8121
8122 /* Fill in 'dependencies' here; we fill in 'users' in a
8123 post-pass. */
8124 pst->number_of_dependencies = len;
8125 pst->dependencies
8126 = objfile->partial_symtabs->allocate_dependencies (len);
8127 for (i = 0; i < len; ++i)
8128 {
8129 pst->dependencies[i]
8130 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8131 }
8132
8133 cu->per_cu->imported_symtabs_free ();
8134 }
8135
8136 /* Get the list of files included in the current compilation unit,
8137 and build a psymtab for each of them. */
8138 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8139
8140 if (dwarf_read_debug)
8141 fprintf_unfiltered (gdb_stdlog,
8142 "Psymtab for %s unit @%s: %s - %s"
8143 ", %d global, %d static syms\n",
8144 per_cu->is_debug_types ? "type" : "comp",
8145 sect_offset_str (per_cu->sect_off),
8146 paddress (gdbarch, pst->text_low (objfile)),
8147 paddress (gdbarch, pst->text_high (objfile)),
8148 pst->n_global_syms, pst->n_static_syms);
8149 }
8150
8151 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8152 Process compilation unit THIS_CU for a psymtab. */
8153
8154 static void
8155 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8156 int want_partial_unit,
8157 enum language pretend_language)
8158 {
8159 /* If this compilation unit was already read in, free the
8160 cached copy in order to read it in again. This is
8161 necessary because we skipped some symbols when we first
8162 read in the compilation unit (see load_partial_dies).
8163 This problem could be avoided, but the benefit is unclear. */
8164 if (this_cu->cu != NULL)
8165 free_one_cached_comp_unit (this_cu);
8166
8167 cutu_reader reader (this_cu, NULL, 0, 0, false);
8168
8169 if (reader.dummy_p)
8170 {
8171 /* Nothing. */
8172 }
8173 else if (this_cu->is_debug_types)
8174 build_type_psymtabs_reader (&reader, reader.info_ptr, reader.comp_unit_die,
8175 reader.has_children);
8176 else
8177 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
8178 reader.comp_unit_die,
8179 reader.has_children,
8180 want_partial_unit,
8181 pretend_language);
8182
8183 /* Age out any secondary CUs. */
8184 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8185 }
8186
8187 /* Reader function for build_type_psymtabs. */
8188
8189 static void
8190 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8191 const gdb_byte *info_ptr,
8192 struct die_info *type_unit_die,
8193 int has_children)
8194 {
8195 struct dwarf2_per_objfile *dwarf2_per_objfile
8196 = reader->cu->per_cu->dwarf2_per_objfile;
8197 struct objfile *objfile = dwarf2_per_objfile->objfile;
8198 struct dwarf2_cu *cu = reader->cu;
8199 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8200 struct signatured_type *sig_type;
8201 struct type_unit_group *tu_group;
8202 struct attribute *attr;
8203 struct partial_die_info *first_die;
8204 CORE_ADDR lowpc, highpc;
8205 dwarf2_psymtab *pst;
8206
8207 gdb_assert (per_cu->is_debug_types);
8208 sig_type = (struct signatured_type *) per_cu;
8209
8210 if (! has_children)
8211 return;
8212
8213 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8214 tu_group = get_type_unit_group (cu, attr);
8215
8216 if (tu_group->tus == nullptr)
8217 tu_group->tus = new std::vector<signatured_type *>;
8218 tu_group->tus->push_back (sig_type);
8219
8220 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8221 pst = create_partial_symtab (per_cu, "");
8222 pst->anonymous = true;
8223
8224 first_die = load_partial_dies (reader, info_ptr, 1);
8225
8226 lowpc = (CORE_ADDR) -1;
8227 highpc = (CORE_ADDR) 0;
8228 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8229
8230 end_psymtab_common (objfile, pst);
8231 }
8232
8233 /* Struct used to sort TUs by their abbreviation table offset. */
8234
8235 struct tu_abbrev_offset
8236 {
8237 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8238 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8239 {}
8240
8241 signatured_type *sig_type;
8242 sect_offset abbrev_offset;
8243 };
8244
8245 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8246
8247 static bool
8248 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8249 const struct tu_abbrev_offset &b)
8250 {
8251 return a.abbrev_offset < b.abbrev_offset;
8252 }
8253
8254 /* Efficiently read all the type units.
8255 This does the bulk of the work for build_type_psymtabs.
8256
8257 The efficiency is because we sort TUs by the abbrev table they use and
8258 only read each abbrev table once. In one program there are 200K TUs
8259 sharing 8K abbrev tables.
8260
8261 The main purpose of this function is to support building the
8262 dwarf2_per_objfile->type_unit_groups table.
8263 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8264 can collapse the search space by grouping them by stmt_list.
8265 The savings can be significant, in the same program from above the 200K TUs
8266 share 8K stmt_list tables.
8267
8268 FUNC is expected to call get_type_unit_group, which will create the
8269 struct type_unit_group if necessary and add it to
8270 dwarf2_per_objfile->type_unit_groups. */
8271
8272 static void
8273 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8274 {
8275 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8276 abbrev_table_up abbrev_table;
8277 sect_offset abbrev_offset;
8278
8279 /* It's up to the caller to not call us multiple times. */
8280 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8281
8282 if (dwarf2_per_objfile->all_type_units.empty ())
8283 return;
8284
8285 /* TUs typically share abbrev tables, and there can be way more TUs than
8286 abbrev tables. Sort by abbrev table to reduce the number of times we
8287 read each abbrev table in.
8288 Alternatives are to punt or to maintain a cache of abbrev tables.
8289 This is simpler and efficient enough for now.
8290
8291 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8292 symtab to use). Typically TUs with the same abbrev offset have the same
8293 stmt_list value too so in practice this should work well.
8294
8295 The basic algorithm here is:
8296
8297 sort TUs by abbrev table
8298 for each TU with same abbrev table:
8299 read abbrev table if first user
8300 read TU top level DIE
8301 [IWBN if DWO skeletons had DW_AT_stmt_list]
8302 call FUNC */
8303
8304 if (dwarf_read_debug)
8305 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8306
8307 /* Sort in a separate table to maintain the order of all_type_units
8308 for .gdb_index: TU indices directly index all_type_units. */
8309 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8310 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8311
8312 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8313 sorted_by_abbrev.emplace_back
8314 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8315 sig_type->per_cu.section,
8316 sig_type->per_cu.sect_off));
8317
8318 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8319 sort_tu_by_abbrev_offset);
8320
8321 abbrev_offset = (sect_offset) ~(unsigned) 0;
8322
8323 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8324 {
8325 /* Switch to the next abbrev table if necessary. */
8326 if (abbrev_table == NULL
8327 || tu.abbrev_offset != abbrev_offset)
8328 {
8329 abbrev_offset = tu.abbrev_offset;
8330 abbrev_table =
8331 abbrev_table_read_table (dwarf2_per_objfile,
8332 &dwarf2_per_objfile->abbrev,
8333 abbrev_offset);
8334 ++tu_stats->nr_uniq_abbrev_tables;
8335 }
8336
8337 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
8338 0, 0, false);
8339 if (!reader.dummy_p)
8340 build_type_psymtabs_reader (&reader, reader.info_ptr,
8341 reader.comp_unit_die,
8342 reader.has_children);
8343 }
8344 }
8345
8346 /* Print collected type unit statistics. */
8347
8348 static void
8349 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8350 {
8351 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8352
8353 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8354 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8355 dwarf2_per_objfile->all_type_units.size ());
8356 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8357 tu_stats->nr_uniq_abbrev_tables);
8358 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8359 tu_stats->nr_symtabs);
8360 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8361 tu_stats->nr_symtab_sharers);
8362 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8363 tu_stats->nr_stmt_less_type_units);
8364 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8365 tu_stats->nr_all_type_units_reallocs);
8366 }
8367
8368 /* Traversal function for build_type_psymtabs. */
8369
8370 static int
8371 build_type_psymtab_dependencies (void **slot, void *info)
8372 {
8373 struct dwarf2_per_objfile *dwarf2_per_objfile
8374 = (struct dwarf2_per_objfile *) info;
8375 struct objfile *objfile = dwarf2_per_objfile->objfile;
8376 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8377 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8378 dwarf2_psymtab *pst = per_cu->v.psymtab;
8379 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8380 int i;
8381
8382 gdb_assert (len > 0);
8383 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8384
8385 pst->number_of_dependencies = len;
8386 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8387 for (i = 0; i < len; ++i)
8388 {
8389 struct signatured_type *iter = tu_group->tus->at (i);
8390 gdb_assert (iter->per_cu.is_debug_types);
8391 pst->dependencies[i] = iter->per_cu.v.psymtab;
8392 iter->type_unit_group = tu_group;
8393 }
8394
8395 delete tu_group->tus;
8396 tu_group->tus = nullptr;
8397
8398 return 1;
8399 }
8400
8401 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8402 Build partial symbol tables for the .debug_types comp-units. */
8403
8404 static void
8405 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8406 {
8407 if (! create_all_type_units (dwarf2_per_objfile))
8408 return;
8409
8410 build_type_psymtabs_1 (dwarf2_per_objfile);
8411 }
8412
8413 /* Traversal function for process_skeletonless_type_unit.
8414 Read a TU in a DWO file and build partial symbols for it. */
8415
8416 static int
8417 process_skeletonless_type_unit (void **slot, void *info)
8418 {
8419 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8420 struct dwarf2_per_objfile *dwarf2_per_objfile
8421 = (struct dwarf2_per_objfile *) info;
8422 struct signatured_type find_entry, *entry;
8423
8424 /* If this TU doesn't exist in the global table, add it and read it in. */
8425
8426 if (dwarf2_per_objfile->signatured_types == NULL)
8427 {
8428 dwarf2_per_objfile->signatured_types
8429 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8430 }
8431
8432 find_entry.signature = dwo_unit->signature;
8433 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8434 INSERT);
8435 /* If we've already seen this type there's nothing to do. What's happening
8436 is we're doing our own version of comdat-folding here. */
8437 if (*slot != NULL)
8438 return 1;
8439
8440 /* This does the job that create_all_type_units would have done for
8441 this TU. */
8442 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8443 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8444 *slot = entry;
8445
8446 /* This does the job that build_type_psymtabs_1 would have done. */
8447 cutu_reader reader (&entry->per_cu, NULL, 0, 0, false);
8448 if (!reader.dummy_p)
8449 build_type_psymtabs_reader (&reader, reader.info_ptr,
8450 reader.comp_unit_die, reader.has_children);
8451
8452 return 1;
8453 }
8454
8455 /* Traversal function for process_skeletonless_type_units. */
8456
8457 static int
8458 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8459 {
8460 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8461
8462 if (dwo_file->tus != NULL)
8463 {
8464 htab_traverse_noresize (dwo_file->tus,
8465 process_skeletonless_type_unit, info);
8466 }
8467
8468 return 1;
8469 }
8470
8471 /* Scan all TUs of DWO files, verifying we've processed them.
8472 This is needed in case a TU was emitted without its skeleton.
8473 Note: This can't be done until we know what all the DWO files are. */
8474
8475 static void
8476 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8477 {
8478 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8479 if (get_dwp_file (dwarf2_per_objfile) == NULL
8480 && dwarf2_per_objfile->dwo_files != NULL)
8481 {
8482 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
8483 process_dwo_file_for_skeletonless_type_units,
8484 dwarf2_per_objfile);
8485 }
8486 }
8487
8488 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8489
8490 static void
8491 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8492 {
8493 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8494 {
8495 dwarf2_psymtab *pst = per_cu->v.psymtab;
8496
8497 if (pst == NULL)
8498 continue;
8499
8500 for (int j = 0; j < pst->number_of_dependencies; ++j)
8501 {
8502 /* Set the 'user' field only if it is not already set. */
8503 if (pst->dependencies[j]->user == NULL)
8504 pst->dependencies[j]->user = pst;
8505 }
8506 }
8507 }
8508
8509 /* Build the partial symbol table by doing a quick pass through the
8510 .debug_info and .debug_abbrev sections. */
8511
8512 static void
8513 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8514 {
8515 struct objfile *objfile = dwarf2_per_objfile->objfile;
8516
8517 if (dwarf_read_debug)
8518 {
8519 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8520 objfile_name (objfile));
8521 }
8522
8523 dwarf2_per_objfile->reading_partial_symbols = 1;
8524
8525 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8526
8527 /* Any cached compilation units will be linked by the per-objfile
8528 read_in_chain. Make sure to free them when we're done. */
8529 free_cached_comp_units freer (dwarf2_per_objfile);
8530
8531 build_type_psymtabs (dwarf2_per_objfile);
8532
8533 create_all_comp_units (dwarf2_per_objfile);
8534
8535 /* Create a temporary address map on a temporary obstack. We later
8536 copy this to the final obstack. */
8537 auto_obstack temp_obstack;
8538
8539 scoped_restore save_psymtabs_addrmap
8540 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8541 addrmap_create_mutable (&temp_obstack));
8542
8543 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8544 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8545
8546 /* This has to wait until we read the CUs, we need the list of DWOs. */
8547 process_skeletonless_type_units (dwarf2_per_objfile);
8548
8549 /* Now that all TUs have been processed we can fill in the dependencies. */
8550 if (dwarf2_per_objfile->type_unit_groups != NULL)
8551 {
8552 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8553 build_type_psymtab_dependencies, dwarf2_per_objfile);
8554 }
8555
8556 if (dwarf_read_debug)
8557 print_tu_stats (dwarf2_per_objfile);
8558
8559 set_partial_user (dwarf2_per_objfile);
8560
8561 objfile->partial_symtabs->psymtabs_addrmap
8562 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8563 objfile->partial_symtabs->obstack ());
8564 /* At this point we want to keep the address map. */
8565 save_psymtabs_addrmap.release ();
8566
8567 if (dwarf_read_debug)
8568 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8569 objfile_name (objfile));
8570 }
8571
8572 /* Load the partial DIEs for a secondary CU into memory.
8573 This is also used when rereading a primary CU with load_all_dies. */
8574
8575 static void
8576 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8577 {
8578 cutu_reader reader (this_cu, NULL, 1, 1, false);
8579
8580 if (!reader.dummy_p)
8581 {
8582 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8583 language_minimal);
8584
8585 /* Check if comp unit has_children.
8586 If so, read the rest of the partial symbols from this comp unit.
8587 If not, there's no more debug_info for this comp unit. */
8588 if (reader.has_children)
8589 load_partial_dies (&reader, reader.info_ptr, 0);
8590 }
8591 }
8592
8593 static void
8594 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8595 struct dwarf2_section_info *section,
8596 struct dwarf2_section_info *abbrev_section,
8597 unsigned int is_dwz)
8598 {
8599 const gdb_byte *info_ptr;
8600 struct objfile *objfile = dwarf2_per_objfile->objfile;
8601
8602 if (dwarf_read_debug)
8603 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8604 get_section_name (section),
8605 get_section_file_name (section));
8606
8607 dwarf2_read_section (objfile, section);
8608
8609 info_ptr = section->buffer;
8610
8611 while (info_ptr < section->buffer + section->size)
8612 {
8613 struct dwarf2_per_cu_data *this_cu;
8614
8615 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8616
8617 comp_unit_head cu_header;
8618 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8619 abbrev_section, info_ptr,
8620 rcuh_kind::COMPILE);
8621
8622 /* Save the compilation unit for later lookup. */
8623 if (cu_header.unit_type != DW_UT_type)
8624 {
8625 this_cu = XOBNEW (&objfile->objfile_obstack,
8626 struct dwarf2_per_cu_data);
8627 memset (this_cu, 0, sizeof (*this_cu));
8628 }
8629 else
8630 {
8631 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8632 struct signatured_type);
8633 memset (sig_type, 0, sizeof (*sig_type));
8634 sig_type->signature = cu_header.signature;
8635 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8636 this_cu = &sig_type->per_cu;
8637 }
8638 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8639 this_cu->sect_off = sect_off;
8640 this_cu->length = cu_header.length + cu_header.initial_length_size;
8641 this_cu->is_dwz = is_dwz;
8642 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8643 this_cu->section = section;
8644
8645 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8646
8647 info_ptr = info_ptr + this_cu->length;
8648 }
8649 }
8650
8651 /* Create a list of all compilation units in OBJFILE.
8652 This is only done for -readnow and building partial symtabs. */
8653
8654 static void
8655 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8656 {
8657 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8658 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8659 &dwarf2_per_objfile->abbrev, 0);
8660
8661 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8662 if (dwz != NULL)
8663 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8664 1);
8665 }
8666
8667 /* Process all loaded DIEs for compilation unit CU, starting at
8668 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8669 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8670 DW_AT_ranges). See the comments of add_partial_subprogram on how
8671 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8672
8673 static void
8674 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8675 CORE_ADDR *highpc, int set_addrmap,
8676 struct dwarf2_cu *cu)
8677 {
8678 struct partial_die_info *pdi;
8679
8680 /* Now, march along the PDI's, descending into ones which have
8681 interesting children but skipping the children of the other ones,
8682 until we reach the end of the compilation unit. */
8683
8684 pdi = first_die;
8685
8686 while (pdi != NULL)
8687 {
8688 pdi->fixup (cu);
8689
8690 /* Anonymous namespaces or modules have no name but have interesting
8691 children, so we need to look at them. Ditto for anonymous
8692 enums. */
8693
8694 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8695 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8696 || pdi->tag == DW_TAG_imported_unit
8697 || pdi->tag == DW_TAG_inlined_subroutine)
8698 {
8699 switch (pdi->tag)
8700 {
8701 case DW_TAG_subprogram:
8702 case DW_TAG_inlined_subroutine:
8703 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8704 break;
8705 case DW_TAG_constant:
8706 case DW_TAG_variable:
8707 case DW_TAG_typedef:
8708 case DW_TAG_union_type:
8709 if (!pdi->is_declaration)
8710 {
8711 add_partial_symbol (pdi, cu);
8712 }
8713 break;
8714 case DW_TAG_class_type:
8715 case DW_TAG_interface_type:
8716 case DW_TAG_structure_type:
8717 if (!pdi->is_declaration)
8718 {
8719 add_partial_symbol (pdi, cu);
8720 }
8721 if ((cu->language == language_rust
8722 || cu->language == language_cplus) && pdi->has_children)
8723 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8724 set_addrmap, cu);
8725 break;
8726 case DW_TAG_enumeration_type:
8727 if (!pdi->is_declaration)
8728 add_partial_enumeration (pdi, cu);
8729 break;
8730 case DW_TAG_base_type:
8731 case DW_TAG_subrange_type:
8732 /* File scope base type definitions are added to the partial
8733 symbol table. */
8734 add_partial_symbol (pdi, cu);
8735 break;
8736 case DW_TAG_namespace:
8737 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8738 break;
8739 case DW_TAG_module:
8740 if (!pdi->is_declaration)
8741 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8742 break;
8743 case DW_TAG_imported_unit:
8744 {
8745 struct dwarf2_per_cu_data *per_cu;
8746
8747 /* For now we don't handle imported units in type units. */
8748 if (cu->per_cu->is_debug_types)
8749 {
8750 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8751 " supported in type units [in module %s]"),
8752 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8753 }
8754
8755 per_cu = dwarf2_find_containing_comp_unit
8756 (pdi->d.sect_off, pdi->is_dwz,
8757 cu->per_cu->dwarf2_per_objfile);
8758
8759 /* Go read the partial unit, if needed. */
8760 if (per_cu->v.psymtab == NULL)
8761 process_psymtab_comp_unit (per_cu, 1, cu->language);
8762
8763 cu->per_cu->imported_symtabs_push (per_cu);
8764 }
8765 break;
8766 case DW_TAG_imported_declaration:
8767 add_partial_symbol (pdi, cu);
8768 break;
8769 default:
8770 break;
8771 }
8772 }
8773
8774 /* If the die has a sibling, skip to the sibling. */
8775
8776 pdi = pdi->die_sibling;
8777 }
8778 }
8779
8780 /* Functions used to compute the fully scoped name of a partial DIE.
8781
8782 Normally, this is simple. For C++, the parent DIE's fully scoped
8783 name is concatenated with "::" and the partial DIE's name.
8784 Enumerators are an exception; they use the scope of their parent
8785 enumeration type, i.e. the name of the enumeration type is not
8786 prepended to the enumerator.
8787
8788 There are two complexities. One is DW_AT_specification; in this
8789 case "parent" means the parent of the target of the specification,
8790 instead of the direct parent of the DIE. The other is compilers
8791 which do not emit DW_TAG_namespace; in this case we try to guess
8792 the fully qualified name of structure types from their members'
8793 linkage names. This must be done using the DIE's children rather
8794 than the children of any DW_AT_specification target. We only need
8795 to do this for structures at the top level, i.e. if the target of
8796 any DW_AT_specification (if any; otherwise the DIE itself) does not
8797 have a parent. */
8798
8799 /* Compute the scope prefix associated with PDI's parent, in
8800 compilation unit CU. The result will be allocated on CU's
8801 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8802 field. NULL is returned if no prefix is necessary. */
8803 static const char *
8804 partial_die_parent_scope (struct partial_die_info *pdi,
8805 struct dwarf2_cu *cu)
8806 {
8807 const char *grandparent_scope;
8808 struct partial_die_info *parent, *real_pdi;
8809
8810 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8811 then this means the parent of the specification DIE. */
8812
8813 real_pdi = pdi;
8814 while (real_pdi->has_specification)
8815 {
8816 auto res = find_partial_die (real_pdi->spec_offset,
8817 real_pdi->spec_is_dwz, cu);
8818 real_pdi = res.pdi;
8819 cu = res.cu;
8820 }
8821
8822 parent = real_pdi->die_parent;
8823 if (parent == NULL)
8824 return NULL;
8825
8826 if (parent->scope_set)
8827 return parent->scope;
8828
8829 parent->fixup (cu);
8830
8831 grandparent_scope = partial_die_parent_scope (parent, cu);
8832
8833 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8834 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8835 Work around this problem here. */
8836 if (cu->language == language_cplus
8837 && parent->tag == DW_TAG_namespace
8838 && strcmp (parent->name, "::") == 0
8839 && grandparent_scope == NULL)
8840 {
8841 parent->scope = NULL;
8842 parent->scope_set = 1;
8843 return NULL;
8844 }
8845
8846 /* Nested subroutines in Fortran get a prefix. */
8847 if (pdi->tag == DW_TAG_enumerator)
8848 /* Enumerators should not get the name of the enumeration as a prefix. */
8849 parent->scope = grandparent_scope;
8850 else if (parent->tag == DW_TAG_namespace
8851 || parent->tag == DW_TAG_module
8852 || parent->tag == DW_TAG_structure_type
8853 || parent->tag == DW_TAG_class_type
8854 || parent->tag == DW_TAG_interface_type
8855 || parent->tag == DW_TAG_union_type
8856 || parent->tag == DW_TAG_enumeration_type
8857 || (cu->language == language_fortran
8858 && parent->tag == DW_TAG_subprogram
8859 && pdi->tag == DW_TAG_subprogram))
8860 {
8861 if (grandparent_scope == NULL)
8862 parent->scope = parent->name;
8863 else
8864 parent->scope = typename_concat (&cu->comp_unit_obstack,
8865 grandparent_scope,
8866 parent->name, 0, cu);
8867 }
8868 else
8869 {
8870 /* FIXME drow/2004-04-01: What should we be doing with
8871 function-local names? For partial symbols, we should probably be
8872 ignoring them. */
8873 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8874 dwarf_tag_name (parent->tag),
8875 sect_offset_str (pdi->sect_off));
8876 parent->scope = grandparent_scope;
8877 }
8878
8879 parent->scope_set = 1;
8880 return parent->scope;
8881 }
8882
8883 /* Return the fully scoped name associated with PDI, from compilation unit
8884 CU. The result will be allocated with malloc. */
8885
8886 static gdb::unique_xmalloc_ptr<char>
8887 partial_die_full_name (struct partial_die_info *pdi,
8888 struct dwarf2_cu *cu)
8889 {
8890 const char *parent_scope;
8891
8892 /* If this is a template instantiation, we can not work out the
8893 template arguments from partial DIEs. So, unfortunately, we have
8894 to go through the full DIEs. At least any work we do building
8895 types here will be reused if full symbols are loaded later. */
8896 if (pdi->has_template_arguments)
8897 {
8898 pdi->fixup (cu);
8899
8900 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8901 {
8902 struct die_info *die;
8903 struct attribute attr;
8904 struct dwarf2_cu *ref_cu = cu;
8905
8906 /* DW_FORM_ref_addr is using section offset. */
8907 attr.name = (enum dwarf_attribute) 0;
8908 attr.form = DW_FORM_ref_addr;
8909 attr.u.unsnd = to_underlying (pdi->sect_off);
8910 die = follow_die_ref (NULL, &attr, &ref_cu);
8911
8912 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8913 }
8914 }
8915
8916 parent_scope = partial_die_parent_scope (pdi, cu);
8917 if (parent_scope == NULL)
8918 return NULL;
8919 else
8920 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8921 pdi->name, 0, cu));
8922 }
8923
8924 static void
8925 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8926 {
8927 struct dwarf2_per_objfile *dwarf2_per_objfile
8928 = cu->per_cu->dwarf2_per_objfile;
8929 struct objfile *objfile = dwarf2_per_objfile->objfile;
8930 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8931 CORE_ADDR addr = 0;
8932 const char *actual_name = NULL;
8933 CORE_ADDR baseaddr;
8934
8935 baseaddr = objfile->text_section_offset ();
8936
8937 gdb::unique_xmalloc_ptr<char> built_actual_name
8938 = partial_die_full_name (pdi, cu);
8939 if (built_actual_name != NULL)
8940 actual_name = built_actual_name.get ();
8941
8942 if (actual_name == NULL)
8943 actual_name = pdi->name;
8944
8945 switch (pdi->tag)
8946 {
8947 case DW_TAG_inlined_subroutine:
8948 case DW_TAG_subprogram:
8949 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8950 - baseaddr);
8951 if (pdi->is_external
8952 || cu->language == language_ada
8953 || (cu->language == language_fortran
8954 && pdi->die_parent != NULL
8955 && pdi->die_parent->tag == DW_TAG_subprogram))
8956 {
8957 /* Normally, only "external" DIEs are part of the global scope.
8958 But in Ada and Fortran, we want to be able to access nested
8959 procedures globally. So all Ada and Fortran subprograms are
8960 stored in the global scope. */
8961 add_psymbol_to_list (actual_name,
8962 built_actual_name != NULL,
8963 VAR_DOMAIN, LOC_BLOCK,
8964 SECT_OFF_TEXT (objfile),
8965 psymbol_placement::GLOBAL,
8966 addr,
8967 cu->language, objfile);
8968 }
8969 else
8970 {
8971 add_psymbol_to_list (actual_name,
8972 built_actual_name != NULL,
8973 VAR_DOMAIN, LOC_BLOCK,
8974 SECT_OFF_TEXT (objfile),
8975 psymbol_placement::STATIC,
8976 addr, cu->language, objfile);
8977 }
8978
8979 if (pdi->main_subprogram && actual_name != NULL)
8980 set_objfile_main_name (objfile, actual_name, cu->language);
8981 break;
8982 case DW_TAG_constant:
8983 add_psymbol_to_list (actual_name,
8984 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8985 -1, (pdi->is_external
8986 ? psymbol_placement::GLOBAL
8987 : psymbol_placement::STATIC),
8988 0, cu->language, objfile);
8989 break;
8990 case DW_TAG_variable:
8991 if (pdi->d.locdesc)
8992 addr = decode_locdesc (pdi->d.locdesc, cu);
8993
8994 if (pdi->d.locdesc
8995 && addr == 0
8996 && !dwarf2_per_objfile->has_section_at_zero)
8997 {
8998 /* A global or static variable may also have been stripped
8999 out by the linker if unused, in which case its address
9000 will be nullified; do not add such variables into partial
9001 symbol table then. */
9002 }
9003 else if (pdi->is_external)
9004 {
9005 /* Global Variable.
9006 Don't enter into the minimal symbol tables as there is
9007 a minimal symbol table entry from the ELF symbols already.
9008 Enter into partial symbol table if it has a location
9009 descriptor or a type.
9010 If the location descriptor is missing, new_symbol will create
9011 a LOC_UNRESOLVED symbol, the address of the variable will then
9012 be determined from the minimal symbol table whenever the variable
9013 is referenced.
9014 The address for the partial symbol table entry is not
9015 used by GDB, but it comes in handy for debugging partial symbol
9016 table building. */
9017
9018 if (pdi->d.locdesc || pdi->has_type)
9019 add_psymbol_to_list (actual_name,
9020 built_actual_name != NULL,
9021 VAR_DOMAIN, LOC_STATIC,
9022 SECT_OFF_TEXT (objfile),
9023 psymbol_placement::GLOBAL,
9024 addr, cu->language, objfile);
9025 }
9026 else
9027 {
9028 int has_loc = pdi->d.locdesc != NULL;
9029
9030 /* Static Variable. Skip symbols whose value we cannot know (those
9031 without location descriptors or constant values). */
9032 if (!has_loc && !pdi->has_const_value)
9033 return;
9034
9035 add_psymbol_to_list (actual_name,
9036 built_actual_name != NULL,
9037 VAR_DOMAIN, LOC_STATIC,
9038 SECT_OFF_TEXT (objfile),
9039 psymbol_placement::STATIC,
9040 has_loc ? addr : 0,
9041 cu->language, objfile);
9042 }
9043 break;
9044 case DW_TAG_typedef:
9045 case DW_TAG_base_type:
9046 case DW_TAG_subrange_type:
9047 add_psymbol_to_list (actual_name,
9048 built_actual_name != NULL,
9049 VAR_DOMAIN, LOC_TYPEDEF, -1,
9050 psymbol_placement::STATIC,
9051 0, cu->language, objfile);
9052 break;
9053 case DW_TAG_imported_declaration:
9054 case DW_TAG_namespace:
9055 add_psymbol_to_list (actual_name,
9056 built_actual_name != NULL,
9057 VAR_DOMAIN, LOC_TYPEDEF, -1,
9058 psymbol_placement::GLOBAL,
9059 0, cu->language, objfile);
9060 break;
9061 case DW_TAG_module:
9062 /* With Fortran 77 there might be a "BLOCK DATA" module
9063 available without any name. If so, we skip the module as it
9064 doesn't bring any value. */
9065 if (actual_name != nullptr)
9066 add_psymbol_to_list (actual_name,
9067 built_actual_name != NULL,
9068 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9069 psymbol_placement::GLOBAL,
9070 0, cu->language, objfile);
9071 break;
9072 case DW_TAG_class_type:
9073 case DW_TAG_interface_type:
9074 case DW_TAG_structure_type:
9075 case DW_TAG_union_type:
9076 case DW_TAG_enumeration_type:
9077 /* Skip external references. The DWARF standard says in the section
9078 about "Structure, Union, and Class Type Entries": "An incomplete
9079 structure, union or class type is represented by a structure,
9080 union or class entry that does not have a byte size attribute
9081 and that has a DW_AT_declaration attribute." */
9082 if (!pdi->has_byte_size && pdi->is_declaration)
9083 return;
9084
9085 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9086 static vs. global. */
9087 add_psymbol_to_list (actual_name,
9088 built_actual_name != NULL,
9089 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9090 cu->language == language_cplus
9091 ? psymbol_placement::GLOBAL
9092 : psymbol_placement::STATIC,
9093 0, cu->language, objfile);
9094
9095 break;
9096 case DW_TAG_enumerator:
9097 add_psymbol_to_list (actual_name,
9098 built_actual_name != NULL,
9099 VAR_DOMAIN, LOC_CONST, -1,
9100 cu->language == language_cplus
9101 ? psymbol_placement::GLOBAL
9102 : psymbol_placement::STATIC,
9103 0, cu->language, objfile);
9104 break;
9105 default:
9106 break;
9107 }
9108 }
9109
9110 /* Read a partial die corresponding to a namespace; also, add a symbol
9111 corresponding to that namespace to the symbol table. NAMESPACE is
9112 the name of the enclosing namespace. */
9113
9114 static void
9115 add_partial_namespace (struct partial_die_info *pdi,
9116 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9117 int set_addrmap, struct dwarf2_cu *cu)
9118 {
9119 /* Add a symbol for the namespace. */
9120
9121 add_partial_symbol (pdi, cu);
9122
9123 /* Now scan partial symbols in that namespace. */
9124
9125 if (pdi->has_children)
9126 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9127 }
9128
9129 /* Read a partial die corresponding to a Fortran module. */
9130
9131 static void
9132 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9133 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9134 {
9135 /* Add a symbol for the namespace. */
9136
9137 add_partial_symbol (pdi, cu);
9138
9139 /* Now scan partial symbols in that module. */
9140
9141 if (pdi->has_children)
9142 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9143 }
9144
9145 /* Read a partial die corresponding to a subprogram or an inlined
9146 subprogram and create a partial symbol for that subprogram.
9147 When the CU language allows it, this routine also defines a partial
9148 symbol for each nested subprogram that this subprogram contains.
9149 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9150 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9151
9152 PDI may also be a lexical block, in which case we simply search
9153 recursively for subprograms defined inside that lexical block.
9154 Again, this is only performed when the CU language allows this
9155 type of definitions. */
9156
9157 static void
9158 add_partial_subprogram (struct partial_die_info *pdi,
9159 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9160 int set_addrmap, struct dwarf2_cu *cu)
9161 {
9162 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9163 {
9164 if (pdi->has_pc_info)
9165 {
9166 if (pdi->lowpc < *lowpc)
9167 *lowpc = pdi->lowpc;
9168 if (pdi->highpc > *highpc)
9169 *highpc = pdi->highpc;
9170 if (set_addrmap)
9171 {
9172 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9173 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9174 CORE_ADDR baseaddr;
9175 CORE_ADDR this_highpc;
9176 CORE_ADDR this_lowpc;
9177
9178 baseaddr = objfile->text_section_offset ();
9179 this_lowpc
9180 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9181 pdi->lowpc + baseaddr)
9182 - baseaddr);
9183 this_highpc
9184 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9185 pdi->highpc + baseaddr)
9186 - baseaddr);
9187 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
9188 this_lowpc, this_highpc - 1,
9189 cu->per_cu->v.psymtab);
9190 }
9191 }
9192
9193 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9194 {
9195 if (!pdi->is_declaration)
9196 /* Ignore subprogram DIEs that do not have a name, they are
9197 illegal. Do not emit a complaint at this point, we will
9198 do so when we convert this psymtab into a symtab. */
9199 if (pdi->name)
9200 add_partial_symbol (pdi, cu);
9201 }
9202 }
9203
9204 if (! pdi->has_children)
9205 return;
9206
9207 if (cu->language == language_ada || cu->language == language_fortran)
9208 {
9209 pdi = pdi->die_child;
9210 while (pdi != NULL)
9211 {
9212 pdi->fixup (cu);
9213 if (pdi->tag == DW_TAG_subprogram
9214 || pdi->tag == DW_TAG_inlined_subroutine
9215 || pdi->tag == DW_TAG_lexical_block)
9216 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9217 pdi = pdi->die_sibling;
9218 }
9219 }
9220 }
9221
9222 /* Read a partial die corresponding to an enumeration type. */
9223
9224 static void
9225 add_partial_enumeration (struct partial_die_info *enum_pdi,
9226 struct dwarf2_cu *cu)
9227 {
9228 struct partial_die_info *pdi;
9229
9230 if (enum_pdi->name != NULL)
9231 add_partial_symbol (enum_pdi, cu);
9232
9233 pdi = enum_pdi->die_child;
9234 while (pdi)
9235 {
9236 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9237 complaint (_("malformed enumerator DIE ignored"));
9238 else
9239 add_partial_symbol (pdi, cu);
9240 pdi = pdi->die_sibling;
9241 }
9242 }
9243
9244 /* Return the initial uleb128 in the die at INFO_PTR. */
9245
9246 static unsigned int
9247 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9248 {
9249 unsigned int bytes_read;
9250
9251 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9252 }
9253
9254 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9255 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9256
9257 Return the corresponding abbrev, or NULL if the number is zero (indicating
9258 an empty DIE). In either case *BYTES_READ will be set to the length of
9259 the initial number. */
9260
9261 static struct abbrev_info *
9262 peek_die_abbrev (const die_reader_specs &reader,
9263 const gdb_byte *info_ptr, unsigned int *bytes_read)
9264 {
9265 dwarf2_cu *cu = reader.cu;
9266 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9267 unsigned int abbrev_number
9268 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9269
9270 if (abbrev_number == 0)
9271 return NULL;
9272
9273 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9274 if (!abbrev)
9275 {
9276 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9277 " at offset %s [in module %s]"),
9278 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9279 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9280 }
9281
9282 return abbrev;
9283 }
9284
9285 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9286 Returns a pointer to the end of a series of DIEs, terminated by an empty
9287 DIE. Any children of the skipped DIEs will also be skipped. */
9288
9289 static const gdb_byte *
9290 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9291 {
9292 while (1)
9293 {
9294 unsigned int bytes_read;
9295 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9296
9297 if (abbrev == NULL)
9298 return info_ptr + bytes_read;
9299 else
9300 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9301 }
9302 }
9303
9304 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9305 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9306 abbrev corresponding to that skipped uleb128 should be passed in
9307 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9308 children. */
9309
9310 static const gdb_byte *
9311 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9312 struct abbrev_info *abbrev)
9313 {
9314 unsigned int bytes_read;
9315 struct attribute attr;
9316 bfd *abfd = reader->abfd;
9317 struct dwarf2_cu *cu = reader->cu;
9318 const gdb_byte *buffer = reader->buffer;
9319 const gdb_byte *buffer_end = reader->buffer_end;
9320 unsigned int form, i;
9321
9322 for (i = 0; i < abbrev->num_attrs; i++)
9323 {
9324 /* The only abbrev we care about is DW_AT_sibling. */
9325 if (abbrev->attrs[i].name == DW_AT_sibling)
9326 {
9327 bool ignored;
9328 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
9329 &ignored);
9330 if (attr.form == DW_FORM_ref_addr)
9331 complaint (_("ignoring absolute DW_AT_sibling"));
9332 else
9333 {
9334 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9335 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9336
9337 if (sibling_ptr < info_ptr)
9338 complaint (_("DW_AT_sibling points backwards"));
9339 else if (sibling_ptr > reader->buffer_end)
9340 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9341 else
9342 return sibling_ptr;
9343 }
9344 }
9345
9346 /* If it isn't DW_AT_sibling, skip this attribute. */
9347 form = abbrev->attrs[i].form;
9348 skip_attribute:
9349 switch (form)
9350 {
9351 case DW_FORM_ref_addr:
9352 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9353 and later it is offset sized. */
9354 if (cu->header.version == 2)
9355 info_ptr += cu->header.addr_size;
9356 else
9357 info_ptr += cu->header.offset_size;
9358 break;
9359 case DW_FORM_GNU_ref_alt:
9360 info_ptr += cu->header.offset_size;
9361 break;
9362 case DW_FORM_addr:
9363 info_ptr += cu->header.addr_size;
9364 break;
9365 case DW_FORM_data1:
9366 case DW_FORM_ref1:
9367 case DW_FORM_flag:
9368 case DW_FORM_strx1:
9369 info_ptr += 1;
9370 break;
9371 case DW_FORM_flag_present:
9372 case DW_FORM_implicit_const:
9373 break;
9374 case DW_FORM_data2:
9375 case DW_FORM_ref2:
9376 case DW_FORM_strx2:
9377 info_ptr += 2;
9378 break;
9379 case DW_FORM_strx3:
9380 info_ptr += 3;
9381 break;
9382 case DW_FORM_data4:
9383 case DW_FORM_ref4:
9384 case DW_FORM_strx4:
9385 info_ptr += 4;
9386 break;
9387 case DW_FORM_data8:
9388 case DW_FORM_ref8:
9389 case DW_FORM_ref_sig8:
9390 info_ptr += 8;
9391 break;
9392 case DW_FORM_data16:
9393 info_ptr += 16;
9394 break;
9395 case DW_FORM_string:
9396 read_direct_string (abfd, info_ptr, &bytes_read);
9397 info_ptr += bytes_read;
9398 break;
9399 case DW_FORM_sec_offset:
9400 case DW_FORM_strp:
9401 case DW_FORM_GNU_strp_alt:
9402 info_ptr += cu->header.offset_size;
9403 break;
9404 case DW_FORM_exprloc:
9405 case DW_FORM_block:
9406 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9407 info_ptr += bytes_read;
9408 break;
9409 case DW_FORM_block1:
9410 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9411 break;
9412 case DW_FORM_block2:
9413 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9414 break;
9415 case DW_FORM_block4:
9416 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9417 break;
9418 case DW_FORM_addrx:
9419 case DW_FORM_strx:
9420 case DW_FORM_sdata:
9421 case DW_FORM_udata:
9422 case DW_FORM_ref_udata:
9423 case DW_FORM_GNU_addr_index:
9424 case DW_FORM_GNU_str_index:
9425 case DW_FORM_rnglistx:
9426 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9427 break;
9428 case DW_FORM_indirect:
9429 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9430 info_ptr += bytes_read;
9431 /* We need to continue parsing from here, so just go back to
9432 the top. */
9433 goto skip_attribute;
9434
9435 default:
9436 error (_("Dwarf Error: Cannot handle %s "
9437 "in DWARF reader [in module %s]"),
9438 dwarf_form_name (form),
9439 bfd_get_filename (abfd));
9440 }
9441 }
9442
9443 if (abbrev->has_children)
9444 return skip_children (reader, info_ptr);
9445 else
9446 return info_ptr;
9447 }
9448
9449 /* Locate ORIG_PDI's sibling.
9450 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9451
9452 static const gdb_byte *
9453 locate_pdi_sibling (const struct die_reader_specs *reader,
9454 struct partial_die_info *orig_pdi,
9455 const gdb_byte *info_ptr)
9456 {
9457 /* Do we know the sibling already? */
9458
9459 if (orig_pdi->sibling)
9460 return orig_pdi->sibling;
9461
9462 /* Are there any children to deal with? */
9463
9464 if (!orig_pdi->has_children)
9465 return info_ptr;
9466
9467 /* Skip the children the long way. */
9468
9469 return skip_children (reader, info_ptr);
9470 }
9471
9472 /* Expand this partial symbol table into a full symbol table. SELF is
9473 not NULL. */
9474
9475 void
9476 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9477 {
9478 struct dwarf2_per_objfile *dwarf2_per_objfile
9479 = get_dwarf2_per_objfile (objfile);
9480
9481 gdb_assert (!readin);
9482 /* If this psymtab is constructed from a debug-only objfile, the
9483 has_section_at_zero flag will not necessarily be correct. We
9484 can get the correct value for this flag by looking at the data
9485 associated with the (presumably stripped) associated objfile. */
9486 if (objfile->separate_debug_objfile_backlink)
9487 {
9488 struct dwarf2_per_objfile *dpo_backlink
9489 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9490
9491 dwarf2_per_objfile->has_section_at_zero
9492 = dpo_backlink->has_section_at_zero;
9493 }
9494
9495 dwarf2_per_objfile->reading_partial_symbols = 0;
9496
9497 expand_psymtab (objfile);
9498
9499 process_cu_includes (dwarf2_per_objfile);
9500 }
9501 \f
9502 /* Reading in full CUs. */
9503
9504 /* Add PER_CU to the queue. */
9505
9506 static void
9507 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9508 enum language pretend_language)
9509 {
9510 struct dwarf2_queue_item *item;
9511
9512 per_cu->queued = 1;
9513 item = XNEW (struct dwarf2_queue_item);
9514 item->per_cu = per_cu;
9515 item->pretend_language = pretend_language;
9516 item->next = NULL;
9517
9518 if (dwarf2_queue == NULL)
9519 dwarf2_queue = item;
9520 else
9521 dwarf2_queue_tail->next = item;
9522
9523 dwarf2_queue_tail = item;
9524 }
9525
9526 /* If PER_CU is not yet queued, add it to the queue.
9527 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9528 dependency.
9529 The result is non-zero if PER_CU was queued, otherwise the result is zero
9530 meaning either PER_CU is already queued or it is already loaded.
9531
9532 N.B. There is an invariant here that if a CU is queued then it is loaded.
9533 The caller is required to load PER_CU if we return non-zero. */
9534
9535 static int
9536 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9537 struct dwarf2_per_cu_data *per_cu,
9538 enum language pretend_language)
9539 {
9540 /* We may arrive here during partial symbol reading, if we need full
9541 DIEs to process an unusual case (e.g. template arguments). Do
9542 not queue PER_CU, just tell our caller to load its DIEs. */
9543 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9544 {
9545 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9546 return 1;
9547 return 0;
9548 }
9549
9550 /* Mark the dependence relation so that we don't flush PER_CU
9551 too early. */
9552 if (dependent_cu != NULL)
9553 dwarf2_add_dependence (dependent_cu, per_cu);
9554
9555 /* If it's already on the queue, we have nothing to do. */
9556 if (per_cu->queued)
9557 return 0;
9558
9559 /* If the compilation unit is already loaded, just mark it as
9560 used. */
9561 if (per_cu->cu != NULL)
9562 {
9563 per_cu->cu->last_used = 0;
9564 return 0;
9565 }
9566
9567 /* Add it to the queue. */
9568 queue_comp_unit (per_cu, pretend_language);
9569
9570 return 1;
9571 }
9572
9573 /* Process the queue. */
9574
9575 static void
9576 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9577 {
9578 struct dwarf2_queue_item *item, *next_item;
9579
9580 if (dwarf_read_debug)
9581 {
9582 fprintf_unfiltered (gdb_stdlog,
9583 "Expanding one or more symtabs of objfile %s ...\n",
9584 objfile_name (dwarf2_per_objfile->objfile));
9585 }
9586
9587 /* The queue starts out with one item, but following a DIE reference
9588 may load a new CU, adding it to the end of the queue. */
9589 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9590 {
9591 if ((dwarf2_per_objfile->using_index
9592 ? !item->per_cu->v.quick->compunit_symtab
9593 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9594 /* Skip dummy CUs. */
9595 && item->per_cu->cu != NULL)
9596 {
9597 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9598 unsigned int debug_print_threshold;
9599 char buf[100];
9600
9601 if (per_cu->is_debug_types)
9602 {
9603 struct signatured_type *sig_type =
9604 (struct signatured_type *) per_cu;
9605
9606 sprintf (buf, "TU %s at offset %s",
9607 hex_string (sig_type->signature),
9608 sect_offset_str (per_cu->sect_off));
9609 /* There can be 100s of TUs.
9610 Only print them in verbose mode. */
9611 debug_print_threshold = 2;
9612 }
9613 else
9614 {
9615 sprintf (buf, "CU at offset %s",
9616 sect_offset_str (per_cu->sect_off));
9617 debug_print_threshold = 1;
9618 }
9619
9620 if (dwarf_read_debug >= debug_print_threshold)
9621 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9622
9623 if (per_cu->is_debug_types)
9624 process_full_type_unit (per_cu, item->pretend_language);
9625 else
9626 process_full_comp_unit (per_cu, item->pretend_language);
9627
9628 if (dwarf_read_debug >= debug_print_threshold)
9629 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9630 }
9631
9632 item->per_cu->queued = 0;
9633 next_item = item->next;
9634 xfree (item);
9635 }
9636
9637 dwarf2_queue_tail = NULL;
9638
9639 if (dwarf_read_debug)
9640 {
9641 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9642 objfile_name (dwarf2_per_objfile->objfile));
9643 }
9644 }
9645
9646 /* Read in full symbols for PST, and anything it depends on. */
9647
9648 void
9649 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9650 {
9651 struct dwarf2_per_cu_data *per_cu;
9652
9653 if (readin)
9654 return;
9655
9656 read_dependencies (objfile);
9657
9658 per_cu = per_cu_data;
9659
9660 if (per_cu == NULL)
9661 {
9662 /* It's an include file, no symbols to read for it.
9663 Everything is in the parent symtab. */
9664 readin = true;
9665 return;
9666 }
9667
9668 dw2_do_instantiate_symtab (per_cu, false);
9669 }
9670
9671 /* Trivial hash function for die_info: the hash value of a DIE
9672 is its offset in .debug_info for this objfile. */
9673
9674 static hashval_t
9675 die_hash (const void *item)
9676 {
9677 const struct die_info *die = (const struct die_info *) item;
9678
9679 return to_underlying (die->sect_off);
9680 }
9681
9682 /* Trivial comparison function for die_info structures: two DIEs
9683 are equal if they have the same offset. */
9684
9685 static int
9686 die_eq (const void *item_lhs, const void *item_rhs)
9687 {
9688 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9689 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9690
9691 return die_lhs->sect_off == die_rhs->sect_off;
9692 }
9693
9694 /* Load the DIEs associated with PER_CU into memory. */
9695
9696 static void
9697 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9698 bool skip_partial,
9699 enum language pretend_language)
9700 {
9701 gdb_assert (! this_cu->is_debug_types);
9702
9703 cutu_reader reader (this_cu, NULL, 1, 1, skip_partial);
9704 if (reader.dummy_p)
9705 return;
9706
9707 struct dwarf2_cu *cu = reader.cu;
9708 const gdb_byte *info_ptr = reader.info_ptr;
9709
9710 gdb_assert (cu->die_hash == NULL);
9711 cu->die_hash =
9712 htab_create_alloc_ex (cu->header.length / 12,
9713 die_hash,
9714 die_eq,
9715 NULL,
9716 &cu->comp_unit_obstack,
9717 hashtab_obstack_allocate,
9718 dummy_obstack_deallocate);
9719
9720 if (reader.has_children)
9721 reader.comp_unit_die->child
9722 = read_die_and_siblings (&reader, reader.info_ptr,
9723 &info_ptr, reader.comp_unit_die);
9724 cu->dies = reader.comp_unit_die;
9725 /* comp_unit_die is not stored in die_hash, no need. */
9726
9727 /* We try not to read any attributes in this function, because not
9728 all CUs needed for references have been loaded yet, and symbol
9729 table processing isn't initialized. But we have to set the CU language,
9730 or we won't be able to build types correctly.
9731 Similarly, if we do not read the producer, we can not apply
9732 producer-specific interpretation. */
9733 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9734 }
9735
9736 /* Add a DIE to the delayed physname list. */
9737
9738 static void
9739 add_to_method_list (struct type *type, int fnfield_index, int index,
9740 const char *name, struct die_info *die,
9741 struct dwarf2_cu *cu)
9742 {
9743 struct delayed_method_info mi;
9744 mi.type = type;
9745 mi.fnfield_index = fnfield_index;
9746 mi.index = index;
9747 mi.name = name;
9748 mi.die = die;
9749 cu->method_list.push_back (mi);
9750 }
9751
9752 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9753 "const" / "volatile". If so, decrements LEN by the length of the
9754 modifier and return true. Otherwise return false. */
9755
9756 template<size_t N>
9757 static bool
9758 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9759 {
9760 size_t mod_len = sizeof (mod) - 1;
9761 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9762 {
9763 len -= mod_len;
9764 return true;
9765 }
9766 return false;
9767 }
9768
9769 /* Compute the physnames of any methods on the CU's method list.
9770
9771 The computation of method physnames is delayed in order to avoid the
9772 (bad) condition that one of the method's formal parameters is of an as yet
9773 incomplete type. */
9774
9775 static void
9776 compute_delayed_physnames (struct dwarf2_cu *cu)
9777 {
9778 /* Only C++ delays computing physnames. */
9779 if (cu->method_list.empty ())
9780 return;
9781 gdb_assert (cu->language == language_cplus);
9782
9783 for (const delayed_method_info &mi : cu->method_list)
9784 {
9785 const char *physname;
9786 struct fn_fieldlist *fn_flp
9787 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9788 physname = dwarf2_physname (mi.name, mi.die, cu);
9789 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9790 = physname ? physname : "";
9791
9792 /* Since there's no tag to indicate whether a method is a
9793 const/volatile overload, extract that information out of the
9794 demangled name. */
9795 if (physname != NULL)
9796 {
9797 size_t len = strlen (physname);
9798
9799 while (1)
9800 {
9801 if (physname[len] == ')') /* shortcut */
9802 break;
9803 else if (check_modifier (physname, len, " const"))
9804 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9805 else if (check_modifier (physname, len, " volatile"))
9806 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9807 else
9808 break;
9809 }
9810 }
9811 }
9812
9813 /* The list is no longer needed. */
9814 cu->method_list.clear ();
9815 }
9816
9817 /* Go objects should be embedded in a DW_TAG_module DIE,
9818 and it's not clear if/how imported objects will appear.
9819 To keep Go support simple until that's worked out,
9820 go back through what we've read and create something usable.
9821 We could do this while processing each DIE, and feels kinda cleaner,
9822 but that way is more invasive.
9823 This is to, for example, allow the user to type "p var" or "b main"
9824 without having to specify the package name, and allow lookups
9825 of module.object to work in contexts that use the expression
9826 parser. */
9827
9828 static void
9829 fixup_go_packaging (struct dwarf2_cu *cu)
9830 {
9831 gdb::unique_xmalloc_ptr<char> package_name;
9832 struct pending *list;
9833 int i;
9834
9835 for (list = *cu->get_builder ()->get_global_symbols ();
9836 list != NULL;
9837 list = list->next)
9838 {
9839 for (i = 0; i < list->nsyms; ++i)
9840 {
9841 struct symbol *sym = list->symbol[i];
9842
9843 if (sym->language () == language_go
9844 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9845 {
9846 gdb::unique_xmalloc_ptr<char> this_package_name
9847 (go_symbol_package_name (sym));
9848
9849 if (this_package_name == NULL)
9850 continue;
9851 if (package_name == NULL)
9852 package_name = std::move (this_package_name);
9853 else
9854 {
9855 struct objfile *objfile
9856 = cu->per_cu->dwarf2_per_objfile->objfile;
9857 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9858 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9859 (symbol_symtab (sym) != NULL
9860 ? symtab_to_filename_for_display
9861 (symbol_symtab (sym))
9862 : objfile_name (objfile)),
9863 this_package_name.get (), package_name.get ());
9864 }
9865 }
9866 }
9867 }
9868
9869 if (package_name != NULL)
9870 {
9871 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9872 const char *saved_package_name
9873 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
9874 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9875 saved_package_name);
9876 struct symbol *sym;
9877
9878 sym = allocate_symbol (objfile);
9879 sym->set_language (language_go, &objfile->objfile_obstack);
9880 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9881 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9882 e.g., "main" finds the "main" module and not C's main(). */
9883 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9884 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9885 SYMBOL_TYPE (sym) = type;
9886
9887 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9888 }
9889 }
9890
9891 /* Allocate a fully-qualified name consisting of the two parts on the
9892 obstack. */
9893
9894 static const char *
9895 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9896 {
9897 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9898 }
9899
9900 /* A helper that allocates a struct discriminant_info to attach to a
9901 union type. */
9902
9903 static struct discriminant_info *
9904 alloc_discriminant_info (struct type *type, int discriminant_index,
9905 int default_index)
9906 {
9907 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9908 gdb_assert (discriminant_index == -1
9909 || (discriminant_index >= 0
9910 && discriminant_index < TYPE_NFIELDS (type)));
9911 gdb_assert (default_index == -1
9912 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9913
9914 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9915
9916 struct discriminant_info *disc
9917 = ((struct discriminant_info *)
9918 TYPE_ZALLOC (type,
9919 offsetof (struct discriminant_info, discriminants)
9920 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9921 disc->default_index = default_index;
9922 disc->discriminant_index = discriminant_index;
9923
9924 struct dynamic_prop prop;
9925 prop.kind = PROP_UNDEFINED;
9926 prop.data.baton = disc;
9927
9928 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9929
9930 return disc;
9931 }
9932
9933 /* Some versions of rustc emitted enums in an unusual way.
9934
9935 Ordinary enums were emitted as unions. The first element of each
9936 structure in the union was named "RUST$ENUM$DISR". This element
9937 held the discriminant.
9938
9939 These versions of Rust also implemented the "non-zero"
9940 optimization. When the enum had two values, and one is empty and
9941 the other holds a pointer that cannot be zero, the pointer is used
9942 as the discriminant, with a zero value meaning the empty variant.
9943 Here, the union's first member is of the form
9944 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9945 where the fieldnos are the indices of the fields that should be
9946 traversed in order to find the field (which may be several fields deep)
9947 and the variantname is the name of the variant of the case when the
9948 field is zero.
9949
9950 This function recognizes whether TYPE is of one of these forms,
9951 and, if so, smashes it to be a variant type. */
9952
9953 static void
9954 quirk_rust_enum (struct type *type, struct objfile *objfile)
9955 {
9956 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9957
9958 /* We don't need to deal with empty enums. */
9959 if (TYPE_NFIELDS (type) == 0)
9960 return;
9961
9962 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9963 if (TYPE_NFIELDS (type) == 1
9964 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9965 {
9966 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9967
9968 /* Decode the field name to find the offset of the
9969 discriminant. */
9970 ULONGEST bit_offset = 0;
9971 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9972 while (name[0] >= '0' && name[0] <= '9')
9973 {
9974 char *tail;
9975 unsigned long index = strtoul (name, &tail, 10);
9976 name = tail;
9977 if (*name != '$'
9978 || index >= TYPE_NFIELDS (field_type)
9979 || (TYPE_FIELD_LOC_KIND (field_type, index)
9980 != FIELD_LOC_KIND_BITPOS))
9981 {
9982 complaint (_("Could not parse Rust enum encoding string \"%s\""
9983 "[in module %s]"),
9984 TYPE_FIELD_NAME (type, 0),
9985 objfile_name (objfile));
9986 return;
9987 }
9988 ++name;
9989
9990 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9991 field_type = TYPE_FIELD_TYPE (field_type, index);
9992 }
9993
9994 /* Make a union to hold the variants. */
9995 struct type *union_type = alloc_type (objfile);
9996 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9997 TYPE_NFIELDS (union_type) = 3;
9998 TYPE_FIELDS (union_type)
9999 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10000 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10001 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10002
10003 /* Put the discriminant must at index 0. */
10004 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10005 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10006 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10007 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10008
10009 /* The order of fields doesn't really matter, so put the real
10010 field at index 1 and the data-less field at index 2. */
10011 struct discriminant_info *disc
10012 = alloc_discriminant_info (union_type, 0, 1);
10013 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10014 TYPE_FIELD_NAME (union_type, 1)
10015 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10016 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10017 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10018 TYPE_FIELD_NAME (union_type, 1));
10019
10020 const char *dataless_name
10021 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10022 name);
10023 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10024 dataless_name);
10025 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10026 /* NAME points into the original discriminant name, which
10027 already has the correct lifetime. */
10028 TYPE_FIELD_NAME (union_type, 2) = name;
10029 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10030 disc->discriminants[2] = 0;
10031
10032 /* Smash this type to be a structure type. We have to do this
10033 because the type has already been recorded. */
10034 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10035 TYPE_NFIELDS (type) = 1;
10036 TYPE_FIELDS (type)
10037 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10038
10039 /* Install the variant part. */
10040 TYPE_FIELD_TYPE (type, 0) = union_type;
10041 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10042 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10043 }
10044 /* A union with a single anonymous field is probably an old-style
10045 univariant enum. */
10046 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
10047 {
10048 /* Smash this type to be a structure type. We have to do this
10049 because the type has already been recorded. */
10050 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10051
10052 /* Make a union to hold the variants. */
10053 struct type *union_type = alloc_type (objfile);
10054 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10055 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10056 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10057 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10058 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10059
10060 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10061 const char *variant_name
10062 = rust_last_path_segment (TYPE_NAME (field_type));
10063 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10064 TYPE_NAME (field_type)
10065 = rust_fully_qualify (&objfile->objfile_obstack,
10066 TYPE_NAME (type), variant_name);
10067
10068 /* Install the union in the outer struct type. */
10069 TYPE_NFIELDS (type) = 1;
10070 TYPE_FIELDS (type)
10071 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10072 TYPE_FIELD_TYPE (type, 0) = union_type;
10073 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10074 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10075
10076 alloc_discriminant_info (union_type, -1, 0);
10077 }
10078 else
10079 {
10080 struct type *disr_type = nullptr;
10081 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10082 {
10083 disr_type = TYPE_FIELD_TYPE (type, i);
10084
10085 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10086 {
10087 /* All fields of a true enum will be structs. */
10088 return;
10089 }
10090 else if (TYPE_NFIELDS (disr_type) == 0)
10091 {
10092 /* Could be data-less variant, so keep going. */
10093 disr_type = nullptr;
10094 }
10095 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10096 "RUST$ENUM$DISR") != 0)
10097 {
10098 /* Not a Rust enum. */
10099 return;
10100 }
10101 else
10102 {
10103 /* Found one. */
10104 break;
10105 }
10106 }
10107
10108 /* If we got here without a discriminant, then it's probably
10109 just a union. */
10110 if (disr_type == nullptr)
10111 return;
10112
10113 /* Smash this type to be a structure type. We have to do this
10114 because the type has already been recorded. */
10115 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10116
10117 /* Make a union to hold the variants. */
10118 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10119 struct type *union_type = alloc_type (objfile);
10120 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10121 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10122 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10123 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10124 TYPE_FIELDS (union_type)
10125 = (struct field *) TYPE_ZALLOC (union_type,
10126 (TYPE_NFIELDS (union_type)
10127 * sizeof (struct field)));
10128
10129 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10130 TYPE_NFIELDS (type) * sizeof (struct field));
10131
10132 /* Install the discriminant at index 0 in the union. */
10133 TYPE_FIELD (union_type, 0) = *disr_field;
10134 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10135 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10136
10137 /* Install the union in the outer struct type. */
10138 TYPE_FIELD_TYPE (type, 0) = union_type;
10139 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10140 TYPE_NFIELDS (type) = 1;
10141
10142 /* Set the size and offset of the union type. */
10143 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10144
10145 /* We need a way to find the correct discriminant given a
10146 variant name. For convenience we build a map here. */
10147 struct type *enum_type = FIELD_TYPE (*disr_field);
10148 std::unordered_map<std::string, ULONGEST> discriminant_map;
10149 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10150 {
10151 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10152 {
10153 const char *name
10154 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10155 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10156 }
10157 }
10158
10159 int n_fields = TYPE_NFIELDS (union_type);
10160 struct discriminant_info *disc
10161 = alloc_discriminant_info (union_type, 0, -1);
10162 /* Skip the discriminant here. */
10163 for (int i = 1; i < n_fields; ++i)
10164 {
10165 /* Find the final word in the name of this variant's type.
10166 That name can be used to look up the correct
10167 discriminant. */
10168 const char *variant_name
10169 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10170 i)));
10171
10172 auto iter = discriminant_map.find (variant_name);
10173 if (iter != discriminant_map.end ())
10174 disc->discriminants[i] = iter->second;
10175
10176 /* Remove the discriminant field, if it exists. */
10177 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10178 if (TYPE_NFIELDS (sub_type) > 0)
10179 {
10180 --TYPE_NFIELDS (sub_type);
10181 ++TYPE_FIELDS (sub_type);
10182 }
10183 TYPE_FIELD_NAME (union_type, i) = variant_name;
10184 TYPE_NAME (sub_type)
10185 = rust_fully_qualify (&objfile->objfile_obstack,
10186 TYPE_NAME (type), variant_name);
10187 }
10188 }
10189 }
10190
10191 /* Rewrite some Rust unions to be structures with variants parts. */
10192
10193 static void
10194 rust_union_quirks (struct dwarf2_cu *cu)
10195 {
10196 gdb_assert (cu->language == language_rust);
10197 for (type *type_ : cu->rust_unions)
10198 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
10199 /* We don't need this any more. */
10200 cu->rust_unions.clear ();
10201 }
10202
10203 /* Return the symtab for PER_CU. This works properly regardless of
10204 whether we're using the index or psymtabs. */
10205
10206 static struct compunit_symtab *
10207 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10208 {
10209 return (per_cu->dwarf2_per_objfile->using_index
10210 ? per_cu->v.quick->compunit_symtab
10211 : per_cu->v.psymtab->compunit_symtab);
10212 }
10213
10214 /* A helper function for computing the list of all symbol tables
10215 included by PER_CU. */
10216
10217 static void
10218 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
10219 htab_t all_children, htab_t all_type_symtabs,
10220 struct dwarf2_per_cu_data *per_cu,
10221 struct compunit_symtab *immediate_parent)
10222 {
10223 void **slot;
10224 struct compunit_symtab *cust;
10225
10226 slot = htab_find_slot (all_children, per_cu, INSERT);
10227 if (*slot != NULL)
10228 {
10229 /* This inclusion and its children have been processed. */
10230 return;
10231 }
10232
10233 *slot = per_cu;
10234 /* Only add a CU if it has a symbol table. */
10235 cust = get_compunit_symtab (per_cu);
10236 if (cust != NULL)
10237 {
10238 /* If this is a type unit only add its symbol table if we haven't
10239 seen it yet (type unit per_cu's can share symtabs). */
10240 if (per_cu->is_debug_types)
10241 {
10242 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10243 if (*slot == NULL)
10244 {
10245 *slot = cust;
10246 result->push_back (cust);
10247 if (cust->user == NULL)
10248 cust->user = immediate_parent;
10249 }
10250 }
10251 else
10252 {
10253 result->push_back (cust);
10254 if (cust->user == NULL)
10255 cust->user = immediate_parent;
10256 }
10257 }
10258
10259 if (!per_cu->imported_symtabs_empty ())
10260 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10261 {
10262 recursively_compute_inclusions (result, all_children,
10263 all_type_symtabs, ptr, cust);
10264 }
10265 }
10266
10267 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10268 PER_CU. */
10269
10270 static void
10271 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10272 {
10273 gdb_assert (! per_cu->is_debug_types);
10274
10275 if (!per_cu->imported_symtabs_empty ())
10276 {
10277 int len;
10278 std::vector<compunit_symtab *> result_symtabs;
10279 htab_t all_children, all_type_symtabs;
10280 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10281
10282 /* If we don't have a symtab, we can just skip this case. */
10283 if (cust == NULL)
10284 return;
10285
10286 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10287 NULL, xcalloc, xfree);
10288 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10289 NULL, xcalloc, xfree);
10290
10291 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10292 {
10293 recursively_compute_inclusions (&result_symtabs, all_children,
10294 all_type_symtabs, ptr, cust);
10295 }
10296
10297 /* Now we have a transitive closure of all the included symtabs. */
10298 len = result_symtabs.size ();
10299 cust->includes
10300 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10301 struct compunit_symtab *, len + 1);
10302 memcpy (cust->includes, result_symtabs.data (),
10303 len * sizeof (compunit_symtab *));
10304 cust->includes[len] = NULL;
10305
10306 htab_delete (all_children);
10307 htab_delete (all_type_symtabs);
10308 }
10309 }
10310
10311 /* Compute the 'includes' field for the symtabs of all the CUs we just
10312 read. */
10313
10314 static void
10315 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10316 {
10317 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
10318 {
10319 if (! iter->is_debug_types)
10320 compute_compunit_symtab_includes (iter);
10321 }
10322
10323 dwarf2_per_objfile->just_read_cus.clear ();
10324 }
10325
10326 /* Generate full symbol information for PER_CU, whose DIEs have
10327 already been loaded into memory. */
10328
10329 static void
10330 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10331 enum language pretend_language)
10332 {
10333 struct dwarf2_cu *cu = per_cu->cu;
10334 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10335 struct objfile *objfile = dwarf2_per_objfile->objfile;
10336 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10337 CORE_ADDR lowpc, highpc;
10338 struct compunit_symtab *cust;
10339 CORE_ADDR baseaddr;
10340 struct block *static_block;
10341 CORE_ADDR addr;
10342
10343 baseaddr = objfile->text_section_offset ();
10344
10345 /* Clear the list here in case something was left over. */
10346 cu->method_list.clear ();
10347
10348 cu->language = pretend_language;
10349 cu->language_defn = language_def (cu->language);
10350
10351 /* Do line number decoding in read_file_scope () */
10352 process_die (cu->dies, cu);
10353
10354 /* For now fudge the Go package. */
10355 if (cu->language == language_go)
10356 fixup_go_packaging (cu);
10357
10358 /* Now that we have processed all the DIEs in the CU, all the types
10359 should be complete, and it should now be safe to compute all of the
10360 physnames. */
10361 compute_delayed_physnames (cu);
10362
10363 if (cu->language == language_rust)
10364 rust_union_quirks (cu);
10365
10366 /* Some compilers don't define a DW_AT_high_pc attribute for the
10367 compilation unit. If the DW_AT_high_pc is missing, synthesize
10368 it, by scanning the DIE's below the compilation unit. */
10369 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10370
10371 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10372 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10373
10374 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10375 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10376 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10377 addrmap to help ensure it has an accurate map of pc values belonging to
10378 this comp unit. */
10379 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10380
10381 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10382 SECT_OFF_TEXT (objfile),
10383 0);
10384
10385 if (cust != NULL)
10386 {
10387 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10388
10389 /* Set symtab language to language from DW_AT_language. If the
10390 compilation is from a C file generated by language preprocessors, do
10391 not set the language if it was already deduced by start_subfile. */
10392 if (!(cu->language == language_c
10393 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10394 COMPUNIT_FILETABS (cust)->language = cu->language;
10395
10396 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10397 produce DW_AT_location with location lists but it can be possibly
10398 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10399 there were bugs in prologue debug info, fixed later in GCC-4.5
10400 by "unwind info for epilogues" patch (which is not directly related).
10401
10402 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10403 needed, it would be wrong due to missing DW_AT_producer there.
10404
10405 Still one can confuse GDB by using non-standard GCC compilation
10406 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10407 */
10408 if (cu->has_loclist && gcc_4_minor >= 5)
10409 cust->locations_valid = 1;
10410
10411 if (gcc_4_minor >= 5)
10412 cust->epilogue_unwind_valid = 1;
10413
10414 cust->call_site_htab = cu->call_site_htab;
10415 }
10416
10417 if (dwarf2_per_objfile->using_index)
10418 per_cu->v.quick->compunit_symtab = cust;
10419 else
10420 {
10421 dwarf2_psymtab *pst = per_cu->v.psymtab;
10422 pst->compunit_symtab = cust;
10423 pst->readin = true;
10424 }
10425
10426 /* Push it for inclusion processing later. */
10427 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10428
10429 /* Not needed any more. */
10430 cu->reset_builder ();
10431 }
10432
10433 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10434 already been loaded into memory. */
10435
10436 static void
10437 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10438 enum language pretend_language)
10439 {
10440 struct dwarf2_cu *cu = per_cu->cu;
10441 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10442 struct objfile *objfile = dwarf2_per_objfile->objfile;
10443 struct compunit_symtab *cust;
10444 struct signatured_type *sig_type;
10445
10446 gdb_assert (per_cu->is_debug_types);
10447 sig_type = (struct signatured_type *) per_cu;
10448
10449 /* Clear the list here in case something was left over. */
10450 cu->method_list.clear ();
10451
10452 cu->language = pretend_language;
10453 cu->language_defn = language_def (cu->language);
10454
10455 /* The symbol tables are set up in read_type_unit_scope. */
10456 process_die (cu->dies, cu);
10457
10458 /* For now fudge the Go package. */
10459 if (cu->language == language_go)
10460 fixup_go_packaging (cu);
10461
10462 /* Now that we have processed all the DIEs in the CU, all the types
10463 should be complete, and it should now be safe to compute all of the
10464 physnames. */
10465 compute_delayed_physnames (cu);
10466
10467 if (cu->language == language_rust)
10468 rust_union_quirks (cu);
10469
10470 /* TUs share symbol tables.
10471 If this is the first TU to use this symtab, complete the construction
10472 of it with end_expandable_symtab. Otherwise, complete the addition of
10473 this TU's symbols to the existing symtab. */
10474 if (sig_type->type_unit_group->compunit_symtab == NULL)
10475 {
10476 buildsym_compunit *builder = cu->get_builder ();
10477 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10478 sig_type->type_unit_group->compunit_symtab = cust;
10479
10480 if (cust != NULL)
10481 {
10482 /* Set symtab language to language from DW_AT_language. If the
10483 compilation is from a C file generated by language preprocessors,
10484 do not set the language if it was already deduced by
10485 start_subfile. */
10486 if (!(cu->language == language_c
10487 && COMPUNIT_FILETABS (cust)->language != language_c))
10488 COMPUNIT_FILETABS (cust)->language = cu->language;
10489 }
10490 }
10491 else
10492 {
10493 cu->get_builder ()->augment_type_symtab ();
10494 cust = sig_type->type_unit_group->compunit_symtab;
10495 }
10496
10497 if (dwarf2_per_objfile->using_index)
10498 per_cu->v.quick->compunit_symtab = cust;
10499 else
10500 {
10501 dwarf2_psymtab *pst = per_cu->v.psymtab;
10502 pst->compunit_symtab = cust;
10503 pst->readin = true;
10504 }
10505
10506 /* Not needed any more. */
10507 cu->reset_builder ();
10508 }
10509
10510 /* Process an imported unit DIE. */
10511
10512 static void
10513 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10514 {
10515 struct attribute *attr;
10516
10517 /* For now we don't handle imported units in type units. */
10518 if (cu->per_cu->is_debug_types)
10519 {
10520 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10521 " supported in type units [in module %s]"),
10522 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10523 }
10524
10525 attr = dwarf2_attr (die, DW_AT_import, cu);
10526 if (attr != NULL)
10527 {
10528 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10529 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10530 dwarf2_per_cu_data *per_cu
10531 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10532 cu->per_cu->dwarf2_per_objfile);
10533
10534 /* If necessary, add it to the queue and load its DIEs. */
10535 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10536 load_full_comp_unit (per_cu, false, cu->language);
10537
10538 cu->per_cu->imported_symtabs_push (per_cu);
10539 }
10540 }
10541
10542 /* RAII object that represents a process_die scope: i.e.,
10543 starts/finishes processing a DIE. */
10544 class process_die_scope
10545 {
10546 public:
10547 process_die_scope (die_info *die, dwarf2_cu *cu)
10548 : m_die (die), m_cu (cu)
10549 {
10550 /* We should only be processing DIEs not already in process. */
10551 gdb_assert (!m_die->in_process);
10552 m_die->in_process = true;
10553 }
10554
10555 ~process_die_scope ()
10556 {
10557 m_die->in_process = false;
10558
10559 /* If we're done processing the DIE for the CU that owns the line
10560 header, we don't need the line header anymore. */
10561 if (m_cu->line_header_die_owner == m_die)
10562 {
10563 delete m_cu->line_header;
10564 m_cu->line_header = NULL;
10565 m_cu->line_header_die_owner = NULL;
10566 }
10567 }
10568
10569 private:
10570 die_info *m_die;
10571 dwarf2_cu *m_cu;
10572 };
10573
10574 /* Process a die and its children. */
10575
10576 static void
10577 process_die (struct die_info *die, struct dwarf2_cu *cu)
10578 {
10579 process_die_scope scope (die, cu);
10580
10581 switch (die->tag)
10582 {
10583 case DW_TAG_padding:
10584 break;
10585 case DW_TAG_compile_unit:
10586 case DW_TAG_partial_unit:
10587 read_file_scope (die, cu);
10588 break;
10589 case DW_TAG_type_unit:
10590 read_type_unit_scope (die, cu);
10591 break;
10592 case DW_TAG_subprogram:
10593 /* Nested subprograms in Fortran get a prefix. */
10594 if (cu->language == language_fortran
10595 && die->parent != NULL
10596 && die->parent->tag == DW_TAG_subprogram)
10597 cu->processing_has_namespace_info = true;
10598 /* Fall through. */
10599 case DW_TAG_inlined_subroutine:
10600 read_func_scope (die, cu);
10601 break;
10602 case DW_TAG_lexical_block:
10603 case DW_TAG_try_block:
10604 case DW_TAG_catch_block:
10605 read_lexical_block_scope (die, cu);
10606 break;
10607 case DW_TAG_call_site:
10608 case DW_TAG_GNU_call_site:
10609 read_call_site_scope (die, cu);
10610 break;
10611 case DW_TAG_class_type:
10612 case DW_TAG_interface_type:
10613 case DW_TAG_structure_type:
10614 case DW_TAG_union_type:
10615 process_structure_scope (die, cu);
10616 break;
10617 case DW_TAG_enumeration_type:
10618 process_enumeration_scope (die, cu);
10619 break;
10620
10621 /* These dies have a type, but processing them does not create
10622 a symbol or recurse to process the children. Therefore we can
10623 read them on-demand through read_type_die. */
10624 case DW_TAG_subroutine_type:
10625 case DW_TAG_set_type:
10626 case DW_TAG_array_type:
10627 case DW_TAG_pointer_type:
10628 case DW_TAG_ptr_to_member_type:
10629 case DW_TAG_reference_type:
10630 case DW_TAG_rvalue_reference_type:
10631 case DW_TAG_string_type:
10632 break;
10633
10634 case DW_TAG_base_type:
10635 case DW_TAG_subrange_type:
10636 case DW_TAG_typedef:
10637 /* Add a typedef symbol for the type definition, if it has a
10638 DW_AT_name. */
10639 new_symbol (die, read_type_die (die, cu), cu);
10640 break;
10641 case DW_TAG_common_block:
10642 read_common_block (die, cu);
10643 break;
10644 case DW_TAG_common_inclusion:
10645 break;
10646 case DW_TAG_namespace:
10647 cu->processing_has_namespace_info = true;
10648 read_namespace (die, cu);
10649 break;
10650 case DW_TAG_module:
10651 cu->processing_has_namespace_info = true;
10652 read_module (die, cu);
10653 break;
10654 case DW_TAG_imported_declaration:
10655 cu->processing_has_namespace_info = true;
10656 if (read_namespace_alias (die, cu))
10657 break;
10658 /* The declaration is not a global namespace alias. */
10659 /* Fall through. */
10660 case DW_TAG_imported_module:
10661 cu->processing_has_namespace_info = true;
10662 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10663 || cu->language != language_fortran))
10664 complaint (_("Tag '%s' has unexpected children"),
10665 dwarf_tag_name (die->tag));
10666 read_import_statement (die, cu);
10667 break;
10668
10669 case DW_TAG_imported_unit:
10670 process_imported_unit_die (die, cu);
10671 break;
10672
10673 case DW_TAG_variable:
10674 read_variable (die, cu);
10675 break;
10676
10677 default:
10678 new_symbol (die, NULL, cu);
10679 break;
10680 }
10681 }
10682 \f
10683 /* DWARF name computation. */
10684
10685 /* A helper function for dwarf2_compute_name which determines whether DIE
10686 needs to have the name of the scope prepended to the name listed in the
10687 die. */
10688
10689 static int
10690 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10691 {
10692 struct attribute *attr;
10693
10694 switch (die->tag)
10695 {
10696 case DW_TAG_namespace:
10697 case DW_TAG_typedef:
10698 case DW_TAG_class_type:
10699 case DW_TAG_interface_type:
10700 case DW_TAG_structure_type:
10701 case DW_TAG_union_type:
10702 case DW_TAG_enumeration_type:
10703 case DW_TAG_enumerator:
10704 case DW_TAG_subprogram:
10705 case DW_TAG_inlined_subroutine:
10706 case DW_TAG_member:
10707 case DW_TAG_imported_declaration:
10708 return 1;
10709
10710 case DW_TAG_variable:
10711 case DW_TAG_constant:
10712 /* We only need to prefix "globally" visible variables. These include
10713 any variable marked with DW_AT_external or any variable that
10714 lives in a namespace. [Variables in anonymous namespaces
10715 require prefixing, but they are not DW_AT_external.] */
10716
10717 if (dwarf2_attr (die, DW_AT_specification, cu))
10718 {
10719 struct dwarf2_cu *spec_cu = cu;
10720
10721 return die_needs_namespace (die_specification (die, &spec_cu),
10722 spec_cu);
10723 }
10724
10725 attr = dwarf2_attr (die, DW_AT_external, cu);
10726 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10727 && die->parent->tag != DW_TAG_module)
10728 return 0;
10729 /* A variable in a lexical block of some kind does not need a
10730 namespace, even though in C++ such variables may be external
10731 and have a mangled name. */
10732 if (die->parent->tag == DW_TAG_lexical_block
10733 || die->parent->tag == DW_TAG_try_block
10734 || die->parent->tag == DW_TAG_catch_block
10735 || die->parent->tag == DW_TAG_subprogram)
10736 return 0;
10737 return 1;
10738
10739 default:
10740 return 0;
10741 }
10742 }
10743
10744 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10745 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10746 defined for the given DIE. */
10747
10748 static struct attribute *
10749 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10750 {
10751 struct attribute *attr;
10752
10753 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10754 if (attr == NULL)
10755 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10756
10757 return attr;
10758 }
10759
10760 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10761 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10762 defined for the given DIE. */
10763
10764 static const char *
10765 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10766 {
10767 const char *linkage_name;
10768
10769 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10770 if (linkage_name == NULL)
10771 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10772
10773 return linkage_name;
10774 }
10775
10776 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10777 compute the physname for the object, which include a method's:
10778 - formal parameters (C++),
10779 - receiver type (Go),
10780
10781 The term "physname" is a bit confusing.
10782 For C++, for example, it is the demangled name.
10783 For Go, for example, it's the mangled name.
10784
10785 For Ada, return the DIE's linkage name rather than the fully qualified
10786 name. PHYSNAME is ignored..
10787
10788 The result is allocated on the objfile_obstack and canonicalized. */
10789
10790 static const char *
10791 dwarf2_compute_name (const char *name,
10792 struct die_info *die, struct dwarf2_cu *cu,
10793 int physname)
10794 {
10795 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10796
10797 if (name == NULL)
10798 name = dwarf2_name (die, cu);
10799
10800 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10801 but otherwise compute it by typename_concat inside GDB.
10802 FIXME: Actually this is not really true, or at least not always true.
10803 It's all very confusing. compute_and_set_names doesn't try to demangle
10804 Fortran names because there is no mangling standard. So new_symbol
10805 will set the demangled name to the result of dwarf2_full_name, and it is
10806 the demangled name that GDB uses if it exists. */
10807 if (cu->language == language_ada
10808 || (cu->language == language_fortran && physname))
10809 {
10810 /* For Ada unit, we prefer the linkage name over the name, as
10811 the former contains the exported name, which the user expects
10812 to be able to reference. Ideally, we want the user to be able
10813 to reference this entity using either natural or linkage name,
10814 but we haven't started looking at this enhancement yet. */
10815 const char *linkage_name = dw2_linkage_name (die, cu);
10816
10817 if (linkage_name != NULL)
10818 return linkage_name;
10819 }
10820
10821 /* These are the only languages we know how to qualify names in. */
10822 if (name != NULL
10823 && (cu->language == language_cplus
10824 || cu->language == language_fortran || cu->language == language_d
10825 || cu->language == language_rust))
10826 {
10827 if (die_needs_namespace (die, cu))
10828 {
10829 const char *prefix;
10830 const char *canonical_name = NULL;
10831
10832 string_file buf;
10833
10834 prefix = determine_prefix (die, cu);
10835 if (*prefix != '\0')
10836 {
10837 gdb::unique_xmalloc_ptr<char> prefixed_name
10838 (typename_concat (NULL, prefix, name, physname, cu));
10839
10840 buf.puts (prefixed_name.get ());
10841 }
10842 else
10843 buf.puts (name);
10844
10845 /* Template parameters may be specified in the DIE's DW_AT_name, or
10846 as children with DW_TAG_template_type_param or
10847 DW_TAG_value_type_param. If the latter, add them to the name
10848 here. If the name already has template parameters, then
10849 skip this step; some versions of GCC emit both, and
10850 it is more efficient to use the pre-computed name.
10851
10852 Something to keep in mind about this process: it is very
10853 unlikely, or in some cases downright impossible, to produce
10854 something that will match the mangled name of a function.
10855 If the definition of the function has the same debug info,
10856 we should be able to match up with it anyway. But fallbacks
10857 using the minimal symbol, for instance to find a method
10858 implemented in a stripped copy of libstdc++, will not work.
10859 If we do not have debug info for the definition, we will have to
10860 match them up some other way.
10861
10862 When we do name matching there is a related problem with function
10863 templates; two instantiated function templates are allowed to
10864 differ only by their return types, which we do not add here. */
10865
10866 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10867 {
10868 struct attribute *attr;
10869 struct die_info *child;
10870 int first = 1;
10871
10872 die->building_fullname = 1;
10873
10874 for (child = die->child; child != NULL; child = child->sibling)
10875 {
10876 struct type *type;
10877 LONGEST value;
10878 const gdb_byte *bytes;
10879 struct dwarf2_locexpr_baton *baton;
10880 struct value *v;
10881
10882 if (child->tag != DW_TAG_template_type_param
10883 && child->tag != DW_TAG_template_value_param)
10884 continue;
10885
10886 if (first)
10887 {
10888 buf.puts ("<");
10889 first = 0;
10890 }
10891 else
10892 buf.puts (", ");
10893
10894 attr = dwarf2_attr (child, DW_AT_type, cu);
10895 if (attr == NULL)
10896 {
10897 complaint (_("template parameter missing DW_AT_type"));
10898 buf.puts ("UNKNOWN_TYPE");
10899 continue;
10900 }
10901 type = die_type (child, cu);
10902
10903 if (child->tag == DW_TAG_template_type_param)
10904 {
10905 c_print_type (type, "", &buf, -1, 0, cu->language,
10906 &type_print_raw_options);
10907 continue;
10908 }
10909
10910 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10911 if (attr == NULL)
10912 {
10913 complaint (_("template parameter missing "
10914 "DW_AT_const_value"));
10915 buf.puts ("UNKNOWN_VALUE");
10916 continue;
10917 }
10918
10919 dwarf2_const_value_attr (attr, type, name,
10920 &cu->comp_unit_obstack, cu,
10921 &value, &bytes, &baton);
10922
10923 if (TYPE_NOSIGN (type))
10924 /* GDB prints characters as NUMBER 'CHAR'. If that's
10925 changed, this can use value_print instead. */
10926 c_printchar (value, type, &buf);
10927 else
10928 {
10929 struct value_print_options opts;
10930
10931 if (baton != NULL)
10932 v = dwarf2_evaluate_loc_desc (type, NULL,
10933 baton->data,
10934 baton->size,
10935 baton->per_cu);
10936 else if (bytes != NULL)
10937 {
10938 v = allocate_value (type);
10939 memcpy (value_contents_writeable (v), bytes,
10940 TYPE_LENGTH (type));
10941 }
10942 else
10943 v = value_from_longest (type, value);
10944
10945 /* Specify decimal so that we do not depend on
10946 the radix. */
10947 get_formatted_print_options (&opts, 'd');
10948 opts.raw = 1;
10949 value_print (v, &buf, &opts);
10950 release_value (v);
10951 }
10952 }
10953
10954 die->building_fullname = 0;
10955
10956 if (!first)
10957 {
10958 /* Close the argument list, with a space if necessary
10959 (nested templates). */
10960 if (!buf.empty () && buf.string ().back () == '>')
10961 buf.puts (" >");
10962 else
10963 buf.puts (">");
10964 }
10965 }
10966
10967 /* For C++ methods, append formal parameter type
10968 information, if PHYSNAME. */
10969
10970 if (physname && die->tag == DW_TAG_subprogram
10971 && cu->language == language_cplus)
10972 {
10973 struct type *type = read_type_die (die, cu);
10974
10975 c_type_print_args (type, &buf, 1, cu->language,
10976 &type_print_raw_options);
10977
10978 if (cu->language == language_cplus)
10979 {
10980 /* Assume that an artificial first parameter is
10981 "this", but do not crash if it is not. RealView
10982 marks unnamed (and thus unused) parameters as
10983 artificial; there is no way to differentiate
10984 the two cases. */
10985 if (TYPE_NFIELDS (type) > 0
10986 && TYPE_FIELD_ARTIFICIAL (type, 0)
10987 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10988 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10989 0))))
10990 buf.puts (" const");
10991 }
10992 }
10993
10994 const std::string &intermediate_name = buf.string ();
10995
10996 if (cu->language == language_cplus)
10997 canonical_name
10998 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10999 &objfile->per_bfd->storage_obstack);
11000
11001 /* If we only computed INTERMEDIATE_NAME, or if
11002 INTERMEDIATE_NAME is already canonical, then we need to
11003 copy it to the appropriate obstack. */
11004 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11005 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11006 intermediate_name);
11007 else
11008 name = canonical_name;
11009 }
11010 }
11011
11012 return name;
11013 }
11014
11015 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11016 If scope qualifiers are appropriate they will be added. The result
11017 will be allocated on the storage_obstack, or NULL if the DIE does
11018 not have a name. NAME may either be from a previous call to
11019 dwarf2_name or NULL.
11020
11021 The output string will be canonicalized (if C++). */
11022
11023 static const char *
11024 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11025 {
11026 return dwarf2_compute_name (name, die, cu, 0);
11027 }
11028
11029 /* Construct a physname for the given DIE in CU. NAME may either be
11030 from a previous call to dwarf2_name or NULL. The result will be
11031 allocated on the objfile_objstack or NULL if the DIE does not have a
11032 name.
11033
11034 The output string will be canonicalized (if C++). */
11035
11036 static const char *
11037 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11038 {
11039 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11040 const char *retval, *mangled = NULL, *canon = NULL;
11041 int need_copy = 1;
11042
11043 /* In this case dwarf2_compute_name is just a shortcut not building anything
11044 on its own. */
11045 if (!die_needs_namespace (die, cu))
11046 return dwarf2_compute_name (name, die, cu, 1);
11047
11048 mangled = dw2_linkage_name (die, cu);
11049
11050 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11051 See https://github.com/rust-lang/rust/issues/32925. */
11052 if (cu->language == language_rust && mangled != NULL
11053 && strchr (mangled, '{') != NULL)
11054 mangled = NULL;
11055
11056 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11057 has computed. */
11058 gdb::unique_xmalloc_ptr<char> demangled;
11059 if (mangled != NULL)
11060 {
11061
11062 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11063 {
11064 /* Do nothing (do not demangle the symbol name). */
11065 }
11066 else if (cu->language == language_go)
11067 {
11068 /* This is a lie, but we already lie to the caller new_symbol.
11069 new_symbol assumes we return the mangled name.
11070 This just undoes that lie until things are cleaned up. */
11071 }
11072 else
11073 {
11074 /* Use DMGL_RET_DROP for C++ template functions to suppress
11075 their return type. It is easier for GDB users to search
11076 for such functions as `name(params)' than `long name(params)'.
11077 In such case the minimal symbol names do not match the full
11078 symbol names but for template functions there is never a need
11079 to look up their definition from their declaration so
11080 the only disadvantage remains the minimal symbol variant
11081 `long name(params)' does not have the proper inferior type. */
11082 demangled.reset (gdb_demangle (mangled,
11083 (DMGL_PARAMS | DMGL_ANSI
11084 | DMGL_RET_DROP)));
11085 }
11086 if (demangled)
11087 canon = demangled.get ();
11088 else
11089 {
11090 canon = mangled;
11091 need_copy = 0;
11092 }
11093 }
11094
11095 if (canon == NULL || check_physname)
11096 {
11097 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11098
11099 if (canon != NULL && strcmp (physname, canon) != 0)
11100 {
11101 /* It may not mean a bug in GDB. The compiler could also
11102 compute DW_AT_linkage_name incorrectly. But in such case
11103 GDB would need to be bug-to-bug compatible. */
11104
11105 complaint (_("Computed physname <%s> does not match demangled <%s> "
11106 "(from linkage <%s>) - DIE at %s [in module %s]"),
11107 physname, canon, mangled, sect_offset_str (die->sect_off),
11108 objfile_name (objfile));
11109
11110 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11111 is available here - over computed PHYSNAME. It is safer
11112 against both buggy GDB and buggy compilers. */
11113
11114 retval = canon;
11115 }
11116 else
11117 {
11118 retval = physname;
11119 need_copy = 0;
11120 }
11121 }
11122 else
11123 retval = canon;
11124
11125 if (need_copy)
11126 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
11127
11128 return retval;
11129 }
11130
11131 /* Inspect DIE in CU for a namespace alias. If one exists, record
11132 a new symbol for it.
11133
11134 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11135
11136 static int
11137 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11138 {
11139 struct attribute *attr;
11140
11141 /* If the die does not have a name, this is not a namespace
11142 alias. */
11143 attr = dwarf2_attr (die, DW_AT_name, cu);
11144 if (attr != NULL)
11145 {
11146 int num;
11147 struct die_info *d = die;
11148 struct dwarf2_cu *imported_cu = cu;
11149
11150 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11151 keep inspecting DIEs until we hit the underlying import. */
11152 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11153 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11154 {
11155 attr = dwarf2_attr (d, DW_AT_import, cu);
11156 if (attr == NULL)
11157 break;
11158
11159 d = follow_die_ref (d, attr, &imported_cu);
11160 if (d->tag != DW_TAG_imported_declaration)
11161 break;
11162 }
11163
11164 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11165 {
11166 complaint (_("DIE at %s has too many recursively imported "
11167 "declarations"), sect_offset_str (d->sect_off));
11168 return 0;
11169 }
11170
11171 if (attr != NULL)
11172 {
11173 struct type *type;
11174 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11175
11176 type = get_die_type_at_offset (sect_off, cu->per_cu);
11177 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11178 {
11179 /* This declaration is a global namespace alias. Add
11180 a symbol for it whose type is the aliased namespace. */
11181 new_symbol (die, type, cu);
11182 return 1;
11183 }
11184 }
11185 }
11186
11187 return 0;
11188 }
11189
11190 /* Return the using directives repository (global or local?) to use in the
11191 current context for CU.
11192
11193 For Ada, imported declarations can materialize renamings, which *may* be
11194 global. However it is impossible (for now?) in DWARF to distinguish
11195 "external" imported declarations and "static" ones. As all imported
11196 declarations seem to be static in all other languages, make them all CU-wide
11197 global only in Ada. */
11198
11199 static struct using_direct **
11200 using_directives (struct dwarf2_cu *cu)
11201 {
11202 if (cu->language == language_ada
11203 && cu->get_builder ()->outermost_context_p ())
11204 return cu->get_builder ()->get_global_using_directives ();
11205 else
11206 return cu->get_builder ()->get_local_using_directives ();
11207 }
11208
11209 /* Read the import statement specified by the given die and record it. */
11210
11211 static void
11212 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11213 {
11214 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11215 struct attribute *import_attr;
11216 struct die_info *imported_die, *child_die;
11217 struct dwarf2_cu *imported_cu;
11218 const char *imported_name;
11219 const char *imported_name_prefix;
11220 const char *canonical_name;
11221 const char *import_alias;
11222 const char *imported_declaration = NULL;
11223 const char *import_prefix;
11224 std::vector<const char *> excludes;
11225
11226 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11227 if (import_attr == NULL)
11228 {
11229 complaint (_("Tag '%s' has no DW_AT_import"),
11230 dwarf_tag_name (die->tag));
11231 return;
11232 }
11233
11234 imported_cu = cu;
11235 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11236 imported_name = dwarf2_name (imported_die, imported_cu);
11237 if (imported_name == NULL)
11238 {
11239 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11240
11241 The import in the following code:
11242 namespace A
11243 {
11244 typedef int B;
11245 }
11246
11247 int main ()
11248 {
11249 using A::B;
11250 B b;
11251 return b;
11252 }
11253
11254 ...
11255 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11256 <52> DW_AT_decl_file : 1
11257 <53> DW_AT_decl_line : 6
11258 <54> DW_AT_import : <0x75>
11259 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11260 <59> DW_AT_name : B
11261 <5b> DW_AT_decl_file : 1
11262 <5c> DW_AT_decl_line : 2
11263 <5d> DW_AT_type : <0x6e>
11264 ...
11265 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11266 <76> DW_AT_byte_size : 4
11267 <77> DW_AT_encoding : 5 (signed)
11268
11269 imports the wrong die ( 0x75 instead of 0x58 ).
11270 This case will be ignored until the gcc bug is fixed. */
11271 return;
11272 }
11273
11274 /* Figure out the local name after import. */
11275 import_alias = dwarf2_name (die, cu);
11276
11277 /* Figure out where the statement is being imported to. */
11278 import_prefix = determine_prefix (die, cu);
11279
11280 /* Figure out what the scope of the imported die is and prepend it
11281 to the name of the imported die. */
11282 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11283
11284 if (imported_die->tag != DW_TAG_namespace
11285 && imported_die->tag != DW_TAG_module)
11286 {
11287 imported_declaration = imported_name;
11288 canonical_name = imported_name_prefix;
11289 }
11290 else if (strlen (imported_name_prefix) > 0)
11291 canonical_name = obconcat (&objfile->objfile_obstack,
11292 imported_name_prefix,
11293 (cu->language == language_d ? "." : "::"),
11294 imported_name, (char *) NULL);
11295 else
11296 canonical_name = imported_name;
11297
11298 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11299 for (child_die = die->child; child_die && child_die->tag;
11300 child_die = sibling_die (child_die))
11301 {
11302 /* DWARF-4: A Fortran use statement with a “rename list” may be
11303 represented by an imported module entry with an import attribute
11304 referring to the module and owned entries corresponding to those
11305 entities that are renamed as part of being imported. */
11306
11307 if (child_die->tag != DW_TAG_imported_declaration)
11308 {
11309 complaint (_("child DW_TAG_imported_declaration expected "
11310 "- DIE at %s [in module %s]"),
11311 sect_offset_str (child_die->sect_off),
11312 objfile_name (objfile));
11313 continue;
11314 }
11315
11316 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11317 if (import_attr == NULL)
11318 {
11319 complaint (_("Tag '%s' has no DW_AT_import"),
11320 dwarf_tag_name (child_die->tag));
11321 continue;
11322 }
11323
11324 imported_cu = cu;
11325 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11326 &imported_cu);
11327 imported_name = dwarf2_name (imported_die, imported_cu);
11328 if (imported_name == NULL)
11329 {
11330 complaint (_("child DW_TAG_imported_declaration has unknown "
11331 "imported name - DIE at %s [in module %s]"),
11332 sect_offset_str (child_die->sect_off),
11333 objfile_name (objfile));
11334 continue;
11335 }
11336
11337 excludes.push_back (imported_name);
11338
11339 process_die (child_die, cu);
11340 }
11341
11342 add_using_directive (using_directives (cu),
11343 import_prefix,
11344 canonical_name,
11345 import_alias,
11346 imported_declaration,
11347 excludes,
11348 0,
11349 &objfile->objfile_obstack);
11350 }
11351
11352 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11353 types, but gives them a size of zero. Starting with version 14,
11354 ICC is compatible with GCC. */
11355
11356 static bool
11357 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11358 {
11359 if (!cu->checked_producer)
11360 check_producer (cu);
11361
11362 return cu->producer_is_icc_lt_14;
11363 }
11364
11365 /* ICC generates a DW_AT_type for C void functions. This was observed on
11366 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11367 which says that void functions should not have a DW_AT_type. */
11368
11369 static bool
11370 producer_is_icc (struct dwarf2_cu *cu)
11371 {
11372 if (!cu->checked_producer)
11373 check_producer (cu);
11374
11375 return cu->producer_is_icc;
11376 }
11377
11378 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11379 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11380 this, it was first present in GCC release 4.3.0. */
11381
11382 static bool
11383 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11384 {
11385 if (!cu->checked_producer)
11386 check_producer (cu);
11387
11388 return cu->producer_is_gcc_lt_4_3;
11389 }
11390
11391 static file_and_directory
11392 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11393 {
11394 file_and_directory res;
11395
11396 /* Find the filename. Do not use dwarf2_name here, since the filename
11397 is not a source language identifier. */
11398 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11399 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11400
11401 if (res.comp_dir == NULL
11402 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11403 && IS_ABSOLUTE_PATH (res.name))
11404 {
11405 res.comp_dir_storage = ldirname (res.name);
11406 if (!res.comp_dir_storage.empty ())
11407 res.comp_dir = res.comp_dir_storage.c_str ();
11408 }
11409 if (res.comp_dir != NULL)
11410 {
11411 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11412 directory, get rid of it. */
11413 const char *cp = strchr (res.comp_dir, ':');
11414
11415 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11416 res.comp_dir = cp + 1;
11417 }
11418
11419 if (res.name == NULL)
11420 res.name = "<unknown>";
11421
11422 return res;
11423 }
11424
11425 /* Handle DW_AT_stmt_list for a compilation unit.
11426 DIE is the DW_TAG_compile_unit die for CU.
11427 COMP_DIR is the compilation directory. LOWPC is passed to
11428 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11429
11430 static void
11431 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11432 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11433 {
11434 struct dwarf2_per_objfile *dwarf2_per_objfile
11435 = cu->per_cu->dwarf2_per_objfile;
11436 struct objfile *objfile = dwarf2_per_objfile->objfile;
11437 struct attribute *attr;
11438 struct line_header line_header_local;
11439 hashval_t line_header_local_hash;
11440 void **slot;
11441 int decode_mapping;
11442
11443 gdb_assert (! cu->per_cu->is_debug_types);
11444
11445 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11446 if (attr == NULL)
11447 return;
11448
11449 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11450
11451 /* The line header hash table is only created if needed (it exists to
11452 prevent redundant reading of the line table for partial_units).
11453 If we're given a partial_unit, we'll need it. If we're given a
11454 compile_unit, then use the line header hash table if it's already
11455 created, but don't create one just yet. */
11456
11457 if (dwarf2_per_objfile->line_header_hash == NULL
11458 && die->tag == DW_TAG_partial_unit)
11459 {
11460 dwarf2_per_objfile->line_header_hash
11461 = htab_create_alloc_ex (127, line_header_hash_voidp,
11462 line_header_eq_voidp,
11463 free_line_header_voidp,
11464 &objfile->objfile_obstack,
11465 hashtab_obstack_allocate,
11466 dummy_obstack_deallocate);
11467 }
11468
11469 line_header_local.sect_off = line_offset;
11470 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11471 line_header_local_hash = line_header_hash (&line_header_local);
11472 if (dwarf2_per_objfile->line_header_hash != NULL)
11473 {
11474 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11475 &line_header_local,
11476 line_header_local_hash, NO_INSERT);
11477
11478 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11479 is not present in *SLOT (since if there is something in *SLOT then
11480 it will be for a partial_unit). */
11481 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11482 {
11483 gdb_assert (*slot != NULL);
11484 cu->line_header = (struct line_header *) *slot;
11485 return;
11486 }
11487 }
11488
11489 /* dwarf_decode_line_header does not yet provide sufficient information.
11490 We always have to call also dwarf_decode_lines for it. */
11491 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11492 if (lh == NULL)
11493 return;
11494
11495 cu->line_header = lh.release ();
11496 cu->line_header_die_owner = die;
11497
11498 if (dwarf2_per_objfile->line_header_hash == NULL)
11499 slot = NULL;
11500 else
11501 {
11502 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11503 &line_header_local,
11504 line_header_local_hash, INSERT);
11505 gdb_assert (slot != NULL);
11506 }
11507 if (slot != NULL && *slot == NULL)
11508 {
11509 /* This newly decoded line number information unit will be owned
11510 by line_header_hash hash table. */
11511 *slot = cu->line_header;
11512 cu->line_header_die_owner = NULL;
11513 }
11514 else
11515 {
11516 /* We cannot free any current entry in (*slot) as that struct line_header
11517 may be already used by multiple CUs. Create only temporary decoded
11518 line_header for this CU - it may happen at most once for each line
11519 number information unit. And if we're not using line_header_hash
11520 then this is what we want as well. */
11521 gdb_assert (die->tag != DW_TAG_partial_unit);
11522 }
11523 decode_mapping = (die->tag != DW_TAG_partial_unit);
11524 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11525 decode_mapping);
11526
11527 }
11528
11529 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11530
11531 static void
11532 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11533 {
11534 struct dwarf2_per_objfile *dwarf2_per_objfile
11535 = cu->per_cu->dwarf2_per_objfile;
11536 struct objfile *objfile = dwarf2_per_objfile->objfile;
11537 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11538 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11539 CORE_ADDR highpc = ((CORE_ADDR) 0);
11540 struct attribute *attr;
11541 struct die_info *child_die;
11542 CORE_ADDR baseaddr;
11543
11544 prepare_one_comp_unit (cu, die, cu->language);
11545 baseaddr = objfile->text_section_offset ();
11546
11547 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11548
11549 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11550 from finish_block. */
11551 if (lowpc == ((CORE_ADDR) -1))
11552 lowpc = highpc;
11553 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11554
11555 file_and_directory fnd = find_file_and_directory (die, cu);
11556
11557 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11558 standardised yet. As a workaround for the language detection we fall
11559 back to the DW_AT_producer string. */
11560 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11561 cu->language = language_opencl;
11562
11563 /* Similar hack for Go. */
11564 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11565 set_cu_language (DW_LANG_Go, cu);
11566
11567 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11568
11569 /* Decode line number information if present. We do this before
11570 processing child DIEs, so that the line header table is available
11571 for DW_AT_decl_file. */
11572 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11573
11574 /* Process all dies in compilation unit. */
11575 if (die->child != NULL)
11576 {
11577 child_die = die->child;
11578 while (child_die && child_die->tag)
11579 {
11580 process_die (child_die, cu);
11581 child_die = sibling_die (child_die);
11582 }
11583 }
11584
11585 /* Decode macro information, if present. Dwarf 2 macro information
11586 refers to information in the line number info statement program
11587 header, so we can only read it if we've read the header
11588 successfully. */
11589 attr = dwarf2_attr (die, DW_AT_macros, cu);
11590 if (attr == NULL)
11591 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11592 if (attr && cu->line_header)
11593 {
11594 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11595 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11596
11597 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11598 }
11599 else
11600 {
11601 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11602 if (attr && cu->line_header)
11603 {
11604 unsigned int macro_offset = DW_UNSND (attr);
11605
11606 dwarf_decode_macros (cu, macro_offset, 0);
11607 }
11608 }
11609 }
11610
11611 void
11612 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11613 {
11614 struct type_unit_group *tu_group;
11615 int first_time;
11616 struct attribute *attr;
11617 unsigned int i;
11618 struct signatured_type *sig_type;
11619
11620 gdb_assert (per_cu->is_debug_types);
11621 sig_type = (struct signatured_type *) per_cu;
11622
11623 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11624
11625 /* If we're using .gdb_index (includes -readnow) then
11626 per_cu->type_unit_group may not have been set up yet. */
11627 if (sig_type->type_unit_group == NULL)
11628 sig_type->type_unit_group = get_type_unit_group (this, attr);
11629 tu_group = sig_type->type_unit_group;
11630
11631 /* If we've already processed this stmt_list there's no real need to
11632 do it again, we could fake it and just recreate the part we need
11633 (file name,index -> symtab mapping). If data shows this optimization
11634 is useful we can do it then. */
11635 first_time = tu_group->compunit_symtab == NULL;
11636
11637 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11638 debug info. */
11639 line_header_up lh;
11640 if (attr != NULL)
11641 {
11642 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11643 lh = dwarf_decode_line_header (line_offset, this);
11644 }
11645 if (lh == NULL)
11646 {
11647 if (first_time)
11648 start_symtab ("", NULL, 0);
11649 else
11650 {
11651 gdb_assert (tu_group->symtabs == NULL);
11652 gdb_assert (m_builder == nullptr);
11653 struct compunit_symtab *cust = tu_group->compunit_symtab;
11654 m_builder.reset (new struct buildsym_compunit
11655 (COMPUNIT_OBJFILE (cust), "",
11656 COMPUNIT_DIRNAME (cust),
11657 compunit_language (cust),
11658 0, cust));
11659 }
11660 return;
11661 }
11662
11663 line_header = lh.release ();
11664 line_header_die_owner = die;
11665
11666 if (first_time)
11667 {
11668 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11669
11670 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11671 still initializing it, and our caller (a few levels up)
11672 process_full_type_unit still needs to know if this is the first
11673 time. */
11674
11675 tu_group->num_symtabs = line_header->file_names_size ();
11676 tu_group->symtabs = XNEWVEC (struct symtab *,
11677 line_header->file_names_size ());
11678
11679 auto &file_names = line_header->file_names ();
11680 for (i = 0; i < file_names.size (); ++i)
11681 {
11682 file_entry &fe = file_names[i];
11683 dwarf2_start_subfile (this, fe.name,
11684 fe.include_dir (line_header));
11685 buildsym_compunit *b = get_builder ();
11686 if (b->get_current_subfile ()->symtab == NULL)
11687 {
11688 /* NOTE: start_subfile will recognize when it's been
11689 passed a file it has already seen. So we can't
11690 assume there's a simple mapping from
11691 cu->line_header->file_names to subfiles, plus
11692 cu->line_header->file_names may contain dups. */
11693 b->get_current_subfile ()->symtab
11694 = allocate_symtab (cust, b->get_current_subfile ()->name);
11695 }
11696
11697 fe.symtab = b->get_current_subfile ()->symtab;
11698 tu_group->symtabs[i] = fe.symtab;
11699 }
11700 }
11701 else
11702 {
11703 gdb_assert (m_builder == nullptr);
11704 struct compunit_symtab *cust = tu_group->compunit_symtab;
11705 m_builder.reset (new struct buildsym_compunit
11706 (COMPUNIT_OBJFILE (cust), "",
11707 COMPUNIT_DIRNAME (cust),
11708 compunit_language (cust),
11709 0, cust));
11710
11711 auto &file_names = line_header->file_names ();
11712 for (i = 0; i < file_names.size (); ++i)
11713 {
11714 file_entry &fe = file_names[i];
11715 fe.symtab = tu_group->symtabs[i];
11716 }
11717 }
11718
11719 /* The main symtab is allocated last. Type units don't have DW_AT_name
11720 so they don't have a "real" (so to speak) symtab anyway.
11721 There is later code that will assign the main symtab to all symbols
11722 that don't have one. We need to handle the case of a symbol with a
11723 missing symtab (DW_AT_decl_file) anyway. */
11724 }
11725
11726 /* Process DW_TAG_type_unit.
11727 For TUs we want to skip the first top level sibling if it's not the
11728 actual type being defined by this TU. In this case the first top
11729 level sibling is there to provide context only. */
11730
11731 static void
11732 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11733 {
11734 struct die_info *child_die;
11735
11736 prepare_one_comp_unit (cu, die, language_minimal);
11737
11738 /* Initialize (or reinitialize) the machinery for building symtabs.
11739 We do this before processing child DIEs, so that the line header table
11740 is available for DW_AT_decl_file. */
11741 cu->setup_type_unit_groups (die);
11742
11743 if (die->child != NULL)
11744 {
11745 child_die = die->child;
11746 while (child_die && child_die->tag)
11747 {
11748 process_die (child_die, cu);
11749 child_die = sibling_die (child_die);
11750 }
11751 }
11752 }
11753 \f
11754 /* DWO/DWP files.
11755
11756 http://gcc.gnu.org/wiki/DebugFission
11757 http://gcc.gnu.org/wiki/DebugFissionDWP
11758
11759 To simplify handling of both DWO files ("object" files with the DWARF info)
11760 and DWP files (a file with the DWOs packaged up into one file), we treat
11761 DWP files as having a collection of virtual DWO files. */
11762
11763 static hashval_t
11764 hash_dwo_file (const void *item)
11765 {
11766 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11767 hashval_t hash;
11768
11769 hash = htab_hash_string (dwo_file->dwo_name);
11770 if (dwo_file->comp_dir != NULL)
11771 hash += htab_hash_string (dwo_file->comp_dir);
11772 return hash;
11773 }
11774
11775 static int
11776 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11777 {
11778 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11779 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11780
11781 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11782 return 0;
11783 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11784 return lhs->comp_dir == rhs->comp_dir;
11785 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11786 }
11787
11788 /* Allocate a hash table for DWO files. */
11789
11790 static htab_up
11791 allocate_dwo_file_hash_table (struct objfile *objfile)
11792 {
11793 auto delete_dwo_file = [] (void *item)
11794 {
11795 struct dwo_file *dwo_file = (struct dwo_file *) item;
11796
11797 delete dwo_file;
11798 };
11799
11800 return htab_up (htab_create_alloc_ex (41,
11801 hash_dwo_file,
11802 eq_dwo_file,
11803 delete_dwo_file,
11804 &objfile->objfile_obstack,
11805 hashtab_obstack_allocate,
11806 dummy_obstack_deallocate));
11807 }
11808
11809 /* Lookup DWO file DWO_NAME. */
11810
11811 static void **
11812 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11813 const char *dwo_name,
11814 const char *comp_dir)
11815 {
11816 struct dwo_file find_entry;
11817 void **slot;
11818
11819 if (dwarf2_per_objfile->dwo_files == NULL)
11820 dwarf2_per_objfile->dwo_files
11821 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11822
11823 find_entry.dwo_name = dwo_name;
11824 find_entry.comp_dir = comp_dir;
11825 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11826 INSERT);
11827
11828 return slot;
11829 }
11830
11831 static hashval_t
11832 hash_dwo_unit (const void *item)
11833 {
11834 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11835
11836 /* This drops the top 32 bits of the id, but is ok for a hash. */
11837 return dwo_unit->signature;
11838 }
11839
11840 static int
11841 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11842 {
11843 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11844 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11845
11846 /* The signature is assumed to be unique within the DWO file.
11847 So while object file CU dwo_id's always have the value zero,
11848 that's OK, assuming each object file DWO file has only one CU,
11849 and that's the rule for now. */
11850 return lhs->signature == rhs->signature;
11851 }
11852
11853 /* Allocate a hash table for DWO CUs,TUs.
11854 There is one of these tables for each of CUs,TUs for each DWO file. */
11855
11856 static htab_t
11857 allocate_dwo_unit_table (struct objfile *objfile)
11858 {
11859 /* Start out with a pretty small number.
11860 Generally DWO files contain only one CU and maybe some TUs. */
11861 return htab_create_alloc_ex (3,
11862 hash_dwo_unit,
11863 eq_dwo_unit,
11864 NULL,
11865 &objfile->objfile_obstack,
11866 hashtab_obstack_allocate,
11867 dummy_obstack_deallocate);
11868 }
11869
11870 /* die_reader_func for create_dwo_cu. */
11871
11872 static void
11873 create_dwo_cu_reader (const struct die_reader_specs *reader,
11874 const gdb_byte *info_ptr,
11875 struct die_info *comp_unit_die,
11876 int has_children,
11877 struct dwo_file *dwo_file,
11878 struct dwo_unit *dwo_unit)
11879 {
11880 struct dwarf2_cu *cu = reader->cu;
11881 sect_offset sect_off = cu->per_cu->sect_off;
11882 struct dwarf2_section_info *section = cu->per_cu->section;
11883
11884 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11885 if (!signature.has_value ())
11886 {
11887 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11888 " its dwo_id [in module %s]"),
11889 sect_offset_str (sect_off), dwo_file->dwo_name);
11890 return;
11891 }
11892
11893 dwo_unit->dwo_file = dwo_file;
11894 dwo_unit->signature = *signature;
11895 dwo_unit->section = section;
11896 dwo_unit->sect_off = sect_off;
11897 dwo_unit->length = cu->per_cu->length;
11898
11899 if (dwarf_read_debug)
11900 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11901 sect_offset_str (sect_off),
11902 hex_string (dwo_unit->signature));
11903 }
11904
11905 /* Create the dwo_units for the CUs in a DWO_FILE.
11906 Note: This function processes DWO files only, not DWP files. */
11907
11908 static void
11909 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11910 dwarf2_cu *cu, struct dwo_file &dwo_file,
11911 dwarf2_section_info &section, htab_t &cus_htab)
11912 {
11913 struct objfile *objfile = dwarf2_per_objfile->objfile;
11914 const gdb_byte *info_ptr, *end_ptr;
11915
11916 dwarf2_read_section (objfile, &section);
11917 info_ptr = section.buffer;
11918
11919 if (info_ptr == NULL)
11920 return;
11921
11922 if (dwarf_read_debug)
11923 {
11924 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11925 get_section_name (&section),
11926 get_section_file_name (&section));
11927 }
11928
11929 end_ptr = info_ptr + section.size;
11930 while (info_ptr < end_ptr)
11931 {
11932 struct dwarf2_per_cu_data per_cu;
11933 struct dwo_unit read_unit {};
11934 struct dwo_unit *dwo_unit;
11935 void **slot;
11936 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11937
11938 memset (&per_cu, 0, sizeof (per_cu));
11939 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11940 per_cu.is_debug_types = 0;
11941 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11942 per_cu.section = &section;
11943
11944 cutu_reader reader (&per_cu, cu, &dwo_file);
11945 if (!reader.dummy_p)
11946 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11947 reader.has_children, &dwo_file, &read_unit);
11948 info_ptr += per_cu.length;
11949
11950 // If the unit could not be parsed, skip it.
11951 if (read_unit.dwo_file == NULL)
11952 continue;
11953
11954 if (cus_htab == NULL)
11955 cus_htab = allocate_dwo_unit_table (objfile);
11956
11957 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11958 *dwo_unit = read_unit;
11959 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11960 gdb_assert (slot != NULL);
11961 if (*slot != NULL)
11962 {
11963 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11964 sect_offset dup_sect_off = dup_cu->sect_off;
11965
11966 complaint (_("debug cu entry at offset %s is duplicate to"
11967 " the entry at offset %s, signature %s"),
11968 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11969 hex_string (dwo_unit->signature));
11970 }
11971 *slot = (void *)dwo_unit;
11972 }
11973 }
11974
11975 /* DWP file .debug_{cu,tu}_index section format:
11976 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11977
11978 DWP Version 1:
11979
11980 Both index sections have the same format, and serve to map a 64-bit
11981 signature to a set of section numbers. Each section begins with a header,
11982 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11983 indexes, and a pool of 32-bit section numbers. The index sections will be
11984 aligned at 8-byte boundaries in the file.
11985
11986 The index section header consists of:
11987
11988 V, 32 bit version number
11989 -, 32 bits unused
11990 N, 32 bit number of compilation units or type units in the index
11991 M, 32 bit number of slots in the hash table
11992
11993 Numbers are recorded using the byte order of the application binary.
11994
11995 The hash table begins at offset 16 in the section, and consists of an array
11996 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11997 order of the application binary). Unused slots in the hash table are 0.
11998 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11999
12000 The parallel table begins immediately after the hash table
12001 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12002 array of 32-bit indexes (using the byte order of the application binary),
12003 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12004 table contains a 32-bit index into the pool of section numbers. For unused
12005 hash table slots, the corresponding entry in the parallel table will be 0.
12006
12007 The pool of section numbers begins immediately following the hash table
12008 (at offset 16 + 12 * M from the beginning of the section). The pool of
12009 section numbers consists of an array of 32-bit words (using the byte order
12010 of the application binary). Each item in the array is indexed starting
12011 from 0. The hash table entry provides the index of the first section
12012 number in the set. Additional section numbers in the set follow, and the
12013 set is terminated by a 0 entry (section number 0 is not used in ELF).
12014
12015 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12016 section must be the first entry in the set, and the .debug_abbrev.dwo must
12017 be the second entry. Other members of the set may follow in any order.
12018
12019 ---
12020
12021 DWP Version 2:
12022
12023 DWP Version 2 combines all the .debug_info, etc. sections into one,
12024 and the entries in the index tables are now offsets into these sections.
12025 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12026 section.
12027
12028 Index Section Contents:
12029 Header
12030 Hash Table of Signatures dwp_hash_table.hash_table
12031 Parallel Table of Indices dwp_hash_table.unit_table
12032 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12033 Table of Section Sizes dwp_hash_table.v2.sizes
12034
12035 The index section header consists of:
12036
12037 V, 32 bit version number
12038 L, 32 bit number of columns in the table of section offsets
12039 N, 32 bit number of compilation units or type units in the index
12040 M, 32 bit number of slots in the hash table
12041
12042 Numbers are recorded using the byte order of the application binary.
12043
12044 The hash table has the same format as version 1.
12045 The parallel table of indices has the same format as version 1,
12046 except that the entries are origin-1 indices into the table of sections
12047 offsets and the table of section sizes.
12048
12049 The table of offsets begins immediately following the parallel table
12050 (at offset 16 + 12 * M from the beginning of the section). The table is
12051 a two-dimensional array of 32-bit words (using the byte order of the
12052 application binary), with L columns and N+1 rows, in row-major order.
12053 Each row in the array is indexed starting from 0. The first row provides
12054 a key to the remaining rows: each column in this row provides an identifier
12055 for a debug section, and the offsets in the same column of subsequent rows
12056 refer to that section. The section identifiers are:
12057
12058 DW_SECT_INFO 1 .debug_info.dwo
12059 DW_SECT_TYPES 2 .debug_types.dwo
12060 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12061 DW_SECT_LINE 4 .debug_line.dwo
12062 DW_SECT_LOC 5 .debug_loc.dwo
12063 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12064 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12065 DW_SECT_MACRO 8 .debug_macro.dwo
12066
12067 The offsets provided by the CU and TU index sections are the base offsets
12068 for the contributions made by each CU or TU to the corresponding section
12069 in the package file. Each CU and TU header contains an abbrev_offset
12070 field, used to find the abbreviations table for that CU or TU within the
12071 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12072 be interpreted as relative to the base offset given in the index section.
12073 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12074 should be interpreted as relative to the base offset for .debug_line.dwo,
12075 and offsets into other debug sections obtained from DWARF attributes should
12076 also be interpreted as relative to the corresponding base offset.
12077
12078 The table of sizes begins immediately following the table of offsets.
12079 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12080 with L columns and N rows, in row-major order. Each row in the array is
12081 indexed starting from 1 (row 0 is shared by the two tables).
12082
12083 ---
12084
12085 Hash table lookup is handled the same in version 1 and 2:
12086
12087 We assume that N and M will not exceed 2^32 - 1.
12088 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12089
12090 Given a 64-bit compilation unit signature or a type signature S, an entry
12091 in the hash table is located as follows:
12092
12093 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12094 the low-order k bits all set to 1.
12095
12096 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12097
12098 3) If the hash table entry at index H matches the signature, use that
12099 entry. If the hash table entry at index H is unused (all zeroes),
12100 terminate the search: the signature is not present in the table.
12101
12102 4) Let H = (H + H') modulo M. Repeat at Step 3.
12103
12104 Because M > N and H' and M are relatively prime, the search is guaranteed
12105 to stop at an unused slot or find the match. */
12106
12107 /* Create a hash table to map DWO IDs to their CU/TU entry in
12108 .debug_{info,types}.dwo in DWP_FILE.
12109 Returns NULL if there isn't one.
12110 Note: This function processes DWP files only, not DWO files. */
12111
12112 static struct dwp_hash_table *
12113 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12114 struct dwp_file *dwp_file, int is_debug_types)
12115 {
12116 struct objfile *objfile = dwarf2_per_objfile->objfile;
12117 bfd *dbfd = dwp_file->dbfd.get ();
12118 const gdb_byte *index_ptr, *index_end;
12119 struct dwarf2_section_info *index;
12120 uint32_t version, nr_columns, nr_units, nr_slots;
12121 struct dwp_hash_table *htab;
12122
12123 if (is_debug_types)
12124 index = &dwp_file->sections.tu_index;
12125 else
12126 index = &dwp_file->sections.cu_index;
12127
12128 if (dwarf2_section_empty_p (index))
12129 return NULL;
12130 dwarf2_read_section (objfile, index);
12131
12132 index_ptr = index->buffer;
12133 index_end = index_ptr + index->size;
12134
12135 version = read_4_bytes (dbfd, index_ptr);
12136 index_ptr += 4;
12137 if (version == 2)
12138 nr_columns = read_4_bytes (dbfd, index_ptr);
12139 else
12140 nr_columns = 0;
12141 index_ptr += 4;
12142 nr_units = read_4_bytes (dbfd, index_ptr);
12143 index_ptr += 4;
12144 nr_slots = read_4_bytes (dbfd, index_ptr);
12145 index_ptr += 4;
12146
12147 if (version != 1 && version != 2)
12148 {
12149 error (_("Dwarf Error: unsupported DWP file version (%s)"
12150 " [in module %s]"),
12151 pulongest (version), dwp_file->name);
12152 }
12153 if (nr_slots != (nr_slots & -nr_slots))
12154 {
12155 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12156 " is not power of 2 [in module %s]"),
12157 pulongest (nr_slots), dwp_file->name);
12158 }
12159
12160 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12161 htab->version = version;
12162 htab->nr_columns = nr_columns;
12163 htab->nr_units = nr_units;
12164 htab->nr_slots = nr_slots;
12165 htab->hash_table = index_ptr;
12166 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12167
12168 /* Exit early if the table is empty. */
12169 if (nr_slots == 0 || nr_units == 0
12170 || (version == 2 && nr_columns == 0))
12171 {
12172 /* All must be zero. */
12173 if (nr_slots != 0 || nr_units != 0
12174 || (version == 2 && nr_columns != 0))
12175 {
12176 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
12177 " all zero [in modules %s]"),
12178 dwp_file->name);
12179 }
12180 return htab;
12181 }
12182
12183 if (version == 1)
12184 {
12185 htab->section_pool.v1.indices =
12186 htab->unit_table + sizeof (uint32_t) * nr_slots;
12187 /* It's harder to decide whether the section is too small in v1.
12188 V1 is deprecated anyway so we punt. */
12189 }
12190 else
12191 {
12192 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12193 int *ids = htab->section_pool.v2.section_ids;
12194 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
12195 /* Reverse map for error checking. */
12196 int ids_seen[DW_SECT_MAX + 1];
12197 int i;
12198
12199 if (nr_columns < 2)
12200 {
12201 error (_("Dwarf Error: bad DWP hash table, too few columns"
12202 " in section table [in module %s]"),
12203 dwp_file->name);
12204 }
12205 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12206 {
12207 error (_("Dwarf Error: bad DWP hash table, too many columns"
12208 " in section table [in module %s]"),
12209 dwp_file->name);
12210 }
12211 memset (ids, 255, sizeof_ids);
12212 memset (ids_seen, 255, sizeof (ids_seen));
12213 for (i = 0; i < nr_columns; ++i)
12214 {
12215 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12216
12217 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12218 {
12219 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12220 " in section table [in module %s]"),
12221 id, dwp_file->name);
12222 }
12223 if (ids_seen[id] != -1)
12224 {
12225 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12226 " id %d in section table [in module %s]"),
12227 id, dwp_file->name);
12228 }
12229 ids_seen[id] = i;
12230 ids[i] = id;
12231 }
12232 /* Must have exactly one info or types section. */
12233 if (((ids_seen[DW_SECT_INFO] != -1)
12234 + (ids_seen[DW_SECT_TYPES] != -1))
12235 != 1)
12236 {
12237 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12238 " DWO info/types section [in module %s]"),
12239 dwp_file->name);
12240 }
12241 /* Must have an abbrev section. */
12242 if (ids_seen[DW_SECT_ABBREV] == -1)
12243 {
12244 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12245 " section [in module %s]"),
12246 dwp_file->name);
12247 }
12248 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12249 htab->section_pool.v2.sizes =
12250 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12251 * nr_units * nr_columns);
12252 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12253 * nr_units * nr_columns))
12254 > index_end)
12255 {
12256 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12257 " [in module %s]"),
12258 dwp_file->name);
12259 }
12260 }
12261
12262 return htab;
12263 }
12264
12265 /* Update SECTIONS with the data from SECTP.
12266
12267 This function is like the other "locate" section routines that are
12268 passed to bfd_map_over_sections, but in this context the sections to
12269 read comes from the DWP V1 hash table, not the full ELF section table.
12270
12271 The result is non-zero for success, or zero if an error was found. */
12272
12273 static int
12274 locate_v1_virtual_dwo_sections (asection *sectp,
12275 struct virtual_v1_dwo_sections *sections)
12276 {
12277 const struct dwop_section_names *names = &dwop_section_names;
12278
12279 if (section_is_p (sectp->name, &names->abbrev_dwo))
12280 {
12281 /* There can be only one. */
12282 if (sections->abbrev.s.section != NULL)
12283 return 0;
12284 sections->abbrev.s.section = sectp;
12285 sections->abbrev.size = bfd_section_size (sectp);
12286 }
12287 else if (section_is_p (sectp->name, &names->info_dwo)
12288 || section_is_p (sectp->name, &names->types_dwo))
12289 {
12290 /* There can be only one. */
12291 if (sections->info_or_types.s.section != NULL)
12292 return 0;
12293 sections->info_or_types.s.section = sectp;
12294 sections->info_or_types.size = bfd_section_size (sectp);
12295 }
12296 else if (section_is_p (sectp->name, &names->line_dwo))
12297 {
12298 /* There can be only one. */
12299 if (sections->line.s.section != NULL)
12300 return 0;
12301 sections->line.s.section = sectp;
12302 sections->line.size = bfd_section_size (sectp);
12303 }
12304 else if (section_is_p (sectp->name, &names->loc_dwo))
12305 {
12306 /* There can be only one. */
12307 if (sections->loc.s.section != NULL)
12308 return 0;
12309 sections->loc.s.section = sectp;
12310 sections->loc.size = bfd_section_size (sectp);
12311 }
12312 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12313 {
12314 /* There can be only one. */
12315 if (sections->macinfo.s.section != NULL)
12316 return 0;
12317 sections->macinfo.s.section = sectp;
12318 sections->macinfo.size = bfd_section_size (sectp);
12319 }
12320 else if (section_is_p (sectp->name, &names->macro_dwo))
12321 {
12322 /* There can be only one. */
12323 if (sections->macro.s.section != NULL)
12324 return 0;
12325 sections->macro.s.section = sectp;
12326 sections->macro.size = bfd_section_size (sectp);
12327 }
12328 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12329 {
12330 /* There can be only one. */
12331 if (sections->str_offsets.s.section != NULL)
12332 return 0;
12333 sections->str_offsets.s.section = sectp;
12334 sections->str_offsets.size = bfd_section_size (sectp);
12335 }
12336 else
12337 {
12338 /* No other kind of section is valid. */
12339 return 0;
12340 }
12341
12342 return 1;
12343 }
12344
12345 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12346 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12347 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12348 This is for DWP version 1 files. */
12349
12350 static struct dwo_unit *
12351 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12352 struct dwp_file *dwp_file,
12353 uint32_t unit_index,
12354 const char *comp_dir,
12355 ULONGEST signature, int is_debug_types)
12356 {
12357 struct objfile *objfile = dwarf2_per_objfile->objfile;
12358 const struct dwp_hash_table *dwp_htab =
12359 is_debug_types ? dwp_file->tus : dwp_file->cus;
12360 bfd *dbfd = dwp_file->dbfd.get ();
12361 const char *kind = is_debug_types ? "TU" : "CU";
12362 struct dwo_file *dwo_file;
12363 struct dwo_unit *dwo_unit;
12364 struct virtual_v1_dwo_sections sections;
12365 void **dwo_file_slot;
12366 int i;
12367
12368 gdb_assert (dwp_file->version == 1);
12369
12370 if (dwarf_read_debug)
12371 {
12372 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12373 kind,
12374 pulongest (unit_index), hex_string (signature),
12375 dwp_file->name);
12376 }
12377
12378 /* Fetch the sections of this DWO unit.
12379 Put a limit on the number of sections we look for so that bad data
12380 doesn't cause us to loop forever. */
12381
12382 #define MAX_NR_V1_DWO_SECTIONS \
12383 (1 /* .debug_info or .debug_types */ \
12384 + 1 /* .debug_abbrev */ \
12385 + 1 /* .debug_line */ \
12386 + 1 /* .debug_loc */ \
12387 + 1 /* .debug_str_offsets */ \
12388 + 1 /* .debug_macro or .debug_macinfo */ \
12389 + 1 /* trailing zero */)
12390
12391 memset (&sections, 0, sizeof (sections));
12392
12393 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12394 {
12395 asection *sectp;
12396 uint32_t section_nr =
12397 read_4_bytes (dbfd,
12398 dwp_htab->section_pool.v1.indices
12399 + (unit_index + i) * sizeof (uint32_t));
12400
12401 if (section_nr == 0)
12402 break;
12403 if (section_nr >= dwp_file->num_sections)
12404 {
12405 error (_("Dwarf Error: bad DWP hash table, section number too large"
12406 " [in module %s]"),
12407 dwp_file->name);
12408 }
12409
12410 sectp = dwp_file->elf_sections[section_nr];
12411 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12412 {
12413 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12414 " [in module %s]"),
12415 dwp_file->name);
12416 }
12417 }
12418
12419 if (i < 2
12420 || dwarf2_section_empty_p (&sections.info_or_types)
12421 || dwarf2_section_empty_p (&sections.abbrev))
12422 {
12423 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12424 " [in module %s]"),
12425 dwp_file->name);
12426 }
12427 if (i == MAX_NR_V1_DWO_SECTIONS)
12428 {
12429 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12430 " [in module %s]"),
12431 dwp_file->name);
12432 }
12433
12434 /* It's easier for the rest of the code if we fake a struct dwo_file and
12435 have dwo_unit "live" in that. At least for now.
12436
12437 The DWP file can be made up of a random collection of CUs and TUs.
12438 However, for each CU + set of TUs that came from the same original DWO
12439 file, we can combine them back into a virtual DWO file to save space
12440 (fewer struct dwo_file objects to allocate). Remember that for really
12441 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12442
12443 std::string virtual_dwo_name =
12444 string_printf ("virtual-dwo/%d-%d-%d-%d",
12445 get_section_id (&sections.abbrev),
12446 get_section_id (&sections.line),
12447 get_section_id (&sections.loc),
12448 get_section_id (&sections.str_offsets));
12449 /* Can we use an existing virtual DWO file? */
12450 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12451 virtual_dwo_name.c_str (),
12452 comp_dir);
12453 /* Create one if necessary. */
12454 if (*dwo_file_slot == NULL)
12455 {
12456 if (dwarf_read_debug)
12457 {
12458 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12459 virtual_dwo_name.c_str ());
12460 }
12461 dwo_file = new struct dwo_file;
12462 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12463 virtual_dwo_name);
12464 dwo_file->comp_dir = comp_dir;
12465 dwo_file->sections.abbrev = sections.abbrev;
12466 dwo_file->sections.line = sections.line;
12467 dwo_file->sections.loc = sections.loc;
12468 dwo_file->sections.macinfo = sections.macinfo;
12469 dwo_file->sections.macro = sections.macro;
12470 dwo_file->sections.str_offsets = sections.str_offsets;
12471 /* The "str" section is global to the entire DWP file. */
12472 dwo_file->sections.str = dwp_file->sections.str;
12473 /* The info or types section is assigned below to dwo_unit,
12474 there's no need to record it in dwo_file.
12475 Also, we can't simply record type sections in dwo_file because
12476 we record a pointer into the vector in dwo_unit. As we collect more
12477 types we'll grow the vector and eventually have to reallocate space
12478 for it, invalidating all copies of pointers into the previous
12479 contents. */
12480 *dwo_file_slot = dwo_file;
12481 }
12482 else
12483 {
12484 if (dwarf_read_debug)
12485 {
12486 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12487 virtual_dwo_name.c_str ());
12488 }
12489 dwo_file = (struct dwo_file *) *dwo_file_slot;
12490 }
12491
12492 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12493 dwo_unit->dwo_file = dwo_file;
12494 dwo_unit->signature = signature;
12495 dwo_unit->section =
12496 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12497 *dwo_unit->section = sections.info_or_types;
12498 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12499
12500 return dwo_unit;
12501 }
12502
12503 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12504 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12505 piece within that section used by a TU/CU, return a virtual section
12506 of just that piece. */
12507
12508 static struct dwarf2_section_info
12509 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12510 struct dwarf2_section_info *section,
12511 bfd_size_type offset, bfd_size_type size)
12512 {
12513 struct dwarf2_section_info result;
12514 asection *sectp;
12515
12516 gdb_assert (section != NULL);
12517 gdb_assert (!section->is_virtual);
12518
12519 memset (&result, 0, sizeof (result));
12520 result.s.containing_section = section;
12521 result.is_virtual = true;
12522
12523 if (size == 0)
12524 return result;
12525
12526 sectp = get_section_bfd_section (section);
12527
12528 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12529 bounds of the real section. This is a pretty-rare event, so just
12530 flag an error (easier) instead of a warning and trying to cope. */
12531 if (sectp == NULL
12532 || offset + size > bfd_section_size (sectp))
12533 {
12534 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12535 " in section %s [in module %s]"),
12536 sectp ? bfd_section_name (sectp) : "<unknown>",
12537 objfile_name (dwarf2_per_objfile->objfile));
12538 }
12539
12540 result.virtual_offset = offset;
12541 result.size = size;
12542 return result;
12543 }
12544
12545 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12546 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12547 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12548 This is for DWP version 2 files. */
12549
12550 static struct dwo_unit *
12551 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12552 struct dwp_file *dwp_file,
12553 uint32_t unit_index,
12554 const char *comp_dir,
12555 ULONGEST signature, int is_debug_types)
12556 {
12557 struct objfile *objfile = dwarf2_per_objfile->objfile;
12558 const struct dwp_hash_table *dwp_htab =
12559 is_debug_types ? dwp_file->tus : dwp_file->cus;
12560 bfd *dbfd = dwp_file->dbfd.get ();
12561 const char *kind = is_debug_types ? "TU" : "CU";
12562 struct dwo_file *dwo_file;
12563 struct dwo_unit *dwo_unit;
12564 struct virtual_v2_dwo_sections sections;
12565 void **dwo_file_slot;
12566 int i;
12567
12568 gdb_assert (dwp_file->version == 2);
12569
12570 if (dwarf_read_debug)
12571 {
12572 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12573 kind,
12574 pulongest (unit_index), hex_string (signature),
12575 dwp_file->name);
12576 }
12577
12578 /* Fetch the section offsets of this DWO unit. */
12579
12580 memset (&sections, 0, sizeof (sections));
12581
12582 for (i = 0; i < dwp_htab->nr_columns; ++i)
12583 {
12584 uint32_t offset = read_4_bytes (dbfd,
12585 dwp_htab->section_pool.v2.offsets
12586 + (((unit_index - 1) * dwp_htab->nr_columns
12587 + i)
12588 * sizeof (uint32_t)));
12589 uint32_t size = read_4_bytes (dbfd,
12590 dwp_htab->section_pool.v2.sizes
12591 + (((unit_index - 1) * dwp_htab->nr_columns
12592 + i)
12593 * sizeof (uint32_t)));
12594
12595 switch (dwp_htab->section_pool.v2.section_ids[i])
12596 {
12597 case DW_SECT_INFO:
12598 case DW_SECT_TYPES:
12599 sections.info_or_types_offset = offset;
12600 sections.info_or_types_size = size;
12601 break;
12602 case DW_SECT_ABBREV:
12603 sections.abbrev_offset = offset;
12604 sections.abbrev_size = size;
12605 break;
12606 case DW_SECT_LINE:
12607 sections.line_offset = offset;
12608 sections.line_size = size;
12609 break;
12610 case DW_SECT_LOC:
12611 sections.loc_offset = offset;
12612 sections.loc_size = size;
12613 break;
12614 case DW_SECT_STR_OFFSETS:
12615 sections.str_offsets_offset = offset;
12616 sections.str_offsets_size = size;
12617 break;
12618 case DW_SECT_MACINFO:
12619 sections.macinfo_offset = offset;
12620 sections.macinfo_size = size;
12621 break;
12622 case DW_SECT_MACRO:
12623 sections.macro_offset = offset;
12624 sections.macro_size = size;
12625 break;
12626 }
12627 }
12628
12629 /* It's easier for the rest of the code if we fake a struct dwo_file and
12630 have dwo_unit "live" in that. At least for now.
12631
12632 The DWP file can be made up of a random collection of CUs and TUs.
12633 However, for each CU + set of TUs that came from the same original DWO
12634 file, we can combine them back into a virtual DWO file to save space
12635 (fewer struct dwo_file objects to allocate). Remember that for really
12636 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12637
12638 std::string virtual_dwo_name =
12639 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12640 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12641 (long) (sections.line_size ? sections.line_offset : 0),
12642 (long) (sections.loc_size ? sections.loc_offset : 0),
12643 (long) (sections.str_offsets_size
12644 ? sections.str_offsets_offset : 0));
12645 /* Can we use an existing virtual DWO file? */
12646 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12647 virtual_dwo_name.c_str (),
12648 comp_dir);
12649 /* Create one if necessary. */
12650 if (*dwo_file_slot == NULL)
12651 {
12652 if (dwarf_read_debug)
12653 {
12654 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12655 virtual_dwo_name.c_str ());
12656 }
12657 dwo_file = new struct dwo_file;
12658 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12659 virtual_dwo_name);
12660 dwo_file->comp_dir = comp_dir;
12661 dwo_file->sections.abbrev =
12662 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12663 sections.abbrev_offset, sections.abbrev_size);
12664 dwo_file->sections.line =
12665 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12666 sections.line_offset, sections.line_size);
12667 dwo_file->sections.loc =
12668 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12669 sections.loc_offset, sections.loc_size);
12670 dwo_file->sections.macinfo =
12671 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12672 sections.macinfo_offset, sections.macinfo_size);
12673 dwo_file->sections.macro =
12674 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12675 sections.macro_offset, sections.macro_size);
12676 dwo_file->sections.str_offsets =
12677 create_dwp_v2_section (dwarf2_per_objfile,
12678 &dwp_file->sections.str_offsets,
12679 sections.str_offsets_offset,
12680 sections.str_offsets_size);
12681 /* The "str" section is global to the entire DWP file. */
12682 dwo_file->sections.str = dwp_file->sections.str;
12683 /* The info or types section is assigned below to dwo_unit,
12684 there's no need to record it in dwo_file.
12685 Also, we can't simply record type sections in dwo_file because
12686 we record a pointer into the vector in dwo_unit. As we collect more
12687 types we'll grow the vector and eventually have to reallocate space
12688 for it, invalidating all copies of pointers into the previous
12689 contents. */
12690 *dwo_file_slot = dwo_file;
12691 }
12692 else
12693 {
12694 if (dwarf_read_debug)
12695 {
12696 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12697 virtual_dwo_name.c_str ());
12698 }
12699 dwo_file = (struct dwo_file *) *dwo_file_slot;
12700 }
12701
12702 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12703 dwo_unit->dwo_file = dwo_file;
12704 dwo_unit->signature = signature;
12705 dwo_unit->section =
12706 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12707 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12708 is_debug_types
12709 ? &dwp_file->sections.types
12710 : &dwp_file->sections.info,
12711 sections.info_or_types_offset,
12712 sections.info_or_types_size);
12713 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12714
12715 return dwo_unit;
12716 }
12717
12718 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12719 Returns NULL if the signature isn't found. */
12720
12721 static struct dwo_unit *
12722 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12723 struct dwp_file *dwp_file, const char *comp_dir,
12724 ULONGEST signature, int is_debug_types)
12725 {
12726 const struct dwp_hash_table *dwp_htab =
12727 is_debug_types ? dwp_file->tus : dwp_file->cus;
12728 bfd *dbfd = dwp_file->dbfd.get ();
12729 uint32_t mask = dwp_htab->nr_slots - 1;
12730 uint32_t hash = signature & mask;
12731 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12732 unsigned int i;
12733 void **slot;
12734 struct dwo_unit find_dwo_cu;
12735
12736 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12737 find_dwo_cu.signature = signature;
12738 slot = htab_find_slot (is_debug_types
12739 ? dwp_file->loaded_tus
12740 : dwp_file->loaded_cus,
12741 &find_dwo_cu, INSERT);
12742
12743 if (*slot != NULL)
12744 return (struct dwo_unit *) *slot;
12745
12746 /* Use a for loop so that we don't loop forever on bad debug info. */
12747 for (i = 0; i < dwp_htab->nr_slots; ++i)
12748 {
12749 ULONGEST signature_in_table;
12750
12751 signature_in_table =
12752 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12753 if (signature_in_table == signature)
12754 {
12755 uint32_t unit_index =
12756 read_4_bytes (dbfd,
12757 dwp_htab->unit_table + hash * sizeof (uint32_t));
12758
12759 if (dwp_file->version == 1)
12760 {
12761 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12762 dwp_file, unit_index,
12763 comp_dir, signature,
12764 is_debug_types);
12765 }
12766 else
12767 {
12768 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12769 dwp_file, unit_index,
12770 comp_dir, signature,
12771 is_debug_types);
12772 }
12773 return (struct dwo_unit *) *slot;
12774 }
12775 if (signature_in_table == 0)
12776 return NULL;
12777 hash = (hash + hash2) & mask;
12778 }
12779
12780 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12781 " [in module %s]"),
12782 dwp_file->name);
12783 }
12784
12785 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12786 Open the file specified by FILE_NAME and hand it off to BFD for
12787 preliminary analysis. Return a newly initialized bfd *, which
12788 includes a canonicalized copy of FILE_NAME.
12789 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12790 SEARCH_CWD is true if the current directory is to be searched.
12791 It will be searched before debug-file-directory.
12792 If successful, the file is added to the bfd include table of the
12793 objfile's bfd (see gdb_bfd_record_inclusion).
12794 If unable to find/open the file, return NULL.
12795 NOTE: This function is derived from symfile_bfd_open. */
12796
12797 static gdb_bfd_ref_ptr
12798 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12799 const char *file_name, int is_dwp, int search_cwd)
12800 {
12801 int desc;
12802 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12803 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12804 to debug_file_directory. */
12805 const char *search_path;
12806 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12807
12808 gdb::unique_xmalloc_ptr<char> search_path_holder;
12809 if (search_cwd)
12810 {
12811 if (*debug_file_directory != '\0')
12812 {
12813 search_path_holder.reset (concat (".", dirname_separator_string,
12814 debug_file_directory,
12815 (char *) NULL));
12816 search_path = search_path_holder.get ();
12817 }
12818 else
12819 search_path = ".";
12820 }
12821 else
12822 search_path = debug_file_directory;
12823
12824 openp_flags flags = OPF_RETURN_REALPATH;
12825 if (is_dwp)
12826 flags |= OPF_SEARCH_IN_PATH;
12827
12828 gdb::unique_xmalloc_ptr<char> absolute_name;
12829 desc = openp (search_path, flags, file_name,
12830 O_RDONLY | O_BINARY, &absolute_name);
12831 if (desc < 0)
12832 return NULL;
12833
12834 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12835 gnutarget, desc));
12836 if (sym_bfd == NULL)
12837 return NULL;
12838 bfd_set_cacheable (sym_bfd.get (), 1);
12839
12840 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12841 return NULL;
12842
12843 /* Success. Record the bfd as having been included by the objfile's bfd.
12844 This is important because things like demangled_names_hash lives in the
12845 objfile's per_bfd space and may have references to things like symbol
12846 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12847 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12848
12849 return sym_bfd;
12850 }
12851
12852 /* Try to open DWO file FILE_NAME.
12853 COMP_DIR is the DW_AT_comp_dir attribute.
12854 The result is the bfd handle of the file.
12855 If there is a problem finding or opening the file, return NULL.
12856 Upon success, the canonicalized path of the file is stored in the bfd,
12857 same as symfile_bfd_open. */
12858
12859 static gdb_bfd_ref_ptr
12860 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12861 const char *file_name, const char *comp_dir)
12862 {
12863 if (IS_ABSOLUTE_PATH (file_name))
12864 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12865 0 /*is_dwp*/, 0 /*search_cwd*/);
12866
12867 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12868
12869 if (comp_dir != NULL)
12870 {
12871 gdb::unique_xmalloc_ptr<char> path_to_try
12872 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12873
12874 /* NOTE: If comp_dir is a relative path, this will also try the
12875 search path, which seems useful. */
12876 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12877 path_to_try.get (),
12878 0 /*is_dwp*/,
12879 1 /*search_cwd*/));
12880 if (abfd != NULL)
12881 return abfd;
12882 }
12883
12884 /* That didn't work, try debug-file-directory, which, despite its name,
12885 is a list of paths. */
12886
12887 if (*debug_file_directory == '\0')
12888 return NULL;
12889
12890 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12891 0 /*is_dwp*/, 1 /*search_cwd*/);
12892 }
12893
12894 /* This function is mapped across the sections and remembers the offset and
12895 size of each of the DWO debugging sections we are interested in. */
12896
12897 static void
12898 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12899 {
12900 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12901 const struct dwop_section_names *names = &dwop_section_names;
12902
12903 if (section_is_p (sectp->name, &names->abbrev_dwo))
12904 {
12905 dwo_sections->abbrev.s.section = sectp;
12906 dwo_sections->abbrev.size = bfd_section_size (sectp);
12907 }
12908 else if (section_is_p (sectp->name, &names->info_dwo))
12909 {
12910 dwo_sections->info.s.section = sectp;
12911 dwo_sections->info.size = bfd_section_size (sectp);
12912 }
12913 else if (section_is_p (sectp->name, &names->line_dwo))
12914 {
12915 dwo_sections->line.s.section = sectp;
12916 dwo_sections->line.size = bfd_section_size (sectp);
12917 }
12918 else if (section_is_p (sectp->name, &names->loc_dwo))
12919 {
12920 dwo_sections->loc.s.section = sectp;
12921 dwo_sections->loc.size = bfd_section_size (sectp);
12922 }
12923 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12924 {
12925 dwo_sections->macinfo.s.section = sectp;
12926 dwo_sections->macinfo.size = bfd_section_size (sectp);
12927 }
12928 else if (section_is_p (sectp->name, &names->macro_dwo))
12929 {
12930 dwo_sections->macro.s.section = sectp;
12931 dwo_sections->macro.size = bfd_section_size (sectp);
12932 }
12933 else if (section_is_p (sectp->name, &names->str_dwo))
12934 {
12935 dwo_sections->str.s.section = sectp;
12936 dwo_sections->str.size = bfd_section_size (sectp);
12937 }
12938 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12939 {
12940 dwo_sections->str_offsets.s.section = sectp;
12941 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12942 }
12943 else if (section_is_p (sectp->name, &names->types_dwo))
12944 {
12945 struct dwarf2_section_info type_section;
12946
12947 memset (&type_section, 0, sizeof (type_section));
12948 type_section.s.section = sectp;
12949 type_section.size = bfd_section_size (sectp);
12950 dwo_sections->types.push_back (type_section);
12951 }
12952 }
12953
12954 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12955 by PER_CU. This is for the non-DWP case.
12956 The result is NULL if DWO_NAME can't be found. */
12957
12958 static struct dwo_file *
12959 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12960 const char *dwo_name, const char *comp_dir)
12961 {
12962 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12963
12964 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12965 if (dbfd == NULL)
12966 {
12967 if (dwarf_read_debug)
12968 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12969 return NULL;
12970 }
12971
12972 dwo_file_up dwo_file (new struct dwo_file);
12973 dwo_file->dwo_name = dwo_name;
12974 dwo_file->comp_dir = comp_dir;
12975 dwo_file->dbfd = std::move (dbfd);
12976
12977 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12978 &dwo_file->sections);
12979
12980 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12981 dwo_file->sections.info, dwo_file->cus);
12982
12983 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12984 dwo_file->sections.types, dwo_file->tus);
12985
12986 if (dwarf_read_debug)
12987 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12988
12989 return dwo_file.release ();
12990 }
12991
12992 /* This function is mapped across the sections and remembers the offset and
12993 size of each of the DWP debugging sections common to version 1 and 2 that
12994 we are interested in. */
12995
12996 static void
12997 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12998 void *dwp_file_ptr)
12999 {
13000 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13001 const struct dwop_section_names *names = &dwop_section_names;
13002 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13003
13004 /* Record the ELF section number for later lookup: this is what the
13005 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13006 gdb_assert (elf_section_nr < dwp_file->num_sections);
13007 dwp_file->elf_sections[elf_section_nr] = sectp;
13008
13009 /* Look for specific sections that we need. */
13010 if (section_is_p (sectp->name, &names->str_dwo))
13011 {
13012 dwp_file->sections.str.s.section = sectp;
13013 dwp_file->sections.str.size = bfd_section_size (sectp);
13014 }
13015 else if (section_is_p (sectp->name, &names->cu_index))
13016 {
13017 dwp_file->sections.cu_index.s.section = sectp;
13018 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
13019 }
13020 else if (section_is_p (sectp->name, &names->tu_index))
13021 {
13022 dwp_file->sections.tu_index.s.section = sectp;
13023 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
13024 }
13025 }
13026
13027 /* This function is mapped across the sections and remembers the offset and
13028 size of each of the DWP version 2 debugging sections that we are interested
13029 in. This is split into a separate function because we don't know if we
13030 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13031
13032 static void
13033 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13034 {
13035 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13036 const struct dwop_section_names *names = &dwop_section_names;
13037 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13038
13039 /* Record the ELF section number for later lookup: this is what the
13040 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13041 gdb_assert (elf_section_nr < dwp_file->num_sections);
13042 dwp_file->elf_sections[elf_section_nr] = sectp;
13043
13044 /* Look for specific sections that we need. */
13045 if (section_is_p (sectp->name, &names->abbrev_dwo))
13046 {
13047 dwp_file->sections.abbrev.s.section = sectp;
13048 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13049 }
13050 else if (section_is_p (sectp->name, &names->info_dwo))
13051 {
13052 dwp_file->sections.info.s.section = sectp;
13053 dwp_file->sections.info.size = bfd_section_size (sectp);
13054 }
13055 else if (section_is_p (sectp->name, &names->line_dwo))
13056 {
13057 dwp_file->sections.line.s.section = sectp;
13058 dwp_file->sections.line.size = bfd_section_size (sectp);
13059 }
13060 else if (section_is_p (sectp->name, &names->loc_dwo))
13061 {
13062 dwp_file->sections.loc.s.section = sectp;
13063 dwp_file->sections.loc.size = bfd_section_size (sectp);
13064 }
13065 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13066 {
13067 dwp_file->sections.macinfo.s.section = sectp;
13068 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
13069 }
13070 else if (section_is_p (sectp->name, &names->macro_dwo))
13071 {
13072 dwp_file->sections.macro.s.section = sectp;
13073 dwp_file->sections.macro.size = bfd_section_size (sectp);
13074 }
13075 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13076 {
13077 dwp_file->sections.str_offsets.s.section = sectp;
13078 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13079 }
13080 else if (section_is_p (sectp->name, &names->types_dwo))
13081 {
13082 dwp_file->sections.types.s.section = sectp;
13083 dwp_file->sections.types.size = bfd_section_size (sectp);
13084 }
13085 }
13086
13087 /* Hash function for dwp_file loaded CUs/TUs. */
13088
13089 static hashval_t
13090 hash_dwp_loaded_cutus (const void *item)
13091 {
13092 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13093
13094 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13095 return dwo_unit->signature;
13096 }
13097
13098 /* Equality function for dwp_file loaded CUs/TUs. */
13099
13100 static int
13101 eq_dwp_loaded_cutus (const void *a, const void *b)
13102 {
13103 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13104 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13105
13106 return dua->signature == dub->signature;
13107 }
13108
13109 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13110
13111 static htab_t
13112 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13113 {
13114 return htab_create_alloc_ex (3,
13115 hash_dwp_loaded_cutus,
13116 eq_dwp_loaded_cutus,
13117 NULL,
13118 &objfile->objfile_obstack,
13119 hashtab_obstack_allocate,
13120 dummy_obstack_deallocate);
13121 }
13122
13123 /* Try to open DWP file FILE_NAME.
13124 The result is the bfd handle of the file.
13125 If there is a problem finding or opening the file, return NULL.
13126 Upon success, the canonicalized path of the file is stored in the bfd,
13127 same as symfile_bfd_open. */
13128
13129 static gdb_bfd_ref_ptr
13130 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13131 const char *file_name)
13132 {
13133 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13134 1 /*is_dwp*/,
13135 1 /*search_cwd*/));
13136 if (abfd != NULL)
13137 return abfd;
13138
13139 /* Work around upstream bug 15652.
13140 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13141 [Whether that's a "bug" is debatable, but it is getting in our way.]
13142 We have no real idea where the dwp file is, because gdb's realpath-ing
13143 of the executable's path may have discarded the needed info.
13144 [IWBN if the dwp file name was recorded in the executable, akin to
13145 .gnu_debuglink, but that doesn't exist yet.]
13146 Strip the directory from FILE_NAME and search again. */
13147 if (*debug_file_directory != '\0')
13148 {
13149 /* Don't implicitly search the current directory here.
13150 If the user wants to search "." to handle this case,
13151 it must be added to debug-file-directory. */
13152 return try_open_dwop_file (dwarf2_per_objfile,
13153 lbasename (file_name), 1 /*is_dwp*/,
13154 0 /*search_cwd*/);
13155 }
13156
13157 return NULL;
13158 }
13159
13160 /* Initialize the use of the DWP file for the current objfile.
13161 By convention the name of the DWP file is ${objfile}.dwp.
13162 The result is NULL if it can't be found. */
13163
13164 static std::unique_ptr<struct dwp_file>
13165 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13166 {
13167 struct objfile *objfile = dwarf2_per_objfile->objfile;
13168
13169 /* Try to find first .dwp for the binary file before any symbolic links
13170 resolving. */
13171
13172 /* If the objfile is a debug file, find the name of the real binary
13173 file and get the name of dwp file from there. */
13174 std::string dwp_name;
13175 if (objfile->separate_debug_objfile_backlink != NULL)
13176 {
13177 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13178 const char *backlink_basename = lbasename (backlink->original_name);
13179
13180 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13181 }
13182 else
13183 dwp_name = objfile->original_name;
13184
13185 dwp_name += ".dwp";
13186
13187 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13188 if (dbfd == NULL
13189 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13190 {
13191 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13192 dwp_name = objfile_name (objfile);
13193 dwp_name += ".dwp";
13194 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13195 }
13196
13197 if (dbfd == NULL)
13198 {
13199 if (dwarf_read_debug)
13200 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13201 return std::unique_ptr<dwp_file> ();
13202 }
13203
13204 const char *name = bfd_get_filename (dbfd.get ());
13205 std::unique_ptr<struct dwp_file> dwp_file
13206 (new struct dwp_file (name, std::move (dbfd)));
13207
13208 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13209 dwp_file->elf_sections =
13210 OBSTACK_CALLOC (&objfile->objfile_obstack,
13211 dwp_file->num_sections, asection *);
13212
13213 bfd_map_over_sections (dwp_file->dbfd.get (),
13214 dwarf2_locate_common_dwp_sections,
13215 dwp_file.get ());
13216
13217 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13218 0);
13219
13220 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13221 1);
13222
13223 /* The DWP file version is stored in the hash table. Oh well. */
13224 if (dwp_file->cus && dwp_file->tus
13225 && dwp_file->cus->version != dwp_file->tus->version)
13226 {
13227 /* Technically speaking, we should try to limp along, but this is
13228 pretty bizarre. We use pulongest here because that's the established
13229 portability solution (e.g, we cannot use %u for uint32_t). */
13230 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13231 " TU version %s [in DWP file %s]"),
13232 pulongest (dwp_file->cus->version),
13233 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13234 }
13235
13236 if (dwp_file->cus)
13237 dwp_file->version = dwp_file->cus->version;
13238 else if (dwp_file->tus)
13239 dwp_file->version = dwp_file->tus->version;
13240 else
13241 dwp_file->version = 2;
13242
13243 if (dwp_file->version == 2)
13244 bfd_map_over_sections (dwp_file->dbfd.get (),
13245 dwarf2_locate_v2_dwp_sections,
13246 dwp_file.get ());
13247
13248 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13249 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13250
13251 if (dwarf_read_debug)
13252 {
13253 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13254 fprintf_unfiltered (gdb_stdlog,
13255 " %s CUs, %s TUs\n",
13256 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13257 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13258 }
13259
13260 return dwp_file;
13261 }
13262
13263 /* Wrapper around open_and_init_dwp_file, only open it once. */
13264
13265 static struct dwp_file *
13266 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13267 {
13268 if (! dwarf2_per_objfile->dwp_checked)
13269 {
13270 dwarf2_per_objfile->dwp_file
13271 = open_and_init_dwp_file (dwarf2_per_objfile);
13272 dwarf2_per_objfile->dwp_checked = 1;
13273 }
13274 return dwarf2_per_objfile->dwp_file.get ();
13275 }
13276
13277 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13278 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13279 or in the DWP file for the objfile, referenced by THIS_UNIT.
13280 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13281 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13282
13283 This is called, for example, when wanting to read a variable with a
13284 complex location. Therefore we don't want to do file i/o for every call.
13285 Therefore we don't want to look for a DWO file on every call.
13286 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13287 then we check if we've already seen DWO_NAME, and only THEN do we check
13288 for a DWO file.
13289
13290 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13291 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13292
13293 static struct dwo_unit *
13294 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13295 const char *dwo_name, const char *comp_dir,
13296 ULONGEST signature, int is_debug_types)
13297 {
13298 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13299 struct objfile *objfile = dwarf2_per_objfile->objfile;
13300 const char *kind = is_debug_types ? "TU" : "CU";
13301 void **dwo_file_slot;
13302 struct dwo_file *dwo_file;
13303 struct dwp_file *dwp_file;
13304
13305 /* First see if there's a DWP file.
13306 If we have a DWP file but didn't find the DWO inside it, don't
13307 look for the original DWO file. It makes gdb behave differently
13308 depending on whether one is debugging in the build tree. */
13309
13310 dwp_file = get_dwp_file (dwarf2_per_objfile);
13311 if (dwp_file != NULL)
13312 {
13313 const struct dwp_hash_table *dwp_htab =
13314 is_debug_types ? dwp_file->tus : dwp_file->cus;
13315
13316 if (dwp_htab != NULL)
13317 {
13318 struct dwo_unit *dwo_cutu =
13319 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13320 signature, is_debug_types);
13321
13322 if (dwo_cutu != NULL)
13323 {
13324 if (dwarf_read_debug)
13325 {
13326 fprintf_unfiltered (gdb_stdlog,
13327 "Virtual DWO %s %s found: @%s\n",
13328 kind, hex_string (signature),
13329 host_address_to_string (dwo_cutu));
13330 }
13331 return dwo_cutu;
13332 }
13333 }
13334 }
13335 else
13336 {
13337 /* No DWP file, look for the DWO file. */
13338
13339 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13340 dwo_name, comp_dir);
13341 if (*dwo_file_slot == NULL)
13342 {
13343 /* Read in the file and build a table of the CUs/TUs it contains. */
13344 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13345 }
13346 /* NOTE: This will be NULL if unable to open the file. */
13347 dwo_file = (struct dwo_file *) *dwo_file_slot;
13348
13349 if (dwo_file != NULL)
13350 {
13351 struct dwo_unit *dwo_cutu = NULL;
13352
13353 if (is_debug_types && dwo_file->tus)
13354 {
13355 struct dwo_unit find_dwo_cutu;
13356
13357 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13358 find_dwo_cutu.signature = signature;
13359 dwo_cutu
13360 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13361 }
13362 else if (!is_debug_types && dwo_file->cus)
13363 {
13364 struct dwo_unit find_dwo_cutu;
13365
13366 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13367 find_dwo_cutu.signature = signature;
13368 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13369 &find_dwo_cutu);
13370 }
13371
13372 if (dwo_cutu != NULL)
13373 {
13374 if (dwarf_read_debug)
13375 {
13376 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13377 kind, dwo_name, hex_string (signature),
13378 host_address_to_string (dwo_cutu));
13379 }
13380 return dwo_cutu;
13381 }
13382 }
13383 }
13384
13385 /* We didn't find it. This could mean a dwo_id mismatch, or
13386 someone deleted the DWO/DWP file, or the search path isn't set up
13387 correctly to find the file. */
13388
13389 if (dwarf_read_debug)
13390 {
13391 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13392 kind, dwo_name, hex_string (signature));
13393 }
13394
13395 /* This is a warning and not a complaint because it can be caused by
13396 pilot error (e.g., user accidentally deleting the DWO). */
13397 {
13398 /* Print the name of the DWP file if we looked there, helps the user
13399 better diagnose the problem. */
13400 std::string dwp_text;
13401
13402 if (dwp_file != NULL)
13403 dwp_text = string_printf (" [in DWP file %s]",
13404 lbasename (dwp_file->name));
13405
13406 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13407 " [in module %s]"),
13408 kind, dwo_name, hex_string (signature),
13409 dwp_text.c_str (),
13410 this_unit->is_debug_types ? "TU" : "CU",
13411 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13412 }
13413 return NULL;
13414 }
13415
13416 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13417 See lookup_dwo_cutu_unit for details. */
13418
13419 static struct dwo_unit *
13420 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13421 const char *dwo_name, const char *comp_dir,
13422 ULONGEST signature)
13423 {
13424 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13425 }
13426
13427 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13428 See lookup_dwo_cutu_unit for details. */
13429
13430 static struct dwo_unit *
13431 lookup_dwo_type_unit (struct signatured_type *this_tu,
13432 const char *dwo_name, const char *comp_dir)
13433 {
13434 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13435 }
13436
13437 /* Traversal function for queue_and_load_all_dwo_tus. */
13438
13439 static int
13440 queue_and_load_dwo_tu (void **slot, void *info)
13441 {
13442 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13443 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13444 ULONGEST signature = dwo_unit->signature;
13445 struct signatured_type *sig_type =
13446 lookup_dwo_signatured_type (per_cu->cu, signature);
13447
13448 if (sig_type != NULL)
13449 {
13450 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13451
13452 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13453 a real dependency of PER_CU on SIG_TYPE. That is detected later
13454 while processing PER_CU. */
13455 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13456 load_full_type_unit (sig_cu);
13457 per_cu->imported_symtabs_push (sig_cu);
13458 }
13459
13460 return 1;
13461 }
13462
13463 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13464 The DWO may have the only definition of the type, though it may not be
13465 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13466 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13467
13468 static void
13469 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13470 {
13471 struct dwo_unit *dwo_unit;
13472 struct dwo_file *dwo_file;
13473
13474 gdb_assert (!per_cu->is_debug_types);
13475 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13476 gdb_assert (per_cu->cu != NULL);
13477
13478 dwo_unit = per_cu->cu->dwo_unit;
13479 gdb_assert (dwo_unit != NULL);
13480
13481 dwo_file = dwo_unit->dwo_file;
13482 if (dwo_file->tus != NULL)
13483 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13484 }
13485
13486 /* Read in various DIEs. */
13487
13488 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13489 Inherit only the children of the DW_AT_abstract_origin DIE not being
13490 already referenced by DW_AT_abstract_origin from the children of the
13491 current DIE. */
13492
13493 static void
13494 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13495 {
13496 struct die_info *child_die;
13497 sect_offset *offsetp;
13498 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13499 struct die_info *origin_die;
13500 /* Iterator of the ORIGIN_DIE children. */
13501 struct die_info *origin_child_die;
13502 struct attribute *attr;
13503 struct dwarf2_cu *origin_cu;
13504 struct pending **origin_previous_list_in_scope;
13505
13506 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13507 if (!attr)
13508 return;
13509
13510 /* Note that following die references may follow to a die in a
13511 different cu. */
13512
13513 origin_cu = cu;
13514 origin_die = follow_die_ref (die, attr, &origin_cu);
13515
13516 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13517 symbols in. */
13518 origin_previous_list_in_scope = origin_cu->list_in_scope;
13519 origin_cu->list_in_scope = cu->list_in_scope;
13520
13521 if (die->tag != origin_die->tag
13522 && !(die->tag == DW_TAG_inlined_subroutine
13523 && origin_die->tag == DW_TAG_subprogram))
13524 complaint (_("DIE %s and its abstract origin %s have different tags"),
13525 sect_offset_str (die->sect_off),
13526 sect_offset_str (origin_die->sect_off));
13527
13528 std::vector<sect_offset> offsets;
13529
13530 for (child_die = die->child;
13531 child_die && child_die->tag;
13532 child_die = sibling_die (child_die))
13533 {
13534 struct die_info *child_origin_die;
13535 struct dwarf2_cu *child_origin_cu;
13536
13537 /* We are trying to process concrete instance entries:
13538 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13539 it's not relevant to our analysis here. i.e. detecting DIEs that are
13540 present in the abstract instance but not referenced in the concrete
13541 one. */
13542 if (child_die->tag == DW_TAG_call_site
13543 || child_die->tag == DW_TAG_GNU_call_site)
13544 continue;
13545
13546 /* For each CHILD_DIE, find the corresponding child of
13547 ORIGIN_DIE. If there is more than one layer of
13548 DW_AT_abstract_origin, follow them all; there shouldn't be,
13549 but GCC versions at least through 4.4 generate this (GCC PR
13550 40573). */
13551 child_origin_die = child_die;
13552 child_origin_cu = cu;
13553 while (1)
13554 {
13555 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13556 child_origin_cu);
13557 if (attr == NULL)
13558 break;
13559 child_origin_die = follow_die_ref (child_origin_die, attr,
13560 &child_origin_cu);
13561 }
13562
13563 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13564 counterpart may exist. */
13565 if (child_origin_die != child_die)
13566 {
13567 if (child_die->tag != child_origin_die->tag
13568 && !(child_die->tag == DW_TAG_inlined_subroutine
13569 && child_origin_die->tag == DW_TAG_subprogram))
13570 complaint (_("Child DIE %s and its abstract origin %s have "
13571 "different tags"),
13572 sect_offset_str (child_die->sect_off),
13573 sect_offset_str (child_origin_die->sect_off));
13574 if (child_origin_die->parent != origin_die)
13575 complaint (_("Child DIE %s and its abstract origin %s have "
13576 "different parents"),
13577 sect_offset_str (child_die->sect_off),
13578 sect_offset_str (child_origin_die->sect_off));
13579 else
13580 offsets.push_back (child_origin_die->sect_off);
13581 }
13582 }
13583 std::sort (offsets.begin (), offsets.end ());
13584 sect_offset *offsets_end = offsets.data () + offsets.size ();
13585 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13586 if (offsetp[-1] == *offsetp)
13587 complaint (_("Multiple children of DIE %s refer "
13588 "to DIE %s as their abstract origin"),
13589 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13590
13591 offsetp = offsets.data ();
13592 origin_child_die = origin_die->child;
13593 while (origin_child_die && origin_child_die->tag)
13594 {
13595 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13596 while (offsetp < offsets_end
13597 && *offsetp < origin_child_die->sect_off)
13598 offsetp++;
13599 if (offsetp >= offsets_end
13600 || *offsetp > origin_child_die->sect_off)
13601 {
13602 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13603 Check whether we're already processing ORIGIN_CHILD_DIE.
13604 This can happen with mutually referenced abstract_origins.
13605 PR 16581. */
13606 if (!origin_child_die->in_process)
13607 process_die (origin_child_die, origin_cu);
13608 }
13609 origin_child_die = sibling_die (origin_child_die);
13610 }
13611 origin_cu->list_in_scope = origin_previous_list_in_scope;
13612
13613 if (cu != origin_cu)
13614 compute_delayed_physnames (origin_cu);
13615 }
13616
13617 static void
13618 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13619 {
13620 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13621 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13622 struct context_stack *newobj;
13623 CORE_ADDR lowpc;
13624 CORE_ADDR highpc;
13625 struct die_info *child_die;
13626 struct attribute *attr, *call_line, *call_file;
13627 const char *name;
13628 CORE_ADDR baseaddr;
13629 struct block *block;
13630 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13631 std::vector<struct symbol *> template_args;
13632 struct template_symbol *templ_func = NULL;
13633
13634 if (inlined_func)
13635 {
13636 /* If we do not have call site information, we can't show the
13637 caller of this inlined function. That's too confusing, so
13638 only use the scope for local variables. */
13639 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13640 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13641 if (call_line == NULL || call_file == NULL)
13642 {
13643 read_lexical_block_scope (die, cu);
13644 return;
13645 }
13646 }
13647
13648 baseaddr = objfile->text_section_offset ();
13649
13650 name = dwarf2_name (die, cu);
13651
13652 /* Ignore functions with missing or empty names. These are actually
13653 illegal according to the DWARF standard. */
13654 if (name == NULL)
13655 {
13656 complaint (_("missing name for subprogram DIE at %s"),
13657 sect_offset_str (die->sect_off));
13658 return;
13659 }
13660
13661 /* Ignore functions with missing or invalid low and high pc attributes. */
13662 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13663 <= PC_BOUNDS_INVALID)
13664 {
13665 attr = dwarf2_attr (die, DW_AT_external, cu);
13666 if (!attr || !DW_UNSND (attr))
13667 complaint (_("cannot get low and high bounds "
13668 "for subprogram DIE at %s"),
13669 sect_offset_str (die->sect_off));
13670 return;
13671 }
13672
13673 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13674 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13675
13676 /* If we have any template arguments, then we must allocate a
13677 different sort of symbol. */
13678 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13679 {
13680 if (child_die->tag == DW_TAG_template_type_param
13681 || child_die->tag == DW_TAG_template_value_param)
13682 {
13683 templ_func = allocate_template_symbol (objfile);
13684 templ_func->subclass = SYMBOL_TEMPLATE;
13685 break;
13686 }
13687 }
13688
13689 newobj = cu->get_builder ()->push_context (0, lowpc);
13690 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13691 (struct symbol *) templ_func);
13692
13693 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13694 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13695 cu->language);
13696
13697 /* If there is a location expression for DW_AT_frame_base, record
13698 it. */
13699 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13700 if (attr != nullptr)
13701 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13702
13703 /* If there is a location for the static link, record it. */
13704 newobj->static_link = NULL;
13705 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13706 if (attr != nullptr)
13707 {
13708 newobj->static_link
13709 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13710 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13711 dwarf2_per_cu_addr_type (cu->per_cu));
13712 }
13713
13714 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13715
13716 if (die->child != NULL)
13717 {
13718 child_die = die->child;
13719 while (child_die && child_die->tag)
13720 {
13721 if (child_die->tag == DW_TAG_template_type_param
13722 || child_die->tag == DW_TAG_template_value_param)
13723 {
13724 struct symbol *arg = new_symbol (child_die, NULL, cu);
13725
13726 if (arg != NULL)
13727 template_args.push_back (arg);
13728 }
13729 else
13730 process_die (child_die, cu);
13731 child_die = sibling_die (child_die);
13732 }
13733 }
13734
13735 inherit_abstract_dies (die, cu);
13736
13737 /* If we have a DW_AT_specification, we might need to import using
13738 directives from the context of the specification DIE. See the
13739 comment in determine_prefix. */
13740 if (cu->language == language_cplus
13741 && dwarf2_attr (die, DW_AT_specification, cu))
13742 {
13743 struct dwarf2_cu *spec_cu = cu;
13744 struct die_info *spec_die = die_specification (die, &spec_cu);
13745
13746 while (spec_die)
13747 {
13748 child_die = spec_die->child;
13749 while (child_die && child_die->tag)
13750 {
13751 if (child_die->tag == DW_TAG_imported_module)
13752 process_die (child_die, spec_cu);
13753 child_die = sibling_die (child_die);
13754 }
13755
13756 /* In some cases, GCC generates specification DIEs that
13757 themselves contain DW_AT_specification attributes. */
13758 spec_die = die_specification (spec_die, &spec_cu);
13759 }
13760 }
13761
13762 struct context_stack cstk = cu->get_builder ()->pop_context ();
13763 /* Make a block for the local symbols within. */
13764 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13765 cstk.static_link, lowpc, highpc);
13766
13767 /* For C++, set the block's scope. */
13768 if ((cu->language == language_cplus
13769 || cu->language == language_fortran
13770 || cu->language == language_d
13771 || cu->language == language_rust)
13772 && cu->processing_has_namespace_info)
13773 block_set_scope (block, determine_prefix (die, cu),
13774 &objfile->objfile_obstack);
13775
13776 /* If we have address ranges, record them. */
13777 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13778
13779 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13780
13781 /* Attach template arguments to function. */
13782 if (!template_args.empty ())
13783 {
13784 gdb_assert (templ_func != NULL);
13785
13786 templ_func->n_template_arguments = template_args.size ();
13787 templ_func->template_arguments
13788 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13789 templ_func->n_template_arguments);
13790 memcpy (templ_func->template_arguments,
13791 template_args.data (),
13792 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13793
13794 /* Make sure that the symtab is set on the new symbols. Even
13795 though they don't appear in this symtab directly, other parts
13796 of gdb assume that symbols do, and this is reasonably
13797 true. */
13798 for (symbol *sym : template_args)
13799 symbol_set_symtab (sym, symbol_symtab (templ_func));
13800 }
13801
13802 /* In C++, we can have functions nested inside functions (e.g., when
13803 a function declares a class that has methods). This means that
13804 when we finish processing a function scope, we may need to go
13805 back to building a containing block's symbol lists. */
13806 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13807 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13808
13809 /* If we've finished processing a top-level function, subsequent
13810 symbols go in the file symbol list. */
13811 if (cu->get_builder ()->outermost_context_p ())
13812 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13813 }
13814
13815 /* Process all the DIES contained within a lexical block scope. Start
13816 a new scope, process the dies, and then close the scope. */
13817
13818 static void
13819 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13820 {
13821 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13822 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13823 CORE_ADDR lowpc, highpc;
13824 struct die_info *child_die;
13825 CORE_ADDR baseaddr;
13826
13827 baseaddr = objfile->text_section_offset ();
13828
13829 /* Ignore blocks with missing or invalid low and high pc attributes. */
13830 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13831 as multiple lexical blocks? Handling children in a sane way would
13832 be nasty. Might be easier to properly extend generic blocks to
13833 describe ranges. */
13834 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13835 {
13836 case PC_BOUNDS_NOT_PRESENT:
13837 /* DW_TAG_lexical_block has no attributes, process its children as if
13838 there was no wrapping by that DW_TAG_lexical_block.
13839 GCC does no longer produces such DWARF since GCC r224161. */
13840 for (child_die = die->child;
13841 child_die != NULL && child_die->tag;
13842 child_die = sibling_die (child_die))
13843 process_die (child_die, cu);
13844 return;
13845 case PC_BOUNDS_INVALID:
13846 return;
13847 }
13848 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13849 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13850
13851 cu->get_builder ()->push_context (0, lowpc);
13852 if (die->child != NULL)
13853 {
13854 child_die = die->child;
13855 while (child_die && child_die->tag)
13856 {
13857 process_die (child_die, cu);
13858 child_die = sibling_die (child_die);
13859 }
13860 }
13861 inherit_abstract_dies (die, cu);
13862 struct context_stack cstk = cu->get_builder ()->pop_context ();
13863
13864 if (*cu->get_builder ()->get_local_symbols () != NULL
13865 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13866 {
13867 struct block *block
13868 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13869 cstk.start_addr, highpc);
13870
13871 /* Note that recording ranges after traversing children, as we
13872 do here, means that recording a parent's ranges entails
13873 walking across all its children's ranges as they appear in
13874 the address map, which is quadratic behavior.
13875
13876 It would be nicer to record the parent's ranges before
13877 traversing its children, simply overriding whatever you find
13878 there. But since we don't even decide whether to create a
13879 block until after we've traversed its children, that's hard
13880 to do. */
13881 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13882 }
13883 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13884 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13885 }
13886
13887 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13888
13889 static void
13890 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13891 {
13892 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13893 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13894 CORE_ADDR pc, baseaddr;
13895 struct attribute *attr;
13896 struct call_site *call_site, call_site_local;
13897 void **slot;
13898 int nparams;
13899 struct die_info *child_die;
13900
13901 baseaddr = objfile->text_section_offset ();
13902
13903 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13904 if (attr == NULL)
13905 {
13906 /* This was a pre-DWARF-5 GNU extension alias
13907 for DW_AT_call_return_pc. */
13908 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13909 }
13910 if (!attr)
13911 {
13912 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13913 "DIE %s [in module %s]"),
13914 sect_offset_str (die->sect_off), objfile_name (objfile));
13915 return;
13916 }
13917 pc = attr_value_as_address (attr) + baseaddr;
13918 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13919
13920 if (cu->call_site_htab == NULL)
13921 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13922 NULL, &objfile->objfile_obstack,
13923 hashtab_obstack_allocate, NULL);
13924 call_site_local.pc = pc;
13925 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13926 if (*slot != NULL)
13927 {
13928 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13929 "DIE %s [in module %s]"),
13930 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13931 objfile_name (objfile));
13932 return;
13933 }
13934
13935 /* Count parameters at the caller. */
13936
13937 nparams = 0;
13938 for (child_die = die->child; child_die && child_die->tag;
13939 child_die = sibling_die (child_die))
13940 {
13941 if (child_die->tag != DW_TAG_call_site_parameter
13942 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13943 {
13944 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13945 "DW_TAG_call_site child DIE %s [in module %s]"),
13946 child_die->tag, sect_offset_str (child_die->sect_off),
13947 objfile_name (objfile));
13948 continue;
13949 }
13950
13951 nparams++;
13952 }
13953
13954 call_site
13955 = ((struct call_site *)
13956 obstack_alloc (&objfile->objfile_obstack,
13957 sizeof (*call_site)
13958 + (sizeof (*call_site->parameter) * (nparams - 1))));
13959 *slot = call_site;
13960 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13961 call_site->pc = pc;
13962
13963 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13964 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13965 {
13966 struct die_info *func_die;
13967
13968 /* Skip also over DW_TAG_inlined_subroutine. */
13969 for (func_die = die->parent;
13970 func_die && func_die->tag != DW_TAG_subprogram
13971 && func_die->tag != DW_TAG_subroutine_type;
13972 func_die = func_die->parent);
13973
13974 /* DW_AT_call_all_calls is a superset
13975 of DW_AT_call_all_tail_calls. */
13976 if (func_die
13977 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13978 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13979 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13980 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13981 {
13982 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13983 not complete. But keep CALL_SITE for look ups via call_site_htab,
13984 both the initial caller containing the real return address PC and
13985 the final callee containing the current PC of a chain of tail
13986 calls do not need to have the tail call list complete. But any
13987 function candidate for a virtual tail call frame searched via
13988 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13989 determined unambiguously. */
13990 }
13991 else
13992 {
13993 struct type *func_type = NULL;
13994
13995 if (func_die)
13996 func_type = get_die_type (func_die, cu);
13997 if (func_type != NULL)
13998 {
13999 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14000
14001 /* Enlist this call site to the function. */
14002 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14003 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14004 }
14005 else
14006 complaint (_("Cannot find function owning DW_TAG_call_site "
14007 "DIE %s [in module %s]"),
14008 sect_offset_str (die->sect_off), objfile_name (objfile));
14009 }
14010 }
14011
14012 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14013 if (attr == NULL)
14014 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14015 if (attr == NULL)
14016 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14017 if (attr == NULL)
14018 {
14019 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14020 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14021 }
14022 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14023 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14024 /* Keep NULL DWARF_BLOCK. */;
14025 else if (attr_form_is_block (attr))
14026 {
14027 struct dwarf2_locexpr_baton *dlbaton;
14028
14029 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14030 dlbaton->data = DW_BLOCK (attr)->data;
14031 dlbaton->size = DW_BLOCK (attr)->size;
14032 dlbaton->per_cu = cu->per_cu;
14033
14034 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14035 }
14036 else if (attr_form_is_ref (attr))
14037 {
14038 struct dwarf2_cu *target_cu = cu;
14039 struct die_info *target_die;
14040
14041 target_die = follow_die_ref (die, attr, &target_cu);
14042 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14043 if (die_is_declaration (target_die, target_cu))
14044 {
14045 const char *target_physname;
14046
14047 /* Prefer the mangled name; otherwise compute the demangled one. */
14048 target_physname = dw2_linkage_name (target_die, target_cu);
14049 if (target_physname == NULL)
14050 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14051 if (target_physname == NULL)
14052 complaint (_("DW_AT_call_target target DIE has invalid "
14053 "physname, for referencing DIE %s [in module %s]"),
14054 sect_offset_str (die->sect_off), objfile_name (objfile));
14055 else
14056 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14057 }
14058 else
14059 {
14060 CORE_ADDR lowpc;
14061
14062 /* DW_AT_entry_pc should be preferred. */
14063 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14064 <= PC_BOUNDS_INVALID)
14065 complaint (_("DW_AT_call_target target DIE has invalid "
14066 "low pc, for referencing DIE %s [in module %s]"),
14067 sect_offset_str (die->sect_off), objfile_name (objfile));
14068 else
14069 {
14070 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14071 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14072 }
14073 }
14074 }
14075 else
14076 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14077 "block nor reference, for DIE %s [in module %s]"),
14078 sect_offset_str (die->sect_off), objfile_name (objfile));
14079
14080 call_site->per_cu = cu->per_cu;
14081
14082 for (child_die = die->child;
14083 child_die && child_die->tag;
14084 child_die = sibling_die (child_die))
14085 {
14086 struct call_site_parameter *parameter;
14087 struct attribute *loc, *origin;
14088
14089 if (child_die->tag != DW_TAG_call_site_parameter
14090 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14091 {
14092 /* Already printed the complaint above. */
14093 continue;
14094 }
14095
14096 gdb_assert (call_site->parameter_count < nparams);
14097 parameter = &call_site->parameter[call_site->parameter_count];
14098
14099 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14100 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14101 register is contained in DW_AT_call_value. */
14102
14103 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14104 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14105 if (origin == NULL)
14106 {
14107 /* This was a pre-DWARF-5 GNU extension alias
14108 for DW_AT_call_parameter. */
14109 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14110 }
14111 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14112 {
14113 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14114
14115 sect_offset sect_off
14116 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14117 if (!offset_in_cu_p (&cu->header, sect_off))
14118 {
14119 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14120 binding can be done only inside one CU. Such referenced DIE
14121 therefore cannot be even moved to DW_TAG_partial_unit. */
14122 complaint (_("DW_AT_call_parameter offset is not in CU for "
14123 "DW_TAG_call_site child DIE %s [in module %s]"),
14124 sect_offset_str (child_die->sect_off),
14125 objfile_name (objfile));
14126 continue;
14127 }
14128 parameter->u.param_cu_off
14129 = (cu_offset) (sect_off - cu->header.sect_off);
14130 }
14131 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14132 {
14133 complaint (_("No DW_FORM_block* DW_AT_location for "
14134 "DW_TAG_call_site child DIE %s [in module %s]"),
14135 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14136 continue;
14137 }
14138 else
14139 {
14140 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14141 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14142 if (parameter->u.dwarf_reg != -1)
14143 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14144 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14145 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14146 &parameter->u.fb_offset))
14147 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14148 else
14149 {
14150 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14151 "for DW_FORM_block* DW_AT_location is supported for "
14152 "DW_TAG_call_site child DIE %s "
14153 "[in module %s]"),
14154 sect_offset_str (child_die->sect_off),
14155 objfile_name (objfile));
14156 continue;
14157 }
14158 }
14159
14160 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14161 if (attr == NULL)
14162 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14163 if (!attr_form_is_block (attr))
14164 {
14165 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14166 "DW_TAG_call_site child DIE %s [in module %s]"),
14167 sect_offset_str (child_die->sect_off),
14168 objfile_name (objfile));
14169 continue;
14170 }
14171 parameter->value = DW_BLOCK (attr)->data;
14172 parameter->value_size = DW_BLOCK (attr)->size;
14173
14174 /* Parameters are not pre-cleared by memset above. */
14175 parameter->data_value = NULL;
14176 parameter->data_value_size = 0;
14177 call_site->parameter_count++;
14178
14179 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14180 if (attr == NULL)
14181 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14182 if (attr != nullptr)
14183 {
14184 if (!attr_form_is_block (attr))
14185 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14186 "DW_TAG_call_site child DIE %s [in module %s]"),
14187 sect_offset_str (child_die->sect_off),
14188 objfile_name (objfile));
14189 else
14190 {
14191 parameter->data_value = DW_BLOCK (attr)->data;
14192 parameter->data_value_size = DW_BLOCK (attr)->size;
14193 }
14194 }
14195 }
14196 }
14197
14198 /* Helper function for read_variable. If DIE represents a virtual
14199 table, then return the type of the concrete object that is
14200 associated with the virtual table. Otherwise, return NULL. */
14201
14202 static struct type *
14203 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14204 {
14205 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14206 if (attr == NULL)
14207 return NULL;
14208
14209 /* Find the type DIE. */
14210 struct die_info *type_die = NULL;
14211 struct dwarf2_cu *type_cu = cu;
14212
14213 if (attr_form_is_ref (attr))
14214 type_die = follow_die_ref (die, attr, &type_cu);
14215 if (type_die == NULL)
14216 return NULL;
14217
14218 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14219 return NULL;
14220 return die_containing_type (type_die, type_cu);
14221 }
14222
14223 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14224
14225 static void
14226 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14227 {
14228 struct rust_vtable_symbol *storage = NULL;
14229
14230 if (cu->language == language_rust)
14231 {
14232 struct type *containing_type = rust_containing_type (die, cu);
14233
14234 if (containing_type != NULL)
14235 {
14236 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14237
14238 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
14239 initialize_objfile_symbol (storage);
14240 storage->concrete_type = containing_type;
14241 storage->subclass = SYMBOL_RUST_VTABLE;
14242 }
14243 }
14244
14245 struct symbol *res = new_symbol (die, NULL, cu, storage);
14246 struct attribute *abstract_origin
14247 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14248 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14249 if (res == NULL && loc && abstract_origin)
14250 {
14251 /* We have a variable without a name, but with a location and an abstract
14252 origin. This may be a concrete instance of an abstract variable
14253 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14254 later. */
14255 struct dwarf2_cu *origin_cu = cu;
14256 struct die_info *origin_die
14257 = follow_die_ref (die, abstract_origin, &origin_cu);
14258 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
14259 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
14260 }
14261 }
14262
14263 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14264 reading .debug_rnglists.
14265 Callback's type should be:
14266 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14267 Return true if the attributes are present and valid, otherwise,
14268 return false. */
14269
14270 template <typename Callback>
14271 static bool
14272 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14273 Callback &&callback)
14274 {
14275 struct dwarf2_per_objfile *dwarf2_per_objfile
14276 = cu->per_cu->dwarf2_per_objfile;
14277 struct objfile *objfile = dwarf2_per_objfile->objfile;
14278 bfd *obfd = objfile->obfd;
14279 /* Base address selection entry. */
14280 CORE_ADDR base;
14281 int found_base;
14282 const gdb_byte *buffer;
14283 CORE_ADDR baseaddr;
14284 bool overflow = false;
14285
14286 found_base = cu->base_known;
14287 base = cu->base_address;
14288
14289 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14290 if (offset >= dwarf2_per_objfile->rnglists.size)
14291 {
14292 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14293 offset);
14294 return false;
14295 }
14296 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14297
14298 baseaddr = objfile->text_section_offset ();
14299
14300 while (1)
14301 {
14302 /* Initialize it due to a false compiler warning. */
14303 CORE_ADDR range_beginning = 0, range_end = 0;
14304 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14305 + dwarf2_per_objfile->rnglists.size);
14306 unsigned int bytes_read;
14307
14308 if (buffer == buf_end)
14309 {
14310 overflow = true;
14311 break;
14312 }
14313 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14314 switch (rlet)
14315 {
14316 case DW_RLE_end_of_list:
14317 break;
14318 case DW_RLE_base_address:
14319 if (buffer + cu->header.addr_size > buf_end)
14320 {
14321 overflow = true;
14322 break;
14323 }
14324 base = read_address (obfd, buffer, cu, &bytes_read);
14325 found_base = 1;
14326 buffer += bytes_read;
14327 break;
14328 case DW_RLE_start_length:
14329 if (buffer + cu->header.addr_size > buf_end)
14330 {
14331 overflow = true;
14332 break;
14333 }
14334 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14335 buffer += bytes_read;
14336 range_end = (range_beginning
14337 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14338 buffer += bytes_read;
14339 if (buffer > buf_end)
14340 {
14341 overflow = true;
14342 break;
14343 }
14344 break;
14345 case DW_RLE_offset_pair:
14346 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14347 buffer += bytes_read;
14348 if (buffer > buf_end)
14349 {
14350 overflow = true;
14351 break;
14352 }
14353 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14354 buffer += bytes_read;
14355 if (buffer > buf_end)
14356 {
14357 overflow = true;
14358 break;
14359 }
14360 break;
14361 case DW_RLE_start_end:
14362 if (buffer + 2 * cu->header.addr_size > buf_end)
14363 {
14364 overflow = true;
14365 break;
14366 }
14367 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14368 buffer += bytes_read;
14369 range_end = read_address (obfd, buffer, cu, &bytes_read);
14370 buffer += bytes_read;
14371 break;
14372 default:
14373 complaint (_("Invalid .debug_rnglists data (no base address)"));
14374 return false;
14375 }
14376 if (rlet == DW_RLE_end_of_list || overflow)
14377 break;
14378 if (rlet == DW_RLE_base_address)
14379 continue;
14380
14381 if (!found_base)
14382 {
14383 /* We have no valid base address for the ranges
14384 data. */
14385 complaint (_("Invalid .debug_rnglists data (no base address)"));
14386 return false;
14387 }
14388
14389 if (range_beginning > range_end)
14390 {
14391 /* Inverted range entries are invalid. */
14392 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14393 return false;
14394 }
14395
14396 /* Empty range entries have no effect. */
14397 if (range_beginning == range_end)
14398 continue;
14399
14400 range_beginning += base;
14401 range_end += base;
14402
14403 /* A not-uncommon case of bad debug info.
14404 Don't pollute the addrmap with bad data. */
14405 if (range_beginning + baseaddr == 0
14406 && !dwarf2_per_objfile->has_section_at_zero)
14407 {
14408 complaint (_(".debug_rnglists entry has start address of zero"
14409 " [in module %s]"), objfile_name (objfile));
14410 continue;
14411 }
14412
14413 callback (range_beginning, range_end);
14414 }
14415
14416 if (overflow)
14417 {
14418 complaint (_("Offset %d is not terminated "
14419 "for DW_AT_ranges attribute"),
14420 offset);
14421 return false;
14422 }
14423
14424 return true;
14425 }
14426
14427 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14428 Callback's type should be:
14429 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14430 Return 1 if the attributes are present and valid, otherwise, return 0. */
14431
14432 template <typename Callback>
14433 static int
14434 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14435 Callback &&callback)
14436 {
14437 struct dwarf2_per_objfile *dwarf2_per_objfile
14438 = cu->per_cu->dwarf2_per_objfile;
14439 struct objfile *objfile = dwarf2_per_objfile->objfile;
14440 struct comp_unit_head *cu_header = &cu->header;
14441 bfd *obfd = objfile->obfd;
14442 unsigned int addr_size = cu_header->addr_size;
14443 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14444 /* Base address selection entry. */
14445 CORE_ADDR base;
14446 int found_base;
14447 unsigned int dummy;
14448 const gdb_byte *buffer;
14449 CORE_ADDR baseaddr;
14450
14451 if (cu_header->version >= 5)
14452 return dwarf2_rnglists_process (offset, cu, callback);
14453
14454 found_base = cu->base_known;
14455 base = cu->base_address;
14456
14457 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14458 if (offset >= dwarf2_per_objfile->ranges.size)
14459 {
14460 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14461 offset);
14462 return 0;
14463 }
14464 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14465
14466 baseaddr = objfile->text_section_offset ();
14467
14468 while (1)
14469 {
14470 CORE_ADDR range_beginning, range_end;
14471
14472 range_beginning = read_address (obfd, buffer, cu, &dummy);
14473 buffer += addr_size;
14474 range_end = read_address (obfd, buffer, cu, &dummy);
14475 buffer += addr_size;
14476 offset += 2 * addr_size;
14477
14478 /* An end of list marker is a pair of zero addresses. */
14479 if (range_beginning == 0 && range_end == 0)
14480 /* Found the end of list entry. */
14481 break;
14482
14483 /* Each base address selection entry is a pair of 2 values.
14484 The first is the largest possible address, the second is
14485 the base address. Check for a base address here. */
14486 if ((range_beginning & mask) == mask)
14487 {
14488 /* If we found the largest possible address, then we already
14489 have the base address in range_end. */
14490 base = range_end;
14491 found_base = 1;
14492 continue;
14493 }
14494
14495 if (!found_base)
14496 {
14497 /* We have no valid base address for the ranges
14498 data. */
14499 complaint (_("Invalid .debug_ranges data (no base address)"));
14500 return 0;
14501 }
14502
14503 if (range_beginning > range_end)
14504 {
14505 /* Inverted range entries are invalid. */
14506 complaint (_("Invalid .debug_ranges data (inverted range)"));
14507 return 0;
14508 }
14509
14510 /* Empty range entries have no effect. */
14511 if (range_beginning == range_end)
14512 continue;
14513
14514 range_beginning += base;
14515 range_end += base;
14516
14517 /* A not-uncommon case of bad debug info.
14518 Don't pollute the addrmap with bad data. */
14519 if (range_beginning + baseaddr == 0
14520 && !dwarf2_per_objfile->has_section_at_zero)
14521 {
14522 complaint (_(".debug_ranges entry has start address of zero"
14523 " [in module %s]"), objfile_name (objfile));
14524 continue;
14525 }
14526
14527 callback (range_beginning, range_end);
14528 }
14529
14530 return 1;
14531 }
14532
14533 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14534 Return 1 if the attributes are present and valid, otherwise, return 0.
14535 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14536
14537 static int
14538 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14539 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14540 dwarf2_psymtab *ranges_pst)
14541 {
14542 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14543 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14544 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14545 int low_set = 0;
14546 CORE_ADDR low = 0;
14547 CORE_ADDR high = 0;
14548 int retval;
14549
14550 retval = dwarf2_ranges_process (offset, cu,
14551 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14552 {
14553 if (ranges_pst != NULL)
14554 {
14555 CORE_ADDR lowpc;
14556 CORE_ADDR highpc;
14557
14558 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14559 range_beginning + baseaddr)
14560 - baseaddr);
14561 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14562 range_end + baseaddr)
14563 - baseaddr);
14564 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14565 lowpc, highpc - 1, ranges_pst);
14566 }
14567
14568 /* FIXME: This is recording everything as a low-high
14569 segment of consecutive addresses. We should have a
14570 data structure for discontiguous block ranges
14571 instead. */
14572 if (! low_set)
14573 {
14574 low = range_beginning;
14575 high = range_end;
14576 low_set = 1;
14577 }
14578 else
14579 {
14580 if (range_beginning < low)
14581 low = range_beginning;
14582 if (range_end > high)
14583 high = range_end;
14584 }
14585 });
14586 if (!retval)
14587 return 0;
14588
14589 if (! low_set)
14590 /* If the first entry is an end-of-list marker, the range
14591 describes an empty scope, i.e. no instructions. */
14592 return 0;
14593
14594 if (low_return)
14595 *low_return = low;
14596 if (high_return)
14597 *high_return = high;
14598 return 1;
14599 }
14600
14601 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14602 definition for the return value. *LOWPC and *HIGHPC are set iff
14603 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14604
14605 static enum pc_bounds_kind
14606 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14607 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14608 dwarf2_psymtab *pst)
14609 {
14610 struct dwarf2_per_objfile *dwarf2_per_objfile
14611 = cu->per_cu->dwarf2_per_objfile;
14612 struct attribute *attr;
14613 struct attribute *attr_high;
14614 CORE_ADDR low = 0;
14615 CORE_ADDR high = 0;
14616 enum pc_bounds_kind ret;
14617
14618 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14619 if (attr_high)
14620 {
14621 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14622 if (attr != nullptr)
14623 {
14624 low = attr_value_as_address (attr);
14625 high = attr_value_as_address (attr_high);
14626 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14627 high += low;
14628 }
14629 else
14630 /* Found high w/o low attribute. */
14631 return PC_BOUNDS_INVALID;
14632
14633 /* Found consecutive range of addresses. */
14634 ret = PC_BOUNDS_HIGH_LOW;
14635 }
14636 else
14637 {
14638 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14639 if (attr != NULL)
14640 {
14641 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14642 We take advantage of the fact that DW_AT_ranges does not appear
14643 in DW_TAG_compile_unit of DWO files. */
14644 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14645 unsigned int ranges_offset = (DW_UNSND (attr)
14646 + (need_ranges_base
14647 ? cu->ranges_base
14648 : 0));
14649
14650 /* Value of the DW_AT_ranges attribute is the offset in the
14651 .debug_ranges section. */
14652 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14653 return PC_BOUNDS_INVALID;
14654 /* Found discontinuous range of addresses. */
14655 ret = PC_BOUNDS_RANGES;
14656 }
14657 else
14658 return PC_BOUNDS_NOT_PRESENT;
14659 }
14660
14661 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14662 if (high <= low)
14663 return PC_BOUNDS_INVALID;
14664
14665 /* When using the GNU linker, .gnu.linkonce. sections are used to
14666 eliminate duplicate copies of functions and vtables and such.
14667 The linker will arbitrarily choose one and discard the others.
14668 The AT_*_pc values for such functions refer to local labels in
14669 these sections. If the section from that file was discarded, the
14670 labels are not in the output, so the relocs get a value of 0.
14671 If this is a discarded function, mark the pc bounds as invalid,
14672 so that GDB will ignore it. */
14673 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14674 return PC_BOUNDS_INVALID;
14675
14676 *lowpc = low;
14677 if (highpc)
14678 *highpc = high;
14679 return ret;
14680 }
14681
14682 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14683 its low and high PC addresses. Do nothing if these addresses could not
14684 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14685 and HIGHPC to the high address if greater than HIGHPC. */
14686
14687 static void
14688 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14689 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14690 struct dwarf2_cu *cu)
14691 {
14692 CORE_ADDR low, high;
14693 struct die_info *child = die->child;
14694
14695 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14696 {
14697 *lowpc = std::min (*lowpc, low);
14698 *highpc = std::max (*highpc, high);
14699 }
14700
14701 /* If the language does not allow nested subprograms (either inside
14702 subprograms or lexical blocks), we're done. */
14703 if (cu->language != language_ada)
14704 return;
14705
14706 /* Check all the children of the given DIE. If it contains nested
14707 subprograms, then check their pc bounds. Likewise, we need to
14708 check lexical blocks as well, as they may also contain subprogram
14709 definitions. */
14710 while (child && child->tag)
14711 {
14712 if (child->tag == DW_TAG_subprogram
14713 || child->tag == DW_TAG_lexical_block)
14714 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14715 child = sibling_die (child);
14716 }
14717 }
14718
14719 /* Get the low and high pc's represented by the scope DIE, and store
14720 them in *LOWPC and *HIGHPC. If the correct values can't be
14721 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14722
14723 static void
14724 get_scope_pc_bounds (struct die_info *die,
14725 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14726 struct dwarf2_cu *cu)
14727 {
14728 CORE_ADDR best_low = (CORE_ADDR) -1;
14729 CORE_ADDR best_high = (CORE_ADDR) 0;
14730 CORE_ADDR current_low, current_high;
14731
14732 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14733 >= PC_BOUNDS_RANGES)
14734 {
14735 best_low = current_low;
14736 best_high = current_high;
14737 }
14738 else
14739 {
14740 struct die_info *child = die->child;
14741
14742 while (child && child->tag)
14743 {
14744 switch (child->tag) {
14745 case DW_TAG_subprogram:
14746 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14747 break;
14748 case DW_TAG_namespace:
14749 case DW_TAG_module:
14750 /* FIXME: carlton/2004-01-16: Should we do this for
14751 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14752 that current GCC's always emit the DIEs corresponding
14753 to definitions of methods of classes as children of a
14754 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14755 the DIEs giving the declarations, which could be
14756 anywhere). But I don't see any reason why the
14757 standards says that they have to be there. */
14758 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14759
14760 if (current_low != ((CORE_ADDR) -1))
14761 {
14762 best_low = std::min (best_low, current_low);
14763 best_high = std::max (best_high, current_high);
14764 }
14765 break;
14766 default:
14767 /* Ignore. */
14768 break;
14769 }
14770
14771 child = sibling_die (child);
14772 }
14773 }
14774
14775 *lowpc = best_low;
14776 *highpc = best_high;
14777 }
14778
14779 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14780 in DIE. */
14781
14782 static void
14783 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14784 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14785 {
14786 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14787 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14788 struct attribute *attr;
14789 struct attribute *attr_high;
14790
14791 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14792 if (attr_high)
14793 {
14794 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14795 if (attr != nullptr)
14796 {
14797 CORE_ADDR low = attr_value_as_address (attr);
14798 CORE_ADDR high = attr_value_as_address (attr_high);
14799
14800 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14801 high += low;
14802
14803 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14804 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14805 cu->get_builder ()->record_block_range (block, low, high - 1);
14806 }
14807 }
14808
14809 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14810 if (attr != nullptr)
14811 {
14812 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14813 We take advantage of the fact that DW_AT_ranges does not appear
14814 in DW_TAG_compile_unit of DWO files. */
14815 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14816
14817 /* The value of the DW_AT_ranges attribute is the offset of the
14818 address range list in the .debug_ranges section. */
14819 unsigned long offset = (DW_UNSND (attr)
14820 + (need_ranges_base ? cu->ranges_base : 0));
14821
14822 std::vector<blockrange> blockvec;
14823 dwarf2_ranges_process (offset, cu,
14824 [&] (CORE_ADDR start, CORE_ADDR end)
14825 {
14826 start += baseaddr;
14827 end += baseaddr;
14828 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14829 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14830 cu->get_builder ()->record_block_range (block, start, end - 1);
14831 blockvec.emplace_back (start, end);
14832 });
14833
14834 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14835 }
14836 }
14837
14838 /* Check whether the producer field indicates either of GCC < 4.6, or the
14839 Intel C/C++ compiler, and cache the result in CU. */
14840
14841 static void
14842 check_producer (struct dwarf2_cu *cu)
14843 {
14844 int major, minor;
14845
14846 if (cu->producer == NULL)
14847 {
14848 /* For unknown compilers expect their behavior is DWARF version
14849 compliant.
14850
14851 GCC started to support .debug_types sections by -gdwarf-4 since
14852 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14853 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14854 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14855 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14856 }
14857 else if (producer_is_gcc (cu->producer, &major, &minor))
14858 {
14859 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14860 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14861 }
14862 else if (producer_is_icc (cu->producer, &major, &minor))
14863 {
14864 cu->producer_is_icc = true;
14865 cu->producer_is_icc_lt_14 = major < 14;
14866 }
14867 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14868 cu->producer_is_codewarrior = true;
14869 else
14870 {
14871 /* For other non-GCC compilers, expect their behavior is DWARF version
14872 compliant. */
14873 }
14874
14875 cu->checked_producer = true;
14876 }
14877
14878 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14879 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14880 during 4.6.0 experimental. */
14881
14882 static bool
14883 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14884 {
14885 if (!cu->checked_producer)
14886 check_producer (cu);
14887
14888 return cu->producer_is_gxx_lt_4_6;
14889 }
14890
14891
14892 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14893 with incorrect is_stmt attributes. */
14894
14895 static bool
14896 producer_is_codewarrior (struct dwarf2_cu *cu)
14897 {
14898 if (!cu->checked_producer)
14899 check_producer (cu);
14900
14901 return cu->producer_is_codewarrior;
14902 }
14903
14904 /* Return the default accessibility type if it is not overridden by
14905 DW_AT_accessibility. */
14906
14907 static enum dwarf_access_attribute
14908 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14909 {
14910 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14911 {
14912 /* The default DWARF 2 accessibility for members is public, the default
14913 accessibility for inheritance is private. */
14914
14915 if (die->tag != DW_TAG_inheritance)
14916 return DW_ACCESS_public;
14917 else
14918 return DW_ACCESS_private;
14919 }
14920 else
14921 {
14922 /* DWARF 3+ defines the default accessibility a different way. The same
14923 rules apply now for DW_TAG_inheritance as for the members and it only
14924 depends on the container kind. */
14925
14926 if (die->parent->tag == DW_TAG_class_type)
14927 return DW_ACCESS_private;
14928 else
14929 return DW_ACCESS_public;
14930 }
14931 }
14932
14933 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14934 offset. If the attribute was not found return 0, otherwise return
14935 1. If it was found but could not properly be handled, set *OFFSET
14936 to 0. */
14937
14938 static int
14939 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14940 LONGEST *offset)
14941 {
14942 struct attribute *attr;
14943
14944 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14945 if (attr != NULL)
14946 {
14947 *offset = 0;
14948
14949 /* Note that we do not check for a section offset first here.
14950 This is because DW_AT_data_member_location is new in DWARF 4,
14951 so if we see it, we can assume that a constant form is really
14952 a constant and not a section offset. */
14953 if (attr_form_is_constant (attr))
14954 *offset = dwarf2_get_attr_constant_value (attr, 0);
14955 else if (attr_form_is_section_offset (attr))
14956 dwarf2_complex_location_expr_complaint ();
14957 else if (attr_form_is_block (attr))
14958 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14959 else
14960 dwarf2_complex_location_expr_complaint ();
14961
14962 return 1;
14963 }
14964
14965 return 0;
14966 }
14967
14968 /* Add an aggregate field to the field list. */
14969
14970 static void
14971 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14972 struct dwarf2_cu *cu)
14973 {
14974 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14975 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14976 struct nextfield *new_field;
14977 struct attribute *attr;
14978 struct field *fp;
14979 const char *fieldname = "";
14980
14981 if (die->tag == DW_TAG_inheritance)
14982 {
14983 fip->baseclasses.emplace_back ();
14984 new_field = &fip->baseclasses.back ();
14985 }
14986 else
14987 {
14988 fip->fields.emplace_back ();
14989 new_field = &fip->fields.back ();
14990 }
14991
14992 fip->nfields++;
14993
14994 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14995 if (attr != nullptr)
14996 new_field->accessibility = DW_UNSND (attr);
14997 else
14998 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14999 if (new_field->accessibility != DW_ACCESS_public)
15000 fip->non_public_fields = 1;
15001
15002 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15003 if (attr != nullptr)
15004 new_field->virtuality = DW_UNSND (attr);
15005 else
15006 new_field->virtuality = DW_VIRTUALITY_none;
15007
15008 fp = &new_field->field;
15009
15010 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15011 {
15012 LONGEST offset;
15013
15014 /* Data member other than a C++ static data member. */
15015
15016 /* Get type of field. */
15017 fp->type = die_type (die, cu);
15018
15019 SET_FIELD_BITPOS (*fp, 0);
15020
15021 /* Get bit size of field (zero if none). */
15022 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15023 if (attr != nullptr)
15024 {
15025 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15026 }
15027 else
15028 {
15029 FIELD_BITSIZE (*fp) = 0;
15030 }
15031
15032 /* Get bit offset of field. */
15033 if (handle_data_member_location (die, cu, &offset))
15034 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15035 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15036 if (attr != nullptr)
15037 {
15038 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15039 {
15040 /* For big endian bits, the DW_AT_bit_offset gives the
15041 additional bit offset from the MSB of the containing
15042 anonymous object to the MSB of the field. We don't
15043 have to do anything special since we don't need to
15044 know the size of the anonymous object. */
15045 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15046 }
15047 else
15048 {
15049 /* For little endian bits, compute the bit offset to the
15050 MSB of the anonymous object, subtract off the number of
15051 bits from the MSB of the field to the MSB of the
15052 object, and then subtract off the number of bits of
15053 the field itself. The result is the bit offset of
15054 the LSB of the field. */
15055 int anonymous_size;
15056 int bit_offset = DW_UNSND (attr);
15057
15058 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15059 if (attr != nullptr)
15060 {
15061 /* The size of the anonymous object containing
15062 the bit field is explicit, so use the
15063 indicated size (in bytes). */
15064 anonymous_size = DW_UNSND (attr);
15065 }
15066 else
15067 {
15068 /* The size of the anonymous object containing
15069 the bit field must be inferred from the type
15070 attribute of the data member containing the
15071 bit field. */
15072 anonymous_size = TYPE_LENGTH (fp->type);
15073 }
15074 SET_FIELD_BITPOS (*fp,
15075 (FIELD_BITPOS (*fp)
15076 + anonymous_size * bits_per_byte
15077 - bit_offset - FIELD_BITSIZE (*fp)));
15078 }
15079 }
15080 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15081 if (attr != NULL)
15082 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15083 + dwarf2_get_attr_constant_value (attr, 0)));
15084
15085 /* Get name of field. */
15086 fieldname = dwarf2_name (die, cu);
15087 if (fieldname == NULL)
15088 fieldname = "";
15089
15090 /* The name is already allocated along with this objfile, so we don't
15091 need to duplicate it for the type. */
15092 fp->name = fieldname;
15093
15094 /* Change accessibility for artificial fields (e.g. virtual table
15095 pointer or virtual base class pointer) to private. */
15096 if (dwarf2_attr (die, DW_AT_artificial, cu))
15097 {
15098 FIELD_ARTIFICIAL (*fp) = 1;
15099 new_field->accessibility = DW_ACCESS_private;
15100 fip->non_public_fields = 1;
15101 }
15102 }
15103 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15104 {
15105 /* C++ static member. */
15106
15107 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15108 is a declaration, but all versions of G++ as of this writing
15109 (so through at least 3.2.1) incorrectly generate
15110 DW_TAG_variable tags. */
15111
15112 const char *physname;
15113
15114 /* Get name of field. */
15115 fieldname = dwarf2_name (die, cu);
15116 if (fieldname == NULL)
15117 return;
15118
15119 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15120 if (attr
15121 /* Only create a symbol if this is an external value.
15122 new_symbol checks this and puts the value in the global symbol
15123 table, which we want. If it is not external, new_symbol
15124 will try to put the value in cu->list_in_scope which is wrong. */
15125 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15126 {
15127 /* A static const member, not much different than an enum as far as
15128 we're concerned, except that we can support more types. */
15129 new_symbol (die, NULL, cu);
15130 }
15131
15132 /* Get physical name. */
15133 physname = dwarf2_physname (fieldname, die, cu);
15134
15135 /* The name is already allocated along with this objfile, so we don't
15136 need to duplicate it for the type. */
15137 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15138 FIELD_TYPE (*fp) = die_type (die, cu);
15139 FIELD_NAME (*fp) = fieldname;
15140 }
15141 else if (die->tag == DW_TAG_inheritance)
15142 {
15143 LONGEST offset;
15144
15145 /* C++ base class field. */
15146 if (handle_data_member_location (die, cu, &offset))
15147 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15148 FIELD_BITSIZE (*fp) = 0;
15149 FIELD_TYPE (*fp) = die_type (die, cu);
15150 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
15151 }
15152 else if (die->tag == DW_TAG_variant_part)
15153 {
15154 /* process_structure_scope will treat this DIE as a union. */
15155 process_structure_scope (die, cu);
15156
15157 /* The variant part is relative to the start of the enclosing
15158 structure. */
15159 SET_FIELD_BITPOS (*fp, 0);
15160 fp->type = get_die_type (die, cu);
15161 fp->artificial = 1;
15162 fp->name = "<<variant>>";
15163
15164 /* Normally a DW_TAG_variant_part won't have a size, but our
15165 representation requires one, so set it to the maximum of the
15166 child sizes, being sure to account for the offset at which
15167 each child is seen. */
15168 if (TYPE_LENGTH (fp->type) == 0)
15169 {
15170 unsigned max = 0;
15171 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15172 {
15173 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
15174 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
15175 if (len > max)
15176 max = len;
15177 }
15178 TYPE_LENGTH (fp->type) = max;
15179 }
15180 }
15181 else
15182 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15183 }
15184
15185 /* Can the type given by DIE define another type? */
15186
15187 static bool
15188 type_can_define_types (const struct die_info *die)
15189 {
15190 switch (die->tag)
15191 {
15192 case DW_TAG_typedef:
15193 case DW_TAG_class_type:
15194 case DW_TAG_structure_type:
15195 case DW_TAG_union_type:
15196 case DW_TAG_enumeration_type:
15197 return true;
15198
15199 default:
15200 return false;
15201 }
15202 }
15203
15204 /* Add a type definition defined in the scope of the FIP's class. */
15205
15206 static void
15207 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15208 struct dwarf2_cu *cu)
15209 {
15210 struct decl_field fp;
15211 memset (&fp, 0, sizeof (fp));
15212
15213 gdb_assert (type_can_define_types (die));
15214
15215 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15216 fp.name = dwarf2_name (die, cu);
15217 fp.type = read_type_die (die, cu);
15218
15219 /* Save accessibility. */
15220 enum dwarf_access_attribute accessibility;
15221 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15222 if (attr != NULL)
15223 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15224 else
15225 accessibility = dwarf2_default_access_attribute (die, cu);
15226 switch (accessibility)
15227 {
15228 case DW_ACCESS_public:
15229 /* The assumed value if neither private nor protected. */
15230 break;
15231 case DW_ACCESS_private:
15232 fp.is_private = 1;
15233 break;
15234 case DW_ACCESS_protected:
15235 fp.is_protected = 1;
15236 break;
15237 default:
15238 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15239 }
15240
15241 if (die->tag == DW_TAG_typedef)
15242 fip->typedef_field_list.push_back (fp);
15243 else
15244 fip->nested_types_list.push_back (fp);
15245 }
15246
15247 /* Create the vector of fields, and attach it to the type. */
15248
15249 static void
15250 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15251 struct dwarf2_cu *cu)
15252 {
15253 int nfields = fip->nfields;
15254
15255 /* Record the field count, allocate space for the array of fields,
15256 and create blank accessibility bitfields if necessary. */
15257 TYPE_NFIELDS (type) = nfields;
15258 TYPE_FIELDS (type) = (struct field *)
15259 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15260
15261 if (fip->non_public_fields && cu->language != language_ada)
15262 {
15263 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15264
15265 TYPE_FIELD_PRIVATE_BITS (type) =
15266 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15267 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15268
15269 TYPE_FIELD_PROTECTED_BITS (type) =
15270 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15271 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15272
15273 TYPE_FIELD_IGNORE_BITS (type) =
15274 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15275 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15276 }
15277
15278 /* If the type has baseclasses, allocate and clear a bit vector for
15279 TYPE_FIELD_VIRTUAL_BITS. */
15280 if (!fip->baseclasses.empty () && cu->language != language_ada)
15281 {
15282 int num_bytes = B_BYTES (fip->baseclasses.size ());
15283 unsigned char *pointer;
15284
15285 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15286 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15287 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15288 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15289 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15290 }
15291
15292 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15293 {
15294 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15295
15296 for (int index = 0; index < nfields; ++index)
15297 {
15298 struct nextfield &field = fip->fields[index];
15299
15300 if (field.variant.is_discriminant)
15301 di->discriminant_index = index;
15302 else if (field.variant.default_branch)
15303 di->default_index = index;
15304 else
15305 di->discriminants[index] = field.variant.discriminant_value;
15306 }
15307 }
15308
15309 /* Copy the saved-up fields into the field vector. */
15310 for (int i = 0; i < nfields; ++i)
15311 {
15312 struct nextfield &field
15313 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15314 : fip->fields[i - fip->baseclasses.size ()]);
15315
15316 TYPE_FIELD (type, i) = field.field;
15317 switch (field.accessibility)
15318 {
15319 case DW_ACCESS_private:
15320 if (cu->language != language_ada)
15321 SET_TYPE_FIELD_PRIVATE (type, i);
15322 break;
15323
15324 case DW_ACCESS_protected:
15325 if (cu->language != language_ada)
15326 SET_TYPE_FIELD_PROTECTED (type, i);
15327 break;
15328
15329 case DW_ACCESS_public:
15330 break;
15331
15332 default:
15333 /* Unknown accessibility. Complain and treat it as public. */
15334 {
15335 complaint (_("unsupported accessibility %d"),
15336 field.accessibility);
15337 }
15338 break;
15339 }
15340 if (i < fip->baseclasses.size ())
15341 {
15342 switch (field.virtuality)
15343 {
15344 case DW_VIRTUALITY_virtual:
15345 case DW_VIRTUALITY_pure_virtual:
15346 if (cu->language == language_ada)
15347 error (_("unexpected virtuality in component of Ada type"));
15348 SET_TYPE_FIELD_VIRTUAL (type, i);
15349 break;
15350 }
15351 }
15352 }
15353 }
15354
15355 /* Return true if this member function is a constructor, false
15356 otherwise. */
15357
15358 static int
15359 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15360 {
15361 const char *fieldname;
15362 const char *type_name;
15363 int len;
15364
15365 if (die->parent == NULL)
15366 return 0;
15367
15368 if (die->parent->tag != DW_TAG_structure_type
15369 && die->parent->tag != DW_TAG_union_type
15370 && die->parent->tag != DW_TAG_class_type)
15371 return 0;
15372
15373 fieldname = dwarf2_name (die, cu);
15374 type_name = dwarf2_name (die->parent, cu);
15375 if (fieldname == NULL || type_name == NULL)
15376 return 0;
15377
15378 len = strlen (fieldname);
15379 return (strncmp (fieldname, type_name, len) == 0
15380 && (type_name[len] == '\0' || type_name[len] == '<'));
15381 }
15382
15383 /* Check if the given VALUE is a recognized enum
15384 dwarf_defaulted_attribute constant according to DWARF5 spec,
15385 Table 7.24. */
15386
15387 static bool
15388 is_valid_DW_AT_defaulted (ULONGEST value)
15389 {
15390 switch (value)
15391 {
15392 case DW_DEFAULTED_no:
15393 case DW_DEFAULTED_in_class:
15394 case DW_DEFAULTED_out_of_class:
15395 return true;
15396 }
15397
15398 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15399 return false;
15400 }
15401
15402 /* Add a member function to the proper fieldlist. */
15403
15404 static void
15405 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15406 struct type *type, struct dwarf2_cu *cu)
15407 {
15408 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15409 struct attribute *attr;
15410 int i;
15411 struct fnfieldlist *flp = nullptr;
15412 struct fn_field *fnp;
15413 const char *fieldname;
15414 struct type *this_type;
15415 enum dwarf_access_attribute accessibility;
15416
15417 if (cu->language == language_ada)
15418 error (_("unexpected member function in Ada type"));
15419
15420 /* Get name of member function. */
15421 fieldname = dwarf2_name (die, cu);
15422 if (fieldname == NULL)
15423 return;
15424
15425 /* Look up member function name in fieldlist. */
15426 for (i = 0; i < fip->fnfieldlists.size (); i++)
15427 {
15428 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15429 {
15430 flp = &fip->fnfieldlists[i];
15431 break;
15432 }
15433 }
15434
15435 /* Create a new fnfieldlist if necessary. */
15436 if (flp == nullptr)
15437 {
15438 fip->fnfieldlists.emplace_back ();
15439 flp = &fip->fnfieldlists.back ();
15440 flp->name = fieldname;
15441 i = fip->fnfieldlists.size () - 1;
15442 }
15443
15444 /* Create a new member function field and add it to the vector of
15445 fnfieldlists. */
15446 flp->fnfields.emplace_back ();
15447 fnp = &flp->fnfields.back ();
15448
15449 /* Delay processing of the physname until later. */
15450 if (cu->language == language_cplus)
15451 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15452 die, cu);
15453 else
15454 {
15455 const char *physname = dwarf2_physname (fieldname, die, cu);
15456 fnp->physname = physname ? physname : "";
15457 }
15458
15459 fnp->type = alloc_type (objfile);
15460 this_type = read_type_die (die, cu);
15461 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15462 {
15463 int nparams = TYPE_NFIELDS (this_type);
15464
15465 /* TYPE is the domain of this method, and THIS_TYPE is the type
15466 of the method itself (TYPE_CODE_METHOD). */
15467 smash_to_method_type (fnp->type, type,
15468 TYPE_TARGET_TYPE (this_type),
15469 TYPE_FIELDS (this_type),
15470 TYPE_NFIELDS (this_type),
15471 TYPE_VARARGS (this_type));
15472
15473 /* Handle static member functions.
15474 Dwarf2 has no clean way to discern C++ static and non-static
15475 member functions. G++ helps GDB by marking the first
15476 parameter for non-static member functions (which is the this
15477 pointer) as artificial. We obtain this information from
15478 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15479 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15480 fnp->voffset = VOFFSET_STATIC;
15481 }
15482 else
15483 complaint (_("member function type missing for '%s'"),
15484 dwarf2_full_name (fieldname, die, cu));
15485
15486 /* Get fcontext from DW_AT_containing_type if present. */
15487 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15488 fnp->fcontext = die_containing_type (die, cu);
15489
15490 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15491 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15492
15493 /* Get accessibility. */
15494 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15495 if (attr != nullptr)
15496 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15497 else
15498 accessibility = dwarf2_default_access_attribute (die, cu);
15499 switch (accessibility)
15500 {
15501 case DW_ACCESS_private:
15502 fnp->is_private = 1;
15503 break;
15504 case DW_ACCESS_protected:
15505 fnp->is_protected = 1;
15506 break;
15507 }
15508
15509 /* Check for artificial methods. */
15510 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15511 if (attr && DW_UNSND (attr) != 0)
15512 fnp->is_artificial = 1;
15513
15514 /* Check for defaulted methods. */
15515 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15516 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15517 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15518
15519 /* Check for deleted methods. */
15520 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15521 if (attr != nullptr && DW_UNSND (attr) != 0)
15522 fnp->is_deleted = 1;
15523
15524 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15525
15526 /* Get index in virtual function table if it is a virtual member
15527 function. For older versions of GCC, this is an offset in the
15528 appropriate virtual table, as specified by DW_AT_containing_type.
15529 For everyone else, it is an expression to be evaluated relative
15530 to the object address. */
15531
15532 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15533 if (attr != nullptr)
15534 {
15535 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15536 {
15537 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15538 {
15539 /* Old-style GCC. */
15540 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15541 }
15542 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15543 || (DW_BLOCK (attr)->size > 1
15544 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15545 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15546 {
15547 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15548 if ((fnp->voffset % cu->header.addr_size) != 0)
15549 dwarf2_complex_location_expr_complaint ();
15550 else
15551 fnp->voffset /= cu->header.addr_size;
15552 fnp->voffset += 2;
15553 }
15554 else
15555 dwarf2_complex_location_expr_complaint ();
15556
15557 if (!fnp->fcontext)
15558 {
15559 /* If there is no `this' field and no DW_AT_containing_type,
15560 we cannot actually find a base class context for the
15561 vtable! */
15562 if (TYPE_NFIELDS (this_type) == 0
15563 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15564 {
15565 complaint (_("cannot determine context for virtual member "
15566 "function \"%s\" (offset %s)"),
15567 fieldname, sect_offset_str (die->sect_off));
15568 }
15569 else
15570 {
15571 fnp->fcontext
15572 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15573 }
15574 }
15575 }
15576 else if (attr_form_is_section_offset (attr))
15577 {
15578 dwarf2_complex_location_expr_complaint ();
15579 }
15580 else
15581 {
15582 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15583 fieldname);
15584 }
15585 }
15586 else
15587 {
15588 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15589 if (attr && DW_UNSND (attr))
15590 {
15591 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15592 complaint (_("Member function \"%s\" (offset %s) is virtual "
15593 "but the vtable offset is not specified"),
15594 fieldname, sect_offset_str (die->sect_off));
15595 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15596 TYPE_CPLUS_DYNAMIC (type) = 1;
15597 }
15598 }
15599 }
15600
15601 /* Create the vector of member function fields, and attach it to the type. */
15602
15603 static void
15604 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15605 struct dwarf2_cu *cu)
15606 {
15607 if (cu->language == language_ada)
15608 error (_("unexpected member functions in Ada type"));
15609
15610 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15611 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15612 TYPE_ALLOC (type,
15613 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15614
15615 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15616 {
15617 struct fnfieldlist &nf = fip->fnfieldlists[i];
15618 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15619
15620 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15621 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15622 fn_flp->fn_fields = (struct fn_field *)
15623 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15624
15625 for (int k = 0; k < nf.fnfields.size (); ++k)
15626 fn_flp->fn_fields[k] = nf.fnfields[k];
15627 }
15628
15629 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15630 }
15631
15632 /* Returns non-zero if NAME is the name of a vtable member in CU's
15633 language, zero otherwise. */
15634 static int
15635 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15636 {
15637 static const char vptr[] = "_vptr";
15638
15639 /* Look for the C++ form of the vtable. */
15640 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15641 return 1;
15642
15643 return 0;
15644 }
15645
15646 /* GCC outputs unnamed structures that are really pointers to member
15647 functions, with the ABI-specified layout. If TYPE describes
15648 such a structure, smash it into a member function type.
15649
15650 GCC shouldn't do this; it should just output pointer to member DIEs.
15651 This is GCC PR debug/28767. */
15652
15653 static void
15654 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15655 {
15656 struct type *pfn_type, *self_type, *new_type;
15657
15658 /* Check for a structure with no name and two children. */
15659 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15660 return;
15661
15662 /* Check for __pfn and __delta members. */
15663 if (TYPE_FIELD_NAME (type, 0) == NULL
15664 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15665 || TYPE_FIELD_NAME (type, 1) == NULL
15666 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15667 return;
15668
15669 /* Find the type of the method. */
15670 pfn_type = TYPE_FIELD_TYPE (type, 0);
15671 if (pfn_type == NULL
15672 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15673 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15674 return;
15675
15676 /* Look for the "this" argument. */
15677 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15678 if (TYPE_NFIELDS (pfn_type) == 0
15679 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15680 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15681 return;
15682
15683 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15684 new_type = alloc_type (objfile);
15685 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15686 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15687 TYPE_VARARGS (pfn_type));
15688 smash_to_methodptr_type (type, new_type);
15689 }
15690
15691 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15692 appropriate error checking and issuing complaints if there is a
15693 problem. */
15694
15695 static ULONGEST
15696 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15697 {
15698 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15699
15700 if (attr == nullptr)
15701 return 0;
15702
15703 if (!attr_form_is_constant (attr))
15704 {
15705 complaint (_("DW_AT_alignment must have constant form"
15706 " - DIE at %s [in module %s]"),
15707 sect_offset_str (die->sect_off),
15708 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15709 return 0;
15710 }
15711
15712 ULONGEST align;
15713 if (attr->form == DW_FORM_sdata)
15714 {
15715 LONGEST val = DW_SND (attr);
15716 if (val < 0)
15717 {
15718 complaint (_("DW_AT_alignment value must not be negative"
15719 " - DIE at %s [in module %s]"),
15720 sect_offset_str (die->sect_off),
15721 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15722 return 0;
15723 }
15724 align = val;
15725 }
15726 else
15727 align = DW_UNSND (attr);
15728
15729 if (align == 0)
15730 {
15731 complaint (_("DW_AT_alignment value must not be zero"
15732 " - DIE at %s [in module %s]"),
15733 sect_offset_str (die->sect_off),
15734 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15735 return 0;
15736 }
15737 if ((align & (align - 1)) != 0)
15738 {
15739 complaint (_("DW_AT_alignment value must be a power of 2"
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
15746 return align;
15747 }
15748
15749 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15750 the alignment for TYPE. */
15751
15752 static void
15753 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15754 struct type *type)
15755 {
15756 if (!set_type_align (type, get_alignment (cu, die)))
15757 complaint (_("DW_AT_alignment value too large"
15758 " - DIE at %s [in module %s]"),
15759 sect_offset_str (die->sect_off),
15760 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15761 }
15762
15763 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15764 constant for a type, according to DWARF5 spec, Table 5.5. */
15765
15766 static bool
15767 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15768 {
15769 switch (value)
15770 {
15771 case DW_CC_normal:
15772 case DW_CC_pass_by_reference:
15773 case DW_CC_pass_by_value:
15774 return true;
15775
15776 default:
15777 complaint (_("unrecognized DW_AT_calling_convention value "
15778 "(%s) for a type"), pulongest (value));
15779 return false;
15780 }
15781 }
15782
15783 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15784 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15785 also according to GNU-specific values (see include/dwarf2.h). */
15786
15787 static bool
15788 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15789 {
15790 switch (value)
15791 {
15792 case DW_CC_normal:
15793 case DW_CC_program:
15794 case DW_CC_nocall:
15795 return true;
15796
15797 case DW_CC_GNU_renesas_sh:
15798 case DW_CC_GNU_borland_fastcall_i386:
15799 case DW_CC_GDB_IBM_OpenCL:
15800 return true;
15801
15802 default:
15803 complaint (_("unrecognized DW_AT_calling_convention value "
15804 "(%s) for a subroutine"), pulongest (value));
15805 return false;
15806 }
15807 }
15808
15809 /* Called when we find the DIE that starts a structure or union scope
15810 (definition) to create a type for the structure or union. Fill in
15811 the type's name and general properties; the members will not be
15812 processed until process_structure_scope. A symbol table entry for
15813 the type will also not be done until process_structure_scope (assuming
15814 the type has a name).
15815
15816 NOTE: we need to call these functions regardless of whether or not the
15817 DIE has a DW_AT_name attribute, since it might be an anonymous
15818 structure or union. This gets the type entered into our set of
15819 user defined types. */
15820
15821 static struct type *
15822 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15823 {
15824 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15825 struct type *type;
15826 struct attribute *attr;
15827 const char *name;
15828
15829 /* If the definition of this type lives in .debug_types, read that type.
15830 Don't follow DW_AT_specification though, that will take us back up
15831 the chain and we want to go down. */
15832 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15833 if (attr != nullptr)
15834 {
15835 type = get_DW_AT_signature_type (die, attr, cu);
15836
15837 /* The type's CU may not be the same as CU.
15838 Ensure TYPE is recorded with CU in die_type_hash. */
15839 return set_die_type (die, type, cu);
15840 }
15841
15842 type = alloc_type (objfile);
15843 INIT_CPLUS_SPECIFIC (type);
15844
15845 name = dwarf2_name (die, cu);
15846 if (name != NULL)
15847 {
15848 if (cu->language == language_cplus
15849 || cu->language == language_d
15850 || cu->language == language_rust)
15851 {
15852 const char *full_name = dwarf2_full_name (name, die, cu);
15853
15854 /* dwarf2_full_name might have already finished building the DIE's
15855 type. If so, there is no need to continue. */
15856 if (get_die_type (die, cu) != NULL)
15857 return get_die_type (die, cu);
15858
15859 TYPE_NAME (type) = full_name;
15860 }
15861 else
15862 {
15863 /* The name is already allocated along with this objfile, so
15864 we don't need to duplicate it for the type. */
15865 TYPE_NAME (type) = name;
15866 }
15867 }
15868
15869 if (die->tag == DW_TAG_structure_type)
15870 {
15871 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15872 }
15873 else if (die->tag == DW_TAG_union_type)
15874 {
15875 TYPE_CODE (type) = TYPE_CODE_UNION;
15876 }
15877 else if (die->tag == DW_TAG_variant_part)
15878 {
15879 TYPE_CODE (type) = TYPE_CODE_UNION;
15880 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15881 }
15882 else
15883 {
15884 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15885 }
15886
15887 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15888 TYPE_DECLARED_CLASS (type) = 1;
15889
15890 /* Store the calling convention in the type if it's available in
15891 the die. Otherwise the calling convention remains set to
15892 the default value DW_CC_normal. */
15893 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15894 if (attr != nullptr
15895 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15896 {
15897 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15898 TYPE_CPLUS_CALLING_CONVENTION (type)
15899 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15900 }
15901
15902 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15903 if (attr != nullptr)
15904 {
15905 if (attr_form_is_constant (attr))
15906 TYPE_LENGTH (type) = DW_UNSND (attr);
15907 else
15908 {
15909 /* For the moment, dynamic type sizes are not supported
15910 by GDB's struct type. The actual size is determined
15911 on-demand when resolving the type of a given object,
15912 so set the type's length to zero for now. Otherwise,
15913 we record an expression as the length, and that expression
15914 could lead to a very large value, which could eventually
15915 lead to us trying to allocate that much memory when creating
15916 a value of that type. */
15917 TYPE_LENGTH (type) = 0;
15918 }
15919 }
15920 else
15921 {
15922 TYPE_LENGTH (type) = 0;
15923 }
15924
15925 maybe_set_alignment (cu, die, type);
15926
15927 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15928 {
15929 /* ICC<14 does not output the required DW_AT_declaration on
15930 incomplete types, but gives them a size of zero. */
15931 TYPE_STUB (type) = 1;
15932 }
15933 else
15934 TYPE_STUB_SUPPORTED (type) = 1;
15935
15936 if (die_is_declaration (die, cu))
15937 TYPE_STUB (type) = 1;
15938 else if (attr == NULL && die->child == NULL
15939 && producer_is_realview (cu->producer))
15940 /* RealView does not output the required DW_AT_declaration
15941 on incomplete types. */
15942 TYPE_STUB (type) = 1;
15943
15944 /* We need to add the type field to the die immediately so we don't
15945 infinitely recurse when dealing with pointers to the structure
15946 type within the structure itself. */
15947 set_die_type (die, type, cu);
15948
15949 /* set_die_type should be already done. */
15950 set_descriptive_type (type, die, cu);
15951
15952 return type;
15953 }
15954
15955 /* A helper for process_structure_scope that handles a single member
15956 DIE. */
15957
15958 static void
15959 handle_struct_member_die (struct die_info *child_die, struct type *type,
15960 struct field_info *fi,
15961 std::vector<struct symbol *> *template_args,
15962 struct dwarf2_cu *cu)
15963 {
15964 if (child_die->tag == DW_TAG_member
15965 || child_die->tag == DW_TAG_variable
15966 || child_die->tag == DW_TAG_variant_part)
15967 {
15968 /* NOTE: carlton/2002-11-05: A C++ static data member
15969 should be a DW_TAG_member that is a declaration, but
15970 all versions of G++ as of this writing (so through at
15971 least 3.2.1) incorrectly generate DW_TAG_variable
15972 tags for them instead. */
15973 dwarf2_add_field (fi, child_die, cu);
15974 }
15975 else if (child_die->tag == DW_TAG_subprogram)
15976 {
15977 /* Rust doesn't have member functions in the C++ sense.
15978 However, it does emit ordinary functions as children
15979 of a struct DIE. */
15980 if (cu->language == language_rust)
15981 read_func_scope (child_die, cu);
15982 else
15983 {
15984 /* C++ member function. */
15985 dwarf2_add_member_fn (fi, child_die, type, cu);
15986 }
15987 }
15988 else if (child_die->tag == DW_TAG_inheritance)
15989 {
15990 /* C++ base class field. */
15991 dwarf2_add_field (fi, child_die, cu);
15992 }
15993 else if (type_can_define_types (child_die))
15994 dwarf2_add_type_defn (fi, child_die, cu);
15995 else if (child_die->tag == DW_TAG_template_type_param
15996 || child_die->tag == DW_TAG_template_value_param)
15997 {
15998 struct symbol *arg = new_symbol (child_die, NULL, cu);
15999
16000 if (arg != NULL)
16001 template_args->push_back (arg);
16002 }
16003 else if (child_die->tag == DW_TAG_variant)
16004 {
16005 /* In a variant we want to get the discriminant and also add a
16006 field for our sole member child. */
16007 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16008
16009 for (die_info *variant_child = child_die->child;
16010 variant_child != NULL;
16011 variant_child = sibling_die (variant_child))
16012 {
16013 if (variant_child->tag == DW_TAG_member)
16014 {
16015 handle_struct_member_die (variant_child, type, fi,
16016 template_args, cu);
16017 /* Only handle the one. */
16018 break;
16019 }
16020 }
16021
16022 /* We don't handle this but we might as well report it if we see
16023 it. */
16024 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
16025 complaint (_("DW_AT_discr_list is not supported yet"
16026 " - DIE at %s [in module %s]"),
16027 sect_offset_str (child_die->sect_off),
16028 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16029
16030 /* The first field was just added, so we can stash the
16031 discriminant there. */
16032 gdb_assert (!fi->fields.empty ());
16033 if (discr == NULL)
16034 fi->fields.back ().variant.default_branch = true;
16035 else
16036 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
16037 }
16038 }
16039
16040 /* Finish creating a structure or union type, including filling in
16041 its members and creating a symbol for it. */
16042
16043 static void
16044 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16045 {
16046 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16047 struct die_info *child_die;
16048 struct type *type;
16049
16050 type = get_die_type (die, cu);
16051 if (type == NULL)
16052 type = read_structure_type (die, cu);
16053
16054 /* When reading a DW_TAG_variant_part, we need to notice when we
16055 read the discriminant member, so we can record it later in the
16056 discriminant_info. */
16057 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16058 sect_offset discr_offset {};
16059 bool has_template_parameters = false;
16060
16061 if (is_variant_part)
16062 {
16063 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16064 if (discr == NULL)
16065 {
16066 /* Maybe it's a univariant form, an extension we support.
16067 In this case arrange not to check the offset. */
16068 is_variant_part = false;
16069 }
16070 else if (attr_form_is_ref (discr))
16071 {
16072 struct dwarf2_cu *target_cu = cu;
16073 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16074
16075 discr_offset = target_die->sect_off;
16076 }
16077 else
16078 {
16079 complaint (_("DW_AT_discr does not have DIE reference form"
16080 " - DIE at %s [in module %s]"),
16081 sect_offset_str (die->sect_off),
16082 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16083 is_variant_part = false;
16084 }
16085 }
16086
16087 if (die->child != NULL && ! die_is_declaration (die, cu))
16088 {
16089 struct field_info fi;
16090 std::vector<struct symbol *> template_args;
16091
16092 child_die = die->child;
16093
16094 while (child_die && child_die->tag)
16095 {
16096 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16097
16098 if (is_variant_part && discr_offset == child_die->sect_off)
16099 fi.fields.back ().variant.is_discriminant = true;
16100
16101 child_die = sibling_die (child_die);
16102 }
16103
16104 /* Attach template arguments to type. */
16105 if (!template_args.empty ())
16106 {
16107 has_template_parameters = true;
16108 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16109 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16110 TYPE_TEMPLATE_ARGUMENTS (type)
16111 = XOBNEWVEC (&objfile->objfile_obstack,
16112 struct symbol *,
16113 TYPE_N_TEMPLATE_ARGUMENTS (type));
16114 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16115 template_args.data (),
16116 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16117 * sizeof (struct symbol *)));
16118 }
16119
16120 /* Attach fields and member functions to the type. */
16121 if (fi.nfields)
16122 dwarf2_attach_fields_to_type (&fi, type, cu);
16123 if (!fi.fnfieldlists.empty ())
16124 {
16125 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16126
16127 /* Get the type which refers to the base class (possibly this
16128 class itself) which contains the vtable pointer for the current
16129 class from the DW_AT_containing_type attribute. This use of
16130 DW_AT_containing_type is a GNU extension. */
16131
16132 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16133 {
16134 struct type *t = die_containing_type (die, cu);
16135
16136 set_type_vptr_basetype (type, t);
16137 if (type == t)
16138 {
16139 int i;
16140
16141 /* Our own class provides vtbl ptr. */
16142 for (i = TYPE_NFIELDS (t) - 1;
16143 i >= TYPE_N_BASECLASSES (t);
16144 --i)
16145 {
16146 const char *fieldname = TYPE_FIELD_NAME (t, i);
16147
16148 if (is_vtable_name (fieldname, cu))
16149 {
16150 set_type_vptr_fieldno (type, i);
16151 break;
16152 }
16153 }
16154
16155 /* Complain if virtual function table field not found. */
16156 if (i < TYPE_N_BASECLASSES (t))
16157 complaint (_("virtual function table pointer "
16158 "not found when defining class '%s'"),
16159 TYPE_NAME (type) ? TYPE_NAME (type) : "");
16160 }
16161 else
16162 {
16163 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16164 }
16165 }
16166 else if (cu->producer
16167 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16168 {
16169 /* The IBM XLC compiler does not provide direct indication
16170 of the containing type, but the vtable pointer is
16171 always named __vfp. */
16172
16173 int i;
16174
16175 for (i = TYPE_NFIELDS (type) - 1;
16176 i >= TYPE_N_BASECLASSES (type);
16177 --i)
16178 {
16179 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16180 {
16181 set_type_vptr_fieldno (type, i);
16182 set_type_vptr_basetype (type, type);
16183 break;
16184 }
16185 }
16186 }
16187 }
16188
16189 /* Copy fi.typedef_field_list linked list elements content into the
16190 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16191 if (!fi.typedef_field_list.empty ())
16192 {
16193 int count = fi.typedef_field_list.size ();
16194
16195 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16196 TYPE_TYPEDEF_FIELD_ARRAY (type)
16197 = ((struct decl_field *)
16198 TYPE_ALLOC (type,
16199 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16200 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16201
16202 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16203 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16204 }
16205
16206 /* Copy fi.nested_types_list linked list elements content into the
16207 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16208 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16209 {
16210 int count = fi.nested_types_list.size ();
16211
16212 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16213 TYPE_NESTED_TYPES_ARRAY (type)
16214 = ((struct decl_field *)
16215 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16216 TYPE_NESTED_TYPES_COUNT (type) = count;
16217
16218 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16219 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16220 }
16221 }
16222
16223 quirk_gcc_member_function_pointer (type, objfile);
16224 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16225 cu->rust_unions.push_back (type);
16226
16227 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16228 snapshots) has been known to create a die giving a declaration
16229 for a class that has, as a child, a die giving a definition for a
16230 nested class. So we have to process our children even if the
16231 current die is a declaration. Normally, of course, a declaration
16232 won't have any children at all. */
16233
16234 child_die = die->child;
16235
16236 while (child_die != NULL && child_die->tag)
16237 {
16238 if (child_die->tag == DW_TAG_member
16239 || child_die->tag == DW_TAG_variable
16240 || child_die->tag == DW_TAG_inheritance
16241 || child_die->tag == DW_TAG_template_value_param
16242 || child_die->tag == DW_TAG_template_type_param)
16243 {
16244 /* Do nothing. */
16245 }
16246 else
16247 process_die (child_die, cu);
16248
16249 child_die = sibling_die (child_die);
16250 }
16251
16252 /* Do not consider external references. According to the DWARF standard,
16253 these DIEs are identified by the fact that they have no byte_size
16254 attribute, and a declaration attribute. */
16255 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16256 || !die_is_declaration (die, cu))
16257 {
16258 struct symbol *sym = new_symbol (die, type, cu);
16259
16260 if (has_template_parameters)
16261 {
16262 struct symtab *symtab;
16263 if (sym != nullptr)
16264 symtab = symbol_symtab (sym);
16265 else if (cu->line_header != nullptr)
16266 {
16267 /* Any related symtab will do. */
16268 symtab
16269 = cu->line_header->file_names ()[0].symtab;
16270 }
16271 else
16272 {
16273 symtab = nullptr;
16274 complaint (_("could not find suitable "
16275 "symtab for template parameter"
16276 " - DIE at %s [in module %s]"),
16277 sect_offset_str (die->sect_off),
16278 objfile_name (objfile));
16279 }
16280
16281 if (symtab != nullptr)
16282 {
16283 /* Make sure that the symtab is set on the new symbols.
16284 Even though they don't appear in this symtab directly,
16285 other parts of gdb assume that symbols do, and this is
16286 reasonably true. */
16287 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16288 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16289 }
16290 }
16291 }
16292 }
16293
16294 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16295 update TYPE using some information only available in DIE's children. */
16296
16297 static void
16298 update_enumeration_type_from_children (struct die_info *die,
16299 struct type *type,
16300 struct dwarf2_cu *cu)
16301 {
16302 struct die_info *child_die;
16303 int unsigned_enum = 1;
16304 int flag_enum = 1;
16305 ULONGEST mask = 0;
16306
16307 auto_obstack obstack;
16308
16309 for (child_die = die->child;
16310 child_die != NULL && child_die->tag;
16311 child_die = sibling_die (child_die))
16312 {
16313 struct attribute *attr;
16314 LONGEST value;
16315 const gdb_byte *bytes;
16316 struct dwarf2_locexpr_baton *baton;
16317 const char *name;
16318
16319 if (child_die->tag != DW_TAG_enumerator)
16320 continue;
16321
16322 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16323 if (attr == NULL)
16324 continue;
16325
16326 name = dwarf2_name (child_die, cu);
16327 if (name == NULL)
16328 name = "<anonymous enumerator>";
16329
16330 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16331 &value, &bytes, &baton);
16332 if (value < 0)
16333 {
16334 unsigned_enum = 0;
16335 flag_enum = 0;
16336 }
16337 else if ((mask & value) != 0)
16338 flag_enum = 0;
16339 else
16340 mask |= value;
16341
16342 /* If we already know that the enum type is neither unsigned, nor
16343 a flag type, no need to look at the rest of the enumerates. */
16344 if (!unsigned_enum && !flag_enum)
16345 break;
16346 }
16347
16348 if (unsigned_enum)
16349 TYPE_UNSIGNED (type) = 1;
16350 if (flag_enum)
16351 TYPE_FLAG_ENUM (type) = 1;
16352 }
16353
16354 /* Given a DW_AT_enumeration_type die, set its type. We do not
16355 complete the type's fields yet, or create any symbols. */
16356
16357 static struct type *
16358 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16359 {
16360 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16361 struct type *type;
16362 struct attribute *attr;
16363 const char *name;
16364
16365 /* If the definition of this type lives in .debug_types, read that type.
16366 Don't follow DW_AT_specification though, that will take us back up
16367 the chain and we want to go down. */
16368 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16369 if (attr != nullptr)
16370 {
16371 type = get_DW_AT_signature_type (die, attr, cu);
16372
16373 /* The type's CU may not be the same as CU.
16374 Ensure TYPE is recorded with CU in die_type_hash. */
16375 return set_die_type (die, type, cu);
16376 }
16377
16378 type = alloc_type (objfile);
16379
16380 TYPE_CODE (type) = TYPE_CODE_ENUM;
16381 name = dwarf2_full_name (NULL, die, cu);
16382 if (name != NULL)
16383 TYPE_NAME (type) = name;
16384
16385 attr = dwarf2_attr (die, DW_AT_type, cu);
16386 if (attr != NULL)
16387 {
16388 struct type *underlying_type = die_type (die, cu);
16389
16390 TYPE_TARGET_TYPE (type) = underlying_type;
16391 }
16392
16393 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16394 if (attr != nullptr)
16395 {
16396 TYPE_LENGTH (type) = DW_UNSND (attr);
16397 }
16398 else
16399 {
16400 TYPE_LENGTH (type) = 0;
16401 }
16402
16403 maybe_set_alignment (cu, die, type);
16404
16405 /* The enumeration DIE can be incomplete. In Ada, any type can be
16406 declared as private in the package spec, and then defined only
16407 inside the package body. Such types are known as Taft Amendment
16408 Types. When another package uses such a type, an incomplete DIE
16409 may be generated by the compiler. */
16410 if (die_is_declaration (die, cu))
16411 TYPE_STUB (type) = 1;
16412
16413 /* Finish the creation of this type by using the enum's children.
16414 We must call this even when the underlying type has been provided
16415 so that we can determine if we're looking at a "flag" enum. */
16416 update_enumeration_type_from_children (die, type, cu);
16417
16418 /* If this type has an underlying type that is not a stub, then we
16419 may use its attributes. We always use the "unsigned" attribute
16420 in this situation, because ordinarily we guess whether the type
16421 is unsigned -- but the guess can be wrong and the underlying type
16422 can tell us the reality. However, we defer to a local size
16423 attribute if one exists, because this lets the compiler override
16424 the underlying type if needed. */
16425 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16426 {
16427 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16428 if (TYPE_LENGTH (type) == 0)
16429 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16430 if (TYPE_RAW_ALIGN (type) == 0
16431 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16432 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16433 }
16434
16435 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16436
16437 return set_die_type (die, type, cu);
16438 }
16439
16440 /* Given a pointer to a die which begins an enumeration, process all
16441 the dies that define the members of the enumeration, and create the
16442 symbol for the enumeration type.
16443
16444 NOTE: We reverse the order of the element list. */
16445
16446 static void
16447 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16448 {
16449 struct type *this_type;
16450
16451 this_type = get_die_type (die, cu);
16452 if (this_type == NULL)
16453 this_type = read_enumeration_type (die, cu);
16454
16455 if (die->child != NULL)
16456 {
16457 struct die_info *child_die;
16458 struct symbol *sym;
16459 std::vector<struct field> fields;
16460 const char *name;
16461
16462 child_die = die->child;
16463 while (child_die && child_die->tag)
16464 {
16465 if (child_die->tag != DW_TAG_enumerator)
16466 {
16467 process_die (child_die, cu);
16468 }
16469 else
16470 {
16471 name = dwarf2_name (child_die, cu);
16472 if (name)
16473 {
16474 sym = new_symbol (child_die, this_type, cu);
16475
16476 fields.emplace_back ();
16477 struct field &field = fields.back ();
16478
16479 FIELD_NAME (field) = sym->linkage_name ();
16480 FIELD_TYPE (field) = NULL;
16481 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
16482 FIELD_BITSIZE (field) = 0;
16483 }
16484 }
16485
16486 child_die = sibling_die (child_die);
16487 }
16488
16489 if (!fields.empty ())
16490 {
16491 TYPE_NFIELDS (this_type) = fields.size ();
16492 TYPE_FIELDS (this_type) = (struct field *)
16493 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
16494 memcpy (TYPE_FIELDS (this_type), fields.data (),
16495 sizeof (struct field) * fields.size ());
16496 }
16497 }
16498
16499 /* If we are reading an enum from a .debug_types unit, and the enum
16500 is a declaration, and the enum is not the signatured type in the
16501 unit, then we do not want to add a symbol for it. Adding a
16502 symbol would in some cases obscure the true definition of the
16503 enum, giving users an incomplete type when the definition is
16504 actually available. Note that we do not want to do this for all
16505 enums which are just declarations, because C++0x allows forward
16506 enum declarations. */
16507 if (cu->per_cu->is_debug_types
16508 && die_is_declaration (die, cu))
16509 {
16510 struct signatured_type *sig_type;
16511
16512 sig_type = (struct signatured_type *) cu->per_cu;
16513 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16514 if (sig_type->type_offset_in_section != die->sect_off)
16515 return;
16516 }
16517
16518 new_symbol (die, this_type, cu);
16519 }
16520
16521 /* Extract all information from a DW_TAG_array_type DIE and put it in
16522 the DIE's type field. For now, this only handles one dimensional
16523 arrays. */
16524
16525 static struct type *
16526 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16527 {
16528 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16529 struct die_info *child_die;
16530 struct type *type;
16531 struct type *element_type, *range_type, *index_type;
16532 struct attribute *attr;
16533 const char *name;
16534 struct dynamic_prop *byte_stride_prop = NULL;
16535 unsigned int bit_stride = 0;
16536
16537 element_type = die_type (die, cu);
16538
16539 /* The die_type call above may have already set the type for this DIE. */
16540 type = get_die_type (die, cu);
16541 if (type)
16542 return type;
16543
16544 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16545 if (attr != NULL)
16546 {
16547 int stride_ok;
16548 struct type *prop_type
16549 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
16550
16551 byte_stride_prop
16552 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16553 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16554 prop_type);
16555 if (!stride_ok)
16556 {
16557 complaint (_("unable to read array DW_AT_byte_stride "
16558 " - DIE at %s [in module %s]"),
16559 sect_offset_str (die->sect_off),
16560 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16561 /* Ignore this attribute. We will likely not be able to print
16562 arrays of this type correctly, but there is little we can do
16563 to help if we cannot read the attribute's value. */
16564 byte_stride_prop = NULL;
16565 }
16566 }
16567
16568 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16569 if (attr != NULL)
16570 bit_stride = DW_UNSND (attr);
16571
16572 /* Irix 6.2 native cc creates array types without children for
16573 arrays with unspecified length. */
16574 if (die->child == NULL)
16575 {
16576 index_type = objfile_type (objfile)->builtin_int;
16577 range_type = create_static_range_type (NULL, index_type, 0, -1);
16578 type = create_array_type_with_stride (NULL, element_type, range_type,
16579 byte_stride_prop, bit_stride);
16580 return set_die_type (die, type, cu);
16581 }
16582
16583 std::vector<struct type *> range_types;
16584 child_die = die->child;
16585 while (child_die && child_die->tag)
16586 {
16587 if (child_die->tag == DW_TAG_subrange_type)
16588 {
16589 struct type *child_type = read_type_die (child_die, cu);
16590
16591 if (child_type != NULL)
16592 {
16593 /* The range type was succesfully read. Save it for the
16594 array type creation. */
16595 range_types.push_back (child_type);
16596 }
16597 }
16598 child_die = sibling_die (child_die);
16599 }
16600
16601 /* Dwarf2 dimensions are output from left to right, create the
16602 necessary array types in backwards order. */
16603
16604 type = element_type;
16605
16606 if (read_array_order (die, cu) == DW_ORD_col_major)
16607 {
16608 int i = 0;
16609
16610 while (i < range_types.size ())
16611 type = create_array_type_with_stride (NULL, type, range_types[i++],
16612 byte_stride_prop, bit_stride);
16613 }
16614 else
16615 {
16616 size_t ndim = range_types.size ();
16617 while (ndim-- > 0)
16618 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16619 byte_stride_prop, bit_stride);
16620 }
16621
16622 /* Understand Dwarf2 support for vector types (like they occur on
16623 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16624 array type. This is not part of the Dwarf2/3 standard yet, but a
16625 custom vendor extension. The main difference between a regular
16626 array and the vector variant is that vectors are passed by value
16627 to functions. */
16628 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16629 if (attr != nullptr)
16630 make_vector_type (type);
16631
16632 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16633 implementation may choose to implement triple vectors using this
16634 attribute. */
16635 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16636 if (attr != nullptr)
16637 {
16638 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16639 TYPE_LENGTH (type) = DW_UNSND (attr);
16640 else
16641 complaint (_("DW_AT_byte_size for array type smaller "
16642 "than the total size of elements"));
16643 }
16644
16645 name = dwarf2_name (die, cu);
16646 if (name)
16647 TYPE_NAME (type) = name;
16648
16649 maybe_set_alignment (cu, die, type);
16650
16651 /* Install the type in the die. */
16652 set_die_type (die, type, cu);
16653
16654 /* set_die_type should be already done. */
16655 set_descriptive_type (type, die, cu);
16656
16657 return type;
16658 }
16659
16660 static enum dwarf_array_dim_ordering
16661 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16662 {
16663 struct attribute *attr;
16664
16665 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16666
16667 if (attr != nullptr)
16668 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16669
16670 /* GNU F77 is a special case, as at 08/2004 array type info is the
16671 opposite order to the dwarf2 specification, but data is still
16672 laid out as per normal fortran.
16673
16674 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16675 version checking. */
16676
16677 if (cu->language == language_fortran
16678 && cu->producer && strstr (cu->producer, "GNU F77"))
16679 {
16680 return DW_ORD_row_major;
16681 }
16682
16683 switch (cu->language_defn->la_array_ordering)
16684 {
16685 case array_column_major:
16686 return DW_ORD_col_major;
16687 case array_row_major:
16688 default:
16689 return DW_ORD_row_major;
16690 };
16691 }
16692
16693 /* Extract all information from a DW_TAG_set_type DIE and put it in
16694 the DIE's type field. */
16695
16696 static struct type *
16697 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16698 {
16699 struct type *domain_type, *set_type;
16700 struct attribute *attr;
16701
16702 domain_type = die_type (die, cu);
16703
16704 /* The die_type call above may have already set the type for this DIE. */
16705 set_type = get_die_type (die, cu);
16706 if (set_type)
16707 return set_type;
16708
16709 set_type = create_set_type (NULL, domain_type);
16710
16711 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16712 if (attr != nullptr)
16713 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16714
16715 maybe_set_alignment (cu, die, set_type);
16716
16717 return set_die_type (die, set_type, cu);
16718 }
16719
16720 /* A helper for read_common_block that creates a locexpr baton.
16721 SYM is the symbol which we are marking as computed.
16722 COMMON_DIE is the DIE for the common block.
16723 COMMON_LOC is the location expression attribute for the common
16724 block itself.
16725 MEMBER_LOC is the location expression attribute for the particular
16726 member of the common block that we are processing.
16727 CU is the CU from which the above come. */
16728
16729 static void
16730 mark_common_block_symbol_computed (struct symbol *sym,
16731 struct die_info *common_die,
16732 struct attribute *common_loc,
16733 struct attribute *member_loc,
16734 struct dwarf2_cu *cu)
16735 {
16736 struct dwarf2_per_objfile *dwarf2_per_objfile
16737 = cu->per_cu->dwarf2_per_objfile;
16738 struct objfile *objfile = dwarf2_per_objfile->objfile;
16739 struct dwarf2_locexpr_baton *baton;
16740 gdb_byte *ptr;
16741 unsigned int cu_off;
16742 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16743 LONGEST offset = 0;
16744
16745 gdb_assert (common_loc && member_loc);
16746 gdb_assert (attr_form_is_block (common_loc));
16747 gdb_assert (attr_form_is_block (member_loc)
16748 || attr_form_is_constant (member_loc));
16749
16750 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16751 baton->per_cu = cu->per_cu;
16752 gdb_assert (baton->per_cu);
16753
16754 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16755
16756 if (attr_form_is_constant (member_loc))
16757 {
16758 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16759 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16760 }
16761 else
16762 baton->size += DW_BLOCK (member_loc)->size;
16763
16764 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16765 baton->data = ptr;
16766
16767 *ptr++ = DW_OP_call4;
16768 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16769 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16770 ptr += 4;
16771
16772 if (attr_form_is_constant (member_loc))
16773 {
16774 *ptr++ = DW_OP_addr;
16775 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16776 ptr += cu->header.addr_size;
16777 }
16778 else
16779 {
16780 /* We have to copy the data here, because DW_OP_call4 will only
16781 use a DW_AT_location attribute. */
16782 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16783 ptr += DW_BLOCK (member_loc)->size;
16784 }
16785
16786 *ptr++ = DW_OP_plus;
16787 gdb_assert (ptr - baton->data == baton->size);
16788
16789 SYMBOL_LOCATION_BATON (sym) = baton;
16790 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16791 }
16792
16793 /* Create appropriate locally-scoped variables for all the
16794 DW_TAG_common_block entries. Also create a struct common_block
16795 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16796 is used to separate the common blocks name namespace from regular
16797 variable names. */
16798
16799 static void
16800 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16801 {
16802 struct attribute *attr;
16803
16804 attr = dwarf2_attr (die, DW_AT_location, cu);
16805 if (attr != nullptr)
16806 {
16807 /* Support the .debug_loc offsets. */
16808 if (attr_form_is_block (attr))
16809 {
16810 /* Ok. */
16811 }
16812 else if (attr_form_is_section_offset (attr))
16813 {
16814 dwarf2_complex_location_expr_complaint ();
16815 attr = NULL;
16816 }
16817 else
16818 {
16819 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16820 "common block member");
16821 attr = NULL;
16822 }
16823 }
16824
16825 if (die->child != NULL)
16826 {
16827 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16828 struct die_info *child_die;
16829 size_t n_entries = 0, size;
16830 struct common_block *common_block;
16831 struct symbol *sym;
16832
16833 for (child_die = die->child;
16834 child_die && child_die->tag;
16835 child_die = sibling_die (child_die))
16836 ++n_entries;
16837
16838 size = (sizeof (struct common_block)
16839 + (n_entries - 1) * sizeof (struct symbol *));
16840 common_block
16841 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16842 size);
16843 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16844 common_block->n_entries = 0;
16845
16846 for (child_die = die->child;
16847 child_die && child_die->tag;
16848 child_die = sibling_die (child_die))
16849 {
16850 /* Create the symbol in the DW_TAG_common_block block in the current
16851 symbol scope. */
16852 sym = new_symbol (child_die, NULL, cu);
16853 if (sym != NULL)
16854 {
16855 struct attribute *member_loc;
16856
16857 common_block->contents[common_block->n_entries++] = sym;
16858
16859 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16860 cu);
16861 if (member_loc)
16862 {
16863 /* GDB has handled this for a long time, but it is
16864 not specified by DWARF. It seems to have been
16865 emitted by gfortran at least as recently as:
16866 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16867 complaint (_("Variable in common block has "
16868 "DW_AT_data_member_location "
16869 "- DIE at %s [in module %s]"),
16870 sect_offset_str (child_die->sect_off),
16871 objfile_name (objfile));
16872
16873 if (attr_form_is_section_offset (member_loc))
16874 dwarf2_complex_location_expr_complaint ();
16875 else if (attr_form_is_constant (member_loc)
16876 || attr_form_is_block (member_loc))
16877 {
16878 if (attr != nullptr)
16879 mark_common_block_symbol_computed (sym, die, attr,
16880 member_loc, cu);
16881 }
16882 else
16883 dwarf2_complex_location_expr_complaint ();
16884 }
16885 }
16886 }
16887
16888 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16889 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16890 }
16891 }
16892
16893 /* Create a type for a C++ namespace. */
16894
16895 static struct type *
16896 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16897 {
16898 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16899 const char *previous_prefix, *name;
16900 int is_anonymous;
16901 struct type *type;
16902
16903 /* For extensions, reuse the type of the original namespace. */
16904 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16905 {
16906 struct die_info *ext_die;
16907 struct dwarf2_cu *ext_cu = cu;
16908
16909 ext_die = dwarf2_extension (die, &ext_cu);
16910 type = read_type_die (ext_die, ext_cu);
16911
16912 /* EXT_CU may not be the same as CU.
16913 Ensure TYPE is recorded with CU in die_type_hash. */
16914 return set_die_type (die, type, cu);
16915 }
16916
16917 name = namespace_name (die, &is_anonymous, cu);
16918
16919 /* Now build the name of the current namespace. */
16920
16921 previous_prefix = determine_prefix (die, cu);
16922 if (previous_prefix[0] != '\0')
16923 name = typename_concat (&objfile->objfile_obstack,
16924 previous_prefix, name, 0, cu);
16925
16926 /* Create the type. */
16927 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16928
16929 return set_die_type (die, type, cu);
16930 }
16931
16932 /* Read a namespace scope. */
16933
16934 static void
16935 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16936 {
16937 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16938 int is_anonymous;
16939
16940 /* Add a symbol associated to this if we haven't seen the namespace
16941 before. Also, add a using directive if it's an anonymous
16942 namespace. */
16943
16944 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16945 {
16946 struct type *type;
16947
16948 type = read_type_die (die, cu);
16949 new_symbol (die, type, cu);
16950
16951 namespace_name (die, &is_anonymous, cu);
16952 if (is_anonymous)
16953 {
16954 const char *previous_prefix = determine_prefix (die, cu);
16955
16956 std::vector<const char *> excludes;
16957 add_using_directive (using_directives (cu),
16958 previous_prefix, TYPE_NAME (type), NULL,
16959 NULL, excludes, 0, &objfile->objfile_obstack);
16960 }
16961 }
16962
16963 if (die->child != NULL)
16964 {
16965 struct die_info *child_die = die->child;
16966
16967 while (child_die && child_die->tag)
16968 {
16969 process_die (child_die, cu);
16970 child_die = sibling_die (child_die);
16971 }
16972 }
16973 }
16974
16975 /* Read a Fortran module as type. This DIE can be only a declaration used for
16976 imported module. Still we need that type as local Fortran "use ... only"
16977 declaration imports depend on the created type in determine_prefix. */
16978
16979 static struct type *
16980 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16981 {
16982 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16983 const char *module_name;
16984 struct type *type;
16985
16986 module_name = dwarf2_name (die, cu);
16987 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16988
16989 return set_die_type (die, type, cu);
16990 }
16991
16992 /* Read a Fortran module. */
16993
16994 static void
16995 read_module (struct die_info *die, struct dwarf2_cu *cu)
16996 {
16997 struct die_info *child_die = die->child;
16998 struct type *type;
16999
17000 type = read_type_die (die, cu);
17001 new_symbol (die, type, cu);
17002
17003 while (child_die && child_die->tag)
17004 {
17005 process_die (child_die, cu);
17006 child_die = sibling_die (child_die);
17007 }
17008 }
17009
17010 /* Return the name of the namespace represented by DIE. Set
17011 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17012 namespace. */
17013
17014 static const char *
17015 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17016 {
17017 struct die_info *current_die;
17018 const char *name = NULL;
17019
17020 /* Loop through the extensions until we find a name. */
17021
17022 for (current_die = die;
17023 current_die != NULL;
17024 current_die = dwarf2_extension (die, &cu))
17025 {
17026 /* We don't use dwarf2_name here so that we can detect the absence
17027 of a name -> anonymous namespace. */
17028 name = dwarf2_string_attr (die, DW_AT_name, cu);
17029
17030 if (name != NULL)
17031 break;
17032 }
17033
17034 /* Is it an anonymous namespace? */
17035
17036 *is_anonymous = (name == NULL);
17037 if (*is_anonymous)
17038 name = CP_ANONYMOUS_NAMESPACE_STR;
17039
17040 return name;
17041 }
17042
17043 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17044 the user defined type vector. */
17045
17046 static struct type *
17047 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17048 {
17049 struct gdbarch *gdbarch
17050 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
17051 struct comp_unit_head *cu_header = &cu->header;
17052 struct type *type;
17053 struct attribute *attr_byte_size;
17054 struct attribute *attr_address_class;
17055 int byte_size, addr_class;
17056 struct type *target_type;
17057
17058 target_type = die_type (die, cu);
17059
17060 /* The die_type call above may have already set the type for this DIE. */
17061 type = get_die_type (die, cu);
17062 if (type)
17063 return type;
17064
17065 type = lookup_pointer_type (target_type);
17066
17067 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17068 if (attr_byte_size)
17069 byte_size = DW_UNSND (attr_byte_size);
17070 else
17071 byte_size = cu_header->addr_size;
17072
17073 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17074 if (attr_address_class)
17075 addr_class = DW_UNSND (attr_address_class);
17076 else
17077 addr_class = DW_ADDR_none;
17078
17079 ULONGEST alignment = get_alignment (cu, die);
17080
17081 /* If the pointer size, alignment, or address class is different
17082 than the default, create a type variant marked as such and set
17083 the length accordingly. */
17084 if (TYPE_LENGTH (type) != byte_size
17085 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17086 && alignment != TYPE_RAW_ALIGN (type))
17087 || addr_class != DW_ADDR_none)
17088 {
17089 if (gdbarch_address_class_type_flags_p (gdbarch))
17090 {
17091 int type_flags;
17092
17093 type_flags = gdbarch_address_class_type_flags
17094 (gdbarch, byte_size, addr_class);
17095 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17096 == 0);
17097 type = make_type_with_address_space (type, type_flags);
17098 }
17099 else if (TYPE_LENGTH (type) != byte_size)
17100 {
17101 complaint (_("invalid pointer size %d"), byte_size);
17102 }
17103 else if (TYPE_RAW_ALIGN (type) != alignment)
17104 {
17105 complaint (_("Invalid DW_AT_alignment"
17106 " - DIE at %s [in module %s]"),
17107 sect_offset_str (die->sect_off),
17108 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17109 }
17110 else
17111 {
17112 /* Should we also complain about unhandled address classes? */
17113 }
17114 }
17115
17116 TYPE_LENGTH (type) = byte_size;
17117 set_type_align (type, alignment);
17118 return set_die_type (die, type, cu);
17119 }
17120
17121 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17122 the user defined type vector. */
17123
17124 static struct type *
17125 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17126 {
17127 struct type *type;
17128 struct type *to_type;
17129 struct type *domain;
17130
17131 to_type = die_type (die, cu);
17132 domain = die_containing_type (die, cu);
17133
17134 /* The calls above may have already set the type for this DIE. */
17135 type = get_die_type (die, cu);
17136 if (type)
17137 return type;
17138
17139 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17140 type = lookup_methodptr_type (to_type);
17141 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17142 {
17143 struct type *new_type
17144 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17145
17146 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17147 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17148 TYPE_VARARGS (to_type));
17149 type = lookup_methodptr_type (new_type);
17150 }
17151 else
17152 type = lookup_memberptr_type (to_type, domain);
17153
17154 return set_die_type (die, type, cu);
17155 }
17156
17157 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17158 the user defined type vector. */
17159
17160 static struct type *
17161 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17162 enum type_code refcode)
17163 {
17164 struct comp_unit_head *cu_header = &cu->header;
17165 struct type *type, *target_type;
17166 struct attribute *attr;
17167
17168 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17169
17170 target_type = die_type (die, cu);
17171
17172 /* The die_type call above may have already set the type for this DIE. */
17173 type = get_die_type (die, cu);
17174 if (type)
17175 return type;
17176
17177 type = lookup_reference_type (target_type, refcode);
17178 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17179 if (attr != nullptr)
17180 {
17181 TYPE_LENGTH (type) = DW_UNSND (attr);
17182 }
17183 else
17184 {
17185 TYPE_LENGTH (type) = cu_header->addr_size;
17186 }
17187 maybe_set_alignment (cu, die, type);
17188 return set_die_type (die, type, cu);
17189 }
17190
17191 /* Add the given cv-qualifiers to the element type of the array. GCC
17192 outputs DWARF type qualifiers that apply to an array, not the
17193 element type. But GDB relies on the array element type to carry
17194 the cv-qualifiers. This mimics section 6.7.3 of the C99
17195 specification. */
17196
17197 static struct type *
17198 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17199 struct type *base_type, int cnst, int voltl)
17200 {
17201 struct type *el_type, *inner_array;
17202
17203 base_type = copy_type (base_type);
17204 inner_array = base_type;
17205
17206 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17207 {
17208 TYPE_TARGET_TYPE (inner_array) =
17209 copy_type (TYPE_TARGET_TYPE (inner_array));
17210 inner_array = TYPE_TARGET_TYPE (inner_array);
17211 }
17212
17213 el_type = TYPE_TARGET_TYPE (inner_array);
17214 cnst |= TYPE_CONST (el_type);
17215 voltl |= TYPE_VOLATILE (el_type);
17216 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17217
17218 return set_die_type (die, base_type, cu);
17219 }
17220
17221 static struct type *
17222 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17223 {
17224 struct type *base_type, *cv_type;
17225
17226 base_type = die_type (die, cu);
17227
17228 /* The die_type call above may have already set the type for this DIE. */
17229 cv_type = get_die_type (die, cu);
17230 if (cv_type)
17231 return cv_type;
17232
17233 /* In case the const qualifier is applied to an array type, the element type
17234 is so qualified, not the array type (section 6.7.3 of C99). */
17235 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17236 return add_array_cv_type (die, cu, base_type, 1, 0);
17237
17238 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17239 return set_die_type (die, cv_type, cu);
17240 }
17241
17242 static struct type *
17243 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17244 {
17245 struct type *base_type, *cv_type;
17246
17247 base_type = die_type (die, cu);
17248
17249 /* The die_type call above may have already set the type for this DIE. */
17250 cv_type = get_die_type (die, cu);
17251 if (cv_type)
17252 return cv_type;
17253
17254 /* In case the volatile qualifier is applied to an array type, the
17255 element type is so qualified, not the array type (section 6.7.3
17256 of C99). */
17257 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17258 return add_array_cv_type (die, cu, base_type, 0, 1);
17259
17260 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17261 return set_die_type (die, cv_type, cu);
17262 }
17263
17264 /* Handle DW_TAG_restrict_type. */
17265
17266 static struct type *
17267 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17268 {
17269 struct type *base_type, *cv_type;
17270
17271 base_type = die_type (die, cu);
17272
17273 /* The die_type call above may have already set the type for this DIE. */
17274 cv_type = get_die_type (die, cu);
17275 if (cv_type)
17276 return cv_type;
17277
17278 cv_type = make_restrict_type (base_type);
17279 return set_die_type (die, cv_type, cu);
17280 }
17281
17282 /* Handle DW_TAG_atomic_type. */
17283
17284 static struct type *
17285 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17286 {
17287 struct type *base_type, *cv_type;
17288
17289 base_type = die_type (die, cu);
17290
17291 /* The die_type call above may have already set the type for this DIE. */
17292 cv_type = get_die_type (die, cu);
17293 if (cv_type)
17294 return cv_type;
17295
17296 cv_type = make_atomic_type (base_type);
17297 return set_die_type (die, cv_type, cu);
17298 }
17299
17300 /* Extract all information from a DW_TAG_string_type DIE and add to
17301 the user defined type vector. It isn't really a user defined type,
17302 but it behaves like one, with other DIE's using an AT_user_def_type
17303 attribute to reference it. */
17304
17305 static struct type *
17306 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17307 {
17308 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17309 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17310 struct type *type, *range_type, *index_type, *char_type;
17311 struct attribute *attr;
17312 struct dynamic_prop prop;
17313 bool length_is_constant = true;
17314 LONGEST length;
17315
17316 /* There are a couple of places where bit sizes might be made use of
17317 when parsing a DW_TAG_string_type, however, no producer that we know
17318 of make use of these. Handling bit sizes that are a multiple of the
17319 byte size is easy enough, but what about other bit sizes? Lets deal
17320 with that problem when we have to. Warn about these attributes being
17321 unsupported, then parse the type and ignore them like we always
17322 have. */
17323 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17324 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17325 {
17326 static bool warning_printed = false;
17327 if (!warning_printed)
17328 {
17329 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17330 "currently supported on DW_TAG_string_type."));
17331 warning_printed = true;
17332 }
17333 }
17334
17335 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17336 if (attr != nullptr && !attr_form_is_constant (attr))
17337 {
17338 /* The string length describes the location at which the length of
17339 the string can be found. The size of the length field can be
17340 specified with one of the attributes below. */
17341 struct type *prop_type;
17342 struct attribute *len
17343 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17344 if (len == nullptr)
17345 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17346 if (len != nullptr && attr_form_is_constant (len))
17347 {
17348 /* Pass 0 as the default as we know this attribute is constant
17349 and the default value will not be returned. */
17350 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17351 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17352 }
17353 else
17354 {
17355 /* If the size is not specified then we assume it is the size of
17356 an address on this target. */
17357 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17358 }
17359
17360 /* Convert the attribute into a dynamic property. */
17361 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17362 length = 1;
17363 else
17364 length_is_constant = false;
17365 }
17366 else if (attr != nullptr)
17367 {
17368 /* This DW_AT_string_length just contains the length with no
17369 indirection. There's no need to create a dynamic property in this
17370 case. Pass 0 for the default value as we know it will not be
17371 returned in this case. */
17372 length = dwarf2_get_attr_constant_value (attr, 0);
17373 }
17374 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17375 {
17376 /* We don't currently support non-constant byte sizes for strings. */
17377 length = dwarf2_get_attr_constant_value (attr, 1);
17378 }
17379 else
17380 {
17381 /* Use 1 as a fallback length if we have nothing else. */
17382 length = 1;
17383 }
17384
17385 index_type = objfile_type (objfile)->builtin_int;
17386 if (length_is_constant)
17387 range_type = create_static_range_type (NULL, index_type, 1, length);
17388 else
17389 {
17390 struct dynamic_prop low_bound;
17391
17392 low_bound.kind = PROP_CONST;
17393 low_bound.data.const_val = 1;
17394 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17395 }
17396 char_type = language_string_char_type (cu->language_defn, gdbarch);
17397 type = create_string_type (NULL, char_type, range_type);
17398
17399 return set_die_type (die, type, cu);
17400 }
17401
17402 /* Assuming that DIE corresponds to a function, returns nonzero
17403 if the function is prototyped. */
17404
17405 static int
17406 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17407 {
17408 struct attribute *attr;
17409
17410 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17411 if (attr && (DW_UNSND (attr) != 0))
17412 return 1;
17413
17414 /* The DWARF standard implies that the DW_AT_prototyped attribute
17415 is only meaningful for C, but the concept also extends to other
17416 languages that allow unprototyped functions (Eg: Objective C).
17417 For all other languages, assume that functions are always
17418 prototyped. */
17419 if (cu->language != language_c
17420 && cu->language != language_objc
17421 && cu->language != language_opencl)
17422 return 1;
17423
17424 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17425 prototyped and unprototyped functions; default to prototyped,
17426 since that is more common in modern code (and RealView warns
17427 about unprototyped functions). */
17428 if (producer_is_realview (cu->producer))
17429 return 1;
17430
17431 return 0;
17432 }
17433
17434 /* Handle DIES due to C code like:
17435
17436 struct foo
17437 {
17438 int (*funcp)(int a, long l);
17439 int b;
17440 };
17441
17442 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17443
17444 static struct type *
17445 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17446 {
17447 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17448 struct type *type; /* Type that this function returns. */
17449 struct type *ftype; /* Function that returns above type. */
17450 struct attribute *attr;
17451
17452 type = die_type (die, cu);
17453
17454 /* The die_type call above may have already set the type for this DIE. */
17455 ftype = get_die_type (die, cu);
17456 if (ftype)
17457 return ftype;
17458
17459 ftype = lookup_function_type (type);
17460
17461 if (prototyped_function_p (die, cu))
17462 TYPE_PROTOTYPED (ftype) = 1;
17463
17464 /* Store the calling convention in the type if it's available in
17465 the subroutine die. Otherwise set the calling convention to
17466 the default value DW_CC_normal. */
17467 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17468 if (attr != nullptr
17469 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17470 TYPE_CALLING_CONVENTION (ftype)
17471 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17472 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17473 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17474 else
17475 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17476
17477 /* Record whether the function returns normally to its caller or not
17478 if the DWARF producer set that information. */
17479 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17480 if (attr && (DW_UNSND (attr) != 0))
17481 TYPE_NO_RETURN (ftype) = 1;
17482
17483 /* We need to add the subroutine type to the die immediately so
17484 we don't infinitely recurse when dealing with parameters
17485 declared as the same subroutine type. */
17486 set_die_type (die, ftype, cu);
17487
17488 if (die->child != NULL)
17489 {
17490 struct type *void_type = objfile_type (objfile)->builtin_void;
17491 struct die_info *child_die;
17492 int nparams, iparams;
17493
17494 /* Count the number of parameters.
17495 FIXME: GDB currently ignores vararg functions, but knows about
17496 vararg member functions. */
17497 nparams = 0;
17498 child_die = die->child;
17499 while (child_die && child_die->tag)
17500 {
17501 if (child_die->tag == DW_TAG_formal_parameter)
17502 nparams++;
17503 else if (child_die->tag == DW_TAG_unspecified_parameters)
17504 TYPE_VARARGS (ftype) = 1;
17505 child_die = sibling_die (child_die);
17506 }
17507
17508 /* Allocate storage for parameters and fill them in. */
17509 TYPE_NFIELDS (ftype) = nparams;
17510 TYPE_FIELDS (ftype) = (struct field *)
17511 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17512
17513 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17514 even if we error out during the parameters reading below. */
17515 for (iparams = 0; iparams < nparams; iparams++)
17516 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17517
17518 iparams = 0;
17519 child_die = die->child;
17520 while (child_die && child_die->tag)
17521 {
17522 if (child_die->tag == DW_TAG_formal_parameter)
17523 {
17524 struct type *arg_type;
17525
17526 /* DWARF version 2 has no clean way to discern C++
17527 static and non-static member functions. G++ helps
17528 GDB by marking the first parameter for non-static
17529 member functions (which is the this pointer) as
17530 artificial. We pass this information to
17531 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17532
17533 DWARF version 3 added DW_AT_object_pointer, which GCC
17534 4.5 does not yet generate. */
17535 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17536 if (attr != nullptr)
17537 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17538 else
17539 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17540 arg_type = die_type (child_die, cu);
17541
17542 /* RealView does not mark THIS as const, which the testsuite
17543 expects. GCC marks THIS as const in method definitions,
17544 but not in the class specifications (GCC PR 43053). */
17545 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17546 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17547 {
17548 int is_this = 0;
17549 struct dwarf2_cu *arg_cu = cu;
17550 const char *name = dwarf2_name (child_die, cu);
17551
17552 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17553 if (attr != nullptr)
17554 {
17555 /* If the compiler emits this, use it. */
17556 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17557 is_this = 1;
17558 }
17559 else if (name && strcmp (name, "this") == 0)
17560 /* Function definitions will have the argument names. */
17561 is_this = 1;
17562 else if (name == NULL && iparams == 0)
17563 /* Declarations may not have the names, so like
17564 elsewhere in GDB, assume an artificial first
17565 argument is "this". */
17566 is_this = 1;
17567
17568 if (is_this)
17569 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17570 arg_type, 0);
17571 }
17572
17573 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17574 iparams++;
17575 }
17576 child_die = sibling_die (child_die);
17577 }
17578 }
17579
17580 return ftype;
17581 }
17582
17583 static struct type *
17584 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17585 {
17586 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17587 const char *name = NULL;
17588 struct type *this_type, *target_type;
17589
17590 name = dwarf2_full_name (NULL, die, cu);
17591 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17592 TYPE_TARGET_STUB (this_type) = 1;
17593 set_die_type (die, this_type, cu);
17594 target_type = die_type (die, cu);
17595 if (target_type != this_type)
17596 TYPE_TARGET_TYPE (this_type) = target_type;
17597 else
17598 {
17599 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17600 spec and cause infinite loops in GDB. */
17601 complaint (_("Self-referential DW_TAG_typedef "
17602 "- DIE at %s [in module %s]"),
17603 sect_offset_str (die->sect_off), objfile_name (objfile));
17604 TYPE_TARGET_TYPE (this_type) = NULL;
17605 }
17606 return this_type;
17607 }
17608
17609 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17610 (which may be different from NAME) to the architecture back-end to allow
17611 it to guess the correct format if necessary. */
17612
17613 static struct type *
17614 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17615 const char *name_hint, enum bfd_endian byte_order)
17616 {
17617 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17618 const struct floatformat **format;
17619 struct type *type;
17620
17621 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17622 if (format)
17623 type = init_float_type (objfile, bits, name, format, byte_order);
17624 else
17625 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17626
17627 return type;
17628 }
17629
17630 /* Allocate an integer type of size BITS and name NAME. */
17631
17632 static struct type *
17633 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17634 int bits, int unsigned_p, const char *name)
17635 {
17636 struct type *type;
17637
17638 /* Versions of Intel's C Compiler generate an integer type called "void"
17639 instead of using DW_TAG_unspecified_type. This has been seen on
17640 at least versions 14, 17, and 18. */
17641 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17642 && strcmp (name, "void") == 0)
17643 type = objfile_type (objfile)->builtin_void;
17644 else
17645 type = init_integer_type (objfile, bits, unsigned_p, name);
17646
17647 return type;
17648 }
17649
17650 /* Initialise and return a floating point type of size BITS suitable for
17651 use as a component of a complex number. The NAME_HINT is passed through
17652 when initialising the floating point type and is the name of the complex
17653 type.
17654
17655 As DWARF doesn't currently provide an explicit name for the components
17656 of a complex number, but it can be helpful to have these components
17657 named, we try to select a suitable name based on the size of the
17658 component. */
17659 static struct type *
17660 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17661 struct objfile *objfile,
17662 int bits, const char *name_hint,
17663 enum bfd_endian byte_order)
17664 {
17665 gdbarch *gdbarch = get_objfile_arch (objfile);
17666 struct type *tt = nullptr;
17667
17668 /* Try to find a suitable floating point builtin type of size BITS.
17669 We're going to use the name of this type as the name for the complex
17670 target type that we are about to create. */
17671 switch (cu->language)
17672 {
17673 case language_fortran:
17674 switch (bits)
17675 {
17676 case 32:
17677 tt = builtin_f_type (gdbarch)->builtin_real;
17678 break;
17679 case 64:
17680 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17681 break;
17682 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17683 case 128:
17684 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17685 break;
17686 }
17687 break;
17688 default:
17689 switch (bits)
17690 {
17691 case 32:
17692 tt = builtin_type (gdbarch)->builtin_float;
17693 break;
17694 case 64:
17695 tt = builtin_type (gdbarch)->builtin_double;
17696 break;
17697 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17698 case 128:
17699 tt = builtin_type (gdbarch)->builtin_long_double;
17700 break;
17701 }
17702 break;
17703 }
17704
17705 /* If the type we found doesn't match the size we were looking for, then
17706 pretend we didn't find a type at all, the complex target type we
17707 create will then be nameless. */
17708 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17709 tt = nullptr;
17710
17711 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17712 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17713 }
17714
17715 /* Find a representation of a given base type and install
17716 it in the TYPE field of the die. */
17717
17718 static struct type *
17719 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17720 {
17721 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17722 struct type *type;
17723 struct attribute *attr;
17724 int encoding = 0, bits = 0;
17725 const char *name;
17726 gdbarch *arch;
17727
17728 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17729 if (attr != nullptr)
17730 encoding = DW_UNSND (attr);
17731 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17732 if (attr != nullptr)
17733 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17734 name = dwarf2_name (die, cu);
17735 if (!name)
17736 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17737
17738 arch = get_objfile_arch (objfile);
17739 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17740
17741 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17742 if (attr)
17743 {
17744 int endianity = DW_UNSND (attr);
17745
17746 switch (endianity)
17747 {
17748 case DW_END_big:
17749 byte_order = BFD_ENDIAN_BIG;
17750 break;
17751 case DW_END_little:
17752 byte_order = BFD_ENDIAN_LITTLE;
17753 break;
17754 default:
17755 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17756 break;
17757 }
17758 }
17759
17760 switch (encoding)
17761 {
17762 case DW_ATE_address:
17763 /* Turn DW_ATE_address into a void * pointer. */
17764 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17765 type = init_pointer_type (objfile, bits, name, type);
17766 break;
17767 case DW_ATE_boolean:
17768 type = init_boolean_type (objfile, bits, 1, name);
17769 break;
17770 case DW_ATE_complex_float:
17771 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17772 byte_order);
17773 type = init_complex_type (objfile, name, type);
17774 break;
17775 case DW_ATE_decimal_float:
17776 type = init_decfloat_type (objfile, bits, name);
17777 break;
17778 case DW_ATE_float:
17779 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17780 break;
17781 case DW_ATE_signed:
17782 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17783 break;
17784 case DW_ATE_unsigned:
17785 if (cu->language == language_fortran
17786 && name
17787 && startswith (name, "character("))
17788 type = init_character_type (objfile, bits, 1, name);
17789 else
17790 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17791 break;
17792 case DW_ATE_signed_char:
17793 if (cu->language == language_ada || cu->language == language_m2
17794 || cu->language == language_pascal
17795 || cu->language == language_fortran)
17796 type = init_character_type (objfile, bits, 0, name);
17797 else
17798 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17799 break;
17800 case DW_ATE_unsigned_char:
17801 if (cu->language == language_ada || cu->language == language_m2
17802 || cu->language == language_pascal
17803 || cu->language == language_fortran
17804 || cu->language == language_rust)
17805 type = init_character_type (objfile, bits, 1, name);
17806 else
17807 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17808 break;
17809 case DW_ATE_UTF:
17810 {
17811 if (bits == 16)
17812 type = builtin_type (arch)->builtin_char16;
17813 else if (bits == 32)
17814 type = builtin_type (arch)->builtin_char32;
17815 else
17816 {
17817 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17818 bits);
17819 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17820 }
17821 return set_die_type (die, type, cu);
17822 }
17823 break;
17824
17825 default:
17826 complaint (_("unsupported DW_AT_encoding: '%s'"),
17827 dwarf_type_encoding_name (encoding));
17828 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17829 break;
17830 }
17831
17832 if (name && strcmp (name, "char") == 0)
17833 TYPE_NOSIGN (type) = 1;
17834
17835 maybe_set_alignment (cu, die, type);
17836
17837 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17838
17839 return set_die_type (die, type, cu);
17840 }
17841
17842 /* Parse dwarf attribute if it's a block, reference or constant and put the
17843 resulting value of the attribute into struct bound_prop.
17844 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17845
17846 static int
17847 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17848 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17849 struct type *default_type)
17850 {
17851 struct dwarf2_property_baton *baton;
17852 struct obstack *obstack
17853 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17854
17855 gdb_assert (default_type != NULL);
17856
17857 if (attr == NULL || prop == NULL)
17858 return 0;
17859
17860 if (attr_form_is_block (attr))
17861 {
17862 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17863 baton->property_type = default_type;
17864 baton->locexpr.per_cu = cu->per_cu;
17865 baton->locexpr.size = DW_BLOCK (attr)->size;
17866 baton->locexpr.data = DW_BLOCK (attr)->data;
17867 switch (attr->name)
17868 {
17869 case DW_AT_string_length:
17870 baton->locexpr.is_reference = true;
17871 break;
17872 default:
17873 baton->locexpr.is_reference = false;
17874 break;
17875 }
17876 prop->data.baton = baton;
17877 prop->kind = PROP_LOCEXPR;
17878 gdb_assert (prop->data.baton != NULL);
17879 }
17880 else if (attr_form_is_ref (attr))
17881 {
17882 struct dwarf2_cu *target_cu = cu;
17883 struct die_info *target_die;
17884 struct attribute *target_attr;
17885
17886 target_die = follow_die_ref (die, attr, &target_cu);
17887 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17888 if (target_attr == NULL)
17889 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17890 target_cu);
17891 if (target_attr == NULL)
17892 return 0;
17893
17894 switch (target_attr->name)
17895 {
17896 case DW_AT_location:
17897 if (attr_form_is_section_offset (target_attr))
17898 {
17899 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17900 baton->property_type = die_type (target_die, target_cu);
17901 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17902 prop->data.baton = baton;
17903 prop->kind = PROP_LOCLIST;
17904 gdb_assert (prop->data.baton != NULL);
17905 }
17906 else if (attr_form_is_block (target_attr))
17907 {
17908 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17909 baton->property_type = die_type (target_die, target_cu);
17910 baton->locexpr.per_cu = cu->per_cu;
17911 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17912 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17913 baton->locexpr.is_reference = true;
17914 prop->data.baton = baton;
17915 prop->kind = PROP_LOCEXPR;
17916 gdb_assert (prop->data.baton != NULL);
17917 }
17918 else
17919 {
17920 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17921 "dynamic property");
17922 return 0;
17923 }
17924 break;
17925 case DW_AT_data_member_location:
17926 {
17927 LONGEST offset;
17928
17929 if (!handle_data_member_location (target_die, target_cu,
17930 &offset))
17931 return 0;
17932
17933 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17934 baton->property_type = read_type_die (target_die->parent,
17935 target_cu);
17936 baton->offset_info.offset = offset;
17937 baton->offset_info.type = die_type (target_die, target_cu);
17938 prop->data.baton = baton;
17939 prop->kind = PROP_ADDR_OFFSET;
17940 break;
17941 }
17942 }
17943 }
17944 else if (attr_form_is_constant (attr))
17945 {
17946 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17947 prop->kind = PROP_CONST;
17948 }
17949 else
17950 {
17951 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17952 dwarf2_name (die, cu));
17953 return 0;
17954 }
17955
17956 return 1;
17957 }
17958
17959 /* Find an integer type SIZE_IN_BYTES bytes in size and return it.
17960 UNSIGNED_P controls if the integer is unsigned or not. */
17961
17962 static struct type *
17963 dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
17964 int size_in_bytes, bool unsigned_p)
17965 {
17966 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
17967 struct type *int_type;
17968
17969 /* Helper macro to examine the various builtin types. */
17970 #define TRY_TYPE(F) \
17971 int_type = (unsigned_p \
17972 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17973 : objfile_type (objfile)->builtin_ ## F); \
17974 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17975 return int_type
17976
17977 TRY_TYPE (char);
17978 TRY_TYPE (short);
17979 TRY_TYPE (int);
17980 TRY_TYPE (long);
17981 TRY_TYPE (long_long);
17982
17983 #undef TRY_TYPE
17984
17985 gdb_assert_not_reached ("unable to find suitable integer type");
17986 }
17987
17988 /* Find an integer type the same size as the address size given in the
17989 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
17990 is unsigned or not. */
17991
17992 static struct type *
17993 dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
17994 bool unsigned_p)
17995 {
17996 int addr_size = dwarf2_per_cu_addr_size (per_cu);
17997 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
17998 }
17999
18000 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18001 present (which is valid) then compute the default type based on the
18002 compilation units address size. */
18003
18004 static struct type *
18005 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18006 {
18007 struct type *index_type = die_type (die, cu);
18008
18009 /* Dwarf-2 specifications explicitly allows to create subrange types
18010 without specifying a base type.
18011 In that case, the base type must be set to the type of
18012 the lower bound, upper bound or count, in that order, if any of these
18013 three attributes references an object that has a type.
18014 If no base type is found, the Dwarf-2 specifications say that
18015 a signed integer type of size equal to the size of an address should
18016 be used.
18017 For the following C code: `extern char gdb_int [];'
18018 GCC produces an empty range DIE.
18019 FIXME: muller/2010-05-28: Possible references to object for low bound,
18020 high bound or count are not yet handled by this code. */
18021 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
18022 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18023
18024 return index_type;
18025 }
18026
18027 /* Read the given DW_AT_subrange DIE. */
18028
18029 static struct type *
18030 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18031 {
18032 struct type *base_type, *orig_base_type;
18033 struct type *range_type;
18034 struct attribute *attr;
18035 struct dynamic_prop low, high;
18036 int low_default_is_valid;
18037 int high_bound_is_count = 0;
18038 const char *name;
18039 ULONGEST negative_mask;
18040
18041 orig_base_type = read_subrange_index_type (die, cu);
18042
18043 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18044 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18045 creating the range type, but we use the result of check_typedef
18046 when examining properties of the type. */
18047 base_type = check_typedef (orig_base_type);
18048
18049 /* The die_type call above may have already set the type for this DIE. */
18050 range_type = get_die_type (die, cu);
18051 if (range_type)
18052 return range_type;
18053
18054 low.kind = PROP_CONST;
18055 high.kind = PROP_CONST;
18056 high.data.const_val = 0;
18057
18058 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18059 omitting DW_AT_lower_bound. */
18060 switch (cu->language)
18061 {
18062 case language_c:
18063 case language_cplus:
18064 low.data.const_val = 0;
18065 low_default_is_valid = 1;
18066 break;
18067 case language_fortran:
18068 low.data.const_val = 1;
18069 low_default_is_valid = 1;
18070 break;
18071 case language_d:
18072 case language_objc:
18073 case language_rust:
18074 low.data.const_val = 0;
18075 low_default_is_valid = (cu->header.version >= 4);
18076 break;
18077 case language_ada:
18078 case language_m2:
18079 case language_pascal:
18080 low.data.const_val = 1;
18081 low_default_is_valid = (cu->header.version >= 4);
18082 break;
18083 default:
18084 low.data.const_val = 0;
18085 low_default_is_valid = 0;
18086 break;
18087 }
18088
18089 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18090 if (attr != nullptr)
18091 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18092 else if (!low_default_is_valid)
18093 complaint (_("Missing DW_AT_lower_bound "
18094 "- DIE at %s [in module %s]"),
18095 sect_offset_str (die->sect_off),
18096 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18097
18098 struct attribute *attr_ub, *attr_count;
18099 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18100 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18101 {
18102 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18103 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18104 {
18105 /* If bounds are constant do the final calculation here. */
18106 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18107 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18108 else
18109 high_bound_is_count = 1;
18110 }
18111 else
18112 {
18113 if (attr_ub != NULL)
18114 complaint (_("Unresolved DW_AT_upper_bound "
18115 "- DIE at %s [in module %s]"),
18116 sect_offset_str (die->sect_off),
18117 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18118 if (attr_count != NULL)
18119 complaint (_("Unresolved DW_AT_count "
18120 "- DIE at %s [in module %s]"),
18121 sect_offset_str (die->sect_off),
18122 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18123 }
18124 }
18125
18126 LONGEST bias = 0;
18127 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18128 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18129 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18130
18131 /* Normally, the DWARF producers are expected to use a signed
18132 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18133 But this is unfortunately not always the case, as witnessed
18134 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18135 is used instead. To work around that ambiguity, we treat
18136 the bounds as signed, and thus sign-extend their values, when
18137 the base type is signed. */
18138 negative_mask =
18139 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18140 if (low.kind == PROP_CONST
18141 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18142 low.data.const_val |= negative_mask;
18143 if (high.kind == PROP_CONST
18144 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18145 high.data.const_val |= negative_mask;
18146
18147 /* Check for bit and byte strides. */
18148 struct dynamic_prop byte_stride_prop;
18149 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18150 if (attr_byte_stride != nullptr)
18151 {
18152 struct type *prop_type
18153 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18154 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18155 prop_type);
18156 }
18157
18158 struct dynamic_prop bit_stride_prop;
18159 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18160 if (attr_bit_stride != nullptr)
18161 {
18162 /* It only makes sense to have either a bit or byte stride. */
18163 if (attr_byte_stride != nullptr)
18164 {
18165 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18166 "- DIE at %s [in module %s]"),
18167 sect_offset_str (die->sect_off),
18168 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18169 attr_bit_stride = nullptr;
18170 }
18171 else
18172 {
18173 struct type *prop_type
18174 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18175 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18176 prop_type);
18177 }
18178 }
18179
18180 if (attr_byte_stride != nullptr
18181 || attr_bit_stride != nullptr)
18182 {
18183 bool byte_stride_p = (attr_byte_stride != nullptr);
18184 struct dynamic_prop *stride
18185 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18186
18187 range_type
18188 = create_range_type_with_stride (NULL, orig_base_type, &low,
18189 &high, bias, stride, byte_stride_p);
18190 }
18191 else
18192 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18193
18194 if (high_bound_is_count)
18195 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18196
18197 /* Ada expects an empty array on no boundary attributes. */
18198 if (attr == NULL && cu->language != language_ada)
18199 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
18200
18201 name = dwarf2_name (die, cu);
18202 if (name)
18203 TYPE_NAME (range_type) = name;
18204
18205 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18206 if (attr != nullptr)
18207 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18208
18209 maybe_set_alignment (cu, die, range_type);
18210
18211 set_die_type (die, range_type, cu);
18212
18213 /* set_die_type should be already done. */
18214 set_descriptive_type (range_type, die, cu);
18215
18216 return range_type;
18217 }
18218
18219 static struct type *
18220 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18221 {
18222 struct type *type;
18223
18224 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18225 NULL);
18226 TYPE_NAME (type) = dwarf2_name (die, cu);
18227
18228 /* In Ada, an unspecified type is typically used when the description
18229 of the type is deferred to a different unit. When encountering
18230 such a type, we treat it as a stub, and try to resolve it later on,
18231 when needed. */
18232 if (cu->language == language_ada)
18233 TYPE_STUB (type) = 1;
18234
18235 return set_die_type (die, type, cu);
18236 }
18237
18238 /* Read a single die and all its descendents. Set the die's sibling
18239 field to NULL; set other fields in the die correctly, and set all
18240 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18241 location of the info_ptr after reading all of those dies. PARENT
18242 is the parent of the die in question. */
18243
18244 static struct die_info *
18245 read_die_and_children (const struct die_reader_specs *reader,
18246 const gdb_byte *info_ptr,
18247 const gdb_byte **new_info_ptr,
18248 struct die_info *parent)
18249 {
18250 struct die_info *die;
18251 const gdb_byte *cur_ptr;
18252 int has_children;
18253
18254 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18255 if (die == NULL)
18256 {
18257 *new_info_ptr = cur_ptr;
18258 return NULL;
18259 }
18260 store_in_ref_table (die, reader->cu);
18261
18262 if (has_children)
18263 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18264 else
18265 {
18266 die->child = NULL;
18267 *new_info_ptr = cur_ptr;
18268 }
18269
18270 die->sibling = NULL;
18271 die->parent = parent;
18272 return die;
18273 }
18274
18275 /* Read a die, all of its descendents, and all of its siblings; set
18276 all of the fields of all of the dies correctly. Arguments are as
18277 in read_die_and_children. */
18278
18279 static struct die_info *
18280 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18281 const gdb_byte *info_ptr,
18282 const gdb_byte **new_info_ptr,
18283 struct die_info *parent)
18284 {
18285 struct die_info *first_die, *last_sibling;
18286 const gdb_byte *cur_ptr;
18287
18288 cur_ptr = info_ptr;
18289 first_die = last_sibling = NULL;
18290
18291 while (1)
18292 {
18293 struct die_info *die
18294 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18295
18296 if (die == NULL)
18297 {
18298 *new_info_ptr = cur_ptr;
18299 return first_die;
18300 }
18301
18302 if (!first_die)
18303 first_die = die;
18304 else
18305 last_sibling->sibling = die;
18306
18307 last_sibling = die;
18308 }
18309 }
18310
18311 /* Read a die, all of its descendents, and all of its siblings; set
18312 all of the fields of all of the dies correctly. Arguments are as
18313 in read_die_and_children.
18314 This the main entry point for reading a DIE and all its children. */
18315
18316 static struct die_info *
18317 read_die_and_siblings (const struct die_reader_specs *reader,
18318 const gdb_byte *info_ptr,
18319 const gdb_byte **new_info_ptr,
18320 struct die_info *parent)
18321 {
18322 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18323 new_info_ptr, parent);
18324
18325 if (dwarf_die_debug)
18326 {
18327 fprintf_unfiltered (gdb_stdlog,
18328 "Read die from %s@0x%x of %s:\n",
18329 get_section_name (reader->die_section),
18330 (unsigned) (info_ptr - reader->die_section->buffer),
18331 bfd_get_filename (reader->abfd));
18332 dump_die (die, dwarf_die_debug);
18333 }
18334
18335 return die;
18336 }
18337
18338 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18339 attributes.
18340 The caller is responsible for filling in the extra attributes
18341 and updating (*DIEP)->num_attrs.
18342 Set DIEP to point to a newly allocated die with its information,
18343 except for its child, sibling, and parent fields.
18344 Set HAS_CHILDREN to tell whether the die has children or not. */
18345
18346 static const gdb_byte *
18347 read_full_die_1 (const struct die_reader_specs *reader,
18348 struct die_info **diep, const gdb_byte *info_ptr,
18349 int *has_children, int num_extra_attrs)
18350 {
18351 unsigned int abbrev_number, bytes_read, i;
18352 struct abbrev_info *abbrev;
18353 struct die_info *die;
18354 struct dwarf2_cu *cu = reader->cu;
18355 bfd *abfd = reader->abfd;
18356
18357 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18358 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18359 info_ptr += bytes_read;
18360 if (!abbrev_number)
18361 {
18362 *diep = NULL;
18363 *has_children = 0;
18364 return info_ptr;
18365 }
18366
18367 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18368 if (!abbrev)
18369 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18370 abbrev_number,
18371 bfd_get_filename (abfd));
18372
18373 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18374 die->sect_off = sect_off;
18375 die->tag = abbrev->tag;
18376 die->abbrev = abbrev_number;
18377
18378 /* Make the result usable.
18379 The caller needs to update num_attrs after adding the extra
18380 attributes. */
18381 die->num_attrs = abbrev->num_attrs;
18382
18383 std::vector<int> indexes_that_need_reprocess;
18384 for (i = 0; i < abbrev->num_attrs; ++i)
18385 {
18386 bool need_reprocess;
18387 info_ptr =
18388 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18389 info_ptr, &need_reprocess);
18390 if (need_reprocess)
18391 indexes_that_need_reprocess.push_back (i);
18392 }
18393
18394 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
18395 if (attr != nullptr)
18396 cu->str_offsets_base = DW_UNSND (attr);
18397
18398 auto maybe_addr_base = lookup_addr_base(die);
18399 if (maybe_addr_base.has_value ())
18400 cu->addr_base = *maybe_addr_base;
18401 for (int index : indexes_that_need_reprocess)
18402 read_attribute_reprocess (reader, &die->attrs[index]);
18403 *diep = die;
18404 *has_children = abbrev->has_children;
18405 return info_ptr;
18406 }
18407
18408 /* Read a die and all its attributes.
18409 Set DIEP to point to a newly allocated die with its information,
18410 except for its child, sibling, and parent fields.
18411 Set HAS_CHILDREN to tell whether the die has children or not. */
18412
18413 static const gdb_byte *
18414 read_full_die (const struct die_reader_specs *reader,
18415 struct die_info **diep, const gdb_byte *info_ptr,
18416 int *has_children)
18417 {
18418 const gdb_byte *result;
18419
18420 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18421
18422 if (dwarf_die_debug)
18423 {
18424 fprintf_unfiltered (gdb_stdlog,
18425 "Read die from %s@0x%x of %s:\n",
18426 get_section_name (reader->die_section),
18427 (unsigned) (info_ptr - reader->die_section->buffer),
18428 bfd_get_filename (reader->abfd));
18429 dump_die (*diep, dwarf_die_debug);
18430 }
18431
18432 return result;
18433 }
18434 \f
18435 /* Abbreviation tables.
18436
18437 In DWARF version 2, the description of the debugging information is
18438 stored in a separate .debug_abbrev section. Before we read any
18439 dies from a section we read in all abbreviations and install them
18440 in a hash table. */
18441
18442 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18443
18444 struct abbrev_info *
18445 abbrev_table::alloc_abbrev ()
18446 {
18447 struct abbrev_info *abbrev;
18448
18449 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18450 memset (abbrev, 0, sizeof (struct abbrev_info));
18451
18452 return abbrev;
18453 }
18454
18455 /* Add an abbreviation to the table. */
18456
18457 void
18458 abbrev_table::add_abbrev (unsigned int abbrev_number,
18459 struct abbrev_info *abbrev)
18460 {
18461 unsigned int hash_number;
18462
18463 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18464 abbrev->next = m_abbrevs[hash_number];
18465 m_abbrevs[hash_number] = abbrev;
18466 }
18467
18468 /* Look up an abbrev in the table.
18469 Returns NULL if the abbrev is not found. */
18470
18471 struct abbrev_info *
18472 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18473 {
18474 unsigned int hash_number;
18475 struct abbrev_info *abbrev;
18476
18477 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18478 abbrev = m_abbrevs[hash_number];
18479
18480 while (abbrev)
18481 {
18482 if (abbrev->number == abbrev_number)
18483 return abbrev;
18484 abbrev = abbrev->next;
18485 }
18486 return NULL;
18487 }
18488
18489 /* Read in an abbrev table. */
18490
18491 static abbrev_table_up
18492 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18493 struct dwarf2_section_info *section,
18494 sect_offset sect_off)
18495 {
18496 struct objfile *objfile = dwarf2_per_objfile->objfile;
18497 bfd *abfd = get_section_bfd_owner (section);
18498 const gdb_byte *abbrev_ptr;
18499 struct abbrev_info *cur_abbrev;
18500 unsigned int abbrev_number, bytes_read, abbrev_name;
18501 unsigned int abbrev_form;
18502 std::vector<struct attr_abbrev> cur_attrs;
18503
18504 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18505
18506 dwarf2_read_section (objfile, section);
18507 abbrev_ptr = section->buffer + to_underlying (sect_off);
18508 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18509 abbrev_ptr += bytes_read;
18510
18511 /* Loop until we reach an abbrev number of 0. */
18512 while (abbrev_number)
18513 {
18514 cur_attrs.clear ();
18515 cur_abbrev = abbrev_table->alloc_abbrev ();
18516
18517 /* read in abbrev header */
18518 cur_abbrev->number = abbrev_number;
18519 cur_abbrev->tag
18520 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18521 abbrev_ptr += bytes_read;
18522 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18523 abbrev_ptr += 1;
18524
18525 /* now read in declarations */
18526 for (;;)
18527 {
18528 LONGEST implicit_const;
18529
18530 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18531 abbrev_ptr += bytes_read;
18532 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18533 abbrev_ptr += bytes_read;
18534 if (abbrev_form == DW_FORM_implicit_const)
18535 {
18536 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18537 &bytes_read);
18538 abbrev_ptr += bytes_read;
18539 }
18540 else
18541 {
18542 /* Initialize it due to a false compiler warning. */
18543 implicit_const = -1;
18544 }
18545
18546 if (abbrev_name == 0)
18547 break;
18548
18549 cur_attrs.emplace_back ();
18550 struct attr_abbrev &cur_attr = cur_attrs.back ();
18551 cur_attr.name = (enum dwarf_attribute) abbrev_name;
18552 cur_attr.form = (enum dwarf_form) abbrev_form;
18553 cur_attr.implicit_const = implicit_const;
18554 ++cur_abbrev->num_attrs;
18555 }
18556
18557 cur_abbrev->attrs =
18558 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18559 cur_abbrev->num_attrs);
18560 memcpy (cur_abbrev->attrs, cur_attrs.data (),
18561 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18562
18563 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18564
18565 /* Get next abbreviation.
18566 Under Irix6 the abbreviations for a compilation unit are not
18567 always properly terminated with an abbrev number of 0.
18568 Exit loop if we encounter an abbreviation which we have
18569 already read (which means we are about to read the abbreviations
18570 for the next compile unit) or if the end of the abbreviation
18571 table is reached. */
18572 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18573 break;
18574 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18575 abbrev_ptr += bytes_read;
18576 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18577 break;
18578 }
18579
18580 return abbrev_table;
18581 }
18582
18583 /* Returns nonzero if TAG represents a type that we might generate a partial
18584 symbol for. */
18585
18586 static int
18587 is_type_tag_for_partial (int tag)
18588 {
18589 switch (tag)
18590 {
18591 #if 0
18592 /* Some types that would be reasonable to generate partial symbols for,
18593 that we don't at present. */
18594 case DW_TAG_array_type:
18595 case DW_TAG_file_type:
18596 case DW_TAG_ptr_to_member_type:
18597 case DW_TAG_set_type:
18598 case DW_TAG_string_type:
18599 case DW_TAG_subroutine_type:
18600 #endif
18601 case DW_TAG_base_type:
18602 case DW_TAG_class_type:
18603 case DW_TAG_interface_type:
18604 case DW_TAG_enumeration_type:
18605 case DW_TAG_structure_type:
18606 case DW_TAG_subrange_type:
18607 case DW_TAG_typedef:
18608 case DW_TAG_union_type:
18609 return 1;
18610 default:
18611 return 0;
18612 }
18613 }
18614
18615 /* Load all DIEs that are interesting for partial symbols into memory. */
18616
18617 static struct partial_die_info *
18618 load_partial_dies (const struct die_reader_specs *reader,
18619 const gdb_byte *info_ptr, int building_psymtab)
18620 {
18621 struct dwarf2_cu *cu = reader->cu;
18622 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18623 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18624 unsigned int bytes_read;
18625 unsigned int load_all = 0;
18626 int nesting_level = 1;
18627
18628 parent_die = NULL;
18629 last_die = NULL;
18630
18631 gdb_assert (cu->per_cu != NULL);
18632 if (cu->per_cu->load_all_dies)
18633 load_all = 1;
18634
18635 cu->partial_dies
18636 = htab_create_alloc_ex (cu->header.length / 12,
18637 partial_die_hash,
18638 partial_die_eq,
18639 NULL,
18640 &cu->comp_unit_obstack,
18641 hashtab_obstack_allocate,
18642 dummy_obstack_deallocate);
18643
18644 while (1)
18645 {
18646 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18647
18648 /* A NULL abbrev means the end of a series of children. */
18649 if (abbrev == NULL)
18650 {
18651 if (--nesting_level == 0)
18652 return first_die;
18653
18654 info_ptr += bytes_read;
18655 last_die = parent_die;
18656 parent_die = parent_die->die_parent;
18657 continue;
18658 }
18659
18660 /* Check for template arguments. We never save these; if
18661 they're seen, we just mark the parent, and go on our way. */
18662 if (parent_die != NULL
18663 && cu->language == language_cplus
18664 && (abbrev->tag == DW_TAG_template_type_param
18665 || abbrev->tag == DW_TAG_template_value_param))
18666 {
18667 parent_die->has_template_arguments = 1;
18668
18669 if (!load_all)
18670 {
18671 /* We don't need a partial DIE for the template argument. */
18672 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18673 continue;
18674 }
18675 }
18676
18677 /* We only recurse into c++ subprograms looking for template arguments.
18678 Skip their other children. */
18679 if (!load_all
18680 && cu->language == language_cplus
18681 && parent_die != NULL
18682 && parent_die->tag == DW_TAG_subprogram)
18683 {
18684 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18685 continue;
18686 }
18687
18688 /* Check whether this DIE is interesting enough to save. Normally
18689 we would not be interested in members here, but there may be
18690 later variables referencing them via DW_AT_specification (for
18691 static members). */
18692 if (!load_all
18693 && !is_type_tag_for_partial (abbrev->tag)
18694 && abbrev->tag != DW_TAG_constant
18695 && abbrev->tag != DW_TAG_enumerator
18696 && abbrev->tag != DW_TAG_subprogram
18697 && abbrev->tag != DW_TAG_inlined_subroutine
18698 && abbrev->tag != DW_TAG_lexical_block
18699 && abbrev->tag != DW_TAG_variable
18700 && abbrev->tag != DW_TAG_namespace
18701 && abbrev->tag != DW_TAG_module
18702 && abbrev->tag != DW_TAG_member
18703 && abbrev->tag != DW_TAG_imported_unit
18704 && abbrev->tag != DW_TAG_imported_declaration)
18705 {
18706 /* Otherwise we skip to the next sibling, if any. */
18707 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18708 continue;
18709 }
18710
18711 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18712 abbrev);
18713
18714 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18715
18716 /* This two-pass algorithm for processing partial symbols has a
18717 high cost in cache pressure. Thus, handle some simple cases
18718 here which cover the majority of C partial symbols. DIEs
18719 which neither have specification tags in them, nor could have
18720 specification tags elsewhere pointing at them, can simply be
18721 processed and discarded.
18722
18723 This segment is also optional; scan_partial_symbols and
18724 add_partial_symbol will handle these DIEs if we chain
18725 them in normally. When compilers which do not emit large
18726 quantities of duplicate debug information are more common,
18727 this code can probably be removed. */
18728
18729 /* Any complete simple types at the top level (pretty much all
18730 of them, for a language without namespaces), can be processed
18731 directly. */
18732 if (parent_die == NULL
18733 && pdi.has_specification == 0
18734 && pdi.is_declaration == 0
18735 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18736 || pdi.tag == DW_TAG_base_type
18737 || pdi.tag == DW_TAG_subrange_type))
18738 {
18739 if (building_psymtab && pdi.name != NULL)
18740 add_psymbol_to_list (pdi.name, false,
18741 VAR_DOMAIN, LOC_TYPEDEF, -1,
18742 psymbol_placement::STATIC,
18743 0, cu->language, objfile);
18744 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18745 continue;
18746 }
18747
18748 /* The exception for DW_TAG_typedef with has_children above is
18749 a workaround of GCC PR debug/47510. In the case of this complaint
18750 type_name_or_error will error on such types later.
18751
18752 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18753 it could not find the child DIEs referenced later, this is checked
18754 above. In correct DWARF DW_TAG_typedef should have no children. */
18755
18756 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18757 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18758 "- DIE at %s [in module %s]"),
18759 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18760
18761 /* If we're at the second level, and we're an enumerator, and
18762 our parent has no specification (meaning possibly lives in a
18763 namespace elsewhere), then we can add the partial symbol now
18764 instead of queueing it. */
18765 if (pdi.tag == DW_TAG_enumerator
18766 && parent_die != NULL
18767 && parent_die->die_parent == NULL
18768 && parent_die->tag == DW_TAG_enumeration_type
18769 && parent_die->has_specification == 0)
18770 {
18771 if (pdi.name == NULL)
18772 complaint (_("malformed enumerator DIE ignored"));
18773 else if (building_psymtab)
18774 add_psymbol_to_list (pdi.name, false,
18775 VAR_DOMAIN, LOC_CONST, -1,
18776 cu->language == language_cplus
18777 ? psymbol_placement::GLOBAL
18778 : psymbol_placement::STATIC,
18779 0, cu->language, objfile);
18780
18781 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18782 continue;
18783 }
18784
18785 struct partial_die_info *part_die
18786 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18787
18788 /* We'll save this DIE so link it in. */
18789 part_die->die_parent = parent_die;
18790 part_die->die_sibling = NULL;
18791 part_die->die_child = NULL;
18792
18793 if (last_die && last_die == parent_die)
18794 last_die->die_child = part_die;
18795 else if (last_die)
18796 last_die->die_sibling = part_die;
18797
18798 last_die = part_die;
18799
18800 if (first_die == NULL)
18801 first_die = part_die;
18802
18803 /* Maybe add the DIE to the hash table. Not all DIEs that we
18804 find interesting need to be in the hash table, because we
18805 also have the parent/sibling/child chains; only those that we
18806 might refer to by offset later during partial symbol reading.
18807
18808 For now this means things that might have be the target of a
18809 DW_AT_specification, DW_AT_abstract_origin, or
18810 DW_AT_extension. DW_AT_extension will refer only to
18811 namespaces; DW_AT_abstract_origin refers to functions (and
18812 many things under the function DIE, but we do not recurse
18813 into function DIEs during partial symbol reading) and
18814 possibly variables as well; DW_AT_specification refers to
18815 declarations. Declarations ought to have the DW_AT_declaration
18816 flag. It happens that GCC forgets to put it in sometimes, but
18817 only for functions, not for types.
18818
18819 Adding more things than necessary to the hash table is harmless
18820 except for the performance cost. Adding too few will result in
18821 wasted time in find_partial_die, when we reread the compilation
18822 unit with load_all_dies set. */
18823
18824 if (load_all
18825 || abbrev->tag == DW_TAG_constant
18826 || abbrev->tag == DW_TAG_subprogram
18827 || abbrev->tag == DW_TAG_variable
18828 || abbrev->tag == DW_TAG_namespace
18829 || part_die->is_declaration)
18830 {
18831 void **slot;
18832
18833 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18834 to_underlying (part_die->sect_off),
18835 INSERT);
18836 *slot = part_die;
18837 }
18838
18839 /* For some DIEs we want to follow their children (if any). For C
18840 we have no reason to follow the children of structures; for other
18841 languages we have to, so that we can get at method physnames
18842 to infer fully qualified class names, for DW_AT_specification,
18843 and for C++ template arguments. For C++, we also look one level
18844 inside functions to find template arguments (if the name of the
18845 function does not already contain the template arguments).
18846
18847 For Ada and Fortran, we need to scan the children of subprograms
18848 and lexical blocks as well because these languages allow the
18849 definition of nested entities that could be interesting for the
18850 debugger, such as nested subprograms for instance. */
18851 if (last_die->has_children
18852 && (load_all
18853 || last_die->tag == DW_TAG_namespace
18854 || last_die->tag == DW_TAG_module
18855 || last_die->tag == DW_TAG_enumeration_type
18856 || (cu->language == language_cplus
18857 && last_die->tag == DW_TAG_subprogram
18858 && (last_die->name == NULL
18859 || strchr (last_die->name, '<') == NULL))
18860 || (cu->language != language_c
18861 && (last_die->tag == DW_TAG_class_type
18862 || last_die->tag == DW_TAG_interface_type
18863 || last_die->tag == DW_TAG_structure_type
18864 || last_die->tag == DW_TAG_union_type))
18865 || ((cu->language == language_ada
18866 || cu->language == language_fortran)
18867 && (last_die->tag == DW_TAG_subprogram
18868 || last_die->tag == DW_TAG_lexical_block))))
18869 {
18870 nesting_level++;
18871 parent_die = last_die;
18872 continue;
18873 }
18874
18875 /* Otherwise we skip to the next sibling, if any. */
18876 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18877
18878 /* Back to the top, do it again. */
18879 }
18880 }
18881
18882 partial_die_info::partial_die_info (sect_offset sect_off_,
18883 struct abbrev_info *abbrev)
18884 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18885 {
18886 }
18887
18888 /* Read a minimal amount of information into the minimal die structure.
18889 INFO_PTR should point just after the initial uleb128 of a DIE. */
18890
18891 const gdb_byte *
18892 partial_die_info::read (const struct die_reader_specs *reader,
18893 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18894 {
18895 struct dwarf2_cu *cu = reader->cu;
18896 struct dwarf2_per_objfile *dwarf2_per_objfile
18897 = cu->per_cu->dwarf2_per_objfile;
18898 unsigned int i;
18899 int has_low_pc_attr = 0;
18900 int has_high_pc_attr = 0;
18901 int high_pc_relative = 0;
18902
18903 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
18904 for (i = 0; i < abbrev.num_attrs; ++i)
18905 {
18906 bool need_reprocess;
18907 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
18908 info_ptr, &need_reprocess);
18909 /* String and address offsets that need to do the reprocessing have
18910 already been read at this point, so there is no need to wait until
18911 the loop terminates to do the reprocessing. */
18912 if (need_reprocess)
18913 read_attribute_reprocess (reader, &attr_vec[i]);
18914 attribute &attr = attr_vec[i];
18915 /* Store the data if it is of an attribute we want to keep in a
18916 partial symbol table. */
18917 switch (attr.name)
18918 {
18919 case DW_AT_name:
18920 switch (tag)
18921 {
18922 case DW_TAG_compile_unit:
18923 case DW_TAG_partial_unit:
18924 case DW_TAG_type_unit:
18925 /* Compilation units have a DW_AT_name that is a filename, not
18926 a source language identifier. */
18927 case DW_TAG_enumeration_type:
18928 case DW_TAG_enumerator:
18929 /* These tags always have simple identifiers already; no need
18930 to canonicalize them. */
18931 name = DW_STRING (&attr);
18932 break;
18933 default:
18934 {
18935 struct objfile *objfile = dwarf2_per_objfile->objfile;
18936
18937 name
18938 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18939 &objfile->per_bfd->storage_obstack);
18940 }
18941 break;
18942 }
18943 break;
18944 case DW_AT_linkage_name:
18945 case DW_AT_MIPS_linkage_name:
18946 /* Note that both forms of linkage name might appear. We
18947 assume they will be the same, and we only store the last
18948 one we see. */
18949 linkage_name = DW_STRING (&attr);
18950 break;
18951 case DW_AT_low_pc:
18952 has_low_pc_attr = 1;
18953 lowpc = attr_value_as_address (&attr);
18954 break;
18955 case DW_AT_high_pc:
18956 has_high_pc_attr = 1;
18957 highpc = attr_value_as_address (&attr);
18958 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18959 high_pc_relative = 1;
18960 break;
18961 case DW_AT_location:
18962 /* Support the .debug_loc offsets. */
18963 if (attr_form_is_block (&attr))
18964 {
18965 d.locdesc = DW_BLOCK (&attr);
18966 }
18967 else if (attr_form_is_section_offset (&attr))
18968 {
18969 dwarf2_complex_location_expr_complaint ();
18970 }
18971 else
18972 {
18973 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18974 "partial symbol information");
18975 }
18976 break;
18977 case DW_AT_external:
18978 is_external = DW_UNSND (&attr);
18979 break;
18980 case DW_AT_declaration:
18981 is_declaration = DW_UNSND (&attr);
18982 break;
18983 case DW_AT_type:
18984 has_type = 1;
18985 break;
18986 case DW_AT_abstract_origin:
18987 case DW_AT_specification:
18988 case DW_AT_extension:
18989 has_specification = 1;
18990 spec_offset = dwarf2_get_ref_die_offset (&attr);
18991 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18992 || cu->per_cu->is_dwz);
18993 break;
18994 case DW_AT_sibling:
18995 /* Ignore absolute siblings, they might point outside of
18996 the current compile unit. */
18997 if (attr.form == DW_FORM_ref_addr)
18998 complaint (_("ignoring absolute DW_AT_sibling"));
18999 else
19000 {
19001 const gdb_byte *buffer = reader->buffer;
19002 sect_offset off = dwarf2_get_ref_die_offset (&attr);
19003 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19004
19005 if (sibling_ptr < info_ptr)
19006 complaint (_("DW_AT_sibling points backwards"));
19007 else if (sibling_ptr > reader->buffer_end)
19008 dwarf2_section_buffer_overflow_complaint (reader->die_section);
19009 else
19010 sibling = sibling_ptr;
19011 }
19012 break;
19013 case DW_AT_byte_size:
19014 has_byte_size = 1;
19015 break;
19016 case DW_AT_const_value:
19017 has_const_value = 1;
19018 break;
19019 case DW_AT_calling_convention:
19020 /* DWARF doesn't provide a way to identify a program's source-level
19021 entry point. DW_AT_calling_convention attributes are only meant
19022 to describe functions' calling conventions.
19023
19024 However, because it's a necessary piece of information in
19025 Fortran, and before DWARF 4 DW_CC_program was the only
19026 piece of debugging information whose definition refers to
19027 a 'main program' at all, several compilers marked Fortran
19028 main programs with DW_CC_program --- even when those
19029 functions use the standard calling conventions.
19030
19031 Although DWARF now specifies a way to provide this
19032 information, we support this practice for backward
19033 compatibility. */
19034 if (DW_UNSND (&attr) == DW_CC_program
19035 && cu->language == language_fortran)
19036 main_subprogram = 1;
19037 break;
19038 case DW_AT_inline:
19039 if (DW_UNSND (&attr) == DW_INL_inlined
19040 || DW_UNSND (&attr) == DW_INL_declared_inlined)
19041 may_be_inlined = 1;
19042 break;
19043
19044 case DW_AT_import:
19045 if (tag == DW_TAG_imported_unit)
19046 {
19047 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19048 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19049 || cu->per_cu->is_dwz);
19050 }
19051 break;
19052
19053 case DW_AT_main_subprogram:
19054 main_subprogram = DW_UNSND (&attr);
19055 break;
19056
19057 case DW_AT_ranges:
19058 {
19059 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19060 but that requires a full DIE, so instead we just
19061 reimplement it. */
19062 int need_ranges_base = tag != DW_TAG_compile_unit;
19063 unsigned int ranges_offset = (DW_UNSND (&attr)
19064 + (need_ranges_base
19065 ? cu->ranges_base
19066 : 0));
19067
19068 /* Value of the DW_AT_ranges attribute is the offset in the
19069 .debug_ranges section. */
19070 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19071 nullptr))
19072 has_pc_info = 1;
19073 }
19074 break;
19075
19076 default:
19077 break;
19078 }
19079 }
19080
19081 /* For Ada, if both the name and the linkage name appear, we prefer
19082 the latter. This lets "catch exception" work better, regardless
19083 of the order in which the name and linkage name were emitted.
19084 Really, though, this is just a workaround for the fact that gdb
19085 doesn't store both the name and the linkage name. */
19086 if (cu->language == language_ada && linkage_name != nullptr)
19087 name = linkage_name;
19088
19089 if (high_pc_relative)
19090 highpc += lowpc;
19091
19092 if (has_low_pc_attr && has_high_pc_attr)
19093 {
19094 /* When using the GNU linker, .gnu.linkonce. sections are used to
19095 eliminate duplicate copies of functions and vtables and such.
19096 The linker will arbitrarily choose one and discard the others.
19097 The AT_*_pc values for such functions refer to local labels in
19098 these sections. If the section from that file was discarded, the
19099 labels are not in the output, so the relocs get a value of 0.
19100 If this is a discarded function, mark the pc bounds as invalid,
19101 so that GDB will ignore it. */
19102 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
19103 {
19104 struct objfile *objfile = dwarf2_per_objfile->objfile;
19105 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19106
19107 complaint (_("DW_AT_low_pc %s is zero "
19108 "for DIE at %s [in module %s]"),
19109 paddress (gdbarch, lowpc),
19110 sect_offset_str (sect_off),
19111 objfile_name (objfile));
19112 }
19113 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19114 else if (lowpc >= highpc)
19115 {
19116 struct objfile *objfile = dwarf2_per_objfile->objfile;
19117 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19118
19119 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19120 "for DIE at %s [in module %s]"),
19121 paddress (gdbarch, lowpc),
19122 paddress (gdbarch, highpc),
19123 sect_offset_str (sect_off),
19124 objfile_name (objfile));
19125 }
19126 else
19127 has_pc_info = 1;
19128 }
19129
19130 return info_ptr;
19131 }
19132
19133 /* Find a cached partial DIE at OFFSET in CU. */
19134
19135 struct partial_die_info *
19136 dwarf2_cu::find_partial_die (sect_offset sect_off)
19137 {
19138 struct partial_die_info *lookup_die = NULL;
19139 struct partial_die_info part_die (sect_off);
19140
19141 lookup_die = ((struct partial_die_info *)
19142 htab_find_with_hash (partial_dies, &part_die,
19143 to_underlying (sect_off)));
19144
19145 return lookup_die;
19146 }
19147
19148 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19149 except in the case of .debug_types DIEs which do not reference
19150 outside their CU (they do however referencing other types via
19151 DW_FORM_ref_sig8). */
19152
19153 static const struct cu_partial_die_info
19154 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19155 {
19156 struct dwarf2_per_objfile *dwarf2_per_objfile
19157 = cu->per_cu->dwarf2_per_objfile;
19158 struct objfile *objfile = dwarf2_per_objfile->objfile;
19159 struct dwarf2_per_cu_data *per_cu = NULL;
19160 struct partial_die_info *pd = NULL;
19161
19162 if (offset_in_dwz == cu->per_cu->is_dwz
19163 && offset_in_cu_p (&cu->header, sect_off))
19164 {
19165 pd = cu->find_partial_die (sect_off);
19166 if (pd != NULL)
19167 return { cu, pd };
19168 /* We missed recording what we needed.
19169 Load all dies and try again. */
19170 per_cu = cu->per_cu;
19171 }
19172 else
19173 {
19174 /* TUs don't reference other CUs/TUs (except via type signatures). */
19175 if (cu->per_cu->is_debug_types)
19176 {
19177 error (_("Dwarf Error: Type Unit at offset %s contains"
19178 " external reference to offset %s [in module %s].\n"),
19179 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19180 bfd_get_filename (objfile->obfd));
19181 }
19182 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19183 dwarf2_per_objfile);
19184
19185 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19186 load_partial_comp_unit (per_cu);
19187
19188 per_cu->cu->last_used = 0;
19189 pd = per_cu->cu->find_partial_die (sect_off);
19190 }
19191
19192 /* If we didn't find it, and not all dies have been loaded,
19193 load them all and try again. */
19194
19195 if (pd == NULL && per_cu->load_all_dies == 0)
19196 {
19197 per_cu->load_all_dies = 1;
19198
19199 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19200 THIS_CU->cu may already be in use. So we can't just free it and
19201 replace its DIEs with the ones we read in. Instead, we leave those
19202 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19203 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19204 set. */
19205 load_partial_comp_unit (per_cu);
19206
19207 pd = per_cu->cu->find_partial_die (sect_off);
19208 }
19209
19210 if (pd == NULL)
19211 internal_error (__FILE__, __LINE__,
19212 _("could not find partial DIE %s "
19213 "in cache [from module %s]\n"),
19214 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19215 return { per_cu->cu, pd };
19216 }
19217
19218 /* See if we can figure out if the class lives in a namespace. We do
19219 this by looking for a member function; its demangled name will
19220 contain namespace info, if there is any. */
19221
19222 static void
19223 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19224 struct dwarf2_cu *cu)
19225 {
19226 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19227 what template types look like, because the demangler
19228 frequently doesn't give the same name as the debug info. We
19229 could fix this by only using the demangled name to get the
19230 prefix (but see comment in read_structure_type). */
19231
19232 struct partial_die_info *real_pdi;
19233 struct partial_die_info *child_pdi;
19234
19235 /* If this DIE (this DIE's specification, if any) has a parent, then
19236 we should not do this. We'll prepend the parent's fully qualified
19237 name when we create the partial symbol. */
19238
19239 real_pdi = struct_pdi;
19240 while (real_pdi->has_specification)
19241 {
19242 auto res = find_partial_die (real_pdi->spec_offset,
19243 real_pdi->spec_is_dwz, cu);
19244 real_pdi = res.pdi;
19245 cu = res.cu;
19246 }
19247
19248 if (real_pdi->die_parent != NULL)
19249 return;
19250
19251 for (child_pdi = struct_pdi->die_child;
19252 child_pdi != NULL;
19253 child_pdi = child_pdi->die_sibling)
19254 {
19255 if (child_pdi->tag == DW_TAG_subprogram
19256 && child_pdi->linkage_name != NULL)
19257 {
19258 gdb::unique_xmalloc_ptr<char> actual_class_name
19259 (language_class_name_from_physname (cu->language_defn,
19260 child_pdi->linkage_name));
19261 if (actual_class_name != NULL)
19262 {
19263 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19264 struct_pdi->name
19265 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19266 actual_class_name.get ());
19267 }
19268 break;
19269 }
19270 }
19271 }
19272
19273 void
19274 partial_die_info::fixup (struct dwarf2_cu *cu)
19275 {
19276 /* Once we've fixed up a die, there's no point in doing so again.
19277 This also avoids a memory leak if we were to call
19278 guess_partial_die_structure_name multiple times. */
19279 if (fixup_called)
19280 return;
19281
19282 /* If we found a reference attribute and the DIE has no name, try
19283 to find a name in the referred to DIE. */
19284
19285 if (name == NULL && has_specification)
19286 {
19287 struct partial_die_info *spec_die;
19288
19289 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19290 spec_die = res.pdi;
19291 cu = res.cu;
19292
19293 spec_die->fixup (cu);
19294
19295 if (spec_die->name)
19296 {
19297 name = spec_die->name;
19298
19299 /* Copy DW_AT_external attribute if it is set. */
19300 if (spec_die->is_external)
19301 is_external = spec_die->is_external;
19302 }
19303 }
19304
19305 /* Set default names for some unnamed DIEs. */
19306
19307 if (name == NULL && tag == DW_TAG_namespace)
19308 name = CP_ANONYMOUS_NAMESPACE_STR;
19309
19310 /* If there is no parent die to provide a namespace, and there are
19311 children, see if we can determine the namespace from their linkage
19312 name. */
19313 if (cu->language == language_cplus
19314 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
19315 && die_parent == NULL
19316 && has_children
19317 && (tag == DW_TAG_class_type
19318 || tag == DW_TAG_structure_type
19319 || tag == DW_TAG_union_type))
19320 guess_partial_die_structure_name (this, cu);
19321
19322 /* GCC might emit a nameless struct or union that has a linkage
19323 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19324 if (name == NULL
19325 && (tag == DW_TAG_class_type
19326 || tag == DW_TAG_interface_type
19327 || tag == DW_TAG_structure_type
19328 || tag == DW_TAG_union_type)
19329 && linkage_name != NULL)
19330 {
19331 gdb::unique_xmalloc_ptr<char> demangled
19332 (gdb_demangle (linkage_name, DMGL_TYPES));
19333 if (demangled != nullptr)
19334 {
19335 const char *base;
19336
19337 /* Strip any leading namespaces/classes, keep only the base name.
19338 DW_AT_name for named DIEs does not contain the prefixes. */
19339 base = strrchr (demangled.get (), ':');
19340 if (base && base > demangled.get () && base[-1] == ':')
19341 base++;
19342 else
19343 base = demangled.get ();
19344
19345 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19346 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
19347 }
19348 }
19349
19350 fixup_called = 1;
19351 }
19352
19353 /* Process the attributes that had to be skipped in the first round. These
19354 attributes are the ones that need str_offsets_base or addr_base attributes.
19355 They could not have been processed in the first round, because at the time
19356 the values of str_offsets_base or addr_base may not have been known. */
19357 void read_attribute_reprocess (const struct die_reader_specs *reader,
19358 struct attribute *attr)
19359 {
19360 struct dwarf2_cu *cu = reader->cu;
19361 switch (attr->form)
19362 {
19363 case DW_FORM_addrx:
19364 case DW_FORM_GNU_addr_index:
19365 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19366 break;
19367 case DW_FORM_strx:
19368 case DW_FORM_strx1:
19369 case DW_FORM_strx2:
19370 case DW_FORM_strx3:
19371 case DW_FORM_strx4:
19372 case DW_FORM_GNU_str_index:
19373 {
19374 unsigned int str_index = DW_UNSND (attr);
19375 if (reader->dwo_file != NULL)
19376 {
19377 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19378 DW_STRING_IS_CANONICAL (attr) = 0;
19379 }
19380 else
19381 {
19382 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19383 DW_STRING_IS_CANONICAL (attr) = 0;
19384 }
19385 break;
19386 }
19387 default:
19388 gdb_assert_not_reached (_("Unexpected DWARF form."));
19389 }
19390 }
19391
19392 /* Read an attribute value described by an attribute form. */
19393
19394 static const gdb_byte *
19395 read_attribute_value (const struct die_reader_specs *reader,
19396 struct attribute *attr, unsigned form,
19397 LONGEST implicit_const, const gdb_byte *info_ptr,
19398 bool *need_reprocess)
19399 {
19400 struct dwarf2_cu *cu = reader->cu;
19401 struct dwarf2_per_objfile *dwarf2_per_objfile
19402 = cu->per_cu->dwarf2_per_objfile;
19403 struct objfile *objfile = dwarf2_per_objfile->objfile;
19404 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19405 bfd *abfd = reader->abfd;
19406 struct comp_unit_head *cu_header = &cu->header;
19407 unsigned int bytes_read;
19408 struct dwarf_block *blk;
19409 *need_reprocess = false;
19410
19411 attr->form = (enum dwarf_form) form;
19412 switch (form)
19413 {
19414 case DW_FORM_ref_addr:
19415 if (cu->header.version == 2)
19416 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19417 else
19418 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19419 &cu->header, &bytes_read);
19420 info_ptr += bytes_read;
19421 break;
19422 case DW_FORM_GNU_ref_alt:
19423 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19424 info_ptr += bytes_read;
19425 break;
19426 case DW_FORM_addr:
19427 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19428 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19429 info_ptr += bytes_read;
19430 break;
19431 case DW_FORM_block2:
19432 blk = dwarf_alloc_block (cu);
19433 blk->size = read_2_bytes (abfd, info_ptr);
19434 info_ptr += 2;
19435 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19436 info_ptr += blk->size;
19437 DW_BLOCK (attr) = blk;
19438 break;
19439 case DW_FORM_block4:
19440 blk = dwarf_alloc_block (cu);
19441 blk->size = read_4_bytes (abfd, info_ptr);
19442 info_ptr += 4;
19443 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19444 info_ptr += blk->size;
19445 DW_BLOCK (attr) = blk;
19446 break;
19447 case DW_FORM_data2:
19448 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19449 info_ptr += 2;
19450 break;
19451 case DW_FORM_data4:
19452 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19453 info_ptr += 4;
19454 break;
19455 case DW_FORM_data8:
19456 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19457 info_ptr += 8;
19458 break;
19459 case DW_FORM_data16:
19460 blk = dwarf_alloc_block (cu);
19461 blk->size = 16;
19462 blk->data = read_n_bytes (abfd, info_ptr, 16);
19463 info_ptr += 16;
19464 DW_BLOCK (attr) = blk;
19465 break;
19466 case DW_FORM_sec_offset:
19467 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19468 info_ptr += bytes_read;
19469 break;
19470 case DW_FORM_string:
19471 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19472 DW_STRING_IS_CANONICAL (attr) = 0;
19473 info_ptr += bytes_read;
19474 break;
19475 case DW_FORM_strp:
19476 if (!cu->per_cu->is_dwz)
19477 {
19478 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19479 abfd, info_ptr, cu_header,
19480 &bytes_read);
19481 DW_STRING_IS_CANONICAL (attr) = 0;
19482 info_ptr += bytes_read;
19483 break;
19484 }
19485 /* FALLTHROUGH */
19486 case DW_FORM_line_strp:
19487 if (!cu->per_cu->is_dwz)
19488 {
19489 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19490 abfd, info_ptr,
19491 cu_header, &bytes_read);
19492 DW_STRING_IS_CANONICAL (attr) = 0;
19493 info_ptr += bytes_read;
19494 break;
19495 }
19496 /* FALLTHROUGH */
19497 case DW_FORM_GNU_strp_alt:
19498 {
19499 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19500 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19501 &bytes_read);
19502
19503 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19504 dwz, str_offset);
19505 DW_STRING_IS_CANONICAL (attr) = 0;
19506 info_ptr += bytes_read;
19507 }
19508 break;
19509 case DW_FORM_exprloc:
19510 case DW_FORM_block:
19511 blk = dwarf_alloc_block (cu);
19512 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19513 info_ptr += bytes_read;
19514 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19515 info_ptr += blk->size;
19516 DW_BLOCK (attr) = blk;
19517 break;
19518 case DW_FORM_block1:
19519 blk = dwarf_alloc_block (cu);
19520 blk->size = read_1_byte (abfd, info_ptr);
19521 info_ptr += 1;
19522 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19523 info_ptr += blk->size;
19524 DW_BLOCK (attr) = blk;
19525 break;
19526 case DW_FORM_data1:
19527 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19528 info_ptr += 1;
19529 break;
19530 case DW_FORM_flag:
19531 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19532 info_ptr += 1;
19533 break;
19534 case DW_FORM_flag_present:
19535 DW_UNSND (attr) = 1;
19536 break;
19537 case DW_FORM_sdata:
19538 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19539 info_ptr += bytes_read;
19540 break;
19541 case DW_FORM_udata:
19542 case DW_FORM_rnglistx:
19543 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19544 info_ptr += bytes_read;
19545 break;
19546 case DW_FORM_ref1:
19547 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19548 + read_1_byte (abfd, info_ptr));
19549 info_ptr += 1;
19550 break;
19551 case DW_FORM_ref2:
19552 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19553 + read_2_bytes (abfd, info_ptr));
19554 info_ptr += 2;
19555 break;
19556 case DW_FORM_ref4:
19557 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19558 + read_4_bytes (abfd, info_ptr));
19559 info_ptr += 4;
19560 break;
19561 case DW_FORM_ref8:
19562 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19563 + read_8_bytes (abfd, info_ptr));
19564 info_ptr += 8;
19565 break;
19566 case DW_FORM_ref_sig8:
19567 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19568 info_ptr += 8;
19569 break;
19570 case DW_FORM_ref_udata:
19571 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19572 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19573 info_ptr += bytes_read;
19574 break;
19575 case DW_FORM_indirect:
19576 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19577 info_ptr += bytes_read;
19578 if (form == DW_FORM_implicit_const)
19579 {
19580 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19581 info_ptr += bytes_read;
19582 }
19583 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19584 info_ptr, need_reprocess);
19585 break;
19586 case DW_FORM_implicit_const:
19587 DW_SND (attr) = implicit_const;
19588 break;
19589 case DW_FORM_addrx:
19590 case DW_FORM_GNU_addr_index:
19591 *need_reprocess = true;
19592 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19593 info_ptr += bytes_read;
19594 break;
19595 case DW_FORM_strx:
19596 case DW_FORM_strx1:
19597 case DW_FORM_strx2:
19598 case DW_FORM_strx3:
19599 case DW_FORM_strx4:
19600 case DW_FORM_GNU_str_index:
19601 {
19602 ULONGEST str_index;
19603 if (form == DW_FORM_strx1)
19604 {
19605 str_index = read_1_byte (abfd, info_ptr);
19606 info_ptr += 1;
19607 }
19608 else if (form == DW_FORM_strx2)
19609 {
19610 str_index = read_2_bytes (abfd, info_ptr);
19611 info_ptr += 2;
19612 }
19613 else if (form == DW_FORM_strx3)
19614 {
19615 str_index = read_3_bytes (abfd, info_ptr);
19616 info_ptr += 3;
19617 }
19618 else if (form == DW_FORM_strx4)
19619 {
19620 str_index = read_4_bytes (abfd, info_ptr);
19621 info_ptr += 4;
19622 }
19623 else
19624 {
19625 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19626 info_ptr += bytes_read;
19627 }
19628 *need_reprocess = true;
19629 DW_UNSND (attr) = str_index;
19630 }
19631 break;
19632 default:
19633 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19634 dwarf_form_name (form),
19635 bfd_get_filename (abfd));
19636 }
19637
19638 /* Super hack. */
19639 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19640 attr->form = DW_FORM_GNU_ref_alt;
19641
19642 /* We have seen instances where the compiler tried to emit a byte
19643 size attribute of -1 which ended up being encoded as an unsigned
19644 0xffffffff. Although 0xffffffff is technically a valid size value,
19645 an object of this size seems pretty unlikely so we can relatively
19646 safely treat these cases as if the size attribute was invalid and
19647 treat them as zero by default. */
19648 if (attr->name == DW_AT_byte_size
19649 && form == DW_FORM_data4
19650 && DW_UNSND (attr) >= 0xffffffff)
19651 {
19652 complaint
19653 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19654 hex_string (DW_UNSND (attr)));
19655 DW_UNSND (attr) = 0;
19656 }
19657
19658 return info_ptr;
19659 }
19660
19661 /* Read an attribute described by an abbreviated attribute. */
19662
19663 static const gdb_byte *
19664 read_attribute (const struct die_reader_specs *reader,
19665 struct attribute *attr, struct attr_abbrev *abbrev,
19666 const gdb_byte *info_ptr, bool *need_reprocess)
19667 {
19668 attr->name = abbrev->name;
19669 return read_attribute_value (reader, attr, abbrev->form,
19670 abbrev->implicit_const, info_ptr,
19671 need_reprocess);
19672 }
19673
19674 static CORE_ADDR
19675 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19676 unsigned int *bytes_read)
19677 {
19678 struct comp_unit_head *cu_header = &cu->header;
19679 CORE_ADDR retval = 0;
19680
19681 if (cu_header->signed_addr_p)
19682 {
19683 switch (cu_header->addr_size)
19684 {
19685 case 2:
19686 retval = bfd_get_signed_16 (abfd, buf);
19687 break;
19688 case 4:
19689 retval = bfd_get_signed_32 (abfd, buf);
19690 break;
19691 case 8:
19692 retval = bfd_get_signed_64 (abfd, buf);
19693 break;
19694 default:
19695 internal_error (__FILE__, __LINE__,
19696 _("read_address: bad switch, signed [in module %s]"),
19697 bfd_get_filename (abfd));
19698 }
19699 }
19700 else
19701 {
19702 switch (cu_header->addr_size)
19703 {
19704 case 2:
19705 retval = bfd_get_16 (abfd, buf);
19706 break;
19707 case 4:
19708 retval = bfd_get_32 (abfd, buf);
19709 break;
19710 case 8:
19711 retval = bfd_get_64 (abfd, buf);
19712 break;
19713 default:
19714 internal_error (__FILE__, __LINE__,
19715 _("read_address: bad switch, "
19716 "unsigned [in module %s]"),
19717 bfd_get_filename (abfd));
19718 }
19719 }
19720
19721 *bytes_read = cu_header->addr_size;
19722 return retval;
19723 }
19724
19725 /* Read the initial length from a section. The (draft) DWARF 3
19726 specification allows the initial length to take up either 4 bytes
19727 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19728 bytes describe the length and all offsets will be 8 bytes in length
19729 instead of 4.
19730
19731 An older, non-standard 64-bit format is also handled by this
19732 function. The older format in question stores the initial length
19733 as an 8-byte quantity without an escape value. Lengths greater
19734 than 2^32 aren't very common which means that the initial 4 bytes
19735 is almost always zero. Since a length value of zero doesn't make
19736 sense for the 32-bit format, this initial zero can be considered to
19737 be an escape value which indicates the presence of the older 64-bit
19738 format. As written, the code can't detect (old format) lengths
19739 greater than 4GB. If it becomes necessary to handle lengths
19740 somewhat larger than 4GB, we could allow other small values (such
19741 as the non-sensical values of 1, 2, and 3) to also be used as
19742 escape values indicating the presence of the old format.
19743
19744 The value returned via bytes_read should be used to increment the
19745 relevant pointer after calling read_initial_length().
19746
19747 [ Note: read_initial_length() and read_offset() are based on the
19748 document entitled "DWARF Debugging Information Format", revision
19749 3, draft 8, dated November 19, 2001. This document was obtained
19750 from:
19751
19752 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19753
19754 This document is only a draft and is subject to change. (So beware.)
19755
19756 Details regarding the older, non-standard 64-bit format were
19757 determined empirically by examining 64-bit ELF files produced by
19758 the SGI toolchain on an IRIX 6.5 machine.
19759
19760 - Kevin, July 16, 2002
19761 ] */
19762
19763 static LONGEST
19764 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19765 {
19766 LONGEST length = bfd_get_32 (abfd, buf);
19767
19768 if (length == 0xffffffff)
19769 {
19770 length = bfd_get_64 (abfd, buf + 4);
19771 *bytes_read = 12;
19772 }
19773 else if (length == 0)
19774 {
19775 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19776 length = bfd_get_64 (abfd, buf);
19777 *bytes_read = 8;
19778 }
19779 else
19780 {
19781 *bytes_read = 4;
19782 }
19783
19784 return length;
19785 }
19786
19787 /* Cover function for read_initial_length.
19788 Returns the length of the object at BUF, and stores the size of the
19789 initial length in *BYTES_READ and stores the size that offsets will be in
19790 *OFFSET_SIZE.
19791 If the initial length size is not equivalent to that specified in
19792 CU_HEADER then issue a complaint.
19793 This is useful when reading non-comp-unit headers. */
19794
19795 static LONGEST
19796 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19797 const struct comp_unit_head *cu_header,
19798 unsigned int *bytes_read,
19799 unsigned int *offset_size)
19800 {
19801 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19802
19803 gdb_assert (cu_header->initial_length_size == 4
19804 || cu_header->initial_length_size == 8
19805 || cu_header->initial_length_size == 12);
19806
19807 if (cu_header->initial_length_size != *bytes_read)
19808 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19809
19810 *offset_size = (*bytes_read == 4) ? 4 : 8;
19811 return length;
19812 }
19813
19814 /* Read an offset from the data stream. The size of the offset is
19815 given by cu_header->offset_size. */
19816
19817 static LONGEST
19818 read_offset (bfd *abfd, const gdb_byte *buf,
19819 const struct comp_unit_head *cu_header,
19820 unsigned int *bytes_read)
19821 {
19822 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19823
19824 *bytes_read = cu_header->offset_size;
19825 return offset;
19826 }
19827
19828 /* Read an offset from the data stream. */
19829
19830 static LONGEST
19831 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19832 {
19833 LONGEST retval = 0;
19834
19835 switch (offset_size)
19836 {
19837 case 4:
19838 retval = bfd_get_32 (abfd, buf);
19839 break;
19840 case 8:
19841 retval = bfd_get_64 (abfd, buf);
19842 break;
19843 default:
19844 internal_error (__FILE__, __LINE__,
19845 _("read_offset_1: bad switch [in module %s]"),
19846 bfd_get_filename (abfd));
19847 }
19848
19849 return retval;
19850 }
19851
19852 static const gdb_byte *
19853 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19854 {
19855 /* If the size of a host char is 8 bits, we can return a pointer
19856 to the buffer, otherwise we have to copy the data to a buffer
19857 allocated on the temporary obstack. */
19858 gdb_assert (HOST_CHAR_BIT == 8);
19859 return buf;
19860 }
19861
19862 static const char *
19863 read_direct_string (bfd *abfd, const gdb_byte *buf,
19864 unsigned int *bytes_read_ptr)
19865 {
19866 /* If the size of a host char is 8 bits, we can return a pointer
19867 to the string, otherwise we have to copy the string to a buffer
19868 allocated on the temporary obstack. */
19869 gdb_assert (HOST_CHAR_BIT == 8);
19870 if (*buf == '\0')
19871 {
19872 *bytes_read_ptr = 1;
19873 return NULL;
19874 }
19875 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19876 return (const char *) buf;
19877 }
19878
19879 /* Return pointer to string at section SECT offset STR_OFFSET with error
19880 reporting strings FORM_NAME and SECT_NAME. */
19881
19882 static const char *
19883 read_indirect_string_at_offset_from (struct objfile *objfile,
19884 bfd *abfd, LONGEST str_offset,
19885 struct dwarf2_section_info *sect,
19886 const char *form_name,
19887 const char *sect_name)
19888 {
19889 dwarf2_read_section (objfile, sect);
19890 if (sect->buffer == NULL)
19891 error (_("%s used without %s section [in module %s]"),
19892 form_name, sect_name, bfd_get_filename (abfd));
19893 if (str_offset >= sect->size)
19894 error (_("%s pointing outside of %s section [in module %s]"),
19895 form_name, sect_name, bfd_get_filename (abfd));
19896 gdb_assert (HOST_CHAR_BIT == 8);
19897 if (sect->buffer[str_offset] == '\0')
19898 return NULL;
19899 return (const char *) (sect->buffer + str_offset);
19900 }
19901
19902 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19903
19904 static const char *
19905 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19906 bfd *abfd, LONGEST str_offset)
19907 {
19908 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19909 abfd, str_offset,
19910 &dwarf2_per_objfile->str,
19911 "DW_FORM_strp", ".debug_str");
19912 }
19913
19914 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19915
19916 static const char *
19917 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19918 bfd *abfd, LONGEST str_offset)
19919 {
19920 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19921 abfd, str_offset,
19922 &dwarf2_per_objfile->line_str,
19923 "DW_FORM_line_strp",
19924 ".debug_line_str");
19925 }
19926
19927 /* Read a string at offset STR_OFFSET in the .debug_str section from
19928 the .dwz file DWZ. Throw an error if the offset is too large. If
19929 the string consists of a single NUL byte, return NULL; otherwise
19930 return a pointer to the string. */
19931
19932 static const char *
19933 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19934 LONGEST str_offset)
19935 {
19936 dwarf2_read_section (objfile, &dwz->str);
19937
19938 if (dwz->str.buffer == NULL)
19939 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19940 "section [in module %s]"),
19941 bfd_get_filename (dwz->dwz_bfd.get ()));
19942 if (str_offset >= dwz->str.size)
19943 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19944 ".debug_str section [in module %s]"),
19945 bfd_get_filename (dwz->dwz_bfd.get ()));
19946 gdb_assert (HOST_CHAR_BIT == 8);
19947 if (dwz->str.buffer[str_offset] == '\0')
19948 return NULL;
19949 return (const char *) (dwz->str.buffer + str_offset);
19950 }
19951
19952 /* Return pointer to string at .debug_str offset as read from BUF.
19953 BUF is assumed to be in a compilation unit described by CU_HEADER.
19954 Return *BYTES_READ_PTR count of bytes read from BUF. */
19955
19956 static const char *
19957 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19958 const gdb_byte *buf,
19959 const struct comp_unit_head *cu_header,
19960 unsigned int *bytes_read_ptr)
19961 {
19962 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19963
19964 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19965 }
19966
19967 /* Return pointer to string at .debug_line_str offset as read from BUF.
19968 BUF is assumed to be in a compilation unit described by CU_HEADER.
19969 Return *BYTES_READ_PTR count of bytes read from BUF. */
19970
19971 static const char *
19972 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19973 bfd *abfd, const gdb_byte *buf,
19974 const struct comp_unit_head *cu_header,
19975 unsigned int *bytes_read_ptr)
19976 {
19977 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19978
19979 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19980 str_offset);
19981 }
19982
19983 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19984 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19985 ADDR_SIZE is the size of addresses from the CU header. */
19986
19987 static CORE_ADDR
19988 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19989 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19990 int addr_size)
19991 {
19992 struct objfile *objfile = dwarf2_per_objfile->objfile;
19993 bfd *abfd = objfile->obfd;
19994 const gdb_byte *info_ptr;
19995 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19996
19997 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19998 if (dwarf2_per_objfile->addr.buffer == NULL)
19999 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20000 objfile_name (objfile));
20001 if (addr_base_or_zero + addr_index * addr_size
20002 >= dwarf2_per_objfile->addr.size)
20003 error (_("DW_FORM_addr_index pointing outside of "
20004 ".debug_addr section [in module %s]"),
20005 objfile_name (objfile));
20006 info_ptr = (dwarf2_per_objfile->addr.buffer
20007 + addr_base_or_zero + addr_index * addr_size);
20008 if (addr_size == 4)
20009 return bfd_get_32 (abfd, info_ptr);
20010 else
20011 return bfd_get_64 (abfd, info_ptr);
20012 }
20013
20014 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20015
20016 static CORE_ADDR
20017 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20018 {
20019 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20020 cu->addr_base, cu->header.addr_size);
20021 }
20022
20023 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20024
20025 static CORE_ADDR
20026 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20027 unsigned int *bytes_read)
20028 {
20029 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
20030 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20031
20032 return read_addr_index (cu, addr_index);
20033 }
20034
20035 /* Given an index in .debug_addr, fetch the value.
20036 NOTE: This can be called during dwarf expression evaluation,
20037 long after the debug information has been read, and thus per_cu->cu
20038 may no longer exist. */
20039
20040 CORE_ADDR
20041 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20042 unsigned int addr_index)
20043 {
20044 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
20045 struct dwarf2_cu *cu = per_cu->cu;
20046 gdb::optional<ULONGEST> addr_base;
20047 int addr_size;
20048
20049 /* We need addr_base and addr_size.
20050 If we don't have PER_CU->cu, we have to get it.
20051 Nasty, but the alternative is storing the needed info in PER_CU,
20052 which at this point doesn't seem justified: it's not clear how frequently
20053 it would get used and it would increase the size of every PER_CU.
20054 Entry points like dwarf2_per_cu_addr_size do a similar thing
20055 so we're not in uncharted territory here.
20056 Alas we need to be a bit more complicated as addr_base is contained
20057 in the DIE.
20058
20059 We don't need to read the entire CU(/TU).
20060 We just need the header and top level die.
20061
20062 IWBN to use the aging mechanism to let us lazily later discard the CU.
20063 For now we skip this optimization. */
20064
20065 if (cu != NULL)
20066 {
20067 addr_base = cu->addr_base;
20068 addr_size = cu->header.addr_size;
20069 }
20070 else
20071 {
20072 cutu_reader reader (per_cu, NULL, 0, 0, false);
20073 addr_base = reader.cu->addr_base;
20074 addr_size = reader.cu->header.addr_size;
20075 }
20076
20077 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20078 addr_size);
20079 }
20080
20081 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20082 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20083 DWO file. */
20084
20085 static const char *
20086 read_str_index (struct dwarf2_cu *cu,
20087 struct dwarf2_section_info *str_section,
20088 struct dwarf2_section_info *str_offsets_section,
20089 ULONGEST str_offsets_base, ULONGEST str_index)
20090 {
20091 struct dwarf2_per_objfile *dwarf2_per_objfile
20092 = cu->per_cu->dwarf2_per_objfile;
20093 struct objfile *objfile = dwarf2_per_objfile->objfile;
20094 const char *objf_name = objfile_name (objfile);
20095 bfd *abfd = objfile->obfd;
20096 const gdb_byte *info_ptr;
20097 ULONGEST str_offset;
20098 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20099
20100 dwarf2_read_section (objfile, str_section);
20101 dwarf2_read_section (objfile, str_offsets_section);
20102 if (str_section->buffer == NULL)
20103 error (_("%s used without %s section"
20104 " in CU at offset %s [in module %s]"),
20105 form_name, get_section_name (str_section),
20106 sect_offset_str (cu->header.sect_off), objf_name);
20107 if (str_offsets_section->buffer == NULL)
20108 error (_("%s used without %s section"
20109 " in CU at offset %s [in module %s]"),
20110 form_name, get_section_name (str_section),
20111 sect_offset_str (cu->header.sect_off), objf_name);
20112 info_ptr = (str_offsets_section->buffer
20113 + str_offsets_base
20114 + str_index * cu->header.offset_size);
20115 if (cu->header.offset_size == 4)
20116 str_offset = bfd_get_32 (abfd, info_ptr);
20117 else
20118 str_offset = bfd_get_64 (abfd, info_ptr);
20119 if (str_offset >= str_section->size)
20120 error (_("Offset from %s pointing outside of"
20121 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20122 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20123 return (const char *) (str_section->buffer + str_offset);
20124 }
20125
20126 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20127
20128 static const char *
20129 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20130 {
20131 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20132 ? reader->cu->header.addr_size : 0;
20133 return read_str_index (reader->cu,
20134 &reader->dwo_file->sections.str,
20135 &reader->dwo_file->sections.str_offsets,
20136 str_offsets_base, str_index);
20137 }
20138
20139 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20140
20141 static const char *
20142 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20143 {
20144 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20145 const char *objf_name = objfile_name (objfile);
20146 static const char form_name[] = "DW_FORM_GNU_str_index";
20147 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20148
20149 if (!cu->str_offsets_base.has_value ())
20150 error (_("%s used in Fission stub without %s"
20151 " in CU at offset 0x%lx [in module %s]"),
20152 form_name, str_offsets_attr_name,
20153 (long) cu->header.offset_size, objf_name);
20154
20155 return read_str_index (cu,
20156 &cu->per_cu->dwarf2_per_objfile->str,
20157 &cu->per_cu->dwarf2_per_objfile->str_offsets,
20158 *cu->str_offsets_base, str_index);
20159 }
20160
20161 /* Return the length of an LEB128 number in BUF. */
20162
20163 static int
20164 leb128_size (const gdb_byte *buf)
20165 {
20166 const gdb_byte *begin = buf;
20167 gdb_byte byte;
20168
20169 while (1)
20170 {
20171 byte = *buf++;
20172 if ((byte & 128) == 0)
20173 return buf - begin;
20174 }
20175 }
20176
20177 static void
20178 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20179 {
20180 switch (lang)
20181 {
20182 case DW_LANG_C89:
20183 case DW_LANG_C99:
20184 case DW_LANG_C11:
20185 case DW_LANG_C:
20186 case DW_LANG_UPC:
20187 cu->language = language_c;
20188 break;
20189 case DW_LANG_Java:
20190 case DW_LANG_C_plus_plus:
20191 case DW_LANG_C_plus_plus_11:
20192 case DW_LANG_C_plus_plus_14:
20193 cu->language = language_cplus;
20194 break;
20195 case DW_LANG_D:
20196 cu->language = language_d;
20197 break;
20198 case DW_LANG_Fortran77:
20199 case DW_LANG_Fortran90:
20200 case DW_LANG_Fortran95:
20201 case DW_LANG_Fortran03:
20202 case DW_LANG_Fortran08:
20203 cu->language = language_fortran;
20204 break;
20205 case DW_LANG_Go:
20206 cu->language = language_go;
20207 break;
20208 case DW_LANG_Mips_Assembler:
20209 cu->language = language_asm;
20210 break;
20211 case DW_LANG_Ada83:
20212 case DW_LANG_Ada95:
20213 cu->language = language_ada;
20214 break;
20215 case DW_LANG_Modula2:
20216 cu->language = language_m2;
20217 break;
20218 case DW_LANG_Pascal83:
20219 cu->language = language_pascal;
20220 break;
20221 case DW_LANG_ObjC:
20222 cu->language = language_objc;
20223 break;
20224 case DW_LANG_Rust:
20225 case DW_LANG_Rust_old:
20226 cu->language = language_rust;
20227 break;
20228 case DW_LANG_Cobol74:
20229 case DW_LANG_Cobol85:
20230 default:
20231 cu->language = language_minimal;
20232 break;
20233 }
20234 cu->language_defn = language_def (cu->language);
20235 }
20236
20237 /* Return the named attribute or NULL if not there. */
20238
20239 static struct attribute *
20240 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20241 {
20242 for (;;)
20243 {
20244 unsigned int i;
20245 struct attribute *spec = NULL;
20246
20247 for (i = 0; i < die->num_attrs; ++i)
20248 {
20249 if (die->attrs[i].name == name)
20250 return &die->attrs[i];
20251 if (die->attrs[i].name == DW_AT_specification
20252 || die->attrs[i].name == DW_AT_abstract_origin)
20253 spec = &die->attrs[i];
20254 }
20255
20256 if (!spec)
20257 break;
20258
20259 die = follow_die_ref (die, spec, &cu);
20260 }
20261
20262 return NULL;
20263 }
20264
20265 /* Return the named attribute or NULL if not there,
20266 but do not follow DW_AT_specification, etc.
20267 This is for use in contexts where we're reading .debug_types dies.
20268 Following DW_AT_specification, DW_AT_abstract_origin will take us
20269 back up the chain, and we want to go down. */
20270
20271 static struct attribute *
20272 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20273 {
20274 unsigned int i;
20275
20276 for (i = 0; i < die->num_attrs; ++i)
20277 if (die->attrs[i].name == name)
20278 return &die->attrs[i];
20279
20280 return NULL;
20281 }
20282
20283 /* Return the string associated with a string-typed attribute, or NULL if it
20284 is either not found or is of an incorrect type. */
20285
20286 static const char *
20287 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20288 {
20289 struct attribute *attr;
20290 const char *str = NULL;
20291
20292 attr = dwarf2_attr (die, name, cu);
20293
20294 if (attr != NULL)
20295 {
20296 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20297 || attr->form == DW_FORM_string
20298 || attr->form == DW_FORM_strx
20299 || attr->form == DW_FORM_strx1
20300 || attr->form == DW_FORM_strx2
20301 || attr->form == DW_FORM_strx3
20302 || attr->form == DW_FORM_strx4
20303 || attr->form == DW_FORM_GNU_str_index
20304 || attr->form == DW_FORM_GNU_strp_alt)
20305 str = DW_STRING (attr);
20306 else
20307 complaint (_("string type expected for attribute %s for "
20308 "DIE at %s in module %s"),
20309 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20310 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20311 }
20312
20313 return str;
20314 }
20315
20316 /* Return the dwo name or NULL if not present. If present, it is in either
20317 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20318 static const char *
20319 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20320 {
20321 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20322 if (dwo_name == nullptr)
20323 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20324 return dwo_name;
20325 }
20326
20327 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20328 and holds a non-zero value. This function should only be used for
20329 DW_FORM_flag or DW_FORM_flag_present attributes. */
20330
20331 static int
20332 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20333 {
20334 struct attribute *attr = dwarf2_attr (die, name, cu);
20335
20336 return (attr && DW_UNSND (attr));
20337 }
20338
20339 static int
20340 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20341 {
20342 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20343 which value is non-zero. However, we have to be careful with
20344 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20345 (via dwarf2_flag_true_p) follows this attribute. So we may
20346 end up accidently finding a declaration attribute that belongs
20347 to a different DIE referenced by the specification attribute,
20348 even though the given DIE does not have a declaration attribute. */
20349 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20350 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20351 }
20352
20353 /* Return the die giving the specification for DIE, if there is
20354 one. *SPEC_CU is the CU containing DIE on input, and the CU
20355 containing the return value on output. If there is no
20356 specification, but there is an abstract origin, that is
20357 returned. */
20358
20359 static struct die_info *
20360 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20361 {
20362 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20363 *spec_cu);
20364
20365 if (spec_attr == NULL)
20366 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20367
20368 if (spec_attr == NULL)
20369 return NULL;
20370 else
20371 return follow_die_ref (die, spec_attr, spec_cu);
20372 }
20373
20374 /* Stub for free_line_header to match void * callback types. */
20375
20376 static void
20377 free_line_header_voidp (void *arg)
20378 {
20379 struct line_header *lh = (struct line_header *) arg;
20380
20381 delete lh;
20382 }
20383
20384 void
20385 line_header::add_include_dir (const char *include_dir)
20386 {
20387 if (dwarf_line_debug >= 2)
20388 {
20389 size_t new_size;
20390 if (version >= 5)
20391 new_size = m_include_dirs.size ();
20392 else
20393 new_size = m_include_dirs.size () + 1;
20394 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20395 new_size, include_dir);
20396 }
20397 m_include_dirs.push_back (include_dir);
20398 }
20399
20400 void
20401 line_header::add_file_name (const char *name,
20402 dir_index d_index,
20403 unsigned int mod_time,
20404 unsigned int length)
20405 {
20406 if (dwarf_line_debug >= 2)
20407 {
20408 size_t new_size;
20409 if (version >= 5)
20410 new_size = file_names_size ();
20411 else
20412 new_size = file_names_size () + 1;
20413 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20414 new_size, name);
20415 }
20416 m_file_names.emplace_back (name, d_index, mod_time, length);
20417 }
20418
20419 /* A convenience function to find the proper .debug_line section for a CU. */
20420
20421 static struct dwarf2_section_info *
20422 get_debug_line_section (struct dwarf2_cu *cu)
20423 {
20424 struct dwarf2_section_info *section;
20425 struct dwarf2_per_objfile *dwarf2_per_objfile
20426 = cu->per_cu->dwarf2_per_objfile;
20427
20428 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20429 DWO file. */
20430 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20431 section = &cu->dwo_unit->dwo_file->sections.line;
20432 else if (cu->per_cu->is_dwz)
20433 {
20434 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20435
20436 section = &dwz->line;
20437 }
20438 else
20439 section = &dwarf2_per_objfile->line;
20440
20441 return section;
20442 }
20443
20444 /* Read directory or file name entry format, starting with byte of
20445 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20446 entries count and the entries themselves in the described entry
20447 format. */
20448
20449 static void
20450 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20451 bfd *abfd, const gdb_byte **bufp,
20452 struct line_header *lh,
20453 const struct comp_unit_head *cu_header,
20454 void (*callback) (struct line_header *lh,
20455 const char *name,
20456 dir_index d_index,
20457 unsigned int mod_time,
20458 unsigned int length))
20459 {
20460 gdb_byte format_count, formati;
20461 ULONGEST data_count, datai;
20462 const gdb_byte *buf = *bufp;
20463 const gdb_byte *format_header_data;
20464 unsigned int bytes_read;
20465
20466 format_count = read_1_byte (abfd, buf);
20467 buf += 1;
20468 format_header_data = buf;
20469 for (formati = 0; formati < format_count; formati++)
20470 {
20471 read_unsigned_leb128 (abfd, buf, &bytes_read);
20472 buf += bytes_read;
20473 read_unsigned_leb128 (abfd, buf, &bytes_read);
20474 buf += bytes_read;
20475 }
20476
20477 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20478 buf += bytes_read;
20479 for (datai = 0; datai < data_count; datai++)
20480 {
20481 const gdb_byte *format = format_header_data;
20482 struct file_entry fe;
20483
20484 for (formati = 0; formati < format_count; formati++)
20485 {
20486 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20487 format += bytes_read;
20488
20489 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20490 format += bytes_read;
20491
20492 gdb::optional<const char *> string;
20493 gdb::optional<unsigned int> uint;
20494
20495 switch (form)
20496 {
20497 case DW_FORM_string:
20498 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20499 buf += bytes_read;
20500 break;
20501
20502 case DW_FORM_line_strp:
20503 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20504 abfd, buf,
20505 cu_header,
20506 &bytes_read));
20507 buf += bytes_read;
20508 break;
20509
20510 case DW_FORM_data1:
20511 uint.emplace (read_1_byte (abfd, buf));
20512 buf += 1;
20513 break;
20514
20515 case DW_FORM_data2:
20516 uint.emplace (read_2_bytes (abfd, buf));
20517 buf += 2;
20518 break;
20519
20520 case DW_FORM_data4:
20521 uint.emplace (read_4_bytes (abfd, buf));
20522 buf += 4;
20523 break;
20524
20525 case DW_FORM_data8:
20526 uint.emplace (read_8_bytes (abfd, buf));
20527 buf += 8;
20528 break;
20529
20530 case DW_FORM_data16:
20531 /* This is used for MD5, but file_entry does not record MD5s. */
20532 buf += 16;
20533 break;
20534
20535 case DW_FORM_udata:
20536 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20537 buf += bytes_read;
20538 break;
20539
20540 case DW_FORM_block:
20541 /* It is valid only for DW_LNCT_timestamp which is ignored by
20542 current GDB. */
20543 break;
20544 }
20545
20546 switch (content_type)
20547 {
20548 case DW_LNCT_path:
20549 if (string.has_value ())
20550 fe.name = *string;
20551 break;
20552 case DW_LNCT_directory_index:
20553 if (uint.has_value ())
20554 fe.d_index = (dir_index) *uint;
20555 break;
20556 case DW_LNCT_timestamp:
20557 if (uint.has_value ())
20558 fe.mod_time = *uint;
20559 break;
20560 case DW_LNCT_size:
20561 if (uint.has_value ())
20562 fe.length = *uint;
20563 break;
20564 case DW_LNCT_MD5:
20565 break;
20566 default:
20567 complaint (_("Unknown format content type %s"),
20568 pulongest (content_type));
20569 }
20570 }
20571
20572 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20573 }
20574
20575 *bufp = buf;
20576 }
20577
20578 /* Read the statement program header starting at OFFSET in
20579 .debug_line, or .debug_line.dwo. Return a pointer
20580 to a struct line_header, allocated using xmalloc.
20581 Returns NULL if there is a problem reading the header, e.g., if it
20582 has a version we don't understand.
20583
20584 NOTE: the strings in the include directory and file name tables of
20585 the returned object point into the dwarf line section buffer,
20586 and must not be freed. */
20587
20588 static line_header_up
20589 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20590 {
20591 const gdb_byte *line_ptr;
20592 unsigned int bytes_read, offset_size;
20593 int i;
20594 const char *cur_dir, *cur_file;
20595 struct dwarf2_section_info *section;
20596 bfd *abfd;
20597 struct dwarf2_per_objfile *dwarf2_per_objfile
20598 = cu->per_cu->dwarf2_per_objfile;
20599
20600 section = get_debug_line_section (cu);
20601 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20602 if (section->buffer == NULL)
20603 {
20604 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20605 complaint (_("missing .debug_line.dwo section"));
20606 else
20607 complaint (_("missing .debug_line section"));
20608 return 0;
20609 }
20610
20611 /* We can't do this until we know the section is non-empty.
20612 Only then do we know we have such a section. */
20613 abfd = get_section_bfd_owner (section);
20614
20615 /* Make sure that at least there's room for the total_length field.
20616 That could be 12 bytes long, but we're just going to fudge that. */
20617 if (to_underlying (sect_off) + 4 >= section->size)
20618 {
20619 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20620 return 0;
20621 }
20622
20623 line_header_up lh (new line_header ());
20624
20625 lh->sect_off = sect_off;
20626 lh->offset_in_dwz = cu->per_cu->is_dwz;
20627
20628 line_ptr = section->buffer + to_underlying (sect_off);
20629
20630 /* Read in the header. */
20631 lh->total_length =
20632 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20633 &bytes_read, &offset_size);
20634 line_ptr += bytes_read;
20635
20636 const gdb_byte *start_here = line_ptr;
20637
20638 if (line_ptr + lh->total_length > (section->buffer + section->size))
20639 {
20640 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20641 return 0;
20642 }
20643 lh->statement_program_end = start_here + lh->total_length;
20644 lh->version = read_2_bytes (abfd, line_ptr);
20645 line_ptr += 2;
20646 if (lh->version > 5)
20647 {
20648 /* This is a version we don't understand. The format could have
20649 changed in ways we don't handle properly so just punt. */
20650 complaint (_("unsupported version in .debug_line section"));
20651 return NULL;
20652 }
20653 if (lh->version >= 5)
20654 {
20655 gdb_byte segment_selector_size;
20656
20657 /* Skip address size. */
20658 read_1_byte (abfd, line_ptr);
20659 line_ptr += 1;
20660
20661 segment_selector_size = read_1_byte (abfd, line_ptr);
20662 line_ptr += 1;
20663 if (segment_selector_size != 0)
20664 {
20665 complaint (_("unsupported segment selector size %u "
20666 "in .debug_line section"),
20667 segment_selector_size);
20668 return NULL;
20669 }
20670 }
20671 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20672 line_ptr += offset_size;
20673 lh->statement_program_start = line_ptr + lh->header_length;
20674 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20675 line_ptr += 1;
20676 if (lh->version >= 4)
20677 {
20678 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20679 line_ptr += 1;
20680 }
20681 else
20682 lh->maximum_ops_per_instruction = 1;
20683
20684 if (lh->maximum_ops_per_instruction == 0)
20685 {
20686 lh->maximum_ops_per_instruction = 1;
20687 complaint (_("invalid maximum_ops_per_instruction "
20688 "in `.debug_line' section"));
20689 }
20690
20691 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20692 line_ptr += 1;
20693 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20694 line_ptr += 1;
20695 lh->line_range = read_1_byte (abfd, line_ptr);
20696 line_ptr += 1;
20697 lh->opcode_base = read_1_byte (abfd, line_ptr);
20698 line_ptr += 1;
20699 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20700
20701 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20702 for (i = 1; i < lh->opcode_base; ++i)
20703 {
20704 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20705 line_ptr += 1;
20706 }
20707
20708 if (lh->version >= 5)
20709 {
20710 /* Read directory table. */
20711 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20712 &cu->header,
20713 [] (struct line_header *header, const char *name,
20714 dir_index d_index, unsigned int mod_time,
20715 unsigned int length)
20716 {
20717 header->add_include_dir (name);
20718 });
20719
20720 /* Read file name table. */
20721 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20722 &cu->header,
20723 [] (struct line_header *header, const char *name,
20724 dir_index d_index, unsigned int mod_time,
20725 unsigned int length)
20726 {
20727 header->add_file_name (name, d_index, mod_time, length);
20728 });
20729 }
20730 else
20731 {
20732 /* Read directory table. */
20733 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20734 {
20735 line_ptr += bytes_read;
20736 lh->add_include_dir (cur_dir);
20737 }
20738 line_ptr += bytes_read;
20739
20740 /* Read file name table. */
20741 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20742 {
20743 unsigned int mod_time, length;
20744 dir_index d_index;
20745
20746 line_ptr += bytes_read;
20747 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20748 line_ptr += bytes_read;
20749 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20750 line_ptr += bytes_read;
20751 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20752 line_ptr += bytes_read;
20753
20754 lh->add_file_name (cur_file, d_index, mod_time, length);
20755 }
20756 line_ptr += bytes_read;
20757 }
20758
20759 if (line_ptr > (section->buffer + section->size))
20760 complaint (_("line number info header doesn't "
20761 "fit in `.debug_line' section"));
20762
20763 return lh;
20764 }
20765
20766 /* Subroutine of dwarf_decode_lines to simplify it.
20767 Return the file name of the psymtab for the given file_entry.
20768 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20769 If space for the result is malloc'd, *NAME_HOLDER will be set.
20770 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20771
20772 static const char *
20773 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20774 const dwarf2_psymtab *pst,
20775 const char *comp_dir,
20776 gdb::unique_xmalloc_ptr<char> *name_holder)
20777 {
20778 const char *include_name = fe.name;
20779 const char *include_name_to_compare = include_name;
20780 const char *pst_filename;
20781 int file_is_pst;
20782
20783 const char *dir_name = fe.include_dir (lh);
20784
20785 gdb::unique_xmalloc_ptr<char> hold_compare;
20786 if (!IS_ABSOLUTE_PATH (include_name)
20787 && (dir_name != NULL || comp_dir != NULL))
20788 {
20789 /* Avoid creating a duplicate psymtab for PST.
20790 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20791 Before we do the comparison, however, we need to account
20792 for DIR_NAME and COMP_DIR.
20793 First prepend dir_name (if non-NULL). If we still don't
20794 have an absolute path prepend comp_dir (if non-NULL).
20795 However, the directory we record in the include-file's
20796 psymtab does not contain COMP_DIR (to match the
20797 corresponding symtab(s)).
20798
20799 Example:
20800
20801 bash$ cd /tmp
20802 bash$ gcc -g ./hello.c
20803 include_name = "hello.c"
20804 dir_name = "."
20805 DW_AT_comp_dir = comp_dir = "/tmp"
20806 DW_AT_name = "./hello.c"
20807
20808 */
20809
20810 if (dir_name != NULL)
20811 {
20812 name_holder->reset (concat (dir_name, SLASH_STRING,
20813 include_name, (char *) NULL));
20814 include_name = name_holder->get ();
20815 include_name_to_compare = include_name;
20816 }
20817 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20818 {
20819 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20820 include_name, (char *) NULL));
20821 include_name_to_compare = hold_compare.get ();
20822 }
20823 }
20824
20825 pst_filename = pst->filename;
20826 gdb::unique_xmalloc_ptr<char> copied_name;
20827 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20828 {
20829 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20830 pst_filename, (char *) NULL));
20831 pst_filename = copied_name.get ();
20832 }
20833
20834 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20835
20836 if (file_is_pst)
20837 return NULL;
20838 return include_name;
20839 }
20840
20841 /* State machine to track the state of the line number program. */
20842
20843 class lnp_state_machine
20844 {
20845 public:
20846 /* Initialize a machine state for the start of a line number
20847 program. */
20848 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20849 bool record_lines_p);
20850
20851 file_entry *current_file ()
20852 {
20853 /* lh->file_names is 0-based, but the file name numbers in the
20854 statement program are 1-based. */
20855 return m_line_header->file_name_at (m_file);
20856 }
20857
20858 /* Record the line in the state machine. END_SEQUENCE is true if
20859 we're processing the end of a sequence. */
20860 void record_line (bool end_sequence);
20861
20862 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20863 nop-out rest of the lines in this sequence. */
20864 void check_line_address (struct dwarf2_cu *cu,
20865 const gdb_byte *line_ptr,
20866 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20867
20868 void handle_set_discriminator (unsigned int discriminator)
20869 {
20870 m_discriminator = discriminator;
20871 m_line_has_non_zero_discriminator |= discriminator != 0;
20872 }
20873
20874 /* Handle DW_LNE_set_address. */
20875 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20876 {
20877 m_op_index = 0;
20878 address += baseaddr;
20879 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20880 }
20881
20882 /* Handle DW_LNS_advance_pc. */
20883 void handle_advance_pc (CORE_ADDR adjust);
20884
20885 /* Handle a special opcode. */
20886 void handle_special_opcode (unsigned char op_code);
20887
20888 /* Handle DW_LNS_advance_line. */
20889 void handle_advance_line (int line_delta)
20890 {
20891 advance_line (line_delta);
20892 }
20893
20894 /* Handle DW_LNS_set_file. */
20895 void handle_set_file (file_name_index file);
20896
20897 /* Handle DW_LNS_negate_stmt. */
20898 void handle_negate_stmt ()
20899 {
20900 m_is_stmt = !m_is_stmt;
20901 }
20902
20903 /* Handle DW_LNS_const_add_pc. */
20904 void handle_const_add_pc ();
20905
20906 /* Handle DW_LNS_fixed_advance_pc. */
20907 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20908 {
20909 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20910 m_op_index = 0;
20911 }
20912
20913 /* Handle DW_LNS_copy. */
20914 void handle_copy ()
20915 {
20916 record_line (false);
20917 m_discriminator = 0;
20918 }
20919
20920 /* Handle DW_LNE_end_sequence. */
20921 void handle_end_sequence ()
20922 {
20923 m_currently_recording_lines = true;
20924 }
20925
20926 private:
20927 /* Advance the line by LINE_DELTA. */
20928 void advance_line (int line_delta)
20929 {
20930 m_line += line_delta;
20931
20932 if (line_delta != 0)
20933 m_line_has_non_zero_discriminator = m_discriminator != 0;
20934 }
20935
20936 struct dwarf2_cu *m_cu;
20937
20938 gdbarch *m_gdbarch;
20939
20940 /* True if we're recording lines.
20941 Otherwise we're building partial symtabs and are just interested in
20942 finding include files mentioned by the line number program. */
20943 bool m_record_lines_p;
20944
20945 /* The line number header. */
20946 line_header *m_line_header;
20947
20948 /* These are part of the standard DWARF line number state machine,
20949 and initialized according to the DWARF spec. */
20950
20951 unsigned char m_op_index = 0;
20952 /* The line table index of the current file. */
20953 file_name_index m_file = 1;
20954 unsigned int m_line = 1;
20955
20956 /* These are initialized in the constructor. */
20957
20958 CORE_ADDR m_address;
20959 bool m_is_stmt;
20960 unsigned int m_discriminator;
20961
20962 /* Additional bits of state we need to track. */
20963
20964 /* The last file that we called dwarf2_start_subfile for.
20965 This is only used for TLLs. */
20966 unsigned int m_last_file = 0;
20967 /* The last file a line number was recorded for. */
20968 struct subfile *m_last_subfile = NULL;
20969
20970 /* When true, record the lines we decode. */
20971 bool m_currently_recording_lines = false;
20972
20973 /* The last line number that was recorded, used to coalesce
20974 consecutive entries for the same line. This can happen, for
20975 example, when discriminators are present. PR 17276. */
20976 unsigned int m_last_line = 0;
20977 bool m_line_has_non_zero_discriminator = false;
20978 };
20979
20980 void
20981 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20982 {
20983 CORE_ADDR addr_adj = (((m_op_index + adjust)
20984 / m_line_header->maximum_ops_per_instruction)
20985 * m_line_header->minimum_instruction_length);
20986 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20987 m_op_index = ((m_op_index + adjust)
20988 % m_line_header->maximum_ops_per_instruction);
20989 }
20990
20991 void
20992 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20993 {
20994 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20995 CORE_ADDR addr_adj = (((m_op_index
20996 + (adj_opcode / m_line_header->line_range))
20997 / m_line_header->maximum_ops_per_instruction)
20998 * m_line_header->minimum_instruction_length);
20999 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21000 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
21001 % m_line_header->maximum_ops_per_instruction);
21002
21003 int line_delta = (m_line_header->line_base
21004 + (adj_opcode % m_line_header->line_range));
21005 advance_line (line_delta);
21006 record_line (false);
21007 m_discriminator = 0;
21008 }
21009
21010 void
21011 lnp_state_machine::handle_set_file (file_name_index file)
21012 {
21013 m_file = file;
21014
21015 const file_entry *fe = current_file ();
21016 if (fe == NULL)
21017 dwarf2_debug_line_missing_file_complaint ();
21018 else if (m_record_lines_p)
21019 {
21020 const char *dir = fe->include_dir (m_line_header);
21021
21022 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21023 m_line_has_non_zero_discriminator = m_discriminator != 0;
21024 dwarf2_start_subfile (m_cu, fe->name, dir);
21025 }
21026 }
21027
21028 void
21029 lnp_state_machine::handle_const_add_pc ()
21030 {
21031 CORE_ADDR adjust
21032 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21033
21034 CORE_ADDR addr_adj
21035 = (((m_op_index + adjust)
21036 / m_line_header->maximum_ops_per_instruction)
21037 * m_line_header->minimum_instruction_length);
21038
21039 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21040 m_op_index = ((m_op_index + adjust)
21041 % m_line_header->maximum_ops_per_instruction);
21042 }
21043
21044 /* Return non-zero if we should add LINE to the line number table.
21045 LINE is the line to add, LAST_LINE is the last line that was added,
21046 LAST_SUBFILE is the subfile for LAST_LINE.
21047 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21048 had a non-zero discriminator.
21049
21050 We have to be careful in the presence of discriminators.
21051 E.g., for this line:
21052
21053 for (i = 0; i < 100000; i++);
21054
21055 clang can emit four line number entries for that one line,
21056 each with a different discriminator.
21057 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21058
21059 However, we want gdb to coalesce all four entries into one.
21060 Otherwise the user could stepi into the middle of the line and
21061 gdb would get confused about whether the pc really was in the
21062 middle of the line.
21063
21064 Things are further complicated by the fact that two consecutive
21065 line number entries for the same line is a heuristic used by gcc
21066 to denote the end of the prologue. So we can't just discard duplicate
21067 entries, we have to be selective about it. The heuristic we use is
21068 that we only collapse consecutive entries for the same line if at least
21069 one of those entries has a non-zero discriminator. PR 17276.
21070
21071 Note: Addresses in the line number state machine can never go backwards
21072 within one sequence, thus this coalescing is ok. */
21073
21074 static int
21075 dwarf_record_line_p (struct dwarf2_cu *cu,
21076 unsigned int line, unsigned int last_line,
21077 int line_has_non_zero_discriminator,
21078 struct subfile *last_subfile)
21079 {
21080 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21081 return 1;
21082 if (line != last_line)
21083 return 1;
21084 /* Same line for the same file that we've seen already.
21085 As a last check, for pr 17276, only record the line if the line
21086 has never had a non-zero discriminator. */
21087 if (!line_has_non_zero_discriminator)
21088 return 1;
21089 return 0;
21090 }
21091
21092 /* Use the CU's builder to record line number LINE beginning at
21093 address ADDRESS in the line table of subfile SUBFILE. */
21094
21095 static void
21096 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21097 unsigned int line, CORE_ADDR address,
21098 struct dwarf2_cu *cu)
21099 {
21100 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21101
21102 if (dwarf_line_debug)
21103 {
21104 fprintf_unfiltered (gdb_stdlog,
21105 "Recording line %u, file %s, address %s\n",
21106 line, lbasename (subfile->name),
21107 paddress (gdbarch, address));
21108 }
21109
21110 if (cu != nullptr)
21111 cu->get_builder ()->record_line (subfile, line, addr);
21112 }
21113
21114 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21115 Mark the end of a set of line number records.
21116 The arguments are the same as for dwarf_record_line_1.
21117 If SUBFILE is NULL the request is ignored. */
21118
21119 static void
21120 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21121 CORE_ADDR address, struct dwarf2_cu *cu)
21122 {
21123 if (subfile == NULL)
21124 return;
21125
21126 if (dwarf_line_debug)
21127 {
21128 fprintf_unfiltered (gdb_stdlog,
21129 "Finishing current line, file %s, address %s\n",
21130 lbasename (subfile->name),
21131 paddress (gdbarch, address));
21132 }
21133
21134 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
21135 }
21136
21137 void
21138 lnp_state_machine::record_line (bool end_sequence)
21139 {
21140 if (dwarf_line_debug)
21141 {
21142 fprintf_unfiltered (gdb_stdlog,
21143 "Processing actual line %u: file %u,"
21144 " address %s, is_stmt %u, discrim %u%s\n",
21145 m_line, m_file,
21146 paddress (m_gdbarch, m_address),
21147 m_is_stmt, m_discriminator,
21148 (end_sequence ? "\t(end sequence)" : ""));
21149 }
21150
21151 file_entry *fe = current_file ();
21152
21153 if (fe == NULL)
21154 dwarf2_debug_line_missing_file_complaint ();
21155 /* For now we ignore lines not starting on an instruction boundary.
21156 But not when processing end_sequence for compatibility with the
21157 previous version of the code. */
21158 else if (m_op_index == 0 || end_sequence)
21159 {
21160 fe->included_p = 1;
21161 if (m_record_lines_p
21162 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
21163 {
21164 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
21165 || end_sequence)
21166 {
21167 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21168 m_currently_recording_lines ? m_cu : nullptr);
21169 }
21170
21171 if (!end_sequence)
21172 {
21173 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21174 m_line_has_non_zero_discriminator,
21175 m_last_subfile))
21176 {
21177 buildsym_compunit *builder = m_cu->get_builder ();
21178 dwarf_record_line_1 (m_gdbarch,
21179 builder->get_current_subfile (),
21180 m_line, m_address,
21181 m_currently_recording_lines ? m_cu : nullptr);
21182 }
21183 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21184 m_last_line = m_line;
21185 }
21186 }
21187 }
21188 }
21189
21190 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21191 line_header *lh, bool record_lines_p)
21192 {
21193 m_cu = cu;
21194 m_gdbarch = arch;
21195 m_record_lines_p = record_lines_p;
21196 m_line_header = lh;
21197
21198 m_currently_recording_lines = true;
21199
21200 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21201 was a line entry for it so that the backend has a chance to adjust it
21202 and also record it in case it needs it. This is currently used by MIPS
21203 code, cf. `mips_adjust_dwarf2_line'. */
21204 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21205 m_is_stmt = lh->default_is_stmt;
21206 m_discriminator = 0;
21207 }
21208
21209 void
21210 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21211 const gdb_byte *line_ptr,
21212 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21213 {
21214 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21215 the pc range of the CU. However, we restrict the test to only ADDRESS
21216 values of zero to preserve GDB's previous behaviour which is to handle
21217 the specific case of a function being GC'd by the linker. */
21218
21219 if (address == 0 && address < unrelocated_lowpc)
21220 {
21221 /* This line table is for a function which has been
21222 GCd by the linker. Ignore it. PR gdb/12528 */
21223
21224 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21225 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21226
21227 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21228 line_offset, objfile_name (objfile));
21229 m_currently_recording_lines = false;
21230 /* Note: m_currently_recording_lines is left as false until we see
21231 DW_LNE_end_sequence. */
21232 }
21233 }
21234
21235 /* Subroutine of dwarf_decode_lines to simplify it.
21236 Process the line number information in LH.
21237 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21238 program in order to set included_p for every referenced header. */
21239
21240 static void
21241 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21242 const int decode_for_pst_p, CORE_ADDR lowpc)
21243 {
21244 const gdb_byte *line_ptr, *extended_end;
21245 const gdb_byte *line_end;
21246 unsigned int bytes_read, extended_len;
21247 unsigned char op_code, extended_op;
21248 CORE_ADDR baseaddr;
21249 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21250 bfd *abfd = objfile->obfd;
21251 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21252 /* True if we're recording line info (as opposed to building partial
21253 symtabs and just interested in finding include files mentioned by
21254 the line number program). */
21255 bool record_lines_p = !decode_for_pst_p;
21256
21257 baseaddr = objfile->text_section_offset ();
21258
21259 line_ptr = lh->statement_program_start;
21260 line_end = lh->statement_program_end;
21261
21262 /* Read the statement sequences until there's nothing left. */
21263 while (line_ptr < line_end)
21264 {
21265 /* The DWARF line number program state machine. Reset the state
21266 machine at the start of each sequence. */
21267 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21268 bool end_sequence = false;
21269
21270 if (record_lines_p)
21271 {
21272 /* Start a subfile for the current file of the state
21273 machine. */
21274 const file_entry *fe = state_machine.current_file ();
21275
21276 if (fe != NULL)
21277 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21278 }
21279
21280 /* Decode the table. */
21281 while (line_ptr < line_end && !end_sequence)
21282 {
21283 op_code = read_1_byte (abfd, line_ptr);
21284 line_ptr += 1;
21285
21286 if (op_code >= lh->opcode_base)
21287 {
21288 /* Special opcode. */
21289 state_machine.handle_special_opcode (op_code);
21290 }
21291 else switch (op_code)
21292 {
21293 case DW_LNS_extended_op:
21294 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21295 &bytes_read);
21296 line_ptr += bytes_read;
21297 extended_end = line_ptr + extended_len;
21298 extended_op = read_1_byte (abfd, line_ptr);
21299 line_ptr += 1;
21300 switch (extended_op)
21301 {
21302 case DW_LNE_end_sequence:
21303 state_machine.handle_end_sequence ();
21304 end_sequence = true;
21305 break;
21306 case DW_LNE_set_address:
21307 {
21308 CORE_ADDR address
21309 = read_address (abfd, line_ptr, cu, &bytes_read);
21310 line_ptr += bytes_read;
21311
21312 state_machine.check_line_address (cu, line_ptr,
21313 lowpc - baseaddr, address);
21314 state_machine.handle_set_address (baseaddr, address);
21315 }
21316 break;
21317 case DW_LNE_define_file:
21318 {
21319 const char *cur_file;
21320 unsigned int mod_time, length;
21321 dir_index dindex;
21322
21323 cur_file = read_direct_string (abfd, line_ptr,
21324 &bytes_read);
21325 line_ptr += bytes_read;
21326 dindex = (dir_index)
21327 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21328 line_ptr += bytes_read;
21329 mod_time =
21330 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21331 line_ptr += bytes_read;
21332 length =
21333 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21334 line_ptr += bytes_read;
21335 lh->add_file_name (cur_file, dindex, mod_time, length);
21336 }
21337 break;
21338 case DW_LNE_set_discriminator:
21339 {
21340 /* The discriminator is not interesting to the
21341 debugger; just ignore it. We still need to
21342 check its value though:
21343 if there are consecutive entries for the same
21344 (non-prologue) line we want to coalesce them.
21345 PR 17276. */
21346 unsigned int discr
21347 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21348 line_ptr += bytes_read;
21349
21350 state_machine.handle_set_discriminator (discr);
21351 }
21352 break;
21353 default:
21354 complaint (_("mangled .debug_line section"));
21355 return;
21356 }
21357 /* Make sure that we parsed the extended op correctly. If e.g.
21358 we expected a different address size than the producer used,
21359 we may have read the wrong number of bytes. */
21360 if (line_ptr != extended_end)
21361 {
21362 complaint (_("mangled .debug_line section"));
21363 return;
21364 }
21365 break;
21366 case DW_LNS_copy:
21367 state_machine.handle_copy ();
21368 break;
21369 case DW_LNS_advance_pc:
21370 {
21371 CORE_ADDR adjust
21372 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21373 line_ptr += bytes_read;
21374
21375 state_machine.handle_advance_pc (adjust);
21376 }
21377 break;
21378 case DW_LNS_advance_line:
21379 {
21380 int line_delta
21381 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21382 line_ptr += bytes_read;
21383
21384 state_machine.handle_advance_line (line_delta);
21385 }
21386 break;
21387 case DW_LNS_set_file:
21388 {
21389 file_name_index file
21390 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21391 &bytes_read);
21392 line_ptr += bytes_read;
21393
21394 state_machine.handle_set_file (file);
21395 }
21396 break;
21397 case DW_LNS_set_column:
21398 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21399 line_ptr += bytes_read;
21400 break;
21401 case DW_LNS_negate_stmt:
21402 state_machine.handle_negate_stmt ();
21403 break;
21404 case DW_LNS_set_basic_block:
21405 break;
21406 /* Add to the address register of the state machine the
21407 address increment value corresponding to special opcode
21408 255. I.e., this value is scaled by the minimum
21409 instruction length since special opcode 255 would have
21410 scaled the increment. */
21411 case DW_LNS_const_add_pc:
21412 state_machine.handle_const_add_pc ();
21413 break;
21414 case DW_LNS_fixed_advance_pc:
21415 {
21416 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21417 line_ptr += 2;
21418
21419 state_machine.handle_fixed_advance_pc (addr_adj);
21420 }
21421 break;
21422 default:
21423 {
21424 /* Unknown standard opcode, ignore it. */
21425 int i;
21426
21427 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21428 {
21429 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21430 line_ptr += bytes_read;
21431 }
21432 }
21433 }
21434 }
21435
21436 if (!end_sequence)
21437 dwarf2_debug_line_missing_end_sequence_complaint ();
21438
21439 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21440 in which case we still finish recording the last line). */
21441 state_machine.record_line (true);
21442 }
21443 }
21444
21445 /* Decode the Line Number Program (LNP) for the given line_header
21446 structure and CU. The actual information extracted and the type
21447 of structures created from the LNP depends on the value of PST.
21448
21449 1. If PST is NULL, then this procedure uses the data from the program
21450 to create all necessary symbol tables, and their linetables.
21451
21452 2. If PST is not NULL, this procedure reads the program to determine
21453 the list of files included by the unit represented by PST, and
21454 builds all the associated partial symbol tables.
21455
21456 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21457 It is used for relative paths in the line table.
21458 NOTE: When processing partial symtabs (pst != NULL),
21459 comp_dir == pst->dirname.
21460
21461 NOTE: It is important that psymtabs have the same file name (via strcmp)
21462 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21463 symtab we don't use it in the name of the psymtabs we create.
21464 E.g. expand_line_sal requires this when finding psymtabs to expand.
21465 A good testcase for this is mb-inline.exp.
21466
21467 LOWPC is the lowest address in CU (or 0 if not known).
21468
21469 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21470 for its PC<->lines mapping information. Otherwise only the filename
21471 table is read in. */
21472
21473 static void
21474 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21475 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21476 CORE_ADDR lowpc, int decode_mapping)
21477 {
21478 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21479 const int decode_for_pst_p = (pst != NULL);
21480
21481 if (decode_mapping)
21482 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21483
21484 if (decode_for_pst_p)
21485 {
21486 /* Now that we're done scanning the Line Header Program, we can
21487 create the psymtab of each included file. */
21488 for (auto &file_entry : lh->file_names ())
21489 if (file_entry.included_p == 1)
21490 {
21491 gdb::unique_xmalloc_ptr<char> name_holder;
21492 const char *include_name =
21493 psymtab_include_file_name (lh, file_entry, pst,
21494 comp_dir, &name_holder);
21495 if (include_name != NULL)
21496 dwarf2_create_include_psymtab (include_name, pst, objfile);
21497 }
21498 }
21499 else
21500 {
21501 /* Make sure a symtab is created for every file, even files
21502 which contain only variables (i.e. no code with associated
21503 line numbers). */
21504 buildsym_compunit *builder = cu->get_builder ();
21505 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21506
21507 for (auto &fe : lh->file_names ())
21508 {
21509 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21510 if (builder->get_current_subfile ()->symtab == NULL)
21511 {
21512 builder->get_current_subfile ()->symtab
21513 = allocate_symtab (cust,
21514 builder->get_current_subfile ()->name);
21515 }
21516 fe.symtab = builder->get_current_subfile ()->symtab;
21517 }
21518 }
21519 }
21520
21521 /* Start a subfile for DWARF. FILENAME is the name of the file and
21522 DIRNAME the name of the source directory which contains FILENAME
21523 or NULL if not known.
21524 This routine tries to keep line numbers from identical absolute and
21525 relative file names in a common subfile.
21526
21527 Using the `list' example from the GDB testsuite, which resides in
21528 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21529 of /srcdir/list0.c yields the following debugging information for list0.c:
21530
21531 DW_AT_name: /srcdir/list0.c
21532 DW_AT_comp_dir: /compdir
21533 files.files[0].name: list0.h
21534 files.files[0].dir: /srcdir
21535 files.files[1].name: list0.c
21536 files.files[1].dir: /srcdir
21537
21538 The line number information for list0.c has to end up in a single
21539 subfile, so that `break /srcdir/list0.c:1' works as expected.
21540 start_subfile will ensure that this happens provided that we pass the
21541 concatenation of files.files[1].dir and files.files[1].name as the
21542 subfile's name. */
21543
21544 static void
21545 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21546 const char *dirname)
21547 {
21548 gdb::unique_xmalloc_ptr<char> copy;
21549
21550 /* In order not to lose the line information directory,
21551 we concatenate it to the filename when it makes sense.
21552 Note that the Dwarf3 standard says (speaking of filenames in line
21553 information): ``The directory index is ignored for file names
21554 that represent full path names''. Thus ignoring dirname in the
21555 `else' branch below isn't an issue. */
21556
21557 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21558 {
21559 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21560 filename = copy.get ();
21561 }
21562
21563 cu->get_builder ()->start_subfile (filename);
21564 }
21565
21566 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21567 buildsym_compunit constructor. */
21568
21569 struct compunit_symtab *
21570 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21571 CORE_ADDR low_pc)
21572 {
21573 gdb_assert (m_builder == nullptr);
21574
21575 m_builder.reset (new struct buildsym_compunit
21576 (per_cu->dwarf2_per_objfile->objfile,
21577 name, comp_dir, language, low_pc));
21578
21579 list_in_scope = get_builder ()->get_file_symbols ();
21580
21581 get_builder ()->record_debugformat ("DWARF 2");
21582 get_builder ()->record_producer (producer);
21583
21584 processing_has_namespace_info = false;
21585
21586 return get_builder ()->get_compunit_symtab ();
21587 }
21588
21589 static void
21590 var_decode_location (struct attribute *attr, struct symbol *sym,
21591 struct dwarf2_cu *cu)
21592 {
21593 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21594 struct comp_unit_head *cu_header = &cu->header;
21595
21596 /* NOTE drow/2003-01-30: There used to be a comment and some special
21597 code here to turn a symbol with DW_AT_external and a
21598 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21599 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21600 with some versions of binutils) where shared libraries could have
21601 relocations against symbols in their debug information - the
21602 minimal symbol would have the right address, but the debug info
21603 would not. It's no longer necessary, because we will explicitly
21604 apply relocations when we read in the debug information now. */
21605
21606 /* A DW_AT_location attribute with no contents indicates that a
21607 variable has been optimized away. */
21608 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21609 {
21610 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21611 return;
21612 }
21613
21614 /* Handle one degenerate form of location expression specially, to
21615 preserve GDB's previous behavior when section offsets are
21616 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21617 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21618
21619 if (attr_form_is_block (attr)
21620 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21621 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21622 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21623 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21624 && (DW_BLOCK (attr)->size
21625 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21626 {
21627 unsigned int dummy;
21628
21629 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21630 SET_SYMBOL_VALUE_ADDRESS (sym,
21631 read_address (objfile->obfd,
21632 DW_BLOCK (attr)->data + 1,
21633 cu, &dummy));
21634 else
21635 SET_SYMBOL_VALUE_ADDRESS
21636 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21637 &dummy));
21638 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21639 fixup_symbol_section (sym, objfile);
21640 SET_SYMBOL_VALUE_ADDRESS
21641 (sym,
21642 SYMBOL_VALUE_ADDRESS (sym)
21643 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
21644 return;
21645 }
21646
21647 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21648 expression evaluator, and use LOC_COMPUTED only when necessary
21649 (i.e. when the value of a register or memory location is
21650 referenced, or a thread-local block, etc.). Then again, it might
21651 not be worthwhile. I'm assuming that it isn't unless performance
21652 or memory numbers show me otherwise. */
21653
21654 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21655
21656 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21657 cu->has_loclist = true;
21658 }
21659
21660 /* Given a pointer to a DWARF information entry, figure out if we need
21661 to make a symbol table entry for it, and if so, create a new entry
21662 and return a pointer to it.
21663 If TYPE is NULL, determine symbol type from the die, otherwise
21664 used the passed type.
21665 If SPACE is not NULL, use it to hold the new symbol. If it is
21666 NULL, allocate a new symbol on the objfile's obstack. */
21667
21668 static struct symbol *
21669 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21670 struct symbol *space)
21671 {
21672 struct dwarf2_per_objfile *dwarf2_per_objfile
21673 = cu->per_cu->dwarf2_per_objfile;
21674 struct objfile *objfile = dwarf2_per_objfile->objfile;
21675 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21676 struct symbol *sym = NULL;
21677 const char *name;
21678 struct attribute *attr = NULL;
21679 struct attribute *attr2 = NULL;
21680 CORE_ADDR baseaddr;
21681 struct pending **list_to_add = NULL;
21682
21683 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21684
21685 baseaddr = objfile->text_section_offset ();
21686
21687 name = dwarf2_name (die, cu);
21688 if (name)
21689 {
21690 const char *linkagename;
21691 int suppress_add = 0;
21692
21693 if (space)
21694 sym = space;
21695 else
21696 sym = allocate_symbol (objfile);
21697 OBJSTAT (objfile, n_syms++);
21698
21699 /* Cache this symbol's name and the name's demangled form (if any). */
21700 sym->set_language (cu->language, &objfile->objfile_obstack);
21701 linkagename = dwarf2_physname (name, die, cu);
21702 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
21703
21704 /* Fortran does not have mangling standard and the mangling does differ
21705 between gfortran, iFort etc. */
21706 if (cu->language == language_fortran
21707 && symbol_get_demangled_name (sym) == NULL)
21708 symbol_set_demangled_name (sym,
21709 dwarf2_full_name (name, die, cu),
21710 NULL);
21711
21712 /* Default assumptions.
21713 Use the passed type or decode it from the die. */
21714 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21715 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21716 if (type != NULL)
21717 SYMBOL_TYPE (sym) = type;
21718 else
21719 SYMBOL_TYPE (sym) = die_type (die, cu);
21720 attr = dwarf2_attr (die,
21721 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21722 cu);
21723 if (attr != nullptr)
21724 {
21725 SYMBOL_LINE (sym) = DW_UNSND (attr);
21726 }
21727
21728 attr = dwarf2_attr (die,
21729 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21730 cu);
21731 if (attr != nullptr)
21732 {
21733 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21734 struct file_entry *fe;
21735
21736 if (cu->line_header != NULL)
21737 fe = cu->line_header->file_name_at (file_index);
21738 else
21739 fe = NULL;
21740
21741 if (fe == NULL)
21742 complaint (_("file index out of range"));
21743 else
21744 symbol_set_symtab (sym, fe->symtab);
21745 }
21746
21747 switch (die->tag)
21748 {
21749 case DW_TAG_label:
21750 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21751 if (attr != nullptr)
21752 {
21753 CORE_ADDR addr;
21754
21755 addr = attr_value_as_address (attr);
21756 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21757 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21758 }
21759 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21760 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21761 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21762 add_symbol_to_list (sym, cu->list_in_scope);
21763 break;
21764 case DW_TAG_subprogram:
21765 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21766 finish_block. */
21767 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21768 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21769 if ((attr2 && (DW_UNSND (attr2) != 0))
21770 || cu->language == language_ada
21771 || cu->language == language_fortran)
21772 {
21773 /* Subprograms marked external are stored as a global symbol.
21774 Ada and Fortran subprograms, whether marked external or
21775 not, are always stored as a global symbol, because we want
21776 to be able to access them globally. For instance, we want
21777 to be able to break on a nested subprogram without having
21778 to specify the context. */
21779 list_to_add = cu->get_builder ()->get_global_symbols ();
21780 }
21781 else
21782 {
21783 list_to_add = cu->list_in_scope;
21784 }
21785 break;
21786 case DW_TAG_inlined_subroutine:
21787 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21788 finish_block. */
21789 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21790 SYMBOL_INLINED (sym) = 1;
21791 list_to_add = cu->list_in_scope;
21792 break;
21793 case DW_TAG_template_value_param:
21794 suppress_add = 1;
21795 /* Fall through. */
21796 case DW_TAG_constant:
21797 case DW_TAG_variable:
21798 case DW_TAG_member:
21799 /* Compilation with minimal debug info may result in
21800 variables with missing type entries. Change the
21801 misleading `void' type to something sensible. */
21802 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21803 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21804
21805 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21806 /* In the case of DW_TAG_member, we should only be called for
21807 static const members. */
21808 if (die->tag == DW_TAG_member)
21809 {
21810 /* dwarf2_add_field uses die_is_declaration,
21811 so we do the same. */
21812 gdb_assert (die_is_declaration (die, cu));
21813 gdb_assert (attr);
21814 }
21815 if (attr != nullptr)
21816 {
21817 dwarf2_const_value (attr, sym, cu);
21818 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21819 if (!suppress_add)
21820 {
21821 if (attr2 && (DW_UNSND (attr2) != 0))
21822 list_to_add = cu->get_builder ()->get_global_symbols ();
21823 else
21824 list_to_add = cu->list_in_scope;
21825 }
21826 break;
21827 }
21828 attr = dwarf2_attr (die, DW_AT_location, cu);
21829 if (attr != nullptr)
21830 {
21831 var_decode_location (attr, sym, cu);
21832 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21833
21834 /* Fortran explicitly imports any global symbols to the local
21835 scope by DW_TAG_common_block. */
21836 if (cu->language == language_fortran && die->parent
21837 && die->parent->tag == DW_TAG_common_block)
21838 attr2 = NULL;
21839
21840 if (SYMBOL_CLASS (sym) == LOC_STATIC
21841 && SYMBOL_VALUE_ADDRESS (sym) == 0
21842 && !dwarf2_per_objfile->has_section_at_zero)
21843 {
21844 /* When a static variable is eliminated by the linker,
21845 the corresponding debug information is not stripped
21846 out, but the variable address is set to null;
21847 do not add such variables into symbol table. */
21848 }
21849 else if (attr2 && (DW_UNSND (attr2) != 0))
21850 {
21851 if (SYMBOL_CLASS (sym) == LOC_STATIC
21852 && (objfile->flags & OBJF_MAINLINE) == 0
21853 && dwarf2_per_objfile->can_copy)
21854 {
21855 /* A global static variable might be subject to
21856 copy relocation. We first check for a local
21857 minsym, though, because maybe the symbol was
21858 marked hidden, in which case this would not
21859 apply. */
21860 bound_minimal_symbol found
21861 = (lookup_minimal_symbol_linkage
21862 (sym->linkage_name (), objfile));
21863 if (found.minsym != nullptr)
21864 sym->maybe_copied = 1;
21865 }
21866
21867 /* A variable with DW_AT_external is never static,
21868 but it may be block-scoped. */
21869 list_to_add
21870 = ((cu->list_in_scope
21871 == cu->get_builder ()->get_file_symbols ())
21872 ? cu->get_builder ()->get_global_symbols ()
21873 : cu->list_in_scope);
21874 }
21875 else
21876 list_to_add = cu->list_in_scope;
21877 }
21878 else
21879 {
21880 /* We do not know the address of this symbol.
21881 If it is an external symbol and we have type information
21882 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21883 The address of the variable will then be determined from
21884 the minimal symbol table whenever the variable is
21885 referenced. */
21886 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21887
21888 /* Fortran explicitly imports any global symbols to the local
21889 scope by DW_TAG_common_block. */
21890 if (cu->language == language_fortran && die->parent
21891 && die->parent->tag == DW_TAG_common_block)
21892 {
21893 /* SYMBOL_CLASS doesn't matter here because
21894 read_common_block is going to reset it. */
21895 if (!suppress_add)
21896 list_to_add = cu->list_in_scope;
21897 }
21898 else if (attr2 && (DW_UNSND (attr2) != 0)
21899 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21900 {
21901 /* A variable with DW_AT_external is never static, but it
21902 may be block-scoped. */
21903 list_to_add
21904 = ((cu->list_in_scope
21905 == cu->get_builder ()->get_file_symbols ())
21906 ? cu->get_builder ()->get_global_symbols ()
21907 : cu->list_in_scope);
21908
21909 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21910 }
21911 else if (!die_is_declaration (die, cu))
21912 {
21913 /* Use the default LOC_OPTIMIZED_OUT class. */
21914 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21915 if (!suppress_add)
21916 list_to_add = cu->list_in_scope;
21917 }
21918 }
21919 break;
21920 case DW_TAG_formal_parameter:
21921 {
21922 /* If we are inside a function, mark this as an argument. If
21923 not, we might be looking at an argument to an inlined function
21924 when we do not have enough information to show inlined frames;
21925 pretend it's a local variable in that case so that the user can
21926 still see it. */
21927 struct context_stack *curr
21928 = cu->get_builder ()->get_current_context_stack ();
21929 if (curr != nullptr && curr->name != nullptr)
21930 SYMBOL_IS_ARGUMENT (sym) = 1;
21931 attr = dwarf2_attr (die, DW_AT_location, cu);
21932 if (attr != nullptr)
21933 {
21934 var_decode_location (attr, sym, cu);
21935 }
21936 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21937 if (attr != nullptr)
21938 {
21939 dwarf2_const_value (attr, sym, cu);
21940 }
21941
21942 list_to_add = cu->list_in_scope;
21943 }
21944 break;
21945 case DW_TAG_unspecified_parameters:
21946 /* From varargs functions; gdb doesn't seem to have any
21947 interest in this information, so just ignore it for now.
21948 (FIXME?) */
21949 break;
21950 case DW_TAG_template_type_param:
21951 suppress_add = 1;
21952 /* Fall through. */
21953 case DW_TAG_class_type:
21954 case DW_TAG_interface_type:
21955 case DW_TAG_structure_type:
21956 case DW_TAG_union_type:
21957 case DW_TAG_set_type:
21958 case DW_TAG_enumeration_type:
21959 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21960 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21961
21962 {
21963 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21964 really ever be static objects: otherwise, if you try
21965 to, say, break of a class's method and you're in a file
21966 which doesn't mention that class, it won't work unless
21967 the check for all static symbols in lookup_symbol_aux
21968 saves you. See the OtherFileClass tests in
21969 gdb.c++/namespace.exp. */
21970
21971 if (!suppress_add)
21972 {
21973 buildsym_compunit *builder = cu->get_builder ();
21974 list_to_add
21975 = (cu->list_in_scope == builder->get_file_symbols ()
21976 && cu->language == language_cplus
21977 ? builder->get_global_symbols ()
21978 : cu->list_in_scope);
21979
21980 /* The semantics of C++ state that "struct foo {
21981 ... }" also defines a typedef for "foo". */
21982 if (cu->language == language_cplus
21983 || cu->language == language_ada
21984 || cu->language == language_d
21985 || cu->language == language_rust)
21986 {
21987 /* The symbol's name is already allocated along
21988 with this objfile, so we don't need to
21989 duplicate it for the type. */
21990 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21991 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
21992 }
21993 }
21994 }
21995 break;
21996 case DW_TAG_typedef:
21997 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21998 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21999 list_to_add = cu->list_in_scope;
22000 break;
22001 case DW_TAG_base_type:
22002 case DW_TAG_subrange_type:
22003 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22004 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22005 list_to_add = cu->list_in_scope;
22006 break;
22007 case DW_TAG_enumerator:
22008 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22009 if (attr != nullptr)
22010 {
22011 dwarf2_const_value (attr, sym, cu);
22012 }
22013 {
22014 /* NOTE: carlton/2003-11-10: See comment above in the
22015 DW_TAG_class_type, etc. block. */
22016
22017 list_to_add
22018 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22019 && cu->language == language_cplus
22020 ? cu->get_builder ()->get_global_symbols ()
22021 : cu->list_in_scope);
22022 }
22023 break;
22024 case DW_TAG_imported_declaration:
22025 case DW_TAG_namespace:
22026 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22027 list_to_add = cu->get_builder ()->get_global_symbols ();
22028 break;
22029 case DW_TAG_module:
22030 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22031 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22032 list_to_add = cu->get_builder ()->get_global_symbols ();
22033 break;
22034 case DW_TAG_common_block:
22035 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22036 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22037 add_symbol_to_list (sym, cu->list_in_scope);
22038 break;
22039 default:
22040 /* Not a tag we recognize. Hopefully we aren't processing
22041 trash data, but since we must specifically ignore things
22042 we don't recognize, there is nothing else we should do at
22043 this point. */
22044 complaint (_("unsupported tag: '%s'"),
22045 dwarf_tag_name (die->tag));
22046 break;
22047 }
22048
22049 if (suppress_add)
22050 {
22051 sym->hash_next = objfile->template_symbols;
22052 objfile->template_symbols = sym;
22053 list_to_add = NULL;
22054 }
22055
22056 if (list_to_add != NULL)
22057 add_symbol_to_list (sym, list_to_add);
22058
22059 /* For the benefit of old versions of GCC, check for anonymous
22060 namespaces based on the demangled name. */
22061 if (!cu->processing_has_namespace_info
22062 && cu->language == language_cplus)
22063 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22064 }
22065 return (sym);
22066 }
22067
22068 /* Given an attr with a DW_FORM_dataN value in host byte order,
22069 zero-extend it as appropriate for the symbol's type. The DWARF
22070 standard (v4) is not entirely clear about the meaning of using
22071 DW_FORM_dataN for a constant with a signed type, where the type is
22072 wider than the data. The conclusion of a discussion on the DWARF
22073 list was that this is unspecified. We choose to always zero-extend
22074 because that is the interpretation long in use by GCC. */
22075
22076 static gdb_byte *
22077 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22078 struct dwarf2_cu *cu, LONGEST *value, int bits)
22079 {
22080 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22081 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22082 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22083 LONGEST l = DW_UNSND (attr);
22084
22085 if (bits < sizeof (*value) * 8)
22086 {
22087 l &= ((LONGEST) 1 << bits) - 1;
22088 *value = l;
22089 }
22090 else if (bits == sizeof (*value) * 8)
22091 *value = l;
22092 else
22093 {
22094 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22095 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22096 return bytes;
22097 }
22098
22099 return NULL;
22100 }
22101
22102 /* Read a constant value from an attribute. Either set *VALUE, or if
22103 the value does not fit in *VALUE, set *BYTES - either already
22104 allocated on the objfile obstack, or newly allocated on OBSTACK,
22105 or, set *BATON, if we translated the constant to a location
22106 expression. */
22107
22108 static void
22109 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22110 const char *name, struct obstack *obstack,
22111 struct dwarf2_cu *cu,
22112 LONGEST *value, const gdb_byte **bytes,
22113 struct dwarf2_locexpr_baton **baton)
22114 {
22115 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22116 struct comp_unit_head *cu_header = &cu->header;
22117 struct dwarf_block *blk;
22118 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22119 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22120
22121 *value = 0;
22122 *bytes = NULL;
22123 *baton = NULL;
22124
22125 switch (attr->form)
22126 {
22127 case DW_FORM_addr:
22128 case DW_FORM_addrx:
22129 case DW_FORM_GNU_addr_index:
22130 {
22131 gdb_byte *data;
22132
22133 if (TYPE_LENGTH (type) != cu_header->addr_size)
22134 dwarf2_const_value_length_mismatch_complaint (name,
22135 cu_header->addr_size,
22136 TYPE_LENGTH (type));
22137 /* Symbols of this form are reasonably rare, so we just
22138 piggyback on the existing location code rather than writing
22139 a new implementation of symbol_computed_ops. */
22140 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22141 (*baton)->per_cu = cu->per_cu;
22142 gdb_assert ((*baton)->per_cu);
22143
22144 (*baton)->size = 2 + cu_header->addr_size;
22145 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22146 (*baton)->data = data;
22147
22148 data[0] = DW_OP_addr;
22149 store_unsigned_integer (&data[1], cu_header->addr_size,
22150 byte_order, DW_ADDR (attr));
22151 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22152 }
22153 break;
22154 case DW_FORM_string:
22155 case DW_FORM_strp:
22156 case DW_FORM_strx:
22157 case DW_FORM_GNU_str_index:
22158 case DW_FORM_GNU_strp_alt:
22159 /* DW_STRING is already allocated on the objfile obstack, point
22160 directly to it. */
22161 *bytes = (const gdb_byte *) DW_STRING (attr);
22162 break;
22163 case DW_FORM_block1:
22164 case DW_FORM_block2:
22165 case DW_FORM_block4:
22166 case DW_FORM_block:
22167 case DW_FORM_exprloc:
22168 case DW_FORM_data16:
22169 blk = DW_BLOCK (attr);
22170 if (TYPE_LENGTH (type) != blk->size)
22171 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22172 TYPE_LENGTH (type));
22173 *bytes = blk->data;
22174 break;
22175
22176 /* The DW_AT_const_value attributes are supposed to carry the
22177 symbol's value "represented as it would be on the target
22178 architecture." By the time we get here, it's already been
22179 converted to host endianness, so we just need to sign- or
22180 zero-extend it as appropriate. */
22181 case DW_FORM_data1:
22182 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22183 break;
22184 case DW_FORM_data2:
22185 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22186 break;
22187 case DW_FORM_data4:
22188 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22189 break;
22190 case DW_FORM_data8:
22191 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22192 break;
22193
22194 case DW_FORM_sdata:
22195 case DW_FORM_implicit_const:
22196 *value = DW_SND (attr);
22197 break;
22198
22199 case DW_FORM_udata:
22200 *value = DW_UNSND (attr);
22201 break;
22202
22203 default:
22204 complaint (_("unsupported const value attribute form: '%s'"),
22205 dwarf_form_name (attr->form));
22206 *value = 0;
22207 break;
22208 }
22209 }
22210
22211
22212 /* Copy constant value from an attribute to a symbol. */
22213
22214 static void
22215 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22216 struct dwarf2_cu *cu)
22217 {
22218 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22219 LONGEST value;
22220 const gdb_byte *bytes;
22221 struct dwarf2_locexpr_baton *baton;
22222
22223 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22224 sym->print_name (),
22225 &objfile->objfile_obstack, cu,
22226 &value, &bytes, &baton);
22227
22228 if (baton != NULL)
22229 {
22230 SYMBOL_LOCATION_BATON (sym) = baton;
22231 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22232 }
22233 else if (bytes != NULL)
22234 {
22235 SYMBOL_VALUE_BYTES (sym) = bytes;
22236 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22237 }
22238 else
22239 {
22240 SYMBOL_VALUE (sym) = value;
22241 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22242 }
22243 }
22244
22245 /* Return the type of the die in question using its DW_AT_type attribute. */
22246
22247 static struct type *
22248 die_type (struct die_info *die, struct dwarf2_cu *cu)
22249 {
22250 struct attribute *type_attr;
22251
22252 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22253 if (!type_attr)
22254 {
22255 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22256 /* A missing DW_AT_type represents a void type. */
22257 return objfile_type (objfile)->builtin_void;
22258 }
22259
22260 return lookup_die_type (die, type_attr, cu);
22261 }
22262
22263 /* True iff CU's producer generates GNAT Ada auxiliary information
22264 that allows to find parallel types through that information instead
22265 of having to do expensive parallel lookups by type name. */
22266
22267 static int
22268 need_gnat_info (struct dwarf2_cu *cu)
22269 {
22270 /* Assume that the Ada compiler was GNAT, which always produces
22271 the auxiliary information. */
22272 return (cu->language == language_ada);
22273 }
22274
22275 /* Return the auxiliary type of the die in question using its
22276 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22277 attribute is not present. */
22278
22279 static struct type *
22280 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22281 {
22282 struct attribute *type_attr;
22283
22284 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22285 if (!type_attr)
22286 return NULL;
22287
22288 return lookup_die_type (die, type_attr, cu);
22289 }
22290
22291 /* If DIE has a descriptive_type attribute, then set the TYPE's
22292 descriptive type accordingly. */
22293
22294 static void
22295 set_descriptive_type (struct type *type, struct die_info *die,
22296 struct dwarf2_cu *cu)
22297 {
22298 struct type *descriptive_type = die_descriptive_type (die, cu);
22299
22300 if (descriptive_type)
22301 {
22302 ALLOCATE_GNAT_AUX_TYPE (type);
22303 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22304 }
22305 }
22306
22307 /* Return the containing type of the die in question using its
22308 DW_AT_containing_type attribute. */
22309
22310 static struct type *
22311 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22312 {
22313 struct attribute *type_attr;
22314 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22315
22316 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22317 if (!type_attr)
22318 error (_("Dwarf Error: Problem turning containing type into gdb type "
22319 "[in module %s]"), objfile_name (objfile));
22320
22321 return lookup_die_type (die, type_attr, cu);
22322 }
22323
22324 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22325
22326 static struct type *
22327 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22328 {
22329 struct dwarf2_per_objfile *dwarf2_per_objfile
22330 = cu->per_cu->dwarf2_per_objfile;
22331 struct objfile *objfile = dwarf2_per_objfile->objfile;
22332 char *saved;
22333
22334 std::string message
22335 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22336 objfile_name (objfile),
22337 sect_offset_str (cu->header.sect_off),
22338 sect_offset_str (die->sect_off));
22339 saved = obstack_strdup (&objfile->objfile_obstack, message);
22340
22341 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22342 }
22343
22344 /* Look up the type of DIE in CU using its type attribute ATTR.
22345 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22346 DW_AT_containing_type.
22347 If there is no type substitute an error marker. */
22348
22349 static struct type *
22350 lookup_die_type (struct die_info *die, const struct attribute *attr,
22351 struct dwarf2_cu *cu)
22352 {
22353 struct dwarf2_per_objfile *dwarf2_per_objfile
22354 = cu->per_cu->dwarf2_per_objfile;
22355 struct objfile *objfile = dwarf2_per_objfile->objfile;
22356 struct type *this_type;
22357
22358 gdb_assert (attr->name == DW_AT_type
22359 || attr->name == DW_AT_GNAT_descriptive_type
22360 || attr->name == DW_AT_containing_type);
22361
22362 /* First see if we have it cached. */
22363
22364 if (attr->form == DW_FORM_GNU_ref_alt)
22365 {
22366 struct dwarf2_per_cu_data *per_cu;
22367 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22368
22369 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22370 dwarf2_per_objfile);
22371 this_type = get_die_type_at_offset (sect_off, per_cu);
22372 }
22373 else if (attr_form_is_ref (attr))
22374 {
22375 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22376
22377 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22378 }
22379 else if (attr->form == DW_FORM_ref_sig8)
22380 {
22381 ULONGEST signature = DW_SIGNATURE (attr);
22382
22383 return get_signatured_type (die, signature, cu);
22384 }
22385 else
22386 {
22387 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22388 " at %s [in module %s]"),
22389 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22390 objfile_name (objfile));
22391 return build_error_marker_type (cu, die);
22392 }
22393
22394 /* If not cached we need to read it in. */
22395
22396 if (this_type == NULL)
22397 {
22398 struct die_info *type_die = NULL;
22399 struct dwarf2_cu *type_cu = cu;
22400
22401 if (attr_form_is_ref (attr))
22402 type_die = follow_die_ref (die, attr, &type_cu);
22403 if (type_die == NULL)
22404 return build_error_marker_type (cu, die);
22405 /* If we find the type now, it's probably because the type came
22406 from an inter-CU reference and the type's CU got expanded before
22407 ours. */
22408 this_type = read_type_die (type_die, type_cu);
22409 }
22410
22411 /* If we still don't have a type use an error marker. */
22412
22413 if (this_type == NULL)
22414 return build_error_marker_type (cu, die);
22415
22416 return this_type;
22417 }
22418
22419 /* Return the type in DIE, CU.
22420 Returns NULL for invalid types.
22421
22422 This first does a lookup in die_type_hash,
22423 and only reads the die in if necessary.
22424
22425 NOTE: This can be called when reading in partial or full symbols. */
22426
22427 static struct type *
22428 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22429 {
22430 struct type *this_type;
22431
22432 this_type = get_die_type (die, cu);
22433 if (this_type)
22434 return this_type;
22435
22436 return read_type_die_1 (die, cu);
22437 }
22438
22439 /* Read the type in DIE, CU.
22440 Returns NULL for invalid types. */
22441
22442 static struct type *
22443 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22444 {
22445 struct type *this_type = NULL;
22446
22447 switch (die->tag)
22448 {
22449 case DW_TAG_class_type:
22450 case DW_TAG_interface_type:
22451 case DW_TAG_structure_type:
22452 case DW_TAG_union_type:
22453 this_type = read_structure_type (die, cu);
22454 break;
22455 case DW_TAG_enumeration_type:
22456 this_type = read_enumeration_type (die, cu);
22457 break;
22458 case DW_TAG_subprogram:
22459 case DW_TAG_subroutine_type:
22460 case DW_TAG_inlined_subroutine:
22461 this_type = read_subroutine_type (die, cu);
22462 break;
22463 case DW_TAG_array_type:
22464 this_type = read_array_type (die, cu);
22465 break;
22466 case DW_TAG_set_type:
22467 this_type = read_set_type (die, cu);
22468 break;
22469 case DW_TAG_pointer_type:
22470 this_type = read_tag_pointer_type (die, cu);
22471 break;
22472 case DW_TAG_ptr_to_member_type:
22473 this_type = read_tag_ptr_to_member_type (die, cu);
22474 break;
22475 case DW_TAG_reference_type:
22476 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22477 break;
22478 case DW_TAG_rvalue_reference_type:
22479 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22480 break;
22481 case DW_TAG_const_type:
22482 this_type = read_tag_const_type (die, cu);
22483 break;
22484 case DW_TAG_volatile_type:
22485 this_type = read_tag_volatile_type (die, cu);
22486 break;
22487 case DW_TAG_restrict_type:
22488 this_type = read_tag_restrict_type (die, cu);
22489 break;
22490 case DW_TAG_string_type:
22491 this_type = read_tag_string_type (die, cu);
22492 break;
22493 case DW_TAG_typedef:
22494 this_type = read_typedef (die, cu);
22495 break;
22496 case DW_TAG_subrange_type:
22497 this_type = read_subrange_type (die, cu);
22498 break;
22499 case DW_TAG_base_type:
22500 this_type = read_base_type (die, cu);
22501 break;
22502 case DW_TAG_unspecified_type:
22503 this_type = read_unspecified_type (die, cu);
22504 break;
22505 case DW_TAG_namespace:
22506 this_type = read_namespace_type (die, cu);
22507 break;
22508 case DW_TAG_module:
22509 this_type = read_module_type (die, cu);
22510 break;
22511 case DW_TAG_atomic_type:
22512 this_type = read_tag_atomic_type (die, cu);
22513 break;
22514 default:
22515 complaint (_("unexpected tag in read_type_die: '%s'"),
22516 dwarf_tag_name (die->tag));
22517 break;
22518 }
22519
22520 return this_type;
22521 }
22522
22523 /* See if we can figure out if the class lives in a namespace. We do
22524 this by looking for a member function; its demangled name will
22525 contain namespace info, if there is any.
22526 Return the computed name or NULL.
22527 Space for the result is allocated on the objfile's obstack.
22528 This is the full-die version of guess_partial_die_structure_name.
22529 In this case we know DIE has no useful parent. */
22530
22531 static const char *
22532 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22533 {
22534 struct die_info *spec_die;
22535 struct dwarf2_cu *spec_cu;
22536 struct die_info *child;
22537 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22538
22539 spec_cu = cu;
22540 spec_die = die_specification (die, &spec_cu);
22541 if (spec_die != NULL)
22542 {
22543 die = spec_die;
22544 cu = spec_cu;
22545 }
22546
22547 for (child = die->child;
22548 child != NULL;
22549 child = child->sibling)
22550 {
22551 if (child->tag == DW_TAG_subprogram)
22552 {
22553 const char *linkage_name = dw2_linkage_name (child, cu);
22554
22555 if (linkage_name != NULL)
22556 {
22557 gdb::unique_xmalloc_ptr<char> actual_name
22558 (language_class_name_from_physname (cu->language_defn,
22559 linkage_name));
22560 const char *name = NULL;
22561
22562 if (actual_name != NULL)
22563 {
22564 const char *die_name = dwarf2_name (die, cu);
22565
22566 if (die_name != NULL
22567 && strcmp (die_name, actual_name.get ()) != 0)
22568 {
22569 /* Strip off the class name from the full name.
22570 We want the prefix. */
22571 int die_name_len = strlen (die_name);
22572 int actual_name_len = strlen (actual_name.get ());
22573 const char *ptr = actual_name.get ();
22574
22575 /* Test for '::' as a sanity check. */
22576 if (actual_name_len > die_name_len + 2
22577 && ptr[actual_name_len - die_name_len - 1] == ':')
22578 name = obstack_strndup (
22579 &objfile->per_bfd->storage_obstack,
22580 ptr, actual_name_len - die_name_len - 2);
22581 }
22582 }
22583 return name;
22584 }
22585 }
22586 }
22587
22588 return NULL;
22589 }
22590
22591 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22592 prefix part in such case. See
22593 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22594
22595 static const char *
22596 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22597 {
22598 struct attribute *attr;
22599 const char *base;
22600
22601 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22602 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22603 return NULL;
22604
22605 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22606 return NULL;
22607
22608 attr = dw2_linkage_name_attr (die, cu);
22609 if (attr == NULL || DW_STRING (attr) == NULL)
22610 return NULL;
22611
22612 /* dwarf2_name had to be already called. */
22613 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22614
22615 /* Strip the base name, keep any leading namespaces/classes. */
22616 base = strrchr (DW_STRING (attr), ':');
22617 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22618 return "";
22619
22620 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22621 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22622 DW_STRING (attr),
22623 &base[-1] - DW_STRING (attr));
22624 }
22625
22626 /* Return the name of the namespace/class that DIE is defined within,
22627 or "" if we can't tell. The caller should not xfree the result.
22628
22629 For example, if we're within the method foo() in the following
22630 code:
22631
22632 namespace N {
22633 class C {
22634 void foo () {
22635 }
22636 };
22637 }
22638
22639 then determine_prefix on foo's die will return "N::C". */
22640
22641 static const char *
22642 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22643 {
22644 struct dwarf2_per_objfile *dwarf2_per_objfile
22645 = cu->per_cu->dwarf2_per_objfile;
22646 struct die_info *parent, *spec_die;
22647 struct dwarf2_cu *spec_cu;
22648 struct type *parent_type;
22649 const char *retval;
22650
22651 if (cu->language != language_cplus
22652 && cu->language != language_fortran && cu->language != language_d
22653 && cu->language != language_rust)
22654 return "";
22655
22656 retval = anonymous_struct_prefix (die, cu);
22657 if (retval)
22658 return retval;
22659
22660 /* We have to be careful in the presence of DW_AT_specification.
22661 For example, with GCC 3.4, given the code
22662
22663 namespace N {
22664 void foo() {
22665 // Definition of N::foo.
22666 }
22667 }
22668
22669 then we'll have a tree of DIEs like this:
22670
22671 1: DW_TAG_compile_unit
22672 2: DW_TAG_namespace // N
22673 3: DW_TAG_subprogram // declaration of N::foo
22674 4: DW_TAG_subprogram // definition of N::foo
22675 DW_AT_specification // refers to die #3
22676
22677 Thus, when processing die #4, we have to pretend that we're in
22678 the context of its DW_AT_specification, namely the contex of die
22679 #3. */
22680 spec_cu = cu;
22681 spec_die = die_specification (die, &spec_cu);
22682 if (spec_die == NULL)
22683 parent = die->parent;
22684 else
22685 {
22686 parent = spec_die->parent;
22687 cu = spec_cu;
22688 }
22689
22690 if (parent == NULL)
22691 return "";
22692 else if (parent->building_fullname)
22693 {
22694 const char *name;
22695 const char *parent_name;
22696
22697 /* It has been seen on RealView 2.2 built binaries,
22698 DW_TAG_template_type_param types actually _defined_ as
22699 children of the parent class:
22700
22701 enum E {};
22702 template class <class Enum> Class{};
22703 Class<enum E> class_e;
22704
22705 1: DW_TAG_class_type (Class)
22706 2: DW_TAG_enumeration_type (E)
22707 3: DW_TAG_enumerator (enum1:0)
22708 3: DW_TAG_enumerator (enum2:1)
22709 ...
22710 2: DW_TAG_template_type_param
22711 DW_AT_type DW_FORM_ref_udata (E)
22712
22713 Besides being broken debug info, it can put GDB into an
22714 infinite loop. Consider:
22715
22716 When we're building the full name for Class<E>, we'll start
22717 at Class, and go look over its template type parameters,
22718 finding E. We'll then try to build the full name of E, and
22719 reach here. We're now trying to build the full name of E,
22720 and look over the parent DIE for containing scope. In the
22721 broken case, if we followed the parent DIE of E, we'd again
22722 find Class, and once again go look at its template type
22723 arguments, etc., etc. Simply don't consider such parent die
22724 as source-level parent of this die (it can't be, the language
22725 doesn't allow it), and break the loop here. */
22726 name = dwarf2_name (die, cu);
22727 parent_name = dwarf2_name (parent, cu);
22728 complaint (_("template param type '%s' defined within parent '%s'"),
22729 name ? name : "<unknown>",
22730 parent_name ? parent_name : "<unknown>");
22731 return "";
22732 }
22733 else
22734 switch (parent->tag)
22735 {
22736 case DW_TAG_namespace:
22737 parent_type = read_type_die (parent, cu);
22738 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22739 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22740 Work around this problem here. */
22741 if (cu->language == language_cplus
22742 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22743 return "";
22744 /* We give a name to even anonymous namespaces. */
22745 return TYPE_NAME (parent_type);
22746 case DW_TAG_class_type:
22747 case DW_TAG_interface_type:
22748 case DW_TAG_structure_type:
22749 case DW_TAG_union_type:
22750 case DW_TAG_module:
22751 parent_type = read_type_die (parent, cu);
22752 if (TYPE_NAME (parent_type) != NULL)
22753 return TYPE_NAME (parent_type);
22754 else
22755 /* An anonymous structure is only allowed non-static data
22756 members; no typedefs, no member functions, et cetera.
22757 So it does not need a prefix. */
22758 return "";
22759 case DW_TAG_compile_unit:
22760 case DW_TAG_partial_unit:
22761 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22762 if (cu->language == language_cplus
22763 && !dwarf2_per_objfile->types.empty ()
22764 && die->child != NULL
22765 && (die->tag == DW_TAG_class_type
22766 || die->tag == DW_TAG_structure_type
22767 || die->tag == DW_TAG_union_type))
22768 {
22769 const char *name = guess_full_die_structure_name (die, cu);
22770 if (name != NULL)
22771 return name;
22772 }
22773 return "";
22774 case DW_TAG_subprogram:
22775 /* Nested subroutines in Fortran get a prefix with the name
22776 of the parent's subroutine. */
22777 if (cu->language == language_fortran)
22778 {
22779 if ((die->tag == DW_TAG_subprogram)
22780 && (dwarf2_name (parent, cu) != NULL))
22781 return dwarf2_name (parent, cu);
22782 }
22783 return determine_prefix (parent, cu);
22784 case DW_TAG_enumeration_type:
22785 parent_type = read_type_die (parent, cu);
22786 if (TYPE_DECLARED_CLASS (parent_type))
22787 {
22788 if (TYPE_NAME (parent_type) != NULL)
22789 return TYPE_NAME (parent_type);
22790 return "";
22791 }
22792 /* Fall through. */
22793 default:
22794 return determine_prefix (parent, cu);
22795 }
22796 }
22797
22798 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22799 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22800 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22801 an obconcat, otherwise allocate storage for the result. The CU argument is
22802 used to determine the language and hence, the appropriate separator. */
22803
22804 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22805
22806 static char *
22807 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22808 int physname, struct dwarf2_cu *cu)
22809 {
22810 const char *lead = "";
22811 const char *sep;
22812
22813 if (suffix == NULL || suffix[0] == '\0'
22814 || prefix == NULL || prefix[0] == '\0')
22815 sep = "";
22816 else if (cu->language == language_d)
22817 {
22818 /* For D, the 'main' function could be defined in any module, but it
22819 should never be prefixed. */
22820 if (strcmp (suffix, "D main") == 0)
22821 {
22822 prefix = "";
22823 sep = "";
22824 }
22825 else
22826 sep = ".";
22827 }
22828 else if (cu->language == language_fortran && physname)
22829 {
22830 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22831 DW_AT_MIPS_linkage_name is preferred and used instead. */
22832
22833 lead = "__";
22834 sep = "_MOD_";
22835 }
22836 else
22837 sep = "::";
22838
22839 if (prefix == NULL)
22840 prefix = "";
22841 if (suffix == NULL)
22842 suffix = "";
22843
22844 if (obs == NULL)
22845 {
22846 char *retval
22847 = ((char *)
22848 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22849
22850 strcpy (retval, lead);
22851 strcat (retval, prefix);
22852 strcat (retval, sep);
22853 strcat (retval, suffix);
22854 return retval;
22855 }
22856 else
22857 {
22858 /* We have an obstack. */
22859 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22860 }
22861 }
22862
22863 /* Return sibling of die, NULL if no sibling. */
22864
22865 static struct die_info *
22866 sibling_die (struct die_info *die)
22867 {
22868 return die->sibling;
22869 }
22870
22871 /* Get name of a die, return NULL if not found. */
22872
22873 static const char *
22874 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22875 struct obstack *obstack)
22876 {
22877 if (name && cu->language == language_cplus)
22878 {
22879 std::string canon_name = cp_canonicalize_string (name);
22880
22881 if (!canon_name.empty ())
22882 {
22883 if (canon_name != name)
22884 name = obstack_strdup (obstack, canon_name);
22885 }
22886 }
22887
22888 return name;
22889 }
22890
22891 /* Get name of a die, return NULL if not found.
22892 Anonymous namespaces are converted to their magic string. */
22893
22894 static const char *
22895 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22896 {
22897 struct attribute *attr;
22898 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22899
22900 attr = dwarf2_attr (die, DW_AT_name, cu);
22901 if ((!attr || !DW_STRING (attr))
22902 && die->tag != DW_TAG_namespace
22903 && die->tag != DW_TAG_class_type
22904 && die->tag != DW_TAG_interface_type
22905 && die->tag != DW_TAG_structure_type
22906 && die->tag != DW_TAG_union_type)
22907 return NULL;
22908
22909 switch (die->tag)
22910 {
22911 case DW_TAG_compile_unit:
22912 case DW_TAG_partial_unit:
22913 /* Compilation units have a DW_AT_name that is a filename, not
22914 a source language identifier. */
22915 case DW_TAG_enumeration_type:
22916 case DW_TAG_enumerator:
22917 /* These tags always have simple identifiers already; no need
22918 to canonicalize them. */
22919 return DW_STRING (attr);
22920
22921 case DW_TAG_namespace:
22922 if (attr != NULL && DW_STRING (attr) != NULL)
22923 return DW_STRING (attr);
22924 return CP_ANONYMOUS_NAMESPACE_STR;
22925
22926 case DW_TAG_class_type:
22927 case DW_TAG_interface_type:
22928 case DW_TAG_structure_type:
22929 case DW_TAG_union_type:
22930 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22931 structures or unions. These were of the form "._%d" in GCC 4.1,
22932 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22933 and GCC 4.4. We work around this problem by ignoring these. */
22934 if (attr && DW_STRING (attr)
22935 && (startswith (DW_STRING (attr), "._")
22936 || startswith (DW_STRING (attr), "<anonymous")))
22937 return NULL;
22938
22939 /* GCC might emit a nameless typedef that has a linkage name. See
22940 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22941 if (!attr || DW_STRING (attr) == NULL)
22942 {
22943 attr = dw2_linkage_name_attr (die, cu);
22944 if (attr == NULL || DW_STRING (attr) == NULL)
22945 return NULL;
22946
22947 /* Avoid demangling DW_STRING (attr) the second time on a second
22948 call for the same DIE. */
22949 if (!DW_STRING_IS_CANONICAL (attr))
22950 {
22951 gdb::unique_xmalloc_ptr<char> demangled
22952 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
22953
22954 const char *base;
22955
22956 /* FIXME: we already did this for the partial symbol... */
22957 DW_STRING (attr)
22958 = obstack_strdup (&objfile->per_bfd->storage_obstack,
22959 demangled.get ());
22960 DW_STRING_IS_CANONICAL (attr) = 1;
22961
22962 /* Strip any leading namespaces/classes, keep only the base name.
22963 DW_AT_name for named DIEs does not contain the prefixes. */
22964 base = strrchr (DW_STRING (attr), ':');
22965 if (base && base > DW_STRING (attr) && base[-1] == ':')
22966 return &base[1];
22967 else
22968 return DW_STRING (attr);
22969 }
22970 }
22971 break;
22972
22973 default:
22974 break;
22975 }
22976
22977 if (!DW_STRING_IS_CANONICAL (attr))
22978 {
22979 DW_STRING (attr)
22980 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22981 &objfile->per_bfd->storage_obstack);
22982 DW_STRING_IS_CANONICAL (attr) = 1;
22983 }
22984 return DW_STRING (attr);
22985 }
22986
22987 /* Return the die that this die in an extension of, or NULL if there
22988 is none. *EXT_CU is the CU containing DIE on input, and the CU
22989 containing the return value on output. */
22990
22991 static struct die_info *
22992 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22993 {
22994 struct attribute *attr;
22995
22996 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22997 if (attr == NULL)
22998 return NULL;
22999
23000 return follow_die_ref (die, attr, ext_cu);
23001 }
23002
23003 /* A convenience function that returns an "unknown" DWARF name,
23004 including the value of V. STR is the name of the entity being
23005 printed, e.g., "TAG". */
23006
23007 static const char *
23008 dwarf_unknown (const char *str, unsigned v)
23009 {
23010 char *cell = get_print_cell ();
23011 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
23012 return cell;
23013 }
23014
23015 /* Convert a DIE tag into its string name. */
23016
23017 static const char *
23018 dwarf_tag_name (unsigned tag)
23019 {
23020 const char *name = get_DW_TAG_name (tag);
23021
23022 if (name == NULL)
23023 return dwarf_unknown ("TAG", tag);
23024
23025 return name;
23026 }
23027
23028 /* Convert a DWARF attribute code into its string name. */
23029
23030 static const char *
23031 dwarf_attr_name (unsigned attr)
23032 {
23033 const char *name;
23034
23035 #ifdef MIPS /* collides with DW_AT_HP_block_index */
23036 if (attr == DW_AT_MIPS_fde)
23037 return "DW_AT_MIPS_fde";
23038 #else
23039 if (attr == DW_AT_HP_block_index)
23040 return "DW_AT_HP_block_index";
23041 #endif
23042
23043 name = get_DW_AT_name (attr);
23044
23045 if (name == NULL)
23046 return dwarf_unknown ("AT", attr);
23047
23048 return name;
23049 }
23050
23051 /* Convert a unit type to corresponding DW_UT name. */
23052
23053 static const char *
23054 dwarf_unit_type_name (int unit_type) {
23055 switch (unit_type)
23056 {
23057 case 0x01:
23058 return "DW_UT_compile (0x01)";
23059 case 0x02:
23060 return "DW_UT_type (0x02)";
23061 case 0x03:
23062 return "DW_UT_partial (0x03)";
23063 case 0x04:
23064 return "DW_UT_skeleton (0x04)";
23065 case 0x05:
23066 return "DW_UT_split_compile (0x05)";
23067 case 0x06:
23068 return "DW_UT_split_type (0x06)";
23069 case 0x80:
23070 return "DW_UT_lo_user (0x80)";
23071 case 0xff:
23072 return "DW_UT_hi_user (0xff)";
23073 default:
23074 return nullptr;
23075 }
23076 }
23077
23078 /* Convert a DWARF value form code into its string name. */
23079
23080 static const char *
23081 dwarf_form_name (unsigned form)
23082 {
23083 const char *name = get_DW_FORM_name (form);
23084
23085 if (name == NULL)
23086 return dwarf_unknown ("FORM", form);
23087
23088 return name;
23089 }
23090
23091 static const char *
23092 dwarf_bool_name (unsigned mybool)
23093 {
23094 if (mybool)
23095 return "TRUE";
23096 else
23097 return "FALSE";
23098 }
23099
23100 /* Convert a DWARF type code into its string name. */
23101
23102 static const char *
23103 dwarf_type_encoding_name (unsigned enc)
23104 {
23105 const char *name = get_DW_ATE_name (enc);
23106
23107 if (name == NULL)
23108 return dwarf_unknown ("ATE", enc);
23109
23110 return name;
23111 }
23112
23113 static void
23114 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23115 {
23116 unsigned int i;
23117
23118 print_spaces (indent, f);
23119 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23120 dwarf_tag_name (die->tag), die->abbrev,
23121 sect_offset_str (die->sect_off));
23122
23123 if (die->parent != NULL)
23124 {
23125 print_spaces (indent, f);
23126 fprintf_unfiltered (f, " parent at offset: %s\n",
23127 sect_offset_str (die->parent->sect_off));
23128 }
23129
23130 print_spaces (indent, f);
23131 fprintf_unfiltered (f, " has children: %s\n",
23132 dwarf_bool_name (die->child != NULL));
23133
23134 print_spaces (indent, f);
23135 fprintf_unfiltered (f, " attributes:\n");
23136
23137 for (i = 0; i < die->num_attrs; ++i)
23138 {
23139 print_spaces (indent, f);
23140 fprintf_unfiltered (f, " %s (%s) ",
23141 dwarf_attr_name (die->attrs[i].name),
23142 dwarf_form_name (die->attrs[i].form));
23143
23144 switch (die->attrs[i].form)
23145 {
23146 case DW_FORM_addr:
23147 case DW_FORM_addrx:
23148 case DW_FORM_GNU_addr_index:
23149 fprintf_unfiltered (f, "address: ");
23150 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
23151 break;
23152 case DW_FORM_block2:
23153 case DW_FORM_block4:
23154 case DW_FORM_block:
23155 case DW_FORM_block1:
23156 fprintf_unfiltered (f, "block: size %s",
23157 pulongest (DW_BLOCK (&die->attrs[i])->size));
23158 break;
23159 case DW_FORM_exprloc:
23160 fprintf_unfiltered (f, "expression: size %s",
23161 pulongest (DW_BLOCK (&die->attrs[i])->size));
23162 break;
23163 case DW_FORM_data16:
23164 fprintf_unfiltered (f, "constant of 16 bytes");
23165 break;
23166 case DW_FORM_ref_addr:
23167 fprintf_unfiltered (f, "ref address: ");
23168 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23169 break;
23170 case DW_FORM_GNU_ref_alt:
23171 fprintf_unfiltered (f, "alt ref address: ");
23172 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23173 break;
23174 case DW_FORM_ref1:
23175 case DW_FORM_ref2:
23176 case DW_FORM_ref4:
23177 case DW_FORM_ref8:
23178 case DW_FORM_ref_udata:
23179 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23180 (long) (DW_UNSND (&die->attrs[i])));
23181 break;
23182 case DW_FORM_data1:
23183 case DW_FORM_data2:
23184 case DW_FORM_data4:
23185 case DW_FORM_data8:
23186 case DW_FORM_udata:
23187 case DW_FORM_sdata:
23188 fprintf_unfiltered (f, "constant: %s",
23189 pulongest (DW_UNSND (&die->attrs[i])));
23190 break;
23191 case DW_FORM_sec_offset:
23192 fprintf_unfiltered (f, "section offset: %s",
23193 pulongest (DW_UNSND (&die->attrs[i])));
23194 break;
23195 case DW_FORM_ref_sig8:
23196 fprintf_unfiltered (f, "signature: %s",
23197 hex_string (DW_SIGNATURE (&die->attrs[i])));
23198 break;
23199 case DW_FORM_string:
23200 case DW_FORM_strp:
23201 case DW_FORM_line_strp:
23202 case DW_FORM_strx:
23203 case DW_FORM_GNU_str_index:
23204 case DW_FORM_GNU_strp_alt:
23205 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23206 DW_STRING (&die->attrs[i])
23207 ? DW_STRING (&die->attrs[i]) : "",
23208 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
23209 break;
23210 case DW_FORM_flag:
23211 if (DW_UNSND (&die->attrs[i]))
23212 fprintf_unfiltered (f, "flag: TRUE");
23213 else
23214 fprintf_unfiltered (f, "flag: FALSE");
23215 break;
23216 case DW_FORM_flag_present:
23217 fprintf_unfiltered (f, "flag: TRUE");
23218 break;
23219 case DW_FORM_indirect:
23220 /* The reader will have reduced the indirect form to
23221 the "base form" so this form should not occur. */
23222 fprintf_unfiltered (f,
23223 "unexpected attribute form: DW_FORM_indirect");
23224 break;
23225 case DW_FORM_implicit_const:
23226 fprintf_unfiltered (f, "constant: %s",
23227 plongest (DW_SND (&die->attrs[i])));
23228 break;
23229 default:
23230 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23231 die->attrs[i].form);
23232 break;
23233 }
23234 fprintf_unfiltered (f, "\n");
23235 }
23236 }
23237
23238 static void
23239 dump_die_for_error (struct die_info *die)
23240 {
23241 dump_die_shallow (gdb_stderr, 0, die);
23242 }
23243
23244 static void
23245 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23246 {
23247 int indent = level * 4;
23248
23249 gdb_assert (die != NULL);
23250
23251 if (level >= max_level)
23252 return;
23253
23254 dump_die_shallow (f, indent, die);
23255
23256 if (die->child != NULL)
23257 {
23258 print_spaces (indent, f);
23259 fprintf_unfiltered (f, " Children:");
23260 if (level + 1 < max_level)
23261 {
23262 fprintf_unfiltered (f, "\n");
23263 dump_die_1 (f, level + 1, max_level, die->child);
23264 }
23265 else
23266 {
23267 fprintf_unfiltered (f,
23268 " [not printed, max nesting level reached]\n");
23269 }
23270 }
23271
23272 if (die->sibling != NULL && level > 0)
23273 {
23274 dump_die_1 (f, level, max_level, die->sibling);
23275 }
23276 }
23277
23278 /* This is called from the pdie macro in gdbinit.in.
23279 It's not static so gcc will keep a copy callable from gdb. */
23280
23281 void
23282 dump_die (struct die_info *die, int max_level)
23283 {
23284 dump_die_1 (gdb_stdlog, 0, max_level, die);
23285 }
23286
23287 static void
23288 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23289 {
23290 void **slot;
23291
23292 slot = htab_find_slot_with_hash (cu->die_hash, die,
23293 to_underlying (die->sect_off),
23294 INSERT);
23295
23296 *slot = die;
23297 }
23298
23299 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23300 required kind. */
23301
23302 static sect_offset
23303 dwarf2_get_ref_die_offset (const struct attribute *attr)
23304 {
23305 if (attr_form_is_ref (attr))
23306 return (sect_offset) DW_UNSND (attr);
23307
23308 complaint (_("unsupported die ref attribute form: '%s'"),
23309 dwarf_form_name (attr->form));
23310 return {};
23311 }
23312
23313 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23314 * the value held by the attribute is not constant. */
23315
23316 static LONGEST
23317 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23318 {
23319 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23320 return DW_SND (attr);
23321 else if (attr->form == DW_FORM_udata
23322 || attr->form == DW_FORM_data1
23323 || attr->form == DW_FORM_data2
23324 || attr->form == DW_FORM_data4
23325 || attr->form == DW_FORM_data8)
23326 return DW_UNSND (attr);
23327 else
23328 {
23329 /* For DW_FORM_data16 see attr_form_is_constant. */
23330 complaint (_("Attribute value is not a constant (%s)"),
23331 dwarf_form_name (attr->form));
23332 return default_value;
23333 }
23334 }
23335
23336 /* Follow reference or signature attribute ATTR of SRC_DIE.
23337 On entry *REF_CU is the CU of SRC_DIE.
23338 On exit *REF_CU is the CU of the result. */
23339
23340 static struct die_info *
23341 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23342 struct dwarf2_cu **ref_cu)
23343 {
23344 struct die_info *die;
23345
23346 if (attr_form_is_ref (attr))
23347 die = follow_die_ref (src_die, attr, ref_cu);
23348 else if (attr->form == DW_FORM_ref_sig8)
23349 die = follow_die_sig (src_die, attr, ref_cu);
23350 else
23351 {
23352 dump_die_for_error (src_die);
23353 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23354 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23355 }
23356
23357 return die;
23358 }
23359
23360 /* Follow reference OFFSET.
23361 On entry *REF_CU is the CU of the source die referencing OFFSET.
23362 On exit *REF_CU is the CU of the result.
23363 Returns NULL if OFFSET is invalid. */
23364
23365 static struct die_info *
23366 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23367 struct dwarf2_cu **ref_cu)
23368 {
23369 struct die_info temp_die;
23370 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23371 struct dwarf2_per_objfile *dwarf2_per_objfile
23372 = cu->per_cu->dwarf2_per_objfile;
23373
23374 gdb_assert (cu->per_cu != NULL);
23375
23376 target_cu = cu;
23377
23378 if (cu->per_cu->is_debug_types)
23379 {
23380 /* .debug_types CUs cannot reference anything outside their CU.
23381 If they need to, they have to reference a signatured type via
23382 DW_FORM_ref_sig8. */
23383 if (!offset_in_cu_p (&cu->header, sect_off))
23384 return NULL;
23385 }
23386 else if (offset_in_dwz != cu->per_cu->is_dwz
23387 || !offset_in_cu_p (&cu->header, sect_off))
23388 {
23389 struct dwarf2_per_cu_data *per_cu;
23390
23391 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23392 dwarf2_per_objfile);
23393
23394 /* If necessary, add it to the queue and load its DIEs. */
23395 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23396 load_full_comp_unit (per_cu, false, cu->language);
23397
23398 target_cu = per_cu->cu;
23399 }
23400 else if (cu->dies == NULL)
23401 {
23402 /* We're loading full DIEs during partial symbol reading. */
23403 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23404 load_full_comp_unit (cu->per_cu, false, language_minimal);
23405 }
23406
23407 *ref_cu = target_cu;
23408 temp_die.sect_off = sect_off;
23409
23410 if (target_cu != cu)
23411 target_cu->ancestor = cu;
23412
23413 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23414 &temp_die,
23415 to_underlying (sect_off));
23416 }
23417
23418 /* Follow reference attribute ATTR of SRC_DIE.
23419 On entry *REF_CU is the CU of SRC_DIE.
23420 On exit *REF_CU is the CU of the result. */
23421
23422 static struct die_info *
23423 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23424 struct dwarf2_cu **ref_cu)
23425 {
23426 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23427 struct dwarf2_cu *cu = *ref_cu;
23428 struct die_info *die;
23429
23430 die = follow_die_offset (sect_off,
23431 (attr->form == DW_FORM_GNU_ref_alt
23432 || cu->per_cu->is_dwz),
23433 ref_cu);
23434 if (!die)
23435 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23436 "at %s [in module %s]"),
23437 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23438 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23439
23440 return die;
23441 }
23442
23443 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23444 Returned value is intended for DW_OP_call*. Returned
23445 dwarf2_locexpr_baton->data has lifetime of
23446 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23447
23448 struct dwarf2_locexpr_baton
23449 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23450 struct dwarf2_per_cu_data *per_cu,
23451 CORE_ADDR (*get_frame_pc) (void *baton),
23452 void *baton, bool resolve_abstract_p)
23453 {
23454 struct dwarf2_cu *cu;
23455 struct die_info *die;
23456 struct attribute *attr;
23457 struct dwarf2_locexpr_baton retval;
23458 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23459 struct objfile *objfile = dwarf2_per_objfile->objfile;
23460
23461 if (per_cu->cu == NULL)
23462 load_cu (per_cu, false);
23463 cu = per_cu->cu;
23464 if (cu == NULL)
23465 {
23466 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23467 Instead just throw an error, not much else we can do. */
23468 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23469 sect_offset_str (sect_off), objfile_name (objfile));
23470 }
23471
23472 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23473 if (!die)
23474 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23475 sect_offset_str (sect_off), objfile_name (objfile));
23476
23477 attr = dwarf2_attr (die, DW_AT_location, cu);
23478 if (!attr && resolve_abstract_p
23479 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
23480 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23481 {
23482 CORE_ADDR pc = (*get_frame_pc) (baton);
23483 CORE_ADDR baseaddr = objfile->text_section_offset ();
23484 struct gdbarch *gdbarch = get_objfile_arch (objfile);
23485
23486 for (const auto &cand_off
23487 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
23488 {
23489 struct dwarf2_cu *cand_cu = cu;
23490 struct die_info *cand
23491 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23492 if (!cand
23493 || !cand->parent
23494 || cand->parent->tag != DW_TAG_subprogram)
23495 continue;
23496
23497 CORE_ADDR pc_low, pc_high;
23498 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23499 if (pc_low == ((CORE_ADDR) -1))
23500 continue;
23501 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23502 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23503 if (!(pc_low <= pc && pc < pc_high))
23504 continue;
23505
23506 die = cand;
23507 attr = dwarf2_attr (die, DW_AT_location, cu);
23508 break;
23509 }
23510 }
23511
23512 if (!attr)
23513 {
23514 /* DWARF: "If there is no such attribute, then there is no effect.".
23515 DATA is ignored if SIZE is 0. */
23516
23517 retval.data = NULL;
23518 retval.size = 0;
23519 }
23520 else if (attr_form_is_section_offset (attr))
23521 {
23522 struct dwarf2_loclist_baton loclist_baton;
23523 CORE_ADDR pc = (*get_frame_pc) (baton);
23524 size_t size;
23525
23526 fill_in_loclist_baton (cu, &loclist_baton, attr);
23527
23528 retval.data = dwarf2_find_location_expression (&loclist_baton,
23529 &size, pc);
23530 retval.size = size;
23531 }
23532 else
23533 {
23534 if (!attr_form_is_block (attr))
23535 error (_("Dwarf Error: DIE at %s referenced in module %s "
23536 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23537 sect_offset_str (sect_off), objfile_name (objfile));
23538
23539 retval.data = DW_BLOCK (attr)->data;
23540 retval.size = DW_BLOCK (attr)->size;
23541 }
23542 retval.per_cu = cu->per_cu;
23543
23544 age_cached_comp_units (dwarf2_per_objfile);
23545
23546 return retval;
23547 }
23548
23549 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23550 offset. */
23551
23552 struct dwarf2_locexpr_baton
23553 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23554 struct dwarf2_per_cu_data *per_cu,
23555 CORE_ADDR (*get_frame_pc) (void *baton),
23556 void *baton)
23557 {
23558 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23559
23560 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23561 }
23562
23563 /* Write a constant of a given type as target-ordered bytes into
23564 OBSTACK. */
23565
23566 static const gdb_byte *
23567 write_constant_as_bytes (struct obstack *obstack,
23568 enum bfd_endian byte_order,
23569 struct type *type,
23570 ULONGEST value,
23571 LONGEST *len)
23572 {
23573 gdb_byte *result;
23574
23575 *len = TYPE_LENGTH (type);
23576 result = (gdb_byte *) obstack_alloc (obstack, *len);
23577 store_unsigned_integer (result, *len, byte_order, value);
23578
23579 return result;
23580 }
23581
23582 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23583 pointer to the constant bytes and set LEN to the length of the
23584 data. If memory is needed, allocate it on OBSTACK. If the DIE
23585 does not have a DW_AT_const_value, return NULL. */
23586
23587 const gdb_byte *
23588 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23589 struct dwarf2_per_cu_data *per_cu,
23590 struct obstack *obstack,
23591 LONGEST *len)
23592 {
23593 struct dwarf2_cu *cu;
23594 struct die_info *die;
23595 struct attribute *attr;
23596 const gdb_byte *result = NULL;
23597 struct type *type;
23598 LONGEST value;
23599 enum bfd_endian byte_order;
23600 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23601
23602 if (per_cu->cu == NULL)
23603 load_cu (per_cu, false);
23604 cu = per_cu->cu;
23605 if (cu == NULL)
23606 {
23607 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23608 Instead just throw an error, not much else we can do. */
23609 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23610 sect_offset_str (sect_off), objfile_name (objfile));
23611 }
23612
23613 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23614 if (!die)
23615 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23616 sect_offset_str (sect_off), objfile_name (objfile));
23617
23618 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23619 if (attr == NULL)
23620 return NULL;
23621
23622 byte_order = (bfd_big_endian (objfile->obfd)
23623 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23624
23625 switch (attr->form)
23626 {
23627 case DW_FORM_addr:
23628 case DW_FORM_addrx:
23629 case DW_FORM_GNU_addr_index:
23630 {
23631 gdb_byte *tem;
23632
23633 *len = cu->header.addr_size;
23634 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23635 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23636 result = tem;
23637 }
23638 break;
23639 case DW_FORM_string:
23640 case DW_FORM_strp:
23641 case DW_FORM_strx:
23642 case DW_FORM_GNU_str_index:
23643 case DW_FORM_GNU_strp_alt:
23644 /* DW_STRING is already allocated on the objfile obstack, point
23645 directly to it. */
23646 result = (const gdb_byte *) DW_STRING (attr);
23647 *len = strlen (DW_STRING (attr));
23648 break;
23649 case DW_FORM_block1:
23650 case DW_FORM_block2:
23651 case DW_FORM_block4:
23652 case DW_FORM_block:
23653 case DW_FORM_exprloc:
23654 case DW_FORM_data16:
23655 result = DW_BLOCK (attr)->data;
23656 *len = DW_BLOCK (attr)->size;
23657 break;
23658
23659 /* The DW_AT_const_value attributes are supposed to carry the
23660 symbol's value "represented as it would be on the target
23661 architecture." By the time we get here, it's already been
23662 converted to host endianness, so we just need to sign- or
23663 zero-extend it as appropriate. */
23664 case DW_FORM_data1:
23665 type = die_type (die, cu);
23666 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23667 if (result == NULL)
23668 result = write_constant_as_bytes (obstack, byte_order,
23669 type, value, len);
23670 break;
23671 case DW_FORM_data2:
23672 type = die_type (die, cu);
23673 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23674 if (result == NULL)
23675 result = write_constant_as_bytes (obstack, byte_order,
23676 type, value, len);
23677 break;
23678 case DW_FORM_data4:
23679 type = die_type (die, cu);
23680 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23681 if (result == NULL)
23682 result = write_constant_as_bytes (obstack, byte_order,
23683 type, value, len);
23684 break;
23685 case DW_FORM_data8:
23686 type = die_type (die, cu);
23687 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23688 if (result == NULL)
23689 result = write_constant_as_bytes (obstack, byte_order,
23690 type, value, len);
23691 break;
23692
23693 case DW_FORM_sdata:
23694 case DW_FORM_implicit_const:
23695 type = die_type (die, cu);
23696 result = write_constant_as_bytes (obstack, byte_order,
23697 type, DW_SND (attr), len);
23698 break;
23699
23700 case DW_FORM_udata:
23701 type = die_type (die, cu);
23702 result = write_constant_as_bytes (obstack, byte_order,
23703 type, DW_UNSND (attr), len);
23704 break;
23705
23706 default:
23707 complaint (_("unsupported const value attribute form: '%s'"),
23708 dwarf_form_name (attr->form));
23709 break;
23710 }
23711
23712 return result;
23713 }
23714
23715 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23716 valid type for this die is found. */
23717
23718 struct type *
23719 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23720 struct dwarf2_per_cu_data *per_cu)
23721 {
23722 struct dwarf2_cu *cu;
23723 struct die_info *die;
23724
23725 if (per_cu->cu == NULL)
23726 load_cu (per_cu, false);
23727 cu = per_cu->cu;
23728 if (!cu)
23729 return NULL;
23730
23731 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23732 if (!die)
23733 return NULL;
23734
23735 return die_type (die, cu);
23736 }
23737
23738 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23739 PER_CU. */
23740
23741 struct type *
23742 dwarf2_get_die_type (cu_offset die_offset,
23743 struct dwarf2_per_cu_data *per_cu)
23744 {
23745 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23746 return get_die_type_at_offset (die_offset_sect, per_cu);
23747 }
23748
23749 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23750 On entry *REF_CU is the CU of SRC_DIE.
23751 On exit *REF_CU is the CU of the result.
23752 Returns NULL if the referenced DIE isn't found. */
23753
23754 static struct die_info *
23755 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23756 struct dwarf2_cu **ref_cu)
23757 {
23758 struct die_info temp_die;
23759 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23760 struct die_info *die;
23761
23762 /* While it might be nice to assert sig_type->type == NULL here,
23763 we can get here for DW_AT_imported_declaration where we need
23764 the DIE not the type. */
23765
23766 /* If necessary, add it to the queue and load its DIEs. */
23767
23768 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23769 read_signatured_type (sig_type);
23770
23771 sig_cu = sig_type->per_cu.cu;
23772 gdb_assert (sig_cu != NULL);
23773 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23774 temp_die.sect_off = sig_type->type_offset_in_section;
23775 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23776 to_underlying (temp_die.sect_off));
23777 if (die)
23778 {
23779 struct dwarf2_per_objfile *dwarf2_per_objfile
23780 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23781
23782 /* For .gdb_index version 7 keep track of included TUs.
23783 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23784 if (dwarf2_per_objfile->index_table != NULL
23785 && dwarf2_per_objfile->index_table->version <= 7)
23786 {
23787 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23788 }
23789
23790 *ref_cu = sig_cu;
23791 if (sig_cu != cu)
23792 sig_cu->ancestor = cu;
23793
23794 return die;
23795 }
23796
23797 return NULL;
23798 }
23799
23800 /* Follow signatured type referenced by ATTR in SRC_DIE.
23801 On entry *REF_CU is the CU of SRC_DIE.
23802 On exit *REF_CU is the CU of the result.
23803 The result is the DIE of the type.
23804 If the referenced type cannot be found an error is thrown. */
23805
23806 static struct die_info *
23807 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23808 struct dwarf2_cu **ref_cu)
23809 {
23810 ULONGEST signature = DW_SIGNATURE (attr);
23811 struct signatured_type *sig_type;
23812 struct die_info *die;
23813
23814 gdb_assert (attr->form == DW_FORM_ref_sig8);
23815
23816 sig_type = lookup_signatured_type (*ref_cu, signature);
23817 /* sig_type will be NULL if the signatured type is missing from
23818 the debug info. */
23819 if (sig_type == NULL)
23820 {
23821 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23822 " from DIE at %s [in module %s]"),
23823 hex_string (signature), sect_offset_str (src_die->sect_off),
23824 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23825 }
23826
23827 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23828 if (die == NULL)
23829 {
23830 dump_die_for_error (src_die);
23831 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23832 " from DIE at %s [in module %s]"),
23833 hex_string (signature), sect_offset_str (src_die->sect_off),
23834 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23835 }
23836
23837 return die;
23838 }
23839
23840 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23841 reading in and processing the type unit if necessary. */
23842
23843 static struct type *
23844 get_signatured_type (struct die_info *die, ULONGEST signature,
23845 struct dwarf2_cu *cu)
23846 {
23847 struct dwarf2_per_objfile *dwarf2_per_objfile
23848 = cu->per_cu->dwarf2_per_objfile;
23849 struct signatured_type *sig_type;
23850 struct dwarf2_cu *type_cu;
23851 struct die_info *type_die;
23852 struct type *type;
23853
23854 sig_type = lookup_signatured_type (cu, signature);
23855 /* sig_type will be NULL if the signatured type is missing from
23856 the debug info. */
23857 if (sig_type == NULL)
23858 {
23859 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23860 " from DIE at %s [in module %s]"),
23861 hex_string (signature), sect_offset_str (die->sect_off),
23862 objfile_name (dwarf2_per_objfile->objfile));
23863 return build_error_marker_type (cu, die);
23864 }
23865
23866 /* If we already know the type we're done. */
23867 if (sig_type->type != NULL)
23868 return sig_type->type;
23869
23870 type_cu = cu;
23871 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23872 if (type_die != NULL)
23873 {
23874 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23875 is created. This is important, for example, because for c++ classes
23876 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23877 type = read_type_die (type_die, type_cu);
23878 if (type == NULL)
23879 {
23880 complaint (_("Dwarf Error: Cannot build signatured type %s"
23881 " referenced from DIE at %s [in module %s]"),
23882 hex_string (signature), sect_offset_str (die->sect_off),
23883 objfile_name (dwarf2_per_objfile->objfile));
23884 type = build_error_marker_type (cu, die);
23885 }
23886 }
23887 else
23888 {
23889 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23890 " from DIE at %s [in module %s]"),
23891 hex_string (signature), sect_offset_str (die->sect_off),
23892 objfile_name (dwarf2_per_objfile->objfile));
23893 type = build_error_marker_type (cu, die);
23894 }
23895 sig_type->type = type;
23896
23897 return type;
23898 }
23899
23900 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23901 reading in and processing the type unit if necessary. */
23902
23903 static struct type *
23904 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23905 struct dwarf2_cu *cu) /* ARI: editCase function */
23906 {
23907 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23908 if (attr_form_is_ref (attr))
23909 {
23910 struct dwarf2_cu *type_cu = cu;
23911 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23912
23913 return read_type_die (type_die, type_cu);
23914 }
23915 else if (attr->form == DW_FORM_ref_sig8)
23916 {
23917 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23918 }
23919 else
23920 {
23921 struct dwarf2_per_objfile *dwarf2_per_objfile
23922 = cu->per_cu->dwarf2_per_objfile;
23923
23924 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23925 " at %s [in module %s]"),
23926 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23927 objfile_name (dwarf2_per_objfile->objfile));
23928 return build_error_marker_type (cu, die);
23929 }
23930 }
23931
23932 /* Load the DIEs associated with type unit PER_CU into memory. */
23933
23934 static void
23935 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23936 {
23937 struct signatured_type *sig_type;
23938
23939 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23940 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23941
23942 /* We have the per_cu, but we need the signatured_type.
23943 Fortunately this is an easy translation. */
23944 gdb_assert (per_cu->is_debug_types);
23945 sig_type = (struct signatured_type *) per_cu;
23946
23947 gdb_assert (per_cu->cu == NULL);
23948
23949 read_signatured_type (sig_type);
23950
23951 gdb_assert (per_cu->cu != NULL);
23952 }
23953
23954 /* Read in a signatured type and build its CU and DIEs.
23955 If the type is a stub for the real type in a DWO file,
23956 read in the real type from the DWO file as well. */
23957
23958 static void
23959 read_signatured_type (struct signatured_type *sig_type)
23960 {
23961 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23962
23963 gdb_assert (per_cu->is_debug_types);
23964 gdb_assert (per_cu->cu == NULL);
23965
23966 cutu_reader reader (per_cu, NULL, 0, 1, false);
23967
23968 if (!reader.dummy_p)
23969 {
23970 struct dwarf2_cu *cu = reader.cu;
23971 const gdb_byte *info_ptr = reader.info_ptr;
23972
23973 gdb_assert (cu->die_hash == NULL);
23974 cu->die_hash =
23975 htab_create_alloc_ex (cu->header.length / 12,
23976 die_hash,
23977 die_eq,
23978 NULL,
23979 &cu->comp_unit_obstack,
23980 hashtab_obstack_allocate,
23981 dummy_obstack_deallocate);
23982
23983 if (reader.has_children)
23984 reader.comp_unit_die->child
23985 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23986 reader.comp_unit_die);
23987 cu->dies = reader.comp_unit_die;
23988 /* comp_unit_die is not stored in die_hash, no need. */
23989
23990 /* We try not to read any attributes in this function, because
23991 not all CUs needed for references have been loaded yet, and
23992 symbol table processing isn't initialized. But we have to
23993 set the CU language, or we won't be able to build types
23994 correctly. Similarly, if we do not read the producer, we can
23995 not apply producer-specific interpretation. */
23996 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23997 }
23998
23999 sig_type->per_cu.tu_read = 1;
24000 }
24001
24002 /* Decode simple location descriptions.
24003 Given a pointer to a dwarf block that defines a location, compute
24004 the location and return the value.
24005
24006 NOTE drow/2003-11-18: This function is called in two situations
24007 now: for the address of static or global variables (partial symbols
24008 only) and for offsets into structures which are expected to be
24009 (more or less) constant. The partial symbol case should go away,
24010 and only the constant case should remain. That will let this
24011 function complain more accurately. A few special modes are allowed
24012 without complaint for global variables (for instance, global
24013 register values and thread-local values).
24014
24015 A location description containing no operations indicates that the
24016 object is optimized out. The return value is 0 for that case.
24017 FIXME drow/2003-11-16: No callers check for this case any more; soon all
24018 callers will only want a very basic result and this can become a
24019 complaint.
24020
24021 Note that stack[0] is unused except as a default error return. */
24022
24023 static CORE_ADDR
24024 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
24025 {
24026 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
24027 size_t i;
24028 size_t size = blk->size;
24029 const gdb_byte *data = blk->data;
24030 CORE_ADDR stack[64];
24031 int stacki;
24032 unsigned int bytes_read, unsnd;
24033 gdb_byte op;
24034
24035 i = 0;
24036 stacki = 0;
24037 stack[stacki] = 0;
24038 stack[++stacki] = 0;
24039
24040 while (i < size)
24041 {
24042 op = data[i++];
24043 switch (op)
24044 {
24045 case DW_OP_lit0:
24046 case DW_OP_lit1:
24047 case DW_OP_lit2:
24048 case DW_OP_lit3:
24049 case DW_OP_lit4:
24050 case DW_OP_lit5:
24051 case DW_OP_lit6:
24052 case DW_OP_lit7:
24053 case DW_OP_lit8:
24054 case DW_OP_lit9:
24055 case DW_OP_lit10:
24056 case DW_OP_lit11:
24057 case DW_OP_lit12:
24058 case DW_OP_lit13:
24059 case DW_OP_lit14:
24060 case DW_OP_lit15:
24061 case DW_OP_lit16:
24062 case DW_OP_lit17:
24063 case DW_OP_lit18:
24064 case DW_OP_lit19:
24065 case DW_OP_lit20:
24066 case DW_OP_lit21:
24067 case DW_OP_lit22:
24068 case DW_OP_lit23:
24069 case DW_OP_lit24:
24070 case DW_OP_lit25:
24071 case DW_OP_lit26:
24072 case DW_OP_lit27:
24073 case DW_OP_lit28:
24074 case DW_OP_lit29:
24075 case DW_OP_lit30:
24076 case DW_OP_lit31:
24077 stack[++stacki] = op - DW_OP_lit0;
24078 break;
24079
24080 case DW_OP_reg0:
24081 case DW_OP_reg1:
24082 case DW_OP_reg2:
24083 case DW_OP_reg3:
24084 case DW_OP_reg4:
24085 case DW_OP_reg5:
24086 case DW_OP_reg6:
24087 case DW_OP_reg7:
24088 case DW_OP_reg8:
24089 case DW_OP_reg9:
24090 case DW_OP_reg10:
24091 case DW_OP_reg11:
24092 case DW_OP_reg12:
24093 case DW_OP_reg13:
24094 case DW_OP_reg14:
24095 case DW_OP_reg15:
24096 case DW_OP_reg16:
24097 case DW_OP_reg17:
24098 case DW_OP_reg18:
24099 case DW_OP_reg19:
24100 case DW_OP_reg20:
24101 case DW_OP_reg21:
24102 case DW_OP_reg22:
24103 case DW_OP_reg23:
24104 case DW_OP_reg24:
24105 case DW_OP_reg25:
24106 case DW_OP_reg26:
24107 case DW_OP_reg27:
24108 case DW_OP_reg28:
24109 case DW_OP_reg29:
24110 case DW_OP_reg30:
24111 case DW_OP_reg31:
24112 stack[++stacki] = op - DW_OP_reg0;
24113 if (i < size)
24114 dwarf2_complex_location_expr_complaint ();
24115 break;
24116
24117 case DW_OP_regx:
24118 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24119 i += bytes_read;
24120 stack[++stacki] = unsnd;
24121 if (i < size)
24122 dwarf2_complex_location_expr_complaint ();
24123 break;
24124
24125 case DW_OP_addr:
24126 stack[++stacki] = read_address (objfile->obfd, &data[i],
24127 cu, &bytes_read);
24128 i += bytes_read;
24129 break;
24130
24131 case DW_OP_const1u:
24132 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24133 i += 1;
24134 break;
24135
24136 case DW_OP_const1s:
24137 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24138 i += 1;
24139 break;
24140
24141 case DW_OP_const2u:
24142 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24143 i += 2;
24144 break;
24145
24146 case DW_OP_const2s:
24147 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24148 i += 2;
24149 break;
24150
24151 case DW_OP_const4u:
24152 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24153 i += 4;
24154 break;
24155
24156 case DW_OP_const4s:
24157 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24158 i += 4;
24159 break;
24160
24161 case DW_OP_const8u:
24162 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24163 i += 8;
24164 break;
24165
24166 case DW_OP_constu:
24167 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24168 &bytes_read);
24169 i += bytes_read;
24170 break;
24171
24172 case DW_OP_consts:
24173 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24174 i += bytes_read;
24175 break;
24176
24177 case DW_OP_dup:
24178 stack[stacki + 1] = stack[stacki];
24179 stacki++;
24180 break;
24181
24182 case DW_OP_plus:
24183 stack[stacki - 1] += stack[stacki];
24184 stacki--;
24185 break;
24186
24187 case DW_OP_plus_uconst:
24188 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24189 &bytes_read);
24190 i += bytes_read;
24191 break;
24192
24193 case DW_OP_minus:
24194 stack[stacki - 1] -= stack[stacki];
24195 stacki--;
24196 break;
24197
24198 case DW_OP_deref:
24199 /* If we're not the last op, then we definitely can't encode
24200 this using GDB's address_class enum. This is valid for partial
24201 global symbols, although the variable's address will be bogus
24202 in the psymtab. */
24203 if (i < size)
24204 dwarf2_complex_location_expr_complaint ();
24205 break;
24206
24207 case DW_OP_GNU_push_tls_address:
24208 case DW_OP_form_tls_address:
24209 /* The top of the stack has the offset from the beginning
24210 of the thread control block at which the variable is located. */
24211 /* Nothing should follow this operator, so the top of stack would
24212 be returned. */
24213 /* This is valid for partial global symbols, but the variable's
24214 address will be bogus in the psymtab. Make it always at least
24215 non-zero to not look as a variable garbage collected by linker
24216 which have DW_OP_addr 0. */
24217 if (i < size)
24218 dwarf2_complex_location_expr_complaint ();
24219 stack[stacki]++;
24220 break;
24221
24222 case DW_OP_GNU_uninit:
24223 break;
24224
24225 case DW_OP_addrx:
24226 case DW_OP_GNU_addr_index:
24227 case DW_OP_GNU_const_index:
24228 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24229 &bytes_read);
24230 i += bytes_read;
24231 break;
24232
24233 default:
24234 {
24235 const char *name = get_DW_OP_name (op);
24236
24237 if (name)
24238 complaint (_("unsupported stack op: '%s'"),
24239 name);
24240 else
24241 complaint (_("unsupported stack op: '%02x'"),
24242 op);
24243 }
24244
24245 return (stack[stacki]);
24246 }
24247
24248 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24249 outside of the allocated space. Also enforce minimum>0. */
24250 if (stacki >= ARRAY_SIZE (stack) - 1)
24251 {
24252 complaint (_("location description stack overflow"));
24253 return 0;
24254 }
24255
24256 if (stacki <= 0)
24257 {
24258 complaint (_("location description stack underflow"));
24259 return 0;
24260 }
24261 }
24262 return (stack[stacki]);
24263 }
24264
24265 /* memory allocation interface */
24266
24267 static struct dwarf_block *
24268 dwarf_alloc_block (struct dwarf2_cu *cu)
24269 {
24270 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24271 }
24272
24273 static struct die_info *
24274 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24275 {
24276 struct die_info *die;
24277 size_t size = sizeof (struct die_info);
24278
24279 if (num_attrs > 1)
24280 size += (num_attrs - 1) * sizeof (struct attribute);
24281
24282 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24283 memset (die, 0, sizeof (struct die_info));
24284 return (die);
24285 }
24286
24287 \f
24288 /* Macro support. */
24289
24290 /* Return file name relative to the compilation directory of file number I in
24291 *LH's file name table. The result is allocated using xmalloc; the caller is
24292 responsible for freeing it. */
24293
24294 static char *
24295 file_file_name (int file, struct line_header *lh)
24296 {
24297 /* Is the file number a valid index into the line header's file name
24298 table? Remember that file numbers start with one, not zero. */
24299 if (lh->is_valid_file_index (file))
24300 {
24301 const file_entry *fe = lh->file_name_at (file);
24302
24303 if (!IS_ABSOLUTE_PATH (fe->name))
24304 {
24305 const char *dir = fe->include_dir (lh);
24306 if (dir != NULL)
24307 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
24308 }
24309 return xstrdup (fe->name);
24310 }
24311 else
24312 {
24313 /* The compiler produced a bogus file number. We can at least
24314 record the macro definitions made in the file, even if we
24315 won't be able to find the file by name. */
24316 char fake_name[80];
24317
24318 xsnprintf (fake_name, sizeof (fake_name),
24319 "<bad macro file number %d>", file);
24320
24321 complaint (_("bad file number in macro information (%d)"),
24322 file);
24323
24324 return xstrdup (fake_name);
24325 }
24326 }
24327
24328 /* Return the full name of file number I in *LH's file name table.
24329 Use COMP_DIR as the name of the current directory of the
24330 compilation. The result is allocated using xmalloc; the caller is
24331 responsible for freeing it. */
24332 static char *
24333 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24334 {
24335 /* Is the file number a valid index into the line header's file name
24336 table? Remember that file numbers start with one, not zero. */
24337 if (lh->is_valid_file_index (file))
24338 {
24339 char *relative = file_file_name (file, lh);
24340
24341 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24342 return relative;
24343 return reconcat (relative, comp_dir, SLASH_STRING,
24344 relative, (char *) NULL);
24345 }
24346 else
24347 return file_file_name (file, lh);
24348 }
24349
24350
24351 static struct macro_source_file *
24352 macro_start_file (struct dwarf2_cu *cu,
24353 int file, int line,
24354 struct macro_source_file *current_file,
24355 struct line_header *lh)
24356 {
24357 /* File name relative to the compilation directory of this source file. */
24358 char *file_name = file_file_name (file, lh);
24359
24360 if (! current_file)
24361 {
24362 /* Note: We don't create a macro table for this compilation unit
24363 at all until we actually get a filename. */
24364 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
24365
24366 /* If we have no current file, then this must be the start_file
24367 directive for the compilation unit's main source file. */
24368 current_file = macro_set_main (macro_table, file_name);
24369 macro_define_special (macro_table);
24370 }
24371 else
24372 current_file = macro_include (current_file, line, file_name);
24373
24374 xfree (file_name);
24375
24376 return current_file;
24377 }
24378
24379 static const char *
24380 consume_improper_spaces (const char *p, const char *body)
24381 {
24382 if (*p == ' ')
24383 {
24384 complaint (_("macro definition contains spaces "
24385 "in formal argument list:\n`%s'"),
24386 body);
24387
24388 while (*p == ' ')
24389 p++;
24390 }
24391
24392 return p;
24393 }
24394
24395
24396 static void
24397 parse_macro_definition (struct macro_source_file *file, int line,
24398 const char *body)
24399 {
24400 const char *p;
24401
24402 /* The body string takes one of two forms. For object-like macro
24403 definitions, it should be:
24404
24405 <macro name> " " <definition>
24406
24407 For function-like macro definitions, it should be:
24408
24409 <macro name> "() " <definition>
24410 or
24411 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24412
24413 Spaces may appear only where explicitly indicated, and in the
24414 <definition>.
24415
24416 The Dwarf 2 spec says that an object-like macro's name is always
24417 followed by a space, but versions of GCC around March 2002 omit
24418 the space when the macro's definition is the empty string.
24419
24420 The Dwarf 2 spec says that there should be no spaces between the
24421 formal arguments in a function-like macro's formal argument list,
24422 but versions of GCC around March 2002 include spaces after the
24423 commas. */
24424
24425
24426 /* Find the extent of the macro name. The macro name is terminated
24427 by either a space or null character (for an object-like macro) or
24428 an opening paren (for a function-like macro). */
24429 for (p = body; *p; p++)
24430 if (*p == ' ' || *p == '(')
24431 break;
24432
24433 if (*p == ' ' || *p == '\0')
24434 {
24435 /* It's an object-like macro. */
24436 int name_len = p - body;
24437 std::string name (body, name_len);
24438 const char *replacement;
24439
24440 if (*p == ' ')
24441 replacement = body + name_len + 1;
24442 else
24443 {
24444 dwarf2_macro_malformed_definition_complaint (body);
24445 replacement = body + name_len;
24446 }
24447
24448 macro_define_object (file, line, name.c_str (), replacement);
24449 }
24450 else if (*p == '(')
24451 {
24452 /* It's a function-like macro. */
24453 std::string name (body, p - body);
24454 int argc = 0;
24455 int argv_size = 1;
24456 char **argv = XNEWVEC (char *, argv_size);
24457
24458 p++;
24459
24460 p = consume_improper_spaces (p, body);
24461
24462 /* Parse the formal argument list. */
24463 while (*p && *p != ')')
24464 {
24465 /* Find the extent of the current argument name. */
24466 const char *arg_start = p;
24467
24468 while (*p && *p != ',' && *p != ')' && *p != ' ')
24469 p++;
24470
24471 if (! *p || p == arg_start)
24472 dwarf2_macro_malformed_definition_complaint (body);
24473 else
24474 {
24475 /* Make sure argv has room for the new argument. */
24476 if (argc >= argv_size)
24477 {
24478 argv_size *= 2;
24479 argv = XRESIZEVEC (char *, argv, argv_size);
24480 }
24481
24482 argv[argc++] = savestring (arg_start, p - arg_start);
24483 }
24484
24485 p = consume_improper_spaces (p, body);
24486
24487 /* Consume the comma, if present. */
24488 if (*p == ',')
24489 {
24490 p++;
24491
24492 p = consume_improper_spaces (p, body);
24493 }
24494 }
24495
24496 if (*p == ')')
24497 {
24498 p++;
24499
24500 if (*p == ' ')
24501 /* Perfectly formed definition, no complaints. */
24502 macro_define_function (file, line, name.c_str (),
24503 argc, (const char **) argv,
24504 p + 1);
24505 else if (*p == '\0')
24506 {
24507 /* Complain, but do define it. */
24508 dwarf2_macro_malformed_definition_complaint (body);
24509 macro_define_function (file, line, name.c_str (),
24510 argc, (const char **) argv,
24511 p);
24512 }
24513 else
24514 /* Just complain. */
24515 dwarf2_macro_malformed_definition_complaint (body);
24516 }
24517 else
24518 /* Just complain. */
24519 dwarf2_macro_malformed_definition_complaint (body);
24520
24521 {
24522 int i;
24523
24524 for (i = 0; i < argc; i++)
24525 xfree (argv[i]);
24526 }
24527 xfree (argv);
24528 }
24529 else
24530 dwarf2_macro_malformed_definition_complaint (body);
24531 }
24532
24533 /* Skip some bytes from BYTES according to the form given in FORM.
24534 Returns the new pointer. */
24535
24536 static const gdb_byte *
24537 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24538 enum dwarf_form form,
24539 unsigned int offset_size,
24540 struct dwarf2_section_info *section)
24541 {
24542 unsigned int bytes_read;
24543
24544 switch (form)
24545 {
24546 case DW_FORM_data1:
24547 case DW_FORM_flag:
24548 ++bytes;
24549 break;
24550
24551 case DW_FORM_data2:
24552 bytes += 2;
24553 break;
24554
24555 case DW_FORM_data4:
24556 bytes += 4;
24557 break;
24558
24559 case DW_FORM_data8:
24560 bytes += 8;
24561 break;
24562
24563 case DW_FORM_data16:
24564 bytes += 16;
24565 break;
24566
24567 case DW_FORM_string:
24568 read_direct_string (abfd, bytes, &bytes_read);
24569 bytes += bytes_read;
24570 break;
24571
24572 case DW_FORM_sec_offset:
24573 case DW_FORM_strp:
24574 case DW_FORM_GNU_strp_alt:
24575 bytes += offset_size;
24576 break;
24577
24578 case DW_FORM_block:
24579 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24580 bytes += bytes_read;
24581 break;
24582
24583 case DW_FORM_block1:
24584 bytes += 1 + read_1_byte (abfd, bytes);
24585 break;
24586 case DW_FORM_block2:
24587 bytes += 2 + read_2_bytes (abfd, bytes);
24588 break;
24589 case DW_FORM_block4:
24590 bytes += 4 + read_4_bytes (abfd, bytes);
24591 break;
24592
24593 case DW_FORM_addrx:
24594 case DW_FORM_sdata:
24595 case DW_FORM_strx:
24596 case DW_FORM_udata:
24597 case DW_FORM_GNU_addr_index:
24598 case DW_FORM_GNU_str_index:
24599 bytes = gdb_skip_leb128 (bytes, buffer_end);
24600 if (bytes == NULL)
24601 {
24602 dwarf2_section_buffer_overflow_complaint (section);
24603 return NULL;
24604 }
24605 break;
24606
24607 case DW_FORM_implicit_const:
24608 break;
24609
24610 default:
24611 {
24612 complaint (_("invalid form 0x%x in `%s'"),
24613 form, get_section_name (section));
24614 return NULL;
24615 }
24616 }
24617
24618 return bytes;
24619 }
24620
24621 /* A helper for dwarf_decode_macros that handles skipping an unknown
24622 opcode. Returns an updated pointer to the macro data buffer; or,
24623 on error, issues a complaint and returns NULL. */
24624
24625 static const gdb_byte *
24626 skip_unknown_opcode (unsigned int opcode,
24627 const gdb_byte **opcode_definitions,
24628 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24629 bfd *abfd,
24630 unsigned int offset_size,
24631 struct dwarf2_section_info *section)
24632 {
24633 unsigned int bytes_read, i;
24634 unsigned long arg;
24635 const gdb_byte *defn;
24636
24637 if (opcode_definitions[opcode] == NULL)
24638 {
24639 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24640 opcode);
24641 return NULL;
24642 }
24643
24644 defn = opcode_definitions[opcode];
24645 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24646 defn += bytes_read;
24647
24648 for (i = 0; i < arg; ++i)
24649 {
24650 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24651 (enum dwarf_form) defn[i], offset_size,
24652 section);
24653 if (mac_ptr == NULL)
24654 {
24655 /* skip_form_bytes already issued the complaint. */
24656 return NULL;
24657 }
24658 }
24659
24660 return mac_ptr;
24661 }
24662
24663 /* A helper function which parses the header of a macro section.
24664 If the macro section is the extended (for now called "GNU") type,
24665 then this updates *OFFSET_SIZE. Returns a pointer to just after
24666 the header, or issues a complaint and returns NULL on error. */
24667
24668 static const gdb_byte *
24669 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24670 bfd *abfd,
24671 const gdb_byte *mac_ptr,
24672 unsigned int *offset_size,
24673 int section_is_gnu)
24674 {
24675 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24676
24677 if (section_is_gnu)
24678 {
24679 unsigned int version, flags;
24680
24681 version = read_2_bytes (abfd, mac_ptr);
24682 if (version != 4 && version != 5)
24683 {
24684 complaint (_("unrecognized version `%d' in .debug_macro section"),
24685 version);
24686 return NULL;
24687 }
24688 mac_ptr += 2;
24689
24690 flags = read_1_byte (abfd, mac_ptr);
24691 ++mac_ptr;
24692 *offset_size = (flags & 1) ? 8 : 4;
24693
24694 if ((flags & 2) != 0)
24695 /* We don't need the line table offset. */
24696 mac_ptr += *offset_size;
24697
24698 /* Vendor opcode descriptions. */
24699 if ((flags & 4) != 0)
24700 {
24701 unsigned int i, count;
24702
24703 count = read_1_byte (abfd, mac_ptr);
24704 ++mac_ptr;
24705 for (i = 0; i < count; ++i)
24706 {
24707 unsigned int opcode, bytes_read;
24708 unsigned long arg;
24709
24710 opcode = read_1_byte (abfd, mac_ptr);
24711 ++mac_ptr;
24712 opcode_definitions[opcode] = mac_ptr;
24713 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24714 mac_ptr += bytes_read;
24715 mac_ptr += arg;
24716 }
24717 }
24718 }
24719
24720 return mac_ptr;
24721 }
24722
24723 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24724 including DW_MACRO_import. */
24725
24726 static void
24727 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24728 bfd *abfd,
24729 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24730 struct macro_source_file *current_file,
24731 struct line_header *lh,
24732 struct dwarf2_section_info *section,
24733 int section_is_gnu, int section_is_dwz,
24734 unsigned int offset_size,
24735 htab_t include_hash)
24736 {
24737 struct dwarf2_per_objfile *dwarf2_per_objfile
24738 = cu->per_cu->dwarf2_per_objfile;
24739 struct objfile *objfile = dwarf2_per_objfile->objfile;
24740 enum dwarf_macro_record_type macinfo_type;
24741 int at_commandline;
24742 const gdb_byte *opcode_definitions[256];
24743
24744 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24745 &offset_size, section_is_gnu);
24746 if (mac_ptr == NULL)
24747 {
24748 /* We already issued a complaint. */
24749 return;
24750 }
24751
24752 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24753 GDB is still reading the definitions from command line. First
24754 DW_MACINFO_start_file will need to be ignored as it was already executed
24755 to create CURRENT_FILE for the main source holding also the command line
24756 definitions. On first met DW_MACINFO_start_file this flag is reset to
24757 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24758
24759 at_commandline = 1;
24760
24761 do
24762 {
24763 /* Do we at least have room for a macinfo type byte? */
24764 if (mac_ptr >= mac_end)
24765 {
24766 dwarf2_section_buffer_overflow_complaint (section);
24767 break;
24768 }
24769
24770 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24771 mac_ptr++;
24772
24773 /* Note that we rely on the fact that the corresponding GNU and
24774 DWARF constants are the same. */
24775 DIAGNOSTIC_PUSH
24776 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24777 switch (macinfo_type)
24778 {
24779 /* A zero macinfo type indicates the end of the macro
24780 information. */
24781 case 0:
24782 break;
24783
24784 case DW_MACRO_define:
24785 case DW_MACRO_undef:
24786 case DW_MACRO_define_strp:
24787 case DW_MACRO_undef_strp:
24788 case DW_MACRO_define_sup:
24789 case DW_MACRO_undef_sup:
24790 {
24791 unsigned int bytes_read;
24792 int line;
24793 const char *body;
24794 int is_define;
24795
24796 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24797 mac_ptr += bytes_read;
24798
24799 if (macinfo_type == DW_MACRO_define
24800 || macinfo_type == DW_MACRO_undef)
24801 {
24802 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24803 mac_ptr += bytes_read;
24804 }
24805 else
24806 {
24807 LONGEST str_offset;
24808
24809 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24810 mac_ptr += offset_size;
24811
24812 if (macinfo_type == DW_MACRO_define_sup
24813 || macinfo_type == DW_MACRO_undef_sup
24814 || section_is_dwz)
24815 {
24816 struct dwz_file *dwz
24817 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24818
24819 body = read_indirect_string_from_dwz (objfile,
24820 dwz, str_offset);
24821 }
24822 else
24823 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24824 abfd, str_offset);
24825 }
24826
24827 is_define = (macinfo_type == DW_MACRO_define
24828 || macinfo_type == DW_MACRO_define_strp
24829 || macinfo_type == DW_MACRO_define_sup);
24830 if (! current_file)
24831 {
24832 /* DWARF violation as no main source is present. */
24833 complaint (_("debug info with no main source gives macro %s "
24834 "on line %d: %s"),
24835 is_define ? _("definition") : _("undefinition"),
24836 line, body);
24837 break;
24838 }
24839 if ((line == 0 && !at_commandline)
24840 || (line != 0 && at_commandline))
24841 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
24842 at_commandline ? _("command-line") : _("in-file"),
24843 is_define ? _("definition") : _("undefinition"),
24844 line == 0 ? _("zero") : _("non-zero"), line, body);
24845
24846 if (body == NULL)
24847 {
24848 /* Fedora's rpm-build's "debugedit" binary
24849 corrupted .debug_macro sections.
24850
24851 For more info, see
24852 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24853 complaint (_("debug info gives %s invalid macro %s "
24854 "without body (corrupted?) at line %d "
24855 "on file %s"),
24856 at_commandline ? _("command-line") : _("in-file"),
24857 is_define ? _("definition") : _("undefinition"),
24858 line, current_file->filename);
24859 }
24860 else if (is_define)
24861 parse_macro_definition (current_file, line, body);
24862 else
24863 {
24864 gdb_assert (macinfo_type == DW_MACRO_undef
24865 || macinfo_type == DW_MACRO_undef_strp
24866 || macinfo_type == DW_MACRO_undef_sup);
24867 macro_undef (current_file, line, body);
24868 }
24869 }
24870 break;
24871
24872 case DW_MACRO_start_file:
24873 {
24874 unsigned int bytes_read;
24875 int line, file;
24876
24877 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24878 mac_ptr += bytes_read;
24879 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24880 mac_ptr += bytes_read;
24881
24882 if ((line == 0 && !at_commandline)
24883 || (line != 0 && at_commandline))
24884 complaint (_("debug info gives source %d included "
24885 "from %s at %s line %d"),
24886 file, at_commandline ? _("command-line") : _("file"),
24887 line == 0 ? _("zero") : _("non-zero"), line);
24888
24889 if (at_commandline)
24890 {
24891 /* This DW_MACRO_start_file was executed in the
24892 pass one. */
24893 at_commandline = 0;
24894 }
24895 else
24896 current_file = macro_start_file (cu, file, line, current_file,
24897 lh);
24898 }
24899 break;
24900
24901 case DW_MACRO_end_file:
24902 if (! current_file)
24903 complaint (_("macro debug info has an unmatched "
24904 "`close_file' directive"));
24905 else
24906 {
24907 current_file = current_file->included_by;
24908 if (! current_file)
24909 {
24910 enum dwarf_macro_record_type next_type;
24911
24912 /* GCC circa March 2002 doesn't produce the zero
24913 type byte marking the end of the compilation
24914 unit. Complain if it's not there, but exit no
24915 matter what. */
24916
24917 /* Do we at least have room for a macinfo type byte? */
24918 if (mac_ptr >= mac_end)
24919 {
24920 dwarf2_section_buffer_overflow_complaint (section);
24921 return;
24922 }
24923
24924 /* We don't increment mac_ptr here, so this is just
24925 a look-ahead. */
24926 next_type
24927 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24928 mac_ptr);
24929 if (next_type != 0)
24930 complaint (_("no terminating 0-type entry for "
24931 "macros in `.debug_macinfo' section"));
24932
24933 return;
24934 }
24935 }
24936 break;
24937
24938 case DW_MACRO_import:
24939 case DW_MACRO_import_sup:
24940 {
24941 LONGEST offset;
24942 void **slot;
24943 bfd *include_bfd = abfd;
24944 struct dwarf2_section_info *include_section = section;
24945 const gdb_byte *include_mac_end = mac_end;
24946 int is_dwz = section_is_dwz;
24947 const gdb_byte *new_mac_ptr;
24948
24949 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24950 mac_ptr += offset_size;
24951
24952 if (macinfo_type == DW_MACRO_import_sup)
24953 {
24954 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24955
24956 dwarf2_read_section (objfile, &dwz->macro);
24957
24958 include_section = &dwz->macro;
24959 include_bfd = get_section_bfd_owner (include_section);
24960 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24961 is_dwz = 1;
24962 }
24963
24964 new_mac_ptr = include_section->buffer + offset;
24965 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24966
24967 if (*slot != NULL)
24968 {
24969 /* This has actually happened; see
24970 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24971 complaint (_("recursive DW_MACRO_import in "
24972 ".debug_macro section"));
24973 }
24974 else
24975 {
24976 *slot = (void *) new_mac_ptr;
24977
24978 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
24979 include_mac_end, current_file, lh,
24980 section, section_is_gnu, is_dwz,
24981 offset_size, include_hash);
24982
24983 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24984 }
24985 }
24986 break;
24987
24988 case DW_MACINFO_vendor_ext:
24989 if (!section_is_gnu)
24990 {
24991 unsigned int bytes_read;
24992
24993 /* This reads the constant, but since we don't recognize
24994 any vendor extensions, we ignore it. */
24995 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24996 mac_ptr += bytes_read;
24997 read_direct_string (abfd, mac_ptr, &bytes_read);
24998 mac_ptr += bytes_read;
24999
25000 /* We don't recognize any vendor extensions. */
25001 break;
25002 }
25003 /* FALLTHROUGH */
25004
25005 default:
25006 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25007 mac_ptr, mac_end, abfd, offset_size,
25008 section);
25009 if (mac_ptr == NULL)
25010 return;
25011 break;
25012 }
25013 DIAGNOSTIC_POP
25014 } while (macinfo_type != 0);
25015 }
25016
25017 static void
25018 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
25019 int section_is_gnu)
25020 {
25021 struct dwarf2_per_objfile *dwarf2_per_objfile
25022 = cu->per_cu->dwarf2_per_objfile;
25023 struct objfile *objfile = dwarf2_per_objfile->objfile;
25024 struct line_header *lh = cu->line_header;
25025 bfd *abfd;
25026 const gdb_byte *mac_ptr, *mac_end;
25027 struct macro_source_file *current_file = 0;
25028 enum dwarf_macro_record_type macinfo_type;
25029 unsigned int offset_size = cu->header.offset_size;
25030 const gdb_byte *opcode_definitions[256];
25031 void **slot;
25032 struct dwarf2_section_info *section;
25033 const char *section_name;
25034
25035 if (cu->dwo_unit != NULL)
25036 {
25037 if (section_is_gnu)
25038 {
25039 section = &cu->dwo_unit->dwo_file->sections.macro;
25040 section_name = ".debug_macro.dwo";
25041 }
25042 else
25043 {
25044 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25045 section_name = ".debug_macinfo.dwo";
25046 }
25047 }
25048 else
25049 {
25050 if (section_is_gnu)
25051 {
25052 section = &dwarf2_per_objfile->macro;
25053 section_name = ".debug_macro";
25054 }
25055 else
25056 {
25057 section = &dwarf2_per_objfile->macinfo;
25058 section_name = ".debug_macinfo";
25059 }
25060 }
25061
25062 dwarf2_read_section (objfile, section);
25063 if (section->buffer == NULL)
25064 {
25065 complaint (_("missing %s section"), section_name);
25066 return;
25067 }
25068 abfd = get_section_bfd_owner (section);
25069
25070 /* First pass: Find the name of the base filename.
25071 This filename is needed in order to process all macros whose definition
25072 (or undefinition) comes from the command line. These macros are defined
25073 before the first DW_MACINFO_start_file entry, and yet still need to be
25074 associated to the base file.
25075
25076 To determine the base file name, we scan the macro definitions until we
25077 reach the first DW_MACINFO_start_file entry. We then initialize
25078 CURRENT_FILE accordingly so that any macro definition found before the
25079 first DW_MACINFO_start_file can still be associated to the base file. */
25080
25081 mac_ptr = section->buffer + offset;
25082 mac_end = section->buffer + section->size;
25083
25084 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25085 &offset_size, section_is_gnu);
25086 if (mac_ptr == NULL)
25087 {
25088 /* We already issued a complaint. */
25089 return;
25090 }
25091
25092 do
25093 {
25094 /* Do we at least have room for a macinfo type byte? */
25095 if (mac_ptr >= mac_end)
25096 {
25097 /* Complaint is printed during the second pass as GDB will probably
25098 stop the first pass earlier upon finding
25099 DW_MACINFO_start_file. */
25100 break;
25101 }
25102
25103 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
25104 mac_ptr++;
25105
25106 /* Note that we rely on the fact that the corresponding GNU and
25107 DWARF constants are the same. */
25108 DIAGNOSTIC_PUSH
25109 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
25110 switch (macinfo_type)
25111 {
25112 /* A zero macinfo type indicates the end of the macro
25113 information. */
25114 case 0:
25115 break;
25116
25117 case DW_MACRO_define:
25118 case DW_MACRO_undef:
25119 /* Only skip the data by MAC_PTR. */
25120 {
25121 unsigned int bytes_read;
25122
25123 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25124 mac_ptr += bytes_read;
25125 read_direct_string (abfd, mac_ptr, &bytes_read);
25126 mac_ptr += bytes_read;
25127 }
25128 break;
25129
25130 case DW_MACRO_start_file:
25131 {
25132 unsigned int bytes_read;
25133 int line, file;
25134
25135 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25136 mac_ptr += bytes_read;
25137 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25138 mac_ptr += bytes_read;
25139
25140 current_file = macro_start_file (cu, file, line, current_file, lh);
25141 }
25142 break;
25143
25144 case DW_MACRO_end_file:
25145 /* No data to skip by MAC_PTR. */
25146 break;
25147
25148 case DW_MACRO_define_strp:
25149 case DW_MACRO_undef_strp:
25150 case DW_MACRO_define_sup:
25151 case DW_MACRO_undef_sup:
25152 {
25153 unsigned int bytes_read;
25154
25155 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25156 mac_ptr += bytes_read;
25157 mac_ptr += offset_size;
25158 }
25159 break;
25160
25161 case DW_MACRO_import:
25162 case DW_MACRO_import_sup:
25163 /* Note that, according to the spec, a transparent include
25164 chain cannot call DW_MACRO_start_file. So, we can just
25165 skip this opcode. */
25166 mac_ptr += offset_size;
25167 break;
25168
25169 case DW_MACINFO_vendor_ext:
25170 /* Only skip the data by MAC_PTR. */
25171 if (!section_is_gnu)
25172 {
25173 unsigned int bytes_read;
25174
25175 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25176 mac_ptr += bytes_read;
25177 read_direct_string (abfd, mac_ptr, &bytes_read);
25178 mac_ptr += bytes_read;
25179 }
25180 /* FALLTHROUGH */
25181
25182 default:
25183 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25184 mac_ptr, mac_end, abfd, offset_size,
25185 section);
25186 if (mac_ptr == NULL)
25187 return;
25188 break;
25189 }
25190 DIAGNOSTIC_POP
25191 } while (macinfo_type != 0 && current_file == NULL);
25192
25193 /* Second pass: Process all entries.
25194
25195 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25196 command-line macro definitions/undefinitions. This flag is unset when we
25197 reach the first DW_MACINFO_start_file entry. */
25198
25199 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25200 htab_eq_pointer,
25201 NULL, xcalloc, xfree));
25202 mac_ptr = section->buffer + offset;
25203 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
25204 *slot = (void *) mac_ptr;
25205 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
25206 current_file, lh, section,
25207 section_is_gnu, 0, offset_size,
25208 include_hash.get ());
25209 }
25210
25211 /* Check if the attribute's form is a DW_FORM_block*
25212 if so return true else false. */
25213
25214 static int
25215 attr_form_is_block (const struct attribute *attr)
25216 {
25217 return (attr == NULL ? 0 :
25218 attr->form == DW_FORM_block1
25219 || attr->form == DW_FORM_block2
25220 || attr->form == DW_FORM_block4
25221 || attr->form == DW_FORM_block
25222 || attr->form == DW_FORM_exprloc);
25223 }
25224
25225 /* Return non-zero if ATTR's value is a section offset --- classes
25226 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25227 You may use DW_UNSND (attr) to retrieve such offsets.
25228
25229 Section 7.5.4, "Attribute Encodings", explains that no attribute
25230 may have a value that belongs to more than one of these classes; it
25231 would be ambiguous if we did, because we use the same forms for all
25232 of them. */
25233
25234 static int
25235 attr_form_is_section_offset (const struct attribute *attr)
25236 {
25237 return (attr->form == DW_FORM_data4
25238 || attr->form == DW_FORM_data8
25239 || attr->form == DW_FORM_sec_offset);
25240 }
25241
25242 /* Return non-zero if ATTR's value falls in the 'constant' class, or
25243 zero otherwise. When this function returns true, you can apply
25244 dwarf2_get_attr_constant_value to it.
25245
25246 However, note that for some attributes you must check
25247 attr_form_is_section_offset before using this test. DW_FORM_data4
25248 and DW_FORM_data8 are members of both the constant class, and of
25249 the classes that contain offsets into other debug sections
25250 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25251 that, if an attribute's can be either a constant or one of the
25252 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25253 taken as section offsets, not constants.
25254
25255 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25256 cannot handle that. */
25257
25258 static int
25259 attr_form_is_constant (const struct attribute *attr)
25260 {
25261 switch (attr->form)
25262 {
25263 case DW_FORM_sdata:
25264 case DW_FORM_udata:
25265 case DW_FORM_data1:
25266 case DW_FORM_data2:
25267 case DW_FORM_data4:
25268 case DW_FORM_data8:
25269 case DW_FORM_implicit_const:
25270 return 1;
25271 default:
25272 return 0;
25273 }
25274 }
25275
25276
25277 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25278 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25279
25280 static int
25281 attr_form_is_ref (const struct attribute *attr)
25282 {
25283 switch (attr->form)
25284 {
25285 case DW_FORM_ref_addr:
25286 case DW_FORM_ref1:
25287 case DW_FORM_ref2:
25288 case DW_FORM_ref4:
25289 case DW_FORM_ref8:
25290 case DW_FORM_ref_udata:
25291 case DW_FORM_GNU_ref_alt:
25292 return 1;
25293 default:
25294 return 0;
25295 }
25296 }
25297
25298 /* Return the .debug_loc section to use for CU.
25299 For DWO files use .debug_loc.dwo. */
25300
25301 static struct dwarf2_section_info *
25302 cu_debug_loc_section (struct dwarf2_cu *cu)
25303 {
25304 struct dwarf2_per_objfile *dwarf2_per_objfile
25305 = cu->per_cu->dwarf2_per_objfile;
25306
25307 if (cu->dwo_unit)
25308 {
25309 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25310
25311 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25312 }
25313 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25314 : &dwarf2_per_objfile->loc);
25315 }
25316
25317 /* A helper function that fills in a dwarf2_loclist_baton. */
25318
25319 static void
25320 fill_in_loclist_baton (struct dwarf2_cu *cu,
25321 struct dwarf2_loclist_baton *baton,
25322 const struct attribute *attr)
25323 {
25324 struct dwarf2_per_objfile *dwarf2_per_objfile
25325 = cu->per_cu->dwarf2_per_objfile;
25326 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25327
25328 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25329
25330 baton->per_cu = cu->per_cu;
25331 gdb_assert (baton->per_cu);
25332 /* We don't know how long the location list is, but make sure we
25333 don't run off the edge of the section. */
25334 baton->size = section->size - DW_UNSND (attr);
25335 baton->data = section->buffer + DW_UNSND (attr);
25336 baton->base_address = cu->base_address;
25337 baton->from_dwo = cu->dwo_unit != NULL;
25338 }
25339
25340 static void
25341 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25342 struct dwarf2_cu *cu, int is_block)
25343 {
25344 struct dwarf2_per_objfile *dwarf2_per_objfile
25345 = cu->per_cu->dwarf2_per_objfile;
25346 struct objfile *objfile = dwarf2_per_objfile->objfile;
25347 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25348
25349 if (attr_form_is_section_offset (attr)
25350 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25351 the section. If so, fall through to the complaint in the
25352 other branch. */
25353 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25354 {
25355 struct dwarf2_loclist_baton *baton;
25356
25357 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25358
25359 fill_in_loclist_baton (cu, baton, attr);
25360
25361 if (cu->base_known == 0)
25362 complaint (_("Location list used without "
25363 "specifying the CU base address."));
25364
25365 SYMBOL_ACLASS_INDEX (sym) = (is_block
25366 ? dwarf2_loclist_block_index
25367 : dwarf2_loclist_index);
25368 SYMBOL_LOCATION_BATON (sym) = baton;
25369 }
25370 else
25371 {
25372 struct dwarf2_locexpr_baton *baton;
25373
25374 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25375 baton->per_cu = cu->per_cu;
25376 gdb_assert (baton->per_cu);
25377
25378 if (attr_form_is_block (attr))
25379 {
25380 /* Note that we're just copying the block's data pointer
25381 here, not the actual data. We're still pointing into the
25382 info_buffer for SYM's objfile; right now we never release
25383 that buffer, but when we do clean up properly this may
25384 need to change. */
25385 baton->size = DW_BLOCK (attr)->size;
25386 baton->data = DW_BLOCK (attr)->data;
25387 }
25388 else
25389 {
25390 dwarf2_invalid_attrib_class_complaint ("location description",
25391 sym->natural_name ());
25392 baton->size = 0;
25393 }
25394
25395 SYMBOL_ACLASS_INDEX (sym) = (is_block
25396 ? dwarf2_locexpr_block_index
25397 : dwarf2_locexpr_index);
25398 SYMBOL_LOCATION_BATON (sym) = baton;
25399 }
25400 }
25401
25402 /* Return the OBJFILE associated with the compilation unit CU. If CU
25403 came from a separate debuginfo file, then the master objfile is
25404 returned. */
25405
25406 struct objfile *
25407 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25408 {
25409 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25410
25411 /* Return the master objfile, so that we can report and look up the
25412 correct file containing this variable. */
25413 if (objfile->separate_debug_objfile_backlink)
25414 objfile = objfile->separate_debug_objfile_backlink;
25415
25416 return objfile;
25417 }
25418
25419 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25420 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25421 CU_HEADERP first. */
25422
25423 static const struct comp_unit_head *
25424 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25425 struct dwarf2_per_cu_data *per_cu)
25426 {
25427 const gdb_byte *info_ptr;
25428
25429 if (per_cu->cu)
25430 return &per_cu->cu->header;
25431
25432 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25433
25434 memset (cu_headerp, 0, sizeof (*cu_headerp));
25435 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25436 rcuh_kind::COMPILE);
25437
25438 return cu_headerp;
25439 }
25440
25441 /* Return the address size given in the compilation unit header for CU. */
25442
25443 int
25444 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25445 {
25446 struct comp_unit_head cu_header_local;
25447 const struct comp_unit_head *cu_headerp;
25448
25449 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25450
25451 return cu_headerp->addr_size;
25452 }
25453
25454 /* Return the offset size given in the compilation unit header for CU. */
25455
25456 int
25457 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25458 {
25459 struct comp_unit_head cu_header_local;
25460 const struct comp_unit_head *cu_headerp;
25461
25462 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25463
25464 return cu_headerp->offset_size;
25465 }
25466
25467 /* See its dwarf2loc.h declaration. */
25468
25469 int
25470 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25471 {
25472 struct comp_unit_head cu_header_local;
25473 const struct comp_unit_head *cu_headerp;
25474
25475 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25476
25477 if (cu_headerp->version == 2)
25478 return cu_headerp->addr_size;
25479 else
25480 return cu_headerp->offset_size;
25481 }
25482
25483 /* Return the text offset of the CU. The returned offset comes from
25484 this CU's objfile. If this objfile came from a separate debuginfo
25485 file, then the offset may be different from the corresponding
25486 offset in the parent objfile. */
25487
25488 CORE_ADDR
25489 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25490 {
25491 return per_cu->dwarf2_per_objfile->objfile->text_section_offset ();
25492 }
25493
25494 /* Return a type that is a generic pointer type, the size of which matches
25495 the address size given in the compilation unit header for PER_CU. */
25496 static struct type *
25497 dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25498 {
25499 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25500 struct type *void_type = objfile_type (objfile)->builtin_void;
25501 struct type *addr_type = lookup_pointer_type (void_type);
25502 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25503
25504 if (TYPE_LENGTH (addr_type) == addr_size)
25505 return addr_type;
25506
25507 addr_type
25508 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25509 return addr_type;
25510 }
25511
25512 /* Return DWARF version number of PER_CU. */
25513
25514 short
25515 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25516 {
25517 return per_cu->dwarf_version;
25518 }
25519
25520 /* Locate the .debug_info compilation unit from CU's objfile which contains
25521 the DIE at OFFSET. Raises an error on failure. */
25522
25523 static struct dwarf2_per_cu_data *
25524 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25525 unsigned int offset_in_dwz,
25526 struct dwarf2_per_objfile *dwarf2_per_objfile)
25527 {
25528 struct dwarf2_per_cu_data *this_cu;
25529 int low, high;
25530
25531 low = 0;
25532 high = dwarf2_per_objfile->all_comp_units.size () - 1;
25533 while (high > low)
25534 {
25535 struct dwarf2_per_cu_data *mid_cu;
25536 int mid = low + (high - low) / 2;
25537
25538 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25539 if (mid_cu->is_dwz > offset_in_dwz
25540 || (mid_cu->is_dwz == offset_in_dwz
25541 && mid_cu->sect_off + mid_cu->length >= sect_off))
25542 high = mid;
25543 else
25544 low = mid + 1;
25545 }
25546 gdb_assert (low == high);
25547 this_cu = dwarf2_per_objfile->all_comp_units[low];
25548 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25549 {
25550 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25551 error (_("Dwarf Error: could not find partial DIE containing "
25552 "offset %s [in module %s]"),
25553 sect_offset_str (sect_off),
25554 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25555
25556 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25557 <= sect_off);
25558 return dwarf2_per_objfile->all_comp_units[low-1];
25559 }
25560 else
25561 {
25562 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25563 && sect_off >= this_cu->sect_off + this_cu->length)
25564 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25565 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25566 return this_cu;
25567 }
25568 }
25569
25570 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25571
25572 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25573 : per_cu (per_cu_),
25574 mark (false),
25575 has_loclist (false),
25576 checked_producer (false),
25577 producer_is_gxx_lt_4_6 (false),
25578 producer_is_gcc_lt_4_3 (false),
25579 producer_is_icc (false),
25580 producer_is_icc_lt_14 (false),
25581 producer_is_codewarrior (false),
25582 processing_has_namespace_info (false)
25583 {
25584 per_cu->cu = this;
25585 }
25586
25587 /* Destroy a dwarf2_cu. */
25588
25589 dwarf2_cu::~dwarf2_cu ()
25590 {
25591 per_cu->cu = NULL;
25592 }
25593
25594 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25595
25596 static void
25597 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25598 enum language pretend_language)
25599 {
25600 struct attribute *attr;
25601
25602 /* Set the language we're debugging. */
25603 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25604 if (attr != nullptr)
25605 set_cu_language (DW_UNSND (attr), cu);
25606 else
25607 {
25608 cu->language = pretend_language;
25609 cu->language_defn = language_def (cu->language);
25610 }
25611
25612 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25613 }
25614
25615 /* Increase the age counter on each cached compilation unit, and free
25616 any that are too old. */
25617
25618 static void
25619 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25620 {
25621 struct dwarf2_per_cu_data *per_cu, **last_chain;
25622
25623 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25624 per_cu = dwarf2_per_objfile->read_in_chain;
25625 while (per_cu != NULL)
25626 {
25627 per_cu->cu->last_used ++;
25628 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25629 dwarf2_mark (per_cu->cu);
25630 per_cu = per_cu->cu->read_in_chain;
25631 }
25632
25633 per_cu = dwarf2_per_objfile->read_in_chain;
25634 last_chain = &dwarf2_per_objfile->read_in_chain;
25635 while (per_cu != NULL)
25636 {
25637 struct dwarf2_per_cu_data *next_cu;
25638
25639 next_cu = per_cu->cu->read_in_chain;
25640
25641 if (!per_cu->cu->mark)
25642 {
25643 delete per_cu->cu;
25644 *last_chain = next_cu;
25645 }
25646 else
25647 last_chain = &per_cu->cu->read_in_chain;
25648
25649 per_cu = next_cu;
25650 }
25651 }
25652
25653 /* Remove a single compilation unit from the cache. */
25654
25655 static void
25656 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25657 {
25658 struct dwarf2_per_cu_data *per_cu, **last_chain;
25659 struct dwarf2_per_objfile *dwarf2_per_objfile
25660 = target_per_cu->dwarf2_per_objfile;
25661
25662 per_cu = dwarf2_per_objfile->read_in_chain;
25663 last_chain = &dwarf2_per_objfile->read_in_chain;
25664 while (per_cu != NULL)
25665 {
25666 struct dwarf2_per_cu_data *next_cu;
25667
25668 next_cu = per_cu->cu->read_in_chain;
25669
25670 if (per_cu == target_per_cu)
25671 {
25672 delete per_cu->cu;
25673 per_cu->cu = NULL;
25674 *last_chain = next_cu;
25675 break;
25676 }
25677 else
25678 last_chain = &per_cu->cu->read_in_chain;
25679
25680 per_cu = next_cu;
25681 }
25682 }
25683
25684 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25685 We store these in a hash table separate from the DIEs, and preserve them
25686 when the DIEs are flushed out of cache.
25687
25688 The CU "per_cu" pointer is needed because offset alone is not enough to
25689 uniquely identify the type. A file may have multiple .debug_types sections,
25690 or the type may come from a DWO file. Furthermore, while it's more logical
25691 to use per_cu->section+offset, with Fission the section with the data is in
25692 the DWO file but we don't know that section at the point we need it.
25693 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25694 because we can enter the lookup routine, get_die_type_at_offset, from
25695 outside this file, and thus won't necessarily have PER_CU->cu.
25696 Fortunately, PER_CU is stable for the life of the objfile. */
25697
25698 struct dwarf2_per_cu_offset_and_type
25699 {
25700 const struct dwarf2_per_cu_data *per_cu;
25701 sect_offset sect_off;
25702 struct type *type;
25703 };
25704
25705 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25706
25707 static hashval_t
25708 per_cu_offset_and_type_hash (const void *item)
25709 {
25710 const struct dwarf2_per_cu_offset_and_type *ofs
25711 = (const struct dwarf2_per_cu_offset_and_type *) item;
25712
25713 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25714 }
25715
25716 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25717
25718 static int
25719 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25720 {
25721 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25722 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25723 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25724 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25725
25726 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25727 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25728 }
25729
25730 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25731 table if necessary. For convenience, return TYPE.
25732
25733 The DIEs reading must have careful ordering to:
25734 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25735 reading current DIE.
25736 * Not trying to dereference contents of still incompletely read in types
25737 while reading in other DIEs.
25738 * Enable referencing still incompletely read in types just by a pointer to
25739 the type without accessing its fields.
25740
25741 Therefore caller should follow these rules:
25742 * Try to fetch any prerequisite types we may need to build this DIE type
25743 before building the type and calling set_die_type.
25744 * After building type call set_die_type for current DIE as soon as
25745 possible before fetching more types to complete the current type.
25746 * Make the type as complete as possible before fetching more types. */
25747
25748 static struct type *
25749 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25750 {
25751 struct dwarf2_per_objfile *dwarf2_per_objfile
25752 = cu->per_cu->dwarf2_per_objfile;
25753 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25754 struct objfile *objfile = dwarf2_per_objfile->objfile;
25755 struct attribute *attr;
25756 struct dynamic_prop prop;
25757
25758 /* For Ada types, make sure that the gnat-specific data is always
25759 initialized (if not already set). There are a few types where
25760 we should not be doing so, because the type-specific area is
25761 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25762 where the type-specific area is used to store the floatformat).
25763 But this is not a problem, because the gnat-specific information
25764 is actually not needed for these types. */
25765 if (need_gnat_info (cu)
25766 && TYPE_CODE (type) != TYPE_CODE_FUNC
25767 && TYPE_CODE (type) != TYPE_CODE_FLT
25768 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25769 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25770 && TYPE_CODE (type) != TYPE_CODE_METHOD
25771 && !HAVE_GNAT_AUX_INFO (type))
25772 INIT_GNAT_SPECIFIC (type);
25773
25774 /* Read DW_AT_allocated and set in type. */
25775 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25776 if (attr_form_is_block (attr))
25777 {
25778 struct type *prop_type
25779 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25780 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25781 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25782 }
25783 else if (attr != NULL)
25784 {
25785 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25786 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25787 sect_offset_str (die->sect_off));
25788 }
25789
25790 /* Read DW_AT_associated and set in type. */
25791 attr = dwarf2_attr (die, DW_AT_associated, cu);
25792 if (attr_form_is_block (attr))
25793 {
25794 struct type *prop_type
25795 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25796 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25797 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25798 }
25799 else if (attr != NULL)
25800 {
25801 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25802 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25803 sect_offset_str (die->sect_off));
25804 }
25805
25806 /* Read DW_AT_data_location and set in type. */
25807 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25808 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25809 dwarf2_per_cu_addr_type (cu->per_cu)))
25810 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25811
25812 if (dwarf2_per_objfile->die_type_hash == NULL)
25813 {
25814 dwarf2_per_objfile->die_type_hash =
25815 htab_create_alloc_ex (127,
25816 per_cu_offset_and_type_hash,
25817 per_cu_offset_and_type_eq,
25818 NULL,
25819 &objfile->objfile_obstack,
25820 hashtab_obstack_allocate,
25821 dummy_obstack_deallocate);
25822 }
25823
25824 ofs.per_cu = cu->per_cu;
25825 ofs.sect_off = die->sect_off;
25826 ofs.type = type;
25827 slot = (struct dwarf2_per_cu_offset_and_type **)
25828 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25829 if (*slot)
25830 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25831 sect_offset_str (die->sect_off));
25832 *slot = XOBNEW (&objfile->objfile_obstack,
25833 struct dwarf2_per_cu_offset_and_type);
25834 **slot = ofs;
25835 return type;
25836 }
25837
25838 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25839 or return NULL if the die does not have a saved type. */
25840
25841 static struct type *
25842 get_die_type_at_offset (sect_offset sect_off,
25843 struct dwarf2_per_cu_data *per_cu)
25844 {
25845 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25846 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25847
25848 if (dwarf2_per_objfile->die_type_hash == NULL)
25849 return NULL;
25850
25851 ofs.per_cu = per_cu;
25852 ofs.sect_off = sect_off;
25853 slot = ((struct dwarf2_per_cu_offset_and_type *)
25854 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25855 if (slot)
25856 return slot->type;
25857 else
25858 return NULL;
25859 }
25860
25861 /* Look up the type for DIE in CU in die_type_hash,
25862 or return NULL if DIE does not have a saved type. */
25863
25864 static struct type *
25865 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25866 {
25867 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25868 }
25869
25870 /* Add a dependence relationship from CU to REF_PER_CU. */
25871
25872 static void
25873 dwarf2_add_dependence (struct dwarf2_cu *cu,
25874 struct dwarf2_per_cu_data *ref_per_cu)
25875 {
25876 void **slot;
25877
25878 if (cu->dependencies == NULL)
25879 cu->dependencies
25880 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25881 NULL, &cu->comp_unit_obstack,
25882 hashtab_obstack_allocate,
25883 dummy_obstack_deallocate);
25884
25885 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25886 if (*slot == NULL)
25887 *slot = ref_per_cu;
25888 }
25889
25890 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25891 Set the mark field in every compilation unit in the
25892 cache that we must keep because we are keeping CU. */
25893
25894 static int
25895 dwarf2_mark_helper (void **slot, void *data)
25896 {
25897 struct dwarf2_per_cu_data *per_cu;
25898
25899 per_cu = (struct dwarf2_per_cu_data *) *slot;
25900
25901 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25902 reading of the chain. As such dependencies remain valid it is not much
25903 useful to track and undo them during QUIT cleanups. */
25904 if (per_cu->cu == NULL)
25905 return 1;
25906
25907 if (per_cu->cu->mark)
25908 return 1;
25909 per_cu->cu->mark = true;
25910
25911 if (per_cu->cu->dependencies != NULL)
25912 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25913
25914 return 1;
25915 }
25916
25917 /* Set the mark field in CU and in every other compilation unit in the
25918 cache that we must keep because we are keeping CU. */
25919
25920 static void
25921 dwarf2_mark (struct dwarf2_cu *cu)
25922 {
25923 if (cu->mark)
25924 return;
25925 cu->mark = true;
25926 if (cu->dependencies != NULL)
25927 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25928 }
25929
25930 static void
25931 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25932 {
25933 while (per_cu)
25934 {
25935 per_cu->cu->mark = false;
25936 per_cu = per_cu->cu->read_in_chain;
25937 }
25938 }
25939
25940 /* Trivial hash function for partial_die_info: the hash value of a DIE
25941 is its offset in .debug_info for this objfile. */
25942
25943 static hashval_t
25944 partial_die_hash (const void *item)
25945 {
25946 const struct partial_die_info *part_die
25947 = (const struct partial_die_info *) item;
25948
25949 return to_underlying (part_die->sect_off);
25950 }
25951
25952 /* Trivial comparison function for partial_die_info structures: two DIEs
25953 are equal if they have the same offset. */
25954
25955 static int
25956 partial_die_eq (const void *item_lhs, const void *item_rhs)
25957 {
25958 const struct partial_die_info *part_die_lhs
25959 = (const struct partial_die_info *) item_lhs;
25960 const struct partial_die_info *part_die_rhs
25961 = (const struct partial_die_info *) item_rhs;
25962
25963 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25964 }
25965
25966 struct cmd_list_element *set_dwarf_cmdlist;
25967 struct cmd_list_element *show_dwarf_cmdlist;
25968
25969 static void
25970 set_dwarf_cmd (const char *args, int from_tty)
25971 {
25972 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25973 gdb_stdout);
25974 }
25975
25976 static void
25977 show_dwarf_cmd (const char *args, int from_tty)
25978 {
25979 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25980 }
25981
25982 bool dwarf_always_disassemble;
25983
25984 static void
25985 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25986 struct cmd_list_element *c, const char *value)
25987 {
25988 fprintf_filtered (file,
25989 _("Whether to always disassemble "
25990 "DWARF expressions is %s.\n"),
25991 value);
25992 }
25993
25994 static void
25995 show_check_physname (struct ui_file *file, int from_tty,
25996 struct cmd_list_element *c, const char *value)
25997 {
25998 fprintf_filtered (file,
25999 _("Whether to check \"physname\" is %s.\n"),
26000 value);
26001 }
26002
26003 void _initialize_dwarf2_read ();
26004 void
26005 _initialize_dwarf2_read ()
26006 {
26007 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26008 Set DWARF specific variables.\n\
26009 Configure DWARF variables such as the cache size."),
26010 &set_dwarf_cmdlist, "maintenance set dwarf ",
26011 0/*allow-unknown*/, &maintenance_set_cmdlist);
26012
26013 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
26014 Show DWARF specific variables.\n\
26015 Show DWARF variables such as the cache size."),
26016 &show_dwarf_cmdlist, "maintenance show dwarf ",
26017 0/*allow-unknown*/, &maintenance_show_cmdlist);
26018
26019 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26020 &dwarf_max_cache_age, _("\
26021 Set the upper bound on the age of cached DWARF compilation units."), _("\
26022 Show the upper bound on the age of cached DWARF compilation units."), _("\
26023 A higher limit means that cached compilation units will be stored\n\
26024 in memory longer, and more total memory will be used. Zero disables\n\
26025 caching, which can slow down startup."),
26026 NULL,
26027 show_dwarf_max_cache_age,
26028 &set_dwarf_cmdlist,
26029 &show_dwarf_cmdlist);
26030
26031 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
26032 &dwarf_always_disassemble, _("\
26033 Set whether `info address' always disassembles DWARF expressions."), _("\
26034 Show whether `info address' always disassembles DWARF expressions."), _("\
26035 When enabled, DWARF expressions are always printed in an assembly-like\n\
26036 syntax. When disabled, expressions will be printed in a more\n\
26037 conversational style, when possible."),
26038 NULL,
26039 show_dwarf_always_disassemble,
26040 &set_dwarf_cmdlist,
26041 &show_dwarf_cmdlist);
26042
26043 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26044 Set debugging of the DWARF reader."), _("\
26045 Show debugging of the DWARF reader."), _("\
26046 When enabled (non-zero), debugging messages are printed during DWARF\n\
26047 reading and symtab expansion. A value of 1 (one) provides basic\n\
26048 information. A value greater than 1 provides more verbose information."),
26049 NULL,
26050 NULL,
26051 &setdebuglist, &showdebuglist);
26052
26053 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26054 Set debugging of the DWARF DIE reader."), _("\
26055 Show debugging of the DWARF DIE reader."), _("\
26056 When enabled (non-zero), DIEs are dumped after they are read in.\n\
26057 The value is the maximum depth to print."),
26058 NULL,
26059 NULL,
26060 &setdebuglist, &showdebuglist);
26061
26062 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
26063 Set debugging of the dwarf line reader."), _("\
26064 Show debugging of the dwarf line reader."), _("\
26065 When enabled (non-zero), line number entries are dumped as they are read in.\n\
26066 A value of 1 (one) provides basic information.\n\
26067 A value greater than 1 provides more verbose information."),
26068 NULL,
26069 NULL,
26070 &setdebuglist, &showdebuglist);
26071
26072 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
26073 Set cross-checking of \"physname\" code against demangler."), _("\
26074 Show cross-checking of \"physname\" code against demangler."), _("\
26075 When enabled, GDB's internal \"physname\" code is checked against\n\
26076 the demangler."),
26077 NULL, show_check_physname,
26078 &setdebuglist, &showdebuglist);
26079
26080 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26081 no_class, &use_deprecated_index_sections, _("\
26082 Set whether to use deprecated gdb_index sections."), _("\
26083 Show whether to use deprecated gdb_index sections."), _("\
26084 When enabled, deprecated .gdb_index sections are used anyway.\n\
26085 Normally they are ignored either because of a missing feature or\n\
26086 performance issue.\n\
26087 Warning: This option must be enabled before gdb reads the file."),
26088 NULL,
26089 NULL,
26090 &setlist, &showlist);
26091
26092 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26093 &dwarf2_locexpr_funcs);
26094 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26095 &dwarf2_loclist_funcs);
26096
26097 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26098 &dwarf2_block_frame_base_locexpr_funcs);
26099 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26100 &dwarf2_block_frame_base_loclist_funcs);
26101
26102 #if GDB_SELF_TEST
26103 selftests::register_test ("dw2_expand_symtabs_matching",
26104 selftests::dw2_expand_symtabs_matching::run_test);
26105 #endif
26106 }
This page took 0.708982 seconds and 4 git commands to generate.