[PATCH v2 2/4] DWARF 5 support: Handle DW_FORM_strx
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2019 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2read.h"
33 #include "dwarf-index-cache.h"
34 #include "dwarf-index-common.h"
35 #include "bfd.h"
36 #include "elf-bfd.h"
37 #include "symtab.h"
38 #include "gdbtypes.h"
39 #include "objfiles.h"
40 #include "dwarf2.h"
41 #include "buildsym.h"
42 #include "demangle.h"
43 #include "gdb-demangle.h"
44 #include "expression.h"
45 #include "filenames.h" /* for DOSish file names */
46 #include "macrotab.h"
47 #include "language.h"
48 #include "complaints.h"
49 #include "dwarf2expr.h"
50 #include "dwarf2loc.h"
51 #include "cp-support.h"
52 #include "hashtab.h"
53 #include "command.h"
54 #include "gdbcmd.h"
55 #include "block.h"
56 #include "addrmap.h"
57 #include "typeprint.h"
58 #include "psympriv.h"
59 #include <sys/stat.h>
60 #include "completer.h"
61 #include "gdbsupport/vec.h"
62 #include "c-lang.h"
63 #include "go-lang.h"
64 #include "valprint.h"
65 #include "gdbcore.h" /* for gnutarget */
66 #include "gdb/gdb-index.h"
67 #include <ctype.h>
68 #include "gdb_bfd.h"
69 #include "f-lang.h"
70 #include "source.h"
71 #include "gdbsupport/filestuff.h"
72 #include "build-id.h"
73 #include "namespace.h"
74 #include "gdbsupport/gdb_unlinker.h"
75 #include "gdbsupport/function-view.h"
76 #include "gdbsupport/gdb_optional.h"
77 #include "gdbsupport/underlying.h"
78 #include "gdbsupport/byte-vector.h"
79 #include "gdbsupport/hash_enum.h"
80 #include "filename-seen-cache.h"
81 #include "producer.h"
82 #include <fcntl.h>
83 #include <sys/types.h>
84 #include <algorithm>
85 #include <unordered_set>
86 #include <unordered_map>
87 #include "gdbsupport/selftest.h"
88 #include <cmath>
89 #include <set>
90 #include <forward_list>
91 #include "rust-lang.h"
92 #include "gdbsupport/pathstuff.h"
93
94 /* When == 1, print basic high level tracing messages.
95 When > 1, be more verbose.
96 This is in contrast to the low level DIE reading of dwarf_die_debug. */
97 static unsigned int dwarf_read_debug = 0;
98
99 /* When non-zero, dump DIEs after they are read in. */
100 static unsigned int dwarf_die_debug = 0;
101
102 /* When non-zero, dump line number entries as they are read in. */
103 static unsigned int dwarf_line_debug = 0;
104
105 /* When true, cross-check physname against demangler. */
106 static bool check_physname = false;
107
108 /* When true, do not reject deprecated .gdb_index sections. */
109 static bool use_deprecated_index_sections = false;
110
111 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
112
113 /* The "aclass" indices for various kinds of computed DWARF symbols. */
114
115 static int dwarf2_locexpr_index;
116 static int dwarf2_loclist_index;
117 static int dwarf2_locexpr_block_index;
118 static int dwarf2_loclist_block_index;
119
120 /* An index into a (C++) symbol name component in a symbol name as
121 recorded in the mapped_index's symbol table. For each C++ symbol
122 in the symbol table, we record one entry for the start of each
123 component in the symbol in a table of name components, and then
124 sort the table, in order to be able to binary search symbol names,
125 ignoring leading namespaces, both completion and regular look up.
126 For example, for symbol "A::B::C", we'll have an entry that points
127 to "A::B::C", another that points to "B::C", and another for "C".
128 Note that function symbols in GDB index have no parameter
129 information, just the function/method names. You can convert a
130 name_component to a "const char *" using the
131 'mapped_index::symbol_name_at(offset_type)' method. */
132
133 struct name_component
134 {
135 /* Offset in the symbol name where the component starts. Stored as
136 a (32-bit) offset instead of a pointer to save memory and improve
137 locality on 64-bit architectures. */
138 offset_type name_offset;
139
140 /* The symbol's index in the symbol and constant pool tables of a
141 mapped_index. */
142 offset_type idx;
143 };
144
145 /* Base class containing bits shared by both .gdb_index and
146 .debug_name indexes. */
147
148 struct mapped_index_base
149 {
150 mapped_index_base () = default;
151 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
152
153 /* The name_component table (a sorted vector). See name_component's
154 description above. */
155 std::vector<name_component> name_components;
156
157 /* How NAME_COMPONENTS is sorted. */
158 enum case_sensitivity name_components_casing;
159
160 /* Return the number of names in the symbol table. */
161 virtual size_t symbol_name_count () const = 0;
162
163 /* Get the name of the symbol at IDX in the symbol table. */
164 virtual const char *symbol_name_at (offset_type idx) const = 0;
165
166 /* Return whether the name at IDX in the symbol table should be
167 ignored. */
168 virtual bool symbol_name_slot_invalid (offset_type idx) const
169 {
170 return false;
171 }
172
173 /* Build the symbol name component sorted vector, if we haven't
174 yet. */
175 void build_name_components ();
176
177 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
178 possible matches for LN_NO_PARAMS in the name component
179 vector. */
180 std::pair<std::vector<name_component>::const_iterator,
181 std::vector<name_component>::const_iterator>
182 find_name_components_bounds (const lookup_name_info &ln_no_params,
183 enum language lang) const;
184
185 /* Prevent deleting/destroying via a base class pointer. */
186 protected:
187 ~mapped_index_base() = default;
188 };
189
190 /* A description of the mapped index. The file format is described in
191 a comment by the code that writes the index. */
192 struct mapped_index final : public mapped_index_base
193 {
194 /* A slot/bucket in the symbol table hash. */
195 struct symbol_table_slot
196 {
197 const offset_type name;
198 const offset_type vec;
199 };
200
201 /* Index data format version. */
202 int version = 0;
203
204 /* The address table data. */
205 gdb::array_view<const gdb_byte> address_table;
206
207 /* The symbol table, implemented as a hash table. */
208 gdb::array_view<symbol_table_slot> symbol_table;
209
210 /* A pointer to the constant pool. */
211 const char *constant_pool = nullptr;
212
213 bool symbol_name_slot_invalid (offset_type idx) const override
214 {
215 const auto &bucket = this->symbol_table[idx];
216 return bucket.name == 0 && bucket.vec == 0;
217 }
218
219 /* Convenience method to get at the name of the symbol at IDX in the
220 symbol table. */
221 const char *symbol_name_at (offset_type idx) const override
222 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
223
224 size_t symbol_name_count () const override
225 { return this->symbol_table.size (); }
226 };
227
228 /* A description of the mapped .debug_names.
229 Uninitialized map has CU_COUNT 0. */
230 struct mapped_debug_names final : public mapped_index_base
231 {
232 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
233 : dwarf2_per_objfile (dwarf2_per_objfile_)
234 {}
235
236 struct dwarf2_per_objfile *dwarf2_per_objfile;
237 bfd_endian dwarf5_byte_order;
238 bool dwarf5_is_dwarf64;
239 bool augmentation_is_gdb;
240 uint8_t offset_size;
241 uint32_t cu_count = 0;
242 uint32_t tu_count, bucket_count, name_count;
243 const gdb_byte *cu_table_reordered, *tu_table_reordered;
244 const uint32_t *bucket_table_reordered, *hash_table_reordered;
245 const gdb_byte *name_table_string_offs_reordered;
246 const gdb_byte *name_table_entry_offs_reordered;
247 const gdb_byte *entry_pool;
248
249 struct index_val
250 {
251 ULONGEST dwarf_tag;
252 struct attr
253 {
254 /* Attribute name DW_IDX_*. */
255 ULONGEST dw_idx;
256
257 /* Attribute form DW_FORM_*. */
258 ULONGEST form;
259
260 /* Value if FORM is DW_FORM_implicit_const. */
261 LONGEST implicit_const;
262 };
263 std::vector<attr> attr_vec;
264 };
265
266 std::unordered_map<ULONGEST, index_val> abbrev_map;
267
268 const char *namei_to_name (uint32_t namei) const;
269
270 /* Implementation of the mapped_index_base virtual interface, for
271 the name_components cache. */
272
273 const char *symbol_name_at (offset_type idx) const override
274 { return namei_to_name (idx); }
275
276 size_t symbol_name_count () const override
277 { return this->name_count; }
278 };
279
280 /* See dwarf2read.h. */
281
282 dwarf2_per_objfile *
283 get_dwarf2_per_objfile (struct objfile *objfile)
284 {
285 return dwarf2_objfile_data_key.get (objfile);
286 }
287
288 /* Default names of the debugging sections. */
289
290 /* Note that if the debugging section has been compressed, it might
291 have a name like .zdebug_info. */
292
293 static const struct dwarf2_debug_sections dwarf2_elf_names =
294 {
295 { ".debug_info", ".zdebug_info" },
296 { ".debug_abbrev", ".zdebug_abbrev" },
297 { ".debug_line", ".zdebug_line" },
298 { ".debug_loc", ".zdebug_loc" },
299 { ".debug_loclists", ".zdebug_loclists" },
300 { ".debug_macinfo", ".zdebug_macinfo" },
301 { ".debug_macro", ".zdebug_macro" },
302 { ".debug_str", ".zdebug_str" },
303 { ".debug_line_str", ".zdebug_line_str" },
304 { ".debug_ranges", ".zdebug_ranges" },
305 { ".debug_rnglists", ".zdebug_rnglists" },
306 { ".debug_types", ".zdebug_types" },
307 { ".debug_addr", ".zdebug_addr" },
308 { ".debug_frame", ".zdebug_frame" },
309 { ".eh_frame", NULL },
310 { ".gdb_index", ".zgdb_index" },
311 { ".debug_names", ".zdebug_names" },
312 { ".debug_aranges", ".zdebug_aranges" },
313 23
314 };
315
316 /* List of DWO/DWP sections. */
317
318 static const struct dwop_section_names
319 {
320 struct dwarf2_section_names abbrev_dwo;
321 struct dwarf2_section_names info_dwo;
322 struct dwarf2_section_names line_dwo;
323 struct dwarf2_section_names loc_dwo;
324 struct dwarf2_section_names loclists_dwo;
325 struct dwarf2_section_names macinfo_dwo;
326 struct dwarf2_section_names macro_dwo;
327 struct dwarf2_section_names str_dwo;
328 struct dwarf2_section_names str_offsets_dwo;
329 struct dwarf2_section_names types_dwo;
330 struct dwarf2_section_names cu_index;
331 struct dwarf2_section_names tu_index;
332 }
333 dwop_section_names =
334 {
335 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
336 { ".debug_info.dwo", ".zdebug_info.dwo" },
337 { ".debug_line.dwo", ".zdebug_line.dwo" },
338 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
339 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
340 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
341 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
342 { ".debug_str.dwo", ".zdebug_str.dwo" },
343 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
344 { ".debug_types.dwo", ".zdebug_types.dwo" },
345 { ".debug_cu_index", ".zdebug_cu_index" },
346 { ".debug_tu_index", ".zdebug_tu_index" },
347 };
348
349 /* local data types */
350
351 /* The data in a compilation unit header, after target2host
352 translation, looks like this. */
353 struct comp_unit_head
354 {
355 unsigned int length;
356 short version;
357 unsigned char addr_size;
358 unsigned char signed_addr_p;
359 sect_offset abbrev_sect_off;
360
361 /* Size of file offsets; either 4 or 8. */
362 unsigned int offset_size;
363
364 /* Size of the length field; either 4 or 12. */
365 unsigned int initial_length_size;
366
367 enum dwarf_unit_type unit_type;
368
369 /* Offset to the first byte of this compilation unit header in the
370 .debug_info section, for resolving relative reference dies. */
371 sect_offset sect_off;
372
373 /* Offset to first die in this cu from the start of the cu.
374 This will be the first byte following the compilation unit header. */
375 cu_offset first_die_cu_offset;
376
377
378 /* 64-bit signature of this unit. For type units, it denotes the signature of
379 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
380 Also used in DWARF 5, to denote the dwo id when the unit type is
381 DW_UT_skeleton or DW_UT_split_compile. */
382 ULONGEST signature;
383
384 /* For types, offset in the type's DIE of the type defined by this TU. */
385 cu_offset type_cu_offset_in_tu;
386 };
387
388 /* Type used for delaying computation of method physnames.
389 See comments for compute_delayed_physnames. */
390 struct delayed_method_info
391 {
392 /* The type to which the method is attached, i.e., its parent class. */
393 struct type *type;
394
395 /* The index of the method in the type's function fieldlists. */
396 int fnfield_index;
397
398 /* The index of the method in the fieldlist. */
399 int index;
400
401 /* The name of the DIE. */
402 const char *name;
403
404 /* The DIE associated with this method. */
405 struct die_info *die;
406 };
407
408 /* Internal state when decoding a particular compilation unit. */
409 struct dwarf2_cu
410 {
411 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
412 ~dwarf2_cu ();
413
414 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
415
416 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
417 Create the set of symtabs used by this TU, or if this TU is sharing
418 symtabs with another TU and the symtabs have already been created
419 then restore those symtabs in the line header.
420 We don't need the pc/line-number mapping for type units. */
421 void setup_type_unit_groups (struct die_info *die);
422
423 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
424 buildsym_compunit constructor. */
425 struct compunit_symtab *start_symtab (const char *name,
426 const char *comp_dir,
427 CORE_ADDR low_pc);
428
429 /* Reset the builder. */
430 void reset_builder () { m_builder.reset (); }
431
432 /* The header of the compilation unit. */
433 struct comp_unit_head header {};
434
435 /* Base address of this compilation unit. */
436 CORE_ADDR base_address = 0;
437
438 /* Non-zero if base_address has been set. */
439 int base_known = 0;
440
441 /* The language we are debugging. */
442 enum language language = language_unknown;
443 const struct language_defn *language_defn = nullptr;
444
445 const char *producer = nullptr;
446
447 private:
448 /* The symtab builder for this CU. This is only non-NULL when full
449 symbols are being read. */
450 std::unique_ptr<buildsym_compunit> m_builder;
451
452 public:
453 /* The generic symbol table building routines have separate lists for
454 file scope symbols and all all other scopes (local scopes). So
455 we need to select the right one to pass to add_symbol_to_list().
456 We do it by keeping a pointer to the correct list in list_in_scope.
457
458 FIXME: The original dwarf code just treated the file scope as the
459 first local scope, and all other local scopes as nested local
460 scopes, and worked fine. Check to see if we really need to
461 distinguish these in buildsym.c. */
462 struct pending **list_in_scope = nullptr;
463
464 /* Hash table holding all the loaded partial DIEs
465 with partial_die->offset.SECT_OFF as hash. */
466 htab_t partial_dies = nullptr;
467
468 /* Storage for things with the same lifetime as this read-in compilation
469 unit, including partial DIEs. */
470 auto_obstack comp_unit_obstack;
471
472 /* When multiple dwarf2_cu structures are living in memory, this field
473 chains them all together, so that they can be released efficiently.
474 We will probably also want a generation counter so that most-recently-used
475 compilation units are cached... */
476 struct dwarf2_per_cu_data *read_in_chain = nullptr;
477
478 /* Backlink to our per_cu entry. */
479 struct dwarf2_per_cu_data *per_cu;
480
481 /* How many compilation units ago was this CU last referenced? */
482 int last_used = 0;
483
484 /* A hash table of DIE cu_offset for following references with
485 die_info->offset.sect_off as hash. */
486 htab_t die_hash = nullptr;
487
488 /* Full DIEs if read in. */
489 struct die_info *dies = nullptr;
490
491 /* A set of pointers to dwarf2_per_cu_data objects for compilation
492 units referenced by this one. Only set during full symbol processing;
493 partial symbol tables do not have dependencies. */
494 htab_t dependencies = nullptr;
495
496 /* Header data from the line table, during full symbol processing. */
497 struct line_header *line_header = nullptr;
498 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
499 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
500 this is the DW_TAG_compile_unit die for this CU. We'll hold on
501 to the line header as long as this DIE is being processed. See
502 process_die_scope. */
503 die_info *line_header_die_owner = nullptr;
504
505 /* A list of methods which need to have physnames computed
506 after all type information has been read. */
507 std::vector<delayed_method_info> method_list;
508
509 /* To be copied to symtab->call_site_htab. */
510 htab_t call_site_htab = nullptr;
511
512 /* Non-NULL if this CU came from a DWO file.
513 There is an invariant here that is important to remember:
514 Except for attributes copied from the top level DIE in the "main"
515 (or "stub") file in preparation for reading the DWO file
516 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
517 Either there isn't a DWO file (in which case this is NULL and the point
518 is moot), or there is and either we're not going to read it (in which
519 case this is NULL) or there is and we are reading it (in which case this
520 is non-NULL). */
521 struct dwo_unit *dwo_unit = nullptr;
522
523 /* The DW_AT_addr_base attribute if present, zero otherwise
524 (zero is a valid value though).
525 Note this value comes from the Fission stub CU/TU's DIE. */
526 ULONGEST addr_base = 0;
527
528 /* The DW_AT_ranges_base attribute if present, zero otherwise
529 (zero is a valid value though).
530 Note this value comes from the Fission stub CU/TU's DIE.
531 Also note that the value is zero in the non-DWO case so this value can
532 be used without needing to know whether DWO files are in use or not.
533 N.B. This does not apply to DW_AT_ranges appearing in
534 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
535 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
536 DW_AT_ranges_base *would* have to be applied, and we'd have to care
537 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
538 ULONGEST ranges_base = 0;
539
540 /* When reading debug info generated by older versions of rustc, we
541 have to rewrite some union types to be struct types with a
542 variant part. This rewriting must be done after the CU is fully
543 read in, because otherwise at the point of rewriting some struct
544 type might not have been fully processed. So, we keep a list of
545 all such types here and process them after expansion. */
546 std::vector<struct type *> rust_unions;
547
548 /* Mark used when releasing cached dies. */
549 bool mark : 1;
550
551 /* This CU references .debug_loc. See the symtab->locations_valid field.
552 This test is imperfect as there may exist optimized debug code not using
553 any location list and still facing inlining issues if handled as
554 unoptimized code. For a future better test see GCC PR other/32998. */
555 bool has_loclist : 1;
556
557 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
558 if all the producer_is_* fields are valid. This information is cached
559 because profiling CU expansion showed excessive time spent in
560 producer_is_gxx_lt_4_6. */
561 bool checked_producer : 1;
562 bool producer_is_gxx_lt_4_6 : 1;
563 bool producer_is_gcc_lt_4_3 : 1;
564 bool producer_is_icc : 1;
565 bool producer_is_icc_lt_14 : 1;
566 bool producer_is_codewarrior : 1;
567
568 /* When true, the file that we're processing is known to have
569 debugging info for C++ namespaces. GCC 3.3.x did not produce
570 this information, but later versions do. */
571
572 bool processing_has_namespace_info : 1;
573
574 struct partial_die_info *find_partial_die (sect_offset sect_off);
575
576 /* If this CU was inherited by another CU (via specification,
577 abstract_origin, etc), this is the ancestor CU. */
578 dwarf2_cu *ancestor;
579
580 /* Get the buildsym_compunit for this CU. */
581 buildsym_compunit *get_builder ()
582 {
583 /* If this CU has a builder associated with it, use that. */
584 if (m_builder != nullptr)
585 return m_builder.get ();
586
587 /* Otherwise, search ancestors for a valid builder. */
588 if (ancestor != nullptr)
589 return ancestor->get_builder ();
590
591 return nullptr;
592 }
593 };
594
595 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
596 This includes type_unit_group and quick_file_names. */
597
598 struct stmt_list_hash
599 {
600 /* The DWO unit this table is from or NULL if there is none. */
601 struct dwo_unit *dwo_unit;
602
603 /* Offset in .debug_line or .debug_line.dwo. */
604 sect_offset line_sect_off;
605 };
606
607 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
608 an object of this type. */
609
610 struct type_unit_group
611 {
612 /* dwarf2read.c's main "handle" on a TU symtab.
613 To simplify things we create an artificial CU that "includes" all the
614 type units using this stmt_list so that the rest of the code still has
615 a "per_cu" handle on the symtab.
616 This PER_CU is recognized by having no section. */
617 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
618 struct dwarf2_per_cu_data per_cu;
619
620 /* The TUs that share this DW_AT_stmt_list entry.
621 This is added to while parsing type units to build partial symtabs,
622 and is deleted afterwards and not used again. */
623 VEC (sig_type_ptr) *tus;
624
625 /* The compunit symtab.
626 Type units in a group needn't all be defined in the same source file,
627 so we create an essentially anonymous symtab as the compunit symtab. */
628 struct compunit_symtab *compunit_symtab;
629
630 /* The data used to construct the hash key. */
631 struct stmt_list_hash hash;
632
633 /* The number of symtabs from the line header.
634 The value here must match line_header.num_file_names. */
635 unsigned int num_symtabs;
636
637 /* The symbol tables for this TU (obtained from the files listed in
638 DW_AT_stmt_list).
639 WARNING: The order of entries here must match the order of entries
640 in the line header. After the first TU using this type_unit_group, the
641 line header for the subsequent TUs is recreated from this. This is done
642 because we need to use the same symtabs for each TU using the same
643 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
644 there's no guarantee the line header doesn't have duplicate entries. */
645 struct symtab **symtabs;
646 };
647
648 /* These sections are what may appear in a (real or virtual) DWO file. */
649
650 struct dwo_sections
651 {
652 struct dwarf2_section_info abbrev;
653 struct dwarf2_section_info line;
654 struct dwarf2_section_info loc;
655 struct dwarf2_section_info loclists;
656 struct dwarf2_section_info macinfo;
657 struct dwarf2_section_info macro;
658 struct dwarf2_section_info str;
659 struct dwarf2_section_info str_offsets;
660 /* In the case of a virtual DWO file, these two are unused. */
661 struct dwarf2_section_info info;
662 std::vector<dwarf2_section_info> types;
663 };
664
665 /* CUs/TUs in DWP/DWO files. */
666
667 struct dwo_unit
668 {
669 /* Backlink to the containing struct dwo_file. */
670 struct dwo_file *dwo_file;
671
672 /* The "id" that distinguishes this CU/TU.
673 .debug_info calls this "dwo_id", .debug_types calls this "signature".
674 Since signatures came first, we stick with it for consistency. */
675 ULONGEST signature;
676
677 /* The section this CU/TU lives in, in the DWO file. */
678 struct dwarf2_section_info *section;
679
680 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
681 sect_offset sect_off;
682 unsigned int length;
683
684 /* For types, offset in the type's DIE of the type defined by this TU. */
685 cu_offset type_offset_in_tu;
686 };
687
688 /* include/dwarf2.h defines the DWP section codes.
689 It defines a max value but it doesn't define a min value, which we
690 use for error checking, so provide one. */
691
692 enum dwp_v2_section_ids
693 {
694 DW_SECT_MIN = 1
695 };
696
697 /* Data for one DWO file.
698
699 This includes virtual DWO files (a virtual DWO file is a DWO file as it
700 appears in a DWP file). DWP files don't really have DWO files per se -
701 comdat folding of types "loses" the DWO file they came from, and from
702 a high level view DWP files appear to contain a mass of random types.
703 However, to maintain consistency with the non-DWP case we pretend DWP
704 files contain virtual DWO files, and we assign each TU with one virtual
705 DWO file (generally based on the line and abbrev section offsets -
706 a heuristic that seems to work in practice). */
707
708 struct dwo_file
709 {
710 dwo_file () = default;
711 DISABLE_COPY_AND_ASSIGN (dwo_file);
712
713 /* The DW_AT_GNU_dwo_name attribute.
714 For virtual DWO files the name is constructed from the section offsets
715 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
716 from related CU+TUs. */
717 const char *dwo_name = nullptr;
718
719 /* The DW_AT_comp_dir attribute. */
720 const char *comp_dir = nullptr;
721
722 /* The bfd, when the file is open. Otherwise this is NULL.
723 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
724 gdb_bfd_ref_ptr dbfd;
725
726 /* The sections that make up this DWO file.
727 Remember that for virtual DWO files in DWP V2, these are virtual
728 sections (for lack of a better name). */
729 struct dwo_sections sections {};
730
731 /* The CUs in the file.
732 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
733 an extension to handle LLVM's Link Time Optimization output (where
734 multiple source files may be compiled into a single object/dwo pair). */
735 htab_t cus {};
736
737 /* Table of TUs in the file.
738 Each element is a struct dwo_unit. */
739 htab_t tus {};
740 };
741
742 /* These sections are what may appear in a DWP file. */
743
744 struct dwp_sections
745 {
746 /* These are used by both DWP version 1 and 2. */
747 struct dwarf2_section_info str;
748 struct dwarf2_section_info cu_index;
749 struct dwarf2_section_info tu_index;
750
751 /* These are only used by DWP version 2 files.
752 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
753 sections are referenced by section number, and are not recorded here.
754 In DWP version 2 there is at most one copy of all these sections, each
755 section being (effectively) comprised of the concatenation of all of the
756 individual sections that exist in the version 1 format.
757 To keep the code simple we treat each of these concatenated pieces as a
758 section itself (a virtual section?). */
759 struct dwarf2_section_info abbrev;
760 struct dwarf2_section_info info;
761 struct dwarf2_section_info line;
762 struct dwarf2_section_info loc;
763 struct dwarf2_section_info macinfo;
764 struct dwarf2_section_info macro;
765 struct dwarf2_section_info str_offsets;
766 struct dwarf2_section_info types;
767 };
768
769 /* These sections are what may appear in a virtual DWO file in DWP version 1.
770 A virtual DWO file is a DWO file as it appears in a DWP file. */
771
772 struct virtual_v1_dwo_sections
773 {
774 struct dwarf2_section_info abbrev;
775 struct dwarf2_section_info line;
776 struct dwarf2_section_info loc;
777 struct dwarf2_section_info macinfo;
778 struct dwarf2_section_info macro;
779 struct dwarf2_section_info str_offsets;
780 /* Each DWP hash table entry records one CU or one TU.
781 That is recorded here, and copied to dwo_unit.section. */
782 struct dwarf2_section_info info_or_types;
783 };
784
785 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
786 In version 2, the sections of the DWO files are concatenated together
787 and stored in one section of that name. Thus each ELF section contains
788 several "virtual" sections. */
789
790 struct virtual_v2_dwo_sections
791 {
792 bfd_size_type abbrev_offset;
793 bfd_size_type abbrev_size;
794
795 bfd_size_type line_offset;
796 bfd_size_type line_size;
797
798 bfd_size_type loc_offset;
799 bfd_size_type loc_size;
800
801 bfd_size_type macinfo_offset;
802 bfd_size_type macinfo_size;
803
804 bfd_size_type macro_offset;
805 bfd_size_type macro_size;
806
807 bfd_size_type str_offsets_offset;
808 bfd_size_type str_offsets_size;
809
810 /* Each DWP hash table entry records one CU or one TU.
811 That is recorded here, and copied to dwo_unit.section. */
812 bfd_size_type info_or_types_offset;
813 bfd_size_type info_or_types_size;
814 };
815
816 /* Contents of DWP hash tables. */
817
818 struct dwp_hash_table
819 {
820 uint32_t version, nr_columns;
821 uint32_t nr_units, nr_slots;
822 const gdb_byte *hash_table, *unit_table;
823 union
824 {
825 struct
826 {
827 const gdb_byte *indices;
828 } v1;
829 struct
830 {
831 /* This is indexed by column number and gives the id of the section
832 in that column. */
833 #define MAX_NR_V2_DWO_SECTIONS \
834 (1 /* .debug_info or .debug_types */ \
835 + 1 /* .debug_abbrev */ \
836 + 1 /* .debug_line */ \
837 + 1 /* .debug_loc */ \
838 + 1 /* .debug_str_offsets */ \
839 + 1 /* .debug_macro or .debug_macinfo */)
840 int section_ids[MAX_NR_V2_DWO_SECTIONS];
841 const gdb_byte *offsets;
842 const gdb_byte *sizes;
843 } v2;
844 } section_pool;
845 };
846
847 /* Data for one DWP file. */
848
849 struct dwp_file
850 {
851 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
852 : name (name_),
853 dbfd (std::move (abfd))
854 {
855 }
856
857 /* Name of the file. */
858 const char *name;
859
860 /* File format version. */
861 int version = 0;
862
863 /* The bfd. */
864 gdb_bfd_ref_ptr dbfd;
865
866 /* Section info for this file. */
867 struct dwp_sections sections {};
868
869 /* Table of CUs in the file. */
870 const struct dwp_hash_table *cus = nullptr;
871
872 /* Table of TUs in the file. */
873 const struct dwp_hash_table *tus = nullptr;
874
875 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
876 htab_t loaded_cus {};
877 htab_t loaded_tus {};
878
879 /* Table to map ELF section numbers to their sections.
880 This is only needed for the DWP V1 file format. */
881 unsigned int num_sections = 0;
882 asection **elf_sections = nullptr;
883 };
884
885 /* Struct used to pass misc. parameters to read_die_and_children, et
886 al. which are used for both .debug_info and .debug_types dies.
887 All parameters here are unchanging for the life of the call. This
888 struct exists to abstract away the constant parameters of die reading. */
889
890 struct die_reader_specs
891 {
892 /* The bfd of die_section. */
893 bfd* abfd;
894
895 /* The CU of the DIE we are parsing. */
896 struct dwarf2_cu *cu;
897
898 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
899 struct dwo_file *dwo_file;
900
901 /* The section the die comes from.
902 This is either .debug_info or .debug_types, or the .dwo variants. */
903 struct dwarf2_section_info *die_section;
904
905 /* die_section->buffer. */
906 const gdb_byte *buffer;
907
908 /* The end of the buffer. */
909 const gdb_byte *buffer_end;
910
911 /* The value of the DW_AT_comp_dir attribute. */
912 const char *comp_dir;
913
914 /* The abbreviation table to use when reading the DIEs. */
915 struct abbrev_table *abbrev_table;
916 };
917
918 /* Type of function passed to init_cutu_and_read_dies, et.al. */
919 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
920 const gdb_byte *info_ptr,
921 struct die_info *comp_unit_die,
922 int has_children,
923 void *data);
924
925 /* A 1-based directory index. This is a strong typedef to prevent
926 accidentally using a directory index as a 0-based index into an
927 array/vector. */
928 enum class dir_index : unsigned int {};
929
930 /* Likewise, a 1-based file name index. */
931 enum class file_name_index : unsigned int {};
932
933 struct file_entry
934 {
935 file_entry () = default;
936
937 file_entry (const char *name_, dir_index d_index_,
938 unsigned int mod_time_, unsigned int length_)
939 : name (name_),
940 d_index (d_index_),
941 mod_time (mod_time_),
942 length (length_)
943 {}
944
945 /* Return the include directory at D_INDEX stored in LH. Returns
946 NULL if D_INDEX is out of bounds. */
947 const char *include_dir (const line_header *lh) const;
948
949 /* The file name. Note this is an observing pointer. The memory is
950 owned by debug_line_buffer. */
951 const char *name {};
952
953 /* The directory index (1-based). */
954 dir_index d_index {};
955
956 unsigned int mod_time {};
957
958 unsigned int length {};
959
960 /* True if referenced by the Line Number Program. */
961 bool included_p {};
962
963 /* The associated symbol table, if any. */
964 struct symtab *symtab {};
965 };
966
967 /* The line number information for a compilation unit (found in the
968 .debug_line section) begins with a "statement program header",
969 which contains the following information. */
970 struct line_header
971 {
972 line_header ()
973 : offset_in_dwz {}
974 {}
975
976 /* Add an entry to the include directory table. */
977 void add_include_dir (const char *include_dir);
978
979 /* Add an entry to the file name table. */
980 void add_file_name (const char *name, dir_index d_index,
981 unsigned int mod_time, unsigned int length);
982
983 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
984 is out of bounds. */
985 const char *include_dir_at (dir_index index) const
986 {
987 /* Convert directory index number (1-based) to vector index
988 (0-based). */
989 size_t vec_index = to_underlying (index) - 1;
990
991 if (vec_index >= include_dirs.size ())
992 return NULL;
993 return include_dirs[vec_index];
994 }
995
996 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
997 is out of bounds. */
998 file_entry *file_name_at (file_name_index index)
999 {
1000 /* Convert file name index number (1-based) to vector index
1001 (0-based). */
1002 size_t vec_index = to_underlying (index) - 1;
1003
1004 if (vec_index >= file_names.size ())
1005 return NULL;
1006 return &file_names[vec_index];
1007 }
1008
1009 /* Offset of line number information in .debug_line section. */
1010 sect_offset sect_off {};
1011
1012 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1013 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1014
1015 unsigned int total_length {};
1016 unsigned short version {};
1017 unsigned int header_length {};
1018 unsigned char minimum_instruction_length {};
1019 unsigned char maximum_ops_per_instruction {};
1020 unsigned char default_is_stmt {};
1021 int line_base {};
1022 unsigned char line_range {};
1023 unsigned char opcode_base {};
1024
1025 /* standard_opcode_lengths[i] is the number of operands for the
1026 standard opcode whose value is i. This means that
1027 standard_opcode_lengths[0] is unused, and the last meaningful
1028 element is standard_opcode_lengths[opcode_base - 1]. */
1029 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1030
1031 /* The include_directories table. Note these are observing
1032 pointers. The memory is owned by debug_line_buffer. */
1033 std::vector<const char *> include_dirs;
1034
1035 /* The file_names table. */
1036 std::vector<file_entry> file_names;
1037
1038 /* The start and end of the statement program following this
1039 header. These point into dwarf2_per_objfile->line_buffer. */
1040 const gdb_byte *statement_program_start {}, *statement_program_end {};
1041 };
1042
1043 typedef std::unique_ptr<line_header> line_header_up;
1044
1045 const char *
1046 file_entry::include_dir (const line_header *lh) const
1047 {
1048 return lh->include_dir_at (d_index);
1049 }
1050
1051 /* When we construct a partial symbol table entry we only
1052 need this much information. */
1053 struct partial_die_info : public allocate_on_obstack
1054 {
1055 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1056
1057 /* Disable assign but still keep copy ctor, which is needed
1058 load_partial_dies. */
1059 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1060
1061 /* Adjust the partial die before generating a symbol for it. This
1062 function may set the is_external flag or change the DIE's
1063 name. */
1064 void fixup (struct dwarf2_cu *cu);
1065
1066 /* Read a minimal amount of information into the minimal die
1067 structure. */
1068 const gdb_byte *read (const struct die_reader_specs *reader,
1069 const struct abbrev_info &abbrev,
1070 const gdb_byte *info_ptr);
1071
1072 /* Offset of this DIE. */
1073 const sect_offset sect_off;
1074
1075 /* DWARF-2 tag for this DIE. */
1076 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1077
1078 /* Assorted flags describing the data found in this DIE. */
1079 const unsigned int has_children : 1;
1080
1081 unsigned int is_external : 1;
1082 unsigned int is_declaration : 1;
1083 unsigned int has_type : 1;
1084 unsigned int has_specification : 1;
1085 unsigned int has_pc_info : 1;
1086 unsigned int may_be_inlined : 1;
1087
1088 /* This DIE has been marked DW_AT_main_subprogram. */
1089 unsigned int main_subprogram : 1;
1090
1091 /* Flag set if the SCOPE field of this structure has been
1092 computed. */
1093 unsigned int scope_set : 1;
1094
1095 /* Flag set if the DIE has a byte_size attribute. */
1096 unsigned int has_byte_size : 1;
1097
1098 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1099 unsigned int has_const_value : 1;
1100
1101 /* Flag set if any of the DIE's children are template arguments. */
1102 unsigned int has_template_arguments : 1;
1103
1104 /* Flag set if fixup has been called on this die. */
1105 unsigned int fixup_called : 1;
1106
1107 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1108 unsigned int is_dwz : 1;
1109
1110 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1111 unsigned int spec_is_dwz : 1;
1112
1113 /* The name of this DIE. Normally the value of DW_AT_name, but
1114 sometimes a default name for unnamed DIEs. */
1115 const char *name = nullptr;
1116
1117 /* The linkage name, if present. */
1118 const char *linkage_name = nullptr;
1119
1120 /* The scope to prepend to our children. This is generally
1121 allocated on the comp_unit_obstack, so will disappear
1122 when this compilation unit leaves the cache. */
1123 const char *scope = nullptr;
1124
1125 /* Some data associated with the partial DIE. The tag determines
1126 which field is live. */
1127 union
1128 {
1129 /* The location description associated with this DIE, if any. */
1130 struct dwarf_block *locdesc;
1131 /* The offset of an import, for DW_TAG_imported_unit. */
1132 sect_offset sect_off;
1133 } d {};
1134
1135 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1136 CORE_ADDR lowpc = 0;
1137 CORE_ADDR highpc = 0;
1138
1139 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1140 DW_AT_sibling, if any. */
1141 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1142 could return DW_AT_sibling values to its caller load_partial_dies. */
1143 const gdb_byte *sibling = nullptr;
1144
1145 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1146 DW_AT_specification (or DW_AT_abstract_origin or
1147 DW_AT_extension). */
1148 sect_offset spec_offset {};
1149
1150 /* Pointers to this DIE's parent, first child, and next sibling,
1151 if any. */
1152 struct partial_die_info *die_parent = nullptr;
1153 struct partial_die_info *die_child = nullptr;
1154 struct partial_die_info *die_sibling = nullptr;
1155
1156 friend struct partial_die_info *
1157 dwarf2_cu::find_partial_die (sect_offset sect_off);
1158
1159 private:
1160 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1161 partial_die_info (sect_offset sect_off)
1162 : partial_die_info (sect_off, DW_TAG_padding, 0)
1163 {
1164 }
1165
1166 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1167 int has_children_)
1168 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1169 {
1170 is_external = 0;
1171 is_declaration = 0;
1172 has_type = 0;
1173 has_specification = 0;
1174 has_pc_info = 0;
1175 may_be_inlined = 0;
1176 main_subprogram = 0;
1177 scope_set = 0;
1178 has_byte_size = 0;
1179 has_const_value = 0;
1180 has_template_arguments = 0;
1181 fixup_called = 0;
1182 is_dwz = 0;
1183 spec_is_dwz = 0;
1184 }
1185 };
1186
1187 /* This data structure holds the information of an abbrev. */
1188 struct abbrev_info
1189 {
1190 unsigned int number; /* number identifying abbrev */
1191 enum dwarf_tag tag; /* dwarf tag */
1192 unsigned short has_children; /* boolean */
1193 unsigned short num_attrs; /* number of attributes */
1194 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1195 struct abbrev_info *next; /* next in chain */
1196 };
1197
1198 struct attr_abbrev
1199 {
1200 ENUM_BITFIELD(dwarf_attribute) name : 16;
1201 ENUM_BITFIELD(dwarf_form) form : 16;
1202
1203 /* It is valid only if FORM is DW_FORM_implicit_const. */
1204 LONGEST implicit_const;
1205 };
1206
1207 /* Size of abbrev_table.abbrev_hash_table. */
1208 #define ABBREV_HASH_SIZE 121
1209
1210 /* Top level data structure to contain an abbreviation table. */
1211
1212 struct abbrev_table
1213 {
1214 explicit abbrev_table (sect_offset off)
1215 : sect_off (off)
1216 {
1217 m_abbrevs =
1218 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1219 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1220 }
1221
1222 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1223
1224 /* Allocate space for a struct abbrev_info object in
1225 ABBREV_TABLE. */
1226 struct abbrev_info *alloc_abbrev ();
1227
1228 /* Add an abbreviation to the table. */
1229 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1230
1231 /* Look up an abbrev in the table.
1232 Returns NULL if the abbrev is not found. */
1233
1234 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1235
1236
1237 /* Where the abbrev table came from.
1238 This is used as a sanity check when the table is used. */
1239 const sect_offset sect_off;
1240
1241 /* Storage for the abbrev table. */
1242 auto_obstack abbrev_obstack;
1243
1244 private:
1245
1246 /* Hash table of abbrevs.
1247 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1248 It could be statically allocated, but the previous code didn't so we
1249 don't either. */
1250 struct abbrev_info **m_abbrevs;
1251 };
1252
1253 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1254
1255 /* Attributes have a name and a value. */
1256 struct attribute
1257 {
1258 ENUM_BITFIELD(dwarf_attribute) name : 16;
1259 ENUM_BITFIELD(dwarf_form) form : 15;
1260
1261 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1262 field should be in u.str (existing only for DW_STRING) but it is kept
1263 here for better struct attribute alignment. */
1264 unsigned int string_is_canonical : 1;
1265
1266 union
1267 {
1268 const char *str;
1269 struct dwarf_block *blk;
1270 ULONGEST unsnd;
1271 LONGEST snd;
1272 CORE_ADDR addr;
1273 ULONGEST signature;
1274 }
1275 u;
1276 };
1277
1278 /* This data structure holds a complete die structure. */
1279 struct die_info
1280 {
1281 /* DWARF-2 tag for this DIE. */
1282 ENUM_BITFIELD(dwarf_tag) tag : 16;
1283
1284 /* Number of attributes */
1285 unsigned char num_attrs;
1286
1287 /* True if we're presently building the full type name for the
1288 type derived from this DIE. */
1289 unsigned char building_fullname : 1;
1290
1291 /* True if this die is in process. PR 16581. */
1292 unsigned char in_process : 1;
1293
1294 /* Abbrev number */
1295 unsigned int abbrev;
1296
1297 /* Offset in .debug_info or .debug_types section. */
1298 sect_offset sect_off;
1299
1300 /* The dies in a compilation unit form an n-ary tree. PARENT
1301 points to this die's parent; CHILD points to the first child of
1302 this node; and all the children of a given node are chained
1303 together via their SIBLING fields. */
1304 struct die_info *child; /* Its first child, if any. */
1305 struct die_info *sibling; /* Its next sibling, if any. */
1306 struct die_info *parent; /* Its parent, if any. */
1307
1308 /* An array of attributes, with NUM_ATTRS elements. There may be
1309 zero, but it's not common and zero-sized arrays are not
1310 sufficiently portable C. */
1311 struct attribute attrs[1];
1312 };
1313
1314 /* Get at parts of an attribute structure. */
1315
1316 #define DW_STRING(attr) ((attr)->u.str)
1317 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1318 #define DW_UNSND(attr) ((attr)->u.unsnd)
1319 #define DW_BLOCK(attr) ((attr)->u.blk)
1320 #define DW_SND(attr) ((attr)->u.snd)
1321 #define DW_ADDR(attr) ((attr)->u.addr)
1322 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1323
1324 /* Blocks are a bunch of untyped bytes. */
1325 struct dwarf_block
1326 {
1327 size_t size;
1328
1329 /* Valid only if SIZE is not zero. */
1330 const gdb_byte *data;
1331 };
1332
1333 #ifndef ATTR_ALLOC_CHUNK
1334 #define ATTR_ALLOC_CHUNK 4
1335 #endif
1336
1337 /* Allocate fields for structs, unions and enums in this size. */
1338 #ifndef DW_FIELD_ALLOC_CHUNK
1339 #define DW_FIELD_ALLOC_CHUNK 4
1340 #endif
1341
1342 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1343 but this would require a corresponding change in unpack_field_as_long
1344 and friends. */
1345 static int bits_per_byte = 8;
1346
1347 /* When reading a variant or variant part, we track a bit more
1348 information about the field, and store it in an object of this
1349 type. */
1350
1351 struct variant_field
1352 {
1353 /* If we see a DW_TAG_variant, then this will be the discriminant
1354 value. */
1355 ULONGEST discriminant_value;
1356 /* If we see a DW_TAG_variant, then this will be set if this is the
1357 default branch. */
1358 bool default_branch;
1359 /* While reading a DW_TAG_variant_part, this will be set if this
1360 field is the discriminant. */
1361 bool is_discriminant;
1362 };
1363
1364 struct nextfield
1365 {
1366 int accessibility = 0;
1367 int virtuality = 0;
1368 /* Extra information to describe a variant or variant part. */
1369 struct variant_field variant {};
1370 struct field field {};
1371 };
1372
1373 struct fnfieldlist
1374 {
1375 const char *name = nullptr;
1376 std::vector<struct fn_field> fnfields;
1377 };
1378
1379 /* The routines that read and process dies for a C struct or C++ class
1380 pass lists of data member fields and lists of member function fields
1381 in an instance of a field_info structure, as defined below. */
1382 struct field_info
1383 {
1384 /* List of data member and baseclasses fields. */
1385 std::vector<struct nextfield> fields;
1386 std::vector<struct nextfield> baseclasses;
1387
1388 /* Number of fields (including baseclasses). */
1389 int nfields = 0;
1390
1391 /* Set if the accesibility of one of the fields is not public. */
1392 int non_public_fields = 0;
1393
1394 /* Member function fieldlist array, contains name of possibly overloaded
1395 member function, number of overloaded member functions and a pointer
1396 to the head of the member function field chain. */
1397 std::vector<struct fnfieldlist> fnfieldlists;
1398
1399 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1400 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1401 std::vector<struct decl_field> typedef_field_list;
1402
1403 /* Nested types defined by this class and the number of elements in this
1404 list. */
1405 std::vector<struct decl_field> nested_types_list;
1406 };
1407
1408 /* One item on the queue of compilation units to read in full symbols
1409 for. */
1410 struct dwarf2_queue_item
1411 {
1412 struct dwarf2_per_cu_data *per_cu;
1413 enum language pretend_language;
1414 struct dwarf2_queue_item *next;
1415 };
1416
1417 /* The current queue. */
1418 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1419
1420 /* Loaded secondary compilation units are kept in memory until they
1421 have not been referenced for the processing of this many
1422 compilation units. Set this to zero to disable caching. Cache
1423 sizes of up to at least twenty will improve startup time for
1424 typical inter-CU-reference binaries, at an obvious memory cost. */
1425 static int dwarf_max_cache_age = 5;
1426 static void
1427 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1428 struct cmd_list_element *c, const char *value)
1429 {
1430 fprintf_filtered (file, _("The upper bound on the age of cached "
1431 "DWARF compilation units is %s.\n"),
1432 value);
1433 }
1434 \f
1435 /* local function prototypes */
1436
1437 static const char *get_section_name (const struct dwarf2_section_info *);
1438
1439 static const char *get_section_file_name (const struct dwarf2_section_info *);
1440
1441 static void dwarf2_find_base_address (struct die_info *die,
1442 struct dwarf2_cu *cu);
1443
1444 static struct partial_symtab *create_partial_symtab
1445 (struct dwarf2_per_cu_data *per_cu, const char *name);
1446
1447 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1448 const gdb_byte *info_ptr,
1449 struct die_info *type_unit_die,
1450 int has_children, void *data);
1451
1452 static void dwarf2_build_psymtabs_hard
1453 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1454
1455 static void scan_partial_symbols (struct partial_die_info *,
1456 CORE_ADDR *, CORE_ADDR *,
1457 int, struct dwarf2_cu *);
1458
1459 static void add_partial_symbol (struct partial_die_info *,
1460 struct dwarf2_cu *);
1461
1462 static void add_partial_namespace (struct partial_die_info *pdi,
1463 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1464 int set_addrmap, struct dwarf2_cu *cu);
1465
1466 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1467 CORE_ADDR *highpc, int set_addrmap,
1468 struct dwarf2_cu *cu);
1469
1470 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1471 struct dwarf2_cu *cu);
1472
1473 static void add_partial_subprogram (struct partial_die_info *pdi,
1474 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1475 int need_pc, struct dwarf2_cu *cu);
1476
1477 static void dwarf2_read_symtab (struct partial_symtab *,
1478 struct objfile *);
1479
1480 static void psymtab_to_symtab_1 (struct partial_symtab *);
1481
1482 static abbrev_table_up abbrev_table_read_table
1483 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1484 sect_offset);
1485
1486 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1487
1488 static struct partial_die_info *load_partial_dies
1489 (const struct die_reader_specs *, const gdb_byte *, int);
1490
1491 /* A pair of partial_die_info and compilation unit. */
1492 struct cu_partial_die_info
1493 {
1494 /* The compilation unit of the partial_die_info. */
1495 struct dwarf2_cu *cu;
1496 /* A partial_die_info. */
1497 struct partial_die_info *pdi;
1498
1499 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1500 : cu (cu),
1501 pdi (pdi)
1502 { /* Nothhing. */ }
1503
1504 private:
1505 cu_partial_die_info () = delete;
1506 };
1507
1508 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1509 struct dwarf2_cu *);
1510
1511 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1512 struct attribute *, struct attr_abbrev *,
1513 const gdb_byte *);
1514
1515 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1516
1517 static int read_1_signed_byte (bfd *, const gdb_byte *);
1518
1519 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1520
1521 /* Read the next three bytes (little-endian order) as an unsigned integer. */
1522 static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1523
1524 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1525
1526 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1527
1528 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1529 unsigned int *);
1530
1531 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1532
1533 static LONGEST read_checked_initial_length_and_offset
1534 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1535 unsigned int *, unsigned int *);
1536
1537 static LONGEST read_offset (bfd *, const gdb_byte *,
1538 const struct comp_unit_head *,
1539 unsigned int *);
1540
1541 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1542
1543 static sect_offset read_abbrev_offset
1544 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1545 struct dwarf2_section_info *, sect_offset);
1546
1547 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1548
1549 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1550
1551 static const char *read_indirect_string
1552 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1553 const struct comp_unit_head *, unsigned int *);
1554
1555 static const char *read_indirect_line_string
1556 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1557 const struct comp_unit_head *, unsigned int *);
1558
1559 static const char *read_indirect_string_at_offset
1560 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1561 LONGEST str_offset);
1562
1563 static const char *read_indirect_string_from_dwz
1564 (struct objfile *objfile, struct dwz_file *, LONGEST);
1565
1566 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1567
1568 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1569 const gdb_byte *,
1570 unsigned int *);
1571
1572 static const char *read_str_index (const struct die_reader_specs *reader,
1573 ULONGEST str_index);
1574
1575 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1576
1577 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1578 struct dwarf2_cu *);
1579
1580 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1581 unsigned int);
1582
1583 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1584 struct dwarf2_cu *cu);
1585
1586 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1587
1588 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1589 struct dwarf2_cu *cu);
1590
1591 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1592
1593 static struct die_info *die_specification (struct die_info *die,
1594 struct dwarf2_cu **);
1595
1596 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1597 struct dwarf2_cu *cu);
1598
1599 static void dwarf_decode_lines (struct line_header *, const char *,
1600 struct dwarf2_cu *, struct partial_symtab *,
1601 CORE_ADDR, int decode_mapping);
1602
1603 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1604 const char *);
1605
1606 static struct symbol *new_symbol (struct die_info *, struct type *,
1607 struct dwarf2_cu *, struct symbol * = NULL);
1608
1609 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1610 struct dwarf2_cu *);
1611
1612 static void dwarf2_const_value_attr (const struct attribute *attr,
1613 struct type *type,
1614 const char *name,
1615 struct obstack *obstack,
1616 struct dwarf2_cu *cu, LONGEST *value,
1617 const gdb_byte **bytes,
1618 struct dwarf2_locexpr_baton **baton);
1619
1620 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1621
1622 static int need_gnat_info (struct dwarf2_cu *);
1623
1624 static struct type *die_descriptive_type (struct die_info *,
1625 struct dwarf2_cu *);
1626
1627 static void set_descriptive_type (struct type *, struct die_info *,
1628 struct dwarf2_cu *);
1629
1630 static struct type *die_containing_type (struct die_info *,
1631 struct dwarf2_cu *);
1632
1633 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1634 struct dwarf2_cu *);
1635
1636 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1637
1638 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1639
1640 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1641
1642 static char *typename_concat (struct obstack *obs, const char *prefix,
1643 const char *suffix, int physname,
1644 struct dwarf2_cu *cu);
1645
1646 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1647
1648 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1649
1650 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1651
1652 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1653
1654 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1655
1656 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1657
1658 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1659 struct dwarf2_cu *, struct partial_symtab *);
1660
1661 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1662 values. Keep the items ordered with increasing constraints compliance. */
1663 enum pc_bounds_kind
1664 {
1665 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1666 PC_BOUNDS_NOT_PRESENT,
1667
1668 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1669 were present but they do not form a valid range of PC addresses. */
1670 PC_BOUNDS_INVALID,
1671
1672 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1673 PC_BOUNDS_RANGES,
1674
1675 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1676 PC_BOUNDS_HIGH_LOW,
1677 };
1678
1679 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1680 CORE_ADDR *, CORE_ADDR *,
1681 struct dwarf2_cu *,
1682 struct partial_symtab *);
1683
1684 static void get_scope_pc_bounds (struct die_info *,
1685 CORE_ADDR *, CORE_ADDR *,
1686 struct dwarf2_cu *);
1687
1688 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1689 CORE_ADDR, struct dwarf2_cu *);
1690
1691 static void dwarf2_add_field (struct field_info *, struct die_info *,
1692 struct dwarf2_cu *);
1693
1694 static void dwarf2_attach_fields_to_type (struct field_info *,
1695 struct type *, struct dwarf2_cu *);
1696
1697 static void dwarf2_add_member_fn (struct field_info *,
1698 struct die_info *, struct type *,
1699 struct dwarf2_cu *);
1700
1701 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1702 struct type *,
1703 struct dwarf2_cu *);
1704
1705 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1706
1707 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1708
1709 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1710
1711 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1712
1713 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1714
1715 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1716
1717 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1718
1719 static struct type *read_module_type (struct die_info *die,
1720 struct dwarf2_cu *cu);
1721
1722 static const char *namespace_name (struct die_info *die,
1723 int *is_anonymous, struct dwarf2_cu *);
1724
1725 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1726
1727 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1728
1729 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1730 struct dwarf2_cu *);
1731
1732 static struct die_info *read_die_and_siblings_1
1733 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1734 struct die_info *);
1735
1736 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1737 const gdb_byte *info_ptr,
1738 const gdb_byte **new_info_ptr,
1739 struct die_info *parent);
1740
1741 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1742 struct die_info **, const gdb_byte *,
1743 int *, int);
1744
1745 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1746 struct die_info **, const gdb_byte *,
1747 int *);
1748
1749 static void process_die (struct die_info *, struct dwarf2_cu *);
1750
1751 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1752 struct obstack *);
1753
1754 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1755
1756 static const char *dwarf2_full_name (const char *name,
1757 struct die_info *die,
1758 struct dwarf2_cu *cu);
1759
1760 static const char *dwarf2_physname (const char *name, struct die_info *die,
1761 struct dwarf2_cu *cu);
1762
1763 static struct die_info *dwarf2_extension (struct die_info *die,
1764 struct dwarf2_cu **);
1765
1766 static const char *dwarf_tag_name (unsigned int);
1767
1768 static const char *dwarf_attr_name (unsigned int);
1769
1770 static const char *dwarf_unit_type_name (int unit_type);
1771
1772 static const char *dwarf_form_name (unsigned int);
1773
1774 static const char *dwarf_bool_name (unsigned int);
1775
1776 static const char *dwarf_type_encoding_name (unsigned int);
1777
1778 static struct die_info *sibling_die (struct die_info *);
1779
1780 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1781
1782 static void dump_die_for_error (struct die_info *);
1783
1784 static void dump_die_1 (struct ui_file *, int level, int max_level,
1785 struct die_info *);
1786
1787 /*static*/ void dump_die (struct die_info *, int max_level);
1788
1789 static void store_in_ref_table (struct die_info *,
1790 struct dwarf2_cu *);
1791
1792 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1793
1794 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1795
1796 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1797 const struct attribute *,
1798 struct dwarf2_cu **);
1799
1800 static struct die_info *follow_die_ref (struct die_info *,
1801 const struct attribute *,
1802 struct dwarf2_cu **);
1803
1804 static struct die_info *follow_die_sig (struct die_info *,
1805 const struct attribute *,
1806 struct dwarf2_cu **);
1807
1808 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1809 struct dwarf2_cu *);
1810
1811 static struct type *get_DW_AT_signature_type (struct die_info *,
1812 const struct attribute *,
1813 struct dwarf2_cu *);
1814
1815 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1816
1817 static void read_signatured_type (struct signatured_type *);
1818
1819 static int attr_to_dynamic_prop (const struct attribute *attr,
1820 struct die_info *die, struct dwarf2_cu *cu,
1821 struct dynamic_prop *prop, struct type *type);
1822
1823 /* memory allocation interface */
1824
1825 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1826
1827 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1828
1829 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1830
1831 static int attr_form_is_block (const struct attribute *);
1832
1833 static int attr_form_is_section_offset (const struct attribute *);
1834
1835 static int attr_form_is_constant (const struct attribute *);
1836
1837 static int attr_form_is_ref (const struct attribute *);
1838
1839 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1840 struct dwarf2_loclist_baton *baton,
1841 const struct attribute *attr);
1842
1843 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1844 struct symbol *sym,
1845 struct dwarf2_cu *cu,
1846 int is_block);
1847
1848 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1849 const gdb_byte *info_ptr,
1850 struct abbrev_info *abbrev);
1851
1852 static hashval_t partial_die_hash (const void *item);
1853
1854 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1855
1856 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1857 (sect_offset sect_off, unsigned int offset_in_dwz,
1858 struct dwarf2_per_objfile *dwarf2_per_objfile);
1859
1860 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1861 struct die_info *comp_unit_die,
1862 enum language pretend_language);
1863
1864 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1865
1866 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1867
1868 static struct type *set_die_type (struct die_info *, struct type *,
1869 struct dwarf2_cu *);
1870
1871 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1872
1873 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1874
1875 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1876 enum language);
1877
1878 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1879 enum language);
1880
1881 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1882 enum language);
1883
1884 static void dwarf2_add_dependence (struct dwarf2_cu *,
1885 struct dwarf2_per_cu_data *);
1886
1887 static void dwarf2_mark (struct dwarf2_cu *);
1888
1889 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1890
1891 static struct type *get_die_type_at_offset (sect_offset,
1892 struct dwarf2_per_cu_data *);
1893
1894 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1895
1896 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1897 enum language pretend_language);
1898
1899 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1900
1901 static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1902 static struct type *dwarf2_per_cu_addr_sized_int_type
1903 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1904
1905 /* Class, the destructor of which frees all allocated queue entries. This
1906 will only have work to do if an error was thrown while processing the
1907 dwarf. If no error was thrown then the queue entries should have all
1908 been processed, and freed, as we went along. */
1909
1910 class dwarf2_queue_guard
1911 {
1912 public:
1913 dwarf2_queue_guard () = default;
1914
1915 /* Free any entries remaining on the queue. There should only be
1916 entries left if we hit an error while processing the dwarf. */
1917 ~dwarf2_queue_guard ()
1918 {
1919 struct dwarf2_queue_item *item, *last;
1920
1921 item = dwarf2_queue;
1922 while (item)
1923 {
1924 /* Anything still marked queued is likely to be in an
1925 inconsistent state, so discard it. */
1926 if (item->per_cu->queued)
1927 {
1928 if (item->per_cu->cu != NULL)
1929 free_one_cached_comp_unit (item->per_cu);
1930 item->per_cu->queued = 0;
1931 }
1932
1933 last = item;
1934 item = item->next;
1935 xfree (last);
1936 }
1937
1938 dwarf2_queue = dwarf2_queue_tail = NULL;
1939 }
1940 };
1941
1942 /* The return type of find_file_and_directory. Note, the enclosed
1943 string pointers are only valid while this object is valid. */
1944
1945 struct file_and_directory
1946 {
1947 /* The filename. This is never NULL. */
1948 const char *name;
1949
1950 /* The compilation directory. NULL if not known. If we needed to
1951 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1952 points directly to the DW_AT_comp_dir string attribute owned by
1953 the obstack that owns the DIE. */
1954 const char *comp_dir;
1955
1956 /* If we needed to build a new string for comp_dir, this is what
1957 owns the storage. */
1958 std::string comp_dir_storage;
1959 };
1960
1961 static file_and_directory find_file_and_directory (struct die_info *die,
1962 struct dwarf2_cu *cu);
1963
1964 static char *file_full_name (int file, struct line_header *lh,
1965 const char *comp_dir);
1966
1967 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1968 enum class rcuh_kind { COMPILE, TYPE };
1969
1970 static const gdb_byte *read_and_check_comp_unit_head
1971 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1972 struct comp_unit_head *header,
1973 struct dwarf2_section_info *section,
1974 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1975 rcuh_kind section_kind);
1976
1977 static void init_cutu_and_read_dies
1978 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1979 int use_existing_cu, int keep, bool skip_partial,
1980 die_reader_func_ftype *die_reader_func, void *data);
1981
1982 static void init_cutu_and_read_dies_simple
1983 (struct dwarf2_per_cu_data *this_cu,
1984 die_reader_func_ftype *die_reader_func, void *data);
1985
1986 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1987
1988 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1989
1990 static struct dwo_unit *lookup_dwo_unit_in_dwp
1991 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1992 struct dwp_file *dwp_file, const char *comp_dir,
1993 ULONGEST signature, int is_debug_types);
1994
1995 static struct dwp_file *get_dwp_file
1996 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1997
1998 static struct dwo_unit *lookup_dwo_comp_unit
1999 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2000
2001 static struct dwo_unit *lookup_dwo_type_unit
2002 (struct signatured_type *, const char *, const char *);
2003
2004 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2005
2006 /* A unique pointer to a dwo_file. */
2007
2008 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
2009
2010 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2011
2012 static void check_producer (struct dwarf2_cu *cu);
2013
2014 static void free_line_header_voidp (void *arg);
2015 \f
2016 /* Various complaints about symbol reading that don't abort the process. */
2017
2018 static void
2019 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2020 {
2021 complaint (_("statement list doesn't fit in .debug_line section"));
2022 }
2023
2024 static void
2025 dwarf2_debug_line_missing_file_complaint (void)
2026 {
2027 complaint (_(".debug_line section has line data without a file"));
2028 }
2029
2030 static void
2031 dwarf2_debug_line_missing_end_sequence_complaint (void)
2032 {
2033 complaint (_(".debug_line section has line "
2034 "program sequence without an end"));
2035 }
2036
2037 static void
2038 dwarf2_complex_location_expr_complaint (void)
2039 {
2040 complaint (_("location expression too complex"));
2041 }
2042
2043 static void
2044 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2045 int arg3)
2046 {
2047 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
2048 arg1, arg2, arg3);
2049 }
2050
2051 static void
2052 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2053 {
2054 complaint (_("debug info runs off end of %s section"
2055 " [in module %s]"),
2056 get_section_name (section),
2057 get_section_file_name (section));
2058 }
2059
2060 static void
2061 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2062 {
2063 complaint (_("macro debug info contains a "
2064 "malformed macro definition:\n`%s'"),
2065 arg1);
2066 }
2067
2068 static void
2069 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2070 {
2071 complaint (_("invalid attribute class or form for '%s' in '%s'"),
2072 arg1, arg2);
2073 }
2074
2075 /* Hash function for line_header_hash. */
2076
2077 static hashval_t
2078 line_header_hash (const struct line_header *ofs)
2079 {
2080 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2081 }
2082
2083 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2084
2085 static hashval_t
2086 line_header_hash_voidp (const void *item)
2087 {
2088 const struct line_header *ofs = (const struct line_header *) item;
2089
2090 return line_header_hash (ofs);
2091 }
2092
2093 /* Equality function for line_header_hash. */
2094
2095 static int
2096 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2097 {
2098 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2099 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2100
2101 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2102 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2103 }
2104
2105 \f
2106
2107 /* Read the given attribute value as an address, taking the attribute's
2108 form into account. */
2109
2110 static CORE_ADDR
2111 attr_value_as_address (struct attribute *attr)
2112 {
2113 CORE_ADDR addr;
2114
2115 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2116 && attr->form != DW_FORM_GNU_addr_index)
2117 {
2118 /* Aside from a few clearly defined exceptions, attributes that
2119 contain an address must always be in DW_FORM_addr form.
2120 Unfortunately, some compilers happen to be violating this
2121 requirement by encoding addresses using other forms, such
2122 as DW_FORM_data4 for example. For those broken compilers,
2123 we try to do our best, without any guarantee of success,
2124 to interpret the address correctly. It would also be nice
2125 to generate a complaint, but that would require us to maintain
2126 a list of legitimate cases where a non-address form is allowed,
2127 as well as update callers to pass in at least the CU's DWARF
2128 version. This is more overhead than what we're willing to
2129 expand for a pretty rare case. */
2130 addr = DW_UNSND (attr);
2131 }
2132 else
2133 addr = DW_ADDR (attr);
2134
2135 return addr;
2136 }
2137
2138 /* See declaration. */
2139
2140 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2141 const dwarf2_debug_sections *names)
2142 : objfile (objfile_)
2143 {
2144 if (names == NULL)
2145 names = &dwarf2_elf_names;
2146
2147 bfd *obfd = objfile->obfd;
2148
2149 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2150 locate_sections (obfd, sec, *names);
2151 }
2152
2153 dwarf2_per_objfile::~dwarf2_per_objfile ()
2154 {
2155 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2156 free_cached_comp_units ();
2157
2158 if (quick_file_names_table)
2159 htab_delete (quick_file_names_table);
2160
2161 if (line_header_hash)
2162 htab_delete (line_header_hash);
2163
2164 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2165 VEC_free (dwarf2_per_cu_ptr, per_cu->imported_symtabs);
2166
2167 for (signatured_type *sig_type : all_type_units)
2168 VEC_free (dwarf2_per_cu_ptr, sig_type->per_cu.imported_symtabs);
2169
2170 /* Everything else should be on the objfile obstack. */
2171 }
2172
2173 /* See declaration. */
2174
2175 void
2176 dwarf2_per_objfile::free_cached_comp_units ()
2177 {
2178 dwarf2_per_cu_data *per_cu = read_in_chain;
2179 dwarf2_per_cu_data **last_chain = &read_in_chain;
2180 while (per_cu != NULL)
2181 {
2182 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2183
2184 delete per_cu->cu;
2185 *last_chain = next_cu;
2186 per_cu = next_cu;
2187 }
2188 }
2189
2190 /* A helper class that calls free_cached_comp_units on
2191 destruction. */
2192
2193 class free_cached_comp_units
2194 {
2195 public:
2196
2197 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2198 : m_per_objfile (per_objfile)
2199 {
2200 }
2201
2202 ~free_cached_comp_units ()
2203 {
2204 m_per_objfile->free_cached_comp_units ();
2205 }
2206
2207 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2208
2209 private:
2210
2211 dwarf2_per_objfile *m_per_objfile;
2212 };
2213
2214 /* Try to locate the sections we need for DWARF 2 debugging
2215 information and return true if we have enough to do something.
2216 NAMES points to the dwarf2 section names, or is NULL if the standard
2217 ELF names are used. */
2218
2219 int
2220 dwarf2_has_info (struct objfile *objfile,
2221 const struct dwarf2_debug_sections *names)
2222 {
2223 if (objfile->flags & OBJF_READNEVER)
2224 return 0;
2225
2226 struct dwarf2_per_objfile *dwarf2_per_objfile
2227 = get_dwarf2_per_objfile (objfile);
2228
2229 if (dwarf2_per_objfile == NULL)
2230 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2231 names);
2232
2233 return (!dwarf2_per_objfile->info.is_virtual
2234 && dwarf2_per_objfile->info.s.section != NULL
2235 && !dwarf2_per_objfile->abbrev.is_virtual
2236 && dwarf2_per_objfile->abbrev.s.section != NULL);
2237 }
2238
2239 /* Return the containing section of virtual section SECTION. */
2240
2241 static struct dwarf2_section_info *
2242 get_containing_section (const struct dwarf2_section_info *section)
2243 {
2244 gdb_assert (section->is_virtual);
2245 return section->s.containing_section;
2246 }
2247
2248 /* Return the bfd owner of SECTION. */
2249
2250 static struct bfd *
2251 get_section_bfd_owner (const struct dwarf2_section_info *section)
2252 {
2253 if (section->is_virtual)
2254 {
2255 section = get_containing_section (section);
2256 gdb_assert (!section->is_virtual);
2257 }
2258 return section->s.section->owner;
2259 }
2260
2261 /* Return the bfd section of SECTION.
2262 Returns NULL if the section is not present. */
2263
2264 static asection *
2265 get_section_bfd_section (const struct dwarf2_section_info *section)
2266 {
2267 if (section->is_virtual)
2268 {
2269 section = get_containing_section (section);
2270 gdb_assert (!section->is_virtual);
2271 }
2272 return section->s.section;
2273 }
2274
2275 /* Return the name of SECTION. */
2276
2277 static const char *
2278 get_section_name (const struct dwarf2_section_info *section)
2279 {
2280 asection *sectp = get_section_bfd_section (section);
2281
2282 gdb_assert (sectp != NULL);
2283 return bfd_section_name (sectp);
2284 }
2285
2286 /* Return the name of the file SECTION is in. */
2287
2288 static const char *
2289 get_section_file_name (const struct dwarf2_section_info *section)
2290 {
2291 bfd *abfd = get_section_bfd_owner (section);
2292
2293 return bfd_get_filename (abfd);
2294 }
2295
2296 /* Return the id of SECTION.
2297 Returns 0 if SECTION doesn't exist. */
2298
2299 static int
2300 get_section_id (const struct dwarf2_section_info *section)
2301 {
2302 asection *sectp = get_section_bfd_section (section);
2303
2304 if (sectp == NULL)
2305 return 0;
2306 return sectp->id;
2307 }
2308
2309 /* Return the flags of SECTION.
2310 SECTION (or containing section if this is a virtual section) must exist. */
2311
2312 static int
2313 get_section_flags (const struct dwarf2_section_info *section)
2314 {
2315 asection *sectp = get_section_bfd_section (section);
2316
2317 gdb_assert (sectp != NULL);
2318 return bfd_section_flags (sectp);
2319 }
2320
2321 /* When loading sections, we look either for uncompressed section or for
2322 compressed section names. */
2323
2324 static int
2325 section_is_p (const char *section_name,
2326 const struct dwarf2_section_names *names)
2327 {
2328 if (names->normal != NULL
2329 && strcmp (section_name, names->normal) == 0)
2330 return 1;
2331 if (names->compressed != NULL
2332 && strcmp (section_name, names->compressed) == 0)
2333 return 1;
2334 return 0;
2335 }
2336
2337 /* See declaration. */
2338
2339 void
2340 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2341 const dwarf2_debug_sections &names)
2342 {
2343 flagword aflag = bfd_section_flags (sectp);
2344
2345 if ((aflag & SEC_HAS_CONTENTS) == 0)
2346 {
2347 }
2348 else if (section_is_p (sectp->name, &names.info))
2349 {
2350 this->info.s.section = sectp;
2351 this->info.size = bfd_section_size (sectp);
2352 }
2353 else if (section_is_p (sectp->name, &names.abbrev))
2354 {
2355 this->abbrev.s.section = sectp;
2356 this->abbrev.size = bfd_section_size (sectp);
2357 }
2358 else if (section_is_p (sectp->name, &names.line))
2359 {
2360 this->line.s.section = sectp;
2361 this->line.size = bfd_section_size (sectp);
2362 }
2363 else if (section_is_p (sectp->name, &names.loc))
2364 {
2365 this->loc.s.section = sectp;
2366 this->loc.size = bfd_section_size (sectp);
2367 }
2368 else if (section_is_p (sectp->name, &names.loclists))
2369 {
2370 this->loclists.s.section = sectp;
2371 this->loclists.size = bfd_section_size (sectp);
2372 }
2373 else if (section_is_p (sectp->name, &names.macinfo))
2374 {
2375 this->macinfo.s.section = sectp;
2376 this->macinfo.size = bfd_section_size (sectp);
2377 }
2378 else if (section_is_p (sectp->name, &names.macro))
2379 {
2380 this->macro.s.section = sectp;
2381 this->macro.size = bfd_section_size (sectp);
2382 }
2383 else if (section_is_p (sectp->name, &names.str))
2384 {
2385 this->str.s.section = sectp;
2386 this->str.size = bfd_section_size (sectp);
2387 }
2388 else if (section_is_p (sectp->name, &names.line_str))
2389 {
2390 this->line_str.s.section = sectp;
2391 this->line_str.size = bfd_section_size (sectp);
2392 }
2393 else if (section_is_p (sectp->name, &names.addr))
2394 {
2395 this->addr.s.section = sectp;
2396 this->addr.size = bfd_section_size (sectp);
2397 }
2398 else if (section_is_p (sectp->name, &names.frame))
2399 {
2400 this->frame.s.section = sectp;
2401 this->frame.size = bfd_section_size (sectp);
2402 }
2403 else if (section_is_p (sectp->name, &names.eh_frame))
2404 {
2405 this->eh_frame.s.section = sectp;
2406 this->eh_frame.size = bfd_section_size (sectp);
2407 }
2408 else if (section_is_p (sectp->name, &names.ranges))
2409 {
2410 this->ranges.s.section = sectp;
2411 this->ranges.size = bfd_section_size (sectp);
2412 }
2413 else if (section_is_p (sectp->name, &names.rnglists))
2414 {
2415 this->rnglists.s.section = sectp;
2416 this->rnglists.size = bfd_section_size (sectp);
2417 }
2418 else if (section_is_p (sectp->name, &names.types))
2419 {
2420 struct dwarf2_section_info type_section;
2421
2422 memset (&type_section, 0, sizeof (type_section));
2423 type_section.s.section = sectp;
2424 type_section.size = bfd_section_size (sectp);
2425
2426 this->types.push_back (type_section);
2427 }
2428 else if (section_is_p (sectp->name, &names.gdb_index))
2429 {
2430 this->gdb_index.s.section = sectp;
2431 this->gdb_index.size = bfd_section_size (sectp);
2432 }
2433 else if (section_is_p (sectp->name, &names.debug_names))
2434 {
2435 this->debug_names.s.section = sectp;
2436 this->debug_names.size = bfd_section_size (sectp);
2437 }
2438 else if (section_is_p (sectp->name, &names.debug_aranges))
2439 {
2440 this->debug_aranges.s.section = sectp;
2441 this->debug_aranges.size = bfd_section_size (sectp);
2442 }
2443
2444 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2445 && bfd_section_vma (sectp) == 0)
2446 this->has_section_at_zero = true;
2447 }
2448
2449 /* A helper function that decides whether a section is empty,
2450 or not present. */
2451
2452 static int
2453 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2454 {
2455 if (section->is_virtual)
2456 return section->size == 0;
2457 return section->s.section == NULL || section->size == 0;
2458 }
2459
2460 /* See dwarf2read.h. */
2461
2462 void
2463 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2464 {
2465 asection *sectp;
2466 bfd *abfd;
2467 gdb_byte *buf, *retbuf;
2468
2469 if (info->readin)
2470 return;
2471 info->buffer = NULL;
2472 info->readin = true;
2473
2474 if (dwarf2_section_empty_p (info))
2475 return;
2476
2477 sectp = get_section_bfd_section (info);
2478
2479 /* If this is a virtual section we need to read in the real one first. */
2480 if (info->is_virtual)
2481 {
2482 struct dwarf2_section_info *containing_section =
2483 get_containing_section (info);
2484
2485 gdb_assert (sectp != NULL);
2486 if ((sectp->flags & SEC_RELOC) != 0)
2487 {
2488 error (_("Dwarf Error: DWP format V2 with relocations is not"
2489 " supported in section %s [in module %s]"),
2490 get_section_name (info), get_section_file_name (info));
2491 }
2492 dwarf2_read_section (objfile, containing_section);
2493 /* Other code should have already caught virtual sections that don't
2494 fit. */
2495 gdb_assert (info->virtual_offset + info->size
2496 <= containing_section->size);
2497 /* If the real section is empty or there was a problem reading the
2498 section we shouldn't get here. */
2499 gdb_assert (containing_section->buffer != NULL);
2500 info->buffer = containing_section->buffer + info->virtual_offset;
2501 return;
2502 }
2503
2504 /* If the section has relocations, we must read it ourselves.
2505 Otherwise we attach it to the BFD. */
2506 if ((sectp->flags & SEC_RELOC) == 0)
2507 {
2508 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2509 return;
2510 }
2511
2512 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2513 info->buffer = buf;
2514
2515 /* When debugging .o files, we may need to apply relocations; see
2516 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2517 We never compress sections in .o files, so we only need to
2518 try this when the section is not compressed. */
2519 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2520 if (retbuf != NULL)
2521 {
2522 info->buffer = retbuf;
2523 return;
2524 }
2525
2526 abfd = get_section_bfd_owner (info);
2527 gdb_assert (abfd != NULL);
2528
2529 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2530 || bfd_bread (buf, info->size, abfd) != info->size)
2531 {
2532 error (_("Dwarf Error: Can't read DWARF data"
2533 " in section %s [in module %s]"),
2534 bfd_section_name (sectp), bfd_get_filename (abfd));
2535 }
2536 }
2537
2538 /* A helper function that returns the size of a section in a safe way.
2539 If you are positive that the section has been read before using the
2540 size, then it is safe to refer to the dwarf2_section_info object's
2541 "size" field directly. In other cases, you must call this
2542 function, because for compressed sections the size field is not set
2543 correctly until the section has been read. */
2544
2545 static bfd_size_type
2546 dwarf2_section_size (struct objfile *objfile,
2547 struct dwarf2_section_info *info)
2548 {
2549 if (!info->readin)
2550 dwarf2_read_section (objfile, info);
2551 return info->size;
2552 }
2553
2554 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2555 SECTION_NAME. */
2556
2557 void
2558 dwarf2_get_section_info (struct objfile *objfile,
2559 enum dwarf2_section_enum sect,
2560 asection **sectp, const gdb_byte **bufp,
2561 bfd_size_type *sizep)
2562 {
2563 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2564 struct dwarf2_section_info *info;
2565
2566 /* We may see an objfile without any DWARF, in which case we just
2567 return nothing. */
2568 if (data == NULL)
2569 {
2570 *sectp = NULL;
2571 *bufp = NULL;
2572 *sizep = 0;
2573 return;
2574 }
2575 switch (sect)
2576 {
2577 case DWARF2_DEBUG_FRAME:
2578 info = &data->frame;
2579 break;
2580 case DWARF2_EH_FRAME:
2581 info = &data->eh_frame;
2582 break;
2583 default:
2584 gdb_assert_not_reached ("unexpected section");
2585 }
2586
2587 dwarf2_read_section (objfile, info);
2588
2589 *sectp = get_section_bfd_section (info);
2590 *bufp = info->buffer;
2591 *sizep = info->size;
2592 }
2593
2594 /* A helper function to find the sections for a .dwz file. */
2595
2596 static void
2597 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2598 {
2599 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2600
2601 /* Note that we only support the standard ELF names, because .dwz
2602 is ELF-only (at the time of writing). */
2603 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2604 {
2605 dwz_file->abbrev.s.section = sectp;
2606 dwz_file->abbrev.size = bfd_section_size (sectp);
2607 }
2608 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2609 {
2610 dwz_file->info.s.section = sectp;
2611 dwz_file->info.size = bfd_section_size (sectp);
2612 }
2613 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2614 {
2615 dwz_file->str.s.section = sectp;
2616 dwz_file->str.size = bfd_section_size (sectp);
2617 }
2618 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2619 {
2620 dwz_file->line.s.section = sectp;
2621 dwz_file->line.size = bfd_section_size (sectp);
2622 }
2623 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2624 {
2625 dwz_file->macro.s.section = sectp;
2626 dwz_file->macro.size = bfd_section_size (sectp);
2627 }
2628 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2629 {
2630 dwz_file->gdb_index.s.section = sectp;
2631 dwz_file->gdb_index.size = bfd_section_size (sectp);
2632 }
2633 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2634 {
2635 dwz_file->debug_names.s.section = sectp;
2636 dwz_file->debug_names.size = bfd_section_size (sectp);
2637 }
2638 }
2639
2640 /* See dwarf2read.h. */
2641
2642 struct dwz_file *
2643 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2644 {
2645 const char *filename;
2646 bfd_size_type buildid_len_arg;
2647 size_t buildid_len;
2648 bfd_byte *buildid;
2649
2650 if (dwarf2_per_objfile->dwz_file != NULL)
2651 return dwarf2_per_objfile->dwz_file.get ();
2652
2653 bfd_set_error (bfd_error_no_error);
2654 gdb::unique_xmalloc_ptr<char> data
2655 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2656 &buildid_len_arg, &buildid));
2657 if (data == NULL)
2658 {
2659 if (bfd_get_error () == bfd_error_no_error)
2660 return NULL;
2661 error (_("could not read '.gnu_debugaltlink' section: %s"),
2662 bfd_errmsg (bfd_get_error ()));
2663 }
2664
2665 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2666
2667 buildid_len = (size_t) buildid_len_arg;
2668
2669 filename = data.get ();
2670
2671 std::string abs_storage;
2672 if (!IS_ABSOLUTE_PATH (filename))
2673 {
2674 gdb::unique_xmalloc_ptr<char> abs
2675 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2676
2677 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2678 filename = abs_storage.c_str ();
2679 }
2680
2681 /* First try the file name given in the section. If that doesn't
2682 work, try to use the build-id instead. */
2683 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2684 if (dwz_bfd != NULL)
2685 {
2686 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2687 dwz_bfd.reset (nullptr);
2688 }
2689
2690 if (dwz_bfd == NULL)
2691 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2692
2693 if (dwz_bfd == NULL)
2694 error (_("could not find '.gnu_debugaltlink' file for %s"),
2695 objfile_name (dwarf2_per_objfile->objfile));
2696
2697 std::unique_ptr<struct dwz_file> result
2698 (new struct dwz_file (std::move (dwz_bfd)));
2699
2700 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2701 result.get ());
2702
2703 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2704 result->dwz_bfd.get ());
2705 dwarf2_per_objfile->dwz_file = std::move (result);
2706 return dwarf2_per_objfile->dwz_file.get ();
2707 }
2708 \f
2709 /* DWARF quick_symbols_functions support. */
2710
2711 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2712 unique line tables, so we maintain a separate table of all .debug_line
2713 derived entries to support the sharing.
2714 All the quick functions need is the list of file names. We discard the
2715 line_header when we're done and don't need to record it here. */
2716 struct quick_file_names
2717 {
2718 /* The data used to construct the hash key. */
2719 struct stmt_list_hash hash;
2720
2721 /* The number of entries in file_names, real_names. */
2722 unsigned int num_file_names;
2723
2724 /* The file names from the line table, after being run through
2725 file_full_name. */
2726 const char **file_names;
2727
2728 /* The file names from the line table after being run through
2729 gdb_realpath. These are computed lazily. */
2730 const char **real_names;
2731 };
2732
2733 /* When using the index (and thus not using psymtabs), each CU has an
2734 object of this type. This is used to hold information needed by
2735 the various "quick" methods. */
2736 struct dwarf2_per_cu_quick_data
2737 {
2738 /* The file table. This can be NULL if there was no file table
2739 or it's currently not read in.
2740 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2741 struct quick_file_names *file_names;
2742
2743 /* The corresponding symbol table. This is NULL if symbols for this
2744 CU have not yet been read. */
2745 struct compunit_symtab *compunit_symtab;
2746
2747 /* A temporary mark bit used when iterating over all CUs in
2748 expand_symtabs_matching. */
2749 unsigned int mark : 1;
2750
2751 /* True if we've tried to read the file table and found there isn't one.
2752 There will be no point in trying to read it again next time. */
2753 unsigned int no_file_data : 1;
2754 };
2755
2756 /* Utility hash function for a stmt_list_hash. */
2757
2758 static hashval_t
2759 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2760 {
2761 hashval_t v = 0;
2762
2763 if (stmt_list_hash->dwo_unit != NULL)
2764 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2765 v += to_underlying (stmt_list_hash->line_sect_off);
2766 return v;
2767 }
2768
2769 /* Utility equality function for a stmt_list_hash. */
2770
2771 static int
2772 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2773 const struct stmt_list_hash *rhs)
2774 {
2775 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2776 return 0;
2777 if (lhs->dwo_unit != NULL
2778 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2779 return 0;
2780
2781 return lhs->line_sect_off == rhs->line_sect_off;
2782 }
2783
2784 /* Hash function for a quick_file_names. */
2785
2786 static hashval_t
2787 hash_file_name_entry (const void *e)
2788 {
2789 const struct quick_file_names *file_data
2790 = (const struct quick_file_names *) e;
2791
2792 return hash_stmt_list_entry (&file_data->hash);
2793 }
2794
2795 /* Equality function for a quick_file_names. */
2796
2797 static int
2798 eq_file_name_entry (const void *a, const void *b)
2799 {
2800 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2801 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2802
2803 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2804 }
2805
2806 /* Delete function for a quick_file_names. */
2807
2808 static void
2809 delete_file_name_entry (void *e)
2810 {
2811 struct quick_file_names *file_data = (struct quick_file_names *) e;
2812 int i;
2813
2814 for (i = 0; i < file_data->num_file_names; ++i)
2815 {
2816 xfree ((void*) file_data->file_names[i]);
2817 if (file_data->real_names)
2818 xfree ((void*) file_data->real_names[i]);
2819 }
2820
2821 /* The space for the struct itself lives on objfile_obstack,
2822 so we don't free it here. */
2823 }
2824
2825 /* Create a quick_file_names hash table. */
2826
2827 static htab_t
2828 create_quick_file_names_table (unsigned int nr_initial_entries)
2829 {
2830 return htab_create_alloc (nr_initial_entries,
2831 hash_file_name_entry, eq_file_name_entry,
2832 delete_file_name_entry, xcalloc, xfree);
2833 }
2834
2835 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2836 have to be created afterwards. You should call age_cached_comp_units after
2837 processing PER_CU->CU. dw2_setup must have been already called. */
2838
2839 static void
2840 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2841 {
2842 if (per_cu->is_debug_types)
2843 load_full_type_unit (per_cu);
2844 else
2845 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2846
2847 if (per_cu->cu == NULL)
2848 return; /* Dummy CU. */
2849
2850 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2851 }
2852
2853 /* Read in the symbols for PER_CU. */
2854
2855 static void
2856 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2857 {
2858 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2859
2860 /* Skip type_unit_groups, reading the type units they contain
2861 is handled elsewhere. */
2862 if (IS_TYPE_UNIT_GROUP (per_cu))
2863 return;
2864
2865 /* The destructor of dwarf2_queue_guard frees any entries left on
2866 the queue. After this point we're guaranteed to leave this function
2867 with the dwarf queue empty. */
2868 dwarf2_queue_guard q_guard;
2869
2870 if (dwarf2_per_objfile->using_index
2871 ? per_cu->v.quick->compunit_symtab == NULL
2872 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2873 {
2874 queue_comp_unit (per_cu, language_minimal);
2875 load_cu (per_cu, skip_partial);
2876
2877 /* If we just loaded a CU from a DWO, and we're working with an index
2878 that may badly handle TUs, load all the TUs in that DWO as well.
2879 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2880 if (!per_cu->is_debug_types
2881 && per_cu->cu != NULL
2882 && per_cu->cu->dwo_unit != NULL
2883 && dwarf2_per_objfile->index_table != NULL
2884 && dwarf2_per_objfile->index_table->version <= 7
2885 /* DWP files aren't supported yet. */
2886 && get_dwp_file (dwarf2_per_objfile) == NULL)
2887 queue_and_load_all_dwo_tus (per_cu);
2888 }
2889
2890 process_queue (dwarf2_per_objfile);
2891
2892 /* Age the cache, releasing compilation units that have not
2893 been used recently. */
2894 age_cached_comp_units (dwarf2_per_objfile);
2895 }
2896
2897 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2898 the objfile from which this CU came. Returns the resulting symbol
2899 table. */
2900
2901 static struct compunit_symtab *
2902 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2903 {
2904 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2905
2906 gdb_assert (dwarf2_per_objfile->using_index);
2907 if (!per_cu->v.quick->compunit_symtab)
2908 {
2909 free_cached_comp_units freer (dwarf2_per_objfile);
2910 scoped_restore decrementer = increment_reading_symtab ();
2911 dw2_do_instantiate_symtab (per_cu, skip_partial);
2912 process_cu_includes (dwarf2_per_objfile);
2913 }
2914
2915 return per_cu->v.quick->compunit_symtab;
2916 }
2917
2918 /* See declaration. */
2919
2920 dwarf2_per_cu_data *
2921 dwarf2_per_objfile::get_cutu (int index)
2922 {
2923 if (index >= this->all_comp_units.size ())
2924 {
2925 index -= this->all_comp_units.size ();
2926 gdb_assert (index < this->all_type_units.size ());
2927 return &this->all_type_units[index]->per_cu;
2928 }
2929
2930 return this->all_comp_units[index];
2931 }
2932
2933 /* See declaration. */
2934
2935 dwarf2_per_cu_data *
2936 dwarf2_per_objfile::get_cu (int index)
2937 {
2938 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2939
2940 return this->all_comp_units[index];
2941 }
2942
2943 /* See declaration. */
2944
2945 signatured_type *
2946 dwarf2_per_objfile::get_tu (int index)
2947 {
2948 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2949
2950 return this->all_type_units[index];
2951 }
2952
2953 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2954 objfile_obstack, and constructed with the specified field
2955 values. */
2956
2957 static dwarf2_per_cu_data *
2958 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2959 struct dwarf2_section_info *section,
2960 int is_dwz,
2961 sect_offset sect_off, ULONGEST length)
2962 {
2963 struct objfile *objfile = dwarf2_per_objfile->objfile;
2964 dwarf2_per_cu_data *the_cu
2965 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2966 struct dwarf2_per_cu_data);
2967 the_cu->sect_off = sect_off;
2968 the_cu->length = length;
2969 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2970 the_cu->section = section;
2971 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2972 struct dwarf2_per_cu_quick_data);
2973 the_cu->is_dwz = is_dwz;
2974 return the_cu;
2975 }
2976
2977 /* A helper for create_cus_from_index that handles a given list of
2978 CUs. */
2979
2980 static void
2981 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2982 const gdb_byte *cu_list, offset_type n_elements,
2983 struct dwarf2_section_info *section,
2984 int is_dwz)
2985 {
2986 for (offset_type i = 0; i < n_elements; i += 2)
2987 {
2988 gdb_static_assert (sizeof (ULONGEST) >= 8);
2989
2990 sect_offset sect_off
2991 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2992 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2993 cu_list += 2 * 8;
2994
2995 dwarf2_per_cu_data *per_cu
2996 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2997 sect_off, length);
2998 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2999 }
3000 }
3001
3002 /* Read the CU list from the mapped index, and use it to create all
3003 the CU objects for this objfile. */
3004
3005 static void
3006 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3007 const gdb_byte *cu_list, offset_type cu_list_elements,
3008 const gdb_byte *dwz_list, offset_type dwz_elements)
3009 {
3010 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3011 dwarf2_per_objfile->all_comp_units.reserve
3012 ((cu_list_elements + dwz_elements) / 2);
3013
3014 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3015 &dwarf2_per_objfile->info, 0);
3016
3017 if (dwz_elements == 0)
3018 return;
3019
3020 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3021 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3022 &dwz->info, 1);
3023 }
3024
3025 /* Create the signatured type hash table from the index. */
3026
3027 static void
3028 create_signatured_type_table_from_index
3029 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3030 struct dwarf2_section_info *section,
3031 const gdb_byte *bytes,
3032 offset_type elements)
3033 {
3034 struct objfile *objfile = dwarf2_per_objfile->objfile;
3035
3036 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3037 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
3038
3039 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3040
3041 for (offset_type i = 0; i < elements; i += 3)
3042 {
3043 struct signatured_type *sig_type;
3044 ULONGEST signature;
3045 void **slot;
3046 cu_offset type_offset_in_tu;
3047
3048 gdb_static_assert (sizeof (ULONGEST) >= 8);
3049 sect_offset sect_off
3050 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3051 type_offset_in_tu
3052 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3053 BFD_ENDIAN_LITTLE);
3054 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3055 bytes += 3 * 8;
3056
3057 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3058 struct signatured_type);
3059 sig_type->signature = signature;
3060 sig_type->type_offset_in_tu = type_offset_in_tu;
3061 sig_type->per_cu.is_debug_types = 1;
3062 sig_type->per_cu.section = section;
3063 sig_type->per_cu.sect_off = sect_off;
3064 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3065 sig_type->per_cu.v.quick
3066 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3067 struct dwarf2_per_cu_quick_data);
3068
3069 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3070 *slot = sig_type;
3071
3072 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3073 }
3074
3075 dwarf2_per_objfile->signatured_types = sig_types_hash;
3076 }
3077
3078 /* Create the signatured type hash table from .debug_names. */
3079
3080 static void
3081 create_signatured_type_table_from_debug_names
3082 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3083 const mapped_debug_names &map,
3084 struct dwarf2_section_info *section,
3085 struct dwarf2_section_info *abbrev_section)
3086 {
3087 struct objfile *objfile = dwarf2_per_objfile->objfile;
3088
3089 dwarf2_read_section (objfile, section);
3090 dwarf2_read_section (objfile, abbrev_section);
3091
3092 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3093 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
3094
3095 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3096
3097 for (uint32_t i = 0; i < map.tu_count; ++i)
3098 {
3099 struct signatured_type *sig_type;
3100 void **slot;
3101
3102 sect_offset sect_off
3103 = (sect_offset) (extract_unsigned_integer
3104 (map.tu_table_reordered + i * map.offset_size,
3105 map.offset_size,
3106 map.dwarf5_byte_order));
3107
3108 comp_unit_head cu_header;
3109 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3110 abbrev_section,
3111 section->buffer + to_underlying (sect_off),
3112 rcuh_kind::TYPE);
3113
3114 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3115 struct signatured_type);
3116 sig_type->signature = cu_header.signature;
3117 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3118 sig_type->per_cu.is_debug_types = 1;
3119 sig_type->per_cu.section = section;
3120 sig_type->per_cu.sect_off = sect_off;
3121 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3122 sig_type->per_cu.v.quick
3123 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3124 struct dwarf2_per_cu_quick_data);
3125
3126 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3127 *slot = sig_type;
3128
3129 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3130 }
3131
3132 dwarf2_per_objfile->signatured_types = sig_types_hash;
3133 }
3134
3135 /* Read the address map data from the mapped index, and use it to
3136 populate the objfile's psymtabs_addrmap. */
3137
3138 static void
3139 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3140 struct mapped_index *index)
3141 {
3142 struct objfile *objfile = dwarf2_per_objfile->objfile;
3143 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3144 const gdb_byte *iter, *end;
3145 struct addrmap *mutable_map;
3146 CORE_ADDR baseaddr;
3147
3148 auto_obstack temp_obstack;
3149
3150 mutable_map = addrmap_create_mutable (&temp_obstack);
3151
3152 iter = index->address_table.data ();
3153 end = iter + index->address_table.size ();
3154
3155 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3156
3157 while (iter < end)
3158 {
3159 ULONGEST hi, lo, cu_index;
3160 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3161 iter += 8;
3162 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3163 iter += 8;
3164 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3165 iter += 4;
3166
3167 if (lo > hi)
3168 {
3169 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
3170 hex_string (lo), hex_string (hi));
3171 continue;
3172 }
3173
3174 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
3175 {
3176 complaint (_(".gdb_index address table has invalid CU number %u"),
3177 (unsigned) cu_index);
3178 continue;
3179 }
3180
3181 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3182 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
3183 addrmap_set_empty (mutable_map, lo, hi - 1,
3184 dwarf2_per_objfile->get_cu (cu_index));
3185 }
3186
3187 objfile->partial_symtabs->psymtabs_addrmap
3188 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3189 }
3190
3191 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3192 populate the objfile's psymtabs_addrmap. */
3193
3194 static void
3195 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3196 struct dwarf2_section_info *section)
3197 {
3198 struct objfile *objfile = dwarf2_per_objfile->objfile;
3199 bfd *abfd = objfile->obfd;
3200 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3201 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3202 SECT_OFF_TEXT (objfile));
3203
3204 auto_obstack temp_obstack;
3205 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3206
3207 std::unordered_map<sect_offset,
3208 dwarf2_per_cu_data *,
3209 gdb::hash_enum<sect_offset>>
3210 debug_info_offset_to_per_cu;
3211 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3212 {
3213 const auto insertpair
3214 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3215 if (!insertpair.second)
3216 {
3217 warning (_("Section .debug_aranges in %s has duplicate "
3218 "debug_info_offset %s, ignoring .debug_aranges."),
3219 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3220 return;
3221 }
3222 }
3223
3224 dwarf2_read_section (objfile, section);
3225
3226 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3227
3228 const gdb_byte *addr = section->buffer;
3229
3230 while (addr < section->buffer + section->size)
3231 {
3232 const gdb_byte *const entry_addr = addr;
3233 unsigned int bytes_read;
3234
3235 const LONGEST entry_length = read_initial_length (abfd, addr,
3236 &bytes_read);
3237 addr += bytes_read;
3238
3239 const gdb_byte *const entry_end = addr + entry_length;
3240 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3241 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3242 if (addr + entry_length > section->buffer + section->size)
3243 {
3244 warning (_("Section .debug_aranges in %s entry at offset %s "
3245 "length %s exceeds section length %s, "
3246 "ignoring .debug_aranges."),
3247 objfile_name (objfile),
3248 plongest (entry_addr - section->buffer),
3249 plongest (bytes_read + entry_length),
3250 pulongest (section->size));
3251 return;
3252 }
3253
3254 /* The version number. */
3255 const uint16_t version = read_2_bytes (abfd, addr);
3256 addr += 2;
3257 if (version != 2)
3258 {
3259 warning (_("Section .debug_aranges in %s entry at offset %s "
3260 "has unsupported version %d, ignoring .debug_aranges."),
3261 objfile_name (objfile),
3262 plongest (entry_addr - section->buffer), version);
3263 return;
3264 }
3265
3266 const uint64_t debug_info_offset
3267 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3268 addr += offset_size;
3269 const auto per_cu_it
3270 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3271 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3272 {
3273 warning (_("Section .debug_aranges in %s entry at offset %s "
3274 "debug_info_offset %s does not exists, "
3275 "ignoring .debug_aranges."),
3276 objfile_name (objfile),
3277 plongest (entry_addr - section->buffer),
3278 pulongest (debug_info_offset));
3279 return;
3280 }
3281 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3282
3283 const uint8_t address_size = *addr++;
3284 if (address_size < 1 || address_size > 8)
3285 {
3286 warning (_("Section .debug_aranges in %s entry at offset %s "
3287 "address_size %u is invalid, ignoring .debug_aranges."),
3288 objfile_name (objfile),
3289 plongest (entry_addr - section->buffer), address_size);
3290 return;
3291 }
3292
3293 const uint8_t segment_selector_size = *addr++;
3294 if (segment_selector_size != 0)
3295 {
3296 warning (_("Section .debug_aranges in %s entry at offset %s "
3297 "segment_selector_size %u is not supported, "
3298 "ignoring .debug_aranges."),
3299 objfile_name (objfile),
3300 plongest (entry_addr - section->buffer),
3301 segment_selector_size);
3302 return;
3303 }
3304
3305 /* Must pad to an alignment boundary that is twice the address
3306 size. It is undocumented by the DWARF standard but GCC does
3307 use it. */
3308 for (size_t padding = ((-(addr - section->buffer))
3309 & (2 * address_size - 1));
3310 padding > 0; padding--)
3311 if (*addr++ != 0)
3312 {
3313 warning (_("Section .debug_aranges in %s entry at offset %s "
3314 "padding is not zero, ignoring .debug_aranges."),
3315 objfile_name (objfile),
3316 plongest (entry_addr - section->buffer));
3317 return;
3318 }
3319
3320 for (;;)
3321 {
3322 if (addr + 2 * address_size > entry_end)
3323 {
3324 warning (_("Section .debug_aranges in %s entry at offset %s "
3325 "address list is not properly terminated, "
3326 "ignoring .debug_aranges."),
3327 objfile_name (objfile),
3328 plongest (entry_addr - section->buffer));
3329 return;
3330 }
3331 ULONGEST start = extract_unsigned_integer (addr, address_size,
3332 dwarf5_byte_order);
3333 addr += address_size;
3334 ULONGEST length = extract_unsigned_integer (addr, address_size,
3335 dwarf5_byte_order);
3336 addr += address_size;
3337 if (start == 0 && length == 0)
3338 break;
3339 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3340 {
3341 /* Symbol was eliminated due to a COMDAT group. */
3342 continue;
3343 }
3344 ULONGEST end = start + length;
3345 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3346 - baseaddr);
3347 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3348 - baseaddr);
3349 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3350 }
3351 }
3352
3353 objfile->partial_symtabs->psymtabs_addrmap
3354 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3355 }
3356
3357 /* Find a slot in the mapped index INDEX for the object named NAME.
3358 If NAME is found, set *VEC_OUT to point to the CU vector in the
3359 constant pool and return true. If NAME cannot be found, return
3360 false. */
3361
3362 static bool
3363 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3364 offset_type **vec_out)
3365 {
3366 offset_type hash;
3367 offset_type slot, step;
3368 int (*cmp) (const char *, const char *);
3369
3370 gdb::unique_xmalloc_ptr<char> without_params;
3371 if (current_language->la_language == language_cplus
3372 || current_language->la_language == language_fortran
3373 || current_language->la_language == language_d)
3374 {
3375 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3376 not contain any. */
3377
3378 if (strchr (name, '(') != NULL)
3379 {
3380 without_params = cp_remove_params (name);
3381
3382 if (without_params != NULL)
3383 name = without_params.get ();
3384 }
3385 }
3386
3387 /* Index version 4 did not support case insensitive searches. But the
3388 indices for case insensitive languages are built in lowercase, therefore
3389 simulate our NAME being searched is also lowercased. */
3390 hash = mapped_index_string_hash ((index->version == 4
3391 && case_sensitivity == case_sensitive_off
3392 ? 5 : index->version),
3393 name);
3394
3395 slot = hash & (index->symbol_table.size () - 1);
3396 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3397 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3398
3399 for (;;)
3400 {
3401 const char *str;
3402
3403 const auto &bucket = index->symbol_table[slot];
3404 if (bucket.name == 0 && bucket.vec == 0)
3405 return false;
3406
3407 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3408 if (!cmp (name, str))
3409 {
3410 *vec_out = (offset_type *) (index->constant_pool
3411 + MAYBE_SWAP (bucket.vec));
3412 return true;
3413 }
3414
3415 slot = (slot + step) & (index->symbol_table.size () - 1);
3416 }
3417 }
3418
3419 /* A helper function that reads the .gdb_index from BUFFER and fills
3420 in MAP. FILENAME is the name of the file containing the data;
3421 it is used for error reporting. DEPRECATED_OK is true if it is
3422 ok to use deprecated sections.
3423
3424 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3425 out parameters that are filled in with information about the CU and
3426 TU lists in the section.
3427
3428 Returns true if all went well, false otherwise. */
3429
3430 static bool
3431 read_gdb_index_from_buffer (struct objfile *objfile,
3432 const char *filename,
3433 bool deprecated_ok,
3434 gdb::array_view<const gdb_byte> buffer,
3435 struct mapped_index *map,
3436 const gdb_byte **cu_list,
3437 offset_type *cu_list_elements,
3438 const gdb_byte **types_list,
3439 offset_type *types_list_elements)
3440 {
3441 const gdb_byte *addr = &buffer[0];
3442
3443 /* Version check. */
3444 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3445 /* Versions earlier than 3 emitted every copy of a psymbol. This
3446 causes the index to behave very poorly for certain requests. Version 3
3447 contained incomplete addrmap. So, it seems better to just ignore such
3448 indices. */
3449 if (version < 4)
3450 {
3451 static int warning_printed = 0;
3452 if (!warning_printed)
3453 {
3454 warning (_("Skipping obsolete .gdb_index section in %s."),
3455 filename);
3456 warning_printed = 1;
3457 }
3458 return 0;
3459 }
3460 /* Index version 4 uses a different hash function than index version
3461 5 and later.
3462
3463 Versions earlier than 6 did not emit psymbols for inlined
3464 functions. Using these files will cause GDB not to be able to
3465 set breakpoints on inlined functions by name, so we ignore these
3466 indices unless the user has done
3467 "set use-deprecated-index-sections on". */
3468 if (version < 6 && !deprecated_ok)
3469 {
3470 static int warning_printed = 0;
3471 if (!warning_printed)
3472 {
3473 warning (_("\
3474 Skipping deprecated .gdb_index section in %s.\n\
3475 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3476 to use the section anyway."),
3477 filename);
3478 warning_printed = 1;
3479 }
3480 return 0;
3481 }
3482 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3483 of the TU (for symbols coming from TUs),
3484 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3485 Plus gold-generated indices can have duplicate entries for global symbols,
3486 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3487 These are just performance bugs, and we can't distinguish gdb-generated
3488 indices from gold-generated ones, so issue no warning here. */
3489
3490 /* Indexes with higher version than the one supported by GDB may be no
3491 longer backward compatible. */
3492 if (version > 8)
3493 return 0;
3494
3495 map->version = version;
3496
3497 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3498
3499 int i = 0;
3500 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3501 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3502 / 8);
3503 ++i;
3504
3505 *types_list = addr + MAYBE_SWAP (metadata[i]);
3506 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3507 - MAYBE_SWAP (metadata[i]))
3508 / 8);
3509 ++i;
3510
3511 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3512 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3513 map->address_table
3514 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3515 ++i;
3516
3517 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3518 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3519 map->symbol_table
3520 = gdb::array_view<mapped_index::symbol_table_slot>
3521 ((mapped_index::symbol_table_slot *) symbol_table,
3522 (mapped_index::symbol_table_slot *) symbol_table_end);
3523
3524 ++i;
3525 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3526
3527 return 1;
3528 }
3529
3530 /* Callback types for dwarf2_read_gdb_index. */
3531
3532 typedef gdb::function_view
3533 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3534 get_gdb_index_contents_ftype;
3535 typedef gdb::function_view
3536 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3537 get_gdb_index_contents_dwz_ftype;
3538
3539 /* Read .gdb_index. If everything went ok, initialize the "quick"
3540 elements of all the CUs and return 1. Otherwise, return 0. */
3541
3542 static int
3543 dwarf2_read_gdb_index
3544 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3545 get_gdb_index_contents_ftype get_gdb_index_contents,
3546 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3547 {
3548 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3549 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3550 struct dwz_file *dwz;
3551 struct objfile *objfile = dwarf2_per_objfile->objfile;
3552
3553 gdb::array_view<const gdb_byte> main_index_contents
3554 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3555
3556 if (main_index_contents.empty ())
3557 return 0;
3558
3559 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3560 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3561 use_deprecated_index_sections,
3562 main_index_contents, map.get (), &cu_list,
3563 &cu_list_elements, &types_list,
3564 &types_list_elements))
3565 return 0;
3566
3567 /* Don't use the index if it's empty. */
3568 if (map->symbol_table.empty ())
3569 return 0;
3570
3571 /* If there is a .dwz file, read it so we can get its CU list as
3572 well. */
3573 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3574 if (dwz != NULL)
3575 {
3576 struct mapped_index dwz_map;
3577 const gdb_byte *dwz_types_ignore;
3578 offset_type dwz_types_elements_ignore;
3579
3580 gdb::array_view<const gdb_byte> dwz_index_content
3581 = get_gdb_index_contents_dwz (objfile, dwz);
3582
3583 if (dwz_index_content.empty ())
3584 return 0;
3585
3586 if (!read_gdb_index_from_buffer (objfile,
3587 bfd_get_filename (dwz->dwz_bfd.get ()),
3588 1, dwz_index_content, &dwz_map,
3589 &dwz_list, &dwz_list_elements,
3590 &dwz_types_ignore,
3591 &dwz_types_elements_ignore))
3592 {
3593 warning (_("could not read '.gdb_index' section from %s; skipping"),
3594 bfd_get_filename (dwz->dwz_bfd.get ()));
3595 return 0;
3596 }
3597 }
3598
3599 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3600 dwz_list, dwz_list_elements);
3601
3602 if (types_list_elements)
3603 {
3604 /* We can only handle a single .debug_types when we have an
3605 index. */
3606 if (dwarf2_per_objfile->types.size () != 1)
3607 return 0;
3608
3609 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3610
3611 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3612 types_list, types_list_elements);
3613 }
3614
3615 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3616
3617 dwarf2_per_objfile->index_table = std::move (map);
3618 dwarf2_per_objfile->using_index = 1;
3619 dwarf2_per_objfile->quick_file_names_table =
3620 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3621
3622 return 1;
3623 }
3624
3625 /* die_reader_func for dw2_get_file_names. */
3626
3627 static void
3628 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3629 const gdb_byte *info_ptr,
3630 struct die_info *comp_unit_die,
3631 int has_children,
3632 void *data)
3633 {
3634 struct dwarf2_cu *cu = reader->cu;
3635 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3636 struct dwarf2_per_objfile *dwarf2_per_objfile
3637 = cu->per_cu->dwarf2_per_objfile;
3638 struct objfile *objfile = dwarf2_per_objfile->objfile;
3639 struct dwarf2_per_cu_data *lh_cu;
3640 struct attribute *attr;
3641 int i;
3642 void **slot;
3643 struct quick_file_names *qfn;
3644
3645 gdb_assert (! this_cu->is_debug_types);
3646
3647 /* Our callers never want to match partial units -- instead they
3648 will match the enclosing full CU. */
3649 if (comp_unit_die->tag == DW_TAG_partial_unit)
3650 {
3651 this_cu->v.quick->no_file_data = 1;
3652 return;
3653 }
3654
3655 lh_cu = this_cu;
3656 slot = NULL;
3657
3658 line_header_up lh;
3659 sect_offset line_offset {};
3660
3661 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3662 if (attr)
3663 {
3664 struct quick_file_names find_entry;
3665
3666 line_offset = (sect_offset) DW_UNSND (attr);
3667
3668 /* We may have already read in this line header (TU line header sharing).
3669 If we have we're done. */
3670 find_entry.hash.dwo_unit = cu->dwo_unit;
3671 find_entry.hash.line_sect_off = line_offset;
3672 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3673 &find_entry, INSERT);
3674 if (*slot != NULL)
3675 {
3676 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3677 return;
3678 }
3679
3680 lh = dwarf_decode_line_header (line_offset, cu);
3681 }
3682 if (lh == NULL)
3683 {
3684 lh_cu->v.quick->no_file_data = 1;
3685 return;
3686 }
3687
3688 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3689 qfn->hash.dwo_unit = cu->dwo_unit;
3690 qfn->hash.line_sect_off = line_offset;
3691 gdb_assert (slot != NULL);
3692 *slot = qfn;
3693
3694 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3695
3696 int offset = 0;
3697 if (strcmp (fnd.name, "<unknown>") != 0)
3698 ++offset;
3699
3700 qfn->num_file_names = offset + lh->file_names.size ();
3701 qfn->file_names =
3702 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3703 if (offset != 0)
3704 qfn->file_names[0] = xstrdup (fnd.name);
3705 for (i = 0; i < lh->file_names.size (); ++i)
3706 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3707 qfn->real_names = NULL;
3708
3709 lh_cu->v.quick->file_names = qfn;
3710 }
3711
3712 /* A helper for the "quick" functions which attempts to read the line
3713 table for THIS_CU. */
3714
3715 static struct quick_file_names *
3716 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3717 {
3718 /* This should never be called for TUs. */
3719 gdb_assert (! this_cu->is_debug_types);
3720 /* Nor type unit groups. */
3721 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3722
3723 if (this_cu->v.quick->file_names != NULL)
3724 return this_cu->v.quick->file_names;
3725 /* If we know there is no line data, no point in looking again. */
3726 if (this_cu->v.quick->no_file_data)
3727 return NULL;
3728
3729 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3730
3731 if (this_cu->v.quick->no_file_data)
3732 return NULL;
3733 return this_cu->v.quick->file_names;
3734 }
3735
3736 /* A helper for the "quick" functions which computes and caches the
3737 real path for a given file name from the line table. */
3738
3739 static const char *
3740 dw2_get_real_path (struct objfile *objfile,
3741 struct quick_file_names *qfn, int index)
3742 {
3743 if (qfn->real_names == NULL)
3744 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3745 qfn->num_file_names, const char *);
3746
3747 if (qfn->real_names[index] == NULL)
3748 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3749
3750 return qfn->real_names[index];
3751 }
3752
3753 static struct symtab *
3754 dw2_find_last_source_symtab (struct objfile *objfile)
3755 {
3756 struct dwarf2_per_objfile *dwarf2_per_objfile
3757 = get_dwarf2_per_objfile (objfile);
3758 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3759 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3760
3761 if (cust == NULL)
3762 return NULL;
3763
3764 return compunit_primary_filetab (cust);
3765 }
3766
3767 /* Traversal function for dw2_forget_cached_source_info. */
3768
3769 static int
3770 dw2_free_cached_file_names (void **slot, void *info)
3771 {
3772 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3773
3774 if (file_data->real_names)
3775 {
3776 int i;
3777
3778 for (i = 0; i < file_data->num_file_names; ++i)
3779 {
3780 xfree ((void*) file_data->real_names[i]);
3781 file_data->real_names[i] = NULL;
3782 }
3783 }
3784
3785 return 1;
3786 }
3787
3788 static void
3789 dw2_forget_cached_source_info (struct objfile *objfile)
3790 {
3791 struct dwarf2_per_objfile *dwarf2_per_objfile
3792 = get_dwarf2_per_objfile (objfile);
3793
3794 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3795 dw2_free_cached_file_names, NULL);
3796 }
3797
3798 /* Helper function for dw2_map_symtabs_matching_filename that expands
3799 the symtabs and calls the iterator. */
3800
3801 static int
3802 dw2_map_expand_apply (struct objfile *objfile,
3803 struct dwarf2_per_cu_data *per_cu,
3804 const char *name, const char *real_path,
3805 gdb::function_view<bool (symtab *)> callback)
3806 {
3807 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3808
3809 /* Don't visit already-expanded CUs. */
3810 if (per_cu->v.quick->compunit_symtab)
3811 return 0;
3812
3813 /* This may expand more than one symtab, and we want to iterate over
3814 all of them. */
3815 dw2_instantiate_symtab (per_cu, false);
3816
3817 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3818 last_made, callback);
3819 }
3820
3821 /* Implementation of the map_symtabs_matching_filename method. */
3822
3823 static bool
3824 dw2_map_symtabs_matching_filename
3825 (struct objfile *objfile, const char *name, const char *real_path,
3826 gdb::function_view<bool (symtab *)> callback)
3827 {
3828 const char *name_basename = lbasename (name);
3829 struct dwarf2_per_objfile *dwarf2_per_objfile
3830 = get_dwarf2_per_objfile (objfile);
3831
3832 /* The rule is CUs specify all the files, including those used by
3833 any TU, so there's no need to scan TUs here. */
3834
3835 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3836 {
3837 /* We only need to look at symtabs not already expanded. */
3838 if (per_cu->v.quick->compunit_symtab)
3839 continue;
3840
3841 quick_file_names *file_data = dw2_get_file_names (per_cu);
3842 if (file_data == NULL)
3843 continue;
3844
3845 for (int j = 0; j < file_data->num_file_names; ++j)
3846 {
3847 const char *this_name = file_data->file_names[j];
3848 const char *this_real_name;
3849
3850 if (compare_filenames_for_search (this_name, name))
3851 {
3852 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3853 callback))
3854 return true;
3855 continue;
3856 }
3857
3858 /* Before we invoke realpath, which can get expensive when many
3859 files are involved, do a quick comparison of the basenames. */
3860 if (! basenames_may_differ
3861 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3862 continue;
3863
3864 this_real_name = dw2_get_real_path (objfile, file_data, j);
3865 if (compare_filenames_for_search (this_real_name, name))
3866 {
3867 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3868 callback))
3869 return true;
3870 continue;
3871 }
3872
3873 if (real_path != NULL)
3874 {
3875 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3876 gdb_assert (IS_ABSOLUTE_PATH (name));
3877 if (this_real_name != NULL
3878 && FILENAME_CMP (real_path, this_real_name) == 0)
3879 {
3880 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3881 callback))
3882 return true;
3883 continue;
3884 }
3885 }
3886 }
3887 }
3888
3889 return false;
3890 }
3891
3892 /* Struct used to manage iterating over all CUs looking for a symbol. */
3893
3894 struct dw2_symtab_iterator
3895 {
3896 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3897 struct dwarf2_per_objfile *dwarf2_per_objfile;
3898 /* If set, only look for symbols that match that block. Valid values are
3899 GLOBAL_BLOCK and STATIC_BLOCK. */
3900 gdb::optional<block_enum> block_index;
3901 /* The kind of symbol we're looking for. */
3902 domain_enum domain;
3903 /* The list of CUs from the index entry of the symbol,
3904 or NULL if not found. */
3905 offset_type *vec;
3906 /* The next element in VEC to look at. */
3907 int next;
3908 /* The number of elements in VEC, or zero if there is no match. */
3909 int length;
3910 /* Have we seen a global version of the symbol?
3911 If so we can ignore all further global instances.
3912 This is to work around gold/15646, inefficient gold-generated
3913 indices. */
3914 int global_seen;
3915 };
3916
3917 /* Initialize the index symtab iterator ITER. */
3918
3919 static void
3920 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3921 struct dwarf2_per_objfile *dwarf2_per_objfile,
3922 gdb::optional<block_enum> block_index,
3923 domain_enum domain,
3924 const char *name)
3925 {
3926 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3927 iter->block_index = block_index;
3928 iter->domain = domain;
3929 iter->next = 0;
3930 iter->global_seen = 0;
3931
3932 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3933
3934 /* index is NULL if OBJF_READNOW. */
3935 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3936 iter->length = MAYBE_SWAP (*iter->vec);
3937 else
3938 {
3939 iter->vec = NULL;
3940 iter->length = 0;
3941 }
3942 }
3943
3944 /* Return the next matching CU or NULL if there are no more. */
3945
3946 static struct dwarf2_per_cu_data *
3947 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3948 {
3949 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3950
3951 for ( ; iter->next < iter->length; ++iter->next)
3952 {
3953 offset_type cu_index_and_attrs =
3954 MAYBE_SWAP (iter->vec[iter->next + 1]);
3955 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3956 gdb_index_symbol_kind symbol_kind =
3957 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3958 /* Only check the symbol attributes if they're present.
3959 Indices prior to version 7 don't record them,
3960 and indices >= 7 may elide them for certain symbols
3961 (gold does this). */
3962 int attrs_valid =
3963 (dwarf2_per_objfile->index_table->version >= 7
3964 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3965
3966 /* Don't crash on bad data. */
3967 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3968 + dwarf2_per_objfile->all_type_units.size ()))
3969 {
3970 complaint (_(".gdb_index entry has bad CU index"
3971 " [in module %s]"),
3972 objfile_name (dwarf2_per_objfile->objfile));
3973 continue;
3974 }
3975
3976 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3977
3978 /* Skip if already read in. */
3979 if (per_cu->v.quick->compunit_symtab)
3980 continue;
3981
3982 /* Check static vs global. */
3983 if (attrs_valid)
3984 {
3985 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3986
3987 if (iter->block_index.has_value ())
3988 {
3989 bool want_static = *iter->block_index == STATIC_BLOCK;
3990
3991 if (is_static != want_static)
3992 continue;
3993 }
3994
3995 /* Work around gold/15646. */
3996 if (!is_static && iter->global_seen)
3997 continue;
3998 if (!is_static)
3999 iter->global_seen = 1;
4000 }
4001
4002 /* Only check the symbol's kind if it has one. */
4003 if (attrs_valid)
4004 {
4005 switch (iter->domain)
4006 {
4007 case VAR_DOMAIN:
4008 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4009 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4010 /* Some types are also in VAR_DOMAIN. */
4011 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4012 continue;
4013 break;
4014 case STRUCT_DOMAIN:
4015 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4016 continue;
4017 break;
4018 case LABEL_DOMAIN:
4019 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4020 continue;
4021 break;
4022 default:
4023 break;
4024 }
4025 }
4026
4027 ++iter->next;
4028 return per_cu;
4029 }
4030
4031 return NULL;
4032 }
4033
4034 static struct compunit_symtab *
4035 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
4036 const char *name, domain_enum domain)
4037 {
4038 struct compunit_symtab *stab_best = NULL;
4039 struct dwarf2_per_objfile *dwarf2_per_objfile
4040 = get_dwarf2_per_objfile (objfile);
4041
4042 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4043
4044 struct dw2_symtab_iterator iter;
4045 struct dwarf2_per_cu_data *per_cu;
4046
4047 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
4048
4049 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4050 {
4051 struct symbol *sym, *with_opaque = NULL;
4052 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
4053 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4054 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4055
4056 sym = block_find_symbol (block, name, domain,
4057 block_find_non_opaque_type_preferred,
4058 &with_opaque);
4059
4060 /* Some caution must be observed with overloaded functions
4061 and methods, since the index will not contain any overload
4062 information (but NAME might contain it). */
4063
4064 if (sym != NULL
4065 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4066 return stab;
4067 if (with_opaque != NULL
4068 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4069 stab_best = stab;
4070
4071 /* Keep looking through other CUs. */
4072 }
4073
4074 return stab_best;
4075 }
4076
4077 static void
4078 dw2_print_stats (struct objfile *objfile)
4079 {
4080 struct dwarf2_per_objfile *dwarf2_per_objfile
4081 = get_dwarf2_per_objfile (objfile);
4082 int total = (dwarf2_per_objfile->all_comp_units.size ()
4083 + dwarf2_per_objfile->all_type_units.size ());
4084 int count = 0;
4085
4086 for (int i = 0; i < total; ++i)
4087 {
4088 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4089
4090 if (!per_cu->v.quick->compunit_symtab)
4091 ++count;
4092 }
4093 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4094 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4095 }
4096
4097 /* This dumps minimal information about the index.
4098 It is called via "mt print objfiles".
4099 One use is to verify .gdb_index has been loaded by the
4100 gdb.dwarf2/gdb-index.exp testcase. */
4101
4102 static void
4103 dw2_dump (struct objfile *objfile)
4104 {
4105 struct dwarf2_per_objfile *dwarf2_per_objfile
4106 = get_dwarf2_per_objfile (objfile);
4107
4108 gdb_assert (dwarf2_per_objfile->using_index);
4109 printf_filtered (".gdb_index:");
4110 if (dwarf2_per_objfile->index_table != NULL)
4111 {
4112 printf_filtered (" version %d\n",
4113 dwarf2_per_objfile->index_table->version);
4114 }
4115 else
4116 printf_filtered (" faked for \"readnow\"\n");
4117 printf_filtered ("\n");
4118 }
4119
4120 static void
4121 dw2_expand_symtabs_for_function (struct objfile *objfile,
4122 const char *func_name)
4123 {
4124 struct dwarf2_per_objfile *dwarf2_per_objfile
4125 = get_dwarf2_per_objfile (objfile);
4126
4127 struct dw2_symtab_iterator iter;
4128 struct dwarf2_per_cu_data *per_cu;
4129
4130 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
4131
4132 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4133 dw2_instantiate_symtab (per_cu, false);
4134
4135 }
4136
4137 static void
4138 dw2_expand_all_symtabs (struct objfile *objfile)
4139 {
4140 struct dwarf2_per_objfile *dwarf2_per_objfile
4141 = get_dwarf2_per_objfile (objfile);
4142 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4143 + dwarf2_per_objfile->all_type_units.size ());
4144
4145 for (int i = 0; i < total_units; ++i)
4146 {
4147 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4148
4149 /* We don't want to directly expand a partial CU, because if we
4150 read it with the wrong language, then assertion failures can
4151 be triggered later on. See PR symtab/23010. So, tell
4152 dw2_instantiate_symtab to skip partial CUs -- any important
4153 partial CU will be read via DW_TAG_imported_unit anyway. */
4154 dw2_instantiate_symtab (per_cu, true);
4155 }
4156 }
4157
4158 static void
4159 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4160 const char *fullname)
4161 {
4162 struct dwarf2_per_objfile *dwarf2_per_objfile
4163 = get_dwarf2_per_objfile (objfile);
4164
4165 /* We don't need to consider type units here.
4166 This is only called for examining code, e.g. expand_line_sal.
4167 There can be an order of magnitude (or more) more type units
4168 than comp units, and we avoid them if we can. */
4169
4170 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4171 {
4172 /* We only need to look at symtabs not already expanded. */
4173 if (per_cu->v.quick->compunit_symtab)
4174 continue;
4175
4176 quick_file_names *file_data = dw2_get_file_names (per_cu);
4177 if (file_data == NULL)
4178 continue;
4179
4180 for (int j = 0; j < file_data->num_file_names; ++j)
4181 {
4182 const char *this_fullname = file_data->file_names[j];
4183
4184 if (filename_cmp (this_fullname, fullname) == 0)
4185 {
4186 dw2_instantiate_symtab (per_cu, false);
4187 break;
4188 }
4189 }
4190 }
4191 }
4192
4193 static void
4194 dw2_map_matching_symbols
4195 (struct objfile *objfile,
4196 const lookup_name_info &name, domain_enum domain,
4197 int global,
4198 gdb::function_view<symbol_found_callback_ftype> callback,
4199 symbol_compare_ftype *ordered_compare)
4200 {
4201 /* Currently unimplemented; used for Ada. The function can be called if the
4202 current language is Ada for a non-Ada objfile using GNU index. As Ada
4203 does not look for non-Ada symbols this function should just return. */
4204 }
4205
4206 /* Starting from a search name, return the string that finds the upper
4207 bound of all strings that start with SEARCH_NAME in a sorted name
4208 list. Returns the empty string to indicate that the upper bound is
4209 the end of the list. */
4210
4211 static std::string
4212 make_sort_after_prefix_name (const char *search_name)
4213 {
4214 /* When looking to complete "func", we find the upper bound of all
4215 symbols that start with "func" by looking for where we'd insert
4216 the closest string that would follow "func" in lexicographical
4217 order. Usually, that's "func"-with-last-character-incremented,
4218 i.e. "fund". Mind non-ASCII characters, though. Usually those
4219 will be UTF-8 multi-byte sequences, but we can't be certain.
4220 Especially mind the 0xff character, which is a valid character in
4221 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4222 rule out compilers allowing it in identifiers. Note that
4223 conveniently, strcmp/strcasecmp are specified to compare
4224 characters interpreted as unsigned char. So what we do is treat
4225 the whole string as a base 256 number composed of a sequence of
4226 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4227 to 0, and carries 1 to the following more-significant position.
4228 If the very first character in SEARCH_NAME ends up incremented
4229 and carries/overflows, then the upper bound is the end of the
4230 list. The string after the empty string is also the empty
4231 string.
4232
4233 Some examples of this operation:
4234
4235 SEARCH_NAME => "+1" RESULT
4236
4237 "abc" => "abd"
4238 "ab\xff" => "ac"
4239 "\xff" "a" "\xff" => "\xff" "b"
4240 "\xff" => ""
4241 "\xff\xff" => ""
4242 "" => ""
4243
4244 Then, with these symbols for example:
4245
4246 func
4247 func1
4248 fund
4249
4250 completing "func" looks for symbols between "func" and
4251 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4252 which finds "func" and "func1", but not "fund".
4253
4254 And with:
4255
4256 funcÿ (Latin1 'ÿ' [0xff])
4257 funcÿ1
4258 fund
4259
4260 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4261 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4262
4263 And with:
4264
4265 ÿÿ (Latin1 'ÿ' [0xff])
4266 ÿÿ1
4267
4268 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4269 the end of the list.
4270 */
4271 std::string after = search_name;
4272 while (!after.empty () && (unsigned char) after.back () == 0xff)
4273 after.pop_back ();
4274 if (!after.empty ())
4275 after.back () = (unsigned char) after.back () + 1;
4276 return after;
4277 }
4278
4279 /* See declaration. */
4280
4281 std::pair<std::vector<name_component>::const_iterator,
4282 std::vector<name_component>::const_iterator>
4283 mapped_index_base::find_name_components_bounds
4284 (const lookup_name_info &lookup_name_without_params, language lang) const
4285 {
4286 auto *name_cmp
4287 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4288
4289 const char *lang_name
4290 = lookup_name_without_params.language_lookup_name (lang).c_str ();
4291
4292 /* Comparison function object for lower_bound that matches against a
4293 given symbol name. */
4294 auto lookup_compare_lower = [&] (const name_component &elem,
4295 const char *name)
4296 {
4297 const char *elem_qualified = this->symbol_name_at (elem.idx);
4298 const char *elem_name = elem_qualified + elem.name_offset;
4299 return name_cmp (elem_name, name) < 0;
4300 };
4301
4302 /* Comparison function object for upper_bound that matches against a
4303 given symbol name. */
4304 auto lookup_compare_upper = [&] (const char *name,
4305 const name_component &elem)
4306 {
4307 const char *elem_qualified = this->symbol_name_at (elem.idx);
4308 const char *elem_name = elem_qualified + elem.name_offset;
4309 return name_cmp (name, elem_name) < 0;
4310 };
4311
4312 auto begin = this->name_components.begin ();
4313 auto end = this->name_components.end ();
4314
4315 /* Find the lower bound. */
4316 auto lower = [&] ()
4317 {
4318 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4319 return begin;
4320 else
4321 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4322 } ();
4323
4324 /* Find the upper bound. */
4325 auto upper = [&] ()
4326 {
4327 if (lookup_name_without_params.completion_mode ())
4328 {
4329 /* In completion mode, we want UPPER to point past all
4330 symbols names that have the same prefix. I.e., with
4331 these symbols, and completing "func":
4332
4333 function << lower bound
4334 function1
4335 other_function << upper bound
4336
4337 We find the upper bound by looking for the insertion
4338 point of "func"-with-last-character-incremented,
4339 i.e. "fund". */
4340 std::string after = make_sort_after_prefix_name (lang_name);
4341 if (after.empty ())
4342 return end;
4343 return std::lower_bound (lower, end, after.c_str (),
4344 lookup_compare_lower);
4345 }
4346 else
4347 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4348 } ();
4349
4350 return {lower, upper};
4351 }
4352
4353 /* See declaration. */
4354
4355 void
4356 mapped_index_base::build_name_components ()
4357 {
4358 if (!this->name_components.empty ())
4359 return;
4360
4361 this->name_components_casing = case_sensitivity;
4362 auto *name_cmp
4363 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4364
4365 /* The code below only knows how to break apart components of C++
4366 symbol names (and other languages that use '::' as
4367 namespace/module separator) and Ada symbol names. */
4368 auto count = this->symbol_name_count ();
4369 for (offset_type idx = 0; idx < count; idx++)
4370 {
4371 if (this->symbol_name_slot_invalid (idx))
4372 continue;
4373
4374 const char *name = this->symbol_name_at (idx);
4375
4376 /* Add each name component to the name component table. */
4377 unsigned int previous_len = 0;
4378
4379 if (strstr (name, "::") != nullptr)
4380 {
4381 for (unsigned int current_len = cp_find_first_component (name);
4382 name[current_len] != '\0';
4383 current_len += cp_find_first_component (name + current_len))
4384 {
4385 gdb_assert (name[current_len] == ':');
4386 this->name_components.push_back ({previous_len, idx});
4387 /* Skip the '::'. */
4388 current_len += 2;
4389 previous_len = current_len;
4390 }
4391 }
4392 else
4393 {
4394 /* Handle the Ada encoded (aka mangled) form here. */
4395 for (const char *iter = strstr (name, "__");
4396 iter != nullptr;
4397 iter = strstr (iter, "__"))
4398 {
4399 this->name_components.push_back ({previous_len, idx});
4400 iter += 2;
4401 previous_len = iter - name;
4402 }
4403 }
4404
4405 this->name_components.push_back ({previous_len, idx});
4406 }
4407
4408 /* Sort name_components elements by name. */
4409 auto name_comp_compare = [&] (const name_component &left,
4410 const name_component &right)
4411 {
4412 const char *left_qualified = this->symbol_name_at (left.idx);
4413 const char *right_qualified = this->symbol_name_at (right.idx);
4414
4415 const char *left_name = left_qualified + left.name_offset;
4416 const char *right_name = right_qualified + right.name_offset;
4417
4418 return name_cmp (left_name, right_name) < 0;
4419 };
4420
4421 std::sort (this->name_components.begin (),
4422 this->name_components.end (),
4423 name_comp_compare);
4424 }
4425
4426 /* Helper for dw2_expand_symtabs_matching that works with a
4427 mapped_index_base instead of the containing objfile. This is split
4428 to a separate function in order to be able to unit test the
4429 name_components matching using a mock mapped_index_base. For each
4430 symbol name that matches, calls MATCH_CALLBACK, passing it the
4431 symbol's index in the mapped_index_base symbol table. */
4432
4433 static void
4434 dw2_expand_symtabs_matching_symbol
4435 (mapped_index_base &index,
4436 const lookup_name_info &lookup_name_in,
4437 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4438 enum search_domain kind,
4439 gdb::function_view<bool (offset_type)> match_callback)
4440 {
4441 lookup_name_info lookup_name_without_params
4442 = lookup_name_in.make_ignore_params ();
4443
4444 /* Build the symbol name component sorted vector, if we haven't
4445 yet. */
4446 index.build_name_components ();
4447
4448 /* The same symbol may appear more than once in the range though.
4449 E.g., if we're looking for symbols that complete "w", and we have
4450 a symbol named "w1::w2", we'll find the two name components for
4451 that same symbol in the range. To be sure we only call the
4452 callback once per symbol, we first collect the symbol name
4453 indexes that matched in a temporary vector and ignore
4454 duplicates. */
4455 std::vector<offset_type> matches;
4456
4457 struct name_and_matcher
4458 {
4459 symbol_name_matcher_ftype *matcher;
4460 const std::string &name;
4461
4462 bool operator== (const name_and_matcher &other) const
4463 {
4464 return matcher == other.matcher && name == other.name;
4465 }
4466 };
4467
4468 /* A vector holding all the different symbol name matchers, for all
4469 languages. */
4470 std::vector<name_and_matcher> matchers;
4471
4472 for (int i = 0; i < nr_languages; i++)
4473 {
4474 enum language lang_e = (enum language) i;
4475
4476 const language_defn *lang = language_def (lang_e);
4477 symbol_name_matcher_ftype *name_matcher
4478 = get_symbol_name_matcher (lang, lookup_name_without_params);
4479
4480 name_and_matcher key {
4481 name_matcher,
4482 lookup_name_without_params.language_lookup_name (lang_e)
4483 };
4484
4485 /* Don't insert the same comparison routine more than once.
4486 Note that we do this linear walk. This is not a problem in
4487 practice because the number of supported languages is
4488 low. */
4489 if (std::find (matchers.begin (), matchers.end (), key)
4490 != matchers.end ())
4491 continue;
4492 matchers.push_back (std::move (key));
4493
4494 auto bounds
4495 = index.find_name_components_bounds (lookup_name_without_params,
4496 lang_e);
4497
4498 /* Now for each symbol name in range, check to see if we have a name
4499 match, and if so, call the MATCH_CALLBACK callback. */
4500
4501 for (; bounds.first != bounds.second; ++bounds.first)
4502 {
4503 const char *qualified = index.symbol_name_at (bounds.first->idx);
4504
4505 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4506 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4507 continue;
4508
4509 matches.push_back (bounds.first->idx);
4510 }
4511 }
4512
4513 std::sort (matches.begin (), matches.end ());
4514
4515 /* Finally call the callback, once per match. */
4516 ULONGEST prev = -1;
4517 for (offset_type idx : matches)
4518 {
4519 if (prev != idx)
4520 {
4521 if (!match_callback (idx))
4522 break;
4523 prev = idx;
4524 }
4525 }
4526
4527 /* Above we use a type wider than idx's for 'prev', since 0 and
4528 (offset_type)-1 are both possible values. */
4529 static_assert (sizeof (prev) > sizeof (offset_type), "");
4530 }
4531
4532 #if GDB_SELF_TEST
4533
4534 namespace selftests { namespace dw2_expand_symtabs_matching {
4535
4536 /* A mock .gdb_index/.debug_names-like name index table, enough to
4537 exercise dw2_expand_symtabs_matching_symbol, which works with the
4538 mapped_index_base interface. Builds an index from the symbol list
4539 passed as parameter to the constructor. */
4540 class mock_mapped_index : public mapped_index_base
4541 {
4542 public:
4543 mock_mapped_index (gdb::array_view<const char *> symbols)
4544 : m_symbol_table (symbols)
4545 {}
4546
4547 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4548
4549 /* Return the number of names in the symbol table. */
4550 size_t symbol_name_count () const override
4551 {
4552 return m_symbol_table.size ();
4553 }
4554
4555 /* Get the name of the symbol at IDX in the symbol table. */
4556 const char *symbol_name_at (offset_type idx) const override
4557 {
4558 return m_symbol_table[idx];
4559 }
4560
4561 private:
4562 gdb::array_view<const char *> m_symbol_table;
4563 };
4564
4565 /* Convenience function that converts a NULL pointer to a "<null>"
4566 string, to pass to print routines. */
4567
4568 static const char *
4569 string_or_null (const char *str)
4570 {
4571 return str != NULL ? str : "<null>";
4572 }
4573
4574 /* Check if a lookup_name_info built from
4575 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4576 index. EXPECTED_LIST is the list of expected matches, in expected
4577 matching order. If no match expected, then an empty list is
4578 specified. Returns true on success. On failure prints a warning
4579 indicating the file:line that failed, and returns false. */
4580
4581 static bool
4582 check_match (const char *file, int line,
4583 mock_mapped_index &mock_index,
4584 const char *name, symbol_name_match_type match_type,
4585 bool completion_mode,
4586 std::initializer_list<const char *> expected_list)
4587 {
4588 lookup_name_info lookup_name (name, match_type, completion_mode);
4589
4590 bool matched = true;
4591
4592 auto mismatch = [&] (const char *expected_str,
4593 const char *got)
4594 {
4595 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4596 "expected=\"%s\", got=\"%s\"\n"),
4597 file, line,
4598 (match_type == symbol_name_match_type::FULL
4599 ? "FULL" : "WILD"),
4600 name, string_or_null (expected_str), string_or_null (got));
4601 matched = false;
4602 };
4603
4604 auto expected_it = expected_list.begin ();
4605 auto expected_end = expected_list.end ();
4606
4607 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4608 NULL, ALL_DOMAIN,
4609 [&] (offset_type idx)
4610 {
4611 const char *matched_name = mock_index.symbol_name_at (idx);
4612 const char *expected_str
4613 = expected_it == expected_end ? NULL : *expected_it++;
4614
4615 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4616 mismatch (expected_str, matched_name);
4617 return true;
4618 });
4619
4620 const char *expected_str
4621 = expected_it == expected_end ? NULL : *expected_it++;
4622 if (expected_str != NULL)
4623 mismatch (expected_str, NULL);
4624
4625 return matched;
4626 }
4627
4628 /* The symbols added to the mock mapped_index for testing (in
4629 canonical form). */
4630 static const char *test_symbols[] = {
4631 "function",
4632 "std::bar",
4633 "std::zfunction",
4634 "std::zfunction2",
4635 "w1::w2",
4636 "ns::foo<char*>",
4637 "ns::foo<int>",
4638 "ns::foo<long>",
4639 "ns2::tmpl<int>::foo2",
4640 "(anonymous namespace)::A::B::C",
4641
4642 /* These are used to check that the increment-last-char in the
4643 matching algorithm for completion doesn't match "t1_fund" when
4644 completing "t1_func". */
4645 "t1_func",
4646 "t1_func1",
4647 "t1_fund",
4648 "t1_fund1",
4649
4650 /* A UTF-8 name with multi-byte sequences to make sure that
4651 cp-name-parser understands this as a single identifier ("função"
4652 is "function" in PT). */
4653 u8"u8função",
4654
4655 /* \377 (0xff) is Latin1 'ÿ'. */
4656 "yfunc\377",
4657
4658 /* \377 (0xff) is Latin1 'ÿ'. */
4659 "\377",
4660 "\377\377123",
4661
4662 /* A name with all sorts of complications. Starts with "z" to make
4663 it easier for the completion tests below. */
4664 #define Z_SYM_NAME \
4665 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4666 "::tuple<(anonymous namespace)::ui*, " \
4667 "std::default_delete<(anonymous namespace)::ui>, void>"
4668
4669 Z_SYM_NAME
4670 };
4671
4672 /* Returns true if the mapped_index_base::find_name_component_bounds
4673 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4674 in completion mode. */
4675
4676 static bool
4677 check_find_bounds_finds (mapped_index_base &index,
4678 const char *search_name,
4679 gdb::array_view<const char *> expected_syms)
4680 {
4681 lookup_name_info lookup_name (search_name,
4682 symbol_name_match_type::FULL, true);
4683
4684 auto bounds = index.find_name_components_bounds (lookup_name,
4685 language_cplus);
4686
4687 size_t distance = std::distance (bounds.first, bounds.second);
4688 if (distance != expected_syms.size ())
4689 return false;
4690
4691 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4692 {
4693 auto nc_elem = bounds.first + exp_elem;
4694 const char *qualified = index.symbol_name_at (nc_elem->idx);
4695 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4696 return false;
4697 }
4698
4699 return true;
4700 }
4701
4702 /* Test the lower-level mapped_index::find_name_component_bounds
4703 method. */
4704
4705 static void
4706 test_mapped_index_find_name_component_bounds ()
4707 {
4708 mock_mapped_index mock_index (test_symbols);
4709
4710 mock_index.build_name_components ();
4711
4712 /* Test the lower-level mapped_index::find_name_component_bounds
4713 method in completion mode. */
4714 {
4715 static const char *expected_syms[] = {
4716 "t1_func",
4717 "t1_func1",
4718 };
4719
4720 SELF_CHECK (check_find_bounds_finds (mock_index,
4721 "t1_func", expected_syms));
4722 }
4723
4724 /* Check that the increment-last-char in the name matching algorithm
4725 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4726 {
4727 static const char *expected_syms1[] = {
4728 "\377",
4729 "\377\377123",
4730 };
4731 SELF_CHECK (check_find_bounds_finds (mock_index,
4732 "\377", expected_syms1));
4733
4734 static const char *expected_syms2[] = {
4735 "\377\377123",
4736 };
4737 SELF_CHECK (check_find_bounds_finds (mock_index,
4738 "\377\377", expected_syms2));
4739 }
4740 }
4741
4742 /* Test dw2_expand_symtabs_matching_symbol. */
4743
4744 static void
4745 test_dw2_expand_symtabs_matching_symbol ()
4746 {
4747 mock_mapped_index mock_index (test_symbols);
4748
4749 /* We let all tests run until the end even if some fails, for debug
4750 convenience. */
4751 bool any_mismatch = false;
4752
4753 /* Create the expected symbols list (an initializer_list). Needed
4754 because lists have commas, and we need to pass them to CHECK,
4755 which is a macro. */
4756 #define EXPECT(...) { __VA_ARGS__ }
4757
4758 /* Wrapper for check_match that passes down the current
4759 __FILE__/__LINE__. */
4760 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4761 any_mismatch |= !check_match (__FILE__, __LINE__, \
4762 mock_index, \
4763 NAME, MATCH_TYPE, COMPLETION_MODE, \
4764 EXPECTED_LIST)
4765
4766 /* Identity checks. */
4767 for (const char *sym : test_symbols)
4768 {
4769 /* Should be able to match all existing symbols. */
4770 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4771 EXPECT (sym));
4772
4773 /* Should be able to match all existing symbols with
4774 parameters. */
4775 std::string with_params = std::string (sym) + "(int)";
4776 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4777 EXPECT (sym));
4778
4779 /* Should be able to match all existing symbols with
4780 parameters and qualifiers. */
4781 with_params = std::string (sym) + " ( int ) const";
4782 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4783 EXPECT (sym));
4784
4785 /* This should really find sym, but cp-name-parser.y doesn't
4786 know about lvalue/rvalue qualifiers yet. */
4787 with_params = std::string (sym) + " ( int ) &&";
4788 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4789 {});
4790 }
4791
4792 /* Check that the name matching algorithm for completion doesn't get
4793 confused with Latin1 'ÿ' / 0xff. */
4794 {
4795 static const char str[] = "\377";
4796 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4797 EXPECT ("\377", "\377\377123"));
4798 }
4799
4800 /* Check that the increment-last-char in the matching algorithm for
4801 completion doesn't match "t1_fund" when completing "t1_func". */
4802 {
4803 static const char str[] = "t1_func";
4804 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4805 EXPECT ("t1_func", "t1_func1"));
4806 }
4807
4808 /* Check that completion mode works at each prefix of the expected
4809 symbol name. */
4810 {
4811 static const char str[] = "function(int)";
4812 size_t len = strlen (str);
4813 std::string lookup;
4814
4815 for (size_t i = 1; i < len; i++)
4816 {
4817 lookup.assign (str, i);
4818 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4819 EXPECT ("function"));
4820 }
4821 }
4822
4823 /* While "w" is a prefix of both components, the match function
4824 should still only be called once. */
4825 {
4826 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4827 EXPECT ("w1::w2"));
4828 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4829 EXPECT ("w1::w2"));
4830 }
4831
4832 /* Same, with a "complicated" symbol. */
4833 {
4834 static const char str[] = Z_SYM_NAME;
4835 size_t len = strlen (str);
4836 std::string lookup;
4837
4838 for (size_t i = 1; i < len; i++)
4839 {
4840 lookup.assign (str, i);
4841 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4842 EXPECT (Z_SYM_NAME));
4843 }
4844 }
4845
4846 /* In FULL mode, an incomplete symbol doesn't match. */
4847 {
4848 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4849 {});
4850 }
4851
4852 /* A complete symbol with parameters matches any overload, since the
4853 index has no overload info. */
4854 {
4855 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4856 EXPECT ("std::zfunction", "std::zfunction2"));
4857 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4858 EXPECT ("std::zfunction", "std::zfunction2"));
4859 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4860 EXPECT ("std::zfunction", "std::zfunction2"));
4861 }
4862
4863 /* Check that whitespace is ignored appropriately. A symbol with a
4864 template argument list. */
4865 {
4866 static const char expected[] = "ns::foo<int>";
4867 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4868 EXPECT (expected));
4869 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4870 EXPECT (expected));
4871 }
4872
4873 /* Check that whitespace is ignored appropriately. A symbol with a
4874 template argument list that includes a pointer. */
4875 {
4876 static const char expected[] = "ns::foo<char*>";
4877 /* Try both completion and non-completion modes. */
4878 static const bool completion_mode[2] = {false, true};
4879 for (size_t i = 0; i < 2; i++)
4880 {
4881 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4882 completion_mode[i], EXPECT (expected));
4883 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4884 completion_mode[i], EXPECT (expected));
4885
4886 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4887 completion_mode[i], EXPECT (expected));
4888 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4889 completion_mode[i], EXPECT (expected));
4890 }
4891 }
4892
4893 {
4894 /* Check method qualifiers are ignored. */
4895 static const char expected[] = "ns::foo<char*>";
4896 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4897 symbol_name_match_type::FULL, true, EXPECT (expected));
4898 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4899 symbol_name_match_type::FULL, true, EXPECT (expected));
4900 CHECK_MATCH ("foo < char * > ( int ) const",
4901 symbol_name_match_type::WILD, true, EXPECT (expected));
4902 CHECK_MATCH ("foo < char * > ( int ) &&",
4903 symbol_name_match_type::WILD, true, EXPECT (expected));
4904 }
4905
4906 /* Test lookup names that don't match anything. */
4907 {
4908 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4909 {});
4910
4911 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4912 {});
4913 }
4914
4915 /* Some wild matching tests, exercising "(anonymous namespace)",
4916 which should not be confused with a parameter list. */
4917 {
4918 static const char *syms[] = {
4919 "A::B::C",
4920 "B::C",
4921 "C",
4922 "A :: B :: C ( int )",
4923 "B :: C ( int )",
4924 "C ( int )",
4925 };
4926
4927 for (const char *s : syms)
4928 {
4929 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4930 EXPECT ("(anonymous namespace)::A::B::C"));
4931 }
4932 }
4933
4934 {
4935 static const char expected[] = "ns2::tmpl<int>::foo2";
4936 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4937 EXPECT (expected));
4938 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4939 EXPECT (expected));
4940 }
4941
4942 SELF_CHECK (!any_mismatch);
4943
4944 #undef EXPECT
4945 #undef CHECK_MATCH
4946 }
4947
4948 static void
4949 run_test ()
4950 {
4951 test_mapped_index_find_name_component_bounds ();
4952 test_dw2_expand_symtabs_matching_symbol ();
4953 }
4954
4955 }} // namespace selftests::dw2_expand_symtabs_matching
4956
4957 #endif /* GDB_SELF_TEST */
4958
4959 /* If FILE_MATCHER is NULL or if PER_CU has
4960 dwarf2_per_cu_quick_data::MARK set (see
4961 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4962 EXPANSION_NOTIFY on it. */
4963
4964 static void
4965 dw2_expand_symtabs_matching_one
4966 (struct dwarf2_per_cu_data *per_cu,
4967 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4968 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4969 {
4970 if (file_matcher == NULL || per_cu->v.quick->mark)
4971 {
4972 bool symtab_was_null
4973 = (per_cu->v.quick->compunit_symtab == NULL);
4974
4975 dw2_instantiate_symtab (per_cu, false);
4976
4977 if (expansion_notify != NULL
4978 && symtab_was_null
4979 && per_cu->v.quick->compunit_symtab != NULL)
4980 expansion_notify (per_cu->v.quick->compunit_symtab);
4981 }
4982 }
4983
4984 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4985 matched, to expand corresponding CUs that were marked. IDX is the
4986 index of the symbol name that matched. */
4987
4988 static void
4989 dw2_expand_marked_cus
4990 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4991 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4992 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4993 search_domain kind)
4994 {
4995 offset_type *vec, vec_len, vec_idx;
4996 bool global_seen = false;
4997 mapped_index &index = *dwarf2_per_objfile->index_table;
4998
4999 vec = (offset_type *) (index.constant_pool
5000 + MAYBE_SWAP (index.symbol_table[idx].vec));
5001 vec_len = MAYBE_SWAP (vec[0]);
5002 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5003 {
5004 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5005 /* This value is only valid for index versions >= 7. */
5006 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5007 gdb_index_symbol_kind symbol_kind =
5008 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5009 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5010 /* Only check the symbol attributes if they're present.
5011 Indices prior to version 7 don't record them,
5012 and indices >= 7 may elide them for certain symbols
5013 (gold does this). */
5014 int attrs_valid =
5015 (index.version >= 7
5016 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5017
5018 /* Work around gold/15646. */
5019 if (attrs_valid)
5020 {
5021 if (!is_static && global_seen)
5022 continue;
5023 if (!is_static)
5024 global_seen = true;
5025 }
5026
5027 /* Only check the symbol's kind if it has one. */
5028 if (attrs_valid)
5029 {
5030 switch (kind)
5031 {
5032 case VARIABLES_DOMAIN:
5033 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5034 continue;
5035 break;
5036 case FUNCTIONS_DOMAIN:
5037 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5038 continue;
5039 break;
5040 case TYPES_DOMAIN:
5041 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5042 continue;
5043 break;
5044 default:
5045 break;
5046 }
5047 }
5048
5049 /* Don't crash on bad data. */
5050 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5051 + dwarf2_per_objfile->all_type_units.size ()))
5052 {
5053 complaint (_(".gdb_index entry has bad CU index"
5054 " [in module %s]"),
5055 objfile_name (dwarf2_per_objfile->objfile));
5056 continue;
5057 }
5058
5059 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5060 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5061 expansion_notify);
5062 }
5063 }
5064
5065 /* If FILE_MATCHER is non-NULL, set all the
5066 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5067 that match FILE_MATCHER. */
5068
5069 static void
5070 dw_expand_symtabs_matching_file_matcher
5071 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5072 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5073 {
5074 if (file_matcher == NULL)
5075 return;
5076
5077 objfile *const objfile = dwarf2_per_objfile->objfile;
5078
5079 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5080 htab_eq_pointer,
5081 NULL, xcalloc, xfree));
5082 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5083 htab_eq_pointer,
5084 NULL, xcalloc, xfree));
5085
5086 /* The rule is CUs specify all the files, including those used by
5087 any TU, so there's no need to scan TUs here. */
5088
5089 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5090 {
5091 QUIT;
5092
5093 per_cu->v.quick->mark = 0;
5094
5095 /* We only need to look at symtabs not already expanded. */
5096 if (per_cu->v.quick->compunit_symtab)
5097 continue;
5098
5099 quick_file_names *file_data = dw2_get_file_names (per_cu);
5100 if (file_data == NULL)
5101 continue;
5102
5103 if (htab_find (visited_not_found.get (), file_data) != NULL)
5104 continue;
5105 else if (htab_find (visited_found.get (), file_data) != NULL)
5106 {
5107 per_cu->v.quick->mark = 1;
5108 continue;
5109 }
5110
5111 for (int j = 0; j < file_data->num_file_names; ++j)
5112 {
5113 const char *this_real_name;
5114
5115 if (file_matcher (file_data->file_names[j], false))
5116 {
5117 per_cu->v.quick->mark = 1;
5118 break;
5119 }
5120
5121 /* Before we invoke realpath, which can get expensive when many
5122 files are involved, do a quick comparison of the basenames. */
5123 if (!basenames_may_differ
5124 && !file_matcher (lbasename (file_data->file_names[j]),
5125 true))
5126 continue;
5127
5128 this_real_name = dw2_get_real_path (objfile, file_data, j);
5129 if (file_matcher (this_real_name, false))
5130 {
5131 per_cu->v.quick->mark = 1;
5132 break;
5133 }
5134 }
5135
5136 void **slot = htab_find_slot (per_cu->v.quick->mark
5137 ? visited_found.get ()
5138 : visited_not_found.get (),
5139 file_data, INSERT);
5140 *slot = file_data;
5141 }
5142 }
5143
5144 static void
5145 dw2_expand_symtabs_matching
5146 (struct objfile *objfile,
5147 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5148 const lookup_name_info &lookup_name,
5149 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5150 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5151 enum search_domain kind)
5152 {
5153 struct dwarf2_per_objfile *dwarf2_per_objfile
5154 = get_dwarf2_per_objfile (objfile);
5155
5156 /* index_table is NULL if OBJF_READNOW. */
5157 if (!dwarf2_per_objfile->index_table)
5158 return;
5159
5160 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5161
5162 mapped_index &index = *dwarf2_per_objfile->index_table;
5163
5164 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5165 symbol_matcher,
5166 kind, [&] (offset_type idx)
5167 {
5168 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5169 expansion_notify, kind);
5170 return true;
5171 });
5172 }
5173
5174 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5175 symtab. */
5176
5177 static struct compunit_symtab *
5178 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5179 CORE_ADDR pc)
5180 {
5181 int i;
5182
5183 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5184 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5185 return cust;
5186
5187 if (cust->includes == NULL)
5188 return NULL;
5189
5190 for (i = 0; cust->includes[i]; ++i)
5191 {
5192 struct compunit_symtab *s = cust->includes[i];
5193
5194 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5195 if (s != NULL)
5196 return s;
5197 }
5198
5199 return NULL;
5200 }
5201
5202 static struct compunit_symtab *
5203 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5204 struct bound_minimal_symbol msymbol,
5205 CORE_ADDR pc,
5206 struct obj_section *section,
5207 int warn_if_readin)
5208 {
5209 struct dwarf2_per_cu_data *data;
5210 struct compunit_symtab *result;
5211
5212 if (!objfile->partial_symtabs->psymtabs_addrmap)
5213 return NULL;
5214
5215 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5216 SECT_OFF_TEXT (objfile));
5217 data = (struct dwarf2_per_cu_data *) addrmap_find
5218 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
5219 if (!data)
5220 return NULL;
5221
5222 if (warn_if_readin && data->v.quick->compunit_symtab)
5223 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5224 paddress (get_objfile_arch (objfile), pc));
5225
5226 result
5227 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5228 false),
5229 pc);
5230 gdb_assert (result != NULL);
5231 return result;
5232 }
5233
5234 static void
5235 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5236 void *data, int need_fullname)
5237 {
5238 struct dwarf2_per_objfile *dwarf2_per_objfile
5239 = get_dwarf2_per_objfile (objfile);
5240
5241 if (!dwarf2_per_objfile->filenames_cache)
5242 {
5243 dwarf2_per_objfile->filenames_cache.emplace ();
5244
5245 htab_up visited (htab_create_alloc (10,
5246 htab_hash_pointer, htab_eq_pointer,
5247 NULL, xcalloc, xfree));
5248
5249 /* The rule is CUs specify all the files, including those used
5250 by any TU, so there's no need to scan TUs here. We can
5251 ignore file names coming from already-expanded CUs. */
5252
5253 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5254 {
5255 if (per_cu->v.quick->compunit_symtab)
5256 {
5257 void **slot = htab_find_slot (visited.get (),
5258 per_cu->v.quick->file_names,
5259 INSERT);
5260
5261 *slot = per_cu->v.quick->file_names;
5262 }
5263 }
5264
5265 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5266 {
5267 /* We only need to look at symtabs not already expanded. */
5268 if (per_cu->v.quick->compunit_symtab)
5269 continue;
5270
5271 quick_file_names *file_data = dw2_get_file_names (per_cu);
5272 if (file_data == NULL)
5273 continue;
5274
5275 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5276 if (*slot)
5277 {
5278 /* Already visited. */
5279 continue;
5280 }
5281 *slot = file_data;
5282
5283 for (int j = 0; j < file_data->num_file_names; ++j)
5284 {
5285 const char *filename = file_data->file_names[j];
5286 dwarf2_per_objfile->filenames_cache->seen (filename);
5287 }
5288 }
5289 }
5290
5291 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5292 {
5293 gdb::unique_xmalloc_ptr<char> this_real_name;
5294
5295 if (need_fullname)
5296 this_real_name = gdb_realpath (filename);
5297 (*fun) (filename, this_real_name.get (), data);
5298 });
5299 }
5300
5301 static int
5302 dw2_has_symbols (struct objfile *objfile)
5303 {
5304 return 1;
5305 }
5306
5307 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5308 {
5309 dw2_has_symbols,
5310 dw2_find_last_source_symtab,
5311 dw2_forget_cached_source_info,
5312 dw2_map_symtabs_matching_filename,
5313 dw2_lookup_symbol,
5314 dw2_print_stats,
5315 dw2_dump,
5316 dw2_expand_symtabs_for_function,
5317 dw2_expand_all_symtabs,
5318 dw2_expand_symtabs_with_fullname,
5319 dw2_map_matching_symbols,
5320 dw2_expand_symtabs_matching,
5321 dw2_find_pc_sect_compunit_symtab,
5322 NULL,
5323 dw2_map_symbol_filenames
5324 };
5325
5326 /* DWARF-5 debug_names reader. */
5327
5328 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5329 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5330
5331 /* A helper function that reads the .debug_names section in SECTION
5332 and fills in MAP. FILENAME is the name of the file containing the
5333 section; it is used for error reporting.
5334
5335 Returns true if all went well, false otherwise. */
5336
5337 static bool
5338 read_debug_names_from_section (struct objfile *objfile,
5339 const char *filename,
5340 struct dwarf2_section_info *section,
5341 mapped_debug_names &map)
5342 {
5343 if (dwarf2_section_empty_p (section))
5344 return false;
5345
5346 /* Older elfutils strip versions could keep the section in the main
5347 executable while splitting it for the separate debug info file. */
5348 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5349 return false;
5350
5351 dwarf2_read_section (objfile, section);
5352
5353 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5354
5355 const gdb_byte *addr = section->buffer;
5356
5357 bfd *const abfd = get_section_bfd_owner (section);
5358
5359 unsigned int bytes_read;
5360 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5361 addr += bytes_read;
5362
5363 map.dwarf5_is_dwarf64 = bytes_read != 4;
5364 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5365 if (bytes_read + length != section->size)
5366 {
5367 /* There may be multiple per-CU indices. */
5368 warning (_("Section .debug_names in %s length %s does not match "
5369 "section length %s, ignoring .debug_names."),
5370 filename, plongest (bytes_read + length),
5371 pulongest (section->size));
5372 return false;
5373 }
5374
5375 /* The version number. */
5376 uint16_t version = read_2_bytes (abfd, addr);
5377 addr += 2;
5378 if (version != 5)
5379 {
5380 warning (_("Section .debug_names in %s has unsupported version %d, "
5381 "ignoring .debug_names."),
5382 filename, version);
5383 return false;
5384 }
5385
5386 /* Padding. */
5387 uint16_t padding = read_2_bytes (abfd, addr);
5388 addr += 2;
5389 if (padding != 0)
5390 {
5391 warning (_("Section .debug_names in %s has unsupported padding %d, "
5392 "ignoring .debug_names."),
5393 filename, padding);
5394 return false;
5395 }
5396
5397 /* comp_unit_count - The number of CUs in the CU list. */
5398 map.cu_count = read_4_bytes (abfd, addr);
5399 addr += 4;
5400
5401 /* local_type_unit_count - The number of TUs in the local TU
5402 list. */
5403 map.tu_count = read_4_bytes (abfd, addr);
5404 addr += 4;
5405
5406 /* foreign_type_unit_count - The number of TUs in the foreign TU
5407 list. */
5408 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5409 addr += 4;
5410 if (foreign_tu_count != 0)
5411 {
5412 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5413 "ignoring .debug_names."),
5414 filename, static_cast<unsigned long> (foreign_tu_count));
5415 return false;
5416 }
5417
5418 /* bucket_count - The number of hash buckets in the hash lookup
5419 table. */
5420 map.bucket_count = read_4_bytes (abfd, addr);
5421 addr += 4;
5422
5423 /* name_count - The number of unique names in the index. */
5424 map.name_count = read_4_bytes (abfd, addr);
5425 addr += 4;
5426
5427 /* abbrev_table_size - The size in bytes of the abbreviations
5428 table. */
5429 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5430 addr += 4;
5431
5432 /* augmentation_string_size - The size in bytes of the augmentation
5433 string. This value is rounded up to a multiple of 4. */
5434 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5435 addr += 4;
5436 map.augmentation_is_gdb = ((augmentation_string_size
5437 == sizeof (dwarf5_augmentation))
5438 && memcmp (addr, dwarf5_augmentation,
5439 sizeof (dwarf5_augmentation)) == 0);
5440 augmentation_string_size += (-augmentation_string_size) & 3;
5441 addr += augmentation_string_size;
5442
5443 /* List of CUs */
5444 map.cu_table_reordered = addr;
5445 addr += map.cu_count * map.offset_size;
5446
5447 /* List of Local TUs */
5448 map.tu_table_reordered = addr;
5449 addr += map.tu_count * map.offset_size;
5450
5451 /* Hash Lookup Table */
5452 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5453 addr += map.bucket_count * 4;
5454 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5455 addr += map.name_count * 4;
5456
5457 /* Name Table */
5458 map.name_table_string_offs_reordered = addr;
5459 addr += map.name_count * map.offset_size;
5460 map.name_table_entry_offs_reordered = addr;
5461 addr += map.name_count * map.offset_size;
5462
5463 const gdb_byte *abbrev_table_start = addr;
5464 for (;;)
5465 {
5466 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5467 addr += bytes_read;
5468 if (index_num == 0)
5469 break;
5470
5471 const auto insertpair
5472 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5473 if (!insertpair.second)
5474 {
5475 warning (_("Section .debug_names in %s has duplicate index %s, "
5476 "ignoring .debug_names."),
5477 filename, pulongest (index_num));
5478 return false;
5479 }
5480 mapped_debug_names::index_val &indexval = insertpair.first->second;
5481 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5482 addr += bytes_read;
5483
5484 for (;;)
5485 {
5486 mapped_debug_names::index_val::attr attr;
5487 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5488 addr += bytes_read;
5489 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5490 addr += bytes_read;
5491 if (attr.form == DW_FORM_implicit_const)
5492 {
5493 attr.implicit_const = read_signed_leb128 (abfd, addr,
5494 &bytes_read);
5495 addr += bytes_read;
5496 }
5497 if (attr.dw_idx == 0 && attr.form == 0)
5498 break;
5499 indexval.attr_vec.push_back (std::move (attr));
5500 }
5501 }
5502 if (addr != abbrev_table_start + abbrev_table_size)
5503 {
5504 warning (_("Section .debug_names in %s has abbreviation_table "
5505 "of size %s vs. written as %u, ignoring .debug_names."),
5506 filename, plongest (addr - abbrev_table_start),
5507 abbrev_table_size);
5508 return false;
5509 }
5510 map.entry_pool = addr;
5511
5512 return true;
5513 }
5514
5515 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5516 list. */
5517
5518 static void
5519 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5520 const mapped_debug_names &map,
5521 dwarf2_section_info &section,
5522 bool is_dwz)
5523 {
5524 sect_offset sect_off_prev;
5525 for (uint32_t i = 0; i <= map.cu_count; ++i)
5526 {
5527 sect_offset sect_off_next;
5528 if (i < map.cu_count)
5529 {
5530 sect_off_next
5531 = (sect_offset) (extract_unsigned_integer
5532 (map.cu_table_reordered + i * map.offset_size,
5533 map.offset_size,
5534 map.dwarf5_byte_order));
5535 }
5536 else
5537 sect_off_next = (sect_offset) section.size;
5538 if (i >= 1)
5539 {
5540 const ULONGEST length = sect_off_next - sect_off_prev;
5541 dwarf2_per_cu_data *per_cu
5542 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5543 sect_off_prev, length);
5544 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5545 }
5546 sect_off_prev = sect_off_next;
5547 }
5548 }
5549
5550 /* Read the CU list from the mapped index, and use it to create all
5551 the CU objects for this dwarf2_per_objfile. */
5552
5553 static void
5554 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5555 const mapped_debug_names &map,
5556 const mapped_debug_names &dwz_map)
5557 {
5558 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5559 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5560
5561 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5562 dwarf2_per_objfile->info,
5563 false /* is_dwz */);
5564
5565 if (dwz_map.cu_count == 0)
5566 return;
5567
5568 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5569 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5570 true /* is_dwz */);
5571 }
5572
5573 /* Read .debug_names. If everything went ok, initialize the "quick"
5574 elements of all the CUs and return true. Otherwise, return false. */
5575
5576 static bool
5577 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5578 {
5579 std::unique_ptr<mapped_debug_names> map
5580 (new mapped_debug_names (dwarf2_per_objfile));
5581 mapped_debug_names dwz_map (dwarf2_per_objfile);
5582 struct objfile *objfile = dwarf2_per_objfile->objfile;
5583
5584 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5585 &dwarf2_per_objfile->debug_names,
5586 *map))
5587 return false;
5588
5589 /* Don't use the index if it's empty. */
5590 if (map->name_count == 0)
5591 return false;
5592
5593 /* If there is a .dwz file, read it so we can get its CU list as
5594 well. */
5595 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5596 if (dwz != NULL)
5597 {
5598 if (!read_debug_names_from_section (objfile,
5599 bfd_get_filename (dwz->dwz_bfd.get ()),
5600 &dwz->debug_names, dwz_map))
5601 {
5602 warning (_("could not read '.debug_names' section from %s; skipping"),
5603 bfd_get_filename (dwz->dwz_bfd.get ()));
5604 return false;
5605 }
5606 }
5607
5608 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5609
5610 if (map->tu_count != 0)
5611 {
5612 /* We can only handle a single .debug_types when we have an
5613 index. */
5614 if (dwarf2_per_objfile->types.size () != 1)
5615 return false;
5616
5617 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5618
5619 create_signatured_type_table_from_debug_names
5620 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5621 }
5622
5623 create_addrmap_from_aranges (dwarf2_per_objfile,
5624 &dwarf2_per_objfile->debug_aranges);
5625
5626 dwarf2_per_objfile->debug_names_table = std::move (map);
5627 dwarf2_per_objfile->using_index = 1;
5628 dwarf2_per_objfile->quick_file_names_table =
5629 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5630
5631 return true;
5632 }
5633
5634 /* Type used to manage iterating over all CUs looking for a symbol for
5635 .debug_names. */
5636
5637 class dw2_debug_names_iterator
5638 {
5639 public:
5640 dw2_debug_names_iterator (const mapped_debug_names &map,
5641 gdb::optional<block_enum> block_index,
5642 domain_enum domain,
5643 const char *name)
5644 : m_map (map), m_block_index (block_index), m_domain (domain),
5645 m_addr (find_vec_in_debug_names (map, name))
5646 {}
5647
5648 dw2_debug_names_iterator (const mapped_debug_names &map,
5649 search_domain search, uint32_t namei)
5650 : m_map (map),
5651 m_search (search),
5652 m_addr (find_vec_in_debug_names (map, namei))
5653 {}
5654
5655 dw2_debug_names_iterator (const mapped_debug_names &map,
5656 block_enum block_index, domain_enum domain,
5657 uint32_t namei)
5658 : m_map (map), m_block_index (block_index), m_domain (domain),
5659 m_addr (find_vec_in_debug_names (map, namei))
5660 {}
5661
5662 /* Return the next matching CU or NULL if there are no more. */
5663 dwarf2_per_cu_data *next ();
5664
5665 private:
5666 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5667 const char *name);
5668 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5669 uint32_t namei);
5670
5671 /* The internalized form of .debug_names. */
5672 const mapped_debug_names &m_map;
5673
5674 /* If set, only look for symbols that match that block. Valid values are
5675 GLOBAL_BLOCK and STATIC_BLOCK. */
5676 const gdb::optional<block_enum> m_block_index;
5677
5678 /* The kind of symbol we're looking for. */
5679 const domain_enum m_domain = UNDEF_DOMAIN;
5680 const search_domain m_search = ALL_DOMAIN;
5681
5682 /* The list of CUs from the index entry of the symbol, or NULL if
5683 not found. */
5684 const gdb_byte *m_addr;
5685 };
5686
5687 const char *
5688 mapped_debug_names::namei_to_name (uint32_t namei) const
5689 {
5690 const ULONGEST namei_string_offs
5691 = extract_unsigned_integer ((name_table_string_offs_reordered
5692 + namei * offset_size),
5693 offset_size,
5694 dwarf5_byte_order);
5695 return read_indirect_string_at_offset
5696 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5697 }
5698
5699 /* Find a slot in .debug_names for the object named NAME. If NAME is
5700 found, return pointer to its pool data. If NAME cannot be found,
5701 return NULL. */
5702
5703 const gdb_byte *
5704 dw2_debug_names_iterator::find_vec_in_debug_names
5705 (const mapped_debug_names &map, const char *name)
5706 {
5707 int (*cmp) (const char *, const char *);
5708
5709 gdb::unique_xmalloc_ptr<char> without_params;
5710 if (current_language->la_language == language_cplus
5711 || current_language->la_language == language_fortran
5712 || current_language->la_language == language_d)
5713 {
5714 /* NAME is already canonical. Drop any qualifiers as
5715 .debug_names does not contain any. */
5716
5717 if (strchr (name, '(') != NULL)
5718 {
5719 without_params = cp_remove_params (name);
5720 if (without_params != NULL)
5721 name = without_params.get ();
5722 }
5723 }
5724
5725 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5726
5727 const uint32_t full_hash = dwarf5_djb_hash (name);
5728 uint32_t namei
5729 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5730 (map.bucket_table_reordered
5731 + (full_hash % map.bucket_count)), 4,
5732 map.dwarf5_byte_order);
5733 if (namei == 0)
5734 return NULL;
5735 --namei;
5736 if (namei >= map.name_count)
5737 {
5738 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5739 "[in module %s]"),
5740 namei, map.name_count,
5741 objfile_name (map.dwarf2_per_objfile->objfile));
5742 return NULL;
5743 }
5744
5745 for (;;)
5746 {
5747 const uint32_t namei_full_hash
5748 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5749 (map.hash_table_reordered + namei), 4,
5750 map.dwarf5_byte_order);
5751 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5752 return NULL;
5753
5754 if (full_hash == namei_full_hash)
5755 {
5756 const char *const namei_string = map.namei_to_name (namei);
5757
5758 #if 0 /* An expensive sanity check. */
5759 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5760 {
5761 complaint (_("Wrong .debug_names hash for string at index %u "
5762 "[in module %s]"),
5763 namei, objfile_name (dwarf2_per_objfile->objfile));
5764 return NULL;
5765 }
5766 #endif
5767
5768 if (cmp (namei_string, name) == 0)
5769 {
5770 const ULONGEST namei_entry_offs
5771 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5772 + namei * map.offset_size),
5773 map.offset_size, map.dwarf5_byte_order);
5774 return map.entry_pool + namei_entry_offs;
5775 }
5776 }
5777
5778 ++namei;
5779 if (namei >= map.name_count)
5780 return NULL;
5781 }
5782 }
5783
5784 const gdb_byte *
5785 dw2_debug_names_iterator::find_vec_in_debug_names
5786 (const mapped_debug_names &map, uint32_t namei)
5787 {
5788 if (namei >= map.name_count)
5789 {
5790 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5791 "[in module %s]"),
5792 namei, map.name_count,
5793 objfile_name (map.dwarf2_per_objfile->objfile));
5794 return NULL;
5795 }
5796
5797 const ULONGEST namei_entry_offs
5798 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5799 + namei * map.offset_size),
5800 map.offset_size, map.dwarf5_byte_order);
5801 return map.entry_pool + namei_entry_offs;
5802 }
5803
5804 /* See dw2_debug_names_iterator. */
5805
5806 dwarf2_per_cu_data *
5807 dw2_debug_names_iterator::next ()
5808 {
5809 if (m_addr == NULL)
5810 return NULL;
5811
5812 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5813 struct objfile *objfile = dwarf2_per_objfile->objfile;
5814 bfd *const abfd = objfile->obfd;
5815
5816 again:
5817
5818 unsigned int bytes_read;
5819 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5820 m_addr += bytes_read;
5821 if (abbrev == 0)
5822 return NULL;
5823
5824 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5825 if (indexval_it == m_map.abbrev_map.cend ())
5826 {
5827 complaint (_("Wrong .debug_names undefined abbrev code %s "
5828 "[in module %s]"),
5829 pulongest (abbrev), objfile_name (objfile));
5830 return NULL;
5831 }
5832 const mapped_debug_names::index_val &indexval = indexval_it->second;
5833 enum class symbol_linkage {
5834 unknown,
5835 static_,
5836 extern_,
5837 } symbol_linkage_ = symbol_linkage::unknown;
5838 dwarf2_per_cu_data *per_cu = NULL;
5839 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5840 {
5841 ULONGEST ull;
5842 switch (attr.form)
5843 {
5844 case DW_FORM_implicit_const:
5845 ull = attr.implicit_const;
5846 break;
5847 case DW_FORM_flag_present:
5848 ull = 1;
5849 break;
5850 case DW_FORM_udata:
5851 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5852 m_addr += bytes_read;
5853 break;
5854 default:
5855 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5856 dwarf_form_name (attr.form),
5857 objfile_name (objfile));
5858 return NULL;
5859 }
5860 switch (attr.dw_idx)
5861 {
5862 case DW_IDX_compile_unit:
5863 /* Don't crash on bad data. */
5864 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5865 {
5866 complaint (_(".debug_names entry has bad CU index %s"
5867 " [in module %s]"),
5868 pulongest (ull),
5869 objfile_name (dwarf2_per_objfile->objfile));
5870 continue;
5871 }
5872 per_cu = dwarf2_per_objfile->get_cutu (ull);
5873 break;
5874 case DW_IDX_type_unit:
5875 /* Don't crash on bad data. */
5876 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5877 {
5878 complaint (_(".debug_names entry has bad TU index %s"
5879 " [in module %s]"),
5880 pulongest (ull),
5881 objfile_name (dwarf2_per_objfile->objfile));
5882 continue;
5883 }
5884 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5885 break;
5886 case DW_IDX_GNU_internal:
5887 if (!m_map.augmentation_is_gdb)
5888 break;
5889 symbol_linkage_ = symbol_linkage::static_;
5890 break;
5891 case DW_IDX_GNU_external:
5892 if (!m_map.augmentation_is_gdb)
5893 break;
5894 symbol_linkage_ = symbol_linkage::extern_;
5895 break;
5896 }
5897 }
5898
5899 /* Skip if already read in. */
5900 if (per_cu->v.quick->compunit_symtab)
5901 goto again;
5902
5903 /* Check static vs global. */
5904 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5905 {
5906 const bool want_static = *m_block_index == STATIC_BLOCK;
5907 const bool symbol_is_static =
5908 symbol_linkage_ == symbol_linkage::static_;
5909 if (want_static != symbol_is_static)
5910 goto again;
5911 }
5912
5913 /* Match dw2_symtab_iter_next, symbol_kind
5914 and debug_names::psymbol_tag. */
5915 switch (m_domain)
5916 {
5917 case VAR_DOMAIN:
5918 switch (indexval.dwarf_tag)
5919 {
5920 case DW_TAG_variable:
5921 case DW_TAG_subprogram:
5922 /* Some types are also in VAR_DOMAIN. */
5923 case DW_TAG_typedef:
5924 case DW_TAG_structure_type:
5925 break;
5926 default:
5927 goto again;
5928 }
5929 break;
5930 case STRUCT_DOMAIN:
5931 switch (indexval.dwarf_tag)
5932 {
5933 case DW_TAG_typedef:
5934 case DW_TAG_structure_type:
5935 break;
5936 default:
5937 goto again;
5938 }
5939 break;
5940 case LABEL_DOMAIN:
5941 switch (indexval.dwarf_tag)
5942 {
5943 case 0:
5944 case DW_TAG_variable:
5945 break;
5946 default:
5947 goto again;
5948 }
5949 break;
5950 default:
5951 break;
5952 }
5953
5954 /* Match dw2_expand_symtabs_matching, symbol_kind and
5955 debug_names::psymbol_tag. */
5956 switch (m_search)
5957 {
5958 case VARIABLES_DOMAIN:
5959 switch (indexval.dwarf_tag)
5960 {
5961 case DW_TAG_variable:
5962 break;
5963 default:
5964 goto again;
5965 }
5966 break;
5967 case FUNCTIONS_DOMAIN:
5968 switch (indexval.dwarf_tag)
5969 {
5970 case DW_TAG_subprogram:
5971 break;
5972 default:
5973 goto again;
5974 }
5975 break;
5976 case TYPES_DOMAIN:
5977 switch (indexval.dwarf_tag)
5978 {
5979 case DW_TAG_typedef:
5980 case DW_TAG_structure_type:
5981 break;
5982 default:
5983 goto again;
5984 }
5985 break;
5986 default:
5987 break;
5988 }
5989
5990 return per_cu;
5991 }
5992
5993 static struct compunit_symtab *
5994 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5995 const char *name, domain_enum domain)
5996 {
5997 struct dwarf2_per_objfile *dwarf2_per_objfile
5998 = get_dwarf2_per_objfile (objfile);
5999
6000 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6001 if (!mapp)
6002 {
6003 /* index is NULL if OBJF_READNOW. */
6004 return NULL;
6005 }
6006 const auto &map = *mapp;
6007
6008 dw2_debug_names_iterator iter (map, block_index, domain, name);
6009
6010 struct compunit_symtab *stab_best = NULL;
6011 struct dwarf2_per_cu_data *per_cu;
6012 while ((per_cu = iter.next ()) != NULL)
6013 {
6014 struct symbol *sym, *with_opaque = NULL;
6015 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
6016 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6017 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6018
6019 sym = block_find_symbol (block, name, domain,
6020 block_find_non_opaque_type_preferred,
6021 &with_opaque);
6022
6023 /* Some caution must be observed with overloaded functions and
6024 methods, since the index will not contain any overload
6025 information (but NAME might contain it). */
6026
6027 if (sym != NULL
6028 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6029 return stab;
6030 if (with_opaque != NULL
6031 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6032 stab_best = stab;
6033
6034 /* Keep looking through other CUs. */
6035 }
6036
6037 return stab_best;
6038 }
6039
6040 /* This dumps minimal information about .debug_names. It is called
6041 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6042 uses this to verify that .debug_names has been loaded. */
6043
6044 static void
6045 dw2_debug_names_dump (struct objfile *objfile)
6046 {
6047 struct dwarf2_per_objfile *dwarf2_per_objfile
6048 = get_dwarf2_per_objfile (objfile);
6049
6050 gdb_assert (dwarf2_per_objfile->using_index);
6051 printf_filtered (".debug_names:");
6052 if (dwarf2_per_objfile->debug_names_table)
6053 printf_filtered (" exists\n");
6054 else
6055 printf_filtered (" faked for \"readnow\"\n");
6056 printf_filtered ("\n");
6057 }
6058
6059 static void
6060 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6061 const char *func_name)
6062 {
6063 struct dwarf2_per_objfile *dwarf2_per_objfile
6064 = get_dwarf2_per_objfile (objfile);
6065
6066 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6067 if (dwarf2_per_objfile->debug_names_table)
6068 {
6069 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6070
6071 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
6072
6073 struct dwarf2_per_cu_data *per_cu;
6074 while ((per_cu = iter.next ()) != NULL)
6075 dw2_instantiate_symtab (per_cu, false);
6076 }
6077 }
6078
6079 static void
6080 dw2_debug_names_map_matching_symbols
6081 (struct objfile *objfile,
6082 const lookup_name_info &name, domain_enum domain,
6083 int global,
6084 gdb::function_view<symbol_found_callback_ftype> callback,
6085 symbol_compare_ftype *ordered_compare)
6086 {
6087 struct dwarf2_per_objfile *dwarf2_per_objfile
6088 = get_dwarf2_per_objfile (objfile);
6089
6090 /* debug_names_table is NULL if OBJF_READNOW. */
6091 if (!dwarf2_per_objfile->debug_names_table)
6092 return;
6093
6094 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6095 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6096
6097 const char *match_name = name.ada ().lookup_name ().c_str ();
6098 auto matcher = [&] (const char *symname)
6099 {
6100 if (ordered_compare == nullptr)
6101 return true;
6102 return ordered_compare (symname, match_name) == 0;
6103 };
6104
6105 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6106 [&] (offset_type namei)
6107 {
6108 /* The name was matched, now expand corresponding CUs that were
6109 marked. */
6110 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6111
6112 struct dwarf2_per_cu_data *per_cu;
6113 while ((per_cu = iter.next ()) != NULL)
6114 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6115 return true;
6116 });
6117
6118 /* It's a shame we couldn't do this inside the
6119 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6120 that have already been expanded. Instead, this loop matches what
6121 the psymtab code does. */
6122 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6123 {
6124 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6125 if (cust != nullptr)
6126 {
6127 const struct block *block
6128 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6129 if (!iterate_over_symbols_terminated (block, name,
6130 domain, callback))
6131 break;
6132 }
6133 }
6134 }
6135
6136 static void
6137 dw2_debug_names_expand_symtabs_matching
6138 (struct objfile *objfile,
6139 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6140 const lookup_name_info &lookup_name,
6141 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6142 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6143 enum search_domain kind)
6144 {
6145 struct dwarf2_per_objfile *dwarf2_per_objfile
6146 = get_dwarf2_per_objfile (objfile);
6147
6148 /* debug_names_table is NULL if OBJF_READNOW. */
6149 if (!dwarf2_per_objfile->debug_names_table)
6150 return;
6151
6152 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6153
6154 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6155
6156 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6157 symbol_matcher,
6158 kind, [&] (offset_type namei)
6159 {
6160 /* The name was matched, now expand corresponding CUs that were
6161 marked. */
6162 dw2_debug_names_iterator iter (map, kind, namei);
6163
6164 struct dwarf2_per_cu_data *per_cu;
6165 while ((per_cu = iter.next ()) != NULL)
6166 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6167 expansion_notify);
6168 return true;
6169 });
6170 }
6171
6172 const struct quick_symbol_functions dwarf2_debug_names_functions =
6173 {
6174 dw2_has_symbols,
6175 dw2_find_last_source_symtab,
6176 dw2_forget_cached_source_info,
6177 dw2_map_symtabs_matching_filename,
6178 dw2_debug_names_lookup_symbol,
6179 dw2_print_stats,
6180 dw2_debug_names_dump,
6181 dw2_debug_names_expand_symtabs_for_function,
6182 dw2_expand_all_symtabs,
6183 dw2_expand_symtabs_with_fullname,
6184 dw2_debug_names_map_matching_symbols,
6185 dw2_debug_names_expand_symtabs_matching,
6186 dw2_find_pc_sect_compunit_symtab,
6187 NULL,
6188 dw2_map_symbol_filenames
6189 };
6190
6191 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6192 to either a dwarf2_per_objfile or dwz_file object. */
6193
6194 template <typename T>
6195 static gdb::array_view<const gdb_byte>
6196 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6197 {
6198 dwarf2_section_info *section = &section_owner->gdb_index;
6199
6200 if (dwarf2_section_empty_p (section))
6201 return {};
6202
6203 /* Older elfutils strip versions could keep the section in the main
6204 executable while splitting it for the separate debug info file. */
6205 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6206 return {};
6207
6208 dwarf2_read_section (obj, section);
6209
6210 /* dwarf2_section_info::size is a bfd_size_type, while
6211 gdb::array_view works with size_t. On 32-bit hosts, with
6212 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6213 is 32-bit. So we need an explicit narrowing conversion here.
6214 This is fine, because it's impossible to allocate or mmap an
6215 array/buffer larger than what size_t can represent. */
6216 return gdb::make_array_view (section->buffer, section->size);
6217 }
6218
6219 /* Lookup the index cache for the contents of the index associated to
6220 DWARF2_OBJ. */
6221
6222 static gdb::array_view<const gdb_byte>
6223 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6224 {
6225 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6226 if (build_id == nullptr)
6227 return {};
6228
6229 return global_index_cache.lookup_gdb_index (build_id,
6230 &dwarf2_obj->index_cache_res);
6231 }
6232
6233 /* Same as the above, but for DWZ. */
6234
6235 static gdb::array_view<const gdb_byte>
6236 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6237 {
6238 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6239 if (build_id == nullptr)
6240 return {};
6241
6242 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6243 }
6244
6245 /* See symfile.h. */
6246
6247 bool
6248 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6249 {
6250 struct dwarf2_per_objfile *dwarf2_per_objfile
6251 = get_dwarf2_per_objfile (objfile);
6252
6253 /* If we're about to read full symbols, don't bother with the
6254 indices. In this case we also don't care if some other debug
6255 format is making psymtabs, because they are all about to be
6256 expanded anyway. */
6257 if ((objfile->flags & OBJF_READNOW))
6258 {
6259 dwarf2_per_objfile->using_index = 1;
6260 create_all_comp_units (dwarf2_per_objfile);
6261 create_all_type_units (dwarf2_per_objfile);
6262 dwarf2_per_objfile->quick_file_names_table
6263 = create_quick_file_names_table
6264 (dwarf2_per_objfile->all_comp_units.size ());
6265
6266 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6267 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6268 {
6269 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6270
6271 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6272 struct dwarf2_per_cu_quick_data);
6273 }
6274
6275 /* Return 1 so that gdb sees the "quick" functions. However,
6276 these functions will be no-ops because we will have expanded
6277 all symtabs. */
6278 *index_kind = dw_index_kind::GDB_INDEX;
6279 return true;
6280 }
6281
6282 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6283 {
6284 *index_kind = dw_index_kind::DEBUG_NAMES;
6285 return true;
6286 }
6287
6288 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6289 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6290 get_gdb_index_contents_from_section<dwz_file>))
6291 {
6292 *index_kind = dw_index_kind::GDB_INDEX;
6293 return true;
6294 }
6295
6296 /* ... otherwise, try to find the index in the index cache. */
6297 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6298 get_gdb_index_contents_from_cache,
6299 get_gdb_index_contents_from_cache_dwz))
6300 {
6301 global_index_cache.hit ();
6302 *index_kind = dw_index_kind::GDB_INDEX;
6303 return true;
6304 }
6305
6306 global_index_cache.miss ();
6307 return false;
6308 }
6309
6310 \f
6311
6312 /* Build a partial symbol table. */
6313
6314 void
6315 dwarf2_build_psymtabs (struct objfile *objfile)
6316 {
6317 struct dwarf2_per_objfile *dwarf2_per_objfile
6318 = get_dwarf2_per_objfile (objfile);
6319
6320 init_psymbol_list (objfile, 1024);
6321
6322 try
6323 {
6324 /* This isn't really ideal: all the data we allocate on the
6325 objfile's obstack is still uselessly kept around. However,
6326 freeing it seems unsafe. */
6327 psymtab_discarder psymtabs (objfile);
6328 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6329 psymtabs.keep ();
6330
6331 /* (maybe) store an index in the cache. */
6332 global_index_cache.store (dwarf2_per_objfile);
6333 }
6334 catch (const gdb_exception_error &except)
6335 {
6336 exception_print (gdb_stderr, except);
6337 }
6338 }
6339
6340 /* Return the total length of the CU described by HEADER. */
6341
6342 static unsigned int
6343 get_cu_length (const struct comp_unit_head *header)
6344 {
6345 return header->initial_length_size + header->length;
6346 }
6347
6348 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6349
6350 static inline bool
6351 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6352 {
6353 sect_offset bottom = cu_header->sect_off;
6354 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6355
6356 return sect_off >= bottom && sect_off < top;
6357 }
6358
6359 /* Find the base address of the compilation unit for range lists and
6360 location lists. It will normally be specified by DW_AT_low_pc.
6361 In DWARF-3 draft 4, the base address could be overridden by
6362 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6363 compilation units with discontinuous ranges. */
6364
6365 static void
6366 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6367 {
6368 struct attribute *attr;
6369
6370 cu->base_known = 0;
6371 cu->base_address = 0;
6372
6373 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6374 if (attr)
6375 {
6376 cu->base_address = attr_value_as_address (attr);
6377 cu->base_known = 1;
6378 }
6379 else
6380 {
6381 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6382 if (attr)
6383 {
6384 cu->base_address = attr_value_as_address (attr);
6385 cu->base_known = 1;
6386 }
6387 }
6388 }
6389
6390 /* Read in the comp unit header information from the debug_info at info_ptr.
6391 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6392 NOTE: This leaves members offset, first_die_offset to be filled in
6393 by the caller. */
6394
6395 static const gdb_byte *
6396 read_comp_unit_head (struct comp_unit_head *cu_header,
6397 const gdb_byte *info_ptr,
6398 struct dwarf2_section_info *section,
6399 rcuh_kind section_kind)
6400 {
6401 int signed_addr;
6402 unsigned int bytes_read;
6403 const char *filename = get_section_file_name (section);
6404 bfd *abfd = get_section_bfd_owner (section);
6405
6406 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6407 cu_header->initial_length_size = bytes_read;
6408 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6409 info_ptr += bytes_read;
6410 cu_header->version = read_2_bytes (abfd, info_ptr);
6411 if (cu_header->version < 2 || cu_header->version > 5)
6412 error (_("Dwarf Error: wrong version in compilation unit header "
6413 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6414 cu_header->version, filename);
6415 info_ptr += 2;
6416 if (cu_header->version < 5)
6417 switch (section_kind)
6418 {
6419 case rcuh_kind::COMPILE:
6420 cu_header->unit_type = DW_UT_compile;
6421 break;
6422 case rcuh_kind::TYPE:
6423 cu_header->unit_type = DW_UT_type;
6424 break;
6425 default:
6426 internal_error (__FILE__, __LINE__,
6427 _("read_comp_unit_head: invalid section_kind"));
6428 }
6429 else
6430 {
6431 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6432 (read_1_byte (abfd, info_ptr));
6433 info_ptr += 1;
6434 switch (cu_header->unit_type)
6435 {
6436 case DW_UT_compile:
6437 case DW_UT_partial:
6438 case DW_UT_skeleton:
6439 case DW_UT_split_compile:
6440 if (section_kind != rcuh_kind::COMPILE)
6441 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6442 "(is %s, should be %s) [in module %s]"),
6443 dwarf_unit_type_name (cu_header->unit_type),
6444 dwarf_unit_type_name (DW_UT_type), filename);
6445 break;
6446 case DW_UT_type:
6447 case DW_UT_split_type:
6448 section_kind = rcuh_kind::TYPE;
6449 break;
6450 default:
6451 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6452 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6453 "[in module %s]"), cu_header->unit_type,
6454 dwarf_unit_type_name (DW_UT_compile),
6455 dwarf_unit_type_name (DW_UT_skeleton),
6456 dwarf_unit_type_name (DW_UT_split_compile),
6457 dwarf_unit_type_name (DW_UT_type),
6458 dwarf_unit_type_name (DW_UT_split_type), filename);
6459 }
6460
6461 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6462 info_ptr += 1;
6463 }
6464 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6465 cu_header,
6466 &bytes_read);
6467 info_ptr += bytes_read;
6468 if (cu_header->version < 5)
6469 {
6470 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6471 info_ptr += 1;
6472 }
6473 signed_addr = bfd_get_sign_extend_vma (abfd);
6474 if (signed_addr < 0)
6475 internal_error (__FILE__, __LINE__,
6476 _("read_comp_unit_head: dwarf from non elf file"));
6477 cu_header->signed_addr_p = signed_addr;
6478
6479 bool header_has_signature = section_kind == rcuh_kind::TYPE
6480 || cu_header->unit_type == DW_UT_skeleton
6481 || cu_header->unit_type == DW_UT_split_compile;
6482
6483 if (header_has_signature)
6484 {
6485 cu_header->signature = read_8_bytes (abfd, info_ptr);
6486 info_ptr += 8;
6487 }
6488
6489 if (section_kind == rcuh_kind::TYPE)
6490 {
6491 LONGEST type_offset;
6492 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6493 info_ptr += bytes_read;
6494 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6495 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6496 error (_("Dwarf Error: Too big type_offset in compilation unit "
6497 "header (is %s) [in module %s]"), plongest (type_offset),
6498 filename);
6499 }
6500
6501 return info_ptr;
6502 }
6503
6504 /* Helper function that returns the proper abbrev section for
6505 THIS_CU. */
6506
6507 static struct dwarf2_section_info *
6508 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6509 {
6510 struct dwarf2_section_info *abbrev;
6511 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6512
6513 if (this_cu->is_dwz)
6514 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6515 else
6516 abbrev = &dwarf2_per_objfile->abbrev;
6517
6518 return abbrev;
6519 }
6520
6521 /* Subroutine of read_and_check_comp_unit_head and
6522 read_and_check_type_unit_head to simplify them.
6523 Perform various error checking on the header. */
6524
6525 static void
6526 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6527 struct comp_unit_head *header,
6528 struct dwarf2_section_info *section,
6529 struct dwarf2_section_info *abbrev_section)
6530 {
6531 const char *filename = get_section_file_name (section);
6532
6533 if (to_underlying (header->abbrev_sect_off)
6534 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6535 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6536 "(offset %s + 6) [in module %s]"),
6537 sect_offset_str (header->abbrev_sect_off),
6538 sect_offset_str (header->sect_off),
6539 filename);
6540
6541 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6542 avoid potential 32-bit overflow. */
6543 if (((ULONGEST) header->sect_off + get_cu_length (header))
6544 > section->size)
6545 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6546 "(offset %s + 0) [in module %s]"),
6547 header->length, sect_offset_str (header->sect_off),
6548 filename);
6549 }
6550
6551 /* Read in a CU/TU header and perform some basic error checking.
6552 The contents of the header are stored in HEADER.
6553 The result is a pointer to the start of the first DIE. */
6554
6555 static const gdb_byte *
6556 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6557 struct comp_unit_head *header,
6558 struct dwarf2_section_info *section,
6559 struct dwarf2_section_info *abbrev_section,
6560 const gdb_byte *info_ptr,
6561 rcuh_kind section_kind)
6562 {
6563 const gdb_byte *beg_of_comp_unit = info_ptr;
6564
6565 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6566
6567 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6568
6569 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6570
6571 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6572 abbrev_section);
6573
6574 return info_ptr;
6575 }
6576
6577 /* Fetch the abbreviation table offset from a comp or type unit header. */
6578
6579 static sect_offset
6580 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6581 struct dwarf2_section_info *section,
6582 sect_offset sect_off)
6583 {
6584 bfd *abfd = get_section_bfd_owner (section);
6585 const gdb_byte *info_ptr;
6586 unsigned int initial_length_size, offset_size;
6587 uint16_t version;
6588
6589 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6590 info_ptr = section->buffer + to_underlying (sect_off);
6591 read_initial_length (abfd, info_ptr, &initial_length_size);
6592 offset_size = initial_length_size == 4 ? 4 : 8;
6593 info_ptr += initial_length_size;
6594
6595 version = read_2_bytes (abfd, info_ptr);
6596 info_ptr += 2;
6597 if (version >= 5)
6598 {
6599 /* Skip unit type and address size. */
6600 info_ptr += 2;
6601 }
6602
6603 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6604 }
6605
6606 /* Allocate a new partial symtab for file named NAME and mark this new
6607 partial symtab as being an include of PST. */
6608
6609 static void
6610 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6611 struct objfile *objfile)
6612 {
6613 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6614
6615 if (!IS_ABSOLUTE_PATH (subpst->filename))
6616 {
6617 /* It shares objfile->objfile_obstack. */
6618 subpst->dirname = pst->dirname;
6619 }
6620
6621 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6622 subpst->dependencies[0] = pst;
6623 subpst->number_of_dependencies = 1;
6624
6625 subpst->read_symtab = pst->read_symtab;
6626
6627 /* No private part is necessary for include psymtabs. This property
6628 can be used to differentiate between such include psymtabs and
6629 the regular ones. */
6630 subpst->read_symtab_private = NULL;
6631 }
6632
6633 /* Read the Line Number Program data and extract the list of files
6634 included by the source file represented by PST. Build an include
6635 partial symtab for each of these included files. */
6636
6637 static void
6638 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6639 struct die_info *die,
6640 struct partial_symtab *pst)
6641 {
6642 line_header_up lh;
6643 struct attribute *attr;
6644
6645 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6646 if (attr)
6647 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6648 if (lh == NULL)
6649 return; /* No linetable, so no includes. */
6650
6651 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6652 that we pass in the raw text_low here; that is ok because we're
6653 only decoding the line table to make include partial symtabs, and
6654 so the addresses aren't really used. */
6655 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6656 pst->raw_text_low (), 1);
6657 }
6658
6659 static hashval_t
6660 hash_signatured_type (const void *item)
6661 {
6662 const struct signatured_type *sig_type
6663 = (const struct signatured_type *) item;
6664
6665 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6666 return sig_type->signature;
6667 }
6668
6669 static int
6670 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6671 {
6672 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6673 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6674
6675 return lhs->signature == rhs->signature;
6676 }
6677
6678 /* Allocate a hash table for signatured types. */
6679
6680 static htab_t
6681 allocate_signatured_type_table (struct objfile *objfile)
6682 {
6683 return htab_create_alloc_ex (41,
6684 hash_signatured_type,
6685 eq_signatured_type,
6686 NULL,
6687 &objfile->objfile_obstack,
6688 hashtab_obstack_allocate,
6689 dummy_obstack_deallocate);
6690 }
6691
6692 /* A helper function to add a signatured type CU to a table. */
6693
6694 static int
6695 add_signatured_type_cu_to_table (void **slot, void *datum)
6696 {
6697 struct signatured_type *sigt = (struct signatured_type *) *slot;
6698 std::vector<signatured_type *> *all_type_units
6699 = (std::vector<signatured_type *> *) datum;
6700
6701 all_type_units->push_back (sigt);
6702
6703 return 1;
6704 }
6705
6706 /* A helper for create_debug_types_hash_table. Read types from SECTION
6707 and fill them into TYPES_HTAB. It will process only type units,
6708 therefore DW_UT_type. */
6709
6710 static void
6711 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6712 struct dwo_file *dwo_file,
6713 dwarf2_section_info *section, htab_t &types_htab,
6714 rcuh_kind section_kind)
6715 {
6716 struct objfile *objfile = dwarf2_per_objfile->objfile;
6717 struct dwarf2_section_info *abbrev_section;
6718 bfd *abfd;
6719 const gdb_byte *info_ptr, *end_ptr;
6720
6721 abbrev_section = (dwo_file != NULL
6722 ? &dwo_file->sections.abbrev
6723 : &dwarf2_per_objfile->abbrev);
6724
6725 if (dwarf_read_debug)
6726 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6727 get_section_name (section),
6728 get_section_file_name (abbrev_section));
6729
6730 dwarf2_read_section (objfile, section);
6731 info_ptr = section->buffer;
6732
6733 if (info_ptr == NULL)
6734 return;
6735
6736 /* We can't set abfd until now because the section may be empty or
6737 not present, in which case the bfd is unknown. */
6738 abfd = get_section_bfd_owner (section);
6739
6740 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6741 because we don't need to read any dies: the signature is in the
6742 header. */
6743
6744 end_ptr = info_ptr + section->size;
6745 while (info_ptr < end_ptr)
6746 {
6747 struct signatured_type *sig_type;
6748 struct dwo_unit *dwo_tu;
6749 void **slot;
6750 const gdb_byte *ptr = info_ptr;
6751 struct comp_unit_head header;
6752 unsigned int length;
6753
6754 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6755
6756 /* Initialize it due to a false compiler warning. */
6757 header.signature = -1;
6758 header.type_cu_offset_in_tu = (cu_offset) -1;
6759
6760 /* We need to read the type's signature in order to build the hash
6761 table, but we don't need anything else just yet. */
6762
6763 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6764 abbrev_section, ptr, section_kind);
6765
6766 length = get_cu_length (&header);
6767
6768 /* Skip dummy type units. */
6769 if (ptr >= info_ptr + length
6770 || peek_abbrev_code (abfd, ptr) == 0
6771 || header.unit_type != DW_UT_type)
6772 {
6773 info_ptr += length;
6774 continue;
6775 }
6776
6777 if (types_htab == NULL)
6778 {
6779 if (dwo_file)
6780 types_htab = allocate_dwo_unit_table (objfile);
6781 else
6782 types_htab = allocate_signatured_type_table (objfile);
6783 }
6784
6785 if (dwo_file)
6786 {
6787 sig_type = NULL;
6788 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6789 struct dwo_unit);
6790 dwo_tu->dwo_file = dwo_file;
6791 dwo_tu->signature = header.signature;
6792 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6793 dwo_tu->section = section;
6794 dwo_tu->sect_off = sect_off;
6795 dwo_tu->length = length;
6796 }
6797 else
6798 {
6799 /* N.B.: type_offset is not usable if this type uses a DWO file.
6800 The real type_offset is in the DWO file. */
6801 dwo_tu = NULL;
6802 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6803 struct signatured_type);
6804 sig_type->signature = header.signature;
6805 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6806 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6807 sig_type->per_cu.is_debug_types = 1;
6808 sig_type->per_cu.section = section;
6809 sig_type->per_cu.sect_off = sect_off;
6810 sig_type->per_cu.length = length;
6811 }
6812
6813 slot = htab_find_slot (types_htab,
6814 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6815 INSERT);
6816 gdb_assert (slot != NULL);
6817 if (*slot != NULL)
6818 {
6819 sect_offset dup_sect_off;
6820
6821 if (dwo_file)
6822 {
6823 const struct dwo_unit *dup_tu
6824 = (const struct dwo_unit *) *slot;
6825
6826 dup_sect_off = dup_tu->sect_off;
6827 }
6828 else
6829 {
6830 const struct signatured_type *dup_tu
6831 = (const struct signatured_type *) *slot;
6832
6833 dup_sect_off = dup_tu->per_cu.sect_off;
6834 }
6835
6836 complaint (_("debug type entry at offset %s is duplicate to"
6837 " the entry at offset %s, signature %s"),
6838 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6839 hex_string (header.signature));
6840 }
6841 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6842
6843 if (dwarf_read_debug > 1)
6844 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6845 sect_offset_str (sect_off),
6846 hex_string (header.signature));
6847
6848 info_ptr += length;
6849 }
6850 }
6851
6852 /* Create the hash table of all entries in the .debug_types
6853 (or .debug_types.dwo) section(s).
6854 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6855 otherwise it is NULL.
6856
6857 The result is a pointer to the hash table or NULL if there are no types.
6858
6859 Note: This function processes DWO files only, not DWP files. */
6860
6861 static void
6862 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6863 struct dwo_file *dwo_file,
6864 gdb::array_view<dwarf2_section_info> type_sections,
6865 htab_t &types_htab)
6866 {
6867 for (dwarf2_section_info &section : type_sections)
6868 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6869 types_htab, rcuh_kind::TYPE);
6870 }
6871
6872 /* Create the hash table of all entries in the .debug_types section,
6873 and initialize all_type_units.
6874 The result is zero if there is an error (e.g. missing .debug_types section),
6875 otherwise non-zero. */
6876
6877 static int
6878 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6879 {
6880 htab_t types_htab = NULL;
6881
6882 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6883 &dwarf2_per_objfile->info, types_htab,
6884 rcuh_kind::COMPILE);
6885 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6886 dwarf2_per_objfile->types, types_htab);
6887 if (types_htab == NULL)
6888 {
6889 dwarf2_per_objfile->signatured_types = NULL;
6890 return 0;
6891 }
6892
6893 dwarf2_per_objfile->signatured_types = types_htab;
6894
6895 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6896 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6897
6898 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6899 &dwarf2_per_objfile->all_type_units);
6900
6901 return 1;
6902 }
6903
6904 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6905 If SLOT is non-NULL, it is the entry to use in the hash table.
6906 Otherwise we find one. */
6907
6908 static struct signatured_type *
6909 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6910 void **slot)
6911 {
6912 struct objfile *objfile = dwarf2_per_objfile->objfile;
6913
6914 if (dwarf2_per_objfile->all_type_units.size ()
6915 == dwarf2_per_objfile->all_type_units.capacity ())
6916 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6917
6918 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6919 struct signatured_type);
6920
6921 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6922 sig_type->signature = sig;
6923 sig_type->per_cu.is_debug_types = 1;
6924 if (dwarf2_per_objfile->using_index)
6925 {
6926 sig_type->per_cu.v.quick =
6927 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6928 struct dwarf2_per_cu_quick_data);
6929 }
6930
6931 if (slot == NULL)
6932 {
6933 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6934 sig_type, INSERT);
6935 }
6936 gdb_assert (*slot == NULL);
6937 *slot = sig_type;
6938 /* The rest of sig_type must be filled in by the caller. */
6939 return sig_type;
6940 }
6941
6942 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6943 Fill in SIG_ENTRY with DWO_ENTRY. */
6944
6945 static void
6946 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6947 struct signatured_type *sig_entry,
6948 struct dwo_unit *dwo_entry)
6949 {
6950 /* Make sure we're not clobbering something we don't expect to. */
6951 gdb_assert (! sig_entry->per_cu.queued);
6952 gdb_assert (sig_entry->per_cu.cu == NULL);
6953 if (dwarf2_per_objfile->using_index)
6954 {
6955 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6956 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6957 }
6958 else
6959 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6960 gdb_assert (sig_entry->signature == dwo_entry->signature);
6961 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6962 gdb_assert (sig_entry->type_unit_group == NULL);
6963 gdb_assert (sig_entry->dwo_unit == NULL);
6964
6965 sig_entry->per_cu.section = dwo_entry->section;
6966 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6967 sig_entry->per_cu.length = dwo_entry->length;
6968 sig_entry->per_cu.reading_dwo_directly = 1;
6969 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6970 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6971 sig_entry->dwo_unit = dwo_entry;
6972 }
6973
6974 /* Subroutine of lookup_signatured_type.
6975 If we haven't read the TU yet, create the signatured_type data structure
6976 for a TU to be read in directly from a DWO file, bypassing the stub.
6977 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6978 using .gdb_index, then when reading a CU we want to stay in the DWO file
6979 containing that CU. Otherwise we could end up reading several other DWO
6980 files (due to comdat folding) to process the transitive closure of all the
6981 mentioned TUs, and that can be slow. The current DWO file will have every
6982 type signature that it needs.
6983 We only do this for .gdb_index because in the psymtab case we already have
6984 to read all the DWOs to build the type unit groups. */
6985
6986 static struct signatured_type *
6987 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6988 {
6989 struct dwarf2_per_objfile *dwarf2_per_objfile
6990 = cu->per_cu->dwarf2_per_objfile;
6991 struct objfile *objfile = dwarf2_per_objfile->objfile;
6992 struct dwo_file *dwo_file;
6993 struct dwo_unit find_dwo_entry, *dwo_entry;
6994 struct signatured_type find_sig_entry, *sig_entry;
6995 void **slot;
6996
6997 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6998
6999 /* If TU skeletons have been removed then we may not have read in any
7000 TUs yet. */
7001 if (dwarf2_per_objfile->signatured_types == NULL)
7002 {
7003 dwarf2_per_objfile->signatured_types
7004 = allocate_signatured_type_table (objfile);
7005 }
7006
7007 /* We only ever need to read in one copy of a signatured type.
7008 Use the global signatured_types array to do our own comdat-folding
7009 of types. If this is the first time we're reading this TU, and
7010 the TU has an entry in .gdb_index, replace the recorded data from
7011 .gdb_index with this TU. */
7012
7013 find_sig_entry.signature = sig;
7014 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7015 &find_sig_entry, INSERT);
7016 sig_entry = (struct signatured_type *) *slot;
7017
7018 /* We can get here with the TU already read, *or* in the process of being
7019 read. Don't reassign the global entry to point to this DWO if that's
7020 the case. Also note that if the TU is already being read, it may not
7021 have come from a DWO, the program may be a mix of Fission-compiled
7022 code and non-Fission-compiled code. */
7023
7024 /* Have we already tried to read this TU?
7025 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7026 needn't exist in the global table yet). */
7027 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7028 return sig_entry;
7029
7030 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7031 dwo_unit of the TU itself. */
7032 dwo_file = cu->dwo_unit->dwo_file;
7033
7034 /* Ok, this is the first time we're reading this TU. */
7035 if (dwo_file->tus == NULL)
7036 return NULL;
7037 find_dwo_entry.signature = sig;
7038 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7039 if (dwo_entry == NULL)
7040 return NULL;
7041
7042 /* If the global table doesn't have an entry for this TU, add one. */
7043 if (sig_entry == NULL)
7044 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7045
7046 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7047 sig_entry->per_cu.tu_read = 1;
7048 return sig_entry;
7049 }
7050
7051 /* Subroutine of lookup_signatured_type.
7052 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7053 then try the DWP file. If the TU stub (skeleton) has been removed then
7054 it won't be in .gdb_index. */
7055
7056 static struct signatured_type *
7057 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7058 {
7059 struct dwarf2_per_objfile *dwarf2_per_objfile
7060 = cu->per_cu->dwarf2_per_objfile;
7061 struct objfile *objfile = dwarf2_per_objfile->objfile;
7062 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7063 struct dwo_unit *dwo_entry;
7064 struct signatured_type find_sig_entry, *sig_entry;
7065 void **slot;
7066
7067 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7068 gdb_assert (dwp_file != NULL);
7069
7070 /* If TU skeletons have been removed then we may not have read in any
7071 TUs yet. */
7072 if (dwarf2_per_objfile->signatured_types == NULL)
7073 {
7074 dwarf2_per_objfile->signatured_types
7075 = allocate_signatured_type_table (objfile);
7076 }
7077
7078 find_sig_entry.signature = sig;
7079 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7080 &find_sig_entry, INSERT);
7081 sig_entry = (struct signatured_type *) *slot;
7082
7083 /* Have we already tried to read this TU?
7084 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7085 needn't exist in the global table yet). */
7086 if (sig_entry != NULL)
7087 return sig_entry;
7088
7089 if (dwp_file->tus == NULL)
7090 return NULL;
7091 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7092 sig, 1 /* is_debug_types */);
7093 if (dwo_entry == NULL)
7094 return NULL;
7095
7096 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7097 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7098
7099 return sig_entry;
7100 }
7101
7102 /* Lookup a signature based type for DW_FORM_ref_sig8.
7103 Returns NULL if signature SIG is not present in the table.
7104 It is up to the caller to complain about this. */
7105
7106 static struct signatured_type *
7107 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7108 {
7109 struct dwarf2_per_objfile *dwarf2_per_objfile
7110 = cu->per_cu->dwarf2_per_objfile;
7111
7112 if (cu->dwo_unit
7113 && dwarf2_per_objfile->using_index)
7114 {
7115 /* We're in a DWO/DWP file, and we're using .gdb_index.
7116 These cases require special processing. */
7117 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7118 return lookup_dwo_signatured_type (cu, sig);
7119 else
7120 return lookup_dwp_signatured_type (cu, sig);
7121 }
7122 else
7123 {
7124 struct signatured_type find_entry, *entry;
7125
7126 if (dwarf2_per_objfile->signatured_types == NULL)
7127 return NULL;
7128 find_entry.signature = sig;
7129 entry = ((struct signatured_type *)
7130 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7131 return entry;
7132 }
7133 }
7134 \f
7135 /* Low level DIE reading support. */
7136
7137 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7138
7139 static void
7140 init_cu_die_reader (struct die_reader_specs *reader,
7141 struct dwarf2_cu *cu,
7142 struct dwarf2_section_info *section,
7143 struct dwo_file *dwo_file,
7144 struct abbrev_table *abbrev_table)
7145 {
7146 gdb_assert (section->readin && section->buffer != NULL);
7147 reader->abfd = get_section_bfd_owner (section);
7148 reader->cu = cu;
7149 reader->dwo_file = dwo_file;
7150 reader->die_section = section;
7151 reader->buffer = section->buffer;
7152 reader->buffer_end = section->buffer + section->size;
7153 reader->comp_dir = NULL;
7154 reader->abbrev_table = abbrev_table;
7155 }
7156
7157 /* Subroutine of init_cutu_and_read_dies to simplify it.
7158 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7159 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7160 already.
7161
7162 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7163 from it to the DIE in the DWO. If NULL we are skipping the stub.
7164 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7165 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7166 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7167 STUB_COMP_DIR may be non-NULL.
7168 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7169 are filled in with the info of the DIE from the DWO file.
7170 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7171 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7172 kept around for at least as long as *RESULT_READER.
7173
7174 The result is non-zero if a valid (non-dummy) DIE was found. */
7175
7176 static int
7177 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7178 struct dwo_unit *dwo_unit,
7179 struct die_info *stub_comp_unit_die,
7180 const char *stub_comp_dir,
7181 struct die_reader_specs *result_reader,
7182 const gdb_byte **result_info_ptr,
7183 struct die_info **result_comp_unit_die,
7184 int *result_has_children,
7185 abbrev_table_up *result_dwo_abbrev_table)
7186 {
7187 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7188 struct objfile *objfile = dwarf2_per_objfile->objfile;
7189 struct dwarf2_cu *cu = this_cu->cu;
7190 bfd *abfd;
7191 const gdb_byte *begin_info_ptr, *info_ptr;
7192 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7193 int i,num_extra_attrs;
7194 struct dwarf2_section_info *dwo_abbrev_section;
7195 struct attribute *attr;
7196 struct die_info *comp_unit_die;
7197
7198 /* At most one of these may be provided. */
7199 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7200
7201 /* These attributes aren't processed until later:
7202 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7203 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7204 referenced later. However, these attributes are found in the stub
7205 which we won't have later. In order to not impose this complication
7206 on the rest of the code, we read them here and copy them to the
7207 DWO CU/TU die. */
7208
7209 stmt_list = NULL;
7210 low_pc = NULL;
7211 high_pc = NULL;
7212 ranges = NULL;
7213 comp_dir = NULL;
7214
7215 if (stub_comp_unit_die != NULL)
7216 {
7217 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7218 DWO file. */
7219 if (! this_cu->is_debug_types)
7220 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7221 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7222 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7223 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7224 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7225
7226 /* There should be a DW_AT_addr_base attribute here (if needed).
7227 We need the value before we can process DW_FORM_GNU_addr_index
7228 or DW_FORM_addrx. */
7229 cu->addr_base = 0;
7230 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7231 if (attr)
7232 cu->addr_base = DW_UNSND (attr);
7233
7234 /* There should be a DW_AT_ranges_base attribute here (if needed).
7235 We need the value before we can process DW_AT_ranges. */
7236 cu->ranges_base = 0;
7237 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7238 if (attr)
7239 cu->ranges_base = DW_UNSND (attr);
7240 }
7241 else if (stub_comp_dir != NULL)
7242 {
7243 /* Reconstruct the comp_dir attribute to simplify the code below. */
7244 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7245 comp_dir->name = DW_AT_comp_dir;
7246 comp_dir->form = DW_FORM_string;
7247 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7248 DW_STRING (comp_dir) = stub_comp_dir;
7249 }
7250
7251 /* Set up for reading the DWO CU/TU. */
7252 cu->dwo_unit = dwo_unit;
7253 dwarf2_section_info *section = dwo_unit->section;
7254 dwarf2_read_section (objfile, section);
7255 abfd = get_section_bfd_owner (section);
7256 begin_info_ptr = info_ptr = (section->buffer
7257 + to_underlying (dwo_unit->sect_off));
7258 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7259
7260 if (this_cu->is_debug_types)
7261 {
7262 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7263
7264 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7265 &cu->header, section,
7266 dwo_abbrev_section,
7267 info_ptr, rcuh_kind::TYPE);
7268 /* This is not an assert because it can be caused by bad debug info. */
7269 if (sig_type->signature != cu->header.signature)
7270 {
7271 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7272 " TU at offset %s [in module %s]"),
7273 hex_string (sig_type->signature),
7274 hex_string (cu->header.signature),
7275 sect_offset_str (dwo_unit->sect_off),
7276 bfd_get_filename (abfd));
7277 }
7278 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7279 /* For DWOs coming from DWP files, we don't know the CU length
7280 nor the type's offset in the TU until now. */
7281 dwo_unit->length = get_cu_length (&cu->header);
7282 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7283
7284 /* Establish the type offset that can be used to lookup the type.
7285 For DWO files, we don't know it until now. */
7286 sig_type->type_offset_in_section
7287 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7288 }
7289 else
7290 {
7291 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7292 &cu->header, section,
7293 dwo_abbrev_section,
7294 info_ptr, rcuh_kind::COMPILE);
7295 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7296 /* For DWOs coming from DWP files, we don't know the CU length
7297 until now. */
7298 dwo_unit->length = get_cu_length (&cu->header);
7299 }
7300
7301 *result_dwo_abbrev_table
7302 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7303 cu->header.abbrev_sect_off);
7304 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7305 result_dwo_abbrev_table->get ());
7306
7307 /* Read in the die, but leave space to copy over the attributes
7308 from the stub. This has the benefit of simplifying the rest of
7309 the code - all the work to maintain the illusion of a single
7310 DW_TAG_{compile,type}_unit DIE is done here. */
7311 num_extra_attrs = ((stmt_list != NULL)
7312 + (low_pc != NULL)
7313 + (high_pc != NULL)
7314 + (ranges != NULL)
7315 + (comp_dir != NULL));
7316 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7317 result_has_children, num_extra_attrs);
7318
7319 /* Copy over the attributes from the stub to the DIE we just read in. */
7320 comp_unit_die = *result_comp_unit_die;
7321 i = comp_unit_die->num_attrs;
7322 if (stmt_list != NULL)
7323 comp_unit_die->attrs[i++] = *stmt_list;
7324 if (low_pc != NULL)
7325 comp_unit_die->attrs[i++] = *low_pc;
7326 if (high_pc != NULL)
7327 comp_unit_die->attrs[i++] = *high_pc;
7328 if (ranges != NULL)
7329 comp_unit_die->attrs[i++] = *ranges;
7330 if (comp_dir != NULL)
7331 comp_unit_die->attrs[i++] = *comp_dir;
7332 comp_unit_die->num_attrs += num_extra_attrs;
7333
7334 if (dwarf_die_debug)
7335 {
7336 fprintf_unfiltered (gdb_stdlog,
7337 "Read die from %s@0x%x of %s:\n",
7338 get_section_name (section),
7339 (unsigned) (begin_info_ptr - section->buffer),
7340 bfd_get_filename (abfd));
7341 dump_die (comp_unit_die, dwarf_die_debug);
7342 }
7343
7344 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7345 TUs by skipping the stub and going directly to the entry in the DWO file.
7346 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7347 to get it via circuitous means. Blech. */
7348 if (comp_dir != NULL)
7349 result_reader->comp_dir = DW_STRING (comp_dir);
7350
7351 /* Skip dummy compilation units. */
7352 if (info_ptr >= begin_info_ptr + dwo_unit->length
7353 || peek_abbrev_code (abfd, info_ptr) == 0)
7354 return 0;
7355
7356 *result_info_ptr = info_ptr;
7357 return 1;
7358 }
7359
7360 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7361 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7362 signature is part of the header. */
7363 static gdb::optional<ULONGEST>
7364 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7365 {
7366 if (cu->header.version >= 5)
7367 return cu->header.signature;
7368 struct attribute *attr;
7369 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7370 if (attr == nullptr)
7371 return gdb::optional<ULONGEST> ();
7372 return DW_UNSND (attr);
7373 }
7374
7375 /* Subroutine of init_cutu_and_read_dies to simplify it.
7376 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7377 Returns NULL if the specified DWO unit cannot be found. */
7378
7379 static struct dwo_unit *
7380 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7381 struct die_info *comp_unit_die)
7382 {
7383 struct dwarf2_cu *cu = this_cu->cu;
7384 struct dwo_unit *dwo_unit;
7385 const char *comp_dir, *dwo_name;
7386
7387 gdb_assert (cu != NULL);
7388
7389 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7390 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7391 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7392
7393 if (this_cu->is_debug_types)
7394 {
7395 struct signatured_type *sig_type;
7396
7397 /* Since this_cu is the first member of struct signatured_type,
7398 we can go from a pointer to one to a pointer to the other. */
7399 sig_type = (struct signatured_type *) this_cu;
7400 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7401 }
7402 else
7403 {
7404 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7405 if (!signature.has_value ())
7406 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7407 " [in module %s]"),
7408 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7409 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7410 *signature);
7411 }
7412
7413 return dwo_unit;
7414 }
7415
7416 /* Subroutine of init_cutu_and_read_dies to simplify it.
7417 See it for a description of the parameters.
7418 Read a TU directly from a DWO file, bypassing the stub. */
7419
7420 static void
7421 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7422 int use_existing_cu, int keep,
7423 die_reader_func_ftype *die_reader_func,
7424 void *data)
7425 {
7426 std::unique_ptr<dwarf2_cu> new_cu;
7427 struct signatured_type *sig_type;
7428 struct die_reader_specs reader;
7429 const gdb_byte *info_ptr;
7430 struct die_info *comp_unit_die;
7431 int has_children;
7432 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7433
7434 /* Verify we can do the following downcast, and that we have the
7435 data we need. */
7436 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7437 sig_type = (struct signatured_type *) this_cu;
7438 gdb_assert (sig_type->dwo_unit != NULL);
7439
7440 if (use_existing_cu && this_cu->cu != NULL)
7441 {
7442 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7443 /* There's no need to do the rereading_dwo_cu handling that
7444 init_cutu_and_read_dies does since we don't read the stub. */
7445 }
7446 else
7447 {
7448 /* If !use_existing_cu, this_cu->cu must be NULL. */
7449 gdb_assert (this_cu->cu == NULL);
7450 new_cu.reset (new dwarf2_cu (this_cu));
7451 }
7452
7453 /* A future optimization, if needed, would be to use an existing
7454 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7455 could share abbrev tables. */
7456
7457 /* The abbreviation table used by READER, this must live at least as long as
7458 READER. */
7459 abbrev_table_up dwo_abbrev_table;
7460
7461 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7462 NULL /* stub_comp_unit_die */,
7463 sig_type->dwo_unit->dwo_file->comp_dir,
7464 &reader, &info_ptr,
7465 &comp_unit_die, &has_children,
7466 &dwo_abbrev_table) == 0)
7467 {
7468 /* Dummy die. */
7469 return;
7470 }
7471
7472 /* All the "real" work is done here. */
7473 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7474
7475 /* This duplicates the code in init_cutu_and_read_dies,
7476 but the alternative is making the latter more complex.
7477 This function is only for the special case of using DWO files directly:
7478 no point in overly complicating the general case just to handle this. */
7479 if (new_cu != NULL && keep)
7480 {
7481 /* Link this CU into read_in_chain. */
7482 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7483 dwarf2_per_objfile->read_in_chain = this_cu;
7484 /* The chain owns it now. */
7485 new_cu.release ();
7486 }
7487 }
7488
7489 /* Initialize a CU (or TU) and read its DIEs.
7490 If the CU defers to a DWO file, read the DWO file as well.
7491
7492 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7493 Otherwise the table specified in the comp unit header is read in and used.
7494 This is an optimization for when we already have the abbrev table.
7495
7496 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7497 Otherwise, a new CU is allocated with xmalloc.
7498
7499 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7500 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7501
7502 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7503 linker) then DIE_READER_FUNC will not get called. */
7504
7505 static void
7506 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7507 struct abbrev_table *abbrev_table,
7508 int use_existing_cu, int keep,
7509 bool skip_partial,
7510 die_reader_func_ftype *die_reader_func,
7511 void *data)
7512 {
7513 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7514 struct objfile *objfile = dwarf2_per_objfile->objfile;
7515 struct dwarf2_section_info *section = this_cu->section;
7516 bfd *abfd = get_section_bfd_owner (section);
7517 struct dwarf2_cu *cu;
7518 const gdb_byte *begin_info_ptr, *info_ptr;
7519 struct die_reader_specs reader;
7520 struct die_info *comp_unit_die;
7521 int has_children;
7522 struct signatured_type *sig_type = NULL;
7523 struct dwarf2_section_info *abbrev_section;
7524 /* Non-zero if CU currently points to a DWO file and we need to
7525 reread it. When this happens we need to reread the skeleton die
7526 before we can reread the DWO file (this only applies to CUs, not TUs). */
7527 int rereading_dwo_cu = 0;
7528
7529 if (dwarf_die_debug)
7530 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7531 this_cu->is_debug_types ? "type" : "comp",
7532 sect_offset_str (this_cu->sect_off));
7533
7534 if (use_existing_cu)
7535 gdb_assert (keep);
7536
7537 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7538 file (instead of going through the stub), short-circuit all of this. */
7539 if (this_cu->reading_dwo_directly)
7540 {
7541 /* Narrow down the scope of possibilities to have to understand. */
7542 gdb_assert (this_cu->is_debug_types);
7543 gdb_assert (abbrev_table == NULL);
7544 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7545 die_reader_func, data);
7546 return;
7547 }
7548
7549 /* This is cheap if the section is already read in. */
7550 dwarf2_read_section (objfile, section);
7551
7552 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7553
7554 abbrev_section = get_abbrev_section_for_cu (this_cu);
7555
7556 std::unique_ptr<dwarf2_cu> new_cu;
7557 if (use_existing_cu && this_cu->cu != NULL)
7558 {
7559 cu = this_cu->cu;
7560 /* If this CU is from a DWO file we need to start over, we need to
7561 refetch the attributes from the skeleton CU.
7562 This could be optimized by retrieving those attributes from when we
7563 were here the first time: the previous comp_unit_die was stored in
7564 comp_unit_obstack. But there's no data yet that we need this
7565 optimization. */
7566 if (cu->dwo_unit != NULL)
7567 rereading_dwo_cu = 1;
7568 }
7569 else
7570 {
7571 /* If !use_existing_cu, this_cu->cu must be NULL. */
7572 gdb_assert (this_cu->cu == NULL);
7573 new_cu.reset (new dwarf2_cu (this_cu));
7574 cu = new_cu.get ();
7575 }
7576
7577 /* Get the header. */
7578 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7579 {
7580 /* We already have the header, there's no need to read it in again. */
7581 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7582 }
7583 else
7584 {
7585 if (this_cu->is_debug_types)
7586 {
7587 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7588 &cu->header, section,
7589 abbrev_section, info_ptr,
7590 rcuh_kind::TYPE);
7591
7592 /* Since per_cu is the first member of struct signatured_type,
7593 we can go from a pointer to one to a pointer to the other. */
7594 sig_type = (struct signatured_type *) this_cu;
7595 gdb_assert (sig_type->signature == cu->header.signature);
7596 gdb_assert (sig_type->type_offset_in_tu
7597 == cu->header.type_cu_offset_in_tu);
7598 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7599
7600 /* LENGTH has not been set yet for type units if we're
7601 using .gdb_index. */
7602 this_cu->length = get_cu_length (&cu->header);
7603
7604 /* Establish the type offset that can be used to lookup the type. */
7605 sig_type->type_offset_in_section =
7606 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7607
7608 this_cu->dwarf_version = cu->header.version;
7609 }
7610 else
7611 {
7612 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7613 &cu->header, section,
7614 abbrev_section,
7615 info_ptr,
7616 rcuh_kind::COMPILE);
7617
7618 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7619 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7620 this_cu->dwarf_version = cu->header.version;
7621 }
7622 }
7623
7624 /* Skip dummy compilation units. */
7625 if (info_ptr >= begin_info_ptr + this_cu->length
7626 || peek_abbrev_code (abfd, info_ptr) == 0)
7627 return;
7628
7629 /* If we don't have them yet, read the abbrevs for this compilation unit.
7630 And if we need to read them now, make sure they're freed when we're
7631 done (own the table through ABBREV_TABLE_HOLDER). */
7632 abbrev_table_up abbrev_table_holder;
7633 if (abbrev_table != NULL)
7634 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7635 else
7636 {
7637 abbrev_table_holder
7638 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7639 cu->header.abbrev_sect_off);
7640 abbrev_table = abbrev_table_holder.get ();
7641 }
7642
7643 /* Read the top level CU/TU die. */
7644 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7645 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7646
7647 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7648 return;
7649
7650 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7651 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7652 table from the DWO file and pass the ownership over to us. It will be
7653 referenced from READER, so we must make sure to free it after we're done
7654 with READER.
7655
7656 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7657 DWO CU, that this test will fail (the attribute will not be present). */
7658 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7659 abbrev_table_up dwo_abbrev_table;
7660 if (dwo_name != nullptr)
7661 {
7662 struct dwo_unit *dwo_unit;
7663 struct die_info *dwo_comp_unit_die;
7664
7665 if (has_children)
7666 {
7667 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7668 " has children (offset %s) [in module %s]"),
7669 sect_offset_str (this_cu->sect_off),
7670 bfd_get_filename (abfd));
7671 }
7672 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7673 if (dwo_unit != NULL)
7674 {
7675 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7676 comp_unit_die, NULL,
7677 &reader, &info_ptr,
7678 &dwo_comp_unit_die, &has_children,
7679 &dwo_abbrev_table) == 0)
7680 {
7681 /* Dummy die. */
7682 return;
7683 }
7684 comp_unit_die = dwo_comp_unit_die;
7685 }
7686 else
7687 {
7688 /* Yikes, we couldn't find the rest of the DIE, we only have
7689 the stub. A complaint has already been logged. There's
7690 not much more we can do except pass on the stub DIE to
7691 die_reader_func. We don't want to throw an error on bad
7692 debug info. */
7693 }
7694 }
7695
7696 /* All of the above is setup for this call. Yikes. */
7697 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7698
7699 /* Done, clean up. */
7700 if (new_cu != NULL && keep)
7701 {
7702 /* Link this CU into read_in_chain. */
7703 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7704 dwarf2_per_objfile->read_in_chain = this_cu;
7705 /* The chain owns it now. */
7706 new_cu.release ();
7707 }
7708 }
7709
7710 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7711 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7712 to have already done the lookup to find the DWO file).
7713
7714 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7715 THIS_CU->is_debug_types, but nothing else.
7716
7717 We fill in THIS_CU->length.
7718
7719 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7720 linker) then DIE_READER_FUNC will not get called.
7721
7722 THIS_CU->cu is always freed when done.
7723 This is done in order to not leave THIS_CU->cu in a state where we have
7724 to care whether it refers to the "main" CU or the DWO CU. */
7725
7726 static void
7727 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7728 struct dwo_file *dwo_file,
7729 die_reader_func_ftype *die_reader_func,
7730 void *data)
7731 {
7732 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7733 struct objfile *objfile = dwarf2_per_objfile->objfile;
7734 struct dwarf2_section_info *section = this_cu->section;
7735 bfd *abfd = get_section_bfd_owner (section);
7736 struct dwarf2_section_info *abbrev_section;
7737 const gdb_byte *begin_info_ptr, *info_ptr;
7738 struct die_reader_specs reader;
7739 struct die_info *comp_unit_die;
7740 int has_children;
7741
7742 if (dwarf_die_debug)
7743 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7744 this_cu->is_debug_types ? "type" : "comp",
7745 sect_offset_str (this_cu->sect_off));
7746
7747 gdb_assert (this_cu->cu == NULL);
7748
7749 abbrev_section = (dwo_file != NULL
7750 ? &dwo_file->sections.abbrev
7751 : get_abbrev_section_for_cu (this_cu));
7752
7753 /* This is cheap if the section is already read in. */
7754 dwarf2_read_section (objfile, section);
7755
7756 struct dwarf2_cu cu (this_cu);
7757
7758 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7759 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7760 &cu.header, section,
7761 abbrev_section, info_ptr,
7762 (this_cu->is_debug_types
7763 ? rcuh_kind::TYPE
7764 : rcuh_kind::COMPILE));
7765
7766 this_cu->length = get_cu_length (&cu.header);
7767
7768 /* Skip dummy compilation units. */
7769 if (info_ptr >= begin_info_ptr + this_cu->length
7770 || peek_abbrev_code (abfd, info_ptr) == 0)
7771 return;
7772
7773 abbrev_table_up abbrev_table
7774 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7775 cu.header.abbrev_sect_off);
7776
7777 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
7778 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7779
7780 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7781 }
7782
7783 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7784 does not lookup the specified DWO file.
7785 This cannot be used to read DWO files.
7786
7787 THIS_CU->cu is always freed when done.
7788 This is done in order to not leave THIS_CU->cu in a state where we have
7789 to care whether it refers to the "main" CU or the DWO CU.
7790 We can revisit this if the data shows there's a performance issue. */
7791
7792 static void
7793 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7794 die_reader_func_ftype *die_reader_func,
7795 void *data)
7796 {
7797 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
7798 }
7799 \f
7800 /* Type Unit Groups.
7801
7802 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7803 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7804 so that all types coming from the same compilation (.o file) are grouped
7805 together. A future step could be to put the types in the same symtab as
7806 the CU the types ultimately came from. */
7807
7808 static hashval_t
7809 hash_type_unit_group (const void *item)
7810 {
7811 const struct type_unit_group *tu_group
7812 = (const struct type_unit_group *) item;
7813
7814 return hash_stmt_list_entry (&tu_group->hash);
7815 }
7816
7817 static int
7818 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7819 {
7820 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7821 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7822
7823 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7824 }
7825
7826 /* Allocate a hash table for type unit groups. */
7827
7828 static htab_t
7829 allocate_type_unit_groups_table (struct objfile *objfile)
7830 {
7831 return htab_create_alloc_ex (3,
7832 hash_type_unit_group,
7833 eq_type_unit_group,
7834 NULL,
7835 &objfile->objfile_obstack,
7836 hashtab_obstack_allocate,
7837 dummy_obstack_deallocate);
7838 }
7839
7840 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7841 partial symtabs. We combine several TUs per psymtab to not let the size
7842 of any one psymtab grow too big. */
7843 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7844 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7845
7846 /* Helper routine for get_type_unit_group.
7847 Create the type_unit_group object used to hold one or more TUs. */
7848
7849 static struct type_unit_group *
7850 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7851 {
7852 struct dwarf2_per_objfile *dwarf2_per_objfile
7853 = cu->per_cu->dwarf2_per_objfile;
7854 struct objfile *objfile = dwarf2_per_objfile->objfile;
7855 struct dwarf2_per_cu_data *per_cu;
7856 struct type_unit_group *tu_group;
7857
7858 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7859 struct type_unit_group);
7860 per_cu = &tu_group->per_cu;
7861 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7862
7863 if (dwarf2_per_objfile->using_index)
7864 {
7865 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7866 struct dwarf2_per_cu_quick_data);
7867 }
7868 else
7869 {
7870 unsigned int line_offset = to_underlying (line_offset_struct);
7871 struct partial_symtab *pst;
7872 std::string name;
7873
7874 /* Give the symtab a useful name for debug purposes. */
7875 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7876 name = string_printf ("<type_units_%d>",
7877 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7878 else
7879 name = string_printf ("<type_units_at_0x%x>", line_offset);
7880
7881 pst = create_partial_symtab (per_cu, name.c_str ());
7882 pst->anonymous = 1;
7883 }
7884
7885 tu_group->hash.dwo_unit = cu->dwo_unit;
7886 tu_group->hash.line_sect_off = line_offset_struct;
7887
7888 return tu_group;
7889 }
7890
7891 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7892 STMT_LIST is a DW_AT_stmt_list attribute. */
7893
7894 static struct type_unit_group *
7895 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7896 {
7897 struct dwarf2_per_objfile *dwarf2_per_objfile
7898 = cu->per_cu->dwarf2_per_objfile;
7899 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7900 struct type_unit_group *tu_group;
7901 void **slot;
7902 unsigned int line_offset;
7903 struct type_unit_group type_unit_group_for_lookup;
7904
7905 if (dwarf2_per_objfile->type_unit_groups == NULL)
7906 {
7907 dwarf2_per_objfile->type_unit_groups =
7908 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7909 }
7910
7911 /* Do we need to create a new group, or can we use an existing one? */
7912
7913 if (stmt_list)
7914 {
7915 line_offset = DW_UNSND (stmt_list);
7916 ++tu_stats->nr_symtab_sharers;
7917 }
7918 else
7919 {
7920 /* Ugh, no stmt_list. Rare, but we have to handle it.
7921 We can do various things here like create one group per TU or
7922 spread them over multiple groups to split up the expansion work.
7923 To avoid worst case scenarios (too many groups or too large groups)
7924 we, umm, group them in bunches. */
7925 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7926 | (tu_stats->nr_stmt_less_type_units
7927 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7928 ++tu_stats->nr_stmt_less_type_units;
7929 }
7930
7931 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7932 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7933 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7934 &type_unit_group_for_lookup, INSERT);
7935 if (*slot != NULL)
7936 {
7937 tu_group = (struct type_unit_group *) *slot;
7938 gdb_assert (tu_group != NULL);
7939 }
7940 else
7941 {
7942 sect_offset line_offset_struct = (sect_offset) line_offset;
7943 tu_group = create_type_unit_group (cu, line_offset_struct);
7944 *slot = tu_group;
7945 ++tu_stats->nr_symtabs;
7946 }
7947
7948 return tu_group;
7949 }
7950 \f
7951 /* Partial symbol tables. */
7952
7953 /* Create a psymtab named NAME and assign it to PER_CU.
7954
7955 The caller must fill in the following details:
7956 dirname, textlow, texthigh. */
7957
7958 static struct partial_symtab *
7959 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7960 {
7961 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7962 struct partial_symtab *pst;
7963
7964 pst = start_psymtab_common (objfile, name, 0);
7965
7966 pst->psymtabs_addrmap_supported = 1;
7967
7968 /* This is the glue that links PST into GDB's symbol API. */
7969 pst->read_symtab_private = per_cu;
7970 pst->read_symtab = dwarf2_read_symtab;
7971 per_cu->v.psymtab = pst;
7972
7973 return pst;
7974 }
7975
7976 /* The DATA object passed to process_psymtab_comp_unit_reader has this
7977 type. */
7978
7979 struct process_psymtab_comp_unit_data
7980 {
7981 /* True if we are reading a DW_TAG_partial_unit. */
7982
7983 int want_partial_unit;
7984
7985 /* The "pretend" language that is used if the CU doesn't declare a
7986 language. */
7987
7988 enum language pretend_language;
7989 };
7990
7991 /* die_reader_func for process_psymtab_comp_unit. */
7992
7993 static void
7994 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7995 const gdb_byte *info_ptr,
7996 struct die_info *comp_unit_die,
7997 int has_children,
7998 void *data)
7999 {
8000 struct dwarf2_cu *cu = reader->cu;
8001 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8002 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8003 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8004 CORE_ADDR baseaddr;
8005 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8006 struct partial_symtab *pst;
8007 enum pc_bounds_kind cu_bounds_kind;
8008 const char *filename;
8009 struct process_psymtab_comp_unit_data *info
8010 = (struct process_psymtab_comp_unit_data *) data;
8011
8012 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8013 return;
8014
8015 gdb_assert (! per_cu->is_debug_types);
8016
8017 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8018
8019 /* Allocate a new partial symbol table structure. */
8020 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8021 if (filename == NULL)
8022 filename = "";
8023
8024 pst = create_partial_symtab (per_cu, filename);
8025
8026 /* This must be done before calling dwarf2_build_include_psymtabs. */
8027 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8028
8029 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8030
8031 dwarf2_find_base_address (comp_unit_die, cu);
8032
8033 /* Possibly set the default values of LOWPC and HIGHPC from
8034 `DW_AT_ranges'. */
8035 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8036 &best_highpc, cu, pst);
8037 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8038 {
8039 CORE_ADDR low
8040 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8041 - baseaddr);
8042 CORE_ADDR high
8043 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8044 - baseaddr - 1);
8045 /* Store the contiguous range if it is not empty; it can be
8046 empty for CUs with no code. */
8047 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8048 low, high, pst);
8049 }
8050
8051 /* Check if comp unit has_children.
8052 If so, read the rest of the partial symbols from this comp unit.
8053 If not, there's no more debug_info for this comp unit. */
8054 if (has_children)
8055 {
8056 struct partial_die_info *first_die;
8057 CORE_ADDR lowpc, highpc;
8058
8059 lowpc = ((CORE_ADDR) -1);
8060 highpc = ((CORE_ADDR) 0);
8061
8062 first_die = load_partial_dies (reader, info_ptr, 1);
8063
8064 scan_partial_symbols (first_die, &lowpc, &highpc,
8065 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8066
8067 /* If we didn't find a lowpc, set it to highpc to avoid
8068 complaints from `maint check'. */
8069 if (lowpc == ((CORE_ADDR) -1))
8070 lowpc = highpc;
8071
8072 /* If the compilation unit didn't have an explicit address range,
8073 then use the information extracted from its child dies. */
8074 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8075 {
8076 best_lowpc = lowpc;
8077 best_highpc = highpc;
8078 }
8079 }
8080 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
8081 best_lowpc + baseaddr)
8082 - baseaddr);
8083 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
8084 best_highpc + baseaddr)
8085 - baseaddr);
8086
8087 end_psymtab_common (objfile, pst);
8088
8089 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8090 {
8091 int i;
8092 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8093 struct dwarf2_per_cu_data *iter;
8094
8095 /* Fill in 'dependencies' here; we fill in 'users' in a
8096 post-pass. */
8097 pst->number_of_dependencies = len;
8098 pst->dependencies
8099 = objfile->partial_symtabs->allocate_dependencies (len);
8100 for (i = 0;
8101 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8102 i, iter);
8103 ++i)
8104 pst->dependencies[i] = iter->v.psymtab;
8105
8106 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8107 }
8108
8109 /* Get the list of files included in the current compilation unit,
8110 and build a psymtab for each of them. */
8111 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8112
8113 if (dwarf_read_debug)
8114 fprintf_unfiltered (gdb_stdlog,
8115 "Psymtab for %s unit @%s: %s - %s"
8116 ", %d global, %d static syms\n",
8117 per_cu->is_debug_types ? "type" : "comp",
8118 sect_offset_str (per_cu->sect_off),
8119 paddress (gdbarch, pst->text_low (objfile)),
8120 paddress (gdbarch, pst->text_high (objfile)),
8121 pst->n_global_syms, pst->n_static_syms);
8122 }
8123
8124 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8125 Process compilation unit THIS_CU for a psymtab. */
8126
8127 static void
8128 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8129 int want_partial_unit,
8130 enum language pretend_language)
8131 {
8132 /* If this compilation unit was already read in, free the
8133 cached copy in order to read it in again. This is
8134 necessary because we skipped some symbols when we first
8135 read in the compilation unit (see load_partial_dies).
8136 This problem could be avoided, but the benefit is unclear. */
8137 if (this_cu->cu != NULL)
8138 free_one_cached_comp_unit (this_cu);
8139
8140 if (this_cu->is_debug_types)
8141 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8142 build_type_psymtabs_reader, NULL);
8143 else
8144 {
8145 process_psymtab_comp_unit_data info;
8146 info.want_partial_unit = want_partial_unit;
8147 info.pretend_language = pretend_language;
8148 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8149 process_psymtab_comp_unit_reader, &info);
8150 }
8151
8152 /* Age out any secondary CUs. */
8153 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8154 }
8155
8156 /* Reader function for build_type_psymtabs. */
8157
8158 static void
8159 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8160 const gdb_byte *info_ptr,
8161 struct die_info *type_unit_die,
8162 int has_children,
8163 void *data)
8164 {
8165 struct dwarf2_per_objfile *dwarf2_per_objfile
8166 = reader->cu->per_cu->dwarf2_per_objfile;
8167 struct objfile *objfile = dwarf2_per_objfile->objfile;
8168 struct dwarf2_cu *cu = reader->cu;
8169 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8170 struct signatured_type *sig_type;
8171 struct type_unit_group *tu_group;
8172 struct attribute *attr;
8173 struct partial_die_info *first_die;
8174 CORE_ADDR lowpc, highpc;
8175 struct partial_symtab *pst;
8176
8177 gdb_assert (data == NULL);
8178 gdb_assert (per_cu->is_debug_types);
8179 sig_type = (struct signatured_type *) per_cu;
8180
8181 if (! has_children)
8182 return;
8183
8184 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8185 tu_group = get_type_unit_group (cu, attr);
8186
8187 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8188
8189 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8190 pst = create_partial_symtab (per_cu, "");
8191 pst->anonymous = 1;
8192
8193 first_die = load_partial_dies (reader, info_ptr, 1);
8194
8195 lowpc = (CORE_ADDR) -1;
8196 highpc = (CORE_ADDR) 0;
8197 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8198
8199 end_psymtab_common (objfile, pst);
8200 }
8201
8202 /* Struct used to sort TUs by their abbreviation table offset. */
8203
8204 struct tu_abbrev_offset
8205 {
8206 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8207 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8208 {}
8209
8210 signatured_type *sig_type;
8211 sect_offset abbrev_offset;
8212 };
8213
8214 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8215
8216 static bool
8217 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8218 const struct tu_abbrev_offset &b)
8219 {
8220 return a.abbrev_offset < b.abbrev_offset;
8221 }
8222
8223 /* Efficiently read all the type units.
8224 This does the bulk of the work for build_type_psymtabs.
8225
8226 The efficiency is because we sort TUs by the abbrev table they use and
8227 only read each abbrev table once. In one program there are 200K TUs
8228 sharing 8K abbrev tables.
8229
8230 The main purpose of this function is to support building the
8231 dwarf2_per_objfile->type_unit_groups table.
8232 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8233 can collapse the search space by grouping them by stmt_list.
8234 The savings can be significant, in the same program from above the 200K TUs
8235 share 8K stmt_list tables.
8236
8237 FUNC is expected to call get_type_unit_group, which will create the
8238 struct type_unit_group if necessary and add it to
8239 dwarf2_per_objfile->type_unit_groups. */
8240
8241 static void
8242 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8243 {
8244 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8245 abbrev_table_up abbrev_table;
8246 sect_offset abbrev_offset;
8247
8248 /* It's up to the caller to not call us multiple times. */
8249 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8250
8251 if (dwarf2_per_objfile->all_type_units.empty ())
8252 return;
8253
8254 /* TUs typically share abbrev tables, and there can be way more TUs than
8255 abbrev tables. Sort by abbrev table to reduce the number of times we
8256 read each abbrev table in.
8257 Alternatives are to punt or to maintain a cache of abbrev tables.
8258 This is simpler and efficient enough for now.
8259
8260 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8261 symtab to use). Typically TUs with the same abbrev offset have the same
8262 stmt_list value too so in practice this should work well.
8263
8264 The basic algorithm here is:
8265
8266 sort TUs by abbrev table
8267 for each TU with same abbrev table:
8268 read abbrev table if first user
8269 read TU top level DIE
8270 [IWBN if DWO skeletons had DW_AT_stmt_list]
8271 call FUNC */
8272
8273 if (dwarf_read_debug)
8274 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8275
8276 /* Sort in a separate table to maintain the order of all_type_units
8277 for .gdb_index: TU indices directly index all_type_units. */
8278 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8279 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8280
8281 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8282 sorted_by_abbrev.emplace_back
8283 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8284 sig_type->per_cu.section,
8285 sig_type->per_cu.sect_off));
8286
8287 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8288 sort_tu_by_abbrev_offset);
8289
8290 abbrev_offset = (sect_offset) ~(unsigned) 0;
8291
8292 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8293 {
8294 /* Switch to the next abbrev table if necessary. */
8295 if (abbrev_table == NULL
8296 || tu.abbrev_offset != abbrev_offset)
8297 {
8298 abbrev_offset = tu.abbrev_offset;
8299 abbrev_table =
8300 abbrev_table_read_table (dwarf2_per_objfile,
8301 &dwarf2_per_objfile->abbrev,
8302 abbrev_offset);
8303 ++tu_stats->nr_uniq_abbrev_tables;
8304 }
8305
8306 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
8307 0, 0, false, build_type_psymtabs_reader, NULL);
8308 }
8309 }
8310
8311 /* Print collected type unit statistics. */
8312
8313 static void
8314 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8315 {
8316 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8317
8318 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8319 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8320 dwarf2_per_objfile->all_type_units.size ());
8321 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8322 tu_stats->nr_uniq_abbrev_tables);
8323 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8324 tu_stats->nr_symtabs);
8325 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8326 tu_stats->nr_symtab_sharers);
8327 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8328 tu_stats->nr_stmt_less_type_units);
8329 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8330 tu_stats->nr_all_type_units_reallocs);
8331 }
8332
8333 /* Traversal function for build_type_psymtabs. */
8334
8335 static int
8336 build_type_psymtab_dependencies (void **slot, void *info)
8337 {
8338 struct dwarf2_per_objfile *dwarf2_per_objfile
8339 = (struct dwarf2_per_objfile *) info;
8340 struct objfile *objfile = dwarf2_per_objfile->objfile;
8341 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8342 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8343 struct partial_symtab *pst = per_cu->v.psymtab;
8344 int len = VEC_length (sig_type_ptr, tu_group->tus);
8345 struct signatured_type *iter;
8346 int i;
8347
8348 gdb_assert (len > 0);
8349 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8350
8351 pst->number_of_dependencies = len;
8352 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8353 for (i = 0;
8354 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8355 ++i)
8356 {
8357 gdb_assert (iter->per_cu.is_debug_types);
8358 pst->dependencies[i] = iter->per_cu.v.psymtab;
8359 iter->type_unit_group = tu_group;
8360 }
8361
8362 VEC_free (sig_type_ptr, tu_group->tus);
8363
8364 return 1;
8365 }
8366
8367 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8368 Build partial symbol tables for the .debug_types comp-units. */
8369
8370 static void
8371 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8372 {
8373 if (! create_all_type_units (dwarf2_per_objfile))
8374 return;
8375
8376 build_type_psymtabs_1 (dwarf2_per_objfile);
8377 }
8378
8379 /* Traversal function for process_skeletonless_type_unit.
8380 Read a TU in a DWO file and build partial symbols for it. */
8381
8382 static int
8383 process_skeletonless_type_unit (void **slot, void *info)
8384 {
8385 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8386 struct dwarf2_per_objfile *dwarf2_per_objfile
8387 = (struct dwarf2_per_objfile *) info;
8388 struct signatured_type find_entry, *entry;
8389
8390 /* If this TU doesn't exist in the global table, add it and read it in. */
8391
8392 if (dwarf2_per_objfile->signatured_types == NULL)
8393 {
8394 dwarf2_per_objfile->signatured_types
8395 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8396 }
8397
8398 find_entry.signature = dwo_unit->signature;
8399 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8400 INSERT);
8401 /* If we've already seen this type there's nothing to do. What's happening
8402 is we're doing our own version of comdat-folding here. */
8403 if (*slot != NULL)
8404 return 1;
8405
8406 /* This does the job that create_all_type_units would have done for
8407 this TU. */
8408 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8409 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8410 *slot = entry;
8411
8412 /* This does the job that build_type_psymtabs_1 would have done. */
8413 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
8414 build_type_psymtabs_reader, NULL);
8415
8416 return 1;
8417 }
8418
8419 /* Traversal function for process_skeletonless_type_units. */
8420
8421 static int
8422 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8423 {
8424 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8425
8426 if (dwo_file->tus != NULL)
8427 {
8428 htab_traverse_noresize (dwo_file->tus,
8429 process_skeletonless_type_unit, info);
8430 }
8431
8432 return 1;
8433 }
8434
8435 /* Scan all TUs of DWO files, verifying we've processed them.
8436 This is needed in case a TU was emitted without its skeleton.
8437 Note: This can't be done until we know what all the DWO files are. */
8438
8439 static void
8440 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8441 {
8442 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8443 if (get_dwp_file (dwarf2_per_objfile) == NULL
8444 && dwarf2_per_objfile->dwo_files != NULL)
8445 {
8446 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
8447 process_dwo_file_for_skeletonless_type_units,
8448 dwarf2_per_objfile);
8449 }
8450 }
8451
8452 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8453
8454 static void
8455 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8456 {
8457 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8458 {
8459 struct partial_symtab *pst = per_cu->v.psymtab;
8460
8461 if (pst == NULL)
8462 continue;
8463
8464 for (int j = 0; j < pst->number_of_dependencies; ++j)
8465 {
8466 /* Set the 'user' field only if it is not already set. */
8467 if (pst->dependencies[j]->user == NULL)
8468 pst->dependencies[j]->user = pst;
8469 }
8470 }
8471 }
8472
8473 /* Build the partial symbol table by doing a quick pass through the
8474 .debug_info and .debug_abbrev sections. */
8475
8476 static void
8477 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8478 {
8479 struct objfile *objfile = dwarf2_per_objfile->objfile;
8480
8481 if (dwarf_read_debug)
8482 {
8483 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8484 objfile_name (objfile));
8485 }
8486
8487 dwarf2_per_objfile->reading_partial_symbols = 1;
8488
8489 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8490
8491 /* Any cached compilation units will be linked by the per-objfile
8492 read_in_chain. Make sure to free them when we're done. */
8493 free_cached_comp_units freer (dwarf2_per_objfile);
8494
8495 build_type_psymtabs (dwarf2_per_objfile);
8496
8497 create_all_comp_units (dwarf2_per_objfile);
8498
8499 /* Create a temporary address map on a temporary obstack. We later
8500 copy this to the final obstack. */
8501 auto_obstack temp_obstack;
8502
8503 scoped_restore save_psymtabs_addrmap
8504 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8505 addrmap_create_mutable (&temp_obstack));
8506
8507 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8508 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8509
8510 /* This has to wait until we read the CUs, we need the list of DWOs. */
8511 process_skeletonless_type_units (dwarf2_per_objfile);
8512
8513 /* Now that all TUs have been processed we can fill in the dependencies. */
8514 if (dwarf2_per_objfile->type_unit_groups != NULL)
8515 {
8516 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8517 build_type_psymtab_dependencies, dwarf2_per_objfile);
8518 }
8519
8520 if (dwarf_read_debug)
8521 print_tu_stats (dwarf2_per_objfile);
8522
8523 set_partial_user (dwarf2_per_objfile);
8524
8525 objfile->partial_symtabs->psymtabs_addrmap
8526 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8527 objfile->partial_symtabs->obstack ());
8528 /* At this point we want to keep the address map. */
8529 save_psymtabs_addrmap.release ();
8530
8531 if (dwarf_read_debug)
8532 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8533 objfile_name (objfile));
8534 }
8535
8536 /* die_reader_func for load_partial_comp_unit. */
8537
8538 static void
8539 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8540 const gdb_byte *info_ptr,
8541 struct die_info *comp_unit_die,
8542 int has_children,
8543 void *data)
8544 {
8545 struct dwarf2_cu *cu = reader->cu;
8546
8547 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8548
8549 /* Check if comp unit has_children.
8550 If so, read the rest of the partial symbols from this comp unit.
8551 If not, there's no more debug_info for this comp unit. */
8552 if (has_children)
8553 load_partial_dies (reader, info_ptr, 0);
8554 }
8555
8556 /* Load the partial DIEs for a secondary CU into memory.
8557 This is also used when rereading a primary CU with load_all_dies. */
8558
8559 static void
8560 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8561 {
8562 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
8563 load_partial_comp_unit_reader, NULL);
8564 }
8565
8566 static void
8567 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8568 struct dwarf2_section_info *section,
8569 struct dwarf2_section_info *abbrev_section,
8570 unsigned int is_dwz)
8571 {
8572 const gdb_byte *info_ptr;
8573 struct objfile *objfile = dwarf2_per_objfile->objfile;
8574
8575 if (dwarf_read_debug)
8576 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8577 get_section_name (section),
8578 get_section_file_name (section));
8579
8580 dwarf2_read_section (objfile, section);
8581
8582 info_ptr = section->buffer;
8583
8584 while (info_ptr < section->buffer + section->size)
8585 {
8586 struct dwarf2_per_cu_data *this_cu;
8587
8588 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8589
8590 comp_unit_head cu_header;
8591 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8592 abbrev_section, info_ptr,
8593 rcuh_kind::COMPILE);
8594
8595 /* Save the compilation unit for later lookup. */
8596 if (cu_header.unit_type != DW_UT_type)
8597 {
8598 this_cu = XOBNEW (&objfile->objfile_obstack,
8599 struct dwarf2_per_cu_data);
8600 memset (this_cu, 0, sizeof (*this_cu));
8601 }
8602 else
8603 {
8604 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8605 struct signatured_type);
8606 memset (sig_type, 0, sizeof (*sig_type));
8607 sig_type->signature = cu_header.signature;
8608 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8609 this_cu = &sig_type->per_cu;
8610 }
8611 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8612 this_cu->sect_off = sect_off;
8613 this_cu->length = cu_header.length + cu_header.initial_length_size;
8614 this_cu->is_dwz = is_dwz;
8615 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8616 this_cu->section = section;
8617
8618 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8619
8620 info_ptr = info_ptr + this_cu->length;
8621 }
8622 }
8623
8624 /* Create a list of all compilation units in OBJFILE.
8625 This is only done for -readnow and building partial symtabs. */
8626
8627 static void
8628 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8629 {
8630 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8631 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8632 &dwarf2_per_objfile->abbrev, 0);
8633
8634 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8635 if (dwz != NULL)
8636 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8637 1);
8638 }
8639
8640 /* Process all loaded DIEs for compilation unit CU, starting at
8641 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8642 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8643 DW_AT_ranges). See the comments of add_partial_subprogram on how
8644 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8645
8646 static void
8647 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8648 CORE_ADDR *highpc, int set_addrmap,
8649 struct dwarf2_cu *cu)
8650 {
8651 struct partial_die_info *pdi;
8652
8653 /* Now, march along the PDI's, descending into ones which have
8654 interesting children but skipping the children of the other ones,
8655 until we reach the end of the compilation unit. */
8656
8657 pdi = first_die;
8658
8659 while (pdi != NULL)
8660 {
8661 pdi->fixup (cu);
8662
8663 /* Anonymous namespaces or modules have no name but have interesting
8664 children, so we need to look at them. Ditto for anonymous
8665 enums. */
8666
8667 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8668 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8669 || pdi->tag == DW_TAG_imported_unit
8670 || pdi->tag == DW_TAG_inlined_subroutine)
8671 {
8672 switch (pdi->tag)
8673 {
8674 case DW_TAG_subprogram:
8675 case DW_TAG_inlined_subroutine:
8676 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8677 break;
8678 case DW_TAG_constant:
8679 case DW_TAG_variable:
8680 case DW_TAG_typedef:
8681 case DW_TAG_union_type:
8682 if (!pdi->is_declaration)
8683 {
8684 add_partial_symbol (pdi, cu);
8685 }
8686 break;
8687 case DW_TAG_class_type:
8688 case DW_TAG_interface_type:
8689 case DW_TAG_structure_type:
8690 if (!pdi->is_declaration)
8691 {
8692 add_partial_symbol (pdi, cu);
8693 }
8694 if ((cu->language == language_rust
8695 || cu->language == language_cplus) && pdi->has_children)
8696 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8697 set_addrmap, cu);
8698 break;
8699 case DW_TAG_enumeration_type:
8700 if (!pdi->is_declaration)
8701 add_partial_enumeration (pdi, cu);
8702 break;
8703 case DW_TAG_base_type:
8704 case DW_TAG_subrange_type:
8705 /* File scope base type definitions are added to the partial
8706 symbol table. */
8707 add_partial_symbol (pdi, cu);
8708 break;
8709 case DW_TAG_namespace:
8710 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8711 break;
8712 case DW_TAG_module:
8713 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8714 break;
8715 case DW_TAG_imported_unit:
8716 {
8717 struct dwarf2_per_cu_data *per_cu;
8718
8719 /* For now we don't handle imported units in type units. */
8720 if (cu->per_cu->is_debug_types)
8721 {
8722 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8723 " supported in type units [in module %s]"),
8724 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8725 }
8726
8727 per_cu = dwarf2_find_containing_comp_unit
8728 (pdi->d.sect_off, pdi->is_dwz,
8729 cu->per_cu->dwarf2_per_objfile);
8730
8731 /* Go read the partial unit, if needed. */
8732 if (per_cu->v.psymtab == NULL)
8733 process_psymtab_comp_unit (per_cu, 1, cu->language);
8734
8735 VEC_safe_push (dwarf2_per_cu_ptr,
8736 cu->per_cu->imported_symtabs, per_cu);
8737 }
8738 break;
8739 case DW_TAG_imported_declaration:
8740 add_partial_symbol (pdi, cu);
8741 break;
8742 default:
8743 break;
8744 }
8745 }
8746
8747 /* If the die has a sibling, skip to the sibling. */
8748
8749 pdi = pdi->die_sibling;
8750 }
8751 }
8752
8753 /* Functions used to compute the fully scoped name of a partial DIE.
8754
8755 Normally, this is simple. For C++, the parent DIE's fully scoped
8756 name is concatenated with "::" and the partial DIE's name.
8757 Enumerators are an exception; they use the scope of their parent
8758 enumeration type, i.e. the name of the enumeration type is not
8759 prepended to the enumerator.
8760
8761 There are two complexities. One is DW_AT_specification; in this
8762 case "parent" means the parent of the target of the specification,
8763 instead of the direct parent of the DIE. The other is compilers
8764 which do not emit DW_TAG_namespace; in this case we try to guess
8765 the fully qualified name of structure types from their members'
8766 linkage names. This must be done using the DIE's children rather
8767 than the children of any DW_AT_specification target. We only need
8768 to do this for structures at the top level, i.e. if the target of
8769 any DW_AT_specification (if any; otherwise the DIE itself) does not
8770 have a parent. */
8771
8772 /* Compute the scope prefix associated with PDI's parent, in
8773 compilation unit CU. The result will be allocated on CU's
8774 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8775 field. NULL is returned if no prefix is necessary. */
8776 static const char *
8777 partial_die_parent_scope (struct partial_die_info *pdi,
8778 struct dwarf2_cu *cu)
8779 {
8780 const char *grandparent_scope;
8781 struct partial_die_info *parent, *real_pdi;
8782
8783 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8784 then this means the parent of the specification DIE. */
8785
8786 real_pdi = pdi;
8787 while (real_pdi->has_specification)
8788 {
8789 auto res = find_partial_die (real_pdi->spec_offset,
8790 real_pdi->spec_is_dwz, cu);
8791 real_pdi = res.pdi;
8792 cu = res.cu;
8793 }
8794
8795 parent = real_pdi->die_parent;
8796 if (parent == NULL)
8797 return NULL;
8798
8799 if (parent->scope_set)
8800 return parent->scope;
8801
8802 parent->fixup (cu);
8803
8804 grandparent_scope = partial_die_parent_scope (parent, cu);
8805
8806 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8807 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8808 Work around this problem here. */
8809 if (cu->language == language_cplus
8810 && parent->tag == DW_TAG_namespace
8811 && strcmp (parent->name, "::") == 0
8812 && grandparent_scope == NULL)
8813 {
8814 parent->scope = NULL;
8815 parent->scope_set = 1;
8816 return NULL;
8817 }
8818
8819 if (pdi->tag == DW_TAG_enumerator)
8820 /* Enumerators should not get the name of the enumeration as a prefix. */
8821 parent->scope = grandparent_scope;
8822 else if (parent->tag == DW_TAG_namespace
8823 || parent->tag == DW_TAG_module
8824 || parent->tag == DW_TAG_structure_type
8825 || parent->tag == DW_TAG_class_type
8826 || parent->tag == DW_TAG_interface_type
8827 || parent->tag == DW_TAG_union_type
8828 || parent->tag == DW_TAG_enumeration_type)
8829 {
8830 if (grandparent_scope == NULL)
8831 parent->scope = parent->name;
8832 else
8833 parent->scope = typename_concat (&cu->comp_unit_obstack,
8834 grandparent_scope,
8835 parent->name, 0, cu);
8836 }
8837 else
8838 {
8839 /* FIXME drow/2004-04-01: What should we be doing with
8840 function-local names? For partial symbols, we should probably be
8841 ignoring them. */
8842 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8843 dwarf_tag_name (parent->tag),
8844 sect_offset_str (pdi->sect_off));
8845 parent->scope = grandparent_scope;
8846 }
8847
8848 parent->scope_set = 1;
8849 return parent->scope;
8850 }
8851
8852 /* Return the fully scoped name associated with PDI, from compilation unit
8853 CU. The result will be allocated with malloc. */
8854
8855 static char *
8856 partial_die_full_name (struct partial_die_info *pdi,
8857 struct dwarf2_cu *cu)
8858 {
8859 const char *parent_scope;
8860
8861 /* If this is a template instantiation, we can not work out the
8862 template arguments from partial DIEs. So, unfortunately, we have
8863 to go through the full DIEs. At least any work we do building
8864 types here will be reused if full symbols are loaded later. */
8865 if (pdi->has_template_arguments)
8866 {
8867 pdi->fixup (cu);
8868
8869 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8870 {
8871 struct die_info *die;
8872 struct attribute attr;
8873 struct dwarf2_cu *ref_cu = cu;
8874
8875 /* DW_FORM_ref_addr is using section offset. */
8876 attr.name = (enum dwarf_attribute) 0;
8877 attr.form = DW_FORM_ref_addr;
8878 attr.u.unsnd = to_underlying (pdi->sect_off);
8879 die = follow_die_ref (NULL, &attr, &ref_cu);
8880
8881 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8882 }
8883 }
8884
8885 parent_scope = partial_die_parent_scope (pdi, cu);
8886 if (parent_scope == NULL)
8887 return NULL;
8888 else
8889 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
8890 }
8891
8892 static void
8893 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8894 {
8895 struct dwarf2_per_objfile *dwarf2_per_objfile
8896 = cu->per_cu->dwarf2_per_objfile;
8897 struct objfile *objfile = dwarf2_per_objfile->objfile;
8898 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8899 CORE_ADDR addr = 0;
8900 const char *actual_name = NULL;
8901 CORE_ADDR baseaddr;
8902 char *built_actual_name;
8903
8904 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8905
8906 built_actual_name = partial_die_full_name (pdi, cu);
8907 if (built_actual_name != NULL)
8908 actual_name = built_actual_name;
8909
8910 if (actual_name == NULL)
8911 actual_name = pdi->name;
8912
8913 switch (pdi->tag)
8914 {
8915 case DW_TAG_inlined_subroutine:
8916 case DW_TAG_subprogram:
8917 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8918 - baseaddr);
8919 if (pdi->is_external || cu->language == language_ada)
8920 {
8921 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8922 of the global scope. But in Ada, we want to be able to access
8923 nested procedures globally. So all Ada subprograms are stored
8924 in the global scope. */
8925 add_psymbol_to_list (actual_name, strlen (actual_name),
8926 built_actual_name != NULL,
8927 VAR_DOMAIN, LOC_BLOCK,
8928 SECT_OFF_TEXT (objfile),
8929 psymbol_placement::GLOBAL,
8930 addr,
8931 cu->language, objfile);
8932 }
8933 else
8934 {
8935 add_psymbol_to_list (actual_name, strlen (actual_name),
8936 built_actual_name != NULL,
8937 VAR_DOMAIN, LOC_BLOCK,
8938 SECT_OFF_TEXT (objfile),
8939 psymbol_placement::STATIC,
8940 addr, cu->language, objfile);
8941 }
8942
8943 if (pdi->main_subprogram && actual_name != NULL)
8944 set_objfile_main_name (objfile, actual_name, cu->language);
8945 break;
8946 case DW_TAG_constant:
8947 add_psymbol_to_list (actual_name, strlen (actual_name),
8948 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8949 -1, (pdi->is_external
8950 ? psymbol_placement::GLOBAL
8951 : psymbol_placement::STATIC),
8952 0, cu->language, objfile);
8953 break;
8954 case DW_TAG_variable:
8955 if (pdi->d.locdesc)
8956 addr = decode_locdesc (pdi->d.locdesc, cu);
8957
8958 if (pdi->d.locdesc
8959 && addr == 0
8960 && !dwarf2_per_objfile->has_section_at_zero)
8961 {
8962 /* A global or static variable may also have been stripped
8963 out by the linker if unused, in which case its address
8964 will be nullified; do not add such variables into partial
8965 symbol table then. */
8966 }
8967 else if (pdi->is_external)
8968 {
8969 /* Global Variable.
8970 Don't enter into the minimal symbol tables as there is
8971 a minimal symbol table entry from the ELF symbols already.
8972 Enter into partial symbol table if it has a location
8973 descriptor or a type.
8974 If the location descriptor is missing, new_symbol will create
8975 a LOC_UNRESOLVED symbol, the address of the variable will then
8976 be determined from the minimal symbol table whenever the variable
8977 is referenced.
8978 The address for the partial symbol table entry is not
8979 used by GDB, but it comes in handy for debugging partial symbol
8980 table building. */
8981
8982 if (pdi->d.locdesc || pdi->has_type)
8983 add_psymbol_to_list (actual_name, strlen (actual_name),
8984 built_actual_name != NULL,
8985 VAR_DOMAIN, LOC_STATIC,
8986 SECT_OFF_TEXT (objfile),
8987 psymbol_placement::GLOBAL,
8988 addr, cu->language, objfile);
8989 }
8990 else
8991 {
8992 int has_loc = pdi->d.locdesc != NULL;
8993
8994 /* Static Variable. Skip symbols whose value we cannot know (those
8995 without location descriptors or constant values). */
8996 if (!has_loc && !pdi->has_const_value)
8997 {
8998 xfree (built_actual_name);
8999 return;
9000 }
9001
9002 add_psymbol_to_list (actual_name, strlen (actual_name),
9003 built_actual_name != NULL,
9004 VAR_DOMAIN, LOC_STATIC,
9005 SECT_OFF_TEXT (objfile),
9006 psymbol_placement::STATIC,
9007 has_loc ? addr : 0,
9008 cu->language, objfile);
9009 }
9010 break;
9011 case DW_TAG_typedef:
9012 case DW_TAG_base_type:
9013 case DW_TAG_subrange_type:
9014 add_psymbol_to_list (actual_name, strlen (actual_name),
9015 built_actual_name != NULL,
9016 VAR_DOMAIN, LOC_TYPEDEF, -1,
9017 psymbol_placement::STATIC,
9018 0, cu->language, objfile);
9019 break;
9020 case DW_TAG_imported_declaration:
9021 case DW_TAG_namespace:
9022 add_psymbol_to_list (actual_name, strlen (actual_name),
9023 built_actual_name != NULL,
9024 VAR_DOMAIN, LOC_TYPEDEF, -1,
9025 psymbol_placement::GLOBAL,
9026 0, cu->language, objfile);
9027 break;
9028 case DW_TAG_module:
9029 /* With Fortran 77 there might be a "BLOCK DATA" module
9030 available without any name. If so, we skip the module as it
9031 doesn't bring any value. */
9032 if (actual_name != nullptr)
9033 add_psymbol_to_list (actual_name, strlen (actual_name),
9034 built_actual_name != NULL,
9035 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9036 psymbol_placement::GLOBAL,
9037 0, cu->language, objfile);
9038 break;
9039 case DW_TAG_class_type:
9040 case DW_TAG_interface_type:
9041 case DW_TAG_structure_type:
9042 case DW_TAG_union_type:
9043 case DW_TAG_enumeration_type:
9044 /* Skip external references. The DWARF standard says in the section
9045 about "Structure, Union, and Class Type Entries": "An incomplete
9046 structure, union or class type is represented by a structure,
9047 union or class entry that does not have a byte size attribute
9048 and that has a DW_AT_declaration attribute." */
9049 if (!pdi->has_byte_size && pdi->is_declaration)
9050 {
9051 xfree (built_actual_name);
9052 return;
9053 }
9054
9055 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9056 static vs. global. */
9057 add_psymbol_to_list (actual_name, strlen (actual_name),
9058 built_actual_name != NULL,
9059 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9060 cu->language == language_cplus
9061 ? psymbol_placement::GLOBAL
9062 : psymbol_placement::STATIC,
9063 0, cu->language, objfile);
9064
9065 break;
9066 case DW_TAG_enumerator:
9067 add_psymbol_to_list (actual_name, strlen (actual_name),
9068 built_actual_name != NULL,
9069 VAR_DOMAIN, LOC_CONST, -1,
9070 cu->language == language_cplus
9071 ? psymbol_placement::GLOBAL
9072 : psymbol_placement::STATIC,
9073 0, cu->language, objfile);
9074 break;
9075 default:
9076 break;
9077 }
9078
9079 xfree (built_actual_name);
9080 }
9081
9082 /* Read a partial die corresponding to a namespace; also, add a symbol
9083 corresponding to that namespace to the symbol table. NAMESPACE is
9084 the name of the enclosing namespace. */
9085
9086 static void
9087 add_partial_namespace (struct partial_die_info *pdi,
9088 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9089 int set_addrmap, struct dwarf2_cu *cu)
9090 {
9091 /* Add a symbol for the namespace. */
9092
9093 add_partial_symbol (pdi, cu);
9094
9095 /* Now scan partial symbols in that namespace. */
9096
9097 if (pdi->has_children)
9098 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9099 }
9100
9101 /* Read a partial die corresponding to a Fortran module. */
9102
9103 static void
9104 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9105 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9106 {
9107 /* Add a symbol for the namespace. */
9108
9109 add_partial_symbol (pdi, cu);
9110
9111 /* Now scan partial symbols in that module. */
9112
9113 if (pdi->has_children)
9114 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9115 }
9116
9117 /* Read a partial die corresponding to a subprogram or an inlined
9118 subprogram and create a partial symbol for that subprogram.
9119 When the CU language allows it, this routine also defines a partial
9120 symbol for each nested subprogram that this subprogram contains.
9121 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9122 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9123
9124 PDI may also be a lexical block, in which case we simply search
9125 recursively for subprograms defined inside that lexical block.
9126 Again, this is only performed when the CU language allows this
9127 type of definitions. */
9128
9129 static void
9130 add_partial_subprogram (struct partial_die_info *pdi,
9131 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9132 int set_addrmap, struct dwarf2_cu *cu)
9133 {
9134 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9135 {
9136 if (pdi->has_pc_info)
9137 {
9138 if (pdi->lowpc < *lowpc)
9139 *lowpc = pdi->lowpc;
9140 if (pdi->highpc > *highpc)
9141 *highpc = pdi->highpc;
9142 if (set_addrmap)
9143 {
9144 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9145 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9146 CORE_ADDR baseaddr;
9147 CORE_ADDR this_highpc;
9148 CORE_ADDR this_lowpc;
9149
9150 baseaddr = ANOFFSET (objfile->section_offsets,
9151 SECT_OFF_TEXT (objfile));
9152 this_lowpc
9153 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9154 pdi->lowpc + baseaddr)
9155 - baseaddr);
9156 this_highpc
9157 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9158 pdi->highpc + baseaddr)
9159 - baseaddr);
9160 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
9161 this_lowpc, this_highpc - 1,
9162 cu->per_cu->v.psymtab);
9163 }
9164 }
9165
9166 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9167 {
9168 if (!pdi->is_declaration)
9169 /* Ignore subprogram DIEs that do not have a name, they are
9170 illegal. Do not emit a complaint at this point, we will
9171 do so when we convert this psymtab into a symtab. */
9172 if (pdi->name)
9173 add_partial_symbol (pdi, cu);
9174 }
9175 }
9176
9177 if (! pdi->has_children)
9178 return;
9179
9180 if (cu->language == language_ada)
9181 {
9182 pdi = pdi->die_child;
9183 while (pdi != NULL)
9184 {
9185 pdi->fixup (cu);
9186 if (pdi->tag == DW_TAG_subprogram
9187 || pdi->tag == DW_TAG_inlined_subroutine
9188 || pdi->tag == DW_TAG_lexical_block)
9189 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9190 pdi = pdi->die_sibling;
9191 }
9192 }
9193 }
9194
9195 /* Read a partial die corresponding to an enumeration type. */
9196
9197 static void
9198 add_partial_enumeration (struct partial_die_info *enum_pdi,
9199 struct dwarf2_cu *cu)
9200 {
9201 struct partial_die_info *pdi;
9202
9203 if (enum_pdi->name != NULL)
9204 add_partial_symbol (enum_pdi, cu);
9205
9206 pdi = enum_pdi->die_child;
9207 while (pdi)
9208 {
9209 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9210 complaint (_("malformed enumerator DIE ignored"));
9211 else
9212 add_partial_symbol (pdi, cu);
9213 pdi = pdi->die_sibling;
9214 }
9215 }
9216
9217 /* Return the initial uleb128 in the die at INFO_PTR. */
9218
9219 static unsigned int
9220 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9221 {
9222 unsigned int bytes_read;
9223
9224 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9225 }
9226
9227 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9228 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9229
9230 Return the corresponding abbrev, or NULL if the number is zero (indicating
9231 an empty DIE). In either case *BYTES_READ will be set to the length of
9232 the initial number. */
9233
9234 static struct abbrev_info *
9235 peek_die_abbrev (const die_reader_specs &reader,
9236 const gdb_byte *info_ptr, unsigned int *bytes_read)
9237 {
9238 dwarf2_cu *cu = reader.cu;
9239 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9240 unsigned int abbrev_number
9241 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9242
9243 if (abbrev_number == 0)
9244 return NULL;
9245
9246 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9247 if (!abbrev)
9248 {
9249 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9250 " at offset %s [in module %s]"),
9251 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9252 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9253 }
9254
9255 return abbrev;
9256 }
9257
9258 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9259 Returns a pointer to the end of a series of DIEs, terminated by an empty
9260 DIE. Any children of the skipped DIEs will also be skipped. */
9261
9262 static const gdb_byte *
9263 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9264 {
9265 while (1)
9266 {
9267 unsigned int bytes_read;
9268 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9269
9270 if (abbrev == NULL)
9271 return info_ptr + bytes_read;
9272 else
9273 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9274 }
9275 }
9276
9277 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9278 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9279 abbrev corresponding to that skipped uleb128 should be passed in
9280 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9281 children. */
9282
9283 static const gdb_byte *
9284 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9285 struct abbrev_info *abbrev)
9286 {
9287 unsigned int bytes_read;
9288 struct attribute attr;
9289 bfd *abfd = reader->abfd;
9290 struct dwarf2_cu *cu = reader->cu;
9291 const gdb_byte *buffer = reader->buffer;
9292 const gdb_byte *buffer_end = reader->buffer_end;
9293 unsigned int form, i;
9294
9295 for (i = 0; i < abbrev->num_attrs; i++)
9296 {
9297 /* The only abbrev we care about is DW_AT_sibling. */
9298 if (abbrev->attrs[i].name == DW_AT_sibling)
9299 {
9300 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9301 if (attr.form == DW_FORM_ref_addr)
9302 complaint (_("ignoring absolute DW_AT_sibling"));
9303 else
9304 {
9305 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9306 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9307
9308 if (sibling_ptr < info_ptr)
9309 complaint (_("DW_AT_sibling points backwards"));
9310 else if (sibling_ptr > reader->buffer_end)
9311 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9312 else
9313 return sibling_ptr;
9314 }
9315 }
9316
9317 /* If it isn't DW_AT_sibling, skip this attribute. */
9318 form = abbrev->attrs[i].form;
9319 skip_attribute:
9320 switch (form)
9321 {
9322 case DW_FORM_ref_addr:
9323 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9324 and later it is offset sized. */
9325 if (cu->header.version == 2)
9326 info_ptr += cu->header.addr_size;
9327 else
9328 info_ptr += cu->header.offset_size;
9329 break;
9330 case DW_FORM_GNU_ref_alt:
9331 info_ptr += cu->header.offset_size;
9332 break;
9333 case DW_FORM_addr:
9334 info_ptr += cu->header.addr_size;
9335 break;
9336 case DW_FORM_data1:
9337 case DW_FORM_ref1:
9338 case DW_FORM_flag:
9339 case DW_FORM_strx1:
9340 info_ptr += 1;
9341 break;
9342 case DW_FORM_flag_present:
9343 case DW_FORM_implicit_const:
9344 break;
9345 case DW_FORM_data2:
9346 case DW_FORM_ref2:
9347 case DW_FORM_strx2:
9348 info_ptr += 2;
9349 break;
9350 case DW_FORM_strx3:
9351 info_ptr += 3;
9352 break;
9353 case DW_FORM_data4:
9354 case DW_FORM_ref4:
9355 case DW_FORM_strx4:
9356 info_ptr += 4;
9357 break;
9358 case DW_FORM_data8:
9359 case DW_FORM_ref8:
9360 case DW_FORM_ref_sig8:
9361 info_ptr += 8;
9362 break;
9363 case DW_FORM_data16:
9364 info_ptr += 16;
9365 break;
9366 case DW_FORM_string:
9367 read_direct_string (abfd, info_ptr, &bytes_read);
9368 info_ptr += bytes_read;
9369 break;
9370 case DW_FORM_sec_offset:
9371 case DW_FORM_strp:
9372 case DW_FORM_GNU_strp_alt:
9373 info_ptr += cu->header.offset_size;
9374 break;
9375 case DW_FORM_exprloc:
9376 case DW_FORM_block:
9377 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9378 info_ptr += bytes_read;
9379 break;
9380 case DW_FORM_block1:
9381 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9382 break;
9383 case DW_FORM_block2:
9384 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9385 break;
9386 case DW_FORM_block4:
9387 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9388 break;
9389 case DW_FORM_addrx:
9390 case DW_FORM_strx:
9391 case DW_FORM_sdata:
9392 case DW_FORM_udata:
9393 case DW_FORM_ref_udata:
9394 case DW_FORM_GNU_addr_index:
9395 case DW_FORM_GNU_str_index:
9396 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9397 break;
9398 case DW_FORM_indirect:
9399 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9400 info_ptr += bytes_read;
9401 /* We need to continue parsing from here, so just go back to
9402 the top. */
9403 goto skip_attribute;
9404
9405 default:
9406 error (_("Dwarf Error: Cannot handle %s "
9407 "in DWARF reader [in module %s]"),
9408 dwarf_form_name (form),
9409 bfd_get_filename (abfd));
9410 }
9411 }
9412
9413 if (abbrev->has_children)
9414 return skip_children (reader, info_ptr);
9415 else
9416 return info_ptr;
9417 }
9418
9419 /* Locate ORIG_PDI's sibling.
9420 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9421
9422 static const gdb_byte *
9423 locate_pdi_sibling (const struct die_reader_specs *reader,
9424 struct partial_die_info *orig_pdi,
9425 const gdb_byte *info_ptr)
9426 {
9427 /* Do we know the sibling already? */
9428
9429 if (orig_pdi->sibling)
9430 return orig_pdi->sibling;
9431
9432 /* Are there any children to deal with? */
9433
9434 if (!orig_pdi->has_children)
9435 return info_ptr;
9436
9437 /* Skip the children the long way. */
9438
9439 return skip_children (reader, info_ptr);
9440 }
9441
9442 /* Expand this partial symbol table into a full symbol table. SELF is
9443 not NULL. */
9444
9445 static void
9446 dwarf2_read_symtab (struct partial_symtab *self,
9447 struct objfile *objfile)
9448 {
9449 struct dwarf2_per_objfile *dwarf2_per_objfile
9450 = get_dwarf2_per_objfile (objfile);
9451
9452 if (self->readin)
9453 {
9454 warning (_("bug: psymtab for %s is already read in."),
9455 self->filename);
9456 }
9457 else
9458 {
9459 if (info_verbose)
9460 {
9461 printf_filtered (_("Reading in symbols for %s..."),
9462 self->filename);
9463 gdb_flush (gdb_stdout);
9464 }
9465
9466 /* If this psymtab is constructed from a debug-only objfile, the
9467 has_section_at_zero flag will not necessarily be correct. We
9468 can get the correct value for this flag by looking at the data
9469 associated with the (presumably stripped) associated objfile. */
9470 if (objfile->separate_debug_objfile_backlink)
9471 {
9472 struct dwarf2_per_objfile *dpo_backlink
9473 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9474
9475 dwarf2_per_objfile->has_section_at_zero
9476 = dpo_backlink->has_section_at_zero;
9477 }
9478
9479 dwarf2_per_objfile->reading_partial_symbols = 0;
9480
9481 psymtab_to_symtab_1 (self);
9482
9483 /* Finish up the debug error message. */
9484 if (info_verbose)
9485 printf_filtered (_("done.\n"));
9486 }
9487
9488 process_cu_includes (dwarf2_per_objfile);
9489 }
9490 \f
9491 /* Reading in full CUs. */
9492
9493 /* Add PER_CU to the queue. */
9494
9495 static void
9496 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9497 enum language pretend_language)
9498 {
9499 struct dwarf2_queue_item *item;
9500
9501 per_cu->queued = 1;
9502 item = XNEW (struct dwarf2_queue_item);
9503 item->per_cu = per_cu;
9504 item->pretend_language = pretend_language;
9505 item->next = NULL;
9506
9507 if (dwarf2_queue == NULL)
9508 dwarf2_queue = item;
9509 else
9510 dwarf2_queue_tail->next = item;
9511
9512 dwarf2_queue_tail = item;
9513 }
9514
9515 /* If PER_CU is not yet queued, add it to the queue.
9516 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9517 dependency.
9518 The result is non-zero if PER_CU was queued, otherwise the result is zero
9519 meaning either PER_CU is already queued or it is already loaded.
9520
9521 N.B. There is an invariant here that if a CU is queued then it is loaded.
9522 The caller is required to load PER_CU if we return non-zero. */
9523
9524 static int
9525 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9526 struct dwarf2_per_cu_data *per_cu,
9527 enum language pretend_language)
9528 {
9529 /* We may arrive here during partial symbol reading, if we need full
9530 DIEs to process an unusual case (e.g. template arguments). Do
9531 not queue PER_CU, just tell our caller to load its DIEs. */
9532 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9533 {
9534 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9535 return 1;
9536 return 0;
9537 }
9538
9539 /* Mark the dependence relation so that we don't flush PER_CU
9540 too early. */
9541 if (dependent_cu != NULL)
9542 dwarf2_add_dependence (dependent_cu, per_cu);
9543
9544 /* If it's already on the queue, we have nothing to do. */
9545 if (per_cu->queued)
9546 return 0;
9547
9548 /* If the compilation unit is already loaded, just mark it as
9549 used. */
9550 if (per_cu->cu != NULL)
9551 {
9552 per_cu->cu->last_used = 0;
9553 return 0;
9554 }
9555
9556 /* Add it to the queue. */
9557 queue_comp_unit (per_cu, pretend_language);
9558
9559 return 1;
9560 }
9561
9562 /* Process the queue. */
9563
9564 static void
9565 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9566 {
9567 struct dwarf2_queue_item *item, *next_item;
9568
9569 if (dwarf_read_debug)
9570 {
9571 fprintf_unfiltered (gdb_stdlog,
9572 "Expanding one or more symtabs of objfile %s ...\n",
9573 objfile_name (dwarf2_per_objfile->objfile));
9574 }
9575
9576 /* The queue starts out with one item, but following a DIE reference
9577 may load a new CU, adding it to the end of the queue. */
9578 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9579 {
9580 if ((dwarf2_per_objfile->using_index
9581 ? !item->per_cu->v.quick->compunit_symtab
9582 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9583 /* Skip dummy CUs. */
9584 && item->per_cu->cu != NULL)
9585 {
9586 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9587 unsigned int debug_print_threshold;
9588 char buf[100];
9589
9590 if (per_cu->is_debug_types)
9591 {
9592 struct signatured_type *sig_type =
9593 (struct signatured_type *) per_cu;
9594
9595 sprintf (buf, "TU %s at offset %s",
9596 hex_string (sig_type->signature),
9597 sect_offset_str (per_cu->sect_off));
9598 /* There can be 100s of TUs.
9599 Only print them in verbose mode. */
9600 debug_print_threshold = 2;
9601 }
9602 else
9603 {
9604 sprintf (buf, "CU at offset %s",
9605 sect_offset_str (per_cu->sect_off));
9606 debug_print_threshold = 1;
9607 }
9608
9609 if (dwarf_read_debug >= debug_print_threshold)
9610 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9611
9612 if (per_cu->is_debug_types)
9613 process_full_type_unit (per_cu, item->pretend_language);
9614 else
9615 process_full_comp_unit (per_cu, item->pretend_language);
9616
9617 if (dwarf_read_debug >= debug_print_threshold)
9618 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9619 }
9620
9621 item->per_cu->queued = 0;
9622 next_item = item->next;
9623 xfree (item);
9624 }
9625
9626 dwarf2_queue_tail = NULL;
9627
9628 if (dwarf_read_debug)
9629 {
9630 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9631 objfile_name (dwarf2_per_objfile->objfile));
9632 }
9633 }
9634
9635 /* Read in full symbols for PST, and anything it depends on. */
9636
9637 static void
9638 psymtab_to_symtab_1 (struct partial_symtab *pst)
9639 {
9640 struct dwarf2_per_cu_data *per_cu;
9641 int i;
9642
9643 if (pst->readin)
9644 return;
9645
9646 for (i = 0; i < pst->number_of_dependencies; i++)
9647 if (!pst->dependencies[i]->readin
9648 && pst->dependencies[i]->user == NULL)
9649 {
9650 /* Inform about additional files that need to be read in. */
9651 if (info_verbose)
9652 {
9653 /* FIXME: i18n: Need to make this a single string. */
9654 fputs_filtered (" ", gdb_stdout);
9655 wrap_here ("");
9656 fputs_filtered ("and ", gdb_stdout);
9657 wrap_here ("");
9658 printf_filtered ("%s...", pst->dependencies[i]->filename);
9659 wrap_here (""); /* Flush output. */
9660 gdb_flush (gdb_stdout);
9661 }
9662 psymtab_to_symtab_1 (pst->dependencies[i]);
9663 }
9664
9665 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9666
9667 if (per_cu == NULL)
9668 {
9669 /* It's an include file, no symbols to read for it.
9670 Everything is in the parent symtab. */
9671 pst->readin = 1;
9672 return;
9673 }
9674
9675 dw2_do_instantiate_symtab (per_cu, false);
9676 }
9677
9678 /* Trivial hash function for die_info: the hash value of a DIE
9679 is its offset in .debug_info for this objfile. */
9680
9681 static hashval_t
9682 die_hash (const void *item)
9683 {
9684 const struct die_info *die = (const struct die_info *) item;
9685
9686 return to_underlying (die->sect_off);
9687 }
9688
9689 /* Trivial comparison function for die_info structures: two DIEs
9690 are equal if they have the same offset. */
9691
9692 static int
9693 die_eq (const void *item_lhs, const void *item_rhs)
9694 {
9695 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9696 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9697
9698 return die_lhs->sect_off == die_rhs->sect_off;
9699 }
9700
9701 /* die_reader_func for load_full_comp_unit.
9702 This is identical to read_signatured_type_reader,
9703 but is kept separate for now. */
9704
9705 static void
9706 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9707 const gdb_byte *info_ptr,
9708 struct die_info *comp_unit_die,
9709 int has_children,
9710 void *data)
9711 {
9712 struct dwarf2_cu *cu = reader->cu;
9713 enum language *language_ptr = (enum language *) data;
9714
9715 gdb_assert (cu->die_hash == NULL);
9716 cu->die_hash =
9717 htab_create_alloc_ex (cu->header.length / 12,
9718 die_hash,
9719 die_eq,
9720 NULL,
9721 &cu->comp_unit_obstack,
9722 hashtab_obstack_allocate,
9723 dummy_obstack_deallocate);
9724
9725 if (has_children)
9726 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9727 &info_ptr, comp_unit_die);
9728 cu->dies = comp_unit_die;
9729 /* comp_unit_die is not stored in die_hash, no need. */
9730
9731 /* We try not to read any attributes in this function, because not
9732 all CUs needed for references have been loaded yet, and symbol
9733 table processing isn't initialized. But we have to set the CU language,
9734 or we won't be able to build types correctly.
9735 Similarly, if we do not read the producer, we can not apply
9736 producer-specific interpretation. */
9737 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
9738 }
9739
9740 /* Load the DIEs associated with PER_CU into memory. */
9741
9742 static void
9743 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9744 bool skip_partial,
9745 enum language pretend_language)
9746 {
9747 gdb_assert (! this_cu->is_debug_types);
9748
9749 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
9750 load_full_comp_unit_reader, &pretend_language);
9751 }
9752
9753 /* Add a DIE to the delayed physname list. */
9754
9755 static void
9756 add_to_method_list (struct type *type, int fnfield_index, int index,
9757 const char *name, struct die_info *die,
9758 struct dwarf2_cu *cu)
9759 {
9760 struct delayed_method_info mi;
9761 mi.type = type;
9762 mi.fnfield_index = fnfield_index;
9763 mi.index = index;
9764 mi.name = name;
9765 mi.die = die;
9766 cu->method_list.push_back (mi);
9767 }
9768
9769 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9770 "const" / "volatile". If so, decrements LEN by the length of the
9771 modifier and return true. Otherwise return false. */
9772
9773 template<size_t N>
9774 static bool
9775 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9776 {
9777 size_t mod_len = sizeof (mod) - 1;
9778 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9779 {
9780 len -= mod_len;
9781 return true;
9782 }
9783 return false;
9784 }
9785
9786 /* Compute the physnames of any methods on the CU's method list.
9787
9788 The computation of method physnames is delayed in order to avoid the
9789 (bad) condition that one of the method's formal parameters is of an as yet
9790 incomplete type. */
9791
9792 static void
9793 compute_delayed_physnames (struct dwarf2_cu *cu)
9794 {
9795 /* Only C++ delays computing physnames. */
9796 if (cu->method_list.empty ())
9797 return;
9798 gdb_assert (cu->language == language_cplus);
9799
9800 for (const delayed_method_info &mi : cu->method_list)
9801 {
9802 const char *physname;
9803 struct fn_fieldlist *fn_flp
9804 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9805 physname = dwarf2_physname (mi.name, mi.die, cu);
9806 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9807 = physname ? physname : "";
9808
9809 /* Since there's no tag to indicate whether a method is a
9810 const/volatile overload, extract that information out of the
9811 demangled name. */
9812 if (physname != NULL)
9813 {
9814 size_t len = strlen (physname);
9815
9816 while (1)
9817 {
9818 if (physname[len] == ')') /* shortcut */
9819 break;
9820 else if (check_modifier (physname, len, " const"))
9821 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9822 else if (check_modifier (physname, len, " volatile"))
9823 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9824 else
9825 break;
9826 }
9827 }
9828 }
9829
9830 /* The list is no longer needed. */
9831 cu->method_list.clear ();
9832 }
9833
9834 /* Go objects should be embedded in a DW_TAG_module DIE,
9835 and it's not clear if/how imported objects will appear.
9836 To keep Go support simple until that's worked out,
9837 go back through what we've read and create something usable.
9838 We could do this while processing each DIE, and feels kinda cleaner,
9839 but that way is more invasive.
9840 This is to, for example, allow the user to type "p var" or "b main"
9841 without having to specify the package name, and allow lookups
9842 of module.object to work in contexts that use the expression
9843 parser. */
9844
9845 static void
9846 fixup_go_packaging (struct dwarf2_cu *cu)
9847 {
9848 char *package_name = NULL;
9849 struct pending *list;
9850 int i;
9851
9852 for (list = *cu->get_builder ()->get_global_symbols ();
9853 list != NULL;
9854 list = list->next)
9855 {
9856 for (i = 0; i < list->nsyms; ++i)
9857 {
9858 struct symbol *sym = list->symbol[i];
9859
9860 if (SYMBOL_LANGUAGE (sym) == language_go
9861 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9862 {
9863 char *this_package_name = go_symbol_package_name (sym);
9864
9865 if (this_package_name == NULL)
9866 continue;
9867 if (package_name == NULL)
9868 package_name = this_package_name;
9869 else
9870 {
9871 struct objfile *objfile
9872 = cu->per_cu->dwarf2_per_objfile->objfile;
9873 if (strcmp (package_name, this_package_name) != 0)
9874 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9875 (symbol_symtab (sym) != NULL
9876 ? symtab_to_filename_for_display
9877 (symbol_symtab (sym))
9878 : objfile_name (objfile)),
9879 this_package_name, package_name);
9880 xfree (this_package_name);
9881 }
9882 }
9883 }
9884 }
9885
9886 if (package_name != NULL)
9887 {
9888 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9889 const char *saved_package_name
9890 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name);
9891 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9892 saved_package_name);
9893 struct symbol *sym;
9894
9895 sym = allocate_symbol (objfile);
9896 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
9897 SYMBOL_SET_NAMES (sym, saved_package_name,
9898 strlen (saved_package_name), 0, objfile);
9899 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9900 e.g., "main" finds the "main" module and not C's main(). */
9901 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9902 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9903 SYMBOL_TYPE (sym) = type;
9904
9905 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9906
9907 xfree (package_name);
9908 }
9909 }
9910
9911 /* Allocate a fully-qualified name consisting of the two parts on the
9912 obstack. */
9913
9914 static const char *
9915 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9916 {
9917 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9918 }
9919
9920 /* A helper that allocates a struct discriminant_info to attach to a
9921 union type. */
9922
9923 static struct discriminant_info *
9924 alloc_discriminant_info (struct type *type, int discriminant_index,
9925 int default_index)
9926 {
9927 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9928 gdb_assert (discriminant_index == -1
9929 || (discriminant_index >= 0
9930 && discriminant_index < TYPE_NFIELDS (type)));
9931 gdb_assert (default_index == -1
9932 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9933
9934 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9935
9936 struct discriminant_info *disc
9937 = ((struct discriminant_info *)
9938 TYPE_ZALLOC (type,
9939 offsetof (struct discriminant_info, discriminants)
9940 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9941 disc->default_index = default_index;
9942 disc->discriminant_index = discriminant_index;
9943
9944 struct dynamic_prop prop;
9945 prop.kind = PROP_UNDEFINED;
9946 prop.data.baton = disc;
9947
9948 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9949
9950 return disc;
9951 }
9952
9953 /* Some versions of rustc emitted enums in an unusual way.
9954
9955 Ordinary enums were emitted as unions. The first element of each
9956 structure in the union was named "RUST$ENUM$DISR". This element
9957 held the discriminant.
9958
9959 These versions of Rust also implemented the "non-zero"
9960 optimization. When the enum had two values, and one is empty and
9961 the other holds a pointer that cannot be zero, the pointer is used
9962 as the discriminant, with a zero value meaning the empty variant.
9963 Here, the union's first member is of the form
9964 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9965 where the fieldnos are the indices of the fields that should be
9966 traversed in order to find the field (which may be several fields deep)
9967 and the variantname is the name of the variant of the case when the
9968 field is zero.
9969
9970 This function recognizes whether TYPE is of one of these forms,
9971 and, if so, smashes it to be a variant type. */
9972
9973 static void
9974 quirk_rust_enum (struct type *type, struct objfile *objfile)
9975 {
9976 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9977
9978 /* We don't need to deal with empty enums. */
9979 if (TYPE_NFIELDS (type) == 0)
9980 return;
9981
9982 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9983 if (TYPE_NFIELDS (type) == 1
9984 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9985 {
9986 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9987
9988 /* Decode the field name to find the offset of the
9989 discriminant. */
9990 ULONGEST bit_offset = 0;
9991 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9992 while (name[0] >= '0' && name[0] <= '9')
9993 {
9994 char *tail;
9995 unsigned long index = strtoul (name, &tail, 10);
9996 name = tail;
9997 if (*name != '$'
9998 || index >= TYPE_NFIELDS (field_type)
9999 || (TYPE_FIELD_LOC_KIND (field_type, index)
10000 != FIELD_LOC_KIND_BITPOS))
10001 {
10002 complaint (_("Could not parse Rust enum encoding string \"%s\""
10003 "[in module %s]"),
10004 TYPE_FIELD_NAME (type, 0),
10005 objfile_name (objfile));
10006 return;
10007 }
10008 ++name;
10009
10010 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10011 field_type = TYPE_FIELD_TYPE (field_type, index);
10012 }
10013
10014 /* Make a union to hold the variants. */
10015 struct type *union_type = alloc_type (objfile);
10016 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10017 TYPE_NFIELDS (union_type) = 3;
10018 TYPE_FIELDS (union_type)
10019 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10020 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10021 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10022
10023 /* Put the discriminant must at index 0. */
10024 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10025 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10026 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10027 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10028
10029 /* The order of fields doesn't really matter, so put the real
10030 field at index 1 and the data-less field at index 2. */
10031 struct discriminant_info *disc
10032 = alloc_discriminant_info (union_type, 0, 1);
10033 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10034 TYPE_FIELD_NAME (union_type, 1)
10035 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10036 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10037 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10038 TYPE_FIELD_NAME (union_type, 1));
10039
10040 const char *dataless_name
10041 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10042 name);
10043 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10044 dataless_name);
10045 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10046 /* NAME points into the original discriminant name, which
10047 already has the correct lifetime. */
10048 TYPE_FIELD_NAME (union_type, 2) = name;
10049 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10050 disc->discriminants[2] = 0;
10051
10052 /* Smash this type to be a structure type. We have to do this
10053 because the type has already been recorded. */
10054 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10055 TYPE_NFIELDS (type) = 1;
10056 TYPE_FIELDS (type)
10057 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10058
10059 /* Install the variant part. */
10060 TYPE_FIELD_TYPE (type, 0) = union_type;
10061 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10062 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10063 }
10064 else if (TYPE_NFIELDS (type) == 1)
10065 {
10066 /* We assume that a union with a single field is a univariant
10067 enum. */
10068 /* Smash this type to be a structure type. We have to do this
10069 because the type has already been recorded. */
10070 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10071
10072 /* Make a union to hold the variants. */
10073 struct type *union_type = alloc_type (objfile);
10074 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10075 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10076 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10077 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10078 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10079
10080 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10081 const char *variant_name
10082 = rust_last_path_segment (TYPE_NAME (field_type));
10083 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10084 TYPE_NAME (field_type)
10085 = rust_fully_qualify (&objfile->objfile_obstack,
10086 TYPE_NAME (type), variant_name);
10087
10088 /* Install the union in the outer struct type. */
10089 TYPE_NFIELDS (type) = 1;
10090 TYPE_FIELDS (type)
10091 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10092 TYPE_FIELD_TYPE (type, 0) = union_type;
10093 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10094 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10095
10096 alloc_discriminant_info (union_type, -1, 0);
10097 }
10098 else
10099 {
10100 struct type *disr_type = nullptr;
10101 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10102 {
10103 disr_type = TYPE_FIELD_TYPE (type, i);
10104
10105 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10106 {
10107 /* All fields of a true enum will be structs. */
10108 return;
10109 }
10110 else if (TYPE_NFIELDS (disr_type) == 0)
10111 {
10112 /* Could be data-less variant, so keep going. */
10113 disr_type = nullptr;
10114 }
10115 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10116 "RUST$ENUM$DISR") != 0)
10117 {
10118 /* Not a Rust enum. */
10119 return;
10120 }
10121 else
10122 {
10123 /* Found one. */
10124 break;
10125 }
10126 }
10127
10128 /* If we got here without a discriminant, then it's probably
10129 just a union. */
10130 if (disr_type == nullptr)
10131 return;
10132
10133 /* Smash this type to be a structure type. We have to do this
10134 because the type has already been recorded. */
10135 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10136
10137 /* Make a union to hold the variants. */
10138 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10139 struct type *union_type = alloc_type (objfile);
10140 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10141 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10142 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10143 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10144 TYPE_FIELDS (union_type)
10145 = (struct field *) TYPE_ZALLOC (union_type,
10146 (TYPE_NFIELDS (union_type)
10147 * sizeof (struct field)));
10148
10149 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10150 TYPE_NFIELDS (type) * sizeof (struct field));
10151
10152 /* Install the discriminant at index 0 in the union. */
10153 TYPE_FIELD (union_type, 0) = *disr_field;
10154 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10155 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10156
10157 /* Install the union in the outer struct type. */
10158 TYPE_FIELD_TYPE (type, 0) = union_type;
10159 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10160 TYPE_NFIELDS (type) = 1;
10161
10162 /* Set the size and offset of the union type. */
10163 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10164
10165 /* We need a way to find the correct discriminant given a
10166 variant name. For convenience we build a map here. */
10167 struct type *enum_type = FIELD_TYPE (*disr_field);
10168 std::unordered_map<std::string, ULONGEST> discriminant_map;
10169 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10170 {
10171 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10172 {
10173 const char *name
10174 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10175 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10176 }
10177 }
10178
10179 int n_fields = TYPE_NFIELDS (union_type);
10180 struct discriminant_info *disc
10181 = alloc_discriminant_info (union_type, 0, -1);
10182 /* Skip the discriminant here. */
10183 for (int i = 1; i < n_fields; ++i)
10184 {
10185 /* Find the final word in the name of this variant's type.
10186 That name can be used to look up the correct
10187 discriminant. */
10188 const char *variant_name
10189 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10190 i)));
10191
10192 auto iter = discriminant_map.find (variant_name);
10193 if (iter != discriminant_map.end ())
10194 disc->discriminants[i] = iter->second;
10195
10196 /* Remove the discriminant field, if it exists. */
10197 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10198 if (TYPE_NFIELDS (sub_type) > 0)
10199 {
10200 --TYPE_NFIELDS (sub_type);
10201 ++TYPE_FIELDS (sub_type);
10202 }
10203 TYPE_FIELD_NAME (union_type, i) = variant_name;
10204 TYPE_NAME (sub_type)
10205 = rust_fully_qualify (&objfile->objfile_obstack,
10206 TYPE_NAME (type), variant_name);
10207 }
10208 }
10209 }
10210
10211 /* Rewrite some Rust unions to be structures with variants parts. */
10212
10213 static void
10214 rust_union_quirks (struct dwarf2_cu *cu)
10215 {
10216 gdb_assert (cu->language == language_rust);
10217 for (type *type_ : cu->rust_unions)
10218 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
10219 /* We don't need this any more. */
10220 cu->rust_unions.clear ();
10221 }
10222
10223 /* Return the symtab for PER_CU. This works properly regardless of
10224 whether we're using the index or psymtabs. */
10225
10226 static struct compunit_symtab *
10227 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10228 {
10229 return (per_cu->dwarf2_per_objfile->using_index
10230 ? per_cu->v.quick->compunit_symtab
10231 : per_cu->v.psymtab->compunit_symtab);
10232 }
10233
10234 /* A helper function for computing the list of all symbol tables
10235 included by PER_CU. */
10236
10237 static void
10238 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
10239 htab_t all_children, htab_t all_type_symtabs,
10240 struct dwarf2_per_cu_data *per_cu,
10241 struct compunit_symtab *immediate_parent)
10242 {
10243 void **slot;
10244 int ix;
10245 struct compunit_symtab *cust;
10246 struct dwarf2_per_cu_data *iter;
10247
10248 slot = htab_find_slot (all_children, per_cu, INSERT);
10249 if (*slot != NULL)
10250 {
10251 /* This inclusion and its children have been processed. */
10252 return;
10253 }
10254
10255 *slot = per_cu;
10256 /* Only add a CU if it has a symbol table. */
10257 cust = get_compunit_symtab (per_cu);
10258 if (cust != NULL)
10259 {
10260 /* If this is a type unit only add its symbol table if we haven't
10261 seen it yet (type unit per_cu's can share symtabs). */
10262 if (per_cu->is_debug_types)
10263 {
10264 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10265 if (*slot == NULL)
10266 {
10267 *slot = cust;
10268 result->push_back (cust);
10269 if (cust->user == NULL)
10270 cust->user = immediate_parent;
10271 }
10272 }
10273 else
10274 {
10275 result->push_back (cust);
10276 if (cust->user == NULL)
10277 cust->user = immediate_parent;
10278 }
10279 }
10280
10281 for (ix = 0;
10282 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10283 ++ix)
10284 {
10285 recursively_compute_inclusions (result, all_children,
10286 all_type_symtabs, iter, cust);
10287 }
10288 }
10289
10290 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10291 PER_CU. */
10292
10293 static void
10294 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10295 {
10296 gdb_assert (! per_cu->is_debug_types);
10297
10298 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10299 {
10300 int ix, len;
10301 struct dwarf2_per_cu_data *per_cu_iter;
10302 std::vector<compunit_symtab *> result_symtabs;
10303 htab_t all_children, all_type_symtabs;
10304 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10305
10306 /* If we don't have a symtab, we can just skip this case. */
10307 if (cust == NULL)
10308 return;
10309
10310 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10311 NULL, xcalloc, xfree);
10312 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10313 NULL, xcalloc, xfree);
10314
10315 for (ix = 0;
10316 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10317 ix, per_cu_iter);
10318 ++ix)
10319 {
10320 recursively_compute_inclusions (&result_symtabs, all_children,
10321 all_type_symtabs, per_cu_iter,
10322 cust);
10323 }
10324
10325 /* Now we have a transitive closure of all the included symtabs. */
10326 len = result_symtabs.size ();
10327 cust->includes
10328 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10329 struct compunit_symtab *, len + 1);
10330 memcpy (cust->includes, result_symtabs.data (),
10331 len * sizeof (compunit_symtab *));
10332 cust->includes[len] = NULL;
10333
10334 htab_delete (all_children);
10335 htab_delete (all_type_symtabs);
10336 }
10337 }
10338
10339 /* Compute the 'includes' field for the symtabs of all the CUs we just
10340 read. */
10341
10342 static void
10343 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10344 {
10345 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
10346 {
10347 if (! iter->is_debug_types)
10348 compute_compunit_symtab_includes (iter);
10349 }
10350
10351 dwarf2_per_objfile->just_read_cus.clear ();
10352 }
10353
10354 /* Generate full symbol information for PER_CU, whose DIEs have
10355 already been loaded into memory. */
10356
10357 static void
10358 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10359 enum language pretend_language)
10360 {
10361 struct dwarf2_cu *cu = per_cu->cu;
10362 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10363 struct objfile *objfile = dwarf2_per_objfile->objfile;
10364 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10365 CORE_ADDR lowpc, highpc;
10366 struct compunit_symtab *cust;
10367 CORE_ADDR baseaddr;
10368 struct block *static_block;
10369 CORE_ADDR addr;
10370
10371 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10372
10373 /* Clear the list here in case something was left over. */
10374 cu->method_list.clear ();
10375
10376 cu->language = pretend_language;
10377 cu->language_defn = language_def (cu->language);
10378
10379 /* Do line number decoding in read_file_scope () */
10380 process_die (cu->dies, cu);
10381
10382 /* For now fudge the Go package. */
10383 if (cu->language == language_go)
10384 fixup_go_packaging (cu);
10385
10386 /* Now that we have processed all the DIEs in the CU, all the types
10387 should be complete, and it should now be safe to compute all of the
10388 physnames. */
10389 compute_delayed_physnames (cu);
10390
10391 if (cu->language == language_rust)
10392 rust_union_quirks (cu);
10393
10394 /* Some compilers don't define a DW_AT_high_pc attribute for the
10395 compilation unit. If the DW_AT_high_pc is missing, synthesize
10396 it, by scanning the DIE's below the compilation unit. */
10397 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10398
10399 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10400 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10401
10402 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10403 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10404 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10405 addrmap to help ensure it has an accurate map of pc values belonging to
10406 this comp unit. */
10407 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10408
10409 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10410 SECT_OFF_TEXT (objfile),
10411 0);
10412
10413 if (cust != NULL)
10414 {
10415 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10416
10417 /* Set symtab language to language from DW_AT_language. If the
10418 compilation is from a C file generated by language preprocessors, do
10419 not set the language if it was already deduced by start_subfile. */
10420 if (!(cu->language == language_c
10421 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10422 COMPUNIT_FILETABS (cust)->language = cu->language;
10423
10424 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10425 produce DW_AT_location with location lists but it can be possibly
10426 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10427 there were bugs in prologue debug info, fixed later in GCC-4.5
10428 by "unwind info for epilogues" patch (which is not directly related).
10429
10430 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10431 needed, it would be wrong due to missing DW_AT_producer there.
10432
10433 Still one can confuse GDB by using non-standard GCC compilation
10434 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10435 */
10436 if (cu->has_loclist && gcc_4_minor >= 5)
10437 cust->locations_valid = 1;
10438
10439 if (gcc_4_minor >= 5)
10440 cust->epilogue_unwind_valid = 1;
10441
10442 cust->call_site_htab = cu->call_site_htab;
10443 }
10444
10445 if (dwarf2_per_objfile->using_index)
10446 per_cu->v.quick->compunit_symtab = cust;
10447 else
10448 {
10449 struct partial_symtab *pst = per_cu->v.psymtab;
10450 pst->compunit_symtab = cust;
10451 pst->readin = 1;
10452 }
10453
10454 /* Push it for inclusion processing later. */
10455 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10456
10457 /* Not needed any more. */
10458 cu->reset_builder ();
10459 }
10460
10461 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10462 already been loaded into memory. */
10463
10464 static void
10465 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10466 enum language pretend_language)
10467 {
10468 struct dwarf2_cu *cu = per_cu->cu;
10469 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10470 struct objfile *objfile = dwarf2_per_objfile->objfile;
10471 struct compunit_symtab *cust;
10472 struct signatured_type *sig_type;
10473
10474 gdb_assert (per_cu->is_debug_types);
10475 sig_type = (struct signatured_type *) per_cu;
10476
10477 /* Clear the list here in case something was left over. */
10478 cu->method_list.clear ();
10479
10480 cu->language = pretend_language;
10481 cu->language_defn = language_def (cu->language);
10482
10483 /* The symbol tables are set up in read_type_unit_scope. */
10484 process_die (cu->dies, cu);
10485
10486 /* For now fudge the Go package. */
10487 if (cu->language == language_go)
10488 fixup_go_packaging (cu);
10489
10490 /* Now that we have processed all the DIEs in the CU, all the types
10491 should be complete, and it should now be safe to compute all of the
10492 physnames. */
10493 compute_delayed_physnames (cu);
10494
10495 if (cu->language == language_rust)
10496 rust_union_quirks (cu);
10497
10498 /* TUs share symbol tables.
10499 If this is the first TU to use this symtab, complete the construction
10500 of it with end_expandable_symtab. Otherwise, complete the addition of
10501 this TU's symbols to the existing symtab. */
10502 if (sig_type->type_unit_group->compunit_symtab == NULL)
10503 {
10504 buildsym_compunit *builder = cu->get_builder ();
10505 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10506 sig_type->type_unit_group->compunit_symtab = cust;
10507
10508 if (cust != NULL)
10509 {
10510 /* Set symtab language to language from DW_AT_language. If the
10511 compilation is from a C file generated by language preprocessors,
10512 do not set the language if it was already deduced by
10513 start_subfile. */
10514 if (!(cu->language == language_c
10515 && COMPUNIT_FILETABS (cust)->language != language_c))
10516 COMPUNIT_FILETABS (cust)->language = cu->language;
10517 }
10518 }
10519 else
10520 {
10521 cu->get_builder ()->augment_type_symtab ();
10522 cust = sig_type->type_unit_group->compunit_symtab;
10523 }
10524
10525 if (dwarf2_per_objfile->using_index)
10526 per_cu->v.quick->compunit_symtab = cust;
10527 else
10528 {
10529 struct partial_symtab *pst = per_cu->v.psymtab;
10530 pst->compunit_symtab = cust;
10531 pst->readin = 1;
10532 }
10533
10534 /* Not needed any more. */
10535 cu->reset_builder ();
10536 }
10537
10538 /* Process an imported unit DIE. */
10539
10540 static void
10541 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10542 {
10543 struct attribute *attr;
10544
10545 /* For now we don't handle imported units in type units. */
10546 if (cu->per_cu->is_debug_types)
10547 {
10548 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10549 " supported in type units [in module %s]"),
10550 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10551 }
10552
10553 attr = dwarf2_attr (die, DW_AT_import, cu);
10554 if (attr != NULL)
10555 {
10556 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10557 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10558 dwarf2_per_cu_data *per_cu
10559 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10560 cu->per_cu->dwarf2_per_objfile);
10561
10562 /* If necessary, add it to the queue and load its DIEs. */
10563 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10564 load_full_comp_unit (per_cu, false, cu->language);
10565
10566 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10567 per_cu);
10568 }
10569 }
10570
10571 /* RAII object that represents a process_die scope: i.e.,
10572 starts/finishes processing a DIE. */
10573 class process_die_scope
10574 {
10575 public:
10576 process_die_scope (die_info *die, dwarf2_cu *cu)
10577 : m_die (die), m_cu (cu)
10578 {
10579 /* We should only be processing DIEs not already in process. */
10580 gdb_assert (!m_die->in_process);
10581 m_die->in_process = true;
10582 }
10583
10584 ~process_die_scope ()
10585 {
10586 m_die->in_process = false;
10587
10588 /* If we're done processing the DIE for the CU that owns the line
10589 header, we don't need the line header anymore. */
10590 if (m_cu->line_header_die_owner == m_die)
10591 {
10592 delete m_cu->line_header;
10593 m_cu->line_header = NULL;
10594 m_cu->line_header_die_owner = NULL;
10595 }
10596 }
10597
10598 private:
10599 die_info *m_die;
10600 dwarf2_cu *m_cu;
10601 };
10602
10603 /* Process a die and its children. */
10604
10605 static void
10606 process_die (struct die_info *die, struct dwarf2_cu *cu)
10607 {
10608 process_die_scope scope (die, cu);
10609
10610 switch (die->tag)
10611 {
10612 case DW_TAG_padding:
10613 break;
10614 case DW_TAG_compile_unit:
10615 case DW_TAG_partial_unit:
10616 read_file_scope (die, cu);
10617 break;
10618 case DW_TAG_type_unit:
10619 read_type_unit_scope (die, cu);
10620 break;
10621 case DW_TAG_subprogram:
10622 case DW_TAG_inlined_subroutine:
10623 read_func_scope (die, cu);
10624 break;
10625 case DW_TAG_lexical_block:
10626 case DW_TAG_try_block:
10627 case DW_TAG_catch_block:
10628 read_lexical_block_scope (die, cu);
10629 break;
10630 case DW_TAG_call_site:
10631 case DW_TAG_GNU_call_site:
10632 read_call_site_scope (die, cu);
10633 break;
10634 case DW_TAG_class_type:
10635 case DW_TAG_interface_type:
10636 case DW_TAG_structure_type:
10637 case DW_TAG_union_type:
10638 process_structure_scope (die, cu);
10639 break;
10640 case DW_TAG_enumeration_type:
10641 process_enumeration_scope (die, cu);
10642 break;
10643
10644 /* These dies have a type, but processing them does not create
10645 a symbol or recurse to process the children. Therefore we can
10646 read them on-demand through read_type_die. */
10647 case DW_TAG_subroutine_type:
10648 case DW_TAG_set_type:
10649 case DW_TAG_array_type:
10650 case DW_TAG_pointer_type:
10651 case DW_TAG_ptr_to_member_type:
10652 case DW_TAG_reference_type:
10653 case DW_TAG_rvalue_reference_type:
10654 case DW_TAG_string_type:
10655 break;
10656
10657 case DW_TAG_base_type:
10658 case DW_TAG_subrange_type:
10659 case DW_TAG_typedef:
10660 /* Add a typedef symbol for the type definition, if it has a
10661 DW_AT_name. */
10662 new_symbol (die, read_type_die (die, cu), cu);
10663 break;
10664 case DW_TAG_common_block:
10665 read_common_block (die, cu);
10666 break;
10667 case DW_TAG_common_inclusion:
10668 break;
10669 case DW_TAG_namespace:
10670 cu->processing_has_namespace_info = true;
10671 read_namespace (die, cu);
10672 break;
10673 case DW_TAG_module:
10674 cu->processing_has_namespace_info = true;
10675 read_module (die, cu);
10676 break;
10677 case DW_TAG_imported_declaration:
10678 cu->processing_has_namespace_info = true;
10679 if (read_namespace_alias (die, cu))
10680 break;
10681 /* The declaration is not a global namespace alias. */
10682 /* Fall through. */
10683 case DW_TAG_imported_module:
10684 cu->processing_has_namespace_info = true;
10685 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10686 || cu->language != language_fortran))
10687 complaint (_("Tag '%s' has unexpected children"),
10688 dwarf_tag_name (die->tag));
10689 read_import_statement (die, cu);
10690 break;
10691
10692 case DW_TAG_imported_unit:
10693 process_imported_unit_die (die, cu);
10694 break;
10695
10696 case DW_TAG_variable:
10697 read_variable (die, cu);
10698 break;
10699
10700 default:
10701 new_symbol (die, NULL, cu);
10702 break;
10703 }
10704 }
10705 \f
10706 /* DWARF name computation. */
10707
10708 /* A helper function for dwarf2_compute_name which determines whether DIE
10709 needs to have the name of the scope prepended to the name listed in the
10710 die. */
10711
10712 static int
10713 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10714 {
10715 struct attribute *attr;
10716
10717 switch (die->tag)
10718 {
10719 case DW_TAG_namespace:
10720 case DW_TAG_typedef:
10721 case DW_TAG_class_type:
10722 case DW_TAG_interface_type:
10723 case DW_TAG_structure_type:
10724 case DW_TAG_union_type:
10725 case DW_TAG_enumeration_type:
10726 case DW_TAG_enumerator:
10727 case DW_TAG_subprogram:
10728 case DW_TAG_inlined_subroutine:
10729 case DW_TAG_member:
10730 case DW_TAG_imported_declaration:
10731 return 1;
10732
10733 case DW_TAG_variable:
10734 case DW_TAG_constant:
10735 /* We only need to prefix "globally" visible variables. These include
10736 any variable marked with DW_AT_external or any variable that
10737 lives in a namespace. [Variables in anonymous namespaces
10738 require prefixing, but they are not DW_AT_external.] */
10739
10740 if (dwarf2_attr (die, DW_AT_specification, cu))
10741 {
10742 struct dwarf2_cu *spec_cu = cu;
10743
10744 return die_needs_namespace (die_specification (die, &spec_cu),
10745 spec_cu);
10746 }
10747
10748 attr = dwarf2_attr (die, DW_AT_external, cu);
10749 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10750 && die->parent->tag != DW_TAG_module)
10751 return 0;
10752 /* A variable in a lexical block of some kind does not need a
10753 namespace, even though in C++ such variables may be external
10754 and have a mangled name. */
10755 if (die->parent->tag == DW_TAG_lexical_block
10756 || die->parent->tag == DW_TAG_try_block
10757 || die->parent->tag == DW_TAG_catch_block
10758 || die->parent->tag == DW_TAG_subprogram)
10759 return 0;
10760 return 1;
10761
10762 default:
10763 return 0;
10764 }
10765 }
10766
10767 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10768 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10769 defined for the given DIE. */
10770
10771 static struct attribute *
10772 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10773 {
10774 struct attribute *attr;
10775
10776 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10777 if (attr == NULL)
10778 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10779
10780 return attr;
10781 }
10782
10783 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10784 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10785 defined for the given DIE. */
10786
10787 static const char *
10788 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10789 {
10790 const char *linkage_name;
10791
10792 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10793 if (linkage_name == NULL)
10794 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10795
10796 return linkage_name;
10797 }
10798
10799 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10800 compute the physname for the object, which include a method's:
10801 - formal parameters (C++),
10802 - receiver type (Go),
10803
10804 The term "physname" is a bit confusing.
10805 For C++, for example, it is the demangled name.
10806 For Go, for example, it's the mangled name.
10807
10808 For Ada, return the DIE's linkage name rather than the fully qualified
10809 name. PHYSNAME is ignored..
10810
10811 The result is allocated on the objfile_obstack and canonicalized. */
10812
10813 static const char *
10814 dwarf2_compute_name (const char *name,
10815 struct die_info *die, struct dwarf2_cu *cu,
10816 int physname)
10817 {
10818 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10819
10820 if (name == NULL)
10821 name = dwarf2_name (die, cu);
10822
10823 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10824 but otherwise compute it by typename_concat inside GDB.
10825 FIXME: Actually this is not really true, or at least not always true.
10826 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10827 Fortran names because there is no mangling standard. So new_symbol
10828 will set the demangled name to the result of dwarf2_full_name, and it is
10829 the demangled name that GDB uses if it exists. */
10830 if (cu->language == language_ada
10831 || (cu->language == language_fortran && physname))
10832 {
10833 /* For Ada unit, we prefer the linkage name over the name, as
10834 the former contains the exported name, which the user expects
10835 to be able to reference. Ideally, we want the user to be able
10836 to reference this entity using either natural or linkage name,
10837 but we haven't started looking at this enhancement yet. */
10838 const char *linkage_name = dw2_linkage_name (die, cu);
10839
10840 if (linkage_name != NULL)
10841 return linkage_name;
10842 }
10843
10844 /* These are the only languages we know how to qualify names in. */
10845 if (name != NULL
10846 && (cu->language == language_cplus
10847 || cu->language == language_fortran || cu->language == language_d
10848 || cu->language == language_rust))
10849 {
10850 if (die_needs_namespace (die, cu))
10851 {
10852 const char *prefix;
10853 const char *canonical_name = NULL;
10854
10855 string_file buf;
10856
10857 prefix = determine_prefix (die, cu);
10858 if (*prefix != '\0')
10859 {
10860 char *prefixed_name = typename_concat (NULL, prefix, name,
10861 physname, cu);
10862
10863 buf.puts (prefixed_name);
10864 xfree (prefixed_name);
10865 }
10866 else
10867 buf.puts (name);
10868
10869 /* Template parameters may be specified in the DIE's DW_AT_name, or
10870 as children with DW_TAG_template_type_param or
10871 DW_TAG_value_type_param. If the latter, add them to the name
10872 here. If the name already has template parameters, then
10873 skip this step; some versions of GCC emit both, and
10874 it is more efficient to use the pre-computed name.
10875
10876 Something to keep in mind about this process: it is very
10877 unlikely, or in some cases downright impossible, to produce
10878 something that will match the mangled name of a function.
10879 If the definition of the function has the same debug info,
10880 we should be able to match up with it anyway. But fallbacks
10881 using the minimal symbol, for instance to find a method
10882 implemented in a stripped copy of libstdc++, will not work.
10883 If we do not have debug info for the definition, we will have to
10884 match them up some other way.
10885
10886 When we do name matching there is a related problem with function
10887 templates; two instantiated function templates are allowed to
10888 differ only by their return types, which we do not add here. */
10889
10890 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10891 {
10892 struct attribute *attr;
10893 struct die_info *child;
10894 int first = 1;
10895
10896 die->building_fullname = 1;
10897
10898 for (child = die->child; child != NULL; child = child->sibling)
10899 {
10900 struct type *type;
10901 LONGEST value;
10902 const gdb_byte *bytes;
10903 struct dwarf2_locexpr_baton *baton;
10904 struct value *v;
10905
10906 if (child->tag != DW_TAG_template_type_param
10907 && child->tag != DW_TAG_template_value_param)
10908 continue;
10909
10910 if (first)
10911 {
10912 buf.puts ("<");
10913 first = 0;
10914 }
10915 else
10916 buf.puts (", ");
10917
10918 attr = dwarf2_attr (child, DW_AT_type, cu);
10919 if (attr == NULL)
10920 {
10921 complaint (_("template parameter missing DW_AT_type"));
10922 buf.puts ("UNKNOWN_TYPE");
10923 continue;
10924 }
10925 type = die_type (child, cu);
10926
10927 if (child->tag == DW_TAG_template_type_param)
10928 {
10929 c_print_type (type, "", &buf, -1, 0, cu->language,
10930 &type_print_raw_options);
10931 continue;
10932 }
10933
10934 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10935 if (attr == NULL)
10936 {
10937 complaint (_("template parameter missing "
10938 "DW_AT_const_value"));
10939 buf.puts ("UNKNOWN_VALUE");
10940 continue;
10941 }
10942
10943 dwarf2_const_value_attr (attr, type, name,
10944 &cu->comp_unit_obstack, cu,
10945 &value, &bytes, &baton);
10946
10947 if (TYPE_NOSIGN (type))
10948 /* GDB prints characters as NUMBER 'CHAR'. If that's
10949 changed, this can use value_print instead. */
10950 c_printchar (value, type, &buf);
10951 else
10952 {
10953 struct value_print_options opts;
10954
10955 if (baton != NULL)
10956 v = dwarf2_evaluate_loc_desc (type, NULL,
10957 baton->data,
10958 baton->size,
10959 baton->per_cu);
10960 else if (bytes != NULL)
10961 {
10962 v = allocate_value (type);
10963 memcpy (value_contents_writeable (v), bytes,
10964 TYPE_LENGTH (type));
10965 }
10966 else
10967 v = value_from_longest (type, value);
10968
10969 /* Specify decimal so that we do not depend on
10970 the radix. */
10971 get_formatted_print_options (&opts, 'd');
10972 opts.raw = 1;
10973 value_print (v, &buf, &opts);
10974 release_value (v);
10975 }
10976 }
10977
10978 die->building_fullname = 0;
10979
10980 if (!first)
10981 {
10982 /* Close the argument list, with a space if necessary
10983 (nested templates). */
10984 if (!buf.empty () && buf.string ().back () == '>')
10985 buf.puts (" >");
10986 else
10987 buf.puts (">");
10988 }
10989 }
10990
10991 /* For C++ methods, append formal parameter type
10992 information, if PHYSNAME. */
10993
10994 if (physname && die->tag == DW_TAG_subprogram
10995 && cu->language == language_cplus)
10996 {
10997 struct type *type = read_type_die (die, cu);
10998
10999 c_type_print_args (type, &buf, 1, cu->language,
11000 &type_print_raw_options);
11001
11002 if (cu->language == language_cplus)
11003 {
11004 /* Assume that an artificial first parameter is
11005 "this", but do not crash if it is not. RealView
11006 marks unnamed (and thus unused) parameters as
11007 artificial; there is no way to differentiate
11008 the two cases. */
11009 if (TYPE_NFIELDS (type) > 0
11010 && TYPE_FIELD_ARTIFICIAL (type, 0)
11011 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11012 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11013 0))))
11014 buf.puts (" const");
11015 }
11016 }
11017
11018 const std::string &intermediate_name = buf.string ();
11019
11020 if (cu->language == language_cplus)
11021 canonical_name
11022 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11023 &objfile->per_bfd->storage_obstack);
11024
11025 /* If we only computed INTERMEDIATE_NAME, or if
11026 INTERMEDIATE_NAME is already canonical, then we need to
11027 copy it to the appropriate obstack. */
11028 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11029 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11030 intermediate_name);
11031 else
11032 name = canonical_name;
11033 }
11034 }
11035
11036 return name;
11037 }
11038
11039 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11040 If scope qualifiers are appropriate they will be added. The result
11041 will be allocated on the storage_obstack, or NULL if the DIE does
11042 not have a name. NAME may either be from a previous call to
11043 dwarf2_name or NULL.
11044
11045 The output string will be canonicalized (if C++). */
11046
11047 static const char *
11048 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11049 {
11050 return dwarf2_compute_name (name, die, cu, 0);
11051 }
11052
11053 /* Construct a physname for the given DIE in CU. NAME may either be
11054 from a previous call to dwarf2_name or NULL. The result will be
11055 allocated on the objfile_objstack or NULL if the DIE does not have a
11056 name.
11057
11058 The output string will be canonicalized (if C++). */
11059
11060 static const char *
11061 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11062 {
11063 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11064 const char *retval, *mangled = NULL, *canon = NULL;
11065 int need_copy = 1;
11066
11067 /* In this case dwarf2_compute_name is just a shortcut not building anything
11068 on its own. */
11069 if (!die_needs_namespace (die, cu))
11070 return dwarf2_compute_name (name, die, cu, 1);
11071
11072 mangled = dw2_linkage_name (die, cu);
11073
11074 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11075 See https://github.com/rust-lang/rust/issues/32925. */
11076 if (cu->language == language_rust && mangled != NULL
11077 && strchr (mangled, '{') != NULL)
11078 mangled = NULL;
11079
11080 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11081 has computed. */
11082 gdb::unique_xmalloc_ptr<char> demangled;
11083 if (mangled != NULL)
11084 {
11085
11086 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11087 {
11088 /* Do nothing (do not demangle the symbol name). */
11089 }
11090 else if (cu->language == language_go)
11091 {
11092 /* This is a lie, but we already lie to the caller new_symbol.
11093 new_symbol assumes we return the mangled name.
11094 This just undoes that lie until things are cleaned up. */
11095 }
11096 else
11097 {
11098 /* Use DMGL_RET_DROP for C++ template functions to suppress
11099 their return type. It is easier for GDB users to search
11100 for such functions as `name(params)' than `long name(params)'.
11101 In such case the minimal symbol names do not match the full
11102 symbol names but for template functions there is never a need
11103 to look up their definition from their declaration so
11104 the only disadvantage remains the minimal symbol variant
11105 `long name(params)' does not have the proper inferior type. */
11106 demangled.reset (gdb_demangle (mangled,
11107 (DMGL_PARAMS | DMGL_ANSI
11108 | DMGL_RET_DROP)));
11109 }
11110 if (demangled)
11111 canon = demangled.get ();
11112 else
11113 {
11114 canon = mangled;
11115 need_copy = 0;
11116 }
11117 }
11118
11119 if (canon == NULL || check_physname)
11120 {
11121 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11122
11123 if (canon != NULL && strcmp (physname, canon) != 0)
11124 {
11125 /* It may not mean a bug in GDB. The compiler could also
11126 compute DW_AT_linkage_name incorrectly. But in such case
11127 GDB would need to be bug-to-bug compatible. */
11128
11129 complaint (_("Computed physname <%s> does not match demangled <%s> "
11130 "(from linkage <%s>) - DIE at %s [in module %s]"),
11131 physname, canon, mangled, sect_offset_str (die->sect_off),
11132 objfile_name (objfile));
11133
11134 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11135 is available here - over computed PHYSNAME. It is safer
11136 against both buggy GDB and buggy compilers. */
11137
11138 retval = canon;
11139 }
11140 else
11141 {
11142 retval = physname;
11143 need_copy = 0;
11144 }
11145 }
11146 else
11147 retval = canon;
11148
11149 if (need_copy)
11150 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
11151
11152 return retval;
11153 }
11154
11155 /* Inspect DIE in CU for a namespace alias. If one exists, record
11156 a new symbol for it.
11157
11158 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11159
11160 static int
11161 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11162 {
11163 struct attribute *attr;
11164
11165 /* If the die does not have a name, this is not a namespace
11166 alias. */
11167 attr = dwarf2_attr (die, DW_AT_name, cu);
11168 if (attr != NULL)
11169 {
11170 int num;
11171 struct die_info *d = die;
11172 struct dwarf2_cu *imported_cu = cu;
11173
11174 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11175 keep inspecting DIEs until we hit the underlying import. */
11176 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11177 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11178 {
11179 attr = dwarf2_attr (d, DW_AT_import, cu);
11180 if (attr == NULL)
11181 break;
11182
11183 d = follow_die_ref (d, attr, &imported_cu);
11184 if (d->tag != DW_TAG_imported_declaration)
11185 break;
11186 }
11187
11188 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11189 {
11190 complaint (_("DIE at %s has too many recursively imported "
11191 "declarations"), sect_offset_str (d->sect_off));
11192 return 0;
11193 }
11194
11195 if (attr != NULL)
11196 {
11197 struct type *type;
11198 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11199
11200 type = get_die_type_at_offset (sect_off, cu->per_cu);
11201 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11202 {
11203 /* This declaration is a global namespace alias. Add
11204 a symbol for it whose type is the aliased namespace. */
11205 new_symbol (die, type, cu);
11206 return 1;
11207 }
11208 }
11209 }
11210
11211 return 0;
11212 }
11213
11214 /* Return the using directives repository (global or local?) to use in the
11215 current context for CU.
11216
11217 For Ada, imported declarations can materialize renamings, which *may* be
11218 global. However it is impossible (for now?) in DWARF to distinguish
11219 "external" imported declarations and "static" ones. As all imported
11220 declarations seem to be static in all other languages, make them all CU-wide
11221 global only in Ada. */
11222
11223 static struct using_direct **
11224 using_directives (struct dwarf2_cu *cu)
11225 {
11226 if (cu->language == language_ada
11227 && cu->get_builder ()->outermost_context_p ())
11228 return cu->get_builder ()->get_global_using_directives ();
11229 else
11230 return cu->get_builder ()->get_local_using_directives ();
11231 }
11232
11233 /* Read the import statement specified by the given die and record it. */
11234
11235 static void
11236 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11237 {
11238 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11239 struct attribute *import_attr;
11240 struct die_info *imported_die, *child_die;
11241 struct dwarf2_cu *imported_cu;
11242 const char *imported_name;
11243 const char *imported_name_prefix;
11244 const char *canonical_name;
11245 const char *import_alias;
11246 const char *imported_declaration = NULL;
11247 const char *import_prefix;
11248 std::vector<const char *> excludes;
11249
11250 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11251 if (import_attr == NULL)
11252 {
11253 complaint (_("Tag '%s' has no DW_AT_import"),
11254 dwarf_tag_name (die->tag));
11255 return;
11256 }
11257
11258 imported_cu = cu;
11259 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11260 imported_name = dwarf2_name (imported_die, imported_cu);
11261 if (imported_name == NULL)
11262 {
11263 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11264
11265 The import in the following code:
11266 namespace A
11267 {
11268 typedef int B;
11269 }
11270
11271 int main ()
11272 {
11273 using A::B;
11274 B b;
11275 return b;
11276 }
11277
11278 ...
11279 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11280 <52> DW_AT_decl_file : 1
11281 <53> DW_AT_decl_line : 6
11282 <54> DW_AT_import : <0x75>
11283 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11284 <59> DW_AT_name : B
11285 <5b> DW_AT_decl_file : 1
11286 <5c> DW_AT_decl_line : 2
11287 <5d> DW_AT_type : <0x6e>
11288 ...
11289 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11290 <76> DW_AT_byte_size : 4
11291 <77> DW_AT_encoding : 5 (signed)
11292
11293 imports the wrong die ( 0x75 instead of 0x58 ).
11294 This case will be ignored until the gcc bug is fixed. */
11295 return;
11296 }
11297
11298 /* Figure out the local name after import. */
11299 import_alias = dwarf2_name (die, cu);
11300
11301 /* Figure out where the statement is being imported to. */
11302 import_prefix = determine_prefix (die, cu);
11303
11304 /* Figure out what the scope of the imported die is and prepend it
11305 to the name of the imported die. */
11306 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11307
11308 if (imported_die->tag != DW_TAG_namespace
11309 && imported_die->tag != DW_TAG_module)
11310 {
11311 imported_declaration = imported_name;
11312 canonical_name = imported_name_prefix;
11313 }
11314 else if (strlen (imported_name_prefix) > 0)
11315 canonical_name = obconcat (&objfile->objfile_obstack,
11316 imported_name_prefix,
11317 (cu->language == language_d ? "." : "::"),
11318 imported_name, (char *) NULL);
11319 else
11320 canonical_name = imported_name;
11321
11322 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11323 for (child_die = die->child; child_die && child_die->tag;
11324 child_die = sibling_die (child_die))
11325 {
11326 /* DWARF-4: A Fortran use statement with a “rename list” may be
11327 represented by an imported module entry with an import attribute
11328 referring to the module and owned entries corresponding to those
11329 entities that are renamed as part of being imported. */
11330
11331 if (child_die->tag != DW_TAG_imported_declaration)
11332 {
11333 complaint (_("child DW_TAG_imported_declaration expected "
11334 "- DIE at %s [in module %s]"),
11335 sect_offset_str (child_die->sect_off),
11336 objfile_name (objfile));
11337 continue;
11338 }
11339
11340 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11341 if (import_attr == NULL)
11342 {
11343 complaint (_("Tag '%s' has no DW_AT_import"),
11344 dwarf_tag_name (child_die->tag));
11345 continue;
11346 }
11347
11348 imported_cu = cu;
11349 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11350 &imported_cu);
11351 imported_name = dwarf2_name (imported_die, imported_cu);
11352 if (imported_name == NULL)
11353 {
11354 complaint (_("child DW_TAG_imported_declaration has unknown "
11355 "imported name - DIE at %s [in module %s]"),
11356 sect_offset_str (child_die->sect_off),
11357 objfile_name (objfile));
11358 continue;
11359 }
11360
11361 excludes.push_back (imported_name);
11362
11363 process_die (child_die, cu);
11364 }
11365
11366 add_using_directive (using_directives (cu),
11367 import_prefix,
11368 canonical_name,
11369 import_alias,
11370 imported_declaration,
11371 excludes,
11372 0,
11373 &objfile->objfile_obstack);
11374 }
11375
11376 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11377 types, but gives them a size of zero. Starting with version 14,
11378 ICC is compatible with GCC. */
11379
11380 static bool
11381 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11382 {
11383 if (!cu->checked_producer)
11384 check_producer (cu);
11385
11386 return cu->producer_is_icc_lt_14;
11387 }
11388
11389 /* ICC generates a DW_AT_type for C void functions. This was observed on
11390 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11391 which says that void functions should not have a DW_AT_type. */
11392
11393 static bool
11394 producer_is_icc (struct dwarf2_cu *cu)
11395 {
11396 if (!cu->checked_producer)
11397 check_producer (cu);
11398
11399 return cu->producer_is_icc;
11400 }
11401
11402 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11403 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11404 this, it was first present in GCC release 4.3.0. */
11405
11406 static bool
11407 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11408 {
11409 if (!cu->checked_producer)
11410 check_producer (cu);
11411
11412 return cu->producer_is_gcc_lt_4_3;
11413 }
11414
11415 static file_and_directory
11416 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11417 {
11418 file_and_directory res;
11419
11420 /* Find the filename. Do not use dwarf2_name here, since the filename
11421 is not a source language identifier. */
11422 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11423 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11424
11425 if (res.comp_dir == NULL
11426 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11427 && IS_ABSOLUTE_PATH (res.name))
11428 {
11429 res.comp_dir_storage = ldirname (res.name);
11430 if (!res.comp_dir_storage.empty ())
11431 res.comp_dir = res.comp_dir_storage.c_str ();
11432 }
11433 if (res.comp_dir != NULL)
11434 {
11435 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11436 directory, get rid of it. */
11437 const char *cp = strchr (res.comp_dir, ':');
11438
11439 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11440 res.comp_dir = cp + 1;
11441 }
11442
11443 if (res.name == NULL)
11444 res.name = "<unknown>";
11445
11446 return res;
11447 }
11448
11449 /* Handle DW_AT_stmt_list for a compilation unit.
11450 DIE is the DW_TAG_compile_unit die for CU.
11451 COMP_DIR is the compilation directory. LOWPC is passed to
11452 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11453
11454 static void
11455 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11456 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11457 {
11458 struct dwarf2_per_objfile *dwarf2_per_objfile
11459 = cu->per_cu->dwarf2_per_objfile;
11460 struct objfile *objfile = dwarf2_per_objfile->objfile;
11461 struct attribute *attr;
11462 struct line_header line_header_local;
11463 hashval_t line_header_local_hash;
11464 void **slot;
11465 int decode_mapping;
11466
11467 gdb_assert (! cu->per_cu->is_debug_types);
11468
11469 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11470 if (attr == NULL)
11471 return;
11472
11473 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11474
11475 /* The line header hash table is only created if needed (it exists to
11476 prevent redundant reading of the line table for partial_units).
11477 If we're given a partial_unit, we'll need it. If we're given a
11478 compile_unit, then use the line header hash table if it's already
11479 created, but don't create one just yet. */
11480
11481 if (dwarf2_per_objfile->line_header_hash == NULL
11482 && die->tag == DW_TAG_partial_unit)
11483 {
11484 dwarf2_per_objfile->line_header_hash
11485 = htab_create_alloc_ex (127, line_header_hash_voidp,
11486 line_header_eq_voidp,
11487 free_line_header_voidp,
11488 &objfile->objfile_obstack,
11489 hashtab_obstack_allocate,
11490 dummy_obstack_deallocate);
11491 }
11492
11493 line_header_local.sect_off = line_offset;
11494 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11495 line_header_local_hash = line_header_hash (&line_header_local);
11496 if (dwarf2_per_objfile->line_header_hash != NULL)
11497 {
11498 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11499 &line_header_local,
11500 line_header_local_hash, NO_INSERT);
11501
11502 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11503 is not present in *SLOT (since if there is something in *SLOT then
11504 it will be for a partial_unit). */
11505 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11506 {
11507 gdb_assert (*slot != NULL);
11508 cu->line_header = (struct line_header *) *slot;
11509 return;
11510 }
11511 }
11512
11513 /* dwarf_decode_line_header does not yet provide sufficient information.
11514 We always have to call also dwarf_decode_lines for it. */
11515 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11516 if (lh == NULL)
11517 return;
11518
11519 cu->line_header = lh.release ();
11520 cu->line_header_die_owner = die;
11521
11522 if (dwarf2_per_objfile->line_header_hash == NULL)
11523 slot = NULL;
11524 else
11525 {
11526 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11527 &line_header_local,
11528 line_header_local_hash, INSERT);
11529 gdb_assert (slot != NULL);
11530 }
11531 if (slot != NULL && *slot == NULL)
11532 {
11533 /* This newly decoded line number information unit will be owned
11534 by line_header_hash hash table. */
11535 *slot = cu->line_header;
11536 cu->line_header_die_owner = NULL;
11537 }
11538 else
11539 {
11540 /* We cannot free any current entry in (*slot) as that struct line_header
11541 may be already used by multiple CUs. Create only temporary decoded
11542 line_header for this CU - it may happen at most once for each line
11543 number information unit. And if we're not using line_header_hash
11544 then this is what we want as well. */
11545 gdb_assert (die->tag != DW_TAG_partial_unit);
11546 }
11547 decode_mapping = (die->tag != DW_TAG_partial_unit);
11548 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11549 decode_mapping);
11550
11551 }
11552
11553 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11554
11555 static void
11556 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11557 {
11558 struct dwarf2_per_objfile *dwarf2_per_objfile
11559 = cu->per_cu->dwarf2_per_objfile;
11560 struct objfile *objfile = dwarf2_per_objfile->objfile;
11561 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11562 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11563 CORE_ADDR highpc = ((CORE_ADDR) 0);
11564 struct attribute *attr;
11565 struct die_info *child_die;
11566 CORE_ADDR baseaddr;
11567
11568 prepare_one_comp_unit (cu, die, cu->language);
11569 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11570
11571 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11572
11573 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11574 from finish_block. */
11575 if (lowpc == ((CORE_ADDR) -1))
11576 lowpc = highpc;
11577 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11578
11579 file_and_directory fnd = find_file_and_directory (die, cu);
11580
11581 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11582 standardised yet. As a workaround for the language detection we fall
11583 back to the DW_AT_producer string. */
11584 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11585 cu->language = language_opencl;
11586
11587 /* Similar hack for Go. */
11588 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11589 set_cu_language (DW_LANG_Go, cu);
11590
11591 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11592
11593 /* Decode line number information if present. We do this before
11594 processing child DIEs, so that the line header table is available
11595 for DW_AT_decl_file. */
11596 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11597
11598 /* Process all dies in compilation unit. */
11599 if (die->child != NULL)
11600 {
11601 child_die = die->child;
11602 while (child_die && child_die->tag)
11603 {
11604 process_die (child_die, cu);
11605 child_die = sibling_die (child_die);
11606 }
11607 }
11608
11609 /* Decode macro information, if present. Dwarf 2 macro information
11610 refers to information in the line number info statement program
11611 header, so we can only read it if we've read the header
11612 successfully. */
11613 attr = dwarf2_attr (die, DW_AT_macros, cu);
11614 if (attr == NULL)
11615 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11616 if (attr && cu->line_header)
11617 {
11618 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11619 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11620
11621 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11622 }
11623 else
11624 {
11625 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11626 if (attr && cu->line_header)
11627 {
11628 unsigned int macro_offset = DW_UNSND (attr);
11629
11630 dwarf_decode_macros (cu, macro_offset, 0);
11631 }
11632 }
11633 }
11634
11635 void
11636 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11637 {
11638 struct type_unit_group *tu_group;
11639 int first_time;
11640 struct attribute *attr;
11641 unsigned int i;
11642 struct signatured_type *sig_type;
11643
11644 gdb_assert (per_cu->is_debug_types);
11645 sig_type = (struct signatured_type *) per_cu;
11646
11647 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11648
11649 /* If we're using .gdb_index (includes -readnow) then
11650 per_cu->type_unit_group may not have been set up yet. */
11651 if (sig_type->type_unit_group == NULL)
11652 sig_type->type_unit_group = get_type_unit_group (this, attr);
11653 tu_group = sig_type->type_unit_group;
11654
11655 /* If we've already processed this stmt_list there's no real need to
11656 do it again, we could fake it and just recreate the part we need
11657 (file name,index -> symtab mapping). If data shows this optimization
11658 is useful we can do it then. */
11659 first_time = tu_group->compunit_symtab == NULL;
11660
11661 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11662 debug info. */
11663 line_header_up lh;
11664 if (attr != NULL)
11665 {
11666 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11667 lh = dwarf_decode_line_header (line_offset, this);
11668 }
11669 if (lh == NULL)
11670 {
11671 if (first_time)
11672 start_symtab ("", NULL, 0);
11673 else
11674 {
11675 gdb_assert (tu_group->symtabs == NULL);
11676 gdb_assert (m_builder == nullptr);
11677 struct compunit_symtab *cust = tu_group->compunit_symtab;
11678 m_builder.reset (new struct buildsym_compunit
11679 (COMPUNIT_OBJFILE (cust), "",
11680 COMPUNIT_DIRNAME (cust),
11681 compunit_language (cust),
11682 0, cust));
11683 }
11684 return;
11685 }
11686
11687 line_header = lh.release ();
11688 line_header_die_owner = die;
11689
11690 if (first_time)
11691 {
11692 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11693
11694 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11695 still initializing it, and our caller (a few levels up)
11696 process_full_type_unit still needs to know if this is the first
11697 time. */
11698
11699 tu_group->num_symtabs = line_header->file_names.size ();
11700 tu_group->symtabs = XNEWVEC (struct symtab *,
11701 line_header->file_names.size ());
11702
11703 for (i = 0; i < line_header->file_names.size (); ++i)
11704 {
11705 file_entry &fe = line_header->file_names[i];
11706
11707 dwarf2_start_subfile (this, fe.name,
11708 fe.include_dir (line_header));
11709 buildsym_compunit *b = get_builder ();
11710 if (b->get_current_subfile ()->symtab == NULL)
11711 {
11712 /* NOTE: start_subfile will recognize when it's been
11713 passed a file it has already seen. So we can't
11714 assume there's a simple mapping from
11715 cu->line_header->file_names to subfiles, plus
11716 cu->line_header->file_names may contain dups. */
11717 b->get_current_subfile ()->symtab
11718 = allocate_symtab (cust, b->get_current_subfile ()->name);
11719 }
11720
11721 fe.symtab = b->get_current_subfile ()->symtab;
11722 tu_group->symtabs[i] = fe.symtab;
11723 }
11724 }
11725 else
11726 {
11727 gdb_assert (m_builder == nullptr);
11728 struct compunit_symtab *cust = tu_group->compunit_symtab;
11729 m_builder.reset (new struct buildsym_compunit
11730 (COMPUNIT_OBJFILE (cust), "",
11731 COMPUNIT_DIRNAME (cust),
11732 compunit_language (cust),
11733 0, cust));
11734
11735 for (i = 0; i < line_header->file_names.size (); ++i)
11736 {
11737 file_entry &fe = line_header->file_names[i];
11738
11739 fe.symtab = tu_group->symtabs[i];
11740 }
11741 }
11742
11743 /* The main symtab is allocated last. Type units don't have DW_AT_name
11744 so they don't have a "real" (so to speak) symtab anyway.
11745 There is later code that will assign the main symtab to all symbols
11746 that don't have one. We need to handle the case of a symbol with a
11747 missing symtab (DW_AT_decl_file) anyway. */
11748 }
11749
11750 /* Process DW_TAG_type_unit.
11751 For TUs we want to skip the first top level sibling if it's not the
11752 actual type being defined by this TU. In this case the first top
11753 level sibling is there to provide context only. */
11754
11755 static void
11756 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11757 {
11758 struct die_info *child_die;
11759
11760 prepare_one_comp_unit (cu, die, language_minimal);
11761
11762 /* Initialize (or reinitialize) the machinery for building symtabs.
11763 We do this before processing child DIEs, so that the line header table
11764 is available for DW_AT_decl_file. */
11765 cu->setup_type_unit_groups (die);
11766
11767 if (die->child != NULL)
11768 {
11769 child_die = die->child;
11770 while (child_die && child_die->tag)
11771 {
11772 process_die (child_die, cu);
11773 child_die = sibling_die (child_die);
11774 }
11775 }
11776 }
11777 \f
11778 /* DWO/DWP files.
11779
11780 http://gcc.gnu.org/wiki/DebugFission
11781 http://gcc.gnu.org/wiki/DebugFissionDWP
11782
11783 To simplify handling of both DWO files ("object" files with the DWARF info)
11784 and DWP files (a file with the DWOs packaged up into one file), we treat
11785 DWP files as having a collection of virtual DWO files. */
11786
11787 static hashval_t
11788 hash_dwo_file (const void *item)
11789 {
11790 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11791 hashval_t hash;
11792
11793 hash = htab_hash_string (dwo_file->dwo_name);
11794 if (dwo_file->comp_dir != NULL)
11795 hash += htab_hash_string (dwo_file->comp_dir);
11796 return hash;
11797 }
11798
11799 static int
11800 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11801 {
11802 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11803 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11804
11805 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11806 return 0;
11807 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11808 return lhs->comp_dir == rhs->comp_dir;
11809 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11810 }
11811
11812 /* Allocate a hash table for DWO files. */
11813
11814 static htab_up
11815 allocate_dwo_file_hash_table (struct objfile *objfile)
11816 {
11817 auto delete_dwo_file = [] (void *item)
11818 {
11819 struct dwo_file *dwo_file = (struct dwo_file *) item;
11820
11821 delete dwo_file;
11822 };
11823
11824 return htab_up (htab_create_alloc_ex (41,
11825 hash_dwo_file,
11826 eq_dwo_file,
11827 delete_dwo_file,
11828 &objfile->objfile_obstack,
11829 hashtab_obstack_allocate,
11830 dummy_obstack_deallocate));
11831 }
11832
11833 /* Lookup DWO file DWO_NAME. */
11834
11835 static void **
11836 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11837 const char *dwo_name,
11838 const char *comp_dir)
11839 {
11840 struct dwo_file find_entry;
11841 void **slot;
11842
11843 if (dwarf2_per_objfile->dwo_files == NULL)
11844 dwarf2_per_objfile->dwo_files
11845 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11846
11847 find_entry.dwo_name = dwo_name;
11848 find_entry.comp_dir = comp_dir;
11849 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11850 INSERT);
11851
11852 return slot;
11853 }
11854
11855 static hashval_t
11856 hash_dwo_unit (const void *item)
11857 {
11858 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11859
11860 /* This drops the top 32 bits of the id, but is ok for a hash. */
11861 return dwo_unit->signature;
11862 }
11863
11864 static int
11865 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11866 {
11867 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11868 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11869
11870 /* The signature is assumed to be unique within the DWO file.
11871 So while object file CU dwo_id's always have the value zero,
11872 that's OK, assuming each object file DWO file has only one CU,
11873 and that's the rule for now. */
11874 return lhs->signature == rhs->signature;
11875 }
11876
11877 /* Allocate a hash table for DWO CUs,TUs.
11878 There is one of these tables for each of CUs,TUs for each DWO file. */
11879
11880 static htab_t
11881 allocate_dwo_unit_table (struct objfile *objfile)
11882 {
11883 /* Start out with a pretty small number.
11884 Generally DWO files contain only one CU and maybe some TUs. */
11885 return htab_create_alloc_ex (3,
11886 hash_dwo_unit,
11887 eq_dwo_unit,
11888 NULL,
11889 &objfile->objfile_obstack,
11890 hashtab_obstack_allocate,
11891 dummy_obstack_deallocate);
11892 }
11893
11894 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11895
11896 struct create_dwo_cu_data
11897 {
11898 struct dwo_file *dwo_file;
11899 struct dwo_unit dwo_unit;
11900 };
11901
11902 /* die_reader_func for create_dwo_cu. */
11903
11904 static void
11905 create_dwo_cu_reader (const struct die_reader_specs *reader,
11906 const gdb_byte *info_ptr,
11907 struct die_info *comp_unit_die,
11908 int has_children,
11909 void *datap)
11910 {
11911 struct dwarf2_cu *cu = reader->cu;
11912 sect_offset sect_off = cu->per_cu->sect_off;
11913 struct dwarf2_section_info *section = cu->per_cu->section;
11914 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11915 struct dwo_file *dwo_file = data->dwo_file;
11916 struct dwo_unit *dwo_unit = &data->dwo_unit;
11917
11918 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11919 if (!signature.has_value ())
11920 {
11921 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11922 " its dwo_id [in module %s]"),
11923 sect_offset_str (sect_off), dwo_file->dwo_name);
11924 return;
11925 }
11926
11927 dwo_unit->dwo_file = dwo_file;
11928 dwo_unit->signature = *signature;
11929 dwo_unit->section = section;
11930 dwo_unit->sect_off = sect_off;
11931 dwo_unit->length = cu->per_cu->length;
11932
11933 if (dwarf_read_debug)
11934 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11935 sect_offset_str (sect_off),
11936 hex_string (dwo_unit->signature));
11937 }
11938
11939 /* Create the dwo_units for the CUs in a DWO_FILE.
11940 Note: This function processes DWO files only, not DWP files. */
11941
11942 static void
11943 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11944 struct dwo_file &dwo_file, dwarf2_section_info &section,
11945 htab_t &cus_htab)
11946 {
11947 struct objfile *objfile = dwarf2_per_objfile->objfile;
11948 const gdb_byte *info_ptr, *end_ptr;
11949
11950 dwarf2_read_section (objfile, &section);
11951 info_ptr = section.buffer;
11952
11953 if (info_ptr == NULL)
11954 return;
11955
11956 if (dwarf_read_debug)
11957 {
11958 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11959 get_section_name (&section),
11960 get_section_file_name (&section));
11961 }
11962
11963 end_ptr = info_ptr + section.size;
11964 while (info_ptr < end_ptr)
11965 {
11966 struct dwarf2_per_cu_data per_cu;
11967 struct create_dwo_cu_data create_dwo_cu_data;
11968 struct dwo_unit *dwo_unit;
11969 void **slot;
11970 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11971
11972 memset (&create_dwo_cu_data.dwo_unit, 0,
11973 sizeof (create_dwo_cu_data.dwo_unit));
11974 memset (&per_cu, 0, sizeof (per_cu));
11975 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11976 per_cu.is_debug_types = 0;
11977 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11978 per_cu.section = &section;
11979 create_dwo_cu_data.dwo_file = &dwo_file;
11980
11981 init_cutu_and_read_dies_no_follow (
11982 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11983 info_ptr += per_cu.length;
11984
11985 // If the unit could not be parsed, skip it.
11986 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11987 continue;
11988
11989 if (cus_htab == NULL)
11990 cus_htab = allocate_dwo_unit_table (objfile);
11991
11992 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11993 *dwo_unit = create_dwo_cu_data.dwo_unit;
11994 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11995 gdb_assert (slot != NULL);
11996 if (*slot != NULL)
11997 {
11998 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11999 sect_offset dup_sect_off = dup_cu->sect_off;
12000
12001 complaint (_("debug cu entry at offset %s is duplicate to"
12002 " the entry at offset %s, signature %s"),
12003 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
12004 hex_string (dwo_unit->signature));
12005 }
12006 *slot = (void *)dwo_unit;
12007 }
12008 }
12009
12010 /* DWP file .debug_{cu,tu}_index section format:
12011 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12012
12013 DWP Version 1:
12014
12015 Both index sections have the same format, and serve to map a 64-bit
12016 signature to a set of section numbers. Each section begins with a header,
12017 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12018 indexes, and a pool of 32-bit section numbers. The index sections will be
12019 aligned at 8-byte boundaries in the file.
12020
12021 The index section header consists of:
12022
12023 V, 32 bit version number
12024 -, 32 bits unused
12025 N, 32 bit number of compilation units or type units in the index
12026 M, 32 bit number of slots in the hash table
12027
12028 Numbers are recorded using the byte order of the application binary.
12029
12030 The hash table begins at offset 16 in the section, and consists of an array
12031 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12032 order of the application binary). Unused slots in the hash table are 0.
12033 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12034
12035 The parallel table begins immediately after the hash table
12036 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12037 array of 32-bit indexes (using the byte order of the application binary),
12038 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12039 table contains a 32-bit index into the pool of section numbers. For unused
12040 hash table slots, the corresponding entry in the parallel table will be 0.
12041
12042 The pool of section numbers begins immediately following the hash table
12043 (at offset 16 + 12 * M from the beginning of the section). The pool of
12044 section numbers consists of an array of 32-bit words (using the byte order
12045 of the application binary). Each item in the array is indexed starting
12046 from 0. The hash table entry provides the index of the first section
12047 number in the set. Additional section numbers in the set follow, and the
12048 set is terminated by a 0 entry (section number 0 is not used in ELF).
12049
12050 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12051 section must be the first entry in the set, and the .debug_abbrev.dwo must
12052 be the second entry. Other members of the set may follow in any order.
12053
12054 ---
12055
12056 DWP Version 2:
12057
12058 DWP Version 2 combines all the .debug_info, etc. sections into one,
12059 and the entries in the index tables are now offsets into these sections.
12060 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12061 section.
12062
12063 Index Section Contents:
12064 Header
12065 Hash Table of Signatures dwp_hash_table.hash_table
12066 Parallel Table of Indices dwp_hash_table.unit_table
12067 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12068 Table of Section Sizes dwp_hash_table.v2.sizes
12069
12070 The index section header consists of:
12071
12072 V, 32 bit version number
12073 L, 32 bit number of columns in the table of section offsets
12074 N, 32 bit number of compilation units or type units in the index
12075 M, 32 bit number of slots in the hash table
12076
12077 Numbers are recorded using the byte order of the application binary.
12078
12079 The hash table has the same format as version 1.
12080 The parallel table of indices has the same format as version 1,
12081 except that the entries are origin-1 indices into the table of sections
12082 offsets and the table of section sizes.
12083
12084 The table of offsets begins immediately following the parallel table
12085 (at offset 16 + 12 * M from the beginning of the section). The table is
12086 a two-dimensional array of 32-bit words (using the byte order of the
12087 application binary), with L columns and N+1 rows, in row-major order.
12088 Each row in the array is indexed starting from 0. The first row provides
12089 a key to the remaining rows: each column in this row provides an identifier
12090 for a debug section, and the offsets in the same column of subsequent rows
12091 refer to that section. The section identifiers are:
12092
12093 DW_SECT_INFO 1 .debug_info.dwo
12094 DW_SECT_TYPES 2 .debug_types.dwo
12095 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12096 DW_SECT_LINE 4 .debug_line.dwo
12097 DW_SECT_LOC 5 .debug_loc.dwo
12098 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12099 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12100 DW_SECT_MACRO 8 .debug_macro.dwo
12101
12102 The offsets provided by the CU and TU index sections are the base offsets
12103 for the contributions made by each CU or TU to the corresponding section
12104 in the package file. Each CU and TU header contains an abbrev_offset
12105 field, used to find the abbreviations table for that CU or TU within the
12106 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12107 be interpreted as relative to the base offset given in the index section.
12108 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12109 should be interpreted as relative to the base offset for .debug_line.dwo,
12110 and offsets into other debug sections obtained from DWARF attributes should
12111 also be interpreted as relative to the corresponding base offset.
12112
12113 The table of sizes begins immediately following the table of offsets.
12114 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12115 with L columns and N rows, in row-major order. Each row in the array is
12116 indexed starting from 1 (row 0 is shared by the two tables).
12117
12118 ---
12119
12120 Hash table lookup is handled the same in version 1 and 2:
12121
12122 We assume that N and M will not exceed 2^32 - 1.
12123 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12124
12125 Given a 64-bit compilation unit signature or a type signature S, an entry
12126 in the hash table is located as follows:
12127
12128 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12129 the low-order k bits all set to 1.
12130
12131 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12132
12133 3) If the hash table entry at index H matches the signature, use that
12134 entry. If the hash table entry at index H is unused (all zeroes),
12135 terminate the search: the signature is not present in the table.
12136
12137 4) Let H = (H + H') modulo M. Repeat at Step 3.
12138
12139 Because M > N and H' and M are relatively prime, the search is guaranteed
12140 to stop at an unused slot or find the match. */
12141
12142 /* Create a hash table to map DWO IDs to their CU/TU entry in
12143 .debug_{info,types}.dwo in DWP_FILE.
12144 Returns NULL if there isn't one.
12145 Note: This function processes DWP files only, not DWO files. */
12146
12147 static struct dwp_hash_table *
12148 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12149 struct dwp_file *dwp_file, int is_debug_types)
12150 {
12151 struct objfile *objfile = dwarf2_per_objfile->objfile;
12152 bfd *dbfd = dwp_file->dbfd.get ();
12153 const gdb_byte *index_ptr, *index_end;
12154 struct dwarf2_section_info *index;
12155 uint32_t version, nr_columns, nr_units, nr_slots;
12156 struct dwp_hash_table *htab;
12157
12158 if (is_debug_types)
12159 index = &dwp_file->sections.tu_index;
12160 else
12161 index = &dwp_file->sections.cu_index;
12162
12163 if (dwarf2_section_empty_p (index))
12164 return NULL;
12165 dwarf2_read_section (objfile, index);
12166
12167 index_ptr = index->buffer;
12168 index_end = index_ptr + index->size;
12169
12170 version = read_4_bytes (dbfd, index_ptr);
12171 index_ptr += 4;
12172 if (version == 2)
12173 nr_columns = read_4_bytes (dbfd, index_ptr);
12174 else
12175 nr_columns = 0;
12176 index_ptr += 4;
12177 nr_units = read_4_bytes (dbfd, index_ptr);
12178 index_ptr += 4;
12179 nr_slots = read_4_bytes (dbfd, index_ptr);
12180 index_ptr += 4;
12181
12182 if (version != 1 && version != 2)
12183 {
12184 error (_("Dwarf Error: unsupported DWP file version (%s)"
12185 " [in module %s]"),
12186 pulongest (version), dwp_file->name);
12187 }
12188 if (nr_slots != (nr_slots & -nr_slots))
12189 {
12190 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12191 " is not power of 2 [in module %s]"),
12192 pulongest (nr_slots), dwp_file->name);
12193 }
12194
12195 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12196 htab->version = version;
12197 htab->nr_columns = nr_columns;
12198 htab->nr_units = nr_units;
12199 htab->nr_slots = nr_slots;
12200 htab->hash_table = index_ptr;
12201 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12202
12203 /* Exit early if the table is empty. */
12204 if (nr_slots == 0 || nr_units == 0
12205 || (version == 2 && nr_columns == 0))
12206 {
12207 /* All must be zero. */
12208 if (nr_slots != 0 || nr_units != 0
12209 || (version == 2 && nr_columns != 0))
12210 {
12211 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
12212 " all zero [in modules %s]"),
12213 dwp_file->name);
12214 }
12215 return htab;
12216 }
12217
12218 if (version == 1)
12219 {
12220 htab->section_pool.v1.indices =
12221 htab->unit_table + sizeof (uint32_t) * nr_slots;
12222 /* It's harder to decide whether the section is too small in v1.
12223 V1 is deprecated anyway so we punt. */
12224 }
12225 else
12226 {
12227 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12228 int *ids = htab->section_pool.v2.section_ids;
12229 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
12230 /* Reverse map for error checking. */
12231 int ids_seen[DW_SECT_MAX + 1];
12232 int i;
12233
12234 if (nr_columns < 2)
12235 {
12236 error (_("Dwarf Error: bad DWP hash table, too few columns"
12237 " in section table [in module %s]"),
12238 dwp_file->name);
12239 }
12240 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12241 {
12242 error (_("Dwarf Error: bad DWP hash table, too many columns"
12243 " in section table [in module %s]"),
12244 dwp_file->name);
12245 }
12246 memset (ids, 255, sizeof_ids);
12247 memset (ids_seen, 255, sizeof (ids_seen));
12248 for (i = 0; i < nr_columns; ++i)
12249 {
12250 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12251
12252 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12253 {
12254 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12255 " in section table [in module %s]"),
12256 id, dwp_file->name);
12257 }
12258 if (ids_seen[id] != -1)
12259 {
12260 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12261 " id %d in section table [in module %s]"),
12262 id, dwp_file->name);
12263 }
12264 ids_seen[id] = i;
12265 ids[i] = id;
12266 }
12267 /* Must have exactly one info or types section. */
12268 if (((ids_seen[DW_SECT_INFO] != -1)
12269 + (ids_seen[DW_SECT_TYPES] != -1))
12270 != 1)
12271 {
12272 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12273 " DWO info/types section [in module %s]"),
12274 dwp_file->name);
12275 }
12276 /* Must have an abbrev section. */
12277 if (ids_seen[DW_SECT_ABBREV] == -1)
12278 {
12279 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12280 " section [in module %s]"),
12281 dwp_file->name);
12282 }
12283 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12284 htab->section_pool.v2.sizes =
12285 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12286 * nr_units * nr_columns);
12287 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12288 * nr_units * nr_columns))
12289 > index_end)
12290 {
12291 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12292 " [in module %s]"),
12293 dwp_file->name);
12294 }
12295 }
12296
12297 return htab;
12298 }
12299
12300 /* Update SECTIONS with the data from SECTP.
12301
12302 This function is like the other "locate" section routines that are
12303 passed to bfd_map_over_sections, but in this context the sections to
12304 read comes from the DWP V1 hash table, not the full ELF section table.
12305
12306 The result is non-zero for success, or zero if an error was found. */
12307
12308 static int
12309 locate_v1_virtual_dwo_sections (asection *sectp,
12310 struct virtual_v1_dwo_sections *sections)
12311 {
12312 const struct dwop_section_names *names = &dwop_section_names;
12313
12314 if (section_is_p (sectp->name, &names->abbrev_dwo))
12315 {
12316 /* There can be only one. */
12317 if (sections->abbrev.s.section != NULL)
12318 return 0;
12319 sections->abbrev.s.section = sectp;
12320 sections->abbrev.size = bfd_section_size (sectp);
12321 }
12322 else if (section_is_p (sectp->name, &names->info_dwo)
12323 || section_is_p (sectp->name, &names->types_dwo))
12324 {
12325 /* There can be only one. */
12326 if (sections->info_or_types.s.section != NULL)
12327 return 0;
12328 sections->info_or_types.s.section = sectp;
12329 sections->info_or_types.size = bfd_section_size (sectp);
12330 }
12331 else if (section_is_p (sectp->name, &names->line_dwo))
12332 {
12333 /* There can be only one. */
12334 if (sections->line.s.section != NULL)
12335 return 0;
12336 sections->line.s.section = sectp;
12337 sections->line.size = bfd_section_size (sectp);
12338 }
12339 else if (section_is_p (sectp->name, &names->loc_dwo))
12340 {
12341 /* There can be only one. */
12342 if (sections->loc.s.section != NULL)
12343 return 0;
12344 sections->loc.s.section = sectp;
12345 sections->loc.size = bfd_section_size (sectp);
12346 }
12347 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12348 {
12349 /* There can be only one. */
12350 if (sections->macinfo.s.section != NULL)
12351 return 0;
12352 sections->macinfo.s.section = sectp;
12353 sections->macinfo.size = bfd_section_size (sectp);
12354 }
12355 else if (section_is_p (sectp->name, &names->macro_dwo))
12356 {
12357 /* There can be only one. */
12358 if (sections->macro.s.section != NULL)
12359 return 0;
12360 sections->macro.s.section = sectp;
12361 sections->macro.size = bfd_section_size (sectp);
12362 }
12363 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12364 {
12365 /* There can be only one. */
12366 if (sections->str_offsets.s.section != NULL)
12367 return 0;
12368 sections->str_offsets.s.section = sectp;
12369 sections->str_offsets.size = bfd_section_size (sectp);
12370 }
12371 else
12372 {
12373 /* No other kind of section is valid. */
12374 return 0;
12375 }
12376
12377 return 1;
12378 }
12379
12380 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12381 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12382 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12383 This is for DWP version 1 files. */
12384
12385 static struct dwo_unit *
12386 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12387 struct dwp_file *dwp_file,
12388 uint32_t unit_index,
12389 const char *comp_dir,
12390 ULONGEST signature, int is_debug_types)
12391 {
12392 struct objfile *objfile = dwarf2_per_objfile->objfile;
12393 const struct dwp_hash_table *dwp_htab =
12394 is_debug_types ? dwp_file->tus : dwp_file->cus;
12395 bfd *dbfd = dwp_file->dbfd.get ();
12396 const char *kind = is_debug_types ? "TU" : "CU";
12397 struct dwo_file *dwo_file;
12398 struct dwo_unit *dwo_unit;
12399 struct virtual_v1_dwo_sections sections;
12400 void **dwo_file_slot;
12401 int i;
12402
12403 gdb_assert (dwp_file->version == 1);
12404
12405 if (dwarf_read_debug)
12406 {
12407 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12408 kind,
12409 pulongest (unit_index), hex_string (signature),
12410 dwp_file->name);
12411 }
12412
12413 /* Fetch the sections of this DWO unit.
12414 Put a limit on the number of sections we look for so that bad data
12415 doesn't cause us to loop forever. */
12416
12417 #define MAX_NR_V1_DWO_SECTIONS \
12418 (1 /* .debug_info or .debug_types */ \
12419 + 1 /* .debug_abbrev */ \
12420 + 1 /* .debug_line */ \
12421 + 1 /* .debug_loc */ \
12422 + 1 /* .debug_str_offsets */ \
12423 + 1 /* .debug_macro or .debug_macinfo */ \
12424 + 1 /* trailing zero */)
12425
12426 memset (&sections, 0, sizeof (sections));
12427
12428 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12429 {
12430 asection *sectp;
12431 uint32_t section_nr =
12432 read_4_bytes (dbfd,
12433 dwp_htab->section_pool.v1.indices
12434 + (unit_index + i) * sizeof (uint32_t));
12435
12436 if (section_nr == 0)
12437 break;
12438 if (section_nr >= dwp_file->num_sections)
12439 {
12440 error (_("Dwarf Error: bad DWP hash table, section number too large"
12441 " [in module %s]"),
12442 dwp_file->name);
12443 }
12444
12445 sectp = dwp_file->elf_sections[section_nr];
12446 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12447 {
12448 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12449 " [in module %s]"),
12450 dwp_file->name);
12451 }
12452 }
12453
12454 if (i < 2
12455 || dwarf2_section_empty_p (&sections.info_or_types)
12456 || dwarf2_section_empty_p (&sections.abbrev))
12457 {
12458 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12459 " [in module %s]"),
12460 dwp_file->name);
12461 }
12462 if (i == MAX_NR_V1_DWO_SECTIONS)
12463 {
12464 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12465 " [in module %s]"),
12466 dwp_file->name);
12467 }
12468
12469 /* It's easier for the rest of the code if we fake a struct dwo_file and
12470 have dwo_unit "live" in that. At least for now.
12471
12472 The DWP file can be made up of a random collection of CUs and TUs.
12473 However, for each CU + set of TUs that came from the same original DWO
12474 file, we can combine them back into a virtual DWO file to save space
12475 (fewer struct dwo_file objects to allocate). Remember that for really
12476 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12477
12478 std::string virtual_dwo_name =
12479 string_printf ("virtual-dwo/%d-%d-%d-%d",
12480 get_section_id (&sections.abbrev),
12481 get_section_id (&sections.line),
12482 get_section_id (&sections.loc),
12483 get_section_id (&sections.str_offsets));
12484 /* Can we use an existing virtual DWO file? */
12485 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12486 virtual_dwo_name.c_str (),
12487 comp_dir);
12488 /* Create one if necessary. */
12489 if (*dwo_file_slot == NULL)
12490 {
12491 if (dwarf_read_debug)
12492 {
12493 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12494 virtual_dwo_name.c_str ());
12495 }
12496 dwo_file = new struct dwo_file;
12497 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12498 virtual_dwo_name);
12499 dwo_file->comp_dir = comp_dir;
12500 dwo_file->sections.abbrev = sections.abbrev;
12501 dwo_file->sections.line = sections.line;
12502 dwo_file->sections.loc = sections.loc;
12503 dwo_file->sections.macinfo = sections.macinfo;
12504 dwo_file->sections.macro = sections.macro;
12505 dwo_file->sections.str_offsets = sections.str_offsets;
12506 /* The "str" section is global to the entire DWP file. */
12507 dwo_file->sections.str = dwp_file->sections.str;
12508 /* The info or types section is assigned below to dwo_unit,
12509 there's no need to record it in dwo_file.
12510 Also, we can't simply record type sections in dwo_file because
12511 we record a pointer into the vector in dwo_unit. As we collect more
12512 types we'll grow the vector and eventually have to reallocate space
12513 for it, invalidating all copies of pointers into the previous
12514 contents. */
12515 *dwo_file_slot = dwo_file;
12516 }
12517 else
12518 {
12519 if (dwarf_read_debug)
12520 {
12521 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12522 virtual_dwo_name.c_str ());
12523 }
12524 dwo_file = (struct dwo_file *) *dwo_file_slot;
12525 }
12526
12527 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12528 dwo_unit->dwo_file = dwo_file;
12529 dwo_unit->signature = signature;
12530 dwo_unit->section =
12531 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12532 *dwo_unit->section = sections.info_or_types;
12533 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12534
12535 return dwo_unit;
12536 }
12537
12538 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12539 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12540 piece within that section used by a TU/CU, return a virtual section
12541 of just that piece. */
12542
12543 static struct dwarf2_section_info
12544 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12545 struct dwarf2_section_info *section,
12546 bfd_size_type offset, bfd_size_type size)
12547 {
12548 struct dwarf2_section_info result;
12549 asection *sectp;
12550
12551 gdb_assert (section != NULL);
12552 gdb_assert (!section->is_virtual);
12553
12554 memset (&result, 0, sizeof (result));
12555 result.s.containing_section = section;
12556 result.is_virtual = true;
12557
12558 if (size == 0)
12559 return result;
12560
12561 sectp = get_section_bfd_section (section);
12562
12563 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12564 bounds of the real section. This is a pretty-rare event, so just
12565 flag an error (easier) instead of a warning and trying to cope. */
12566 if (sectp == NULL
12567 || offset + size > bfd_section_size (sectp))
12568 {
12569 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12570 " in section %s [in module %s]"),
12571 sectp ? bfd_section_name (sectp) : "<unknown>",
12572 objfile_name (dwarf2_per_objfile->objfile));
12573 }
12574
12575 result.virtual_offset = offset;
12576 result.size = size;
12577 return result;
12578 }
12579
12580 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12581 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12582 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12583 This is for DWP version 2 files. */
12584
12585 static struct dwo_unit *
12586 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12587 struct dwp_file *dwp_file,
12588 uint32_t unit_index,
12589 const char *comp_dir,
12590 ULONGEST signature, int is_debug_types)
12591 {
12592 struct objfile *objfile = dwarf2_per_objfile->objfile;
12593 const struct dwp_hash_table *dwp_htab =
12594 is_debug_types ? dwp_file->tus : dwp_file->cus;
12595 bfd *dbfd = dwp_file->dbfd.get ();
12596 const char *kind = is_debug_types ? "TU" : "CU";
12597 struct dwo_file *dwo_file;
12598 struct dwo_unit *dwo_unit;
12599 struct virtual_v2_dwo_sections sections;
12600 void **dwo_file_slot;
12601 int i;
12602
12603 gdb_assert (dwp_file->version == 2);
12604
12605 if (dwarf_read_debug)
12606 {
12607 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12608 kind,
12609 pulongest (unit_index), hex_string (signature),
12610 dwp_file->name);
12611 }
12612
12613 /* Fetch the section offsets of this DWO unit. */
12614
12615 memset (&sections, 0, sizeof (sections));
12616
12617 for (i = 0; i < dwp_htab->nr_columns; ++i)
12618 {
12619 uint32_t offset = read_4_bytes (dbfd,
12620 dwp_htab->section_pool.v2.offsets
12621 + (((unit_index - 1) * dwp_htab->nr_columns
12622 + i)
12623 * sizeof (uint32_t)));
12624 uint32_t size = read_4_bytes (dbfd,
12625 dwp_htab->section_pool.v2.sizes
12626 + (((unit_index - 1) * dwp_htab->nr_columns
12627 + i)
12628 * sizeof (uint32_t)));
12629
12630 switch (dwp_htab->section_pool.v2.section_ids[i])
12631 {
12632 case DW_SECT_INFO:
12633 case DW_SECT_TYPES:
12634 sections.info_or_types_offset = offset;
12635 sections.info_or_types_size = size;
12636 break;
12637 case DW_SECT_ABBREV:
12638 sections.abbrev_offset = offset;
12639 sections.abbrev_size = size;
12640 break;
12641 case DW_SECT_LINE:
12642 sections.line_offset = offset;
12643 sections.line_size = size;
12644 break;
12645 case DW_SECT_LOC:
12646 sections.loc_offset = offset;
12647 sections.loc_size = size;
12648 break;
12649 case DW_SECT_STR_OFFSETS:
12650 sections.str_offsets_offset = offset;
12651 sections.str_offsets_size = size;
12652 break;
12653 case DW_SECT_MACINFO:
12654 sections.macinfo_offset = offset;
12655 sections.macinfo_size = size;
12656 break;
12657 case DW_SECT_MACRO:
12658 sections.macro_offset = offset;
12659 sections.macro_size = size;
12660 break;
12661 }
12662 }
12663
12664 /* It's easier for the rest of the code if we fake a struct dwo_file and
12665 have dwo_unit "live" in that. At least for now.
12666
12667 The DWP file can be made up of a random collection of CUs and TUs.
12668 However, for each CU + set of TUs that came from the same original DWO
12669 file, we can combine them back into a virtual DWO file to save space
12670 (fewer struct dwo_file objects to allocate). Remember that for really
12671 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12672
12673 std::string virtual_dwo_name =
12674 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12675 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12676 (long) (sections.line_size ? sections.line_offset : 0),
12677 (long) (sections.loc_size ? sections.loc_offset : 0),
12678 (long) (sections.str_offsets_size
12679 ? sections.str_offsets_offset : 0));
12680 /* Can we use an existing virtual DWO file? */
12681 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12682 virtual_dwo_name.c_str (),
12683 comp_dir);
12684 /* Create one if necessary. */
12685 if (*dwo_file_slot == NULL)
12686 {
12687 if (dwarf_read_debug)
12688 {
12689 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12690 virtual_dwo_name.c_str ());
12691 }
12692 dwo_file = new struct dwo_file;
12693 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12694 virtual_dwo_name);
12695 dwo_file->comp_dir = comp_dir;
12696 dwo_file->sections.abbrev =
12697 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12698 sections.abbrev_offset, sections.abbrev_size);
12699 dwo_file->sections.line =
12700 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12701 sections.line_offset, sections.line_size);
12702 dwo_file->sections.loc =
12703 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12704 sections.loc_offset, sections.loc_size);
12705 dwo_file->sections.macinfo =
12706 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12707 sections.macinfo_offset, sections.macinfo_size);
12708 dwo_file->sections.macro =
12709 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12710 sections.macro_offset, sections.macro_size);
12711 dwo_file->sections.str_offsets =
12712 create_dwp_v2_section (dwarf2_per_objfile,
12713 &dwp_file->sections.str_offsets,
12714 sections.str_offsets_offset,
12715 sections.str_offsets_size);
12716 /* The "str" section is global to the entire DWP file. */
12717 dwo_file->sections.str = dwp_file->sections.str;
12718 /* The info or types section is assigned below to dwo_unit,
12719 there's no need to record it in dwo_file.
12720 Also, we can't simply record type sections in dwo_file because
12721 we record a pointer into the vector in dwo_unit. As we collect more
12722 types we'll grow the vector and eventually have to reallocate space
12723 for it, invalidating all copies of pointers into the previous
12724 contents. */
12725 *dwo_file_slot = dwo_file;
12726 }
12727 else
12728 {
12729 if (dwarf_read_debug)
12730 {
12731 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12732 virtual_dwo_name.c_str ());
12733 }
12734 dwo_file = (struct dwo_file *) *dwo_file_slot;
12735 }
12736
12737 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12738 dwo_unit->dwo_file = dwo_file;
12739 dwo_unit->signature = signature;
12740 dwo_unit->section =
12741 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12742 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12743 is_debug_types
12744 ? &dwp_file->sections.types
12745 : &dwp_file->sections.info,
12746 sections.info_or_types_offset,
12747 sections.info_or_types_size);
12748 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12749
12750 return dwo_unit;
12751 }
12752
12753 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12754 Returns NULL if the signature isn't found. */
12755
12756 static struct dwo_unit *
12757 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12758 struct dwp_file *dwp_file, const char *comp_dir,
12759 ULONGEST signature, int is_debug_types)
12760 {
12761 const struct dwp_hash_table *dwp_htab =
12762 is_debug_types ? dwp_file->tus : dwp_file->cus;
12763 bfd *dbfd = dwp_file->dbfd.get ();
12764 uint32_t mask = dwp_htab->nr_slots - 1;
12765 uint32_t hash = signature & mask;
12766 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12767 unsigned int i;
12768 void **slot;
12769 struct dwo_unit find_dwo_cu;
12770
12771 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12772 find_dwo_cu.signature = signature;
12773 slot = htab_find_slot (is_debug_types
12774 ? dwp_file->loaded_tus
12775 : dwp_file->loaded_cus,
12776 &find_dwo_cu, INSERT);
12777
12778 if (*slot != NULL)
12779 return (struct dwo_unit *) *slot;
12780
12781 /* Use a for loop so that we don't loop forever on bad debug info. */
12782 for (i = 0; i < dwp_htab->nr_slots; ++i)
12783 {
12784 ULONGEST signature_in_table;
12785
12786 signature_in_table =
12787 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12788 if (signature_in_table == signature)
12789 {
12790 uint32_t unit_index =
12791 read_4_bytes (dbfd,
12792 dwp_htab->unit_table + hash * sizeof (uint32_t));
12793
12794 if (dwp_file->version == 1)
12795 {
12796 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12797 dwp_file, unit_index,
12798 comp_dir, signature,
12799 is_debug_types);
12800 }
12801 else
12802 {
12803 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12804 dwp_file, unit_index,
12805 comp_dir, signature,
12806 is_debug_types);
12807 }
12808 return (struct dwo_unit *) *slot;
12809 }
12810 if (signature_in_table == 0)
12811 return NULL;
12812 hash = (hash + hash2) & mask;
12813 }
12814
12815 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12816 " [in module %s]"),
12817 dwp_file->name);
12818 }
12819
12820 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12821 Open the file specified by FILE_NAME and hand it off to BFD for
12822 preliminary analysis. Return a newly initialized bfd *, which
12823 includes a canonicalized copy of FILE_NAME.
12824 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12825 SEARCH_CWD is true if the current directory is to be searched.
12826 It will be searched before debug-file-directory.
12827 If successful, the file is added to the bfd include table of the
12828 objfile's bfd (see gdb_bfd_record_inclusion).
12829 If unable to find/open the file, return NULL.
12830 NOTE: This function is derived from symfile_bfd_open. */
12831
12832 static gdb_bfd_ref_ptr
12833 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12834 const char *file_name, int is_dwp, int search_cwd)
12835 {
12836 int desc;
12837 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12838 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12839 to debug_file_directory. */
12840 const char *search_path;
12841 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12842
12843 gdb::unique_xmalloc_ptr<char> search_path_holder;
12844 if (search_cwd)
12845 {
12846 if (*debug_file_directory != '\0')
12847 {
12848 search_path_holder.reset (concat (".", dirname_separator_string,
12849 debug_file_directory,
12850 (char *) NULL));
12851 search_path = search_path_holder.get ();
12852 }
12853 else
12854 search_path = ".";
12855 }
12856 else
12857 search_path = debug_file_directory;
12858
12859 openp_flags flags = OPF_RETURN_REALPATH;
12860 if (is_dwp)
12861 flags |= OPF_SEARCH_IN_PATH;
12862
12863 gdb::unique_xmalloc_ptr<char> absolute_name;
12864 desc = openp (search_path, flags, file_name,
12865 O_RDONLY | O_BINARY, &absolute_name);
12866 if (desc < 0)
12867 return NULL;
12868
12869 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12870 gnutarget, desc));
12871 if (sym_bfd == NULL)
12872 return NULL;
12873 bfd_set_cacheable (sym_bfd.get (), 1);
12874
12875 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12876 return NULL;
12877
12878 /* Success. Record the bfd as having been included by the objfile's bfd.
12879 This is important because things like demangled_names_hash lives in the
12880 objfile's per_bfd space and may have references to things like symbol
12881 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12882 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12883
12884 return sym_bfd;
12885 }
12886
12887 /* Try to open DWO file FILE_NAME.
12888 COMP_DIR is the DW_AT_comp_dir attribute.
12889 The result is the bfd handle of the file.
12890 If there is a problem finding or opening the file, return NULL.
12891 Upon success, the canonicalized path of the file is stored in the bfd,
12892 same as symfile_bfd_open. */
12893
12894 static gdb_bfd_ref_ptr
12895 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12896 const char *file_name, const char *comp_dir)
12897 {
12898 if (IS_ABSOLUTE_PATH (file_name))
12899 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12900 0 /*is_dwp*/, 0 /*search_cwd*/);
12901
12902 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12903
12904 if (comp_dir != NULL)
12905 {
12906 char *path_to_try = concat (comp_dir, SLASH_STRING,
12907 file_name, (char *) NULL);
12908
12909 /* NOTE: If comp_dir is a relative path, this will also try the
12910 search path, which seems useful. */
12911 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12912 path_to_try,
12913 0 /*is_dwp*/,
12914 1 /*search_cwd*/));
12915 xfree (path_to_try);
12916 if (abfd != NULL)
12917 return abfd;
12918 }
12919
12920 /* That didn't work, try debug-file-directory, which, despite its name,
12921 is a list of paths. */
12922
12923 if (*debug_file_directory == '\0')
12924 return NULL;
12925
12926 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12927 0 /*is_dwp*/, 1 /*search_cwd*/);
12928 }
12929
12930 /* This function is mapped across the sections and remembers the offset and
12931 size of each of the DWO debugging sections we are interested in. */
12932
12933 static void
12934 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12935 {
12936 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12937 const struct dwop_section_names *names = &dwop_section_names;
12938
12939 if (section_is_p (sectp->name, &names->abbrev_dwo))
12940 {
12941 dwo_sections->abbrev.s.section = sectp;
12942 dwo_sections->abbrev.size = bfd_section_size (sectp);
12943 }
12944 else if (section_is_p (sectp->name, &names->info_dwo))
12945 {
12946 dwo_sections->info.s.section = sectp;
12947 dwo_sections->info.size = bfd_section_size (sectp);
12948 }
12949 else if (section_is_p (sectp->name, &names->line_dwo))
12950 {
12951 dwo_sections->line.s.section = sectp;
12952 dwo_sections->line.size = bfd_section_size (sectp);
12953 }
12954 else if (section_is_p (sectp->name, &names->loc_dwo))
12955 {
12956 dwo_sections->loc.s.section = sectp;
12957 dwo_sections->loc.size = bfd_section_size (sectp);
12958 }
12959 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12960 {
12961 dwo_sections->macinfo.s.section = sectp;
12962 dwo_sections->macinfo.size = bfd_section_size (sectp);
12963 }
12964 else if (section_is_p (sectp->name, &names->macro_dwo))
12965 {
12966 dwo_sections->macro.s.section = sectp;
12967 dwo_sections->macro.size = bfd_section_size (sectp);
12968 }
12969 else if (section_is_p (sectp->name, &names->str_dwo))
12970 {
12971 dwo_sections->str.s.section = sectp;
12972 dwo_sections->str.size = bfd_section_size (sectp);
12973 }
12974 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12975 {
12976 dwo_sections->str_offsets.s.section = sectp;
12977 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12978 }
12979 else if (section_is_p (sectp->name, &names->types_dwo))
12980 {
12981 struct dwarf2_section_info type_section;
12982
12983 memset (&type_section, 0, sizeof (type_section));
12984 type_section.s.section = sectp;
12985 type_section.size = bfd_section_size (sectp);
12986 dwo_sections->types.push_back (type_section);
12987 }
12988 }
12989
12990 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12991 by PER_CU. This is for the non-DWP case.
12992 The result is NULL if DWO_NAME can't be found. */
12993
12994 static struct dwo_file *
12995 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12996 const char *dwo_name, const char *comp_dir)
12997 {
12998 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12999
13000 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
13001 if (dbfd == NULL)
13002 {
13003 if (dwarf_read_debug)
13004 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13005 return NULL;
13006 }
13007
13008 dwo_file_up dwo_file (new struct dwo_file);
13009 dwo_file->dwo_name = dwo_name;
13010 dwo_file->comp_dir = comp_dir;
13011 dwo_file->dbfd = std::move (dbfd);
13012
13013 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
13014 &dwo_file->sections);
13015
13016 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13017 dwo_file->cus);
13018
13019 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
13020 dwo_file->sections.types, dwo_file->tus);
13021
13022 if (dwarf_read_debug)
13023 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13024
13025 return dwo_file.release ();
13026 }
13027
13028 /* This function is mapped across the sections and remembers the offset and
13029 size of each of the DWP debugging sections common to version 1 and 2 that
13030 we are interested in. */
13031
13032 static void
13033 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13034 void *dwp_file_ptr)
13035 {
13036 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13037 const struct dwop_section_names *names = &dwop_section_names;
13038 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13039
13040 /* Record the ELF section number for later lookup: this is what the
13041 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13042 gdb_assert (elf_section_nr < dwp_file->num_sections);
13043 dwp_file->elf_sections[elf_section_nr] = sectp;
13044
13045 /* Look for specific sections that we need. */
13046 if (section_is_p (sectp->name, &names->str_dwo))
13047 {
13048 dwp_file->sections.str.s.section = sectp;
13049 dwp_file->sections.str.size = bfd_section_size (sectp);
13050 }
13051 else if (section_is_p (sectp->name, &names->cu_index))
13052 {
13053 dwp_file->sections.cu_index.s.section = sectp;
13054 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
13055 }
13056 else if (section_is_p (sectp->name, &names->tu_index))
13057 {
13058 dwp_file->sections.tu_index.s.section = sectp;
13059 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
13060 }
13061 }
13062
13063 /* This function is mapped across the sections and remembers the offset and
13064 size of each of the DWP version 2 debugging sections that we are interested
13065 in. This is split into a separate function because we don't know if we
13066 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13067
13068 static void
13069 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13070 {
13071 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13072 const struct dwop_section_names *names = &dwop_section_names;
13073 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13074
13075 /* Record the ELF section number for later lookup: this is what the
13076 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13077 gdb_assert (elf_section_nr < dwp_file->num_sections);
13078 dwp_file->elf_sections[elf_section_nr] = sectp;
13079
13080 /* Look for specific sections that we need. */
13081 if (section_is_p (sectp->name, &names->abbrev_dwo))
13082 {
13083 dwp_file->sections.abbrev.s.section = sectp;
13084 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13085 }
13086 else if (section_is_p (sectp->name, &names->info_dwo))
13087 {
13088 dwp_file->sections.info.s.section = sectp;
13089 dwp_file->sections.info.size = bfd_section_size (sectp);
13090 }
13091 else if (section_is_p (sectp->name, &names->line_dwo))
13092 {
13093 dwp_file->sections.line.s.section = sectp;
13094 dwp_file->sections.line.size = bfd_section_size (sectp);
13095 }
13096 else if (section_is_p (sectp->name, &names->loc_dwo))
13097 {
13098 dwp_file->sections.loc.s.section = sectp;
13099 dwp_file->sections.loc.size = bfd_section_size (sectp);
13100 }
13101 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13102 {
13103 dwp_file->sections.macinfo.s.section = sectp;
13104 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
13105 }
13106 else if (section_is_p (sectp->name, &names->macro_dwo))
13107 {
13108 dwp_file->sections.macro.s.section = sectp;
13109 dwp_file->sections.macro.size = bfd_section_size (sectp);
13110 }
13111 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13112 {
13113 dwp_file->sections.str_offsets.s.section = sectp;
13114 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13115 }
13116 else if (section_is_p (sectp->name, &names->types_dwo))
13117 {
13118 dwp_file->sections.types.s.section = sectp;
13119 dwp_file->sections.types.size = bfd_section_size (sectp);
13120 }
13121 }
13122
13123 /* Hash function for dwp_file loaded CUs/TUs. */
13124
13125 static hashval_t
13126 hash_dwp_loaded_cutus (const void *item)
13127 {
13128 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13129
13130 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13131 return dwo_unit->signature;
13132 }
13133
13134 /* Equality function for dwp_file loaded CUs/TUs. */
13135
13136 static int
13137 eq_dwp_loaded_cutus (const void *a, const void *b)
13138 {
13139 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13140 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13141
13142 return dua->signature == dub->signature;
13143 }
13144
13145 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13146
13147 static htab_t
13148 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13149 {
13150 return htab_create_alloc_ex (3,
13151 hash_dwp_loaded_cutus,
13152 eq_dwp_loaded_cutus,
13153 NULL,
13154 &objfile->objfile_obstack,
13155 hashtab_obstack_allocate,
13156 dummy_obstack_deallocate);
13157 }
13158
13159 /* Try to open DWP file FILE_NAME.
13160 The result is the bfd handle of the file.
13161 If there is a problem finding or opening the file, return NULL.
13162 Upon success, the canonicalized path of the file is stored in the bfd,
13163 same as symfile_bfd_open. */
13164
13165 static gdb_bfd_ref_ptr
13166 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13167 const char *file_name)
13168 {
13169 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13170 1 /*is_dwp*/,
13171 1 /*search_cwd*/));
13172 if (abfd != NULL)
13173 return abfd;
13174
13175 /* Work around upstream bug 15652.
13176 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13177 [Whether that's a "bug" is debatable, but it is getting in our way.]
13178 We have no real idea where the dwp file is, because gdb's realpath-ing
13179 of the executable's path may have discarded the needed info.
13180 [IWBN if the dwp file name was recorded in the executable, akin to
13181 .gnu_debuglink, but that doesn't exist yet.]
13182 Strip the directory from FILE_NAME and search again. */
13183 if (*debug_file_directory != '\0')
13184 {
13185 /* Don't implicitly search the current directory here.
13186 If the user wants to search "." to handle this case,
13187 it must be added to debug-file-directory. */
13188 return try_open_dwop_file (dwarf2_per_objfile,
13189 lbasename (file_name), 1 /*is_dwp*/,
13190 0 /*search_cwd*/);
13191 }
13192
13193 return NULL;
13194 }
13195
13196 /* Initialize the use of the DWP file for the current objfile.
13197 By convention the name of the DWP file is ${objfile}.dwp.
13198 The result is NULL if it can't be found. */
13199
13200 static std::unique_ptr<struct dwp_file>
13201 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13202 {
13203 struct objfile *objfile = dwarf2_per_objfile->objfile;
13204
13205 /* Try to find first .dwp for the binary file before any symbolic links
13206 resolving. */
13207
13208 /* If the objfile is a debug file, find the name of the real binary
13209 file and get the name of dwp file from there. */
13210 std::string dwp_name;
13211 if (objfile->separate_debug_objfile_backlink != NULL)
13212 {
13213 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13214 const char *backlink_basename = lbasename (backlink->original_name);
13215
13216 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13217 }
13218 else
13219 dwp_name = objfile->original_name;
13220
13221 dwp_name += ".dwp";
13222
13223 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13224 if (dbfd == NULL
13225 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13226 {
13227 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13228 dwp_name = objfile_name (objfile);
13229 dwp_name += ".dwp";
13230 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13231 }
13232
13233 if (dbfd == NULL)
13234 {
13235 if (dwarf_read_debug)
13236 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13237 return std::unique_ptr<dwp_file> ();
13238 }
13239
13240 const char *name = bfd_get_filename (dbfd.get ());
13241 std::unique_ptr<struct dwp_file> dwp_file
13242 (new struct dwp_file (name, std::move (dbfd)));
13243
13244 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13245 dwp_file->elf_sections =
13246 OBSTACK_CALLOC (&objfile->objfile_obstack,
13247 dwp_file->num_sections, asection *);
13248
13249 bfd_map_over_sections (dwp_file->dbfd.get (),
13250 dwarf2_locate_common_dwp_sections,
13251 dwp_file.get ());
13252
13253 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13254 0);
13255
13256 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13257 1);
13258
13259 /* The DWP file version is stored in the hash table. Oh well. */
13260 if (dwp_file->cus && dwp_file->tus
13261 && dwp_file->cus->version != dwp_file->tus->version)
13262 {
13263 /* Technically speaking, we should try to limp along, but this is
13264 pretty bizarre. We use pulongest here because that's the established
13265 portability solution (e.g, we cannot use %u for uint32_t). */
13266 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13267 " TU version %s [in DWP file %s]"),
13268 pulongest (dwp_file->cus->version),
13269 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13270 }
13271
13272 if (dwp_file->cus)
13273 dwp_file->version = dwp_file->cus->version;
13274 else if (dwp_file->tus)
13275 dwp_file->version = dwp_file->tus->version;
13276 else
13277 dwp_file->version = 2;
13278
13279 if (dwp_file->version == 2)
13280 bfd_map_over_sections (dwp_file->dbfd.get (),
13281 dwarf2_locate_v2_dwp_sections,
13282 dwp_file.get ());
13283
13284 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13285 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13286
13287 if (dwarf_read_debug)
13288 {
13289 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13290 fprintf_unfiltered (gdb_stdlog,
13291 " %s CUs, %s TUs\n",
13292 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13293 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13294 }
13295
13296 return dwp_file;
13297 }
13298
13299 /* Wrapper around open_and_init_dwp_file, only open it once. */
13300
13301 static struct dwp_file *
13302 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13303 {
13304 if (! dwarf2_per_objfile->dwp_checked)
13305 {
13306 dwarf2_per_objfile->dwp_file
13307 = open_and_init_dwp_file (dwarf2_per_objfile);
13308 dwarf2_per_objfile->dwp_checked = 1;
13309 }
13310 return dwarf2_per_objfile->dwp_file.get ();
13311 }
13312
13313 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13314 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13315 or in the DWP file for the objfile, referenced by THIS_UNIT.
13316 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13317 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13318
13319 This is called, for example, when wanting to read a variable with a
13320 complex location. Therefore we don't want to do file i/o for every call.
13321 Therefore we don't want to look for a DWO file on every call.
13322 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13323 then we check if we've already seen DWO_NAME, and only THEN do we check
13324 for a DWO file.
13325
13326 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13327 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13328
13329 static struct dwo_unit *
13330 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13331 const char *dwo_name, const char *comp_dir,
13332 ULONGEST signature, int is_debug_types)
13333 {
13334 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13335 struct objfile *objfile = dwarf2_per_objfile->objfile;
13336 const char *kind = is_debug_types ? "TU" : "CU";
13337 void **dwo_file_slot;
13338 struct dwo_file *dwo_file;
13339 struct dwp_file *dwp_file;
13340
13341 /* First see if there's a DWP file.
13342 If we have a DWP file but didn't find the DWO inside it, don't
13343 look for the original DWO file. It makes gdb behave differently
13344 depending on whether one is debugging in the build tree. */
13345
13346 dwp_file = get_dwp_file (dwarf2_per_objfile);
13347 if (dwp_file != NULL)
13348 {
13349 const struct dwp_hash_table *dwp_htab =
13350 is_debug_types ? dwp_file->tus : dwp_file->cus;
13351
13352 if (dwp_htab != NULL)
13353 {
13354 struct dwo_unit *dwo_cutu =
13355 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13356 signature, is_debug_types);
13357
13358 if (dwo_cutu != NULL)
13359 {
13360 if (dwarf_read_debug)
13361 {
13362 fprintf_unfiltered (gdb_stdlog,
13363 "Virtual DWO %s %s found: @%s\n",
13364 kind, hex_string (signature),
13365 host_address_to_string (dwo_cutu));
13366 }
13367 return dwo_cutu;
13368 }
13369 }
13370 }
13371 else
13372 {
13373 /* No DWP file, look for the DWO file. */
13374
13375 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13376 dwo_name, comp_dir);
13377 if (*dwo_file_slot == NULL)
13378 {
13379 /* Read in the file and build a table of the CUs/TUs it contains. */
13380 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13381 }
13382 /* NOTE: This will be NULL if unable to open the file. */
13383 dwo_file = (struct dwo_file *) *dwo_file_slot;
13384
13385 if (dwo_file != NULL)
13386 {
13387 struct dwo_unit *dwo_cutu = NULL;
13388
13389 if (is_debug_types && dwo_file->tus)
13390 {
13391 struct dwo_unit find_dwo_cutu;
13392
13393 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13394 find_dwo_cutu.signature = signature;
13395 dwo_cutu
13396 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13397 }
13398 else if (!is_debug_types && dwo_file->cus)
13399 {
13400 struct dwo_unit find_dwo_cutu;
13401
13402 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13403 find_dwo_cutu.signature = signature;
13404 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13405 &find_dwo_cutu);
13406 }
13407
13408 if (dwo_cutu != NULL)
13409 {
13410 if (dwarf_read_debug)
13411 {
13412 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13413 kind, dwo_name, hex_string (signature),
13414 host_address_to_string (dwo_cutu));
13415 }
13416 return dwo_cutu;
13417 }
13418 }
13419 }
13420
13421 /* We didn't find it. This could mean a dwo_id mismatch, or
13422 someone deleted the DWO/DWP file, or the search path isn't set up
13423 correctly to find the file. */
13424
13425 if (dwarf_read_debug)
13426 {
13427 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13428 kind, dwo_name, hex_string (signature));
13429 }
13430
13431 /* This is a warning and not a complaint because it can be caused by
13432 pilot error (e.g., user accidentally deleting the DWO). */
13433 {
13434 /* Print the name of the DWP file if we looked there, helps the user
13435 better diagnose the problem. */
13436 std::string dwp_text;
13437
13438 if (dwp_file != NULL)
13439 dwp_text = string_printf (" [in DWP file %s]",
13440 lbasename (dwp_file->name));
13441
13442 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13443 " [in module %s]"),
13444 kind, dwo_name, hex_string (signature),
13445 dwp_text.c_str (),
13446 this_unit->is_debug_types ? "TU" : "CU",
13447 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13448 }
13449 return NULL;
13450 }
13451
13452 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13453 See lookup_dwo_cutu_unit for details. */
13454
13455 static struct dwo_unit *
13456 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13457 const char *dwo_name, const char *comp_dir,
13458 ULONGEST signature)
13459 {
13460 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13461 }
13462
13463 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13464 See lookup_dwo_cutu_unit for details. */
13465
13466 static struct dwo_unit *
13467 lookup_dwo_type_unit (struct signatured_type *this_tu,
13468 const char *dwo_name, const char *comp_dir)
13469 {
13470 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13471 }
13472
13473 /* Traversal function for queue_and_load_all_dwo_tus. */
13474
13475 static int
13476 queue_and_load_dwo_tu (void **slot, void *info)
13477 {
13478 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13479 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13480 ULONGEST signature = dwo_unit->signature;
13481 struct signatured_type *sig_type =
13482 lookup_dwo_signatured_type (per_cu->cu, signature);
13483
13484 if (sig_type != NULL)
13485 {
13486 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13487
13488 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13489 a real dependency of PER_CU on SIG_TYPE. That is detected later
13490 while processing PER_CU. */
13491 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13492 load_full_type_unit (sig_cu);
13493 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13494 }
13495
13496 return 1;
13497 }
13498
13499 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13500 The DWO may have the only definition of the type, though it may not be
13501 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13502 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13503
13504 static void
13505 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13506 {
13507 struct dwo_unit *dwo_unit;
13508 struct dwo_file *dwo_file;
13509
13510 gdb_assert (!per_cu->is_debug_types);
13511 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13512 gdb_assert (per_cu->cu != NULL);
13513
13514 dwo_unit = per_cu->cu->dwo_unit;
13515 gdb_assert (dwo_unit != NULL);
13516
13517 dwo_file = dwo_unit->dwo_file;
13518 if (dwo_file->tus != NULL)
13519 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13520 }
13521
13522 /* Read in various DIEs. */
13523
13524 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13525 Inherit only the children of the DW_AT_abstract_origin DIE not being
13526 already referenced by DW_AT_abstract_origin from the children of the
13527 current DIE. */
13528
13529 static void
13530 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13531 {
13532 struct die_info *child_die;
13533 sect_offset *offsetp;
13534 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13535 struct die_info *origin_die;
13536 /* Iterator of the ORIGIN_DIE children. */
13537 struct die_info *origin_child_die;
13538 struct attribute *attr;
13539 struct dwarf2_cu *origin_cu;
13540 struct pending **origin_previous_list_in_scope;
13541
13542 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13543 if (!attr)
13544 return;
13545
13546 /* Note that following die references may follow to a die in a
13547 different cu. */
13548
13549 origin_cu = cu;
13550 origin_die = follow_die_ref (die, attr, &origin_cu);
13551
13552 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13553 symbols in. */
13554 origin_previous_list_in_scope = origin_cu->list_in_scope;
13555 origin_cu->list_in_scope = cu->list_in_scope;
13556
13557 if (die->tag != origin_die->tag
13558 && !(die->tag == DW_TAG_inlined_subroutine
13559 && origin_die->tag == DW_TAG_subprogram))
13560 complaint (_("DIE %s and its abstract origin %s have different tags"),
13561 sect_offset_str (die->sect_off),
13562 sect_offset_str (origin_die->sect_off));
13563
13564 std::vector<sect_offset> offsets;
13565
13566 for (child_die = die->child;
13567 child_die && child_die->tag;
13568 child_die = sibling_die (child_die))
13569 {
13570 struct die_info *child_origin_die;
13571 struct dwarf2_cu *child_origin_cu;
13572
13573 /* We are trying to process concrete instance entries:
13574 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13575 it's not relevant to our analysis here. i.e. detecting DIEs that are
13576 present in the abstract instance but not referenced in the concrete
13577 one. */
13578 if (child_die->tag == DW_TAG_call_site
13579 || child_die->tag == DW_TAG_GNU_call_site)
13580 continue;
13581
13582 /* For each CHILD_DIE, find the corresponding child of
13583 ORIGIN_DIE. If there is more than one layer of
13584 DW_AT_abstract_origin, follow them all; there shouldn't be,
13585 but GCC versions at least through 4.4 generate this (GCC PR
13586 40573). */
13587 child_origin_die = child_die;
13588 child_origin_cu = cu;
13589 while (1)
13590 {
13591 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13592 child_origin_cu);
13593 if (attr == NULL)
13594 break;
13595 child_origin_die = follow_die_ref (child_origin_die, attr,
13596 &child_origin_cu);
13597 }
13598
13599 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13600 counterpart may exist. */
13601 if (child_origin_die != child_die)
13602 {
13603 if (child_die->tag != child_origin_die->tag
13604 && !(child_die->tag == DW_TAG_inlined_subroutine
13605 && child_origin_die->tag == DW_TAG_subprogram))
13606 complaint (_("Child DIE %s and its abstract origin %s have "
13607 "different tags"),
13608 sect_offset_str (child_die->sect_off),
13609 sect_offset_str (child_origin_die->sect_off));
13610 if (child_origin_die->parent != origin_die)
13611 complaint (_("Child DIE %s and its abstract origin %s have "
13612 "different parents"),
13613 sect_offset_str (child_die->sect_off),
13614 sect_offset_str (child_origin_die->sect_off));
13615 else
13616 offsets.push_back (child_origin_die->sect_off);
13617 }
13618 }
13619 std::sort (offsets.begin (), offsets.end ());
13620 sect_offset *offsets_end = offsets.data () + offsets.size ();
13621 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13622 if (offsetp[-1] == *offsetp)
13623 complaint (_("Multiple children of DIE %s refer "
13624 "to DIE %s as their abstract origin"),
13625 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13626
13627 offsetp = offsets.data ();
13628 origin_child_die = origin_die->child;
13629 while (origin_child_die && origin_child_die->tag)
13630 {
13631 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13632 while (offsetp < offsets_end
13633 && *offsetp < origin_child_die->sect_off)
13634 offsetp++;
13635 if (offsetp >= offsets_end
13636 || *offsetp > origin_child_die->sect_off)
13637 {
13638 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13639 Check whether we're already processing ORIGIN_CHILD_DIE.
13640 This can happen with mutually referenced abstract_origins.
13641 PR 16581. */
13642 if (!origin_child_die->in_process)
13643 process_die (origin_child_die, origin_cu);
13644 }
13645 origin_child_die = sibling_die (origin_child_die);
13646 }
13647 origin_cu->list_in_scope = origin_previous_list_in_scope;
13648 }
13649
13650 static void
13651 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13652 {
13653 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13654 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13655 struct context_stack *newobj;
13656 CORE_ADDR lowpc;
13657 CORE_ADDR highpc;
13658 struct die_info *child_die;
13659 struct attribute *attr, *call_line, *call_file;
13660 const char *name;
13661 CORE_ADDR baseaddr;
13662 struct block *block;
13663 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13664 std::vector<struct symbol *> template_args;
13665 struct template_symbol *templ_func = NULL;
13666
13667 if (inlined_func)
13668 {
13669 /* If we do not have call site information, we can't show the
13670 caller of this inlined function. That's too confusing, so
13671 only use the scope for local variables. */
13672 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13673 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13674 if (call_line == NULL || call_file == NULL)
13675 {
13676 read_lexical_block_scope (die, cu);
13677 return;
13678 }
13679 }
13680
13681 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13682
13683 name = dwarf2_name (die, cu);
13684
13685 /* Ignore functions with missing or empty names. These are actually
13686 illegal according to the DWARF standard. */
13687 if (name == NULL)
13688 {
13689 complaint (_("missing name for subprogram DIE at %s"),
13690 sect_offset_str (die->sect_off));
13691 return;
13692 }
13693
13694 /* Ignore functions with missing or invalid low and high pc attributes. */
13695 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13696 <= PC_BOUNDS_INVALID)
13697 {
13698 attr = dwarf2_attr (die, DW_AT_external, cu);
13699 if (!attr || !DW_UNSND (attr))
13700 complaint (_("cannot get low and high bounds "
13701 "for subprogram DIE at %s"),
13702 sect_offset_str (die->sect_off));
13703 return;
13704 }
13705
13706 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13707 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13708
13709 /* If we have any template arguments, then we must allocate a
13710 different sort of symbol. */
13711 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13712 {
13713 if (child_die->tag == DW_TAG_template_type_param
13714 || child_die->tag == DW_TAG_template_value_param)
13715 {
13716 templ_func = allocate_template_symbol (objfile);
13717 templ_func->subclass = SYMBOL_TEMPLATE;
13718 break;
13719 }
13720 }
13721
13722 newobj = cu->get_builder ()->push_context (0, lowpc);
13723 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13724 (struct symbol *) templ_func);
13725
13726 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13727 set_objfile_main_name (objfile, SYMBOL_LINKAGE_NAME (newobj->name),
13728 cu->language);
13729
13730 /* If there is a location expression for DW_AT_frame_base, record
13731 it. */
13732 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13733 if (attr)
13734 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13735
13736 /* If there is a location for the static link, record it. */
13737 newobj->static_link = NULL;
13738 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13739 if (attr)
13740 {
13741 newobj->static_link
13742 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13743 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13744 dwarf2_per_cu_addr_type (cu->per_cu));
13745 }
13746
13747 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13748
13749 if (die->child != NULL)
13750 {
13751 child_die = die->child;
13752 while (child_die && child_die->tag)
13753 {
13754 if (child_die->tag == DW_TAG_template_type_param
13755 || child_die->tag == DW_TAG_template_value_param)
13756 {
13757 struct symbol *arg = new_symbol (child_die, NULL, cu);
13758
13759 if (arg != NULL)
13760 template_args.push_back (arg);
13761 }
13762 else
13763 process_die (child_die, cu);
13764 child_die = sibling_die (child_die);
13765 }
13766 }
13767
13768 inherit_abstract_dies (die, cu);
13769
13770 /* If we have a DW_AT_specification, we might need to import using
13771 directives from the context of the specification DIE. See the
13772 comment in determine_prefix. */
13773 if (cu->language == language_cplus
13774 && dwarf2_attr (die, DW_AT_specification, cu))
13775 {
13776 struct dwarf2_cu *spec_cu = cu;
13777 struct die_info *spec_die = die_specification (die, &spec_cu);
13778
13779 while (spec_die)
13780 {
13781 child_die = spec_die->child;
13782 while (child_die && child_die->tag)
13783 {
13784 if (child_die->tag == DW_TAG_imported_module)
13785 process_die (child_die, spec_cu);
13786 child_die = sibling_die (child_die);
13787 }
13788
13789 /* In some cases, GCC generates specification DIEs that
13790 themselves contain DW_AT_specification attributes. */
13791 spec_die = die_specification (spec_die, &spec_cu);
13792 }
13793 }
13794
13795 struct context_stack cstk = cu->get_builder ()->pop_context ();
13796 /* Make a block for the local symbols within. */
13797 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13798 cstk.static_link, lowpc, highpc);
13799
13800 /* For C++, set the block's scope. */
13801 if ((cu->language == language_cplus
13802 || cu->language == language_fortran
13803 || cu->language == language_d
13804 || cu->language == language_rust)
13805 && cu->processing_has_namespace_info)
13806 block_set_scope (block, determine_prefix (die, cu),
13807 &objfile->objfile_obstack);
13808
13809 /* If we have address ranges, record them. */
13810 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13811
13812 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13813
13814 /* Attach template arguments to function. */
13815 if (!template_args.empty ())
13816 {
13817 gdb_assert (templ_func != NULL);
13818
13819 templ_func->n_template_arguments = template_args.size ();
13820 templ_func->template_arguments
13821 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13822 templ_func->n_template_arguments);
13823 memcpy (templ_func->template_arguments,
13824 template_args.data (),
13825 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13826
13827 /* Make sure that the symtab is set on the new symbols. Even
13828 though they don't appear in this symtab directly, other parts
13829 of gdb assume that symbols do, and this is reasonably
13830 true. */
13831 for (symbol *sym : template_args)
13832 symbol_set_symtab (sym, symbol_symtab (templ_func));
13833 }
13834
13835 /* In C++, we can have functions nested inside functions (e.g., when
13836 a function declares a class that has methods). This means that
13837 when we finish processing a function scope, we may need to go
13838 back to building a containing block's symbol lists. */
13839 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13840 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13841
13842 /* If we've finished processing a top-level function, subsequent
13843 symbols go in the file symbol list. */
13844 if (cu->get_builder ()->outermost_context_p ())
13845 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13846 }
13847
13848 /* Process all the DIES contained within a lexical block scope. Start
13849 a new scope, process the dies, and then close the scope. */
13850
13851 static void
13852 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13853 {
13854 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13855 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13856 CORE_ADDR lowpc, highpc;
13857 struct die_info *child_die;
13858 CORE_ADDR baseaddr;
13859
13860 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13861
13862 /* Ignore blocks with missing or invalid low and high pc attributes. */
13863 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13864 as multiple lexical blocks? Handling children in a sane way would
13865 be nasty. Might be easier to properly extend generic blocks to
13866 describe ranges. */
13867 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13868 {
13869 case PC_BOUNDS_NOT_PRESENT:
13870 /* DW_TAG_lexical_block has no attributes, process its children as if
13871 there was no wrapping by that DW_TAG_lexical_block.
13872 GCC does no longer produces such DWARF since GCC r224161. */
13873 for (child_die = die->child;
13874 child_die != NULL && child_die->tag;
13875 child_die = sibling_die (child_die))
13876 process_die (child_die, cu);
13877 return;
13878 case PC_BOUNDS_INVALID:
13879 return;
13880 }
13881 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13882 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13883
13884 cu->get_builder ()->push_context (0, lowpc);
13885 if (die->child != NULL)
13886 {
13887 child_die = die->child;
13888 while (child_die && child_die->tag)
13889 {
13890 process_die (child_die, cu);
13891 child_die = sibling_die (child_die);
13892 }
13893 }
13894 inherit_abstract_dies (die, cu);
13895 struct context_stack cstk = cu->get_builder ()->pop_context ();
13896
13897 if (*cu->get_builder ()->get_local_symbols () != NULL
13898 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13899 {
13900 struct block *block
13901 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13902 cstk.start_addr, highpc);
13903
13904 /* Note that recording ranges after traversing children, as we
13905 do here, means that recording a parent's ranges entails
13906 walking across all its children's ranges as they appear in
13907 the address map, which is quadratic behavior.
13908
13909 It would be nicer to record the parent's ranges before
13910 traversing its children, simply overriding whatever you find
13911 there. But since we don't even decide whether to create a
13912 block until after we've traversed its children, that's hard
13913 to do. */
13914 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13915 }
13916 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13917 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13918 }
13919
13920 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13921
13922 static void
13923 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13924 {
13925 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13926 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13927 CORE_ADDR pc, baseaddr;
13928 struct attribute *attr;
13929 struct call_site *call_site, call_site_local;
13930 void **slot;
13931 int nparams;
13932 struct die_info *child_die;
13933
13934 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13935
13936 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13937 if (attr == NULL)
13938 {
13939 /* This was a pre-DWARF-5 GNU extension alias
13940 for DW_AT_call_return_pc. */
13941 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13942 }
13943 if (!attr)
13944 {
13945 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13946 "DIE %s [in module %s]"),
13947 sect_offset_str (die->sect_off), objfile_name (objfile));
13948 return;
13949 }
13950 pc = attr_value_as_address (attr) + baseaddr;
13951 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13952
13953 if (cu->call_site_htab == NULL)
13954 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13955 NULL, &objfile->objfile_obstack,
13956 hashtab_obstack_allocate, NULL);
13957 call_site_local.pc = pc;
13958 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13959 if (*slot != NULL)
13960 {
13961 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13962 "DIE %s [in module %s]"),
13963 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13964 objfile_name (objfile));
13965 return;
13966 }
13967
13968 /* Count parameters at the caller. */
13969
13970 nparams = 0;
13971 for (child_die = die->child; child_die && child_die->tag;
13972 child_die = sibling_die (child_die))
13973 {
13974 if (child_die->tag != DW_TAG_call_site_parameter
13975 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13976 {
13977 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13978 "DW_TAG_call_site child DIE %s [in module %s]"),
13979 child_die->tag, sect_offset_str (child_die->sect_off),
13980 objfile_name (objfile));
13981 continue;
13982 }
13983
13984 nparams++;
13985 }
13986
13987 call_site
13988 = ((struct call_site *)
13989 obstack_alloc (&objfile->objfile_obstack,
13990 sizeof (*call_site)
13991 + (sizeof (*call_site->parameter) * (nparams - 1))));
13992 *slot = call_site;
13993 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13994 call_site->pc = pc;
13995
13996 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13997 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13998 {
13999 struct die_info *func_die;
14000
14001 /* Skip also over DW_TAG_inlined_subroutine. */
14002 for (func_die = die->parent;
14003 func_die && func_die->tag != DW_TAG_subprogram
14004 && func_die->tag != DW_TAG_subroutine_type;
14005 func_die = func_die->parent);
14006
14007 /* DW_AT_call_all_calls is a superset
14008 of DW_AT_call_all_tail_calls. */
14009 if (func_die
14010 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14011 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14012 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14013 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14014 {
14015 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14016 not complete. But keep CALL_SITE for look ups via call_site_htab,
14017 both the initial caller containing the real return address PC and
14018 the final callee containing the current PC of a chain of tail
14019 calls do not need to have the tail call list complete. But any
14020 function candidate for a virtual tail call frame searched via
14021 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14022 determined unambiguously. */
14023 }
14024 else
14025 {
14026 struct type *func_type = NULL;
14027
14028 if (func_die)
14029 func_type = get_die_type (func_die, cu);
14030 if (func_type != NULL)
14031 {
14032 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14033
14034 /* Enlist this call site to the function. */
14035 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14036 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14037 }
14038 else
14039 complaint (_("Cannot find function owning DW_TAG_call_site "
14040 "DIE %s [in module %s]"),
14041 sect_offset_str (die->sect_off), objfile_name (objfile));
14042 }
14043 }
14044
14045 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14046 if (attr == NULL)
14047 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14048 if (attr == NULL)
14049 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14050 if (attr == NULL)
14051 {
14052 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14053 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14054 }
14055 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14056 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14057 /* Keep NULL DWARF_BLOCK. */;
14058 else if (attr_form_is_block (attr))
14059 {
14060 struct dwarf2_locexpr_baton *dlbaton;
14061
14062 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14063 dlbaton->data = DW_BLOCK (attr)->data;
14064 dlbaton->size = DW_BLOCK (attr)->size;
14065 dlbaton->per_cu = cu->per_cu;
14066
14067 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14068 }
14069 else if (attr_form_is_ref (attr))
14070 {
14071 struct dwarf2_cu *target_cu = cu;
14072 struct die_info *target_die;
14073
14074 target_die = follow_die_ref (die, attr, &target_cu);
14075 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14076 if (die_is_declaration (target_die, target_cu))
14077 {
14078 const char *target_physname;
14079
14080 /* Prefer the mangled name; otherwise compute the demangled one. */
14081 target_physname = dw2_linkage_name (target_die, target_cu);
14082 if (target_physname == NULL)
14083 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14084 if (target_physname == NULL)
14085 complaint (_("DW_AT_call_target target DIE has invalid "
14086 "physname, for referencing DIE %s [in module %s]"),
14087 sect_offset_str (die->sect_off), objfile_name (objfile));
14088 else
14089 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14090 }
14091 else
14092 {
14093 CORE_ADDR lowpc;
14094
14095 /* DW_AT_entry_pc should be preferred. */
14096 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14097 <= PC_BOUNDS_INVALID)
14098 complaint (_("DW_AT_call_target target DIE has invalid "
14099 "low pc, for referencing DIE %s [in module %s]"),
14100 sect_offset_str (die->sect_off), objfile_name (objfile));
14101 else
14102 {
14103 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14104 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14105 }
14106 }
14107 }
14108 else
14109 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14110 "block nor reference, for DIE %s [in module %s]"),
14111 sect_offset_str (die->sect_off), objfile_name (objfile));
14112
14113 call_site->per_cu = cu->per_cu;
14114
14115 for (child_die = die->child;
14116 child_die && child_die->tag;
14117 child_die = sibling_die (child_die))
14118 {
14119 struct call_site_parameter *parameter;
14120 struct attribute *loc, *origin;
14121
14122 if (child_die->tag != DW_TAG_call_site_parameter
14123 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14124 {
14125 /* Already printed the complaint above. */
14126 continue;
14127 }
14128
14129 gdb_assert (call_site->parameter_count < nparams);
14130 parameter = &call_site->parameter[call_site->parameter_count];
14131
14132 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14133 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14134 register is contained in DW_AT_call_value. */
14135
14136 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14137 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14138 if (origin == NULL)
14139 {
14140 /* This was a pre-DWARF-5 GNU extension alias
14141 for DW_AT_call_parameter. */
14142 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14143 }
14144 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14145 {
14146 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14147
14148 sect_offset sect_off
14149 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14150 if (!offset_in_cu_p (&cu->header, sect_off))
14151 {
14152 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14153 binding can be done only inside one CU. Such referenced DIE
14154 therefore cannot be even moved to DW_TAG_partial_unit. */
14155 complaint (_("DW_AT_call_parameter offset is not in CU for "
14156 "DW_TAG_call_site child DIE %s [in module %s]"),
14157 sect_offset_str (child_die->sect_off),
14158 objfile_name (objfile));
14159 continue;
14160 }
14161 parameter->u.param_cu_off
14162 = (cu_offset) (sect_off - cu->header.sect_off);
14163 }
14164 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14165 {
14166 complaint (_("No DW_FORM_block* DW_AT_location for "
14167 "DW_TAG_call_site child DIE %s [in module %s]"),
14168 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14169 continue;
14170 }
14171 else
14172 {
14173 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14174 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14175 if (parameter->u.dwarf_reg != -1)
14176 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14177 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14178 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14179 &parameter->u.fb_offset))
14180 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14181 else
14182 {
14183 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14184 "for DW_FORM_block* DW_AT_location is supported for "
14185 "DW_TAG_call_site child DIE %s "
14186 "[in module %s]"),
14187 sect_offset_str (child_die->sect_off),
14188 objfile_name (objfile));
14189 continue;
14190 }
14191 }
14192
14193 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14194 if (attr == NULL)
14195 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14196 if (!attr_form_is_block (attr))
14197 {
14198 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14199 "DW_TAG_call_site child DIE %s [in module %s]"),
14200 sect_offset_str (child_die->sect_off),
14201 objfile_name (objfile));
14202 continue;
14203 }
14204 parameter->value = DW_BLOCK (attr)->data;
14205 parameter->value_size = DW_BLOCK (attr)->size;
14206
14207 /* Parameters are not pre-cleared by memset above. */
14208 parameter->data_value = NULL;
14209 parameter->data_value_size = 0;
14210 call_site->parameter_count++;
14211
14212 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14213 if (attr == NULL)
14214 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14215 if (attr)
14216 {
14217 if (!attr_form_is_block (attr))
14218 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14219 "DW_TAG_call_site child DIE %s [in module %s]"),
14220 sect_offset_str (child_die->sect_off),
14221 objfile_name (objfile));
14222 else
14223 {
14224 parameter->data_value = DW_BLOCK (attr)->data;
14225 parameter->data_value_size = DW_BLOCK (attr)->size;
14226 }
14227 }
14228 }
14229 }
14230
14231 /* Helper function for read_variable. If DIE represents a virtual
14232 table, then return the type of the concrete object that is
14233 associated with the virtual table. Otherwise, return NULL. */
14234
14235 static struct type *
14236 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14237 {
14238 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14239 if (attr == NULL)
14240 return NULL;
14241
14242 /* Find the type DIE. */
14243 struct die_info *type_die = NULL;
14244 struct dwarf2_cu *type_cu = cu;
14245
14246 if (attr_form_is_ref (attr))
14247 type_die = follow_die_ref (die, attr, &type_cu);
14248 if (type_die == NULL)
14249 return NULL;
14250
14251 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14252 return NULL;
14253 return die_containing_type (type_die, type_cu);
14254 }
14255
14256 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14257
14258 static void
14259 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14260 {
14261 struct rust_vtable_symbol *storage = NULL;
14262
14263 if (cu->language == language_rust)
14264 {
14265 struct type *containing_type = rust_containing_type (die, cu);
14266
14267 if (containing_type != NULL)
14268 {
14269 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14270
14271 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14272 struct rust_vtable_symbol);
14273 initialize_objfile_symbol (storage);
14274 storage->concrete_type = containing_type;
14275 storage->subclass = SYMBOL_RUST_VTABLE;
14276 }
14277 }
14278
14279 struct symbol *res = new_symbol (die, NULL, cu, storage);
14280 struct attribute *abstract_origin
14281 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14282 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14283 if (res == NULL && loc && abstract_origin)
14284 {
14285 /* We have a variable without a name, but with a location and an abstract
14286 origin. This may be a concrete instance of an abstract variable
14287 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14288 later. */
14289 struct dwarf2_cu *origin_cu = cu;
14290 struct die_info *origin_die
14291 = follow_die_ref (die, abstract_origin, &origin_cu);
14292 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
14293 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
14294 }
14295 }
14296
14297 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14298 reading .debug_rnglists.
14299 Callback's type should be:
14300 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14301 Return true if the attributes are present and valid, otherwise,
14302 return false. */
14303
14304 template <typename Callback>
14305 static bool
14306 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14307 Callback &&callback)
14308 {
14309 struct dwarf2_per_objfile *dwarf2_per_objfile
14310 = cu->per_cu->dwarf2_per_objfile;
14311 struct objfile *objfile = dwarf2_per_objfile->objfile;
14312 bfd *obfd = objfile->obfd;
14313 /* Base address selection entry. */
14314 CORE_ADDR base;
14315 int found_base;
14316 const gdb_byte *buffer;
14317 CORE_ADDR baseaddr;
14318 bool overflow = false;
14319
14320 found_base = cu->base_known;
14321 base = cu->base_address;
14322
14323 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14324 if (offset >= dwarf2_per_objfile->rnglists.size)
14325 {
14326 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14327 offset);
14328 return false;
14329 }
14330 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14331
14332 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14333
14334 while (1)
14335 {
14336 /* Initialize it due to a false compiler warning. */
14337 CORE_ADDR range_beginning = 0, range_end = 0;
14338 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14339 + dwarf2_per_objfile->rnglists.size);
14340 unsigned int bytes_read;
14341
14342 if (buffer == buf_end)
14343 {
14344 overflow = true;
14345 break;
14346 }
14347 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14348 switch (rlet)
14349 {
14350 case DW_RLE_end_of_list:
14351 break;
14352 case DW_RLE_base_address:
14353 if (buffer + cu->header.addr_size > buf_end)
14354 {
14355 overflow = true;
14356 break;
14357 }
14358 base = read_address (obfd, buffer, cu, &bytes_read);
14359 found_base = 1;
14360 buffer += bytes_read;
14361 break;
14362 case DW_RLE_start_length:
14363 if (buffer + cu->header.addr_size > buf_end)
14364 {
14365 overflow = true;
14366 break;
14367 }
14368 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14369 buffer += bytes_read;
14370 range_end = (range_beginning
14371 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14372 buffer += bytes_read;
14373 if (buffer > buf_end)
14374 {
14375 overflow = true;
14376 break;
14377 }
14378 break;
14379 case DW_RLE_offset_pair:
14380 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14381 buffer += bytes_read;
14382 if (buffer > buf_end)
14383 {
14384 overflow = true;
14385 break;
14386 }
14387 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14388 buffer += bytes_read;
14389 if (buffer > buf_end)
14390 {
14391 overflow = true;
14392 break;
14393 }
14394 break;
14395 case DW_RLE_start_end:
14396 if (buffer + 2 * cu->header.addr_size > buf_end)
14397 {
14398 overflow = true;
14399 break;
14400 }
14401 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14402 buffer += bytes_read;
14403 range_end = read_address (obfd, buffer, cu, &bytes_read);
14404 buffer += bytes_read;
14405 break;
14406 default:
14407 complaint (_("Invalid .debug_rnglists data (no base address)"));
14408 return false;
14409 }
14410 if (rlet == DW_RLE_end_of_list || overflow)
14411 break;
14412 if (rlet == DW_RLE_base_address)
14413 continue;
14414
14415 if (!found_base)
14416 {
14417 /* We have no valid base address for the ranges
14418 data. */
14419 complaint (_("Invalid .debug_rnglists data (no base address)"));
14420 return false;
14421 }
14422
14423 if (range_beginning > range_end)
14424 {
14425 /* Inverted range entries are invalid. */
14426 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14427 return false;
14428 }
14429
14430 /* Empty range entries have no effect. */
14431 if (range_beginning == range_end)
14432 continue;
14433
14434 range_beginning += base;
14435 range_end += base;
14436
14437 /* A not-uncommon case of bad debug info.
14438 Don't pollute the addrmap with bad data. */
14439 if (range_beginning + baseaddr == 0
14440 && !dwarf2_per_objfile->has_section_at_zero)
14441 {
14442 complaint (_(".debug_rnglists entry has start address of zero"
14443 " [in module %s]"), objfile_name (objfile));
14444 continue;
14445 }
14446
14447 callback (range_beginning, range_end);
14448 }
14449
14450 if (overflow)
14451 {
14452 complaint (_("Offset %d is not terminated "
14453 "for DW_AT_ranges attribute"),
14454 offset);
14455 return false;
14456 }
14457
14458 return true;
14459 }
14460
14461 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14462 Callback's type should be:
14463 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14464 Return 1 if the attributes are present and valid, otherwise, return 0. */
14465
14466 template <typename Callback>
14467 static int
14468 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14469 Callback &&callback)
14470 {
14471 struct dwarf2_per_objfile *dwarf2_per_objfile
14472 = cu->per_cu->dwarf2_per_objfile;
14473 struct objfile *objfile = dwarf2_per_objfile->objfile;
14474 struct comp_unit_head *cu_header = &cu->header;
14475 bfd *obfd = objfile->obfd;
14476 unsigned int addr_size = cu_header->addr_size;
14477 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14478 /* Base address selection entry. */
14479 CORE_ADDR base;
14480 int found_base;
14481 unsigned int dummy;
14482 const gdb_byte *buffer;
14483 CORE_ADDR baseaddr;
14484
14485 if (cu_header->version >= 5)
14486 return dwarf2_rnglists_process (offset, cu, callback);
14487
14488 found_base = cu->base_known;
14489 base = cu->base_address;
14490
14491 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14492 if (offset >= dwarf2_per_objfile->ranges.size)
14493 {
14494 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14495 offset);
14496 return 0;
14497 }
14498 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14499
14500 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14501
14502 while (1)
14503 {
14504 CORE_ADDR range_beginning, range_end;
14505
14506 range_beginning = read_address (obfd, buffer, cu, &dummy);
14507 buffer += addr_size;
14508 range_end = read_address (obfd, buffer, cu, &dummy);
14509 buffer += addr_size;
14510 offset += 2 * addr_size;
14511
14512 /* An end of list marker is a pair of zero addresses. */
14513 if (range_beginning == 0 && range_end == 0)
14514 /* Found the end of list entry. */
14515 break;
14516
14517 /* Each base address selection entry is a pair of 2 values.
14518 The first is the largest possible address, the second is
14519 the base address. Check for a base address here. */
14520 if ((range_beginning & mask) == mask)
14521 {
14522 /* If we found the largest possible address, then we already
14523 have the base address in range_end. */
14524 base = range_end;
14525 found_base = 1;
14526 continue;
14527 }
14528
14529 if (!found_base)
14530 {
14531 /* We have no valid base address for the ranges
14532 data. */
14533 complaint (_("Invalid .debug_ranges data (no base address)"));
14534 return 0;
14535 }
14536
14537 if (range_beginning > range_end)
14538 {
14539 /* Inverted range entries are invalid. */
14540 complaint (_("Invalid .debug_ranges data (inverted range)"));
14541 return 0;
14542 }
14543
14544 /* Empty range entries have no effect. */
14545 if (range_beginning == range_end)
14546 continue;
14547
14548 range_beginning += base;
14549 range_end += base;
14550
14551 /* A not-uncommon case of bad debug info.
14552 Don't pollute the addrmap with bad data. */
14553 if (range_beginning + baseaddr == 0
14554 && !dwarf2_per_objfile->has_section_at_zero)
14555 {
14556 complaint (_(".debug_ranges entry has start address of zero"
14557 " [in module %s]"), objfile_name (objfile));
14558 continue;
14559 }
14560
14561 callback (range_beginning, range_end);
14562 }
14563
14564 return 1;
14565 }
14566
14567 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14568 Return 1 if the attributes are present and valid, otherwise, return 0.
14569 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14570
14571 static int
14572 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14573 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14574 struct partial_symtab *ranges_pst)
14575 {
14576 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14577 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14578 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14579 SECT_OFF_TEXT (objfile));
14580 int low_set = 0;
14581 CORE_ADDR low = 0;
14582 CORE_ADDR high = 0;
14583 int retval;
14584
14585 retval = dwarf2_ranges_process (offset, cu,
14586 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14587 {
14588 if (ranges_pst != NULL)
14589 {
14590 CORE_ADDR lowpc;
14591 CORE_ADDR highpc;
14592
14593 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14594 range_beginning + baseaddr)
14595 - baseaddr);
14596 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14597 range_end + baseaddr)
14598 - baseaddr);
14599 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14600 lowpc, highpc - 1, ranges_pst);
14601 }
14602
14603 /* FIXME: This is recording everything as a low-high
14604 segment of consecutive addresses. We should have a
14605 data structure for discontiguous block ranges
14606 instead. */
14607 if (! low_set)
14608 {
14609 low = range_beginning;
14610 high = range_end;
14611 low_set = 1;
14612 }
14613 else
14614 {
14615 if (range_beginning < low)
14616 low = range_beginning;
14617 if (range_end > high)
14618 high = range_end;
14619 }
14620 });
14621 if (!retval)
14622 return 0;
14623
14624 if (! low_set)
14625 /* If the first entry is an end-of-list marker, the range
14626 describes an empty scope, i.e. no instructions. */
14627 return 0;
14628
14629 if (low_return)
14630 *low_return = low;
14631 if (high_return)
14632 *high_return = high;
14633 return 1;
14634 }
14635
14636 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14637 definition for the return value. *LOWPC and *HIGHPC are set iff
14638 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14639
14640 static enum pc_bounds_kind
14641 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14642 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14643 struct partial_symtab *pst)
14644 {
14645 struct dwarf2_per_objfile *dwarf2_per_objfile
14646 = cu->per_cu->dwarf2_per_objfile;
14647 struct attribute *attr;
14648 struct attribute *attr_high;
14649 CORE_ADDR low = 0;
14650 CORE_ADDR high = 0;
14651 enum pc_bounds_kind ret;
14652
14653 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14654 if (attr_high)
14655 {
14656 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14657 if (attr)
14658 {
14659 low = attr_value_as_address (attr);
14660 high = attr_value_as_address (attr_high);
14661 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14662 high += low;
14663 }
14664 else
14665 /* Found high w/o low attribute. */
14666 return PC_BOUNDS_INVALID;
14667
14668 /* Found consecutive range of addresses. */
14669 ret = PC_BOUNDS_HIGH_LOW;
14670 }
14671 else
14672 {
14673 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14674 if (attr != NULL)
14675 {
14676 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14677 We take advantage of the fact that DW_AT_ranges does not appear
14678 in DW_TAG_compile_unit of DWO files. */
14679 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14680 unsigned int ranges_offset = (DW_UNSND (attr)
14681 + (need_ranges_base
14682 ? cu->ranges_base
14683 : 0));
14684
14685 /* Value of the DW_AT_ranges attribute is the offset in the
14686 .debug_ranges section. */
14687 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14688 return PC_BOUNDS_INVALID;
14689 /* Found discontinuous range of addresses. */
14690 ret = PC_BOUNDS_RANGES;
14691 }
14692 else
14693 return PC_BOUNDS_NOT_PRESENT;
14694 }
14695
14696 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14697 if (high <= low)
14698 return PC_BOUNDS_INVALID;
14699
14700 /* When using the GNU linker, .gnu.linkonce. sections are used to
14701 eliminate duplicate copies of functions and vtables and such.
14702 The linker will arbitrarily choose one and discard the others.
14703 The AT_*_pc values for such functions refer to local labels in
14704 these sections. If the section from that file was discarded, the
14705 labels are not in the output, so the relocs get a value of 0.
14706 If this is a discarded function, mark the pc bounds as invalid,
14707 so that GDB will ignore it. */
14708 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14709 return PC_BOUNDS_INVALID;
14710
14711 *lowpc = low;
14712 if (highpc)
14713 *highpc = high;
14714 return ret;
14715 }
14716
14717 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14718 its low and high PC addresses. Do nothing if these addresses could not
14719 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14720 and HIGHPC to the high address if greater than HIGHPC. */
14721
14722 static void
14723 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14724 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14725 struct dwarf2_cu *cu)
14726 {
14727 CORE_ADDR low, high;
14728 struct die_info *child = die->child;
14729
14730 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14731 {
14732 *lowpc = std::min (*lowpc, low);
14733 *highpc = std::max (*highpc, high);
14734 }
14735
14736 /* If the language does not allow nested subprograms (either inside
14737 subprograms or lexical blocks), we're done. */
14738 if (cu->language != language_ada)
14739 return;
14740
14741 /* Check all the children of the given DIE. If it contains nested
14742 subprograms, then check their pc bounds. Likewise, we need to
14743 check lexical blocks as well, as they may also contain subprogram
14744 definitions. */
14745 while (child && child->tag)
14746 {
14747 if (child->tag == DW_TAG_subprogram
14748 || child->tag == DW_TAG_lexical_block)
14749 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14750 child = sibling_die (child);
14751 }
14752 }
14753
14754 /* Get the low and high pc's represented by the scope DIE, and store
14755 them in *LOWPC and *HIGHPC. If the correct values can't be
14756 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14757
14758 static void
14759 get_scope_pc_bounds (struct die_info *die,
14760 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14761 struct dwarf2_cu *cu)
14762 {
14763 CORE_ADDR best_low = (CORE_ADDR) -1;
14764 CORE_ADDR best_high = (CORE_ADDR) 0;
14765 CORE_ADDR current_low, current_high;
14766
14767 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14768 >= PC_BOUNDS_RANGES)
14769 {
14770 best_low = current_low;
14771 best_high = current_high;
14772 }
14773 else
14774 {
14775 struct die_info *child = die->child;
14776
14777 while (child && child->tag)
14778 {
14779 switch (child->tag) {
14780 case DW_TAG_subprogram:
14781 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14782 break;
14783 case DW_TAG_namespace:
14784 case DW_TAG_module:
14785 /* FIXME: carlton/2004-01-16: Should we do this for
14786 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14787 that current GCC's always emit the DIEs corresponding
14788 to definitions of methods of classes as children of a
14789 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14790 the DIEs giving the declarations, which could be
14791 anywhere). But I don't see any reason why the
14792 standards says that they have to be there. */
14793 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14794
14795 if (current_low != ((CORE_ADDR) -1))
14796 {
14797 best_low = std::min (best_low, current_low);
14798 best_high = std::max (best_high, current_high);
14799 }
14800 break;
14801 default:
14802 /* Ignore. */
14803 break;
14804 }
14805
14806 child = sibling_die (child);
14807 }
14808 }
14809
14810 *lowpc = best_low;
14811 *highpc = best_high;
14812 }
14813
14814 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14815 in DIE. */
14816
14817 static void
14818 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14819 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14820 {
14821 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14822 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14823 struct attribute *attr;
14824 struct attribute *attr_high;
14825
14826 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14827 if (attr_high)
14828 {
14829 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14830 if (attr)
14831 {
14832 CORE_ADDR low = attr_value_as_address (attr);
14833 CORE_ADDR high = attr_value_as_address (attr_high);
14834
14835 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14836 high += low;
14837
14838 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14839 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14840 cu->get_builder ()->record_block_range (block, low, high - 1);
14841 }
14842 }
14843
14844 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14845 if (attr)
14846 {
14847 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14848 We take advantage of the fact that DW_AT_ranges does not appear
14849 in DW_TAG_compile_unit of DWO files. */
14850 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14851
14852 /* The value of the DW_AT_ranges attribute is the offset of the
14853 address range list in the .debug_ranges section. */
14854 unsigned long offset = (DW_UNSND (attr)
14855 + (need_ranges_base ? cu->ranges_base : 0));
14856
14857 std::vector<blockrange> blockvec;
14858 dwarf2_ranges_process (offset, cu,
14859 [&] (CORE_ADDR start, CORE_ADDR end)
14860 {
14861 start += baseaddr;
14862 end += baseaddr;
14863 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14864 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14865 cu->get_builder ()->record_block_range (block, start, end - 1);
14866 blockvec.emplace_back (start, end);
14867 });
14868
14869 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14870 }
14871 }
14872
14873 /* Check whether the producer field indicates either of GCC < 4.6, or the
14874 Intel C/C++ compiler, and cache the result in CU. */
14875
14876 static void
14877 check_producer (struct dwarf2_cu *cu)
14878 {
14879 int major, minor;
14880
14881 if (cu->producer == NULL)
14882 {
14883 /* For unknown compilers expect their behavior is DWARF version
14884 compliant.
14885
14886 GCC started to support .debug_types sections by -gdwarf-4 since
14887 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14888 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14889 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14890 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14891 }
14892 else if (producer_is_gcc (cu->producer, &major, &minor))
14893 {
14894 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14895 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14896 }
14897 else if (producer_is_icc (cu->producer, &major, &minor))
14898 {
14899 cu->producer_is_icc = true;
14900 cu->producer_is_icc_lt_14 = major < 14;
14901 }
14902 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14903 cu->producer_is_codewarrior = true;
14904 else
14905 {
14906 /* For other non-GCC compilers, expect their behavior is DWARF version
14907 compliant. */
14908 }
14909
14910 cu->checked_producer = true;
14911 }
14912
14913 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14914 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14915 during 4.6.0 experimental. */
14916
14917 static bool
14918 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14919 {
14920 if (!cu->checked_producer)
14921 check_producer (cu);
14922
14923 return cu->producer_is_gxx_lt_4_6;
14924 }
14925
14926
14927 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14928 with incorrect is_stmt attributes. */
14929
14930 static bool
14931 producer_is_codewarrior (struct dwarf2_cu *cu)
14932 {
14933 if (!cu->checked_producer)
14934 check_producer (cu);
14935
14936 return cu->producer_is_codewarrior;
14937 }
14938
14939 /* Return the default accessibility type if it is not overriden by
14940 DW_AT_accessibility. */
14941
14942 static enum dwarf_access_attribute
14943 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14944 {
14945 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14946 {
14947 /* The default DWARF 2 accessibility for members is public, the default
14948 accessibility for inheritance is private. */
14949
14950 if (die->tag != DW_TAG_inheritance)
14951 return DW_ACCESS_public;
14952 else
14953 return DW_ACCESS_private;
14954 }
14955 else
14956 {
14957 /* DWARF 3+ defines the default accessibility a different way. The same
14958 rules apply now for DW_TAG_inheritance as for the members and it only
14959 depends on the container kind. */
14960
14961 if (die->parent->tag == DW_TAG_class_type)
14962 return DW_ACCESS_private;
14963 else
14964 return DW_ACCESS_public;
14965 }
14966 }
14967
14968 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14969 offset. If the attribute was not found return 0, otherwise return
14970 1. If it was found but could not properly be handled, set *OFFSET
14971 to 0. */
14972
14973 static int
14974 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14975 LONGEST *offset)
14976 {
14977 struct attribute *attr;
14978
14979 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14980 if (attr != NULL)
14981 {
14982 *offset = 0;
14983
14984 /* Note that we do not check for a section offset first here.
14985 This is because DW_AT_data_member_location is new in DWARF 4,
14986 so if we see it, we can assume that a constant form is really
14987 a constant and not a section offset. */
14988 if (attr_form_is_constant (attr))
14989 *offset = dwarf2_get_attr_constant_value (attr, 0);
14990 else if (attr_form_is_section_offset (attr))
14991 dwarf2_complex_location_expr_complaint ();
14992 else if (attr_form_is_block (attr))
14993 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14994 else
14995 dwarf2_complex_location_expr_complaint ();
14996
14997 return 1;
14998 }
14999
15000 return 0;
15001 }
15002
15003 /* Add an aggregate field to the field list. */
15004
15005 static void
15006 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15007 struct dwarf2_cu *cu)
15008 {
15009 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15010 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15011 struct nextfield *new_field;
15012 struct attribute *attr;
15013 struct field *fp;
15014 const char *fieldname = "";
15015
15016 if (die->tag == DW_TAG_inheritance)
15017 {
15018 fip->baseclasses.emplace_back ();
15019 new_field = &fip->baseclasses.back ();
15020 }
15021 else
15022 {
15023 fip->fields.emplace_back ();
15024 new_field = &fip->fields.back ();
15025 }
15026
15027 fip->nfields++;
15028
15029 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15030 if (attr)
15031 new_field->accessibility = DW_UNSND (attr);
15032 else
15033 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15034 if (new_field->accessibility != DW_ACCESS_public)
15035 fip->non_public_fields = 1;
15036
15037 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15038 if (attr)
15039 new_field->virtuality = DW_UNSND (attr);
15040 else
15041 new_field->virtuality = DW_VIRTUALITY_none;
15042
15043 fp = &new_field->field;
15044
15045 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15046 {
15047 LONGEST offset;
15048
15049 /* Data member other than a C++ static data member. */
15050
15051 /* Get type of field. */
15052 fp->type = die_type (die, cu);
15053
15054 SET_FIELD_BITPOS (*fp, 0);
15055
15056 /* Get bit size of field (zero if none). */
15057 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15058 if (attr)
15059 {
15060 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15061 }
15062 else
15063 {
15064 FIELD_BITSIZE (*fp) = 0;
15065 }
15066
15067 /* Get bit offset of field. */
15068 if (handle_data_member_location (die, cu, &offset))
15069 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15070 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15071 if (attr)
15072 {
15073 if (gdbarch_bits_big_endian (gdbarch))
15074 {
15075 /* For big endian bits, the DW_AT_bit_offset gives the
15076 additional bit offset from the MSB of the containing
15077 anonymous object to the MSB of the field. We don't
15078 have to do anything special since we don't need to
15079 know the size of the anonymous object. */
15080 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15081 }
15082 else
15083 {
15084 /* For little endian bits, compute the bit offset to the
15085 MSB of the anonymous object, subtract off the number of
15086 bits from the MSB of the field to the MSB of the
15087 object, and then subtract off the number of bits of
15088 the field itself. The result is the bit offset of
15089 the LSB of the field. */
15090 int anonymous_size;
15091 int bit_offset = DW_UNSND (attr);
15092
15093 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15094 if (attr)
15095 {
15096 /* The size of the anonymous object containing
15097 the bit field is explicit, so use the
15098 indicated size (in bytes). */
15099 anonymous_size = DW_UNSND (attr);
15100 }
15101 else
15102 {
15103 /* The size of the anonymous object containing
15104 the bit field must be inferred from the type
15105 attribute of the data member containing the
15106 bit field. */
15107 anonymous_size = TYPE_LENGTH (fp->type);
15108 }
15109 SET_FIELD_BITPOS (*fp,
15110 (FIELD_BITPOS (*fp)
15111 + anonymous_size * bits_per_byte
15112 - bit_offset - FIELD_BITSIZE (*fp)));
15113 }
15114 }
15115 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15116 if (attr != NULL)
15117 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15118 + dwarf2_get_attr_constant_value (attr, 0)));
15119
15120 /* Get name of field. */
15121 fieldname = dwarf2_name (die, cu);
15122 if (fieldname == NULL)
15123 fieldname = "";
15124
15125 /* The name is already allocated along with this objfile, so we don't
15126 need to duplicate it for the type. */
15127 fp->name = fieldname;
15128
15129 /* Change accessibility for artificial fields (e.g. virtual table
15130 pointer or virtual base class pointer) to private. */
15131 if (dwarf2_attr (die, DW_AT_artificial, cu))
15132 {
15133 FIELD_ARTIFICIAL (*fp) = 1;
15134 new_field->accessibility = DW_ACCESS_private;
15135 fip->non_public_fields = 1;
15136 }
15137 }
15138 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15139 {
15140 /* C++ static member. */
15141
15142 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15143 is a declaration, but all versions of G++ as of this writing
15144 (so through at least 3.2.1) incorrectly generate
15145 DW_TAG_variable tags. */
15146
15147 const char *physname;
15148
15149 /* Get name of field. */
15150 fieldname = dwarf2_name (die, cu);
15151 if (fieldname == NULL)
15152 return;
15153
15154 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15155 if (attr
15156 /* Only create a symbol if this is an external value.
15157 new_symbol checks this and puts the value in the global symbol
15158 table, which we want. If it is not external, new_symbol
15159 will try to put the value in cu->list_in_scope which is wrong. */
15160 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15161 {
15162 /* A static const member, not much different than an enum as far as
15163 we're concerned, except that we can support more types. */
15164 new_symbol (die, NULL, cu);
15165 }
15166
15167 /* Get physical name. */
15168 physname = dwarf2_physname (fieldname, die, cu);
15169
15170 /* The name is already allocated along with this objfile, so we don't
15171 need to duplicate it for the type. */
15172 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15173 FIELD_TYPE (*fp) = die_type (die, cu);
15174 FIELD_NAME (*fp) = fieldname;
15175 }
15176 else if (die->tag == DW_TAG_inheritance)
15177 {
15178 LONGEST offset;
15179
15180 /* C++ base class field. */
15181 if (handle_data_member_location (die, cu, &offset))
15182 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15183 FIELD_BITSIZE (*fp) = 0;
15184 FIELD_TYPE (*fp) = die_type (die, cu);
15185 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
15186 }
15187 else if (die->tag == DW_TAG_variant_part)
15188 {
15189 /* process_structure_scope will treat this DIE as a union. */
15190 process_structure_scope (die, cu);
15191
15192 /* The variant part is relative to the start of the enclosing
15193 structure. */
15194 SET_FIELD_BITPOS (*fp, 0);
15195 fp->type = get_die_type (die, cu);
15196 fp->artificial = 1;
15197 fp->name = "<<variant>>";
15198
15199 /* Normally a DW_TAG_variant_part won't have a size, but our
15200 representation requires one, so set it to the maximum of the
15201 child sizes. */
15202 if (TYPE_LENGTH (fp->type) == 0)
15203 {
15204 unsigned max = 0;
15205 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15206 if (TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)) > max)
15207 max = TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i));
15208 TYPE_LENGTH (fp->type) = max;
15209 }
15210 }
15211 else
15212 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15213 }
15214
15215 /* Can the type given by DIE define another type? */
15216
15217 static bool
15218 type_can_define_types (const struct die_info *die)
15219 {
15220 switch (die->tag)
15221 {
15222 case DW_TAG_typedef:
15223 case DW_TAG_class_type:
15224 case DW_TAG_structure_type:
15225 case DW_TAG_union_type:
15226 case DW_TAG_enumeration_type:
15227 return true;
15228
15229 default:
15230 return false;
15231 }
15232 }
15233
15234 /* Add a type definition defined in the scope of the FIP's class. */
15235
15236 static void
15237 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15238 struct dwarf2_cu *cu)
15239 {
15240 struct decl_field fp;
15241 memset (&fp, 0, sizeof (fp));
15242
15243 gdb_assert (type_can_define_types (die));
15244
15245 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15246 fp.name = dwarf2_name (die, cu);
15247 fp.type = read_type_die (die, cu);
15248
15249 /* Save accessibility. */
15250 enum dwarf_access_attribute accessibility;
15251 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15252 if (attr != NULL)
15253 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15254 else
15255 accessibility = dwarf2_default_access_attribute (die, cu);
15256 switch (accessibility)
15257 {
15258 case DW_ACCESS_public:
15259 /* The assumed value if neither private nor protected. */
15260 break;
15261 case DW_ACCESS_private:
15262 fp.is_private = 1;
15263 break;
15264 case DW_ACCESS_protected:
15265 fp.is_protected = 1;
15266 break;
15267 default:
15268 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15269 }
15270
15271 if (die->tag == DW_TAG_typedef)
15272 fip->typedef_field_list.push_back (fp);
15273 else
15274 fip->nested_types_list.push_back (fp);
15275 }
15276
15277 /* Create the vector of fields, and attach it to the type. */
15278
15279 static void
15280 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15281 struct dwarf2_cu *cu)
15282 {
15283 int nfields = fip->nfields;
15284
15285 /* Record the field count, allocate space for the array of fields,
15286 and create blank accessibility bitfields if necessary. */
15287 TYPE_NFIELDS (type) = nfields;
15288 TYPE_FIELDS (type) = (struct field *)
15289 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15290
15291 if (fip->non_public_fields && cu->language != language_ada)
15292 {
15293 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15294
15295 TYPE_FIELD_PRIVATE_BITS (type) =
15296 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15297 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15298
15299 TYPE_FIELD_PROTECTED_BITS (type) =
15300 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15301 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15302
15303 TYPE_FIELD_IGNORE_BITS (type) =
15304 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15305 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15306 }
15307
15308 /* If the type has baseclasses, allocate and clear a bit vector for
15309 TYPE_FIELD_VIRTUAL_BITS. */
15310 if (!fip->baseclasses.empty () && cu->language != language_ada)
15311 {
15312 int num_bytes = B_BYTES (fip->baseclasses.size ());
15313 unsigned char *pointer;
15314
15315 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15316 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15317 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15318 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15319 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15320 }
15321
15322 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15323 {
15324 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15325
15326 for (int index = 0; index < nfields; ++index)
15327 {
15328 struct nextfield &field = fip->fields[index];
15329
15330 if (field.variant.is_discriminant)
15331 di->discriminant_index = index;
15332 else if (field.variant.default_branch)
15333 di->default_index = index;
15334 else
15335 di->discriminants[index] = field.variant.discriminant_value;
15336 }
15337 }
15338
15339 /* Copy the saved-up fields into the field vector. */
15340 for (int i = 0; i < nfields; ++i)
15341 {
15342 struct nextfield &field
15343 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15344 : fip->fields[i - fip->baseclasses.size ()]);
15345
15346 TYPE_FIELD (type, i) = field.field;
15347 switch (field.accessibility)
15348 {
15349 case DW_ACCESS_private:
15350 if (cu->language != language_ada)
15351 SET_TYPE_FIELD_PRIVATE (type, i);
15352 break;
15353
15354 case DW_ACCESS_protected:
15355 if (cu->language != language_ada)
15356 SET_TYPE_FIELD_PROTECTED (type, i);
15357 break;
15358
15359 case DW_ACCESS_public:
15360 break;
15361
15362 default:
15363 /* Unknown accessibility. Complain and treat it as public. */
15364 {
15365 complaint (_("unsupported accessibility %d"),
15366 field.accessibility);
15367 }
15368 break;
15369 }
15370 if (i < fip->baseclasses.size ())
15371 {
15372 switch (field.virtuality)
15373 {
15374 case DW_VIRTUALITY_virtual:
15375 case DW_VIRTUALITY_pure_virtual:
15376 if (cu->language == language_ada)
15377 error (_("unexpected virtuality in component of Ada type"));
15378 SET_TYPE_FIELD_VIRTUAL (type, i);
15379 break;
15380 }
15381 }
15382 }
15383 }
15384
15385 /* Return true if this member function is a constructor, false
15386 otherwise. */
15387
15388 static int
15389 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15390 {
15391 const char *fieldname;
15392 const char *type_name;
15393 int len;
15394
15395 if (die->parent == NULL)
15396 return 0;
15397
15398 if (die->parent->tag != DW_TAG_structure_type
15399 && die->parent->tag != DW_TAG_union_type
15400 && die->parent->tag != DW_TAG_class_type)
15401 return 0;
15402
15403 fieldname = dwarf2_name (die, cu);
15404 type_name = dwarf2_name (die->parent, cu);
15405 if (fieldname == NULL || type_name == NULL)
15406 return 0;
15407
15408 len = strlen (fieldname);
15409 return (strncmp (fieldname, type_name, len) == 0
15410 && (type_name[len] == '\0' || type_name[len] == '<'));
15411 }
15412
15413 /* Add a member function to the proper fieldlist. */
15414
15415 static void
15416 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15417 struct type *type, struct dwarf2_cu *cu)
15418 {
15419 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15420 struct attribute *attr;
15421 int i;
15422 struct fnfieldlist *flp = nullptr;
15423 struct fn_field *fnp;
15424 const char *fieldname;
15425 struct type *this_type;
15426 enum dwarf_access_attribute accessibility;
15427
15428 if (cu->language == language_ada)
15429 error (_("unexpected member function in Ada type"));
15430
15431 /* Get name of member function. */
15432 fieldname = dwarf2_name (die, cu);
15433 if (fieldname == NULL)
15434 return;
15435
15436 /* Look up member function name in fieldlist. */
15437 for (i = 0; i < fip->fnfieldlists.size (); i++)
15438 {
15439 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15440 {
15441 flp = &fip->fnfieldlists[i];
15442 break;
15443 }
15444 }
15445
15446 /* Create a new fnfieldlist if necessary. */
15447 if (flp == nullptr)
15448 {
15449 fip->fnfieldlists.emplace_back ();
15450 flp = &fip->fnfieldlists.back ();
15451 flp->name = fieldname;
15452 i = fip->fnfieldlists.size () - 1;
15453 }
15454
15455 /* Create a new member function field and add it to the vector of
15456 fnfieldlists. */
15457 flp->fnfields.emplace_back ();
15458 fnp = &flp->fnfields.back ();
15459
15460 /* Delay processing of the physname until later. */
15461 if (cu->language == language_cplus)
15462 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15463 die, cu);
15464 else
15465 {
15466 const char *physname = dwarf2_physname (fieldname, die, cu);
15467 fnp->physname = physname ? physname : "";
15468 }
15469
15470 fnp->type = alloc_type (objfile);
15471 this_type = read_type_die (die, cu);
15472 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15473 {
15474 int nparams = TYPE_NFIELDS (this_type);
15475
15476 /* TYPE is the domain of this method, and THIS_TYPE is the type
15477 of the method itself (TYPE_CODE_METHOD). */
15478 smash_to_method_type (fnp->type, type,
15479 TYPE_TARGET_TYPE (this_type),
15480 TYPE_FIELDS (this_type),
15481 TYPE_NFIELDS (this_type),
15482 TYPE_VARARGS (this_type));
15483
15484 /* Handle static member functions.
15485 Dwarf2 has no clean way to discern C++ static and non-static
15486 member functions. G++ helps GDB by marking the first
15487 parameter for non-static member functions (which is the this
15488 pointer) as artificial. We obtain this information from
15489 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15490 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15491 fnp->voffset = VOFFSET_STATIC;
15492 }
15493 else
15494 complaint (_("member function type missing for '%s'"),
15495 dwarf2_full_name (fieldname, die, cu));
15496
15497 /* Get fcontext from DW_AT_containing_type if present. */
15498 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15499 fnp->fcontext = die_containing_type (die, cu);
15500
15501 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15502 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15503
15504 /* Get accessibility. */
15505 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15506 if (attr)
15507 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15508 else
15509 accessibility = dwarf2_default_access_attribute (die, cu);
15510 switch (accessibility)
15511 {
15512 case DW_ACCESS_private:
15513 fnp->is_private = 1;
15514 break;
15515 case DW_ACCESS_protected:
15516 fnp->is_protected = 1;
15517 break;
15518 }
15519
15520 /* Check for artificial methods. */
15521 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15522 if (attr && DW_UNSND (attr) != 0)
15523 fnp->is_artificial = 1;
15524
15525 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15526
15527 /* Get index in virtual function table if it is a virtual member
15528 function. For older versions of GCC, this is an offset in the
15529 appropriate virtual table, as specified by DW_AT_containing_type.
15530 For everyone else, it is an expression to be evaluated relative
15531 to the object address. */
15532
15533 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15534 if (attr)
15535 {
15536 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15537 {
15538 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15539 {
15540 /* Old-style GCC. */
15541 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15542 }
15543 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15544 || (DW_BLOCK (attr)->size > 1
15545 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15546 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15547 {
15548 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15549 if ((fnp->voffset % cu->header.addr_size) != 0)
15550 dwarf2_complex_location_expr_complaint ();
15551 else
15552 fnp->voffset /= cu->header.addr_size;
15553 fnp->voffset += 2;
15554 }
15555 else
15556 dwarf2_complex_location_expr_complaint ();
15557
15558 if (!fnp->fcontext)
15559 {
15560 /* If there is no `this' field and no DW_AT_containing_type,
15561 we cannot actually find a base class context for the
15562 vtable! */
15563 if (TYPE_NFIELDS (this_type) == 0
15564 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15565 {
15566 complaint (_("cannot determine context for virtual member "
15567 "function \"%s\" (offset %s)"),
15568 fieldname, sect_offset_str (die->sect_off));
15569 }
15570 else
15571 {
15572 fnp->fcontext
15573 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15574 }
15575 }
15576 }
15577 else if (attr_form_is_section_offset (attr))
15578 {
15579 dwarf2_complex_location_expr_complaint ();
15580 }
15581 else
15582 {
15583 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15584 fieldname);
15585 }
15586 }
15587 else
15588 {
15589 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15590 if (attr && DW_UNSND (attr))
15591 {
15592 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15593 complaint (_("Member function \"%s\" (offset %s) is virtual "
15594 "but the vtable offset is not specified"),
15595 fieldname, sect_offset_str (die->sect_off));
15596 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15597 TYPE_CPLUS_DYNAMIC (type) = 1;
15598 }
15599 }
15600 }
15601
15602 /* Create the vector of member function fields, and attach it to the type. */
15603
15604 static void
15605 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15606 struct dwarf2_cu *cu)
15607 {
15608 if (cu->language == language_ada)
15609 error (_("unexpected member functions in Ada type"));
15610
15611 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15612 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15613 TYPE_ALLOC (type,
15614 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15615
15616 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15617 {
15618 struct fnfieldlist &nf = fip->fnfieldlists[i];
15619 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15620
15621 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15622 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15623 fn_flp->fn_fields = (struct fn_field *)
15624 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15625
15626 for (int k = 0; k < nf.fnfields.size (); ++k)
15627 fn_flp->fn_fields[k] = nf.fnfields[k];
15628 }
15629
15630 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15631 }
15632
15633 /* Returns non-zero if NAME is the name of a vtable member in CU's
15634 language, zero otherwise. */
15635 static int
15636 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15637 {
15638 static const char vptr[] = "_vptr";
15639
15640 /* Look for the C++ form of the vtable. */
15641 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15642 return 1;
15643
15644 return 0;
15645 }
15646
15647 /* GCC outputs unnamed structures that are really pointers to member
15648 functions, with the ABI-specified layout. If TYPE describes
15649 such a structure, smash it into a member function type.
15650
15651 GCC shouldn't do this; it should just output pointer to member DIEs.
15652 This is GCC PR debug/28767. */
15653
15654 static void
15655 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15656 {
15657 struct type *pfn_type, *self_type, *new_type;
15658
15659 /* Check for a structure with no name and two children. */
15660 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15661 return;
15662
15663 /* Check for __pfn and __delta members. */
15664 if (TYPE_FIELD_NAME (type, 0) == NULL
15665 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15666 || TYPE_FIELD_NAME (type, 1) == NULL
15667 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15668 return;
15669
15670 /* Find the type of the method. */
15671 pfn_type = TYPE_FIELD_TYPE (type, 0);
15672 if (pfn_type == NULL
15673 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15674 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15675 return;
15676
15677 /* Look for the "this" argument. */
15678 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15679 if (TYPE_NFIELDS (pfn_type) == 0
15680 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15681 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15682 return;
15683
15684 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15685 new_type = alloc_type (objfile);
15686 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15687 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15688 TYPE_VARARGS (pfn_type));
15689 smash_to_methodptr_type (type, new_type);
15690 }
15691
15692 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15693 appropriate error checking and issuing complaints if there is a
15694 problem. */
15695
15696 static ULONGEST
15697 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15698 {
15699 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15700
15701 if (attr == nullptr)
15702 return 0;
15703
15704 if (!attr_form_is_constant (attr))
15705 {
15706 complaint (_("DW_AT_alignment must have constant form"
15707 " - DIE at %s [in module %s]"),
15708 sect_offset_str (die->sect_off),
15709 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15710 return 0;
15711 }
15712
15713 ULONGEST align;
15714 if (attr->form == DW_FORM_sdata)
15715 {
15716 LONGEST val = DW_SND (attr);
15717 if (val < 0)
15718 {
15719 complaint (_("DW_AT_alignment value must not be negative"
15720 " - DIE at %s [in module %s]"),
15721 sect_offset_str (die->sect_off),
15722 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15723 return 0;
15724 }
15725 align = val;
15726 }
15727 else
15728 align = DW_UNSND (attr);
15729
15730 if (align == 0)
15731 {
15732 complaint (_("DW_AT_alignment value must not be zero"
15733 " - DIE at %s [in module %s]"),
15734 sect_offset_str (die->sect_off),
15735 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15736 return 0;
15737 }
15738 if ((align & (align - 1)) != 0)
15739 {
15740 complaint (_("DW_AT_alignment value must be a power of 2"
15741 " - DIE at %s [in module %s]"),
15742 sect_offset_str (die->sect_off),
15743 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15744 return 0;
15745 }
15746
15747 return align;
15748 }
15749
15750 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15751 the alignment for TYPE. */
15752
15753 static void
15754 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15755 struct type *type)
15756 {
15757 if (!set_type_align (type, get_alignment (cu, die)))
15758 complaint (_("DW_AT_alignment value too large"
15759 " - DIE at %s [in module %s]"),
15760 sect_offset_str (die->sect_off),
15761 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15762 }
15763
15764 /* Called when we find the DIE that starts a structure or union scope
15765 (definition) to create a type for the structure or union. Fill in
15766 the type's name and general properties; the members will not be
15767 processed until process_structure_scope. A symbol table entry for
15768 the type will also not be done until process_structure_scope (assuming
15769 the type has a name).
15770
15771 NOTE: we need to call these functions regardless of whether or not the
15772 DIE has a DW_AT_name attribute, since it might be an anonymous
15773 structure or union. This gets the type entered into our set of
15774 user defined types. */
15775
15776 static struct type *
15777 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15778 {
15779 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15780 struct type *type;
15781 struct attribute *attr;
15782 const char *name;
15783
15784 /* If the definition of this type lives in .debug_types, read that type.
15785 Don't follow DW_AT_specification though, that will take us back up
15786 the chain and we want to go down. */
15787 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15788 if (attr)
15789 {
15790 type = get_DW_AT_signature_type (die, attr, cu);
15791
15792 /* The type's CU may not be the same as CU.
15793 Ensure TYPE is recorded with CU in die_type_hash. */
15794 return set_die_type (die, type, cu);
15795 }
15796
15797 type = alloc_type (objfile);
15798 INIT_CPLUS_SPECIFIC (type);
15799
15800 name = dwarf2_name (die, cu);
15801 if (name != NULL)
15802 {
15803 if (cu->language == language_cplus
15804 || cu->language == language_d
15805 || cu->language == language_rust)
15806 {
15807 const char *full_name = dwarf2_full_name (name, die, cu);
15808
15809 /* dwarf2_full_name might have already finished building the DIE's
15810 type. If so, there is no need to continue. */
15811 if (get_die_type (die, cu) != NULL)
15812 return get_die_type (die, cu);
15813
15814 TYPE_NAME (type) = full_name;
15815 }
15816 else
15817 {
15818 /* The name is already allocated along with this objfile, so
15819 we don't need to duplicate it for the type. */
15820 TYPE_NAME (type) = name;
15821 }
15822 }
15823
15824 if (die->tag == DW_TAG_structure_type)
15825 {
15826 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15827 }
15828 else if (die->tag == DW_TAG_union_type)
15829 {
15830 TYPE_CODE (type) = TYPE_CODE_UNION;
15831 }
15832 else if (die->tag == DW_TAG_variant_part)
15833 {
15834 TYPE_CODE (type) = TYPE_CODE_UNION;
15835 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15836 }
15837 else
15838 {
15839 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15840 }
15841
15842 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15843 TYPE_DECLARED_CLASS (type) = 1;
15844
15845 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15846 if (attr)
15847 {
15848 if (attr_form_is_constant (attr))
15849 TYPE_LENGTH (type) = DW_UNSND (attr);
15850 else
15851 {
15852 /* For the moment, dynamic type sizes are not supported
15853 by GDB's struct type. The actual size is determined
15854 on-demand when resolving the type of a given object,
15855 so set the type's length to zero for now. Otherwise,
15856 we record an expression as the length, and that expression
15857 could lead to a very large value, which could eventually
15858 lead to us trying to allocate that much memory when creating
15859 a value of that type. */
15860 TYPE_LENGTH (type) = 0;
15861 }
15862 }
15863 else
15864 {
15865 TYPE_LENGTH (type) = 0;
15866 }
15867
15868 maybe_set_alignment (cu, die, type);
15869
15870 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15871 {
15872 /* ICC<14 does not output the required DW_AT_declaration on
15873 incomplete types, but gives them a size of zero. */
15874 TYPE_STUB (type) = 1;
15875 }
15876 else
15877 TYPE_STUB_SUPPORTED (type) = 1;
15878
15879 if (die_is_declaration (die, cu))
15880 TYPE_STUB (type) = 1;
15881 else if (attr == NULL && die->child == NULL
15882 && producer_is_realview (cu->producer))
15883 /* RealView does not output the required DW_AT_declaration
15884 on incomplete types. */
15885 TYPE_STUB (type) = 1;
15886
15887 /* We need to add the type field to the die immediately so we don't
15888 infinitely recurse when dealing with pointers to the structure
15889 type within the structure itself. */
15890 set_die_type (die, type, cu);
15891
15892 /* set_die_type should be already done. */
15893 set_descriptive_type (type, die, cu);
15894
15895 return type;
15896 }
15897
15898 /* A helper for process_structure_scope that handles a single member
15899 DIE. */
15900
15901 static void
15902 handle_struct_member_die (struct die_info *child_die, struct type *type,
15903 struct field_info *fi,
15904 std::vector<struct symbol *> *template_args,
15905 struct dwarf2_cu *cu)
15906 {
15907 if (child_die->tag == DW_TAG_member
15908 || child_die->tag == DW_TAG_variable
15909 || child_die->tag == DW_TAG_variant_part)
15910 {
15911 /* NOTE: carlton/2002-11-05: A C++ static data member
15912 should be a DW_TAG_member that is a declaration, but
15913 all versions of G++ as of this writing (so through at
15914 least 3.2.1) incorrectly generate DW_TAG_variable
15915 tags for them instead. */
15916 dwarf2_add_field (fi, child_die, cu);
15917 }
15918 else if (child_die->tag == DW_TAG_subprogram)
15919 {
15920 /* Rust doesn't have member functions in the C++ sense.
15921 However, it does emit ordinary functions as children
15922 of a struct DIE. */
15923 if (cu->language == language_rust)
15924 read_func_scope (child_die, cu);
15925 else
15926 {
15927 /* C++ member function. */
15928 dwarf2_add_member_fn (fi, child_die, type, cu);
15929 }
15930 }
15931 else if (child_die->tag == DW_TAG_inheritance)
15932 {
15933 /* C++ base class field. */
15934 dwarf2_add_field (fi, child_die, cu);
15935 }
15936 else if (type_can_define_types (child_die))
15937 dwarf2_add_type_defn (fi, child_die, cu);
15938 else if (child_die->tag == DW_TAG_template_type_param
15939 || child_die->tag == DW_TAG_template_value_param)
15940 {
15941 struct symbol *arg = new_symbol (child_die, NULL, cu);
15942
15943 if (arg != NULL)
15944 template_args->push_back (arg);
15945 }
15946 else if (child_die->tag == DW_TAG_variant)
15947 {
15948 /* In a variant we want to get the discriminant and also add a
15949 field for our sole member child. */
15950 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15951
15952 for (die_info *variant_child = child_die->child;
15953 variant_child != NULL;
15954 variant_child = sibling_die (variant_child))
15955 {
15956 if (variant_child->tag == DW_TAG_member)
15957 {
15958 handle_struct_member_die (variant_child, type, fi,
15959 template_args, cu);
15960 /* Only handle the one. */
15961 break;
15962 }
15963 }
15964
15965 /* We don't handle this but we might as well report it if we see
15966 it. */
15967 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15968 complaint (_("DW_AT_discr_list is not supported yet"
15969 " - DIE at %s [in module %s]"),
15970 sect_offset_str (child_die->sect_off),
15971 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15972
15973 /* The first field was just added, so we can stash the
15974 discriminant there. */
15975 gdb_assert (!fi->fields.empty ());
15976 if (discr == NULL)
15977 fi->fields.back ().variant.default_branch = true;
15978 else
15979 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15980 }
15981 }
15982
15983 /* Finish creating a structure or union type, including filling in
15984 its members and creating a symbol for it. */
15985
15986 static void
15987 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15988 {
15989 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15990 struct die_info *child_die;
15991 struct type *type;
15992
15993 type = get_die_type (die, cu);
15994 if (type == NULL)
15995 type = read_structure_type (die, cu);
15996
15997 /* When reading a DW_TAG_variant_part, we need to notice when we
15998 read the discriminant member, so we can record it later in the
15999 discriminant_info. */
16000 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16001 sect_offset discr_offset;
16002 bool has_template_parameters = false;
16003
16004 if (is_variant_part)
16005 {
16006 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16007 if (discr == NULL)
16008 {
16009 /* Maybe it's a univariant form, an extension we support.
16010 In this case arrange not to check the offset. */
16011 is_variant_part = false;
16012 }
16013 else if (attr_form_is_ref (discr))
16014 {
16015 struct dwarf2_cu *target_cu = cu;
16016 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16017
16018 discr_offset = target_die->sect_off;
16019 }
16020 else
16021 {
16022 complaint (_("DW_AT_discr does not have DIE reference form"
16023 " - DIE at %s [in module %s]"),
16024 sect_offset_str (die->sect_off),
16025 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16026 is_variant_part = false;
16027 }
16028 }
16029
16030 if (die->child != NULL && ! die_is_declaration (die, cu))
16031 {
16032 struct field_info fi;
16033 std::vector<struct symbol *> template_args;
16034
16035 child_die = die->child;
16036
16037 while (child_die && child_die->tag)
16038 {
16039 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16040
16041 if (is_variant_part && discr_offset == child_die->sect_off)
16042 fi.fields.back ().variant.is_discriminant = true;
16043
16044 child_die = sibling_die (child_die);
16045 }
16046
16047 /* Attach template arguments to type. */
16048 if (!template_args.empty ())
16049 {
16050 has_template_parameters = true;
16051 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16052 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16053 TYPE_TEMPLATE_ARGUMENTS (type)
16054 = XOBNEWVEC (&objfile->objfile_obstack,
16055 struct symbol *,
16056 TYPE_N_TEMPLATE_ARGUMENTS (type));
16057 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16058 template_args.data (),
16059 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16060 * sizeof (struct symbol *)));
16061 }
16062
16063 /* Attach fields and member functions to the type. */
16064 if (fi.nfields)
16065 dwarf2_attach_fields_to_type (&fi, type, cu);
16066 if (!fi.fnfieldlists.empty ())
16067 {
16068 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16069
16070 /* Get the type which refers to the base class (possibly this
16071 class itself) which contains the vtable pointer for the current
16072 class from the DW_AT_containing_type attribute. This use of
16073 DW_AT_containing_type is a GNU extension. */
16074
16075 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16076 {
16077 struct type *t = die_containing_type (die, cu);
16078
16079 set_type_vptr_basetype (type, t);
16080 if (type == t)
16081 {
16082 int i;
16083
16084 /* Our own class provides vtbl ptr. */
16085 for (i = TYPE_NFIELDS (t) - 1;
16086 i >= TYPE_N_BASECLASSES (t);
16087 --i)
16088 {
16089 const char *fieldname = TYPE_FIELD_NAME (t, i);
16090
16091 if (is_vtable_name (fieldname, cu))
16092 {
16093 set_type_vptr_fieldno (type, i);
16094 break;
16095 }
16096 }
16097
16098 /* Complain if virtual function table field not found. */
16099 if (i < TYPE_N_BASECLASSES (t))
16100 complaint (_("virtual function table pointer "
16101 "not found when defining class '%s'"),
16102 TYPE_NAME (type) ? TYPE_NAME (type) : "");
16103 }
16104 else
16105 {
16106 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16107 }
16108 }
16109 else if (cu->producer
16110 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16111 {
16112 /* The IBM XLC compiler does not provide direct indication
16113 of the containing type, but the vtable pointer is
16114 always named __vfp. */
16115
16116 int i;
16117
16118 for (i = TYPE_NFIELDS (type) - 1;
16119 i >= TYPE_N_BASECLASSES (type);
16120 --i)
16121 {
16122 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16123 {
16124 set_type_vptr_fieldno (type, i);
16125 set_type_vptr_basetype (type, type);
16126 break;
16127 }
16128 }
16129 }
16130 }
16131
16132 /* Copy fi.typedef_field_list linked list elements content into the
16133 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16134 if (!fi.typedef_field_list.empty ())
16135 {
16136 int count = fi.typedef_field_list.size ();
16137
16138 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16139 TYPE_TYPEDEF_FIELD_ARRAY (type)
16140 = ((struct decl_field *)
16141 TYPE_ALLOC (type,
16142 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16143 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16144
16145 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16146 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16147 }
16148
16149 /* Copy fi.nested_types_list linked list elements content into the
16150 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16151 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16152 {
16153 int count = fi.nested_types_list.size ();
16154
16155 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16156 TYPE_NESTED_TYPES_ARRAY (type)
16157 = ((struct decl_field *)
16158 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16159 TYPE_NESTED_TYPES_COUNT (type) = count;
16160
16161 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16162 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16163 }
16164 }
16165
16166 quirk_gcc_member_function_pointer (type, objfile);
16167 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16168 cu->rust_unions.push_back (type);
16169
16170 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16171 snapshots) has been known to create a die giving a declaration
16172 for a class that has, as a child, a die giving a definition for a
16173 nested class. So we have to process our children even if the
16174 current die is a declaration. Normally, of course, a declaration
16175 won't have any children at all. */
16176
16177 child_die = die->child;
16178
16179 while (child_die != NULL && child_die->tag)
16180 {
16181 if (child_die->tag == DW_TAG_member
16182 || child_die->tag == DW_TAG_variable
16183 || child_die->tag == DW_TAG_inheritance
16184 || child_die->tag == DW_TAG_template_value_param
16185 || child_die->tag == DW_TAG_template_type_param)
16186 {
16187 /* Do nothing. */
16188 }
16189 else
16190 process_die (child_die, cu);
16191
16192 child_die = sibling_die (child_die);
16193 }
16194
16195 /* Do not consider external references. According to the DWARF standard,
16196 these DIEs are identified by the fact that they have no byte_size
16197 attribute, and a declaration attribute. */
16198 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16199 || !die_is_declaration (die, cu))
16200 {
16201 struct symbol *sym = new_symbol (die, type, cu);
16202
16203 if (has_template_parameters)
16204 {
16205 struct symtab *symtab;
16206 if (sym != nullptr)
16207 symtab = symbol_symtab (sym);
16208 else if (cu->line_header != nullptr)
16209 {
16210 /* Any related symtab will do. */
16211 symtab
16212 = cu->line_header->file_name_at (file_name_index (1))->symtab;
16213 }
16214 else
16215 {
16216 symtab = nullptr;
16217 complaint (_("could not find suitable "
16218 "symtab for template parameter"
16219 " - DIE at %s [in module %s]"),
16220 sect_offset_str (die->sect_off),
16221 objfile_name (objfile));
16222 }
16223
16224 if (symtab != nullptr)
16225 {
16226 /* Make sure that the symtab is set on the new symbols.
16227 Even though they don't appear in this symtab directly,
16228 other parts of gdb assume that symbols do, and this is
16229 reasonably true. */
16230 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16231 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16232 }
16233 }
16234 }
16235 }
16236
16237 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16238 update TYPE using some information only available in DIE's children. */
16239
16240 static void
16241 update_enumeration_type_from_children (struct die_info *die,
16242 struct type *type,
16243 struct dwarf2_cu *cu)
16244 {
16245 struct die_info *child_die;
16246 int unsigned_enum = 1;
16247 int flag_enum = 1;
16248 ULONGEST mask = 0;
16249
16250 auto_obstack obstack;
16251
16252 for (child_die = die->child;
16253 child_die != NULL && child_die->tag;
16254 child_die = sibling_die (child_die))
16255 {
16256 struct attribute *attr;
16257 LONGEST value;
16258 const gdb_byte *bytes;
16259 struct dwarf2_locexpr_baton *baton;
16260 const char *name;
16261
16262 if (child_die->tag != DW_TAG_enumerator)
16263 continue;
16264
16265 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16266 if (attr == NULL)
16267 continue;
16268
16269 name = dwarf2_name (child_die, cu);
16270 if (name == NULL)
16271 name = "<anonymous enumerator>";
16272
16273 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16274 &value, &bytes, &baton);
16275 if (value < 0)
16276 {
16277 unsigned_enum = 0;
16278 flag_enum = 0;
16279 }
16280 else if ((mask & value) != 0)
16281 flag_enum = 0;
16282 else
16283 mask |= value;
16284
16285 /* If we already know that the enum type is neither unsigned, nor
16286 a flag type, no need to look at the rest of the enumerates. */
16287 if (!unsigned_enum && !flag_enum)
16288 break;
16289 }
16290
16291 if (unsigned_enum)
16292 TYPE_UNSIGNED (type) = 1;
16293 if (flag_enum)
16294 TYPE_FLAG_ENUM (type) = 1;
16295 }
16296
16297 /* Given a DW_AT_enumeration_type die, set its type. We do not
16298 complete the type's fields yet, or create any symbols. */
16299
16300 static struct type *
16301 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16302 {
16303 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16304 struct type *type;
16305 struct attribute *attr;
16306 const char *name;
16307
16308 /* If the definition of this type lives in .debug_types, read that type.
16309 Don't follow DW_AT_specification though, that will take us back up
16310 the chain and we want to go down. */
16311 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16312 if (attr)
16313 {
16314 type = get_DW_AT_signature_type (die, attr, cu);
16315
16316 /* The type's CU may not be the same as CU.
16317 Ensure TYPE is recorded with CU in die_type_hash. */
16318 return set_die_type (die, type, cu);
16319 }
16320
16321 type = alloc_type (objfile);
16322
16323 TYPE_CODE (type) = TYPE_CODE_ENUM;
16324 name = dwarf2_full_name (NULL, die, cu);
16325 if (name != NULL)
16326 TYPE_NAME (type) = name;
16327
16328 attr = dwarf2_attr (die, DW_AT_type, cu);
16329 if (attr != NULL)
16330 {
16331 struct type *underlying_type = die_type (die, cu);
16332
16333 TYPE_TARGET_TYPE (type) = underlying_type;
16334 }
16335
16336 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16337 if (attr)
16338 {
16339 TYPE_LENGTH (type) = DW_UNSND (attr);
16340 }
16341 else
16342 {
16343 TYPE_LENGTH (type) = 0;
16344 }
16345
16346 maybe_set_alignment (cu, die, type);
16347
16348 /* The enumeration DIE can be incomplete. In Ada, any type can be
16349 declared as private in the package spec, and then defined only
16350 inside the package body. Such types are known as Taft Amendment
16351 Types. When another package uses such a type, an incomplete DIE
16352 may be generated by the compiler. */
16353 if (die_is_declaration (die, cu))
16354 TYPE_STUB (type) = 1;
16355
16356 /* Finish the creation of this type by using the enum's children.
16357 We must call this even when the underlying type has been provided
16358 so that we can determine if we're looking at a "flag" enum. */
16359 update_enumeration_type_from_children (die, type, cu);
16360
16361 /* If this type has an underlying type that is not a stub, then we
16362 may use its attributes. We always use the "unsigned" attribute
16363 in this situation, because ordinarily we guess whether the type
16364 is unsigned -- but the guess can be wrong and the underlying type
16365 can tell us the reality. However, we defer to a local size
16366 attribute if one exists, because this lets the compiler override
16367 the underlying type if needed. */
16368 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16369 {
16370 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16371 if (TYPE_LENGTH (type) == 0)
16372 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16373 if (TYPE_RAW_ALIGN (type) == 0
16374 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16375 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16376 }
16377
16378 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16379
16380 return set_die_type (die, type, cu);
16381 }
16382
16383 /* Given a pointer to a die which begins an enumeration, process all
16384 the dies that define the members of the enumeration, and create the
16385 symbol for the enumeration type.
16386
16387 NOTE: We reverse the order of the element list. */
16388
16389 static void
16390 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16391 {
16392 struct type *this_type;
16393
16394 this_type = get_die_type (die, cu);
16395 if (this_type == NULL)
16396 this_type = read_enumeration_type (die, cu);
16397
16398 if (die->child != NULL)
16399 {
16400 struct die_info *child_die;
16401 struct symbol *sym;
16402 struct field *fields = NULL;
16403 int num_fields = 0;
16404 const char *name;
16405
16406 child_die = die->child;
16407 while (child_die && child_die->tag)
16408 {
16409 if (child_die->tag != DW_TAG_enumerator)
16410 {
16411 process_die (child_die, cu);
16412 }
16413 else
16414 {
16415 name = dwarf2_name (child_die, cu);
16416 if (name)
16417 {
16418 sym = new_symbol (child_die, this_type, cu);
16419
16420 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16421 {
16422 fields = (struct field *)
16423 xrealloc (fields,
16424 (num_fields + DW_FIELD_ALLOC_CHUNK)
16425 * sizeof (struct field));
16426 }
16427
16428 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16429 FIELD_TYPE (fields[num_fields]) = NULL;
16430 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16431 FIELD_BITSIZE (fields[num_fields]) = 0;
16432
16433 num_fields++;
16434 }
16435 }
16436
16437 child_die = sibling_die (child_die);
16438 }
16439
16440 if (num_fields)
16441 {
16442 TYPE_NFIELDS (this_type) = num_fields;
16443 TYPE_FIELDS (this_type) = (struct field *)
16444 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16445 memcpy (TYPE_FIELDS (this_type), fields,
16446 sizeof (struct field) * num_fields);
16447 xfree (fields);
16448 }
16449 }
16450
16451 /* If we are reading an enum from a .debug_types unit, and the enum
16452 is a declaration, and the enum is not the signatured type in the
16453 unit, then we do not want to add a symbol for it. Adding a
16454 symbol would in some cases obscure the true definition of the
16455 enum, giving users an incomplete type when the definition is
16456 actually available. Note that we do not want to do this for all
16457 enums which are just declarations, because C++0x allows forward
16458 enum declarations. */
16459 if (cu->per_cu->is_debug_types
16460 && die_is_declaration (die, cu))
16461 {
16462 struct signatured_type *sig_type;
16463
16464 sig_type = (struct signatured_type *) cu->per_cu;
16465 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16466 if (sig_type->type_offset_in_section != die->sect_off)
16467 return;
16468 }
16469
16470 new_symbol (die, this_type, cu);
16471 }
16472
16473 /* Extract all information from a DW_TAG_array_type DIE and put it in
16474 the DIE's type field. For now, this only handles one dimensional
16475 arrays. */
16476
16477 static struct type *
16478 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16479 {
16480 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16481 struct die_info *child_die;
16482 struct type *type;
16483 struct type *element_type, *range_type, *index_type;
16484 struct attribute *attr;
16485 const char *name;
16486 struct dynamic_prop *byte_stride_prop = NULL;
16487 unsigned int bit_stride = 0;
16488
16489 element_type = die_type (die, cu);
16490
16491 /* The die_type call above may have already set the type for this DIE. */
16492 type = get_die_type (die, cu);
16493 if (type)
16494 return type;
16495
16496 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16497 if (attr != NULL)
16498 {
16499 int stride_ok;
16500 struct type *prop_type
16501 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
16502
16503 byte_stride_prop
16504 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16505 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16506 prop_type);
16507 if (!stride_ok)
16508 {
16509 complaint (_("unable to read array DW_AT_byte_stride "
16510 " - DIE at %s [in module %s]"),
16511 sect_offset_str (die->sect_off),
16512 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16513 /* Ignore this attribute. We will likely not be able to print
16514 arrays of this type correctly, but there is little we can do
16515 to help if we cannot read the attribute's value. */
16516 byte_stride_prop = NULL;
16517 }
16518 }
16519
16520 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16521 if (attr != NULL)
16522 bit_stride = DW_UNSND (attr);
16523
16524 /* Irix 6.2 native cc creates array types without children for
16525 arrays with unspecified length. */
16526 if (die->child == NULL)
16527 {
16528 index_type = objfile_type (objfile)->builtin_int;
16529 range_type = create_static_range_type (NULL, index_type, 0, -1);
16530 type = create_array_type_with_stride (NULL, element_type, range_type,
16531 byte_stride_prop, bit_stride);
16532 return set_die_type (die, type, cu);
16533 }
16534
16535 std::vector<struct type *> range_types;
16536 child_die = die->child;
16537 while (child_die && child_die->tag)
16538 {
16539 if (child_die->tag == DW_TAG_subrange_type)
16540 {
16541 struct type *child_type = read_type_die (child_die, cu);
16542
16543 if (child_type != NULL)
16544 {
16545 /* The range type was succesfully read. Save it for the
16546 array type creation. */
16547 range_types.push_back (child_type);
16548 }
16549 }
16550 child_die = sibling_die (child_die);
16551 }
16552
16553 /* Dwarf2 dimensions are output from left to right, create the
16554 necessary array types in backwards order. */
16555
16556 type = element_type;
16557
16558 if (read_array_order (die, cu) == DW_ORD_col_major)
16559 {
16560 int i = 0;
16561
16562 while (i < range_types.size ())
16563 type = create_array_type_with_stride (NULL, type, range_types[i++],
16564 byte_stride_prop, bit_stride);
16565 }
16566 else
16567 {
16568 size_t ndim = range_types.size ();
16569 while (ndim-- > 0)
16570 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16571 byte_stride_prop, bit_stride);
16572 }
16573
16574 /* Understand Dwarf2 support for vector types (like they occur on
16575 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16576 array type. This is not part of the Dwarf2/3 standard yet, but a
16577 custom vendor extension. The main difference between a regular
16578 array and the vector variant is that vectors are passed by value
16579 to functions. */
16580 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16581 if (attr)
16582 make_vector_type (type);
16583
16584 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16585 implementation may choose to implement triple vectors using this
16586 attribute. */
16587 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16588 if (attr)
16589 {
16590 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16591 TYPE_LENGTH (type) = DW_UNSND (attr);
16592 else
16593 complaint (_("DW_AT_byte_size for array type smaller "
16594 "than the total size of elements"));
16595 }
16596
16597 name = dwarf2_name (die, cu);
16598 if (name)
16599 TYPE_NAME (type) = name;
16600
16601 maybe_set_alignment (cu, die, type);
16602
16603 /* Install the type in the die. */
16604 set_die_type (die, type, cu);
16605
16606 /* set_die_type should be already done. */
16607 set_descriptive_type (type, die, cu);
16608
16609 return type;
16610 }
16611
16612 static enum dwarf_array_dim_ordering
16613 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16614 {
16615 struct attribute *attr;
16616
16617 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16618
16619 if (attr)
16620 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16621
16622 /* GNU F77 is a special case, as at 08/2004 array type info is the
16623 opposite order to the dwarf2 specification, but data is still
16624 laid out as per normal fortran.
16625
16626 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16627 version checking. */
16628
16629 if (cu->language == language_fortran
16630 && cu->producer && strstr (cu->producer, "GNU F77"))
16631 {
16632 return DW_ORD_row_major;
16633 }
16634
16635 switch (cu->language_defn->la_array_ordering)
16636 {
16637 case array_column_major:
16638 return DW_ORD_col_major;
16639 case array_row_major:
16640 default:
16641 return DW_ORD_row_major;
16642 };
16643 }
16644
16645 /* Extract all information from a DW_TAG_set_type DIE and put it in
16646 the DIE's type field. */
16647
16648 static struct type *
16649 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16650 {
16651 struct type *domain_type, *set_type;
16652 struct attribute *attr;
16653
16654 domain_type = die_type (die, cu);
16655
16656 /* The die_type call above may have already set the type for this DIE. */
16657 set_type = get_die_type (die, cu);
16658 if (set_type)
16659 return set_type;
16660
16661 set_type = create_set_type (NULL, domain_type);
16662
16663 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16664 if (attr)
16665 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16666
16667 maybe_set_alignment (cu, die, set_type);
16668
16669 return set_die_type (die, set_type, cu);
16670 }
16671
16672 /* A helper for read_common_block that creates a locexpr baton.
16673 SYM is the symbol which we are marking as computed.
16674 COMMON_DIE is the DIE for the common block.
16675 COMMON_LOC is the location expression attribute for the common
16676 block itself.
16677 MEMBER_LOC is the location expression attribute for the particular
16678 member of the common block that we are processing.
16679 CU is the CU from which the above come. */
16680
16681 static void
16682 mark_common_block_symbol_computed (struct symbol *sym,
16683 struct die_info *common_die,
16684 struct attribute *common_loc,
16685 struct attribute *member_loc,
16686 struct dwarf2_cu *cu)
16687 {
16688 struct dwarf2_per_objfile *dwarf2_per_objfile
16689 = cu->per_cu->dwarf2_per_objfile;
16690 struct objfile *objfile = dwarf2_per_objfile->objfile;
16691 struct dwarf2_locexpr_baton *baton;
16692 gdb_byte *ptr;
16693 unsigned int cu_off;
16694 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16695 LONGEST offset = 0;
16696
16697 gdb_assert (common_loc && member_loc);
16698 gdb_assert (attr_form_is_block (common_loc));
16699 gdb_assert (attr_form_is_block (member_loc)
16700 || attr_form_is_constant (member_loc));
16701
16702 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16703 baton->per_cu = cu->per_cu;
16704 gdb_assert (baton->per_cu);
16705
16706 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16707
16708 if (attr_form_is_constant (member_loc))
16709 {
16710 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16711 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16712 }
16713 else
16714 baton->size += DW_BLOCK (member_loc)->size;
16715
16716 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16717 baton->data = ptr;
16718
16719 *ptr++ = DW_OP_call4;
16720 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16721 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16722 ptr += 4;
16723
16724 if (attr_form_is_constant (member_loc))
16725 {
16726 *ptr++ = DW_OP_addr;
16727 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16728 ptr += cu->header.addr_size;
16729 }
16730 else
16731 {
16732 /* We have to copy the data here, because DW_OP_call4 will only
16733 use a DW_AT_location attribute. */
16734 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16735 ptr += DW_BLOCK (member_loc)->size;
16736 }
16737
16738 *ptr++ = DW_OP_plus;
16739 gdb_assert (ptr - baton->data == baton->size);
16740
16741 SYMBOL_LOCATION_BATON (sym) = baton;
16742 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16743 }
16744
16745 /* Create appropriate locally-scoped variables for all the
16746 DW_TAG_common_block entries. Also create a struct common_block
16747 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16748 is used to sepate the common blocks name namespace from regular
16749 variable names. */
16750
16751 static void
16752 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16753 {
16754 struct attribute *attr;
16755
16756 attr = dwarf2_attr (die, DW_AT_location, cu);
16757 if (attr)
16758 {
16759 /* Support the .debug_loc offsets. */
16760 if (attr_form_is_block (attr))
16761 {
16762 /* Ok. */
16763 }
16764 else if (attr_form_is_section_offset (attr))
16765 {
16766 dwarf2_complex_location_expr_complaint ();
16767 attr = NULL;
16768 }
16769 else
16770 {
16771 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16772 "common block member");
16773 attr = NULL;
16774 }
16775 }
16776
16777 if (die->child != NULL)
16778 {
16779 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16780 struct die_info *child_die;
16781 size_t n_entries = 0, size;
16782 struct common_block *common_block;
16783 struct symbol *sym;
16784
16785 for (child_die = die->child;
16786 child_die && child_die->tag;
16787 child_die = sibling_die (child_die))
16788 ++n_entries;
16789
16790 size = (sizeof (struct common_block)
16791 + (n_entries - 1) * sizeof (struct symbol *));
16792 common_block
16793 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16794 size);
16795 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16796 common_block->n_entries = 0;
16797
16798 for (child_die = die->child;
16799 child_die && child_die->tag;
16800 child_die = sibling_die (child_die))
16801 {
16802 /* Create the symbol in the DW_TAG_common_block block in the current
16803 symbol scope. */
16804 sym = new_symbol (child_die, NULL, cu);
16805 if (sym != NULL)
16806 {
16807 struct attribute *member_loc;
16808
16809 common_block->contents[common_block->n_entries++] = sym;
16810
16811 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16812 cu);
16813 if (member_loc)
16814 {
16815 /* GDB has handled this for a long time, but it is
16816 not specified by DWARF. It seems to have been
16817 emitted by gfortran at least as recently as:
16818 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16819 complaint (_("Variable in common block has "
16820 "DW_AT_data_member_location "
16821 "- DIE at %s [in module %s]"),
16822 sect_offset_str (child_die->sect_off),
16823 objfile_name (objfile));
16824
16825 if (attr_form_is_section_offset (member_loc))
16826 dwarf2_complex_location_expr_complaint ();
16827 else if (attr_form_is_constant (member_loc)
16828 || attr_form_is_block (member_loc))
16829 {
16830 if (attr)
16831 mark_common_block_symbol_computed (sym, die, attr,
16832 member_loc, cu);
16833 }
16834 else
16835 dwarf2_complex_location_expr_complaint ();
16836 }
16837 }
16838 }
16839
16840 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16841 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16842 }
16843 }
16844
16845 /* Create a type for a C++ namespace. */
16846
16847 static struct type *
16848 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16849 {
16850 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16851 const char *previous_prefix, *name;
16852 int is_anonymous;
16853 struct type *type;
16854
16855 /* For extensions, reuse the type of the original namespace. */
16856 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16857 {
16858 struct die_info *ext_die;
16859 struct dwarf2_cu *ext_cu = cu;
16860
16861 ext_die = dwarf2_extension (die, &ext_cu);
16862 type = read_type_die (ext_die, ext_cu);
16863
16864 /* EXT_CU may not be the same as CU.
16865 Ensure TYPE is recorded with CU in die_type_hash. */
16866 return set_die_type (die, type, cu);
16867 }
16868
16869 name = namespace_name (die, &is_anonymous, cu);
16870
16871 /* Now build the name of the current namespace. */
16872
16873 previous_prefix = determine_prefix (die, cu);
16874 if (previous_prefix[0] != '\0')
16875 name = typename_concat (&objfile->objfile_obstack,
16876 previous_prefix, name, 0, cu);
16877
16878 /* Create the type. */
16879 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16880
16881 return set_die_type (die, type, cu);
16882 }
16883
16884 /* Read a namespace scope. */
16885
16886 static void
16887 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16888 {
16889 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16890 int is_anonymous;
16891
16892 /* Add a symbol associated to this if we haven't seen the namespace
16893 before. Also, add a using directive if it's an anonymous
16894 namespace. */
16895
16896 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16897 {
16898 struct type *type;
16899
16900 type = read_type_die (die, cu);
16901 new_symbol (die, type, cu);
16902
16903 namespace_name (die, &is_anonymous, cu);
16904 if (is_anonymous)
16905 {
16906 const char *previous_prefix = determine_prefix (die, cu);
16907
16908 std::vector<const char *> excludes;
16909 add_using_directive (using_directives (cu),
16910 previous_prefix, TYPE_NAME (type), NULL,
16911 NULL, excludes, 0, &objfile->objfile_obstack);
16912 }
16913 }
16914
16915 if (die->child != NULL)
16916 {
16917 struct die_info *child_die = die->child;
16918
16919 while (child_die && child_die->tag)
16920 {
16921 process_die (child_die, cu);
16922 child_die = sibling_die (child_die);
16923 }
16924 }
16925 }
16926
16927 /* Read a Fortran module as type. This DIE can be only a declaration used for
16928 imported module. Still we need that type as local Fortran "use ... only"
16929 declaration imports depend on the created type in determine_prefix. */
16930
16931 static struct type *
16932 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16933 {
16934 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16935 const char *module_name;
16936 struct type *type;
16937
16938 module_name = dwarf2_name (die, cu);
16939 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16940
16941 return set_die_type (die, type, cu);
16942 }
16943
16944 /* Read a Fortran module. */
16945
16946 static void
16947 read_module (struct die_info *die, struct dwarf2_cu *cu)
16948 {
16949 struct die_info *child_die = die->child;
16950 struct type *type;
16951
16952 type = read_type_die (die, cu);
16953 new_symbol (die, type, cu);
16954
16955 while (child_die && child_die->tag)
16956 {
16957 process_die (child_die, cu);
16958 child_die = sibling_die (child_die);
16959 }
16960 }
16961
16962 /* Return the name of the namespace represented by DIE. Set
16963 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16964 namespace. */
16965
16966 static const char *
16967 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16968 {
16969 struct die_info *current_die;
16970 const char *name = NULL;
16971
16972 /* Loop through the extensions until we find a name. */
16973
16974 for (current_die = die;
16975 current_die != NULL;
16976 current_die = dwarf2_extension (die, &cu))
16977 {
16978 /* We don't use dwarf2_name here so that we can detect the absence
16979 of a name -> anonymous namespace. */
16980 name = dwarf2_string_attr (die, DW_AT_name, cu);
16981
16982 if (name != NULL)
16983 break;
16984 }
16985
16986 /* Is it an anonymous namespace? */
16987
16988 *is_anonymous = (name == NULL);
16989 if (*is_anonymous)
16990 name = CP_ANONYMOUS_NAMESPACE_STR;
16991
16992 return name;
16993 }
16994
16995 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16996 the user defined type vector. */
16997
16998 static struct type *
16999 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17000 {
17001 struct gdbarch *gdbarch
17002 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
17003 struct comp_unit_head *cu_header = &cu->header;
17004 struct type *type;
17005 struct attribute *attr_byte_size;
17006 struct attribute *attr_address_class;
17007 int byte_size, addr_class;
17008 struct type *target_type;
17009
17010 target_type = die_type (die, cu);
17011
17012 /* The die_type call above may have already set the type for this DIE. */
17013 type = get_die_type (die, cu);
17014 if (type)
17015 return type;
17016
17017 type = lookup_pointer_type (target_type);
17018
17019 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17020 if (attr_byte_size)
17021 byte_size = DW_UNSND (attr_byte_size);
17022 else
17023 byte_size = cu_header->addr_size;
17024
17025 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17026 if (attr_address_class)
17027 addr_class = DW_UNSND (attr_address_class);
17028 else
17029 addr_class = DW_ADDR_none;
17030
17031 ULONGEST alignment = get_alignment (cu, die);
17032
17033 /* If the pointer size, alignment, or address class is different
17034 than the default, create a type variant marked as such and set
17035 the length accordingly. */
17036 if (TYPE_LENGTH (type) != byte_size
17037 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17038 && alignment != TYPE_RAW_ALIGN (type))
17039 || addr_class != DW_ADDR_none)
17040 {
17041 if (gdbarch_address_class_type_flags_p (gdbarch))
17042 {
17043 int type_flags;
17044
17045 type_flags = gdbarch_address_class_type_flags
17046 (gdbarch, byte_size, addr_class);
17047 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17048 == 0);
17049 type = make_type_with_address_space (type, type_flags);
17050 }
17051 else if (TYPE_LENGTH (type) != byte_size)
17052 {
17053 complaint (_("invalid pointer size %d"), byte_size);
17054 }
17055 else if (TYPE_RAW_ALIGN (type) != alignment)
17056 {
17057 complaint (_("Invalid DW_AT_alignment"
17058 " - DIE at %s [in module %s]"),
17059 sect_offset_str (die->sect_off),
17060 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17061 }
17062 else
17063 {
17064 /* Should we also complain about unhandled address classes? */
17065 }
17066 }
17067
17068 TYPE_LENGTH (type) = byte_size;
17069 set_type_align (type, alignment);
17070 return set_die_type (die, type, cu);
17071 }
17072
17073 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17074 the user defined type vector. */
17075
17076 static struct type *
17077 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17078 {
17079 struct type *type;
17080 struct type *to_type;
17081 struct type *domain;
17082
17083 to_type = die_type (die, cu);
17084 domain = die_containing_type (die, cu);
17085
17086 /* The calls above may have already set the type for this DIE. */
17087 type = get_die_type (die, cu);
17088 if (type)
17089 return type;
17090
17091 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17092 type = lookup_methodptr_type (to_type);
17093 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17094 {
17095 struct type *new_type
17096 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17097
17098 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17099 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17100 TYPE_VARARGS (to_type));
17101 type = lookup_methodptr_type (new_type);
17102 }
17103 else
17104 type = lookup_memberptr_type (to_type, domain);
17105
17106 return set_die_type (die, type, cu);
17107 }
17108
17109 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17110 the user defined type vector. */
17111
17112 static struct type *
17113 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17114 enum type_code refcode)
17115 {
17116 struct comp_unit_head *cu_header = &cu->header;
17117 struct type *type, *target_type;
17118 struct attribute *attr;
17119
17120 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17121
17122 target_type = die_type (die, cu);
17123
17124 /* The die_type call above may have already set the type for this DIE. */
17125 type = get_die_type (die, cu);
17126 if (type)
17127 return type;
17128
17129 type = lookup_reference_type (target_type, refcode);
17130 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17131 if (attr)
17132 {
17133 TYPE_LENGTH (type) = DW_UNSND (attr);
17134 }
17135 else
17136 {
17137 TYPE_LENGTH (type) = cu_header->addr_size;
17138 }
17139 maybe_set_alignment (cu, die, type);
17140 return set_die_type (die, type, cu);
17141 }
17142
17143 /* Add the given cv-qualifiers to the element type of the array. GCC
17144 outputs DWARF type qualifiers that apply to an array, not the
17145 element type. But GDB relies on the array element type to carry
17146 the cv-qualifiers. This mimics section 6.7.3 of the C99
17147 specification. */
17148
17149 static struct type *
17150 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17151 struct type *base_type, int cnst, int voltl)
17152 {
17153 struct type *el_type, *inner_array;
17154
17155 base_type = copy_type (base_type);
17156 inner_array = base_type;
17157
17158 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17159 {
17160 TYPE_TARGET_TYPE (inner_array) =
17161 copy_type (TYPE_TARGET_TYPE (inner_array));
17162 inner_array = TYPE_TARGET_TYPE (inner_array);
17163 }
17164
17165 el_type = TYPE_TARGET_TYPE (inner_array);
17166 cnst |= TYPE_CONST (el_type);
17167 voltl |= TYPE_VOLATILE (el_type);
17168 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17169
17170 return set_die_type (die, base_type, cu);
17171 }
17172
17173 static struct type *
17174 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17175 {
17176 struct type *base_type, *cv_type;
17177
17178 base_type = die_type (die, cu);
17179
17180 /* The die_type call above may have already set the type for this DIE. */
17181 cv_type = get_die_type (die, cu);
17182 if (cv_type)
17183 return cv_type;
17184
17185 /* In case the const qualifier is applied to an array type, the element type
17186 is so qualified, not the array type (section 6.7.3 of C99). */
17187 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17188 return add_array_cv_type (die, cu, base_type, 1, 0);
17189
17190 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17191 return set_die_type (die, cv_type, cu);
17192 }
17193
17194 static struct type *
17195 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17196 {
17197 struct type *base_type, *cv_type;
17198
17199 base_type = die_type (die, cu);
17200
17201 /* The die_type call above may have already set the type for this DIE. */
17202 cv_type = get_die_type (die, cu);
17203 if (cv_type)
17204 return cv_type;
17205
17206 /* In case the volatile qualifier is applied to an array type, the
17207 element type is so qualified, not the array type (section 6.7.3
17208 of C99). */
17209 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17210 return add_array_cv_type (die, cu, base_type, 0, 1);
17211
17212 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17213 return set_die_type (die, cv_type, cu);
17214 }
17215
17216 /* Handle DW_TAG_restrict_type. */
17217
17218 static struct type *
17219 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17220 {
17221 struct type *base_type, *cv_type;
17222
17223 base_type = die_type (die, cu);
17224
17225 /* The die_type call above may have already set the type for this DIE. */
17226 cv_type = get_die_type (die, cu);
17227 if (cv_type)
17228 return cv_type;
17229
17230 cv_type = make_restrict_type (base_type);
17231 return set_die_type (die, cv_type, cu);
17232 }
17233
17234 /* Handle DW_TAG_atomic_type. */
17235
17236 static struct type *
17237 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17238 {
17239 struct type *base_type, *cv_type;
17240
17241 base_type = die_type (die, cu);
17242
17243 /* The die_type call above may have already set the type for this DIE. */
17244 cv_type = get_die_type (die, cu);
17245 if (cv_type)
17246 return cv_type;
17247
17248 cv_type = make_atomic_type (base_type);
17249 return set_die_type (die, cv_type, cu);
17250 }
17251
17252 /* Extract all information from a DW_TAG_string_type DIE and add to
17253 the user defined type vector. It isn't really a user defined type,
17254 but it behaves like one, with other DIE's using an AT_user_def_type
17255 attribute to reference it. */
17256
17257 static struct type *
17258 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17259 {
17260 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17261 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17262 struct type *type, *range_type, *index_type, *char_type;
17263 struct attribute *attr;
17264 unsigned int length;
17265
17266 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17267 if (attr)
17268 {
17269 length = DW_UNSND (attr);
17270 }
17271 else
17272 {
17273 /* Check for the DW_AT_byte_size attribute. */
17274 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17275 if (attr)
17276 {
17277 length = DW_UNSND (attr);
17278 }
17279 else
17280 {
17281 length = 1;
17282 }
17283 }
17284
17285 index_type = objfile_type (objfile)->builtin_int;
17286 range_type = create_static_range_type (NULL, index_type, 1, length);
17287 char_type = language_string_char_type (cu->language_defn, gdbarch);
17288 type = create_string_type (NULL, char_type, range_type);
17289
17290 return set_die_type (die, type, cu);
17291 }
17292
17293 /* Assuming that DIE corresponds to a function, returns nonzero
17294 if the function is prototyped. */
17295
17296 static int
17297 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17298 {
17299 struct attribute *attr;
17300
17301 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17302 if (attr && (DW_UNSND (attr) != 0))
17303 return 1;
17304
17305 /* The DWARF standard implies that the DW_AT_prototyped attribute
17306 is only meaninful for C, but the concept also extends to other
17307 languages that allow unprototyped functions (Eg: Objective C).
17308 For all other languages, assume that functions are always
17309 prototyped. */
17310 if (cu->language != language_c
17311 && cu->language != language_objc
17312 && cu->language != language_opencl)
17313 return 1;
17314
17315 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17316 prototyped and unprototyped functions; default to prototyped,
17317 since that is more common in modern code (and RealView warns
17318 about unprototyped functions). */
17319 if (producer_is_realview (cu->producer))
17320 return 1;
17321
17322 return 0;
17323 }
17324
17325 /* Handle DIES due to C code like:
17326
17327 struct foo
17328 {
17329 int (*funcp)(int a, long l);
17330 int b;
17331 };
17332
17333 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17334
17335 static struct type *
17336 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17337 {
17338 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17339 struct type *type; /* Type that this function returns. */
17340 struct type *ftype; /* Function that returns above type. */
17341 struct attribute *attr;
17342
17343 type = die_type (die, cu);
17344
17345 /* The die_type call above may have already set the type for this DIE. */
17346 ftype = get_die_type (die, cu);
17347 if (ftype)
17348 return ftype;
17349
17350 ftype = lookup_function_type (type);
17351
17352 if (prototyped_function_p (die, cu))
17353 TYPE_PROTOTYPED (ftype) = 1;
17354
17355 /* Store the calling convention in the type if it's available in
17356 the subroutine die. Otherwise set the calling convention to
17357 the default value DW_CC_normal. */
17358 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17359 if (attr)
17360 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17361 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17362 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17363 else
17364 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17365
17366 /* Record whether the function returns normally to its caller or not
17367 if the DWARF producer set that information. */
17368 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17369 if (attr && (DW_UNSND (attr) != 0))
17370 TYPE_NO_RETURN (ftype) = 1;
17371
17372 /* We need to add the subroutine type to the die immediately so
17373 we don't infinitely recurse when dealing with parameters
17374 declared as the same subroutine type. */
17375 set_die_type (die, ftype, cu);
17376
17377 if (die->child != NULL)
17378 {
17379 struct type *void_type = objfile_type (objfile)->builtin_void;
17380 struct die_info *child_die;
17381 int nparams, iparams;
17382
17383 /* Count the number of parameters.
17384 FIXME: GDB currently ignores vararg functions, but knows about
17385 vararg member functions. */
17386 nparams = 0;
17387 child_die = die->child;
17388 while (child_die && child_die->tag)
17389 {
17390 if (child_die->tag == DW_TAG_formal_parameter)
17391 nparams++;
17392 else if (child_die->tag == DW_TAG_unspecified_parameters)
17393 TYPE_VARARGS (ftype) = 1;
17394 child_die = sibling_die (child_die);
17395 }
17396
17397 /* Allocate storage for parameters and fill them in. */
17398 TYPE_NFIELDS (ftype) = nparams;
17399 TYPE_FIELDS (ftype) = (struct field *)
17400 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17401
17402 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17403 even if we error out during the parameters reading below. */
17404 for (iparams = 0; iparams < nparams; iparams++)
17405 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17406
17407 iparams = 0;
17408 child_die = die->child;
17409 while (child_die && child_die->tag)
17410 {
17411 if (child_die->tag == DW_TAG_formal_parameter)
17412 {
17413 struct type *arg_type;
17414
17415 /* DWARF version 2 has no clean way to discern C++
17416 static and non-static member functions. G++ helps
17417 GDB by marking the first parameter for non-static
17418 member functions (which is the this pointer) as
17419 artificial. We pass this information to
17420 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17421
17422 DWARF version 3 added DW_AT_object_pointer, which GCC
17423 4.5 does not yet generate. */
17424 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17425 if (attr)
17426 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17427 else
17428 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17429 arg_type = die_type (child_die, cu);
17430
17431 /* RealView does not mark THIS as const, which the testsuite
17432 expects. GCC marks THIS as const in method definitions,
17433 but not in the class specifications (GCC PR 43053). */
17434 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17435 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17436 {
17437 int is_this = 0;
17438 struct dwarf2_cu *arg_cu = cu;
17439 const char *name = dwarf2_name (child_die, cu);
17440
17441 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17442 if (attr)
17443 {
17444 /* If the compiler emits this, use it. */
17445 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17446 is_this = 1;
17447 }
17448 else if (name && strcmp (name, "this") == 0)
17449 /* Function definitions will have the argument names. */
17450 is_this = 1;
17451 else if (name == NULL && iparams == 0)
17452 /* Declarations may not have the names, so like
17453 elsewhere in GDB, assume an artificial first
17454 argument is "this". */
17455 is_this = 1;
17456
17457 if (is_this)
17458 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17459 arg_type, 0);
17460 }
17461
17462 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17463 iparams++;
17464 }
17465 child_die = sibling_die (child_die);
17466 }
17467 }
17468
17469 return ftype;
17470 }
17471
17472 static struct type *
17473 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17474 {
17475 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17476 const char *name = NULL;
17477 struct type *this_type, *target_type;
17478
17479 name = dwarf2_full_name (NULL, die, cu);
17480 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17481 TYPE_TARGET_STUB (this_type) = 1;
17482 set_die_type (die, this_type, cu);
17483 target_type = die_type (die, cu);
17484 if (target_type != this_type)
17485 TYPE_TARGET_TYPE (this_type) = target_type;
17486 else
17487 {
17488 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17489 spec and cause infinite loops in GDB. */
17490 complaint (_("Self-referential DW_TAG_typedef "
17491 "- DIE at %s [in module %s]"),
17492 sect_offset_str (die->sect_off), objfile_name (objfile));
17493 TYPE_TARGET_TYPE (this_type) = NULL;
17494 }
17495 return this_type;
17496 }
17497
17498 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17499 (which may be different from NAME) to the architecture back-end to allow
17500 it to guess the correct format if necessary. */
17501
17502 static struct type *
17503 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17504 const char *name_hint)
17505 {
17506 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17507 const struct floatformat **format;
17508 struct type *type;
17509
17510 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17511 if (format)
17512 type = init_float_type (objfile, bits, name, format);
17513 else
17514 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17515
17516 return type;
17517 }
17518
17519 /* Allocate an integer type of size BITS and name NAME. */
17520
17521 static struct type *
17522 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17523 int bits, int unsigned_p, const char *name)
17524 {
17525 struct type *type;
17526
17527 /* Versions of Intel's C Compiler generate an integer type called "void"
17528 instead of using DW_TAG_unspecified_type. This has been seen on
17529 at least versions 14, 17, and 18. */
17530 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17531 && strcmp (name, "void") == 0)
17532 type = objfile_type (objfile)->builtin_void;
17533 else
17534 type = init_integer_type (objfile, bits, unsigned_p, name);
17535
17536 return type;
17537 }
17538
17539 /* Initialise and return a floating point type of size BITS suitable for
17540 use as a component of a complex number. The NAME_HINT is passed through
17541 when initialising the floating point type and is the name of the complex
17542 type.
17543
17544 As DWARF doesn't currently provide an explicit name for the components
17545 of a complex number, but it can be helpful to have these components
17546 named, we try to select a suitable name based on the size of the
17547 component. */
17548 static struct type *
17549 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17550 struct objfile *objfile,
17551 int bits, const char *name_hint)
17552 {
17553 gdbarch *gdbarch = get_objfile_arch (objfile);
17554 struct type *tt = nullptr;
17555
17556 /* Try to find a suitable floating point builtin type of size BITS.
17557 We're going to use the name of this type as the name for the complex
17558 target type that we are about to create. */
17559 switch (cu->language)
17560 {
17561 case language_fortran:
17562 switch (bits)
17563 {
17564 case 32:
17565 tt = builtin_f_type (gdbarch)->builtin_real;
17566 break;
17567 case 64:
17568 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17569 break;
17570 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17571 case 128:
17572 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17573 break;
17574 }
17575 break;
17576 default:
17577 switch (bits)
17578 {
17579 case 32:
17580 tt = builtin_type (gdbarch)->builtin_float;
17581 break;
17582 case 64:
17583 tt = builtin_type (gdbarch)->builtin_double;
17584 break;
17585 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17586 case 128:
17587 tt = builtin_type (gdbarch)->builtin_long_double;
17588 break;
17589 }
17590 break;
17591 }
17592
17593 /* If the type we found doesn't match the size we were looking for, then
17594 pretend we didn't find a type at all, the complex target type we
17595 create will then be nameless. */
17596 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17597 tt = nullptr;
17598
17599 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17600 return dwarf2_init_float_type (objfile, bits, name, name_hint);
17601 }
17602
17603 /* Find a representation of a given base type and install
17604 it in the TYPE field of the die. */
17605
17606 static struct type *
17607 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17608 {
17609 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17610 struct type *type;
17611 struct attribute *attr;
17612 int encoding = 0, bits = 0;
17613 const char *name;
17614
17615 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17616 if (attr)
17617 {
17618 encoding = DW_UNSND (attr);
17619 }
17620 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17621 if (attr)
17622 {
17623 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17624 }
17625 name = dwarf2_name (die, cu);
17626 if (!name)
17627 {
17628 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17629 }
17630
17631 switch (encoding)
17632 {
17633 case DW_ATE_address:
17634 /* Turn DW_ATE_address into a void * pointer. */
17635 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17636 type = init_pointer_type (objfile, bits, name, type);
17637 break;
17638 case DW_ATE_boolean:
17639 type = init_boolean_type (objfile, bits, 1, name);
17640 break;
17641 case DW_ATE_complex_float:
17642 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name);
17643 type = init_complex_type (objfile, name, type);
17644 break;
17645 case DW_ATE_decimal_float:
17646 type = init_decfloat_type (objfile, bits, name);
17647 break;
17648 case DW_ATE_float:
17649 type = dwarf2_init_float_type (objfile, bits, name, name);
17650 break;
17651 case DW_ATE_signed:
17652 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17653 break;
17654 case DW_ATE_unsigned:
17655 if (cu->language == language_fortran
17656 && name
17657 && startswith (name, "character("))
17658 type = init_character_type (objfile, bits, 1, name);
17659 else
17660 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17661 break;
17662 case DW_ATE_signed_char:
17663 if (cu->language == language_ada || cu->language == language_m2
17664 || cu->language == language_pascal
17665 || cu->language == language_fortran)
17666 type = init_character_type (objfile, bits, 0, name);
17667 else
17668 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17669 break;
17670 case DW_ATE_unsigned_char:
17671 if (cu->language == language_ada || cu->language == language_m2
17672 || cu->language == language_pascal
17673 || cu->language == language_fortran
17674 || cu->language == language_rust)
17675 type = init_character_type (objfile, bits, 1, name);
17676 else
17677 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17678 break;
17679 case DW_ATE_UTF:
17680 {
17681 gdbarch *arch = get_objfile_arch (objfile);
17682
17683 if (bits == 16)
17684 type = builtin_type (arch)->builtin_char16;
17685 else if (bits == 32)
17686 type = builtin_type (arch)->builtin_char32;
17687 else
17688 {
17689 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17690 bits);
17691 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17692 }
17693 return set_die_type (die, type, cu);
17694 }
17695 break;
17696
17697 default:
17698 complaint (_("unsupported DW_AT_encoding: '%s'"),
17699 dwarf_type_encoding_name (encoding));
17700 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17701 break;
17702 }
17703
17704 if (name && strcmp (name, "char") == 0)
17705 TYPE_NOSIGN (type) = 1;
17706
17707 maybe_set_alignment (cu, die, type);
17708
17709 return set_die_type (die, type, cu);
17710 }
17711
17712 /* Parse dwarf attribute if it's a block, reference or constant and put the
17713 resulting value of the attribute into struct bound_prop.
17714 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17715
17716 static int
17717 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17718 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17719 struct type *default_type)
17720 {
17721 struct dwarf2_property_baton *baton;
17722 struct obstack *obstack
17723 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17724
17725 gdb_assert (default_type != NULL);
17726
17727 if (attr == NULL || prop == NULL)
17728 return 0;
17729
17730 if (attr_form_is_block (attr))
17731 {
17732 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17733 baton->property_type = default_type;
17734 baton->locexpr.per_cu = cu->per_cu;
17735 baton->locexpr.size = DW_BLOCK (attr)->size;
17736 baton->locexpr.data = DW_BLOCK (attr)->data;
17737 baton->locexpr.is_reference = false;
17738 prop->data.baton = baton;
17739 prop->kind = PROP_LOCEXPR;
17740 gdb_assert (prop->data.baton != NULL);
17741 }
17742 else if (attr_form_is_ref (attr))
17743 {
17744 struct dwarf2_cu *target_cu = cu;
17745 struct die_info *target_die;
17746 struct attribute *target_attr;
17747
17748 target_die = follow_die_ref (die, attr, &target_cu);
17749 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17750 if (target_attr == NULL)
17751 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17752 target_cu);
17753 if (target_attr == NULL)
17754 return 0;
17755
17756 switch (target_attr->name)
17757 {
17758 case DW_AT_location:
17759 if (attr_form_is_section_offset (target_attr))
17760 {
17761 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17762 baton->property_type = die_type (target_die, target_cu);
17763 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17764 prop->data.baton = baton;
17765 prop->kind = PROP_LOCLIST;
17766 gdb_assert (prop->data.baton != NULL);
17767 }
17768 else if (attr_form_is_block (target_attr))
17769 {
17770 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17771 baton->property_type = die_type (target_die, target_cu);
17772 baton->locexpr.per_cu = cu->per_cu;
17773 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17774 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17775 baton->locexpr.is_reference = true;
17776 prop->data.baton = baton;
17777 prop->kind = PROP_LOCEXPR;
17778 gdb_assert (prop->data.baton != NULL);
17779 }
17780 else
17781 {
17782 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17783 "dynamic property");
17784 return 0;
17785 }
17786 break;
17787 case DW_AT_data_member_location:
17788 {
17789 LONGEST offset;
17790
17791 if (!handle_data_member_location (target_die, target_cu,
17792 &offset))
17793 return 0;
17794
17795 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17796 baton->property_type = read_type_die (target_die->parent,
17797 target_cu);
17798 baton->offset_info.offset = offset;
17799 baton->offset_info.type = die_type (target_die, target_cu);
17800 prop->data.baton = baton;
17801 prop->kind = PROP_ADDR_OFFSET;
17802 break;
17803 }
17804 }
17805 }
17806 else if (attr_form_is_constant (attr))
17807 {
17808 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17809 prop->kind = PROP_CONST;
17810 }
17811 else
17812 {
17813 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17814 dwarf2_name (die, cu));
17815 return 0;
17816 }
17817
17818 return 1;
17819 }
17820
17821 /* Find an integer type the same size as the address size given in the
17822 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
17823 is unsigned or not. */
17824
17825 static struct type *
17826 dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
17827 bool unsigned_p)
17828 {
17829 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
17830 int addr_size = dwarf2_per_cu_addr_size (per_cu);
17831 struct type *int_type;
17832
17833 /* Helper macro to examine the various builtin types. */
17834 #define TRY_TYPE(F) \
17835 int_type = (unsigned_p \
17836 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17837 : objfile_type (objfile)->builtin_ ## F); \
17838 if (int_type != NULL && TYPE_LENGTH (int_type) == addr_size) \
17839 return int_type
17840
17841 TRY_TYPE (char);
17842 TRY_TYPE (short);
17843 TRY_TYPE (int);
17844 TRY_TYPE (long);
17845 TRY_TYPE (long_long);
17846
17847 #undef TRY_TYPE
17848
17849 gdb_assert_not_reached ("unable to find suitable integer type");
17850 }
17851
17852 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17853 present (which is valid) then compute the default type based on the
17854 compilation units address size. */
17855
17856 static struct type *
17857 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17858 {
17859 struct type *index_type = die_type (die, cu);
17860
17861 /* Dwarf-2 specifications explicitly allows to create subrange types
17862 without specifying a base type.
17863 In that case, the base type must be set to the type of
17864 the lower bound, upper bound or count, in that order, if any of these
17865 three attributes references an object that has a type.
17866 If no base type is found, the Dwarf-2 specifications say that
17867 a signed integer type of size equal to the size of an address should
17868 be used.
17869 For the following C code: `extern char gdb_int [];'
17870 GCC produces an empty range DIE.
17871 FIXME: muller/2010-05-28: Possible references to object for low bound,
17872 high bound or count are not yet handled by this code. */
17873 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17874 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17875
17876 return index_type;
17877 }
17878
17879 /* Read the given DW_AT_subrange DIE. */
17880
17881 static struct type *
17882 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17883 {
17884 struct type *base_type, *orig_base_type;
17885 struct type *range_type;
17886 struct attribute *attr;
17887 struct dynamic_prop low, high;
17888 int low_default_is_valid;
17889 int high_bound_is_count = 0;
17890 const char *name;
17891 ULONGEST negative_mask;
17892
17893 orig_base_type = read_subrange_index_type (die, cu);
17894
17895 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17896 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17897 creating the range type, but we use the result of check_typedef
17898 when examining properties of the type. */
17899 base_type = check_typedef (orig_base_type);
17900
17901 /* The die_type call above may have already set the type for this DIE. */
17902 range_type = get_die_type (die, cu);
17903 if (range_type)
17904 return range_type;
17905
17906 low.kind = PROP_CONST;
17907 high.kind = PROP_CONST;
17908 high.data.const_val = 0;
17909
17910 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17911 omitting DW_AT_lower_bound. */
17912 switch (cu->language)
17913 {
17914 case language_c:
17915 case language_cplus:
17916 low.data.const_val = 0;
17917 low_default_is_valid = 1;
17918 break;
17919 case language_fortran:
17920 low.data.const_val = 1;
17921 low_default_is_valid = 1;
17922 break;
17923 case language_d:
17924 case language_objc:
17925 case language_rust:
17926 low.data.const_val = 0;
17927 low_default_is_valid = (cu->header.version >= 4);
17928 break;
17929 case language_ada:
17930 case language_m2:
17931 case language_pascal:
17932 low.data.const_val = 1;
17933 low_default_is_valid = (cu->header.version >= 4);
17934 break;
17935 default:
17936 low.data.const_val = 0;
17937 low_default_is_valid = 0;
17938 break;
17939 }
17940
17941 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17942 if (attr)
17943 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17944 else if (!low_default_is_valid)
17945 complaint (_("Missing DW_AT_lower_bound "
17946 "- DIE at %s [in module %s]"),
17947 sect_offset_str (die->sect_off),
17948 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17949
17950 struct attribute *attr_ub, *attr_count;
17951 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17952 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17953 {
17954 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17955 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17956 {
17957 /* If bounds are constant do the final calculation here. */
17958 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17959 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17960 else
17961 high_bound_is_count = 1;
17962 }
17963 else
17964 {
17965 if (attr_ub != NULL)
17966 complaint (_("Unresolved DW_AT_upper_bound "
17967 "- DIE at %s [in module %s]"),
17968 sect_offset_str (die->sect_off),
17969 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17970 if (attr_count != NULL)
17971 complaint (_("Unresolved DW_AT_count "
17972 "- DIE at %s [in module %s]"),
17973 sect_offset_str (die->sect_off),
17974 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17975 }
17976 }
17977
17978 LONGEST bias = 0;
17979 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17980 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
17981 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17982
17983 /* Normally, the DWARF producers are expected to use a signed
17984 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17985 But this is unfortunately not always the case, as witnessed
17986 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17987 is used instead. To work around that ambiguity, we treat
17988 the bounds as signed, and thus sign-extend their values, when
17989 the base type is signed. */
17990 negative_mask =
17991 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17992 if (low.kind == PROP_CONST
17993 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17994 low.data.const_val |= negative_mask;
17995 if (high.kind == PROP_CONST
17996 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17997 high.data.const_val |= negative_mask;
17998
17999 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18000
18001 if (high_bound_is_count)
18002 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18003
18004 /* Ada expects an empty array on no boundary attributes. */
18005 if (attr == NULL && cu->language != language_ada)
18006 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
18007
18008 name = dwarf2_name (die, cu);
18009 if (name)
18010 TYPE_NAME (range_type) = name;
18011
18012 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18013 if (attr)
18014 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18015
18016 maybe_set_alignment (cu, die, range_type);
18017
18018 set_die_type (die, range_type, cu);
18019
18020 /* set_die_type should be already done. */
18021 set_descriptive_type (range_type, die, cu);
18022
18023 return range_type;
18024 }
18025
18026 static struct type *
18027 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18028 {
18029 struct type *type;
18030
18031 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18032 NULL);
18033 TYPE_NAME (type) = dwarf2_name (die, cu);
18034
18035 /* In Ada, an unspecified type is typically used when the description
18036 of the type is defered to a different unit. When encountering
18037 such a type, we treat it as a stub, and try to resolve it later on,
18038 when needed. */
18039 if (cu->language == language_ada)
18040 TYPE_STUB (type) = 1;
18041
18042 return set_die_type (die, type, cu);
18043 }
18044
18045 /* Read a single die and all its descendents. Set the die's sibling
18046 field to NULL; set other fields in the die correctly, and set all
18047 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18048 location of the info_ptr after reading all of those dies. PARENT
18049 is the parent of the die in question. */
18050
18051 static struct die_info *
18052 read_die_and_children (const struct die_reader_specs *reader,
18053 const gdb_byte *info_ptr,
18054 const gdb_byte **new_info_ptr,
18055 struct die_info *parent)
18056 {
18057 struct die_info *die;
18058 const gdb_byte *cur_ptr;
18059 int has_children;
18060
18061 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18062 if (die == NULL)
18063 {
18064 *new_info_ptr = cur_ptr;
18065 return NULL;
18066 }
18067 store_in_ref_table (die, reader->cu);
18068
18069 if (has_children)
18070 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18071 else
18072 {
18073 die->child = NULL;
18074 *new_info_ptr = cur_ptr;
18075 }
18076
18077 die->sibling = NULL;
18078 die->parent = parent;
18079 return die;
18080 }
18081
18082 /* Read a die, all of its descendents, and all of its siblings; set
18083 all of the fields of all of the dies correctly. Arguments are as
18084 in read_die_and_children. */
18085
18086 static struct die_info *
18087 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18088 const gdb_byte *info_ptr,
18089 const gdb_byte **new_info_ptr,
18090 struct die_info *parent)
18091 {
18092 struct die_info *first_die, *last_sibling;
18093 const gdb_byte *cur_ptr;
18094
18095 cur_ptr = info_ptr;
18096 first_die = last_sibling = NULL;
18097
18098 while (1)
18099 {
18100 struct die_info *die
18101 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18102
18103 if (die == NULL)
18104 {
18105 *new_info_ptr = cur_ptr;
18106 return first_die;
18107 }
18108
18109 if (!first_die)
18110 first_die = die;
18111 else
18112 last_sibling->sibling = die;
18113
18114 last_sibling = die;
18115 }
18116 }
18117
18118 /* Read a die, all of its descendents, and all of its siblings; set
18119 all of the fields of all of the dies correctly. Arguments are as
18120 in read_die_and_children.
18121 This the main entry point for reading a DIE and all its children. */
18122
18123 static struct die_info *
18124 read_die_and_siblings (const struct die_reader_specs *reader,
18125 const gdb_byte *info_ptr,
18126 const gdb_byte **new_info_ptr,
18127 struct die_info *parent)
18128 {
18129 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18130 new_info_ptr, parent);
18131
18132 if (dwarf_die_debug)
18133 {
18134 fprintf_unfiltered (gdb_stdlog,
18135 "Read die from %s@0x%x of %s:\n",
18136 get_section_name (reader->die_section),
18137 (unsigned) (info_ptr - reader->die_section->buffer),
18138 bfd_get_filename (reader->abfd));
18139 dump_die (die, dwarf_die_debug);
18140 }
18141
18142 return die;
18143 }
18144
18145 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18146 attributes.
18147 The caller is responsible for filling in the extra attributes
18148 and updating (*DIEP)->num_attrs.
18149 Set DIEP to point to a newly allocated die with its information,
18150 except for its child, sibling, and parent fields.
18151 Set HAS_CHILDREN to tell whether the die has children or not. */
18152
18153 static const gdb_byte *
18154 read_full_die_1 (const struct die_reader_specs *reader,
18155 struct die_info **diep, const gdb_byte *info_ptr,
18156 int *has_children, int num_extra_attrs)
18157 {
18158 unsigned int abbrev_number, bytes_read, i;
18159 struct abbrev_info *abbrev;
18160 struct die_info *die;
18161 struct dwarf2_cu *cu = reader->cu;
18162 bfd *abfd = reader->abfd;
18163
18164 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18165 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18166 info_ptr += bytes_read;
18167 if (!abbrev_number)
18168 {
18169 *diep = NULL;
18170 *has_children = 0;
18171 return info_ptr;
18172 }
18173
18174 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18175 if (!abbrev)
18176 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18177 abbrev_number,
18178 bfd_get_filename (abfd));
18179
18180 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18181 die->sect_off = sect_off;
18182 die->tag = abbrev->tag;
18183 die->abbrev = abbrev_number;
18184
18185 /* Make the result usable.
18186 The caller needs to update num_attrs after adding the extra
18187 attributes. */
18188 die->num_attrs = abbrev->num_attrs;
18189
18190 for (i = 0; i < abbrev->num_attrs; ++i)
18191 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18192 info_ptr);
18193
18194 *diep = die;
18195 *has_children = abbrev->has_children;
18196 return info_ptr;
18197 }
18198
18199 /* Read a die and all its attributes.
18200 Set DIEP to point to a newly allocated die with its information,
18201 except for its child, sibling, and parent fields.
18202 Set HAS_CHILDREN to tell whether the die has children or not. */
18203
18204 static const gdb_byte *
18205 read_full_die (const struct die_reader_specs *reader,
18206 struct die_info **diep, const gdb_byte *info_ptr,
18207 int *has_children)
18208 {
18209 const gdb_byte *result;
18210
18211 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18212
18213 if (dwarf_die_debug)
18214 {
18215 fprintf_unfiltered (gdb_stdlog,
18216 "Read die from %s@0x%x of %s:\n",
18217 get_section_name (reader->die_section),
18218 (unsigned) (info_ptr - reader->die_section->buffer),
18219 bfd_get_filename (reader->abfd));
18220 dump_die (*diep, dwarf_die_debug);
18221 }
18222
18223 return result;
18224 }
18225 \f
18226 /* Abbreviation tables.
18227
18228 In DWARF version 2, the description of the debugging information is
18229 stored in a separate .debug_abbrev section. Before we read any
18230 dies from a section we read in all abbreviations and install them
18231 in a hash table. */
18232
18233 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18234
18235 struct abbrev_info *
18236 abbrev_table::alloc_abbrev ()
18237 {
18238 struct abbrev_info *abbrev;
18239
18240 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18241 memset (abbrev, 0, sizeof (struct abbrev_info));
18242
18243 return abbrev;
18244 }
18245
18246 /* Add an abbreviation to the table. */
18247
18248 void
18249 abbrev_table::add_abbrev (unsigned int abbrev_number,
18250 struct abbrev_info *abbrev)
18251 {
18252 unsigned int hash_number;
18253
18254 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18255 abbrev->next = m_abbrevs[hash_number];
18256 m_abbrevs[hash_number] = abbrev;
18257 }
18258
18259 /* Look up an abbrev in the table.
18260 Returns NULL if the abbrev is not found. */
18261
18262 struct abbrev_info *
18263 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18264 {
18265 unsigned int hash_number;
18266 struct abbrev_info *abbrev;
18267
18268 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18269 abbrev = m_abbrevs[hash_number];
18270
18271 while (abbrev)
18272 {
18273 if (abbrev->number == abbrev_number)
18274 return abbrev;
18275 abbrev = abbrev->next;
18276 }
18277 return NULL;
18278 }
18279
18280 /* Read in an abbrev table. */
18281
18282 static abbrev_table_up
18283 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18284 struct dwarf2_section_info *section,
18285 sect_offset sect_off)
18286 {
18287 struct objfile *objfile = dwarf2_per_objfile->objfile;
18288 bfd *abfd = get_section_bfd_owner (section);
18289 const gdb_byte *abbrev_ptr;
18290 struct abbrev_info *cur_abbrev;
18291 unsigned int abbrev_number, bytes_read, abbrev_name;
18292 unsigned int abbrev_form;
18293 struct attr_abbrev *cur_attrs;
18294 unsigned int allocated_attrs;
18295
18296 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18297
18298 dwarf2_read_section (objfile, section);
18299 abbrev_ptr = section->buffer + to_underlying (sect_off);
18300 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18301 abbrev_ptr += bytes_read;
18302
18303 allocated_attrs = ATTR_ALLOC_CHUNK;
18304 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18305
18306 /* Loop until we reach an abbrev number of 0. */
18307 while (abbrev_number)
18308 {
18309 cur_abbrev = abbrev_table->alloc_abbrev ();
18310
18311 /* read in abbrev header */
18312 cur_abbrev->number = abbrev_number;
18313 cur_abbrev->tag
18314 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18315 abbrev_ptr += bytes_read;
18316 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18317 abbrev_ptr += 1;
18318
18319 /* now read in declarations */
18320 for (;;)
18321 {
18322 LONGEST implicit_const;
18323
18324 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18325 abbrev_ptr += bytes_read;
18326 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18327 abbrev_ptr += bytes_read;
18328 if (abbrev_form == DW_FORM_implicit_const)
18329 {
18330 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18331 &bytes_read);
18332 abbrev_ptr += bytes_read;
18333 }
18334 else
18335 {
18336 /* Initialize it due to a false compiler warning. */
18337 implicit_const = -1;
18338 }
18339
18340 if (abbrev_name == 0)
18341 break;
18342
18343 if (cur_abbrev->num_attrs == allocated_attrs)
18344 {
18345 allocated_attrs += ATTR_ALLOC_CHUNK;
18346 cur_attrs
18347 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18348 }
18349
18350 cur_attrs[cur_abbrev->num_attrs].name
18351 = (enum dwarf_attribute) abbrev_name;
18352 cur_attrs[cur_abbrev->num_attrs].form
18353 = (enum dwarf_form) abbrev_form;
18354 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18355 ++cur_abbrev->num_attrs;
18356 }
18357
18358 cur_abbrev->attrs =
18359 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18360 cur_abbrev->num_attrs);
18361 memcpy (cur_abbrev->attrs, cur_attrs,
18362 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18363
18364 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18365
18366 /* Get next abbreviation.
18367 Under Irix6 the abbreviations for a compilation unit are not
18368 always properly terminated with an abbrev number of 0.
18369 Exit loop if we encounter an abbreviation which we have
18370 already read (which means we are about to read the abbreviations
18371 for the next compile unit) or if the end of the abbreviation
18372 table is reached. */
18373 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18374 break;
18375 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18376 abbrev_ptr += bytes_read;
18377 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18378 break;
18379 }
18380
18381 xfree (cur_attrs);
18382 return abbrev_table;
18383 }
18384
18385 /* Returns nonzero if TAG represents a type that we might generate a partial
18386 symbol for. */
18387
18388 static int
18389 is_type_tag_for_partial (int tag)
18390 {
18391 switch (tag)
18392 {
18393 #if 0
18394 /* Some types that would be reasonable to generate partial symbols for,
18395 that we don't at present. */
18396 case DW_TAG_array_type:
18397 case DW_TAG_file_type:
18398 case DW_TAG_ptr_to_member_type:
18399 case DW_TAG_set_type:
18400 case DW_TAG_string_type:
18401 case DW_TAG_subroutine_type:
18402 #endif
18403 case DW_TAG_base_type:
18404 case DW_TAG_class_type:
18405 case DW_TAG_interface_type:
18406 case DW_TAG_enumeration_type:
18407 case DW_TAG_structure_type:
18408 case DW_TAG_subrange_type:
18409 case DW_TAG_typedef:
18410 case DW_TAG_union_type:
18411 return 1;
18412 default:
18413 return 0;
18414 }
18415 }
18416
18417 /* Load all DIEs that are interesting for partial symbols into memory. */
18418
18419 static struct partial_die_info *
18420 load_partial_dies (const struct die_reader_specs *reader,
18421 const gdb_byte *info_ptr, int building_psymtab)
18422 {
18423 struct dwarf2_cu *cu = reader->cu;
18424 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18425 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18426 unsigned int bytes_read;
18427 unsigned int load_all = 0;
18428 int nesting_level = 1;
18429
18430 parent_die = NULL;
18431 last_die = NULL;
18432
18433 gdb_assert (cu->per_cu != NULL);
18434 if (cu->per_cu->load_all_dies)
18435 load_all = 1;
18436
18437 cu->partial_dies
18438 = htab_create_alloc_ex (cu->header.length / 12,
18439 partial_die_hash,
18440 partial_die_eq,
18441 NULL,
18442 &cu->comp_unit_obstack,
18443 hashtab_obstack_allocate,
18444 dummy_obstack_deallocate);
18445
18446 while (1)
18447 {
18448 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18449
18450 /* A NULL abbrev means the end of a series of children. */
18451 if (abbrev == NULL)
18452 {
18453 if (--nesting_level == 0)
18454 return first_die;
18455
18456 info_ptr += bytes_read;
18457 last_die = parent_die;
18458 parent_die = parent_die->die_parent;
18459 continue;
18460 }
18461
18462 /* Check for template arguments. We never save these; if
18463 they're seen, we just mark the parent, and go on our way. */
18464 if (parent_die != NULL
18465 && cu->language == language_cplus
18466 && (abbrev->tag == DW_TAG_template_type_param
18467 || abbrev->tag == DW_TAG_template_value_param))
18468 {
18469 parent_die->has_template_arguments = 1;
18470
18471 if (!load_all)
18472 {
18473 /* We don't need a partial DIE for the template argument. */
18474 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18475 continue;
18476 }
18477 }
18478
18479 /* We only recurse into c++ subprograms looking for template arguments.
18480 Skip their other children. */
18481 if (!load_all
18482 && cu->language == language_cplus
18483 && parent_die != NULL
18484 && parent_die->tag == DW_TAG_subprogram)
18485 {
18486 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18487 continue;
18488 }
18489
18490 /* Check whether this DIE is interesting enough to save. Normally
18491 we would not be interested in members here, but there may be
18492 later variables referencing them via DW_AT_specification (for
18493 static members). */
18494 if (!load_all
18495 && !is_type_tag_for_partial (abbrev->tag)
18496 && abbrev->tag != DW_TAG_constant
18497 && abbrev->tag != DW_TAG_enumerator
18498 && abbrev->tag != DW_TAG_subprogram
18499 && abbrev->tag != DW_TAG_inlined_subroutine
18500 && abbrev->tag != DW_TAG_lexical_block
18501 && abbrev->tag != DW_TAG_variable
18502 && abbrev->tag != DW_TAG_namespace
18503 && abbrev->tag != DW_TAG_module
18504 && abbrev->tag != DW_TAG_member
18505 && abbrev->tag != DW_TAG_imported_unit
18506 && abbrev->tag != DW_TAG_imported_declaration)
18507 {
18508 /* Otherwise we skip to the next sibling, if any. */
18509 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18510 continue;
18511 }
18512
18513 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18514 abbrev);
18515
18516 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18517
18518 /* This two-pass algorithm for processing partial symbols has a
18519 high cost in cache pressure. Thus, handle some simple cases
18520 here which cover the majority of C partial symbols. DIEs
18521 which neither have specification tags in them, nor could have
18522 specification tags elsewhere pointing at them, can simply be
18523 processed and discarded.
18524
18525 This segment is also optional; scan_partial_symbols and
18526 add_partial_symbol will handle these DIEs if we chain
18527 them in normally. When compilers which do not emit large
18528 quantities of duplicate debug information are more common,
18529 this code can probably be removed. */
18530
18531 /* Any complete simple types at the top level (pretty much all
18532 of them, for a language without namespaces), can be processed
18533 directly. */
18534 if (parent_die == NULL
18535 && pdi.has_specification == 0
18536 && pdi.is_declaration == 0
18537 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18538 || pdi.tag == DW_TAG_base_type
18539 || pdi.tag == DW_TAG_subrange_type))
18540 {
18541 if (building_psymtab && pdi.name != NULL)
18542 add_psymbol_to_list (pdi.name, strlen (pdi.name), false,
18543 VAR_DOMAIN, LOC_TYPEDEF, -1,
18544 psymbol_placement::STATIC,
18545 0, cu->language, objfile);
18546 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18547 continue;
18548 }
18549
18550 /* The exception for DW_TAG_typedef with has_children above is
18551 a workaround of GCC PR debug/47510. In the case of this complaint
18552 type_name_or_error will error on such types later.
18553
18554 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18555 it could not find the child DIEs referenced later, this is checked
18556 above. In correct DWARF DW_TAG_typedef should have no children. */
18557
18558 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18559 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18560 "- DIE at %s [in module %s]"),
18561 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18562
18563 /* If we're at the second level, and we're an enumerator, and
18564 our parent has no specification (meaning possibly lives in a
18565 namespace elsewhere), then we can add the partial symbol now
18566 instead of queueing it. */
18567 if (pdi.tag == DW_TAG_enumerator
18568 && parent_die != NULL
18569 && parent_die->die_parent == NULL
18570 && parent_die->tag == DW_TAG_enumeration_type
18571 && parent_die->has_specification == 0)
18572 {
18573 if (pdi.name == NULL)
18574 complaint (_("malformed enumerator DIE ignored"));
18575 else if (building_psymtab)
18576 add_psymbol_to_list (pdi.name, strlen (pdi.name), false,
18577 VAR_DOMAIN, LOC_CONST, -1,
18578 cu->language == language_cplus
18579 ? psymbol_placement::GLOBAL
18580 : psymbol_placement::STATIC,
18581 0, cu->language, objfile);
18582
18583 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18584 continue;
18585 }
18586
18587 struct partial_die_info *part_die
18588 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18589
18590 /* We'll save this DIE so link it in. */
18591 part_die->die_parent = parent_die;
18592 part_die->die_sibling = NULL;
18593 part_die->die_child = NULL;
18594
18595 if (last_die && last_die == parent_die)
18596 last_die->die_child = part_die;
18597 else if (last_die)
18598 last_die->die_sibling = part_die;
18599
18600 last_die = part_die;
18601
18602 if (first_die == NULL)
18603 first_die = part_die;
18604
18605 /* Maybe add the DIE to the hash table. Not all DIEs that we
18606 find interesting need to be in the hash table, because we
18607 also have the parent/sibling/child chains; only those that we
18608 might refer to by offset later during partial symbol reading.
18609
18610 For now this means things that might have be the target of a
18611 DW_AT_specification, DW_AT_abstract_origin, or
18612 DW_AT_extension. DW_AT_extension will refer only to
18613 namespaces; DW_AT_abstract_origin refers to functions (and
18614 many things under the function DIE, but we do not recurse
18615 into function DIEs during partial symbol reading) and
18616 possibly variables as well; DW_AT_specification refers to
18617 declarations. Declarations ought to have the DW_AT_declaration
18618 flag. It happens that GCC forgets to put it in sometimes, but
18619 only for functions, not for types.
18620
18621 Adding more things than necessary to the hash table is harmless
18622 except for the performance cost. Adding too few will result in
18623 wasted time in find_partial_die, when we reread the compilation
18624 unit with load_all_dies set. */
18625
18626 if (load_all
18627 || abbrev->tag == DW_TAG_constant
18628 || abbrev->tag == DW_TAG_subprogram
18629 || abbrev->tag == DW_TAG_variable
18630 || abbrev->tag == DW_TAG_namespace
18631 || part_die->is_declaration)
18632 {
18633 void **slot;
18634
18635 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18636 to_underlying (part_die->sect_off),
18637 INSERT);
18638 *slot = part_die;
18639 }
18640
18641 /* For some DIEs we want to follow their children (if any). For C
18642 we have no reason to follow the children of structures; for other
18643 languages we have to, so that we can get at method physnames
18644 to infer fully qualified class names, for DW_AT_specification,
18645 and for C++ template arguments. For C++, we also look one level
18646 inside functions to find template arguments (if the name of the
18647 function does not already contain the template arguments).
18648
18649 For Ada, we need to scan the children of subprograms and lexical
18650 blocks as well because Ada allows the definition of nested
18651 entities that could be interesting for the debugger, such as
18652 nested subprograms for instance. */
18653 if (last_die->has_children
18654 && (load_all
18655 || last_die->tag == DW_TAG_namespace
18656 || last_die->tag == DW_TAG_module
18657 || last_die->tag == DW_TAG_enumeration_type
18658 || (cu->language == language_cplus
18659 && last_die->tag == DW_TAG_subprogram
18660 && (last_die->name == NULL
18661 || strchr (last_die->name, '<') == NULL))
18662 || (cu->language != language_c
18663 && (last_die->tag == DW_TAG_class_type
18664 || last_die->tag == DW_TAG_interface_type
18665 || last_die->tag == DW_TAG_structure_type
18666 || last_die->tag == DW_TAG_union_type))
18667 || (cu->language == language_ada
18668 && (last_die->tag == DW_TAG_subprogram
18669 || last_die->tag == DW_TAG_lexical_block))))
18670 {
18671 nesting_level++;
18672 parent_die = last_die;
18673 continue;
18674 }
18675
18676 /* Otherwise we skip to the next sibling, if any. */
18677 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18678
18679 /* Back to the top, do it again. */
18680 }
18681 }
18682
18683 partial_die_info::partial_die_info (sect_offset sect_off_,
18684 struct abbrev_info *abbrev)
18685 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18686 {
18687 }
18688
18689 /* Read a minimal amount of information into the minimal die structure.
18690 INFO_PTR should point just after the initial uleb128 of a DIE. */
18691
18692 const gdb_byte *
18693 partial_die_info::read (const struct die_reader_specs *reader,
18694 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18695 {
18696 struct dwarf2_cu *cu = reader->cu;
18697 struct dwarf2_per_objfile *dwarf2_per_objfile
18698 = cu->per_cu->dwarf2_per_objfile;
18699 unsigned int i;
18700 int has_low_pc_attr = 0;
18701 int has_high_pc_attr = 0;
18702 int high_pc_relative = 0;
18703
18704 for (i = 0; i < abbrev.num_attrs; ++i)
18705 {
18706 struct attribute attr;
18707
18708 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18709
18710 /* Store the data if it is of an attribute we want to keep in a
18711 partial symbol table. */
18712 switch (attr.name)
18713 {
18714 case DW_AT_name:
18715 switch (tag)
18716 {
18717 case DW_TAG_compile_unit:
18718 case DW_TAG_partial_unit:
18719 case DW_TAG_type_unit:
18720 /* Compilation units have a DW_AT_name that is a filename, not
18721 a source language identifier. */
18722 case DW_TAG_enumeration_type:
18723 case DW_TAG_enumerator:
18724 /* These tags always have simple identifiers already; no need
18725 to canonicalize them. */
18726 name = DW_STRING (&attr);
18727 break;
18728 default:
18729 {
18730 struct objfile *objfile = dwarf2_per_objfile->objfile;
18731
18732 name
18733 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18734 &objfile->per_bfd->storage_obstack);
18735 }
18736 break;
18737 }
18738 break;
18739 case DW_AT_linkage_name:
18740 case DW_AT_MIPS_linkage_name:
18741 /* Note that both forms of linkage name might appear. We
18742 assume they will be the same, and we only store the last
18743 one we see. */
18744 linkage_name = DW_STRING (&attr);
18745 break;
18746 case DW_AT_low_pc:
18747 has_low_pc_attr = 1;
18748 lowpc = attr_value_as_address (&attr);
18749 break;
18750 case DW_AT_high_pc:
18751 has_high_pc_attr = 1;
18752 highpc = attr_value_as_address (&attr);
18753 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18754 high_pc_relative = 1;
18755 break;
18756 case DW_AT_location:
18757 /* Support the .debug_loc offsets. */
18758 if (attr_form_is_block (&attr))
18759 {
18760 d.locdesc = DW_BLOCK (&attr);
18761 }
18762 else if (attr_form_is_section_offset (&attr))
18763 {
18764 dwarf2_complex_location_expr_complaint ();
18765 }
18766 else
18767 {
18768 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18769 "partial symbol information");
18770 }
18771 break;
18772 case DW_AT_external:
18773 is_external = DW_UNSND (&attr);
18774 break;
18775 case DW_AT_declaration:
18776 is_declaration = DW_UNSND (&attr);
18777 break;
18778 case DW_AT_type:
18779 has_type = 1;
18780 break;
18781 case DW_AT_abstract_origin:
18782 case DW_AT_specification:
18783 case DW_AT_extension:
18784 has_specification = 1;
18785 spec_offset = dwarf2_get_ref_die_offset (&attr);
18786 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18787 || cu->per_cu->is_dwz);
18788 break;
18789 case DW_AT_sibling:
18790 /* Ignore absolute siblings, they might point outside of
18791 the current compile unit. */
18792 if (attr.form == DW_FORM_ref_addr)
18793 complaint (_("ignoring absolute DW_AT_sibling"));
18794 else
18795 {
18796 const gdb_byte *buffer = reader->buffer;
18797 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18798 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18799
18800 if (sibling_ptr < info_ptr)
18801 complaint (_("DW_AT_sibling points backwards"));
18802 else if (sibling_ptr > reader->buffer_end)
18803 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18804 else
18805 sibling = sibling_ptr;
18806 }
18807 break;
18808 case DW_AT_byte_size:
18809 has_byte_size = 1;
18810 break;
18811 case DW_AT_const_value:
18812 has_const_value = 1;
18813 break;
18814 case DW_AT_calling_convention:
18815 /* DWARF doesn't provide a way to identify a program's source-level
18816 entry point. DW_AT_calling_convention attributes are only meant
18817 to describe functions' calling conventions.
18818
18819 However, because it's a necessary piece of information in
18820 Fortran, and before DWARF 4 DW_CC_program was the only
18821 piece of debugging information whose definition refers to
18822 a 'main program' at all, several compilers marked Fortran
18823 main programs with DW_CC_program --- even when those
18824 functions use the standard calling conventions.
18825
18826 Although DWARF now specifies a way to provide this
18827 information, we support this practice for backward
18828 compatibility. */
18829 if (DW_UNSND (&attr) == DW_CC_program
18830 && cu->language == language_fortran)
18831 main_subprogram = 1;
18832 break;
18833 case DW_AT_inline:
18834 if (DW_UNSND (&attr) == DW_INL_inlined
18835 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18836 may_be_inlined = 1;
18837 break;
18838
18839 case DW_AT_import:
18840 if (tag == DW_TAG_imported_unit)
18841 {
18842 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18843 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18844 || cu->per_cu->is_dwz);
18845 }
18846 break;
18847
18848 case DW_AT_main_subprogram:
18849 main_subprogram = DW_UNSND (&attr);
18850 break;
18851
18852 case DW_AT_ranges:
18853 {
18854 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18855 but that requires a full DIE, so instead we just
18856 reimplement it. */
18857 int need_ranges_base = tag != DW_TAG_compile_unit;
18858 unsigned int ranges_offset = (DW_UNSND (&attr)
18859 + (need_ranges_base
18860 ? cu->ranges_base
18861 : 0));
18862
18863 /* Value of the DW_AT_ranges attribute is the offset in the
18864 .debug_ranges section. */
18865 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18866 nullptr))
18867 has_pc_info = 1;
18868 }
18869 break;
18870
18871 default:
18872 break;
18873 }
18874 }
18875
18876 /* For Ada, if both the name and the linkage name appear, we prefer
18877 the latter. This lets "catch exception" work better, regardless
18878 of the order in which the name and linkage name were emitted.
18879 Really, though, this is just a workaround for the fact that gdb
18880 doesn't store both the name and the linkage name. */
18881 if (cu->language == language_ada && linkage_name != nullptr)
18882 name = linkage_name;
18883
18884 if (high_pc_relative)
18885 highpc += lowpc;
18886
18887 if (has_low_pc_attr && has_high_pc_attr)
18888 {
18889 /* When using the GNU linker, .gnu.linkonce. sections are used to
18890 eliminate duplicate copies of functions and vtables and such.
18891 The linker will arbitrarily choose one and discard the others.
18892 The AT_*_pc values for such functions refer to local labels in
18893 these sections. If the section from that file was discarded, the
18894 labels are not in the output, so the relocs get a value of 0.
18895 If this is a discarded function, mark the pc bounds as invalid,
18896 so that GDB will ignore it. */
18897 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18898 {
18899 struct objfile *objfile = dwarf2_per_objfile->objfile;
18900 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18901
18902 complaint (_("DW_AT_low_pc %s is zero "
18903 "for DIE at %s [in module %s]"),
18904 paddress (gdbarch, lowpc),
18905 sect_offset_str (sect_off),
18906 objfile_name (objfile));
18907 }
18908 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18909 else if (lowpc >= highpc)
18910 {
18911 struct objfile *objfile = dwarf2_per_objfile->objfile;
18912 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18913
18914 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18915 "for DIE at %s [in module %s]"),
18916 paddress (gdbarch, lowpc),
18917 paddress (gdbarch, highpc),
18918 sect_offset_str (sect_off),
18919 objfile_name (objfile));
18920 }
18921 else
18922 has_pc_info = 1;
18923 }
18924
18925 return info_ptr;
18926 }
18927
18928 /* Find a cached partial DIE at OFFSET in CU. */
18929
18930 struct partial_die_info *
18931 dwarf2_cu::find_partial_die (sect_offset sect_off)
18932 {
18933 struct partial_die_info *lookup_die = NULL;
18934 struct partial_die_info part_die (sect_off);
18935
18936 lookup_die = ((struct partial_die_info *)
18937 htab_find_with_hash (partial_dies, &part_die,
18938 to_underlying (sect_off)));
18939
18940 return lookup_die;
18941 }
18942
18943 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18944 except in the case of .debug_types DIEs which do not reference
18945 outside their CU (they do however referencing other types via
18946 DW_FORM_ref_sig8). */
18947
18948 static const struct cu_partial_die_info
18949 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18950 {
18951 struct dwarf2_per_objfile *dwarf2_per_objfile
18952 = cu->per_cu->dwarf2_per_objfile;
18953 struct objfile *objfile = dwarf2_per_objfile->objfile;
18954 struct dwarf2_per_cu_data *per_cu = NULL;
18955 struct partial_die_info *pd = NULL;
18956
18957 if (offset_in_dwz == cu->per_cu->is_dwz
18958 && offset_in_cu_p (&cu->header, sect_off))
18959 {
18960 pd = cu->find_partial_die (sect_off);
18961 if (pd != NULL)
18962 return { cu, pd };
18963 /* We missed recording what we needed.
18964 Load all dies and try again. */
18965 per_cu = cu->per_cu;
18966 }
18967 else
18968 {
18969 /* TUs don't reference other CUs/TUs (except via type signatures). */
18970 if (cu->per_cu->is_debug_types)
18971 {
18972 error (_("Dwarf Error: Type Unit at offset %s contains"
18973 " external reference to offset %s [in module %s].\n"),
18974 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18975 bfd_get_filename (objfile->obfd));
18976 }
18977 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18978 dwarf2_per_objfile);
18979
18980 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18981 load_partial_comp_unit (per_cu);
18982
18983 per_cu->cu->last_used = 0;
18984 pd = per_cu->cu->find_partial_die (sect_off);
18985 }
18986
18987 /* If we didn't find it, and not all dies have been loaded,
18988 load them all and try again. */
18989
18990 if (pd == NULL && per_cu->load_all_dies == 0)
18991 {
18992 per_cu->load_all_dies = 1;
18993
18994 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18995 THIS_CU->cu may already be in use. So we can't just free it and
18996 replace its DIEs with the ones we read in. Instead, we leave those
18997 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18998 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18999 set. */
19000 load_partial_comp_unit (per_cu);
19001
19002 pd = per_cu->cu->find_partial_die (sect_off);
19003 }
19004
19005 if (pd == NULL)
19006 internal_error (__FILE__, __LINE__,
19007 _("could not find partial DIE %s "
19008 "in cache [from module %s]\n"),
19009 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19010 return { per_cu->cu, pd };
19011 }
19012
19013 /* See if we can figure out if the class lives in a namespace. We do
19014 this by looking for a member function; its demangled name will
19015 contain namespace info, if there is any. */
19016
19017 static void
19018 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19019 struct dwarf2_cu *cu)
19020 {
19021 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19022 what template types look like, because the demangler
19023 frequently doesn't give the same name as the debug info. We
19024 could fix this by only using the demangled name to get the
19025 prefix (but see comment in read_structure_type). */
19026
19027 struct partial_die_info *real_pdi;
19028 struct partial_die_info *child_pdi;
19029
19030 /* If this DIE (this DIE's specification, if any) has a parent, then
19031 we should not do this. We'll prepend the parent's fully qualified
19032 name when we create the partial symbol. */
19033
19034 real_pdi = struct_pdi;
19035 while (real_pdi->has_specification)
19036 {
19037 auto res = find_partial_die (real_pdi->spec_offset,
19038 real_pdi->spec_is_dwz, cu);
19039 real_pdi = res.pdi;
19040 cu = res.cu;
19041 }
19042
19043 if (real_pdi->die_parent != NULL)
19044 return;
19045
19046 for (child_pdi = struct_pdi->die_child;
19047 child_pdi != NULL;
19048 child_pdi = child_pdi->die_sibling)
19049 {
19050 if (child_pdi->tag == DW_TAG_subprogram
19051 && child_pdi->linkage_name != NULL)
19052 {
19053 char *actual_class_name
19054 = language_class_name_from_physname (cu->language_defn,
19055 child_pdi->linkage_name);
19056 if (actual_class_name != NULL)
19057 {
19058 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19059 struct_pdi->name
19060 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19061 actual_class_name);
19062 xfree (actual_class_name);
19063 }
19064 break;
19065 }
19066 }
19067 }
19068
19069 void
19070 partial_die_info::fixup (struct dwarf2_cu *cu)
19071 {
19072 /* Once we've fixed up a die, there's no point in doing so again.
19073 This also avoids a memory leak if we were to call
19074 guess_partial_die_structure_name multiple times. */
19075 if (fixup_called)
19076 return;
19077
19078 /* If we found a reference attribute and the DIE has no name, try
19079 to find a name in the referred to DIE. */
19080
19081 if (name == NULL && has_specification)
19082 {
19083 struct partial_die_info *spec_die;
19084
19085 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19086 spec_die = res.pdi;
19087 cu = res.cu;
19088
19089 spec_die->fixup (cu);
19090
19091 if (spec_die->name)
19092 {
19093 name = spec_die->name;
19094
19095 /* Copy DW_AT_external attribute if it is set. */
19096 if (spec_die->is_external)
19097 is_external = spec_die->is_external;
19098 }
19099 }
19100
19101 /* Set default names for some unnamed DIEs. */
19102
19103 if (name == NULL && tag == DW_TAG_namespace)
19104 name = CP_ANONYMOUS_NAMESPACE_STR;
19105
19106 /* If there is no parent die to provide a namespace, and there are
19107 children, see if we can determine the namespace from their linkage
19108 name. */
19109 if (cu->language == language_cplus
19110 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
19111 && die_parent == NULL
19112 && has_children
19113 && (tag == DW_TAG_class_type
19114 || tag == DW_TAG_structure_type
19115 || tag == DW_TAG_union_type))
19116 guess_partial_die_structure_name (this, cu);
19117
19118 /* GCC might emit a nameless struct or union that has a linkage
19119 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19120 if (name == NULL
19121 && (tag == DW_TAG_class_type
19122 || tag == DW_TAG_interface_type
19123 || tag == DW_TAG_structure_type
19124 || tag == DW_TAG_union_type)
19125 && linkage_name != NULL)
19126 {
19127 char *demangled;
19128
19129 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
19130 if (demangled)
19131 {
19132 const char *base;
19133
19134 /* Strip any leading namespaces/classes, keep only the base name.
19135 DW_AT_name for named DIEs does not contain the prefixes. */
19136 base = strrchr (demangled, ':');
19137 if (base && base > demangled && base[-1] == ':')
19138 base++;
19139 else
19140 base = demangled;
19141
19142 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19143 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
19144 xfree (demangled);
19145 }
19146 }
19147
19148 fixup_called = 1;
19149 }
19150
19151 /* Read an attribute value described by an attribute form. */
19152
19153 static const gdb_byte *
19154 read_attribute_value (const struct die_reader_specs *reader,
19155 struct attribute *attr, unsigned form,
19156 LONGEST implicit_const, const gdb_byte *info_ptr)
19157 {
19158 struct dwarf2_cu *cu = reader->cu;
19159 struct dwarf2_per_objfile *dwarf2_per_objfile
19160 = cu->per_cu->dwarf2_per_objfile;
19161 struct objfile *objfile = dwarf2_per_objfile->objfile;
19162 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19163 bfd *abfd = reader->abfd;
19164 struct comp_unit_head *cu_header = &cu->header;
19165 unsigned int bytes_read;
19166 struct dwarf_block *blk;
19167
19168 attr->form = (enum dwarf_form) form;
19169 switch (form)
19170 {
19171 case DW_FORM_ref_addr:
19172 if (cu->header.version == 2)
19173 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19174 else
19175 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19176 &cu->header, &bytes_read);
19177 info_ptr += bytes_read;
19178 break;
19179 case DW_FORM_GNU_ref_alt:
19180 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19181 info_ptr += bytes_read;
19182 break;
19183 case DW_FORM_addr:
19184 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19185 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19186 info_ptr += bytes_read;
19187 break;
19188 case DW_FORM_block2:
19189 blk = dwarf_alloc_block (cu);
19190 blk->size = read_2_bytes (abfd, info_ptr);
19191 info_ptr += 2;
19192 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19193 info_ptr += blk->size;
19194 DW_BLOCK (attr) = blk;
19195 break;
19196 case DW_FORM_block4:
19197 blk = dwarf_alloc_block (cu);
19198 blk->size = read_4_bytes (abfd, info_ptr);
19199 info_ptr += 4;
19200 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19201 info_ptr += blk->size;
19202 DW_BLOCK (attr) = blk;
19203 break;
19204 case DW_FORM_data2:
19205 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19206 info_ptr += 2;
19207 break;
19208 case DW_FORM_data4:
19209 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19210 info_ptr += 4;
19211 break;
19212 case DW_FORM_data8:
19213 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19214 info_ptr += 8;
19215 break;
19216 case DW_FORM_data16:
19217 blk = dwarf_alloc_block (cu);
19218 blk->size = 16;
19219 blk->data = read_n_bytes (abfd, info_ptr, 16);
19220 info_ptr += 16;
19221 DW_BLOCK (attr) = blk;
19222 break;
19223 case DW_FORM_sec_offset:
19224 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19225 info_ptr += bytes_read;
19226 break;
19227 case DW_FORM_string:
19228 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19229 DW_STRING_IS_CANONICAL (attr) = 0;
19230 info_ptr += bytes_read;
19231 break;
19232 case DW_FORM_strp:
19233 if (!cu->per_cu->is_dwz)
19234 {
19235 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19236 abfd, info_ptr, cu_header,
19237 &bytes_read);
19238 DW_STRING_IS_CANONICAL (attr) = 0;
19239 info_ptr += bytes_read;
19240 break;
19241 }
19242 /* FALLTHROUGH */
19243 case DW_FORM_line_strp:
19244 if (!cu->per_cu->is_dwz)
19245 {
19246 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19247 abfd, info_ptr,
19248 cu_header, &bytes_read);
19249 DW_STRING_IS_CANONICAL (attr) = 0;
19250 info_ptr += bytes_read;
19251 break;
19252 }
19253 /* FALLTHROUGH */
19254 case DW_FORM_GNU_strp_alt:
19255 {
19256 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19257 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19258 &bytes_read);
19259
19260 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19261 dwz, str_offset);
19262 DW_STRING_IS_CANONICAL (attr) = 0;
19263 info_ptr += bytes_read;
19264 }
19265 break;
19266 case DW_FORM_exprloc:
19267 case DW_FORM_block:
19268 blk = dwarf_alloc_block (cu);
19269 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19270 info_ptr += bytes_read;
19271 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19272 info_ptr += blk->size;
19273 DW_BLOCK (attr) = blk;
19274 break;
19275 case DW_FORM_block1:
19276 blk = dwarf_alloc_block (cu);
19277 blk->size = read_1_byte (abfd, info_ptr);
19278 info_ptr += 1;
19279 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19280 info_ptr += blk->size;
19281 DW_BLOCK (attr) = blk;
19282 break;
19283 case DW_FORM_data1:
19284 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19285 info_ptr += 1;
19286 break;
19287 case DW_FORM_flag:
19288 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19289 info_ptr += 1;
19290 break;
19291 case DW_FORM_flag_present:
19292 DW_UNSND (attr) = 1;
19293 break;
19294 case DW_FORM_sdata:
19295 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19296 info_ptr += bytes_read;
19297 break;
19298 case DW_FORM_udata:
19299 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19300 info_ptr += bytes_read;
19301 break;
19302 case DW_FORM_ref1:
19303 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19304 + read_1_byte (abfd, info_ptr));
19305 info_ptr += 1;
19306 break;
19307 case DW_FORM_ref2:
19308 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19309 + read_2_bytes (abfd, info_ptr));
19310 info_ptr += 2;
19311 break;
19312 case DW_FORM_ref4:
19313 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19314 + read_4_bytes (abfd, info_ptr));
19315 info_ptr += 4;
19316 break;
19317 case DW_FORM_ref8:
19318 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19319 + read_8_bytes (abfd, info_ptr));
19320 info_ptr += 8;
19321 break;
19322 case DW_FORM_ref_sig8:
19323 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19324 info_ptr += 8;
19325 break;
19326 case DW_FORM_ref_udata:
19327 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19328 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19329 info_ptr += bytes_read;
19330 break;
19331 case DW_FORM_indirect:
19332 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19333 info_ptr += bytes_read;
19334 if (form == DW_FORM_implicit_const)
19335 {
19336 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19337 info_ptr += bytes_read;
19338 }
19339 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19340 info_ptr);
19341 break;
19342 case DW_FORM_implicit_const:
19343 DW_SND (attr) = implicit_const;
19344 break;
19345 case DW_FORM_addrx:
19346 case DW_FORM_GNU_addr_index:
19347 if (reader->dwo_file == NULL)
19348 {
19349 /* For now flag a hard error.
19350 Later we can turn this into a complaint. */
19351 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19352 dwarf_form_name (form),
19353 bfd_get_filename (abfd));
19354 }
19355 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19356 info_ptr += bytes_read;
19357 break;
19358 case DW_FORM_strx:
19359 case DW_FORM_strx1:
19360 case DW_FORM_strx2:
19361 case DW_FORM_strx3:
19362 case DW_FORM_strx4:
19363 case DW_FORM_GNU_str_index:
19364 if (reader->dwo_file == NULL)
19365 {
19366 /* For now flag a hard error.
19367 Later we can turn this into a complaint if warranted. */
19368 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19369 dwarf_form_name (form),
19370 bfd_get_filename (abfd));
19371 }
19372 {
19373 ULONGEST str_index;
19374 if (form == DW_FORM_strx1)
19375 {
19376 str_index = read_1_byte (abfd, info_ptr);
19377 info_ptr += 1;
19378 }
19379 else if (form == DW_FORM_strx2)
19380 {
19381 str_index = read_2_bytes (abfd, info_ptr);
19382 info_ptr += 2;
19383 }
19384 else if (form == DW_FORM_strx3)
19385 {
19386 str_index = read_3_bytes (abfd, info_ptr);
19387 info_ptr += 3;
19388 }
19389 else if (form == DW_FORM_strx4)
19390 {
19391 str_index = read_4_bytes (abfd, info_ptr);
19392 info_ptr += 4;
19393 }
19394 else
19395 {
19396 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19397 info_ptr += bytes_read;
19398 }
19399 DW_STRING (attr) = read_str_index (reader, str_index);
19400 DW_STRING_IS_CANONICAL (attr) = 0;
19401 }
19402 break;
19403 default:
19404 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19405 dwarf_form_name (form),
19406 bfd_get_filename (abfd));
19407 }
19408
19409 /* Super hack. */
19410 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19411 attr->form = DW_FORM_GNU_ref_alt;
19412
19413 /* We have seen instances where the compiler tried to emit a byte
19414 size attribute of -1 which ended up being encoded as an unsigned
19415 0xffffffff. Although 0xffffffff is technically a valid size value,
19416 an object of this size seems pretty unlikely so we can relatively
19417 safely treat these cases as if the size attribute was invalid and
19418 treat them as zero by default. */
19419 if (attr->name == DW_AT_byte_size
19420 && form == DW_FORM_data4
19421 && DW_UNSND (attr) >= 0xffffffff)
19422 {
19423 complaint
19424 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19425 hex_string (DW_UNSND (attr)));
19426 DW_UNSND (attr) = 0;
19427 }
19428
19429 return info_ptr;
19430 }
19431
19432 /* Read an attribute described by an abbreviated attribute. */
19433
19434 static const gdb_byte *
19435 read_attribute (const struct die_reader_specs *reader,
19436 struct attribute *attr, struct attr_abbrev *abbrev,
19437 const gdb_byte *info_ptr)
19438 {
19439 attr->name = abbrev->name;
19440 return read_attribute_value (reader, attr, abbrev->form,
19441 abbrev->implicit_const, info_ptr);
19442 }
19443
19444 /* Read dwarf information from a buffer. */
19445
19446 static unsigned int
19447 read_1_byte (bfd *abfd, const gdb_byte *buf)
19448 {
19449 return bfd_get_8 (abfd, buf);
19450 }
19451
19452 static int
19453 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19454 {
19455 return bfd_get_signed_8 (abfd, buf);
19456 }
19457
19458 static unsigned int
19459 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19460 {
19461 return bfd_get_16 (abfd, buf);
19462 }
19463
19464 static int
19465 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19466 {
19467 return bfd_get_signed_16 (abfd, buf);
19468 }
19469
19470 static unsigned int
19471 read_3_bytes (bfd *abfd, const gdb_byte *buf)
19472 {
19473 unsigned int result = 0;
19474 for (int i = 0; i < 3; ++i)
19475 {
19476 unsigned char byte = bfd_get_8 (abfd, buf);
19477 buf++;
19478 result |= ((unsigned int) byte << (i * 8));
19479 }
19480 return result;
19481 }
19482
19483 static unsigned int
19484 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19485 {
19486 return bfd_get_32 (abfd, buf);
19487 }
19488
19489 static int
19490 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19491 {
19492 return bfd_get_signed_32 (abfd, buf);
19493 }
19494
19495 static ULONGEST
19496 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19497 {
19498 return bfd_get_64 (abfd, buf);
19499 }
19500
19501 static CORE_ADDR
19502 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19503 unsigned int *bytes_read)
19504 {
19505 struct comp_unit_head *cu_header = &cu->header;
19506 CORE_ADDR retval = 0;
19507
19508 if (cu_header->signed_addr_p)
19509 {
19510 switch (cu_header->addr_size)
19511 {
19512 case 2:
19513 retval = bfd_get_signed_16 (abfd, buf);
19514 break;
19515 case 4:
19516 retval = bfd_get_signed_32 (abfd, buf);
19517 break;
19518 case 8:
19519 retval = bfd_get_signed_64 (abfd, buf);
19520 break;
19521 default:
19522 internal_error (__FILE__, __LINE__,
19523 _("read_address: bad switch, signed [in module %s]"),
19524 bfd_get_filename (abfd));
19525 }
19526 }
19527 else
19528 {
19529 switch (cu_header->addr_size)
19530 {
19531 case 2:
19532 retval = bfd_get_16 (abfd, buf);
19533 break;
19534 case 4:
19535 retval = bfd_get_32 (abfd, buf);
19536 break;
19537 case 8:
19538 retval = bfd_get_64 (abfd, buf);
19539 break;
19540 default:
19541 internal_error (__FILE__, __LINE__,
19542 _("read_address: bad switch, "
19543 "unsigned [in module %s]"),
19544 bfd_get_filename (abfd));
19545 }
19546 }
19547
19548 *bytes_read = cu_header->addr_size;
19549 return retval;
19550 }
19551
19552 /* Read the initial length from a section. The (draft) DWARF 3
19553 specification allows the initial length to take up either 4 bytes
19554 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19555 bytes describe the length and all offsets will be 8 bytes in length
19556 instead of 4.
19557
19558 An older, non-standard 64-bit format is also handled by this
19559 function. The older format in question stores the initial length
19560 as an 8-byte quantity without an escape value. Lengths greater
19561 than 2^32 aren't very common which means that the initial 4 bytes
19562 is almost always zero. Since a length value of zero doesn't make
19563 sense for the 32-bit format, this initial zero can be considered to
19564 be an escape value which indicates the presence of the older 64-bit
19565 format. As written, the code can't detect (old format) lengths
19566 greater than 4GB. If it becomes necessary to handle lengths
19567 somewhat larger than 4GB, we could allow other small values (such
19568 as the non-sensical values of 1, 2, and 3) to also be used as
19569 escape values indicating the presence of the old format.
19570
19571 The value returned via bytes_read should be used to increment the
19572 relevant pointer after calling read_initial_length().
19573
19574 [ Note: read_initial_length() and read_offset() are based on the
19575 document entitled "DWARF Debugging Information Format", revision
19576 3, draft 8, dated November 19, 2001. This document was obtained
19577 from:
19578
19579 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19580
19581 This document is only a draft and is subject to change. (So beware.)
19582
19583 Details regarding the older, non-standard 64-bit format were
19584 determined empirically by examining 64-bit ELF files produced by
19585 the SGI toolchain on an IRIX 6.5 machine.
19586
19587 - Kevin, July 16, 2002
19588 ] */
19589
19590 static LONGEST
19591 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19592 {
19593 LONGEST length = bfd_get_32 (abfd, buf);
19594
19595 if (length == 0xffffffff)
19596 {
19597 length = bfd_get_64 (abfd, buf + 4);
19598 *bytes_read = 12;
19599 }
19600 else if (length == 0)
19601 {
19602 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19603 length = bfd_get_64 (abfd, buf);
19604 *bytes_read = 8;
19605 }
19606 else
19607 {
19608 *bytes_read = 4;
19609 }
19610
19611 return length;
19612 }
19613
19614 /* Cover function for read_initial_length.
19615 Returns the length of the object at BUF, and stores the size of the
19616 initial length in *BYTES_READ and stores the size that offsets will be in
19617 *OFFSET_SIZE.
19618 If the initial length size is not equivalent to that specified in
19619 CU_HEADER then issue a complaint.
19620 This is useful when reading non-comp-unit headers. */
19621
19622 static LONGEST
19623 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19624 const struct comp_unit_head *cu_header,
19625 unsigned int *bytes_read,
19626 unsigned int *offset_size)
19627 {
19628 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19629
19630 gdb_assert (cu_header->initial_length_size == 4
19631 || cu_header->initial_length_size == 8
19632 || cu_header->initial_length_size == 12);
19633
19634 if (cu_header->initial_length_size != *bytes_read)
19635 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19636
19637 *offset_size = (*bytes_read == 4) ? 4 : 8;
19638 return length;
19639 }
19640
19641 /* Read an offset from the data stream. The size of the offset is
19642 given by cu_header->offset_size. */
19643
19644 static LONGEST
19645 read_offset (bfd *abfd, const gdb_byte *buf,
19646 const struct comp_unit_head *cu_header,
19647 unsigned int *bytes_read)
19648 {
19649 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19650
19651 *bytes_read = cu_header->offset_size;
19652 return offset;
19653 }
19654
19655 /* Read an offset from the data stream. */
19656
19657 static LONGEST
19658 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19659 {
19660 LONGEST retval = 0;
19661
19662 switch (offset_size)
19663 {
19664 case 4:
19665 retval = bfd_get_32 (abfd, buf);
19666 break;
19667 case 8:
19668 retval = bfd_get_64 (abfd, buf);
19669 break;
19670 default:
19671 internal_error (__FILE__, __LINE__,
19672 _("read_offset_1: bad switch [in module %s]"),
19673 bfd_get_filename (abfd));
19674 }
19675
19676 return retval;
19677 }
19678
19679 static const gdb_byte *
19680 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19681 {
19682 /* If the size of a host char is 8 bits, we can return a pointer
19683 to the buffer, otherwise we have to copy the data to a buffer
19684 allocated on the temporary obstack. */
19685 gdb_assert (HOST_CHAR_BIT == 8);
19686 return buf;
19687 }
19688
19689 static const char *
19690 read_direct_string (bfd *abfd, const gdb_byte *buf,
19691 unsigned int *bytes_read_ptr)
19692 {
19693 /* If the size of a host char is 8 bits, we can return a pointer
19694 to the string, otherwise we have to copy the string to a buffer
19695 allocated on the temporary obstack. */
19696 gdb_assert (HOST_CHAR_BIT == 8);
19697 if (*buf == '\0')
19698 {
19699 *bytes_read_ptr = 1;
19700 return NULL;
19701 }
19702 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19703 return (const char *) buf;
19704 }
19705
19706 /* Return pointer to string at section SECT offset STR_OFFSET with error
19707 reporting strings FORM_NAME and SECT_NAME. */
19708
19709 static const char *
19710 read_indirect_string_at_offset_from (struct objfile *objfile,
19711 bfd *abfd, LONGEST str_offset,
19712 struct dwarf2_section_info *sect,
19713 const char *form_name,
19714 const char *sect_name)
19715 {
19716 dwarf2_read_section (objfile, sect);
19717 if (sect->buffer == NULL)
19718 error (_("%s used without %s section [in module %s]"),
19719 form_name, sect_name, bfd_get_filename (abfd));
19720 if (str_offset >= sect->size)
19721 error (_("%s pointing outside of %s section [in module %s]"),
19722 form_name, sect_name, bfd_get_filename (abfd));
19723 gdb_assert (HOST_CHAR_BIT == 8);
19724 if (sect->buffer[str_offset] == '\0')
19725 return NULL;
19726 return (const char *) (sect->buffer + str_offset);
19727 }
19728
19729 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19730
19731 static const char *
19732 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19733 bfd *abfd, LONGEST str_offset)
19734 {
19735 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19736 abfd, str_offset,
19737 &dwarf2_per_objfile->str,
19738 "DW_FORM_strp", ".debug_str");
19739 }
19740
19741 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19742
19743 static const char *
19744 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19745 bfd *abfd, LONGEST str_offset)
19746 {
19747 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19748 abfd, str_offset,
19749 &dwarf2_per_objfile->line_str,
19750 "DW_FORM_line_strp",
19751 ".debug_line_str");
19752 }
19753
19754 /* Read a string at offset STR_OFFSET in the .debug_str section from
19755 the .dwz file DWZ. Throw an error if the offset is too large. If
19756 the string consists of a single NUL byte, return NULL; otherwise
19757 return a pointer to the string. */
19758
19759 static const char *
19760 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19761 LONGEST str_offset)
19762 {
19763 dwarf2_read_section (objfile, &dwz->str);
19764
19765 if (dwz->str.buffer == NULL)
19766 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19767 "section [in module %s]"),
19768 bfd_get_filename (dwz->dwz_bfd.get ()));
19769 if (str_offset >= dwz->str.size)
19770 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19771 ".debug_str section [in module %s]"),
19772 bfd_get_filename (dwz->dwz_bfd.get ()));
19773 gdb_assert (HOST_CHAR_BIT == 8);
19774 if (dwz->str.buffer[str_offset] == '\0')
19775 return NULL;
19776 return (const char *) (dwz->str.buffer + str_offset);
19777 }
19778
19779 /* Return pointer to string at .debug_str offset as read from BUF.
19780 BUF is assumed to be in a compilation unit described by CU_HEADER.
19781 Return *BYTES_READ_PTR count of bytes read from BUF. */
19782
19783 static const char *
19784 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19785 const gdb_byte *buf,
19786 const struct comp_unit_head *cu_header,
19787 unsigned int *bytes_read_ptr)
19788 {
19789 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19790
19791 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19792 }
19793
19794 /* Return pointer to string at .debug_line_str offset as read from BUF.
19795 BUF is assumed to be in a compilation unit described by CU_HEADER.
19796 Return *BYTES_READ_PTR count of bytes read from BUF. */
19797
19798 static const char *
19799 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19800 bfd *abfd, const gdb_byte *buf,
19801 const struct comp_unit_head *cu_header,
19802 unsigned int *bytes_read_ptr)
19803 {
19804 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19805
19806 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19807 str_offset);
19808 }
19809
19810 ULONGEST
19811 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19812 unsigned int *bytes_read_ptr)
19813 {
19814 ULONGEST result;
19815 unsigned int num_read;
19816 int shift;
19817 unsigned char byte;
19818
19819 result = 0;
19820 shift = 0;
19821 num_read = 0;
19822 while (1)
19823 {
19824 byte = bfd_get_8 (abfd, buf);
19825 buf++;
19826 num_read++;
19827 result |= ((ULONGEST) (byte & 127) << shift);
19828 if ((byte & 128) == 0)
19829 {
19830 break;
19831 }
19832 shift += 7;
19833 }
19834 *bytes_read_ptr = num_read;
19835 return result;
19836 }
19837
19838 static LONGEST
19839 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19840 unsigned int *bytes_read_ptr)
19841 {
19842 ULONGEST result;
19843 int shift, num_read;
19844 unsigned char byte;
19845
19846 result = 0;
19847 shift = 0;
19848 num_read = 0;
19849 while (1)
19850 {
19851 byte = bfd_get_8 (abfd, buf);
19852 buf++;
19853 num_read++;
19854 result |= ((ULONGEST) (byte & 127) << shift);
19855 shift += 7;
19856 if ((byte & 128) == 0)
19857 {
19858 break;
19859 }
19860 }
19861 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19862 result |= -(((ULONGEST) 1) << shift);
19863 *bytes_read_ptr = num_read;
19864 return result;
19865 }
19866
19867 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19868 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19869 ADDR_SIZE is the size of addresses from the CU header. */
19870
19871 static CORE_ADDR
19872 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19873 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19874 {
19875 struct objfile *objfile = dwarf2_per_objfile->objfile;
19876 bfd *abfd = objfile->obfd;
19877 const gdb_byte *info_ptr;
19878
19879 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19880 if (dwarf2_per_objfile->addr.buffer == NULL)
19881 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19882 objfile_name (objfile));
19883 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19884 error (_("DW_FORM_addr_index pointing outside of "
19885 ".debug_addr section [in module %s]"),
19886 objfile_name (objfile));
19887 info_ptr = (dwarf2_per_objfile->addr.buffer
19888 + addr_base + addr_index * addr_size);
19889 if (addr_size == 4)
19890 return bfd_get_32 (abfd, info_ptr);
19891 else
19892 return bfd_get_64 (abfd, info_ptr);
19893 }
19894
19895 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19896
19897 static CORE_ADDR
19898 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19899 {
19900 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19901 cu->addr_base, cu->header.addr_size);
19902 }
19903
19904 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19905
19906 static CORE_ADDR
19907 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19908 unsigned int *bytes_read)
19909 {
19910 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19911 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19912
19913 return read_addr_index (cu, addr_index);
19914 }
19915
19916 /* Data structure to pass results from dwarf2_read_addr_index_reader
19917 back to dwarf2_read_addr_index. */
19918
19919 struct dwarf2_read_addr_index_data
19920 {
19921 ULONGEST addr_base;
19922 int addr_size;
19923 };
19924
19925 /* die_reader_func for dwarf2_read_addr_index. */
19926
19927 static void
19928 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19929 const gdb_byte *info_ptr,
19930 struct die_info *comp_unit_die,
19931 int has_children,
19932 void *data)
19933 {
19934 struct dwarf2_cu *cu = reader->cu;
19935 struct dwarf2_read_addr_index_data *aidata =
19936 (struct dwarf2_read_addr_index_data *) data;
19937
19938 aidata->addr_base = cu->addr_base;
19939 aidata->addr_size = cu->header.addr_size;
19940 }
19941
19942 /* Given an index in .debug_addr, fetch the value.
19943 NOTE: This can be called during dwarf expression evaluation,
19944 long after the debug information has been read, and thus per_cu->cu
19945 may no longer exist. */
19946
19947 CORE_ADDR
19948 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19949 unsigned int addr_index)
19950 {
19951 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19952 struct dwarf2_cu *cu = per_cu->cu;
19953 ULONGEST addr_base;
19954 int addr_size;
19955
19956 /* We need addr_base and addr_size.
19957 If we don't have PER_CU->cu, we have to get it.
19958 Nasty, but the alternative is storing the needed info in PER_CU,
19959 which at this point doesn't seem justified: it's not clear how frequently
19960 it would get used and it would increase the size of every PER_CU.
19961 Entry points like dwarf2_per_cu_addr_size do a similar thing
19962 so we're not in uncharted territory here.
19963 Alas we need to be a bit more complicated as addr_base is contained
19964 in the DIE.
19965
19966 We don't need to read the entire CU(/TU).
19967 We just need the header and top level die.
19968
19969 IWBN to use the aging mechanism to let us lazily later discard the CU.
19970 For now we skip this optimization. */
19971
19972 if (cu != NULL)
19973 {
19974 addr_base = cu->addr_base;
19975 addr_size = cu->header.addr_size;
19976 }
19977 else
19978 {
19979 struct dwarf2_read_addr_index_data aidata;
19980
19981 /* Note: We can't use init_cutu_and_read_dies_simple here,
19982 we need addr_base. */
19983 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
19984 dwarf2_read_addr_index_reader, &aidata);
19985 addr_base = aidata.addr_base;
19986 addr_size = aidata.addr_size;
19987 }
19988
19989 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19990 addr_size);
19991 }
19992
19993 /* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
19994 This is only used by the Fission support. */
19995
19996 static const char *
19997 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19998 {
19999 struct dwarf2_cu *cu = reader->cu;
20000 struct dwarf2_per_objfile *dwarf2_per_objfile
20001 = cu->per_cu->dwarf2_per_objfile;
20002 struct objfile *objfile = dwarf2_per_objfile->objfile;
20003 const char *objf_name = objfile_name (objfile);
20004 bfd *abfd = objfile->obfd;
20005 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20006 struct dwarf2_section_info *str_offsets_section =
20007 &reader->dwo_file->sections.str_offsets;
20008 const gdb_byte *info_ptr;
20009 ULONGEST str_offset;
20010 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20011
20012 dwarf2_read_section (objfile, str_section);
20013 dwarf2_read_section (objfile, str_offsets_section);
20014 if (str_section->buffer == NULL)
20015 error (_("%s used without .debug_str.dwo section"
20016 " in CU at offset %s [in module %s]"),
20017 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20018 if (str_offsets_section->buffer == NULL)
20019 error (_("%s used without .debug_str_offsets.dwo section"
20020 " in CU at offset %s [in module %s]"),
20021 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20022 if (str_index * cu->header.offset_size >= str_offsets_section->size)
20023 error (_("%s pointing outside of .debug_str_offsets.dwo"
20024 " section in CU at offset %s [in module %s]"),
20025 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20026 info_ptr = (str_offsets_section->buffer
20027 + str_index * cu->header.offset_size);
20028 if (cu->header.offset_size == 4)
20029 str_offset = bfd_get_32 (abfd, info_ptr);
20030 else
20031 str_offset = bfd_get_64 (abfd, info_ptr);
20032 if (str_offset >= str_section->size)
20033 error (_("Offset from %s pointing outside of"
20034 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20035 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20036 return (const char *) (str_section->buffer + str_offset);
20037 }
20038
20039 /* Return the length of an LEB128 number in BUF. */
20040
20041 static int
20042 leb128_size (const gdb_byte *buf)
20043 {
20044 const gdb_byte *begin = buf;
20045 gdb_byte byte;
20046
20047 while (1)
20048 {
20049 byte = *buf++;
20050 if ((byte & 128) == 0)
20051 return buf - begin;
20052 }
20053 }
20054
20055 static void
20056 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20057 {
20058 switch (lang)
20059 {
20060 case DW_LANG_C89:
20061 case DW_LANG_C99:
20062 case DW_LANG_C11:
20063 case DW_LANG_C:
20064 case DW_LANG_UPC:
20065 cu->language = language_c;
20066 break;
20067 case DW_LANG_Java:
20068 case DW_LANG_C_plus_plus:
20069 case DW_LANG_C_plus_plus_11:
20070 case DW_LANG_C_plus_plus_14:
20071 cu->language = language_cplus;
20072 break;
20073 case DW_LANG_D:
20074 cu->language = language_d;
20075 break;
20076 case DW_LANG_Fortran77:
20077 case DW_LANG_Fortran90:
20078 case DW_LANG_Fortran95:
20079 case DW_LANG_Fortran03:
20080 case DW_LANG_Fortran08:
20081 cu->language = language_fortran;
20082 break;
20083 case DW_LANG_Go:
20084 cu->language = language_go;
20085 break;
20086 case DW_LANG_Mips_Assembler:
20087 cu->language = language_asm;
20088 break;
20089 case DW_LANG_Ada83:
20090 case DW_LANG_Ada95:
20091 cu->language = language_ada;
20092 break;
20093 case DW_LANG_Modula2:
20094 cu->language = language_m2;
20095 break;
20096 case DW_LANG_Pascal83:
20097 cu->language = language_pascal;
20098 break;
20099 case DW_LANG_ObjC:
20100 cu->language = language_objc;
20101 break;
20102 case DW_LANG_Rust:
20103 case DW_LANG_Rust_old:
20104 cu->language = language_rust;
20105 break;
20106 case DW_LANG_Cobol74:
20107 case DW_LANG_Cobol85:
20108 default:
20109 cu->language = language_minimal;
20110 break;
20111 }
20112 cu->language_defn = language_def (cu->language);
20113 }
20114
20115 /* Return the named attribute or NULL if not there. */
20116
20117 static struct attribute *
20118 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20119 {
20120 for (;;)
20121 {
20122 unsigned int i;
20123 struct attribute *spec = NULL;
20124
20125 for (i = 0; i < die->num_attrs; ++i)
20126 {
20127 if (die->attrs[i].name == name)
20128 return &die->attrs[i];
20129 if (die->attrs[i].name == DW_AT_specification
20130 || die->attrs[i].name == DW_AT_abstract_origin)
20131 spec = &die->attrs[i];
20132 }
20133
20134 if (!spec)
20135 break;
20136
20137 die = follow_die_ref (die, spec, &cu);
20138 }
20139
20140 return NULL;
20141 }
20142
20143 /* Return the named attribute or NULL if not there,
20144 but do not follow DW_AT_specification, etc.
20145 This is for use in contexts where we're reading .debug_types dies.
20146 Following DW_AT_specification, DW_AT_abstract_origin will take us
20147 back up the chain, and we want to go down. */
20148
20149 static struct attribute *
20150 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20151 {
20152 unsigned int i;
20153
20154 for (i = 0; i < die->num_attrs; ++i)
20155 if (die->attrs[i].name == name)
20156 return &die->attrs[i];
20157
20158 return NULL;
20159 }
20160
20161 /* Return the string associated with a string-typed attribute, or NULL if it
20162 is either not found or is of an incorrect type. */
20163
20164 static const char *
20165 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20166 {
20167 struct attribute *attr;
20168 const char *str = NULL;
20169
20170 attr = dwarf2_attr (die, name, cu);
20171
20172 if (attr != NULL)
20173 {
20174 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20175 || attr->form == DW_FORM_string
20176 || attr->form == DW_FORM_strx
20177 || attr->form == DW_FORM_strx1
20178 || attr->form == DW_FORM_strx2
20179 || attr->form == DW_FORM_strx3
20180 || attr->form == DW_FORM_strx4
20181 || attr->form == DW_FORM_GNU_str_index
20182 || attr->form == DW_FORM_GNU_strp_alt)
20183 str = DW_STRING (attr);
20184 else
20185 complaint (_("string type expected for attribute %s for "
20186 "DIE at %s in module %s"),
20187 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20188 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20189 }
20190
20191 return str;
20192 }
20193
20194 /* Return the dwo name or NULL if not present. If present, it is in either
20195 DW_AT_GNU_dwo_name or DW_AT_dwo_name atrribute. */
20196 static const char *
20197 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20198 {
20199 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20200 if (dwo_name == nullptr)
20201 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20202 return dwo_name;
20203 }
20204
20205 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20206 and holds a non-zero value. This function should only be used for
20207 DW_FORM_flag or DW_FORM_flag_present attributes. */
20208
20209 static int
20210 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20211 {
20212 struct attribute *attr = dwarf2_attr (die, name, cu);
20213
20214 return (attr && DW_UNSND (attr));
20215 }
20216
20217 static int
20218 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20219 {
20220 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20221 which value is non-zero. However, we have to be careful with
20222 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20223 (via dwarf2_flag_true_p) follows this attribute. So we may
20224 end up accidently finding a declaration attribute that belongs
20225 to a different DIE referenced by the specification attribute,
20226 even though the given DIE does not have a declaration attribute. */
20227 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20228 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20229 }
20230
20231 /* Return the die giving the specification for DIE, if there is
20232 one. *SPEC_CU is the CU containing DIE on input, and the CU
20233 containing the return value on output. If there is no
20234 specification, but there is an abstract origin, that is
20235 returned. */
20236
20237 static struct die_info *
20238 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20239 {
20240 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20241 *spec_cu);
20242
20243 if (spec_attr == NULL)
20244 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20245
20246 if (spec_attr == NULL)
20247 return NULL;
20248 else
20249 return follow_die_ref (die, spec_attr, spec_cu);
20250 }
20251
20252 /* Stub for free_line_header to match void * callback types. */
20253
20254 static void
20255 free_line_header_voidp (void *arg)
20256 {
20257 struct line_header *lh = (struct line_header *) arg;
20258
20259 delete lh;
20260 }
20261
20262 void
20263 line_header::add_include_dir (const char *include_dir)
20264 {
20265 if (dwarf_line_debug >= 2)
20266 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20267 include_dirs.size () + 1, include_dir);
20268
20269 include_dirs.push_back (include_dir);
20270 }
20271
20272 void
20273 line_header::add_file_name (const char *name,
20274 dir_index d_index,
20275 unsigned int mod_time,
20276 unsigned int length)
20277 {
20278 if (dwarf_line_debug >= 2)
20279 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
20280 (unsigned) file_names.size () + 1, name);
20281
20282 file_names.emplace_back (name, d_index, mod_time, length);
20283 }
20284
20285 /* A convenience function to find the proper .debug_line section for a CU. */
20286
20287 static struct dwarf2_section_info *
20288 get_debug_line_section (struct dwarf2_cu *cu)
20289 {
20290 struct dwarf2_section_info *section;
20291 struct dwarf2_per_objfile *dwarf2_per_objfile
20292 = cu->per_cu->dwarf2_per_objfile;
20293
20294 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20295 DWO file. */
20296 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20297 section = &cu->dwo_unit->dwo_file->sections.line;
20298 else if (cu->per_cu->is_dwz)
20299 {
20300 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20301
20302 section = &dwz->line;
20303 }
20304 else
20305 section = &dwarf2_per_objfile->line;
20306
20307 return section;
20308 }
20309
20310 /* Read directory or file name entry format, starting with byte of
20311 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20312 entries count and the entries themselves in the described entry
20313 format. */
20314
20315 static void
20316 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20317 bfd *abfd, const gdb_byte **bufp,
20318 struct line_header *lh,
20319 const struct comp_unit_head *cu_header,
20320 void (*callback) (struct line_header *lh,
20321 const char *name,
20322 dir_index d_index,
20323 unsigned int mod_time,
20324 unsigned int length))
20325 {
20326 gdb_byte format_count, formati;
20327 ULONGEST data_count, datai;
20328 const gdb_byte *buf = *bufp;
20329 const gdb_byte *format_header_data;
20330 unsigned int bytes_read;
20331
20332 format_count = read_1_byte (abfd, buf);
20333 buf += 1;
20334 format_header_data = buf;
20335 for (formati = 0; formati < format_count; formati++)
20336 {
20337 read_unsigned_leb128 (abfd, buf, &bytes_read);
20338 buf += bytes_read;
20339 read_unsigned_leb128 (abfd, buf, &bytes_read);
20340 buf += bytes_read;
20341 }
20342
20343 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20344 buf += bytes_read;
20345 for (datai = 0; datai < data_count; datai++)
20346 {
20347 const gdb_byte *format = format_header_data;
20348 struct file_entry fe;
20349
20350 for (formati = 0; formati < format_count; formati++)
20351 {
20352 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20353 format += bytes_read;
20354
20355 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20356 format += bytes_read;
20357
20358 gdb::optional<const char *> string;
20359 gdb::optional<unsigned int> uint;
20360
20361 switch (form)
20362 {
20363 case DW_FORM_string:
20364 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20365 buf += bytes_read;
20366 break;
20367
20368 case DW_FORM_line_strp:
20369 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20370 abfd, buf,
20371 cu_header,
20372 &bytes_read));
20373 buf += bytes_read;
20374 break;
20375
20376 case DW_FORM_data1:
20377 uint.emplace (read_1_byte (abfd, buf));
20378 buf += 1;
20379 break;
20380
20381 case DW_FORM_data2:
20382 uint.emplace (read_2_bytes (abfd, buf));
20383 buf += 2;
20384 break;
20385
20386 case DW_FORM_data4:
20387 uint.emplace (read_4_bytes (abfd, buf));
20388 buf += 4;
20389 break;
20390
20391 case DW_FORM_data8:
20392 uint.emplace (read_8_bytes (abfd, buf));
20393 buf += 8;
20394 break;
20395
20396 case DW_FORM_udata:
20397 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20398 buf += bytes_read;
20399 break;
20400
20401 case DW_FORM_block:
20402 /* It is valid only for DW_LNCT_timestamp which is ignored by
20403 current GDB. */
20404 break;
20405 }
20406
20407 switch (content_type)
20408 {
20409 case DW_LNCT_path:
20410 if (string.has_value ())
20411 fe.name = *string;
20412 break;
20413 case DW_LNCT_directory_index:
20414 if (uint.has_value ())
20415 fe.d_index = (dir_index) *uint;
20416 break;
20417 case DW_LNCT_timestamp:
20418 if (uint.has_value ())
20419 fe.mod_time = *uint;
20420 break;
20421 case DW_LNCT_size:
20422 if (uint.has_value ())
20423 fe.length = *uint;
20424 break;
20425 case DW_LNCT_MD5:
20426 break;
20427 default:
20428 complaint (_("Unknown format content type %s"),
20429 pulongest (content_type));
20430 }
20431 }
20432
20433 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20434 }
20435
20436 *bufp = buf;
20437 }
20438
20439 /* Read the statement program header starting at OFFSET in
20440 .debug_line, or .debug_line.dwo. Return a pointer
20441 to a struct line_header, allocated using xmalloc.
20442 Returns NULL if there is a problem reading the header, e.g., if it
20443 has a version we don't understand.
20444
20445 NOTE: the strings in the include directory and file name tables of
20446 the returned object point into the dwarf line section buffer,
20447 and must not be freed. */
20448
20449 static line_header_up
20450 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20451 {
20452 const gdb_byte *line_ptr;
20453 unsigned int bytes_read, offset_size;
20454 int i;
20455 const char *cur_dir, *cur_file;
20456 struct dwarf2_section_info *section;
20457 bfd *abfd;
20458 struct dwarf2_per_objfile *dwarf2_per_objfile
20459 = cu->per_cu->dwarf2_per_objfile;
20460
20461 section = get_debug_line_section (cu);
20462 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20463 if (section->buffer == NULL)
20464 {
20465 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20466 complaint (_("missing .debug_line.dwo section"));
20467 else
20468 complaint (_("missing .debug_line section"));
20469 return 0;
20470 }
20471
20472 /* We can't do this until we know the section is non-empty.
20473 Only then do we know we have such a section. */
20474 abfd = get_section_bfd_owner (section);
20475
20476 /* Make sure that at least there's room for the total_length field.
20477 That could be 12 bytes long, but we're just going to fudge that. */
20478 if (to_underlying (sect_off) + 4 >= section->size)
20479 {
20480 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20481 return 0;
20482 }
20483
20484 line_header_up lh (new line_header ());
20485
20486 lh->sect_off = sect_off;
20487 lh->offset_in_dwz = cu->per_cu->is_dwz;
20488
20489 line_ptr = section->buffer + to_underlying (sect_off);
20490
20491 /* Read in the header. */
20492 lh->total_length =
20493 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20494 &bytes_read, &offset_size);
20495 line_ptr += bytes_read;
20496 if (line_ptr + lh->total_length > (section->buffer + section->size))
20497 {
20498 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20499 return 0;
20500 }
20501 lh->statement_program_end = line_ptr + lh->total_length;
20502 lh->version = read_2_bytes (abfd, line_ptr);
20503 line_ptr += 2;
20504 if (lh->version > 5)
20505 {
20506 /* This is a version we don't understand. The format could have
20507 changed in ways we don't handle properly so just punt. */
20508 complaint (_("unsupported version in .debug_line section"));
20509 return NULL;
20510 }
20511 if (lh->version >= 5)
20512 {
20513 gdb_byte segment_selector_size;
20514
20515 /* Skip address size. */
20516 read_1_byte (abfd, line_ptr);
20517 line_ptr += 1;
20518
20519 segment_selector_size = read_1_byte (abfd, line_ptr);
20520 line_ptr += 1;
20521 if (segment_selector_size != 0)
20522 {
20523 complaint (_("unsupported segment selector size %u "
20524 "in .debug_line section"),
20525 segment_selector_size);
20526 return NULL;
20527 }
20528 }
20529 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20530 line_ptr += offset_size;
20531 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20532 line_ptr += 1;
20533 if (lh->version >= 4)
20534 {
20535 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20536 line_ptr += 1;
20537 }
20538 else
20539 lh->maximum_ops_per_instruction = 1;
20540
20541 if (lh->maximum_ops_per_instruction == 0)
20542 {
20543 lh->maximum_ops_per_instruction = 1;
20544 complaint (_("invalid maximum_ops_per_instruction "
20545 "in `.debug_line' section"));
20546 }
20547
20548 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20549 line_ptr += 1;
20550 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20551 line_ptr += 1;
20552 lh->line_range = read_1_byte (abfd, line_ptr);
20553 line_ptr += 1;
20554 lh->opcode_base = read_1_byte (abfd, line_ptr);
20555 line_ptr += 1;
20556 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20557
20558 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20559 for (i = 1; i < lh->opcode_base; ++i)
20560 {
20561 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20562 line_ptr += 1;
20563 }
20564
20565 if (lh->version >= 5)
20566 {
20567 /* Read directory table. */
20568 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20569 &cu->header,
20570 [] (struct line_header *header, const char *name,
20571 dir_index d_index, unsigned int mod_time,
20572 unsigned int length)
20573 {
20574 header->add_include_dir (name);
20575 });
20576
20577 /* Read file name table. */
20578 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20579 &cu->header,
20580 [] (struct line_header *header, const char *name,
20581 dir_index d_index, unsigned int mod_time,
20582 unsigned int length)
20583 {
20584 header->add_file_name (name, d_index, mod_time, length);
20585 });
20586 }
20587 else
20588 {
20589 /* Read directory table. */
20590 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20591 {
20592 line_ptr += bytes_read;
20593 lh->add_include_dir (cur_dir);
20594 }
20595 line_ptr += bytes_read;
20596
20597 /* Read file name table. */
20598 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20599 {
20600 unsigned int mod_time, length;
20601 dir_index d_index;
20602
20603 line_ptr += bytes_read;
20604 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20605 line_ptr += bytes_read;
20606 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20607 line_ptr += bytes_read;
20608 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20609 line_ptr += bytes_read;
20610
20611 lh->add_file_name (cur_file, d_index, mod_time, length);
20612 }
20613 line_ptr += bytes_read;
20614 }
20615 lh->statement_program_start = line_ptr;
20616
20617 if (line_ptr > (section->buffer + section->size))
20618 complaint (_("line number info header doesn't "
20619 "fit in `.debug_line' section"));
20620
20621 return lh;
20622 }
20623
20624 /* Subroutine of dwarf_decode_lines to simplify it.
20625 Return the file name of the psymtab for included file FILE_INDEX
20626 in line header LH of PST.
20627 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20628 If space for the result is malloc'd, *NAME_HOLDER will be set.
20629 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20630
20631 static const char *
20632 psymtab_include_file_name (const struct line_header *lh, int file_index,
20633 const struct partial_symtab *pst,
20634 const char *comp_dir,
20635 gdb::unique_xmalloc_ptr<char> *name_holder)
20636 {
20637 const file_entry &fe = lh->file_names[file_index];
20638 const char *include_name = fe.name;
20639 const char *include_name_to_compare = include_name;
20640 const char *pst_filename;
20641 int file_is_pst;
20642
20643 const char *dir_name = fe.include_dir (lh);
20644
20645 gdb::unique_xmalloc_ptr<char> hold_compare;
20646 if (!IS_ABSOLUTE_PATH (include_name)
20647 && (dir_name != NULL || comp_dir != NULL))
20648 {
20649 /* Avoid creating a duplicate psymtab for PST.
20650 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20651 Before we do the comparison, however, we need to account
20652 for DIR_NAME and COMP_DIR.
20653 First prepend dir_name (if non-NULL). If we still don't
20654 have an absolute path prepend comp_dir (if non-NULL).
20655 However, the directory we record in the include-file's
20656 psymtab does not contain COMP_DIR (to match the
20657 corresponding symtab(s)).
20658
20659 Example:
20660
20661 bash$ cd /tmp
20662 bash$ gcc -g ./hello.c
20663 include_name = "hello.c"
20664 dir_name = "."
20665 DW_AT_comp_dir = comp_dir = "/tmp"
20666 DW_AT_name = "./hello.c"
20667
20668 */
20669
20670 if (dir_name != NULL)
20671 {
20672 name_holder->reset (concat (dir_name, SLASH_STRING,
20673 include_name, (char *) NULL));
20674 include_name = name_holder->get ();
20675 include_name_to_compare = include_name;
20676 }
20677 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20678 {
20679 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20680 include_name, (char *) NULL));
20681 include_name_to_compare = hold_compare.get ();
20682 }
20683 }
20684
20685 pst_filename = pst->filename;
20686 gdb::unique_xmalloc_ptr<char> copied_name;
20687 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20688 {
20689 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20690 pst_filename, (char *) NULL));
20691 pst_filename = copied_name.get ();
20692 }
20693
20694 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20695
20696 if (file_is_pst)
20697 return NULL;
20698 return include_name;
20699 }
20700
20701 /* State machine to track the state of the line number program. */
20702
20703 class lnp_state_machine
20704 {
20705 public:
20706 /* Initialize a machine state for the start of a line number
20707 program. */
20708 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20709 bool record_lines_p);
20710
20711 file_entry *current_file ()
20712 {
20713 /* lh->file_names is 0-based, but the file name numbers in the
20714 statement program are 1-based. */
20715 return m_line_header->file_name_at (m_file);
20716 }
20717
20718 /* Record the line in the state machine. END_SEQUENCE is true if
20719 we're processing the end of a sequence. */
20720 void record_line (bool end_sequence);
20721
20722 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20723 nop-out rest of the lines in this sequence. */
20724 void check_line_address (struct dwarf2_cu *cu,
20725 const gdb_byte *line_ptr,
20726 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20727
20728 void handle_set_discriminator (unsigned int discriminator)
20729 {
20730 m_discriminator = discriminator;
20731 m_line_has_non_zero_discriminator |= discriminator != 0;
20732 }
20733
20734 /* Handle DW_LNE_set_address. */
20735 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20736 {
20737 m_op_index = 0;
20738 address += baseaddr;
20739 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20740 }
20741
20742 /* Handle DW_LNS_advance_pc. */
20743 void handle_advance_pc (CORE_ADDR adjust);
20744
20745 /* Handle a special opcode. */
20746 void handle_special_opcode (unsigned char op_code);
20747
20748 /* Handle DW_LNS_advance_line. */
20749 void handle_advance_line (int line_delta)
20750 {
20751 advance_line (line_delta);
20752 }
20753
20754 /* Handle DW_LNS_set_file. */
20755 void handle_set_file (file_name_index file);
20756
20757 /* Handle DW_LNS_negate_stmt. */
20758 void handle_negate_stmt ()
20759 {
20760 m_is_stmt = !m_is_stmt;
20761 }
20762
20763 /* Handle DW_LNS_const_add_pc. */
20764 void handle_const_add_pc ();
20765
20766 /* Handle DW_LNS_fixed_advance_pc. */
20767 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20768 {
20769 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20770 m_op_index = 0;
20771 }
20772
20773 /* Handle DW_LNS_copy. */
20774 void handle_copy ()
20775 {
20776 record_line (false);
20777 m_discriminator = 0;
20778 }
20779
20780 /* Handle DW_LNE_end_sequence. */
20781 void handle_end_sequence ()
20782 {
20783 m_currently_recording_lines = true;
20784 }
20785
20786 private:
20787 /* Advance the line by LINE_DELTA. */
20788 void advance_line (int line_delta)
20789 {
20790 m_line += line_delta;
20791
20792 if (line_delta != 0)
20793 m_line_has_non_zero_discriminator = m_discriminator != 0;
20794 }
20795
20796 struct dwarf2_cu *m_cu;
20797
20798 gdbarch *m_gdbarch;
20799
20800 /* True if we're recording lines.
20801 Otherwise we're building partial symtabs and are just interested in
20802 finding include files mentioned by the line number program. */
20803 bool m_record_lines_p;
20804
20805 /* The line number header. */
20806 line_header *m_line_header;
20807
20808 /* These are part of the standard DWARF line number state machine,
20809 and initialized according to the DWARF spec. */
20810
20811 unsigned char m_op_index = 0;
20812 /* The line table index (1-based) of the current file. */
20813 file_name_index m_file = (file_name_index) 1;
20814 unsigned int m_line = 1;
20815
20816 /* These are initialized in the constructor. */
20817
20818 CORE_ADDR m_address;
20819 bool m_is_stmt;
20820 unsigned int m_discriminator;
20821
20822 /* Additional bits of state we need to track. */
20823
20824 /* The last file that we called dwarf2_start_subfile for.
20825 This is only used for TLLs. */
20826 unsigned int m_last_file = 0;
20827 /* The last file a line number was recorded for. */
20828 struct subfile *m_last_subfile = NULL;
20829
20830 /* When true, record the lines we decode. */
20831 bool m_currently_recording_lines = false;
20832
20833 /* The last line number that was recorded, used to coalesce
20834 consecutive entries for the same line. This can happen, for
20835 example, when discriminators are present. PR 17276. */
20836 unsigned int m_last_line = 0;
20837 bool m_line_has_non_zero_discriminator = false;
20838 };
20839
20840 void
20841 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20842 {
20843 CORE_ADDR addr_adj = (((m_op_index + adjust)
20844 / m_line_header->maximum_ops_per_instruction)
20845 * m_line_header->minimum_instruction_length);
20846 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20847 m_op_index = ((m_op_index + adjust)
20848 % m_line_header->maximum_ops_per_instruction);
20849 }
20850
20851 void
20852 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20853 {
20854 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20855 CORE_ADDR addr_adj = (((m_op_index
20856 + (adj_opcode / m_line_header->line_range))
20857 / m_line_header->maximum_ops_per_instruction)
20858 * m_line_header->minimum_instruction_length);
20859 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20860 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20861 % m_line_header->maximum_ops_per_instruction);
20862
20863 int line_delta = (m_line_header->line_base
20864 + (adj_opcode % m_line_header->line_range));
20865 advance_line (line_delta);
20866 record_line (false);
20867 m_discriminator = 0;
20868 }
20869
20870 void
20871 lnp_state_machine::handle_set_file (file_name_index file)
20872 {
20873 m_file = file;
20874
20875 const file_entry *fe = current_file ();
20876 if (fe == NULL)
20877 dwarf2_debug_line_missing_file_complaint ();
20878 else if (m_record_lines_p)
20879 {
20880 const char *dir = fe->include_dir (m_line_header);
20881
20882 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20883 m_line_has_non_zero_discriminator = m_discriminator != 0;
20884 dwarf2_start_subfile (m_cu, fe->name, dir);
20885 }
20886 }
20887
20888 void
20889 lnp_state_machine::handle_const_add_pc ()
20890 {
20891 CORE_ADDR adjust
20892 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20893
20894 CORE_ADDR addr_adj
20895 = (((m_op_index + adjust)
20896 / m_line_header->maximum_ops_per_instruction)
20897 * m_line_header->minimum_instruction_length);
20898
20899 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20900 m_op_index = ((m_op_index + adjust)
20901 % m_line_header->maximum_ops_per_instruction);
20902 }
20903
20904 /* Return non-zero if we should add LINE to the line number table.
20905 LINE is the line to add, LAST_LINE is the last line that was added,
20906 LAST_SUBFILE is the subfile for LAST_LINE.
20907 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20908 had a non-zero discriminator.
20909
20910 We have to be careful in the presence of discriminators.
20911 E.g., for this line:
20912
20913 for (i = 0; i < 100000; i++);
20914
20915 clang can emit four line number entries for that one line,
20916 each with a different discriminator.
20917 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20918
20919 However, we want gdb to coalesce all four entries into one.
20920 Otherwise the user could stepi into the middle of the line and
20921 gdb would get confused about whether the pc really was in the
20922 middle of the line.
20923
20924 Things are further complicated by the fact that two consecutive
20925 line number entries for the same line is a heuristic used by gcc
20926 to denote the end of the prologue. So we can't just discard duplicate
20927 entries, we have to be selective about it. The heuristic we use is
20928 that we only collapse consecutive entries for the same line if at least
20929 one of those entries has a non-zero discriminator. PR 17276.
20930
20931 Note: Addresses in the line number state machine can never go backwards
20932 within one sequence, thus this coalescing is ok. */
20933
20934 static int
20935 dwarf_record_line_p (struct dwarf2_cu *cu,
20936 unsigned int line, unsigned int last_line,
20937 int line_has_non_zero_discriminator,
20938 struct subfile *last_subfile)
20939 {
20940 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20941 return 1;
20942 if (line != last_line)
20943 return 1;
20944 /* Same line for the same file that we've seen already.
20945 As a last check, for pr 17276, only record the line if the line
20946 has never had a non-zero discriminator. */
20947 if (!line_has_non_zero_discriminator)
20948 return 1;
20949 return 0;
20950 }
20951
20952 /* Use the CU's builder to record line number LINE beginning at
20953 address ADDRESS in the line table of subfile SUBFILE. */
20954
20955 static void
20956 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20957 unsigned int line, CORE_ADDR address,
20958 struct dwarf2_cu *cu)
20959 {
20960 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20961
20962 if (dwarf_line_debug)
20963 {
20964 fprintf_unfiltered (gdb_stdlog,
20965 "Recording line %u, file %s, address %s\n",
20966 line, lbasename (subfile->name),
20967 paddress (gdbarch, address));
20968 }
20969
20970 if (cu != nullptr)
20971 cu->get_builder ()->record_line (subfile, line, addr);
20972 }
20973
20974 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20975 Mark the end of a set of line number records.
20976 The arguments are the same as for dwarf_record_line_1.
20977 If SUBFILE is NULL the request is ignored. */
20978
20979 static void
20980 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20981 CORE_ADDR address, struct dwarf2_cu *cu)
20982 {
20983 if (subfile == NULL)
20984 return;
20985
20986 if (dwarf_line_debug)
20987 {
20988 fprintf_unfiltered (gdb_stdlog,
20989 "Finishing current line, file %s, address %s\n",
20990 lbasename (subfile->name),
20991 paddress (gdbarch, address));
20992 }
20993
20994 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
20995 }
20996
20997 void
20998 lnp_state_machine::record_line (bool end_sequence)
20999 {
21000 if (dwarf_line_debug)
21001 {
21002 fprintf_unfiltered (gdb_stdlog,
21003 "Processing actual line %u: file %u,"
21004 " address %s, is_stmt %u, discrim %u\n",
21005 m_line, to_underlying (m_file),
21006 paddress (m_gdbarch, m_address),
21007 m_is_stmt, m_discriminator);
21008 }
21009
21010 file_entry *fe = current_file ();
21011
21012 if (fe == NULL)
21013 dwarf2_debug_line_missing_file_complaint ();
21014 /* For now we ignore lines not starting on an instruction boundary.
21015 But not when processing end_sequence for compatibility with the
21016 previous version of the code. */
21017 else if (m_op_index == 0 || end_sequence)
21018 {
21019 fe->included_p = 1;
21020 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
21021 {
21022 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
21023 || end_sequence)
21024 {
21025 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21026 m_currently_recording_lines ? m_cu : nullptr);
21027 }
21028
21029 if (!end_sequence)
21030 {
21031 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21032 m_line_has_non_zero_discriminator,
21033 m_last_subfile))
21034 {
21035 buildsym_compunit *builder = m_cu->get_builder ();
21036 dwarf_record_line_1 (m_gdbarch,
21037 builder->get_current_subfile (),
21038 m_line, m_address,
21039 m_currently_recording_lines ? m_cu : nullptr);
21040 }
21041 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21042 m_last_line = m_line;
21043 }
21044 }
21045 }
21046 }
21047
21048 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21049 line_header *lh, bool record_lines_p)
21050 {
21051 m_cu = cu;
21052 m_gdbarch = arch;
21053 m_record_lines_p = record_lines_p;
21054 m_line_header = lh;
21055
21056 m_currently_recording_lines = true;
21057
21058 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21059 was a line entry for it so that the backend has a chance to adjust it
21060 and also record it in case it needs it. This is currently used by MIPS
21061 code, cf. `mips_adjust_dwarf2_line'. */
21062 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21063 m_is_stmt = lh->default_is_stmt;
21064 m_discriminator = 0;
21065 }
21066
21067 void
21068 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21069 const gdb_byte *line_ptr,
21070 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21071 {
21072 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21073 the pc range of the CU. However, we restrict the test to only ADDRESS
21074 values of zero to preserve GDB's previous behaviour which is to handle
21075 the specific case of a function being GC'd by the linker. */
21076
21077 if (address == 0 && address < unrelocated_lowpc)
21078 {
21079 /* This line table is for a function which has been
21080 GCd by the linker. Ignore it. PR gdb/12528 */
21081
21082 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21083 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21084
21085 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21086 line_offset, objfile_name (objfile));
21087 m_currently_recording_lines = false;
21088 /* Note: m_currently_recording_lines is left as false until we see
21089 DW_LNE_end_sequence. */
21090 }
21091 }
21092
21093 /* Subroutine of dwarf_decode_lines to simplify it.
21094 Process the line number information in LH.
21095 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21096 program in order to set included_p for every referenced header. */
21097
21098 static void
21099 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21100 const int decode_for_pst_p, CORE_ADDR lowpc)
21101 {
21102 const gdb_byte *line_ptr, *extended_end;
21103 const gdb_byte *line_end;
21104 unsigned int bytes_read, extended_len;
21105 unsigned char op_code, extended_op;
21106 CORE_ADDR baseaddr;
21107 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21108 bfd *abfd = objfile->obfd;
21109 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21110 /* True if we're recording line info (as opposed to building partial
21111 symtabs and just interested in finding include files mentioned by
21112 the line number program). */
21113 bool record_lines_p = !decode_for_pst_p;
21114
21115 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21116
21117 line_ptr = lh->statement_program_start;
21118 line_end = lh->statement_program_end;
21119
21120 /* Read the statement sequences until there's nothing left. */
21121 while (line_ptr < line_end)
21122 {
21123 /* The DWARF line number program state machine. Reset the state
21124 machine at the start of each sequence. */
21125 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21126 bool end_sequence = false;
21127
21128 if (record_lines_p)
21129 {
21130 /* Start a subfile for the current file of the state
21131 machine. */
21132 const file_entry *fe = state_machine.current_file ();
21133
21134 if (fe != NULL)
21135 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21136 }
21137
21138 /* Decode the table. */
21139 while (line_ptr < line_end && !end_sequence)
21140 {
21141 op_code = read_1_byte (abfd, line_ptr);
21142 line_ptr += 1;
21143
21144 if (op_code >= lh->opcode_base)
21145 {
21146 /* Special opcode. */
21147 state_machine.handle_special_opcode (op_code);
21148 }
21149 else switch (op_code)
21150 {
21151 case DW_LNS_extended_op:
21152 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21153 &bytes_read);
21154 line_ptr += bytes_read;
21155 extended_end = line_ptr + extended_len;
21156 extended_op = read_1_byte (abfd, line_ptr);
21157 line_ptr += 1;
21158 switch (extended_op)
21159 {
21160 case DW_LNE_end_sequence:
21161 state_machine.handle_end_sequence ();
21162 end_sequence = true;
21163 break;
21164 case DW_LNE_set_address:
21165 {
21166 CORE_ADDR address
21167 = read_address (abfd, line_ptr, cu, &bytes_read);
21168 line_ptr += bytes_read;
21169
21170 state_machine.check_line_address (cu, line_ptr,
21171 lowpc - baseaddr, address);
21172 state_machine.handle_set_address (baseaddr, address);
21173 }
21174 break;
21175 case DW_LNE_define_file:
21176 {
21177 const char *cur_file;
21178 unsigned int mod_time, length;
21179 dir_index dindex;
21180
21181 cur_file = read_direct_string (abfd, line_ptr,
21182 &bytes_read);
21183 line_ptr += bytes_read;
21184 dindex = (dir_index)
21185 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21186 line_ptr += bytes_read;
21187 mod_time =
21188 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21189 line_ptr += bytes_read;
21190 length =
21191 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21192 line_ptr += bytes_read;
21193 lh->add_file_name (cur_file, dindex, mod_time, length);
21194 }
21195 break;
21196 case DW_LNE_set_discriminator:
21197 {
21198 /* The discriminator is not interesting to the
21199 debugger; just ignore it. We still need to
21200 check its value though:
21201 if there are consecutive entries for the same
21202 (non-prologue) line we want to coalesce them.
21203 PR 17276. */
21204 unsigned int discr
21205 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21206 line_ptr += bytes_read;
21207
21208 state_machine.handle_set_discriminator (discr);
21209 }
21210 break;
21211 default:
21212 complaint (_("mangled .debug_line section"));
21213 return;
21214 }
21215 /* Make sure that we parsed the extended op correctly. If e.g.
21216 we expected a different address size than the producer used,
21217 we may have read the wrong number of bytes. */
21218 if (line_ptr != extended_end)
21219 {
21220 complaint (_("mangled .debug_line section"));
21221 return;
21222 }
21223 break;
21224 case DW_LNS_copy:
21225 state_machine.handle_copy ();
21226 break;
21227 case DW_LNS_advance_pc:
21228 {
21229 CORE_ADDR adjust
21230 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21231 line_ptr += bytes_read;
21232
21233 state_machine.handle_advance_pc (adjust);
21234 }
21235 break;
21236 case DW_LNS_advance_line:
21237 {
21238 int line_delta
21239 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21240 line_ptr += bytes_read;
21241
21242 state_machine.handle_advance_line (line_delta);
21243 }
21244 break;
21245 case DW_LNS_set_file:
21246 {
21247 file_name_index file
21248 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21249 &bytes_read);
21250 line_ptr += bytes_read;
21251
21252 state_machine.handle_set_file (file);
21253 }
21254 break;
21255 case DW_LNS_set_column:
21256 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21257 line_ptr += bytes_read;
21258 break;
21259 case DW_LNS_negate_stmt:
21260 state_machine.handle_negate_stmt ();
21261 break;
21262 case DW_LNS_set_basic_block:
21263 break;
21264 /* Add to the address register of the state machine the
21265 address increment value corresponding to special opcode
21266 255. I.e., this value is scaled by the minimum
21267 instruction length since special opcode 255 would have
21268 scaled the increment. */
21269 case DW_LNS_const_add_pc:
21270 state_machine.handle_const_add_pc ();
21271 break;
21272 case DW_LNS_fixed_advance_pc:
21273 {
21274 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21275 line_ptr += 2;
21276
21277 state_machine.handle_fixed_advance_pc (addr_adj);
21278 }
21279 break;
21280 default:
21281 {
21282 /* Unknown standard opcode, ignore it. */
21283 int i;
21284
21285 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21286 {
21287 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21288 line_ptr += bytes_read;
21289 }
21290 }
21291 }
21292 }
21293
21294 if (!end_sequence)
21295 dwarf2_debug_line_missing_end_sequence_complaint ();
21296
21297 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21298 in which case we still finish recording the last line). */
21299 state_machine.record_line (true);
21300 }
21301 }
21302
21303 /* Decode the Line Number Program (LNP) for the given line_header
21304 structure and CU. The actual information extracted and the type
21305 of structures created from the LNP depends on the value of PST.
21306
21307 1. If PST is NULL, then this procedure uses the data from the program
21308 to create all necessary symbol tables, and their linetables.
21309
21310 2. If PST is not NULL, this procedure reads the program to determine
21311 the list of files included by the unit represented by PST, and
21312 builds all the associated partial symbol tables.
21313
21314 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21315 It is used for relative paths in the line table.
21316 NOTE: When processing partial symtabs (pst != NULL),
21317 comp_dir == pst->dirname.
21318
21319 NOTE: It is important that psymtabs have the same file name (via strcmp)
21320 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21321 symtab we don't use it in the name of the psymtabs we create.
21322 E.g. expand_line_sal requires this when finding psymtabs to expand.
21323 A good testcase for this is mb-inline.exp.
21324
21325 LOWPC is the lowest address in CU (or 0 if not known).
21326
21327 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21328 for its PC<->lines mapping information. Otherwise only the filename
21329 table is read in. */
21330
21331 static void
21332 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21333 struct dwarf2_cu *cu, struct partial_symtab *pst,
21334 CORE_ADDR lowpc, int decode_mapping)
21335 {
21336 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21337 const int decode_for_pst_p = (pst != NULL);
21338
21339 if (decode_mapping)
21340 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21341
21342 if (decode_for_pst_p)
21343 {
21344 int file_index;
21345
21346 /* Now that we're done scanning the Line Header Program, we can
21347 create the psymtab of each included file. */
21348 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21349 if (lh->file_names[file_index].included_p == 1)
21350 {
21351 gdb::unique_xmalloc_ptr<char> name_holder;
21352 const char *include_name =
21353 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21354 &name_holder);
21355 if (include_name != NULL)
21356 dwarf2_create_include_psymtab (include_name, pst, objfile);
21357 }
21358 }
21359 else
21360 {
21361 /* Make sure a symtab is created for every file, even files
21362 which contain only variables (i.e. no code with associated
21363 line numbers). */
21364 buildsym_compunit *builder = cu->get_builder ();
21365 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21366 int i;
21367
21368 for (i = 0; i < lh->file_names.size (); i++)
21369 {
21370 file_entry &fe = lh->file_names[i];
21371
21372 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21373
21374 if (builder->get_current_subfile ()->symtab == NULL)
21375 {
21376 builder->get_current_subfile ()->symtab
21377 = allocate_symtab (cust,
21378 builder->get_current_subfile ()->name);
21379 }
21380 fe.symtab = builder->get_current_subfile ()->symtab;
21381 }
21382 }
21383 }
21384
21385 /* Start a subfile for DWARF. FILENAME is the name of the file and
21386 DIRNAME the name of the source directory which contains FILENAME
21387 or NULL if not known.
21388 This routine tries to keep line numbers from identical absolute and
21389 relative file names in a common subfile.
21390
21391 Using the `list' example from the GDB testsuite, which resides in
21392 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21393 of /srcdir/list0.c yields the following debugging information for list0.c:
21394
21395 DW_AT_name: /srcdir/list0.c
21396 DW_AT_comp_dir: /compdir
21397 files.files[0].name: list0.h
21398 files.files[0].dir: /srcdir
21399 files.files[1].name: list0.c
21400 files.files[1].dir: /srcdir
21401
21402 The line number information for list0.c has to end up in a single
21403 subfile, so that `break /srcdir/list0.c:1' works as expected.
21404 start_subfile will ensure that this happens provided that we pass the
21405 concatenation of files.files[1].dir and files.files[1].name as the
21406 subfile's name. */
21407
21408 static void
21409 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21410 const char *dirname)
21411 {
21412 char *copy = NULL;
21413
21414 /* In order not to lose the line information directory,
21415 we concatenate it to the filename when it makes sense.
21416 Note that the Dwarf3 standard says (speaking of filenames in line
21417 information): ``The directory index is ignored for file names
21418 that represent full path names''. Thus ignoring dirname in the
21419 `else' branch below isn't an issue. */
21420
21421 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21422 {
21423 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21424 filename = copy;
21425 }
21426
21427 cu->get_builder ()->start_subfile (filename);
21428
21429 if (copy != NULL)
21430 xfree (copy);
21431 }
21432
21433 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21434 buildsym_compunit constructor. */
21435
21436 struct compunit_symtab *
21437 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21438 CORE_ADDR low_pc)
21439 {
21440 gdb_assert (m_builder == nullptr);
21441
21442 m_builder.reset (new struct buildsym_compunit
21443 (per_cu->dwarf2_per_objfile->objfile,
21444 name, comp_dir, language, low_pc));
21445
21446 list_in_scope = get_builder ()->get_file_symbols ();
21447
21448 get_builder ()->record_debugformat ("DWARF 2");
21449 get_builder ()->record_producer (producer);
21450
21451 processing_has_namespace_info = false;
21452
21453 return get_builder ()->get_compunit_symtab ();
21454 }
21455
21456 static void
21457 var_decode_location (struct attribute *attr, struct symbol *sym,
21458 struct dwarf2_cu *cu)
21459 {
21460 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21461 struct comp_unit_head *cu_header = &cu->header;
21462
21463 /* NOTE drow/2003-01-30: There used to be a comment and some special
21464 code here to turn a symbol with DW_AT_external and a
21465 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21466 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21467 with some versions of binutils) where shared libraries could have
21468 relocations against symbols in their debug information - the
21469 minimal symbol would have the right address, but the debug info
21470 would not. It's no longer necessary, because we will explicitly
21471 apply relocations when we read in the debug information now. */
21472
21473 /* A DW_AT_location attribute with no contents indicates that a
21474 variable has been optimized away. */
21475 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21476 {
21477 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21478 return;
21479 }
21480
21481 /* Handle one degenerate form of location expression specially, to
21482 preserve GDB's previous behavior when section offsets are
21483 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21484 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21485
21486 if (attr_form_is_block (attr)
21487 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21488 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21489 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21490 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21491 && (DW_BLOCK (attr)->size
21492 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21493 {
21494 unsigned int dummy;
21495
21496 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21497 SYMBOL_VALUE_ADDRESS (sym) =
21498 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21499 else
21500 SYMBOL_VALUE_ADDRESS (sym) =
21501 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21502 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21503 fixup_symbol_section (sym, objfile);
21504 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21505 SYMBOL_SECTION (sym));
21506 return;
21507 }
21508
21509 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21510 expression evaluator, and use LOC_COMPUTED only when necessary
21511 (i.e. when the value of a register or memory location is
21512 referenced, or a thread-local block, etc.). Then again, it might
21513 not be worthwhile. I'm assuming that it isn't unless performance
21514 or memory numbers show me otherwise. */
21515
21516 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21517
21518 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21519 cu->has_loclist = true;
21520 }
21521
21522 /* Given a pointer to a DWARF information entry, figure out if we need
21523 to make a symbol table entry for it, and if so, create a new entry
21524 and return a pointer to it.
21525 If TYPE is NULL, determine symbol type from the die, otherwise
21526 used the passed type.
21527 If SPACE is not NULL, use it to hold the new symbol. If it is
21528 NULL, allocate a new symbol on the objfile's obstack. */
21529
21530 static struct symbol *
21531 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21532 struct symbol *space)
21533 {
21534 struct dwarf2_per_objfile *dwarf2_per_objfile
21535 = cu->per_cu->dwarf2_per_objfile;
21536 struct objfile *objfile = dwarf2_per_objfile->objfile;
21537 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21538 struct symbol *sym = NULL;
21539 const char *name;
21540 struct attribute *attr = NULL;
21541 struct attribute *attr2 = NULL;
21542 CORE_ADDR baseaddr;
21543 struct pending **list_to_add = NULL;
21544
21545 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21546
21547 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21548
21549 name = dwarf2_name (die, cu);
21550 if (name)
21551 {
21552 const char *linkagename;
21553 int suppress_add = 0;
21554
21555 if (space)
21556 sym = space;
21557 else
21558 sym = allocate_symbol (objfile);
21559 OBJSTAT (objfile, n_syms++);
21560
21561 /* Cache this symbol's name and the name's demangled form (if any). */
21562 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21563 linkagename = dwarf2_physname (name, die, cu);
21564 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21565
21566 /* Fortran does not have mangling standard and the mangling does differ
21567 between gfortran, iFort etc. */
21568 if (cu->language == language_fortran
21569 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21570 symbol_set_demangled_name (&(sym->ginfo),
21571 dwarf2_full_name (name, die, cu),
21572 NULL);
21573
21574 /* Default assumptions.
21575 Use the passed type or decode it from the die. */
21576 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21577 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21578 if (type != NULL)
21579 SYMBOL_TYPE (sym) = type;
21580 else
21581 SYMBOL_TYPE (sym) = die_type (die, cu);
21582 attr = dwarf2_attr (die,
21583 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21584 cu);
21585 if (attr)
21586 {
21587 SYMBOL_LINE (sym) = DW_UNSND (attr);
21588 }
21589
21590 attr = dwarf2_attr (die,
21591 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21592 cu);
21593 if (attr)
21594 {
21595 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21596 struct file_entry *fe;
21597
21598 if (cu->line_header != NULL)
21599 fe = cu->line_header->file_name_at (file_index);
21600 else
21601 fe = NULL;
21602
21603 if (fe == NULL)
21604 complaint (_("file index out of range"));
21605 else
21606 symbol_set_symtab (sym, fe->symtab);
21607 }
21608
21609 switch (die->tag)
21610 {
21611 case DW_TAG_label:
21612 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21613 if (attr)
21614 {
21615 CORE_ADDR addr;
21616
21617 addr = attr_value_as_address (attr);
21618 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21619 SYMBOL_VALUE_ADDRESS (sym) = addr;
21620 }
21621 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21622 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21623 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21624 add_symbol_to_list (sym, cu->list_in_scope);
21625 break;
21626 case DW_TAG_subprogram:
21627 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21628 finish_block. */
21629 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21630 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21631 if ((attr2 && (DW_UNSND (attr2) != 0))
21632 || cu->language == language_ada)
21633 {
21634 /* Subprograms marked external are stored as a global symbol.
21635 Ada subprograms, whether marked external or not, are always
21636 stored as a global symbol, because we want to be able to
21637 access them globally. For instance, we want to be able
21638 to break on a nested subprogram without having to
21639 specify the context. */
21640 list_to_add = cu->get_builder ()->get_global_symbols ();
21641 }
21642 else
21643 {
21644 list_to_add = cu->list_in_scope;
21645 }
21646 break;
21647 case DW_TAG_inlined_subroutine:
21648 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21649 finish_block. */
21650 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21651 SYMBOL_INLINED (sym) = 1;
21652 list_to_add = cu->list_in_scope;
21653 break;
21654 case DW_TAG_template_value_param:
21655 suppress_add = 1;
21656 /* Fall through. */
21657 case DW_TAG_constant:
21658 case DW_TAG_variable:
21659 case DW_TAG_member:
21660 /* Compilation with minimal debug info may result in
21661 variables with missing type entries. Change the
21662 misleading `void' type to something sensible. */
21663 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21664 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21665
21666 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21667 /* In the case of DW_TAG_member, we should only be called for
21668 static const members. */
21669 if (die->tag == DW_TAG_member)
21670 {
21671 /* dwarf2_add_field uses die_is_declaration,
21672 so we do the same. */
21673 gdb_assert (die_is_declaration (die, cu));
21674 gdb_assert (attr);
21675 }
21676 if (attr)
21677 {
21678 dwarf2_const_value (attr, sym, cu);
21679 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21680 if (!suppress_add)
21681 {
21682 if (attr2 && (DW_UNSND (attr2) != 0))
21683 list_to_add = cu->get_builder ()->get_global_symbols ();
21684 else
21685 list_to_add = cu->list_in_scope;
21686 }
21687 break;
21688 }
21689 attr = dwarf2_attr (die, DW_AT_location, cu);
21690 if (attr)
21691 {
21692 var_decode_location (attr, sym, cu);
21693 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21694
21695 /* Fortran explicitly imports any global symbols to the local
21696 scope by DW_TAG_common_block. */
21697 if (cu->language == language_fortran && die->parent
21698 && die->parent->tag == DW_TAG_common_block)
21699 attr2 = NULL;
21700
21701 if (SYMBOL_CLASS (sym) == LOC_STATIC
21702 && SYMBOL_VALUE_ADDRESS (sym) == 0
21703 && !dwarf2_per_objfile->has_section_at_zero)
21704 {
21705 /* When a static variable is eliminated by the linker,
21706 the corresponding debug information is not stripped
21707 out, but the variable address is set to null;
21708 do not add such variables into symbol table. */
21709 }
21710 else if (attr2 && (DW_UNSND (attr2) != 0))
21711 {
21712 /* Workaround gfortran PR debug/40040 - it uses
21713 DW_AT_location for variables in -fPIC libraries which may
21714 get overriden by other libraries/executable and get
21715 a different address. Resolve it by the minimal symbol
21716 which may come from inferior's executable using copy
21717 relocation. Make this workaround only for gfortran as for
21718 other compilers GDB cannot guess the minimal symbol
21719 Fortran mangling kind. */
21720 if (cu->language == language_fortran && die->parent
21721 && die->parent->tag == DW_TAG_module
21722 && cu->producer
21723 && startswith (cu->producer, "GNU Fortran"))
21724 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21725
21726 /* A variable with DW_AT_external is never static,
21727 but it may be block-scoped. */
21728 list_to_add
21729 = ((cu->list_in_scope
21730 == cu->get_builder ()->get_file_symbols ())
21731 ? cu->get_builder ()->get_global_symbols ()
21732 : cu->list_in_scope);
21733 }
21734 else
21735 list_to_add = cu->list_in_scope;
21736 }
21737 else
21738 {
21739 /* We do not know the address of this symbol.
21740 If it is an external symbol and we have type information
21741 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21742 The address of the variable will then be determined from
21743 the minimal symbol table whenever the variable is
21744 referenced. */
21745 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21746
21747 /* Fortran explicitly imports any global symbols to the local
21748 scope by DW_TAG_common_block. */
21749 if (cu->language == language_fortran && die->parent
21750 && die->parent->tag == DW_TAG_common_block)
21751 {
21752 /* SYMBOL_CLASS doesn't matter here because
21753 read_common_block is going to reset it. */
21754 if (!suppress_add)
21755 list_to_add = cu->list_in_scope;
21756 }
21757 else if (attr2 && (DW_UNSND (attr2) != 0)
21758 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21759 {
21760 /* A variable with DW_AT_external is never static, but it
21761 may be block-scoped. */
21762 list_to_add
21763 = ((cu->list_in_scope
21764 == cu->get_builder ()->get_file_symbols ())
21765 ? cu->get_builder ()->get_global_symbols ()
21766 : cu->list_in_scope);
21767
21768 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21769 }
21770 else if (!die_is_declaration (die, cu))
21771 {
21772 /* Use the default LOC_OPTIMIZED_OUT class. */
21773 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21774 if (!suppress_add)
21775 list_to_add = cu->list_in_scope;
21776 }
21777 }
21778 break;
21779 case DW_TAG_formal_parameter:
21780 {
21781 /* If we are inside a function, mark this as an argument. If
21782 not, we might be looking at an argument to an inlined function
21783 when we do not have enough information to show inlined frames;
21784 pretend it's a local variable in that case so that the user can
21785 still see it. */
21786 struct context_stack *curr
21787 = cu->get_builder ()->get_current_context_stack ();
21788 if (curr != nullptr && curr->name != nullptr)
21789 SYMBOL_IS_ARGUMENT (sym) = 1;
21790 attr = dwarf2_attr (die, DW_AT_location, cu);
21791 if (attr)
21792 {
21793 var_decode_location (attr, sym, cu);
21794 }
21795 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21796 if (attr)
21797 {
21798 dwarf2_const_value (attr, sym, cu);
21799 }
21800
21801 list_to_add = cu->list_in_scope;
21802 }
21803 break;
21804 case DW_TAG_unspecified_parameters:
21805 /* From varargs functions; gdb doesn't seem to have any
21806 interest in this information, so just ignore it for now.
21807 (FIXME?) */
21808 break;
21809 case DW_TAG_template_type_param:
21810 suppress_add = 1;
21811 /* Fall through. */
21812 case DW_TAG_class_type:
21813 case DW_TAG_interface_type:
21814 case DW_TAG_structure_type:
21815 case DW_TAG_union_type:
21816 case DW_TAG_set_type:
21817 case DW_TAG_enumeration_type:
21818 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21819 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21820
21821 {
21822 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21823 really ever be static objects: otherwise, if you try
21824 to, say, break of a class's method and you're in a file
21825 which doesn't mention that class, it won't work unless
21826 the check for all static symbols in lookup_symbol_aux
21827 saves you. See the OtherFileClass tests in
21828 gdb.c++/namespace.exp. */
21829
21830 if (!suppress_add)
21831 {
21832 buildsym_compunit *builder = cu->get_builder ();
21833 list_to_add
21834 = (cu->list_in_scope == builder->get_file_symbols ()
21835 && cu->language == language_cplus
21836 ? builder->get_global_symbols ()
21837 : cu->list_in_scope);
21838
21839 /* The semantics of C++ state that "struct foo {
21840 ... }" also defines a typedef for "foo". */
21841 if (cu->language == language_cplus
21842 || cu->language == language_ada
21843 || cu->language == language_d
21844 || cu->language == language_rust)
21845 {
21846 /* The symbol's name is already allocated along
21847 with this objfile, so we don't need to
21848 duplicate it for the type. */
21849 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21850 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21851 }
21852 }
21853 }
21854 break;
21855 case DW_TAG_typedef:
21856 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21857 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21858 list_to_add = cu->list_in_scope;
21859 break;
21860 case DW_TAG_base_type:
21861 case DW_TAG_subrange_type:
21862 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21863 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21864 list_to_add = cu->list_in_scope;
21865 break;
21866 case DW_TAG_enumerator:
21867 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21868 if (attr)
21869 {
21870 dwarf2_const_value (attr, sym, cu);
21871 }
21872 {
21873 /* NOTE: carlton/2003-11-10: See comment above in the
21874 DW_TAG_class_type, etc. block. */
21875
21876 list_to_add
21877 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21878 && cu->language == language_cplus
21879 ? cu->get_builder ()->get_global_symbols ()
21880 : cu->list_in_scope);
21881 }
21882 break;
21883 case DW_TAG_imported_declaration:
21884 case DW_TAG_namespace:
21885 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21886 list_to_add = cu->get_builder ()->get_global_symbols ();
21887 break;
21888 case DW_TAG_module:
21889 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21890 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21891 list_to_add = cu->get_builder ()->get_global_symbols ();
21892 break;
21893 case DW_TAG_common_block:
21894 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21895 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21896 add_symbol_to_list (sym, cu->list_in_scope);
21897 break;
21898 default:
21899 /* Not a tag we recognize. Hopefully we aren't processing
21900 trash data, but since we must specifically ignore things
21901 we don't recognize, there is nothing else we should do at
21902 this point. */
21903 complaint (_("unsupported tag: '%s'"),
21904 dwarf_tag_name (die->tag));
21905 break;
21906 }
21907
21908 if (suppress_add)
21909 {
21910 sym->hash_next = objfile->template_symbols;
21911 objfile->template_symbols = sym;
21912 list_to_add = NULL;
21913 }
21914
21915 if (list_to_add != NULL)
21916 add_symbol_to_list (sym, list_to_add);
21917
21918 /* For the benefit of old versions of GCC, check for anonymous
21919 namespaces based on the demangled name. */
21920 if (!cu->processing_has_namespace_info
21921 && cu->language == language_cplus)
21922 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21923 }
21924 return (sym);
21925 }
21926
21927 /* Given an attr with a DW_FORM_dataN value in host byte order,
21928 zero-extend it as appropriate for the symbol's type. The DWARF
21929 standard (v4) is not entirely clear about the meaning of using
21930 DW_FORM_dataN for a constant with a signed type, where the type is
21931 wider than the data. The conclusion of a discussion on the DWARF
21932 list was that this is unspecified. We choose to always zero-extend
21933 because that is the interpretation long in use by GCC. */
21934
21935 static gdb_byte *
21936 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21937 struct dwarf2_cu *cu, LONGEST *value, int bits)
21938 {
21939 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21940 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21941 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21942 LONGEST l = DW_UNSND (attr);
21943
21944 if (bits < sizeof (*value) * 8)
21945 {
21946 l &= ((LONGEST) 1 << bits) - 1;
21947 *value = l;
21948 }
21949 else if (bits == sizeof (*value) * 8)
21950 *value = l;
21951 else
21952 {
21953 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21954 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21955 return bytes;
21956 }
21957
21958 return NULL;
21959 }
21960
21961 /* Read a constant value from an attribute. Either set *VALUE, or if
21962 the value does not fit in *VALUE, set *BYTES - either already
21963 allocated on the objfile obstack, or newly allocated on OBSTACK,
21964 or, set *BATON, if we translated the constant to a location
21965 expression. */
21966
21967 static void
21968 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21969 const char *name, struct obstack *obstack,
21970 struct dwarf2_cu *cu,
21971 LONGEST *value, const gdb_byte **bytes,
21972 struct dwarf2_locexpr_baton **baton)
21973 {
21974 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21975 struct comp_unit_head *cu_header = &cu->header;
21976 struct dwarf_block *blk;
21977 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21978 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21979
21980 *value = 0;
21981 *bytes = NULL;
21982 *baton = NULL;
21983
21984 switch (attr->form)
21985 {
21986 case DW_FORM_addr:
21987 case DW_FORM_addrx:
21988 case DW_FORM_GNU_addr_index:
21989 {
21990 gdb_byte *data;
21991
21992 if (TYPE_LENGTH (type) != cu_header->addr_size)
21993 dwarf2_const_value_length_mismatch_complaint (name,
21994 cu_header->addr_size,
21995 TYPE_LENGTH (type));
21996 /* Symbols of this form are reasonably rare, so we just
21997 piggyback on the existing location code rather than writing
21998 a new implementation of symbol_computed_ops. */
21999 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22000 (*baton)->per_cu = cu->per_cu;
22001 gdb_assert ((*baton)->per_cu);
22002
22003 (*baton)->size = 2 + cu_header->addr_size;
22004 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22005 (*baton)->data = data;
22006
22007 data[0] = DW_OP_addr;
22008 store_unsigned_integer (&data[1], cu_header->addr_size,
22009 byte_order, DW_ADDR (attr));
22010 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22011 }
22012 break;
22013 case DW_FORM_string:
22014 case DW_FORM_strp:
22015 case DW_FORM_strx:
22016 case DW_FORM_GNU_str_index:
22017 case DW_FORM_GNU_strp_alt:
22018 /* DW_STRING is already allocated on the objfile obstack, point
22019 directly to it. */
22020 *bytes = (const gdb_byte *) DW_STRING (attr);
22021 break;
22022 case DW_FORM_block1:
22023 case DW_FORM_block2:
22024 case DW_FORM_block4:
22025 case DW_FORM_block:
22026 case DW_FORM_exprloc:
22027 case DW_FORM_data16:
22028 blk = DW_BLOCK (attr);
22029 if (TYPE_LENGTH (type) != blk->size)
22030 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22031 TYPE_LENGTH (type));
22032 *bytes = blk->data;
22033 break;
22034
22035 /* The DW_AT_const_value attributes are supposed to carry the
22036 symbol's value "represented as it would be on the target
22037 architecture." By the time we get here, it's already been
22038 converted to host endianness, so we just need to sign- or
22039 zero-extend it as appropriate. */
22040 case DW_FORM_data1:
22041 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22042 break;
22043 case DW_FORM_data2:
22044 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22045 break;
22046 case DW_FORM_data4:
22047 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22048 break;
22049 case DW_FORM_data8:
22050 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22051 break;
22052
22053 case DW_FORM_sdata:
22054 case DW_FORM_implicit_const:
22055 *value = DW_SND (attr);
22056 break;
22057
22058 case DW_FORM_udata:
22059 *value = DW_UNSND (attr);
22060 break;
22061
22062 default:
22063 complaint (_("unsupported const value attribute form: '%s'"),
22064 dwarf_form_name (attr->form));
22065 *value = 0;
22066 break;
22067 }
22068 }
22069
22070
22071 /* Copy constant value from an attribute to a symbol. */
22072
22073 static void
22074 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22075 struct dwarf2_cu *cu)
22076 {
22077 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22078 LONGEST value;
22079 const gdb_byte *bytes;
22080 struct dwarf2_locexpr_baton *baton;
22081
22082 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22083 SYMBOL_PRINT_NAME (sym),
22084 &objfile->objfile_obstack, cu,
22085 &value, &bytes, &baton);
22086
22087 if (baton != NULL)
22088 {
22089 SYMBOL_LOCATION_BATON (sym) = baton;
22090 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22091 }
22092 else if (bytes != NULL)
22093 {
22094 SYMBOL_VALUE_BYTES (sym) = bytes;
22095 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22096 }
22097 else
22098 {
22099 SYMBOL_VALUE (sym) = value;
22100 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22101 }
22102 }
22103
22104 /* Return the type of the die in question using its DW_AT_type attribute. */
22105
22106 static struct type *
22107 die_type (struct die_info *die, struct dwarf2_cu *cu)
22108 {
22109 struct attribute *type_attr;
22110
22111 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22112 if (!type_attr)
22113 {
22114 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22115 /* A missing DW_AT_type represents a void type. */
22116 return objfile_type (objfile)->builtin_void;
22117 }
22118
22119 return lookup_die_type (die, type_attr, cu);
22120 }
22121
22122 /* True iff CU's producer generates GNAT Ada auxiliary information
22123 that allows to find parallel types through that information instead
22124 of having to do expensive parallel lookups by type name. */
22125
22126 static int
22127 need_gnat_info (struct dwarf2_cu *cu)
22128 {
22129 /* Assume that the Ada compiler was GNAT, which always produces
22130 the auxiliary information. */
22131 return (cu->language == language_ada);
22132 }
22133
22134 /* Return the auxiliary type of the die in question using its
22135 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22136 attribute is not present. */
22137
22138 static struct type *
22139 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22140 {
22141 struct attribute *type_attr;
22142
22143 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22144 if (!type_attr)
22145 return NULL;
22146
22147 return lookup_die_type (die, type_attr, cu);
22148 }
22149
22150 /* If DIE has a descriptive_type attribute, then set the TYPE's
22151 descriptive type accordingly. */
22152
22153 static void
22154 set_descriptive_type (struct type *type, struct die_info *die,
22155 struct dwarf2_cu *cu)
22156 {
22157 struct type *descriptive_type = die_descriptive_type (die, cu);
22158
22159 if (descriptive_type)
22160 {
22161 ALLOCATE_GNAT_AUX_TYPE (type);
22162 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22163 }
22164 }
22165
22166 /* Return the containing type of the die in question using its
22167 DW_AT_containing_type attribute. */
22168
22169 static struct type *
22170 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22171 {
22172 struct attribute *type_attr;
22173 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22174
22175 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22176 if (!type_attr)
22177 error (_("Dwarf Error: Problem turning containing type into gdb type "
22178 "[in module %s]"), objfile_name (objfile));
22179
22180 return lookup_die_type (die, type_attr, cu);
22181 }
22182
22183 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22184
22185 static struct type *
22186 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22187 {
22188 struct dwarf2_per_objfile *dwarf2_per_objfile
22189 = cu->per_cu->dwarf2_per_objfile;
22190 struct objfile *objfile = dwarf2_per_objfile->objfile;
22191 char *saved;
22192
22193 std::string message
22194 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22195 objfile_name (objfile),
22196 sect_offset_str (cu->header.sect_off),
22197 sect_offset_str (die->sect_off));
22198 saved = obstack_strdup (&objfile->objfile_obstack, message);
22199
22200 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22201 }
22202
22203 /* Look up the type of DIE in CU using its type attribute ATTR.
22204 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22205 DW_AT_containing_type.
22206 If there is no type substitute an error marker. */
22207
22208 static struct type *
22209 lookup_die_type (struct die_info *die, const struct attribute *attr,
22210 struct dwarf2_cu *cu)
22211 {
22212 struct dwarf2_per_objfile *dwarf2_per_objfile
22213 = cu->per_cu->dwarf2_per_objfile;
22214 struct objfile *objfile = dwarf2_per_objfile->objfile;
22215 struct type *this_type;
22216
22217 gdb_assert (attr->name == DW_AT_type
22218 || attr->name == DW_AT_GNAT_descriptive_type
22219 || attr->name == DW_AT_containing_type);
22220
22221 /* First see if we have it cached. */
22222
22223 if (attr->form == DW_FORM_GNU_ref_alt)
22224 {
22225 struct dwarf2_per_cu_data *per_cu;
22226 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22227
22228 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22229 dwarf2_per_objfile);
22230 this_type = get_die_type_at_offset (sect_off, per_cu);
22231 }
22232 else if (attr_form_is_ref (attr))
22233 {
22234 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22235
22236 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22237 }
22238 else if (attr->form == DW_FORM_ref_sig8)
22239 {
22240 ULONGEST signature = DW_SIGNATURE (attr);
22241
22242 return get_signatured_type (die, signature, cu);
22243 }
22244 else
22245 {
22246 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22247 " at %s [in module %s]"),
22248 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22249 objfile_name (objfile));
22250 return build_error_marker_type (cu, die);
22251 }
22252
22253 /* If not cached we need to read it in. */
22254
22255 if (this_type == NULL)
22256 {
22257 struct die_info *type_die = NULL;
22258 struct dwarf2_cu *type_cu = cu;
22259
22260 if (attr_form_is_ref (attr))
22261 type_die = follow_die_ref (die, attr, &type_cu);
22262 if (type_die == NULL)
22263 return build_error_marker_type (cu, die);
22264 /* If we find the type now, it's probably because the type came
22265 from an inter-CU reference and the type's CU got expanded before
22266 ours. */
22267 this_type = read_type_die (type_die, type_cu);
22268 }
22269
22270 /* If we still don't have a type use an error marker. */
22271
22272 if (this_type == NULL)
22273 return build_error_marker_type (cu, die);
22274
22275 return this_type;
22276 }
22277
22278 /* Return the type in DIE, CU.
22279 Returns NULL for invalid types.
22280
22281 This first does a lookup in die_type_hash,
22282 and only reads the die in if necessary.
22283
22284 NOTE: This can be called when reading in partial or full symbols. */
22285
22286 static struct type *
22287 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22288 {
22289 struct type *this_type;
22290
22291 this_type = get_die_type (die, cu);
22292 if (this_type)
22293 return this_type;
22294
22295 return read_type_die_1 (die, cu);
22296 }
22297
22298 /* Read the type in DIE, CU.
22299 Returns NULL for invalid types. */
22300
22301 static struct type *
22302 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22303 {
22304 struct type *this_type = NULL;
22305
22306 switch (die->tag)
22307 {
22308 case DW_TAG_class_type:
22309 case DW_TAG_interface_type:
22310 case DW_TAG_structure_type:
22311 case DW_TAG_union_type:
22312 this_type = read_structure_type (die, cu);
22313 break;
22314 case DW_TAG_enumeration_type:
22315 this_type = read_enumeration_type (die, cu);
22316 break;
22317 case DW_TAG_subprogram:
22318 case DW_TAG_subroutine_type:
22319 case DW_TAG_inlined_subroutine:
22320 this_type = read_subroutine_type (die, cu);
22321 break;
22322 case DW_TAG_array_type:
22323 this_type = read_array_type (die, cu);
22324 break;
22325 case DW_TAG_set_type:
22326 this_type = read_set_type (die, cu);
22327 break;
22328 case DW_TAG_pointer_type:
22329 this_type = read_tag_pointer_type (die, cu);
22330 break;
22331 case DW_TAG_ptr_to_member_type:
22332 this_type = read_tag_ptr_to_member_type (die, cu);
22333 break;
22334 case DW_TAG_reference_type:
22335 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22336 break;
22337 case DW_TAG_rvalue_reference_type:
22338 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22339 break;
22340 case DW_TAG_const_type:
22341 this_type = read_tag_const_type (die, cu);
22342 break;
22343 case DW_TAG_volatile_type:
22344 this_type = read_tag_volatile_type (die, cu);
22345 break;
22346 case DW_TAG_restrict_type:
22347 this_type = read_tag_restrict_type (die, cu);
22348 break;
22349 case DW_TAG_string_type:
22350 this_type = read_tag_string_type (die, cu);
22351 break;
22352 case DW_TAG_typedef:
22353 this_type = read_typedef (die, cu);
22354 break;
22355 case DW_TAG_subrange_type:
22356 this_type = read_subrange_type (die, cu);
22357 break;
22358 case DW_TAG_base_type:
22359 this_type = read_base_type (die, cu);
22360 break;
22361 case DW_TAG_unspecified_type:
22362 this_type = read_unspecified_type (die, cu);
22363 break;
22364 case DW_TAG_namespace:
22365 this_type = read_namespace_type (die, cu);
22366 break;
22367 case DW_TAG_module:
22368 this_type = read_module_type (die, cu);
22369 break;
22370 case DW_TAG_atomic_type:
22371 this_type = read_tag_atomic_type (die, cu);
22372 break;
22373 default:
22374 complaint (_("unexpected tag in read_type_die: '%s'"),
22375 dwarf_tag_name (die->tag));
22376 break;
22377 }
22378
22379 return this_type;
22380 }
22381
22382 /* See if we can figure out if the class lives in a namespace. We do
22383 this by looking for a member function; its demangled name will
22384 contain namespace info, if there is any.
22385 Return the computed name or NULL.
22386 Space for the result is allocated on the objfile's obstack.
22387 This is the full-die version of guess_partial_die_structure_name.
22388 In this case we know DIE has no useful parent. */
22389
22390 static char *
22391 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22392 {
22393 struct die_info *spec_die;
22394 struct dwarf2_cu *spec_cu;
22395 struct die_info *child;
22396 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22397
22398 spec_cu = cu;
22399 spec_die = die_specification (die, &spec_cu);
22400 if (spec_die != NULL)
22401 {
22402 die = spec_die;
22403 cu = spec_cu;
22404 }
22405
22406 for (child = die->child;
22407 child != NULL;
22408 child = child->sibling)
22409 {
22410 if (child->tag == DW_TAG_subprogram)
22411 {
22412 const char *linkage_name = dw2_linkage_name (child, cu);
22413
22414 if (linkage_name != NULL)
22415 {
22416 char *actual_name
22417 = language_class_name_from_physname (cu->language_defn,
22418 linkage_name);
22419 char *name = NULL;
22420
22421 if (actual_name != NULL)
22422 {
22423 const char *die_name = dwarf2_name (die, cu);
22424
22425 if (die_name != NULL
22426 && strcmp (die_name, actual_name) != 0)
22427 {
22428 /* Strip off the class name from the full name.
22429 We want the prefix. */
22430 int die_name_len = strlen (die_name);
22431 int actual_name_len = strlen (actual_name);
22432
22433 /* Test for '::' as a sanity check. */
22434 if (actual_name_len > die_name_len + 2
22435 && actual_name[actual_name_len
22436 - die_name_len - 1] == ':')
22437 name = obstack_strndup (
22438 &objfile->per_bfd->storage_obstack,
22439 actual_name, actual_name_len - die_name_len - 2);
22440 }
22441 }
22442 xfree (actual_name);
22443 return name;
22444 }
22445 }
22446 }
22447
22448 return NULL;
22449 }
22450
22451 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22452 prefix part in such case. See
22453 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22454
22455 static const char *
22456 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22457 {
22458 struct attribute *attr;
22459 const char *base;
22460
22461 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22462 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22463 return NULL;
22464
22465 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22466 return NULL;
22467
22468 attr = dw2_linkage_name_attr (die, cu);
22469 if (attr == NULL || DW_STRING (attr) == NULL)
22470 return NULL;
22471
22472 /* dwarf2_name had to be already called. */
22473 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22474
22475 /* Strip the base name, keep any leading namespaces/classes. */
22476 base = strrchr (DW_STRING (attr), ':');
22477 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22478 return "";
22479
22480 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22481 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22482 DW_STRING (attr),
22483 &base[-1] - DW_STRING (attr));
22484 }
22485
22486 /* Return the name of the namespace/class that DIE is defined within,
22487 or "" if we can't tell. The caller should not xfree the result.
22488
22489 For example, if we're within the method foo() in the following
22490 code:
22491
22492 namespace N {
22493 class C {
22494 void foo () {
22495 }
22496 };
22497 }
22498
22499 then determine_prefix on foo's die will return "N::C". */
22500
22501 static const char *
22502 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22503 {
22504 struct dwarf2_per_objfile *dwarf2_per_objfile
22505 = cu->per_cu->dwarf2_per_objfile;
22506 struct die_info *parent, *spec_die;
22507 struct dwarf2_cu *spec_cu;
22508 struct type *parent_type;
22509 const char *retval;
22510
22511 if (cu->language != language_cplus
22512 && cu->language != language_fortran && cu->language != language_d
22513 && cu->language != language_rust)
22514 return "";
22515
22516 retval = anonymous_struct_prefix (die, cu);
22517 if (retval)
22518 return retval;
22519
22520 /* We have to be careful in the presence of DW_AT_specification.
22521 For example, with GCC 3.4, given the code
22522
22523 namespace N {
22524 void foo() {
22525 // Definition of N::foo.
22526 }
22527 }
22528
22529 then we'll have a tree of DIEs like this:
22530
22531 1: DW_TAG_compile_unit
22532 2: DW_TAG_namespace // N
22533 3: DW_TAG_subprogram // declaration of N::foo
22534 4: DW_TAG_subprogram // definition of N::foo
22535 DW_AT_specification // refers to die #3
22536
22537 Thus, when processing die #4, we have to pretend that we're in
22538 the context of its DW_AT_specification, namely the contex of die
22539 #3. */
22540 spec_cu = cu;
22541 spec_die = die_specification (die, &spec_cu);
22542 if (spec_die == NULL)
22543 parent = die->parent;
22544 else
22545 {
22546 parent = spec_die->parent;
22547 cu = spec_cu;
22548 }
22549
22550 if (parent == NULL)
22551 return "";
22552 else if (parent->building_fullname)
22553 {
22554 const char *name;
22555 const char *parent_name;
22556
22557 /* It has been seen on RealView 2.2 built binaries,
22558 DW_TAG_template_type_param types actually _defined_ as
22559 children of the parent class:
22560
22561 enum E {};
22562 template class <class Enum> Class{};
22563 Class<enum E> class_e;
22564
22565 1: DW_TAG_class_type (Class)
22566 2: DW_TAG_enumeration_type (E)
22567 3: DW_TAG_enumerator (enum1:0)
22568 3: DW_TAG_enumerator (enum2:1)
22569 ...
22570 2: DW_TAG_template_type_param
22571 DW_AT_type DW_FORM_ref_udata (E)
22572
22573 Besides being broken debug info, it can put GDB into an
22574 infinite loop. Consider:
22575
22576 When we're building the full name for Class<E>, we'll start
22577 at Class, and go look over its template type parameters,
22578 finding E. We'll then try to build the full name of E, and
22579 reach here. We're now trying to build the full name of E,
22580 and look over the parent DIE for containing scope. In the
22581 broken case, if we followed the parent DIE of E, we'd again
22582 find Class, and once again go look at its template type
22583 arguments, etc., etc. Simply don't consider such parent die
22584 as source-level parent of this die (it can't be, the language
22585 doesn't allow it), and break the loop here. */
22586 name = dwarf2_name (die, cu);
22587 parent_name = dwarf2_name (parent, cu);
22588 complaint (_("template param type '%s' defined within parent '%s'"),
22589 name ? name : "<unknown>",
22590 parent_name ? parent_name : "<unknown>");
22591 return "";
22592 }
22593 else
22594 switch (parent->tag)
22595 {
22596 case DW_TAG_namespace:
22597 parent_type = read_type_die (parent, cu);
22598 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22599 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22600 Work around this problem here. */
22601 if (cu->language == language_cplus
22602 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22603 return "";
22604 /* We give a name to even anonymous namespaces. */
22605 return TYPE_NAME (parent_type);
22606 case DW_TAG_class_type:
22607 case DW_TAG_interface_type:
22608 case DW_TAG_structure_type:
22609 case DW_TAG_union_type:
22610 case DW_TAG_module:
22611 parent_type = read_type_die (parent, cu);
22612 if (TYPE_NAME (parent_type) != NULL)
22613 return TYPE_NAME (parent_type);
22614 else
22615 /* An anonymous structure is only allowed non-static data
22616 members; no typedefs, no member functions, et cetera.
22617 So it does not need a prefix. */
22618 return "";
22619 case DW_TAG_compile_unit:
22620 case DW_TAG_partial_unit:
22621 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22622 if (cu->language == language_cplus
22623 && !dwarf2_per_objfile->types.empty ()
22624 && die->child != NULL
22625 && (die->tag == DW_TAG_class_type
22626 || die->tag == DW_TAG_structure_type
22627 || die->tag == DW_TAG_union_type))
22628 {
22629 char *name = guess_full_die_structure_name (die, cu);
22630 if (name != NULL)
22631 return name;
22632 }
22633 return "";
22634 case DW_TAG_enumeration_type:
22635 parent_type = read_type_die (parent, cu);
22636 if (TYPE_DECLARED_CLASS (parent_type))
22637 {
22638 if (TYPE_NAME (parent_type) != NULL)
22639 return TYPE_NAME (parent_type);
22640 return "";
22641 }
22642 /* Fall through. */
22643 default:
22644 return determine_prefix (parent, cu);
22645 }
22646 }
22647
22648 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22649 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22650 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22651 an obconcat, otherwise allocate storage for the result. The CU argument is
22652 used to determine the language and hence, the appropriate separator. */
22653
22654 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22655
22656 static char *
22657 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22658 int physname, struct dwarf2_cu *cu)
22659 {
22660 const char *lead = "";
22661 const char *sep;
22662
22663 if (suffix == NULL || suffix[0] == '\0'
22664 || prefix == NULL || prefix[0] == '\0')
22665 sep = "";
22666 else if (cu->language == language_d)
22667 {
22668 /* For D, the 'main' function could be defined in any module, but it
22669 should never be prefixed. */
22670 if (strcmp (suffix, "D main") == 0)
22671 {
22672 prefix = "";
22673 sep = "";
22674 }
22675 else
22676 sep = ".";
22677 }
22678 else if (cu->language == language_fortran && physname)
22679 {
22680 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22681 DW_AT_MIPS_linkage_name is preferred and used instead. */
22682
22683 lead = "__";
22684 sep = "_MOD_";
22685 }
22686 else
22687 sep = "::";
22688
22689 if (prefix == NULL)
22690 prefix = "";
22691 if (suffix == NULL)
22692 suffix = "";
22693
22694 if (obs == NULL)
22695 {
22696 char *retval
22697 = ((char *)
22698 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22699
22700 strcpy (retval, lead);
22701 strcat (retval, prefix);
22702 strcat (retval, sep);
22703 strcat (retval, suffix);
22704 return retval;
22705 }
22706 else
22707 {
22708 /* We have an obstack. */
22709 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22710 }
22711 }
22712
22713 /* Return sibling of die, NULL if no sibling. */
22714
22715 static struct die_info *
22716 sibling_die (struct die_info *die)
22717 {
22718 return die->sibling;
22719 }
22720
22721 /* Get name of a die, return NULL if not found. */
22722
22723 static const char *
22724 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22725 struct obstack *obstack)
22726 {
22727 if (name && cu->language == language_cplus)
22728 {
22729 std::string canon_name = cp_canonicalize_string (name);
22730
22731 if (!canon_name.empty ())
22732 {
22733 if (canon_name != name)
22734 name = obstack_strdup (obstack, canon_name);
22735 }
22736 }
22737
22738 return name;
22739 }
22740
22741 /* Get name of a die, return NULL if not found.
22742 Anonymous namespaces are converted to their magic string. */
22743
22744 static const char *
22745 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22746 {
22747 struct attribute *attr;
22748 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22749
22750 attr = dwarf2_attr (die, DW_AT_name, cu);
22751 if ((!attr || !DW_STRING (attr))
22752 && die->tag != DW_TAG_namespace
22753 && die->tag != DW_TAG_class_type
22754 && die->tag != DW_TAG_interface_type
22755 && die->tag != DW_TAG_structure_type
22756 && die->tag != DW_TAG_union_type)
22757 return NULL;
22758
22759 switch (die->tag)
22760 {
22761 case DW_TAG_compile_unit:
22762 case DW_TAG_partial_unit:
22763 /* Compilation units have a DW_AT_name that is a filename, not
22764 a source language identifier. */
22765 case DW_TAG_enumeration_type:
22766 case DW_TAG_enumerator:
22767 /* These tags always have simple identifiers already; no need
22768 to canonicalize them. */
22769 return DW_STRING (attr);
22770
22771 case DW_TAG_namespace:
22772 if (attr != NULL && DW_STRING (attr) != NULL)
22773 return DW_STRING (attr);
22774 return CP_ANONYMOUS_NAMESPACE_STR;
22775
22776 case DW_TAG_class_type:
22777 case DW_TAG_interface_type:
22778 case DW_TAG_structure_type:
22779 case DW_TAG_union_type:
22780 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22781 structures or unions. These were of the form "._%d" in GCC 4.1,
22782 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22783 and GCC 4.4. We work around this problem by ignoring these. */
22784 if (attr && DW_STRING (attr)
22785 && (startswith (DW_STRING (attr), "._")
22786 || startswith (DW_STRING (attr), "<anonymous")))
22787 return NULL;
22788
22789 /* GCC might emit a nameless typedef that has a linkage name. See
22790 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22791 if (!attr || DW_STRING (attr) == NULL)
22792 {
22793 char *demangled = NULL;
22794
22795 attr = dw2_linkage_name_attr (die, cu);
22796 if (attr == NULL || DW_STRING (attr) == NULL)
22797 return NULL;
22798
22799 /* Avoid demangling DW_STRING (attr) the second time on a second
22800 call for the same DIE. */
22801 if (!DW_STRING_IS_CANONICAL (attr))
22802 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22803
22804 if (demangled)
22805 {
22806 const char *base;
22807
22808 /* FIXME: we already did this for the partial symbol... */
22809 DW_STRING (attr)
22810 = obstack_strdup (&objfile->per_bfd->storage_obstack,
22811 demangled);
22812 DW_STRING_IS_CANONICAL (attr) = 1;
22813 xfree (demangled);
22814
22815 /* Strip any leading namespaces/classes, keep only the base name.
22816 DW_AT_name for named DIEs does not contain the prefixes. */
22817 base = strrchr (DW_STRING (attr), ':');
22818 if (base && base > DW_STRING (attr) && base[-1] == ':')
22819 return &base[1];
22820 else
22821 return DW_STRING (attr);
22822 }
22823 }
22824 break;
22825
22826 default:
22827 break;
22828 }
22829
22830 if (!DW_STRING_IS_CANONICAL (attr))
22831 {
22832 DW_STRING (attr)
22833 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22834 &objfile->per_bfd->storage_obstack);
22835 DW_STRING_IS_CANONICAL (attr) = 1;
22836 }
22837 return DW_STRING (attr);
22838 }
22839
22840 /* Return the die that this die in an extension of, or NULL if there
22841 is none. *EXT_CU is the CU containing DIE on input, and the CU
22842 containing the return value on output. */
22843
22844 static struct die_info *
22845 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22846 {
22847 struct attribute *attr;
22848
22849 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22850 if (attr == NULL)
22851 return NULL;
22852
22853 return follow_die_ref (die, attr, ext_cu);
22854 }
22855
22856 /* A convenience function that returns an "unknown" DWARF name,
22857 including the value of V. STR is the name of the entity being
22858 printed, e.g., "TAG". */
22859
22860 static const char *
22861 dwarf_unknown (const char *str, unsigned v)
22862 {
22863 char *cell = get_print_cell ();
22864 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
22865 return cell;
22866 }
22867
22868 /* Convert a DIE tag into its string name. */
22869
22870 static const char *
22871 dwarf_tag_name (unsigned tag)
22872 {
22873 const char *name = get_DW_TAG_name (tag);
22874
22875 if (name == NULL)
22876 return dwarf_unknown ("TAG", tag);
22877
22878 return name;
22879 }
22880
22881 /* Convert a DWARF attribute code into its string name. */
22882
22883 static const char *
22884 dwarf_attr_name (unsigned attr)
22885 {
22886 const char *name;
22887
22888 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22889 if (attr == DW_AT_MIPS_fde)
22890 return "DW_AT_MIPS_fde";
22891 #else
22892 if (attr == DW_AT_HP_block_index)
22893 return "DW_AT_HP_block_index";
22894 #endif
22895
22896 name = get_DW_AT_name (attr);
22897
22898 if (name == NULL)
22899 return dwarf_unknown ("AT", attr);
22900
22901 return name;
22902 }
22903
22904 /* Convert a unit type to corresponding DW_UT name. */
22905
22906 static const char *
22907 dwarf_unit_type_name (int unit_type) {
22908 switch (unit_type)
22909 {
22910 case 0x01:
22911 return "DW_UT_compile (0x01)";
22912 case 0x02:
22913 return "DW_UT_type (0x02)";
22914 case 0x03:
22915 return "DW_UT_partial (0x03)";
22916 case 0x04:
22917 return "DW_UT_skeleton (0x04)";
22918 case 0x05:
22919 return "DW_UT_split_compile (0x05)";
22920 case 0x06:
22921 return "DW_UT_split_type (0x06)";
22922 case 0x80:
22923 return "DW_UT_lo_user (0x80)";
22924 case 0xff:
22925 return "DW_UT_hi_user (0xff)";
22926 default:
22927 return nullptr;
22928 }
22929 }
22930
22931 /* Convert a DWARF value form code into its string name. */
22932
22933 static const char *
22934 dwarf_form_name (unsigned form)
22935 {
22936 const char *name = get_DW_FORM_name (form);
22937
22938 if (name == NULL)
22939 return dwarf_unknown ("FORM", form);
22940
22941 return name;
22942 }
22943
22944 static const char *
22945 dwarf_bool_name (unsigned mybool)
22946 {
22947 if (mybool)
22948 return "TRUE";
22949 else
22950 return "FALSE";
22951 }
22952
22953 /* Convert a DWARF type code into its string name. */
22954
22955 static const char *
22956 dwarf_type_encoding_name (unsigned enc)
22957 {
22958 const char *name = get_DW_ATE_name (enc);
22959
22960 if (name == NULL)
22961 return dwarf_unknown ("ATE", enc);
22962
22963 return name;
22964 }
22965
22966 static void
22967 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22968 {
22969 unsigned int i;
22970
22971 print_spaces (indent, f);
22972 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22973 dwarf_tag_name (die->tag), die->abbrev,
22974 sect_offset_str (die->sect_off));
22975
22976 if (die->parent != NULL)
22977 {
22978 print_spaces (indent, f);
22979 fprintf_unfiltered (f, " parent at offset: %s\n",
22980 sect_offset_str (die->parent->sect_off));
22981 }
22982
22983 print_spaces (indent, f);
22984 fprintf_unfiltered (f, " has children: %s\n",
22985 dwarf_bool_name (die->child != NULL));
22986
22987 print_spaces (indent, f);
22988 fprintf_unfiltered (f, " attributes:\n");
22989
22990 for (i = 0; i < die->num_attrs; ++i)
22991 {
22992 print_spaces (indent, f);
22993 fprintf_unfiltered (f, " %s (%s) ",
22994 dwarf_attr_name (die->attrs[i].name),
22995 dwarf_form_name (die->attrs[i].form));
22996
22997 switch (die->attrs[i].form)
22998 {
22999 case DW_FORM_addr:
23000 case DW_FORM_addrx:
23001 case DW_FORM_GNU_addr_index:
23002 fprintf_unfiltered (f, "address: ");
23003 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
23004 break;
23005 case DW_FORM_block2:
23006 case DW_FORM_block4:
23007 case DW_FORM_block:
23008 case DW_FORM_block1:
23009 fprintf_unfiltered (f, "block: size %s",
23010 pulongest (DW_BLOCK (&die->attrs[i])->size));
23011 break;
23012 case DW_FORM_exprloc:
23013 fprintf_unfiltered (f, "expression: size %s",
23014 pulongest (DW_BLOCK (&die->attrs[i])->size));
23015 break;
23016 case DW_FORM_data16:
23017 fprintf_unfiltered (f, "constant of 16 bytes");
23018 break;
23019 case DW_FORM_ref_addr:
23020 fprintf_unfiltered (f, "ref address: ");
23021 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23022 break;
23023 case DW_FORM_GNU_ref_alt:
23024 fprintf_unfiltered (f, "alt ref address: ");
23025 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23026 break;
23027 case DW_FORM_ref1:
23028 case DW_FORM_ref2:
23029 case DW_FORM_ref4:
23030 case DW_FORM_ref8:
23031 case DW_FORM_ref_udata:
23032 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23033 (long) (DW_UNSND (&die->attrs[i])));
23034 break;
23035 case DW_FORM_data1:
23036 case DW_FORM_data2:
23037 case DW_FORM_data4:
23038 case DW_FORM_data8:
23039 case DW_FORM_udata:
23040 case DW_FORM_sdata:
23041 fprintf_unfiltered (f, "constant: %s",
23042 pulongest (DW_UNSND (&die->attrs[i])));
23043 break;
23044 case DW_FORM_sec_offset:
23045 fprintf_unfiltered (f, "section offset: %s",
23046 pulongest (DW_UNSND (&die->attrs[i])));
23047 break;
23048 case DW_FORM_ref_sig8:
23049 fprintf_unfiltered (f, "signature: %s",
23050 hex_string (DW_SIGNATURE (&die->attrs[i])));
23051 break;
23052 case DW_FORM_string:
23053 case DW_FORM_strp:
23054 case DW_FORM_line_strp:
23055 case DW_FORM_strx:
23056 case DW_FORM_GNU_str_index:
23057 case DW_FORM_GNU_strp_alt:
23058 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23059 DW_STRING (&die->attrs[i])
23060 ? DW_STRING (&die->attrs[i]) : "",
23061 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
23062 break;
23063 case DW_FORM_flag:
23064 if (DW_UNSND (&die->attrs[i]))
23065 fprintf_unfiltered (f, "flag: TRUE");
23066 else
23067 fprintf_unfiltered (f, "flag: FALSE");
23068 break;
23069 case DW_FORM_flag_present:
23070 fprintf_unfiltered (f, "flag: TRUE");
23071 break;
23072 case DW_FORM_indirect:
23073 /* The reader will have reduced the indirect form to
23074 the "base form" so this form should not occur. */
23075 fprintf_unfiltered (f,
23076 "unexpected attribute form: DW_FORM_indirect");
23077 break;
23078 case DW_FORM_implicit_const:
23079 fprintf_unfiltered (f, "constant: %s",
23080 plongest (DW_SND (&die->attrs[i])));
23081 break;
23082 default:
23083 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23084 die->attrs[i].form);
23085 break;
23086 }
23087 fprintf_unfiltered (f, "\n");
23088 }
23089 }
23090
23091 static void
23092 dump_die_for_error (struct die_info *die)
23093 {
23094 dump_die_shallow (gdb_stderr, 0, die);
23095 }
23096
23097 static void
23098 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23099 {
23100 int indent = level * 4;
23101
23102 gdb_assert (die != NULL);
23103
23104 if (level >= max_level)
23105 return;
23106
23107 dump_die_shallow (f, indent, die);
23108
23109 if (die->child != NULL)
23110 {
23111 print_spaces (indent, f);
23112 fprintf_unfiltered (f, " Children:");
23113 if (level + 1 < max_level)
23114 {
23115 fprintf_unfiltered (f, "\n");
23116 dump_die_1 (f, level + 1, max_level, die->child);
23117 }
23118 else
23119 {
23120 fprintf_unfiltered (f,
23121 " [not printed, max nesting level reached]\n");
23122 }
23123 }
23124
23125 if (die->sibling != NULL && level > 0)
23126 {
23127 dump_die_1 (f, level, max_level, die->sibling);
23128 }
23129 }
23130
23131 /* This is called from the pdie macro in gdbinit.in.
23132 It's not static so gcc will keep a copy callable from gdb. */
23133
23134 void
23135 dump_die (struct die_info *die, int max_level)
23136 {
23137 dump_die_1 (gdb_stdlog, 0, max_level, die);
23138 }
23139
23140 static void
23141 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23142 {
23143 void **slot;
23144
23145 slot = htab_find_slot_with_hash (cu->die_hash, die,
23146 to_underlying (die->sect_off),
23147 INSERT);
23148
23149 *slot = die;
23150 }
23151
23152 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23153 required kind. */
23154
23155 static sect_offset
23156 dwarf2_get_ref_die_offset (const struct attribute *attr)
23157 {
23158 if (attr_form_is_ref (attr))
23159 return (sect_offset) DW_UNSND (attr);
23160
23161 complaint (_("unsupported die ref attribute form: '%s'"),
23162 dwarf_form_name (attr->form));
23163 return {};
23164 }
23165
23166 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23167 * the value held by the attribute is not constant. */
23168
23169 static LONGEST
23170 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23171 {
23172 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23173 return DW_SND (attr);
23174 else if (attr->form == DW_FORM_udata
23175 || attr->form == DW_FORM_data1
23176 || attr->form == DW_FORM_data2
23177 || attr->form == DW_FORM_data4
23178 || attr->form == DW_FORM_data8)
23179 return DW_UNSND (attr);
23180 else
23181 {
23182 /* For DW_FORM_data16 see attr_form_is_constant. */
23183 complaint (_("Attribute value is not a constant (%s)"),
23184 dwarf_form_name (attr->form));
23185 return default_value;
23186 }
23187 }
23188
23189 /* Follow reference or signature attribute ATTR of SRC_DIE.
23190 On entry *REF_CU is the CU of SRC_DIE.
23191 On exit *REF_CU is the CU of the result. */
23192
23193 static struct die_info *
23194 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23195 struct dwarf2_cu **ref_cu)
23196 {
23197 struct die_info *die;
23198
23199 if (attr_form_is_ref (attr))
23200 die = follow_die_ref (src_die, attr, ref_cu);
23201 else if (attr->form == DW_FORM_ref_sig8)
23202 die = follow_die_sig (src_die, attr, ref_cu);
23203 else
23204 {
23205 dump_die_for_error (src_die);
23206 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23207 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23208 }
23209
23210 return die;
23211 }
23212
23213 /* Follow reference OFFSET.
23214 On entry *REF_CU is the CU of the source die referencing OFFSET.
23215 On exit *REF_CU is the CU of the result.
23216 Returns NULL if OFFSET is invalid. */
23217
23218 static struct die_info *
23219 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23220 struct dwarf2_cu **ref_cu)
23221 {
23222 struct die_info temp_die;
23223 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23224 struct dwarf2_per_objfile *dwarf2_per_objfile
23225 = cu->per_cu->dwarf2_per_objfile;
23226
23227 gdb_assert (cu->per_cu != NULL);
23228
23229 target_cu = cu;
23230
23231 if (cu->per_cu->is_debug_types)
23232 {
23233 /* .debug_types CUs cannot reference anything outside their CU.
23234 If they need to, they have to reference a signatured type via
23235 DW_FORM_ref_sig8. */
23236 if (!offset_in_cu_p (&cu->header, sect_off))
23237 return NULL;
23238 }
23239 else if (offset_in_dwz != cu->per_cu->is_dwz
23240 || !offset_in_cu_p (&cu->header, sect_off))
23241 {
23242 struct dwarf2_per_cu_data *per_cu;
23243
23244 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23245 dwarf2_per_objfile);
23246
23247 /* If necessary, add it to the queue and load its DIEs. */
23248 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23249 load_full_comp_unit (per_cu, false, cu->language);
23250
23251 target_cu = per_cu->cu;
23252 }
23253 else if (cu->dies == NULL)
23254 {
23255 /* We're loading full DIEs during partial symbol reading. */
23256 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23257 load_full_comp_unit (cu->per_cu, false, language_minimal);
23258 }
23259
23260 *ref_cu = target_cu;
23261 temp_die.sect_off = sect_off;
23262
23263 if (target_cu != cu)
23264 target_cu->ancestor = cu;
23265
23266 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23267 &temp_die,
23268 to_underlying (sect_off));
23269 }
23270
23271 /* Follow reference attribute ATTR of SRC_DIE.
23272 On entry *REF_CU is the CU of SRC_DIE.
23273 On exit *REF_CU is the CU of the result. */
23274
23275 static struct die_info *
23276 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23277 struct dwarf2_cu **ref_cu)
23278 {
23279 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23280 struct dwarf2_cu *cu = *ref_cu;
23281 struct die_info *die;
23282
23283 die = follow_die_offset (sect_off,
23284 (attr->form == DW_FORM_GNU_ref_alt
23285 || cu->per_cu->is_dwz),
23286 ref_cu);
23287 if (!die)
23288 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23289 "at %s [in module %s]"),
23290 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23291 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23292
23293 return die;
23294 }
23295
23296 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23297 Returned value is intended for DW_OP_call*. Returned
23298 dwarf2_locexpr_baton->data has lifetime of
23299 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23300
23301 struct dwarf2_locexpr_baton
23302 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23303 struct dwarf2_per_cu_data *per_cu,
23304 CORE_ADDR (*get_frame_pc) (void *baton),
23305 void *baton, bool resolve_abstract_p)
23306 {
23307 struct dwarf2_cu *cu;
23308 struct die_info *die;
23309 struct attribute *attr;
23310 struct dwarf2_locexpr_baton retval;
23311 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23312 struct objfile *objfile = dwarf2_per_objfile->objfile;
23313
23314 if (per_cu->cu == NULL)
23315 load_cu (per_cu, false);
23316 cu = per_cu->cu;
23317 if (cu == NULL)
23318 {
23319 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23320 Instead just throw an error, not much else we can do. */
23321 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23322 sect_offset_str (sect_off), objfile_name (objfile));
23323 }
23324
23325 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23326 if (!die)
23327 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23328 sect_offset_str (sect_off), objfile_name (objfile));
23329
23330 attr = dwarf2_attr (die, DW_AT_location, cu);
23331 if (!attr && resolve_abstract_p
23332 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
23333 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23334 {
23335 CORE_ADDR pc = (*get_frame_pc) (baton);
23336 CORE_ADDR baseaddr
23337 = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23338 struct gdbarch *gdbarch = get_objfile_arch (objfile);
23339
23340 for (const auto &cand_off
23341 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
23342 {
23343 struct dwarf2_cu *cand_cu = cu;
23344 struct die_info *cand
23345 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23346 if (!cand
23347 || !cand->parent
23348 || cand->parent->tag != DW_TAG_subprogram)
23349 continue;
23350
23351 CORE_ADDR pc_low, pc_high;
23352 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23353 if (pc_low == ((CORE_ADDR) -1))
23354 continue;
23355 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23356 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23357 if (!(pc_low <= pc && pc < pc_high))
23358 continue;
23359
23360 die = cand;
23361 attr = dwarf2_attr (die, DW_AT_location, cu);
23362 break;
23363 }
23364 }
23365
23366 if (!attr)
23367 {
23368 /* DWARF: "If there is no such attribute, then there is no effect.".
23369 DATA is ignored if SIZE is 0. */
23370
23371 retval.data = NULL;
23372 retval.size = 0;
23373 }
23374 else if (attr_form_is_section_offset (attr))
23375 {
23376 struct dwarf2_loclist_baton loclist_baton;
23377 CORE_ADDR pc = (*get_frame_pc) (baton);
23378 size_t size;
23379
23380 fill_in_loclist_baton (cu, &loclist_baton, attr);
23381
23382 retval.data = dwarf2_find_location_expression (&loclist_baton,
23383 &size, pc);
23384 retval.size = size;
23385 }
23386 else
23387 {
23388 if (!attr_form_is_block (attr))
23389 error (_("Dwarf Error: DIE at %s referenced in module %s "
23390 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23391 sect_offset_str (sect_off), objfile_name (objfile));
23392
23393 retval.data = DW_BLOCK (attr)->data;
23394 retval.size = DW_BLOCK (attr)->size;
23395 }
23396 retval.per_cu = cu->per_cu;
23397
23398 age_cached_comp_units (dwarf2_per_objfile);
23399
23400 return retval;
23401 }
23402
23403 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23404 offset. */
23405
23406 struct dwarf2_locexpr_baton
23407 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23408 struct dwarf2_per_cu_data *per_cu,
23409 CORE_ADDR (*get_frame_pc) (void *baton),
23410 void *baton)
23411 {
23412 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23413
23414 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23415 }
23416
23417 /* Write a constant of a given type as target-ordered bytes into
23418 OBSTACK. */
23419
23420 static const gdb_byte *
23421 write_constant_as_bytes (struct obstack *obstack,
23422 enum bfd_endian byte_order,
23423 struct type *type,
23424 ULONGEST value,
23425 LONGEST *len)
23426 {
23427 gdb_byte *result;
23428
23429 *len = TYPE_LENGTH (type);
23430 result = (gdb_byte *) obstack_alloc (obstack, *len);
23431 store_unsigned_integer (result, *len, byte_order, value);
23432
23433 return result;
23434 }
23435
23436 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23437 pointer to the constant bytes and set LEN to the length of the
23438 data. If memory is needed, allocate it on OBSTACK. If the DIE
23439 does not have a DW_AT_const_value, return NULL. */
23440
23441 const gdb_byte *
23442 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23443 struct dwarf2_per_cu_data *per_cu,
23444 struct obstack *obstack,
23445 LONGEST *len)
23446 {
23447 struct dwarf2_cu *cu;
23448 struct die_info *die;
23449 struct attribute *attr;
23450 const gdb_byte *result = NULL;
23451 struct type *type;
23452 LONGEST value;
23453 enum bfd_endian byte_order;
23454 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23455
23456 if (per_cu->cu == NULL)
23457 load_cu (per_cu, false);
23458 cu = per_cu->cu;
23459 if (cu == NULL)
23460 {
23461 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23462 Instead just throw an error, not much else we can do. */
23463 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23464 sect_offset_str (sect_off), objfile_name (objfile));
23465 }
23466
23467 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23468 if (!die)
23469 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23470 sect_offset_str (sect_off), objfile_name (objfile));
23471
23472 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23473 if (attr == NULL)
23474 return NULL;
23475
23476 byte_order = (bfd_big_endian (objfile->obfd)
23477 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23478
23479 switch (attr->form)
23480 {
23481 case DW_FORM_addr:
23482 case DW_FORM_addrx:
23483 case DW_FORM_GNU_addr_index:
23484 {
23485 gdb_byte *tem;
23486
23487 *len = cu->header.addr_size;
23488 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23489 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23490 result = tem;
23491 }
23492 break;
23493 case DW_FORM_string:
23494 case DW_FORM_strp:
23495 case DW_FORM_strx:
23496 case DW_FORM_GNU_str_index:
23497 case DW_FORM_GNU_strp_alt:
23498 /* DW_STRING is already allocated on the objfile obstack, point
23499 directly to it. */
23500 result = (const gdb_byte *) DW_STRING (attr);
23501 *len = strlen (DW_STRING (attr));
23502 break;
23503 case DW_FORM_block1:
23504 case DW_FORM_block2:
23505 case DW_FORM_block4:
23506 case DW_FORM_block:
23507 case DW_FORM_exprloc:
23508 case DW_FORM_data16:
23509 result = DW_BLOCK (attr)->data;
23510 *len = DW_BLOCK (attr)->size;
23511 break;
23512
23513 /* The DW_AT_const_value attributes are supposed to carry the
23514 symbol's value "represented as it would be on the target
23515 architecture." By the time we get here, it's already been
23516 converted to host endianness, so we just need to sign- or
23517 zero-extend it as appropriate. */
23518 case DW_FORM_data1:
23519 type = die_type (die, cu);
23520 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23521 if (result == NULL)
23522 result = write_constant_as_bytes (obstack, byte_order,
23523 type, value, len);
23524 break;
23525 case DW_FORM_data2:
23526 type = die_type (die, cu);
23527 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23528 if (result == NULL)
23529 result = write_constant_as_bytes (obstack, byte_order,
23530 type, value, len);
23531 break;
23532 case DW_FORM_data4:
23533 type = die_type (die, cu);
23534 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23535 if (result == NULL)
23536 result = write_constant_as_bytes (obstack, byte_order,
23537 type, value, len);
23538 break;
23539 case DW_FORM_data8:
23540 type = die_type (die, cu);
23541 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23542 if (result == NULL)
23543 result = write_constant_as_bytes (obstack, byte_order,
23544 type, value, len);
23545 break;
23546
23547 case DW_FORM_sdata:
23548 case DW_FORM_implicit_const:
23549 type = die_type (die, cu);
23550 result = write_constant_as_bytes (obstack, byte_order,
23551 type, DW_SND (attr), len);
23552 break;
23553
23554 case DW_FORM_udata:
23555 type = die_type (die, cu);
23556 result = write_constant_as_bytes (obstack, byte_order,
23557 type, DW_UNSND (attr), len);
23558 break;
23559
23560 default:
23561 complaint (_("unsupported const value attribute form: '%s'"),
23562 dwarf_form_name (attr->form));
23563 break;
23564 }
23565
23566 return result;
23567 }
23568
23569 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23570 valid type for this die is found. */
23571
23572 struct type *
23573 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23574 struct dwarf2_per_cu_data *per_cu)
23575 {
23576 struct dwarf2_cu *cu;
23577 struct die_info *die;
23578
23579 if (per_cu->cu == NULL)
23580 load_cu (per_cu, false);
23581 cu = per_cu->cu;
23582 if (!cu)
23583 return NULL;
23584
23585 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23586 if (!die)
23587 return NULL;
23588
23589 return die_type (die, cu);
23590 }
23591
23592 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23593 PER_CU. */
23594
23595 struct type *
23596 dwarf2_get_die_type (cu_offset die_offset,
23597 struct dwarf2_per_cu_data *per_cu)
23598 {
23599 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23600 return get_die_type_at_offset (die_offset_sect, per_cu);
23601 }
23602
23603 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23604 On entry *REF_CU is the CU of SRC_DIE.
23605 On exit *REF_CU is the CU of the result.
23606 Returns NULL if the referenced DIE isn't found. */
23607
23608 static struct die_info *
23609 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23610 struct dwarf2_cu **ref_cu)
23611 {
23612 struct die_info temp_die;
23613 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23614 struct die_info *die;
23615
23616 /* While it might be nice to assert sig_type->type == NULL here,
23617 we can get here for DW_AT_imported_declaration where we need
23618 the DIE not the type. */
23619
23620 /* If necessary, add it to the queue and load its DIEs. */
23621
23622 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23623 read_signatured_type (sig_type);
23624
23625 sig_cu = sig_type->per_cu.cu;
23626 gdb_assert (sig_cu != NULL);
23627 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23628 temp_die.sect_off = sig_type->type_offset_in_section;
23629 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23630 to_underlying (temp_die.sect_off));
23631 if (die)
23632 {
23633 struct dwarf2_per_objfile *dwarf2_per_objfile
23634 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23635
23636 /* For .gdb_index version 7 keep track of included TUs.
23637 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23638 if (dwarf2_per_objfile->index_table != NULL
23639 && dwarf2_per_objfile->index_table->version <= 7)
23640 {
23641 VEC_safe_push (dwarf2_per_cu_ptr,
23642 (*ref_cu)->per_cu->imported_symtabs,
23643 sig_cu->per_cu);
23644 }
23645
23646 *ref_cu = sig_cu;
23647 if (sig_cu != cu)
23648 sig_cu->ancestor = cu;
23649
23650 return die;
23651 }
23652
23653 return NULL;
23654 }
23655
23656 /* Follow signatured type referenced by ATTR in SRC_DIE.
23657 On entry *REF_CU is the CU of SRC_DIE.
23658 On exit *REF_CU is the CU of the result.
23659 The result is the DIE of the type.
23660 If the referenced type cannot be found an error is thrown. */
23661
23662 static struct die_info *
23663 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23664 struct dwarf2_cu **ref_cu)
23665 {
23666 ULONGEST signature = DW_SIGNATURE (attr);
23667 struct signatured_type *sig_type;
23668 struct die_info *die;
23669
23670 gdb_assert (attr->form == DW_FORM_ref_sig8);
23671
23672 sig_type = lookup_signatured_type (*ref_cu, signature);
23673 /* sig_type will be NULL if the signatured type is missing from
23674 the debug info. */
23675 if (sig_type == NULL)
23676 {
23677 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23678 " from DIE at %s [in module %s]"),
23679 hex_string (signature), sect_offset_str (src_die->sect_off),
23680 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23681 }
23682
23683 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23684 if (die == NULL)
23685 {
23686 dump_die_for_error (src_die);
23687 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23688 " from DIE at %s [in module %s]"),
23689 hex_string (signature), sect_offset_str (src_die->sect_off),
23690 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23691 }
23692
23693 return die;
23694 }
23695
23696 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23697 reading in and processing the type unit if necessary. */
23698
23699 static struct type *
23700 get_signatured_type (struct die_info *die, ULONGEST signature,
23701 struct dwarf2_cu *cu)
23702 {
23703 struct dwarf2_per_objfile *dwarf2_per_objfile
23704 = cu->per_cu->dwarf2_per_objfile;
23705 struct signatured_type *sig_type;
23706 struct dwarf2_cu *type_cu;
23707 struct die_info *type_die;
23708 struct type *type;
23709
23710 sig_type = lookup_signatured_type (cu, signature);
23711 /* sig_type will be NULL if the signatured type is missing from
23712 the debug info. */
23713 if (sig_type == NULL)
23714 {
23715 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23716 " from DIE at %s [in module %s]"),
23717 hex_string (signature), sect_offset_str (die->sect_off),
23718 objfile_name (dwarf2_per_objfile->objfile));
23719 return build_error_marker_type (cu, die);
23720 }
23721
23722 /* If we already know the type we're done. */
23723 if (sig_type->type != NULL)
23724 return sig_type->type;
23725
23726 type_cu = cu;
23727 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23728 if (type_die != NULL)
23729 {
23730 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23731 is created. This is important, for example, because for c++ classes
23732 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23733 type = read_type_die (type_die, type_cu);
23734 if (type == NULL)
23735 {
23736 complaint (_("Dwarf Error: Cannot build signatured type %s"
23737 " referenced from DIE at %s [in module %s]"),
23738 hex_string (signature), sect_offset_str (die->sect_off),
23739 objfile_name (dwarf2_per_objfile->objfile));
23740 type = build_error_marker_type (cu, die);
23741 }
23742 }
23743 else
23744 {
23745 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23746 " from DIE at %s [in module %s]"),
23747 hex_string (signature), sect_offset_str (die->sect_off),
23748 objfile_name (dwarf2_per_objfile->objfile));
23749 type = build_error_marker_type (cu, die);
23750 }
23751 sig_type->type = type;
23752
23753 return type;
23754 }
23755
23756 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23757 reading in and processing the type unit if necessary. */
23758
23759 static struct type *
23760 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23761 struct dwarf2_cu *cu) /* ARI: editCase function */
23762 {
23763 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23764 if (attr_form_is_ref (attr))
23765 {
23766 struct dwarf2_cu *type_cu = cu;
23767 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23768
23769 return read_type_die (type_die, type_cu);
23770 }
23771 else if (attr->form == DW_FORM_ref_sig8)
23772 {
23773 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23774 }
23775 else
23776 {
23777 struct dwarf2_per_objfile *dwarf2_per_objfile
23778 = cu->per_cu->dwarf2_per_objfile;
23779
23780 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23781 " at %s [in module %s]"),
23782 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23783 objfile_name (dwarf2_per_objfile->objfile));
23784 return build_error_marker_type (cu, die);
23785 }
23786 }
23787
23788 /* Load the DIEs associated with type unit PER_CU into memory. */
23789
23790 static void
23791 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23792 {
23793 struct signatured_type *sig_type;
23794
23795 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23796 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23797
23798 /* We have the per_cu, but we need the signatured_type.
23799 Fortunately this is an easy translation. */
23800 gdb_assert (per_cu->is_debug_types);
23801 sig_type = (struct signatured_type *) per_cu;
23802
23803 gdb_assert (per_cu->cu == NULL);
23804
23805 read_signatured_type (sig_type);
23806
23807 gdb_assert (per_cu->cu != NULL);
23808 }
23809
23810 /* die_reader_func for read_signatured_type.
23811 This is identical to load_full_comp_unit_reader,
23812 but is kept separate for now. */
23813
23814 static void
23815 read_signatured_type_reader (const struct die_reader_specs *reader,
23816 const gdb_byte *info_ptr,
23817 struct die_info *comp_unit_die,
23818 int has_children,
23819 void *data)
23820 {
23821 struct dwarf2_cu *cu = reader->cu;
23822
23823 gdb_assert (cu->die_hash == NULL);
23824 cu->die_hash =
23825 htab_create_alloc_ex (cu->header.length / 12,
23826 die_hash,
23827 die_eq,
23828 NULL,
23829 &cu->comp_unit_obstack,
23830 hashtab_obstack_allocate,
23831 dummy_obstack_deallocate);
23832
23833 if (has_children)
23834 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23835 &info_ptr, comp_unit_die);
23836 cu->dies = comp_unit_die;
23837 /* comp_unit_die is not stored in die_hash, no need. */
23838
23839 /* We try not to read any attributes in this function, because not
23840 all CUs needed for references have been loaded yet, and symbol
23841 table processing isn't initialized. But we have to set the CU language,
23842 or we won't be able to build types correctly.
23843 Similarly, if we do not read the producer, we can not apply
23844 producer-specific interpretation. */
23845 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23846 }
23847
23848 /* Read in a signatured type and build its CU and DIEs.
23849 If the type is a stub for the real type in a DWO file,
23850 read in the real type from the DWO file as well. */
23851
23852 static void
23853 read_signatured_type (struct signatured_type *sig_type)
23854 {
23855 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23856
23857 gdb_assert (per_cu->is_debug_types);
23858 gdb_assert (per_cu->cu == NULL);
23859
23860 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
23861 read_signatured_type_reader, NULL);
23862 sig_type->per_cu.tu_read = 1;
23863 }
23864
23865 /* Decode simple location descriptions.
23866 Given a pointer to a dwarf block that defines a location, compute
23867 the location and return the value.
23868
23869 NOTE drow/2003-11-18: This function is called in two situations
23870 now: for the address of static or global variables (partial symbols
23871 only) and for offsets into structures which are expected to be
23872 (more or less) constant. The partial symbol case should go away,
23873 and only the constant case should remain. That will let this
23874 function complain more accurately. A few special modes are allowed
23875 without complaint for global variables (for instance, global
23876 register values and thread-local values).
23877
23878 A location description containing no operations indicates that the
23879 object is optimized out. The return value is 0 for that case.
23880 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23881 callers will only want a very basic result and this can become a
23882 complaint.
23883
23884 Note that stack[0] is unused except as a default error return. */
23885
23886 static CORE_ADDR
23887 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23888 {
23889 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23890 size_t i;
23891 size_t size = blk->size;
23892 const gdb_byte *data = blk->data;
23893 CORE_ADDR stack[64];
23894 int stacki;
23895 unsigned int bytes_read, unsnd;
23896 gdb_byte op;
23897
23898 i = 0;
23899 stacki = 0;
23900 stack[stacki] = 0;
23901 stack[++stacki] = 0;
23902
23903 while (i < size)
23904 {
23905 op = data[i++];
23906 switch (op)
23907 {
23908 case DW_OP_lit0:
23909 case DW_OP_lit1:
23910 case DW_OP_lit2:
23911 case DW_OP_lit3:
23912 case DW_OP_lit4:
23913 case DW_OP_lit5:
23914 case DW_OP_lit6:
23915 case DW_OP_lit7:
23916 case DW_OP_lit8:
23917 case DW_OP_lit9:
23918 case DW_OP_lit10:
23919 case DW_OP_lit11:
23920 case DW_OP_lit12:
23921 case DW_OP_lit13:
23922 case DW_OP_lit14:
23923 case DW_OP_lit15:
23924 case DW_OP_lit16:
23925 case DW_OP_lit17:
23926 case DW_OP_lit18:
23927 case DW_OP_lit19:
23928 case DW_OP_lit20:
23929 case DW_OP_lit21:
23930 case DW_OP_lit22:
23931 case DW_OP_lit23:
23932 case DW_OP_lit24:
23933 case DW_OP_lit25:
23934 case DW_OP_lit26:
23935 case DW_OP_lit27:
23936 case DW_OP_lit28:
23937 case DW_OP_lit29:
23938 case DW_OP_lit30:
23939 case DW_OP_lit31:
23940 stack[++stacki] = op - DW_OP_lit0;
23941 break;
23942
23943 case DW_OP_reg0:
23944 case DW_OP_reg1:
23945 case DW_OP_reg2:
23946 case DW_OP_reg3:
23947 case DW_OP_reg4:
23948 case DW_OP_reg5:
23949 case DW_OP_reg6:
23950 case DW_OP_reg7:
23951 case DW_OP_reg8:
23952 case DW_OP_reg9:
23953 case DW_OP_reg10:
23954 case DW_OP_reg11:
23955 case DW_OP_reg12:
23956 case DW_OP_reg13:
23957 case DW_OP_reg14:
23958 case DW_OP_reg15:
23959 case DW_OP_reg16:
23960 case DW_OP_reg17:
23961 case DW_OP_reg18:
23962 case DW_OP_reg19:
23963 case DW_OP_reg20:
23964 case DW_OP_reg21:
23965 case DW_OP_reg22:
23966 case DW_OP_reg23:
23967 case DW_OP_reg24:
23968 case DW_OP_reg25:
23969 case DW_OP_reg26:
23970 case DW_OP_reg27:
23971 case DW_OP_reg28:
23972 case DW_OP_reg29:
23973 case DW_OP_reg30:
23974 case DW_OP_reg31:
23975 stack[++stacki] = op - DW_OP_reg0;
23976 if (i < size)
23977 dwarf2_complex_location_expr_complaint ();
23978 break;
23979
23980 case DW_OP_regx:
23981 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23982 i += bytes_read;
23983 stack[++stacki] = unsnd;
23984 if (i < size)
23985 dwarf2_complex_location_expr_complaint ();
23986 break;
23987
23988 case DW_OP_addr:
23989 stack[++stacki] = read_address (objfile->obfd, &data[i],
23990 cu, &bytes_read);
23991 i += bytes_read;
23992 break;
23993
23994 case DW_OP_const1u:
23995 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23996 i += 1;
23997 break;
23998
23999 case DW_OP_const1s:
24000 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24001 i += 1;
24002 break;
24003
24004 case DW_OP_const2u:
24005 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24006 i += 2;
24007 break;
24008
24009 case DW_OP_const2s:
24010 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24011 i += 2;
24012 break;
24013
24014 case DW_OP_const4u:
24015 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24016 i += 4;
24017 break;
24018
24019 case DW_OP_const4s:
24020 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24021 i += 4;
24022 break;
24023
24024 case DW_OP_const8u:
24025 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24026 i += 8;
24027 break;
24028
24029 case DW_OP_constu:
24030 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24031 &bytes_read);
24032 i += bytes_read;
24033 break;
24034
24035 case DW_OP_consts:
24036 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24037 i += bytes_read;
24038 break;
24039
24040 case DW_OP_dup:
24041 stack[stacki + 1] = stack[stacki];
24042 stacki++;
24043 break;
24044
24045 case DW_OP_plus:
24046 stack[stacki - 1] += stack[stacki];
24047 stacki--;
24048 break;
24049
24050 case DW_OP_plus_uconst:
24051 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24052 &bytes_read);
24053 i += bytes_read;
24054 break;
24055
24056 case DW_OP_minus:
24057 stack[stacki - 1] -= stack[stacki];
24058 stacki--;
24059 break;
24060
24061 case DW_OP_deref:
24062 /* If we're not the last op, then we definitely can't encode
24063 this using GDB's address_class enum. This is valid for partial
24064 global symbols, although the variable's address will be bogus
24065 in the psymtab. */
24066 if (i < size)
24067 dwarf2_complex_location_expr_complaint ();
24068 break;
24069
24070 case DW_OP_GNU_push_tls_address:
24071 case DW_OP_form_tls_address:
24072 /* The top of the stack has the offset from the beginning
24073 of the thread control block at which the variable is located. */
24074 /* Nothing should follow this operator, so the top of stack would
24075 be returned. */
24076 /* This is valid for partial global symbols, but the variable's
24077 address will be bogus in the psymtab. Make it always at least
24078 non-zero to not look as a variable garbage collected by linker
24079 which have DW_OP_addr 0. */
24080 if (i < size)
24081 dwarf2_complex_location_expr_complaint ();
24082 stack[stacki]++;
24083 break;
24084
24085 case DW_OP_GNU_uninit:
24086 break;
24087
24088 case DW_OP_addrx:
24089 case DW_OP_GNU_addr_index:
24090 case DW_OP_GNU_const_index:
24091 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24092 &bytes_read);
24093 i += bytes_read;
24094 break;
24095
24096 default:
24097 {
24098 const char *name = get_DW_OP_name (op);
24099
24100 if (name)
24101 complaint (_("unsupported stack op: '%s'"),
24102 name);
24103 else
24104 complaint (_("unsupported stack op: '%02x'"),
24105 op);
24106 }
24107
24108 return (stack[stacki]);
24109 }
24110
24111 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24112 outside of the allocated space. Also enforce minimum>0. */
24113 if (stacki >= ARRAY_SIZE (stack) - 1)
24114 {
24115 complaint (_("location description stack overflow"));
24116 return 0;
24117 }
24118
24119 if (stacki <= 0)
24120 {
24121 complaint (_("location description stack underflow"));
24122 return 0;
24123 }
24124 }
24125 return (stack[stacki]);
24126 }
24127
24128 /* memory allocation interface */
24129
24130 static struct dwarf_block *
24131 dwarf_alloc_block (struct dwarf2_cu *cu)
24132 {
24133 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24134 }
24135
24136 static struct die_info *
24137 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24138 {
24139 struct die_info *die;
24140 size_t size = sizeof (struct die_info);
24141
24142 if (num_attrs > 1)
24143 size += (num_attrs - 1) * sizeof (struct attribute);
24144
24145 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24146 memset (die, 0, sizeof (struct die_info));
24147 return (die);
24148 }
24149
24150 \f
24151 /* Macro support. */
24152
24153 /* Return file name relative to the compilation directory of file number I in
24154 *LH's file name table. The result is allocated using xmalloc; the caller is
24155 responsible for freeing it. */
24156
24157 static char *
24158 file_file_name (int file, struct line_header *lh)
24159 {
24160 /* Is the file number a valid index into the line header's file name
24161 table? Remember that file numbers start with one, not zero. */
24162 if (1 <= file && file <= lh->file_names.size ())
24163 {
24164 const file_entry &fe = lh->file_names[file - 1];
24165
24166 if (!IS_ABSOLUTE_PATH (fe.name))
24167 {
24168 const char *dir = fe.include_dir (lh);
24169 if (dir != NULL)
24170 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
24171 }
24172 return xstrdup (fe.name);
24173 }
24174 else
24175 {
24176 /* The compiler produced a bogus file number. We can at least
24177 record the macro definitions made in the file, even if we
24178 won't be able to find the file by name. */
24179 char fake_name[80];
24180
24181 xsnprintf (fake_name, sizeof (fake_name),
24182 "<bad macro file number %d>", file);
24183
24184 complaint (_("bad file number in macro information (%d)"),
24185 file);
24186
24187 return xstrdup (fake_name);
24188 }
24189 }
24190
24191 /* Return the full name of file number I in *LH's file name table.
24192 Use COMP_DIR as the name of the current directory of the
24193 compilation. The result is allocated using xmalloc; the caller is
24194 responsible for freeing it. */
24195 static char *
24196 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24197 {
24198 /* Is the file number a valid index into the line header's file name
24199 table? Remember that file numbers start with one, not zero. */
24200 if (1 <= file && file <= lh->file_names.size ())
24201 {
24202 char *relative = file_file_name (file, lh);
24203
24204 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24205 return relative;
24206 return reconcat (relative, comp_dir, SLASH_STRING,
24207 relative, (char *) NULL);
24208 }
24209 else
24210 return file_file_name (file, lh);
24211 }
24212
24213
24214 static struct macro_source_file *
24215 macro_start_file (struct dwarf2_cu *cu,
24216 int file, int line,
24217 struct macro_source_file *current_file,
24218 struct line_header *lh)
24219 {
24220 /* File name relative to the compilation directory of this source file. */
24221 char *file_name = file_file_name (file, lh);
24222
24223 if (! current_file)
24224 {
24225 /* Note: We don't create a macro table for this compilation unit
24226 at all until we actually get a filename. */
24227 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
24228
24229 /* If we have no current file, then this must be the start_file
24230 directive for the compilation unit's main source file. */
24231 current_file = macro_set_main (macro_table, file_name);
24232 macro_define_special (macro_table);
24233 }
24234 else
24235 current_file = macro_include (current_file, line, file_name);
24236
24237 xfree (file_name);
24238
24239 return current_file;
24240 }
24241
24242 static const char *
24243 consume_improper_spaces (const char *p, const char *body)
24244 {
24245 if (*p == ' ')
24246 {
24247 complaint (_("macro definition contains spaces "
24248 "in formal argument list:\n`%s'"),
24249 body);
24250
24251 while (*p == ' ')
24252 p++;
24253 }
24254
24255 return p;
24256 }
24257
24258
24259 static void
24260 parse_macro_definition (struct macro_source_file *file, int line,
24261 const char *body)
24262 {
24263 const char *p;
24264
24265 /* The body string takes one of two forms. For object-like macro
24266 definitions, it should be:
24267
24268 <macro name> " " <definition>
24269
24270 For function-like macro definitions, it should be:
24271
24272 <macro name> "() " <definition>
24273 or
24274 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24275
24276 Spaces may appear only where explicitly indicated, and in the
24277 <definition>.
24278
24279 The Dwarf 2 spec says that an object-like macro's name is always
24280 followed by a space, but versions of GCC around March 2002 omit
24281 the space when the macro's definition is the empty string.
24282
24283 The Dwarf 2 spec says that there should be no spaces between the
24284 formal arguments in a function-like macro's formal argument list,
24285 but versions of GCC around March 2002 include spaces after the
24286 commas. */
24287
24288
24289 /* Find the extent of the macro name. The macro name is terminated
24290 by either a space or null character (for an object-like macro) or
24291 an opening paren (for a function-like macro). */
24292 for (p = body; *p; p++)
24293 if (*p == ' ' || *p == '(')
24294 break;
24295
24296 if (*p == ' ' || *p == '\0')
24297 {
24298 /* It's an object-like macro. */
24299 int name_len = p - body;
24300 char *name = savestring (body, name_len);
24301 const char *replacement;
24302
24303 if (*p == ' ')
24304 replacement = body + name_len + 1;
24305 else
24306 {
24307 dwarf2_macro_malformed_definition_complaint (body);
24308 replacement = body + name_len;
24309 }
24310
24311 macro_define_object (file, line, name, replacement);
24312
24313 xfree (name);
24314 }
24315 else if (*p == '(')
24316 {
24317 /* It's a function-like macro. */
24318 char *name = savestring (body, p - body);
24319 int argc = 0;
24320 int argv_size = 1;
24321 char **argv = XNEWVEC (char *, argv_size);
24322
24323 p++;
24324
24325 p = consume_improper_spaces (p, body);
24326
24327 /* Parse the formal argument list. */
24328 while (*p && *p != ')')
24329 {
24330 /* Find the extent of the current argument name. */
24331 const char *arg_start = p;
24332
24333 while (*p && *p != ',' && *p != ')' && *p != ' ')
24334 p++;
24335
24336 if (! *p || p == arg_start)
24337 dwarf2_macro_malformed_definition_complaint (body);
24338 else
24339 {
24340 /* Make sure argv has room for the new argument. */
24341 if (argc >= argv_size)
24342 {
24343 argv_size *= 2;
24344 argv = XRESIZEVEC (char *, argv, argv_size);
24345 }
24346
24347 argv[argc++] = savestring (arg_start, p - arg_start);
24348 }
24349
24350 p = consume_improper_spaces (p, body);
24351
24352 /* Consume the comma, if present. */
24353 if (*p == ',')
24354 {
24355 p++;
24356
24357 p = consume_improper_spaces (p, body);
24358 }
24359 }
24360
24361 if (*p == ')')
24362 {
24363 p++;
24364
24365 if (*p == ' ')
24366 /* Perfectly formed definition, no complaints. */
24367 macro_define_function (file, line, name,
24368 argc, (const char **) argv,
24369 p + 1);
24370 else if (*p == '\0')
24371 {
24372 /* Complain, but do define it. */
24373 dwarf2_macro_malformed_definition_complaint (body);
24374 macro_define_function (file, line, name,
24375 argc, (const char **) argv,
24376 p);
24377 }
24378 else
24379 /* Just complain. */
24380 dwarf2_macro_malformed_definition_complaint (body);
24381 }
24382 else
24383 /* Just complain. */
24384 dwarf2_macro_malformed_definition_complaint (body);
24385
24386 xfree (name);
24387 {
24388 int i;
24389
24390 for (i = 0; i < argc; i++)
24391 xfree (argv[i]);
24392 }
24393 xfree (argv);
24394 }
24395 else
24396 dwarf2_macro_malformed_definition_complaint (body);
24397 }
24398
24399 /* Skip some bytes from BYTES according to the form given in FORM.
24400 Returns the new pointer. */
24401
24402 static const gdb_byte *
24403 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24404 enum dwarf_form form,
24405 unsigned int offset_size,
24406 struct dwarf2_section_info *section)
24407 {
24408 unsigned int bytes_read;
24409
24410 switch (form)
24411 {
24412 case DW_FORM_data1:
24413 case DW_FORM_flag:
24414 ++bytes;
24415 break;
24416
24417 case DW_FORM_data2:
24418 bytes += 2;
24419 break;
24420
24421 case DW_FORM_data4:
24422 bytes += 4;
24423 break;
24424
24425 case DW_FORM_data8:
24426 bytes += 8;
24427 break;
24428
24429 case DW_FORM_data16:
24430 bytes += 16;
24431 break;
24432
24433 case DW_FORM_string:
24434 read_direct_string (abfd, bytes, &bytes_read);
24435 bytes += bytes_read;
24436 break;
24437
24438 case DW_FORM_sec_offset:
24439 case DW_FORM_strp:
24440 case DW_FORM_GNU_strp_alt:
24441 bytes += offset_size;
24442 break;
24443
24444 case DW_FORM_block:
24445 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24446 bytes += bytes_read;
24447 break;
24448
24449 case DW_FORM_block1:
24450 bytes += 1 + read_1_byte (abfd, bytes);
24451 break;
24452 case DW_FORM_block2:
24453 bytes += 2 + read_2_bytes (abfd, bytes);
24454 break;
24455 case DW_FORM_block4:
24456 bytes += 4 + read_4_bytes (abfd, bytes);
24457 break;
24458
24459 case DW_FORM_addrx:
24460 case DW_FORM_sdata:
24461 case DW_FORM_strx:
24462 case DW_FORM_udata:
24463 case DW_FORM_GNU_addr_index:
24464 case DW_FORM_GNU_str_index:
24465 bytes = gdb_skip_leb128 (bytes, buffer_end);
24466 if (bytes == NULL)
24467 {
24468 dwarf2_section_buffer_overflow_complaint (section);
24469 return NULL;
24470 }
24471 break;
24472
24473 case DW_FORM_implicit_const:
24474 break;
24475
24476 default:
24477 {
24478 complaint (_("invalid form 0x%x in `%s'"),
24479 form, get_section_name (section));
24480 return NULL;
24481 }
24482 }
24483
24484 return bytes;
24485 }
24486
24487 /* A helper for dwarf_decode_macros that handles skipping an unknown
24488 opcode. Returns an updated pointer to the macro data buffer; or,
24489 on error, issues a complaint and returns NULL. */
24490
24491 static const gdb_byte *
24492 skip_unknown_opcode (unsigned int opcode,
24493 const gdb_byte **opcode_definitions,
24494 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24495 bfd *abfd,
24496 unsigned int offset_size,
24497 struct dwarf2_section_info *section)
24498 {
24499 unsigned int bytes_read, i;
24500 unsigned long arg;
24501 const gdb_byte *defn;
24502
24503 if (opcode_definitions[opcode] == NULL)
24504 {
24505 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24506 opcode);
24507 return NULL;
24508 }
24509
24510 defn = opcode_definitions[opcode];
24511 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24512 defn += bytes_read;
24513
24514 for (i = 0; i < arg; ++i)
24515 {
24516 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24517 (enum dwarf_form) defn[i], offset_size,
24518 section);
24519 if (mac_ptr == NULL)
24520 {
24521 /* skip_form_bytes already issued the complaint. */
24522 return NULL;
24523 }
24524 }
24525
24526 return mac_ptr;
24527 }
24528
24529 /* A helper function which parses the header of a macro section.
24530 If the macro section is the extended (for now called "GNU") type,
24531 then this updates *OFFSET_SIZE. Returns a pointer to just after
24532 the header, or issues a complaint and returns NULL on error. */
24533
24534 static const gdb_byte *
24535 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24536 bfd *abfd,
24537 const gdb_byte *mac_ptr,
24538 unsigned int *offset_size,
24539 int section_is_gnu)
24540 {
24541 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24542
24543 if (section_is_gnu)
24544 {
24545 unsigned int version, flags;
24546
24547 version = read_2_bytes (abfd, mac_ptr);
24548 if (version != 4 && version != 5)
24549 {
24550 complaint (_("unrecognized version `%d' in .debug_macro section"),
24551 version);
24552 return NULL;
24553 }
24554 mac_ptr += 2;
24555
24556 flags = read_1_byte (abfd, mac_ptr);
24557 ++mac_ptr;
24558 *offset_size = (flags & 1) ? 8 : 4;
24559
24560 if ((flags & 2) != 0)
24561 /* We don't need the line table offset. */
24562 mac_ptr += *offset_size;
24563
24564 /* Vendor opcode descriptions. */
24565 if ((flags & 4) != 0)
24566 {
24567 unsigned int i, count;
24568
24569 count = read_1_byte (abfd, mac_ptr);
24570 ++mac_ptr;
24571 for (i = 0; i < count; ++i)
24572 {
24573 unsigned int opcode, bytes_read;
24574 unsigned long arg;
24575
24576 opcode = read_1_byte (abfd, mac_ptr);
24577 ++mac_ptr;
24578 opcode_definitions[opcode] = mac_ptr;
24579 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24580 mac_ptr += bytes_read;
24581 mac_ptr += arg;
24582 }
24583 }
24584 }
24585
24586 return mac_ptr;
24587 }
24588
24589 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24590 including DW_MACRO_import. */
24591
24592 static void
24593 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24594 bfd *abfd,
24595 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24596 struct macro_source_file *current_file,
24597 struct line_header *lh,
24598 struct dwarf2_section_info *section,
24599 int section_is_gnu, int section_is_dwz,
24600 unsigned int offset_size,
24601 htab_t include_hash)
24602 {
24603 struct dwarf2_per_objfile *dwarf2_per_objfile
24604 = cu->per_cu->dwarf2_per_objfile;
24605 struct objfile *objfile = dwarf2_per_objfile->objfile;
24606 enum dwarf_macro_record_type macinfo_type;
24607 int at_commandline;
24608 const gdb_byte *opcode_definitions[256];
24609
24610 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24611 &offset_size, section_is_gnu);
24612 if (mac_ptr == NULL)
24613 {
24614 /* We already issued a complaint. */
24615 return;
24616 }
24617
24618 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24619 GDB is still reading the definitions from command line. First
24620 DW_MACINFO_start_file will need to be ignored as it was already executed
24621 to create CURRENT_FILE for the main source holding also the command line
24622 definitions. On first met DW_MACINFO_start_file this flag is reset to
24623 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24624
24625 at_commandline = 1;
24626
24627 do
24628 {
24629 /* Do we at least have room for a macinfo type byte? */
24630 if (mac_ptr >= mac_end)
24631 {
24632 dwarf2_section_buffer_overflow_complaint (section);
24633 break;
24634 }
24635
24636 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24637 mac_ptr++;
24638
24639 /* Note that we rely on the fact that the corresponding GNU and
24640 DWARF constants are the same. */
24641 DIAGNOSTIC_PUSH
24642 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24643 switch (macinfo_type)
24644 {
24645 /* A zero macinfo type indicates the end of the macro
24646 information. */
24647 case 0:
24648 break;
24649
24650 case DW_MACRO_define:
24651 case DW_MACRO_undef:
24652 case DW_MACRO_define_strp:
24653 case DW_MACRO_undef_strp:
24654 case DW_MACRO_define_sup:
24655 case DW_MACRO_undef_sup:
24656 {
24657 unsigned int bytes_read;
24658 int line;
24659 const char *body;
24660 int is_define;
24661
24662 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24663 mac_ptr += bytes_read;
24664
24665 if (macinfo_type == DW_MACRO_define
24666 || macinfo_type == DW_MACRO_undef)
24667 {
24668 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24669 mac_ptr += bytes_read;
24670 }
24671 else
24672 {
24673 LONGEST str_offset;
24674
24675 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24676 mac_ptr += offset_size;
24677
24678 if (macinfo_type == DW_MACRO_define_sup
24679 || macinfo_type == DW_MACRO_undef_sup
24680 || section_is_dwz)
24681 {
24682 struct dwz_file *dwz
24683 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24684
24685 body = read_indirect_string_from_dwz (objfile,
24686 dwz, str_offset);
24687 }
24688 else
24689 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24690 abfd, str_offset);
24691 }
24692
24693 is_define = (macinfo_type == DW_MACRO_define
24694 || macinfo_type == DW_MACRO_define_strp
24695 || macinfo_type == DW_MACRO_define_sup);
24696 if (! current_file)
24697 {
24698 /* DWARF violation as no main source is present. */
24699 complaint (_("debug info with no main source gives macro %s "
24700 "on line %d: %s"),
24701 is_define ? _("definition") : _("undefinition"),
24702 line, body);
24703 break;
24704 }
24705 if ((line == 0 && !at_commandline)
24706 || (line != 0 && at_commandline))
24707 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
24708 at_commandline ? _("command-line") : _("in-file"),
24709 is_define ? _("definition") : _("undefinition"),
24710 line == 0 ? _("zero") : _("non-zero"), line, body);
24711
24712 if (body == NULL)
24713 {
24714 /* Fedora's rpm-build's "debugedit" binary
24715 corrupted .debug_macro sections.
24716
24717 For more info, see
24718 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24719 complaint (_("debug info gives %s invalid macro %s "
24720 "without body (corrupted?) at line %d "
24721 "on file %s"),
24722 at_commandline ? _("command-line") : _("in-file"),
24723 is_define ? _("definition") : _("undefinition"),
24724 line, current_file->filename);
24725 }
24726 else if (is_define)
24727 parse_macro_definition (current_file, line, body);
24728 else
24729 {
24730 gdb_assert (macinfo_type == DW_MACRO_undef
24731 || macinfo_type == DW_MACRO_undef_strp
24732 || macinfo_type == DW_MACRO_undef_sup);
24733 macro_undef (current_file, line, body);
24734 }
24735 }
24736 break;
24737
24738 case DW_MACRO_start_file:
24739 {
24740 unsigned int bytes_read;
24741 int line, file;
24742
24743 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24744 mac_ptr += bytes_read;
24745 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24746 mac_ptr += bytes_read;
24747
24748 if ((line == 0 && !at_commandline)
24749 || (line != 0 && at_commandline))
24750 complaint (_("debug info gives source %d included "
24751 "from %s at %s line %d"),
24752 file, at_commandline ? _("command-line") : _("file"),
24753 line == 0 ? _("zero") : _("non-zero"), line);
24754
24755 if (at_commandline)
24756 {
24757 /* This DW_MACRO_start_file was executed in the
24758 pass one. */
24759 at_commandline = 0;
24760 }
24761 else
24762 current_file = macro_start_file (cu, file, line, current_file,
24763 lh);
24764 }
24765 break;
24766
24767 case DW_MACRO_end_file:
24768 if (! current_file)
24769 complaint (_("macro debug info has an unmatched "
24770 "`close_file' directive"));
24771 else
24772 {
24773 current_file = current_file->included_by;
24774 if (! current_file)
24775 {
24776 enum dwarf_macro_record_type next_type;
24777
24778 /* GCC circa March 2002 doesn't produce the zero
24779 type byte marking the end of the compilation
24780 unit. Complain if it's not there, but exit no
24781 matter what. */
24782
24783 /* Do we at least have room for a macinfo type byte? */
24784 if (mac_ptr >= mac_end)
24785 {
24786 dwarf2_section_buffer_overflow_complaint (section);
24787 return;
24788 }
24789
24790 /* We don't increment mac_ptr here, so this is just
24791 a look-ahead. */
24792 next_type
24793 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24794 mac_ptr);
24795 if (next_type != 0)
24796 complaint (_("no terminating 0-type entry for "
24797 "macros in `.debug_macinfo' section"));
24798
24799 return;
24800 }
24801 }
24802 break;
24803
24804 case DW_MACRO_import:
24805 case DW_MACRO_import_sup:
24806 {
24807 LONGEST offset;
24808 void **slot;
24809 bfd *include_bfd = abfd;
24810 struct dwarf2_section_info *include_section = section;
24811 const gdb_byte *include_mac_end = mac_end;
24812 int is_dwz = section_is_dwz;
24813 const gdb_byte *new_mac_ptr;
24814
24815 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24816 mac_ptr += offset_size;
24817
24818 if (macinfo_type == DW_MACRO_import_sup)
24819 {
24820 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24821
24822 dwarf2_read_section (objfile, &dwz->macro);
24823
24824 include_section = &dwz->macro;
24825 include_bfd = get_section_bfd_owner (include_section);
24826 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24827 is_dwz = 1;
24828 }
24829
24830 new_mac_ptr = include_section->buffer + offset;
24831 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24832
24833 if (*slot != NULL)
24834 {
24835 /* This has actually happened; see
24836 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24837 complaint (_("recursive DW_MACRO_import in "
24838 ".debug_macro section"));
24839 }
24840 else
24841 {
24842 *slot = (void *) new_mac_ptr;
24843
24844 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
24845 include_mac_end, current_file, lh,
24846 section, section_is_gnu, is_dwz,
24847 offset_size, include_hash);
24848
24849 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24850 }
24851 }
24852 break;
24853
24854 case DW_MACINFO_vendor_ext:
24855 if (!section_is_gnu)
24856 {
24857 unsigned int bytes_read;
24858
24859 /* This reads the constant, but since we don't recognize
24860 any vendor extensions, we ignore it. */
24861 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24862 mac_ptr += bytes_read;
24863 read_direct_string (abfd, mac_ptr, &bytes_read);
24864 mac_ptr += bytes_read;
24865
24866 /* We don't recognize any vendor extensions. */
24867 break;
24868 }
24869 /* FALLTHROUGH */
24870
24871 default:
24872 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24873 mac_ptr, mac_end, abfd, offset_size,
24874 section);
24875 if (mac_ptr == NULL)
24876 return;
24877 break;
24878 }
24879 DIAGNOSTIC_POP
24880 } while (macinfo_type != 0);
24881 }
24882
24883 static void
24884 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24885 int section_is_gnu)
24886 {
24887 struct dwarf2_per_objfile *dwarf2_per_objfile
24888 = cu->per_cu->dwarf2_per_objfile;
24889 struct objfile *objfile = dwarf2_per_objfile->objfile;
24890 struct line_header *lh = cu->line_header;
24891 bfd *abfd;
24892 const gdb_byte *mac_ptr, *mac_end;
24893 struct macro_source_file *current_file = 0;
24894 enum dwarf_macro_record_type macinfo_type;
24895 unsigned int offset_size = cu->header.offset_size;
24896 const gdb_byte *opcode_definitions[256];
24897 void **slot;
24898 struct dwarf2_section_info *section;
24899 const char *section_name;
24900
24901 if (cu->dwo_unit != NULL)
24902 {
24903 if (section_is_gnu)
24904 {
24905 section = &cu->dwo_unit->dwo_file->sections.macro;
24906 section_name = ".debug_macro.dwo";
24907 }
24908 else
24909 {
24910 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24911 section_name = ".debug_macinfo.dwo";
24912 }
24913 }
24914 else
24915 {
24916 if (section_is_gnu)
24917 {
24918 section = &dwarf2_per_objfile->macro;
24919 section_name = ".debug_macro";
24920 }
24921 else
24922 {
24923 section = &dwarf2_per_objfile->macinfo;
24924 section_name = ".debug_macinfo";
24925 }
24926 }
24927
24928 dwarf2_read_section (objfile, section);
24929 if (section->buffer == NULL)
24930 {
24931 complaint (_("missing %s section"), section_name);
24932 return;
24933 }
24934 abfd = get_section_bfd_owner (section);
24935
24936 /* First pass: Find the name of the base filename.
24937 This filename is needed in order to process all macros whose definition
24938 (or undefinition) comes from the command line. These macros are defined
24939 before the first DW_MACINFO_start_file entry, and yet still need to be
24940 associated to the base file.
24941
24942 To determine the base file name, we scan the macro definitions until we
24943 reach the first DW_MACINFO_start_file entry. We then initialize
24944 CURRENT_FILE accordingly so that any macro definition found before the
24945 first DW_MACINFO_start_file can still be associated to the base file. */
24946
24947 mac_ptr = section->buffer + offset;
24948 mac_end = section->buffer + section->size;
24949
24950 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24951 &offset_size, section_is_gnu);
24952 if (mac_ptr == NULL)
24953 {
24954 /* We already issued a complaint. */
24955 return;
24956 }
24957
24958 do
24959 {
24960 /* Do we at least have room for a macinfo type byte? */
24961 if (mac_ptr >= mac_end)
24962 {
24963 /* Complaint is printed during the second pass as GDB will probably
24964 stop the first pass earlier upon finding
24965 DW_MACINFO_start_file. */
24966 break;
24967 }
24968
24969 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24970 mac_ptr++;
24971
24972 /* Note that we rely on the fact that the corresponding GNU and
24973 DWARF constants are the same. */
24974 DIAGNOSTIC_PUSH
24975 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24976 switch (macinfo_type)
24977 {
24978 /* A zero macinfo type indicates the end of the macro
24979 information. */
24980 case 0:
24981 break;
24982
24983 case DW_MACRO_define:
24984 case DW_MACRO_undef:
24985 /* Only skip the data by MAC_PTR. */
24986 {
24987 unsigned int bytes_read;
24988
24989 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24990 mac_ptr += bytes_read;
24991 read_direct_string (abfd, mac_ptr, &bytes_read);
24992 mac_ptr += bytes_read;
24993 }
24994 break;
24995
24996 case DW_MACRO_start_file:
24997 {
24998 unsigned int bytes_read;
24999 int line, file;
25000
25001 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25002 mac_ptr += bytes_read;
25003 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25004 mac_ptr += bytes_read;
25005
25006 current_file = macro_start_file (cu, file, line, current_file, lh);
25007 }
25008 break;
25009
25010 case DW_MACRO_end_file:
25011 /* No data to skip by MAC_PTR. */
25012 break;
25013
25014 case DW_MACRO_define_strp:
25015 case DW_MACRO_undef_strp:
25016 case DW_MACRO_define_sup:
25017 case DW_MACRO_undef_sup:
25018 {
25019 unsigned int bytes_read;
25020
25021 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25022 mac_ptr += bytes_read;
25023 mac_ptr += offset_size;
25024 }
25025 break;
25026
25027 case DW_MACRO_import:
25028 case DW_MACRO_import_sup:
25029 /* Note that, according to the spec, a transparent include
25030 chain cannot call DW_MACRO_start_file. So, we can just
25031 skip this opcode. */
25032 mac_ptr += offset_size;
25033 break;
25034
25035 case DW_MACINFO_vendor_ext:
25036 /* Only skip the data by MAC_PTR. */
25037 if (!section_is_gnu)
25038 {
25039 unsigned int bytes_read;
25040
25041 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25042 mac_ptr += bytes_read;
25043 read_direct_string (abfd, mac_ptr, &bytes_read);
25044 mac_ptr += bytes_read;
25045 }
25046 /* FALLTHROUGH */
25047
25048 default:
25049 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25050 mac_ptr, mac_end, abfd, offset_size,
25051 section);
25052 if (mac_ptr == NULL)
25053 return;
25054 break;
25055 }
25056 DIAGNOSTIC_POP
25057 } while (macinfo_type != 0 && current_file == NULL);
25058
25059 /* Second pass: Process all entries.
25060
25061 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25062 command-line macro definitions/undefinitions. This flag is unset when we
25063 reach the first DW_MACINFO_start_file entry. */
25064
25065 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25066 htab_eq_pointer,
25067 NULL, xcalloc, xfree));
25068 mac_ptr = section->buffer + offset;
25069 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
25070 *slot = (void *) mac_ptr;
25071 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
25072 current_file, lh, section,
25073 section_is_gnu, 0, offset_size,
25074 include_hash.get ());
25075 }
25076
25077 /* Check if the attribute's form is a DW_FORM_block*
25078 if so return true else false. */
25079
25080 static int
25081 attr_form_is_block (const struct attribute *attr)
25082 {
25083 return (attr == NULL ? 0 :
25084 attr->form == DW_FORM_block1
25085 || attr->form == DW_FORM_block2
25086 || attr->form == DW_FORM_block4
25087 || attr->form == DW_FORM_block
25088 || attr->form == DW_FORM_exprloc);
25089 }
25090
25091 /* Return non-zero if ATTR's value is a section offset --- classes
25092 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25093 You may use DW_UNSND (attr) to retrieve such offsets.
25094
25095 Section 7.5.4, "Attribute Encodings", explains that no attribute
25096 may have a value that belongs to more than one of these classes; it
25097 would be ambiguous if we did, because we use the same forms for all
25098 of them. */
25099
25100 static int
25101 attr_form_is_section_offset (const struct attribute *attr)
25102 {
25103 return (attr->form == DW_FORM_data4
25104 || attr->form == DW_FORM_data8
25105 || attr->form == DW_FORM_sec_offset);
25106 }
25107
25108 /* Return non-zero if ATTR's value falls in the 'constant' class, or
25109 zero otherwise. When this function returns true, you can apply
25110 dwarf2_get_attr_constant_value to it.
25111
25112 However, note that for some attributes you must check
25113 attr_form_is_section_offset before using this test. DW_FORM_data4
25114 and DW_FORM_data8 are members of both the constant class, and of
25115 the classes that contain offsets into other debug sections
25116 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25117 that, if an attribute's can be either a constant or one of the
25118 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25119 taken as section offsets, not constants.
25120
25121 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25122 cannot handle that. */
25123
25124 static int
25125 attr_form_is_constant (const struct attribute *attr)
25126 {
25127 switch (attr->form)
25128 {
25129 case DW_FORM_sdata:
25130 case DW_FORM_udata:
25131 case DW_FORM_data1:
25132 case DW_FORM_data2:
25133 case DW_FORM_data4:
25134 case DW_FORM_data8:
25135 case DW_FORM_implicit_const:
25136 return 1;
25137 default:
25138 return 0;
25139 }
25140 }
25141
25142
25143 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25144 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25145
25146 static int
25147 attr_form_is_ref (const struct attribute *attr)
25148 {
25149 switch (attr->form)
25150 {
25151 case DW_FORM_ref_addr:
25152 case DW_FORM_ref1:
25153 case DW_FORM_ref2:
25154 case DW_FORM_ref4:
25155 case DW_FORM_ref8:
25156 case DW_FORM_ref_udata:
25157 case DW_FORM_GNU_ref_alt:
25158 return 1;
25159 default:
25160 return 0;
25161 }
25162 }
25163
25164 /* Return the .debug_loc section to use for CU.
25165 For DWO files use .debug_loc.dwo. */
25166
25167 static struct dwarf2_section_info *
25168 cu_debug_loc_section (struct dwarf2_cu *cu)
25169 {
25170 struct dwarf2_per_objfile *dwarf2_per_objfile
25171 = cu->per_cu->dwarf2_per_objfile;
25172
25173 if (cu->dwo_unit)
25174 {
25175 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25176
25177 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25178 }
25179 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25180 : &dwarf2_per_objfile->loc);
25181 }
25182
25183 /* A helper function that fills in a dwarf2_loclist_baton. */
25184
25185 static void
25186 fill_in_loclist_baton (struct dwarf2_cu *cu,
25187 struct dwarf2_loclist_baton *baton,
25188 const struct attribute *attr)
25189 {
25190 struct dwarf2_per_objfile *dwarf2_per_objfile
25191 = cu->per_cu->dwarf2_per_objfile;
25192 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25193
25194 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25195
25196 baton->per_cu = cu->per_cu;
25197 gdb_assert (baton->per_cu);
25198 /* We don't know how long the location list is, but make sure we
25199 don't run off the edge of the section. */
25200 baton->size = section->size - DW_UNSND (attr);
25201 baton->data = section->buffer + DW_UNSND (attr);
25202 baton->base_address = cu->base_address;
25203 baton->from_dwo = cu->dwo_unit != NULL;
25204 }
25205
25206 static void
25207 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25208 struct dwarf2_cu *cu, int is_block)
25209 {
25210 struct dwarf2_per_objfile *dwarf2_per_objfile
25211 = cu->per_cu->dwarf2_per_objfile;
25212 struct objfile *objfile = dwarf2_per_objfile->objfile;
25213 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25214
25215 if (attr_form_is_section_offset (attr)
25216 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25217 the section. If so, fall through to the complaint in the
25218 other branch. */
25219 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25220 {
25221 struct dwarf2_loclist_baton *baton;
25222
25223 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25224
25225 fill_in_loclist_baton (cu, baton, attr);
25226
25227 if (cu->base_known == 0)
25228 complaint (_("Location list used without "
25229 "specifying the CU base address."));
25230
25231 SYMBOL_ACLASS_INDEX (sym) = (is_block
25232 ? dwarf2_loclist_block_index
25233 : dwarf2_loclist_index);
25234 SYMBOL_LOCATION_BATON (sym) = baton;
25235 }
25236 else
25237 {
25238 struct dwarf2_locexpr_baton *baton;
25239
25240 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25241 baton->per_cu = cu->per_cu;
25242 gdb_assert (baton->per_cu);
25243
25244 if (attr_form_is_block (attr))
25245 {
25246 /* Note that we're just copying the block's data pointer
25247 here, not the actual data. We're still pointing into the
25248 info_buffer for SYM's objfile; right now we never release
25249 that buffer, but when we do clean up properly this may
25250 need to change. */
25251 baton->size = DW_BLOCK (attr)->size;
25252 baton->data = DW_BLOCK (attr)->data;
25253 }
25254 else
25255 {
25256 dwarf2_invalid_attrib_class_complaint ("location description",
25257 SYMBOL_NATURAL_NAME (sym));
25258 baton->size = 0;
25259 }
25260
25261 SYMBOL_ACLASS_INDEX (sym) = (is_block
25262 ? dwarf2_locexpr_block_index
25263 : dwarf2_locexpr_index);
25264 SYMBOL_LOCATION_BATON (sym) = baton;
25265 }
25266 }
25267
25268 /* Return the OBJFILE associated with the compilation unit CU. If CU
25269 came from a separate debuginfo file, then the master objfile is
25270 returned. */
25271
25272 struct objfile *
25273 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25274 {
25275 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25276
25277 /* Return the master objfile, so that we can report and look up the
25278 correct file containing this variable. */
25279 if (objfile->separate_debug_objfile_backlink)
25280 objfile = objfile->separate_debug_objfile_backlink;
25281
25282 return objfile;
25283 }
25284
25285 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25286 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25287 CU_HEADERP first. */
25288
25289 static const struct comp_unit_head *
25290 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25291 struct dwarf2_per_cu_data *per_cu)
25292 {
25293 const gdb_byte *info_ptr;
25294
25295 if (per_cu->cu)
25296 return &per_cu->cu->header;
25297
25298 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25299
25300 memset (cu_headerp, 0, sizeof (*cu_headerp));
25301 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25302 rcuh_kind::COMPILE);
25303
25304 return cu_headerp;
25305 }
25306
25307 /* Return the address size given in the compilation unit header for CU. */
25308
25309 int
25310 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25311 {
25312 struct comp_unit_head cu_header_local;
25313 const struct comp_unit_head *cu_headerp;
25314
25315 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25316
25317 return cu_headerp->addr_size;
25318 }
25319
25320 /* Return the offset size given in the compilation unit header for CU. */
25321
25322 int
25323 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25324 {
25325 struct comp_unit_head cu_header_local;
25326 const struct comp_unit_head *cu_headerp;
25327
25328 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25329
25330 return cu_headerp->offset_size;
25331 }
25332
25333 /* See its dwarf2loc.h declaration. */
25334
25335 int
25336 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25337 {
25338 struct comp_unit_head cu_header_local;
25339 const struct comp_unit_head *cu_headerp;
25340
25341 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25342
25343 if (cu_headerp->version == 2)
25344 return cu_headerp->addr_size;
25345 else
25346 return cu_headerp->offset_size;
25347 }
25348
25349 /* Return the text offset of the CU. The returned offset comes from
25350 this CU's objfile. If this objfile came from a separate debuginfo
25351 file, then the offset may be different from the corresponding
25352 offset in the parent objfile. */
25353
25354 CORE_ADDR
25355 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25356 {
25357 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25358
25359 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25360 }
25361
25362 /* Return a type that is a generic pointer type, the size of which matches
25363 the address size given in the compilation unit header for PER_CU. */
25364 static struct type *
25365 dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25366 {
25367 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25368 struct type *void_type = objfile_type (objfile)->builtin_void;
25369 struct type *addr_type = lookup_pointer_type (void_type);
25370 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25371
25372 if (TYPE_LENGTH (addr_type) == addr_size)
25373 return addr_type;
25374
25375 addr_type
25376 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25377 return addr_type;
25378 }
25379
25380 /* Return DWARF version number of PER_CU. */
25381
25382 short
25383 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25384 {
25385 return per_cu->dwarf_version;
25386 }
25387
25388 /* Locate the .debug_info compilation unit from CU's objfile which contains
25389 the DIE at OFFSET. Raises an error on failure. */
25390
25391 static struct dwarf2_per_cu_data *
25392 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25393 unsigned int offset_in_dwz,
25394 struct dwarf2_per_objfile *dwarf2_per_objfile)
25395 {
25396 struct dwarf2_per_cu_data *this_cu;
25397 int low, high;
25398
25399 low = 0;
25400 high = dwarf2_per_objfile->all_comp_units.size () - 1;
25401 while (high > low)
25402 {
25403 struct dwarf2_per_cu_data *mid_cu;
25404 int mid = low + (high - low) / 2;
25405
25406 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25407 if (mid_cu->is_dwz > offset_in_dwz
25408 || (mid_cu->is_dwz == offset_in_dwz
25409 && mid_cu->sect_off + mid_cu->length >= sect_off))
25410 high = mid;
25411 else
25412 low = mid + 1;
25413 }
25414 gdb_assert (low == high);
25415 this_cu = dwarf2_per_objfile->all_comp_units[low];
25416 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25417 {
25418 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25419 error (_("Dwarf Error: could not find partial DIE containing "
25420 "offset %s [in module %s]"),
25421 sect_offset_str (sect_off),
25422 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25423
25424 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25425 <= sect_off);
25426 return dwarf2_per_objfile->all_comp_units[low-1];
25427 }
25428 else
25429 {
25430 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25431 && sect_off >= this_cu->sect_off + this_cu->length)
25432 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25433 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25434 return this_cu;
25435 }
25436 }
25437
25438 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25439
25440 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25441 : per_cu (per_cu_),
25442 mark (false),
25443 has_loclist (false),
25444 checked_producer (false),
25445 producer_is_gxx_lt_4_6 (false),
25446 producer_is_gcc_lt_4_3 (false),
25447 producer_is_icc (false),
25448 producer_is_icc_lt_14 (false),
25449 producer_is_codewarrior (false),
25450 processing_has_namespace_info (false)
25451 {
25452 per_cu->cu = this;
25453 }
25454
25455 /* Destroy a dwarf2_cu. */
25456
25457 dwarf2_cu::~dwarf2_cu ()
25458 {
25459 per_cu->cu = NULL;
25460 }
25461
25462 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25463
25464 static void
25465 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25466 enum language pretend_language)
25467 {
25468 struct attribute *attr;
25469
25470 /* Set the language we're debugging. */
25471 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25472 if (attr)
25473 set_cu_language (DW_UNSND (attr), cu);
25474 else
25475 {
25476 cu->language = pretend_language;
25477 cu->language_defn = language_def (cu->language);
25478 }
25479
25480 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25481 }
25482
25483 /* Increase the age counter on each cached compilation unit, and free
25484 any that are too old. */
25485
25486 static void
25487 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25488 {
25489 struct dwarf2_per_cu_data *per_cu, **last_chain;
25490
25491 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25492 per_cu = dwarf2_per_objfile->read_in_chain;
25493 while (per_cu != NULL)
25494 {
25495 per_cu->cu->last_used ++;
25496 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25497 dwarf2_mark (per_cu->cu);
25498 per_cu = per_cu->cu->read_in_chain;
25499 }
25500
25501 per_cu = dwarf2_per_objfile->read_in_chain;
25502 last_chain = &dwarf2_per_objfile->read_in_chain;
25503 while (per_cu != NULL)
25504 {
25505 struct dwarf2_per_cu_data *next_cu;
25506
25507 next_cu = per_cu->cu->read_in_chain;
25508
25509 if (!per_cu->cu->mark)
25510 {
25511 delete per_cu->cu;
25512 *last_chain = next_cu;
25513 }
25514 else
25515 last_chain = &per_cu->cu->read_in_chain;
25516
25517 per_cu = next_cu;
25518 }
25519 }
25520
25521 /* Remove a single compilation unit from the cache. */
25522
25523 static void
25524 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25525 {
25526 struct dwarf2_per_cu_data *per_cu, **last_chain;
25527 struct dwarf2_per_objfile *dwarf2_per_objfile
25528 = target_per_cu->dwarf2_per_objfile;
25529
25530 per_cu = dwarf2_per_objfile->read_in_chain;
25531 last_chain = &dwarf2_per_objfile->read_in_chain;
25532 while (per_cu != NULL)
25533 {
25534 struct dwarf2_per_cu_data *next_cu;
25535
25536 next_cu = per_cu->cu->read_in_chain;
25537
25538 if (per_cu == target_per_cu)
25539 {
25540 delete per_cu->cu;
25541 per_cu->cu = NULL;
25542 *last_chain = next_cu;
25543 break;
25544 }
25545 else
25546 last_chain = &per_cu->cu->read_in_chain;
25547
25548 per_cu = next_cu;
25549 }
25550 }
25551
25552 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25553 We store these in a hash table separate from the DIEs, and preserve them
25554 when the DIEs are flushed out of cache.
25555
25556 The CU "per_cu" pointer is needed because offset alone is not enough to
25557 uniquely identify the type. A file may have multiple .debug_types sections,
25558 or the type may come from a DWO file. Furthermore, while it's more logical
25559 to use per_cu->section+offset, with Fission the section with the data is in
25560 the DWO file but we don't know that section at the point we need it.
25561 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25562 because we can enter the lookup routine, get_die_type_at_offset, from
25563 outside this file, and thus won't necessarily have PER_CU->cu.
25564 Fortunately, PER_CU is stable for the life of the objfile. */
25565
25566 struct dwarf2_per_cu_offset_and_type
25567 {
25568 const struct dwarf2_per_cu_data *per_cu;
25569 sect_offset sect_off;
25570 struct type *type;
25571 };
25572
25573 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25574
25575 static hashval_t
25576 per_cu_offset_and_type_hash (const void *item)
25577 {
25578 const struct dwarf2_per_cu_offset_and_type *ofs
25579 = (const struct dwarf2_per_cu_offset_and_type *) item;
25580
25581 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25582 }
25583
25584 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25585
25586 static int
25587 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25588 {
25589 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25590 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25591 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25592 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25593
25594 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25595 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25596 }
25597
25598 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25599 table if necessary. For convenience, return TYPE.
25600
25601 The DIEs reading must have careful ordering to:
25602 * Not cause infite loops trying to read in DIEs as a prerequisite for
25603 reading current DIE.
25604 * Not trying to dereference contents of still incompletely read in types
25605 while reading in other DIEs.
25606 * Enable referencing still incompletely read in types just by a pointer to
25607 the type without accessing its fields.
25608
25609 Therefore caller should follow these rules:
25610 * Try to fetch any prerequisite types we may need to build this DIE type
25611 before building the type and calling set_die_type.
25612 * After building type call set_die_type for current DIE as soon as
25613 possible before fetching more types to complete the current type.
25614 * Make the type as complete as possible before fetching more types. */
25615
25616 static struct type *
25617 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25618 {
25619 struct dwarf2_per_objfile *dwarf2_per_objfile
25620 = cu->per_cu->dwarf2_per_objfile;
25621 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25622 struct objfile *objfile = dwarf2_per_objfile->objfile;
25623 struct attribute *attr;
25624 struct dynamic_prop prop;
25625
25626 /* For Ada types, make sure that the gnat-specific data is always
25627 initialized (if not already set). There are a few types where
25628 we should not be doing so, because the type-specific area is
25629 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25630 where the type-specific area is used to store the floatformat).
25631 But this is not a problem, because the gnat-specific information
25632 is actually not needed for these types. */
25633 if (need_gnat_info (cu)
25634 && TYPE_CODE (type) != TYPE_CODE_FUNC
25635 && TYPE_CODE (type) != TYPE_CODE_FLT
25636 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25637 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25638 && TYPE_CODE (type) != TYPE_CODE_METHOD
25639 && !HAVE_GNAT_AUX_INFO (type))
25640 INIT_GNAT_SPECIFIC (type);
25641
25642 /* Read DW_AT_allocated and set in type. */
25643 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25644 if (attr_form_is_block (attr))
25645 {
25646 struct type *prop_type
25647 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25648 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25649 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25650 }
25651 else if (attr != NULL)
25652 {
25653 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25654 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25655 sect_offset_str (die->sect_off));
25656 }
25657
25658 /* Read DW_AT_associated and set in type. */
25659 attr = dwarf2_attr (die, DW_AT_associated, cu);
25660 if (attr_form_is_block (attr))
25661 {
25662 struct type *prop_type
25663 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25664 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25665 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25666 }
25667 else if (attr != NULL)
25668 {
25669 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25670 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25671 sect_offset_str (die->sect_off));
25672 }
25673
25674 /* Read DW_AT_data_location and set in type. */
25675 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25676 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25677 dwarf2_per_cu_addr_type (cu->per_cu)))
25678 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25679
25680 if (dwarf2_per_objfile->die_type_hash == NULL)
25681 {
25682 dwarf2_per_objfile->die_type_hash =
25683 htab_create_alloc_ex (127,
25684 per_cu_offset_and_type_hash,
25685 per_cu_offset_and_type_eq,
25686 NULL,
25687 &objfile->objfile_obstack,
25688 hashtab_obstack_allocate,
25689 dummy_obstack_deallocate);
25690 }
25691
25692 ofs.per_cu = cu->per_cu;
25693 ofs.sect_off = die->sect_off;
25694 ofs.type = type;
25695 slot = (struct dwarf2_per_cu_offset_and_type **)
25696 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25697 if (*slot)
25698 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25699 sect_offset_str (die->sect_off));
25700 *slot = XOBNEW (&objfile->objfile_obstack,
25701 struct dwarf2_per_cu_offset_and_type);
25702 **slot = ofs;
25703 return type;
25704 }
25705
25706 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25707 or return NULL if the die does not have a saved type. */
25708
25709 static struct type *
25710 get_die_type_at_offset (sect_offset sect_off,
25711 struct dwarf2_per_cu_data *per_cu)
25712 {
25713 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25714 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25715
25716 if (dwarf2_per_objfile->die_type_hash == NULL)
25717 return NULL;
25718
25719 ofs.per_cu = per_cu;
25720 ofs.sect_off = sect_off;
25721 slot = ((struct dwarf2_per_cu_offset_and_type *)
25722 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25723 if (slot)
25724 return slot->type;
25725 else
25726 return NULL;
25727 }
25728
25729 /* Look up the type for DIE in CU in die_type_hash,
25730 or return NULL if DIE does not have a saved type. */
25731
25732 static struct type *
25733 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25734 {
25735 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25736 }
25737
25738 /* Add a dependence relationship from CU to REF_PER_CU. */
25739
25740 static void
25741 dwarf2_add_dependence (struct dwarf2_cu *cu,
25742 struct dwarf2_per_cu_data *ref_per_cu)
25743 {
25744 void **slot;
25745
25746 if (cu->dependencies == NULL)
25747 cu->dependencies
25748 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25749 NULL, &cu->comp_unit_obstack,
25750 hashtab_obstack_allocate,
25751 dummy_obstack_deallocate);
25752
25753 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25754 if (*slot == NULL)
25755 *slot = ref_per_cu;
25756 }
25757
25758 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25759 Set the mark field in every compilation unit in the
25760 cache that we must keep because we are keeping CU. */
25761
25762 static int
25763 dwarf2_mark_helper (void **slot, void *data)
25764 {
25765 struct dwarf2_per_cu_data *per_cu;
25766
25767 per_cu = (struct dwarf2_per_cu_data *) *slot;
25768
25769 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25770 reading of the chain. As such dependencies remain valid it is not much
25771 useful to track and undo them during QUIT cleanups. */
25772 if (per_cu->cu == NULL)
25773 return 1;
25774
25775 if (per_cu->cu->mark)
25776 return 1;
25777 per_cu->cu->mark = true;
25778
25779 if (per_cu->cu->dependencies != NULL)
25780 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25781
25782 return 1;
25783 }
25784
25785 /* Set the mark field in CU and in every other compilation unit in the
25786 cache that we must keep because we are keeping CU. */
25787
25788 static void
25789 dwarf2_mark (struct dwarf2_cu *cu)
25790 {
25791 if (cu->mark)
25792 return;
25793 cu->mark = true;
25794 if (cu->dependencies != NULL)
25795 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25796 }
25797
25798 static void
25799 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25800 {
25801 while (per_cu)
25802 {
25803 per_cu->cu->mark = false;
25804 per_cu = per_cu->cu->read_in_chain;
25805 }
25806 }
25807
25808 /* Trivial hash function for partial_die_info: the hash value of a DIE
25809 is its offset in .debug_info for this objfile. */
25810
25811 static hashval_t
25812 partial_die_hash (const void *item)
25813 {
25814 const struct partial_die_info *part_die
25815 = (const struct partial_die_info *) item;
25816
25817 return to_underlying (part_die->sect_off);
25818 }
25819
25820 /* Trivial comparison function for partial_die_info structures: two DIEs
25821 are equal if they have the same offset. */
25822
25823 static int
25824 partial_die_eq (const void *item_lhs, const void *item_rhs)
25825 {
25826 const struct partial_die_info *part_die_lhs
25827 = (const struct partial_die_info *) item_lhs;
25828 const struct partial_die_info *part_die_rhs
25829 = (const struct partial_die_info *) item_rhs;
25830
25831 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25832 }
25833
25834 struct cmd_list_element *set_dwarf_cmdlist;
25835 struct cmd_list_element *show_dwarf_cmdlist;
25836
25837 static void
25838 set_dwarf_cmd (const char *args, int from_tty)
25839 {
25840 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25841 gdb_stdout);
25842 }
25843
25844 static void
25845 show_dwarf_cmd (const char *args, int from_tty)
25846 {
25847 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25848 }
25849
25850 bool dwarf_always_disassemble;
25851
25852 static void
25853 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25854 struct cmd_list_element *c, const char *value)
25855 {
25856 fprintf_filtered (file,
25857 _("Whether to always disassemble "
25858 "DWARF expressions is %s.\n"),
25859 value);
25860 }
25861
25862 static void
25863 show_check_physname (struct ui_file *file, int from_tty,
25864 struct cmd_list_element *c, const char *value)
25865 {
25866 fprintf_filtered (file,
25867 _("Whether to check \"physname\" is %s.\n"),
25868 value);
25869 }
25870
25871 void
25872 _initialize_dwarf2_read (void)
25873 {
25874 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25875 Set DWARF specific variables.\n\
25876 Configure DWARF variables such as the cache size."),
25877 &set_dwarf_cmdlist, "maintenance set dwarf ",
25878 0/*allow-unknown*/, &maintenance_set_cmdlist);
25879
25880 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25881 Show DWARF specific variables.\n\
25882 Show DWARF variables such as the cache size."),
25883 &show_dwarf_cmdlist, "maintenance show dwarf ",
25884 0/*allow-unknown*/, &maintenance_show_cmdlist);
25885
25886 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25887 &dwarf_max_cache_age, _("\
25888 Set the upper bound on the age of cached DWARF compilation units."), _("\
25889 Show the upper bound on the age of cached DWARF compilation units."), _("\
25890 A higher limit means that cached compilation units will be stored\n\
25891 in memory longer, and more total memory will be used. Zero disables\n\
25892 caching, which can slow down startup."),
25893 NULL,
25894 show_dwarf_max_cache_age,
25895 &set_dwarf_cmdlist,
25896 &show_dwarf_cmdlist);
25897
25898 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25899 &dwarf_always_disassemble, _("\
25900 Set whether `info address' always disassembles DWARF expressions."), _("\
25901 Show whether `info address' always disassembles DWARF expressions."), _("\
25902 When enabled, DWARF expressions are always printed in an assembly-like\n\
25903 syntax. When disabled, expressions will be printed in a more\n\
25904 conversational style, when possible."),
25905 NULL,
25906 show_dwarf_always_disassemble,
25907 &set_dwarf_cmdlist,
25908 &show_dwarf_cmdlist);
25909
25910 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25911 Set debugging of the DWARF reader."), _("\
25912 Show debugging of the DWARF reader."), _("\
25913 When enabled (non-zero), debugging messages are printed during DWARF\n\
25914 reading and symtab expansion. A value of 1 (one) provides basic\n\
25915 information. A value greater than 1 provides more verbose information."),
25916 NULL,
25917 NULL,
25918 &setdebuglist, &showdebuglist);
25919
25920 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25921 Set debugging of the DWARF DIE reader."), _("\
25922 Show debugging of the DWARF DIE reader."), _("\
25923 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25924 The value is the maximum depth to print."),
25925 NULL,
25926 NULL,
25927 &setdebuglist, &showdebuglist);
25928
25929 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25930 Set debugging of the dwarf line reader."), _("\
25931 Show debugging of the dwarf line reader."), _("\
25932 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25933 A value of 1 (one) provides basic information.\n\
25934 A value greater than 1 provides more verbose information."),
25935 NULL,
25936 NULL,
25937 &setdebuglist, &showdebuglist);
25938
25939 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25940 Set cross-checking of \"physname\" code against demangler."), _("\
25941 Show cross-checking of \"physname\" code against demangler."), _("\
25942 When enabled, GDB's internal \"physname\" code is checked against\n\
25943 the demangler."),
25944 NULL, show_check_physname,
25945 &setdebuglist, &showdebuglist);
25946
25947 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25948 no_class, &use_deprecated_index_sections, _("\
25949 Set whether to use deprecated gdb_index sections."), _("\
25950 Show whether to use deprecated gdb_index sections."), _("\
25951 When enabled, deprecated .gdb_index sections are used anyway.\n\
25952 Normally they are ignored either because of a missing feature or\n\
25953 performance issue.\n\
25954 Warning: This option must be enabled before gdb reads the file."),
25955 NULL,
25956 NULL,
25957 &setlist, &showlist);
25958
25959 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25960 &dwarf2_locexpr_funcs);
25961 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25962 &dwarf2_loclist_funcs);
25963
25964 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25965 &dwarf2_block_frame_base_locexpr_funcs);
25966 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25967 &dwarf2_block_frame_base_loclist_funcs);
25968
25969 #if GDB_SELF_TEST
25970 selftests::register_test ("dw2_expand_symtabs_matching",
25971 selftests::dw2_expand_symtabs_matching::run_test);
25972 #endif
25973 }
This page took 1.007943 seconds and 5 git commands to generate.