03c27982b184f49fc1f94c623751209a9e0262c8
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2read.h"
33 #include "dwarf-index-cache.h"
34 #include "dwarf-index-common.h"
35 #include "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 "filenames.h" /* for DOSish file names */
45 #include "macrotab.h"
46 #include "language.h"
47 #include "complaints.h"
48 #include "dwarf2expr.h"
49 #include "dwarf2loc.h"
50 #include "cp-support.h"
51 #include "hashtab.h"
52 #include "command.h"
53 #include "gdbcmd.h"
54 #include "block.h"
55 #include "addrmap.h"
56 #include "typeprint.h"
57 #include "psympriv.h"
58 #include "c-lang.h"
59 #include "go-lang.h"
60 #include "valprint.h"
61 #include "gdbcore.h" /* for gnutarget */
62 #include "gdb/gdb-index.h"
63 #include "gdb_bfd.h"
64 #include "f-lang.h"
65 #include "source.h"
66 #include "build-id.h"
67 #include "namespace.h"
68 #include "gdbsupport/function-view.h"
69 #include "gdbsupport/gdb_optional.h"
70 #include "gdbsupport/underlying.h"
71 #include "gdbsupport/hash_enum.h"
72 #include "filename-seen-cache.h"
73 #include "producer.h"
74 #include <fcntl.h>
75 #include <algorithm>
76 #include <unordered_map>
77 #include "gdbsupport/selftest.h"
78 #include "rust-lang.h"
79 #include "gdbsupport/pathstuff.h"
80
81 /* When == 1, print basic high level tracing messages.
82 When > 1, be more verbose.
83 This is in contrast to the low level DIE reading of dwarf_die_debug. */
84 static unsigned int dwarf_read_debug = 0;
85
86 /* When non-zero, dump DIEs after they are read in. */
87 static unsigned int dwarf_die_debug = 0;
88
89 /* When non-zero, dump line number entries as they are read in. */
90 static unsigned int dwarf_line_debug = 0;
91
92 /* When true, cross-check physname against demangler. */
93 static bool check_physname = false;
94
95 /* When true, do not reject deprecated .gdb_index sections. */
96 static bool use_deprecated_index_sections = false;
97
98 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
99
100 /* The "aclass" indices for various kinds of computed DWARF symbols. */
101
102 static int dwarf2_locexpr_index;
103 static int dwarf2_loclist_index;
104 static int dwarf2_locexpr_block_index;
105 static int dwarf2_loclist_block_index;
106
107 /* An index into a (C++) symbol name component in a symbol name as
108 recorded in the mapped_index's symbol table. For each C++ symbol
109 in the symbol table, we record one entry for the start of each
110 component in the symbol in a table of name components, and then
111 sort the table, in order to be able to binary search symbol names,
112 ignoring leading namespaces, both completion and regular look up.
113 For example, for symbol "A::B::C", we'll have an entry that points
114 to "A::B::C", another that points to "B::C", and another for "C".
115 Note that function symbols in GDB index have no parameter
116 information, just the function/method names. You can convert a
117 name_component to a "const char *" using the
118 'mapped_index::symbol_name_at(offset_type)' method. */
119
120 struct name_component
121 {
122 /* Offset in the symbol name where the component starts. Stored as
123 a (32-bit) offset instead of a pointer to save memory and improve
124 locality on 64-bit architectures. */
125 offset_type name_offset;
126
127 /* The symbol's index in the symbol and constant pool tables of a
128 mapped_index. */
129 offset_type idx;
130 };
131
132 /* Base class containing bits shared by both .gdb_index and
133 .debug_name indexes. */
134
135 struct mapped_index_base
136 {
137 mapped_index_base () = default;
138 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
139
140 /* The name_component table (a sorted vector). See name_component's
141 description above. */
142 std::vector<name_component> name_components;
143
144 /* How NAME_COMPONENTS is sorted. */
145 enum case_sensitivity name_components_casing;
146
147 /* Return the number of names in the symbol table. */
148 virtual size_t symbol_name_count () const = 0;
149
150 /* Get the name of the symbol at IDX in the symbol table. */
151 virtual const char *symbol_name_at (offset_type idx) const = 0;
152
153 /* Return whether the name at IDX in the symbol table should be
154 ignored. */
155 virtual bool symbol_name_slot_invalid (offset_type idx) const
156 {
157 return false;
158 }
159
160 /* Build the symbol name component sorted vector, if we haven't
161 yet. */
162 void build_name_components ();
163
164 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
165 possible matches for LN_NO_PARAMS in the name component
166 vector. */
167 std::pair<std::vector<name_component>::const_iterator,
168 std::vector<name_component>::const_iterator>
169 find_name_components_bounds (const lookup_name_info &ln_no_params,
170 enum language lang) const;
171
172 /* Prevent deleting/destroying via a base class pointer. */
173 protected:
174 ~mapped_index_base() = default;
175 };
176
177 /* A description of the mapped index. The file format is described in
178 a comment by the code that writes the index. */
179 struct mapped_index final : public mapped_index_base
180 {
181 /* A slot/bucket in the symbol table hash. */
182 struct symbol_table_slot
183 {
184 const offset_type name;
185 const offset_type vec;
186 };
187
188 /* Index data format version. */
189 int version = 0;
190
191 /* The address table data. */
192 gdb::array_view<const gdb_byte> address_table;
193
194 /* The symbol table, implemented as a hash table. */
195 gdb::array_view<symbol_table_slot> symbol_table;
196
197 /* A pointer to the constant pool. */
198 const char *constant_pool = nullptr;
199
200 bool symbol_name_slot_invalid (offset_type idx) const override
201 {
202 const auto &bucket = this->symbol_table[idx];
203 return bucket.name == 0 && bucket.vec == 0;
204 }
205
206 /* Convenience method to get at the name of the symbol at IDX in the
207 symbol table. */
208 const char *symbol_name_at (offset_type idx) const override
209 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
210
211 size_t symbol_name_count () const override
212 { return this->symbol_table.size (); }
213 };
214
215 /* A description of the mapped .debug_names.
216 Uninitialized map has CU_COUNT 0. */
217 struct mapped_debug_names final : public mapped_index_base
218 {
219 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
220 : dwarf2_per_objfile (dwarf2_per_objfile_)
221 {}
222
223 struct dwarf2_per_objfile *dwarf2_per_objfile;
224 bfd_endian dwarf5_byte_order;
225 bool dwarf5_is_dwarf64;
226 bool augmentation_is_gdb;
227 uint8_t offset_size;
228 uint32_t cu_count = 0;
229 uint32_t tu_count, bucket_count, name_count;
230 const gdb_byte *cu_table_reordered, *tu_table_reordered;
231 const uint32_t *bucket_table_reordered, *hash_table_reordered;
232 const gdb_byte *name_table_string_offs_reordered;
233 const gdb_byte *name_table_entry_offs_reordered;
234 const gdb_byte *entry_pool;
235
236 struct index_val
237 {
238 ULONGEST dwarf_tag;
239 struct attr
240 {
241 /* Attribute name DW_IDX_*. */
242 ULONGEST dw_idx;
243
244 /* Attribute form DW_FORM_*. */
245 ULONGEST form;
246
247 /* Value if FORM is DW_FORM_implicit_const. */
248 LONGEST implicit_const;
249 };
250 std::vector<attr> attr_vec;
251 };
252
253 std::unordered_map<ULONGEST, index_val> abbrev_map;
254
255 const char *namei_to_name (uint32_t namei) const;
256
257 /* Implementation of the mapped_index_base virtual interface, for
258 the name_components cache. */
259
260 const char *symbol_name_at (offset_type idx) const override
261 { return namei_to_name (idx); }
262
263 size_t symbol_name_count () const override
264 { return this->name_count; }
265 };
266
267 /* See dwarf2read.h. */
268
269 dwarf2_per_objfile *
270 get_dwarf2_per_objfile (struct objfile *objfile)
271 {
272 return dwarf2_objfile_data_key.get (objfile);
273 }
274
275 /* Default names of the debugging sections. */
276
277 /* Note that if the debugging section has been compressed, it might
278 have a name like .zdebug_info. */
279
280 static const struct dwarf2_debug_sections dwarf2_elf_names =
281 {
282 { ".debug_info", ".zdebug_info" },
283 { ".debug_abbrev", ".zdebug_abbrev" },
284 { ".debug_line", ".zdebug_line" },
285 { ".debug_loc", ".zdebug_loc" },
286 { ".debug_loclists", ".zdebug_loclists" },
287 { ".debug_macinfo", ".zdebug_macinfo" },
288 { ".debug_macro", ".zdebug_macro" },
289 { ".debug_str", ".zdebug_str" },
290 { ".debug_line_str", ".zdebug_line_str" },
291 { ".debug_ranges", ".zdebug_ranges" },
292 { ".debug_rnglists", ".zdebug_rnglists" },
293 { ".debug_types", ".zdebug_types" },
294 { ".debug_addr", ".zdebug_addr" },
295 { ".debug_frame", ".zdebug_frame" },
296 { ".eh_frame", NULL },
297 { ".gdb_index", ".zgdb_index" },
298 { ".debug_names", ".zdebug_names" },
299 { ".debug_aranges", ".zdebug_aranges" },
300 23
301 };
302
303 /* List of DWO/DWP sections. */
304
305 static const struct dwop_section_names
306 {
307 struct dwarf2_section_names abbrev_dwo;
308 struct dwarf2_section_names info_dwo;
309 struct dwarf2_section_names line_dwo;
310 struct dwarf2_section_names loc_dwo;
311 struct dwarf2_section_names loclists_dwo;
312 struct dwarf2_section_names macinfo_dwo;
313 struct dwarf2_section_names macro_dwo;
314 struct dwarf2_section_names str_dwo;
315 struct dwarf2_section_names str_offsets_dwo;
316 struct dwarf2_section_names types_dwo;
317 struct dwarf2_section_names cu_index;
318 struct dwarf2_section_names tu_index;
319 }
320 dwop_section_names =
321 {
322 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
323 { ".debug_info.dwo", ".zdebug_info.dwo" },
324 { ".debug_line.dwo", ".zdebug_line.dwo" },
325 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
326 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
327 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
328 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
329 { ".debug_str.dwo", ".zdebug_str.dwo" },
330 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
331 { ".debug_types.dwo", ".zdebug_types.dwo" },
332 { ".debug_cu_index", ".zdebug_cu_index" },
333 { ".debug_tu_index", ".zdebug_tu_index" },
334 };
335
336 /* local data types */
337
338 /* The data in a compilation unit header, after target2host
339 translation, looks like this. */
340 struct comp_unit_head
341 {
342 unsigned int length;
343 short version;
344 unsigned char addr_size;
345 unsigned char signed_addr_p;
346 sect_offset abbrev_sect_off;
347
348 /* Size of file offsets; either 4 or 8. */
349 unsigned int offset_size;
350
351 /* Size of the length field; either 4 or 12. */
352 unsigned int initial_length_size;
353
354 enum dwarf_unit_type unit_type;
355
356 /* Offset to the first byte of this compilation unit header in the
357 .debug_info section, for resolving relative reference dies. */
358 sect_offset sect_off;
359
360 /* Offset to first die in this cu from the start of the cu.
361 This will be the first byte following the compilation unit header. */
362 cu_offset first_die_cu_offset;
363
364
365 /* 64-bit signature of this unit. For type units, it denotes the signature of
366 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
367 Also used in DWARF 5, to denote the dwo id when the unit type is
368 DW_UT_skeleton or DW_UT_split_compile. */
369 ULONGEST signature;
370
371 /* For types, offset in the type's DIE of the type defined by this TU. */
372 cu_offset type_cu_offset_in_tu;
373 };
374
375 /* Type used for delaying computation of method physnames.
376 See comments for compute_delayed_physnames. */
377 struct delayed_method_info
378 {
379 /* The type to which the method is attached, i.e., its parent class. */
380 struct type *type;
381
382 /* The index of the method in the type's function fieldlists. */
383 int fnfield_index;
384
385 /* The index of the method in the fieldlist. */
386 int index;
387
388 /* The name of the DIE. */
389 const char *name;
390
391 /* The DIE associated with this method. */
392 struct die_info *die;
393 };
394
395 /* Internal state when decoding a particular compilation unit. */
396 struct dwarf2_cu
397 {
398 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
399 ~dwarf2_cu ();
400
401 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
402
403 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
404 Create the set of symtabs used by this TU, or if this TU is sharing
405 symtabs with another TU and the symtabs have already been created
406 then restore those symtabs in the line header.
407 We don't need the pc/line-number mapping for type units. */
408 void setup_type_unit_groups (struct die_info *die);
409
410 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
411 buildsym_compunit constructor. */
412 struct compunit_symtab *start_symtab (const char *name,
413 const char *comp_dir,
414 CORE_ADDR low_pc);
415
416 /* Reset the builder. */
417 void reset_builder () { m_builder.reset (); }
418
419 /* The header of the compilation unit. */
420 struct comp_unit_head header {};
421
422 /* Base address of this compilation unit. */
423 CORE_ADDR base_address = 0;
424
425 /* Non-zero if base_address has been set. */
426 int base_known = 0;
427
428 /* The language we are debugging. */
429 enum language language = language_unknown;
430 const struct language_defn *language_defn = nullptr;
431
432 const char *producer = nullptr;
433
434 private:
435 /* The symtab builder for this CU. This is only non-NULL when full
436 symbols are being read. */
437 std::unique_ptr<buildsym_compunit> m_builder;
438
439 public:
440 /* The generic symbol table building routines have separate lists for
441 file scope symbols and all all other scopes (local scopes). So
442 we need to select the right one to pass to add_symbol_to_list().
443 We do it by keeping a pointer to the correct list in list_in_scope.
444
445 FIXME: The original dwarf code just treated the file scope as the
446 first local scope, and all other local scopes as nested local
447 scopes, and worked fine. Check to see if we really need to
448 distinguish these in buildsym.c. */
449 struct pending **list_in_scope = nullptr;
450
451 /* Hash table holding all the loaded partial DIEs
452 with partial_die->offset.SECT_OFF as hash. */
453 htab_t partial_dies = nullptr;
454
455 /* Storage for things with the same lifetime as this read-in compilation
456 unit, including partial DIEs. */
457 auto_obstack comp_unit_obstack;
458
459 /* When multiple dwarf2_cu structures are living in memory, this field
460 chains them all together, so that they can be released efficiently.
461 We will probably also want a generation counter so that most-recently-used
462 compilation units are cached... */
463 struct dwarf2_per_cu_data *read_in_chain = nullptr;
464
465 /* Backlink to our per_cu entry. */
466 struct dwarf2_per_cu_data *per_cu;
467
468 /* How many compilation units ago was this CU last referenced? */
469 int last_used = 0;
470
471 /* A hash table of DIE cu_offset for following references with
472 die_info->offset.sect_off as hash. */
473 htab_t die_hash = nullptr;
474
475 /* Full DIEs if read in. */
476 struct die_info *dies = nullptr;
477
478 /* A set of pointers to dwarf2_per_cu_data objects for compilation
479 units referenced by this one. Only set during full symbol processing;
480 partial symbol tables do not have dependencies. */
481 htab_t dependencies = nullptr;
482
483 /* Header data from the line table, during full symbol processing. */
484 struct line_header *line_header = nullptr;
485 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
486 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
487 this is the DW_TAG_compile_unit die for this CU. We'll hold on
488 to the line header as long as this DIE is being processed. See
489 process_die_scope. */
490 die_info *line_header_die_owner = nullptr;
491
492 /* A list of methods which need to have physnames computed
493 after all type information has been read. */
494 std::vector<delayed_method_info> method_list;
495
496 /* To be copied to symtab->call_site_htab. */
497 htab_t call_site_htab = nullptr;
498
499 /* Non-NULL if this CU came from a DWO file.
500 There is an invariant here that is important to remember:
501 Except for attributes copied from the top level DIE in the "main"
502 (or "stub") file in preparation for reading the DWO file
503 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
504 Either there isn't a DWO file (in which case this is NULL and the point
505 is moot), or there is and either we're not going to read it (in which
506 case this is NULL) or there is and we are reading it (in which case this
507 is non-NULL). */
508 struct dwo_unit *dwo_unit = nullptr;
509
510 /* The DW_AT_addr_base attribute if present, zero otherwise
511 (zero is a valid value though).
512 Note this value comes from the Fission stub CU/TU's DIE. */
513 ULONGEST addr_base = 0;
514
515 /* The DW_AT_ranges_base attribute if present, zero otherwise
516 (zero is a valid value though).
517 Note this value comes from the Fission stub CU/TU's DIE.
518 Also note that the value is zero in the non-DWO case so this value can
519 be used without needing to know whether DWO files are in use or not.
520 N.B. This does not apply to DW_AT_ranges appearing in
521 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
522 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
523 DW_AT_ranges_base *would* have to be applied, and we'd have to care
524 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
525 ULONGEST ranges_base = 0;
526
527 /* When reading debug info generated by older versions of rustc, we
528 have to rewrite some union types to be struct types with a
529 variant part. This rewriting must be done after the CU is fully
530 read in, because otherwise at the point of rewriting some struct
531 type might not have been fully processed. So, we keep a list of
532 all such types here and process them after expansion. */
533 std::vector<struct type *> rust_unions;
534
535 /* Mark used when releasing cached dies. */
536 bool mark : 1;
537
538 /* This CU references .debug_loc. See the symtab->locations_valid field.
539 This test is imperfect as there may exist optimized debug code not using
540 any location list and still facing inlining issues if handled as
541 unoptimized code. For a future better test see GCC PR other/32998. */
542 bool has_loclist : 1;
543
544 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
545 if all the producer_is_* fields are valid. This information is cached
546 because profiling CU expansion showed excessive time spent in
547 producer_is_gxx_lt_4_6. */
548 bool checked_producer : 1;
549 bool producer_is_gxx_lt_4_6 : 1;
550 bool producer_is_gcc_lt_4_3 : 1;
551 bool producer_is_icc : 1;
552 bool producer_is_icc_lt_14 : 1;
553 bool producer_is_codewarrior : 1;
554
555 /* When true, the file that we're processing is known to have
556 debugging info for C++ namespaces. GCC 3.3.x did not produce
557 this information, but later versions do. */
558
559 bool processing_has_namespace_info : 1;
560
561 struct partial_die_info *find_partial_die (sect_offset sect_off);
562
563 /* If this CU was inherited by another CU (via specification,
564 abstract_origin, etc), this is the ancestor CU. */
565 dwarf2_cu *ancestor;
566
567 /* Get the buildsym_compunit for this CU. */
568 buildsym_compunit *get_builder ()
569 {
570 /* If this CU has a builder associated with it, use that. */
571 if (m_builder != nullptr)
572 return m_builder.get ();
573
574 /* Otherwise, search ancestors for a valid builder. */
575 if (ancestor != nullptr)
576 return ancestor->get_builder ();
577
578 return nullptr;
579 }
580 };
581
582 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
583 This includes type_unit_group and quick_file_names. */
584
585 struct stmt_list_hash
586 {
587 /* The DWO unit this table is from or NULL if there is none. */
588 struct dwo_unit *dwo_unit;
589
590 /* Offset in .debug_line or .debug_line.dwo. */
591 sect_offset line_sect_off;
592 };
593
594 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
595 an object of this type. */
596
597 struct type_unit_group
598 {
599 /* dwarf2read.c's main "handle" on a TU symtab.
600 To simplify things we create an artificial CU that "includes" all the
601 type units using this stmt_list so that the rest of the code still has
602 a "per_cu" handle on the symtab.
603 This PER_CU is recognized by having no section. */
604 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
605 struct dwarf2_per_cu_data per_cu;
606
607 /* The TUs that share this DW_AT_stmt_list entry.
608 This is added to while parsing type units to build partial symtabs,
609 and is deleted afterwards and not used again. */
610 std::vector<signatured_type *> *tus;
611
612 /* The compunit symtab.
613 Type units in a group needn't all be defined in the same source file,
614 so we create an essentially anonymous symtab as the compunit symtab. */
615 struct compunit_symtab *compunit_symtab;
616
617 /* The data used to construct the hash key. */
618 struct stmt_list_hash hash;
619
620 /* The number of symtabs from the line header.
621 The value here must match line_header.num_file_names. */
622 unsigned int num_symtabs;
623
624 /* The symbol tables for this TU (obtained from the files listed in
625 DW_AT_stmt_list).
626 WARNING: The order of entries here must match the order of entries
627 in the line header. After the first TU using this type_unit_group, the
628 line header for the subsequent TUs is recreated from this. This is done
629 because we need to use the same symtabs for each TU using the same
630 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
631 there's no guarantee the line header doesn't have duplicate entries. */
632 struct symtab **symtabs;
633 };
634
635 /* These sections are what may appear in a (real or virtual) DWO file. */
636
637 struct dwo_sections
638 {
639 struct dwarf2_section_info abbrev;
640 struct dwarf2_section_info line;
641 struct dwarf2_section_info loc;
642 struct dwarf2_section_info loclists;
643 struct dwarf2_section_info macinfo;
644 struct dwarf2_section_info macro;
645 struct dwarf2_section_info str;
646 struct dwarf2_section_info str_offsets;
647 /* In the case of a virtual DWO file, these two are unused. */
648 struct dwarf2_section_info info;
649 std::vector<dwarf2_section_info> types;
650 };
651
652 /* CUs/TUs in DWP/DWO files. */
653
654 struct dwo_unit
655 {
656 /* Backlink to the containing struct dwo_file. */
657 struct dwo_file *dwo_file;
658
659 /* The "id" that distinguishes this CU/TU.
660 .debug_info calls this "dwo_id", .debug_types calls this "signature".
661 Since signatures came first, we stick with it for consistency. */
662 ULONGEST signature;
663
664 /* The section this CU/TU lives in, in the DWO file. */
665 struct dwarf2_section_info *section;
666
667 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
668 sect_offset sect_off;
669 unsigned int length;
670
671 /* For types, offset in the type's DIE of the type defined by this TU. */
672 cu_offset type_offset_in_tu;
673 };
674
675 /* include/dwarf2.h defines the DWP section codes.
676 It defines a max value but it doesn't define a min value, which we
677 use for error checking, so provide one. */
678
679 enum dwp_v2_section_ids
680 {
681 DW_SECT_MIN = 1
682 };
683
684 /* Data for one DWO file.
685
686 This includes virtual DWO files (a virtual DWO file is a DWO file as it
687 appears in a DWP file). DWP files don't really have DWO files per se -
688 comdat folding of types "loses" the DWO file they came from, and from
689 a high level view DWP files appear to contain a mass of random types.
690 However, to maintain consistency with the non-DWP case we pretend DWP
691 files contain virtual DWO files, and we assign each TU with one virtual
692 DWO file (generally based on the line and abbrev section offsets -
693 a heuristic that seems to work in practice). */
694
695 struct dwo_file
696 {
697 dwo_file () = default;
698 DISABLE_COPY_AND_ASSIGN (dwo_file);
699
700 /* The DW_AT_GNU_dwo_name attribute.
701 For virtual DWO files the name is constructed from the section offsets
702 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
703 from related CU+TUs. */
704 const char *dwo_name = nullptr;
705
706 /* The DW_AT_comp_dir attribute. */
707 const char *comp_dir = nullptr;
708
709 /* The bfd, when the file is open. Otherwise this is NULL.
710 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
711 gdb_bfd_ref_ptr dbfd;
712
713 /* The sections that make up this DWO file.
714 Remember that for virtual DWO files in DWP V2, these are virtual
715 sections (for lack of a better name). */
716 struct dwo_sections sections {};
717
718 /* The CUs in the file.
719 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
720 an extension to handle LLVM's Link Time Optimization output (where
721 multiple source files may be compiled into a single object/dwo pair). */
722 htab_t cus {};
723
724 /* Table of TUs in the file.
725 Each element is a struct dwo_unit. */
726 htab_t tus {};
727 };
728
729 /* These sections are what may appear in a DWP file. */
730
731 struct dwp_sections
732 {
733 /* These are used by both DWP version 1 and 2. */
734 struct dwarf2_section_info str;
735 struct dwarf2_section_info cu_index;
736 struct dwarf2_section_info tu_index;
737
738 /* These are only used by DWP version 2 files.
739 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
740 sections are referenced by section number, and are not recorded here.
741 In DWP version 2 there is at most one copy of all these sections, each
742 section being (effectively) comprised of the concatenation of all of the
743 individual sections that exist in the version 1 format.
744 To keep the code simple we treat each of these concatenated pieces as a
745 section itself (a virtual section?). */
746 struct dwarf2_section_info abbrev;
747 struct dwarf2_section_info info;
748 struct dwarf2_section_info line;
749 struct dwarf2_section_info loc;
750 struct dwarf2_section_info macinfo;
751 struct dwarf2_section_info macro;
752 struct dwarf2_section_info str_offsets;
753 struct dwarf2_section_info types;
754 };
755
756 /* These sections are what may appear in a virtual DWO file in DWP version 1.
757 A virtual DWO file is a DWO file as it appears in a DWP file. */
758
759 struct virtual_v1_dwo_sections
760 {
761 struct dwarf2_section_info abbrev;
762 struct dwarf2_section_info line;
763 struct dwarf2_section_info loc;
764 struct dwarf2_section_info macinfo;
765 struct dwarf2_section_info macro;
766 struct dwarf2_section_info str_offsets;
767 /* Each DWP hash table entry records one CU or one TU.
768 That is recorded here, and copied to dwo_unit.section. */
769 struct dwarf2_section_info info_or_types;
770 };
771
772 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
773 In version 2, the sections of the DWO files are concatenated together
774 and stored in one section of that name. Thus each ELF section contains
775 several "virtual" sections. */
776
777 struct virtual_v2_dwo_sections
778 {
779 bfd_size_type abbrev_offset;
780 bfd_size_type abbrev_size;
781
782 bfd_size_type line_offset;
783 bfd_size_type line_size;
784
785 bfd_size_type loc_offset;
786 bfd_size_type loc_size;
787
788 bfd_size_type macinfo_offset;
789 bfd_size_type macinfo_size;
790
791 bfd_size_type macro_offset;
792 bfd_size_type macro_size;
793
794 bfd_size_type str_offsets_offset;
795 bfd_size_type str_offsets_size;
796
797 /* Each DWP hash table entry records one CU or one TU.
798 That is recorded here, and copied to dwo_unit.section. */
799 bfd_size_type info_or_types_offset;
800 bfd_size_type info_or_types_size;
801 };
802
803 /* Contents of DWP hash tables. */
804
805 struct dwp_hash_table
806 {
807 uint32_t version, nr_columns;
808 uint32_t nr_units, nr_slots;
809 const gdb_byte *hash_table, *unit_table;
810 union
811 {
812 struct
813 {
814 const gdb_byte *indices;
815 } v1;
816 struct
817 {
818 /* This is indexed by column number and gives the id of the section
819 in that column. */
820 #define MAX_NR_V2_DWO_SECTIONS \
821 (1 /* .debug_info or .debug_types */ \
822 + 1 /* .debug_abbrev */ \
823 + 1 /* .debug_line */ \
824 + 1 /* .debug_loc */ \
825 + 1 /* .debug_str_offsets */ \
826 + 1 /* .debug_macro or .debug_macinfo */)
827 int section_ids[MAX_NR_V2_DWO_SECTIONS];
828 const gdb_byte *offsets;
829 const gdb_byte *sizes;
830 } v2;
831 } section_pool;
832 };
833
834 /* Data for one DWP file. */
835
836 struct dwp_file
837 {
838 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
839 : name (name_),
840 dbfd (std::move (abfd))
841 {
842 }
843
844 /* Name of the file. */
845 const char *name;
846
847 /* File format version. */
848 int version = 0;
849
850 /* The bfd. */
851 gdb_bfd_ref_ptr dbfd;
852
853 /* Section info for this file. */
854 struct dwp_sections sections {};
855
856 /* Table of CUs in the file. */
857 const struct dwp_hash_table *cus = nullptr;
858
859 /* Table of TUs in the file. */
860 const struct dwp_hash_table *tus = nullptr;
861
862 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
863 htab_t loaded_cus {};
864 htab_t loaded_tus {};
865
866 /* Table to map ELF section numbers to their sections.
867 This is only needed for the DWP V1 file format. */
868 unsigned int num_sections = 0;
869 asection **elf_sections = nullptr;
870 };
871
872 /* Struct used to pass misc. parameters to read_die_and_children, et
873 al. which are used for both .debug_info and .debug_types dies.
874 All parameters here are unchanging for the life of the call. This
875 struct exists to abstract away the constant parameters of die reading. */
876
877 struct die_reader_specs
878 {
879 /* The bfd of die_section. */
880 bfd* abfd;
881
882 /* The CU of the DIE we are parsing. */
883 struct dwarf2_cu *cu;
884
885 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
886 struct dwo_file *dwo_file;
887
888 /* The section the die comes from.
889 This is either .debug_info or .debug_types, or the .dwo variants. */
890 struct dwarf2_section_info *die_section;
891
892 /* die_section->buffer. */
893 const gdb_byte *buffer;
894
895 /* The end of the buffer. */
896 const gdb_byte *buffer_end;
897
898 /* The value of the DW_AT_comp_dir attribute. */
899 const char *comp_dir;
900
901 /* The abbreviation table to use when reading the DIEs. */
902 struct abbrev_table *abbrev_table;
903 };
904
905 /* Type of function passed to init_cutu_and_read_dies, et.al. */
906 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
907 const gdb_byte *info_ptr,
908 struct die_info *comp_unit_die,
909 int has_children,
910 void *data);
911
912 /* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
913 later. */
914 typedef int dir_index;
915
916 /* file_name_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5
917 and later. */
918 typedef int file_name_index;
919
920 struct file_entry
921 {
922 file_entry () = default;
923
924 file_entry (const char *name_, dir_index d_index_,
925 unsigned int mod_time_, unsigned int length_)
926 : name (name_),
927 d_index (d_index_),
928 mod_time (mod_time_),
929 length (length_)
930 {}
931
932 /* Return the include directory at D_INDEX stored in LH. Returns
933 NULL if D_INDEX is out of bounds. */
934 const char *include_dir (const line_header *lh) const;
935
936 /* The file name. Note this is an observing pointer. The memory is
937 owned by debug_line_buffer. */
938 const char *name {};
939
940 /* The directory index (1-based). */
941 dir_index d_index {};
942
943 unsigned int mod_time {};
944
945 unsigned int length {};
946
947 /* True if referenced by the Line Number Program. */
948 bool included_p {};
949
950 /* The associated symbol table, if any. */
951 struct symtab *symtab {};
952 };
953
954 /* The line number information for a compilation unit (found in the
955 .debug_line section) begins with a "statement program header",
956 which contains the following information. */
957 struct line_header
958 {
959 line_header ()
960 : offset_in_dwz {}
961 {}
962
963 /* Add an entry to the include directory table. */
964 void add_include_dir (const char *include_dir);
965
966 /* Add an entry to the file name table. */
967 void add_file_name (const char *name, dir_index d_index,
968 unsigned int mod_time, unsigned int length);
969
970 /* Return the include dir at INDEX (0-based in DWARF 5 and 1-based before).
971 Returns NULL if INDEX is out of bounds. */
972 const char *include_dir_at (dir_index index) const
973 {
974 int vec_index;
975 if (version >= 5)
976 vec_index = index;
977 else
978 vec_index = index - 1;
979 if (vec_index < 0 || vec_index >= m_include_dirs.size ())
980 return NULL;
981 return m_include_dirs[vec_index];
982 }
983
984 bool is_valid_file_index (int file_index)
985 {
986 if (version >= 5)
987 return 0 <= file_index && file_index < file_names_size ();
988 return 1 <= file_index && file_index <= file_names_size ();
989 }
990
991 /* Return the file name at INDEX (0-based in DWARF 5 and 1-based before).
992 Returns NULL if INDEX is out of bounds. */
993 file_entry *file_name_at (file_name_index index)
994 {
995 int vec_index;
996 if (version >= 5)
997 vec_index = index;
998 else
999 vec_index = index - 1;
1000 if (vec_index < 0 || vec_index >= m_file_names.size ())
1001 return NULL;
1002 return &m_file_names[vec_index];
1003 }
1004
1005 /* The indexes are 0-based in DWARF 5 and 1-based in DWARF 4. Therefore,
1006 this method should only be used to iterate through all file entries in an
1007 index-agnostic manner. */
1008 std::vector<file_entry> &file_names ()
1009 { return m_file_names; }
1010
1011 /* Offset of line number information in .debug_line section. */
1012 sect_offset sect_off {};
1013
1014 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1015 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1016
1017 unsigned int total_length {};
1018 unsigned short version {};
1019 unsigned int header_length {};
1020 unsigned char minimum_instruction_length {};
1021 unsigned char maximum_ops_per_instruction {};
1022 unsigned char default_is_stmt {};
1023 int line_base {};
1024 unsigned char line_range {};
1025 unsigned char opcode_base {};
1026
1027 /* standard_opcode_lengths[i] is the number of operands for the
1028 standard opcode whose value is i. This means that
1029 standard_opcode_lengths[0] is unused, and the last meaningful
1030 element is standard_opcode_lengths[opcode_base - 1]. */
1031 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1032
1033 int file_names_size ()
1034 { return m_file_names.size(); }
1035
1036 /* The start and end of the statement program following this
1037 header. These point into dwarf2_per_objfile->line_buffer. */
1038 const gdb_byte *statement_program_start {}, *statement_program_end {};
1039
1040 private:
1041 /* The include_directories table. Note these are observing
1042 pointers. The memory is owned by debug_line_buffer. */
1043 std::vector<const char *> m_include_dirs;
1044
1045 /* The file_names table. This is private because the meaning of indexes
1046 differs among DWARF versions (The first valid index is 1 in DWARF 4 and
1047 before, and is 0 in DWARF 5 and later). So the client should use
1048 file_name_at method for access. */
1049 std::vector<file_entry> m_file_names;
1050 };
1051
1052 typedef std::unique_ptr<line_header> line_header_up;
1053
1054 const char *
1055 file_entry::include_dir (const line_header *lh) const
1056 {
1057 return lh->include_dir_at (d_index);
1058 }
1059
1060 /* When we construct a partial symbol table entry we only
1061 need this much information. */
1062 struct partial_die_info : public allocate_on_obstack
1063 {
1064 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1065
1066 /* Disable assign but still keep copy ctor, which is needed
1067 load_partial_dies. */
1068 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1069
1070 /* Adjust the partial die before generating a symbol for it. This
1071 function may set the is_external flag or change the DIE's
1072 name. */
1073 void fixup (struct dwarf2_cu *cu);
1074
1075 /* Read a minimal amount of information into the minimal die
1076 structure. */
1077 const gdb_byte *read (const struct die_reader_specs *reader,
1078 const struct abbrev_info &abbrev,
1079 const gdb_byte *info_ptr);
1080
1081 /* Offset of this DIE. */
1082 const sect_offset sect_off;
1083
1084 /* DWARF-2 tag for this DIE. */
1085 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1086
1087 /* Assorted flags describing the data found in this DIE. */
1088 const unsigned int has_children : 1;
1089
1090 unsigned int is_external : 1;
1091 unsigned int is_declaration : 1;
1092 unsigned int has_type : 1;
1093 unsigned int has_specification : 1;
1094 unsigned int has_pc_info : 1;
1095 unsigned int may_be_inlined : 1;
1096
1097 /* This DIE has been marked DW_AT_main_subprogram. */
1098 unsigned int main_subprogram : 1;
1099
1100 /* Flag set if the SCOPE field of this structure has been
1101 computed. */
1102 unsigned int scope_set : 1;
1103
1104 /* Flag set if the DIE has a byte_size attribute. */
1105 unsigned int has_byte_size : 1;
1106
1107 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1108 unsigned int has_const_value : 1;
1109
1110 /* Flag set if any of the DIE's children are template arguments. */
1111 unsigned int has_template_arguments : 1;
1112
1113 /* Flag set if fixup has been called on this die. */
1114 unsigned int fixup_called : 1;
1115
1116 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1117 unsigned int is_dwz : 1;
1118
1119 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1120 unsigned int spec_is_dwz : 1;
1121
1122 /* The name of this DIE. Normally the value of DW_AT_name, but
1123 sometimes a default name for unnamed DIEs. */
1124 const char *name = nullptr;
1125
1126 /* The linkage name, if present. */
1127 const char *linkage_name = nullptr;
1128
1129 /* The scope to prepend to our children. This is generally
1130 allocated on the comp_unit_obstack, so will disappear
1131 when this compilation unit leaves the cache. */
1132 const char *scope = nullptr;
1133
1134 /* Some data associated with the partial DIE. The tag determines
1135 which field is live. */
1136 union
1137 {
1138 /* The location description associated with this DIE, if any. */
1139 struct dwarf_block *locdesc;
1140 /* The offset of an import, for DW_TAG_imported_unit. */
1141 sect_offset sect_off;
1142 } d {};
1143
1144 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1145 CORE_ADDR lowpc = 0;
1146 CORE_ADDR highpc = 0;
1147
1148 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1149 DW_AT_sibling, if any. */
1150 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1151 could return DW_AT_sibling values to its caller load_partial_dies. */
1152 const gdb_byte *sibling = nullptr;
1153
1154 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1155 DW_AT_specification (or DW_AT_abstract_origin or
1156 DW_AT_extension). */
1157 sect_offset spec_offset {};
1158
1159 /* Pointers to this DIE's parent, first child, and next sibling,
1160 if any. */
1161 struct partial_die_info *die_parent = nullptr;
1162 struct partial_die_info *die_child = nullptr;
1163 struct partial_die_info *die_sibling = nullptr;
1164
1165 friend struct partial_die_info *
1166 dwarf2_cu::find_partial_die (sect_offset sect_off);
1167
1168 private:
1169 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1170 partial_die_info (sect_offset sect_off)
1171 : partial_die_info (sect_off, DW_TAG_padding, 0)
1172 {
1173 }
1174
1175 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1176 int has_children_)
1177 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1178 {
1179 is_external = 0;
1180 is_declaration = 0;
1181 has_type = 0;
1182 has_specification = 0;
1183 has_pc_info = 0;
1184 may_be_inlined = 0;
1185 main_subprogram = 0;
1186 scope_set = 0;
1187 has_byte_size = 0;
1188 has_const_value = 0;
1189 has_template_arguments = 0;
1190 fixup_called = 0;
1191 is_dwz = 0;
1192 spec_is_dwz = 0;
1193 }
1194 };
1195
1196 /* This data structure holds the information of an abbrev. */
1197 struct abbrev_info
1198 {
1199 unsigned int number; /* number identifying abbrev */
1200 enum dwarf_tag tag; /* dwarf tag */
1201 unsigned short has_children; /* boolean */
1202 unsigned short num_attrs; /* number of attributes */
1203 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1204 struct abbrev_info *next; /* next in chain */
1205 };
1206
1207 struct attr_abbrev
1208 {
1209 ENUM_BITFIELD(dwarf_attribute) name : 16;
1210 ENUM_BITFIELD(dwarf_form) form : 16;
1211
1212 /* It is valid only if FORM is DW_FORM_implicit_const. */
1213 LONGEST implicit_const;
1214 };
1215
1216 /* Size of abbrev_table.abbrev_hash_table. */
1217 #define ABBREV_HASH_SIZE 121
1218
1219 /* Top level data structure to contain an abbreviation table. */
1220
1221 struct abbrev_table
1222 {
1223 explicit abbrev_table (sect_offset off)
1224 : sect_off (off)
1225 {
1226 m_abbrevs =
1227 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1228 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1229 }
1230
1231 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1232
1233 /* Allocate space for a struct abbrev_info object in
1234 ABBREV_TABLE. */
1235 struct abbrev_info *alloc_abbrev ();
1236
1237 /* Add an abbreviation to the table. */
1238 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1239
1240 /* Look up an abbrev in the table.
1241 Returns NULL if the abbrev is not found. */
1242
1243 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1244
1245
1246 /* Where the abbrev table came from.
1247 This is used as a sanity check when the table is used. */
1248 const sect_offset sect_off;
1249
1250 /* Storage for the abbrev table. */
1251 auto_obstack abbrev_obstack;
1252
1253 private:
1254
1255 /* Hash table of abbrevs.
1256 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1257 It could be statically allocated, but the previous code didn't so we
1258 don't either. */
1259 struct abbrev_info **m_abbrevs;
1260 };
1261
1262 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1263
1264 /* Attributes have a name and a value. */
1265 struct attribute
1266 {
1267 ENUM_BITFIELD(dwarf_attribute) name : 16;
1268 ENUM_BITFIELD(dwarf_form) form : 15;
1269
1270 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1271 field should be in u.str (existing only for DW_STRING) but it is kept
1272 here for better struct attribute alignment. */
1273 unsigned int string_is_canonical : 1;
1274
1275 union
1276 {
1277 const char *str;
1278 struct dwarf_block *blk;
1279 ULONGEST unsnd;
1280 LONGEST snd;
1281 CORE_ADDR addr;
1282 ULONGEST signature;
1283 }
1284 u;
1285 };
1286
1287 /* This data structure holds a complete die structure. */
1288 struct die_info
1289 {
1290 /* DWARF-2 tag for this DIE. */
1291 ENUM_BITFIELD(dwarf_tag) tag : 16;
1292
1293 /* Number of attributes */
1294 unsigned char num_attrs;
1295
1296 /* True if we're presently building the full type name for the
1297 type derived from this DIE. */
1298 unsigned char building_fullname : 1;
1299
1300 /* True if this die is in process. PR 16581. */
1301 unsigned char in_process : 1;
1302
1303 /* Abbrev number */
1304 unsigned int abbrev;
1305
1306 /* Offset in .debug_info or .debug_types section. */
1307 sect_offset sect_off;
1308
1309 /* The dies in a compilation unit form an n-ary tree. PARENT
1310 points to this die's parent; CHILD points to the first child of
1311 this node; and all the children of a given node are chained
1312 together via their SIBLING fields. */
1313 struct die_info *child; /* Its first child, if any. */
1314 struct die_info *sibling; /* Its next sibling, if any. */
1315 struct die_info *parent; /* Its parent, if any. */
1316
1317 /* An array of attributes, with NUM_ATTRS elements. There may be
1318 zero, but it's not common and zero-sized arrays are not
1319 sufficiently portable C. */
1320 struct attribute attrs[1];
1321 };
1322
1323 /* Get at parts of an attribute structure. */
1324
1325 #define DW_STRING(attr) ((attr)->u.str)
1326 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1327 #define DW_UNSND(attr) ((attr)->u.unsnd)
1328 #define DW_BLOCK(attr) ((attr)->u.blk)
1329 #define DW_SND(attr) ((attr)->u.snd)
1330 #define DW_ADDR(attr) ((attr)->u.addr)
1331 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1332
1333 /* Blocks are a bunch of untyped bytes. */
1334 struct dwarf_block
1335 {
1336 size_t size;
1337
1338 /* Valid only if SIZE is not zero. */
1339 const gdb_byte *data;
1340 };
1341
1342 #ifndef ATTR_ALLOC_CHUNK
1343 #define ATTR_ALLOC_CHUNK 4
1344 #endif
1345
1346 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1347 but this would require a corresponding change in unpack_field_as_long
1348 and friends. */
1349 static int bits_per_byte = 8;
1350
1351 /* When reading a variant or variant part, we track a bit more
1352 information about the field, and store it in an object of this
1353 type. */
1354
1355 struct variant_field
1356 {
1357 /* If we see a DW_TAG_variant, then this will be the discriminant
1358 value. */
1359 ULONGEST discriminant_value;
1360 /* If we see a DW_TAG_variant, then this will be set if this is the
1361 default branch. */
1362 bool default_branch;
1363 /* While reading a DW_TAG_variant_part, this will be set if this
1364 field is the discriminant. */
1365 bool is_discriminant;
1366 };
1367
1368 struct nextfield
1369 {
1370 int accessibility = 0;
1371 int virtuality = 0;
1372 /* Extra information to describe a variant or variant part. */
1373 struct variant_field variant {};
1374 struct field field {};
1375 };
1376
1377 struct fnfieldlist
1378 {
1379 const char *name = nullptr;
1380 std::vector<struct fn_field> fnfields;
1381 };
1382
1383 /* The routines that read and process dies for a C struct or C++ class
1384 pass lists of data member fields and lists of member function fields
1385 in an instance of a field_info structure, as defined below. */
1386 struct field_info
1387 {
1388 /* List of data member and baseclasses fields. */
1389 std::vector<struct nextfield> fields;
1390 std::vector<struct nextfield> baseclasses;
1391
1392 /* Number of fields (including baseclasses). */
1393 int nfields = 0;
1394
1395 /* Set if the accessibility of one of the fields is not public. */
1396 int non_public_fields = 0;
1397
1398 /* Member function fieldlist array, contains name of possibly overloaded
1399 member function, number of overloaded member functions and a pointer
1400 to the head of the member function field chain. */
1401 std::vector<struct fnfieldlist> fnfieldlists;
1402
1403 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1404 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1405 std::vector<struct decl_field> typedef_field_list;
1406
1407 /* Nested types defined by this class and the number of elements in this
1408 list. */
1409 std::vector<struct decl_field> nested_types_list;
1410 };
1411
1412 /* One item on the queue of compilation units to read in full symbols
1413 for. */
1414 struct dwarf2_queue_item
1415 {
1416 struct dwarf2_per_cu_data *per_cu;
1417 enum language pretend_language;
1418 struct dwarf2_queue_item *next;
1419 };
1420
1421 /* The current queue. */
1422 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1423
1424 /* Loaded secondary compilation units are kept in memory until they
1425 have not been referenced for the processing of this many
1426 compilation units. Set this to zero to disable caching. Cache
1427 sizes of up to at least twenty will improve startup time for
1428 typical inter-CU-reference binaries, at an obvious memory cost. */
1429 static int dwarf_max_cache_age = 5;
1430 static void
1431 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1432 struct cmd_list_element *c, const char *value)
1433 {
1434 fprintf_filtered (file, _("The upper bound on the age of cached "
1435 "DWARF compilation units is %s.\n"),
1436 value);
1437 }
1438 \f
1439 /* local function prototypes */
1440
1441 static const char *get_section_name (const struct dwarf2_section_info *);
1442
1443 static const char *get_section_file_name (const struct dwarf2_section_info *);
1444
1445 static void dwarf2_find_base_address (struct die_info *die,
1446 struct dwarf2_cu *cu);
1447
1448 static struct partial_symtab *create_partial_symtab
1449 (struct dwarf2_per_cu_data *per_cu, const char *name);
1450
1451 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1452 const gdb_byte *info_ptr,
1453 struct die_info *type_unit_die,
1454 int has_children, void *data);
1455
1456 static void dwarf2_build_psymtabs_hard
1457 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1458
1459 static void scan_partial_symbols (struct partial_die_info *,
1460 CORE_ADDR *, CORE_ADDR *,
1461 int, struct dwarf2_cu *);
1462
1463 static void add_partial_symbol (struct partial_die_info *,
1464 struct dwarf2_cu *);
1465
1466 static void add_partial_namespace (struct partial_die_info *pdi,
1467 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1468 int set_addrmap, struct dwarf2_cu *cu);
1469
1470 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1471 CORE_ADDR *highpc, int set_addrmap,
1472 struct dwarf2_cu *cu);
1473
1474 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1475 struct dwarf2_cu *cu);
1476
1477 static void add_partial_subprogram (struct partial_die_info *pdi,
1478 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1479 int need_pc, struct dwarf2_cu *cu);
1480
1481 static void dwarf2_read_symtab (struct partial_symtab *,
1482 struct objfile *);
1483
1484 static void psymtab_to_symtab_1 (struct partial_symtab *);
1485
1486 static abbrev_table_up abbrev_table_read_table
1487 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1488 sect_offset);
1489
1490 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1491
1492 static struct partial_die_info *load_partial_dies
1493 (const struct die_reader_specs *, const gdb_byte *, int);
1494
1495 /* A pair of partial_die_info and compilation unit. */
1496 struct cu_partial_die_info
1497 {
1498 /* The compilation unit of the partial_die_info. */
1499 struct dwarf2_cu *cu;
1500 /* A partial_die_info. */
1501 struct partial_die_info *pdi;
1502
1503 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1504 : cu (cu),
1505 pdi (pdi)
1506 { /* Nothing. */ }
1507
1508 private:
1509 cu_partial_die_info () = delete;
1510 };
1511
1512 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1513 struct dwarf2_cu *);
1514
1515 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1516 struct attribute *, struct attr_abbrev *,
1517 const gdb_byte *);
1518
1519 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1520
1521 static int read_1_signed_byte (bfd *, const gdb_byte *);
1522
1523 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1524
1525 /* Read the next three bytes (little-endian order) as an unsigned integer. */
1526 static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1527
1528 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1529
1530 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1531
1532 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1533 unsigned int *);
1534
1535 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1536
1537 static LONGEST read_checked_initial_length_and_offset
1538 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1539 unsigned int *, unsigned int *);
1540
1541 static LONGEST read_offset (bfd *, const gdb_byte *,
1542 const struct comp_unit_head *,
1543 unsigned int *);
1544
1545 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1546
1547 static sect_offset read_abbrev_offset
1548 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1549 struct dwarf2_section_info *, sect_offset);
1550
1551 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1552
1553 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1554
1555 static const char *read_indirect_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_line_string
1560 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1561 const struct comp_unit_head *, unsigned int *);
1562
1563 static const char *read_indirect_string_at_offset
1564 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1565 LONGEST str_offset);
1566
1567 static const char *read_indirect_string_from_dwz
1568 (struct objfile *objfile, struct dwz_file *, LONGEST);
1569
1570 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1571
1572 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1573 const gdb_byte *,
1574 unsigned int *);
1575
1576 static const char *read_str_index (const struct die_reader_specs *reader,
1577 ULONGEST str_index);
1578
1579 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1580
1581 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1582 struct dwarf2_cu *);
1583
1584 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1585 unsigned int);
1586
1587 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1588 struct dwarf2_cu *cu);
1589
1590 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1591
1592 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1593 struct dwarf2_cu *cu);
1594
1595 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1596
1597 static struct die_info *die_specification (struct die_info *die,
1598 struct dwarf2_cu **);
1599
1600 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1601 struct dwarf2_cu *cu);
1602
1603 static void dwarf_decode_lines (struct line_header *, const char *,
1604 struct dwarf2_cu *, struct partial_symtab *,
1605 CORE_ADDR, int decode_mapping);
1606
1607 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1608 const char *);
1609
1610 static struct symbol *new_symbol (struct die_info *, struct type *,
1611 struct dwarf2_cu *, struct symbol * = NULL);
1612
1613 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1614 struct dwarf2_cu *);
1615
1616 static void dwarf2_const_value_attr (const struct attribute *attr,
1617 struct type *type,
1618 const char *name,
1619 struct obstack *obstack,
1620 struct dwarf2_cu *cu, LONGEST *value,
1621 const gdb_byte **bytes,
1622 struct dwarf2_locexpr_baton **baton);
1623
1624 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1625
1626 static int need_gnat_info (struct dwarf2_cu *);
1627
1628 static struct type *die_descriptive_type (struct die_info *,
1629 struct dwarf2_cu *);
1630
1631 static void set_descriptive_type (struct type *, struct die_info *,
1632 struct dwarf2_cu *);
1633
1634 static struct type *die_containing_type (struct die_info *,
1635 struct dwarf2_cu *);
1636
1637 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1638 struct dwarf2_cu *);
1639
1640 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1641
1642 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1643
1644 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1645
1646 static char *typename_concat (struct obstack *obs, const char *prefix,
1647 const char *suffix, int physname,
1648 struct dwarf2_cu *cu);
1649
1650 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1651
1652 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1653
1654 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1655
1656 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1657
1658 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1659
1660 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1661
1662 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1663 struct dwarf2_cu *, struct partial_symtab *);
1664
1665 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1666 values. Keep the items ordered with increasing constraints compliance. */
1667 enum pc_bounds_kind
1668 {
1669 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1670 PC_BOUNDS_NOT_PRESENT,
1671
1672 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1673 were present but they do not form a valid range of PC addresses. */
1674 PC_BOUNDS_INVALID,
1675
1676 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1677 PC_BOUNDS_RANGES,
1678
1679 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1680 PC_BOUNDS_HIGH_LOW,
1681 };
1682
1683 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1684 CORE_ADDR *, CORE_ADDR *,
1685 struct dwarf2_cu *,
1686 struct partial_symtab *);
1687
1688 static void get_scope_pc_bounds (struct die_info *,
1689 CORE_ADDR *, CORE_ADDR *,
1690 struct dwarf2_cu *);
1691
1692 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1693 CORE_ADDR, struct dwarf2_cu *);
1694
1695 static void dwarf2_add_field (struct field_info *, struct die_info *,
1696 struct dwarf2_cu *);
1697
1698 static void dwarf2_attach_fields_to_type (struct field_info *,
1699 struct type *, struct dwarf2_cu *);
1700
1701 static void dwarf2_add_member_fn (struct field_info *,
1702 struct die_info *, struct type *,
1703 struct dwarf2_cu *);
1704
1705 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1706 struct type *,
1707 struct dwarf2_cu *);
1708
1709 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1710
1711 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1712
1713 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1714
1715 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1716
1717 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1718
1719 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1720
1721 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1722
1723 static struct type *read_module_type (struct die_info *die,
1724 struct dwarf2_cu *cu);
1725
1726 static const char *namespace_name (struct die_info *die,
1727 int *is_anonymous, struct dwarf2_cu *);
1728
1729 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1730
1731 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1732
1733 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1734 struct dwarf2_cu *);
1735
1736 static struct die_info *read_die_and_siblings_1
1737 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1738 struct die_info *);
1739
1740 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1741 const gdb_byte *info_ptr,
1742 const gdb_byte **new_info_ptr,
1743 struct die_info *parent);
1744
1745 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1746 struct die_info **, const gdb_byte *,
1747 int *, int);
1748
1749 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1750 struct die_info **, const gdb_byte *,
1751 int *);
1752
1753 static void process_die (struct die_info *, struct dwarf2_cu *);
1754
1755 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1756 struct obstack *);
1757
1758 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1759
1760 static const char *dwarf2_full_name (const char *name,
1761 struct die_info *die,
1762 struct dwarf2_cu *cu);
1763
1764 static const char *dwarf2_physname (const char *name, struct die_info *die,
1765 struct dwarf2_cu *cu);
1766
1767 static struct die_info *dwarf2_extension (struct die_info *die,
1768 struct dwarf2_cu **);
1769
1770 static const char *dwarf_tag_name (unsigned int);
1771
1772 static const char *dwarf_attr_name (unsigned int);
1773
1774 static const char *dwarf_unit_type_name (int unit_type);
1775
1776 static const char *dwarf_form_name (unsigned int);
1777
1778 static const char *dwarf_bool_name (unsigned int);
1779
1780 static const char *dwarf_type_encoding_name (unsigned int);
1781
1782 static struct die_info *sibling_die (struct die_info *);
1783
1784 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1785
1786 static void dump_die_for_error (struct die_info *);
1787
1788 static void dump_die_1 (struct ui_file *, int level, int max_level,
1789 struct die_info *);
1790
1791 /*static*/ void dump_die (struct die_info *, int max_level);
1792
1793 static void store_in_ref_table (struct die_info *,
1794 struct dwarf2_cu *);
1795
1796 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1797
1798 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1799
1800 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1801 const struct attribute *,
1802 struct dwarf2_cu **);
1803
1804 static struct die_info *follow_die_ref (struct die_info *,
1805 const struct attribute *,
1806 struct dwarf2_cu **);
1807
1808 static struct die_info *follow_die_sig (struct die_info *,
1809 const struct attribute *,
1810 struct dwarf2_cu **);
1811
1812 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1813 struct dwarf2_cu *);
1814
1815 static struct type *get_DW_AT_signature_type (struct die_info *,
1816 const struct attribute *,
1817 struct dwarf2_cu *);
1818
1819 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1820
1821 static void read_signatured_type (struct signatured_type *);
1822
1823 static int attr_to_dynamic_prop (const struct attribute *attr,
1824 struct die_info *die, struct dwarf2_cu *cu,
1825 struct dynamic_prop *prop, struct type *type);
1826
1827 /* memory allocation interface */
1828
1829 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1830
1831 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1832
1833 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1834
1835 static int attr_form_is_block (const struct attribute *);
1836
1837 static int attr_form_is_section_offset (const struct attribute *);
1838
1839 static int attr_form_is_constant (const struct attribute *);
1840
1841 static int attr_form_is_ref (const struct attribute *);
1842
1843 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1844 struct dwarf2_loclist_baton *baton,
1845 const struct attribute *attr);
1846
1847 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1848 struct symbol *sym,
1849 struct dwarf2_cu *cu,
1850 int is_block);
1851
1852 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1853 const gdb_byte *info_ptr,
1854 struct abbrev_info *abbrev);
1855
1856 static hashval_t partial_die_hash (const void *item);
1857
1858 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1859
1860 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1861 (sect_offset sect_off, unsigned int offset_in_dwz,
1862 struct dwarf2_per_objfile *dwarf2_per_objfile);
1863
1864 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1865 struct die_info *comp_unit_die,
1866 enum language pretend_language);
1867
1868 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1869
1870 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1871
1872 static struct type *set_die_type (struct die_info *, struct type *,
1873 struct dwarf2_cu *);
1874
1875 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1876
1877 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1878
1879 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1880 enum language);
1881
1882 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1883 enum language);
1884
1885 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1886 enum language);
1887
1888 static void dwarf2_add_dependence (struct dwarf2_cu *,
1889 struct dwarf2_per_cu_data *);
1890
1891 static void dwarf2_mark (struct dwarf2_cu *);
1892
1893 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1894
1895 static struct type *get_die_type_at_offset (sect_offset,
1896 struct dwarf2_per_cu_data *);
1897
1898 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1899
1900 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1901 enum language pretend_language);
1902
1903 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1904
1905 static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1906 static struct type *dwarf2_per_cu_addr_sized_int_type
1907 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1908 static struct type *dwarf2_per_cu_int_type
1909 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1910 bool unsigned_p);
1911
1912 /* Class, the destructor of which frees all allocated queue entries. This
1913 will only have work to do if an error was thrown while processing the
1914 dwarf. If no error was thrown then the queue entries should have all
1915 been processed, and freed, as we went along. */
1916
1917 class dwarf2_queue_guard
1918 {
1919 public:
1920 dwarf2_queue_guard () = default;
1921
1922 /* Free any entries remaining on the queue. There should only be
1923 entries left if we hit an error while processing the dwarf. */
1924 ~dwarf2_queue_guard ()
1925 {
1926 struct dwarf2_queue_item *item, *last;
1927
1928 item = dwarf2_queue;
1929 while (item)
1930 {
1931 /* Anything still marked queued is likely to be in an
1932 inconsistent state, so discard it. */
1933 if (item->per_cu->queued)
1934 {
1935 if (item->per_cu->cu != NULL)
1936 free_one_cached_comp_unit (item->per_cu);
1937 item->per_cu->queued = 0;
1938 }
1939
1940 last = item;
1941 item = item->next;
1942 xfree (last);
1943 }
1944
1945 dwarf2_queue = dwarf2_queue_tail = NULL;
1946 }
1947 };
1948
1949 /* The return type of find_file_and_directory. Note, the enclosed
1950 string pointers are only valid while this object is valid. */
1951
1952 struct file_and_directory
1953 {
1954 /* The filename. This is never NULL. */
1955 const char *name;
1956
1957 /* The compilation directory. NULL if not known. If we needed to
1958 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1959 points directly to the DW_AT_comp_dir string attribute owned by
1960 the obstack that owns the DIE. */
1961 const char *comp_dir;
1962
1963 /* If we needed to build a new string for comp_dir, this is what
1964 owns the storage. */
1965 std::string comp_dir_storage;
1966 };
1967
1968 static file_and_directory find_file_and_directory (struct die_info *die,
1969 struct dwarf2_cu *cu);
1970
1971 static char *file_full_name (int file, struct line_header *lh,
1972 const char *comp_dir);
1973
1974 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1975 enum class rcuh_kind { COMPILE, TYPE };
1976
1977 static const gdb_byte *read_and_check_comp_unit_head
1978 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1979 struct comp_unit_head *header,
1980 struct dwarf2_section_info *section,
1981 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1982 rcuh_kind section_kind);
1983
1984 static void init_cutu_and_read_dies
1985 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1986 int use_existing_cu, int keep, bool skip_partial,
1987 die_reader_func_ftype *die_reader_func, void *data);
1988
1989 static void init_cutu_and_read_dies_simple
1990 (struct dwarf2_per_cu_data *this_cu,
1991 die_reader_func_ftype *die_reader_func, void *data);
1992
1993 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1994
1995 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1996
1997 static struct dwo_unit *lookup_dwo_unit_in_dwp
1998 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1999 struct dwp_file *dwp_file, const char *comp_dir,
2000 ULONGEST signature, int is_debug_types);
2001
2002 static struct dwp_file *get_dwp_file
2003 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2004
2005 static struct dwo_unit *lookup_dwo_comp_unit
2006 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2007
2008 static struct dwo_unit *lookup_dwo_type_unit
2009 (struct signatured_type *, const char *, const char *);
2010
2011 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2012
2013 /* A unique pointer to a dwo_file. */
2014
2015 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
2016
2017 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2018
2019 static void check_producer (struct dwarf2_cu *cu);
2020
2021 static void free_line_header_voidp (void *arg);
2022 \f
2023 /* Various complaints about symbol reading that don't abort the process. */
2024
2025 static void
2026 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2027 {
2028 complaint (_("statement list doesn't fit in .debug_line section"));
2029 }
2030
2031 static void
2032 dwarf2_debug_line_missing_file_complaint (void)
2033 {
2034 complaint (_(".debug_line section has line data without a file"));
2035 }
2036
2037 static void
2038 dwarf2_debug_line_missing_end_sequence_complaint (void)
2039 {
2040 complaint (_(".debug_line section has line "
2041 "program sequence without an end"));
2042 }
2043
2044 static void
2045 dwarf2_complex_location_expr_complaint (void)
2046 {
2047 complaint (_("location expression too complex"));
2048 }
2049
2050 static void
2051 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2052 int arg3)
2053 {
2054 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
2055 arg1, arg2, arg3);
2056 }
2057
2058 static void
2059 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2060 {
2061 complaint (_("debug info runs off end of %s section"
2062 " [in module %s]"),
2063 get_section_name (section),
2064 get_section_file_name (section));
2065 }
2066
2067 static void
2068 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2069 {
2070 complaint (_("macro debug info contains a "
2071 "malformed macro definition:\n`%s'"),
2072 arg1);
2073 }
2074
2075 static void
2076 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2077 {
2078 complaint (_("invalid attribute class or form for '%s' in '%s'"),
2079 arg1, arg2);
2080 }
2081
2082 /* Hash function for line_header_hash. */
2083
2084 static hashval_t
2085 line_header_hash (const struct line_header *ofs)
2086 {
2087 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2088 }
2089
2090 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2091
2092 static hashval_t
2093 line_header_hash_voidp (const void *item)
2094 {
2095 const struct line_header *ofs = (const struct line_header *) item;
2096
2097 return line_header_hash (ofs);
2098 }
2099
2100 /* Equality function for line_header_hash. */
2101
2102 static int
2103 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2104 {
2105 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2106 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2107
2108 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2109 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2110 }
2111
2112 \f
2113
2114 /* Read the given attribute value as an address, taking the attribute's
2115 form into account. */
2116
2117 static CORE_ADDR
2118 attr_value_as_address (struct attribute *attr)
2119 {
2120 CORE_ADDR addr;
2121
2122 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2123 && attr->form != DW_FORM_GNU_addr_index)
2124 {
2125 /* Aside from a few clearly defined exceptions, attributes that
2126 contain an address must always be in DW_FORM_addr form.
2127 Unfortunately, some compilers happen to be violating this
2128 requirement by encoding addresses using other forms, such
2129 as DW_FORM_data4 for example. For those broken compilers,
2130 we try to do our best, without any guarantee of success,
2131 to interpret the address correctly. It would also be nice
2132 to generate a complaint, but that would require us to maintain
2133 a list of legitimate cases where a non-address form is allowed,
2134 as well as update callers to pass in at least the CU's DWARF
2135 version. This is more overhead than what we're willing to
2136 expand for a pretty rare case. */
2137 addr = DW_UNSND (attr);
2138 }
2139 else
2140 addr = DW_ADDR (attr);
2141
2142 return addr;
2143 }
2144
2145 /* See declaration. */
2146
2147 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2148 const dwarf2_debug_sections *names,
2149 bool can_copy_)
2150 : objfile (objfile_),
2151 can_copy (can_copy_)
2152 {
2153 if (names == NULL)
2154 names = &dwarf2_elf_names;
2155
2156 bfd *obfd = objfile->obfd;
2157
2158 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2159 locate_sections (obfd, sec, *names);
2160 }
2161
2162 dwarf2_per_objfile::~dwarf2_per_objfile ()
2163 {
2164 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2165 free_cached_comp_units ();
2166
2167 if (quick_file_names_table)
2168 htab_delete (quick_file_names_table);
2169
2170 if (line_header_hash)
2171 htab_delete (line_header_hash);
2172
2173 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2174 per_cu->imported_symtabs_free ();
2175
2176 for (signatured_type *sig_type : all_type_units)
2177 sig_type->per_cu.imported_symtabs_free ();
2178
2179 /* Everything else should be on the objfile obstack. */
2180 }
2181
2182 /* See declaration. */
2183
2184 void
2185 dwarf2_per_objfile::free_cached_comp_units ()
2186 {
2187 dwarf2_per_cu_data *per_cu = read_in_chain;
2188 dwarf2_per_cu_data **last_chain = &read_in_chain;
2189 while (per_cu != NULL)
2190 {
2191 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2192
2193 delete per_cu->cu;
2194 *last_chain = next_cu;
2195 per_cu = next_cu;
2196 }
2197 }
2198
2199 /* A helper class that calls free_cached_comp_units on
2200 destruction. */
2201
2202 class free_cached_comp_units
2203 {
2204 public:
2205
2206 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2207 : m_per_objfile (per_objfile)
2208 {
2209 }
2210
2211 ~free_cached_comp_units ()
2212 {
2213 m_per_objfile->free_cached_comp_units ();
2214 }
2215
2216 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2217
2218 private:
2219
2220 dwarf2_per_objfile *m_per_objfile;
2221 };
2222
2223 /* Try to locate the sections we need for DWARF 2 debugging
2224 information and return true if we have enough to do something.
2225 NAMES points to the dwarf2 section names, or is NULL if the standard
2226 ELF names are used. CAN_COPY is true for formats where symbol
2227 interposition is possible and so symbol values must follow copy
2228 relocation rules. */
2229
2230 int
2231 dwarf2_has_info (struct objfile *objfile,
2232 const struct dwarf2_debug_sections *names,
2233 bool can_copy)
2234 {
2235 if (objfile->flags & OBJF_READNEVER)
2236 return 0;
2237
2238 struct dwarf2_per_objfile *dwarf2_per_objfile
2239 = get_dwarf2_per_objfile (objfile);
2240
2241 if (dwarf2_per_objfile == NULL)
2242 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2243 names,
2244 can_copy);
2245
2246 return (!dwarf2_per_objfile->info.is_virtual
2247 && dwarf2_per_objfile->info.s.section != NULL
2248 && !dwarf2_per_objfile->abbrev.is_virtual
2249 && dwarf2_per_objfile->abbrev.s.section != NULL);
2250 }
2251
2252 /* Return the containing section of virtual section SECTION. */
2253
2254 static struct dwarf2_section_info *
2255 get_containing_section (const struct dwarf2_section_info *section)
2256 {
2257 gdb_assert (section->is_virtual);
2258 return section->s.containing_section;
2259 }
2260
2261 /* Return the bfd owner of SECTION. */
2262
2263 static struct bfd *
2264 get_section_bfd_owner (const struct dwarf2_section_info *section)
2265 {
2266 if (section->is_virtual)
2267 {
2268 section = get_containing_section (section);
2269 gdb_assert (!section->is_virtual);
2270 }
2271 return section->s.section->owner;
2272 }
2273
2274 /* Return the bfd section of SECTION.
2275 Returns NULL if the section is not present. */
2276
2277 static asection *
2278 get_section_bfd_section (const struct dwarf2_section_info *section)
2279 {
2280 if (section->is_virtual)
2281 {
2282 section = get_containing_section (section);
2283 gdb_assert (!section->is_virtual);
2284 }
2285 return section->s.section;
2286 }
2287
2288 /* Return the name of SECTION. */
2289
2290 static const char *
2291 get_section_name (const struct dwarf2_section_info *section)
2292 {
2293 asection *sectp = get_section_bfd_section (section);
2294
2295 gdb_assert (sectp != NULL);
2296 return bfd_section_name (sectp);
2297 }
2298
2299 /* Return the name of the file SECTION is in. */
2300
2301 static const char *
2302 get_section_file_name (const struct dwarf2_section_info *section)
2303 {
2304 bfd *abfd = get_section_bfd_owner (section);
2305
2306 return bfd_get_filename (abfd);
2307 }
2308
2309 /* Return the id of SECTION.
2310 Returns 0 if SECTION doesn't exist. */
2311
2312 static int
2313 get_section_id (const struct dwarf2_section_info *section)
2314 {
2315 asection *sectp = get_section_bfd_section (section);
2316
2317 if (sectp == NULL)
2318 return 0;
2319 return sectp->id;
2320 }
2321
2322 /* Return the flags of SECTION.
2323 SECTION (or containing section if this is a virtual section) must exist. */
2324
2325 static int
2326 get_section_flags (const struct dwarf2_section_info *section)
2327 {
2328 asection *sectp = get_section_bfd_section (section);
2329
2330 gdb_assert (sectp != NULL);
2331 return bfd_section_flags (sectp);
2332 }
2333
2334 /* When loading sections, we look either for uncompressed section or for
2335 compressed section names. */
2336
2337 static int
2338 section_is_p (const char *section_name,
2339 const struct dwarf2_section_names *names)
2340 {
2341 if (names->normal != NULL
2342 && strcmp (section_name, names->normal) == 0)
2343 return 1;
2344 if (names->compressed != NULL
2345 && strcmp (section_name, names->compressed) == 0)
2346 return 1;
2347 return 0;
2348 }
2349
2350 /* See declaration. */
2351
2352 void
2353 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2354 const dwarf2_debug_sections &names)
2355 {
2356 flagword aflag = bfd_section_flags (sectp);
2357
2358 if ((aflag & SEC_HAS_CONTENTS) == 0)
2359 {
2360 }
2361 else if (elf_section_data (sectp)->this_hdr.sh_size
2362 > bfd_get_file_size (abfd))
2363 {
2364 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2365 warning (_("Discarding section %s which has a section size (%s"
2366 ") larger than the file size [in module %s]"),
2367 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2368 bfd_get_filename (abfd));
2369 }
2370 else if (section_is_p (sectp->name, &names.info))
2371 {
2372 this->info.s.section = sectp;
2373 this->info.size = bfd_section_size (sectp);
2374 }
2375 else if (section_is_p (sectp->name, &names.abbrev))
2376 {
2377 this->abbrev.s.section = sectp;
2378 this->abbrev.size = bfd_section_size (sectp);
2379 }
2380 else if (section_is_p (sectp->name, &names.line))
2381 {
2382 this->line.s.section = sectp;
2383 this->line.size = bfd_section_size (sectp);
2384 }
2385 else if (section_is_p (sectp->name, &names.loc))
2386 {
2387 this->loc.s.section = sectp;
2388 this->loc.size = bfd_section_size (sectp);
2389 }
2390 else if (section_is_p (sectp->name, &names.loclists))
2391 {
2392 this->loclists.s.section = sectp;
2393 this->loclists.size = bfd_section_size (sectp);
2394 }
2395 else if (section_is_p (sectp->name, &names.macinfo))
2396 {
2397 this->macinfo.s.section = sectp;
2398 this->macinfo.size = bfd_section_size (sectp);
2399 }
2400 else if (section_is_p (sectp->name, &names.macro))
2401 {
2402 this->macro.s.section = sectp;
2403 this->macro.size = bfd_section_size (sectp);
2404 }
2405 else if (section_is_p (sectp->name, &names.str))
2406 {
2407 this->str.s.section = sectp;
2408 this->str.size = bfd_section_size (sectp);
2409 }
2410 else if (section_is_p (sectp->name, &names.line_str))
2411 {
2412 this->line_str.s.section = sectp;
2413 this->line_str.size = bfd_section_size (sectp);
2414 }
2415 else if (section_is_p (sectp->name, &names.addr))
2416 {
2417 this->addr.s.section = sectp;
2418 this->addr.size = bfd_section_size (sectp);
2419 }
2420 else if (section_is_p (sectp->name, &names.frame))
2421 {
2422 this->frame.s.section = sectp;
2423 this->frame.size = bfd_section_size (sectp);
2424 }
2425 else if (section_is_p (sectp->name, &names.eh_frame))
2426 {
2427 this->eh_frame.s.section = sectp;
2428 this->eh_frame.size = bfd_section_size (sectp);
2429 }
2430 else if (section_is_p (sectp->name, &names.ranges))
2431 {
2432 this->ranges.s.section = sectp;
2433 this->ranges.size = bfd_section_size (sectp);
2434 }
2435 else if (section_is_p (sectp->name, &names.rnglists))
2436 {
2437 this->rnglists.s.section = sectp;
2438 this->rnglists.size = bfd_section_size (sectp);
2439 }
2440 else if (section_is_p (sectp->name, &names.types))
2441 {
2442 struct dwarf2_section_info type_section;
2443
2444 memset (&type_section, 0, sizeof (type_section));
2445 type_section.s.section = sectp;
2446 type_section.size = bfd_section_size (sectp);
2447
2448 this->types.push_back (type_section);
2449 }
2450 else if (section_is_p (sectp->name, &names.gdb_index))
2451 {
2452 this->gdb_index.s.section = sectp;
2453 this->gdb_index.size = bfd_section_size (sectp);
2454 }
2455 else if (section_is_p (sectp->name, &names.debug_names))
2456 {
2457 this->debug_names.s.section = sectp;
2458 this->debug_names.size = bfd_section_size (sectp);
2459 }
2460 else if (section_is_p (sectp->name, &names.debug_aranges))
2461 {
2462 this->debug_aranges.s.section = sectp;
2463 this->debug_aranges.size = bfd_section_size (sectp);
2464 }
2465
2466 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2467 && bfd_section_vma (sectp) == 0)
2468 this->has_section_at_zero = true;
2469 }
2470
2471 /* A helper function that decides whether a section is empty,
2472 or not present. */
2473
2474 static int
2475 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2476 {
2477 if (section->is_virtual)
2478 return section->size == 0;
2479 return section->s.section == NULL || section->size == 0;
2480 }
2481
2482 /* See dwarf2read.h. */
2483
2484 void
2485 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2486 {
2487 asection *sectp;
2488 bfd *abfd;
2489 gdb_byte *buf, *retbuf;
2490
2491 if (info->readin)
2492 return;
2493 info->buffer = NULL;
2494 info->readin = true;
2495
2496 if (dwarf2_section_empty_p (info))
2497 return;
2498
2499 sectp = get_section_bfd_section (info);
2500
2501 /* If this is a virtual section we need to read in the real one first. */
2502 if (info->is_virtual)
2503 {
2504 struct dwarf2_section_info *containing_section =
2505 get_containing_section (info);
2506
2507 gdb_assert (sectp != NULL);
2508 if ((sectp->flags & SEC_RELOC) != 0)
2509 {
2510 error (_("Dwarf Error: DWP format V2 with relocations is not"
2511 " supported in section %s [in module %s]"),
2512 get_section_name (info), get_section_file_name (info));
2513 }
2514 dwarf2_read_section (objfile, containing_section);
2515 /* Other code should have already caught virtual sections that don't
2516 fit. */
2517 gdb_assert (info->virtual_offset + info->size
2518 <= containing_section->size);
2519 /* If the real section is empty or there was a problem reading the
2520 section we shouldn't get here. */
2521 gdb_assert (containing_section->buffer != NULL);
2522 info->buffer = containing_section->buffer + info->virtual_offset;
2523 return;
2524 }
2525
2526 /* If the section has relocations, we must read it ourselves.
2527 Otherwise we attach it to the BFD. */
2528 if ((sectp->flags & SEC_RELOC) == 0)
2529 {
2530 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2531 return;
2532 }
2533
2534 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2535 info->buffer = buf;
2536
2537 /* When debugging .o files, we may need to apply relocations; see
2538 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2539 We never compress sections in .o files, so we only need to
2540 try this when the section is not compressed. */
2541 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2542 if (retbuf != NULL)
2543 {
2544 info->buffer = retbuf;
2545 return;
2546 }
2547
2548 abfd = get_section_bfd_owner (info);
2549 gdb_assert (abfd != NULL);
2550
2551 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2552 || bfd_bread (buf, info->size, abfd) != info->size)
2553 {
2554 error (_("Dwarf Error: Can't read DWARF data"
2555 " in section %s [in module %s]"),
2556 bfd_section_name (sectp), bfd_get_filename (abfd));
2557 }
2558 }
2559
2560 /* A helper function that returns the size of a section in a safe way.
2561 If you are positive that the section has been read before using the
2562 size, then it is safe to refer to the dwarf2_section_info object's
2563 "size" field directly. In other cases, you must call this
2564 function, because for compressed sections the size field is not set
2565 correctly until the section has been read. */
2566
2567 static bfd_size_type
2568 dwarf2_section_size (struct objfile *objfile,
2569 struct dwarf2_section_info *info)
2570 {
2571 if (!info->readin)
2572 dwarf2_read_section (objfile, info);
2573 return info->size;
2574 }
2575
2576 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2577 SECTION_NAME. */
2578
2579 void
2580 dwarf2_get_section_info (struct objfile *objfile,
2581 enum dwarf2_section_enum sect,
2582 asection **sectp, const gdb_byte **bufp,
2583 bfd_size_type *sizep)
2584 {
2585 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2586 struct dwarf2_section_info *info;
2587
2588 /* We may see an objfile without any DWARF, in which case we just
2589 return nothing. */
2590 if (data == NULL)
2591 {
2592 *sectp = NULL;
2593 *bufp = NULL;
2594 *sizep = 0;
2595 return;
2596 }
2597 switch (sect)
2598 {
2599 case DWARF2_DEBUG_FRAME:
2600 info = &data->frame;
2601 break;
2602 case DWARF2_EH_FRAME:
2603 info = &data->eh_frame;
2604 break;
2605 default:
2606 gdb_assert_not_reached ("unexpected section");
2607 }
2608
2609 dwarf2_read_section (objfile, info);
2610
2611 *sectp = get_section_bfd_section (info);
2612 *bufp = info->buffer;
2613 *sizep = info->size;
2614 }
2615
2616 /* A helper function to find the sections for a .dwz file. */
2617
2618 static void
2619 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2620 {
2621 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2622
2623 /* Note that we only support the standard ELF names, because .dwz
2624 is ELF-only (at the time of writing). */
2625 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2626 {
2627 dwz_file->abbrev.s.section = sectp;
2628 dwz_file->abbrev.size = bfd_section_size (sectp);
2629 }
2630 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2631 {
2632 dwz_file->info.s.section = sectp;
2633 dwz_file->info.size = bfd_section_size (sectp);
2634 }
2635 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2636 {
2637 dwz_file->str.s.section = sectp;
2638 dwz_file->str.size = bfd_section_size (sectp);
2639 }
2640 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2641 {
2642 dwz_file->line.s.section = sectp;
2643 dwz_file->line.size = bfd_section_size (sectp);
2644 }
2645 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2646 {
2647 dwz_file->macro.s.section = sectp;
2648 dwz_file->macro.size = bfd_section_size (sectp);
2649 }
2650 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2651 {
2652 dwz_file->gdb_index.s.section = sectp;
2653 dwz_file->gdb_index.size = bfd_section_size (sectp);
2654 }
2655 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2656 {
2657 dwz_file->debug_names.s.section = sectp;
2658 dwz_file->debug_names.size = bfd_section_size (sectp);
2659 }
2660 }
2661
2662 /* See dwarf2read.h. */
2663
2664 struct dwz_file *
2665 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2666 {
2667 const char *filename;
2668 bfd_size_type buildid_len_arg;
2669 size_t buildid_len;
2670 bfd_byte *buildid;
2671
2672 if (dwarf2_per_objfile->dwz_file != NULL)
2673 return dwarf2_per_objfile->dwz_file.get ();
2674
2675 bfd_set_error (bfd_error_no_error);
2676 gdb::unique_xmalloc_ptr<char> data
2677 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2678 &buildid_len_arg, &buildid));
2679 if (data == NULL)
2680 {
2681 if (bfd_get_error () == bfd_error_no_error)
2682 return NULL;
2683 error (_("could not read '.gnu_debugaltlink' section: %s"),
2684 bfd_errmsg (bfd_get_error ()));
2685 }
2686
2687 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2688
2689 buildid_len = (size_t) buildid_len_arg;
2690
2691 filename = data.get ();
2692
2693 std::string abs_storage;
2694 if (!IS_ABSOLUTE_PATH (filename))
2695 {
2696 gdb::unique_xmalloc_ptr<char> abs
2697 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2698
2699 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2700 filename = abs_storage.c_str ();
2701 }
2702
2703 /* First try the file name given in the section. If that doesn't
2704 work, try to use the build-id instead. */
2705 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2706 if (dwz_bfd != NULL)
2707 {
2708 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2709 dwz_bfd.reset (nullptr);
2710 }
2711
2712 if (dwz_bfd == NULL)
2713 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2714
2715 if (dwz_bfd == NULL)
2716 error (_("could not find '.gnu_debugaltlink' file for %s"),
2717 objfile_name (dwarf2_per_objfile->objfile));
2718
2719 std::unique_ptr<struct dwz_file> result
2720 (new struct dwz_file (std::move (dwz_bfd)));
2721
2722 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2723 result.get ());
2724
2725 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2726 result->dwz_bfd.get ());
2727 dwarf2_per_objfile->dwz_file = std::move (result);
2728 return dwarf2_per_objfile->dwz_file.get ();
2729 }
2730 \f
2731 /* DWARF quick_symbols_functions support. */
2732
2733 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2734 unique line tables, so we maintain a separate table of all .debug_line
2735 derived entries to support the sharing.
2736 All the quick functions need is the list of file names. We discard the
2737 line_header when we're done and don't need to record it here. */
2738 struct quick_file_names
2739 {
2740 /* The data used to construct the hash key. */
2741 struct stmt_list_hash hash;
2742
2743 /* The number of entries in file_names, real_names. */
2744 unsigned int num_file_names;
2745
2746 /* The file names from the line table, after being run through
2747 file_full_name. */
2748 const char **file_names;
2749
2750 /* The file names from the line table after being run through
2751 gdb_realpath. These are computed lazily. */
2752 const char **real_names;
2753 };
2754
2755 /* When using the index (and thus not using psymtabs), each CU has an
2756 object of this type. This is used to hold information needed by
2757 the various "quick" methods. */
2758 struct dwarf2_per_cu_quick_data
2759 {
2760 /* The file table. This can be NULL if there was no file table
2761 or it's currently not read in.
2762 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2763 struct quick_file_names *file_names;
2764
2765 /* The corresponding symbol table. This is NULL if symbols for this
2766 CU have not yet been read. */
2767 struct compunit_symtab *compunit_symtab;
2768
2769 /* A temporary mark bit used when iterating over all CUs in
2770 expand_symtabs_matching. */
2771 unsigned int mark : 1;
2772
2773 /* True if we've tried to read the file table and found there isn't one.
2774 There will be no point in trying to read it again next time. */
2775 unsigned int no_file_data : 1;
2776 };
2777
2778 /* Utility hash function for a stmt_list_hash. */
2779
2780 static hashval_t
2781 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2782 {
2783 hashval_t v = 0;
2784
2785 if (stmt_list_hash->dwo_unit != NULL)
2786 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2787 v += to_underlying (stmt_list_hash->line_sect_off);
2788 return v;
2789 }
2790
2791 /* Utility equality function for a stmt_list_hash. */
2792
2793 static int
2794 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2795 const struct stmt_list_hash *rhs)
2796 {
2797 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2798 return 0;
2799 if (lhs->dwo_unit != NULL
2800 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2801 return 0;
2802
2803 return lhs->line_sect_off == rhs->line_sect_off;
2804 }
2805
2806 /* Hash function for a quick_file_names. */
2807
2808 static hashval_t
2809 hash_file_name_entry (const void *e)
2810 {
2811 const struct quick_file_names *file_data
2812 = (const struct quick_file_names *) e;
2813
2814 return hash_stmt_list_entry (&file_data->hash);
2815 }
2816
2817 /* Equality function for a quick_file_names. */
2818
2819 static int
2820 eq_file_name_entry (const void *a, const void *b)
2821 {
2822 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2823 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2824
2825 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2826 }
2827
2828 /* Delete function for a quick_file_names. */
2829
2830 static void
2831 delete_file_name_entry (void *e)
2832 {
2833 struct quick_file_names *file_data = (struct quick_file_names *) e;
2834 int i;
2835
2836 for (i = 0; i < file_data->num_file_names; ++i)
2837 {
2838 xfree ((void*) file_data->file_names[i]);
2839 if (file_data->real_names)
2840 xfree ((void*) file_data->real_names[i]);
2841 }
2842
2843 /* The space for the struct itself lives on objfile_obstack,
2844 so we don't free it here. */
2845 }
2846
2847 /* Create a quick_file_names hash table. */
2848
2849 static htab_t
2850 create_quick_file_names_table (unsigned int nr_initial_entries)
2851 {
2852 return htab_create_alloc (nr_initial_entries,
2853 hash_file_name_entry, eq_file_name_entry,
2854 delete_file_name_entry, xcalloc, xfree);
2855 }
2856
2857 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2858 have to be created afterwards. You should call age_cached_comp_units after
2859 processing PER_CU->CU. dw2_setup must have been already called. */
2860
2861 static void
2862 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2863 {
2864 if (per_cu->is_debug_types)
2865 load_full_type_unit (per_cu);
2866 else
2867 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2868
2869 if (per_cu->cu == NULL)
2870 return; /* Dummy CU. */
2871
2872 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2873 }
2874
2875 /* Read in the symbols for PER_CU. */
2876
2877 static void
2878 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2879 {
2880 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2881
2882 /* Skip type_unit_groups, reading the type units they contain
2883 is handled elsewhere. */
2884 if (IS_TYPE_UNIT_GROUP (per_cu))
2885 return;
2886
2887 /* The destructor of dwarf2_queue_guard frees any entries left on
2888 the queue. After this point we're guaranteed to leave this function
2889 with the dwarf queue empty. */
2890 dwarf2_queue_guard q_guard;
2891
2892 if (dwarf2_per_objfile->using_index
2893 ? per_cu->v.quick->compunit_symtab == NULL
2894 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2895 {
2896 queue_comp_unit (per_cu, language_minimal);
2897 load_cu (per_cu, skip_partial);
2898
2899 /* If we just loaded a CU from a DWO, and we're working with an index
2900 that may badly handle TUs, load all the TUs in that DWO as well.
2901 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2902 if (!per_cu->is_debug_types
2903 && per_cu->cu != NULL
2904 && per_cu->cu->dwo_unit != NULL
2905 && dwarf2_per_objfile->index_table != NULL
2906 && dwarf2_per_objfile->index_table->version <= 7
2907 /* DWP files aren't supported yet. */
2908 && get_dwp_file (dwarf2_per_objfile) == NULL)
2909 queue_and_load_all_dwo_tus (per_cu);
2910 }
2911
2912 process_queue (dwarf2_per_objfile);
2913
2914 /* Age the cache, releasing compilation units that have not
2915 been used recently. */
2916 age_cached_comp_units (dwarf2_per_objfile);
2917 }
2918
2919 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2920 the objfile from which this CU came. Returns the resulting symbol
2921 table. */
2922
2923 static struct compunit_symtab *
2924 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2925 {
2926 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2927
2928 gdb_assert (dwarf2_per_objfile->using_index);
2929 if (!per_cu->v.quick->compunit_symtab)
2930 {
2931 free_cached_comp_units freer (dwarf2_per_objfile);
2932 scoped_restore decrementer = increment_reading_symtab ();
2933 dw2_do_instantiate_symtab (per_cu, skip_partial);
2934 process_cu_includes (dwarf2_per_objfile);
2935 }
2936
2937 return per_cu->v.quick->compunit_symtab;
2938 }
2939
2940 /* See declaration. */
2941
2942 dwarf2_per_cu_data *
2943 dwarf2_per_objfile::get_cutu (int index)
2944 {
2945 if (index >= this->all_comp_units.size ())
2946 {
2947 index -= this->all_comp_units.size ();
2948 gdb_assert (index < this->all_type_units.size ());
2949 return &this->all_type_units[index]->per_cu;
2950 }
2951
2952 return this->all_comp_units[index];
2953 }
2954
2955 /* See declaration. */
2956
2957 dwarf2_per_cu_data *
2958 dwarf2_per_objfile::get_cu (int index)
2959 {
2960 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2961
2962 return this->all_comp_units[index];
2963 }
2964
2965 /* See declaration. */
2966
2967 signatured_type *
2968 dwarf2_per_objfile::get_tu (int index)
2969 {
2970 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2971
2972 return this->all_type_units[index];
2973 }
2974
2975 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2976 objfile_obstack, and constructed with the specified field
2977 values. */
2978
2979 static dwarf2_per_cu_data *
2980 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2981 struct dwarf2_section_info *section,
2982 int is_dwz,
2983 sect_offset sect_off, ULONGEST length)
2984 {
2985 struct objfile *objfile = dwarf2_per_objfile->objfile;
2986 dwarf2_per_cu_data *the_cu
2987 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2988 struct dwarf2_per_cu_data);
2989 the_cu->sect_off = sect_off;
2990 the_cu->length = length;
2991 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2992 the_cu->section = section;
2993 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2994 struct dwarf2_per_cu_quick_data);
2995 the_cu->is_dwz = is_dwz;
2996 return the_cu;
2997 }
2998
2999 /* A helper for create_cus_from_index that handles a given list of
3000 CUs. */
3001
3002 static void
3003 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3004 const gdb_byte *cu_list, offset_type n_elements,
3005 struct dwarf2_section_info *section,
3006 int is_dwz)
3007 {
3008 for (offset_type i = 0; i < n_elements; i += 2)
3009 {
3010 gdb_static_assert (sizeof (ULONGEST) >= 8);
3011
3012 sect_offset sect_off
3013 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3014 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3015 cu_list += 2 * 8;
3016
3017 dwarf2_per_cu_data *per_cu
3018 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3019 sect_off, length);
3020 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
3021 }
3022 }
3023
3024 /* Read the CU list from the mapped index, and use it to create all
3025 the CU objects for this objfile. */
3026
3027 static void
3028 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3029 const gdb_byte *cu_list, offset_type cu_list_elements,
3030 const gdb_byte *dwz_list, offset_type dwz_elements)
3031 {
3032 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3033 dwarf2_per_objfile->all_comp_units.reserve
3034 ((cu_list_elements + dwz_elements) / 2);
3035
3036 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3037 &dwarf2_per_objfile->info, 0);
3038
3039 if (dwz_elements == 0)
3040 return;
3041
3042 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3043 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3044 &dwz->info, 1);
3045 }
3046
3047 /* Create the signatured type hash table from the index. */
3048
3049 static void
3050 create_signatured_type_table_from_index
3051 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3052 struct dwarf2_section_info *section,
3053 const gdb_byte *bytes,
3054 offset_type elements)
3055 {
3056 struct objfile *objfile = dwarf2_per_objfile->objfile;
3057
3058 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3059 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
3060
3061 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3062
3063 for (offset_type i = 0; i < elements; i += 3)
3064 {
3065 struct signatured_type *sig_type;
3066 ULONGEST signature;
3067 void **slot;
3068 cu_offset type_offset_in_tu;
3069
3070 gdb_static_assert (sizeof (ULONGEST) >= 8);
3071 sect_offset sect_off
3072 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3073 type_offset_in_tu
3074 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3075 BFD_ENDIAN_LITTLE);
3076 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3077 bytes += 3 * 8;
3078
3079 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3080 struct signatured_type);
3081 sig_type->signature = signature;
3082 sig_type->type_offset_in_tu = type_offset_in_tu;
3083 sig_type->per_cu.is_debug_types = 1;
3084 sig_type->per_cu.section = section;
3085 sig_type->per_cu.sect_off = sect_off;
3086 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3087 sig_type->per_cu.v.quick
3088 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3089 struct dwarf2_per_cu_quick_data);
3090
3091 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3092 *slot = sig_type;
3093
3094 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3095 }
3096
3097 dwarf2_per_objfile->signatured_types = sig_types_hash;
3098 }
3099
3100 /* Create the signatured type hash table from .debug_names. */
3101
3102 static void
3103 create_signatured_type_table_from_debug_names
3104 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3105 const mapped_debug_names &map,
3106 struct dwarf2_section_info *section,
3107 struct dwarf2_section_info *abbrev_section)
3108 {
3109 struct objfile *objfile = dwarf2_per_objfile->objfile;
3110
3111 dwarf2_read_section (objfile, section);
3112 dwarf2_read_section (objfile, abbrev_section);
3113
3114 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3115 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
3116
3117 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3118
3119 for (uint32_t i = 0; i < map.tu_count; ++i)
3120 {
3121 struct signatured_type *sig_type;
3122 void **slot;
3123
3124 sect_offset sect_off
3125 = (sect_offset) (extract_unsigned_integer
3126 (map.tu_table_reordered + i * map.offset_size,
3127 map.offset_size,
3128 map.dwarf5_byte_order));
3129
3130 comp_unit_head cu_header;
3131 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3132 abbrev_section,
3133 section->buffer + to_underlying (sect_off),
3134 rcuh_kind::TYPE);
3135
3136 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3137 struct signatured_type);
3138 sig_type->signature = cu_header.signature;
3139 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3140 sig_type->per_cu.is_debug_types = 1;
3141 sig_type->per_cu.section = section;
3142 sig_type->per_cu.sect_off = sect_off;
3143 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3144 sig_type->per_cu.v.quick
3145 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3146 struct dwarf2_per_cu_quick_data);
3147
3148 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3149 *slot = sig_type;
3150
3151 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3152 }
3153
3154 dwarf2_per_objfile->signatured_types = sig_types_hash;
3155 }
3156
3157 /* Read the address map data from the mapped index, and use it to
3158 populate the objfile's psymtabs_addrmap. */
3159
3160 static void
3161 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3162 struct mapped_index *index)
3163 {
3164 struct objfile *objfile = dwarf2_per_objfile->objfile;
3165 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3166 const gdb_byte *iter, *end;
3167 struct addrmap *mutable_map;
3168 CORE_ADDR baseaddr;
3169
3170 auto_obstack temp_obstack;
3171
3172 mutable_map = addrmap_create_mutable (&temp_obstack);
3173
3174 iter = index->address_table.data ();
3175 end = iter + index->address_table.size ();
3176
3177 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3178
3179 while (iter < end)
3180 {
3181 ULONGEST hi, lo, cu_index;
3182 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3183 iter += 8;
3184 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3185 iter += 8;
3186 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3187 iter += 4;
3188
3189 if (lo > hi)
3190 {
3191 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
3192 hex_string (lo), hex_string (hi));
3193 continue;
3194 }
3195
3196 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
3197 {
3198 complaint (_(".gdb_index address table has invalid CU number %u"),
3199 (unsigned) cu_index);
3200 continue;
3201 }
3202
3203 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3204 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
3205 addrmap_set_empty (mutable_map, lo, hi - 1,
3206 dwarf2_per_objfile->get_cu (cu_index));
3207 }
3208
3209 objfile->partial_symtabs->psymtabs_addrmap
3210 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3211 }
3212
3213 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3214 populate the objfile's psymtabs_addrmap. */
3215
3216 static void
3217 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3218 struct dwarf2_section_info *section)
3219 {
3220 struct objfile *objfile = dwarf2_per_objfile->objfile;
3221 bfd *abfd = objfile->obfd;
3222 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3223 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3224 SECT_OFF_TEXT (objfile));
3225
3226 auto_obstack temp_obstack;
3227 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3228
3229 std::unordered_map<sect_offset,
3230 dwarf2_per_cu_data *,
3231 gdb::hash_enum<sect_offset>>
3232 debug_info_offset_to_per_cu;
3233 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3234 {
3235 const auto insertpair
3236 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3237 if (!insertpair.second)
3238 {
3239 warning (_("Section .debug_aranges in %s has duplicate "
3240 "debug_info_offset %s, ignoring .debug_aranges."),
3241 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3242 return;
3243 }
3244 }
3245
3246 dwarf2_read_section (objfile, section);
3247
3248 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3249
3250 const gdb_byte *addr = section->buffer;
3251
3252 while (addr < section->buffer + section->size)
3253 {
3254 const gdb_byte *const entry_addr = addr;
3255 unsigned int bytes_read;
3256
3257 const LONGEST entry_length = read_initial_length (abfd, addr,
3258 &bytes_read);
3259 addr += bytes_read;
3260
3261 const gdb_byte *const entry_end = addr + entry_length;
3262 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3263 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3264 if (addr + entry_length > section->buffer + section->size)
3265 {
3266 warning (_("Section .debug_aranges in %s entry at offset %s "
3267 "length %s exceeds section length %s, "
3268 "ignoring .debug_aranges."),
3269 objfile_name (objfile),
3270 plongest (entry_addr - section->buffer),
3271 plongest (bytes_read + entry_length),
3272 pulongest (section->size));
3273 return;
3274 }
3275
3276 /* The version number. */
3277 const uint16_t version = read_2_bytes (abfd, addr);
3278 addr += 2;
3279 if (version != 2)
3280 {
3281 warning (_("Section .debug_aranges in %s entry at offset %s "
3282 "has unsupported version %d, ignoring .debug_aranges."),
3283 objfile_name (objfile),
3284 plongest (entry_addr - section->buffer), version);
3285 return;
3286 }
3287
3288 const uint64_t debug_info_offset
3289 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3290 addr += offset_size;
3291 const auto per_cu_it
3292 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3293 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3294 {
3295 warning (_("Section .debug_aranges in %s entry at offset %s "
3296 "debug_info_offset %s does not exists, "
3297 "ignoring .debug_aranges."),
3298 objfile_name (objfile),
3299 plongest (entry_addr - section->buffer),
3300 pulongest (debug_info_offset));
3301 return;
3302 }
3303 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3304
3305 const uint8_t address_size = *addr++;
3306 if (address_size < 1 || address_size > 8)
3307 {
3308 warning (_("Section .debug_aranges in %s entry at offset %s "
3309 "address_size %u is invalid, ignoring .debug_aranges."),
3310 objfile_name (objfile),
3311 plongest (entry_addr - section->buffer), address_size);
3312 return;
3313 }
3314
3315 const uint8_t segment_selector_size = *addr++;
3316 if (segment_selector_size != 0)
3317 {
3318 warning (_("Section .debug_aranges in %s entry at offset %s "
3319 "segment_selector_size %u is not supported, "
3320 "ignoring .debug_aranges."),
3321 objfile_name (objfile),
3322 plongest (entry_addr - section->buffer),
3323 segment_selector_size);
3324 return;
3325 }
3326
3327 /* Must pad to an alignment boundary that is twice the address
3328 size. It is undocumented by the DWARF standard but GCC does
3329 use it. */
3330 for (size_t padding = ((-(addr - section->buffer))
3331 & (2 * address_size - 1));
3332 padding > 0; padding--)
3333 if (*addr++ != 0)
3334 {
3335 warning (_("Section .debug_aranges in %s entry at offset %s "
3336 "padding is not zero, ignoring .debug_aranges."),
3337 objfile_name (objfile),
3338 plongest (entry_addr - section->buffer));
3339 return;
3340 }
3341
3342 for (;;)
3343 {
3344 if (addr + 2 * address_size > entry_end)
3345 {
3346 warning (_("Section .debug_aranges in %s entry at offset %s "
3347 "address list is not properly terminated, "
3348 "ignoring .debug_aranges."),
3349 objfile_name (objfile),
3350 plongest (entry_addr - section->buffer));
3351 return;
3352 }
3353 ULONGEST start = extract_unsigned_integer (addr, address_size,
3354 dwarf5_byte_order);
3355 addr += address_size;
3356 ULONGEST length = extract_unsigned_integer (addr, address_size,
3357 dwarf5_byte_order);
3358 addr += address_size;
3359 if (start == 0 && length == 0)
3360 break;
3361 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3362 {
3363 /* Symbol was eliminated due to a COMDAT group. */
3364 continue;
3365 }
3366 ULONGEST end = start + length;
3367 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3368 - baseaddr);
3369 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3370 - baseaddr);
3371 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3372 }
3373 }
3374
3375 objfile->partial_symtabs->psymtabs_addrmap
3376 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3377 }
3378
3379 /* Find a slot in the mapped index INDEX for the object named NAME.
3380 If NAME is found, set *VEC_OUT to point to the CU vector in the
3381 constant pool and return true. If NAME cannot be found, return
3382 false. */
3383
3384 static bool
3385 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3386 offset_type **vec_out)
3387 {
3388 offset_type hash;
3389 offset_type slot, step;
3390 int (*cmp) (const char *, const char *);
3391
3392 gdb::unique_xmalloc_ptr<char> without_params;
3393 if (current_language->la_language == language_cplus
3394 || current_language->la_language == language_fortran
3395 || current_language->la_language == language_d)
3396 {
3397 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3398 not contain any. */
3399
3400 if (strchr (name, '(') != NULL)
3401 {
3402 without_params = cp_remove_params (name);
3403
3404 if (without_params != NULL)
3405 name = without_params.get ();
3406 }
3407 }
3408
3409 /* Index version 4 did not support case insensitive searches. But the
3410 indices for case insensitive languages are built in lowercase, therefore
3411 simulate our NAME being searched is also lowercased. */
3412 hash = mapped_index_string_hash ((index->version == 4
3413 && case_sensitivity == case_sensitive_off
3414 ? 5 : index->version),
3415 name);
3416
3417 slot = hash & (index->symbol_table.size () - 1);
3418 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3419 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3420
3421 for (;;)
3422 {
3423 const char *str;
3424
3425 const auto &bucket = index->symbol_table[slot];
3426 if (bucket.name == 0 && bucket.vec == 0)
3427 return false;
3428
3429 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3430 if (!cmp (name, str))
3431 {
3432 *vec_out = (offset_type *) (index->constant_pool
3433 + MAYBE_SWAP (bucket.vec));
3434 return true;
3435 }
3436
3437 slot = (slot + step) & (index->symbol_table.size () - 1);
3438 }
3439 }
3440
3441 /* A helper function that reads the .gdb_index from BUFFER and fills
3442 in MAP. FILENAME is the name of the file containing the data;
3443 it is used for error reporting. DEPRECATED_OK is true if it is
3444 ok to use deprecated sections.
3445
3446 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3447 out parameters that are filled in with information about the CU and
3448 TU lists in the section.
3449
3450 Returns true if all went well, false otherwise. */
3451
3452 static bool
3453 read_gdb_index_from_buffer (struct objfile *objfile,
3454 const char *filename,
3455 bool deprecated_ok,
3456 gdb::array_view<const gdb_byte> buffer,
3457 struct mapped_index *map,
3458 const gdb_byte **cu_list,
3459 offset_type *cu_list_elements,
3460 const gdb_byte **types_list,
3461 offset_type *types_list_elements)
3462 {
3463 const gdb_byte *addr = &buffer[0];
3464
3465 /* Version check. */
3466 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3467 /* Versions earlier than 3 emitted every copy of a psymbol. This
3468 causes the index to behave very poorly for certain requests. Version 3
3469 contained incomplete addrmap. So, it seems better to just ignore such
3470 indices. */
3471 if (version < 4)
3472 {
3473 static int warning_printed = 0;
3474 if (!warning_printed)
3475 {
3476 warning (_("Skipping obsolete .gdb_index section in %s."),
3477 filename);
3478 warning_printed = 1;
3479 }
3480 return 0;
3481 }
3482 /* Index version 4 uses a different hash function than index version
3483 5 and later.
3484
3485 Versions earlier than 6 did not emit psymbols for inlined
3486 functions. Using these files will cause GDB not to be able to
3487 set breakpoints on inlined functions by name, so we ignore these
3488 indices unless the user has done
3489 "set use-deprecated-index-sections on". */
3490 if (version < 6 && !deprecated_ok)
3491 {
3492 static int warning_printed = 0;
3493 if (!warning_printed)
3494 {
3495 warning (_("\
3496 Skipping deprecated .gdb_index section in %s.\n\
3497 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3498 to use the section anyway."),
3499 filename);
3500 warning_printed = 1;
3501 }
3502 return 0;
3503 }
3504 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3505 of the TU (for symbols coming from TUs),
3506 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3507 Plus gold-generated indices can have duplicate entries for global symbols,
3508 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3509 These are just performance bugs, and we can't distinguish gdb-generated
3510 indices from gold-generated ones, so issue no warning here. */
3511
3512 /* Indexes with higher version than the one supported by GDB may be no
3513 longer backward compatible. */
3514 if (version > 8)
3515 return 0;
3516
3517 map->version = version;
3518
3519 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3520
3521 int i = 0;
3522 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3523 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3524 / 8);
3525 ++i;
3526
3527 *types_list = addr + MAYBE_SWAP (metadata[i]);
3528 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3529 - MAYBE_SWAP (metadata[i]))
3530 / 8);
3531 ++i;
3532
3533 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3534 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3535 map->address_table
3536 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3537 ++i;
3538
3539 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3540 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3541 map->symbol_table
3542 = gdb::array_view<mapped_index::symbol_table_slot>
3543 ((mapped_index::symbol_table_slot *) symbol_table,
3544 (mapped_index::symbol_table_slot *) symbol_table_end);
3545
3546 ++i;
3547 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3548
3549 return 1;
3550 }
3551
3552 /* Callback types for dwarf2_read_gdb_index. */
3553
3554 typedef gdb::function_view
3555 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3556 get_gdb_index_contents_ftype;
3557 typedef gdb::function_view
3558 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3559 get_gdb_index_contents_dwz_ftype;
3560
3561 /* Read .gdb_index. If everything went ok, initialize the "quick"
3562 elements of all the CUs and return 1. Otherwise, return 0. */
3563
3564 static int
3565 dwarf2_read_gdb_index
3566 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3567 get_gdb_index_contents_ftype get_gdb_index_contents,
3568 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3569 {
3570 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3571 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3572 struct dwz_file *dwz;
3573 struct objfile *objfile = dwarf2_per_objfile->objfile;
3574
3575 gdb::array_view<const gdb_byte> main_index_contents
3576 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3577
3578 if (main_index_contents.empty ())
3579 return 0;
3580
3581 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3582 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3583 use_deprecated_index_sections,
3584 main_index_contents, map.get (), &cu_list,
3585 &cu_list_elements, &types_list,
3586 &types_list_elements))
3587 return 0;
3588
3589 /* Don't use the index if it's empty. */
3590 if (map->symbol_table.empty ())
3591 return 0;
3592
3593 /* If there is a .dwz file, read it so we can get its CU list as
3594 well. */
3595 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3596 if (dwz != NULL)
3597 {
3598 struct mapped_index dwz_map;
3599 const gdb_byte *dwz_types_ignore;
3600 offset_type dwz_types_elements_ignore;
3601
3602 gdb::array_view<const gdb_byte> dwz_index_content
3603 = get_gdb_index_contents_dwz (objfile, dwz);
3604
3605 if (dwz_index_content.empty ())
3606 return 0;
3607
3608 if (!read_gdb_index_from_buffer (objfile,
3609 bfd_get_filename (dwz->dwz_bfd.get ()),
3610 1, dwz_index_content, &dwz_map,
3611 &dwz_list, &dwz_list_elements,
3612 &dwz_types_ignore,
3613 &dwz_types_elements_ignore))
3614 {
3615 warning (_("could not read '.gdb_index' section from %s; skipping"),
3616 bfd_get_filename (dwz->dwz_bfd.get ()));
3617 return 0;
3618 }
3619 }
3620
3621 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3622 dwz_list, dwz_list_elements);
3623
3624 if (types_list_elements)
3625 {
3626 /* We can only handle a single .debug_types when we have an
3627 index. */
3628 if (dwarf2_per_objfile->types.size () != 1)
3629 return 0;
3630
3631 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3632
3633 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3634 types_list, types_list_elements);
3635 }
3636
3637 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3638
3639 dwarf2_per_objfile->index_table = std::move (map);
3640 dwarf2_per_objfile->using_index = 1;
3641 dwarf2_per_objfile->quick_file_names_table =
3642 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3643
3644 return 1;
3645 }
3646
3647 /* die_reader_func for dw2_get_file_names. */
3648
3649 static void
3650 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3651 const gdb_byte *info_ptr,
3652 struct die_info *comp_unit_die,
3653 int has_children,
3654 void *data)
3655 {
3656 struct dwarf2_cu *cu = reader->cu;
3657 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3658 struct dwarf2_per_objfile *dwarf2_per_objfile
3659 = cu->per_cu->dwarf2_per_objfile;
3660 struct objfile *objfile = dwarf2_per_objfile->objfile;
3661 struct dwarf2_per_cu_data *lh_cu;
3662 struct attribute *attr;
3663 void **slot;
3664 struct quick_file_names *qfn;
3665
3666 gdb_assert (! this_cu->is_debug_types);
3667
3668 /* Our callers never want to match partial units -- instead they
3669 will match the enclosing full CU. */
3670 if (comp_unit_die->tag == DW_TAG_partial_unit)
3671 {
3672 this_cu->v.quick->no_file_data = 1;
3673 return;
3674 }
3675
3676 lh_cu = this_cu;
3677 slot = NULL;
3678
3679 line_header_up lh;
3680 sect_offset line_offset {};
3681
3682 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3683 if (attr != nullptr)
3684 {
3685 struct quick_file_names find_entry;
3686
3687 line_offset = (sect_offset) DW_UNSND (attr);
3688
3689 /* We may have already read in this line header (TU line header sharing).
3690 If we have we're done. */
3691 find_entry.hash.dwo_unit = cu->dwo_unit;
3692 find_entry.hash.line_sect_off = line_offset;
3693 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3694 &find_entry, INSERT);
3695 if (*slot != NULL)
3696 {
3697 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3698 return;
3699 }
3700
3701 lh = dwarf_decode_line_header (line_offset, cu);
3702 }
3703 if (lh == NULL)
3704 {
3705 lh_cu->v.quick->no_file_data = 1;
3706 return;
3707 }
3708
3709 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3710 qfn->hash.dwo_unit = cu->dwo_unit;
3711 qfn->hash.line_sect_off = line_offset;
3712 gdb_assert (slot != NULL);
3713 *slot = qfn;
3714
3715 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3716
3717 int offset = 0;
3718 if (strcmp (fnd.name, "<unknown>") != 0)
3719 ++offset;
3720
3721 qfn->num_file_names = offset + lh->file_names_size ();
3722 qfn->file_names =
3723 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3724 if (offset != 0)
3725 qfn->file_names[0] = xstrdup (fnd.name);
3726 for (int i = 0; i < lh->file_names_size (); ++i)
3727 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3728 qfn->real_names = NULL;
3729
3730 lh_cu->v.quick->file_names = qfn;
3731 }
3732
3733 /* A helper for the "quick" functions which attempts to read the line
3734 table for THIS_CU. */
3735
3736 static struct quick_file_names *
3737 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3738 {
3739 /* This should never be called for TUs. */
3740 gdb_assert (! this_cu->is_debug_types);
3741 /* Nor type unit groups. */
3742 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3743
3744 if (this_cu->v.quick->file_names != NULL)
3745 return this_cu->v.quick->file_names;
3746 /* If we know there is no line data, no point in looking again. */
3747 if (this_cu->v.quick->no_file_data)
3748 return NULL;
3749
3750 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3751
3752 if (this_cu->v.quick->no_file_data)
3753 return NULL;
3754 return this_cu->v.quick->file_names;
3755 }
3756
3757 /* A helper for the "quick" functions which computes and caches the
3758 real path for a given file name from the line table. */
3759
3760 static const char *
3761 dw2_get_real_path (struct objfile *objfile,
3762 struct quick_file_names *qfn, int index)
3763 {
3764 if (qfn->real_names == NULL)
3765 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3766 qfn->num_file_names, const char *);
3767
3768 if (qfn->real_names[index] == NULL)
3769 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3770
3771 return qfn->real_names[index];
3772 }
3773
3774 static struct symtab *
3775 dw2_find_last_source_symtab (struct objfile *objfile)
3776 {
3777 struct dwarf2_per_objfile *dwarf2_per_objfile
3778 = get_dwarf2_per_objfile (objfile);
3779 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3780 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3781
3782 if (cust == NULL)
3783 return NULL;
3784
3785 return compunit_primary_filetab (cust);
3786 }
3787
3788 /* Traversal function for dw2_forget_cached_source_info. */
3789
3790 static int
3791 dw2_free_cached_file_names (void **slot, void *info)
3792 {
3793 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3794
3795 if (file_data->real_names)
3796 {
3797 int i;
3798
3799 for (i = 0; i < file_data->num_file_names; ++i)
3800 {
3801 xfree ((void*) file_data->real_names[i]);
3802 file_data->real_names[i] = NULL;
3803 }
3804 }
3805
3806 return 1;
3807 }
3808
3809 static void
3810 dw2_forget_cached_source_info (struct objfile *objfile)
3811 {
3812 struct dwarf2_per_objfile *dwarf2_per_objfile
3813 = get_dwarf2_per_objfile (objfile);
3814
3815 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3816 dw2_free_cached_file_names, NULL);
3817 }
3818
3819 /* Helper function for dw2_map_symtabs_matching_filename that expands
3820 the symtabs and calls the iterator. */
3821
3822 static int
3823 dw2_map_expand_apply (struct objfile *objfile,
3824 struct dwarf2_per_cu_data *per_cu,
3825 const char *name, const char *real_path,
3826 gdb::function_view<bool (symtab *)> callback)
3827 {
3828 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3829
3830 /* Don't visit already-expanded CUs. */
3831 if (per_cu->v.quick->compunit_symtab)
3832 return 0;
3833
3834 /* This may expand more than one symtab, and we want to iterate over
3835 all of them. */
3836 dw2_instantiate_symtab (per_cu, false);
3837
3838 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3839 last_made, callback);
3840 }
3841
3842 /* Implementation of the map_symtabs_matching_filename method. */
3843
3844 static bool
3845 dw2_map_symtabs_matching_filename
3846 (struct objfile *objfile, const char *name, const char *real_path,
3847 gdb::function_view<bool (symtab *)> callback)
3848 {
3849 const char *name_basename = lbasename (name);
3850 struct dwarf2_per_objfile *dwarf2_per_objfile
3851 = get_dwarf2_per_objfile (objfile);
3852
3853 /* The rule is CUs specify all the files, including those used by
3854 any TU, so there's no need to scan TUs here. */
3855
3856 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3857 {
3858 /* We only need to look at symtabs not already expanded. */
3859 if (per_cu->v.quick->compunit_symtab)
3860 continue;
3861
3862 quick_file_names *file_data = dw2_get_file_names (per_cu);
3863 if (file_data == NULL)
3864 continue;
3865
3866 for (int j = 0; j < file_data->num_file_names; ++j)
3867 {
3868 const char *this_name = file_data->file_names[j];
3869 const char *this_real_name;
3870
3871 if (compare_filenames_for_search (this_name, name))
3872 {
3873 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3874 callback))
3875 return true;
3876 continue;
3877 }
3878
3879 /* Before we invoke realpath, which can get expensive when many
3880 files are involved, do a quick comparison of the basenames. */
3881 if (! basenames_may_differ
3882 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3883 continue;
3884
3885 this_real_name = dw2_get_real_path (objfile, file_data, j);
3886 if (compare_filenames_for_search (this_real_name, name))
3887 {
3888 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3889 callback))
3890 return true;
3891 continue;
3892 }
3893
3894 if (real_path != NULL)
3895 {
3896 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3897 gdb_assert (IS_ABSOLUTE_PATH (name));
3898 if (this_real_name != NULL
3899 && FILENAME_CMP (real_path, this_real_name) == 0)
3900 {
3901 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3902 callback))
3903 return true;
3904 continue;
3905 }
3906 }
3907 }
3908 }
3909
3910 return false;
3911 }
3912
3913 /* Struct used to manage iterating over all CUs looking for a symbol. */
3914
3915 struct dw2_symtab_iterator
3916 {
3917 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3918 struct dwarf2_per_objfile *dwarf2_per_objfile;
3919 /* If set, only look for symbols that match that block. Valid values are
3920 GLOBAL_BLOCK and STATIC_BLOCK. */
3921 gdb::optional<block_enum> block_index;
3922 /* The kind of symbol we're looking for. */
3923 domain_enum domain;
3924 /* The list of CUs from the index entry of the symbol,
3925 or NULL if not found. */
3926 offset_type *vec;
3927 /* The next element in VEC to look at. */
3928 int next;
3929 /* The number of elements in VEC, or zero if there is no match. */
3930 int length;
3931 /* Have we seen a global version of the symbol?
3932 If so we can ignore all further global instances.
3933 This is to work around gold/15646, inefficient gold-generated
3934 indices. */
3935 int global_seen;
3936 };
3937
3938 /* Initialize the index symtab iterator ITER. */
3939
3940 static void
3941 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3942 struct dwarf2_per_objfile *dwarf2_per_objfile,
3943 gdb::optional<block_enum> block_index,
3944 domain_enum domain,
3945 const char *name)
3946 {
3947 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3948 iter->block_index = block_index;
3949 iter->domain = domain;
3950 iter->next = 0;
3951 iter->global_seen = 0;
3952
3953 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3954
3955 /* index is NULL if OBJF_READNOW. */
3956 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3957 iter->length = MAYBE_SWAP (*iter->vec);
3958 else
3959 {
3960 iter->vec = NULL;
3961 iter->length = 0;
3962 }
3963 }
3964
3965 /* Return the next matching CU or NULL if there are no more. */
3966
3967 static struct dwarf2_per_cu_data *
3968 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3969 {
3970 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3971
3972 for ( ; iter->next < iter->length; ++iter->next)
3973 {
3974 offset_type cu_index_and_attrs =
3975 MAYBE_SWAP (iter->vec[iter->next + 1]);
3976 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3977 gdb_index_symbol_kind symbol_kind =
3978 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3979 /* Only check the symbol attributes if they're present.
3980 Indices prior to version 7 don't record them,
3981 and indices >= 7 may elide them for certain symbols
3982 (gold does this). */
3983 int attrs_valid =
3984 (dwarf2_per_objfile->index_table->version >= 7
3985 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3986
3987 /* Don't crash on bad data. */
3988 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3989 + dwarf2_per_objfile->all_type_units.size ()))
3990 {
3991 complaint (_(".gdb_index entry has bad CU index"
3992 " [in module %s]"),
3993 objfile_name (dwarf2_per_objfile->objfile));
3994 continue;
3995 }
3996
3997 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3998
3999 /* Skip if already read in. */
4000 if (per_cu->v.quick->compunit_symtab)
4001 continue;
4002
4003 /* Check static vs global. */
4004 if (attrs_valid)
4005 {
4006 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4007
4008 if (iter->block_index.has_value ())
4009 {
4010 bool want_static = *iter->block_index == STATIC_BLOCK;
4011
4012 if (is_static != want_static)
4013 continue;
4014 }
4015
4016 /* Work around gold/15646. */
4017 if (!is_static && iter->global_seen)
4018 continue;
4019 if (!is_static)
4020 iter->global_seen = 1;
4021 }
4022
4023 /* Only check the symbol's kind if it has one. */
4024 if (attrs_valid)
4025 {
4026 switch (iter->domain)
4027 {
4028 case VAR_DOMAIN:
4029 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4030 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4031 /* Some types are also in VAR_DOMAIN. */
4032 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4033 continue;
4034 break;
4035 case STRUCT_DOMAIN:
4036 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4037 continue;
4038 break;
4039 case LABEL_DOMAIN:
4040 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4041 continue;
4042 break;
4043 case MODULE_DOMAIN:
4044 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4045 continue;
4046 break;
4047 default:
4048 break;
4049 }
4050 }
4051
4052 ++iter->next;
4053 return per_cu;
4054 }
4055
4056 return NULL;
4057 }
4058
4059 static struct compunit_symtab *
4060 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
4061 const char *name, domain_enum domain)
4062 {
4063 struct compunit_symtab *stab_best = NULL;
4064 struct dwarf2_per_objfile *dwarf2_per_objfile
4065 = get_dwarf2_per_objfile (objfile);
4066
4067 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4068
4069 struct dw2_symtab_iterator iter;
4070 struct dwarf2_per_cu_data *per_cu;
4071
4072 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
4073
4074 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4075 {
4076 struct symbol *sym, *with_opaque = NULL;
4077 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
4078 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4079 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4080
4081 sym = block_find_symbol (block, name, domain,
4082 block_find_non_opaque_type_preferred,
4083 &with_opaque);
4084
4085 /* Some caution must be observed with overloaded functions
4086 and methods, since the index will not contain any overload
4087 information (but NAME might contain it). */
4088
4089 if (sym != NULL
4090 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4091 return stab;
4092 if (with_opaque != NULL
4093 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4094 stab_best = stab;
4095
4096 /* Keep looking through other CUs. */
4097 }
4098
4099 return stab_best;
4100 }
4101
4102 static void
4103 dw2_print_stats (struct objfile *objfile)
4104 {
4105 struct dwarf2_per_objfile *dwarf2_per_objfile
4106 = get_dwarf2_per_objfile (objfile);
4107 int total = (dwarf2_per_objfile->all_comp_units.size ()
4108 + dwarf2_per_objfile->all_type_units.size ());
4109 int count = 0;
4110
4111 for (int i = 0; i < total; ++i)
4112 {
4113 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4114
4115 if (!per_cu->v.quick->compunit_symtab)
4116 ++count;
4117 }
4118 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4119 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4120 }
4121
4122 /* This dumps minimal information about the index.
4123 It is called via "mt print objfiles".
4124 One use is to verify .gdb_index has been loaded by the
4125 gdb.dwarf2/gdb-index.exp testcase. */
4126
4127 static void
4128 dw2_dump (struct objfile *objfile)
4129 {
4130 struct dwarf2_per_objfile *dwarf2_per_objfile
4131 = get_dwarf2_per_objfile (objfile);
4132
4133 gdb_assert (dwarf2_per_objfile->using_index);
4134 printf_filtered (".gdb_index:");
4135 if (dwarf2_per_objfile->index_table != NULL)
4136 {
4137 printf_filtered (" version %d\n",
4138 dwarf2_per_objfile->index_table->version);
4139 }
4140 else
4141 printf_filtered (" faked for \"readnow\"\n");
4142 printf_filtered ("\n");
4143 }
4144
4145 static void
4146 dw2_expand_symtabs_for_function (struct objfile *objfile,
4147 const char *func_name)
4148 {
4149 struct dwarf2_per_objfile *dwarf2_per_objfile
4150 = get_dwarf2_per_objfile (objfile);
4151
4152 struct dw2_symtab_iterator iter;
4153 struct dwarf2_per_cu_data *per_cu;
4154
4155 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
4156
4157 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4158 dw2_instantiate_symtab (per_cu, false);
4159
4160 }
4161
4162 static void
4163 dw2_expand_all_symtabs (struct objfile *objfile)
4164 {
4165 struct dwarf2_per_objfile *dwarf2_per_objfile
4166 = get_dwarf2_per_objfile (objfile);
4167 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4168 + dwarf2_per_objfile->all_type_units.size ());
4169
4170 for (int i = 0; i < total_units; ++i)
4171 {
4172 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4173
4174 /* We don't want to directly expand a partial CU, because if we
4175 read it with the wrong language, then assertion failures can
4176 be triggered later on. See PR symtab/23010. So, tell
4177 dw2_instantiate_symtab to skip partial CUs -- any important
4178 partial CU will be read via DW_TAG_imported_unit anyway. */
4179 dw2_instantiate_symtab (per_cu, true);
4180 }
4181 }
4182
4183 static void
4184 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4185 const char *fullname)
4186 {
4187 struct dwarf2_per_objfile *dwarf2_per_objfile
4188 = get_dwarf2_per_objfile (objfile);
4189
4190 /* We don't need to consider type units here.
4191 This is only called for examining code, e.g. expand_line_sal.
4192 There can be an order of magnitude (or more) more type units
4193 than comp units, and we avoid them if we can. */
4194
4195 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4196 {
4197 /* We only need to look at symtabs not already expanded. */
4198 if (per_cu->v.quick->compunit_symtab)
4199 continue;
4200
4201 quick_file_names *file_data = dw2_get_file_names (per_cu);
4202 if (file_data == NULL)
4203 continue;
4204
4205 for (int j = 0; j < file_data->num_file_names; ++j)
4206 {
4207 const char *this_fullname = file_data->file_names[j];
4208
4209 if (filename_cmp (this_fullname, fullname) == 0)
4210 {
4211 dw2_instantiate_symtab (per_cu, false);
4212 break;
4213 }
4214 }
4215 }
4216 }
4217
4218 static void
4219 dw2_map_matching_symbols
4220 (struct objfile *objfile,
4221 const lookup_name_info &name, domain_enum domain,
4222 int global,
4223 gdb::function_view<symbol_found_callback_ftype> callback,
4224 symbol_compare_ftype *ordered_compare)
4225 {
4226 /* Currently unimplemented; used for Ada. The function can be called if the
4227 current language is Ada for a non-Ada objfile using GNU index. As Ada
4228 does not look for non-Ada symbols this function should just return. */
4229 }
4230
4231 /* Starting from a search name, return the string that finds the upper
4232 bound of all strings that start with SEARCH_NAME in a sorted name
4233 list. Returns the empty string to indicate that the upper bound is
4234 the end of the list. */
4235
4236 static std::string
4237 make_sort_after_prefix_name (const char *search_name)
4238 {
4239 /* When looking to complete "func", we find the upper bound of all
4240 symbols that start with "func" by looking for where we'd insert
4241 the closest string that would follow "func" in lexicographical
4242 order. Usually, that's "func"-with-last-character-incremented,
4243 i.e. "fund". Mind non-ASCII characters, though. Usually those
4244 will be UTF-8 multi-byte sequences, but we can't be certain.
4245 Especially mind the 0xff character, which is a valid character in
4246 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4247 rule out compilers allowing it in identifiers. Note that
4248 conveniently, strcmp/strcasecmp are specified to compare
4249 characters interpreted as unsigned char. So what we do is treat
4250 the whole string as a base 256 number composed of a sequence of
4251 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4252 to 0, and carries 1 to the following more-significant position.
4253 If the very first character in SEARCH_NAME ends up incremented
4254 and carries/overflows, then the upper bound is the end of the
4255 list. The string after the empty string is also the empty
4256 string.
4257
4258 Some examples of this operation:
4259
4260 SEARCH_NAME => "+1" RESULT
4261
4262 "abc" => "abd"
4263 "ab\xff" => "ac"
4264 "\xff" "a" "\xff" => "\xff" "b"
4265 "\xff" => ""
4266 "\xff\xff" => ""
4267 "" => ""
4268
4269 Then, with these symbols for example:
4270
4271 func
4272 func1
4273 fund
4274
4275 completing "func" looks for symbols between "func" and
4276 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4277 which finds "func" and "func1", but not "fund".
4278
4279 And with:
4280
4281 funcÿ (Latin1 'ÿ' [0xff])
4282 funcÿ1
4283 fund
4284
4285 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4286 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4287
4288 And with:
4289
4290 ÿÿ (Latin1 'ÿ' [0xff])
4291 ÿÿ1
4292
4293 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4294 the end of the list.
4295 */
4296 std::string after = search_name;
4297 while (!after.empty () && (unsigned char) after.back () == 0xff)
4298 after.pop_back ();
4299 if (!after.empty ())
4300 after.back () = (unsigned char) after.back () + 1;
4301 return after;
4302 }
4303
4304 /* See declaration. */
4305
4306 std::pair<std::vector<name_component>::const_iterator,
4307 std::vector<name_component>::const_iterator>
4308 mapped_index_base::find_name_components_bounds
4309 (const lookup_name_info &lookup_name_without_params, language lang) const
4310 {
4311 auto *name_cmp
4312 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4313
4314 const char *lang_name
4315 = lookup_name_without_params.language_lookup_name (lang).c_str ();
4316
4317 /* Comparison function object for lower_bound that matches against a
4318 given symbol name. */
4319 auto lookup_compare_lower = [&] (const name_component &elem,
4320 const char *name)
4321 {
4322 const char *elem_qualified = this->symbol_name_at (elem.idx);
4323 const char *elem_name = elem_qualified + elem.name_offset;
4324 return name_cmp (elem_name, name) < 0;
4325 };
4326
4327 /* Comparison function object for upper_bound that matches against a
4328 given symbol name. */
4329 auto lookup_compare_upper = [&] (const char *name,
4330 const name_component &elem)
4331 {
4332 const char *elem_qualified = this->symbol_name_at (elem.idx);
4333 const char *elem_name = elem_qualified + elem.name_offset;
4334 return name_cmp (name, elem_name) < 0;
4335 };
4336
4337 auto begin = this->name_components.begin ();
4338 auto end = this->name_components.end ();
4339
4340 /* Find the lower bound. */
4341 auto lower = [&] ()
4342 {
4343 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4344 return begin;
4345 else
4346 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4347 } ();
4348
4349 /* Find the upper bound. */
4350 auto upper = [&] ()
4351 {
4352 if (lookup_name_without_params.completion_mode ())
4353 {
4354 /* In completion mode, we want UPPER to point past all
4355 symbols names that have the same prefix. I.e., with
4356 these symbols, and completing "func":
4357
4358 function << lower bound
4359 function1
4360 other_function << upper bound
4361
4362 We find the upper bound by looking for the insertion
4363 point of "func"-with-last-character-incremented,
4364 i.e. "fund". */
4365 std::string after = make_sort_after_prefix_name (lang_name);
4366 if (after.empty ())
4367 return end;
4368 return std::lower_bound (lower, end, after.c_str (),
4369 lookup_compare_lower);
4370 }
4371 else
4372 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4373 } ();
4374
4375 return {lower, upper};
4376 }
4377
4378 /* See declaration. */
4379
4380 void
4381 mapped_index_base::build_name_components ()
4382 {
4383 if (!this->name_components.empty ())
4384 return;
4385
4386 this->name_components_casing = case_sensitivity;
4387 auto *name_cmp
4388 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4389
4390 /* The code below only knows how to break apart components of C++
4391 symbol names (and other languages that use '::' as
4392 namespace/module separator) and Ada symbol names. */
4393 auto count = this->symbol_name_count ();
4394 for (offset_type idx = 0; idx < count; idx++)
4395 {
4396 if (this->symbol_name_slot_invalid (idx))
4397 continue;
4398
4399 const char *name = this->symbol_name_at (idx);
4400
4401 /* Add each name component to the name component table. */
4402 unsigned int previous_len = 0;
4403
4404 if (strstr (name, "::") != nullptr)
4405 {
4406 for (unsigned int current_len = cp_find_first_component (name);
4407 name[current_len] != '\0';
4408 current_len += cp_find_first_component (name + current_len))
4409 {
4410 gdb_assert (name[current_len] == ':');
4411 this->name_components.push_back ({previous_len, idx});
4412 /* Skip the '::'. */
4413 current_len += 2;
4414 previous_len = current_len;
4415 }
4416 }
4417 else
4418 {
4419 /* Handle the Ada encoded (aka mangled) form here. */
4420 for (const char *iter = strstr (name, "__");
4421 iter != nullptr;
4422 iter = strstr (iter, "__"))
4423 {
4424 this->name_components.push_back ({previous_len, idx});
4425 iter += 2;
4426 previous_len = iter - name;
4427 }
4428 }
4429
4430 this->name_components.push_back ({previous_len, idx});
4431 }
4432
4433 /* Sort name_components elements by name. */
4434 auto name_comp_compare = [&] (const name_component &left,
4435 const name_component &right)
4436 {
4437 const char *left_qualified = this->symbol_name_at (left.idx);
4438 const char *right_qualified = this->symbol_name_at (right.idx);
4439
4440 const char *left_name = left_qualified + left.name_offset;
4441 const char *right_name = right_qualified + right.name_offset;
4442
4443 return name_cmp (left_name, right_name) < 0;
4444 };
4445
4446 std::sort (this->name_components.begin (),
4447 this->name_components.end (),
4448 name_comp_compare);
4449 }
4450
4451 /* Helper for dw2_expand_symtabs_matching that works with a
4452 mapped_index_base instead of the containing objfile. This is split
4453 to a separate function in order to be able to unit test the
4454 name_components matching using a mock mapped_index_base. For each
4455 symbol name that matches, calls MATCH_CALLBACK, passing it the
4456 symbol's index in the mapped_index_base symbol table. */
4457
4458 static void
4459 dw2_expand_symtabs_matching_symbol
4460 (mapped_index_base &index,
4461 const lookup_name_info &lookup_name_in,
4462 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4463 enum search_domain kind,
4464 gdb::function_view<bool (offset_type)> match_callback)
4465 {
4466 lookup_name_info lookup_name_without_params
4467 = lookup_name_in.make_ignore_params ();
4468
4469 /* Build the symbol name component sorted vector, if we haven't
4470 yet. */
4471 index.build_name_components ();
4472
4473 /* The same symbol may appear more than once in the range though.
4474 E.g., if we're looking for symbols that complete "w", and we have
4475 a symbol named "w1::w2", we'll find the two name components for
4476 that same symbol in the range. To be sure we only call the
4477 callback once per symbol, we first collect the symbol name
4478 indexes that matched in a temporary vector and ignore
4479 duplicates. */
4480 std::vector<offset_type> matches;
4481
4482 struct name_and_matcher
4483 {
4484 symbol_name_matcher_ftype *matcher;
4485 const std::string &name;
4486
4487 bool operator== (const name_and_matcher &other) const
4488 {
4489 return matcher == other.matcher && name == other.name;
4490 }
4491 };
4492
4493 /* A vector holding all the different symbol name matchers, for all
4494 languages. */
4495 std::vector<name_and_matcher> matchers;
4496
4497 for (int i = 0; i < nr_languages; i++)
4498 {
4499 enum language lang_e = (enum language) i;
4500
4501 const language_defn *lang = language_def (lang_e);
4502 symbol_name_matcher_ftype *name_matcher
4503 = get_symbol_name_matcher (lang, lookup_name_without_params);
4504
4505 name_and_matcher key {
4506 name_matcher,
4507 lookup_name_without_params.language_lookup_name (lang_e)
4508 };
4509
4510 /* Don't insert the same comparison routine more than once.
4511 Note that we do this linear walk. This is not a problem in
4512 practice because the number of supported languages is
4513 low. */
4514 if (std::find (matchers.begin (), matchers.end (), key)
4515 != matchers.end ())
4516 continue;
4517 matchers.push_back (std::move (key));
4518
4519 auto bounds
4520 = index.find_name_components_bounds (lookup_name_without_params,
4521 lang_e);
4522
4523 /* Now for each symbol name in range, check to see if we have a name
4524 match, and if so, call the MATCH_CALLBACK callback. */
4525
4526 for (; bounds.first != bounds.second; ++bounds.first)
4527 {
4528 const char *qualified = index.symbol_name_at (bounds.first->idx);
4529
4530 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4531 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4532 continue;
4533
4534 matches.push_back (bounds.first->idx);
4535 }
4536 }
4537
4538 std::sort (matches.begin (), matches.end ());
4539
4540 /* Finally call the callback, once per match. */
4541 ULONGEST prev = -1;
4542 for (offset_type idx : matches)
4543 {
4544 if (prev != idx)
4545 {
4546 if (!match_callback (idx))
4547 break;
4548 prev = idx;
4549 }
4550 }
4551
4552 /* Above we use a type wider than idx's for 'prev', since 0 and
4553 (offset_type)-1 are both possible values. */
4554 static_assert (sizeof (prev) > sizeof (offset_type), "");
4555 }
4556
4557 #if GDB_SELF_TEST
4558
4559 namespace selftests { namespace dw2_expand_symtabs_matching {
4560
4561 /* A mock .gdb_index/.debug_names-like name index table, enough to
4562 exercise dw2_expand_symtabs_matching_symbol, which works with the
4563 mapped_index_base interface. Builds an index from the symbol list
4564 passed as parameter to the constructor. */
4565 class mock_mapped_index : public mapped_index_base
4566 {
4567 public:
4568 mock_mapped_index (gdb::array_view<const char *> symbols)
4569 : m_symbol_table (symbols)
4570 {}
4571
4572 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4573
4574 /* Return the number of names in the symbol table. */
4575 size_t symbol_name_count () const override
4576 {
4577 return m_symbol_table.size ();
4578 }
4579
4580 /* Get the name of the symbol at IDX in the symbol table. */
4581 const char *symbol_name_at (offset_type idx) const override
4582 {
4583 return m_symbol_table[idx];
4584 }
4585
4586 private:
4587 gdb::array_view<const char *> m_symbol_table;
4588 };
4589
4590 /* Convenience function that converts a NULL pointer to a "<null>"
4591 string, to pass to print routines. */
4592
4593 static const char *
4594 string_or_null (const char *str)
4595 {
4596 return str != NULL ? str : "<null>";
4597 }
4598
4599 /* Check if a lookup_name_info built from
4600 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4601 index. EXPECTED_LIST is the list of expected matches, in expected
4602 matching order. If no match expected, then an empty list is
4603 specified. Returns true on success. On failure prints a warning
4604 indicating the file:line that failed, and returns false. */
4605
4606 static bool
4607 check_match (const char *file, int line,
4608 mock_mapped_index &mock_index,
4609 const char *name, symbol_name_match_type match_type,
4610 bool completion_mode,
4611 std::initializer_list<const char *> expected_list)
4612 {
4613 lookup_name_info lookup_name (name, match_type, completion_mode);
4614
4615 bool matched = true;
4616
4617 auto mismatch = [&] (const char *expected_str,
4618 const char *got)
4619 {
4620 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4621 "expected=\"%s\", got=\"%s\"\n"),
4622 file, line,
4623 (match_type == symbol_name_match_type::FULL
4624 ? "FULL" : "WILD"),
4625 name, string_or_null (expected_str), string_or_null (got));
4626 matched = false;
4627 };
4628
4629 auto expected_it = expected_list.begin ();
4630 auto expected_end = expected_list.end ();
4631
4632 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4633 NULL, ALL_DOMAIN,
4634 [&] (offset_type idx)
4635 {
4636 const char *matched_name = mock_index.symbol_name_at (idx);
4637 const char *expected_str
4638 = expected_it == expected_end ? NULL : *expected_it++;
4639
4640 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4641 mismatch (expected_str, matched_name);
4642 return true;
4643 });
4644
4645 const char *expected_str
4646 = expected_it == expected_end ? NULL : *expected_it++;
4647 if (expected_str != NULL)
4648 mismatch (expected_str, NULL);
4649
4650 return matched;
4651 }
4652
4653 /* The symbols added to the mock mapped_index for testing (in
4654 canonical form). */
4655 static const char *test_symbols[] = {
4656 "function",
4657 "std::bar",
4658 "std::zfunction",
4659 "std::zfunction2",
4660 "w1::w2",
4661 "ns::foo<char*>",
4662 "ns::foo<int>",
4663 "ns::foo<long>",
4664 "ns2::tmpl<int>::foo2",
4665 "(anonymous namespace)::A::B::C",
4666
4667 /* These are used to check that the increment-last-char in the
4668 matching algorithm for completion doesn't match "t1_fund" when
4669 completing "t1_func". */
4670 "t1_func",
4671 "t1_func1",
4672 "t1_fund",
4673 "t1_fund1",
4674
4675 /* A UTF-8 name with multi-byte sequences to make sure that
4676 cp-name-parser understands this as a single identifier ("função"
4677 is "function" in PT). */
4678 u8"u8função",
4679
4680 /* \377 (0xff) is Latin1 'ÿ'. */
4681 "yfunc\377",
4682
4683 /* \377 (0xff) is Latin1 'ÿ'. */
4684 "\377",
4685 "\377\377123",
4686
4687 /* A name with all sorts of complications. Starts with "z" to make
4688 it easier for the completion tests below. */
4689 #define Z_SYM_NAME \
4690 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4691 "::tuple<(anonymous namespace)::ui*, " \
4692 "std::default_delete<(anonymous namespace)::ui>, void>"
4693
4694 Z_SYM_NAME
4695 };
4696
4697 /* Returns true if the mapped_index_base::find_name_component_bounds
4698 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4699 in completion mode. */
4700
4701 static bool
4702 check_find_bounds_finds (mapped_index_base &index,
4703 const char *search_name,
4704 gdb::array_view<const char *> expected_syms)
4705 {
4706 lookup_name_info lookup_name (search_name,
4707 symbol_name_match_type::FULL, true);
4708
4709 auto bounds = index.find_name_components_bounds (lookup_name,
4710 language_cplus);
4711
4712 size_t distance = std::distance (bounds.first, bounds.second);
4713 if (distance != expected_syms.size ())
4714 return false;
4715
4716 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4717 {
4718 auto nc_elem = bounds.first + exp_elem;
4719 const char *qualified = index.symbol_name_at (nc_elem->idx);
4720 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4721 return false;
4722 }
4723
4724 return true;
4725 }
4726
4727 /* Test the lower-level mapped_index::find_name_component_bounds
4728 method. */
4729
4730 static void
4731 test_mapped_index_find_name_component_bounds ()
4732 {
4733 mock_mapped_index mock_index (test_symbols);
4734
4735 mock_index.build_name_components ();
4736
4737 /* Test the lower-level mapped_index::find_name_component_bounds
4738 method in completion mode. */
4739 {
4740 static const char *expected_syms[] = {
4741 "t1_func",
4742 "t1_func1",
4743 };
4744
4745 SELF_CHECK (check_find_bounds_finds (mock_index,
4746 "t1_func", expected_syms));
4747 }
4748
4749 /* Check that the increment-last-char in the name matching algorithm
4750 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4751 {
4752 static const char *expected_syms1[] = {
4753 "\377",
4754 "\377\377123",
4755 };
4756 SELF_CHECK (check_find_bounds_finds (mock_index,
4757 "\377", expected_syms1));
4758
4759 static const char *expected_syms2[] = {
4760 "\377\377123",
4761 };
4762 SELF_CHECK (check_find_bounds_finds (mock_index,
4763 "\377\377", expected_syms2));
4764 }
4765 }
4766
4767 /* Test dw2_expand_symtabs_matching_symbol. */
4768
4769 static void
4770 test_dw2_expand_symtabs_matching_symbol ()
4771 {
4772 mock_mapped_index mock_index (test_symbols);
4773
4774 /* We let all tests run until the end even if some fails, for debug
4775 convenience. */
4776 bool any_mismatch = false;
4777
4778 /* Create the expected symbols list (an initializer_list). Needed
4779 because lists have commas, and we need to pass them to CHECK,
4780 which is a macro. */
4781 #define EXPECT(...) { __VA_ARGS__ }
4782
4783 /* Wrapper for check_match that passes down the current
4784 __FILE__/__LINE__. */
4785 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4786 any_mismatch |= !check_match (__FILE__, __LINE__, \
4787 mock_index, \
4788 NAME, MATCH_TYPE, COMPLETION_MODE, \
4789 EXPECTED_LIST)
4790
4791 /* Identity checks. */
4792 for (const char *sym : test_symbols)
4793 {
4794 /* Should be able to match all existing symbols. */
4795 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4796 EXPECT (sym));
4797
4798 /* Should be able to match all existing symbols with
4799 parameters. */
4800 std::string with_params = std::string (sym) + "(int)";
4801 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4802 EXPECT (sym));
4803
4804 /* Should be able to match all existing symbols with
4805 parameters and qualifiers. */
4806 with_params = std::string (sym) + " ( int ) const";
4807 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4808 EXPECT (sym));
4809
4810 /* This should really find sym, but cp-name-parser.y doesn't
4811 know about lvalue/rvalue qualifiers yet. */
4812 with_params = std::string (sym) + " ( int ) &&";
4813 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4814 {});
4815 }
4816
4817 /* Check that the name matching algorithm for completion doesn't get
4818 confused with Latin1 'ÿ' / 0xff. */
4819 {
4820 static const char str[] = "\377";
4821 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4822 EXPECT ("\377", "\377\377123"));
4823 }
4824
4825 /* Check that the increment-last-char in the matching algorithm for
4826 completion doesn't match "t1_fund" when completing "t1_func". */
4827 {
4828 static const char str[] = "t1_func";
4829 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4830 EXPECT ("t1_func", "t1_func1"));
4831 }
4832
4833 /* Check that completion mode works at each prefix of the expected
4834 symbol name. */
4835 {
4836 static const char str[] = "function(int)";
4837 size_t len = strlen (str);
4838 std::string lookup;
4839
4840 for (size_t i = 1; i < len; i++)
4841 {
4842 lookup.assign (str, i);
4843 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4844 EXPECT ("function"));
4845 }
4846 }
4847
4848 /* While "w" is a prefix of both components, the match function
4849 should still only be called once. */
4850 {
4851 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4852 EXPECT ("w1::w2"));
4853 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4854 EXPECT ("w1::w2"));
4855 }
4856
4857 /* Same, with a "complicated" symbol. */
4858 {
4859 static const char str[] = Z_SYM_NAME;
4860 size_t len = strlen (str);
4861 std::string lookup;
4862
4863 for (size_t i = 1; i < len; i++)
4864 {
4865 lookup.assign (str, i);
4866 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4867 EXPECT (Z_SYM_NAME));
4868 }
4869 }
4870
4871 /* In FULL mode, an incomplete symbol doesn't match. */
4872 {
4873 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4874 {});
4875 }
4876
4877 /* A complete symbol with parameters matches any overload, since the
4878 index has no overload info. */
4879 {
4880 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4881 EXPECT ("std::zfunction", "std::zfunction2"));
4882 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4883 EXPECT ("std::zfunction", "std::zfunction2"));
4884 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4885 EXPECT ("std::zfunction", "std::zfunction2"));
4886 }
4887
4888 /* Check that whitespace is ignored appropriately. A symbol with a
4889 template argument list. */
4890 {
4891 static const char expected[] = "ns::foo<int>";
4892 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4893 EXPECT (expected));
4894 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4895 EXPECT (expected));
4896 }
4897
4898 /* Check that whitespace is ignored appropriately. A symbol with a
4899 template argument list that includes a pointer. */
4900 {
4901 static const char expected[] = "ns::foo<char*>";
4902 /* Try both completion and non-completion modes. */
4903 static const bool completion_mode[2] = {false, true};
4904 for (size_t i = 0; i < 2; i++)
4905 {
4906 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4907 completion_mode[i], EXPECT (expected));
4908 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4909 completion_mode[i], EXPECT (expected));
4910
4911 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4912 completion_mode[i], EXPECT (expected));
4913 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4914 completion_mode[i], EXPECT (expected));
4915 }
4916 }
4917
4918 {
4919 /* Check method qualifiers are ignored. */
4920 static const char expected[] = "ns::foo<char*>";
4921 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4922 symbol_name_match_type::FULL, true, EXPECT (expected));
4923 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4924 symbol_name_match_type::FULL, true, EXPECT (expected));
4925 CHECK_MATCH ("foo < char * > ( int ) const",
4926 symbol_name_match_type::WILD, true, EXPECT (expected));
4927 CHECK_MATCH ("foo < char * > ( int ) &&",
4928 symbol_name_match_type::WILD, true, EXPECT (expected));
4929 }
4930
4931 /* Test lookup names that don't match anything. */
4932 {
4933 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4934 {});
4935
4936 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4937 {});
4938 }
4939
4940 /* Some wild matching tests, exercising "(anonymous namespace)",
4941 which should not be confused with a parameter list. */
4942 {
4943 static const char *syms[] = {
4944 "A::B::C",
4945 "B::C",
4946 "C",
4947 "A :: B :: C ( int )",
4948 "B :: C ( int )",
4949 "C ( int )",
4950 };
4951
4952 for (const char *s : syms)
4953 {
4954 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4955 EXPECT ("(anonymous namespace)::A::B::C"));
4956 }
4957 }
4958
4959 {
4960 static const char expected[] = "ns2::tmpl<int>::foo2";
4961 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4962 EXPECT (expected));
4963 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4964 EXPECT (expected));
4965 }
4966
4967 SELF_CHECK (!any_mismatch);
4968
4969 #undef EXPECT
4970 #undef CHECK_MATCH
4971 }
4972
4973 static void
4974 run_test ()
4975 {
4976 test_mapped_index_find_name_component_bounds ();
4977 test_dw2_expand_symtabs_matching_symbol ();
4978 }
4979
4980 }} // namespace selftests::dw2_expand_symtabs_matching
4981
4982 #endif /* GDB_SELF_TEST */
4983
4984 /* If FILE_MATCHER is NULL or if PER_CU has
4985 dwarf2_per_cu_quick_data::MARK set (see
4986 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4987 EXPANSION_NOTIFY on it. */
4988
4989 static void
4990 dw2_expand_symtabs_matching_one
4991 (struct dwarf2_per_cu_data *per_cu,
4992 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4993 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4994 {
4995 if (file_matcher == NULL || per_cu->v.quick->mark)
4996 {
4997 bool symtab_was_null
4998 = (per_cu->v.quick->compunit_symtab == NULL);
4999
5000 dw2_instantiate_symtab (per_cu, false);
5001
5002 if (expansion_notify != NULL
5003 && symtab_was_null
5004 && per_cu->v.quick->compunit_symtab != NULL)
5005 expansion_notify (per_cu->v.quick->compunit_symtab);
5006 }
5007 }
5008
5009 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5010 matched, to expand corresponding CUs that were marked. IDX is the
5011 index of the symbol name that matched. */
5012
5013 static void
5014 dw2_expand_marked_cus
5015 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5016 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5017 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5018 search_domain kind)
5019 {
5020 offset_type *vec, vec_len, vec_idx;
5021 bool global_seen = false;
5022 mapped_index &index = *dwarf2_per_objfile->index_table;
5023
5024 vec = (offset_type *) (index.constant_pool
5025 + MAYBE_SWAP (index.symbol_table[idx].vec));
5026 vec_len = MAYBE_SWAP (vec[0]);
5027 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5028 {
5029 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5030 /* This value is only valid for index versions >= 7. */
5031 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5032 gdb_index_symbol_kind symbol_kind =
5033 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5034 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5035 /* Only check the symbol attributes if they're present.
5036 Indices prior to version 7 don't record them,
5037 and indices >= 7 may elide them for certain symbols
5038 (gold does this). */
5039 int attrs_valid =
5040 (index.version >= 7
5041 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5042
5043 /* Work around gold/15646. */
5044 if (attrs_valid)
5045 {
5046 if (!is_static && global_seen)
5047 continue;
5048 if (!is_static)
5049 global_seen = true;
5050 }
5051
5052 /* Only check the symbol's kind if it has one. */
5053 if (attrs_valid)
5054 {
5055 switch (kind)
5056 {
5057 case VARIABLES_DOMAIN:
5058 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5059 continue;
5060 break;
5061 case FUNCTIONS_DOMAIN:
5062 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5063 continue;
5064 break;
5065 case TYPES_DOMAIN:
5066 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5067 continue;
5068 break;
5069 case MODULES_DOMAIN:
5070 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
5071 continue;
5072 break;
5073 default:
5074 break;
5075 }
5076 }
5077
5078 /* Don't crash on bad data. */
5079 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5080 + dwarf2_per_objfile->all_type_units.size ()))
5081 {
5082 complaint (_(".gdb_index entry has bad CU index"
5083 " [in module %s]"),
5084 objfile_name (dwarf2_per_objfile->objfile));
5085 continue;
5086 }
5087
5088 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5089 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5090 expansion_notify);
5091 }
5092 }
5093
5094 /* If FILE_MATCHER is non-NULL, set all the
5095 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5096 that match FILE_MATCHER. */
5097
5098 static void
5099 dw_expand_symtabs_matching_file_matcher
5100 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5101 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5102 {
5103 if (file_matcher == NULL)
5104 return;
5105
5106 objfile *const objfile = dwarf2_per_objfile->objfile;
5107
5108 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5109 htab_eq_pointer,
5110 NULL, xcalloc, xfree));
5111 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5112 htab_eq_pointer,
5113 NULL, xcalloc, xfree));
5114
5115 /* The rule is CUs specify all the files, including those used by
5116 any TU, so there's no need to scan TUs here. */
5117
5118 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5119 {
5120 QUIT;
5121
5122 per_cu->v.quick->mark = 0;
5123
5124 /* We only need to look at symtabs not already expanded. */
5125 if (per_cu->v.quick->compunit_symtab)
5126 continue;
5127
5128 quick_file_names *file_data = dw2_get_file_names (per_cu);
5129 if (file_data == NULL)
5130 continue;
5131
5132 if (htab_find (visited_not_found.get (), file_data) != NULL)
5133 continue;
5134 else if (htab_find (visited_found.get (), file_data) != NULL)
5135 {
5136 per_cu->v.quick->mark = 1;
5137 continue;
5138 }
5139
5140 for (int j = 0; j < file_data->num_file_names; ++j)
5141 {
5142 const char *this_real_name;
5143
5144 if (file_matcher (file_data->file_names[j], false))
5145 {
5146 per_cu->v.quick->mark = 1;
5147 break;
5148 }
5149
5150 /* Before we invoke realpath, which can get expensive when many
5151 files are involved, do a quick comparison of the basenames. */
5152 if (!basenames_may_differ
5153 && !file_matcher (lbasename (file_data->file_names[j]),
5154 true))
5155 continue;
5156
5157 this_real_name = dw2_get_real_path (objfile, file_data, j);
5158 if (file_matcher (this_real_name, false))
5159 {
5160 per_cu->v.quick->mark = 1;
5161 break;
5162 }
5163 }
5164
5165 void **slot = htab_find_slot (per_cu->v.quick->mark
5166 ? visited_found.get ()
5167 : visited_not_found.get (),
5168 file_data, INSERT);
5169 *slot = file_data;
5170 }
5171 }
5172
5173 static void
5174 dw2_expand_symtabs_matching
5175 (struct objfile *objfile,
5176 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5177 const lookup_name_info &lookup_name,
5178 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5179 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5180 enum search_domain kind)
5181 {
5182 struct dwarf2_per_objfile *dwarf2_per_objfile
5183 = get_dwarf2_per_objfile (objfile);
5184
5185 /* index_table is NULL if OBJF_READNOW. */
5186 if (!dwarf2_per_objfile->index_table)
5187 return;
5188
5189 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5190
5191 mapped_index &index = *dwarf2_per_objfile->index_table;
5192
5193 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5194 symbol_matcher,
5195 kind, [&] (offset_type idx)
5196 {
5197 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5198 expansion_notify, kind);
5199 return true;
5200 });
5201 }
5202
5203 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5204 symtab. */
5205
5206 static struct compunit_symtab *
5207 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5208 CORE_ADDR pc)
5209 {
5210 int i;
5211
5212 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5213 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5214 return cust;
5215
5216 if (cust->includes == NULL)
5217 return NULL;
5218
5219 for (i = 0; cust->includes[i]; ++i)
5220 {
5221 struct compunit_symtab *s = cust->includes[i];
5222
5223 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5224 if (s != NULL)
5225 return s;
5226 }
5227
5228 return NULL;
5229 }
5230
5231 static struct compunit_symtab *
5232 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5233 struct bound_minimal_symbol msymbol,
5234 CORE_ADDR pc,
5235 struct obj_section *section,
5236 int warn_if_readin)
5237 {
5238 struct dwarf2_per_cu_data *data;
5239 struct compunit_symtab *result;
5240
5241 if (!objfile->partial_symtabs->psymtabs_addrmap)
5242 return NULL;
5243
5244 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5245 SECT_OFF_TEXT (objfile));
5246 data = (struct dwarf2_per_cu_data *) addrmap_find
5247 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
5248 if (!data)
5249 return NULL;
5250
5251 if (warn_if_readin && data->v.quick->compunit_symtab)
5252 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5253 paddress (get_objfile_arch (objfile), pc));
5254
5255 result
5256 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5257 false),
5258 pc);
5259 gdb_assert (result != NULL);
5260 return result;
5261 }
5262
5263 static void
5264 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5265 void *data, int need_fullname)
5266 {
5267 struct dwarf2_per_objfile *dwarf2_per_objfile
5268 = get_dwarf2_per_objfile (objfile);
5269
5270 if (!dwarf2_per_objfile->filenames_cache)
5271 {
5272 dwarf2_per_objfile->filenames_cache.emplace ();
5273
5274 htab_up visited (htab_create_alloc (10,
5275 htab_hash_pointer, htab_eq_pointer,
5276 NULL, xcalloc, xfree));
5277
5278 /* The rule is CUs specify all the files, including those used
5279 by any TU, so there's no need to scan TUs here. We can
5280 ignore file names coming from already-expanded CUs. */
5281
5282 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5283 {
5284 if (per_cu->v.quick->compunit_symtab)
5285 {
5286 void **slot = htab_find_slot (visited.get (),
5287 per_cu->v.quick->file_names,
5288 INSERT);
5289
5290 *slot = per_cu->v.quick->file_names;
5291 }
5292 }
5293
5294 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5295 {
5296 /* We only need to look at symtabs not already expanded. */
5297 if (per_cu->v.quick->compunit_symtab)
5298 continue;
5299
5300 quick_file_names *file_data = dw2_get_file_names (per_cu);
5301 if (file_data == NULL)
5302 continue;
5303
5304 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5305 if (*slot)
5306 {
5307 /* Already visited. */
5308 continue;
5309 }
5310 *slot = file_data;
5311
5312 for (int j = 0; j < file_data->num_file_names; ++j)
5313 {
5314 const char *filename = file_data->file_names[j];
5315 dwarf2_per_objfile->filenames_cache->seen (filename);
5316 }
5317 }
5318 }
5319
5320 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5321 {
5322 gdb::unique_xmalloc_ptr<char> this_real_name;
5323
5324 if (need_fullname)
5325 this_real_name = gdb_realpath (filename);
5326 (*fun) (filename, this_real_name.get (), data);
5327 });
5328 }
5329
5330 static int
5331 dw2_has_symbols (struct objfile *objfile)
5332 {
5333 return 1;
5334 }
5335
5336 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5337 {
5338 dw2_has_symbols,
5339 dw2_find_last_source_symtab,
5340 dw2_forget_cached_source_info,
5341 dw2_map_symtabs_matching_filename,
5342 dw2_lookup_symbol,
5343 dw2_print_stats,
5344 dw2_dump,
5345 dw2_expand_symtabs_for_function,
5346 dw2_expand_all_symtabs,
5347 dw2_expand_symtabs_with_fullname,
5348 dw2_map_matching_symbols,
5349 dw2_expand_symtabs_matching,
5350 dw2_find_pc_sect_compunit_symtab,
5351 NULL,
5352 dw2_map_symbol_filenames
5353 };
5354
5355 /* DWARF-5 debug_names reader. */
5356
5357 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5358 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5359
5360 /* A helper function that reads the .debug_names section in SECTION
5361 and fills in MAP. FILENAME is the name of the file containing the
5362 section; it is used for error reporting.
5363
5364 Returns true if all went well, false otherwise. */
5365
5366 static bool
5367 read_debug_names_from_section (struct objfile *objfile,
5368 const char *filename,
5369 struct dwarf2_section_info *section,
5370 mapped_debug_names &map)
5371 {
5372 if (dwarf2_section_empty_p (section))
5373 return false;
5374
5375 /* Older elfutils strip versions could keep the section in the main
5376 executable while splitting it for the separate debug info file. */
5377 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5378 return false;
5379
5380 dwarf2_read_section (objfile, section);
5381
5382 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5383
5384 const gdb_byte *addr = section->buffer;
5385
5386 bfd *const abfd = get_section_bfd_owner (section);
5387
5388 unsigned int bytes_read;
5389 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5390 addr += bytes_read;
5391
5392 map.dwarf5_is_dwarf64 = bytes_read != 4;
5393 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5394 if (bytes_read + length != section->size)
5395 {
5396 /* There may be multiple per-CU indices. */
5397 warning (_("Section .debug_names in %s length %s does not match "
5398 "section length %s, ignoring .debug_names."),
5399 filename, plongest (bytes_read + length),
5400 pulongest (section->size));
5401 return false;
5402 }
5403
5404 /* The version number. */
5405 uint16_t version = read_2_bytes (abfd, addr);
5406 addr += 2;
5407 if (version != 5)
5408 {
5409 warning (_("Section .debug_names in %s has unsupported version %d, "
5410 "ignoring .debug_names."),
5411 filename, version);
5412 return false;
5413 }
5414
5415 /* Padding. */
5416 uint16_t padding = read_2_bytes (abfd, addr);
5417 addr += 2;
5418 if (padding != 0)
5419 {
5420 warning (_("Section .debug_names in %s has unsupported padding %d, "
5421 "ignoring .debug_names."),
5422 filename, padding);
5423 return false;
5424 }
5425
5426 /* comp_unit_count - The number of CUs in the CU list. */
5427 map.cu_count = read_4_bytes (abfd, addr);
5428 addr += 4;
5429
5430 /* local_type_unit_count - The number of TUs in the local TU
5431 list. */
5432 map.tu_count = read_4_bytes (abfd, addr);
5433 addr += 4;
5434
5435 /* foreign_type_unit_count - The number of TUs in the foreign TU
5436 list. */
5437 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5438 addr += 4;
5439 if (foreign_tu_count != 0)
5440 {
5441 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5442 "ignoring .debug_names."),
5443 filename, static_cast<unsigned long> (foreign_tu_count));
5444 return false;
5445 }
5446
5447 /* bucket_count - The number of hash buckets in the hash lookup
5448 table. */
5449 map.bucket_count = read_4_bytes (abfd, addr);
5450 addr += 4;
5451
5452 /* name_count - The number of unique names in the index. */
5453 map.name_count = read_4_bytes (abfd, addr);
5454 addr += 4;
5455
5456 /* abbrev_table_size - The size in bytes of the abbreviations
5457 table. */
5458 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5459 addr += 4;
5460
5461 /* augmentation_string_size - The size in bytes of the augmentation
5462 string. This value is rounded up to a multiple of 4. */
5463 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5464 addr += 4;
5465 map.augmentation_is_gdb = ((augmentation_string_size
5466 == sizeof (dwarf5_augmentation))
5467 && memcmp (addr, dwarf5_augmentation,
5468 sizeof (dwarf5_augmentation)) == 0);
5469 augmentation_string_size += (-augmentation_string_size) & 3;
5470 addr += augmentation_string_size;
5471
5472 /* List of CUs */
5473 map.cu_table_reordered = addr;
5474 addr += map.cu_count * map.offset_size;
5475
5476 /* List of Local TUs */
5477 map.tu_table_reordered = addr;
5478 addr += map.tu_count * map.offset_size;
5479
5480 /* Hash Lookup Table */
5481 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5482 addr += map.bucket_count * 4;
5483 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5484 addr += map.name_count * 4;
5485
5486 /* Name Table */
5487 map.name_table_string_offs_reordered = addr;
5488 addr += map.name_count * map.offset_size;
5489 map.name_table_entry_offs_reordered = addr;
5490 addr += map.name_count * map.offset_size;
5491
5492 const gdb_byte *abbrev_table_start = addr;
5493 for (;;)
5494 {
5495 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5496 addr += bytes_read;
5497 if (index_num == 0)
5498 break;
5499
5500 const auto insertpair
5501 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5502 if (!insertpair.second)
5503 {
5504 warning (_("Section .debug_names in %s has duplicate index %s, "
5505 "ignoring .debug_names."),
5506 filename, pulongest (index_num));
5507 return false;
5508 }
5509 mapped_debug_names::index_val &indexval = insertpair.first->second;
5510 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5511 addr += bytes_read;
5512
5513 for (;;)
5514 {
5515 mapped_debug_names::index_val::attr attr;
5516 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5517 addr += bytes_read;
5518 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5519 addr += bytes_read;
5520 if (attr.form == DW_FORM_implicit_const)
5521 {
5522 attr.implicit_const = read_signed_leb128 (abfd, addr,
5523 &bytes_read);
5524 addr += bytes_read;
5525 }
5526 if (attr.dw_idx == 0 && attr.form == 0)
5527 break;
5528 indexval.attr_vec.push_back (std::move (attr));
5529 }
5530 }
5531 if (addr != abbrev_table_start + abbrev_table_size)
5532 {
5533 warning (_("Section .debug_names in %s has abbreviation_table "
5534 "of size %s vs. written as %u, ignoring .debug_names."),
5535 filename, plongest (addr - abbrev_table_start),
5536 abbrev_table_size);
5537 return false;
5538 }
5539 map.entry_pool = addr;
5540
5541 return true;
5542 }
5543
5544 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5545 list. */
5546
5547 static void
5548 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5549 const mapped_debug_names &map,
5550 dwarf2_section_info &section,
5551 bool is_dwz)
5552 {
5553 sect_offset sect_off_prev;
5554 for (uint32_t i = 0; i <= map.cu_count; ++i)
5555 {
5556 sect_offset sect_off_next;
5557 if (i < map.cu_count)
5558 {
5559 sect_off_next
5560 = (sect_offset) (extract_unsigned_integer
5561 (map.cu_table_reordered + i * map.offset_size,
5562 map.offset_size,
5563 map.dwarf5_byte_order));
5564 }
5565 else
5566 sect_off_next = (sect_offset) section.size;
5567 if (i >= 1)
5568 {
5569 const ULONGEST length = sect_off_next - sect_off_prev;
5570 dwarf2_per_cu_data *per_cu
5571 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5572 sect_off_prev, length);
5573 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5574 }
5575 sect_off_prev = sect_off_next;
5576 }
5577 }
5578
5579 /* Read the CU list from the mapped index, and use it to create all
5580 the CU objects for this dwarf2_per_objfile. */
5581
5582 static void
5583 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5584 const mapped_debug_names &map,
5585 const mapped_debug_names &dwz_map)
5586 {
5587 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5588 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5589
5590 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5591 dwarf2_per_objfile->info,
5592 false /* is_dwz */);
5593
5594 if (dwz_map.cu_count == 0)
5595 return;
5596
5597 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5598 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5599 true /* is_dwz */);
5600 }
5601
5602 /* Read .debug_names. If everything went ok, initialize the "quick"
5603 elements of all the CUs and return true. Otherwise, return false. */
5604
5605 static bool
5606 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5607 {
5608 std::unique_ptr<mapped_debug_names> map
5609 (new mapped_debug_names (dwarf2_per_objfile));
5610 mapped_debug_names dwz_map (dwarf2_per_objfile);
5611 struct objfile *objfile = dwarf2_per_objfile->objfile;
5612
5613 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5614 &dwarf2_per_objfile->debug_names,
5615 *map))
5616 return false;
5617
5618 /* Don't use the index if it's empty. */
5619 if (map->name_count == 0)
5620 return false;
5621
5622 /* If there is a .dwz file, read it so we can get its CU list as
5623 well. */
5624 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5625 if (dwz != NULL)
5626 {
5627 if (!read_debug_names_from_section (objfile,
5628 bfd_get_filename (dwz->dwz_bfd.get ()),
5629 &dwz->debug_names, dwz_map))
5630 {
5631 warning (_("could not read '.debug_names' section from %s; skipping"),
5632 bfd_get_filename (dwz->dwz_bfd.get ()));
5633 return false;
5634 }
5635 }
5636
5637 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5638
5639 if (map->tu_count != 0)
5640 {
5641 /* We can only handle a single .debug_types when we have an
5642 index. */
5643 if (dwarf2_per_objfile->types.size () != 1)
5644 return false;
5645
5646 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5647
5648 create_signatured_type_table_from_debug_names
5649 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5650 }
5651
5652 create_addrmap_from_aranges (dwarf2_per_objfile,
5653 &dwarf2_per_objfile->debug_aranges);
5654
5655 dwarf2_per_objfile->debug_names_table = std::move (map);
5656 dwarf2_per_objfile->using_index = 1;
5657 dwarf2_per_objfile->quick_file_names_table =
5658 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5659
5660 return true;
5661 }
5662
5663 /* Type used to manage iterating over all CUs looking for a symbol for
5664 .debug_names. */
5665
5666 class dw2_debug_names_iterator
5667 {
5668 public:
5669 dw2_debug_names_iterator (const mapped_debug_names &map,
5670 gdb::optional<block_enum> block_index,
5671 domain_enum domain,
5672 const char *name)
5673 : m_map (map), m_block_index (block_index), m_domain (domain),
5674 m_addr (find_vec_in_debug_names (map, name))
5675 {}
5676
5677 dw2_debug_names_iterator (const mapped_debug_names &map,
5678 search_domain search, uint32_t namei)
5679 : m_map (map),
5680 m_search (search),
5681 m_addr (find_vec_in_debug_names (map, namei))
5682 {}
5683
5684 dw2_debug_names_iterator (const mapped_debug_names &map,
5685 block_enum block_index, domain_enum domain,
5686 uint32_t namei)
5687 : m_map (map), m_block_index (block_index), m_domain (domain),
5688 m_addr (find_vec_in_debug_names (map, namei))
5689 {}
5690
5691 /* Return the next matching CU or NULL if there are no more. */
5692 dwarf2_per_cu_data *next ();
5693
5694 private:
5695 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5696 const char *name);
5697 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5698 uint32_t namei);
5699
5700 /* The internalized form of .debug_names. */
5701 const mapped_debug_names &m_map;
5702
5703 /* If set, only look for symbols that match that block. Valid values are
5704 GLOBAL_BLOCK and STATIC_BLOCK. */
5705 const gdb::optional<block_enum> m_block_index;
5706
5707 /* The kind of symbol we're looking for. */
5708 const domain_enum m_domain = UNDEF_DOMAIN;
5709 const search_domain m_search = ALL_DOMAIN;
5710
5711 /* The list of CUs from the index entry of the symbol, or NULL if
5712 not found. */
5713 const gdb_byte *m_addr;
5714 };
5715
5716 const char *
5717 mapped_debug_names::namei_to_name (uint32_t namei) const
5718 {
5719 const ULONGEST namei_string_offs
5720 = extract_unsigned_integer ((name_table_string_offs_reordered
5721 + namei * offset_size),
5722 offset_size,
5723 dwarf5_byte_order);
5724 return read_indirect_string_at_offset
5725 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5726 }
5727
5728 /* Find a slot in .debug_names for the object named NAME. If NAME is
5729 found, return pointer to its pool data. If NAME cannot be found,
5730 return NULL. */
5731
5732 const gdb_byte *
5733 dw2_debug_names_iterator::find_vec_in_debug_names
5734 (const mapped_debug_names &map, const char *name)
5735 {
5736 int (*cmp) (const char *, const char *);
5737
5738 gdb::unique_xmalloc_ptr<char> without_params;
5739 if (current_language->la_language == language_cplus
5740 || current_language->la_language == language_fortran
5741 || current_language->la_language == language_d)
5742 {
5743 /* NAME is already canonical. Drop any qualifiers as
5744 .debug_names does not contain any. */
5745
5746 if (strchr (name, '(') != NULL)
5747 {
5748 without_params = cp_remove_params (name);
5749 if (without_params != NULL)
5750 name = without_params.get ();
5751 }
5752 }
5753
5754 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5755
5756 const uint32_t full_hash = dwarf5_djb_hash (name);
5757 uint32_t namei
5758 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5759 (map.bucket_table_reordered
5760 + (full_hash % map.bucket_count)), 4,
5761 map.dwarf5_byte_order);
5762 if (namei == 0)
5763 return NULL;
5764 --namei;
5765 if (namei >= map.name_count)
5766 {
5767 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5768 "[in module %s]"),
5769 namei, map.name_count,
5770 objfile_name (map.dwarf2_per_objfile->objfile));
5771 return NULL;
5772 }
5773
5774 for (;;)
5775 {
5776 const uint32_t namei_full_hash
5777 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5778 (map.hash_table_reordered + namei), 4,
5779 map.dwarf5_byte_order);
5780 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5781 return NULL;
5782
5783 if (full_hash == namei_full_hash)
5784 {
5785 const char *const namei_string = map.namei_to_name (namei);
5786
5787 #if 0 /* An expensive sanity check. */
5788 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5789 {
5790 complaint (_("Wrong .debug_names hash for string at index %u "
5791 "[in module %s]"),
5792 namei, objfile_name (dwarf2_per_objfile->objfile));
5793 return NULL;
5794 }
5795 #endif
5796
5797 if (cmp (namei_string, name) == 0)
5798 {
5799 const ULONGEST namei_entry_offs
5800 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5801 + namei * map.offset_size),
5802 map.offset_size, map.dwarf5_byte_order);
5803 return map.entry_pool + namei_entry_offs;
5804 }
5805 }
5806
5807 ++namei;
5808 if (namei >= map.name_count)
5809 return NULL;
5810 }
5811 }
5812
5813 const gdb_byte *
5814 dw2_debug_names_iterator::find_vec_in_debug_names
5815 (const mapped_debug_names &map, uint32_t namei)
5816 {
5817 if (namei >= map.name_count)
5818 {
5819 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5820 "[in module %s]"),
5821 namei, map.name_count,
5822 objfile_name (map.dwarf2_per_objfile->objfile));
5823 return NULL;
5824 }
5825
5826 const ULONGEST namei_entry_offs
5827 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5828 + namei * map.offset_size),
5829 map.offset_size, map.dwarf5_byte_order);
5830 return map.entry_pool + namei_entry_offs;
5831 }
5832
5833 /* See dw2_debug_names_iterator. */
5834
5835 dwarf2_per_cu_data *
5836 dw2_debug_names_iterator::next ()
5837 {
5838 if (m_addr == NULL)
5839 return NULL;
5840
5841 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5842 struct objfile *objfile = dwarf2_per_objfile->objfile;
5843 bfd *const abfd = objfile->obfd;
5844
5845 again:
5846
5847 unsigned int bytes_read;
5848 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5849 m_addr += bytes_read;
5850 if (abbrev == 0)
5851 return NULL;
5852
5853 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5854 if (indexval_it == m_map.abbrev_map.cend ())
5855 {
5856 complaint (_("Wrong .debug_names undefined abbrev code %s "
5857 "[in module %s]"),
5858 pulongest (abbrev), objfile_name (objfile));
5859 return NULL;
5860 }
5861 const mapped_debug_names::index_val &indexval = indexval_it->second;
5862 enum class symbol_linkage {
5863 unknown,
5864 static_,
5865 extern_,
5866 } symbol_linkage_ = symbol_linkage::unknown;
5867 dwarf2_per_cu_data *per_cu = NULL;
5868 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5869 {
5870 ULONGEST ull;
5871 switch (attr.form)
5872 {
5873 case DW_FORM_implicit_const:
5874 ull = attr.implicit_const;
5875 break;
5876 case DW_FORM_flag_present:
5877 ull = 1;
5878 break;
5879 case DW_FORM_udata:
5880 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5881 m_addr += bytes_read;
5882 break;
5883 default:
5884 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5885 dwarf_form_name (attr.form),
5886 objfile_name (objfile));
5887 return NULL;
5888 }
5889 switch (attr.dw_idx)
5890 {
5891 case DW_IDX_compile_unit:
5892 /* Don't crash on bad data. */
5893 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5894 {
5895 complaint (_(".debug_names entry has bad CU index %s"
5896 " [in module %s]"),
5897 pulongest (ull),
5898 objfile_name (dwarf2_per_objfile->objfile));
5899 continue;
5900 }
5901 per_cu = dwarf2_per_objfile->get_cutu (ull);
5902 break;
5903 case DW_IDX_type_unit:
5904 /* Don't crash on bad data. */
5905 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5906 {
5907 complaint (_(".debug_names entry has bad TU index %s"
5908 " [in module %s]"),
5909 pulongest (ull),
5910 objfile_name (dwarf2_per_objfile->objfile));
5911 continue;
5912 }
5913 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5914 break;
5915 case DW_IDX_GNU_internal:
5916 if (!m_map.augmentation_is_gdb)
5917 break;
5918 symbol_linkage_ = symbol_linkage::static_;
5919 break;
5920 case DW_IDX_GNU_external:
5921 if (!m_map.augmentation_is_gdb)
5922 break;
5923 symbol_linkage_ = symbol_linkage::extern_;
5924 break;
5925 }
5926 }
5927
5928 /* Skip if already read in. */
5929 if (per_cu->v.quick->compunit_symtab)
5930 goto again;
5931
5932 /* Check static vs global. */
5933 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5934 {
5935 const bool want_static = *m_block_index == STATIC_BLOCK;
5936 const bool symbol_is_static =
5937 symbol_linkage_ == symbol_linkage::static_;
5938 if (want_static != symbol_is_static)
5939 goto again;
5940 }
5941
5942 /* Match dw2_symtab_iter_next, symbol_kind
5943 and debug_names::psymbol_tag. */
5944 switch (m_domain)
5945 {
5946 case VAR_DOMAIN:
5947 switch (indexval.dwarf_tag)
5948 {
5949 case DW_TAG_variable:
5950 case DW_TAG_subprogram:
5951 /* Some types are also in VAR_DOMAIN. */
5952 case DW_TAG_typedef:
5953 case DW_TAG_structure_type:
5954 break;
5955 default:
5956 goto again;
5957 }
5958 break;
5959 case STRUCT_DOMAIN:
5960 switch (indexval.dwarf_tag)
5961 {
5962 case DW_TAG_typedef:
5963 case DW_TAG_structure_type:
5964 break;
5965 default:
5966 goto again;
5967 }
5968 break;
5969 case LABEL_DOMAIN:
5970 switch (indexval.dwarf_tag)
5971 {
5972 case 0:
5973 case DW_TAG_variable:
5974 break;
5975 default:
5976 goto again;
5977 }
5978 break;
5979 case MODULE_DOMAIN:
5980 switch (indexval.dwarf_tag)
5981 {
5982 case DW_TAG_module:
5983 break;
5984 default:
5985 goto again;
5986 }
5987 break;
5988 default:
5989 break;
5990 }
5991
5992 /* Match dw2_expand_symtabs_matching, symbol_kind and
5993 debug_names::psymbol_tag. */
5994 switch (m_search)
5995 {
5996 case VARIABLES_DOMAIN:
5997 switch (indexval.dwarf_tag)
5998 {
5999 case DW_TAG_variable:
6000 break;
6001 default:
6002 goto again;
6003 }
6004 break;
6005 case FUNCTIONS_DOMAIN:
6006 switch (indexval.dwarf_tag)
6007 {
6008 case DW_TAG_subprogram:
6009 break;
6010 default:
6011 goto again;
6012 }
6013 break;
6014 case TYPES_DOMAIN:
6015 switch (indexval.dwarf_tag)
6016 {
6017 case DW_TAG_typedef:
6018 case DW_TAG_structure_type:
6019 break;
6020 default:
6021 goto again;
6022 }
6023 break;
6024 case MODULES_DOMAIN:
6025 switch (indexval.dwarf_tag)
6026 {
6027 case DW_TAG_module:
6028 break;
6029 default:
6030 goto again;
6031 }
6032 default:
6033 break;
6034 }
6035
6036 return per_cu;
6037 }
6038
6039 static struct compunit_symtab *
6040 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
6041 const char *name, domain_enum domain)
6042 {
6043 struct dwarf2_per_objfile *dwarf2_per_objfile
6044 = get_dwarf2_per_objfile (objfile);
6045
6046 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6047 if (!mapp)
6048 {
6049 /* index is NULL if OBJF_READNOW. */
6050 return NULL;
6051 }
6052 const auto &map = *mapp;
6053
6054 dw2_debug_names_iterator iter (map, block_index, domain, name);
6055
6056 struct compunit_symtab *stab_best = NULL;
6057 struct dwarf2_per_cu_data *per_cu;
6058 while ((per_cu = iter.next ()) != NULL)
6059 {
6060 struct symbol *sym, *with_opaque = NULL;
6061 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
6062 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6063 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6064
6065 sym = block_find_symbol (block, name, domain,
6066 block_find_non_opaque_type_preferred,
6067 &with_opaque);
6068
6069 /* Some caution must be observed with overloaded functions and
6070 methods, since the index will not contain any overload
6071 information (but NAME might contain it). */
6072
6073 if (sym != NULL
6074 && strcmp_iw (sym->search_name (), name) == 0)
6075 return stab;
6076 if (with_opaque != NULL
6077 && strcmp_iw (with_opaque->search_name (), name) == 0)
6078 stab_best = stab;
6079
6080 /* Keep looking through other CUs. */
6081 }
6082
6083 return stab_best;
6084 }
6085
6086 /* This dumps minimal information about .debug_names. It is called
6087 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6088 uses this to verify that .debug_names has been loaded. */
6089
6090 static void
6091 dw2_debug_names_dump (struct objfile *objfile)
6092 {
6093 struct dwarf2_per_objfile *dwarf2_per_objfile
6094 = get_dwarf2_per_objfile (objfile);
6095
6096 gdb_assert (dwarf2_per_objfile->using_index);
6097 printf_filtered (".debug_names:");
6098 if (dwarf2_per_objfile->debug_names_table)
6099 printf_filtered (" exists\n");
6100 else
6101 printf_filtered (" faked for \"readnow\"\n");
6102 printf_filtered ("\n");
6103 }
6104
6105 static void
6106 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6107 const char *func_name)
6108 {
6109 struct dwarf2_per_objfile *dwarf2_per_objfile
6110 = get_dwarf2_per_objfile (objfile);
6111
6112 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6113 if (dwarf2_per_objfile->debug_names_table)
6114 {
6115 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6116
6117 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
6118
6119 struct dwarf2_per_cu_data *per_cu;
6120 while ((per_cu = iter.next ()) != NULL)
6121 dw2_instantiate_symtab (per_cu, false);
6122 }
6123 }
6124
6125 static void
6126 dw2_debug_names_map_matching_symbols
6127 (struct objfile *objfile,
6128 const lookup_name_info &name, domain_enum domain,
6129 int global,
6130 gdb::function_view<symbol_found_callback_ftype> callback,
6131 symbol_compare_ftype *ordered_compare)
6132 {
6133 struct dwarf2_per_objfile *dwarf2_per_objfile
6134 = get_dwarf2_per_objfile (objfile);
6135
6136 /* debug_names_table is NULL if OBJF_READNOW. */
6137 if (!dwarf2_per_objfile->debug_names_table)
6138 return;
6139
6140 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6141 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6142
6143 const char *match_name = name.ada ().lookup_name ().c_str ();
6144 auto matcher = [&] (const char *symname)
6145 {
6146 if (ordered_compare == nullptr)
6147 return true;
6148 return ordered_compare (symname, match_name) == 0;
6149 };
6150
6151 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6152 [&] (offset_type namei)
6153 {
6154 /* The name was matched, now expand corresponding CUs that were
6155 marked. */
6156 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6157
6158 struct dwarf2_per_cu_data *per_cu;
6159 while ((per_cu = iter.next ()) != NULL)
6160 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6161 return true;
6162 });
6163
6164 /* It's a shame we couldn't do this inside the
6165 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6166 that have already been expanded. Instead, this loop matches what
6167 the psymtab code does. */
6168 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6169 {
6170 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6171 if (cust != nullptr)
6172 {
6173 const struct block *block
6174 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6175 if (!iterate_over_symbols_terminated (block, name,
6176 domain, callback))
6177 break;
6178 }
6179 }
6180 }
6181
6182 static void
6183 dw2_debug_names_expand_symtabs_matching
6184 (struct objfile *objfile,
6185 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6186 const lookup_name_info &lookup_name,
6187 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6188 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6189 enum search_domain kind)
6190 {
6191 struct dwarf2_per_objfile *dwarf2_per_objfile
6192 = get_dwarf2_per_objfile (objfile);
6193
6194 /* debug_names_table is NULL if OBJF_READNOW. */
6195 if (!dwarf2_per_objfile->debug_names_table)
6196 return;
6197
6198 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6199
6200 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6201
6202 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6203 symbol_matcher,
6204 kind, [&] (offset_type namei)
6205 {
6206 /* The name was matched, now expand corresponding CUs that were
6207 marked. */
6208 dw2_debug_names_iterator iter (map, kind, namei);
6209
6210 struct dwarf2_per_cu_data *per_cu;
6211 while ((per_cu = iter.next ()) != NULL)
6212 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6213 expansion_notify);
6214 return true;
6215 });
6216 }
6217
6218 const struct quick_symbol_functions dwarf2_debug_names_functions =
6219 {
6220 dw2_has_symbols,
6221 dw2_find_last_source_symtab,
6222 dw2_forget_cached_source_info,
6223 dw2_map_symtabs_matching_filename,
6224 dw2_debug_names_lookup_symbol,
6225 dw2_print_stats,
6226 dw2_debug_names_dump,
6227 dw2_debug_names_expand_symtabs_for_function,
6228 dw2_expand_all_symtabs,
6229 dw2_expand_symtabs_with_fullname,
6230 dw2_debug_names_map_matching_symbols,
6231 dw2_debug_names_expand_symtabs_matching,
6232 dw2_find_pc_sect_compunit_symtab,
6233 NULL,
6234 dw2_map_symbol_filenames
6235 };
6236
6237 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6238 to either a dwarf2_per_objfile or dwz_file object. */
6239
6240 template <typename T>
6241 static gdb::array_view<const gdb_byte>
6242 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6243 {
6244 dwarf2_section_info *section = &section_owner->gdb_index;
6245
6246 if (dwarf2_section_empty_p (section))
6247 return {};
6248
6249 /* Older elfutils strip versions could keep the section in the main
6250 executable while splitting it for the separate debug info file. */
6251 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6252 return {};
6253
6254 dwarf2_read_section (obj, section);
6255
6256 /* dwarf2_section_info::size is a bfd_size_type, while
6257 gdb::array_view works with size_t. On 32-bit hosts, with
6258 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6259 is 32-bit. So we need an explicit narrowing conversion here.
6260 This is fine, because it's impossible to allocate or mmap an
6261 array/buffer larger than what size_t can represent. */
6262 return gdb::make_array_view (section->buffer, section->size);
6263 }
6264
6265 /* Lookup the index cache for the contents of the index associated to
6266 DWARF2_OBJ. */
6267
6268 static gdb::array_view<const gdb_byte>
6269 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6270 {
6271 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6272 if (build_id == nullptr)
6273 return {};
6274
6275 return global_index_cache.lookup_gdb_index (build_id,
6276 &dwarf2_obj->index_cache_res);
6277 }
6278
6279 /* Same as the above, but for DWZ. */
6280
6281 static gdb::array_view<const gdb_byte>
6282 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6283 {
6284 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6285 if (build_id == nullptr)
6286 return {};
6287
6288 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6289 }
6290
6291 /* See symfile.h. */
6292
6293 bool
6294 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6295 {
6296 struct dwarf2_per_objfile *dwarf2_per_objfile
6297 = get_dwarf2_per_objfile (objfile);
6298
6299 /* If we're about to read full symbols, don't bother with the
6300 indices. In this case we also don't care if some other debug
6301 format is making psymtabs, because they are all about to be
6302 expanded anyway. */
6303 if ((objfile->flags & OBJF_READNOW))
6304 {
6305 dwarf2_per_objfile->using_index = 1;
6306 create_all_comp_units (dwarf2_per_objfile);
6307 create_all_type_units (dwarf2_per_objfile);
6308 dwarf2_per_objfile->quick_file_names_table
6309 = create_quick_file_names_table
6310 (dwarf2_per_objfile->all_comp_units.size ());
6311
6312 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6313 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6314 {
6315 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6316
6317 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6318 struct dwarf2_per_cu_quick_data);
6319 }
6320
6321 /* Return 1 so that gdb sees the "quick" functions. However,
6322 these functions will be no-ops because we will have expanded
6323 all symtabs. */
6324 *index_kind = dw_index_kind::GDB_INDEX;
6325 return true;
6326 }
6327
6328 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6329 {
6330 *index_kind = dw_index_kind::DEBUG_NAMES;
6331 return true;
6332 }
6333
6334 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6335 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6336 get_gdb_index_contents_from_section<dwz_file>))
6337 {
6338 *index_kind = dw_index_kind::GDB_INDEX;
6339 return true;
6340 }
6341
6342 /* ... otherwise, try to find the index in the index cache. */
6343 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6344 get_gdb_index_contents_from_cache,
6345 get_gdb_index_contents_from_cache_dwz))
6346 {
6347 global_index_cache.hit ();
6348 *index_kind = dw_index_kind::GDB_INDEX;
6349 return true;
6350 }
6351
6352 global_index_cache.miss ();
6353 return false;
6354 }
6355
6356 \f
6357
6358 /* Build a partial symbol table. */
6359
6360 void
6361 dwarf2_build_psymtabs (struct objfile *objfile)
6362 {
6363 struct dwarf2_per_objfile *dwarf2_per_objfile
6364 = get_dwarf2_per_objfile (objfile);
6365
6366 init_psymbol_list (objfile, 1024);
6367
6368 try
6369 {
6370 /* This isn't really ideal: all the data we allocate on the
6371 objfile's obstack is still uselessly kept around. However,
6372 freeing it seems unsafe. */
6373 psymtab_discarder psymtabs (objfile);
6374 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6375 psymtabs.keep ();
6376
6377 /* (maybe) store an index in the cache. */
6378 global_index_cache.store (dwarf2_per_objfile);
6379 }
6380 catch (const gdb_exception_error &except)
6381 {
6382 exception_print (gdb_stderr, except);
6383 }
6384 }
6385
6386 /* Return the total length of the CU described by HEADER. */
6387
6388 static unsigned int
6389 get_cu_length (const struct comp_unit_head *header)
6390 {
6391 return header->initial_length_size + header->length;
6392 }
6393
6394 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6395
6396 static inline bool
6397 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6398 {
6399 sect_offset bottom = cu_header->sect_off;
6400 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6401
6402 return sect_off >= bottom && sect_off < top;
6403 }
6404
6405 /* Find the base address of the compilation unit for range lists and
6406 location lists. It will normally be specified by DW_AT_low_pc.
6407 In DWARF-3 draft 4, the base address could be overridden by
6408 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6409 compilation units with discontinuous ranges. */
6410
6411 static void
6412 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6413 {
6414 struct attribute *attr;
6415
6416 cu->base_known = 0;
6417 cu->base_address = 0;
6418
6419 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6420 if (attr != nullptr)
6421 {
6422 cu->base_address = attr_value_as_address (attr);
6423 cu->base_known = 1;
6424 }
6425 else
6426 {
6427 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6428 if (attr != nullptr)
6429 {
6430 cu->base_address = attr_value_as_address (attr);
6431 cu->base_known = 1;
6432 }
6433 }
6434 }
6435
6436 /* Read in the comp unit header information from the debug_info at info_ptr.
6437 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6438 NOTE: This leaves members offset, first_die_offset to be filled in
6439 by the caller. */
6440
6441 static const gdb_byte *
6442 read_comp_unit_head (struct comp_unit_head *cu_header,
6443 const gdb_byte *info_ptr,
6444 struct dwarf2_section_info *section,
6445 rcuh_kind section_kind)
6446 {
6447 int signed_addr;
6448 unsigned int bytes_read;
6449 const char *filename = get_section_file_name (section);
6450 bfd *abfd = get_section_bfd_owner (section);
6451
6452 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6453 cu_header->initial_length_size = bytes_read;
6454 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6455 info_ptr += bytes_read;
6456 cu_header->version = read_2_bytes (abfd, info_ptr);
6457 if (cu_header->version < 2 || cu_header->version > 5)
6458 error (_("Dwarf Error: wrong version in compilation unit header "
6459 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6460 cu_header->version, filename);
6461 info_ptr += 2;
6462 if (cu_header->version < 5)
6463 switch (section_kind)
6464 {
6465 case rcuh_kind::COMPILE:
6466 cu_header->unit_type = DW_UT_compile;
6467 break;
6468 case rcuh_kind::TYPE:
6469 cu_header->unit_type = DW_UT_type;
6470 break;
6471 default:
6472 internal_error (__FILE__, __LINE__,
6473 _("read_comp_unit_head: invalid section_kind"));
6474 }
6475 else
6476 {
6477 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6478 (read_1_byte (abfd, info_ptr));
6479 info_ptr += 1;
6480 switch (cu_header->unit_type)
6481 {
6482 case DW_UT_compile:
6483 case DW_UT_partial:
6484 case DW_UT_skeleton:
6485 case DW_UT_split_compile:
6486 if (section_kind != rcuh_kind::COMPILE)
6487 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6488 "(is %s, should be %s) [in module %s]"),
6489 dwarf_unit_type_name (cu_header->unit_type),
6490 dwarf_unit_type_name (DW_UT_type), filename);
6491 break;
6492 case DW_UT_type:
6493 case DW_UT_split_type:
6494 section_kind = rcuh_kind::TYPE;
6495 break;
6496 default:
6497 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6498 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6499 "[in module %s]"), cu_header->unit_type,
6500 dwarf_unit_type_name (DW_UT_compile),
6501 dwarf_unit_type_name (DW_UT_skeleton),
6502 dwarf_unit_type_name (DW_UT_split_compile),
6503 dwarf_unit_type_name (DW_UT_type),
6504 dwarf_unit_type_name (DW_UT_split_type), filename);
6505 }
6506
6507 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6508 info_ptr += 1;
6509 }
6510 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6511 cu_header,
6512 &bytes_read);
6513 info_ptr += bytes_read;
6514 if (cu_header->version < 5)
6515 {
6516 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6517 info_ptr += 1;
6518 }
6519 signed_addr = bfd_get_sign_extend_vma (abfd);
6520 if (signed_addr < 0)
6521 internal_error (__FILE__, __LINE__,
6522 _("read_comp_unit_head: dwarf from non elf file"));
6523 cu_header->signed_addr_p = signed_addr;
6524
6525 bool header_has_signature = section_kind == rcuh_kind::TYPE
6526 || cu_header->unit_type == DW_UT_skeleton
6527 || cu_header->unit_type == DW_UT_split_compile;
6528
6529 if (header_has_signature)
6530 {
6531 cu_header->signature = read_8_bytes (abfd, info_ptr);
6532 info_ptr += 8;
6533 }
6534
6535 if (section_kind == rcuh_kind::TYPE)
6536 {
6537 LONGEST type_offset;
6538 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6539 info_ptr += bytes_read;
6540 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6541 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6542 error (_("Dwarf Error: Too big type_offset in compilation unit "
6543 "header (is %s) [in module %s]"), plongest (type_offset),
6544 filename);
6545 }
6546
6547 return info_ptr;
6548 }
6549
6550 /* Helper function that returns the proper abbrev section for
6551 THIS_CU. */
6552
6553 static struct dwarf2_section_info *
6554 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6555 {
6556 struct dwarf2_section_info *abbrev;
6557 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6558
6559 if (this_cu->is_dwz)
6560 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6561 else
6562 abbrev = &dwarf2_per_objfile->abbrev;
6563
6564 return abbrev;
6565 }
6566
6567 /* Subroutine of read_and_check_comp_unit_head and
6568 read_and_check_type_unit_head to simplify them.
6569 Perform various error checking on the header. */
6570
6571 static void
6572 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6573 struct comp_unit_head *header,
6574 struct dwarf2_section_info *section,
6575 struct dwarf2_section_info *abbrev_section)
6576 {
6577 const char *filename = get_section_file_name (section);
6578
6579 if (to_underlying (header->abbrev_sect_off)
6580 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6581 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6582 "(offset %s + 6) [in module %s]"),
6583 sect_offset_str (header->abbrev_sect_off),
6584 sect_offset_str (header->sect_off),
6585 filename);
6586
6587 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6588 avoid potential 32-bit overflow. */
6589 if (((ULONGEST) header->sect_off + get_cu_length (header))
6590 > section->size)
6591 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6592 "(offset %s + 0) [in module %s]"),
6593 header->length, sect_offset_str (header->sect_off),
6594 filename);
6595 }
6596
6597 /* Read in a CU/TU header and perform some basic error checking.
6598 The contents of the header are stored in HEADER.
6599 The result is a pointer to the start of the first DIE. */
6600
6601 static const gdb_byte *
6602 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6603 struct comp_unit_head *header,
6604 struct dwarf2_section_info *section,
6605 struct dwarf2_section_info *abbrev_section,
6606 const gdb_byte *info_ptr,
6607 rcuh_kind section_kind)
6608 {
6609 const gdb_byte *beg_of_comp_unit = info_ptr;
6610
6611 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6612
6613 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6614
6615 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6616
6617 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6618 abbrev_section);
6619
6620 return info_ptr;
6621 }
6622
6623 /* Fetch the abbreviation table offset from a comp or type unit header. */
6624
6625 static sect_offset
6626 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6627 struct dwarf2_section_info *section,
6628 sect_offset sect_off)
6629 {
6630 bfd *abfd = get_section_bfd_owner (section);
6631 const gdb_byte *info_ptr;
6632 unsigned int initial_length_size, offset_size;
6633 uint16_t version;
6634
6635 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6636 info_ptr = section->buffer + to_underlying (sect_off);
6637 read_initial_length (abfd, info_ptr, &initial_length_size);
6638 offset_size = initial_length_size == 4 ? 4 : 8;
6639 info_ptr += initial_length_size;
6640
6641 version = read_2_bytes (abfd, info_ptr);
6642 info_ptr += 2;
6643 if (version >= 5)
6644 {
6645 /* Skip unit type and address size. */
6646 info_ptr += 2;
6647 }
6648
6649 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6650 }
6651
6652 /* Allocate a new partial symtab for file named NAME and mark this new
6653 partial symtab as being an include of PST. */
6654
6655 static void
6656 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6657 struct objfile *objfile)
6658 {
6659 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6660
6661 if (!IS_ABSOLUTE_PATH (subpst->filename))
6662 {
6663 /* It shares objfile->objfile_obstack. */
6664 subpst->dirname = pst->dirname;
6665 }
6666
6667 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6668 subpst->dependencies[0] = pst;
6669 subpst->number_of_dependencies = 1;
6670
6671 subpst->read_symtab = pst->read_symtab;
6672
6673 /* No private part is necessary for include psymtabs. This property
6674 can be used to differentiate between such include psymtabs and
6675 the regular ones. */
6676 subpst->read_symtab_private = NULL;
6677 }
6678
6679 /* Read the Line Number Program data and extract the list of files
6680 included by the source file represented by PST. Build an include
6681 partial symtab for each of these included files. */
6682
6683 static void
6684 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6685 struct die_info *die,
6686 struct partial_symtab *pst)
6687 {
6688 line_header_up lh;
6689 struct attribute *attr;
6690
6691 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6692 if (attr != nullptr)
6693 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6694 if (lh == NULL)
6695 return; /* No linetable, so no includes. */
6696
6697 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6698 that we pass in the raw text_low here; that is ok because we're
6699 only decoding the line table to make include partial symtabs, and
6700 so the addresses aren't really used. */
6701 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6702 pst->raw_text_low (), 1);
6703 }
6704
6705 static hashval_t
6706 hash_signatured_type (const void *item)
6707 {
6708 const struct signatured_type *sig_type
6709 = (const struct signatured_type *) item;
6710
6711 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6712 return sig_type->signature;
6713 }
6714
6715 static int
6716 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6717 {
6718 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6719 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6720
6721 return lhs->signature == rhs->signature;
6722 }
6723
6724 /* Allocate a hash table for signatured types. */
6725
6726 static htab_t
6727 allocate_signatured_type_table (struct objfile *objfile)
6728 {
6729 return htab_create_alloc_ex (41,
6730 hash_signatured_type,
6731 eq_signatured_type,
6732 NULL,
6733 &objfile->objfile_obstack,
6734 hashtab_obstack_allocate,
6735 dummy_obstack_deallocate);
6736 }
6737
6738 /* A helper function to add a signatured type CU to a table. */
6739
6740 static int
6741 add_signatured_type_cu_to_table (void **slot, void *datum)
6742 {
6743 struct signatured_type *sigt = (struct signatured_type *) *slot;
6744 std::vector<signatured_type *> *all_type_units
6745 = (std::vector<signatured_type *> *) datum;
6746
6747 all_type_units->push_back (sigt);
6748
6749 return 1;
6750 }
6751
6752 /* A helper for create_debug_types_hash_table. Read types from SECTION
6753 and fill them into TYPES_HTAB. It will process only type units,
6754 therefore DW_UT_type. */
6755
6756 static void
6757 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6758 struct dwo_file *dwo_file,
6759 dwarf2_section_info *section, htab_t &types_htab,
6760 rcuh_kind section_kind)
6761 {
6762 struct objfile *objfile = dwarf2_per_objfile->objfile;
6763 struct dwarf2_section_info *abbrev_section;
6764 bfd *abfd;
6765 const gdb_byte *info_ptr, *end_ptr;
6766
6767 abbrev_section = (dwo_file != NULL
6768 ? &dwo_file->sections.abbrev
6769 : &dwarf2_per_objfile->abbrev);
6770
6771 if (dwarf_read_debug)
6772 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6773 get_section_name (section),
6774 get_section_file_name (abbrev_section));
6775
6776 dwarf2_read_section (objfile, section);
6777 info_ptr = section->buffer;
6778
6779 if (info_ptr == NULL)
6780 return;
6781
6782 /* We can't set abfd until now because the section may be empty or
6783 not present, in which case the bfd is unknown. */
6784 abfd = get_section_bfd_owner (section);
6785
6786 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6787 because we don't need to read any dies: the signature is in the
6788 header. */
6789
6790 end_ptr = info_ptr + section->size;
6791 while (info_ptr < end_ptr)
6792 {
6793 struct signatured_type *sig_type;
6794 struct dwo_unit *dwo_tu;
6795 void **slot;
6796 const gdb_byte *ptr = info_ptr;
6797 struct comp_unit_head header;
6798 unsigned int length;
6799
6800 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6801
6802 /* Initialize it due to a false compiler warning. */
6803 header.signature = -1;
6804 header.type_cu_offset_in_tu = (cu_offset) -1;
6805
6806 /* We need to read the type's signature in order to build the hash
6807 table, but we don't need anything else just yet. */
6808
6809 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6810 abbrev_section, ptr, section_kind);
6811
6812 length = get_cu_length (&header);
6813
6814 /* Skip dummy type units. */
6815 if (ptr >= info_ptr + length
6816 || peek_abbrev_code (abfd, ptr) == 0
6817 || header.unit_type != DW_UT_type)
6818 {
6819 info_ptr += length;
6820 continue;
6821 }
6822
6823 if (types_htab == NULL)
6824 {
6825 if (dwo_file)
6826 types_htab = allocate_dwo_unit_table (objfile);
6827 else
6828 types_htab = allocate_signatured_type_table (objfile);
6829 }
6830
6831 if (dwo_file)
6832 {
6833 sig_type = NULL;
6834 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6835 struct dwo_unit);
6836 dwo_tu->dwo_file = dwo_file;
6837 dwo_tu->signature = header.signature;
6838 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6839 dwo_tu->section = section;
6840 dwo_tu->sect_off = sect_off;
6841 dwo_tu->length = length;
6842 }
6843 else
6844 {
6845 /* N.B.: type_offset is not usable if this type uses a DWO file.
6846 The real type_offset is in the DWO file. */
6847 dwo_tu = NULL;
6848 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6849 struct signatured_type);
6850 sig_type->signature = header.signature;
6851 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6852 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6853 sig_type->per_cu.is_debug_types = 1;
6854 sig_type->per_cu.section = section;
6855 sig_type->per_cu.sect_off = sect_off;
6856 sig_type->per_cu.length = length;
6857 }
6858
6859 slot = htab_find_slot (types_htab,
6860 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6861 INSERT);
6862 gdb_assert (slot != NULL);
6863 if (*slot != NULL)
6864 {
6865 sect_offset dup_sect_off;
6866
6867 if (dwo_file)
6868 {
6869 const struct dwo_unit *dup_tu
6870 = (const struct dwo_unit *) *slot;
6871
6872 dup_sect_off = dup_tu->sect_off;
6873 }
6874 else
6875 {
6876 const struct signatured_type *dup_tu
6877 = (const struct signatured_type *) *slot;
6878
6879 dup_sect_off = dup_tu->per_cu.sect_off;
6880 }
6881
6882 complaint (_("debug type entry at offset %s is duplicate to"
6883 " the entry at offset %s, signature %s"),
6884 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6885 hex_string (header.signature));
6886 }
6887 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6888
6889 if (dwarf_read_debug > 1)
6890 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6891 sect_offset_str (sect_off),
6892 hex_string (header.signature));
6893
6894 info_ptr += length;
6895 }
6896 }
6897
6898 /* Create the hash table of all entries in the .debug_types
6899 (or .debug_types.dwo) section(s).
6900 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6901 otherwise it is NULL.
6902
6903 The result is a pointer to the hash table or NULL if there are no types.
6904
6905 Note: This function processes DWO files only, not DWP files. */
6906
6907 static void
6908 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6909 struct dwo_file *dwo_file,
6910 gdb::array_view<dwarf2_section_info> type_sections,
6911 htab_t &types_htab)
6912 {
6913 for (dwarf2_section_info &section : type_sections)
6914 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6915 types_htab, rcuh_kind::TYPE);
6916 }
6917
6918 /* Create the hash table of all entries in the .debug_types section,
6919 and initialize all_type_units.
6920 The result is zero if there is an error (e.g. missing .debug_types section),
6921 otherwise non-zero. */
6922
6923 static int
6924 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6925 {
6926 htab_t types_htab = NULL;
6927
6928 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6929 &dwarf2_per_objfile->info, types_htab,
6930 rcuh_kind::COMPILE);
6931 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6932 dwarf2_per_objfile->types, types_htab);
6933 if (types_htab == NULL)
6934 {
6935 dwarf2_per_objfile->signatured_types = NULL;
6936 return 0;
6937 }
6938
6939 dwarf2_per_objfile->signatured_types = types_htab;
6940
6941 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6942 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6943
6944 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6945 &dwarf2_per_objfile->all_type_units);
6946
6947 return 1;
6948 }
6949
6950 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6951 If SLOT is non-NULL, it is the entry to use in the hash table.
6952 Otherwise we find one. */
6953
6954 static struct signatured_type *
6955 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6956 void **slot)
6957 {
6958 struct objfile *objfile = dwarf2_per_objfile->objfile;
6959
6960 if (dwarf2_per_objfile->all_type_units.size ()
6961 == dwarf2_per_objfile->all_type_units.capacity ())
6962 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6963
6964 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6965 struct signatured_type);
6966
6967 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6968 sig_type->signature = sig;
6969 sig_type->per_cu.is_debug_types = 1;
6970 if (dwarf2_per_objfile->using_index)
6971 {
6972 sig_type->per_cu.v.quick =
6973 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6974 struct dwarf2_per_cu_quick_data);
6975 }
6976
6977 if (slot == NULL)
6978 {
6979 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6980 sig_type, INSERT);
6981 }
6982 gdb_assert (*slot == NULL);
6983 *slot = sig_type;
6984 /* The rest of sig_type must be filled in by the caller. */
6985 return sig_type;
6986 }
6987
6988 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6989 Fill in SIG_ENTRY with DWO_ENTRY. */
6990
6991 static void
6992 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6993 struct signatured_type *sig_entry,
6994 struct dwo_unit *dwo_entry)
6995 {
6996 /* Make sure we're not clobbering something we don't expect to. */
6997 gdb_assert (! sig_entry->per_cu.queued);
6998 gdb_assert (sig_entry->per_cu.cu == NULL);
6999 if (dwarf2_per_objfile->using_index)
7000 {
7001 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7002 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7003 }
7004 else
7005 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7006 gdb_assert (sig_entry->signature == dwo_entry->signature);
7007 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7008 gdb_assert (sig_entry->type_unit_group == NULL);
7009 gdb_assert (sig_entry->dwo_unit == NULL);
7010
7011 sig_entry->per_cu.section = dwo_entry->section;
7012 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7013 sig_entry->per_cu.length = dwo_entry->length;
7014 sig_entry->per_cu.reading_dwo_directly = 1;
7015 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7016 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7017 sig_entry->dwo_unit = dwo_entry;
7018 }
7019
7020 /* Subroutine of lookup_signatured_type.
7021 If we haven't read the TU yet, create the signatured_type data structure
7022 for a TU to be read in directly from a DWO file, bypassing the stub.
7023 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7024 using .gdb_index, then when reading a CU we want to stay in the DWO file
7025 containing that CU. Otherwise we could end up reading several other DWO
7026 files (due to comdat folding) to process the transitive closure of all the
7027 mentioned TUs, and that can be slow. The current DWO file will have every
7028 type signature that it needs.
7029 We only do this for .gdb_index because in the psymtab case we already have
7030 to read all the DWOs to build the type unit groups. */
7031
7032 static struct signatured_type *
7033 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7034 {
7035 struct dwarf2_per_objfile *dwarf2_per_objfile
7036 = cu->per_cu->dwarf2_per_objfile;
7037 struct objfile *objfile = dwarf2_per_objfile->objfile;
7038 struct dwo_file *dwo_file;
7039 struct dwo_unit find_dwo_entry, *dwo_entry;
7040 struct signatured_type find_sig_entry, *sig_entry;
7041 void **slot;
7042
7043 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7044
7045 /* If TU skeletons have been removed then we may not have read in any
7046 TUs yet. */
7047 if (dwarf2_per_objfile->signatured_types == NULL)
7048 {
7049 dwarf2_per_objfile->signatured_types
7050 = allocate_signatured_type_table (objfile);
7051 }
7052
7053 /* We only ever need to read in one copy of a signatured type.
7054 Use the global signatured_types array to do our own comdat-folding
7055 of types. If this is the first time we're reading this TU, and
7056 the TU has an entry in .gdb_index, replace the recorded data from
7057 .gdb_index with this TU. */
7058
7059 find_sig_entry.signature = sig;
7060 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7061 &find_sig_entry, INSERT);
7062 sig_entry = (struct signatured_type *) *slot;
7063
7064 /* We can get here with the TU already read, *or* in the process of being
7065 read. Don't reassign the global entry to point to this DWO if that's
7066 the case. Also note that if the TU is already being read, it may not
7067 have come from a DWO, the program may be a mix of Fission-compiled
7068 code and non-Fission-compiled code. */
7069
7070 /* Have we already tried to read this TU?
7071 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7072 needn't exist in the global table yet). */
7073 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7074 return sig_entry;
7075
7076 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7077 dwo_unit of the TU itself. */
7078 dwo_file = cu->dwo_unit->dwo_file;
7079
7080 /* Ok, this is the first time we're reading this TU. */
7081 if (dwo_file->tus == NULL)
7082 return NULL;
7083 find_dwo_entry.signature = sig;
7084 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7085 if (dwo_entry == NULL)
7086 return NULL;
7087
7088 /* If the global table doesn't have an entry for this TU, add one. */
7089 if (sig_entry == NULL)
7090 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7091
7092 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7093 sig_entry->per_cu.tu_read = 1;
7094 return sig_entry;
7095 }
7096
7097 /* Subroutine of lookup_signatured_type.
7098 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7099 then try the DWP file. If the TU stub (skeleton) has been removed then
7100 it won't be in .gdb_index. */
7101
7102 static struct signatured_type *
7103 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7104 {
7105 struct dwarf2_per_objfile *dwarf2_per_objfile
7106 = cu->per_cu->dwarf2_per_objfile;
7107 struct objfile *objfile = dwarf2_per_objfile->objfile;
7108 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7109 struct dwo_unit *dwo_entry;
7110 struct signatured_type find_sig_entry, *sig_entry;
7111 void **slot;
7112
7113 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7114 gdb_assert (dwp_file != NULL);
7115
7116 /* If TU skeletons have been removed then we may not have read in any
7117 TUs yet. */
7118 if (dwarf2_per_objfile->signatured_types == NULL)
7119 {
7120 dwarf2_per_objfile->signatured_types
7121 = allocate_signatured_type_table (objfile);
7122 }
7123
7124 find_sig_entry.signature = sig;
7125 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7126 &find_sig_entry, INSERT);
7127 sig_entry = (struct signatured_type *) *slot;
7128
7129 /* Have we already tried to read this TU?
7130 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7131 needn't exist in the global table yet). */
7132 if (sig_entry != NULL)
7133 return sig_entry;
7134
7135 if (dwp_file->tus == NULL)
7136 return NULL;
7137 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7138 sig, 1 /* is_debug_types */);
7139 if (dwo_entry == NULL)
7140 return NULL;
7141
7142 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7143 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7144
7145 return sig_entry;
7146 }
7147
7148 /* Lookup a signature based type for DW_FORM_ref_sig8.
7149 Returns NULL if signature SIG is not present in the table.
7150 It is up to the caller to complain about this. */
7151
7152 static struct signatured_type *
7153 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7154 {
7155 struct dwarf2_per_objfile *dwarf2_per_objfile
7156 = cu->per_cu->dwarf2_per_objfile;
7157
7158 if (cu->dwo_unit
7159 && dwarf2_per_objfile->using_index)
7160 {
7161 /* We're in a DWO/DWP file, and we're using .gdb_index.
7162 These cases require special processing. */
7163 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7164 return lookup_dwo_signatured_type (cu, sig);
7165 else
7166 return lookup_dwp_signatured_type (cu, sig);
7167 }
7168 else
7169 {
7170 struct signatured_type find_entry, *entry;
7171
7172 if (dwarf2_per_objfile->signatured_types == NULL)
7173 return NULL;
7174 find_entry.signature = sig;
7175 entry = ((struct signatured_type *)
7176 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7177 return entry;
7178 }
7179 }
7180 \f
7181 /* Low level DIE reading support. */
7182
7183 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7184
7185 static void
7186 init_cu_die_reader (struct die_reader_specs *reader,
7187 struct dwarf2_cu *cu,
7188 struct dwarf2_section_info *section,
7189 struct dwo_file *dwo_file,
7190 struct abbrev_table *abbrev_table)
7191 {
7192 gdb_assert (section->readin && section->buffer != NULL);
7193 reader->abfd = get_section_bfd_owner (section);
7194 reader->cu = cu;
7195 reader->dwo_file = dwo_file;
7196 reader->die_section = section;
7197 reader->buffer = section->buffer;
7198 reader->buffer_end = section->buffer + section->size;
7199 reader->comp_dir = NULL;
7200 reader->abbrev_table = abbrev_table;
7201 }
7202
7203 /* Subroutine of init_cutu_and_read_dies to simplify it.
7204 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7205 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7206 already.
7207
7208 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7209 from it to the DIE in the DWO. If NULL we are skipping the stub.
7210 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7211 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7212 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7213 STUB_COMP_DIR may be non-NULL.
7214 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7215 are filled in with the info of the DIE from the DWO file.
7216 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7217 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7218 kept around for at least as long as *RESULT_READER.
7219
7220 The result is non-zero if a valid (non-dummy) DIE was found. */
7221
7222 static int
7223 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7224 struct dwo_unit *dwo_unit,
7225 struct die_info *stub_comp_unit_die,
7226 const char *stub_comp_dir,
7227 struct die_reader_specs *result_reader,
7228 const gdb_byte **result_info_ptr,
7229 struct die_info **result_comp_unit_die,
7230 int *result_has_children,
7231 abbrev_table_up *result_dwo_abbrev_table)
7232 {
7233 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7234 struct objfile *objfile = dwarf2_per_objfile->objfile;
7235 struct dwarf2_cu *cu = this_cu->cu;
7236 bfd *abfd;
7237 const gdb_byte *begin_info_ptr, *info_ptr;
7238 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7239 int i,num_extra_attrs;
7240 struct dwarf2_section_info *dwo_abbrev_section;
7241 struct attribute *attr;
7242 struct die_info *comp_unit_die;
7243
7244 /* At most one of these may be provided. */
7245 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7246
7247 /* These attributes aren't processed until later:
7248 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7249 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7250 referenced later. However, these attributes are found in the stub
7251 which we won't have later. In order to not impose this complication
7252 on the rest of the code, we read them here and copy them to the
7253 DWO CU/TU die. */
7254
7255 stmt_list = NULL;
7256 low_pc = NULL;
7257 high_pc = NULL;
7258 ranges = NULL;
7259 comp_dir = NULL;
7260
7261 if (stub_comp_unit_die != NULL)
7262 {
7263 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7264 DWO file. */
7265 if (! this_cu->is_debug_types)
7266 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7267 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7268 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7269 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7270 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7271
7272 /* There should be a DW_AT_addr_base attribute here (if needed).
7273 We need the value before we can process DW_FORM_GNU_addr_index
7274 or DW_FORM_addrx. */
7275 cu->addr_base = 0;
7276 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7277 if (attr != nullptr)
7278 cu->addr_base = DW_UNSND (attr);
7279
7280 /* There should be a DW_AT_ranges_base attribute here (if needed).
7281 We need the value before we can process DW_AT_ranges. */
7282 cu->ranges_base = 0;
7283 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7284 if (attr != nullptr)
7285 cu->ranges_base = DW_UNSND (attr);
7286 }
7287 else if (stub_comp_dir != NULL)
7288 {
7289 /* Reconstruct the comp_dir attribute to simplify the code below. */
7290 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7291 comp_dir->name = DW_AT_comp_dir;
7292 comp_dir->form = DW_FORM_string;
7293 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7294 DW_STRING (comp_dir) = stub_comp_dir;
7295 }
7296
7297 /* Set up for reading the DWO CU/TU. */
7298 cu->dwo_unit = dwo_unit;
7299 dwarf2_section_info *section = dwo_unit->section;
7300 dwarf2_read_section (objfile, section);
7301 abfd = get_section_bfd_owner (section);
7302 begin_info_ptr = info_ptr = (section->buffer
7303 + to_underlying (dwo_unit->sect_off));
7304 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7305
7306 if (this_cu->is_debug_types)
7307 {
7308 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7309
7310 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7311 &cu->header, section,
7312 dwo_abbrev_section,
7313 info_ptr, rcuh_kind::TYPE);
7314 /* This is not an assert because it can be caused by bad debug info. */
7315 if (sig_type->signature != cu->header.signature)
7316 {
7317 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7318 " TU at offset %s [in module %s]"),
7319 hex_string (sig_type->signature),
7320 hex_string (cu->header.signature),
7321 sect_offset_str (dwo_unit->sect_off),
7322 bfd_get_filename (abfd));
7323 }
7324 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7325 /* For DWOs coming from DWP files, we don't know the CU length
7326 nor the type's offset in the TU until now. */
7327 dwo_unit->length = get_cu_length (&cu->header);
7328 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7329
7330 /* Establish the type offset that can be used to lookup the type.
7331 For DWO files, we don't know it until now. */
7332 sig_type->type_offset_in_section
7333 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7334 }
7335 else
7336 {
7337 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7338 &cu->header, section,
7339 dwo_abbrev_section,
7340 info_ptr, rcuh_kind::COMPILE);
7341 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7342 /* For DWOs coming from DWP files, we don't know the CU length
7343 until now. */
7344 dwo_unit->length = get_cu_length (&cu->header);
7345 }
7346
7347 *result_dwo_abbrev_table
7348 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7349 cu->header.abbrev_sect_off);
7350 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7351 result_dwo_abbrev_table->get ());
7352
7353 /* Read in the die, but leave space to copy over the attributes
7354 from the stub. This has the benefit of simplifying the rest of
7355 the code - all the work to maintain the illusion of a single
7356 DW_TAG_{compile,type}_unit DIE is done here. */
7357 num_extra_attrs = ((stmt_list != NULL)
7358 + (low_pc != NULL)
7359 + (high_pc != NULL)
7360 + (ranges != NULL)
7361 + (comp_dir != NULL));
7362 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7363 result_has_children, num_extra_attrs);
7364
7365 /* Copy over the attributes from the stub to the DIE we just read in. */
7366 comp_unit_die = *result_comp_unit_die;
7367 i = comp_unit_die->num_attrs;
7368 if (stmt_list != NULL)
7369 comp_unit_die->attrs[i++] = *stmt_list;
7370 if (low_pc != NULL)
7371 comp_unit_die->attrs[i++] = *low_pc;
7372 if (high_pc != NULL)
7373 comp_unit_die->attrs[i++] = *high_pc;
7374 if (ranges != NULL)
7375 comp_unit_die->attrs[i++] = *ranges;
7376 if (comp_dir != NULL)
7377 comp_unit_die->attrs[i++] = *comp_dir;
7378 comp_unit_die->num_attrs += num_extra_attrs;
7379
7380 if (dwarf_die_debug)
7381 {
7382 fprintf_unfiltered (gdb_stdlog,
7383 "Read die from %s@0x%x of %s:\n",
7384 get_section_name (section),
7385 (unsigned) (begin_info_ptr - section->buffer),
7386 bfd_get_filename (abfd));
7387 dump_die (comp_unit_die, dwarf_die_debug);
7388 }
7389
7390 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7391 TUs by skipping the stub and going directly to the entry in the DWO file.
7392 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7393 to get it via circuitous means. Blech. */
7394 if (comp_dir != NULL)
7395 result_reader->comp_dir = DW_STRING (comp_dir);
7396
7397 /* Skip dummy compilation units. */
7398 if (info_ptr >= begin_info_ptr + dwo_unit->length
7399 || peek_abbrev_code (abfd, info_ptr) == 0)
7400 return 0;
7401
7402 *result_info_ptr = info_ptr;
7403 return 1;
7404 }
7405
7406 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7407 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7408 signature is part of the header. */
7409 static gdb::optional<ULONGEST>
7410 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7411 {
7412 if (cu->header.version >= 5)
7413 return cu->header.signature;
7414 struct attribute *attr;
7415 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7416 if (attr == nullptr)
7417 return gdb::optional<ULONGEST> ();
7418 return DW_UNSND (attr);
7419 }
7420
7421 /* Subroutine of init_cutu_and_read_dies to simplify it.
7422 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7423 Returns NULL if the specified DWO unit cannot be found. */
7424
7425 static struct dwo_unit *
7426 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7427 struct die_info *comp_unit_die)
7428 {
7429 struct dwarf2_cu *cu = this_cu->cu;
7430 struct dwo_unit *dwo_unit;
7431 const char *comp_dir, *dwo_name;
7432
7433 gdb_assert (cu != NULL);
7434
7435 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7436 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7437 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7438
7439 if (this_cu->is_debug_types)
7440 {
7441 struct signatured_type *sig_type;
7442
7443 /* Since this_cu is the first member of struct signatured_type,
7444 we can go from a pointer to one to a pointer to the other. */
7445 sig_type = (struct signatured_type *) this_cu;
7446 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7447 }
7448 else
7449 {
7450 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7451 if (!signature.has_value ())
7452 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7453 " [in module %s]"),
7454 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7455 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7456 *signature);
7457 }
7458
7459 return dwo_unit;
7460 }
7461
7462 /* Subroutine of init_cutu_and_read_dies to simplify it.
7463 See it for a description of the parameters.
7464 Read a TU directly from a DWO file, bypassing the stub. */
7465
7466 static void
7467 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7468 int use_existing_cu, int keep,
7469 die_reader_func_ftype *die_reader_func,
7470 void *data)
7471 {
7472 std::unique_ptr<dwarf2_cu> new_cu;
7473 struct signatured_type *sig_type;
7474 struct die_reader_specs reader;
7475 const gdb_byte *info_ptr;
7476 struct die_info *comp_unit_die;
7477 int has_children;
7478 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7479
7480 /* Verify we can do the following downcast, and that we have the
7481 data we need. */
7482 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7483 sig_type = (struct signatured_type *) this_cu;
7484 gdb_assert (sig_type->dwo_unit != NULL);
7485
7486 if (use_existing_cu && this_cu->cu != NULL)
7487 {
7488 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7489 /* There's no need to do the rereading_dwo_cu handling that
7490 init_cutu_and_read_dies does since we don't read the stub. */
7491 }
7492 else
7493 {
7494 /* If !use_existing_cu, this_cu->cu must be NULL. */
7495 gdb_assert (this_cu->cu == NULL);
7496 new_cu.reset (new dwarf2_cu (this_cu));
7497 }
7498
7499 /* A future optimization, if needed, would be to use an existing
7500 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7501 could share abbrev tables. */
7502
7503 /* The abbreviation table used by READER, this must live at least as long as
7504 READER. */
7505 abbrev_table_up dwo_abbrev_table;
7506
7507 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7508 NULL /* stub_comp_unit_die */,
7509 sig_type->dwo_unit->dwo_file->comp_dir,
7510 &reader, &info_ptr,
7511 &comp_unit_die, &has_children,
7512 &dwo_abbrev_table) == 0)
7513 {
7514 /* Dummy die. */
7515 return;
7516 }
7517
7518 /* All the "real" work is done here. */
7519 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7520
7521 /* This duplicates the code in init_cutu_and_read_dies,
7522 but the alternative is making the latter more complex.
7523 This function is only for the special case of using DWO files directly:
7524 no point in overly complicating the general case just to handle this. */
7525 if (new_cu != NULL && keep)
7526 {
7527 /* Link this CU into read_in_chain. */
7528 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7529 dwarf2_per_objfile->read_in_chain = this_cu;
7530 /* The chain owns it now. */
7531 new_cu.release ();
7532 }
7533 }
7534
7535 /* Initialize a CU (or TU) and read its DIEs.
7536 If the CU defers to a DWO file, read the DWO file as well.
7537
7538 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7539 Otherwise the table specified in the comp unit header is read in and used.
7540 This is an optimization for when we already have the abbrev table.
7541
7542 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7543 Otherwise, a new CU is allocated with xmalloc.
7544
7545 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7546 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7547
7548 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7549 linker) then DIE_READER_FUNC will not get called. */
7550
7551 static void
7552 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7553 struct abbrev_table *abbrev_table,
7554 int use_existing_cu, int keep,
7555 bool skip_partial,
7556 die_reader_func_ftype *die_reader_func,
7557 void *data)
7558 {
7559 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7560 struct objfile *objfile = dwarf2_per_objfile->objfile;
7561 struct dwarf2_section_info *section = this_cu->section;
7562 bfd *abfd = get_section_bfd_owner (section);
7563 struct dwarf2_cu *cu;
7564 const gdb_byte *begin_info_ptr, *info_ptr;
7565 struct die_reader_specs reader;
7566 struct die_info *comp_unit_die;
7567 int has_children;
7568 struct signatured_type *sig_type = NULL;
7569 struct dwarf2_section_info *abbrev_section;
7570 /* Non-zero if CU currently points to a DWO file and we need to
7571 reread it. When this happens we need to reread the skeleton die
7572 before we can reread the DWO file (this only applies to CUs, not TUs). */
7573 int rereading_dwo_cu = 0;
7574
7575 if (dwarf_die_debug)
7576 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7577 this_cu->is_debug_types ? "type" : "comp",
7578 sect_offset_str (this_cu->sect_off));
7579
7580 if (use_existing_cu)
7581 gdb_assert (keep);
7582
7583 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7584 file (instead of going through the stub), short-circuit all of this. */
7585 if (this_cu->reading_dwo_directly)
7586 {
7587 /* Narrow down the scope of possibilities to have to understand. */
7588 gdb_assert (this_cu->is_debug_types);
7589 gdb_assert (abbrev_table == NULL);
7590 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7591 die_reader_func, data);
7592 return;
7593 }
7594
7595 /* This is cheap if the section is already read in. */
7596 dwarf2_read_section (objfile, section);
7597
7598 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7599
7600 abbrev_section = get_abbrev_section_for_cu (this_cu);
7601
7602 std::unique_ptr<dwarf2_cu> new_cu;
7603 if (use_existing_cu && this_cu->cu != NULL)
7604 {
7605 cu = this_cu->cu;
7606 /* If this CU is from a DWO file we need to start over, we need to
7607 refetch the attributes from the skeleton CU.
7608 This could be optimized by retrieving those attributes from when we
7609 were here the first time: the previous comp_unit_die was stored in
7610 comp_unit_obstack. But there's no data yet that we need this
7611 optimization. */
7612 if (cu->dwo_unit != NULL)
7613 rereading_dwo_cu = 1;
7614 }
7615 else
7616 {
7617 /* If !use_existing_cu, this_cu->cu must be NULL. */
7618 gdb_assert (this_cu->cu == NULL);
7619 new_cu.reset (new dwarf2_cu (this_cu));
7620 cu = new_cu.get ();
7621 }
7622
7623 /* Get the header. */
7624 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7625 {
7626 /* We already have the header, there's no need to read it in again. */
7627 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7628 }
7629 else
7630 {
7631 if (this_cu->is_debug_types)
7632 {
7633 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7634 &cu->header, section,
7635 abbrev_section, info_ptr,
7636 rcuh_kind::TYPE);
7637
7638 /* Since per_cu is the first member of struct signatured_type,
7639 we can go from a pointer to one to a pointer to the other. */
7640 sig_type = (struct signatured_type *) this_cu;
7641 gdb_assert (sig_type->signature == cu->header.signature);
7642 gdb_assert (sig_type->type_offset_in_tu
7643 == cu->header.type_cu_offset_in_tu);
7644 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7645
7646 /* LENGTH has not been set yet for type units if we're
7647 using .gdb_index. */
7648 this_cu->length = get_cu_length (&cu->header);
7649
7650 /* Establish the type offset that can be used to lookup the type. */
7651 sig_type->type_offset_in_section =
7652 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7653
7654 this_cu->dwarf_version = cu->header.version;
7655 }
7656 else
7657 {
7658 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7659 &cu->header, section,
7660 abbrev_section,
7661 info_ptr,
7662 rcuh_kind::COMPILE);
7663
7664 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7665 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7666 this_cu->dwarf_version = cu->header.version;
7667 }
7668 }
7669
7670 /* Skip dummy compilation units. */
7671 if (info_ptr >= begin_info_ptr + this_cu->length
7672 || peek_abbrev_code (abfd, info_ptr) == 0)
7673 return;
7674
7675 /* If we don't have them yet, read the abbrevs for this compilation unit.
7676 And if we need to read them now, make sure they're freed when we're
7677 done (own the table through ABBREV_TABLE_HOLDER). */
7678 abbrev_table_up abbrev_table_holder;
7679 if (abbrev_table != NULL)
7680 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7681 else
7682 {
7683 abbrev_table_holder
7684 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7685 cu->header.abbrev_sect_off);
7686 abbrev_table = abbrev_table_holder.get ();
7687 }
7688
7689 /* Read the top level CU/TU die. */
7690 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7691 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7692
7693 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7694 return;
7695
7696 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7697 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7698 table from the DWO file and pass the ownership over to us. It will be
7699 referenced from READER, so we must make sure to free it after we're done
7700 with READER.
7701
7702 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7703 DWO CU, that this test will fail (the attribute will not be present). */
7704 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7705 abbrev_table_up dwo_abbrev_table;
7706 if (dwo_name != nullptr)
7707 {
7708 struct dwo_unit *dwo_unit;
7709 struct die_info *dwo_comp_unit_die;
7710
7711 if (has_children)
7712 {
7713 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7714 " has children (offset %s) [in module %s]"),
7715 sect_offset_str (this_cu->sect_off),
7716 bfd_get_filename (abfd));
7717 }
7718 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7719 if (dwo_unit != NULL)
7720 {
7721 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7722 comp_unit_die, NULL,
7723 &reader, &info_ptr,
7724 &dwo_comp_unit_die, &has_children,
7725 &dwo_abbrev_table) == 0)
7726 {
7727 /* Dummy die. */
7728 return;
7729 }
7730 comp_unit_die = dwo_comp_unit_die;
7731 }
7732 else
7733 {
7734 /* Yikes, we couldn't find the rest of the DIE, we only have
7735 the stub. A complaint has already been logged. There's
7736 not much more we can do except pass on the stub DIE to
7737 die_reader_func. We don't want to throw an error on bad
7738 debug info. */
7739 }
7740 }
7741
7742 /* All of the above is setup for this call. Yikes. */
7743 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7744
7745 /* Done, clean up. */
7746 if (new_cu != NULL && keep)
7747 {
7748 /* Link this CU into read_in_chain. */
7749 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7750 dwarf2_per_objfile->read_in_chain = this_cu;
7751 /* The chain owns it now. */
7752 new_cu.release ();
7753 }
7754 }
7755
7756 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7757 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7758 to have already done the lookup to find the DWO file).
7759
7760 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7761 THIS_CU->is_debug_types, but nothing else.
7762
7763 We fill in THIS_CU->length.
7764
7765 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7766 linker) then DIE_READER_FUNC will not get called.
7767
7768 THIS_CU->cu is always freed when done.
7769 This is done in order to not leave THIS_CU->cu in a state where we have
7770 to care whether it refers to the "main" CU or the DWO CU. */
7771
7772 static void
7773 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7774 struct dwo_file *dwo_file,
7775 die_reader_func_ftype *die_reader_func,
7776 void *data)
7777 {
7778 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7779 struct objfile *objfile = dwarf2_per_objfile->objfile;
7780 struct dwarf2_section_info *section = this_cu->section;
7781 bfd *abfd = get_section_bfd_owner (section);
7782 struct dwarf2_section_info *abbrev_section;
7783 const gdb_byte *begin_info_ptr, *info_ptr;
7784 struct die_reader_specs reader;
7785 struct die_info *comp_unit_die;
7786 int has_children;
7787
7788 if (dwarf_die_debug)
7789 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7790 this_cu->is_debug_types ? "type" : "comp",
7791 sect_offset_str (this_cu->sect_off));
7792
7793 gdb_assert (this_cu->cu == NULL);
7794
7795 abbrev_section = (dwo_file != NULL
7796 ? &dwo_file->sections.abbrev
7797 : get_abbrev_section_for_cu (this_cu));
7798
7799 /* This is cheap if the section is already read in. */
7800 dwarf2_read_section (objfile, section);
7801
7802 struct dwarf2_cu cu (this_cu);
7803
7804 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7805 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7806 &cu.header, section,
7807 abbrev_section, info_ptr,
7808 (this_cu->is_debug_types
7809 ? rcuh_kind::TYPE
7810 : rcuh_kind::COMPILE));
7811
7812 this_cu->length = get_cu_length (&cu.header);
7813
7814 /* Skip dummy compilation units. */
7815 if (info_ptr >= begin_info_ptr + this_cu->length
7816 || peek_abbrev_code (abfd, info_ptr) == 0)
7817 return;
7818
7819 abbrev_table_up abbrev_table
7820 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7821 cu.header.abbrev_sect_off);
7822
7823 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
7824 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7825
7826 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7827 }
7828
7829 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7830 does not lookup the specified DWO file.
7831 This cannot be used to read DWO files.
7832
7833 THIS_CU->cu is always freed when done.
7834 This is done in order to not leave THIS_CU->cu in a state where we have
7835 to care whether it refers to the "main" CU or the DWO CU.
7836 We can revisit this if the data shows there's a performance issue. */
7837
7838 static void
7839 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7840 die_reader_func_ftype *die_reader_func,
7841 void *data)
7842 {
7843 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
7844 }
7845 \f
7846 /* Type Unit Groups.
7847
7848 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7849 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7850 so that all types coming from the same compilation (.o file) are grouped
7851 together. A future step could be to put the types in the same symtab as
7852 the CU the types ultimately came from. */
7853
7854 static hashval_t
7855 hash_type_unit_group (const void *item)
7856 {
7857 const struct type_unit_group *tu_group
7858 = (const struct type_unit_group *) item;
7859
7860 return hash_stmt_list_entry (&tu_group->hash);
7861 }
7862
7863 static int
7864 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7865 {
7866 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7867 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7868
7869 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7870 }
7871
7872 /* Allocate a hash table for type unit groups. */
7873
7874 static htab_t
7875 allocate_type_unit_groups_table (struct objfile *objfile)
7876 {
7877 return htab_create_alloc_ex (3,
7878 hash_type_unit_group,
7879 eq_type_unit_group,
7880 NULL,
7881 &objfile->objfile_obstack,
7882 hashtab_obstack_allocate,
7883 dummy_obstack_deallocate);
7884 }
7885
7886 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7887 partial symtabs. We combine several TUs per psymtab to not let the size
7888 of any one psymtab grow too big. */
7889 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7890 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7891
7892 /* Helper routine for get_type_unit_group.
7893 Create the type_unit_group object used to hold one or more TUs. */
7894
7895 static struct type_unit_group *
7896 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7897 {
7898 struct dwarf2_per_objfile *dwarf2_per_objfile
7899 = cu->per_cu->dwarf2_per_objfile;
7900 struct objfile *objfile = dwarf2_per_objfile->objfile;
7901 struct dwarf2_per_cu_data *per_cu;
7902 struct type_unit_group *tu_group;
7903
7904 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7905 struct type_unit_group);
7906 per_cu = &tu_group->per_cu;
7907 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7908
7909 if (dwarf2_per_objfile->using_index)
7910 {
7911 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7912 struct dwarf2_per_cu_quick_data);
7913 }
7914 else
7915 {
7916 unsigned int line_offset = to_underlying (line_offset_struct);
7917 struct partial_symtab *pst;
7918 std::string name;
7919
7920 /* Give the symtab a useful name for debug purposes. */
7921 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7922 name = string_printf ("<type_units_%d>",
7923 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7924 else
7925 name = string_printf ("<type_units_at_0x%x>", line_offset);
7926
7927 pst = create_partial_symtab (per_cu, name.c_str ());
7928 pst->anonymous = 1;
7929 }
7930
7931 tu_group->hash.dwo_unit = cu->dwo_unit;
7932 tu_group->hash.line_sect_off = line_offset_struct;
7933
7934 return tu_group;
7935 }
7936
7937 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7938 STMT_LIST is a DW_AT_stmt_list attribute. */
7939
7940 static struct type_unit_group *
7941 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7942 {
7943 struct dwarf2_per_objfile *dwarf2_per_objfile
7944 = cu->per_cu->dwarf2_per_objfile;
7945 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7946 struct type_unit_group *tu_group;
7947 void **slot;
7948 unsigned int line_offset;
7949 struct type_unit_group type_unit_group_for_lookup;
7950
7951 if (dwarf2_per_objfile->type_unit_groups == NULL)
7952 {
7953 dwarf2_per_objfile->type_unit_groups =
7954 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7955 }
7956
7957 /* Do we need to create a new group, or can we use an existing one? */
7958
7959 if (stmt_list)
7960 {
7961 line_offset = DW_UNSND (stmt_list);
7962 ++tu_stats->nr_symtab_sharers;
7963 }
7964 else
7965 {
7966 /* Ugh, no stmt_list. Rare, but we have to handle it.
7967 We can do various things here like create one group per TU or
7968 spread them over multiple groups to split up the expansion work.
7969 To avoid worst case scenarios (too many groups or too large groups)
7970 we, umm, group them in bunches. */
7971 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7972 | (tu_stats->nr_stmt_less_type_units
7973 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7974 ++tu_stats->nr_stmt_less_type_units;
7975 }
7976
7977 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7978 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7979 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7980 &type_unit_group_for_lookup, INSERT);
7981 if (*slot != NULL)
7982 {
7983 tu_group = (struct type_unit_group *) *slot;
7984 gdb_assert (tu_group != NULL);
7985 }
7986 else
7987 {
7988 sect_offset line_offset_struct = (sect_offset) line_offset;
7989 tu_group = create_type_unit_group (cu, line_offset_struct);
7990 *slot = tu_group;
7991 ++tu_stats->nr_symtabs;
7992 }
7993
7994 return tu_group;
7995 }
7996 \f
7997 /* Partial symbol tables. */
7998
7999 /* Create a psymtab named NAME and assign it to PER_CU.
8000
8001 The caller must fill in the following details:
8002 dirname, textlow, texthigh. */
8003
8004 static struct partial_symtab *
8005 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8006 {
8007 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
8008 struct partial_symtab *pst;
8009
8010 pst = start_psymtab_common (objfile, name, 0);
8011
8012 pst->psymtabs_addrmap_supported = 1;
8013
8014 /* This is the glue that links PST into GDB's symbol API. */
8015 pst->read_symtab_private = per_cu;
8016 pst->read_symtab = dwarf2_read_symtab;
8017 per_cu->v.psymtab = pst;
8018
8019 return pst;
8020 }
8021
8022 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8023 type. */
8024
8025 struct process_psymtab_comp_unit_data
8026 {
8027 /* True if we are reading a DW_TAG_partial_unit. */
8028
8029 int want_partial_unit;
8030
8031 /* The "pretend" language that is used if the CU doesn't declare a
8032 language. */
8033
8034 enum language pretend_language;
8035 };
8036
8037 /* die_reader_func for process_psymtab_comp_unit. */
8038
8039 static void
8040 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8041 const gdb_byte *info_ptr,
8042 struct die_info *comp_unit_die,
8043 int has_children,
8044 void *data)
8045 {
8046 struct dwarf2_cu *cu = reader->cu;
8047 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8048 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8049 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8050 CORE_ADDR baseaddr;
8051 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8052 struct partial_symtab *pst;
8053 enum pc_bounds_kind cu_bounds_kind;
8054 const char *filename;
8055 struct process_psymtab_comp_unit_data *info
8056 = (struct process_psymtab_comp_unit_data *) data;
8057
8058 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8059 return;
8060
8061 gdb_assert (! per_cu->is_debug_types);
8062
8063 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8064
8065 /* Allocate a new partial symbol table structure. */
8066 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8067 if (filename == NULL)
8068 filename = "";
8069
8070 pst = create_partial_symtab (per_cu, filename);
8071
8072 /* This must be done before calling dwarf2_build_include_psymtabs. */
8073 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8074
8075 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8076
8077 dwarf2_find_base_address (comp_unit_die, cu);
8078
8079 /* Possibly set the default values of LOWPC and HIGHPC from
8080 `DW_AT_ranges'. */
8081 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8082 &best_highpc, cu, pst);
8083 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8084 {
8085 CORE_ADDR low
8086 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8087 - baseaddr);
8088 CORE_ADDR high
8089 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8090 - baseaddr - 1);
8091 /* Store the contiguous range if it is not empty; it can be
8092 empty for CUs with no code. */
8093 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8094 low, high, pst);
8095 }
8096
8097 /* Check if comp unit has_children.
8098 If so, read the rest of the partial symbols from this comp unit.
8099 If not, there's no more debug_info for this comp unit. */
8100 if (has_children)
8101 {
8102 struct partial_die_info *first_die;
8103 CORE_ADDR lowpc, highpc;
8104
8105 lowpc = ((CORE_ADDR) -1);
8106 highpc = ((CORE_ADDR) 0);
8107
8108 first_die = load_partial_dies (reader, info_ptr, 1);
8109
8110 scan_partial_symbols (first_die, &lowpc, &highpc,
8111 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8112
8113 /* If we didn't find a lowpc, set it to highpc to avoid
8114 complaints from `maint check'. */
8115 if (lowpc == ((CORE_ADDR) -1))
8116 lowpc = highpc;
8117
8118 /* If the compilation unit didn't have an explicit address range,
8119 then use the information extracted from its child dies. */
8120 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8121 {
8122 best_lowpc = lowpc;
8123 best_highpc = highpc;
8124 }
8125 }
8126 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
8127 best_lowpc + baseaddr)
8128 - baseaddr);
8129 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
8130 best_highpc + baseaddr)
8131 - baseaddr);
8132
8133 end_psymtab_common (objfile, pst);
8134
8135 if (!cu->per_cu->imported_symtabs_empty ())
8136 {
8137 int i;
8138 int len = cu->per_cu->imported_symtabs_size ();
8139
8140 /* Fill in 'dependencies' here; we fill in 'users' in a
8141 post-pass. */
8142 pst->number_of_dependencies = len;
8143 pst->dependencies
8144 = objfile->partial_symtabs->allocate_dependencies (len);
8145 for (i = 0; i < len; ++i)
8146 {
8147 pst->dependencies[i]
8148 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8149 }
8150
8151 cu->per_cu->imported_symtabs_free ();
8152 }
8153
8154 /* Get the list of files included in the current compilation unit,
8155 and build a psymtab for each of them. */
8156 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8157
8158 if (dwarf_read_debug)
8159 fprintf_unfiltered (gdb_stdlog,
8160 "Psymtab for %s unit @%s: %s - %s"
8161 ", %d global, %d static syms\n",
8162 per_cu->is_debug_types ? "type" : "comp",
8163 sect_offset_str (per_cu->sect_off),
8164 paddress (gdbarch, pst->text_low (objfile)),
8165 paddress (gdbarch, pst->text_high (objfile)),
8166 pst->n_global_syms, pst->n_static_syms);
8167 }
8168
8169 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8170 Process compilation unit THIS_CU for a psymtab. */
8171
8172 static void
8173 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8174 int want_partial_unit,
8175 enum language pretend_language)
8176 {
8177 /* If this compilation unit was already read in, free the
8178 cached copy in order to read it in again. This is
8179 necessary because we skipped some symbols when we first
8180 read in the compilation unit (see load_partial_dies).
8181 This problem could be avoided, but the benefit is unclear. */
8182 if (this_cu->cu != NULL)
8183 free_one_cached_comp_unit (this_cu);
8184
8185 if (this_cu->is_debug_types)
8186 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8187 build_type_psymtabs_reader, NULL);
8188 else
8189 {
8190 process_psymtab_comp_unit_data info;
8191 info.want_partial_unit = want_partial_unit;
8192 info.pretend_language = pretend_language;
8193 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8194 process_psymtab_comp_unit_reader, &info);
8195 }
8196
8197 /* Age out any secondary CUs. */
8198 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8199 }
8200
8201 /* Reader function for build_type_psymtabs. */
8202
8203 static void
8204 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8205 const gdb_byte *info_ptr,
8206 struct die_info *type_unit_die,
8207 int has_children,
8208 void *data)
8209 {
8210 struct dwarf2_per_objfile *dwarf2_per_objfile
8211 = reader->cu->per_cu->dwarf2_per_objfile;
8212 struct objfile *objfile = dwarf2_per_objfile->objfile;
8213 struct dwarf2_cu *cu = reader->cu;
8214 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8215 struct signatured_type *sig_type;
8216 struct type_unit_group *tu_group;
8217 struct attribute *attr;
8218 struct partial_die_info *first_die;
8219 CORE_ADDR lowpc, highpc;
8220 struct partial_symtab *pst;
8221
8222 gdb_assert (data == NULL);
8223 gdb_assert (per_cu->is_debug_types);
8224 sig_type = (struct signatured_type *) per_cu;
8225
8226 if (! has_children)
8227 return;
8228
8229 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8230 tu_group = get_type_unit_group (cu, attr);
8231
8232 if (tu_group->tus == nullptr)
8233 tu_group->tus = new std::vector<signatured_type *>;
8234 tu_group->tus->push_back (sig_type);
8235
8236 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8237 pst = create_partial_symtab (per_cu, "");
8238 pst->anonymous = 1;
8239
8240 first_die = load_partial_dies (reader, info_ptr, 1);
8241
8242 lowpc = (CORE_ADDR) -1;
8243 highpc = (CORE_ADDR) 0;
8244 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8245
8246 end_psymtab_common (objfile, pst);
8247 }
8248
8249 /* Struct used to sort TUs by their abbreviation table offset. */
8250
8251 struct tu_abbrev_offset
8252 {
8253 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8254 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8255 {}
8256
8257 signatured_type *sig_type;
8258 sect_offset abbrev_offset;
8259 };
8260
8261 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8262
8263 static bool
8264 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8265 const struct tu_abbrev_offset &b)
8266 {
8267 return a.abbrev_offset < b.abbrev_offset;
8268 }
8269
8270 /* Efficiently read all the type units.
8271 This does the bulk of the work for build_type_psymtabs.
8272
8273 The efficiency is because we sort TUs by the abbrev table they use and
8274 only read each abbrev table once. In one program there are 200K TUs
8275 sharing 8K abbrev tables.
8276
8277 The main purpose of this function is to support building the
8278 dwarf2_per_objfile->type_unit_groups table.
8279 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8280 can collapse the search space by grouping them by stmt_list.
8281 The savings can be significant, in the same program from above the 200K TUs
8282 share 8K stmt_list tables.
8283
8284 FUNC is expected to call get_type_unit_group, which will create the
8285 struct type_unit_group if necessary and add it to
8286 dwarf2_per_objfile->type_unit_groups. */
8287
8288 static void
8289 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8290 {
8291 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8292 abbrev_table_up abbrev_table;
8293 sect_offset abbrev_offset;
8294
8295 /* It's up to the caller to not call us multiple times. */
8296 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8297
8298 if (dwarf2_per_objfile->all_type_units.empty ())
8299 return;
8300
8301 /* TUs typically share abbrev tables, and there can be way more TUs than
8302 abbrev tables. Sort by abbrev table to reduce the number of times we
8303 read each abbrev table in.
8304 Alternatives are to punt or to maintain a cache of abbrev tables.
8305 This is simpler and efficient enough for now.
8306
8307 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8308 symtab to use). Typically TUs with the same abbrev offset have the same
8309 stmt_list value too so in practice this should work well.
8310
8311 The basic algorithm here is:
8312
8313 sort TUs by abbrev table
8314 for each TU with same abbrev table:
8315 read abbrev table if first user
8316 read TU top level DIE
8317 [IWBN if DWO skeletons had DW_AT_stmt_list]
8318 call FUNC */
8319
8320 if (dwarf_read_debug)
8321 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8322
8323 /* Sort in a separate table to maintain the order of all_type_units
8324 for .gdb_index: TU indices directly index all_type_units. */
8325 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8326 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8327
8328 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8329 sorted_by_abbrev.emplace_back
8330 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8331 sig_type->per_cu.section,
8332 sig_type->per_cu.sect_off));
8333
8334 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8335 sort_tu_by_abbrev_offset);
8336
8337 abbrev_offset = (sect_offset) ~(unsigned) 0;
8338
8339 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8340 {
8341 /* Switch to the next abbrev table if necessary. */
8342 if (abbrev_table == NULL
8343 || tu.abbrev_offset != abbrev_offset)
8344 {
8345 abbrev_offset = tu.abbrev_offset;
8346 abbrev_table =
8347 abbrev_table_read_table (dwarf2_per_objfile,
8348 &dwarf2_per_objfile->abbrev,
8349 abbrev_offset);
8350 ++tu_stats->nr_uniq_abbrev_tables;
8351 }
8352
8353 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
8354 0, 0, false, build_type_psymtabs_reader, NULL);
8355 }
8356 }
8357
8358 /* Print collected type unit statistics. */
8359
8360 static void
8361 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8362 {
8363 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8364
8365 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8366 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8367 dwarf2_per_objfile->all_type_units.size ());
8368 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8369 tu_stats->nr_uniq_abbrev_tables);
8370 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8371 tu_stats->nr_symtabs);
8372 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8373 tu_stats->nr_symtab_sharers);
8374 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8375 tu_stats->nr_stmt_less_type_units);
8376 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8377 tu_stats->nr_all_type_units_reallocs);
8378 }
8379
8380 /* Traversal function for build_type_psymtabs. */
8381
8382 static int
8383 build_type_psymtab_dependencies (void **slot, void *info)
8384 {
8385 struct dwarf2_per_objfile *dwarf2_per_objfile
8386 = (struct dwarf2_per_objfile *) info;
8387 struct objfile *objfile = dwarf2_per_objfile->objfile;
8388 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8389 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8390 struct partial_symtab *pst = per_cu->v.psymtab;
8391 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8392 int i;
8393
8394 gdb_assert (len > 0);
8395 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8396
8397 pst->number_of_dependencies = len;
8398 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8399 for (i = 0; i < len; ++i)
8400 {
8401 struct signatured_type *iter = tu_group->tus->at (i);
8402 gdb_assert (iter->per_cu.is_debug_types);
8403 pst->dependencies[i] = iter->per_cu.v.psymtab;
8404 iter->type_unit_group = tu_group;
8405 }
8406
8407 delete tu_group->tus;
8408 tu_group->tus = nullptr;
8409
8410 return 1;
8411 }
8412
8413 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8414 Build partial symbol tables for the .debug_types comp-units. */
8415
8416 static void
8417 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8418 {
8419 if (! create_all_type_units (dwarf2_per_objfile))
8420 return;
8421
8422 build_type_psymtabs_1 (dwarf2_per_objfile);
8423 }
8424
8425 /* Traversal function for process_skeletonless_type_unit.
8426 Read a TU in a DWO file and build partial symbols for it. */
8427
8428 static int
8429 process_skeletonless_type_unit (void **slot, void *info)
8430 {
8431 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8432 struct dwarf2_per_objfile *dwarf2_per_objfile
8433 = (struct dwarf2_per_objfile *) info;
8434 struct signatured_type find_entry, *entry;
8435
8436 /* If this TU doesn't exist in the global table, add it and read it in. */
8437
8438 if (dwarf2_per_objfile->signatured_types == NULL)
8439 {
8440 dwarf2_per_objfile->signatured_types
8441 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8442 }
8443
8444 find_entry.signature = dwo_unit->signature;
8445 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8446 INSERT);
8447 /* If we've already seen this type there's nothing to do. What's happening
8448 is we're doing our own version of comdat-folding here. */
8449 if (*slot != NULL)
8450 return 1;
8451
8452 /* This does the job that create_all_type_units would have done for
8453 this TU. */
8454 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8455 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8456 *slot = entry;
8457
8458 /* This does the job that build_type_psymtabs_1 would have done. */
8459 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
8460 build_type_psymtabs_reader, NULL);
8461
8462 return 1;
8463 }
8464
8465 /* Traversal function for process_skeletonless_type_units. */
8466
8467 static int
8468 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8469 {
8470 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8471
8472 if (dwo_file->tus != NULL)
8473 {
8474 htab_traverse_noresize (dwo_file->tus,
8475 process_skeletonless_type_unit, info);
8476 }
8477
8478 return 1;
8479 }
8480
8481 /* Scan all TUs of DWO files, verifying we've processed them.
8482 This is needed in case a TU was emitted without its skeleton.
8483 Note: This can't be done until we know what all the DWO files are. */
8484
8485 static void
8486 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8487 {
8488 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8489 if (get_dwp_file (dwarf2_per_objfile) == NULL
8490 && dwarf2_per_objfile->dwo_files != NULL)
8491 {
8492 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
8493 process_dwo_file_for_skeletonless_type_units,
8494 dwarf2_per_objfile);
8495 }
8496 }
8497
8498 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8499
8500 static void
8501 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8502 {
8503 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8504 {
8505 struct partial_symtab *pst = per_cu->v.psymtab;
8506
8507 if (pst == NULL)
8508 continue;
8509
8510 for (int j = 0; j < pst->number_of_dependencies; ++j)
8511 {
8512 /* Set the 'user' field only if it is not already set. */
8513 if (pst->dependencies[j]->user == NULL)
8514 pst->dependencies[j]->user = pst;
8515 }
8516 }
8517 }
8518
8519 /* Build the partial symbol table by doing a quick pass through the
8520 .debug_info and .debug_abbrev sections. */
8521
8522 static void
8523 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8524 {
8525 struct objfile *objfile = dwarf2_per_objfile->objfile;
8526
8527 if (dwarf_read_debug)
8528 {
8529 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8530 objfile_name (objfile));
8531 }
8532
8533 dwarf2_per_objfile->reading_partial_symbols = 1;
8534
8535 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8536
8537 /* Any cached compilation units will be linked by the per-objfile
8538 read_in_chain. Make sure to free them when we're done. */
8539 free_cached_comp_units freer (dwarf2_per_objfile);
8540
8541 build_type_psymtabs (dwarf2_per_objfile);
8542
8543 create_all_comp_units (dwarf2_per_objfile);
8544
8545 /* Create a temporary address map on a temporary obstack. We later
8546 copy this to the final obstack. */
8547 auto_obstack temp_obstack;
8548
8549 scoped_restore save_psymtabs_addrmap
8550 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8551 addrmap_create_mutable (&temp_obstack));
8552
8553 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8554 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8555
8556 /* This has to wait until we read the CUs, we need the list of DWOs. */
8557 process_skeletonless_type_units (dwarf2_per_objfile);
8558
8559 /* Now that all TUs have been processed we can fill in the dependencies. */
8560 if (dwarf2_per_objfile->type_unit_groups != NULL)
8561 {
8562 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8563 build_type_psymtab_dependencies, dwarf2_per_objfile);
8564 }
8565
8566 if (dwarf_read_debug)
8567 print_tu_stats (dwarf2_per_objfile);
8568
8569 set_partial_user (dwarf2_per_objfile);
8570
8571 objfile->partial_symtabs->psymtabs_addrmap
8572 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8573 objfile->partial_symtabs->obstack ());
8574 /* At this point we want to keep the address map. */
8575 save_psymtabs_addrmap.release ();
8576
8577 if (dwarf_read_debug)
8578 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8579 objfile_name (objfile));
8580 }
8581
8582 /* die_reader_func for load_partial_comp_unit. */
8583
8584 static void
8585 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8586 const gdb_byte *info_ptr,
8587 struct die_info *comp_unit_die,
8588 int has_children,
8589 void *data)
8590 {
8591 struct dwarf2_cu *cu = reader->cu;
8592
8593 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8594
8595 /* Check if comp unit has_children.
8596 If so, read the rest of the partial symbols from this comp unit.
8597 If not, there's no more debug_info for this comp unit. */
8598 if (has_children)
8599 load_partial_dies (reader, info_ptr, 0);
8600 }
8601
8602 /* Load the partial DIEs for a secondary CU into memory.
8603 This is also used when rereading a primary CU with load_all_dies. */
8604
8605 static void
8606 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8607 {
8608 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
8609 load_partial_comp_unit_reader, NULL);
8610 }
8611
8612 static void
8613 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8614 struct dwarf2_section_info *section,
8615 struct dwarf2_section_info *abbrev_section,
8616 unsigned int is_dwz)
8617 {
8618 const gdb_byte *info_ptr;
8619 struct objfile *objfile = dwarf2_per_objfile->objfile;
8620
8621 if (dwarf_read_debug)
8622 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8623 get_section_name (section),
8624 get_section_file_name (section));
8625
8626 dwarf2_read_section (objfile, section);
8627
8628 info_ptr = section->buffer;
8629
8630 while (info_ptr < section->buffer + section->size)
8631 {
8632 struct dwarf2_per_cu_data *this_cu;
8633
8634 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8635
8636 comp_unit_head cu_header;
8637 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8638 abbrev_section, info_ptr,
8639 rcuh_kind::COMPILE);
8640
8641 /* Save the compilation unit for later lookup. */
8642 if (cu_header.unit_type != DW_UT_type)
8643 {
8644 this_cu = XOBNEW (&objfile->objfile_obstack,
8645 struct dwarf2_per_cu_data);
8646 memset (this_cu, 0, sizeof (*this_cu));
8647 }
8648 else
8649 {
8650 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8651 struct signatured_type);
8652 memset (sig_type, 0, sizeof (*sig_type));
8653 sig_type->signature = cu_header.signature;
8654 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8655 this_cu = &sig_type->per_cu;
8656 }
8657 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8658 this_cu->sect_off = sect_off;
8659 this_cu->length = cu_header.length + cu_header.initial_length_size;
8660 this_cu->is_dwz = is_dwz;
8661 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8662 this_cu->section = section;
8663
8664 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8665
8666 info_ptr = info_ptr + this_cu->length;
8667 }
8668 }
8669
8670 /* Create a list of all compilation units in OBJFILE.
8671 This is only done for -readnow and building partial symtabs. */
8672
8673 static void
8674 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8675 {
8676 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8677 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8678 &dwarf2_per_objfile->abbrev, 0);
8679
8680 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8681 if (dwz != NULL)
8682 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8683 1);
8684 }
8685
8686 /* Process all loaded DIEs for compilation unit CU, starting at
8687 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8688 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8689 DW_AT_ranges). See the comments of add_partial_subprogram on how
8690 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8691
8692 static void
8693 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8694 CORE_ADDR *highpc, int set_addrmap,
8695 struct dwarf2_cu *cu)
8696 {
8697 struct partial_die_info *pdi;
8698
8699 /* Now, march along the PDI's, descending into ones which have
8700 interesting children but skipping the children of the other ones,
8701 until we reach the end of the compilation unit. */
8702
8703 pdi = first_die;
8704
8705 while (pdi != NULL)
8706 {
8707 pdi->fixup (cu);
8708
8709 /* Anonymous namespaces or modules have no name but have interesting
8710 children, so we need to look at them. Ditto for anonymous
8711 enums. */
8712
8713 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8714 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8715 || pdi->tag == DW_TAG_imported_unit
8716 || pdi->tag == DW_TAG_inlined_subroutine)
8717 {
8718 switch (pdi->tag)
8719 {
8720 case DW_TAG_subprogram:
8721 case DW_TAG_inlined_subroutine:
8722 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8723 break;
8724 case DW_TAG_constant:
8725 case DW_TAG_variable:
8726 case DW_TAG_typedef:
8727 case DW_TAG_union_type:
8728 if (!pdi->is_declaration)
8729 {
8730 add_partial_symbol (pdi, cu);
8731 }
8732 break;
8733 case DW_TAG_class_type:
8734 case DW_TAG_interface_type:
8735 case DW_TAG_structure_type:
8736 if (!pdi->is_declaration)
8737 {
8738 add_partial_symbol (pdi, cu);
8739 }
8740 if ((cu->language == language_rust
8741 || cu->language == language_cplus) && pdi->has_children)
8742 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8743 set_addrmap, cu);
8744 break;
8745 case DW_TAG_enumeration_type:
8746 if (!pdi->is_declaration)
8747 add_partial_enumeration (pdi, cu);
8748 break;
8749 case DW_TAG_base_type:
8750 case DW_TAG_subrange_type:
8751 /* File scope base type definitions are added to the partial
8752 symbol table. */
8753 add_partial_symbol (pdi, cu);
8754 break;
8755 case DW_TAG_namespace:
8756 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8757 break;
8758 case DW_TAG_module:
8759 if (!pdi->is_declaration)
8760 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8761 break;
8762 case DW_TAG_imported_unit:
8763 {
8764 struct dwarf2_per_cu_data *per_cu;
8765
8766 /* For now we don't handle imported units in type units. */
8767 if (cu->per_cu->is_debug_types)
8768 {
8769 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8770 " supported in type units [in module %s]"),
8771 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8772 }
8773
8774 per_cu = dwarf2_find_containing_comp_unit
8775 (pdi->d.sect_off, pdi->is_dwz,
8776 cu->per_cu->dwarf2_per_objfile);
8777
8778 /* Go read the partial unit, if needed. */
8779 if (per_cu->v.psymtab == NULL)
8780 process_psymtab_comp_unit (per_cu, 1, cu->language);
8781
8782 cu->per_cu->imported_symtabs_push (per_cu);
8783 }
8784 break;
8785 case DW_TAG_imported_declaration:
8786 add_partial_symbol (pdi, cu);
8787 break;
8788 default:
8789 break;
8790 }
8791 }
8792
8793 /* If the die has a sibling, skip to the sibling. */
8794
8795 pdi = pdi->die_sibling;
8796 }
8797 }
8798
8799 /* Functions used to compute the fully scoped name of a partial DIE.
8800
8801 Normally, this is simple. For C++, the parent DIE's fully scoped
8802 name is concatenated with "::" and the partial DIE's name.
8803 Enumerators are an exception; they use the scope of their parent
8804 enumeration type, i.e. the name of the enumeration type is not
8805 prepended to the enumerator.
8806
8807 There are two complexities. One is DW_AT_specification; in this
8808 case "parent" means the parent of the target of the specification,
8809 instead of the direct parent of the DIE. The other is compilers
8810 which do not emit DW_TAG_namespace; in this case we try to guess
8811 the fully qualified name of structure types from their members'
8812 linkage names. This must be done using the DIE's children rather
8813 than the children of any DW_AT_specification target. We only need
8814 to do this for structures at the top level, i.e. if the target of
8815 any DW_AT_specification (if any; otherwise the DIE itself) does not
8816 have a parent. */
8817
8818 /* Compute the scope prefix associated with PDI's parent, in
8819 compilation unit CU. The result will be allocated on CU's
8820 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8821 field. NULL is returned if no prefix is necessary. */
8822 static const char *
8823 partial_die_parent_scope (struct partial_die_info *pdi,
8824 struct dwarf2_cu *cu)
8825 {
8826 const char *grandparent_scope;
8827 struct partial_die_info *parent, *real_pdi;
8828
8829 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8830 then this means the parent of the specification DIE. */
8831
8832 real_pdi = pdi;
8833 while (real_pdi->has_specification)
8834 {
8835 auto res = find_partial_die (real_pdi->spec_offset,
8836 real_pdi->spec_is_dwz, cu);
8837 real_pdi = res.pdi;
8838 cu = res.cu;
8839 }
8840
8841 parent = real_pdi->die_parent;
8842 if (parent == NULL)
8843 return NULL;
8844
8845 if (parent->scope_set)
8846 return parent->scope;
8847
8848 parent->fixup (cu);
8849
8850 grandparent_scope = partial_die_parent_scope (parent, cu);
8851
8852 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8853 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8854 Work around this problem here. */
8855 if (cu->language == language_cplus
8856 && parent->tag == DW_TAG_namespace
8857 && strcmp (parent->name, "::") == 0
8858 && grandparent_scope == NULL)
8859 {
8860 parent->scope = NULL;
8861 parent->scope_set = 1;
8862 return NULL;
8863 }
8864
8865 /* Nested subroutines in Fortran get a prefix. */
8866 if (pdi->tag == DW_TAG_enumerator)
8867 /* Enumerators should not get the name of the enumeration as a prefix. */
8868 parent->scope = grandparent_scope;
8869 else if (parent->tag == DW_TAG_namespace
8870 || parent->tag == DW_TAG_module
8871 || parent->tag == DW_TAG_structure_type
8872 || parent->tag == DW_TAG_class_type
8873 || parent->tag == DW_TAG_interface_type
8874 || parent->tag == DW_TAG_union_type
8875 || parent->tag == DW_TAG_enumeration_type
8876 || (cu->language == language_fortran
8877 && parent->tag == DW_TAG_subprogram
8878 && pdi->tag == DW_TAG_subprogram))
8879 {
8880 if (grandparent_scope == NULL)
8881 parent->scope = parent->name;
8882 else
8883 parent->scope = typename_concat (&cu->comp_unit_obstack,
8884 grandparent_scope,
8885 parent->name, 0, cu);
8886 }
8887 else
8888 {
8889 /* FIXME drow/2004-04-01: What should we be doing with
8890 function-local names? For partial symbols, we should probably be
8891 ignoring them. */
8892 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8893 dwarf_tag_name (parent->tag),
8894 sect_offset_str (pdi->sect_off));
8895 parent->scope = grandparent_scope;
8896 }
8897
8898 parent->scope_set = 1;
8899 return parent->scope;
8900 }
8901
8902 /* Return the fully scoped name associated with PDI, from compilation unit
8903 CU. The result will be allocated with malloc. */
8904
8905 static gdb::unique_xmalloc_ptr<char>
8906 partial_die_full_name (struct partial_die_info *pdi,
8907 struct dwarf2_cu *cu)
8908 {
8909 const char *parent_scope;
8910
8911 /* If this is a template instantiation, we can not work out the
8912 template arguments from partial DIEs. So, unfortunately, we have
8913 to go through the full DIEs. At least any work we do building
8914 types here will be reused if full symbols are loaded later. */
8915 if (pdi->has_template_arguments)
8916 {
8917 pdi->fixup (cu);
8918
8919 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8920 {
8921 struct die_info *die;
8922 struct attribute attr;
8923 struct dwarf2_cu *ref_cu = cu;
8924
8925 /* DW_FORM_ref_addr is using section offset. */
8926 attr.name = (enum dwarf_attribute) 0;
8927 attr.form = DW_FORM_ref_addr;
8928 attr.u.unsnd = to_underlying (pdi->sect_off);
8929 die = follow_die_ref (NULL, &attr, &ref_cu);
8930
8931 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8932 }
8933 }
8934
8935 parent_scope = partial_die_parent_scope (pdi, cu);
8936 if (parent_scope == NULL)
8937 return NULL;
8938 else
8939 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8940 pdi->name, 0, cu));
8941 }
8942
8943 static void
8944 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8945 {
8946 struct dwarf2_per_objfile *dwarf2_per_objfile
8947 = cu->per_cu->dwarf2_per_objfile;
8948 struct objfile *objfile = dwarf2_per_objfile->objfile;
8949 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8950 CORE_ADDR addr = 0;
8951 const char *actual_name = NULL;
8952 CORE_ADDR baseaddr;
8953
8954 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8955
8956 gdb::unique_xmalloc_ptr<char> built_actual_name
8957 = partial_die_full_name (pdi, cu);
8958 if (built_actual_name != NULL)
8959 actual_name = built_actual_name.get ();
8960
8961 if (actual_name == NULL)
8962 actual_name = pdi->name;
8963
8964 switch (pdi->tag)
8965 {
8966 case DW_TAG_inlined_subroutine:
8967 case DW_TAG_subprogram:
8968 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8969 - baseaddr);
8970 if (pdi->is_external
8971 || cu->language == language_ada
8972 || (cu->language == language_fortran
8973 && pdi->die_parent != NULL
8974 && pdi->die_parent->tag == DW_TAG_subprogram))
8975 {
8976 /* Normally, only "external" DIEs are part of the global scope.
8977 But in Ada and Fortran, we want to be able to access nested
8978 procedures globally. So all Ada and Fortran subprograms are
8979 stored in the global scope. */
8980 add_psymbol_to_list (actual_name,
8981 built_actual_name != NULL,
8982 VAR_DOMAIN, LOC_BLOCK,
8983 SECT_OFF_TEXT (objfile),
8984 psymbol_placement::GLOBAL,
8985 addr,
8986 cu->language, objfile);
8987 }
8988 else
8989 {
8990 add_psymbol_to_list (actual_name,
8991 built_actual_name != NULL,
8992 VAR_DOMAIN, LOC_BLOCK,
8993 SECT_OFF_TEXT (objfile),
8994 psymbol_placement::STATIC,
8995 addr, cu->language, objfile);
8996 }
8997
8998 if (pdi->main_subprogram && actual_name != NULL)
8999 set_objfile_main_name (objfile, actual_name, cu->language);
9000 break;
9001 case DW_TAG_constant:
9002 add_psymbol_to_list (actual_name,
9003 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9004 -1, (pdi->is_external
9005 ? psymbol_placement::GLOBAL
9006 : psymbol_placement::STATIC),
9007 0, cu->language, objfile);
9008 break;
9009 case DW_TAG_variable:
9010 if (pdi->d.locdesc)
9011 addr = decode_locdesc (pdi->d.locdesc, cu);
9012
9013 if (pdi->d.locdesc
9014 && addr == 0
9015 && !dwarf2_per_objfile->has_section_at_zero)
9016 {
9017 /* A global or static variable may also have been stripped
9018 out by the linker if unused, in which case its address
9019 will be nullified; do not add such variables into partial
9020 symbol table then. */
9021 }
9022 else if (pdi->is_external)
9023 {
9024 /* Global Variable.
9025 Don't enter into the minimal symbol tables as there is
9026 a minimal symbol table entry from the ELF symbols already.
9027 Enter into partial symbol table if it has a location
9028 descriptor or a type.
9029 If the location descriptor is missing, new_symbol will create
9030 a LOC_UNRESOLVED symbol, the address of the variable will then
9031 be determined from the minimal symbol table whenever the variable
9032 is referenced.
9033 The address for the partial symbol table entry is not
9034 used by GDB, but it comes in handy for debugging partial symbol
9035 table building. */
9036
9037 if (pdi->d.locdesc || pdi->has_type)
9038 add_psymbol_to_list (actual_name,
9039 built_actual_name != NULL,
9040 VAR_DOMAIN, LOC_STATIC,
9041 SECT_OFF_TEXT (objfile),
9042 psymbol_placement::GLOBAL,
9043 addr, cu->language, objfile);
9044 }
9045 else
9046 {
9047 int has_loc = pdi->d.locdesc != NULL;
9048
9049 /* Static Variable. Skip symbols whose value we cannot know (those
9050 without location descriptors or constant values). */
9051 if (!has_loc && !pdi->has_const_value)
9052 return;
9053
9054 add_psymbol_to_list (actual_name,
9055 built_actual_name != NULL,
9056 VAR_DOMAIN, LOC_STATIC,
9057 SECT_OFF_TEXT (objfile),
9058 psymbol_placement::STATIC,
9059 has_loc ? addr : 0,
9060 cu->language, objfile);
9061 }
9062 break;
9063 case DW_TAG_typedef:
9064 case DW_TAG_base_type:
9065 case DW_TAG_subrange_type:
9066 add_psymbol_to_list (actual_name,
9067 built_actual_name != NULL,
9068 VAR_DOMAIN, LOC_TYPEDEF, -1,
9069 psymbol_placement::STATIC,
9070 0, cu->language, objfile);
9071 break;
9072 case DW_TAG_imported_declaration:
9073 case DW_TAG_namespace:
9074 add_psymbol_to_list (actual_name,
9075 built_actual_name != NULL,
9076 VAR_DOMAIN, LOC_TYPEDEF, -1,
9077 psymbol_placement::GLOBAL,
9078 0, cu->language, objfile);
9079 break;
9080 case DW_TAG_module:
9081 /* With Fortran 77 there might be a "BLOCK DATA" module
9082 available without any name. If so, we skip the module as it
9083 doesn't bring any value. */
9084 if (actual_name != nullptr)
9085 add_psymbol_to_list (actual_name,
9086 built_actual_name != NULL,
9087 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9088 psymbol_placement::GLOBAL,
9089 0, cu->language, objfile);
9090 break;
9091 case DW_TAG_class_type:
9092 case DW_TAG_interface_type:
9093 case DW_TAG_structure_type:
9094 case DW_TAG_union_type:
9095 case DW_TAG_enumeration_type:
9096 /* Skip external references. The DWARF standard says in the section
9097 about "Structure, Union, and Class Type Entries": "An incomplete
9098 structure, union or class type is represented by a structure,
9099 union or class entry that does not have a byte size attribute
9100 and that has a DW_AT_declaration attribute." */
9101 if (!pdi->has_byte_size && pdi->is_declaration)
9102 return;
9103
9104 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9105 static vs. global. */
9106 add_psymbol_to_list (actual_name,
9107 built_actual_name != NULL,
9108 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9109 cu->language == language_cplus
9110 ? psymbol_placement::GLOBAL
9111 : psymbol_placement::STATIC,
9112 0, cu->language, objfile);
9113
9114 break;
9115 case DW_TAG_enumerator:
9116 add_psymbol_to_list (actual_name,
9117 built_actual_name != NULL,
9118 VAR_DOMAIN, LOC_CONST, -1,
9119 cu->language == language_cplus
9120 ? psymbol_placement::GLOBAL
9121 : psymbol_placement::STATIC,
9122 0, cu->language, objfile);
9123 break;
9124 default:
9125 break;
9126 }
9127 }
9128
9129 /* Read a partial die corresponding to a namespace; also, add a symbol
9130 corresponding to that namespace to the symbol table. NAMESPACE is
9131 the name of the enclosing namespace. */
9132
9133 static void
9134 add_partial_namespace (struct partial_die_info *pdi,
9135 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9136 int set_addrmap, struct dwarf2_cu *cu)
9137 {
9138 /* Add a symbol for the namespace. */
9139
9140 add_partial_symbol (pdi, cu);
9141
9142 /* Now scan partial symbols in that namespace. */
9143
9144 if (pdi->has_children)
9145 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9146 }
9147
9148 /* Read a partial die corresponding to a Fortran module. */
9149
9150 static void
9151 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9152 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9153 {
9154 /* Add a symbol for the namespace. */
9155
9156 add_partial_symbol (pdi, cu);
9157
9158 /* Now scan partial symbols in that module. */
9159
9160 if (pdi->has_children)
9161 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9162 }
9163
9164 /* Read a partial die corresponding to a subprogram or an inlined
9165 subprogram and create a partial symbol for that subprogram.
9166 When the CU language allows it, this routine also defines a partial
9167 symbol for each nested subprogram that this subprogram contains.
9168 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9169 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9170
9171 PDI may also be a lexical block, in which case we simply search
9172 recursively for subprograms defined inside that lexical block.
9173 Again, this is only performed when the CU language allows this
9174 type of definitions. */
9175
9176 static void
9177 add_partial_subprogram (struct partial_die_info *pdi,
9178 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9179 int set_addrmap, struct dwarf2_cu *cu)
9180 {
9181 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9182 {
9183 if (pdi->has_pc_info)
9184 {
9185 if (pdi->lowpc < *lowpc)
9186 *lowpc = pdi->lowpc;
9187 if (pdi->highpc > *highpc)
9188 *highpc = pdi->highpc;
9189 if (set_addrmap)
9190 {
9191 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9192 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9193 CORE_ADDR baseaddr;
9194 CORE_ADDR this_highpc;
9195 CORE_ADDR this_lowpc;
9196
9197 baseaddr = ANOFFSET (objfile->section_offsets,
9198 SECT_OFF_TEXT (objfile));
9199 this_lowpc
9200 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9201 pdi->lowpc + baseaddr)
9202 - baseaddr);
9203 this_highpc
9204 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9205 pdi->highpc + baseaddr)
9206 - baseaddr);
9207 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
9208 this_lowpc, this_highpc - 1,
9209 cu->per_cu->v.psymtab);
9210 }
9211 }
9212
9213 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9214 {
9215 if (!pdi->is_declaration)
9216 /* Ignore subprogram DIEs that do not have a name, they are
9217 illegal. Do not emit a complaint at this point, we will
9218 do so when we convert this psymtab into a symtab. */
9219 if (pdi->name)
9220 add_partial_symbol (pdi, cu);
9221 }
9222 }
9223
9224 if (! pdi->has_children)
9225 return;
9226
9227 if (cu->language == language_ada || cu->language == language_fortran)
9228 {
9229 pdi = pdi->die_child;
9230 while (pdi != NULL)
9231 {
9232 pdi->fixup (cu);
9233 if (pdi->tag == DW_TAG_subprogram
9234 || pdi->tag == DW_TAG_inlined_subroutine
9235 || pdi->tag == DW_TAG_lexical_block)
9236 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9237 pdi = pdi->die_sibling;
9238 }
9239 }
9240 }
9241
9242 /* Read a partial die corresponding to an enumeration type. */
9243
9244 static void
9245 add_partial_enumeration (struct partial_die_info *enum_pdi,
9246 struct dwarf2_cu *cu)
9247 {
9248 struct partial_die_info *pdi;
9249
9250 if (enum_pdi->name != NULL)
9251 add_partial_symbol (enum_pdi, cu);
9252
9253 pdi = enum_pdi->die_child;
9254 while (pdi)
9255 {
9256 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9257 complaint (_("malformed enumerator DIE ignored"));
9258 else
9259 add_partial_symbol (pdi, cu);
9260 pdi = pdi->die_sibling;
9261 }
9262 }
9263
9264 /* Return the initial uleb128 in the die at INFO_PTR. */
9265
9266 static unsigned int
9267 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9268 {
9269 unsigned int bytes_read;
9270
9271 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9272 }
9273
9274 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9275 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9276
9277 Return the corresponding abbrev, or NULL if the number is zero (indicating
9278 an empty DIE). In either case *BYTES_READ will be set to the length of
9279 the initial number. */
9280
9281 static struct abbrev_info *
9282 peek_die_abbrev (const die_reader_specs &reader,
9283 const gdb_byte *info_ptr, unsigned int *bytes_read)
9284 {
9285 dwarf2_cu *cu = reader.cu;
9286 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9287 unsigned int abbrev_number
9288 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9289
9290 if (abbrev_number == 0)
9291 return NULL;
9292
9293 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9294 if (!abbrev)
9295 {
9296 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9297 " at offset %s [in module %s]"),
9298 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9299 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9300 }
9301
9302 return abbrev;
9303 }
9304
9305 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9306 Returns a pointer to the end of a series of DIEs, terminated by an empty
9307 DIE. Any children of the skipped DIEs will also be skipped. */
9308
9309 static const gdb_byte *
9310 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9311 {
9312 while (1)
9313 {
9314 unsigned int bytes_read;
9315 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9316
9317 if (abbrev == NULL)
9318 return info_ptr + bytes_read;
9319 else
9320 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9321 }
9322 }
9323
9324 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9325 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9326 abbrev corresponding to that skipped uleb128 should be passed in
9327 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9328 children. */
9329
9330 static const gdb_byte *
9331 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9332 struct abbrev_info *abbrev)
9333 {
9334 unsigned int bytes_read;
9335 struct attribute attr;
9336 bfd *abfd = reader->abfd;
9337 struct dwarf2_cu *cu = reader->cu;
9338 const gdb_byte *buffer = reader->buffer;
9339 const gdb_byte *buffer_end = reader->buffer_end;
9340 unsigned int form, i;
9341
9342 for (i = 0; i < abbrev->num_attrs; i++)
9343 {
9344 /* The only abbrev we care about is DW_AT_sibling. */
9345 if (abbrev->attrs[i].name == DW_AT_sibling)
9346 {
9347 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9348 if (attr.form == DW_FORM_ref_addr)
9349 complaint (_("ignoring absolute DW_AT_sibling"));
9350 else
9351 {
9352 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9353 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9354
9355 if (sibling_ptr < info_ptr)
9356 complaint (_("DW_AT_sibling points backwards"));
9357 else if (sibling_ptr > reader->buffer_end)
9358 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9359 else
9360 return sibling_ptr;
9361 }
9362 }
9363
9364 /* If it isn't DW_AT_sibling, skip this attribute. */
9365 form = abbrev->attrs[i].form;
9366 skip_attribute:
9367 switch (form)
9368 {
9369 case DW_FORM_ref_addr:
9370 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9371 and later it is offset sized. */
9372 if (cu->header.version == 2)
9373 info_ptr += cu->header.addr_size;
9374 else
9375 info_ptr += cu->header.offset_size;
9376 break;
9377 case DW_FORM_GNU_ref_alt:
9378 info_ptr += cu->header.offset_size;
9379 break;
9380 case DW_FORM_addr:
9381 info_ptr += cu->header.addr_size;
9382 break;
9383 case DW_FORM_data1:
9384 case DW_FORM_ref1:
9385 case DW_FORM_flag:
9386 case DW_FORM_strx1:
9387 info_ptr += 1;
9388 break;
9389 case DW_FORM_flag_present:
9390 case DW_FORM_implicit_const:
9391 break;
9392 case DW_FORM_data2:
9393 case DW_FORM_ref2:
9394 case DW_FORM_strx2:
9395 info_ptr += 2;
9396 break;
9397 case DW_FORM_strx3:
9398 info_ptr += 3;
9399 break;
9400 case DW_FORM_data4:
9401 case DW_FORM_ref4:
9402 case DW_FORM_strx4:
9403 info_ptr += 4;
9404 break;
9405 case DW_FORM_data8:
9406 case DW_FORM_ref8:
9407 case DW_FORM_ref_sig8:
9408 info_ptr += 8;
9409 break;
9410 case DW_FORM_data16:
9411 info_ptr += 16;
9412 break;
9413 case DW_FORM_string:
9414 read_direct_string (abfd, info_ptr, &bytes_read);
9415 info_ptr += bytes_read;
9416 break;
9417 case DW_FORM_sec_offset:
9418 case DW_FORM_strp:
9419 case DW_FORM_GNU_strp_alt:
9420 info_ptr += cu->header.offset_size;
9421 break;
9422 case DW_FORM_exprloc:
9423 case DW_FORM_block:
9424 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9425 info_ptr += bytes_read;
9426 break;
9427 case DW_FORM_block1:
9428 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9429 break;
9430 case DW_FORM_block2:
9431 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9432 break;
9433 case DW_FORM_block4:
9434 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9435 break;
9436 case DW_FORM_addrx:
9437 case DW_FORM_strx:
9438 case DW_FORM_sdata:
9439 case DW_FORM_udata:
9440 case DW_FORM_ref_udata:
9441 case DW_FORM_GNU_addr_index:
9442 case DW_FORM_GNU_str_index:
9443 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9444 break;
9445 case DW_FORM_indirect:
9446 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9447 info_ptr += bytes_read;
9448 /* We need to continue parsing from here, so just go back to
9449 the top. */
9450 goto skip_attribute;
9451
9452 default:
9453 error (_("Dwarf Error: Cannot handle %s "
9454 "in DWARF reader [in module %s]"),
9455 dwarf_form_name (form),
9456 bfd_get_filename (abfd));
9457 }
9458 }
9459
9460 if (abbrev->has_children)
9461 return skip_children (reader, info_ptr);
9462 else
9463 return info_ptr;
9464 }
9465
9466 /* Locate ORIG_PDI's sibling.
9467 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9468
9469 static const gdb_byte *
9470 locate_pdi_sibling (const struct die_reader_specs *reader,
9471 struct partial_die_info *orig_pdi,
9472 const gdb_byte *info_ptr)
9473 {
9474 /* Do we know the sibling already? */
9475
9476 if (orig_pdi->sibling)
9477 return orig_pdi->sibling;
9478
9479 /* Are there any children to deal with? */
9480
9481 if (!orig_pdi->has_children)
9482 return info_ptr;
9483
9484 /* Skip the children the long way. */
9485
9486 return skip_children (reader, info_ptr);
9487 }
9488
9489 /* Expand this partial symbol table into a full symbol table. SELF is
9490 not NULL. */
9491
9492 static void
9493 dwarf2_read_symtab (struct partial_symtab *self,
9494 struct objfile *objfile)
9495 {
9496 struct dwarf2_per_objfile *dwarf2_per_objfile
9497 = get_dwarf2_per_objfile (objfile);
9498
9499 if (self->readin)
9500 {
9501 warning (_("bug: psymtab for %s is already read in."),
9502 self->filename);
9503 }
9504 else
9505 {
9506 if (info_verbose)
9507 {
9508 printf_filtered (_("Reading in symbols for %s..."),
9509 self->filename);
9510 gdb_flush (gdb_stdout);
9511 }
9512
9513 /* If this psymtab is constructed from a debug-only objfile, the
9514 has_section_at_zero flag will not necessarily be correct. We
9515 can get the correct value for this flag by looking at the data
9516 associated with the (presumably stripped) associated objfile. */
9517 if (objfile->separate_debug_objfile_backlink)
9518 {
9519 struct dwarf2_per_objfile *dpo_backlink
9520 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9521
9522 dwarf2_per_objfile->has_section_at_zero
9523 = dpo_backlink->has_section_at_zero;
9524 }
9525
9526 dwarf2_per_objfile->reading_partial_symbols = 0;
9527
9528 psymtab_to_symtab_1 (self);
9529
9530 /* Finish up the debug error message. */
9531 if (info_verbose)
9532 printf_filtered (_("done.\n"));
9533 }
9534
9535 process_cu_includes (dwarf2_per_objfile);
9536 }
9537 \f
9538 /* Reading in full CUs. */
9539
9540 /* Add PER_CU to the queue. */
9541
9542 static void
9543 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9544 enum language pretend_language)
9545 {
9546 struct dwarf2_queue_item *item;
9547
9548 per_cu->queued = 1;
9549 item = XNEW (struct dwarf2_queue_item);
9550 item->per_cu = per_cu;
9551 item->pretend_language = pretend_language;
9552 item->next = NULL;
9553
9554 if (dwarf2_queue == NULL)
9555 dwarf2_queue = item;
9556 else
9557 dwarf2_queue_tail->next = item;
9558
9559 dwarf2_queue_tail = item;
9560 }
9561
9562 /* If PER_CU is not yet queued, add it to the queue.
9563 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9564 dependency.
9565 The result is non-zero if PER_CU was queued, otherwise the result is zero
9566 meaning either PER_CU is already queued or it is already loaded.
9567
9568 N.B. There is an invariant here that if a CU is queued then it is loaded.
9569 The caller is required to load PER_CU if we return non-zero. */
9570
9571 static int
9572 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9573 struct dwarf2_per_cu_data *per_cu,
9574 enum language pretend_language)
9575 {
9576 /* We may arrive here during partial symbol reading, if we need full
9577 DIEs to process an unusual case (e.g. template arguments). Do
9578 not queue PER_CU, just tell our caller to load its DIEs. */
9579 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9580 {
9581 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9582 return 1;
9583 return 0;
9584 }
9585
9586 /* Mark the dependence relation so that we don't flush PER_CU
9587 too early. */
9588 if (dependent_cu != NULL)
9589 dwarf2_add_dependence (dependent_cu, per_cu);
9590
9591 /* If it's already on the queue, we have nothing to do. */
9592 if (per_cu->queued)
9593 return 0;
9594
9595 /* If the compilation unit is already loaded, just mark it as
9596 used. */
9597 if (per_cu->cu != NULL)
9598 {
9599 per_cu->cu->last_used = 0;
9600 return 0;
9601 }
9602
9603 /* Add it to the queue. */
9604 queue_comp_unit (per_cu, pretend_language);
9605
9606 return 1;
9607 }
9608
9609 /* Process the queue. */
9610
9611 static void
9612 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9613 {
9614 struct dwarf2_queue_item *item, *next_item;
9615
9616 if (dwarf_read_debug)
9617 {
9618 fprintf_unfiltered (gdb_stdlog,
9619 "Expanding one or more symtabs of objfile %s ...\n",
9620 objfile_name (dwarf2_per_objfile->objfile));
9621 }
9622
9623 /* The queue starts out with one item, but following a DIE reference
9624 may load a new CU, adding it to the end of the queue. */
9625 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9626 {
9627 if ((dwarf2_per_objfile->using_index
9628 ? !item->per_cu->v.quick->compunit_symtab
9629 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9630 /* Skip dummy CUs. */
9631 && item->per_cu->cu != NULL)
9632 {
9633 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9634 unsigned int debug_print_threshold;
9635 char buf[100];
9636
9637 if (per_cu->is_debug_types)
9638 {
9639 struct signatured_type *sig_type =
9640 (struct signatured_type *) per_cu;
9641
9642 sprintf (buf, "TU %s at offset %s",
9643 hex_string (sig_type->signature),
9644 sect_offset_str (per_cu->sect_off));
9645 /* There can be 100s of TUs.
9646 Only print them in verbose mode. */
9647 debug_print_threshold = 2;
9648 }
9649 else
9650 {
9651 sprintf (buf, "CU at offset %s",
9652 sect_offset_str (per_cu->sect_off));
9653 debug_print_threshold = 1;
9654 }
9655
9656 if (dwarf_read_debug >= debug_print_threshold)
9657 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9658
9659 if (per_cu->is_debug_types)
9660 process_full_type_unit (per_cu, item->pretend_language);
9661 else
9662 process_full_comp_unit (per_cu, item->pretend_language);
9663
9664 if (dwarf_read_debug >= debug_print_threshold)
9665 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9666 }
9667
9668 item->per_cu->queued = 0;
9669 next_item = item->next;
9670 xfree (item);
9671 }
9672
9673 dwarf2_queue_tail = NULL;
9674
9675 if (dwarf_read_debug)
9676 {
9677 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9678 objfile_name (dwarf2_per_objfile->objfile));
9679 }
9680 }
9681
9682 /* Read in full symbols for PST, and anything it depends on. */
9683
9684 static void
9685 psymtab_to_symtab_1 (struct partial_symtab *pst)
9686 {
9687 struct dwarf2_per_cu_data *per_cu;
9688 int i;
9689
9690 if (pst->readin)
9691 return;
9692
9693 for (i = 0; i < pst->number_of_dependencies; i++)
9694 if (!pst->dependencies[i]->readin
9695 && pst->dependencies[i]->user == NULL)
9696 {
9697 /* Inform about additional files that need to be read in. */
9698 if (info_verbose)
9699 {
9700 /* FIXME: i18n: Need to make this a single string. */
9701 fputs_filtered (" ", gdb_stdout);
9702 wrap_here ("");
9703 fputs_filtered ("and ", gdb_stdout);
9704 wrap_here ("");
9705 printf_filtered ("%s...", pst->dependencies[i]->filename);
9706 wrap_here (""); /* Flush output. */
9707 gdb_flush (gdb_stdout);
9708 }
9709 psymtab_to_symtab_1 (pst->dependencies[i]);
9710 }
9711
9712 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9713
9714 if (per_cu == NULL)
9715 {
9716 /* It's an include file, no symbols to read for it.
9717 Everything is in the parent symtab. */
9718 pst->readin = 1;
9719 return;
9720 }
9721
9722 dw2_do_instantiate_symtab (per_cu, false);
9723 }
9724
9725 /* Trivial hash function for die_info: the hash value of a DIE
9726 is its offset in .debug_info for this objfile. */
9727
9728 static hashval_t
9729 die_hash (const void *item)
9730 {
9731 const struct die_info *die = (const struct die_info *) item;
9732
9733 return to_underlying (die->sect_off);
9734 }
9735
9736 /* Trivial comparison function for die_info structures: two DIEs
9737 are equal if they have the same offset. */
9738
9739 static int
9740 die_eq (const void *item_lhs, const void *item_rhs)
9741 {
9742 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9743 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9744
9745 return die_lhs->sect_off == die_rhs->sect_off;
9746 }
9747
9748 /* die_reader_func for load_full_comp_unit.
9749 This is identical to read_signatured_type_reader,
9750 but is kept separate for now. */
9751
9752 static void
9753 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9754 const gdb_byte *info_ptr,
9755 struct die_info *comp_unit_die,
9756 int has_children,
9757 void *data)
9758 {
9759 struct dwarf2_cu *cu = reader->cu;
9760 enum language *language_ptr = (enum language *) data;
9761
9762 gdb_assert (cu->die_hash == NULL);
9763 cu->die_hash =
9764 htab_create_alloc_ex (cu->header.length / 12,
9765 die_hash,
9766 die_eq,
9767 NULL,
9768 &cu->comp_unit_obstack,
9769 hashtab_obstack_allocate,
9770 dummy_obstack_deallocate);
9771
9772 if (has_children)
9773 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9774 &info_ptr, comp_unit_die);
9775 cu->dies = comp_unit_die;
9776 /* comp_unit_die is not stored in die_hash, no need. */
9777
9778 /* We try not to read any attributes in this function, because not
9779 all CUs needed for references have been loaded yet, and symbol
9780 table processing isn't initialized. But we have to set the CU language,
9781 or we won't be able to build types correctly.
9782 Similarly, if we do not read the producer, we can not apply
9783 producer-specific interpretation. */
9784 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
9785 }
9786
9787 /* Load the DIEs associated with PER_CU into memory. */
9788
9789 static void
9790 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9791 bool skip_partial,
9792 enum language pretend_language)
9793 {
9794 gdb_assert (! this_cu->is_debug_types);
9795
9796 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
9797 load_full_comp_unit_reader, &pretend_language);
9798 }
9799
9800 /* Add a DIE to the delayed physname list. */
9801
9802 static void
9803 add_to_method_list (struct type *type, int fnfield_index, int index,
9804 const char *name, struct die_info *die,
9805 struct dwarf2_cu *cu)
9806 {
9807 struct delayed_method_info mi;
9808 mi.type = type;
9809 mi.fnfield_index = fnfield_index;
9810 mi.index = index;
9811 mi.name = name;
9812 mi.die = die;
9813 cu->method_list.push_back (mi);
9814 }
9815
9816 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9817 "const" / "volatile". If so, decrements LEN by the length of the
9818 modifier and return true. Otherwise return false. */
9819
9820 template<size_t N>
9821 static bool
9822 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9823 {
9824 size_t mod_len = sizeof (mod) - 1;
9825 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9826 {
9827 len -= mod_len;
9828 return true;
9829 }
9830 return false;
9831 }
9832
9833 /* Compute the physnames of any methods on the CU's method list.
9834
9835 The computation of method physnames is delayed in order to avoid the
9836 (bad) condition that one of the method's formal parameters is of an as yet
9837 incomplete type. */
9838
9839 static void
9840 compute_delayed_physnames (struct dwarf2_cu *cu)
9841 {
9842 /* Only C++ delays computing physnames. */
9843 if (cu->method_list.empty ())
9844 return;
9845 gdb_assert (cu->language == language_cplus);
9846
9847 for (const delayed_method_info &mi : cu->method_list)
9848 {
9849 const char *physname;
9850 struct fn_fieldlist *fn_flp
9851 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9852 physname = dwarf2_physname (mi.name, mi.die, cu);
9853 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9854 = physname ? physname : "";
9855
9856 /* Since there's no tag to indicate whether a method is a
9857 const/volatile overload, extract that information out of the
9858 demangled name. */
9859 if (physname != NULL)
9860 {
9861 size_t len = strlen (physname);
9862
9863 while (1)
9864 {
9865 if (physname[len] == ')') /* shortcut */
9866 break;
9867 else if (check_modifier (physname, len, " const"))
9868 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9869 else if (check_modifier (physname, len, " volatile"))
9870 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9871 else
9872 break;
9873 }
9874 }
9875 }
9876
9877 /* The list is no longer needed. */
9878 cu->method_list.clear ();
9879 }
9880
9881 /* Go objects should be embedded in a DW_TAG_module DIE,
9882 and it's not clear if/how imported objects will appear.
9883 To keep Go support simple until that's worked out,
9884 go back through what we've read and create something usable.
9885 We could do this while processing each DIE, and feels kinda cleaner,
9886 but that way is more invasive.
9887 This is to, for example, allow the user to type "p var" or "b main"
9888 without having to specify the package name, and allow lookups
9889 of module.object to work in contexts that use the expression
9890 parser. */
9891
9892 static void
9893 fixup_go_packaging (struct dwarf2_cu *cu)
9894 {
9895 char *package_name = NULL;
9896 struct pending *list;
9897 int i;
9898
9899 for (list = *cu->get_builder ()->get_global_symbols ();
9900 list != NULL;
9901 list = list->next)
9902 {
9903 for (i = 0; i < list->nsyms; ++i)
9904 {
9905 struct symbol *sym = list->symbol[i];
9906
9907 if (sym->language () == language_go
9908 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9909 {
9910 char *this_package_name = go_symbol_package_name (sym);
9911
9912 if (this_package_name == NULL)
9913 continue;
9914 if (package_name == NULL)
9915 package_name = this_package_name;
9916 else
9917 {
9918 struct objfile *objfile
9919 = cu->per_cu->dwarf2_per_objfile->objfile;
9920 if (strcmp (package_name, this_package_name) != 0)
9921 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9922 (symbol_symtab (sym) != NULL
9923 ? symtab_to_filename_for_display
9924 (symbol_symtab (sym))
9925 : objfile_name (objfile)),
9926 this_package_name, package_name);
9927 xfree (this_package_name);
9928 }
9929 }
9930 }
9931 }
9932
9933 if (package_name != NULL)
9934 {
9935 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9936 const char *saved_package_name
9937 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name);
9938 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9939 saved_package_name);
9940 struct symbol *sym;
9941
9942 sym = allocate_symbol (objfile);
9943 sym->set_language (language_go, &objfile->objfile_obstack);
9944 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9945 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9946 e.g., "main" finds the "main" module and not C's main(). */
9947 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9948 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9949 SYMBOL_TYPE (sym) = type;
9950
9951 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9952
9953 xfree (package_name);
9954 }
9955 }
9956
9957 /* Allocate a fully-qualified name consisting of the two parts on the
9958 obstack. */
9959
9960 static const char *
9961 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9962 {
9963 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9964 }
9965
9966 /* A helper that allocates a struct discriminant_info to attach to a
9967 union type. */
9968
9969 static struct discriminant_info *
9970 alloc_discriminant_info (struct type *type, int discriminant_index,
9971 int default_index)
9972 {
9973 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9974 gdb_assert (discriminant_index == -1
9975 || (discriminant_index >= 0
9976 && discriminant_index < TYPE_NFIELDS (type)));
9977 gdb_assert (default_index == -1
9978 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9979
9980 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9981
9982 struct discriminant_info *disc
9983 = ((struct discriminant_info *)
9984 TYPE_ZALLOC (type,
9985 offsetof (struct discriminant_info, discriminants)
9986 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9987 disc->default_index = default_index;
9988 disc->discriminant_index = discriminant_index;
9989
9990 struct dynamic_prop prop;
9991 prop.kind = PROP_UNDEFINED;
9992 prop.data.baton = disc;
9993
9994 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9995
9996 return disc;
9997 }
9998
9999 /* Some versions of rustc emitted enums in an unusual way.
10000
10001 Ordinary enums were emitted as unions. The first element of each
10002 structure in the union was named "RUST$ENUM$DISR". This element
10003 held the discriminant.
10004
10005 These versions of Rust also implemented the "non-zero"
10006 optimization. When the enum had two values, and one is empty and
10007 the other holds a pointer that cannot be zero, the pointer is used
10008 as the discriminant, with a zero value meaning the empty variant.
10009 Here, the union's first member is of the form
10010 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
10011 where the fieldnos are the indices of the fields that should be
10012 traversed in order to find the field (which may be several fields deep)
10013 and the variantname is the name of the variant of the case when the
10014 field is zero.
10015
10016 This function recognizes whether TYPE is of one of these forms,
10017 and, if so, smashes it to be a variant type. */
10018
10019 static void
10020 quirk_rust_enum (struct type *type, struct objfile *objfile)
10021 {
10022 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10023
10024 /* We don't need to deal with empty enums. */
10025 if (TYPE_NFIELDS (type) == 0)
10026 return;
10027
10028 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10029 if (TYPE_NFIELDS (type) == 1
10030 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10031 {
10032 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10033
10034 /* Decode the field name to find the offset of the
10035 discriminant. */
10036 ULONGEST bit_offset = 0;
10037 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10038 while (name[0] >= '0' && name[0] <= '9')
10039 {
10040 char *tail;
10041 unsigned long index = strtoul (name, &tail, 10);
10042 name = tail;
10043 if (*name != '$'
10044 || index >= TYPE_NFIELDS (field_type)
10045 || (TYPE_FIELD_LOC_KIND (field_type, index)
10046 != FIELD_LOC_KIND_BITPOS))
10047 {
10048 complaint (_("Could not parse Rust enum encoding string \"%s\""
10049 "[in module %s]"),
10050 TYPE_FIELD_NAME (type, 0),
10051 objfile_name (objfile));
10052 return;
10053 }
10054 ++name;
10055
10056 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10057 field_type = TYPE_FIELD_TYPE (field_type, index);
10058 }
10059
10060 /* Make a union to hold the variants. */
10061 struct type *union_type = alloc_type (objfile);
10062 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10063 TYPE_NFIELDS (union_type) = 3;
10064 TYPE_FIELDS (union_type)
10065 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10066 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10067 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10068
10069 /* Put the discriminant must at index 0. */
10070 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10071 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10072 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10073 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10074
10075 /* The order of fields doesn't really matter, so put the real
10076 field at index 1 and the data-less field at index 2. */
10077 struct discriminant_info *disc
10078 = alloc_discriminant_info (union_type, 0, 1);
10079 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10080 TYPE_FIELD_NAME (union_type, 1)
10081 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10082 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10083 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10084 TYPE_FIELD_NAME (union_type, 1));
10085
10086 const char *dataless_name
10087 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10088 name);
10089 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10090 dataless_name);
10091 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10092 /* NAME points into the original discriminant name, which
10093 already has the correct lifetime. */
10094 TYPE_FIELD_NAME (union_type, 2) = name;
10095 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10096 disc->discriminants[2] = 0;
10097
10098 /* Smash this type to be a structure type. We have to do this
10099 because the type has already been recorded. */
10100 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10101 TYPE_NFIELDS (type) = 1;
10102 TYPE_FIELDS (type)
10103 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10104
10105 /* Install the variant part. */
10106 TYPE_FIELD_TYPE (type, 0) = union_type;
10107 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10108 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10109 }
10110 /* A union with a single anonymous field is probably an old-style
10111 univariant enum. */
10112 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
10113 {
10114 /* Smash this type to be a structure type. We have to do this
10115 because the type has already been recorded. */
10116 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10117
10118 /* Make a union to hold the variants. */
10119 struct type *union_type = alloc_type (objfile);
10120 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10121 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10122 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10123 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10124 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10125
10126 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10127 const char *variant_name
10128 = rust_last_path_segment (TYPE_NAME (field_type));
10129 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10130 TYPE_NAME (field_type)
10131 = rust_fully_qualify (&objfile->objfile_obstack,
10132 TYPE_NAME (type), variant_name);
10133
10134 /* Install the union in the outer struct type. */
10135 TYPE_NFIELDS (type) = 1;
10136 TYPE_FIELDS (type)
10137 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10138 TYPE_FIELD_TYPE (type, 0) = union_type;
10139 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10140 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10141
10142 alloc_discriminant_info (union_type, -1, 0);
10143 }
10144 else
10145 {
10146 struct type *disr_type = nullptr;
10147 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10148 {
10149 disr_type = TYPE_FIELD_TYPE (type, i);
10150
10151 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10152 {
10153 /* All fields of a true enum will be structs. */
10154 return;
10155 }
10156 else if (TYPE_NFIELDS (disr_type) == 0)
10157 {
10158 /* Could be data-less variant, so keep going. */
10159 disr_type = nullptr;
10160 }
10161 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10162 "RUST$ENUM$DISR") != 0)
10163 {
10164 /* Not a Rust enum. */
10165 return;
10166 }
10167 else
10168 {
10169 /* Found one. */
10170 break;
10171 }
10172 }
10173
10174 /* If we got here without a discriminant, then it's probably
10175 just a union. */
10176 if (disr_type == nullptr)
10177 return;
10178
10179 /* Smash this type to be a structure type. We have to do this
10180 because the type has already been recorded. */
10181 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10182
10183 /* Make a union to hold the variants. */
10184 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10185 struct type *union_type = alloc_type (objfile);
10186 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10187 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10188 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10189 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10190 TYPE_FIELDS (union_type)
10191 = (struct field *) TYPE_ZALLOC (union_type,
10192 (TYPE_NFIELDS (union_type)
10193 * sizeof (struct field)));
10194
10195 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10196 TYPE_NFIELDS (type) * sizeof (struct field));
10197
10198 /* Install the discriminant at index 0 in the union. */
10199 TYPE_FIELD (union_type, 0) = *disr_field;
10200 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10201 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10202
10203 /* Install the union in the outer struct type. */
10204 TYPE_FIELD_TYPE (type, 0) = union_type;
10205 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10206 TYPE_NFIELDS (type) = 1;
10207
10208 /* Set the size and offset of the union type. */
10209 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10210
10211 /* We need a way to find the correct discriminant given a
10212 variant name. For convenience we build a map here. */
10213 struct type *enum_type = FIELD_TYPE (*disr_field);
10214 std::unordered_map<std::string, ULONGEST> discriminant_map;
10215 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10216 {
10217 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10218 {
10219 const char *name
10220 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10221 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10222 }
10223 }
10224
10225 int n_fields = TYPE_NFIELDS (union_type);
10226 struct discriminant_info *disc
10227 = alloc_discriminant_info (union_type, 0, -1);
10228 /* Skip the discriminant here. */
10229 for (int i = 1; i < n_fields; ++i)
10230 {
10231 /* Find the final word in the name of this variant's type.
10232 That name can be used to look up the correct
10233 discriminant. */
10234 const char *variant_name
10235 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10236 i)));
10237
10238 auto iter = discriminant_map.find (variant_name);
10239 if (iter != discriminant_map.end ())
10240 disc->discriminants[i] = iter->second;
10241
10242 /* Remove the discriminant field, if it exists. */
10243 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10244 if (TYPE_NFIELDS (sub_type) > 0)
10245 {
10246 --TYPE_NFIELDS (sub_type);
10247 ++TYPE_FIELDS (sub_type);
10248 }
10249 TYPE_FIELD_NAME (union_type, i) = variant_name;
10250 TYPE_NAME (sub_type)
10251 = rust_fully_qualify (&objfile->objfile_obstack,
10252 TYPE_NAME (type), variant_name);
10253 }
10254 }
10255 }
10256
10257 /* Rewrite some Rust unions to be structures with variants parts. */
10258
10259 static void
10260 rust_union_quirks (struct dwarf2_cu *cu)
10261 {
10262 gdb_assert (cu->language == language_rust);
10263 for (type *type_ : cu->rust_unions)
10264 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
10265 /* We don't need this any more. */
10266 cu->rust_unions.clear ();
10267 }
10268
10269 /* Return the symtab for PER_CU. This works properly regardless of
10270 whether we're using the index or psymtabs. */
10271
10272 static struct compunit_symtab *
10273 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10274 {
10275 return (per_cu->dwarf2_per_objfile->using_index
10276 ? per_cu->v.quick->compunit_symtab
10277 : per_cu->v.psymtab->compunit_symtab);
10278 }
10279
10280 /* A helper function for computing the list of all symbol tables
10281 included by PER_CU. */
10282
10283 static void
10284 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
10285 htab_t all_children, htab_t all_type_symtabs,
10286 struct dwarf2_per_cu_data *per_cu,
10287 struct compunit_symtab *immediate_parent)
10288 {
10289 void **slot;
10290 struct compunit_symtab *cust;
10291
10292 slot = htab_find_slot (all_children, per_cu, INSERT);
10293 if (*slot != NULL)
10294 {
10295 /* This inclusion and its children have been processed. */
10296 return;
10297 }
10298
10299 *slot = per_cu;
10300 /* Only add a CU if it has a symbol table. */
10301 cust = get_compunit_symtab (per_cu);
10302 if (cust != NULL)
10303 {
10304 /* If this is a type unit only add its symbol table if we haven't
10305 seen it yet (type unit per_cu's can share symtabs). */
10306 if (per_cu->is_debug_types)
10307 {
10308 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10309 if (*slot == NULL)
10310 {
10311 *slot = cust;
10312 result->push_back (cust);
10313 if (cust->user == NULL)
10314 cust->user = immediate_parent;
10315 }
10316 }
10317 else
10318 {
10319 result->push_back (cust);
10320 if (cust->user == NULL)
10321 cust->user = immediate_parent;
10322 }
10323 }
10324
10325 if (!per_cu->imported_symtabs_empty ())
10326 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10327 {
10328 recursively_compute_inclusions (result, all_children,
10329 all_type_symtabs, ptr, cust);
10330 }
10331 }
10332
10333 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10334 PER_CU. */
10335
10336 static void
10337 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10338 {
10339 gdb_assert (! per_cu->is_debug_types);
10340
10341 if (!per_cu->imported_symtabs_empty ())
10342 {
10343 int len;
10344 std::vector<compunit_symtab *> result_symtabs;
10345 htab_t all_children, all_type_symtabs;
10346 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10347
10348 /* If we don't have a symtab, we can just skip this case. */
10349 if (cust == NULL)
10350 return;
10351
10352 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10353 NULL, xcalloc, xfree);
10354 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10355 NULL, xcalloc, xfree);
10356
10357 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10358 {
10359 recursively_compute_inclusions (&result_symtabs, all_children,
10360 all_type_symtabs, ptr, cust);
10361 }
10362
10363 /* Now we have a transitive closure of all the included symtabs. */
10364 len = result_symtabs.size ();
10365 cust->includes
10366 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10367 struct compunit_symtab *, len + 1);
10368 memcpy (cust->includes, result_symtabs.data (),
10369 len * sizeof (compunit_symtab *));
10370 cust->includes[len] = NULL;
10371
10372 htab_delete (all_children);
10373 htab_delete (all_type_symtabs);
10374 }
10375 }
10376
10377 /* Compute the 'includes' field for the symtabs of all the CUs we just
10378 read. */
10379
10380 static void
10381 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10382 {
10383 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
10384 {
10385 if (! iter->is_debug_types)
10386 compute_compunit_symtab_includes (iter);
10387 }
10388
10389 dwarf2_per_objfile->just_read_cus.clear ();
10390 }
10391
10392 /* Generate full symbol information for PER_CU, whose DIEs have
10393 already been loaded into memory. */
10394
10395 static void
10396 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10397 enum language pretend_language)
10398 {
10399 struct dwarf2_cu *cu = per_cu->cu;
10400 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10401 struct objfile *objfile = dwarf2_per_objfile->objfile;
10402 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10403 CORE_ADDR lowpc, highpc;
10404 struct compunit_symtab *cust;
10405 CORE_ADDR baseaddr;
10406 struct block *static_block;
10407 CORE_ADDR addr;
10408
10409 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10410
10411 /* Clear the list here in case something was left over. */
10412 cu->method_list.clear ();
10413
10414 cu->language = pretend_language;
10415 cu->language_defn = language_def (cu->language);
10416
10417 /* Do line number decoding in read_file_scope () */
10418 process_die (cu->dies, cu);
10419
10420 /* For now fudge the Go package. */
10421 if (cu->language == language_go)
10422 fixup_go_packaging (cu);
10423
10424 /* Now that we have processed all the DIEs in the CU, all the types
10425 should be complete, and it should now be safe to compute all of the
10426 physnames. */
10427 compute_delayed_physnames (cu);
10428
10429 if (cu->language == language_rust)
10430 rust_union_quirks (cu);
10431
10432 /* Some compilers don't define a DW_AT_high_pc attribute for the
10433 compilation unit. If the DW_AT_high_pc is missing, synthesize
10434 it, by scanning the DIE's below the compilation unit. */
10435 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10436
10437 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10438 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10439
10440 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10441 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10442 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10443 addrmap to help ensure it has an accurate map of pc values belonging to
10444 this comp unit. */
10445 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10446
10447 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10448 SECT_OFF_TEXT (objfile),
10449 0);
10450
10451 if (cust != NULL)
10452 {
10453 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10454
10455 /* Set symtab language to language from DW_AT_language. If the
10456 compilation is from a C file generated by language preprocessors, do
10457 not set the language if it was already deduced by start_subfile. */
10458 if (!(cu->language == language_c
10459 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10460 COMPUNIT_FILETABS (cust)->language = cu->language;
10461
10462 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10463 produce DW_AT_location with location lists but it can be possibly
10464 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10465 there were bugs in prologue debug info, fixed later in GCC-4.5
10466 by "unwind info for epilogues" patch (which is not directly related).
10467
10468 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10469 needed, it would be wrong due to missing DW_AT_producer there.
10470
10471 Still one can confuse GDB by using non-standard GCC compilation
10472 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10473 */
10474 if (cu->has_loclist && gcc_4_minor >= 5)
10475 cust->locations_valid = 1;
10476
10477 if (gcc_4_minor >= 5)
10478 cust->epilogue_unwind_valid = 1;
10479
10480 cust->call_site_htab = cu->call_site_htab;
10481 }
10482
10483 if (dwarf2_per_objfile->using_index)
10484 per_cu->v.quick->compunit_symtab = cust;
10485 else
10486 {
10487 struct partial_symtab *pst = per_cu->v.psymtab;
10488 pst->compunit_symtab = cust;
10489 pst->readin = 1;
10490 }
10491
10492 /* Push it for inclusion processing later. */
10493 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10494
10495 /* Not needed any more. */
10496 cu->reset_builder ();
10497 }
10498
10499 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10500 already been loaded into memory. */
10501
10502 static void
10503 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10504 enum language pretend_language)
10505 {
10506 struct dwarf2_cu *cu = per_cu->cu;
10507 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10508 struct objfile *objfile = dwarf2_per_objfile->objfile;
10509 struct compunit_symtab *cust;
10510 struct signatured_type *sig_type;
10511
10512 gdb_assert (per_cu->is_debug_types);
10513 sig_type = (struct signatured_type *) per_cu;
10514
10515 /* Clear the list here in case something was left over. */
10516 cu->method_list.clear ();
10517
10518 cu->language = pretend_language;
10519 cu->language_defn = language_def (cu->language);
10520
10521 /* The symbol tables are set up in read_type_unit_scope. */
10522 process_die (cu->dies, cu);
10523
10524 /* For now fudge the Go package. */
10525 if (cu->language == language_go)
10526 fixup_go_packaging (cu);
10527
10528 /* Now that we have processed all the DIEs in the CU, all the types
10529 should be complete, and it should now be safe to compute all of the
10530 physnames. */
10531 compute_delayed_physnames (cu);
10532
10533 if (cu->language == language_rust)
10534 rust_union_quirks (cu);
10535
10536 /* TUs share symbol tables.
10537 If this is the first TU to use this symtab, complete the construction
10538 of it with end_expandable_symtab. Otherwise, complete the addition of
10539 this TU's symbols to the existing symtab. */
10540 if (sig_type->type_unit_group->compunit_symtab == NULL)
10541 {
10542 buildsym_compunit *builder = cu->get_builder ();
10543 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10544 sig_type->type_unit_group->compunit_symtab = cust;
10545
10546 if (cust != NULL)
10547 {
10548 /* Set symtab language to language from DW_AT_language. If the
10549 compilation is from a C file generated by language preprocessors,
10550 do not set the language if it was already deduced by
10551 start_subfile. */
10552 if (!(cu->language == language_c
10553 && COMPUNIT_FILETABS (cust)->language != language_c))
10554 COMPUNIT_FILETABS (cust)->language = cu->language;
10555 }
10556 }
10557 else
10558 {
10559 cu->get_builder ()->augment_type_symtab ();
10560 cust = sig_type->type_unit_group->compunit_symtab;
10561 }
10562
10563 if (dwarf2_per_objfile->using_index)
10564 per_cu->v.quick->compunit_symtab = cust;
10565 else
10566 {
10567 struct partial_symtab *pst = per_cu->v.psymtab;
10568 pst->compunit_symtab = cust;
10569 pst->readin = 1;
10570 }
10571
10572 /* Not needed any more. */
10573 cu->reset_builder ();
10574 }
10575
10576 /* Process an imported unit DIE. */
10577
10578 static void
10579 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10580 {
10581 struct attribute *attr;
10582
10583 /* For now we don't handle imported units in type units. */
10584 if (cu->per_cu->is_debug_types)
10585 {
10586 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10587 " supported in type units [in module %s]"),
10588 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10589 }
10590
10591 attr = dwarf2_attr (die, DW_AT_import, cu);
10592 if (attr != NULL)
10593 {
10594 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10595 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10596 dwarf2_per_cu_data *per_cu
10597 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10598 cu->per_cu->dwarf2_per_objfile);
10599
10600 /* If necessary, add it to the queue and load its DIEs. */
10601 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10602 load_full_comp_unit (per_cu, false, cu->language);
10603
10604 cu->per_cu->imported_symtabs_push (per_cu);
10605 }
10606 }
10607
10608 /* RAII object that represents a process_die scope: i.e.,
10609 starts/finishes processing a DIE. */
10610 class process_die_scope
10611 {
10612 public:
10613 process_die_scope (die_info *die, dwarf2_cu *cu)
10614 : m_die (die), m_cu (cu)
10615 {
10616 /* We should only be processing DIEs not already in process. */
10617 gdb_assert (!m_die->in_process);
10618 m_die->in_process = true;
10619 }
10620
10621 ~process_die_scope ()
10622 {
10623 m_die->in_process = false;
10624
10625 /* If we're done processing the DIE for the CU that owns the line
10626 header, we don't need the line header anymore. */
10627 if (m_cu->line_header_die_owner == m_die)
10628 {
10629 delete m_cu->line_header;
10630 m_cu->line_header = NULL;
10631 m_cu->line_header_die_owner = NULL;
10632 }
10633 }
10634
10635 private:
10636 die_info *m_die;
10637 dwarf2_cu *m_cu;
10638 };
10639
10640 /* Process a die and its children. */
10641
10642 static void
10643 process_die (struct die_info *die, struct dwarf2_cu *cu)
10644 {
10645 process_die_scope scope (die, cu);
10646
10647 switch (die->tag)
10648 {
10649 case DW_TAG_padding:
10650 break;
10651 case DW_TAG_compile_unit:
10652 case DW_TAG_partial_unit:
10653 read_file_scope (die, cu);
10654 break;
10655 case DW_TAG_type_unit:
10656 read_type_unit_scope (die, cu);
10657 break;
10658 case DW_TAG_subprogram:
10659 /* Nested subprograms in Fortran get a prefix. */
10660 if (cu->language == language_fortran
10661 && die->parent != NULL
10662 && die->parent->tag == DW_TAG_subprogram)
10663 cu->processing_has_namespace_info = true;
10664 /* Fall through. */
10665 case DW_TAG_inlined_subroutine:
10666 read_func_scope (die, cu);
10667 break;
10668 case DW_TAG_lexical_block:
10669 case DW_TAG_try_block:
10670 case DW_TAG_catch_block:
10671 read_lexical_block_scope (die, cu);
10672 break;
10673 case DW_TAG_call_site:
10674 case DW_TAG_GNU_call_site:
10675 read_call_site_scope (die, cu);
10676 break;
10677 case DW_TAG_class_type:
10678 case DW_TAG_interface_type:
10679 case DW_TAG_structure_type:
10680 case DW_TAG_union_type:
10681 process_structure_scope (die, cu);
10682 break;
10683 case DW_TAG_enumeration_type:
10684 process_enumeration_scope (die, cu);
10685 break;
10686
10687 /* These dies have a type, but processing them does not create
10688 a symbol or recurse to process the children. Therefore we can
10689 read them on-demand through read_type_die. */
10690 case DW_TAG_subroutine_type:
10691 case DW_TAG_set_type:
10692 case DW_TAG_array_type:
10693 case DW_TAG_pointer_type:
10694 case DW_TAG_ptr_to_member_type:
10695 case DW_TAG_reference_type:
10696 case DW_TAG_rvalue_reference_type:
10697 case DW_TAG_string_type:
10698 break;
10699
10700 case DW_TAG_base_type:
10701 case DW_TAG_subrange_type:
10702 case DW_TAG_typedef:
10703 /* Add a typedef symbol for the type definition, if it has a
10704 DW_AT_name. */
10705 new_symbol (die, read_type_die (die, cu), cu);
10706 break;
10707 case DW_TAG_common_block:
10708 read_common_block (die, cu);
10709 break;
10710 case DW_TAG_common_inclusion:
10711 break;
10712 case DW_TAG_namespace:
10713 cu->processing_has_namespace_info = true;
10714 read_namespace (die, cu);
10715 break;
10716 case DW_TAG_module:
10717 cu->processing_has_namespace_info = true;
10718 read_module (die, cu);
10719 break;
10720 case DW_TAG_imported_declaration:
10721 cu->processing_has_namespace_info = true;
10722 if (read_namespace_alias (die, cu))
10723 break;
10724 /* The declaration is not a global namespace alias. */
10725 /* Fall through. */
10726 case DW_TAG_imported_module:
10727 cu->processing_has_namespace_info = true;
10728 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10729 || cu->language != language_fortran))
10730 complaint (_("Tag '%s' has unexpected children"),
10731 dwarf_tag_name (die->tag));
10732 read_import_statement (die, cu);
10733 break;
10734
10735 case DW_TAG_imported_unit:
10736 process_imported_unit_die (die, cu);
10737 break;
10738
10739 case DW_TAG_variable:
10740 read_variable (die, cu);
10741 break;
10742
10743 default:
10744 new_symbol (die, NULL, cu);
10745 break;
10746 }
10747 }
10748 \f
10749 /* DWARF name computation. */
10750
10751 /* A helper function for dwarf2_compute_name which determines whether DIE
10752 needs to have the name of the scope prepended to the name listed in the
10753 die. */
10754
10755 static int
10756 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10757 {
10758 struct attribute *attr;
10759
10760 switch (die->tag)
10761 {
10762 case DW_TAG_namespace:
10763 case DW_TAG_typedef:
10764 case DW_TAG_class_type:
10765 case DW_TAG_interface_type:
10766 case DW_TAG_structure_type:
10767 case DW_TAG_union_type:
10768 case DW_TAG_enumeration_type:
10769 case DW_TAG_enumerator:
10770 case DW_TAG_subprogram:
10771 case DW_TAG_inlined_subroutine:
10772 case DW_TAG_member:
10773 case DW_TAG_imported_declaration:
10774 return 1;
10775
10776 case DW_TAG_variable:
10777 case DW_TAG_constant:
10778 /* We only need to prefix "globally" visible variables. These include
10779 any variable marked with DW_AT_external or any variable that
10780 lives in a namespace. [Variables in anonymous namespaces
10781 require prefixing, but they are not DW_AT_external.] */
10782
10783 if (dwarf2_attr (die, DW_AT_specification, cu))
10784 {
10785 struct dwarf2_cu *spec_cu = cu;
10786
10787 return die_needs_namespace (die_specification (die, &spec_cu),
10788 spec_cu);
10789 }
10790
10791 attr = dwarf2_attr (die, DW_AT_external, cu);
10792 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10793 && die->parent->tag != DW_TAG_module)
10794 return 0;
10795 /* A variable in a lexical block of some kind does not need a
10796 namespace, even though in C++ such variables may be external
10797 and have a mangled name. */
10798 if (die->parent->tag == DW_TAG_lexical_block
10799 || die->parent->tag == DW_TAG_try_block
10800 || die->parent->tag == DW_TAG_catch_block
10801 || die->parent->tag == DW_TAG_subprogram)
10802 return 0;
10803 return 1;
10804
10805 default:
10806 return 0;
10807 }
10808 }
10809
10810 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10811 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10812 defined for the given DIE. */
10813
10814 static struct attribute *
10815 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10816 {
10817 struct attribute *attr;
10818
10819 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10820 if (attr == NULL)
10821 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10822
10823 return attr;
10824 }
10825
10826 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10827 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10828 defined for the given DIE. */
10829
10830 static const char *
10831 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10832 {
10833 const char *linkage_name;
10834
10835 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10836 if (linkage_name == NULL)
10837 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10838
10839 return linkage_name;
10840 }
10841
10842 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10843 compute the physname for the object, which include a method's:
10844 - formal parameters (C++),
10845 - receiver type (Go),
10846
10847 The term "physname" is a bit confusing.
10848 For C++, for example, it is the demangled name.
10849 For Go, for example, it's the mangled name.
10850
10851 For Ada, return the DIE's linkage name rather than the fully qualified
10852 name. PHYSNAME is ignored..
10853
10854 The result is allocated on the objfile_obstack and canonicalized. */
10855
10856 static const char *
10857 dwarf2_compute_name (const char *name,
10858 struct die_info *die, struct dwarf2_cu *cu,
10859 int physname)
10860 {
10861 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10862
10863 if (name == NULL)
10864 name = dwarf2_name (die, cu);
10865
10866 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10867 but otherwise compute it by typename_concat inside GDB.
10868 FIXME: Actually this is not really true, or at least not always true.
10869 It's all very confusing. compute_and_set_names doesn't try to demangle
10870 Fortran names because there is no mangling standard. So new_symbol
10871 will set the demangled name to the result of dwarf2_full_name, and it is
10872 the demangled name that GDB uses if it exists. */
10873 if (cu->language == language_ada
10874 || (cu->language == language_fortran && physname))
10875 {
10876 /* For Ada unit, we prefer the linkage name over the name, as
10877 the former contains the exported name, which the user expects
10878 to be able to reference. Ideally, we want the user to be able
10879 to reference this entity using either natural or linkage name,
10880 but we haven't started looking at this enhancement yet. */
10881 const char *linkage_name = dw2_linkage_name (die, cu);
10882
10883 if (linkage_name != NULL)
10884 return linkage_name;
10885 }
10886
10887 /* These are the only languages we know how to qualify names in. */
10888 if (name != NULL
10889 && (cu->language == language_cplus
10890 || cu->language == language_fortran || cu->language == language_d
10891 || cu->language == language_rust))
10892 {
10893 if (die_needs_namespace (die, cu))
10894 {
10895 const char *prefix;
10896 const char *canonical_name = NULL;
10897
10898 string_file buf;
10899
10900 prefix = determine_prefix (die, cu);
10901 if (*prefix != '\0')
10902 {
10903 gdb::unique_xmalloc_ptr<char> prefixed_name
10904 (typename_concat (NULL, prefix, name, physname, cu));
10905
10906 buf.puts (prefixed_name.get ());
10907 }
10908 else
10909 buf.puts (name);
10910
10911 /* Template parameters may be specified in the DIE's DW_AT_name, or
10912 as children with DW_TAG_template_type_param or
10913 DW_TAG_value_type_param. If the latter, add them to the name
10914 here. If the name already has template parameters, then
10915 skip this step; some versions of GCC emit both, and
10916 it is more efficient to use the pre-computed name.
10917
10918 Something to keep in mind about this process: it is very
10919 unlikely, or in some cases downright impossible, to produce
10920 something that will match the mangled name of a function.
10921 If the definition of the function has the same debug info,
10922 we should be able to match up with it anyway. But fallbacks
10923 using the minimal symbol, for instance to find a method
10924 implemented in a stripped copy of libstdc++, will not work.
10925 If we do not have debug info for the definition, we will have to
10926 match them up some other way.
10927
10928 When we do name matching there is a related problem with function
10929 templates; two instantiated function templates are allowed to
10930 differ only by their return types, which we do not add here. */
10931
10932 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10933 {
10934 struct attribute *attr;
10935 struct die_info *child;
10936 int first = 1;
10937
10938 die->building_fullname = 1;
10939
10940 for (child = die->child; child != NULL; child = child->sibling)
10941 {
10942 struct type *type;
10943 LONGEST value;
10944 const gdb_byte *bytes;
10945 struct dwarf2_locexpr_baton *baton;
10946 struct value *v;
10947
10948 if (child->tag != DW_TAG_template_type_param
10949 && child->tag != DW_TAG_template_value_param)
10950 continue;
10951
10952 if (first)
10953 {
10954 buf.puts ("<");
10955 first = 0;
10956 }
10957 else
10958 buf.puts (", ");
10959
10960 attr = dwarf2_attr (child, DW_AT_type, cu);
10961 if (attr == NULL)
10962 {
10963 complaint (_("template parameter missing DW_AT_type"));
10964 buf.puts ("UNKNOWN_TYPE");
10965 continue;
10966 }
10967 type = die_type (child, cu);
10968
10969 if (child->tag == DW_TAG_template_type_param)
10970 {
10971 c_print_type (type, "", &buf, -1, 0, cu->language,
10972 &type_print_raw_options);
10973 continue;
10974 }
10975
10976 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10977 if (attr == NULL)
10978 {
10979 complaint (_("template parameter missing "
10980 "DW_AT_const_value"));
10981 buf.puts ("UNKNOWN_VALUE");
10982 continue;
10983 }
10984
10985 dwarf2_const_value_attr (attr, type, name,
10986 &cu->comp_unit_obstack, cu,
10987 &value, &bytes, &baton);
10988
10989 if (TYPE_NOSIGN (type))
10990 /* GDB prints characters as NUMBER 'CHAR'. If that's
10991 changed, this can use value_print instead. */
10992 c_printchar (value, type, &buf);
10993 else
10994 {
10995 struct value_print_options opts;
10996
10997 if (baton != NULL)
10998 v = dwarf2_evaluate_loc_desc (type, NULL,
10999 baton->data,
11000 baton->size,
11001 baton->per_cu);
11002 else if (bytes != NULL)
11003 {
11004 v = allocate_value (type);
11005 memcpy (value_contents_writeable (v), bytes,
11006 TYPE_LENGTH (type));
11007 }
11008 else
11009 v = value_from_longest (type, value);
11010
11011 /* Specify decimal so that we do not depend on
11012 the radix. */
11013 get_formatted_print_options (&opts, 'd');
11014 opts.raw = 1;
11015 value_print (v, &buf, &opts);
11016 release_value (v);
11017 }
11018 }
11019
11020 die->building_fullname = 0;
11021
11022 if (!first)
11023 {
11024 /* Close the argument list, with a space if necessary
11025 (nested templates). */
11026 if (!buf.empty () && buf.string ().back () == '>')
11027 buf.puts (" >");
11028 else
11029 buf.puts (">");
11030 }
11031 }
11032
11033 /* For C++ methods, append formal parameter type
11034 information, if PHYSNAME. */
11035
11036 if (physname && die->tag == DW_TAG_subprogram
11037 && cu->language == language_cplus)
11038 {
11039 struct type *type = read_type_die (die, cu);
11040
11041 c_type_print_args (type, &buf, 1, cu->language,
11042 &type_print_raw_options);
11043
11044 if (cu->language == language_cplus)
11045 {
11046 /* Assume that an artificial first parameter is
11047 "this", but do not crash if it is not. RealView
11048 marks unnamed (and thus unused) parameters as
11049 artificial; there is no way to differentiate
11050 the two cases. */
11051 if (TYPE_NFIELDS (type) > 0
11052 && TYPE_FIELD_ARTIFICIAL (type, 0)
11053 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11054 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11055 0))))
11056 buf.puts (" const");
11057 }
11058 }
11059
11060 const std::string &intermediate_name = buf.string ();
11061
11062 if (cu->language == language_cplus)
11063 canonical_name
11064 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11065 &objfile->per_bfd->storage_obstack);
11066
11067 /* If we only computed INTERMEDIATE_NAME, or if
11068 INTERMEDIATE_NAME is already canonical, then we need to
11069 copy it to the appropriate obstack. */
11070 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11071 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11072 intermediate_name);
11073 else
11074 name = canonical_name;
11075 }
11076 }
11077
11078 return name;
11079 }
11080
11081 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11082 If scope qualifiers are appropriate they will be added. The result
11083 will be allocated on the storage_obstack, or NULL if the DIE does
11084 not have a name. NAME may either be from a previous call to
11085 dwarf2_name or NULL.
11086
11087 The output string will be canonicalized (if C++). */
11088
11089 static const char *
11090 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11091 {
11092 return dwarf2_compute_name (name, die, cu, 0);
11093 }
11094
11095 /* Construct a physname for the given DIE in CU. NAME may either be
11096 from a previous call to dwarf2_name or NULL. The result will be
11097 allocated on the objfile_objstack or NULL if the DIE does not have a
11098 name.
11099
11100 The output string will be canonicalized (if C++). */
11101
11102 static const char *
11103 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11104 {
11105 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11106 const char *retval, *mangled = NULL, *canon = NULL;
11107 int need_copy = 1;
11108
11109 /* In this case dwarf2_compute_name is just a shortcut not building anything
11110 on its own. */
11111 if (!die_needs_namespace (die, cu))
11112 return dwarf2_compute_name (name, die, cu, 1);
11113
11114 mangled = dw2_linkage_name (die, cu);
11115
11116 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11117 See https://github.com/rust-lang/rust/issues/32925. */
11118 if (cu->language == language_rust && mangled != NULL
11119 && strchr (mangled, '{') != NULL)
11120 mangled = NULL;
11121
11122 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11123 has computed. */
11124 gdb::unique_xmalloc_ptr<char> demangled;
11125 if (mangled != NULL)
11126 {
11127
11128 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11129 {
11130 /* Do nothing (do not demangle the symbol name). */
11131 }
11132 else if (cu->language == language_go)
11133 {
11134 /* This is a lie, but we already lie to the caller new_symbol.
11135 new_symbol assumes we return the mangled name.
11136 This just undoes that lie until things are cleaned up. */
11137 }
11138 else
11139 {
11140 /* Use DMGL_RET_DROP for C++ template functions to suppress
11141 their return type. It is easier for GDB users to search
11142 for such functions as `name(params)' than `long name(params)'.
11143 In such case the minimal symbol names do not match the full
11144 symbol names but for template functions there is never a need
11145 to look up their definition from their declaration so
11146 the only disadvantage remains the minimal symbol variant
11147 `long name(params)' does not have the proper inferior type. */
11148 demangled.reset (gdb_demangle (mangled,
11149 (DMGL_PARAMS | DMGL_ANSI
11150 | DMGL_RET_DROP)));
11151 }
11152 if (demangled)
11153 canon = demangled.get ();
11154 else
11155 {
11156 canon = mangled;
11157 need_copy = 0;
11158 }
11159 }
11160
11161 if (canon == NULL || check_physname)
11162 {
11163 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11164
11165 if (canon != NULL && strcmp (physname, canon) != 0)
11166 {
11167 /* It may not mean a bug in GDB. The compiler could also
11168 compute DW_AT_linkage_name incorrectly. But in such case
11169 GDB would need to be bug-to-bug compatible. */
11170
11171 complaint (_("Computed physname <%s> does not match demangled <%s> "
11172 "(from linkage <%s>) - DIE at %s [in module %s]"),
11173 physname, canon, mangled, sect_offset_str (die->sect_off),
11174 objfile_name (objfile));
11175
11176 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11177 is available here - over computed PHYSNAME. It is safer
11178 against both buggy GDB and buggy compilers. */
11179
11180 retval = canon;
11181 }
11182 else
11183 {
11184 retval = physname;
11185 need_copy = 0;
11186 }
11187 }
11188 else
11189 retval = canon;
11190
11191 if (need_copy)
11192 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
11193
11194 return retval;
11195 }
11196
11197 /* Inspect DIE in CU for a namespace alias. If one exists, record
11198 a new symbol for it.
11199
11200 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11201
11202 static int
11203 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11204 {
11205 struct attribute *attr;
11206
11207 /* If the die does not have a name, this is not a namespace
11208 alias. */
11209 attr = dwarf2_attr (die, DW_AT_name, cu);
11210 if (attr != NULL)
11211 {
11212 int num;
11213 struct die_info *d = die;
11214 struct dwarf2_cu *imported_cu = cu;
11215
11216 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11217 keep inspecting DIEs until we hit the underlying import. */
11218 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11219 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11220 {
11221 attr = dwarf2_attr (d, DW_AT_import, cu);
11222 if (attr == NULL)
11223 break;
11224
11225 d = follow_die_ref (d, attr, &imported_cu);
11226 if (d->tag != DW_TAG_imported_declaration)
11227 break;
11228 }
11229
11230 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11231 {
11232 complaint (_("DIE at %s has too many recursively imported "
11233 "declarations"), sect_offset_str (d->sect_off));
11234 return 0;
11235 }
11236
11237 if (attr != NULL)
11238 {
11239 struct type *type;
11240 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11241
11242 type = get_die_type_at_offset (sect_off, cu->per_cu);
11243 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11244 {
11245 /* This declaration is a global namespace alias. Add
11246 a symbol for it whose type is the aliased namespace. */
11247 new_symbol (die, type, cu);
11248 return 1;
11249 }
11250 }
11251 }
11252
11253 return 0;
11254 }
11255
11256 /* Return the using directives repository (global or local?) to use in the
11257 current context for CU.
11258
11259 For Ada, imported declarations can materialize renamings, which *may* be
11260 global. However it is impossible (for now?) in DWARF to distinguish
11261 "external" imported declarations and "static" ones. As all imported
11262 declarations seem to be static in all other languages, make them all CU-wide
11263 global only in Ada. */
11264
11265 static struct using_direct **
11266 using_directives (struct dwarf2_cu *cu)
11267 {
11268 if (cu->language == language_ada
11269 && cu->get_builder ()->outermost_context_p ())
11270 return cu->get_builder ()->get_global_using_directives ();
11271 else
11272 return cu->get_builder ()->get_local_using_directives ();
11273 }
11274
11275 /* Read the import statement specified by the given die and record it. */
11276
11277 static void
11278 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11279 {
11280 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11281 struct attribute *import_attr;
11282 struct die_info *imported_die, *child_die;
11283 struct dwarf2_cu *imported_cu;
11284 const char *imported_name;
11285 const char *imported_name_prefix;
11286 const char *canonical_name;
11287 const char *import_alias;
11288 const char *imported_declaration = NULL;
11289 const char *import_prefix;
11290 std::vector<const char *> excludes;
11291
11292 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11293 if (import_attr == NULL)
11294 {
11295 complaint (_("Tag '%s' has no DW_AT_import"),
11296 dwarf_tag_name (die->tag));
11297 return;
11298 }
11299
11300 imported_cu = cu;
11301 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11302 imported_name = dwarf2_name (imported_die, imported_cu);
11303 if (imported_name == NULL)
11304 {
11305 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11306
11307 The import in the following code:
11308 namespace A
11309 {
11310 typedef int B;
11311 }
11312
11313 int main ()
11314 {
11315 using A::B;
11316 B b;
11317 return b;
11318 }
11319
11320 ...
11321 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11322 <52> DW_AT_decl_file : 1
11323 <53> DW_AT_decl_line : 6
11324 <54> DW_AT_import : <0x75>
11325 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11326 <59> DW_AT_name : B
11327 <5b> DW_AT_decl_file : 1
11328 <5c> DW_AT_decl_line : 2
11329 <5d> DW_AT_type : <0x6e>
11330 ...
11331 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11332 <76> DW_AT_byte_size : 4
11333 <77> DW_AT_encoding : 5 (signed)
11334
11335 imports the wrong die ( 0x75 instead of 0x58 ).
11336 This case will be ignored until the gcc bug is fixed. */
11337 return;
11338 }
11339
11340 /* Figure out the local name after import. */
11341 import_alias = dwarf2_name (die, cu);
11342
11343 /* Figure out where the statement is being imported to. */
11344 import_prefix = determine_prefix (die, cu);
11345
11346 /* Figure out what the scope of the imported die is and prepend it
11347 to the name of the imported die. */
11348 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11349
11350 if (imported_die->tag != DW_TAG_namespace
11351 && imported_die->tag != DW_TAG_module)
11352 {
11353 imported_declaration = imported_name;
11354 canonical_name = imported_name_prefix;
11355 }
11356 else if (strlen (imported_name_prefix) > 0)
11357 canonical_name = obconcat (&objfile->objfile_obstack,
11358 imported_name_prefix,
11359 (cu->language == language_d ? "." : "::"),
11360 imported_name, (char *) NULL);
11361 else
11362 canonical_name = imported_name;
11363
11364 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11365 for (child_die = die->child; child_die && child_die->tag;
11366 child_die = sibling_die (child_die))
11367 {
11368 /* DWARF-4: A Fortran use statement with a “rename list” may be
11369 represented by an imported module entry with an import attribute
11370 referring to the module and owned entries corresponding to those
11371 entities that are renamed as part of being imported. */
11372
11373 if (child_die->tag != DW_TAG_imported_declaration)
11374 {
11375 complaint (_("child DW_TAG_imported_declaration expected "
11376 "- DIE at %s [in module %s]"),
11377 sect_offset_str (child_die->sect_off),
11378 objfile_name (objfile));
11379 continue;
11380 }
11381
11382 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11383 if (import_attr == NULL)
11384 {
11385 complaint (_("Tag '%s' has no DW_AT_import"),
11386 dwarf_tag_name (child_die->tag));
11387 continue;
11388 }
11389
11390 imported_cu = cu;
11391 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11392 &imported_cu);
11393 imported_name = dwarf2_name (imported_die, imported_cu);
11394 if (imported_name == NULL)
11395 {
11396 complaint (_("child DW_TAG_imported_declaration has unknown "
11397 "imported name - DIE at %s [in module %s]"),
11398 sect_offset_str (child_die->sect_off),
11399 objfile_name (objfile));
11400 continue;
11401 }
11402
11403 excludes.push_back (imported_name);
11404
11405 process_die (child_die, cu);
11406 }
11407
11408 add_using_directive (using_directives (cu),
11409 import_prefix,
11410 canonical_name,
11411 import_alias,
11412 imported_declaration,
11413 excludes,
11414 0,
11415 &objfile->objfile_obstack);
11416 }
11417
11418 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11419 types, but gives them a size of zero. Starting with version 14,
11420 ICC is compatible with GCC. */
11421
11422 static bool
11423 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11424 {
11425 if (!cu->checked_producer)
11426 check_producer (cu);
11427
11428 return cu->producer_is_icc_lt_14;
11429 }
11430
11431 /* ICC generates a DW_AT_type for C void functions. This was observed on
11432 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11433 which says that void functions should not have a DW_AT_type. */
11434
11435 static bool
11436 producer_is_icc (struct dwarf2_cu *cu)
11437 {
11438 if (!cu->checked_producer)
11439 check_producer (cu);
11440
11441 return cu->producer_is_icc;
11442 }
11443
11444 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11445 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11446 this, it was first present in GCC release 4.3.0. */
11447
11448 static bool
11449 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11450 {
11451 if (!cu->checked_producer)
11452 check_producer (cu);
11453
11454 return cu->producer_is_gcc_lt_4_3;
11455 }
11456
11457 static file_and_directory
11458 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11459 {
11460 file_and_directory res;
11461
11462 /* Find the filename. Do not use dwarf2_name here, since the filename
11463 is not a source language identifier. */
11464 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11465 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11466
11467 if (res.comp_dir == NULL
11468 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11469 && IS_ABSOLUTE_PATH (res.name))
11470 {
11471 res.comp_dir_storage = ldirname (res.name);
11472 if (!res.comp_dir_storage.empty ())
11473 res.comp_dir = res.comp_dir_storage.c_str ();
11474 }
11475 if (res.comp_dir != NULL)
11476 {
11477 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11478 directory, get rid of it. */
11479 const char *cp = strchr (res.comp_dir, ':');
11480
11481 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11482 res.comp_dir = cp + 1;
11483 }
11484
11485 if (res.name == NULL)
11486 res.name = "<unknown>";
11487
11488 return res;
11489 }
11490
11491 /* Handle DW_AT_stmt_list for a compilation unit.
11492 DIE is the DW_TAG_compile_unit die for CU.
11493 COMP_DIR is the compilation directory. LOWPC is passed to
11494 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11495
11496 static void
11497 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11498 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11499 {
11500 struct dwarf2_per_objfile *dwarf2_per_objfile
11501 = cu->per_cu->dwarf2_per_objfile;
11502 struct objfile *objfile = dwarf2_per_objfile->objfile;
11503 struct attribute *attr;
11504 struct line_header line_header_local;
11505 hashval_t line_header_local_hash;
11506 void **slot;
11507 int decode_mapping;
11508
11509 gdb_assert (! cu->per_cu->is_debug_types);
11510
11511 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11512 if (attr == NULL)
11513 return;
11514
11515 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11516
11517 /* The line header hash table is only created if needed (it exists to
11518 prevent redundant reading of the line table for partial_units).
11519 If we're given a partial_unit, we'll need it. If we're given a
11520 compile_unit, then use the line header hash table if it's already
11521 created, but don't create one just yet. */
11522
11523 if (dwarf2_per_objfile->line_header_hash == NULL
11524 && die->tag == DW_TAG_partial_unit)
11525 {
11526 dwarf2_per_objfile->line_header_hash
11527 = htab_create_alloc_ex (127, line_header_hash_voidp,
11528 line_header_eq_voidp,
11529 free_line_header_voidp,
11530 &objfile->objfile_obstack,
11531 hashtab_obstack_allocate,
11532 dummy_obstack_deallocate);
11533 }
11534
11535 line_header_local.sect_off = line_offset;
11536 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11537 line_header_local_hash = line_header_hash (&line_header_local);
11538 if (dwarf2_per_objfile->line_header_hash != NULL)
11539 {
11540 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11541 &line_header_local,
11542 line_header_local_hash, NO_INSERT);
11543
11544 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11545 is not present in *SLOT (since if there is something in *SLOT then
11546 it will be for a partial_unit). */
11547 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11548 {
11549 gdb_assert (*slot != NULL);
11550 cu->line_header = (struct line_header *) *slot;
11551 return;
11552 }
11553 }
11554
11555 /* dwarf_decode_line_header does not yet provide sufficient information.
11556 We always have to call also dwarf_decode_lines for it. */
11557 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11558 if (lh == NULL)
11559 return;
11560
11561 cu->line_header = lh.release ();
11562 cu->line_header_die_owner = die;
11563
11564 if (dwarf2_per_objfile->line_header_hash == NULL)
11565 slot = NULL;
11566 else
11567 {
11568 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11569 &line_header_local,
11570 line_header_local_hash, INSERT);
11571 gdb_assert (slot != NULL);
11572 }
11573 if (slot != NULL && *slot == NULL)
11574 {
11575 /* This newly decoded line number information unit will be owned
11576 by line_header_hash hash table. */
11577 *slot = cu->line_header;
11578 cu->line_header_die_owner = NULL;
11579 }
11580 else
11581 {
11582 /* We cannot free any current entry in (*slot) as that struct line_header
11583 may be already used by multiple CUs. Create only temporary decoded
11584 line_header for this CU - it may happen at most once for each line
11585 number information unit. And if we're not using line_header_hash
11586 then this is what we want as well. */
11587 gdb_assert (die->tag != DW_TAG_partial_unit);
11588 }
11589 decode_mapping = (die->tag != DW_TAG_partial_unit);
11590 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11591 decode_mapping);
11592
11593 }
11594
11595 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11596
11597 static void
11598 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11599 {
11600 struct dwarf2_per_objfile *dwarf2_per_objfile
11601 = cu->per_cu->dwarf2_per_objfile;
11602 struct objfile *objfile = dwarf2_per_objfile->objfile;
11603 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11604 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11605 CORE_ADDR highpc = ((CORE_ADDR) 0);
11606 struct attribute *attr;
11607 struct die_info *child_die;
11608 CORE_ADDR baseaddr;
11609
11610 prepare_one_comp_unit (cu, die, cu->language);
11611 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11612
11613 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11614
11615 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11616 from finish_block. */
11617 if (lowpc == ((CORE_ADDR) -1))
11618 lowpc = highpc;
11619 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11620
11621 file_and_directory fnd = find_file_and_directory (die, cu);
11622
11623 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11624 standardised yet. As a workaround for the language detection we fall
11625 back to the DW_AT_producer string. */
11626 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11627 cu->language = language_opencl;
11628
11629 /* Similar hack for Go. */
11630 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11631 set_cu_language (DW_LANG_Go, cu);
11632
11633 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11634
11635 /* Decode line number information if present. We do this before
11636 processing child DIEs, so that the line header table is available
11637 for DW_AT_decl_file. */
11638 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11639
11640 /* Process all dies in compilation unit. */
11641 if (die->child != NULL)
11642 {
11643 child_die = die->child;
11644 while (child_die && child_die->tag)
11645 {
11646 process_die (child_die, cu);
11647 child_die = sibling_die (child_die);
11648 }
11649 }
11650
11651 /* Decode macro information, if present. Dwarf 2 macro information
11652 refers to information in the line number info statement program
11653 header, so we can only read it if we've read the header
11654 successfully. */
11655 attr = dwarf2_attr (die, DW_AT_macros, cu);
11656 if (attr == NULL)
11657 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11658 if (attr && cu->line_header)
11659 {
11660 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11661 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11662
11663 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11664 }
11665 else
11666 {
11667 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11668 if (attr && cu->line_header)
11669 {
11670 unsigned int macro_offset = DW_UNSND (attr);
11671
11672 dwarf_decode_macros (cu, macro_offset, 0);
11673 }
11674 }
11675 }
11676
11677 void
11678 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11679 {
11680 struct type_unit_group *tu_group;
11681 int first_time;
11682 struct attribute *attr;
11683 unsigned int i;
11684 struct signatured_type *sig_type;
11685
11686 gdb_assert (per_cu->is_debug_types);
11687 sig_type = (struct signatured_type *) per_cu;
11688
11689 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11690
11691 /* If we're using .gdb_index (includes -readnow) then
11692 per_cu->type_unit_group may not have been set up yet. */
11693 if (sig_type->type_unit_group == NULL)
11694 sig_type->type_unit_group = get_type_unit_group (this, attr);
11695 tu_group = sig_type->type_unit_group;
11696
11697 /* If we've already processed this stmt_list there's no real need to
11698 do it again, we could fake it and just recreate the part we need
11699 (file name,index -> symtab mapping). If data shows this optimization
11700 is useful we can do it then. */
11701 first_time = tu_group->compunit_symtab == NULL;
11702
11703 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11704 debug info. */
11705 line_header_up lh;
11706 if (attr != NULL)
11707 {
11708 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11709 lh = dwarf_decode_line_header (line_offset, this);
11710 }
11711 if (lh == NULL)
11712 {
11713 if (first_time)
11714 start_symtab ("", NULL, 0);
11715 else
11716 {
11717 gdb_assert (tu_group->symtabs == NULL);
11718 gdb_assert (m_builder == nullptr);
11719 struct compunit_symtab *cust = tu_group->compunit_symtab;
11720 m_builder.reset (new struct buildsym_compunit
11721 (COMPUNIT_OBJFILE (cust), "",
11722 COMPUNIT_DIRNAME (cust),
11723 compunit_language (cust),
11724 0, cust));
11725 }
11726 return;
11727 }
11728
11729 line_header = lh.release ();
11730 line_header_die_owner = die;
11731
11732 if (first_time)
11733 {
11734 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11735
11736 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11737 still initializing it, and our caller (a few levels up)
11738 process_full_type_unit still needs to know if this is the first
11739 time. */
11740
11741 tu_group->num_symtabs = line_header->file_names_size ();
11742 tu_group->symtabs = XNEWVEC (struct symtab *,
11743 line_header->file_names_size ());
11744
11745 auto &file_names = line_header->file_names ();
11746 for (i = 0; i < file_names.size (); ++i)
11747 {
11748 file_entry &fe = file_names[i];
11749 dwarf2_start_subfile (this, fe.name,
11750 fe.include_dir (line_header));
11751 buildsym_compunit *b = get_builder ();
11752 if (b->get_current_subfile ()->symtab == NULL)
11753 {
11754 /* NOTE: start_subfile will recognize when it's been
11755 passed a file it has already seen. So we can't
11756 assume there's a simple mapping from
11757 cu->line_header->file_names to subfiles, plus
11758 cu->line_header->file_names may contain dups. */
11759 b->get_current_subfile ()->symtab
11760 = allocate_symtab (cust, b->get_current_subfile ()->name);
11761 }
11762
11763 fe.symtab = b->get_current_subfile ()->symtab;
11764 tu_group->symtabs[i] = fe.symtab;
11765 }
11766 }
11767 else
11768 {
11769 gdb_assert (m_builder == nullptr);
11770 struct compunit_symtab *cust = tu_group->compunit_symtab;
11771 m_builder.reset (new struct buildsym_compunit
11772 (COMPUNIT_OBJFILE (cust), "",
11773 COMPUNIT_DIRNAME (cust),
11774 compunit_language (cust),
11775 0, cust));
11776
11777 auto &file_names = line_header->file_names ();
11778 for (i = 0; i < file_names.size (); ++i)
11779 {
11780 file_entry &fe = file_names[i];
11781 fe.symtab = tu_group->symtabs[i];
11782 }
11783 }
11784
11785 /* The main symtab is allocated last. Type units don't have DW_AT_name
11786 so they don't have a "real" (so to speak) symtab anyway.
11787 There is later code that will assign the main symtab to all symbols
11788 that don't have one. We need to handle the case of a symbol with a
11789 missing symtab (DW_AT_decl_file) anyway. */
11790 }
11791
11792 /* Process DW_TAG_type_unit.
11793 For TUs we want to skip the first top level sibling if it's not the
11794 actual type being defined by this TU. In this case the first top
11795 level sibling is there to provide context only. */
11796
11797 static void
11798 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11799 {
11800 struct die_info *child_die;
11801
11802 prepare_one_comp_unit (cu, die, language_minimal);
11803
11804 /* Initialize (or reinitialize) the machinery for building symtabs.
11805 We do this before processing child DIEs, so that the line header table
11806 is available for DW_AT_decl_file. */
11807 cu->setup_type_unit_groups (die);
11808
11809 if (die->child != NULL)
11810 {
11811 child_die = die->child;
11812 while (child_die && child_die->tag)
11813 {
11814 process_die (child_die, cu);
11815 child_die = sibling_die (child_die);
11816 }
11817 }
11818 }
11819 \f
11820 /* DWO/DWP files.
11821
11822 http://gcc.gnu.org/wiki/DebugFission
11823 http://gcc.gnu.org/wiki/DebugFissionDWP
11824
11825 To simplify handling of both DWO files ("object" files with the DWARF info)
11826 and DWP files (a file with the DWOs packaged up into one file), we treat
11827 DWP files as having a collection of virtual DWO files. */
11828
11829 static hashval_t
11830 hash_dwo_file (const void *item)
11831 {
11832 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11833 hashval_t hash;
11834
11835 hash = htab_hash_string (dwo_file->dwo_name);
11836 if (dwo_file->comp_dir != NULL)
11837 hash += htab_hash_string (dwo_file->comp_dir);
11838 return hash;
11839 }
11840
11841 static int
11842 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11843 {
11844 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11845 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11846
11847 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11848 return 0;
11849 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11850 return lhs->comp_dir == rhs->comp_dir;
11851 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11852 }
11853
11854 /* Allocate a hash table for DWO files. */
11855
11856 static htab_up
11857 allocate_dwo_file_hash_table (struct objfile *objfile)
11858 {
11859 auto delete_dwo_file = [] (void *item)
11860 {
11861 struct dwo_file *dwo_file = (struct dwo_file *) item;
11862
11863 delete dwo_file;
11864 };
11865
11866 return htab_up (htab_create_alloc_ex (41,
11867 hash_dwo_file,
11868 eq_dwo_file,
11869 delete_dwo_file,
11870 &objfile->objfile_obstack,
11871 hashtab_obstack_allocate,
11872 dummy_obstack_deallocate));
11873 }
11874
11875 /* Lookup DWO file DWO_NAME. */
11876
11877 static void **
11878 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11879 const char *dwo_name,
11880 const char *comp_dir)
11881 {
11882 struct dwo_file find_entry;
11883 void **slot;
11884
11885 if (dwarf2_per_objfile->dwo_files == NULL)
11886 dwarf2_per_objfile->dwo_files
11887 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11888
11889 find_entry.dwo_name = dwo_name;
11890 find_entry.comp_dir = comp_dir;
11891 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11892 INSERT);
11893
11894 return slot;
11895 }
11896
11897 static hashval_t
11898 hash_dwo_unit (const void *item)
11899 {
11900 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11901
11902 /* This drops the top 32 bits of the id, but is ok for a hash. */
11903 return dwo_unit->signature;
11904 }
11905
11906 static int
11907 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11908 {
11909 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11910 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11911
11912 /* The signature is assumed to be unique within the DWO file.
11913 So while object file CU dwo_id's always have the value zero,
11914 that's OK, assuming each object file DWO file has only one CU,
11915 and that's the rule for now. */
11916 return lhs->signature == rhs->signature;
11917 }
11918
11919 /* Allocate a hash table for DWO CUs,TUs.
11920 There is one of these tables for each of CUs,TUs for each DWO file. */
11921
11922 static htab_t
11923 allocate_dwo_unit_table (struct objfile *objfile)
11924 {
11925 /* Start out with a pretty small number.
11926 Generally DWO files contain only one CU and maybe some TUs. */
11927 return htab_create_alloc_ex (3,
11928 hash_dwo_unit,
11929 eq_dwo_unit,
11930 NULL,
11931 &objfile->objfile_obstack,
11932 hashtab_obstack_allocate,
11933 dummy_obstack_deallocate);
11934 }
11935
11936 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11937
11938 struct create_dwo_cu_data
11939 {
11940 struct dwo_file *dwo_file;
11941 struct dwo_unit dwo_unit;
11942 };
11943
11944 /* die_reader_func for create_dwo_cu. */
11945
11946 static void
11947 create_dwo_cu_reader (const struct die_reader_specs *reader,
11948 const gdb_byte *info_ptr,
11949 struct die_info *comp_unit_die,
11950 int has_children,
11951 void *datap)
11952 {
11953 struct dwarf2_cu *cu = reader->cu;
11954 sect_offset sect_off = cu->per_cu->sect_off;
11955 struct dwarf2_section_info *section = cu->per_cu->section;
11956 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11957 struct dwo_file *dwo_file = data->dwo_file;
11958 struct dwo_unit *dwo_unit = &data->dwo_unit;
11959
11960 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11961 if (!signature.has_value ())
11962 {
11963 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11964 " its dwo_id [in module %s]"),
11965 sect_offset_str (sect_off), dwo_file->dwo_name);
11966 return;
11967 }
11968
11969 dwo_unit->dwo_file = dwo_file;
11970 dwo_unit->signature = *signature;
11971 dwo_unit->section = section;
11972 dwo_unit->sect_off = sect_off;
11973 dwo_unit->length = cu->per_cu->length;
11974
11975 if (dwarf_read_debug)
11976 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11977 sect_offset_str (sect_off),
11978 hex_string (dwo_unit->signature));
11979 }
11980
11981 /* Create the dwo_units for the CUs in a DWO_FILE.
11982 Note: This function processes DWO files only, not DWP files. */
11983
11984 static void
11985 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11986 struct dwo_file &dwo_file, dwarf2_section_info &section,
11987 htab_t &cus_htab)
11988 {
11989 struct objfile *objfile = dwarf2_per_objfile->objfile;
11990 const gdb_byte *info_ptr, *end_ptr;
11991
11992 dwarf2_read_section (objfile, &section);
11993 info_ptr = section.buffer;
11994
11995 if (info_ptr == NULL)
11996 return;
11997
11998 if (dwarf_read_debug)
11999 {
12000 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
12001 get_section_name (&section),
12002 get_section_file_name (&section));
12003 }
12004
12005 end_ptr = info_ptr + section.size;
12006 while (info_ptr < end_ptr)
12007 {
12008 struct dwarf2_per_cu_data per_cu;
12009 struct create_dwo_cu_data create_dwo_cu_data;
12010 struct dwo_unit *dwo_unit;
12011 void **slot;
12012 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
12013
12014 memset (&create_dwo_cu_data.dwo_unit, 0,
12015 sizeof (create_dwo_cu_data.dwo_unit));
12016 memset (&per_cu, 0, sizeof (per_cu));
12017 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
12018 per_cu.is_debug_types = 0;
12019 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12020 per_cu.section = &section;
12021 create_dwo_cu_data.dwo_file = &dwo_file;
12022
12023 init_cutu_and_read_dies_no_follow (
12024 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12025 info_ptr += per_cu.length;
12026
12027 // If the unit could not be parsed, skip it.
12028 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12029 continue;
12030
12031 if (cus_htab == NULL)
12032 cus_htab = allocate_dwo_unit_table (objfile);
12033
12034 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12035 *dwo_unit = create_dwo_cu_data.dwo_unit;
12036 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12037 gdb_assert (slot != NULL);
12038 if (*slot != NULL)
12039 {
12040 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12041 sect_offset dup_sect_off = dup_cu->sect_off;
12042
12043 complaint (_("debug cu entry at offset %s is duplicate to"
12044 " the entry at offset %s, signature %s"),
12045 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
12046 hex_string (dwo_unit->signature));
12047 }
12048 *slot = (void *)dwo_unit;
12049 }
12050 }
12051
12052 /* DWP file .debug_{cu,tu}_index section format:
12053 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12054
12055 DWP Version 1:
12056
12057 Both index sections have the same format, and serve to map a 64-bit
12058 signature to a set of section numbers. Each section begins with a header,
12059 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12060 indexes, and a pool of 32-bit section numbers. The index sections will be
12061 aligned at 8-byte boundaries in the file.
12062
12063 The index section header consists of:
12064
12065 V, 32 bit version number
12066 -, 32 bits unused
12067 N, 32 bit number of compilation units or type units in the index
12068 M, 32 bit number of slots in the hash table
12069
12070 Numbers are recorded using the byte order of the application binary.
12071
12072 The hash table begins at offset 16 in the section, and consists of an array
12073 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12074 order of the application binary). Unused slots in the hash table are 0.
12075 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12076
12077 The parallel table begins immediately after the hash table
12078 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12079 array of 32-bit indexes (using the byte order of the application binary),
12080 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12081 table contains a 32-bit index into the pool of section numbers. For unused
12082 hash table slots, the corresponding entry in the parallel table will be 0.
12083
12084 The pool of section numbers begins immediately following the hash table
12085 (at offset 16 + 12 * M from the beginning of the section). The pool of
12086 section numbers consists of an array of 32-bit words (using the byte order
12087 of the application binary). Each item in the array is indexed starting
12088 from 0. The hash table entry provides the index of the first section
12089 number in the set. Additional section numbers in the set follow, and the
12090 set is terminated by a 0 entry (section number 0 is not used in ELF).
12091
12092 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12093 section must be the first entry in the set, and the .debug_abbrev.dwo must
12094 be the second entry. Other members of the set may follow in any order.
12095
12096 ---
12097
12098 DWP Version 2:
12099
12100 DWP Version 2 combines all the .debug_info, etc. sections into one,
12101 and the entries in the index tables are now offsets into these sections.
12102 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12103 section.
12104
12105 Index Section Contents:
12106 Header
12107 Hash Table of Signatures dwp_hash_table.hash_table
12108 Parallel Table of Indices dwp_hash_table.unit_table
12109 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12110 Table of Section Sizes dwp_hash_table.v2.sizes
12111
12112 The index section header consists of:
12113
12114 V, 32 bit version number
12115 L, 32 bit number of columns in the table of section offsets
12116 N, 32 bit number of compilation units or type units in the index
12117 M, 32 bit number of slots in the hash table
12118
12119 Numbers are recorded using the byte order of the application binary.
12120
12121 The hash table has the same format as version 1.
12122 The parallel table of indices has the same format as version 1,
12123 except that the entries are origin-1 indices into the table of sections
12124 offsets and the table of section sizes.
12125
12126 The table of offsets begins immediately following the parallel table
12127 (at offset 16 + 12 * M from the beginning of the section). The table is
12128 a two-dimensional array of 32-bit words (using the byte order of the
12129 application binary), with L columns and N+1 rows, in row-major order.
12130 Each row in the array is indexed starting from 0. The first row provides
12131 a key to the remaining rows: each column in this row provides an identifier
12132 for a debug section, and the offsets in the same column of subsequent rows
12133 refer to that section. The section identifiers are:
12134
12135 DW_SECT_INFO 1 .debug_info.dwo
12136 DW_SECT_TYPES 2 .debug_types.dwo
12137 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12138 DW_SECT_LINE 4 .debug_line.dwo
12139 DW_SECT_LOC 5 .debug_loc.dwo
12140 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12141 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12142 DW_SECT_MACRO 8 .debug_macro.dwo
12143
12144 The offsets provided by the CU and TU index sections are the base offsets
12145 for the contributions made by each CU or TU to the corresponding section
12146 in the package file. Each CU and TU header contains an abbrev_offset
12147 field, used to find the abbreviations table for that CU or TU within the
12148 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12149 be interpreted as relative to the base offset given in the index section.
12150 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12151 should be interpreted as relative to the base offset for .debug_line.dwo,
12152 and offsets into other debug sections obtained from DWARF attributes should
12153 also be interpreted as relative to the corresponding base offset.
12154
12155 The table of sizes begins immediately following the table of offsets.
12156 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12157 with L columns and N rows, in row-major order. Each row in the array is
12158 indexed starting from 1 (row 0 is shared by the two tables).
12159
12160 ---
12161
12162 Hash table lookup is handled the same in version 1 and 2:
12163
12164 We assume that N and M will not exceed 2^32 - 1.
12165 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12166
12167 Given a 64-bit compilation unit signature or a type signature S, an entry
12168 in the hash table is located as follows:
12169
12170 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12171 the low-order k bits all set to 1.
12172
12173 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12174
12175 3) If the hash table entry at index H matches the signature, use that
12176 entry. If the hash table entry at index H is unused (all zeroes),
12177 terminate the search: the signature is not present in the table.
12178
12179 4) Let H = (H + H') modulo M. Repeat at Step 3.
12180
12181 Because M > N and H' and M are relatively prime, the search is guaranteed
12182 to stop at an unused slot or find the match. */
12183
12184 /* Create a hash table to map DWO IDs to their CU/TU entry in
12185 .debug_{info,types}.dwo in DWP_FILE.
12186 Returns NULL if there isn't one.
12187 Note: This function processes DWP files only, not DWO files. */
12188
12189 static struct dwp_hash_table *
12190 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12191 struct dwp_file *dwp_file, int is_debug_types)
12192 {
12193 struct objfile *objfile = dwarf2_per_objfile->objfile;
12194 bfd *dbfd = dwp_file->dbfd.get ();
12195 const gdb_byte *index_ptr, *index_end;
12196 struct dwarf2_section_info *index;
12197 uint32_t version, nr_columns, nr_units, nr_slots;
12198 struct dwp_hash_table *htab;
12199
12200 if (is_debug_types)
12201 index = &dwp_file->sections.tu_index;
12202 else
12203 index = &dwp_file->sections.cu_index;
12204
12205 if (dwarf2_section_empty_p (index))
12206 return NULL;
12207 dwarf2_read_section (objfile, index);
12208
12209 index_ptr = index->buffer;
12210 index_end = index_ptr + index->size;
12211
12212 version = read_4_bytes (dbfd, index_ptr);
12213 index_ptr += 4;
12214 if (version == 2)
12215 nr_columns = read_4_bytes (dbfd, index_ptr);
12216 else
12217 nr_columns = 0;
12218 index_ptr += 4;
12219 nr_units = read_4_bytes (dbfd, index_ptr);
12220 index_ptr += 4;
12221 nr_slots = read_4_bytes (dbfd, index_ptr);
12222 index_ptr += 4;
12223
12224 if (version != 1 && version != 2)
12225 {
12226 error (_("Dwarf Error: unsupported DWP file version (%s)"
12227 " [in module %s]"),
12228 pulongest (version), dwp_file->name);
12229 }
12230 if (nr_slots != (nr_slots & -nr_slots))
12231 {
12232 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12233 " is not power of 2 [in module %s]"),
12234 pulongest (nr_slots), dwp_file->name);
12235 }
12236
12237 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12238 htab->version = version;
12239 htab->nr_columns = nr_columns;
12240 htab->nr_units = nr_units;
12241 htab->nr_slots = nr_slots;
12242 htab->hash_table = index_ptr;
12243 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12244
12245 /* Exit early if the table is empty. */
12246 if (nr_slots == 0 || nr_units == 0
12247 || (version == 2 && nr_columns == 0))
12248 {
12249 /* All must be zero. */
12250 if (nr_slots != 0 || nr_units != 0
12251 || (version == 2 && nr_columns != 0))
12252 {
12253 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
12254 " all zero [in modules %s]"),
12255 dwp_file->name);
12256 }
12257 return htab;
12258 }
12259
12260 if (version == 1)
12261 {
12262 htab->section_pool.v1.indices =
12263 htab->unit_table + sizeof (uint32_t) * nr_slots;
12264 /* It's harder to decide whether the section is too small in v1.
12265 V1 is deprecated anyway so we punt. */
12266 }
12267 else
12268 {
12269 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12270 int *ids = htab->section_pool.v2.section_ids;
12271 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
12272 /* Reverse map for error checking. */
12273 int ids_seen[DW_SECT_MAX + 1];
12274 int i;
12275
12276 if (nr_columns < 2)
12277 {
12278 error (_("Dwarf Error: bad DWP hash table, too few columns"
12279 " in section table [in module %s]"),
12280 dwp_file->name);
12281 }
12282 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12283 {
12284 error (_("Dwarf Error: bad DWP hash table, too many columns"
12285 " in section table [in module %s]"),
12286 dwp_file->name);
12287 }
12288 memset (ids, 255, sizeof_ids);
12289 memset (ids_seen, 255, sizeof (ids_seen));
12290 for (i = 0; i < nr_columns; ++i)
12291 {
12292 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12293
12294 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12295 {
12296 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12297 " in section table [in module %s]"),
12298 id, dwp_file->name);
12299 }
12300 if (ids_seen[id] != -1)
12301 {
12302 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12303 " id %d in section table [in module %s]"),
12304 id, dwp_file->name);
12305 }
12306 ids_seen[id] = i;
12307 ids[i] = id;
12308 }
12309 /* Must have exactly one info or types section. */
12310 if (((ids_seen[DW_SECT_INFO] != -1)
12311 + (ids_seen[DW_SECT_TYPES] != -1))
12312 != 1)
12313 {
12314 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12315 " DWO info/types section [in module %s]"),
12316 dwp_file->name);
12317 }
12318 /* Must have an abbrev section. */
12319 if (ids_seen[DW_SECT_ABBREV] == -1)
12320 {
12321 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12322 " section [in module %s]"),
12323 dwp_file->name);
12324 }
12325 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12326 htab->section_pool.v2.sizes =
12327 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12328 * nr_units * nr_columns);
12329 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12330 * nr_units * nr_columns))
12331 > index_end)
12332 {
12333 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12334 " [in module %s]"),
12335 dwp_file->name);
12336 }
12337 }
12338
12339 return htab;
12340 }
12341
12342 /* Update SECTIONS with the data from SECTP.
12343
12344 This function is like the other "locate" section routines that are
12345 passed to bfd_map_over_sections, but in this context the sections to
12346 read comes from the DWP V1 hash table, not the full ELF section table.
12347
12348 The result is non-zero for success, or zero if an error was found. */
12349
12350 static int
12351 locate_v1_virtual_dwo_sections (asection *sectp,
12352 struct virtual_v1_dwo_sections *sections)
12353 {
12354 const struct dwop_section_names *names = &dwop_section_names;
12355
12356 if (section_is_p (sectp->name, &names->abbrev_dwo))
12357 {
12358 /* There can be only one. */
12359 if (sections->abbrev.s.section != NULL)
12360 return 0;
12361 sections->abbrev.s.section = sectp;
12362 sections->abbrev.size = bfd_section_size (sectp);
12363 }
12364 else if (section_is_p (sectp->name, &names->info_dwo)
12365 || section_is_p (sectp->name, &names->types_dwo))
12366 {
12367 /* There can be only one. */
12368 if (sections->info_or_types.s.section != NULL)
12369 return 0;
12370 sections->info_or_types.s.section = sectp;
12371 sections->info_or_types.size = bfd_section_size (sectp);
12372 }
12373 else if (section_is_p (sectp->name, &names->line_dwo))
12374 {
12375 /* There can be only one. */
12376 if (sections->line.s.section != NULL)
12377 return 0;
12378 sections->line.s.section = sectp;
12379 sections->line.size = bfd_section_size (sectp);
12380 }
12381 else if (section_is_p (sectp->name, &names->loc_dwo))
12382 {
12383 /* There can be only one. */
12384 if (sections->loc.s.section != NULL)
12385 return 0;
12386 sections->loc.s.section = sectp;
12387 sections->loc.size = bfd_section_size (sectp);
12388 }
12389 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12390 {
12391 /* There can be only one. */
12392 if (sections->macinfo.s.section != NULL)
12393 return 0;
12394 sections->macinfo.s.section = sectp;
12395 sections->macinfo.size = bfd_section_size (sectp);
12396 }
12397 else if (section_is_p (sectp->name, &names->macro_dwo))
12398 {
12399 /* There can be only one. */
12400 if (sections->macro.s.section != NULL)
12401 return 0;
12402 sections->macro.s.section = sectp;
12403 sections->macro.size = bfd_section_size (sectp);
12404 }
12405 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12406 {
12407 /* There can be only one. */
12408 if (sections->str_offsets.s.section != NULL)
12409 return 0;
12410 sections->str_offsets.s.section = sectp;
12411 sections->str_offsets.size = bfd_section_size (sectp);
12412 }
12413 else
12414 {
12415 /* No other kind of section is valid. */
12416 return 0;
12417 }
12418
12419 return 1;
12420 }
12421
12422 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12423 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12424 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12425 This is for DWP version 1 files. */
12426
12427 static struct dwo_unit *
12428 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12429 struct dwp_file *dwp_file,
12430 uint32_t unit_index,
12431 const char *comp_dir,
12432 ULONGEST signature, int is_debug_types)
12433 {
12434 struct objfile *objfile = dwarf2_per_objfile->objfile;
12435 const struct dwp_hash_table *dwp_htab =
12436 is_debug_types ? dwp_file->tus : dwp_file->cus;
12437 bfd *dbfd = dwp_file->dbfd.get ();
12438 const char *kind = is_debug_types ? "TU" : "CU";
12439 struct dwo_file *dwo_file;
12440 struct dwo_unit *dwo_unit;
12441 struct virtual_v1_dwo_sections sections;
12442 void **dwo_file_slot;
12443 int i;
12444
12445 gdb_assert (dwp_file->version == 1);
12446
12447 if (dwarf_read_debug)
12448 {
12449 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12450 kind,
12451 pulongest (unit_index), hex_string (signature),
12452 dwp_file->name);
12453 }
12454
12455 /* Fetch the sections of this DWO unit.
12456 Put a limit on the number of sections we look for so that bad data
12457 doesn't cause us to loop forever. */
12458
12459 #define MAX_NR_V1_DWO_SECTIONS \
12460 (1 /* .debug_info or .debug_types */ \
12461 + 1 /* .debug_abbrev */ \
12462 + 1 /* .debug_line */ \
12463 + 1 /* .debug_loc */ \
12464 + 1 /* .debug_str_offsets */ \
12465 + 1 /* .debug_macro or .debug_macinfo */ \
12466 + 1 /* trailing zero */)
12467
12468 memset (&sections, 0, sizeof (sections));
12469
12470 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12471 {
12472 asection *sectp;
12473 uint32_t section_nr =
12474 read_4_bytes (dbfd,
12475 dwp_htab->section_pool.v1.indices
12476 + (unit_index + i) * sizeof (uint32_t));
12477
12478 if (section_nr == 0)
12479 break;
12480 if (section_nr >= dwp_file->num_sections)
12481 {
12482 error (_("Dwarf Error: bad DWP hash table, section number too large"
12483 " [in module %s]"),
12484 dwp_file->name);
12485 }
12486
12487 sectp = dwp_file->elf_sections[section_nr];
12488 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12489 {
12490 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12491 " [in module %s]"),
12492 dwp_file->name);
12493 }
12494 }
12495
12496 if (i < 2
12497 || dwarf2_section_empty_p (&sections.info_or_types)
12498 || dwarf2_section_empty_p (&sections.abbrev))
12499 {
12500 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12501 " [in module %s]"),
12502 dwp_file->name);
12503 }
12504 if (i == MAX_NR_V1_DWO_SECTIONS)
12505 {
12506 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12507 " [in module %s]"),
12508 dwp_file->name);
12509 }
12510
12511 /* It's easier for the rest of the code if we fake a struct dwo_file and
12512 have dwo_unit "live" in that. At least for now.
12513
12514 The DWP file can be made up of a random collection of CUs and TUs.
12515 However, for each CU + set of TUs that came from the same original DWO
12516 file, we can combine them back into a virtual DWO file to save space
12517 (fewer struct dwo_file objects to allocate). Remember that for really
12518 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12519
12520 std::string virtual_dwo_name =
12521 string_printf ("virtual-dwo/%d-%d-%d-%d",
12522 get_section_id (&sections.abbrev),
12523 get_section_id (&sections.line),
12524 get_section_id (&sections.loc),
12525 get_section_id (&sections.str_offsets));
12526 /* Can we use an existing virtual DWO file? */
12527 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12528 virtual_dwo_name.c_str (),
12529 comp_dir);
12530 /* Create one if necessary. */
12531 if (*dwo_file_slot == NULL)
12532 {
12533 if (dwarf_read_debug)
12534 {
12535 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12536 virtual_dwo_name.c_str ());
12537 }
12538 dwo_file = new struct dwo_file;
12539 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12540 virtual_dwo_name);
12541 dwo_file->comp_dir = comp_dir;
12542 dwo_file->sections.abbrev = sections.abbrev;
12543 dwo_file->sections.line = sections.line;
12544 dwo_file->sections.loc = sections.loc;
12545 dwo_file->sections.macinfo = sections.macinfo;
12546 dwo_file->sections.macro = sections.macro;
12547 dwo_file->sections.str_offsets = sections.str_offsets;
12548 /* The "str" section is global to the entire DWP file. */
12549 dwo_file->sections.str = dwp_file->sections.str;
12550 /* The info or types section is assigned below to dwo_unit,
12551 there's no need to record it in dwo_file.
12552 Also, we can't simply record type sections in dwo_file because
12553 we record a pointer into the vector in dwo_unit. As we collect more
12554 types we'll grow the vector and eventually have to reallocate space
12555 for it, invalidating all copies of pointers into the previous
12556 contents. */
12557 *dwo_file_slot = dwo_file;
12558 }
12559 else
12560 {
12561 if (dwarf_read_debug)
12562 {
12563 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12564 virtual_dwo_name.c_str ());
12565 }
12566 dwo_file = (struct dwo_file *) *dwo_file_slot;
12567 }
12568
12569 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12570 dwo_unit->dwo_file = dwo_file;
12571 dwo_unit->signature = signature;
12572 dwo_unit->section =
12573 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12574 *dwo_unit->section = sections.info_or_types;
12575 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12576
12577 return dwo_unit;
12578 }
12579
12580 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12581 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12582 piece within that section used by a TU/CU, return a virtual section
12583 of just that piece. */
12584
12585 static struct dwarf2_section_info
12586 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12587 struct dwarf2_section_info *section,
12588 bfd_size_type offset, bfd_size_type size)
12589 {
12590 struct dwarf2_section_info result;
12591 asection *sectp;
12592
12593 gdb_assert (section != NULL);
12594 gdb_assert (!section->is_virtual);
12595
12596 memset (&result, 0, sizeof (result));
12597 result.s.containing_section = section;
12598 result.is_virtual = true;
12599
12600 if (size == 0)
12601 return result;
12602
12603 sectp = get_section_bfd_section (section);
12604
12605 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12606 bounds of the real section. This is a pretty-rare event, so just
12607 flag an error (easier) instead of a warning and trying to cope. */
12608 if (sectp == NULL
12609 || offset + size > bfd_section_size (sectp))
12610 {
12611 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12612 " in section %s [in module %s]"),
12613 sectp ? bfd_section_name (sectp) : "<unknown>",
12614 objfile_name (dwarf2_per_objfile->objfile));
12615 }
12616
12617 result.virtual_offset = offset;
12618 result.size = size;
12619 return result;
12620 }
12621
12622 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12623 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12624 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12625 This is for DWP version 2 files. */
12626
12627 static struct dwo_unit *
12628 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12629 struct dwp_file *dwp_file,
12630 uint32_t unit_index,
12631 const char *comp_dir,
12632 ULONGEST signature, int is_debug_types)
12633 {
12634 struct objfile *objfile = dwarf2_per_objfile->objfile;
12635 const struct dwp_hash_table *dwp_htab =
12636 is_debug_types ? dwp_file->tus : dwp_file->cus;
12637 bfd *dbfd = dwp_file->dbfd.get ();
12638 const char *kind = is_debug_types ? "TU" : "CU";
12639 struct dwo_file *dwo_file;
12640 struct dwo_unit *dwo_unit;
12641 struct virtual_v2_dwo_sections sections;
12642 void **dwo_file_slot;
12643 int i;
12644
12645 gdb_assert (dwp_file->version == 2);
12646
12647 if (dwarf_read_debug)
12648 {
12649 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12650 kind,
12651 pulongest (unit_index), hex_string (signature),
12652 dwp_file->name);
12653 }
12654
12655 /* Fetch the section offsets of this DWO unit. */
12656
12657 memset (&sections, 0, sizeof (sections));
12658
12659 for (i = 0; i < dwp_htab->nr_columns; ++i)
12660 {
12661 uint32_t offset = read_4_bytes (dbfd,
12662 dwp_htab->section_pool.v2.offsets
12663 + (((unit_index - 1) * dwp_htab->nr_columns
12664 + i)
12665 * sizeof (uint32_t)));
12666 uint32_t size = read_4_bytes (dbfd,
12667 dwp_htab->section_pool.v2.sizes
12668 + (((unit_index - 1) * dwp_htab->nr_columns
12669 + i)
12670 * sizeof (uint32_t)));
12671
12672 switch (dwp_htab->section_pool.v2.section_ids[i])
12673 {
12674 case DW_SECT_INFO:
12675 case DW_SECT_TYPES:
12676 sections.info_or_types_offset = offset;
12677 sections.info_or_types_size = size;
12678 break;
12679 case DW_SECT_ABBREV:
12680 sections.abbrev_offset = offset;
12681 sections.abbrev_size = size;
12682 break;
12683 case DW_SECT_LINE:
12684 sections.line_offset = offset;
12685 sections.line_size = size;
12686 break;
12687 case DW_SECT_LOC:
12688 sections.loc_offset = offset;
12689 sections.loc_size = size;
12690 break;
12691 case DW_SECT_STR_OFFSETS:
12692 sections.str_offsets_offset = offset;
12693 sections.str_offsets_size = size;
12694 break;
12695 case DW_SECT_MACINFO:
12696 sections.macinfo_offset = offset;
12697 sections.macinfo_size = size;
12698 break;
12699 case DW_SECT_MACRO:
12700 sections.macro_offset = offset;
12701 sections.macro_size = size;
12702 break;
12703 }
12704 }
12705
12706 /* It's easier for the rest of the code if we fake a struct dwo_file and
12707 have dwo_unit "live" in that. At least for now.
12708
12709 The DWP file can be made up of a random collection of CUs and TUs.
12710 However, for each CU + set of TUs that came from the same original DWO
12711 file, we can combine them back into a virtual DWO file to save space
12712 (fewer struct dwo_file objects to allocate). Remember that for really
12713 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12714
12715 std::string virtual_dwo_name =
12716 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12717 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12718 (long) (sections.line_size ? sections.line_offset : 0),
12719 (long) (sections.loc_size ? sections.loc_offset : 0),
12720 (long) (sections.str_offsets_size
12721 ? sections.str_offsets_offset : 0));
12722 /* Can we use an existing virtual DWO file? */
12723 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12724 virtual_dwo_name.c_str (),
12725 comp_dir);
12726 /* Create one if necessary. */
12727 if (*dwo_file_slot == NULL)
12728 {
12729 if (dwarf_read_debug)
12730 {
12731 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12732 virtual_dwo_name.c_str ());
12733 }
12734 dwo_file = new struct dwo_file;
12735 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12736 virtual_dwo_name);
12737 dwo_file->comp_dir = comp_dir;
12738 dwo_file->sections.abbrev =
12739 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12740 sections.abbrev_offset, sections.abbrev_size);
12741 dwo_file->sections.line =
12742 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12743 sections.line_offset, sections.line_size);
12744 dwo_file->sections.loc =
12745 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12746 sections.loc_offset, sections.loc_size);
12747 dwo_file->sections.macinfo =
12748 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12749 sections.macinfo_offset, sections.macinfo_size);
12750 dwo_file->sections.macro =
12751 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12752 sections.macro_offset, sections.macro_size);
12753 dwo_file->sections.str_offsets =
12754 create_dwp_v2_section (dwarf2_per_objfile,
12755 &dwp_file->sections.str_offsets,
12756 sections.str_offsets_offset,
12757 sections.str_offsets_size);
12758 /* The "str" section is global to the entire DWP file. */
12759 dwo_file->sections.str = dwp_file->sections.str;
12760 /* The info or types section is assigned below to dwo_unit,
12761 there's no need to record it in dwo_file.
12762 Also, we can't simply record type sections in dwo_file because
12763 we record a pointer into the vector in dwo_unit. As we collect more
12764 types we'll grow the vector and eventually have to reallocate space
12765 for it, invalidating all copies of pointers into the previous
12766 contents. */
12767 *dwo_file_slot = dwo_file;
12768 }
12769 else
12770 {
12771 if (dwarf_read_debug)
12772 {
12773 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12774 virtual_dwo_name.c_str ());
12775 }
12776 dwo_file = (struct dwo_file *) *dwo_file_slot;
12777 }
12778
12779 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12780 dwo_unit->dwo_file = dwo_file;
12781 dwo_unit->signature = signature;
12782 dwo_unit->section =
12783 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12784 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12785 is_debug_types
12786 ? &dwp_file->sections.types
12787 : &dwp_file->sections.info,
12788 sections.info_or_types_offset,
12789 sections.info_or_types_size);
12790 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12791
12792 return dwo_unit;
12793 }
12794
12795 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12796 Returns NULL if the signature isn't found. */
12797
12798 static struct dwo_unit *
12799 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12800 struct dwp_file *dwp_file, const char *comp_dir,
12801 ULONGEST signature, int is_debug_types)
12802 {
12803 const struct dwp_hash_table *dwp_htab =
12804 is_debug_types ? dwp_file->tus : dwp_file->cus;
12805 bfd *dbfd = dwp_file->dbfd.get ();
12806 uint32_t mask = dwp_htab->nr_slots - 1;
12807 uint32_t hash = signature & mask;
12808 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12809 unsigned int i;
12810 void **slot;
12811 struct dwo_unit find_dwo_cu;
12812
12813 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12814 find_dwo_cu.signature = signature;
12815 slot = htab_find_slot (is_debug_types
12816 ? dwp_file->loaded_tus
12817 : dwp_file->loaded_cus,
12818 &find_dwo_cu, INSERT);
12819
12820 if (*slot != NULL)
12821 return (struct dwo_unit *) *slot;
12822
12823 /* Use a for loop so that we don't loop forever on bad debug info. */
12824 for (i = 0; i < dwp_htab->nr_slots; ++i)
12825 {
12826 ULONGEST signature_in_table;
12827
12828 signature_in_table =
12829 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12830 if (signature_in_table == signature)
12831 {
12832 uint32_t unit_index =
12833 read_4_bytes (dbfd,
12834 dwp_htab->unit_table + hash * sizeof (uint32_t));
12835
12836 if (dwp_file->version == 1)
12837 {
12838 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12839 dwp_file, unit_index,
12840 comp_dir, signature,
12841 is_debug_types);
12842 }
12843 else
12844 {
12845 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12846 dwp_file, unit_index,
12847 comp_dir, signature,
12848 is_debug_types);
12849 }
12850 return (struct dwo_unit *) *slot;
12851 }
12852 if (signature_in_table == 0)
12853 return NULL;
12854 hash = (hash + hash2) & mask;
12855 }
12856
12857 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12858 " [in module %s]"),
12859 dwp_file->name);
12860 }
12861
12862 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12863 Open the file specified by FILE_NAME and hand it off to BFD for
12864 preliminary analysis. Return a newly initialized bfd *, which
12865 includes a canonicalized copy of FILE_NAME.
12866 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12867 SEARCH_CWD is true if the current directory is to be searched.
12868 It will be searched before debug-file-directory.
12869 If successful, the file is added to the bfd include table of the
12870 objfile's bfd (see gdb_bfd_record_inclusion).
12871 If unable to find/open the file, return NULL.
12872 NOTE: This function is derived from symfile_bfd_open. */
12873
12874 static gdb_bfd_ref_ptr
12875 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12876 const char *file_name, int is_dwp, int search_cwd)
12877 {
12878 int desc;
12879 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12880 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12881 to debug_file_directory. */
12882 const char *search_path;
12883 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12884
12885 gdb::unique_xmalloc_ptr<char> search_path_holder;
12886 if (search_cwd)
12887 {
12888 if (*debug_file_directory != '\0')
12889 {
12890 search_path_holder.reset (concat (".", dirname_separator_string,
12891 debug_file_directory,
12892 (char *) NULL));
12893 search_path = search_path_holder.get ();
12894 }
12895 else
12896 search_path = ".";
12897 }
12898 else
12899 search_path = debug_file_directory;
12900
12901 openp_flags flags = OPF_RETURN_REALPATH;
12902 if (is_dwp)
12903 flags |= OPF_SEARCH_IN_PATH;
12904
12905 gdb::unique_xmalloc_ptr<char> absolute_name;
12906 desc = openp (search_path, flags, file_name,
12907 O_RDONLY | O_BINARY, &absolute_name);
12908 if (desc < 0)
12909 return NULL;
12910
12911 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12912 gnutarget, desc));
12913 if (sym_bfd == NULL)
12914 return NULL;
12915 bfd_set_cacheable (sym_bfd.get (), 1);
12916
12917 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12918 return NULL;
12919
12920 /* Success. Record the bfd as having been included by the objfile's bfd.
12921 This is important because things like demangled_names_hash lives in the
12922 objfile's per_bfd space and may have references to things like symbol
12923 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12924 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12925
12926 return sym_bfd;
12927 }
12928
12929 /* Try to open DWO file FILE_NAME.
12930 COMP_DIR is the DW_AT_comp_dir attribute.
12931 The result is the bfd handle of the file.
12932 If there is a problem finding or opening the file, return NULL.
12933 Upon success, the canonicalized path of the file is stored in the bfd,
12934 same as symfile_bfd_open. */
12935
12936 static gdb_bfd_ref_ptr
12937 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12938 const char *file_name, const char *comp_dir)
12939 {
12940 if (IS_ABSOLUTE_PATH (file_name))
12941 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12942 0 /*is_dwp*/, 0 /*search_cwd*/);
12943
12944 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12945
12946 if (comp_dir != NULL)
12947 {
12948 gdb::unique_xmalloc_ptr<char> path_to_try
12949 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12950
12951 /* NOTE: If comp_dir is a relative path, this will also try the
12952 search path, which seems useful. */
12953 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12954 path_to_try.get (),
12955 0 /*is_dwp*/,
12956 1 /*search_cwd*/));
12957 if (abfd != NULL)
12958 return abfd;
12959 }
12960
12961 /* That didn't work, try debug-file-directory, which, despite its name,
12962 is a list of paths. */
12963
12964 if (*debug_file_directory == '\0')
12965 return NULL;
12966
12967 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12968 0 /*is_dwp*/, 1 /*search_cwd*/);
12969 }
12970
12971 /* This function is mapped across the sections and remembers the offset and
12972 size of each of the DWO debugging sections we are interested in. */
12973
12974 static void
12975 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12976 {
12977 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12978 const struct dwop_section_names *names = &dwop_section_names;
12979
12980 if (section_is_p (sectp->name, &names->abbrev_dwo))
12981 {
12982 dwo_sections->abbrev.s.section = sectp;
12983 dwo_sections->abbrev.size = bfd_section_size (sectp);
12984 }
12985 else if (section_is_p (sectp->name, &names->info_dwo))
12986 {
12987 dwo_sections->info.s.section = sectp;
12988 dwo_sections->info.size = bfd_section_size (sectp);
12989 }
12990 else if (section_is_p (sectp->name, &names->line_dwo))
12991 {
12992 dwo_sections->line.s.section = sectp;
12993 dwo_sections->line.size = bfd_section_size (sectp);
12994 }
12995 else if (section_is_p (sectp->name, &names->loc_dwo))
12996 {
12997 dwo_sections->loc.s.section = sectp;
12998 dwo_sections->loc.size = bfd_section_size (sectp);
12999 }
13000 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13001 {
13002 dwo_sections->macinfo.s.section = sectp;
13003 dwo_sections->macinfo.size = bfd_section_size (sectp);
13004 }
13005 else if (section_is_p (sectp->name, &names->macro_dwo))
13006 {
13007 dwo_sections->macro.s.section = sectp;
13008 dwo_sections->macro.size = bfd_section_size (sectp);
13009 }
13010 else if (section_is_p (sectp->name, &names->str_dwo))
13011 {
13012 dwo_sections->str.s.section = sectp;
13013 dwo_sections->str.size = bfd_section_size (sectp);
13014 }
13015 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13016 {
13017 dwo_sections->str_offsets.s.section = sectp;
13018 dwo_sections->str_offsets.size = bfd_section_size (sectp);
13019 }
13020 else if (section_is_p (sectp->name, &names->types_dwo))
13021 {
13022 struct dwarf2_section_info type_section;
13023
13024 memset (&type_section, 0, sizeof (type_section));
13025 type_section.s.section = sectp;
13026 type_section.size = bfd_section_size (sectp);
13027 dwo_sections->types.push_back (type_section);
13028 }
13029 }
13030
13031 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
13032 by PER_CU. This is for the non-DWP case.
13033 The result is NULL if DWO_NAME can't be found. */
13034
13035 static struct dwo_file *
13036 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13037 const char *dwo_name, const char *comp_dir)
13038 {
13039 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13040
13041 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
13042 if (dbfd == NULL)
13043 {
13044 if (dwarf_read_debug)
13045 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13046 return NULL;
13047 }
13048
13049 dwo_file_up dwo_file (new struct dwo_file);
13050 dwo_file->dwo_name = dwo_name;
13051 dwo_file->comp_dir = comp_dir;
13052 dwo_file->dbfd = std::move (dbfd);
13053
13054 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
13055 &dwo_file->sections);
13056
13057 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13058 dwo_file->cus);
13059
13060 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
13061 dwo_file->sections.types, dwo_file->tus);
13062
13063 if (dwarf_read_debug)
13064 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13065
13066 return dwo_file.release ();
13067 }
13068
13069 /* This function is mapped across the sections and remembers the offset and
13070 size of each of the DWP debugging sections common to version 1 and 2 that
13071 we are interested in. */
13072
13073 static void
13074 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13075 void *dwp_file_ptr)
13076 {
13077 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13078 const struct dwop_section_names *names = &dwop_section_names;
13079 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13080
13081 /* Record the ELF section number for later lookup: this is what the
13082 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13083 gdb_assert (elf_section_nr < dwp_file->num_sections);
13084 dwp_file->elf_sections[elf_section_nr] = sectp;
13085
13086 /* Look for specific sections that we need. */
13087 if (section_is_p (sectp->name, &names->str_dwo))
13088 {
13089 dwp_file->sections.str.s.section = sectp;
13090 dwp_file->sections.str.size = bfd_section_size (sectp);
13091 }
13092 else if (section_is_p (sectp->name, &names->cu_index))
13093 {
13094 dwp_file->sections.cu_index.s.section = sectp;
13095 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
13096 }
13097 else if (section_is_p (sectp->name, &names->tu_index))
13098 {
13099 dwp_file->sections.tu_index.s.section = sectp;
13100 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
13101 }
13102 }
13103
13104 /* This function is mapped across the sections and remembers the offset and
13105 size of each of the DWP version 2 debugging sections that we are interested
13106 in. This is split into a separate function because we don't know if we
13107 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13108
13109 static void
13110 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13111 {
13112 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13113 const struct dwop_section_names *names = &dwop_section_names;
13114 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13115
13116 /* Record the ELF section number for later lookup: this is what the
13117 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13118 gdb_assert (elf_section_nr < dwp_file->num_sections);
13119 dwp_file->elf_sections[elf_section_nr] = sectp;
13120
13121 /* Look for specific sections that we need. */
13122 if (section_is_p (sectp->name, &names->abbrev_dwo))
13123 {
13124 dwp_file->sections.abbrev.s.section = sectp;
13125 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13126 }
13127 else if (section_is_p (sectp->name, &names->info_dwo))
13128 {
13129 dwp_file->sections.info.s.section = sectp;
13130 dwp_file->sections.info.size = bfd_section_size (sectp);
13131 }
13132 else if (section_is_p (sectp->name, &names->line_dwo))
13133 {
13134 dwp_file->sections.line.s.section = sectp;
13135 dwp_file->sections.line.size = bfd_section_size (sectp);
13136 }
13137 else if (section_is_p (sectp->name, &names->loc_dwo))
13138 {
13139 dwp_file->sections.loc.s.section = sectp;
13140 dwp_file->sections.loc.size = bfd_section_size (sectp);
13141 }
13142 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13143 {
13144 dwp_file->sections.macinfo.s.section = sectp;
13145 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
13146 }
13147 else if (section_is_p (sectp->name, &names->macro_dwo))
13148 {
13149 dwp_file->sections.macro.s.section = sectp;
13150 dwp_file->sections.macro.size = bfd_section_size (sectp);
13151 }
13152 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13153 {
13154 dwp_file->sections.str_offsets.s.section = sectp;
13155 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13156 }
13157 else if (section_is_p (sectp->name, &names->types_dwo))
13158 {
13159 dwp_file->sections.types.s.section = sectp;
13160 dwp_file->sections.types.size = bfd_section_size (sectp);
13161 }
13162 }
13163
13164 /* Hash function for dwp_file loaded CUs/TUs. */
13165
13166 static hashval_t
13167 hash_dwp_loaded_cutus (const void *item)
13168 {
13169 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13170
13171 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13172 return dwo_unit->signature;
13173 }
13174
13175 /* Equality function for dwp_file loaded CUs/TUs. */
13176
13177 static int
13178 eq_dwp_loaded_cutus (const void *a, const void *b)
13179 {
13180 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13181 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13182
13183 return dua->signature == dub->signature;
13184 }
13185
13186 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13187
13188 static htab_t
13189 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13190 {
13191 return htab_create_alloc_ex (3,
13192 hash_dwp_loaded_cutus,
13193 eq_dwp_loaded_cutus,
13194 NULL,
13195 &objfile->objfile_obstack,
13196 hashtab_obstack_allocate,
13197 dummy_obstack_deallocate);
13198 }
13199
13200 /* Try to open DWP file FILE_NAME.
13201 The result is the bfd handle of the file.
13202 If there is a problem finding or opening the file, return NULL.
13203 Upon success, the canonicalized path of the file is stored in the bfd,
13204 same as symfile_bfd_open. */
13205
13206 static gdb_bfd_ref_ptr
13207 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13208 const char *file_name)
13209 {
13210 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13211 1 /*is_dwp*/,
13212 1 /*search_cwd*/));
13213 if (abfd != NULL)
13214 return abfd;
13215
13216 /* Work around upstream bug 15652.
13217 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13218 [Whether that's a "bug" is debatable, but it is getting in our way.]
13219 We have no real idea where the dwp file is, because gdb's realpath-ing
13220 of the executable's path may have discarded the needed info.
13221 [IWBN if the dwp file name was recorded in the executable, akin to
13222 .gnu_debuglink, but that doesn't exist yet.]
13223 Strip the directory from FILE_NAME and search again. */
13224 if (*debug_file_directory != '\0')
13225 {
13226 /* Don't implicitly search the current directory here.
13227 If the user wants to search "." to handle this case,
13228 it must be added to debug-file-directory. */
13229 return try_open_dwop_file (dwarf2_per_objfile,
13230 lbasename (file_name), 1 /*is_dwp*/,
13231 0 /*search_cwd*/);
13232 }
13233
13234 return NULL;
13235 }
13236
13237 /* Initialize the use of the DWP file for the current objfile.
13238 By convention the name of the DWP file is ${objfile}.dwp.
13239 The result is NULL if it can't be found. */
13240
13241 static std::unique_ptr<struct dwp_file>
13242 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13243 {
13244 struct objfile *objfile = dwarf2_per_objfile->objfile;
13245
13246 /* Try to find first .dwp for the binary file before any symbolic links
13247 resolving. */
13248
13249 /* If the objfile is a debug file, find the name of the real binary
13250 file and get the name of dwp file from there. */
13251 std::string dwp_name;
13252 if (objfile->separate_debug_objfile_backlink != NULL)
13253 {
13254 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13255 const char *backlink_basename = lbasename (backlink->original_name);
13256
13257 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13258 }
13259 else
13260 dwp_name = objfile->original_name;
13261
13262 dwp_name += ".dwp";
13263
13264 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13265 if (dbfd == NULL
13266 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13267 {
13268 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13269 dwp_name = objfile_name (objfile);
13270 dwp_name += ".dwp";
13271 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13272 }
13273
13274 if (dbfd == NULL)
13275 {
13276 if (dwarf_read_debug)
13277 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13278 return std::unique_ptr<dwp_file> ();
13279 }
13280
13281 const char *name = bfd_get_filename (dbfd.get ());
13282 std::unique_ptr<struct dwp_file> dwp_file
13283 (new struct dwp_file (name, std::move (dbfd)));
13284
13285 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13286 dwp_file->elf_sections =
13287 OBSTACK_CALLOC (&objfile->objfile_obstack,
13288 dwp_file->num_sections, asection *);
13289
13290 bfd_map_over_sections (dwp_file->dbfd.get (),
13291 dwarf2_locate_common_dwp_sections,
13292 dwp_file.get ());
13293
13294 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13295 0);
13296
13297 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13298 1);
13299
13300 /* The DWP file version is stored in the hash table. Oh well. */
13301 if (dwp_file->cus && dwp_file->tus
13302 && dwp_file->cus->version != dwp_file->tus->version)
13303 {
13304 /* Technically speaking, we should try to limp along, but this is
13305 pretty bizarre. We use pulongest here because that's the established
13306 portability solution (e.g, we cannot use %u for uint32_t). */
13307 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13308 " TU version %s [in DWP file %s]"),
13309 pulongest (dwp_file->cus->version),
13310 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13311 }
13312
13313 if (dwp_file->cus)
13314 dwp_file->version = dwp_file->cus->version;
13315 else if (dwp_file->tus)
13316 dwp_file->version = dwp_file->tus->version;
13317 else
13318 dwp_file->version = 2;
13319
13320 if (dwp_file->version == 2)
13321 bfd_map_over_sections (dwp_file->dbfd.get (),
13322 dwarf2_locate_v2_dwp_sections,
13323 dwp_file.get ());
13324
13325 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13326 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13327
13328 if (dwarf_read_debug)
13329 {
13330 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13331 fprintf_unfiltered (gdb_stdlog,
13332 " %s CUs, %s TUs\n",
13333 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13334 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13335 }
13336
13337 return dwp_file;
13338 }
13339
13340 /* Wrapper around open_and_init_dwp_file, only open it once. */
13341
13342 static struct dwp_file *
13343 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13344 {
13345 if (! dwarf2_per_objfile->dwp_checked)
13346 {
13347 dwarf2_per_objfile->dwp_file
13348 = open_and_init_dwp_file (dwarf2_per_objfile);
13349 dwarf2_per_objfile->dwp_checked = 1;
13350 }
13351 return dwarf2_per_objfile->dwp_file.get ();
13352 }
13353
13354 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13355 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13356 or in the DWP file for the objfile, referenced by THIS_UNIT.
13357 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13358 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13359
13360 This is called, for example, when wanting to read a variable with a
13361 complex location. Therefore we don't want to do file i/o for every call.
13362 Therefore we don't want to look for a DWO file on every call.
13363 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13364 then we check if we've already seen DWO_NAME, and only THEN do we check
13365 for a DWO file.
13366
13367 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13368 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13369
13370 static struct dwo_unit *
13371 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13372 const char *dwo_name, const char *comp_dir,
13373 ULONGEST signature, int is_debug_types)
13374 {
13375 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13376 struct objfile *objfile = dwarf2_per_objfile->objfile;
13377 const char *kind = is_debug_types ? "TU" : "CU";
13378 void **dwo_file_slot;
13379 struct dwo_file *dwo_file;
13380 struct dwp_file *dwp_file;
13381
13382 /* First see if there's a DWP file.
13383 If we have a DWP file but didn't find the DWO inside it, don't
13384 look for the original DWO file. It makes gdb behave differently
13385 depending on whether one is debugging in the build tree. */
13386
13387 dwp_file = get_dwp_file (dwarf2_per_objfile);
13388 if (dwp_file != NULL)
13389 {
13390 const struct dwp_hash_table *dwp_htab =
13391 is_debug_types ? dwp_file->tus : dwp_file->cus;
13392
13393 if (dwp_htab != NULL)
13394 {
13395 struct dwo_unit *dwo_cutu =
13396 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13397 signature, is_debug_types);
13398
13399 if (dwo_cutu != NULL)
13400 {
13401 if (dwarf_read_debug)
13402 {
13403 fprintf_unfiltered (gdb_stdlog,
13404 "Virtual DWO %s %s found: @%s\n",
13405 kind, hex_string (signature),
13406 host_address_to_string (dwo_cutu));
13407 }
13408 return dwo_cutu;
13409 }
13410 }
13411 }
13412 else
13413 {
13414 /* No DWP file, look for the DWO file. */
13415
13416 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13417 dwo_name, comp_dir);
13418 if (*dwo_file_slot == NULL)
13419 {
13420 /* Read in the file and build a table of the CUs/TUs it contains. */
13421 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13422 }
13423 /* NOTE: This will be NULL if unable to open the file. */
13424 dwo_file = (struct dwo_file *) *dwo_file_slot;
13425
13426 if (dwo_file != NULL)
13427 {
13428 struct dwo_unit *dwo_cutu = NULL;
13429
13430 if (is_debug_types && dwo_file->tus)
13431 {
13432 struct dwo_unit find_dwo_cutu;
13433
13434 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13435 find_dwo_cutu.signature = signature;
13436 dwo_cutu
13437 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13438 }
13439 else if (!is_debug_types && dwo_file->cus)
13440 {
13441 struct dwo_unit find_dwo_cutu;
13442
13443 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13444 find_dwo_cutu.signature = signature;
13445 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13446 &find_dwo_cutu);
13447 }
13448
13449 if (dwo_cutu != NULL)
13450 {
13451 if (dwarf_read_debug)
13452 {
13453 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13454 kind, dwo_name, hex_string (signature),
13455 host_address_to_string (dwo_cutu));
13456 }
13457 return dwo_cutu;
13458 }
13459 }
13460 }
13461
13462 /* We didn't find it. This could mean a dwo_id mismatch, or
13463 someone deleted the DWO/DWP file, or the search path isn't set up
13464 correctly to find the file. */
13465
13466 if (dwarf_read_debug)
13467 {
13468 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13469 kind, dwo_name, hex_string (signature));
13470 }
13471
13472 /* This is a warning and not a complaint because it can be caused by
13473 pilot error (e.g., user accidentally deleting the DWO). */
13474 {
13475 /* Print the name of the DWP file if we looked there, helps the user
13476 better diagnose the problem. */
13477 std::string dwp_text;
13478
13479 if (dwp_file != NULL)
13480 dwp_text = string_printf (" [in DWP file %s]",
13481 lbasename (dwp_file->name));
13482
13483 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13484 " [in module %s]"),
13485 kind, dwo_name, hex_string (signature),
13486 dwp_text.c_str (),
13487 this_unit->is_debug_types ? "TU" : "CU",
13488 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13489 }
13490 return NULL;
13491 }
13492
13493 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13494 See lookup_dwo_cutu_unit for details. */
13495
13496 static struct dwo_unit *
13497 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13498 const char *dwo_name, const char *comp_dir,
13499 ULONGEST signature)
13500 {
13501 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13502 }
13503
13504 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13505 See lookup_dwo_cutu_unit for details. */
13506
13507 static struct dwo_unit *
13508 lookup_dwo_type_unit (struct signatured_type *this_tu,
13509 const char *dwo_name, const char *comp_dir)
13510 {
13511 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13512 }
13513
13514 /* Traversal function for queue_and_load_all_dwo_tus. */
13515
13516 static int
13517 queue_and_load_dwo_tu (void **slot, void *info)
13518 {
13519 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13520 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13521 ULONGEST signature = dwo_unit->signature;
13522 struct signatured_type *sig_type =
13523 lookup_dwo_signatured_type (per_cu->cu, signature);
13524
13525 if (sig_type != NULL)
13526 {
13527 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13528
13529 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13530 a real dependency of PER_CU on SIG_TYPE. That is detected later
13531 while processing PER_CU. */
13532 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13533 load_full_type_unit (sig_cu);
13534 per_cu->imported_symtabs_push (sig_cu);
13535 }
13536
13537 return 1;
13538 }
13539
13540 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13541 The DWO may have the only definition of the type, though it may not be
13542 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13543 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13544
13545 static void
13546 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13547 {
13548 struct dwo_unit *dwo_unit;
13549 struct dwo_file *dwo_file;
13550
13551 gdb_assert (!per_cu->is_debug_types);
13552 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13553 gdb_assert (per_cu->cu != NULL);
13554
13555 dwo_unit = per_cu->cu->dwo_unit;
13556 gdb_assert (dwo_unit != NULL);
13557
13558 dwo_file = dwo_unit->dwo_file;
13559 if (dwo_file->tus != NULL)
13560 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13561 }
13562
13563 /* Read in various DIEs. */
13564
13565 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13566 Inherit only the children of the DW_AT_abstract_origin DIE not being
13567 already referenced by DW_AT_abstract_origin from the children of the
13568 current DIE. */
13569
13570 static void
13571 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13572 {
13573 struct die_info *child_die;
13574 sect_offset *offsetp;
13575 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13576 struct die_info *origin_die;
13577 /* Iterator of the ORIGIN_DIE children. */
13578 struct die_info *origin_child_die;
13579 struct attribute *attr;
13580 struct dwarf2_cu *origin_cu;
13581 struct pending **origin_previous_list_in_scope;
13582
13583 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13584 if (!attr)
13585 return;
13586
13587 /* Note that following die references may follow to a die in a
13588 different cu. */
13589
13590 origin_cu = cu;
13591 origin_die = follow_die_ref (die, attr, &origin_cu);
13592
13593 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13594 symbols in. */
13595 origin_previous_list_in_scope = origin_cu->list_in_scope;
13596 origin_cu->list_in_scope = cu->list_in_scope;
13597
13598 if (die->tag != origin_die->tag
13599 && !(die->tag == DW_TAG_inlined_subroutine
13600 && origin_die->tag == DW_TAG_subprogram))
13601 complaint (_("DIE %s and its abstract origin %s have different tags"),
13602 sect_offset_str (die->sect_off),
13603 sect_offset_str (origin_die->sect_off));
13604
13605 std::vector<sect_offset> offsets;
13606
13607 for (child_die = die->child;
13608 child_die && child_die->tag;
13609 child_die = sibling_die (child_die))
13610 {
13611 struct die_info *child_origin_die;
13612 struct dwarf2_cu *child_origin_cu;
13613
13614 /* We are trying to process concrete instance entries:
13615 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13616 it's not relevant to our analysis here. i.e. detecting DIEs that are
13617 present in the abstract instance but not referenced in the concrete
13618 one. */
13619 if (child_die->tag == DW_TAG_call_site
13620 || child_die->tag == DW_TAG_GNU_call_site)
13621 continue;
13622
13623 /* For each CHILD_DIE, find the corresponding child of
13624 ORIGIN_DIE. If there is more than one layer of
13625 DW_AT_abstract_origin, follow them all; there shouldn't be,
13626 but GCC versions at least through 4.4 generate this (GCC PR
13627 40573). */
13628 child_origin_die = child_die;
13629 child_origin_cu = cu;
13630 while (1)
13631 {
13632 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13633 child_origin_cu);
13634 if (attr == NULL)
13635 break;
13636 child_origin_die = follow_die_ref (child_origin_die, attr,
13637 &child_origin_cu);
13638 }
13639
13640 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13641 counterpart may exist. */
13642 if (child_origin_die != child_die)
13643 {
13644 if (child_die->tag != child_origin_die->tag
13645 && !(child_die->tag == DW_TAG_inlined_subroutine
13646 && child_origin_die->tag == DW_TAG_subprogram))
13647 complaint (_("Child DIE %s and its abstract origin %s have "
13648 "different tags"),
13649 sect_offset_str (child_die->sect_off),
13650 sect_offset_str (child_origin_die->sect_off));
13651 if (child_origin_die->parent != origin_die)
13652 complaint (_("Child DIE %s and its abstract origin %s have "
13653 "different parents"),
13654 sect_offset_str (child_die->sect_off),
13655 sect_offset_str (child_origin_die->sect_off));
13656 else
13657 offsets.push_back (child_origin_die->sect_off);
13658 }
13659 }
13660 std::sort (offsets.begin (), offsets.end ());
13661 sect_offset *offsets_end = offsets.data () + offsets.size ();
13662 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13663 if (offsetp[-1] == *offsetp)
13664 complaint (_("Multiple children of DIE %s refer "
13665 "to DIE %s as their abstract origin"),
13666 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13667
13668 offsetp = offsets.data ();
13669 origin_child_die = origin_die->child;
13670 while (origin_child_die && origin_child_die->tag)
13671 {
13672 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13673 while (offsetp < offsets_end
13674 && *offsetp < origin_child_die->sect_off)
13675 offsetp++;
13676 if (offsetp >= offsets_end
13677 || *offsetp > origin_child_die->sect_off)
13678 {
13679 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13680 Check whether we're already processing ORIGIN_CHILD_DIE.
13681 This can happen with mutually referenced abstract_origins.
13682 PR 16581. */
13683 if (!origin_child_die->in_process)
13684 process_die (origin_child_die, origin_cu);
13685 }
13686 origin_child_die = sibling_die (origin_child_die);
13687 }
13688 origin_cu->list_in_scope = origin_previous_list_in_scope;
13689
13690 if (cu != origin_cu)
13691 compute_delayed_physnames (origin_cu);
13692 }
13693
13694 static void
13695 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13696 {
13697 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13698 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13699 struct context_stack *newobj;
13700 CORE_ADDR lowpc;
13701 CORE_ADDR highpc;
13702 struct die_info *child_die;
13703 struct attribute *attr, *call_line, *call_file;
13704 const char *name;
13705 CORE_ADDR baseaddr;
13706 struct block *block;
13707 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13708 std::vector<struct symbol *> template_args;
13709 struct template_symbol *templ_func = NULL;
13710
13711 if (inlined_func)
13712 {
13713 /* If we do not have call site information, we can't show the
13714 caller of this inlined function. That's too confusing, so
13715 only use the scope for local variables. */
13716 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13717 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13718 if (call_line == NULL || call_file == NULL)
13719 {
13720 read_lexical_block_scope (die, cu);
13721 return;
13722 }
13723 }
13724
13725 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13726
13727 name = dwarf2_name (die, cu);
13728
13729 /* Ignore functions with missing or empty names. These are actually
13730 illegal according to the DWARF standard. */
13731 if (name == NULL)
13732 {
13733 complaint (_("missing name for subprogram DIE at %s"),
13734 sect_offset_str (die->sect_off));
13735 return;
13736 }
13737
13738 /* Ignore functions with missing or invalid low and high pc attributes. */
13739 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13740 <= PC_BOUNDS_INVALID)
13741 {
13742 attr = dwarf2_attr (die, DW_AT_external, cu);
13743 if (!attr || !DW_UNSND (attr))
13744 complaint (_("cannot get low and high bounds "
13745 "for subprogram DIE at %s"),
13746 sect_offset_str (die->sect_off));
13747 return;
13748 }
13749
13750 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13751 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13752
13753 /* If we have any template arguments, then we must allocate a
13754 different sort of symbol. */
13755 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13756 {
13757 if (child_die->tag == DW_TAG_template_type_param
13758 || child_die->tag == DW_TAG_template_value_param)
13759 {
13760 templ_func = allocate_template_symbol (objfile);
13761 templ_func->subclass = SYMBOL_TEMPLATE;
13762 break;
13763 }
13764 }
13765
13766 newobj = cu->get_builder ()->push_context (0, lowpc);
13767 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13768 (struct symbol *) templ_func);
13769
13770 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13771 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13772 cu->language);
13773
13774 /* If there is a location expression for DW_AT_frame_base, record
13775 it. */
13776 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13777 if (attr != nullptr)
13778 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13779
13780 /* If there is a location for the static link, record it. */
13781 newobj->static_link = NULL;
13782 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13783 if (attr != nullptr)
13784 {
13785 newobj->static_link
13786 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13787 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13788 dwarf2_per_cu_addr_type (cu->per_cu));
13789 }
13790
13791 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13792
13793 if (die->child != NULL)
13794 {
13795 child_die = die->child;
13796 while (child_die && child_die->tag)
13797 {
13798 if (child_die->tag == DW_TAG_template_type_param
13799 || child_die->tag == DW_TAG_template_value_param)
13800 {
13801 struct symbol *arg = new_symbol (child_die, NULL, cu);
13802
13803 if (arg != NULL)
13804 template_args.push_back (arg);
13805 }
13806 else
13807 process_die (child_die, cu);
13808 child_die = sibling_die (child_die);
13809 }
13810 }
13811
13812 inherit_abstract_dies (die, cu);
13813
13814 /* If we have a DW_AT_specification, we might need to import using
13815 directives from the context of the specification DIE. See the
13816 comment in determine_prefix. */
13817 if (cu->language == language_cplus
13818 && dwarf2_attr (die, DW_AT_specification, cu))
13819 {
13820 struct dwarf2_cu *spec_cu = cu;
13821 struct die_info *spec_die = die_specification (die, &spec_cu);
13822
13823 while (spec_die)
13824 {
13825 child_die = spec_die->child;
13826 while (child_die && child_die->tag)
13827 {
13828 if (child_die->tag == DW_TAG_imported_module)
13829 process_die (child_die, spec_cu);
13830 child_die = sibling_die (child_die);
13831 }
13832
13833 /* In some cases, GCC generates specification DIEs that
13834 themselves contain DW_AT_specification attributes. */
13835 spec_die = die_specification (spec_die, &spec_cu);
13836 }
13837 }
13838
13839 struct context_stack cstk = cu->get_builder ()->pop_context ();
13840 /* Make a block for the local symbols within. */
13841 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13842 cstk.static_link, lowpc, highpc);
13843
13844 /* For C++, set the block's scope. */
13845 if ((cu->language == language_cplus
13846 || cu->language == language_fortran
13847 || cu->language == language_d
13848 || cu->language == language_rust)
13849 && cu->processing_has_namespace_info)
13850 block_set_scope (block, determine_prefix (die, cu),
13851 &objfile->objfile_obstack);
13852
13853 /* If we have address ranges, record them. */
13854 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13855
13856 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13857
13858 /* Attach template arguments to function. */
13859 if (!template_args.empty ())
13860 {
13861 gdb_assert (templ_func != NULL);
13862
13863 templ_func->n_template_arguments = template_args.size ();
13864 templ_func->template_arguments
13865 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13866 templ_func->n_template_arguments);
13867 memcpy (templ_func->template_arguments,
13868 template_args.data (),
13869 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13870
13871 /* Make sure that the symtab is set on the new symbols. Even
13872 though they don't appear in this symtab directly, other parts
13873 of gdb assume that symbols do, and this is reasonably
13874 true. */
13875 for (symbol *sym : template_args)
13876 symbol_set_symtab (sym, symbol_symtab (templ_func));
13877 }
13878
13879 /* In C++, we can have functions nested inside functions (e.g., when
13880 a function declares a class that has methods). This means that
13881 when we finish processing a function scope, we may need to go
13882 back to building a containing block's symbol lists. */
13883 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13884 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13885
13886 /* If we've finished processing a top-level function, subsequent
13887 symbols go in the file symbol list. */
13888 if (cu->get_builder ()->outermost_context_p ())
13889 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13890 }
13891
13892 /* Process all the DIES contained within a lexical block scope. Start
13893 a new scope, process the dies, and then close the scope. */
13894
13895 static void
13896 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13897 {
13898 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13899 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13900 CORE_ADDR lowpc, highpc;
13901 struct die_info *child_die;
13902 CORE_ADDR baseaddr;
13903
13904 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13905
13906 /* Ignore blocks with missing or invalid low and high pc attributes. */
13907 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13908 as multiple lexical blocks? Handling children in a sane way would
13909 be nasty. Might be easier to properly extend generic blocks to
13910 describe ranges. */
13911 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13912 {
13913 case PC_BOUNDS_NOT_PRESENT:
13914 /* DW_TAG_lexical_block has no attributes, process its children as if
13915 there was no wrapping by that DW_TAG_lexical_block.
13916 GCC does no longer produces such DWARF since GCC r224161. */
13917 for (child_die = die->child;
13918 child_die != NULL && child_die->tag;
13919 child_die = sibling_die (child_die))
13920 process_die (child_die, cu);
13921 return;
13922 case PC_BOUNDS_INVALID:
13923 return;
13924 }
13925 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13926 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13927
13928 cu->get_builder ()->push_context (0, lowpc);
13929 if (die->child != NULL)
13930 {
13931 child_die = die->child;
13932 while (child_die && child_die->tag)
13933 {
13934 process_die (child_die, cu);
13935 child_die = sibling_die (child_die);
13936 }
13937 }
13938 inherit_abstract_dies (die, cu);
13939 struct context_stack cstk = cu->get_builder ()->pop_context ();
13940
13941 if (*cu->get_builder ()->get_local_symbols () != NULL
13942 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13943 {
13944 struct block *block
13945 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13946 cstk.start_addr, highpc);
13947
13948 /* Note that recording ranges after traversing children, as we
13949 do here, means that recording a parent's ranges entails
13950 walking across all its children's ranges as they appear in
13951 the address map, which is quadratic behavior.
13952
13953 It would be nicer to record the parent's ranges before
13954 traversing its children, simply overriding whatever you find
13955 there. But since we don't even decide whether to create a
13956 block until after we've traversed its children, that's hard
13957 to do. */
13958 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13959 }
13960 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13961 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13962 }
13963
13964 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13965
13966 static void
13967 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13968 {
13969 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13970 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13971 CORE_ADDR pc, baseaddr;
13972 struct attribute *attr;
13973 struct call_site *call_site, call_site_local;
13974 void **slot;
13975 int nparams;
13976 struct die_info *child_die;
13977
13978 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13979
13980 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13981 if (attr == NULL)
13982 {
13983 /* This was a pre-DWARF-5 GNU extension alias
13984 for DW_AT_call_return_pc. */
13985 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13986 }
13987 if (!attr)
13988 {
13989 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13990 "DIE %s [in module %s]"),
13991 sect_offset_str (die->sect_off), objfile_name (objfile));
13992 return;
13993 }
13994 pc = attr_value_as_address (attr) + baseaddr;
13995 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13996
13997 if (cu->call_site_htab == NULL)
13998 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13999 NULL, &objfile->objfile_obstack,
14000 hashtab_obstack_allocate, NULL);
14001 call_site_local.pc = pc;
14002 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14003 if (*slot != NULL)
14004 {
14005 complaint (_("Duplicate PC %s for DW_TAG_call_site "
14006 "DIE %s [in module %s]"),
14007 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
14008 objfile_name (objfile));
14009 return;
14010 }
14011
14012 /* Count parameters at the caller. */
14013
14014 nparams = 0;
14015 for (child_die = die->child; child_die && child_die->tag;
14016 child_die = sibling_die (child_die))
14017 {
14018 if (child_die->tag != DW_TAG_call_site_parameter
14019 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14020 {
14021 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
14022 "DW_TAG_call_site child DIE %s [in module %s]"),
14023 child_die->tag, sect_offset_str (child_die->sect_off),
14024 objfile_name (objfile));
14025 continue;
14026 }
14027
14028 nparams++;
14029 }
14030
14031 call_site
14032 = ((struct call_site *)
14033 obstack_alloc (&objfile->objfile_obstack,
14034 sizeof (*call_site)
14035 + (sizeof (*call_site->parameter) * (nparams - 1))));
14036 *slot = call_site;
14037 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14038 call_site->pc = pc;
14039
14040 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14041 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14042 {
14043 struct die_info *func_die;
14044
14045 /* Skip also over DW_TAG_inlined_subroutine. */
14046 for (func_die = die->parent;
14047 func_die && func_die->tag != DW_TAG_subprogram
14048 && func_die->tag != DW_TAG_subroutine_type;
14049 func_die = func_die->parent);
14050
14051 /* DW_AT_call_all_calls is a superset
14052 of DW_AT_call_all_tail_calls. */
14053 if (func_die
14054 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14055 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14056 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14057 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14058 {
14059 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14060 not complete. But keep CALL_SITE for look ups via call_site_htab,
14061 both the initial caller containing the real return address PC and
14062 the final callee containing the current PC of a chain of tail
14063 calls do not need to have the tail call list complete. But any
14064 function candidate for a virtual tail call frame searched via
14065 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14066 determined unambiguously. */
14067 }
14068 else
14069 {
14070 struct type *func_type = NULL;
14071
14072 if (func_die)
14073 func_type = get_die_type (func_die, cu);
14074 if (func_type != NULL)
14075 {
14076 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14077
14078 /* Enlist this call site to the function. */
14079 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14080 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14081 }
14082 else
14083 complaint (_("Cannot find function owning DW_TAG_call_site "
14084 "DIE %s [in module %s]"),
14085 sect_offset_str (die->sect_off), objfile_name (objfile));
14086 }
14087 }
14088
14089 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14090 if (attr == NULL)
14091 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14092 if (attr == NULL)
14093 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14094 if (attr == NULL)
14095 {
14096 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14097 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14098 }
14099 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14100 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14101 /* Keep NULL DWARF_BLOCK. */;
14102 else if (attr_form_is_block (attr))
14103 {
14104 struct dwarf2_locexpr_baton *dlbaton;
14105
14106 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14107 dlbaton->data = DW_BLOCK (attr)->data;
14108 dlbaton->size = DW_BLOCK (attr)->size;
14109 dlbaton->per_cu = cu->per_cu;
14110
14111 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14112 }
14113 else if (attr_form_is_ref (attr))
14114 {
14115 struct dwarf2_cu *target_cu = cu;
14116 struct die_info *target_die;
14117
14118 target_die = follow_die_ref (die, attr, &target_cu);
14119 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14120 if (die_is_declaration (target_die, target_cu))
14121 {
14122 const char *target_physname;
14123
14124 /* Prefer the mangled name; otherwise compute the demangled one. */
14125 target_physname = dw2_linkage_name (target_die, target_cu);
14126 if (target_physname == NULL)
14127 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14128 if (target_physname == NULL)
14129 complaint (_("DW_AT_call_target target DIE has invalid "
14130 "physname, for referencing DIE %s [in module %s]"),
14131 sect_offset_str (die->sect_off), objfile_name (objfile));
14132 else
14133 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14134 }
14135 else
14136 {
14137 CORE_ADDR lowpc;
14138
14139 /* DW_AT_entry_pc should be preferred. */
14140 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14141 <= PC_BOUNDS_INVALID)
14142 complaint (_("DW_AT_call_target target DIE has invalid "
14143 "low pc, for referencing DIE %s [in module %s]"),
14144 sect_offset_str (die->sect_off), objfile_name (objfile));
14145 else
14146 {
14147 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14148 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14149 }
14150 }
14151 }
14152 else
14153 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14154 "block nor reference, for DIE %s [in module %s]"),
14155 sect_offset_str (die->sect_off), objfile_name (objfile));
14156
14157 call_site->per_cu = cu->per_cu;
14158
14159 for (child_die = die->child;
14160 child_die && child_die->tag;
14161 child_die = sibling_die (child_die))
14162 {
14163 struct call_site_parameter *parameter;
14164 struct attribute *loc, *origin;
14165
14166 if (child_die->tag != DW_TAG_call_site_parameter
14167 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14168 {
14169 /* Already printed the complaint above. */
14170 continue;
14171 }
14172
14173 gdb_assert (call_site->parameter_count < nparams);
14174 parameter = &call_site->parameter[call_site->parameter_count];
14175
14176 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14177 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14178 register is contained in DW_AT_call_value. */
14179
14180 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14181 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14182 if (origin == NULL)
14183 {
14184 /* This was a pre-DWARF-5 GNU extension alias
14185 for DW_AT_call_parameter. */
14186 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14187 }
14188 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14189 {
14190 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14191
14192 sect_offset sect_off
14193 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14194 if (!offset_in_cu_p (&cu->header, sect_off))
14195 {
14196 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14197 binding can be done only inside one CU. Such referenced DIE
14198 therefore cannot be even moved to DW_TAG_partial_unit. */
14199 complaint (_("DW_AT_call_parameter offset is not in CU for "
14200 "DW_TAG_call_site child DIE %s [in module %s]"),
14201 sect_offset_str (child_die->sect_off),
14202 objfile_name (objfile));
14203 continue;
14204 }
14205 parameter->u.param_cu_off
14206 = (cu_offset) (sect_off - cu->header.sect_off);
14207 }
14208 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14209 {
14210 complaint (_("No DW_FORM_block* DW_AT_location for "
14211 "DW_TAG_call_site child DIE %s [in module %s]"),
14212 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14213 continue;
14214 }
14215 else
14216 {
14217 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14218 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14219 if (parameter->u.dwarf_reg != -1)
14220 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14221 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14222 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14223 &parameter->u.fb_offset))
14224 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14225 else
14226 {
14227 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14228 "for DW_FORM_block* DW_AT_location is supported for "
14229 "DW_TAG_call_site child DIE %s "
14230 "[in module %s]"),
14231 sect_offset_str (child_die->sect_off),
14232 objfile_name (objfile));
14233 continue;
14234 }
14235 }
14236
14237 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14238 if (attr == NULL)
14239 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14240 if (!attr_form_is_block (attr))
14241 {
14242 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14243 "DW_TAG_call_site child DIE %s [in module %s]"),
14244 sect_offset_str (child_die->sect_off),
14245 objfile_name (objfile));
14246 continue;
14247 }
14248 parameter->value = DW_BLOCK (attr)->data;
14249 parameter->value_size = DW_BLOCK (attr)->size;
14250
14251 /* Parameters are not pre-cleared by memset above. */
14252 parameter->data_value = NULL;
14253 parameter->data_value_size = 0;
14254 call_site->parameter_count++;
14255
14256 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14257 if (attr == NULL)
14258 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14259 if (attr != nullptr)
14260 {
14261 if (!attr_form_is_block (attr))
14262 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14263 "DW_TAG_call_site child DIE %s [in module %s]"),
14264 sect_offset_str (child_die->sect_off),
14265 objfile_name (objfile));
14266 else
14267 {
14268 parameter->data_value = DW_BLOCK (attr)->data;
14269 parameter->data_value_size = DW_BLOCK (attr)->size;
14270 }
14271 }
14272 }
14273 }
14274
14275 /* Helper function for read_variable. If DIE represents a virtual
14276 table, then return the type of the concrete object that is
14277 associated with the virtual table. Otherwise, return NULL. */
14278
14279 static struct type *
14280 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14281 {
14282 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14283 if (attr == NULL)
14284 return NULL;
14285
14286 /* Find the type DIE. */
14287 struct die_info *type_die = NULL;
14288 struct dwarf2_cu *type_cu = cu;
14289
14290 if (attr_form_is_ref (attr))
14291 type_die = follow_die_ref (die, attr, &type_cu);
14292 if (type_die == NULL)
14293 return NULL;
14294
14295 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14296 return NULL;
14297 return die_containing_type (type_die, type_cu);
14298 }
14299
14300 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14301
14302 static void
14303 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14304 {
14305 struct rust_vtable_symbol *storage = NULL;
14306
14307 if (cu->language == language_rust)
14308 {
14309 struct type *containing_type = rust_containing_type (die, cu);
14310
14311 if (containing_type != NULL)
14312 {
14313 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14314
14315 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
14316 initialize_objfile_symbol (storage);
14317 storage->concrete_type = containing_type;
14318 storage->subclass = SYMBOL_RUST_VTABLE;
14319 }
14320 }
14321
14322 struct symbol *res = new_symbol (die, NULL, cu, storage);
14323 struct attribute *abstract_origin
14324 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14325 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14326 if (res == NULL && loc && abstract_origin)
14327 {
14328 /* We have a variable without a name, but with a location and an abstract
14329 origin. This may be a concrete instance of an abstract variable
14330 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14331 later. */
14332 struct dwarf2_cu *origin_cu = cu;
14333 struct die_info *origin_die
14334 = follow_die_ref (die, abstract_origin, &origin_cu);
14335 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
14336 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
14337 }
14338 }
14339
14340 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14341 reading .debug_rnglists.
14342 Callback's type should be:
14343 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14344 Return true if the attributes are present and valid, otherwise,
14345 return false. */
14346
14347 template <typename Callback>
14348 static bool
14349 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14350 Callback &&callback)
14351 {
14352 struct dwarf2_per_objfile *dwarf2_per_objfile
14353 = cu->per_cu->dwarf2_per_objfile;
14354 struct objfile *objfile = dwarf2_per_objfile->objfile;
14355 bfd *obfd = objfile->obfd;
14356 /* Base address selection entry. */
14357 CORE_ADDR base;
14358 int found_base;
14359 const gdb_byte *buffer;
14360 CORE_ADDR baseaddr;
14361 bool overflow = false;
14362
14363 found_base = cu->base_known;
14364 base = cu->base_address;
14365
14366 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14367 if (offset >= dwarf2_per_objfile->rnglists.size)
14368 {
14369 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14370 offset);
14371 return false;
14372 }
14373 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14374
14375 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14376
14377 while (1)
14378 {
14379 /* Initialize it due to a false compiler warning. */
14380 CORE_ADDR range_beginning = 0, range_end = 0;
14381 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14382 + dwarf2_per_objfile->rnglists.size);
14383 unsigned int bytes_read;
14384
14385 if (buffer == buf_end)
14386 {
14387 overflow = true;
14388 break;
14389 }
14390 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14391 switch (rlet)
14392 {
14393 case DW_RLE_end_of_list:
14394 break;
14395 case DW_RLE_base_address:
14396 if (buffer + cu->header.addr_size > buf_end)
14397 {
14398 overflow = true;
14399 break;
14400 }
14401 base = read_address (obfd, buffer, cu, &bytes_read);
14402 found_base = 1;
14403 buffer += bytes_read;
14404 break;
14405 case DW_RLE_start_length:
14406 if (buffer + cu->header.addr_size > buf_end)
14407 {
14408 overflow = true;
14409 break;
14410 }
14411 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14412 buffer += bytes_read;
14413 range_end = (range_beginning
14414 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14415 buffer += bytes_read;
14416 if (buffer > buf_end)
14417 {
14418 overflow = true;
14419 break;
14420 }
14421 break;
14422 case DW_RLE_offset_pair:
14423 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14424 buffer += bytes_read;
14425 if (buffer > buf_end)
14426 {
14427 overflow = true;
14428 break;
14429 }
14430 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14431 buffer += bytes_read;
14432 if (buffer > buf_end)
14433 {
14434 overflow = true;
14435 break;
14436 }
14437 break;
14438 case DW_RLE_start_end:
14439 if (buffer + 2 * cu->header.addr_size > buf_end)
14440 {
14441 overflow = true;
14442 break;
14443 }
14444 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14445 buffer += bytes_read;
14446 range_end = read_address (obfd, buffer, cu, &bytes_read);
14447 buffer += bytes_read;
14448 break;
14449 default:
14450 complaint (_("Invalid .debug_rnglists data (no base address)"));
14451 return false;
14452 }
14453 if (rlet == DW_RLE_end_of_list || overflow)
14454 break;
14455 if (rlet == DW_RLE_base_address)
14456 continue;
14457
14458 if (!found_base)
14459 {
14460 /* We have no valid base address for the ranges
14461 data. */
14462 complaint (_("Invalid .debug_rnglists data (no base address)"));
14463 return false;
14464 }
14465
14466 if (range_beginning > range_end)
14467 {
14468 /* Inverted range entries are invalid. */
14469 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14470 return false;
14471 }
14472
14473 /* Empty range entries have no effect. */
14474 if (range_beginning == range_end)
14475 continue;
14476
14477 range_beginning += base;
14478 range_end += base;
14479
14480 /* A not-uncommon case of bad debug info.
14481 Don't pollute the addrmap with bad data. */
14482 if (range_beginning + baseaddr == 0
14483 && !dwarf2_per_objfile->has_section_at_zero)
14484 {
14485 complaint (_(".debug_rnglists entry has start address of zero"
14486 " [in module %s]"), objfile_name (objfile));
14487 continue;
14488 }
14489
14490 callback (range_beginning, range_end);
14491 }
14492
14493 if (overflow)
14494 {
14495 complaint (_("Offset %d is not terminated "
14496 "for DW_AT_ranges attribute"),
14497 offset);
14498 return false;
14499 }
14500
14501 return true;
14502 }
14503
14504 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14505 Callback's type should be:
14506 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14507 Return 1 if the attributes are present and valid, otherwise, return 0. */
14508
14509 template <typename Callback>
14510 static int
14511 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14512 Callback &&callback)
14513 {
14514 struct dwarf2_per_objfile *dwarf2_per_objfile
14515 = cu->per_cu->dwarf2_per_objfile;
14516 struct objfile *objfile = dwarf2_per_objfile->objfile;
14517 struct comp_unit_head *cu_header = &cu->header;
14518 bfd *obfd = objfile->obfd;
14519 unsigned int addr_size = cu_header->addr_size;
14520 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14521 /* Base address selection entry. */
14522 CORE_ADDR base;
14523 int found_base;
14524 unsigned int dummy;
14525 const gdb_byte *buffer;
14526 CORE_ADDR baseaddr;
14527
14528 if (cu_header->version >= 5)
14529 return dwarf2_rnglists_process (offset, cu, callback);
14530
14531 found_base = cu->base_known;
14532 base = cu->base_address;
14533
14534 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14535 if (offset >= dwarf2_per_objfile->ranges.size)
14536 {
14537 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14538 offset);
14539 return 0;
14540 }
14541 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14542
14543 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14544
14545 while (1)
14546 {
14547 CORE_ADDR range_beginning, range_end;
14548
14549 range_beginning = read_address (obfd, buffer, cu, &dummy);
14550 buffer += addr_size;
14551 range_end = read_address (obfd, buffer, cu, &dummy);
14552 buffer += addr_size;
14553 offset += 2 * addr_size;
14554
14555 /* An end of list marker is a pair of zero addresses. */
14556 if (range_beginning == 0 && range_end == 0)
14557 /* Found the end of list entry. */
14558 break;
14559
14560 /* Each base address selection entry is a pair of 2 values.
14561 The first is the largest possible address, the second is
14562 the base address. Check for a base address here. */
14563 if ((range_beginning & mask) == mask)
14564 {
14565 /* If we found the largest possible address, then we already
14566 have the base address in range_end. */
14567 base = range_end;
14568 found_base = 1;
14569 continue;
14570 }
14571
14572 if (!found_base)
14573 {
14574 /* We have no valid base address for the ranges
14575 data. */
14576 complaint (_("Invalid .debug_ranges data (no base address)"));
14577 return 0;
14578 }
14579
14580 if (range_beginning > range_end)
14581 {
14582 /* Inverted range entries are invalid. */
14583 complaint (_("Invalid .debug_ranges data (inverted range)"));
14584 return 0;
14585 }
14586
14587 /* Empty range entries have no effect. */
14588 if (range_beginning == range_end)
14589 continue;
14590
14591 range_beginning += base;
14592 range_end += base;
14593
14594 /* A not-uncommon case of bad debug info.
14595 Don't pollute the addrmap with bad data. */
14596 if (range_beginning + baseaddr == 0
14597 && !dwarf2_per_objfile->has_section_at_zero)
14598 {
14599 complaint (_(".debug_ranges entry has start address of zero"
14600 " [in module %s]"), objfile_name (objfile));
14601 continue;
14602 }
14603
14604 callback (range_beginning, range_end);
14605 }
14606
14607 return 1;
14608 }
14609
14610 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14611 Return 1 if the attributes are present and valid, otherwise, return 0.
14612 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14613
14614 static int
14615 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14616 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14617 struct partial_symtab *ranges_pst)
14618 {
14619 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14620 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14621 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14622 SECT_OFF_TEXT (objfile));
14623 int low_set = 0;
14624 CORE_ADDR low = 0;
14625 CORE_ADDR high = 0;
14626 int retval;
14627
14628 retval = dwarf2_ranges_process (offset, cu,
14629 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14630 {
14631 if (ranges_pst != NULL)
14632 {
14633 CORE_ADDR lowpc;
14634 CORE_ADDR highpc;
14635
14636 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14637 range_beginning + baseaddr)
14638 - baseaddr);
14639 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14640 range_end + baseaddr)
14641 - baseaddr);
14642 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14643 lowpc, highpc - 1, ranges_pst);
14644 }
14645
14646 /* FIXME: This is recording everything as a low-high
14647 segment of consecutive addresses. We should have a
14648 data structure for discontiguous block ranges
14649 instead. */
14650 if (! low_set)
14651 {
14652 low = range_beginning;
14653 high = range_end;
14654 low_set = 1;
14655 }
14656 else
14657 {
14658 if (range_beginning < low)
14659 low = range_beginning;
14660 if (range_end > high)
14661 high = range_end;
14662 }
14663 });
14664 if (!retval)
14665 return 0;
14666
14667 if (! low_set)
14668 /* If the first entry is an end-of-list marker, the range
14669 describes an empty scope, i.e. no instructions. */
14670 return 0;
14671
14672 if (low_return)
14673 *low_return = low;
14674 if (high_return)
14675 *high_return = high;
14676 return 1;
14677 }
14678
14679 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14680 definition for the return value. *LOWPC and *HIGHPC are set iff
14681 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14682
14683 static enum pc_bounds_kind
14684 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14685 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14686 struct partial_symtab *pst)
14687 {
14688 struct dwarf2_per_objfile *dwarf2_per_objfile
14689 = cu->per_cu->dwarf2_per_objfile;
14690 struct attribute *attr;
14691 struct attribute *attr_high;
14692 CORE_ADDR low = 0;
14693 CORE_ADDR high = 0;
14694 enum pc_bounds_kind ret;
14695
14696 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14697 if (attr_high)
14698 {
14699 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14700 if (attr != nullptr)
14701 {
14702 low = attr_value_as_address (attr);
14703 high = attr_value_as_address (attr_high);
14704 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14705 high += low;
14706 }
14707 else
14708 /* Found high w/o low attribute. */
14709 return PC_BOUNDS_INVALID;
14710
14711 /* Found consecutive range of addresses. */
14712 ret = PC_BOUNDS_HIGH_LOW;
14713 }
14714 else
14715 {
14716 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14717 if (attr != NULL)
14718 {
14719 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14720 We take advantage of the fact that DW_AT_ranges does not appear
14721 in DW_TAG_compile_unit of DWO files. */
14722 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14723 unsigned int ranges_offset = (DW_UNSND (attr)
14724 + (need_ranges_base
14725 ? cu->ranges_base
14726 : 0));
14727
14728 /* Value of the DW_AT_ranges attribute is the offset in the
14729 .debug_ranges section. */
14730 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14731 return PC_BOUNDS_INVALID;
14732 /* Found discontinuous range of addresses. */
14733 ret = PC_BOUNDS_RANGES;
14734 }
14735 else
14736 return PC_BOUNDS_NOT_PRESENT;
14737 }
14738
14739 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14740 if (high <= low)
14741 return PC_BOUNDS_INVALID;
14742
14743 /* When using the GNU linker, .gnu.linkonce. sections are used to
14744 eliminate duplicate copies of functions and vtables and such.
14745 The linker will arbitrarily choose one and discard the others.
14746 The AT_*_pc values for such functions refer to local labels in
14747 these sections. If the section from that file was discarded, the
14748 labels are not in the output, so the relocs get a value of 0.
14749 If this is a discarded function, mark the pc bounds as invalid,
14750 so that GDB will ignore it. */
14751 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14752 return PC_BOUNDS_INVALID;
14753
14754 *lowpc = low;
14755 if (highpc)
14756 *highpc = high;
14757 return ret;
14758 }
14759
14760 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14761 its low and high PC addresses. Do nothing if these addresses could not
14762 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14763 and HIGHPC to the high address if greater than HIGHPC. */
14764
14765 static void
14766 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14767 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14768 struct dwarf2_cu *cu)
14769 {
14770 CORE_ADDR low, high;
14771 struct die_info *child = die->child;
14772
14773 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14774 {
14775 *lowpc = std::min (*lowpc, low);
14776 *highpc = std::max (*highpc, high);
14777 }
14778
14779 /* If the language does not allow nested subprograms (either inside
14780 subprograms or lexical blocks), we're done. */
14781 if (cu->language != language_ada)
14782 return;
14783
14784 /* Check all the children of the given DIE. If it contains nested
14785 subprograms, then check their pc bounds. Likewise, we need to
14786 check lexical blocks as well, as they may also contain subprogram
14787 definitions. */
14788 while (child && child->tag)
14789 {
14790 if (child->tag == DW_TAG_subprogram
14791 || child->tag == DW_TAG_lexical_block)
14792 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14793 child = sibling_die (child);
14794 }
14795 }
14796
14797 /* Get the low and high pc's represented by the scope DIE, and store
14798 them in *LOWPC and *HIGHPC. If the correct values can't be
14799 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14800
14801 static void
14802 get_scope_pc_bounds (struct die_info *die,
14803 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14804 struct dwarf2_cu *cu)
14805 {
14806 CORE_ADDR best_low = (CORE_ADDR) -1;
14807 CORE_ADDR best_high = (CORE_ADDR) 0;
14808 CORE_ADDR current_low, current_high;
14809
14810 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14811 >= PC_BOUNDS_RANGES)
14812 {
14813 best_low = current_low;
14814 best_high = current_high;
14815 }
14816 else
14817 {
14818 struct die_info *child = die->child;
14819
14820 while (child && child->tag)
14821 {
14822 switch (child->tag) {
14823 case DW_TAG_subprogram:
14824 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14825 break;
14826 case DW_TAG_namespace:
14827 case DW_TAG_module:
14828 /* FIXME: carlton/2004-01-16: Should we do this for
14829 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14830 that current GCC's always emit the DIEs corresponding
14831 to definitions of methods of classes as children of a
14832 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14833 the DIEs giving the declarations, which could be
14834 anywhere). But I don't see any reason why the
14835 standards says that they have to be there. */
14836 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14837
14838 if (current_low != ((CORE_ADDR) -1))
14839 {
14840 best_low = std::min (best_low, current_low);
14841 best_high = std::max (best_high, current_high);
14842 }
14843 break;
14844 default:
14845 /* Ignore. */
14846 break;
14847 }
14848
14849 child = sibling_die (child);
14850 }
14851 }
14852
14853 *lowpc = best_low;
14854 *highpc = best_high;
14855 }
14856
14857 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14858 in DIE. */
14859
14860 static void
14861 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14862 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14863 {
14864 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14865 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14866 struct attribute *attr;
14867 struct attribute *attr_high;
14868
14869 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14870 if (attr_high)
14871 {
14872 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14873 if (attr != nullptr)
14874 {
14875 CORE_ADDR low = attr_value_as_address (attr);
14876 CORE_ADDR high = attr_value_as_address (attr_high);
14877
14878 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14879 high += low;
14880
14881 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14882 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14883 cu->get_builder ()->record_block_range (block, low, high - 1);
14884 }
14885 }
14886
14887 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14888 if (attr != nullptr)
14889 {
14890 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14891 We take advantage of the fact that DW_AT_ranges does not appear
14892 in DW_TAG_compile_unit of DWO files. */
14893 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14894
14895 /* The value of the DW_AT_ranges attribute is the offset of the
14896 address range list in the .debug_ranges section. */
14897 unsigned long offset = (DW_UNSND (attr)
14898 + (need_ranges_base ? cu->ranges_base : 0));
14899
14900 std::vector<blockrange> blockvec;
14901 dwarf2_ranges_process (offset, cu,
14902 [&] (CORE_ADDR start, CORE_ADDR end)
14903 {
14904 start += baseaddr;
14905 end += baseaddr;
14906 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14907 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14908 cu->get_builder ()->record_block_range (block, start, end - 1);
14909 blockvec.emplace_back (start, end);
14910 });
14911
14912 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14913 }
14914 }
14915
14916 /* Check whether the producer field indicates either of GCC < 4.6, or the
14917 Intel C/C++ compiler, and cache the result in CU. */
14918
14919 static void
14920 check_producer (struct dwarf2_cu *cu)
14921 {
14922 int major, minor;
14923
14924 if (cu->producer == NULL)
14925 {
14926 /* For unknown compilers expect their behavior is DWARF version
14927 compliant.
14928
14929 GCC started to support .debug_types sections by -gdwarf-4 since
14930 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14931 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14932 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14933 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14934 }
14935 else if (producer_is_gcc (cu->producer, &major, &minor))
14936 {
14937 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14938 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14939 }
14940 else if (producer_is_icc (cu->producer, &major, &minor))
14941 {
14942 cu->producer_is_icc = true;
14943 cu->producer_is_icc_lt_14 = major < 14;
14944 }
14945 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14946 cu->producer_is_codewarrior = true;
14947 else
14948 {
14949 /* For other non-GCC compilers, expect their behavior is DWARF version
14950 compliant. */
14951 }
14952
14953 cu->checked_producer = true;
14954 }
14955
14956 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14957 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14958 during 4.6.0 experimental. */
14959
14960 static bool
14961 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14962 {
14963 if (!cu->checked_producer)
14964 check_producer (cu);
14965
14966 return cu->producer_is_gxx_lt_4_6;
14967 }
14968
14969
14970 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14971 with incorrect is_stmt attributes. */
14972
14973 static bool
14974 producer_is_codewarrior (struct dwarf2_cu *cu)
14975 {
14976 if (!cu->checked_producer)
14977 check_producer (cu);
14978
14979 return cu->producer_is_codewarrior;
14980 }
14981
14982 /* Return the default accessibility type if it is not overridden by
14983 DW_AT_accessibility. */
14984
14985 static enum dwarf_access_attribute
14986 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14987 {
14988 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14989 {
14990 /* The default DWARF 2 accessibility for members is public, the default
14991 accessibility for inheritance is private. */
14992
14993 if (die->tag != DW_TAG_inheritance)
14994 return DW_ACCESS_public;
14995 else
14996 return DW_ACCESS_private;
14997 }
14998 else
14999 {
15000 /* DWARF 3+ defines the default accessibility a different way. The same
15001 rules apply now for DW_TAG_inheritance as for the members and it only
15002 depends on the container kind. */
15003
15004 if (die->parent->tag == DW_TAG_class_type)
15005 return DW_ACCESS_private;
15006 else
15007 return DW_ACCESS_public;
15008 }
15009 }
15010
15011 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15012 offset. If the attribute was not found return 0, otherwise return
15013 1. If it was found but could not properly be handled, set *OFFSET
15014 to 0. */
15015
15016 static int
15017 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15018 LONGEST *offset)
15019 {
15020 struct attribute *attr;
15021
15022 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15023 if (attr != NULL)
15024 {
15025 *offset = 0;
15026
15027 /* Note that we do not check for a section offset first here.
15028 This is because DW_AT_data_member_location is new in DWARF 4,
15029 so if we see it, we can assume that a constant form is really
15030 a constant and not a section offset. */
15031 if (attr_form_is_constant (attr))
15032 *offset = dwarf2_get_attr_constant_value (attr, 0);
15033 else if (attr_form_is_section_offset (attr))
15034 dwarf2_complex_location_expr_complaint ();
15035 else if (attr_form_is_block (attr))
15036 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15037 else
15038 dwarf2_complex_location_expr_complaint ();
15039
15040 return 1;
15041 }
15042
15043 return 0;
15044 }
15045
15046 /* Add an aggregate field to the field list. */
15047
15048 static void
15049 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15050 struct dwarf2_cu *cu)
15051 {
15052 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15053 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15054 struct nextfield *new_field;
15055 struct attribute *attr;
15056 struct field *fp;
15057 const char *fieldname = "";
15058
15059 if (die->tag == DW_TAG_inheritance)
15060 {
15061 fip->baseclasses.emplace_back ();
15062 new_field = &fip->baseclasses.back ();
15063 }
15064 else
15065 {
15066 fip->fields.emplace_back ();
15067 new_field = &fip->fields.back ();
15068 }
15069
15070 fip->nfields++;
15071
15072 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15073 if (attr != nullptr)
15074 new_field->accessibility = DW_UNSND (attr);
15075 else
15076 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15077 if (new_field->accessibility != DW_ACCESS_public)
15078 fip->non_public_fields = 1;
15079
15080 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15081 if (attr != nullptr)
15082 new_field->virtuality = DW_UNSND (attr);
15083 else
15084 new_field->virtuality = DW_VIRTUALITY_none;
15085
15086 fp = &new_field->field;
15087
15088 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15089 {
15090 LONGEST offset;
15091
15092 /* Data member other than a C++ static data member. */
15093
15094 /* Get type of field. */
15095 fp->type = die_type (die, cu);
15096
15097 SET_FIELD_BITPOS (*fp, 0);
15098
15099 /* Get bit size of field (zero if none). */
15100 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15101 if (attr != nullptr)
15102 {
15103 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15104 }
15105 else
15106 {
15107 FIELD_BITSIZE (*fp) = 0;
15108 }
15109
15110 /* Get bit offset of field. */
15111 if (handle_data_member_location (die, cu, &offset))
15112 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15113 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15114 if (attr != nullptr)
15115 {
15116 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15117 {
15118 /* For big endian bits, the DW_AT_bit_offset gives the
15119 additional bit offset from the MSB of the containing
15120 anonymous object to the MSB of the field. We don't
15121 have to do anything special since we don't need to
15122 know the size of the anonymous object. */
15123 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15124 }
15125 else
15126 {
15127 /* For little endian bits, compute the bit offset to the
15128 MSB of the anonymous object, subtract off the number of
15129 bits from the MSB of the field to the MSB of the
15130 object, and then subtract off the number of bits of
15131 the field itself. The result is the bit offset of
15132 the LSB of the field. */
15133 int anonymous_size;
15134 int bit_offset = DW_UNSND (attr);
15135
15136 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15137 if (attr != nullptr)
15138 {
15139 /* The size of the anonymous object containing
15140 the bit field is explicit, so use the
15141 indicated size (in bytes). */
15142 anonymous_size = DW_UNSND (attr);
15143 }
15144 else
15145 {
15146 /* The size of the anonymous object containing
15147 the bit field must be inferred from the type
15148 attribute of the data member containing the
15149 bit field. */
15150 anonymous_size = TYPE_LENGTH (fp->type);
15151 }
15152 SET_FIELD_BITPOS (*fp,
15153 (FIELD_BITPOS (*fp)
15154 + anonymous_size * bits_per_byte
15155 - bit_offset - FIELD_BITSIZE (*fp)));
15156 }
15157 }
15158 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15159 if (attr != NULL)
15160 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15161 + dwarf2_get_attr_constant_value (attr, 0)));
15162
15163 /* Get name of field. */
15164 fieldname = dwarf2_name (die, cu);
15165 if (fieldname == NULL)
15166 fieldname = "";
15167
15168 /* The name is already allocated along with this objfile, so we don't
15169 need to duplicate it for the type. */
15170 fp->name = fieldname;
15171
15172 /* Change accessibility for artificial fields (e.g. virtual table
15173 pointer or virtual base class pointer) to private. */
15174 if (dwarf2_attr (die, DW_AT_artificial, cu))
15175 {
15176 FIELD_ARTIFICIAL (*fp) = 1;
15177 new_field->accessibility = DW_ACCESS_private;
15178 fip->non_public_fields = 1;
15179 }
15180 }
15181 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15182 {
15183 /* C++ static member. */
15184
15185 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15186 is a declaration, but all versions of G++ as of this writing
15187 (so through at least 3.2.1) incorrectly generate
15188 DW_TAG_variable tags. */
15189
15190 const char *physname;
15191
15192 /* Get name of field. */
15193 fieldname = dwarf2_name (die, cu);
15194 if (fieldname == NULL)
15195 return;
15196
15197 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15198 if (attr
15199 /* Only create a symbol if this is an external value.
15200 new_symbol checks this and puts the value in the global symbol
15201 table, which we want. If it is not external, new_symbol
15202 will try to put the value in cu->list_in_scope which is wrong. */
15203 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15204 {
15205 /* A static const member, not much different than an enum as far as
15206 we're concerned, except that we can support more types. */
15207 new_symbol (die, NULL, cu);
15208 }
15209
15210 /* Get physical name. */
15211 physname = dwarf2_physname (fieldname, die, cu);
15212
15213 /* The name is already allocated along with this objfile, so we don't
15214 need to duplicate it for the type. */
15215 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15216 FIELD_TYPE (*fp) = die_type (die, cu);
15217 FIELD_NAME (*fp) = fieldname;
15218 }
15219 else if (die->tag == DW_TAG_inheritance)
15220 {
15221 LONGEST offset;
15222
15223 /* C++ base class field. */
15224 if (handle_data_member_location (die, cu, &offset))
15225 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15226 FIELD_BITSIZE (*fp) = 0;
15227 FIELD_TYPE (*fp) = die_type (die, cu);
15228 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
15229 }
15230 else if (die->tag == DW_TAG_variant_part)
15231 {
15232 /* process_structure_scope will treat this DIE as a union. */
15233 process_structure_scope (die, cu);
15234
15235 /* The variant part is relative to the start of the enclosing
15236 structure. */
15237 SET_FIELD_BITPOS (*fp, 0);
15238 fp->type = get_die_type (die, cu);
15239 fp->artificial = 1;
15240 fp->name = "<<variant>>";
15241
15242 /* Normally a DW_TAG_variant_part won't have a size, but our
15243 representation requires one, so set it to the maximum of the
15244 child sizes, being sure to account for the offset at which
15245 each child is seen. */
15246 if (TYPE_LENGTH (fp->type) == 0)
15247 {
15248 unsigned max = 0;
15249 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15250 {
15251 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
15252 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
15253 if (len > max)
15254 max = len;
15255 }
15256 TYPE_LENGTH (fp->type) = max;
15257 }
15258 }
15259 else
15260 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15261 }
15262
15263 /* Can the type given by DIE define another type? */
15264
15265 static bool
15266 type_can_define_types (const struct die_info *die)
15267 {
15268 switch (die->tag)
15269 {
15270 case DW_TAG_typedef:
15271 case DW_TAG_class_type:
15272 case DW_TAG_structure_type:
15273 case DW_TAG_union_type:
15274 case DW_TAG_enumeration_type:
15275 return true;
15276
15277 default:
15278 return false;
15279 }
15280 }
15281
15282 /* Add a type definition defined in the scope of the FIP's class. */
15283
15284 static void
15285 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15286 struct dwarf2_cu *cu)
15287 {
15288 struct decl_field fp;
15289 memset (&fp, 0, sizeof (fp));
15290
15291 gdb_assert (type_can_define_types (die));
15292
15293 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15294 fp.name = dwarf2_name (die, cu);
15295 fp.type = read_type_die (die, cu);
15296
15297 /* Save accessibility. */
15298 enum dwarf_access_attribute accessibility;
15299 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15300 if (attr != NULL)
15301 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15302 else
15303 accessibility = dwarf2_default_access_attribute (die, cu);
15304 switch (accessibility)
15305 {
15306 case DW_ACCESS_public:
15307 /* The assumed value if neither private nor protected. */
15308 break;
15309 case DW_ACCESS_private:
15310 fp.is_private = 1;
15311 break;
15312 case DW_ACCESS_protected:
15313 fp.is_protected = 1;
15314 break;
15315 default:
15316 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15317 }
15318
15319 if (die->tag == DW_TAG_typedef)
15320 fip->typedef_field_list.push_back (fp);
15321 else
15322 fip->nested_types_list.push_back (fp);
15323 }
15324
15325 /* Create the vector of fields, and attach it to the type. */
15326
15327 static void
15328 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15329 struct dwarf2_cu *cu)
15330 {
15331 int nfields = fip->nfields;
15332
15333 /* Record the field count, allocate space for the array of fields,
15334 and create blank accessibility bitfields if necessary. */
15335 TYPE_NFIELDS (type) = nfields;
15336 TYPE_FIELDS (type) = (struct field *)
15337 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15338
15339 if (fip->non_public_fields && cu->language != language_ada)
15340 {
15341 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15342
15343 TYPE_FIELD_PRIVATE_BITS (type) =
15344 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15345 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15346
15347 TYPE_FIELD_PROTECTED_BITS (type) =
15348 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15349 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15350
15351 TYPE_FIELD_IGNORE_BITS (type) =
15352 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15353 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15354 }
15355
15356 /* If the type has baseclasses, allocate and clear a bit vector for
15357 TYPE_FIELD_VIRTUAL_BITS. */
15358 if (!fip->baseclasses.empty () && cu->language != language_ada)
15359 {
15360 int num_bytes = B_BYTES (fip->baseclasses.size ());
15361 unsigned char *pointer;
15362
15363 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15364 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15365 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15366 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15367 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15368 }
15369
15370 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15371 {
15372 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15373
15374 for (int index = 0; index < nfields; ++index)
15375 {
15376 struct nextfield &field = fip->fields[index];
15377
15378 if (field.variant.is_discriminant)
15379 di->discriminant_index = index;
15380 else if (field.variant.default_branch)
15381 di->default_index = index;
15382 else
15383 di->discriminants[index] = field.variant.discriminant_value;
15384 }
15385 }
15386
15387 /* Copy the saved-up fields into the field vector. */
15388 for (int i = 0; i < nfields; ++i)
15389 {
15390 struct nextfield &field
15391 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15392 : fip->fields[i - fip->baseclasses.size ()]);
15393
15394 TYPE_FIELD (type, i) = field.field;
15395 switch (field.accessibility)
15396 {
15397 case DW_ACCESS_private:
15398 if (cu->language != language_ada)
15399 SET_TYPE_FIELD_PRIVATE (type, i);
15400 break;
15401
15402 case DW_ACCESS_protected:
15403 if (cu->language != language_ada)
15404 SET_TYPE_FIELD_PROTECTED (type, i);
15405 break;
15406
15407 case DW_ACCESS_public:
15408 break;
15409
15410 default:
15411 /* Unknown accessibility. Complain and treat it as public. */
15412 {
15413 complaint (_("unsupported accessibility %d"),
15414 field.accessibility);
15415 }
15416 break;
15417 }
15418 if (i < fip->baseclasses.size ())
15419 {
15420 switch (field.virtuality)
15421 {
15422 case DW_VIRTUALITY_virtual:
15423 case DW_VIRTUALITY_pure_virtual:
15424 if (cu->language == language_ada)
15425 error (_("unexpected virtuality in component of Ada type"));
15426 SET_TYPE_FIELD_VIRTUAL (type, i);
15427 break;
15428 }
15429 }
15430 }
15431 }
15432
15433 /* Return true if this member function is a constructor, false
15434 otherwise. */
15435
15436 static int
15437 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15438 {
15439 const char *fieldname;
15440 const char *type_name;
15441 int len;
15442
15443 if (die->parent == NULL)
15444 return 0;
15445
15446 if (die->parent->tag != DW_TAG_structure_type
15447 && die->parent->tag != DW_TAG_union_type
15448 && die->parent->tag != DW_TAG_class_type)
15449 return 0;
15450
15451 fieldname = dwarf2_name (die, cu);
15452 type_name = dwarf2_name (die->parent, cu);
15453 if (fieldname == NULL || type_name == NULL)
15454 return 0;
15455
15456 len = strlen (fieldname);
15457 return (strncmp (fieldname, type_name, len) == 0
15458 && (type_name[len] == '\0' || type_name[len] == '<'));
15459 }
15460
15461 /* Check if the given VALUE is a recognized enum
15462 dwarf_defaulted_attribute constant according to DWARF5 spec,
15463 Table 7.24. */
15464
15465 static bool
15466 is_valid_DW_AT_defaulted (ULONGEST value)
15467 {
15468 switch (value)
15469 {
15470 case DW_DEFAULTED_no:
15471 case DW_DEFAULTED_in_class:
15472 case DW_DEFAULTED_out_of_class:
15473 return true;
15474 }
15475
15476 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15477 return false;
15478 }
15479
15480 /* Add a member function to the proper fieldlist. */
15481
15482 static void
15483 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15484 struct type *type, struct dwarf2_cu *cu)
15485 {
15486 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15487 struct attribute *attr;
15488 int i;
15489 struct fnfieldlist *flp = nullptr;
15490 struct fn_field *fnp;
15491 const char *fieldname;
15492 struct type *this_type;
15493 enum dwarf_access_attribute accessibility;
15494
15495 if (cu->language == language_ada)
15496 error (_("unexpected member function in Ada type"));
15497
15498 /* Get name of member function. */
15499 fieldname = dwarf2_name (die, cu);
15500 if (fieldname == NULL)
15501 return;
15502
15503 /* Look up member function name in fieldlist. */
15504 for (i = 0; i < fip->fnfieldlists.size (); i++)
15505 {
15506 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15507 {
15508 flp = &fip->fnfieldlists[i];
15509 break;
15510 }
15511 }
15512
15513 /* Create a new fnfieldlist if necessary. */
15514 if (flp == nullptr)
15515 {
15516 fip->fnfieldlists.emplace_back ();
15517 flp = &fip->fnfieldlists.back ();
15518 flp->name = fieldname;
15519 i = fip->fnfieldlists.size () - 1;
15520 }
15521
15522 /* Create a new member function field and add it to the vector of
15523 fnfieldlists. */
15524 flp->fnfields.emplace_back ();
15525 fnp = &flp->fnfields.back ();
15526
15527 /* Delay processing of the physname until later. */
15528 if (cu->language == language_cplus)
15529 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15530 die, cu);
15531 else
15532 {
15533 const char *physname = dwarf2_physname (fieldname, die, cu);
15534 fnp->physname = physname ? physname : "";
15535 }
15536
15537 fnp->type = alloc_type (objfile);
15538 this_type = read_type_die (die, cu);
15539 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15540 {
15541 int nparams = TYPE_NFIELDS (this_type);
15542
15543 /* TYPE is the domain of this method, and THIS_TYPE is the type
15544 of the method itself (TYPE_CODE_METHOD). */
15545 smash_to_method_type (fnp->type, type,
15546 TYPE_TARGET_TYPE (this_type),
15547 TYPE_FIELDS (this_type),
15548 TYPE_NFIELDS (this_type),
15549 TYPE_VARARGS (this_type));
15550
15551 /* Handle static member functions.
15552 Dwarf2 has no clean way to discern C++ static and non-static
15553 member functions. G++ helps GDB by marking the first
15554 parameter for non-static member functions (which is the this
15555 pointer) as artificial. We obtain this information from
15556 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15557 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15558 fnp->voffset = VOFFSET_STATIC;
15559 }
15560 else
15561 complaint (_("member function type missing for '%s'"),
15562 dwarf2_full_name (fieldname, die, cu));
15563
15564 /* Get fcontext from DW_AT_containing_type if present. */
15565 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15566 fnp->fcontext = die_containing_type (die, cu);
15567
15568 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15569 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15570
15571 /* Get accessibility. */
15572 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15573 if (attr != nullptr)
15574 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15575 else
15576 accessibility = dwarf2_default_access_attribute (die, cu);
15577 switch (accessibility)
15578 {
15579 case DW_ACCESS_private:
15580 fnp->is_private = 1;
15581 break;
15582 case DW_ACCESS_protected:
15583 fnp->is_protected = 1;
15584 break;
15585 }
15586
15587 /* Check for artificial methods. */
15588 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15589 if (attr && DW_UNSND (attr) != 0)
15590 fnp->is_artificial = 1;
15591
15592 /* Check for defaulted methods. */
15593 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15594 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15595 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15596
15597 /* Check for deleted methods. */
15598 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15599 if (attr != nullptr && DW_UNSND (attr) != 0)
15600 fnp->is_deleted = 1;
15601
15602 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15603
15604 /* Get index in virtual function table if it is a virtual member
15605 function. For older versions of GCC, this is an offset in the
15606 appropriate virtual table, as specified by DW_AT_containing_type.
15607 For everyone else, it is an expression to be evaluated relative
15608 to the object address. */
15609
15610 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15611 if (attr != nullptr)
15612 {
15613 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15614 {
15615 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15616 {
15617 /* Old-style GCC. */
15618 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15619 }
15620 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15621 || (DW_BLOCK (attr)->size > 1
15622 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15623 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15624 {
15625 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15626 if ((fnp->voffset % cu->header.addr_size) != 0)
15627 dwarf2_complex_location_expr_complaint ();
15628 else
15629 fnp->voffset /= cu->header.addr_size;
15630 fnp->voffset += 2;
15631 }
15632 else
15633 dwarf2_complex_location_expr_complaint ();
15634
15635 if (!fnp->fcontext)
15636 {
15637 /* If there is no `this' field and no DW_AT_containing_type,
15638 we cannot actually find a base class context for the
15639 vtable! */
15640 if (TYPE_NFIELDS (this_type) == 0
15641 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15642 {
15643 complaint (_("cannot determine context for virtual member "
15644 "function \"%s\" (offset %s)"),
15645 fieldname, sect_offset_str (die->sect_off));
15646 }
15647 else
15648 {
15649 fnp->fcontext
15650 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15651 }
15652 }
15653 }
15654 else if (attr_form_is_section_offset (attr))
15655 {
15656 dwarf2_complex_location_expr_complaint ();
15657 }
15658 else
15659 {
15660 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15661 fieldname);
15662 }
15663 }
15664 else
15665 {
15666 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15667 if (attr && DW_UNSND (attr))
15668 {
15669 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15670 complaint (_("Member function \"%s\" (offset %s) is virtual "
15671 "but the vtable offset is not specified"),
15672 fieldname, sect_offset_str (die->sect_off));
15673 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15674 TYPE_CPLUS_DYNAMIC (type) = 1;
15675 }
15676 }
15677 }
15678
15679 /* Create the vector of member function fields, and attach it to the type. */
15680
15681 static void
15682 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15683 struct dwarf2_cu *cu)
15684 {
15685 if (cu->language == language_ada)
15686 error (_("unexpected member functions in Ada type"));
15687
15688 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15689 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15690 TYPE_ALLOC (type,
15691 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15692
15693 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15694 {
15695 struct fnfieldlist &nf = fip->fnfieldlists[i];
15696 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15697
15698 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15699 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15700 fn_flp->fn_fields = (struct fn_field *)
15701 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15702
15703 for (int k = 0; k < nf.fnfields.size (); ++k)
15704 fn_flp->fn_fields[k] = nf.fnfields[k];
15705 }
15706
15707 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15708 }
15709
15710 /* Returns non-zero if NAME is the name of a vtable member in CU's
15711 language, zero otherwise. */
15712 static int
15713 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15714 {
15715 static const char vptr[] = "_vptr";
15716
15717 /* Look for the C++ form of the vtable. */
15718 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15719 return 1;
15720
15721 return 0;
15722 }
15723
15724 /* GCC outputs unnamed structures that are really pointers to member
15725 functions, with the ABI-specified layout. If TYPE describes
15726 such a structure, smash it into a member function type.
15727
15728 GCC shouldn't do this; it should just output pointer to member DIEs.
15729 This is GCC PR debug/28767. */
15730
15731 static void
15732 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15733 {
15734 struct type *pfn_type, *self_type, *new_type;
15735
15736 /* Check for a structure with no name and two children. */
15737 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15738 return;
15739
15740 /* Check for __pfn and __delta members. */
15741 if (TYPE_FIELD_NAME (type, 0) == NULL
15742 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15743 || TYPE_FIELD_NAME (type, 1) == NULL
15744 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15745 return;
15746
15747 /* Find the type of the method. */
15748 pfn_type = TYPE_FIELD_TYPE (type, 0);
15749 if (pfn_type == NULL
15750 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15751 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15752 return;
15753
15754 /* Look for the "this" argument. */
15755 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15756 if (TYPE_NFIELDS (pfn_type) == 0
15757 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15758 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15759 return;
15760
15761 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15762 new_type = alloc_type (objfile);
15763 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15764 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15765 TYPE_VARARGS (pfn_type));
15766 smash_to_methodptr_type (type, new_type);
15767 }
15768
15769 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15770 appropriate error checking and issuing complaints if there is a
15771 problem. */
15772
15773 static ULONGEST
15774 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15775 {
15776 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15777
15778 if (attr == nullptr)
15779 return 0;
15780
15781 if (!attr_form_is_constant (attr))
15782 {
15783 complaint (_("DW_AT_alignment must have constant form"
15784 " - DIE at %s [in module %s]"),
15785 sect_offset_str (die->sect_off),
15786 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15787 return 0;
15788 }
15789
15790 ULONGEST align;
15791 if (attr->form == DW_FORM_sdata)
15792 {
15793 LONGEST val = DW_SND (attr);
15794 if (val < 0)
15795 {
15796 complaint (_("DW_AT_alignment value must not be negative"
15797 " - DIE at %s [in module %s]"),
15798 sect_offset_str (die->sect_off),
15799 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15800 return 0;
15801 }
15802 align = val;
15803 }
15804 else
15805 align = DW_UNSND (attr);
15806
15807 if (align == 0)
15808 {
15809 complaint (_("DW_AT_alignment value must not be zero"
15810 " - DIE at %s [in module %s]"),
15811 sect_offset_str (die->sect_off),
15812 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15813 return 0;
15814 }
15815 if ((align & (align - 1)) != 0)
15816 {
15817 complaint (_("DW_AT_alignment value must be a power of 2"
15818 " - DIE at %s [in module %s]"),
15819 sect_offset_str (die->sect_off),
15820 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15821 return 0;
15822 }
15823
15824 return align;
15825 }
15826
15827 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15828 the alignment for TYPE. */
15829
15830 static void
15831 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15832 struct type *type)
15833 {
15834 if (!set_type_align (type, get_alignment (cu, die)))
15835 complaint (_("DW_AT_alignment value too large"
15836 " - DIE at %s [in module %s]"),
15837 sect_offset_str (die->sect_off),
15838 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15839 }
15840
15841 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15842 constant for a type, according to DWARF5 spec, Table 5.5. */
15843
15844 static bool
15845 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15846 {
15847 switch (value)
15848 {
15849 case DW_CC_normal:
15850 case DW_CC_pass_by_reference:
15851 case DW_CC_pass_by_value:
15852 return true;
15853
15854 default:
15855 complaint (_("unrecognized DW_AT_calling_convention value "
15856 "(%s) for a type"), pulongest (value));
15857 return false;
15858 }
15859 }
15860
15861 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15862 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15863 also according to GNU-specific values (see include/dwarf2.h). */
15864
15865 static bool
15866 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15867 {
15868 switch (value)
15869 {
15870 case DW_CC_normal:
15871 case DW_CC_program:
15872 case DW_CC_nocall:
15873 return true;
15874
15875 case DW_CC_GNU_renesas_sh:
15876 case DW_CC_GNU_borland_fastcall_i386:
15877 case DW_CC_GDB_IBM_OpenCL:
15878 return true;
15879
15880 default:
15881 complaint (_("unrecognized DW_AT_calling_convention value "
15882 "(%s) for a subroutine"), pulongest (value));
15883 return false;
15884 }
15885 }
15886
15887 /* Called when we find the DIE that starts a structure or union scope
15888 (definition) to create a type for the structure or union. Fill in
15889 the type's name and general properties; the members will not be
15890 processed until process_structure_scope. A symbol table entry for
15891 the type will also not be done until process_structure_scope (assuming
15892 the type has a name).
15893
15894 NOTE: we need to call these functions regardless of whether or not the
15895 DIE has a DW_AT_name attribute, since it might be an anonymous
15896 structure or union. This gets the type entered into our set of
15897 user defined types. */
15898
15899 static struct type *
15900 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15901 {
15902 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15903 struct type *type;
15904 struct attribute *attr;
15905 const char *name;
15906
15907 /* If the definition of this type lives in .debug_types, read that type.
15908 Don't follow DW_AT_specification though, that will take us back up
15909 the chain and we want to go down. */
15910 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15911 if (attr != nullptr)
15912 {
15913 type = get_DW_AT_signature_type (die, attr, cu);
15914
15915 /* The type's CU may not be the same as CU.
15916 Ensure TYPE is recorded with CU in die_type_hash. */
15917 return set_die_type (die, type, cu);
15918 }
15919
15920 type = alloc_type (objfile);
15921 INIT_CPLUS_SPECIFIC (type);
15922
15923 name = dwarf2_name (die, cu);
15924 if (name != NULL)
15925 {
15926 if (cu->language == language_cplus
15927 || cu->language == language_d
15928 || cu->language == language_rust)
15929 {
15930 const char *full_name = dwarf2_full_name (name, die, cu);
15931
15932 /* dwarf2_full_name might have already finished building the DIE's
15933 type. If so, there is no need to continue. */
15934 if (get_die_type (die, cu) != NULL)
15935 return get_die_type (die, cu);
15936
15937 TYPE_NAME (type) = full_name;
15938 }
15939 else
15940 {
15941 /* The name is already allocated along with this objfile, so
15942 we don't need to duplicate it for the type. */
15943 TYPE_NAME (type) = name;
15944 }
15945 }
15946
15947 if (die->tag == DW_TAG_structure_type)
15948 {
15949 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15950 }
15951 else if (die->tag == DW_TAG_union_type)
15952 {
15953 TYPE_CODE (type) = TYPE_CODE_UNION;
15954 }
15955 else if (die->tag == DW_TAG_variant_part)
15956 {
15957 TYPE_CODE (type) = TYPE_CODE_UNION;
15958 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15959 }
15960 else
15961 {
15962 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15963 }
15964
15965 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15966 TYPE_DECLARED_CLASS (type) = 1;
15967
15968 /* Store the calling convention in the type if it's available in
15969 the die. Otherwise the calling convention remains set to
15970 the default value DW_CC_normal. */
15971 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15972 if (attr != nullptr
15973 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15974 {
15975 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15976 TYPE_CPLUS_CALLING_CONVENTION (type)
15977 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15978 }
15979
15980 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15981 if (attr != nullptr)
15982 {
15983 if (attr_form_is_constant (attr))
15984 TYPE_LENGTH (type) = DW_UNSND (attr);
15985 else
15986 {
15987 /* For the moment, dynamic type sizes are not supported
15988 by GDB's struct type. The actual size is determined
15989 on-demand when resolving the type of a given object,
15990 so set the type's length to zero for now. Otherwise,
15991 we record an expression as the length, and that expression
15992 could lead to a very large value, which could eventually
15993 lead to us trying to allocate that much memory when creating
15994 a value of that type. */
15995 TYPE_LENGTH (type) = 0;
15996 }
15997 }
15998 else
15999 {
16000 TYPE_LENGTH (type) = 0;
16001 }
16002
16003 maybe_set_alignment (cu, die, type);
16004
16005 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16006 {
16007 /* ICC<14 does not output the required DW_AT_declaration on
16008 incomplete types, but gives them a size of zero. */
16009 TYPE_STUB (type) = 1;
16010 }
16011 else
16012 TYPE_STUB_SUPPORTED (type) = 1;
16013
16014 if (die_is_declaration (die, cu))
16015 TYPE_STUB (type) = 1;
16016 else if (attr == NULL && die->child == NULL
16017 && producer_is_realview (cu->producer))
16018 /* RealView does not output the required DW_AT_declaration
16019 on incomplete types. */
16020 TYPE_STUB (type) = 1;
16021
16022 /* We need to add the type field to the die immediately so we don't
16023 infinitely recurse when dealing with pointers to the structure
16024 type within the structure itself. */
16025 set_die_type (die, type, cu);
16026
16027 /* set_die_type should be already done. */
16028 set_descriptive_type (type, die, cu);
16029
16030 return type;
16031 }
16032
16033 /* A helper for process_structure_scope that handles a single member
16034 DIE. */
16035
16036 static void
16037 handle_struct_member_die (struct die_info *child_die, struct type *type,
16038 struct field_info *fi,
16039 std::vector<struct symbol *> *template_args,
16040 struct dwarf2_cu *cu)
16041 {
16042 if (child_die->tag == DW_TAG_member
16043 || child_die->tag == DW_TAG_variable
16044 || child_die->tag == DW_TAG_variant_part)
16045 {
16046 /* NOTE: carlton/2002-11-05: A C++ static data member
16047 should be a DW_TAG_member that is a declaration, but
16048 all versions of G++ as of this writing (so through at
16049 least 3.2.1) incorrectly generate DW_TAG_variable
16050 tags for them instead. */
16051 dwarf2_add_field (fi, child_die, cu);
16052 }
16053 else if (child_die->tag == DW_TAG_subprogram)
16054 {
16055 /* Rust doesn't have member functions in the C++ sense.
16056 However, it does emit ordinary functions as children
16057 of a struct DIE. */
16058 if (cu->language == language_rust)
16059 read_func_scope (child_die, cu);
16060 else
16061 {
16062 /* C++ member function. */
16063 dwarf2_add_member_fn (fi, child_die, type, cu);
16064 }
16065 }
16066 else if (child_die->tag == DW_TAG_inheritance)
16067 {
16068 /* C++ base class field. */
16069 dwarf2_add_field (fi, child_die, cu);
16070 }
16071 else if (type_can_define_types (child_die))
16072 dwarf2_add_type_defn (fi, child_die, cu);
16073 else if (child_die->tag == DW_TAG_template_type_param
16074 || child_die->tag == DW_TAG_template_value_param)
16075 {
16076 struct symbol *arg = new_symbol (child_die, NULL, cu);
16077
16078 if (arg != NULL)
16079 template_args->push_back (arg);
16080 }
16081 else if (child_die->tag == DW_TAG_variant)
16082 {
16083 /* In a variant we want to get the discriminant and also add a
16084 field for our sole member child. */
16085 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16086
16087 for (die_info *variant_child = child_die->child;
16088 variant_child != NULL;
16089 variant_child = sibling_die (variant_child))
16090 {
16091 if (variant_child->tag == DW_TAG_member)
16092 {
16093 handle_struct_member_die (variant_child, type, fi,
16094 template_args, cu);
16095 /* Only handle the one. */
16096 break;
16097 }
16098 }
16099
16100 /* We don't handle this but we might as well report it if we see
16101 it. */
16102 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
16103 complaint (_("DW_AT_discr_list is not supported yet"
16104 " - DIE at %s [in module %s]"),
16105 sect_offset_str (child_die->sect_off),
16106 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16107
16108 /* The first field was just added, so we can stash the
16109 discriminant there. */
16110 gdb_assert (!fi->fields.empty ());
16111 if (discr == NULL)
16112 fi->fields.back ().variant.default_branch = true;
16113 else
16114 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
16115 }
16116 }
16117
16118 /* Finish creating a structure or union type, including filling in
16119 its members and creating a symbol for it. */
16120
16121 static void
16122 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16123 {
16124 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16125 struct die_info *child_die;
16126 struct type *type;
16127
16128 type = get_die_type (die, cu);
16129 if (type == NULL)
16130 type = read_structure_type (die, cu);
16131
16132 /* When reading a DW_TAG_variant_part, we need to notice when we
16133 read the discriminant member, so we can record it later in the
16134 discriminant_info. */
16135 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16136 sect_offset discr_offset {};
16137 bool has_template_parameters = false;
16138
16139 if (is_variant_part)
16140 {
16141 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16142 if (discr == NULL)
16143 {
16144 /* Maybe it's a univariant form, an extension we support.
16145 In this case arrange not to check the offset. */
16146 is_variant_part = false;
16147 }
16148 else if (attr_form_is_ref (discr))
16149 {
16150 struct dwarf2_cu *target_cu = cu;
16151 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16152
16153 discr_offset = target_die->sect_off;
16154 }
16155 else
16156 {
16157 complaint (_("DW_AT_discr does not have DIE reference form"
16158 " - DIE at %s [in module %s]"),
16159 sect_offset_str (die->sect_off),
16160 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16161 is_variant_part = false;
16162 }
16163 }
16164
16165 if (die->child != NULL && ! die_is_declaration (die, cu))
16166 {
16167 struct field_info fi;
16168 std::vector<struct symbol *> template_args;
16169
16170 child_die = die->child;
16171
16172 while (child_die && child_die->tag)
16173 {
16174 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16175
16176 if (is_variant_part && discr_offset == child_die->sect_off)
16177 fi.fields.back ().variant.is_discriminant = true;
16178
16179 child_die = sibling_die (child_die);
16180 }
16181
16182 /* Attach template arguments to type. */
16183 if (!template_args.empty ())
16184 {
16185 has_template_parameters = true;
16186 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16187 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16188 TYPE_TEMPLATE_ARGUMENTS (type)
16189 = XOBNEWVEC (&objfile->objfile_obstack,
16190 struct symbol *,
16191 TYPE_N_TEMPLATE_ARGUMENTS (type));
16192 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16193 template_args.data (),
16194 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16195 * sizeof (struct symbol *)));
16196 }
16197
16198 /* Attach fields and member functions to the type. */
16199 if (fi.nfields)
16200 dwarf2_attach_fields_to_type (&fi, type, cu);
16201 if (!fi.fnfieldlists.empty ())
16202 {
16203 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16204
16205 /* Get the type which refers to the base class (possibly this
16206 class itself) which contains the vtable pointer for the current
16207 class from the DW_AT_containing_type attribute. This use of
16208 DW_AT_containing_type is a GNU extension. */
16209
16210 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16211 {
16212 struct type *t = die_containing_type (die, cu);
16213
16214 set_type_vptr_basetype (type, t);
16215 if (type == t)
16216 {
16217 int i;
16218
16219 /* Our own class provides vtbl ptr. */
16220 for (i = TYPE_NFIELDS (t) - 1;
16221 i >= TYPE_N_BASECLASSES (t);
16222 --i)
16223 {
16224 const char *fieldname = TYPE_FIELD_NAME (t, i);
16225
16226 if (is_vtable_name (fieldname, cu))
16227 {
16228 set_type_vptr_fieldno (type, i);
16229 break;
16230 }
16231 }
16232
16233 /* Complain if virtual function table field not found. */
16234 if (i < TYPE_N_BASECLASSES (t))
16235 complaint (_("virtual function table pointer "
16236 "not found when defining class '%s'"),
16237 TYPE_NAME (type) ? TYPE_NAME (type) : "");
16238 }
16239 else
16240 {
16241 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16242 }
16243 }
16244 else if (cu->producer
16245 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16246 {
16247 /* The IBM XLC compiler does not provide direct indication
16248 of the containing type, but the vtable pointer is
16249 always named __vfp. */
16250
16251 int i;
16252
16253 for (i = TYPE_NFIELDS (type) - 1;
16254 i >= TYPE_N_BASECLASSES (type);
16255 --i)
16256 {
16257 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16258 {
16259 set_type_vptr_fieldno (type, i);
16260 set_type_vptr_basetype (type, type);
16261 break;
16262 }
16263 }
16264 }
16265 }
16266
16267 /* Copy fi.typedef_field_list linked list elements content into the
16268 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16269 if (!fi.typedef_field_list.empty ())
16270 {
16271 int count = fi.typedef_field_list.size ();
16272
16273 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16274 TYPE_TYPEDEF_FIELD_ARRAY (type)
16275 = ((struct decl_field *)
16276 TYPE_ALLOC (type,
16277 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16278 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16279
16280 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16281 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16282 }
16283
16284 /* Copy fi.nested_types_list linked list elements content into the
16285 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16286 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16287 {
16288 int count = fi.nested_types_list.size ();
16289
16290 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16291 TYPE_NESTED_TYPES_ARRAY (type)
16292 = ((struct decl_field *)
16293 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16294 TYPE_NESTED_TYPES_COUNT (type) = count;
16295
16296 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16297 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16298 }
16299 }
16300
16301 quirk_gcc_member_function_pointer (type, objfile);
16302 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16303 cu->rust_unions.push_back (type);
16304
16305 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16306 snapshots) has been known to create a die giving a declaration
16307 for a class that has, as a child, a die giving a definition for a
16308 nested class. So we have to process our children even if the
16309 current die is a declaration. Normally, of course, a declaration
16310 won't have any children at all. */
16311
16312 child_die = die->child;
16313
16314 while (child_die != NULL && child_die->tag)
16315 {
16316 if (child_die->tag == DW_TAG_member
16317 || child_die->tag == DW_TAG_variable
16318 || child_die->tag == DW_TAG_inheritance
16319 || child_die->tag == DW_TAG_template_value_param
16320 || child_die->tag == DW_TAG_template_type_param)
16321 {
16322 /* Do nothing. */
16323 }
16324 else
16325 process_die (child_die, cu);
16326
16327 child_die = sibling_die (child_die);
16328 }
16329
16330 /* Do not consider external references. According to the DWARF standard,
16331 these DIEs are identified by the fact that they have no byte_size
16332 attribute, and a declaration attribute. */
16333 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16334 || !die_is_declaration (die, cu))
16335 {
16336 struct symbol *sym = new_symbol (die, type, cu);
16337
16338 if (has_template_parameters)
16339 {
16340 struct symtab *symtab;
16341 if (sym != nullptr)
16342 symtab = symbol_symtab (sym);
16343 else if (cu->line_header != nullptr)
16344 {
16345 /* Any related symtab will do. */
16346 symtab
16347 = cu->line_header->file_names ()[0].symtab;
16348 }
16349 else
16350 {
16351 symtab = nullptr;
16352 complaint (_("could not find suitable "
16353 "symtab for template parameter"
16354 " - DIE at %s [in module %s]"),
16355 sect_offset_str (die->sect_off),
16356 objfile_name (objfile));
16357 }
16358
16359 if (symtab != nullptr)
16360 {
16361 /* Make sure that the symtab is set on the new symbols.
16362 Even though they don't appear in this symtab directly,
16363 other parts of gdb assume that symbols do, and this is
16364 reasonably true. */
16365 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16366 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16367 }
16368 }
16369 }
16370 }
16371
16372 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16373 update TYPE using some information only available in DIE's children. */
16374
16375 static void
16376 update_enumeration_type_from_children (struct die_info *die,
16377 struct type *type,
16378 struct dwarf2_cu *cu)
16379 {
16380 struct die_info *child_die;
16381 int unsigned_enum = 1;
16382 int flag_enum = 1;
16383 ULONGEST mask = 0;
16384
16385 auto_obstack obstack;
16386
16387 for (child_die = die->child;
16388 child_die != NULL && child_die->tag;
16389 child_die = sibling_die (child_die))
16390 {
16391 struct attribute *attr;
16392 LONGEST value;
16393 const gdb_byte *bytes;
16394 struct dwarf2_locexpr_baton *baton;
16395 const char *name;
16396
16397 if (child_die->tag != DW_TAG_enumerator)
16398 continue;
16399
16400 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16401 if (attr == NULL)
16402 continue;
16403
16404 name = dwarf2_name (child_die, cu);
16405 if (name == NULL)
16406 name = "<anonymous enumerator>";
16407
16408 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16409 &value, &bytes, &baton);
16410 if (value < 0)
16411 {
16412 unsigned_enum = 0;
16413 flag_enum = 0;
16414 }
16415 else if ((mask & value) != 0)
16416 flag_enum = 0;
16417 else
16418 mask |= value;
16419
16420 /* If we already know that the enum type is neither unsigned, nor
16421 a flag type, no need to look at the rest of the enumerates. */
16422 if (!unsigned_enum && !flag_enum)
16423 break;
16424 }
16425
16426 if (unsigned_enum)
16427 TYPE_UNSIGNED (type) = 1;
16428 if (flag_enum)
16429 TYPE_FLAG_ENUM (type) = 1;
16430 }
16431
16432 /* Given a DW_AT_enumeration_type die, set its type. We do not
16433 complete the type's fields yet, or create any symbols. */
16434
16435 static struct type *
16436 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16437 {
16438 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16439 struct type *type;
16440 struct attribute *attr;
16441 const char *name;
16442
16443 /* If the definition of this type lives in .debug_types, read that type.
16444 Don't follow DW_AT_specification though, that will take us back up
16445 the chain and we want to go down. */
16446 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16447 if (attr != nullptr)
16448 {
16449 type = get_DW_AT_signature_type (die, attr, cu);
16450
16451 /* The type's CU may not be the same as CU.
16452 Ensure TYPE is recorded with CU in die_type_hash. */
16453 return set_die_type (die, type, cu);
16454 }
16455
16456 type = alloc_type (objfile);
16457
16458 TYPE_CODE (type) = TYPE_CODE_ENUM;
16459 name = dwarf2_full_name (NULL, die, cu);
16460 if (name != NULL)
16461 TYPE_NAME (type) = name;
16462
16463 attr = dwarf2_attr (die, DW_AT_type, cu);
16464 if (attr != NULL)
16465 {
16466 struct type *underlying_type = die_type (die, cu);
16467
16468 TYPE_TARGET_TYPE (type) = underlying_type;
16469 }
16470
16471 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16472 if (attr != nullptr)
16473 {
16474 TYPE_LENGTH (type) = DW_UNSND (attr);
16475 }
16476 else
16477 {
16478 TYPE_LENGTH (type) = 0;
16479 }
16480
16481 maybe_set_alignment (cu, die, type);
16482
16483 /* The enumeration DIE can be incomplete. In Ada, any type can be
16484 declared as private in the package spec, and then defined only
16485 inside the package body. Such types are known as Taft Amendment
16486 Types. When another package uses such a type, an incomplete DIE
16487 may be generated by the compiler. */
16488 if (die_is_declaration (die, cu))
16489 TYPE_STUB (type) = 1;
16490
16491 /* Finish the creation of this type by using the enum's children.
16492 We must call this even when the underlying type has been provided
16493 so that we can determine if we're looking at a "flag" enum. */
16494 update_enumeration_type_from_children (die, type, cu);
16495
16496 /* If this type has an underlying type that is not a stub, then we
16497 may use its attributes. We always use the "unsigned" attribute
16498 in this situation, because ordinarily we guess whether the type
16499 is unsigned -- but the guess can be wrong and the underlying type
16500 can tell us the reality. However, we defer to a local size
16501 attribute if one exists, because this lets the compiler override
16502 the underlying type if needed. */
16503 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16504 {
16505 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16506 if (TYPE_LENGTH (type) == 0)
16507 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16508 if (TYPE_RAW_ALIGN (type) == 0
16509 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16510 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16511 }
16512
16513 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16514
16515 return set_die_type (die, type, cu);
16516 }
16517
16518 /* Given a pointer to a die which begins an enumeration, process all
16519 the dies that define the members of the enumeration, and create the
16520 symbol for the enumeration type.
16521
16522 NOTE: We reverse the order of the element list. */
16523
16524 static void
16525 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16526 {
16527 struct type *this_type;
16528
16529 this_type = get_die_type (die, cu);
16530 if (this_type == NULL)
16531 this_type = read_enumeration_type (die, cu);
16532
16533 if (die->child != NULL)
16534 {
16535 struct die_info *child_die;
16536 struct symbol *sym;
16537 std::vector<struct field> fields;
16538 const char *name;
16539
16540 child_die = die->child;
16541 while (child_die && child_die->tag)
16542 {
16543 if (child_die->tag != DW_TAG_enumerator)
16544 {
16545 process_die (child_die, cu);
16546 }
16547 else
16548 {
16549 name = dwarf2_name (child_die, cu);
16550 if (name)
16551 {
16552 sym = new_symbol (child_die, this_type, cu);
16553
16554 fields.emplace_back ();
16555 struct field &field = fields.back ();
16556
16557 FIELD_NAME (field) = sym->linkage_name ();
16558 FIELD_TYPE (field) = NULL;
16559 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
16560 FIELD_BITSIZE (field) = 0;
16561 }
16562 }
16563
16564 child_die = sibling_die (child_die);
16565 }
16566
16567 if (!fields.empty ())
16568 {
16569 TYPE_NFIELDS (this_type) = fields.size ();
16570 TYPE_FIELDS (this_type) = (struct field *)
16571 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
16572 memcpy (TYPE_FIELDS (this_type), fields.data (),
16573 sizeof (struct field) * fields.size ());
16574 }
16575 }
16576
16577 /* If we are reading an enum from a .debug_types unit, and the enum
16578 is a declaration, and the enum is not the signatured type in the
16579 unit, then we do not want to add a symbol for it. Adding a
16580 symbol would in some cases obscure the true definition of the
16581 enum, giving users an incomplete type when the definition is
16582 actually available. Note that we do not want to do this for all
16583 enums which are just declarations, because C++0x allows forward
16584 enum declarations. */
16585 if (cu->per_cu->is_debug_types
16586 && die_is_declaration (die, cu))
16587 {
16588 struct signatured_type *sig_type;
16589
16590 sig_type = (struct signatured_type *) cu->per_cu;
16591 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16592 if (sig_type->type_offset_in_section != die->sect_off)
16593 return;
16594 }
16595
16596 new_symbol (die, this_type, cu);
16597 }
16598
16599 /* Extract all information from a DW_TAG_array_type DIE and put it in
16600 the DIE's type field. For now, this only handles one dimensional
16601 arrays. */
16602
16603 static struct type *
16604 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16605 {
16606 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16607 struct die_info *child_die;
16608 struct type *type;
16609 struct type *element_type, *range_type, *index_type;
16610 struct attribute *attr;
16611 const char *name;
16612 struct dynamic_prop *byte_stride_prop = NULL;
16613 unsigned int bit_stride = 0;
16614
16615 element_type = die_type (die, cu);
16616
16617 /* The die_type call above may have already set the type for this DIE. */
16618 type = get_die_type (die, cu);
16619 if (type)
16620 return type;
16621
16622 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16623 if (attr != NULL)
16624 {
16625 int stride_ok;
16626 struct type *prop_type
16627 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
16628
16629 byte_stride_prop
16630 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16631 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16632 prop_type);
16633 if (!stride_ok)
16634 {
16635 complaint (_("unable to read array DW_AT_byte_stride "
16636 " - DIE at %s [in module %s]"),
16637 sect_offset_str (die->sect_off),
16638 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16639 /* Ignore this attribute. We will likely not be able to print
16640 arrays of this type correctly, but there is little we can do
16641 to help if we cannot read the attribute's value. */
16642 byte_stride_prop = NULL;
16643 }
16644 }
16645
16646 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16647 if (attr != NULL)
16648 bit_stride = DW_UNSND (attr);
16649
16650 /* Irix 6.2 native cc creates array types without children for
16651 arrays with unspecified length. */
16652 if (die->child == NULL)
16653 {
16654 index_type = objfile_type (objfile)->builtin_int;
16655 range_type = create_static_range_type (NULL, index_type, 0, -1);
16656 type = create_array_type_with_stride (NULL, element_type, range_type,
16657 byte_stride_prop, bit_stride);
16658 return set_die_type (die, type, cu);
16659 }
16660
16661 std::vector<struct type *> range_types;
16662 child_die = die->child;
16663 while (child_die && child_die->tag)
16664 {
16665 if (child_die->tag == DW_TAG_subrange_type)
16666 {
16667 struct type *child_type = read_type_die (child_die, cu);
16668
16669 if (child_type != NULL)
16670 {
16671 /* The range type was succesfully read. Save it for the
16672 array type creation. */
16673 range_types.push_back (child_type);
16674 }
16675 }
16676 child_die = sibling_die (child_die);
16677 }
16678
16679 /* Dwarf2 dimensions are output from left to right, create the
16680 necessary array types in backwards order. */
16681
16682 type = element_type;
16683
16684 if (read_array_order (die, cu) == DW_ORD_col_major)
16685 {
16686 int i = 0;
16687
16688 while (i < range_types.size ())
16689 type = create_array_type_with_stride (NULL, type, range_types[i++],
16690 byte_stride_prop, bit_stride);
16691 }
16692 else
16693 {
16694 size_t ndim = range_types.size ();
16695 while (ndim-- > 0)
16696 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16697 byte_stride_prop, bit_stride);
16698 }
16699
16700 /* Understand Dwarf2 support for vector types (like they occur on
16701 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16702 array type. This is not part of the Dwarf2/3 standard yet, but a
16703 custom vendor extension. The main difference between a regular
16704 array and the vector variant is that vectors are passed by value
16705 to functions. */
16706 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16707 if (attr != nullptr)
16708 make_vector_type (type);
16709
16710 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16711 implementation may choose to implement triple vectors using this
16712 attribute. */
16713 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16714 if (attr != nullptr)
16715 {
16716 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16717 TYPE_LENGTH (type) = DW_UNSND (attr);
16718 else
16719 complaint (_("DW_AT_byte_size for array type smaller "
16720 "than the total size of elements"));
16721 }
16722
16723 name = dwarf2_name (die, cu);
16724 if (name)
16725 TYPE_NAME (type) = name;
16726
16727 maybe_set_alignment (cu, die, type);
16728
16729 /* Install the type in the die. */
16730 set_die_type (die, type, cu);
16731
16732 /* set_die_type should be already done. */
16733 set_descriptive_type (type, die, cu);
16734
16735 return type;
16736 }
16737
16738 static enum dwarf_array_dim_ordering
16739 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16740 {
16741 struct attribute *attr;
16742
16743 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16744
16745 if (attr != nullptr)
16746 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16747
16748 /* GNU F77 is a special case, as at 08/2004 array type info is the
16749 opposite order to the dwarf2 specification, but data is still
16750 laid out as per normal fortran.
16751
16752 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16753 version checking. */
16754
16755 if (cu->language == language_fortran
16756 && cu->producer && strstr (cu->producer, "GNU F77"))
16757 {
16758 return DW_ORD_row_major;
16759 }
16760
16761 switch (cu->language_defn->la_array_ordering)
16762 {
16763 case array_column_major:
16764 return DW_ORD_col_major;
16765 case array_row_major:
16766 default:
16767 return DW_ORD_row_major;
16768 };
16769 }
16770
16771 /* Extract all information from a DW_TAG_set_type DIE and put it in
16772 the DIE's type field. */
16773
16774 static struct type *
16775 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16776 {
16777 struct type *domain_type, *set_type;
16778 struct attribute *attr;
16779
16780 domain_type = die_type (die, cu);
16781
16782 /* The die_type call above may have already set the type for this DIE. */
16783 set_type = get_die_type (die, cu);
16784 if (set_type)
16785 return set_type;
16786
16787 set_type = create_set_type (NULL, domain_type);
16788
16789 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16790 if (attr != nullptr)
16791 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16792
16793 maybe_set_alignment (cu, die, set_type);
16794
16795 return set_die_type (die, set_type, cu);
16796 }
16797
16798 /* A helper for read_common_block that creates a locexpr baton.
16799 SYM is the symbol which we are marking as computed.
16800 COMMON_DIE is the DIE for the common block.
16801 COMMON_LOC is the location expression attribute for the common
16802 block itself.
16803 MEMBER_LOC is the location expression attribute for the particular
16804 member of the common block that we are processing.
16805 CU is the CU from which the above come. */
16806
16807 static void
16808 mark_common_block_symbol_computed (struct symbol *sym,
16809 struct die_info *common_die,
16810 struct attribute *common_loc,
16811 struct attribute *member_loc,
16812 struct dwarf2_cu *cu)
16813 {
16814 struct dwarf2_per_objfile *dwarf2_per_objfile
16815 = cu->per_cu->dwarf2_per_objfile;
16816 struct objfile *objfile = dwarf2_per_objfile->objfile;
16817 struct dwarf2_locexpr_baton *baton;
16818 gdb_byte *ptr;
16819 unsigned int cu_off;
16820 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16821 LONGEST offset = 0;
16822
16823 gdb_assert (common_loc && member_loc);
16824 gdb_assert (attr_form_is_block (common_loc));
16825 gdb_assert (attr_form_is_block (member_loc)
16826 || attr_form_is_constant (member_loc));
16827
16828 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16829 baton->per_cu = cu->per_cu;
16830 gdb_assert (baton->per_cu);
16831
16832 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16833
16834 if (attr_form_is_constant (member_loc))
16835 {
16836 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16837 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16838 }
16839 else
16840 baton->size += DW_BLOCK (member_loc)->size;
16841
16842 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16843 baton->data = ptr;
16844
16845 *ptr++ = DW_OP_call4;
16846 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16847 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16848 ptr += 4;
16849
16850 if (attr_form_is_constant (member_loc))
16851 {
16852 *ptr++ = DW_OP_addr;
16853 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16854 ptr += cu->header.addr_size;
16855 }
16856 else
16857 {
16858 /* We have to copy the data here, because DW_OP_call4 will only
16859 use a DW_AT_location attribute. */
16860 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16861 ptr += DW_BLOCK (member_loc)->size;
16862 }
16863
16864 *ptr++ = DW_OP_plus;
16865 gdb_assert (ptr - baton->data == baton->size);
16866
16867 SYMBOL_LOCATION_BATON (sym) = baton;
16868 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16869 }
16870
16871 /* Create appropriate locally-scoped variables for all the
16872 DW_TAG_common_block entries. Also create a struct common_block
16873 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16874 is used to separate the common blocks name namespace from regular
16875 variable names. */
16876
16877 static void
16878 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16879 {
16880 struct attribute *attr;
16881
16882 attr = dwarf2_attr (die, DW_AT_location, cu);
16883 if (attr != nullptr)
16884 {
16885 /* Support the .debug_loc offsets. */
16886 if (attr_form_is_block (attr))
16887 {
16888 /* Ok. */
16889 }
16890 else if (attr_form_is_section_offset (attr))
16891 {
16892 dwarf2_complex_location_expr_complaint ();
16893 attr = NULL;
16894 }
16895 else
16896 {
16897 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16898 "common block member");
16899 attr = NULL;
16900 }
16901 }
16902
16903 if (die->child != NULL)
16904 {
16905 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16906 struct die_info *child_die;
16907 size_t n_entries = 0, size;
16908 struct common_block *common_block;
16909 struct symbol *sym;
16910
16911 for (child_die = die->child;
16912 child_die && child_die->tag;
16913 child_die = sibling_die (child_die))
16914 ++n_entries;
16915
16916 size = (sizeof (struct common_block)
16917 + (n_entries - 1) * sizeof (struct symbol *));
16918 common_block
16919 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16920 size);
16921 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16922 common_block->n_entries = 0;
16923
16924 for (child_die = die->child;
16925 child_die && child_die->tag;
16926 child_die = sibling_die (child_die))
16927 {
16928 /* Create the symbol in the DW_TAG_common_block block in the current
16929 symbol scope. */
16930 sym = new_symbol (child_die, NULL, cu);
16931 if (sym != NULL)
16932 {
16933 struct attribute *member_loc;
16934
16935 common_block->contents[common_block->n_entries++] = sym;
16936
16937 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16938 cu);
16939 if (member_loc)
16940 {
16941 /* GDB has handled this for a long time, but it is
16942 not specified by DWARF. It seems to have been
16943 emitted by gfortran at least as recently as:
16944 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16945 complaint (_("Variable in common block has "
16946 "DW_AT_data_member_location "
16947 "- DIE at %s [in module %s]"),
16948 sect_offset_str (child_die->sect_off),
16949 objfile_name (objfile));
16950
16951 if (attr_form_is_section_offset (member_loc))
16952 dwarf2_complex_location_expr_complaint ();
16953 else if (attr_form_is_constant (member_loc)
16954 || attr_form_is_block (member_loc))
16955 {
16956 if (attr != nullptr)
16957 mark_common_block_symbol_computed (sym, die, attr,
16958 member_loc, cu);
16959 }
16960 else
16961 dwarf2_complex_location_expr_complaint ();
16962 }
16963 }
16964 }
16965
16966 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16967 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16968 }
16969 }
16970
16971 /* Create a type for a C++ namespace. */
16972
16973 static struct type *
16974 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16975 {
16976 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16977 const char *previous_prefix, *name;
16978 int is_anonymous;
16979 struct type *type;
16980
16981 /* For extensions, reuse the type of the original namespace. */
16982 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16983 {
16984 struct die_info *ext_die;
16985 struct dwarf2_cu *ext_cu = cu;
16986
16987 ext_die = dwarf2_extension (die, &ext_cu);
16988 type = read_type_die (ext_die, ext_cu);
16989
16990 /* EXT_CU may not be the same as CU.
16991 Ensure TYPE is recorded with CU in die_type_hash. */
16992 return set_die_type (die, type, cu);
16993 }
16994
16995 name = namespace_name (die, &is_anonymous, cu);
16996
16997 /* Now build the name of the current namespace. */
16998
16999 previous_prefix = determine_prefix (die, cu);
17000 if (previous_prefix[0] != '\0')
17001 name = typename_concat (&objfile->objfile_obstack,
17002 previous_prefix, name, 0, cu);
17003
17004 /* Create the type. */
17005 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17006
17007 return set_die_type (die, type, cu);
17008 }
17009
17010 /* Read a namespace scope. */
17011
17012 static void
17013 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17014 {
17015 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17016 int is_anonymous;
17017
17018 /* Add a symbol associated to this if we haven't seen the namespace
17019 before. Also, add a using directive if it's an anonymous
17020 namespace. */
17021
17022 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17023 {
17024 struct type *type;
17025
17026 type = read_type_die (die, cu);
17027 new_symbol (die, type, cu);
17028
17029 namespace_name (die, &is_anonymous, cu);
17030 if (is_anonymous)
17031 {
17032 const char *previous_prefix = determine_prefix (die, cu);
17033
17034 std::vector<const char *> excludes;
17035 add_using_directive (using_directives (cu),
17036 previous_prefix, TYPE_NAME (type), NULL,
17037 NULL, excludes, 0, &objfile->objfile_obstack);
17038 }
17039 }
17040
17041 if (die->child != NULL)
17042 {
17043 struct die_info *child_die = die->child;
17044
17045 while (child_die && child_die->tag)
17046 {
17047 process_die (child_die, cu);
17048 child_die = sibling_die (child_die);
17049 }
17050 }
17051 }
17052
17053 /* Read a Fortran module as type. This DIE can be only a declaration used for
17054 imported module. Still we need that type as local Fortran "use ... only"
17055 declaration imports depend on the created type in determine_prefix. */
17056
17057 static struct type *
17058 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17059 {
17060 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17061 const char *module_name;
17062 struct type *type;
17063
17064 module_name = dwarf2_name (die, cu);
17065 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17066
17067 return set_die_type (die, type, cu);
17068 }
17069
17070 /* Read a Fortran module. */
17071
17072 static void
17073 read_module (struct die_info *die, struct dwarf2_cu *cu)
17074 {
17075 struct die_info *child_die = die->child;
17076 struct type *type;
17077
17078 type = read_type_die (die, cu);
17079 new_symbol (die, type, cu);
17080
17081 while (child_die && child_die->tag)
17082 {
17083 process_die (child_die, cu);
17084 child_die = sibling_die (child_die);
17085 }
17086 }
17087
17088 /* Return the name of the namespace represented by DIE. Set
17089 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17090 namespace. */
17091
17092 static const char *
17093 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17094 {
17095 struct die_info *current_die;
17096 const char *name = NULL;
17097
17098 /* Loop through the extensions until we find a name. */
17099
17100 for (current_die = die;
17101 current_die != NULL;
17102 current_die = dwarf2_extension (die, &cu))
17103 {
17104 /* We don't use dwarf2_name here so that we can detect the absence
17105 of a name -> anonymous namespace. */
17106 name = dwarf2_string_attr (die, DW_AT_name, cu);
17107
17108 if (name != NULL)
17109 break;
17110 }
17111
17112 /* Is it an anonymous namespace? */
17113
17114 *is_anonymous = (name == NULL);
17115 if (*is_anonymous)
17116 name = CP_ANONYMOUS_NAMESPACE_STR;
17117
17118 return name;
17119 }
17120
17121 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17122 the user defined type vector. */
17123
17124 static struct type *
17125 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17126 {
17127 struct gdbarch *gdbarch
17128 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
17129 struct comp_unit_head *cu_header = &cu->header;
17130 struct type *type;
17131 struct attribute *attr_byte_size;
17132 struct attribute *attr_address_class;
17133 int byte_size, addr_class;
17134 struct type *target_type;
17135
17136 target_type = die_type (die, cu);
17137
17138 /* The die_type call above may have already set the type for this DIE. */
17139 type = get_die_type (die, cu);
17140 if (type)
17141 return type;
17142
17143 type = lookup_pointer_type (target_type);
17144
17145 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17146 if (attr_byte_size)
17147 byte_size = DW_UNSND (attr_byte_size);
17148 else
17149 byte_size = cu_header->addr_size;
17150
17151 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17152 if (attr_address_class)
17153 addr_class = DW_UNSND (attr_address_class);
17154 else
17155 addr_class = DW_ADDR_none;
17156
17157 ULONGEST alignment = get_alignment (cu, die);
17158
17159 /* If the pointer size, alignment, or address class is different
17160 than the default, create a type variant marked as such and set
17161 the length accordingly. */
17162 if (TYPE_LENGTH (type) != byte_size
17163 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17164 && alignment != TYPE_RAW_ALIGN (type))
17165 || addr_class != DW_ADDR_none)
17166 {
17167 if (gdbarch_address_class_type_flags_p (gdbarch))
17168 {
17169 int type_flags;
17170
17171 type_flags = gdbarch_address_class_type_flags
17172 (gdbarch, byte_size, addr_class);
17173 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17174 == 0);
17175 type = make_type_with_address_space (type, type_flags);
17176 }
17177 else if (TYPE_LENGTH (type) != byte_size)
17178 {
17179 complaint (_("invalid pointer size %d"), byte_size);
17180 }
17181 else if (TYPE_RAW_ALIGN (type) != alignment)
17182 {
17183 complaint (_("Invalid DW_AT_alignment"
17184 " - DIE at %s [in module %s]"),
17185 sect_offset_str (die->sect_off),
17186 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17187 }
17188 else
17189 {
17190 /* Should we also complain about unhandled address classes? */
17191 }
17192 }
17193
17194 TYPE_LENGTH (type) = byte_size;
17195 set_type_align (type, alignment);
17196 return set_die_type (die, type, cu);
17197 }
17198
17199 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17200 the user defined type vector. */
17201
17202 static struct type *
17203 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17204 {
17205 struct type *type;
17206 struct type *to_type;
17207 struct type *domain;
17208
17209 to_type = die_type (die, cu);
17210 domain = die_containing_type (die, cu);
17211
17212 /* The calls above may have already set the type for this DIE. */
17213 type = get_die_type (die, cu);
17214 if (type)
17215 return type;
17216
17217 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17218 type = lookup_methodptr_type (to_type);
17219 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17220 {
17221 struct type *new_type
17222 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17223
17224 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17225 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17226 TYPE_VARARGS (to_type));
17227 type = lookup_methodptr_type (new_type);
17228 }
17229 else
17230 type = lookup_memberptr_type (to_type, domain);
17231
17232 return set_die_type (die, type, cu);
17233 }
17234
17235 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17236 the user defined type vector. */
17237
17238 static struct type *
17239 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17240 enum type_code refcode)
17241 {
17242 struct comp_unit_head *cu_header = &cu->header;
17243 struct type *type, *target_type;
17244 struct attribute *attr;
17245
17246 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17247
17248 target_type = die_type (die, cu);
17249
17250 /* The die_type call above may have already set the type for this DIE. */
17251 type = get_die_type (die, cu);
17252 if (type)
17253 return type;
17254
17255 type = lookup_reference_type (target_type, refcode);
17256 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17257 if (attr != nullptr)
17258 {
17259 TYPE_LENGTH (type) = DW_UNSND (attr);
17260 }
17261 else
17262 {
17263 TYPE_LENGTH (type) = cu_header->addr_size;
17264 }
17265 maybe_set_alignment (cu, die, type);
17266 return set_die_type (die, type, cu);
17267 }
17268
17269 /* Add the given cv-qualifiers to the element type of the array. GCC
17270 outputs DWARF type qualifiers that apply to an array, not the
17271 element type. But GDB relies on the array element type to carry
17272 the cv-qualifiers. This mimics section 6.7.3 of the C99
17273 specification. */
17274
17275 static struct type *
17276 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17277 struct type *base_type, int cnst, int voltl)
17278 {
17279 struct type *el_type, *inner_array;
17280
17281 base_type = copy_type (base_type);
17282 inner_array = base_type;
17283
17284 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17285 {
17286 TYPE_TARGET_TYPE (inner_array) =
17287 copy_type (TYPE_TARGET_TYPE (inner_array));
17288 inner_array = TYPE_TARGET_TYPE (inner_array);
17289 }
17290
17291 el_type = TYPE_TARGET_TYPE (inner_array);
17292 cnst |= TYPE_CONST (el_type);
17293 voltl |= TYPE_VOLATILE (el_type);
17294 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17295
17296 return set_die_type (die, base_type, cu);
17297 }
17298
17299 static struct type *
17300 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17301 {
17302 struct type *base_type, *cv_type;
17303
17304 base_type = die_type (die, cu);
17305
17306 /* The die_type call above may have already set the type for this DIE. */
17307 cv_type = get_die_type (die, cu);
17308 if (cv_type)
17309 return cv_type;
17310
17311 /* In case the const qualifier is applied to an array type, the element type
17312 is so qualified, not the array type (section 6.7.3 of C99). */
17313 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17314 return add_array_cv_type (die, cu, base_type, 1, 0);
17315
17316 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17317 return set_die_type (die, cv_type, cu);
17318 }
17319
17320 static struct type *
17321 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17322 {
17323 struct type *base_type, *cv_type;
17324
17325 base_type = die_type (die, cu);
17326
17327 /* The die_type call above may have already set the type for this DIE. */
17328 cv_type = get_die_type (die, cu);
17329 if (cv_type)
17330 return cv_type;
17331
17332 /* In case the volatile qualifier is applied to an array type, the
17333 element type is so qualified, not the array type (section 6.7.3
17334 of C99). */
17335 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17336 return add_array_cv_type (die, cu, base_type, 0, 1);
17337
17338 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17339 return set_die_type (die, cv_type, cu);
17340 }
17341
17342 /* Handle DW_TAG_restrict_type. */
17343
17344 static struct type *
17345 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17346 {
17347 struct type *base_type, *cv_type;
17348
17349 base_type = die_type (die, cu);
17350
17351 /* The die_type call above may have already set the type for this DIE. */
17352 cv_type = get_die_type (die, cu);
17353 if (cv_type)
17354 return cv_type;
17355
17356 cv_type = make_restrict_type (base_type);
17357 return set_die_type (die, cv_type, cu);
17358 }
17359
17360 /* Handle DW_TAG_atomic_type. */
17361
17362 static struct type *
17363 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17364 {
17365 struct type *base_type, *cv_type;
17366
17367 base_type = die_type (die, cu);
17368
17369 /* The die_type call above may have already set the type for this DIE. */
17370 cv_type = get_die_type (die, cu);
17371 if (cv_type)
17372 return cv_type;
17373
17374 cv_type = make_atomic_type (base_type);
17375 return set_die_type (die, cv_type, cu);
17376 }
17377
17378 /* Extract all information from a DW_TAG_string_type DIE and add to
17379 the user defined type vector. It isn't really a user defined type,
17380 but it behaves like one, with other DIE's using an AT_user_def_type
17381 attribute to reference it. */
17382
17383 static struct type *
17384 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17385 {
17386 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17387 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17388 struct type *type, *range_type, *index_type, *char_type;
17389 struct attribute *attr;
17390 struct dynamic_prop prop;
17391 bool length_is_constant = true;
17392 LONGEST length;
17393
17394 /* There are a couple of places where bit sizes might be made use of
17395 when parsing a DW_TAG_string_type, however, no producer that we know
17396 of make use of these. Handling bit sizes that are a multiple of the
17397 byte size is easy enough, but what about other bit sizes? Lets deal
17398 with that problem when we have to. Warn about these attributes being
17399 unsupported, then parse the type and ignore them like we always
17400 have. */
17401 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17402 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17403 {
17404 static bool warning_printed = false;
17405 if (!warning_printed)
17406 {
17407 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17408 "currently supported on DW_TAG_string_type."));
17409 warning_printed = true;
17410 }
17411 }
17412
17413 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17414 if (attr != nullptr && !attr_form_is_constant (attr))
17415 {
17416 /* The string length describes the location at which the length of
17417 the string can be found. The size of the length field can be
17418 specified with one of the attributes below. */
17419 struct type *prop_type;
17420 struct attribute *len
17421 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17422 if (len == nullptr)
17423 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17424 if (len != nullptr && attr_form_is_constant (len))
17425 {
17426 /* Pass 0 as the default as we know this attribute is constant
17427 and the default value will not be returned. */
17428 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17429 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17430 }
17431 else
17432 {
17433 /* If the size is not specified then we assume it is the size of
17434 an address on this target. */
17435 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17436 }
17437
17438 /* Convert the attribute into a dynamic property. */
17439 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17440 length = 1;
17441 else
17442 length_is_constant = false;
17443 }
17444 else if (attr != nullptr)
17445 {
17446 /* This DW_AT_string_length just contains the length with no
17447 indirection. There's no need to create a dynamic property in this
17448 case. Pass 0 for the default value as we know it will not be
17449 returned in this case. */
17450 length = dwarf2_get_attr_constant_value (attr, 0);
17451 }
17452 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17453 {
17454 /* We don't currently support non-constant byte sizes for strings. */
17455 length = dwarf2_get_attr_constant_value (attr, 1);
17456 }
17457 else
17458 {
17459 /* Use 1 as a fallback length if we have nothing else. */
17460 length = 1;
17461 }
17462
17463 index_type = objfile_type (objfile)->builtin_int;
17464 if (length_is_constant)
17465 range_type = create_static_range_type (NULL, index_type, 1, length);
17466 else
17467 {
17468 struct dynamic_prop low_bound;
17469
17470 low_bound.kind = PROP_CONST;
17471 low_bound.data.const_val = 1;
17472 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17473 }
17474 char_type = language_string_char_type (cu->language_defn, gdbarch);
17475 type = create_string_type (NULL, char_type, range_type);
17476
17477 return set_die_type (die, type, cu);
17478 }
17479
17480 /* Assuming that DIE corresponds to a function, returns nonzero
17481 if the function is prototyped. */
17482
17483 static int
17484 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17485 {
17486 struct attribute *attr;
17487
17488 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17489 if (attr && (DW_UNSND (attr) != 0))
17490 return 1;
17491
17492 /* The DWARF standard implies that the DW_AT_prototyped attribute
17493 is only meaningful for C, but the concept also extends to other
17494 languages that allow unprototyped functions (Eg: Objective C).
17495 For all other languages, assume that functions are always
17496 prototyped. */
17497 if (cu->language != language_c
17498 && cu->language != language_objc
17499 && cu->language != language_opencl)
17500 return 1;
17501
17502 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17503 prototyped and unprototyped functions; default to prototyped,
17504 since that is more common in modern code (and RealView warns
17505 about unprototyped functions). */
17506 if (producer_is_realview (cu->producer))
17507 return 1;
17508
17509 return 0;
17510 }
17511
17512 /* Handle DIES due to C code like:
17513
17514 struct foo
17515 {
17516 int (*funcp)(int a, long l);
17517 int b;
17518 };
17519
17520 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17521
17522 static struct type *
17523 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17524 {
17525 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17526 struct type *type; /* Type that this function returns. */
17527 struct type *ftype; /* Function that returns above type. */
17528 struct attribute *attr;
17529
17530 type = die_type (die, cu);
17531
17532 /* The die_type call above may have already set the type for this DIE. */
17533 ftype = get_die_type (die, cu);
17534 if (ftype)
17535 return ftype;
17536
17537 ftype = lookup_function_type (type);
17538
17539 if (prototyped_function_p (die, cu))
17540 TYPE_PROTOTYPED (ftype) = 1;
17541
17542 /* Store the calling convention in the type if it's available in
17543 the subroutine die. Otherwise set the calling convention to
17544 the default value DW_CC_normal. */
17545 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17546 if (attr != nullptr
17547 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17548 TYPE_CALLING_CONVENTION (ftype)
17549 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17550 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17551 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17552 else
17553 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17554
17555 /* Record whether the function returns normally to its caller or not
17556 if the DWARF producer set that information. */
17557 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17558 if (attr && (DW_UNSND (attr) != 0))
17559 TYPE_NO_RETURN (ftype) = 1;
17560
17561 /* We need to add the subroutine type to the die immediately so
17562 we don't infinitely recurse when dealing with parameters
17563 declared as the same subroutine type. */
17564 set_die_type (die, ftype, cu);
17565
17566 if (die->child != NULL)
17567 {
17568 struct type *void_type = objfile_type (objfile)->builtin_void;
17569 struct die_info *child_die;
17570 int nparams, iparams;
17571
17572 /* Count the number of parameters.
17573 FIXME: GDB currently ignores vararg functions, but knows about
17574 vararg member functions. */
17575 nparams = 0;
17576 child_die = die->child;
17577 while (child_die && child_die->tag)
17578 {
17579 if (child_die->tag == DW_TAG_formal_parameter)
17580 nparams++;
17581 else if (child_die->tag == DW_TAG_unspecified_parameters)
17582 TYPE_VARARGS (ftype) = 1;
17583 child_die = sibling_die (child_die);
17584 }
17585
17586 /* Allocate storage for parameters and fill them in. */
17587 TYPE_NFIELDS (ftype) = nparams;
17588 TYPE_FIELDS (ftype) = (struct field *)
17589 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17590
17591 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17592 even if we error out during the parameters reading below. */
17593 for (iparams = 0; iparams < nparams; iparams++)
17594 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17595
17596 iparams = 0;
17597 child_die = die->child;
17598 while (child_die && child_die->tag)
17599 {
17600 if (child_die->tag == DW_TAG_formal_parameter)
17601 {
17602 struct type *arg_type;
17603
17604 /* DWARF version 2 has no clean way to discern C++
17605 static and non-static member functions. G++ helps
17606 GDB by marking the first parameter for non-static
17607 member functions (which is the this pointer) as
17608 artificial. We pass this information to
17609 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17610
17611 DWARF version 3 added DW_AT_object_pointer, which GCC
17612 4.5 does not yet generate. */
17613 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17614 if (attr != nullptr)
17615 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17616 else
17617 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17618 arg_type = die_type (child_die, cu);
17619
17620 /* RealView does not mark THIS as const, which the testsuite
17621 expects. GCC marks THIS as const in method definitions,
17622 but not in the class specifications (GCC PR 43053). */
17623 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17624 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17625 {
17626 int is_this = 0;
17627 struct dwarf2_cu *arg_cu = cu;
17628 const char *name = dwarf2_name (child_die, cu);
17629
17630 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17631 if (attr != nullptr)
17632 {
17633 /* If the compiler emits this, use it. */
17634 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17635 is_this = 1;
17636 }
17637 else if (name && strcmp (name, "this") == 0)
17638 /* Function definitions will have the argument names. */
17639 is_this = 1;
17640 else if (name == NULL && iparams == 0)
17641 /* Declarations may not have the names, so like
17642 elsewhere in GDB, assume an artificial first
17643 argument is "this". */
17644 is_this = 1;
17645
17646 if (is_this)
17647 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17648 arg_type, 0);
17649 }
17650
17651 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17652 iparams++;
17653 }
17654 child_die = sibling_die (child_die);
17655 }
17656 }
17657
17658 return ftype;
17659 }
17660
17661 static struct type *
17662 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17663 {
17664 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17665 const char *name = NULL;
17666 struct type *this_type, *target_type;
17667
17668 name = dwarf2_full_name (NULL, die, cu);
17669 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17670 TYPE_TARGET_STUB (this_type) = 1;
17671 set_die_type (die, this_type, cu);
17672 target_type = die_type (die, cu);
17673 if (target_type != this_type)
17674 TYPE_TARGET_TYPE (this_type) = target_type;
17675 else
17676 {
17677 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17678 spec and cause infinite loops in GDB. */
17679 complaint (_("Self-referential DW_TAG_typedef "
17680 "- DIE at %s [in module %s]"),
17681 sect_offset_str (die->sect_off), objfile_name (objfile));
17682 TYPE_TARGET_TYPE (this_type) = NULL;
17683 }
17684 return this_type;
17685 }
17686
17687 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17688 (which may be different from NAME) to the architecture back-end to allow
17689 it to guess the correct format if necessary. */
17690
17691 static struct type *
17692 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17693 const char *name_hint, enum bfd_endian byte_order)
17694 {
17695 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17696 const struct floatformat **format;
17697 struct type *type;
17698
17699 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17700 if (format)
17701 type = init_float_type (objfile, bits, name, format, byte_order);
17702 else
17703 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17704
17705 return type;
17706 }
17707
17708 /* Allocate an integer type of size BITS and name NAME. */
17709
17710 static struct type *
17711 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17712 int bits, int unsigned_p, const char *name)
17713 {
17714 struct type *type;
17715
17716 /* Versions of Intel's C Compiler generate an integer type called "void"
17717 instead of using DW_TAG_unspecified_type. This has been seen on
17718 at least versions 14, 17, and 18. */
17719 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17720 && strcmp (name, "void") == 0)
17721 type = objfile_type (objfile)->builtin_void;
17722 else
17723 type = init_integer_type (objfile, bits, unsigned_p, name);
17724
17725 return type;
17726 }
17727
17728 /* Initialise and return a floating point type of size BITS suitable for
17729 use as a component of a complex number. The NAME_HINT is passed through
17730 when initialising the floating point type and is the name of the complex
17731 type.
17732
17733 As DWARF doesn't currently provide an explicit name for the components
17734 of a complex number, but it can be helpful to have these components
17735 named, we try to select a suitable name based on the size of the
17736 component. */
17737 static struct type *
17738 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17739 struct objfile *objfile,
17740 int bits, const char *name_hint,
17741 enum bfd_endian byte_order)
17742 {
17743 gdbarch *gdbarch = get_objfile_arch (objfile);
17744 struct type *tt = nullptr;
17745
17746 /* Try to find a suitable floating point builtin type of size BITS.
17747 We're going to use the name of this type as the name for the complex
17748 target type that we are about to create. */
17749 switch (cu->language)
17750 {
17751 case language_fortran:
17752 switch (bits)
17753 {
17754 case 32:
17755 tt = builtin_f_type (gdbarch)->builtin_real;
17756 break;
17757 case 64:
17758 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17759 break;
17760 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17761 case 128:
17762 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17763 break;
17764 }
17765 break;
17766 default:
17767 switch (bits)
17768 {
17769 case 32:
17770 tt = builtin_type (gdbarch)->builtin_float;
17771 break;
17772 case 64:
17773 tt = builtin_type (gdbarch)->builtin_double;
17774 break;
17775 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17776 case 128:
17777 tt = builtin_type (gdbarch)->builtin_long_double;
17778 break;
17779 }
17780 break;
17781 }
17782
17783 /* If the type we found doesn't match the size we were looking for, then
17784 pretend we didn't find a type at all, the complex target type we
17785 create will then be nameless. */
17786 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17787 tt = nullptr;
17788
17789 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17790 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17791 }
17792
17793 /* Find a representation of a given base type and install
17794 it in the TYPE field of the die. */
17795
17796 static struct type *
17797 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17798 {
17799 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17800 struct type *type;
17801 struct attribute *attr;
17802 int encoding = 0, bits = 0;
17803 const char *name;
17804 gdbarch *arch;
17805
17806 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17807 if (attr != nullptr)
17808 encoding = DW_UNSND (attr);
17809 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17810 if (attr != nullptr)
17811 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17812 name = dwarf2_name (die, cu);
17813 if (!name)
17814 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17815
17816 arch = get_objfile_arch (objfile);
17817 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17818
17819 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17820 if (attr)
17821 {
17822 int endianity = DW_UNSND (attr);
17823
17824 switch (endianity)
17825 {
17826 case DW_END_big:
17827 byte_order = BFD_ENDIAN_BIG;
17828 break;
17829 case DW_END_little:
17830 byte_order = BFD_ENDIAN_LITTLE;
17831 break;
17832 default:
17833 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17834 break;
17835 }
17836 }
17837
17838 switch (encoding)
17839 {
17840 case DW_ATE_address:
17841 /* Turn DW_ATE_address into a void * pointer. */
17842 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17843 type = init_pointer_type (objfile, bits, name, type);
17844 break;
17845 case DW_ATE_boolean:
17846 type = init_boolean_type (objfile, bits, 1, name);
17847 break;
17848 case DW_ATE_complex_float:
17849 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17850 byte_order);
17851 type = init_complex_type (objfile, name, type);
17852 break;
17853 case DW_ATE_decimal_float:
17854 type = init_decfloat_type (objfile, bits, name);
17855 break;
17856 case DW_ATE_float:
17857 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17858 break;
17859 case DW_ATE_signed:
17860 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17861 break;
17862 case DW_ATE_unsigned:
17863 if (cu->language == language_fortran
17864 && name
17865 && startswith (name, "character("))
17866 type = init_character_type (objfile, bits, 1, name);
17867 else
17868 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17869 break;
17870 case DW_ATE_signed_char:
17871 if (cu->language == language_ada || cu->language == language_m2
17872 || cu->language == language_pascal
17873 || cu->language == language_fortran)
17874 type = init_character_type (objfile, bits, 0, name);
17875 else
17876 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17877 break;
17878 case DW_ATE_unsigned_char:
17879 if (cu->language == language_ada || cu->language == language_m2
17880 || cu->language == language_pascal
17881 || cu->language == language_fortran
17882 || cu->language == language_rust)
17883 type = init_character_type (objfile, bits, 1, name);
17884 else
17885 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17886 break;
17887 case DW_ATE_UTF:
17888 {
17889 if (bits == 16)
17890 type = builtin_type (arch)->builtin_char16;
17891 else if (bits == 32)
17892 type = builtin_type (arch)->builtin_char32;
17893 else
17894 {
17895 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17896 bits);
17897 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17898 }
17899 return set_die_type (die, type, cu);
17900 }
17901 break;
17902
17903 default:
17904 complaint (_("unsupported DW_AT_encoding: '%s'"),
17905 dwarf_type_encoding_name (encoding));
17906 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17907 break;
17908 }
17909
17910 if (name && strcmp (name, "char") == 0)
17911 TYPE_NOSIGN (type) = 1;
17912
17913 maybe_set_alignment (cu, die, type);
17914
17915 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17916
17917 return set_die_type (die, type, cu);
17918 }
17919
17920 /* Parse dwarf attribute if it's a block, reference or constant and put the
17921 resulting value of the attribute into struct bound_prop.
17922 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17923
17924 static int
17925 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17926 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17927 struct type *default_type)
17928 {
17929 struct dwarf2_property_baton *baton;
17930 struct obstack *obstack
17931 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17932
17933 gdb_assert (default_type != NULL);
17934
17935 if (attr == NULL || prop == NULL)
17936 return 0;
17937
17938 if (attr_form_is_block (attr))
17939 {
17940 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17941 baton->property_type = default_type;
17942 baton->locexpr.per_cu = cu->per_cu;
17943 baton->locexpr.size = DW_BLOCK (attr)->size;
17944 baton->locexpr.data = DW_BLOCK (attr)->data;
17945 switch (attr->name)
17946 {
17947 case DW_AT_string_length:
17948 baton->locexpr.is_reference = true;
17949 break;
17950 default:
17951 baton->locexpr.is_reference = false;
17952 break;
17953 }
17954 prop->data.baton = baton;
17955 prop->kind = PROP_LOCEXPR;
17956 gdb_assert (prop->data.baton != NULL);
17957 }
17958 else if (attr_form_is_ref (attr))
17959 {
17960 struct dwarf2_cu *target_cu = cu;
17961 struct die_info *target_die;
17962 struct attribute *target_attr;
17963
17964 target_die = follow_die_ref (die, attr, &target_cu);
17965 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17966 if (target_attr == NULL)
17967 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17968 target_cu);
17969 if (target_attr == NULL)
17970 return 0;
17971
17972 switch (target_attr->name)
17973 {
17974 case DW_AT_location:
17975 if (attr_form_is_section_offset (target_attr))
17976 {
17977 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17978 baton->property_type = die_type (target_die, target_cu);
17979 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17980 prop->data.baton = baton;
17981 prop->kind = PROP_LOCLIST;
17982 gdb_assert (prop->data.baton != NULL);
17983 }
17984 else if (attr_form_is_block (target_attr))
17985 {
17986 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17987 baton->property_type = die_type (target_die, target_cu);
17988 baton->locexpr.per_cu = cu->per_cu;
17989 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17990 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17991 baton->locexpr.is_reference = true;
17992 prop->data.baton = baton;
17993 prop->kind = PROP_LOCEXPR;
17994 gdb_assert (prop->data.baton != NULL);
17995 }
17996 else
17997 {
17998 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17999 "dynamic property");
18000 return 0;
18001 }
18002 break;
18003 case DW_AT_data_member_location:
18004 {
18005 LONGEST offset;
18006
18007 if (!handle_data_member_location (target_die, target_cu,
18008 &offset))
18009 return 0;
18010
18011 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18012 baton->property_type = read_type_die (target_die->parent,
18013 target_cu);
18014 baton->offset_info.offset = offset;
18015 baton->offset_info.type = die_type (target_die, target_cu);
18016 prop->data.baton = baton;
18017 prop->kind = PROP_ADDR_OFFSET;
18018 break;
18019 }
18020 }
18021 }
18022 else if (attr_form_is_constant (attr))
18023 {
18024 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
18025 prop->kind = PROP_CONST;
18026 }
18027 else
18028 {
18029 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18030 dwarf2_name (die, cu));
18031 return 0;
18032 }
18033
18034 return 1;
18035 }
18036
18037 /* Find an integer type SIZE_IN_BYTES bytes in size and return it.
18038 UNSIGNED_P controls if the integer is unsigned or not. */
18039
18040 static struct type *
18041 dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
18042 int size_in_bytes, bool unsigned_p)
18043 {
18044 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
18045 struct type *int_type;
18046
18047 /* Helper macro to examine the various builtin types. */
18048 #define TRY_TYPE(F) \
18049 int_type = (unsigned_p \
18050 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18051 : objfile_type (objfile)->builtin_ ## F); \
18052 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18053 return int_type
18054
18055 TRY_TYPE (char);
18056 TRY_TYPE (short);
18057 TRY_TYPE (int);
18058 TRY_TYPE (long);
18059 TRY_TYPE (long_long);
18060
18061 #undef TRY_TYPE
18062
18063 gdb_assert_not_reached ("unable to find suitable integer type");
18064 }
18065
18066 /* Find an integer type the same size as the address size given in the
18067 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
18068 is unsigned or not. */
18069
18070 static struct type *
18071 dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
18072 bool unsigned_p)
18073 {
18074 int addr_size = dwarf2_per_cu_addr_size (per_cu);
18075 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
18076 }
18077
18078 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18079 present (which is valid) then compute the default type based on the
18080 compilation units address size. */
18081
18082 static struct type *
18083 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18084 {
18085 struct type *index_type = die_type (die, cu);
18086
18087 /* Dwarf-2 specifications explicitly allows to create subrange types
18088 without specifying a base type.
18089 In that case, the base type must be set to the type of
18090 the lower bound, upper bound or count, in that order, if any of these
18091 three attributes references an object that has a type.
18092 If no base type is found, the Dwarf-2 specifications say that
18093 a signed integer type of size equal to the size of an address should
18094 be used.
18095 For the following C code: `extern char gdb_int [];'
18096 GCC produces an empty range DIE.
18097 FIXME: muller/2010-05-28: Possible references to object for low bound,
18098 high bound or count are not yet handled by this code. */
18099 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
18100 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18101
18102 return index_type;
18103 }
18104
18105 /* Read the given DW_AT_subrange DIE. */
18106
18107 static struct type *
18108 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18109 {
18110 struct type *base_type, *orig_base_type;
18111 struct type *range_type;
18112 struct attribute *attr;
18113 struct dynamic_prop low, high;
18114 int low_default_is_valid;
18115 int high_bound_is_count = 0;
18116 const char *name;
18117 ULONGEST negative_mask;
18118
18119 orig_base_type = read_subrange_index_type (die, cu);
18120
18121 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18122 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18123 creating the range type, but we use the result of check_typedef
18124 when examining properties of the type. */
18125 base_type = check_typedef (orig_base_type);
18126
18127 /* The die_type call above may have already set the type for this DIE. */
18128 range_type = get_die_type (die, cu);
18129 if (range_type)
18130 return range_type;
18131
18132 low.kind = PROP_CONST;
18133 high.kind = PROP_CONST;
18134 high.data.const_val = 0;
18135
18136 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18137 omitting DW_AT_lower_bound. */
18138 switch (cu->language)
18139 {
18140 case language_c:
18141 case language_cplus:
18142 low.data.const_val = 0;
18143 low_default_is_valid = 1;
18144 break;
18145 case language_fortran:
18146 low.data.const_val = 1;
18147 low_default_is_valid = 1;
18148 break;
18149 case language_d:
18150 case language_objc:
18151 case language_rust:
18152 low.data.const_val = 0;
18153 low_default_is_valid = (cu->header.version >= 4);
18154 break;
18155 case language_ada:
18156 case language_m2:
18157 case language_pascal:
18158 low.data.const_val = 1;
18159 low_default_is_valid = (cu->header.version >= 4);
18160 break;
18161 default:
18162 low.data.const_val = 0;
18163 low_default_is_valid = 0;
18164 break;
18165 }
18166
18167 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18168 if (attr != nullptr)
18169 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18170 else if (!low_default_is_valid)
18171 complaint (_("Missing DW_AT_lower_bound "
18172 "- DIE at %s [in module %s]"),
18173 sect_offset_str (die->sect_off),
18174 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18175
18176 struct attribute *attr_ub, *attr_count;
18177 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18178 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18179 {
18180 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18181 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18182 {
18183 /* If bounds are constant do the final calculation here. */
18184 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18185 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18186 else
18187 high_bound_is_count = 1;
18188 }
18189 else
18190 {
18191 if (attr_ub != NULL)
18192 complaint (_("Unresolved DW_AT_upper_bound "
18193 "- DIE at %s [in module %s]"),
18194 sect_offset_str (die->sect_off),
18195 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18196 if (attr_count != NULL)
18197 complaint (_("Unresolved DW_AT_count "
18198 "- DIE at %s [in module %s]"),
18199 sect_offset_str (die->sect_off),
18200 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18201 }
18202 }
18203
18204 LONGEST bias = 0;
18205 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18206 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18207 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18208
18209 /* Normally, the DWARF producers are expected to use a signed
18210 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18211 But this is unfortunately not always the case, as witnessed
18212 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18213 is used instead. To work around that ambiguity, we treat
18214 the bounds as signed, and thus sign-extend their values, when
18215 the base type is signed. */
18216 negative_mask =
18217 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18218 if (low.kind == PROP_CONST
18219 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18220 low.data.const_val |= negative_mask;
18221 if (high.kind == PROP_CONST
18222 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18223 high.data.const_val |= negative_mask;
18224
18225 /* Check for bit and byte strides. */
18226 struct dynamic_prop byte_stride_prop;
18227 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18228 if (attr_byte_stride != nullptr)
18229 {
18230 struct type *prop_type
18231 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18232 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18233 prop_type);
18234 }
18235
18236 struct dynamic_prop bit_stride_prop;
18237 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18238 if (attr_bit_stride != nullptr)
18239 {
18240 /* It only makes sense to have either a bit or byte stride. */
18241 if (attr_byte_stride != nullptr)
18242 {
18243 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18244 "- DIE at %s [in module %s]"),
18245 sect_offset_str (die->sect_off),
18246 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18247 attr_bit_stride = nullptr;
18248 }
18249 else
18250 {
18251 struct type *prop_type
18252 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18253 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18254 prop_type);
18255 }
18256 }
18257
18258 if (attr_byte_stride != nullptr
18259 || attr_bit_stride != nullptr)
18260 {
18261 bool byte_stride_p = (attr_byte_stride != nullptr);
18262 struct dynamic_prop *stride
18263 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18264
18265 range_type
18266 = create_range_type_with_stride (NULL, orig_base_type, &low,
18267 &high, bias, stride, byte_stride_p);
18268 }
18269 else
18270 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18271
18272 if (high_bound_is_count)
18273 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18274
18275 /* Ada expects an empty array on no boundary attributes. */
18276 if (attr == NULL && cu->language != language_ada)
18277 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
18278
18279 name = dwarf2_name (die, cu);
18280 if (name)
18281 TYPE_NAME (range_type) = name;
18282
18283 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18284 if (attr != nullptr)
18285 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18286
18287 maybe_set_alignment (cu, die, range_type);
18288
18289 set_die_type (die, range_type, cu);
18290
18291 /* set_die_type should be already done. */
18292 set_descriptive_type (range_type, die, cu);
18293
18294 return range_type;
18295 }
18296
18297 static struct type *
18298 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18299 {
18300 struct type *type;
18301
18302 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18303 NULL);
18304 TYPE_NAME (type) = dwarf2_name (die, cu);
18305
18306 /* In Ada, an unspecified type is typically used when the description
18307 of the type is deferred to a different unit. When encountering
18308 such a type, we treat it as a stub, and try to resolve it later on,
18309 when needed. */
18310 if (cu->language == language_ada)
18311 TYPE_STUB (type) = 1;
18312
18313 return set_die_type (die, type, cu);
18314 }
18315
18316 /* Read a single die and all its descendents. Set the die's sibling
18317 field to NULL; set other fields in the die correctly, and set all
18318 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18319 location of the info_ptr after reading all of those dies. PARENT
18320 is the parent of the die in question. */
18321
18322 static struct die_info *
18323 read_die_and_children (const struct die_reader_specs *reader,
18324 const gdb_byte *info_ptr,
18325 const gdb_byte **new_info_ptr,
18326 struct die_info *parent)
18327 {
18328 struct die_info *die;
18329 const gdb_byte *cur_ptr;
18330 int has_children;
18331
18332 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18333 if (die == NULL)
18334 {
18335 *new_info_ptr = cur_ptr;
18336 return NULL;
18337 }
18338 store_in_ref_table (die, reader->cu);
18339
18340 if (has_children)
18341 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18342 else
18343 {
18344 die->child = NULL;
18345 *new_info_ptr = cur_ptr;
18346 }
18347
18348 die->sibling = NULL;
18349 die->parent = parent;
18350 return die;
18351 }
18352
18353 /* Read a die, all of its descendents, and all of its siblings; set
18354 all of the fields of all of the dies correctly. Arguments are as
18355 in read_die_and_children. */
18356
18357 static struct die_info *
18358 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18359 const gdb_byte *info_ptr,
18360 const gdb_byte **new_info_ptr,
18361 struct die_info *parent)
18362 {
18363 struct die_info *first_die, *last_sibling;
18364 const gdb_byte *cur_ptr;
18365
18366 cur_ptr = info_ptr;
18367 first_die = last_sibling = NULL;
18368
18369 while (1)
18370 {
18371 struct die_info *die
18372 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18373
18374 if (die == NULL)
18375 {
18376 *new_info_ptr = cur_ptr;
18377 return first_die;
18378 }
18379
18380 if (!first_die)
18381 first_die = die;
18382 else
18383 last_sibling->sibling = die;
18384
18385 last_sibling = die;
18386 }
18387 }
18388
18389 /* Read a die, all of its descendents, and all of its siblings; set
18390 all of the fields of all of the dies correctly. Arguments are as
18391 in read_die_and_children.
18392 This the main entry point for reading a DIE and all its children. */
18393
18394 static struct die_info *
18395 read_die_and_siblings (const struct die_reader_specs *reader,
18396 const gdb_byte *info_ptr,
18397 const gdb_byte **new_info_ptr,
18398 struct die_info *parent)
18399 {
18400 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18401 new_info_ptr, parent);
18402
18403 if (dwarf_die_debug)
18404 {
18405 fprintf_unfiltered (gdb_stdlog,
18406 "Read die from %s@0x%x of %s:\n",
18407 get_section_name (reader->die_section),
18408 (unsigned) (info_ptr - reader->die_section->buffer),
18409 bfd_get_filename (reader->abfd));
18410 dump_die (die, dwarf_die_debug);
18411 }
18412
18413 return die;
18414 }
18415
18416 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18417 attributes.
18418 The caller is responsible for filling in the extra attributes
18419 and updating (*DIEP)->num_attrs.
18420 Set DIEP to point to a newly allocated die with its information,
18421 except for its child, sibling, and parent fields.
18422 Set HAS_CHILDREN to tell whether the die has children or not. */
18423
18424 static const gdb_byte *
18425 read_full_die_1 (const struct die_reader_specs *reader,
18426 struct die_info **diep, const gdb_byte *info_ptr,
18427 int *has_children, int num_extra_attrs)
18428 {
18429 unsigned int abbrev_number, bytes_read, i;
18430 struct abbrev_info *abbrev;
18431 struct die_info *die;
18432 struct dwarf2_cu *cu = reader->cu;
18433 bfd *abfd = reader->abfd;
18434
18435 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18436 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18437 info_ptr += bytes_read;
18438 if (!abbrev_number)
18439 {
18440 *diep = NULL;
18441 *has_children = 0;
18442 return info_ptr;
18443 }
18444
18445 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18446 if (!abbrev)
18447 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18448 abbrev_number,
18449 bfd_get_filename (abfd));
18450
18451 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18452 die->sect_off = sect_off;
18453 die->tag = abbrev->tag;
18454 die->abbrev = abbrev_number;
18455
18456 /* Make the result usable.
18457 The caller needs to update num_attrs after adding the extra
18458 attributes. */
18459 die->num_attrs = abbrev->num_attrs;
18460
18461 for (i = 0; i < abbrev->num_attrs; ++i)
18462 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18463 info_ptr);
18464
18465 *diep = die;
18466 *has_children = abbrev->has_children;
18467 return info_ptr;
18468 }
18469
18470 /* Read a die and all its attributes.
18471 Set DIEP to point to a newly allocated die with its information,
18472 except for its child, sibling, and parent fields.
18473 Set HAS_CHILDREN to tell whether the die has children or not. */
18474
18475 static const gdb_byte *
18476 read_full_die (const struct die_reader_specs *reader,
18477 struct die_info **diep, const gdb_byte *info_ptr,
18478 int *has_children)
18479 {
18480 const gdb_byte *result;
18481
18482 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18483
18484 if (dwarf_die_debug)
18485 {
18486 fprintf_unfiltered (gdb_stdlog,
18487 "Read die from %s@0x%x of %s:\n",
18488 get_section_name (reader->die_section),
18489 (unsigned) (info_ptr - reader->die_section->buffer),
18490 bfd_get_filename (reader->abfd));
18491 dump_die (*diep, dwarf_die_debug);
18492 }
18493
18494 return result;
18495 }
18496 \f
18497 /* Abbreviation tables.
18498
18499 In DWARF version 2, the description of the debugging information is
18500 stored in a separate .debug_abbrev section. Before we read any
18501 dies from a section we read in all abbreviations and install them
18502 in a hash table. */
18503
18504 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18505
18506 struct abbrev_info *
18507 abbrev_table::alloc_abbrev ()
18508 {
18509 struct abbrev_info *abbrev;
18510
18511 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18512 memset (abbrev, 0, sizeof (struct abbrev_info));
18513
18514 return abbrev;
18515 }
18516
18517 /* Add an abbreviation to the table. */
18518
18519 void
18520 abbrev_table::add_abbrev (unsigned int abbrev_number,
18521 struct abbrev_info *abbrev)
18522 {
18523 unsigned int hash_number;
18524
18525 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18526 abbrev->next = m_abbrevs[hash_number];
18527 m_abbrevs[hash_number] = abbrev;
18528 }
18529
18530 /* Look up an abbrev in the table.
18531 Returns NULL if the abbrev is not found. */
18532
18533 struct abbrev_info *
18534 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18535 {
18536 unsigned int hash_number;
18537 struct abbrev_info *abbrev;
18538
18539 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18540 abbrev = m_abbrevs[hash_number];
18541
18542 while (abbrev)
18543 {
18544 if (abbrev->number == abbrev_number)
18545 return abbrev;
18546 abbrev = abbrev->next;
18547 }
18548 return NULL;
18549 }
18550
18551 /* Read in an abbrev table. */
18552
18553 static abbrev_table_up
18554 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18555 struct dwarf2_section_info *section,
18556 sect_offset sect_off)
18557 {
18558 struct objfile *objfile = dwarf2_per_objfile->objfile;
18559 bfd *abfd = get_section_bfd_owner (section);
18560 const gdb_byte *abbrev_ptr;
18561 struct abbrev_info *cur_abbrev;
18562 unsigned int abbrev_number, bytes_read, abbrev_name;
18563 unsigned int abbrev_form;
18564 struct attr_abbrev *cur_attrs;
18565 unsigned int allocated_attrs;
18566
18567 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18568
18569 dwarf2_read_section (objfile, section);
18570 abbrev_ptr = section->buffer + to_underlying (sect_off);
18571 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18572 abbrev_ptr += bytes_read;
18573
18574 allocated_attrs = ATTR_ALLOC_CHUNK;
18575 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18576
18577 /* Loop until we reach an abbrev number of 0. */
18578 while (abbrev_number)
18579 {
18580 cur_abbrev = abbrev_table->alloc_abbrev ();
18581
18582 /* read in abbrev header */
18583 cur_abbrev->number = abbrev_number;
18584 cur_abbrev->tag
18585 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18586 abbrev_ptr += bytes_read;
18587 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18588 abbrev_ptr += 1;
18589
18590 /* now read in declarations */
18591 for (;;)
18592 {
18593 LONGEST implicit_const;
18594
18595 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18596 abbrev_ptr += bytes_read;
18597 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18598 abbrev_ptr += bytes_read;
18599 if (abbrev_form == DW_FORM_implicit_const)
18600 {
18601 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18602 &bytes_read);
18603 abbrev_ptr += bytes_read;
18604 }
18605 else
18606 {
18607 /* Initialize it due to a false compiler warning. */
18608 implicit_const = -1;
18609 }
18610
18611 if (abbrev_name == 0)
18612 break;
18613
18614 if (cur_abbrev->num_attrs == allocated_attrs)
18615 {
18616 allocated_attrs += ATTR_ALLOC_CHUNK;
18617 cur_attrs
18618 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18619 }
18620
18621 cur_attrs[cur_abbrev->num_attrs].name
18622 = (enum dwarf_attribute) abbrev_name;
18623 cur_attrs[cur_abbrev->num_attrs].form
18624 = (enum dwarf_form) abbrev_form;
18625 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18626 ++cur_abbrev->num_attrs;
18627 }
18628
18629 cur_abbrev->attrs =
18630 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18631 cur_abbrev->num_attrs);
18632 memcpy (cur_abbrev->attrs, cur_attrs,
18633 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18634
18635 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18636
18637 /* Get next abbreviation.
18638 Under Irix6 the abbreviations for a compilation unit are not
18639 always properly terminated with an abbrev number of 0.
18640 Exit loop if we encounter an abbreviation which we have
18641 already read (which means we are about to read the abbreviations
18642 for the next compile unit) or if the end of the abbreviation
18643 table is reached. */
18644 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18645 break;
18646 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18647 abbrev_ptr += bytes_read;
18648 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18649 break;
18650 }
18651
18652 xfree (cur_attrs);
18653 return abbrev_table;
18654 }
18655
18656 /* Returns nonzero if TAG represents a type that we might generate a partial
18657 symbol for. */
18658
18659 static int
18660 is_type_tag_for_partial (int tag)
18661 {
18662 switch (tag)
18663 {
18664 #if 0
18665 /* Some types that would be reasonable to generate partial symbols for,
18666 that we don't at present. */
18667 case DW_TAG_array_type:
18668 case DW_TAG_file_type:
18669 case DW_TAG_ptr_to_member_type:
18670 case DW_TAG_set_type:
18671 case DW_TAG_string_type:
18672 case DW_TAG_subroutine_type:
18673 #endif
18674 case DW_TAG_base_type:
18675 case DW_TAG_class_type:
18676 case DW_TAG_interface_type:
18677 case DW_TAG_enumeration_type:
18678 case DW_TAG_structure_type:
18679 case DW_TAG_subrange_type:
18680 case DW_TAG_typedef:
18681 case DW_TAG_union_type:
18682 return 1;
18683 default:
18684 return 0;
18685 }
18686 }
18687
18688 /* Load all DIEs that are interesting for partial symbols into memory. */
18689
18690 static struct partial_die_info *
18691 load_partial_dies (const struct die_reader_specs *reader,
18692 const gdb_byte *info_ptr, int building_psymtab)
18693 {
18694 struct dwarf2_cu *cu = reader->cu;
18695 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18696 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18697 unsigned int bytes_read;
18698 unsigned int load_all = 0;
18699 int nesting_level = 1;
18700
18701 parent_die = NULL;
18702 last_die = NULL;
18703
18704 gdb_assert (cu->per_cu != NULL);
18705 if (cu->per_cu->load_all_dies)
18706 load_all = 1;
18707
18708 cu->partial_dies
18709 = htab_create_alloc_ex (cu->header.length / 12,
18710 partial_die_hash,
18711 partial_die_eq,
18712 NULL,
18713 &cu->comp_unit_obstack,
18714 hashtab_obstack_allocate,
18715 dummy_obstack_deallocate);
18716
18717 while (1)
18718 {
18719 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18720
18721 /* A NULL abbrev means the end of a series of children. */
18722 if (abbrev == NULL)
18723 {
18724 if (--nesting_level == 0)
18725 return first_die;
18726
18727 info_ptr += bytes_read;
18728 last_die = parent_die;
18729 parent_die = parent_die->die_parent;
18730 continue;
18731 }
18732
18733 /* Check for template arguments. We never save these; if
18734 they're seen, we just mark the parent, and go on our way. */
18735 if (parent_die != NULL
18736 && cu->language == language_cplus
18737 && (abbrev->tag == DW_TAG_template_type_param
18738 || abbrev->tag == DW_TAG_template_value_param))
18739 {
18740 parent_die->has_template_arguments = 1;
18741
18742 if (!load_all)
18743 {
18744 /* We don't need a partial DIE for the template argument. */
18745 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18746 continue;
18747 }
18748 }
18749
18750 /* We only recurse into c++ subprograms looking for template arguments.
18751 Skip their other children. */
18752 if (!load_all
18753 && cu->language == language_cplus
18754 && parent_die != NULL
18755 && parent_die->tag == DW_TAG_subprogram)
18756 {
18757 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18758 continue;
18759 }
18760
18761 /* Check whether this DIE is interesting enough to save. Normally
18762 we would not be interested in members here, but there may be
18763 later variables referencing them via DW_AT_specification (for
18764 static members). */
18765 if (!load_all
18766 && !is_type_tag_for_partial (abbrev->tag)
18767 && abbrev->tag != DW_TAG_constant
18768 && abbrev->tag != DW_TAG_enumerator
18769 && abbrev->tag != DW_TAG_subprogram
18770 && abbrev->tag != DW_TAG_inlined_subroutine
18771 && abbrev->tag != DW_TAG_lexical_block
18772 && abbrev->tag != DW_TAG_variable
18773 && abbrev->tag != DW_TAG_namespace
18774 && abbrev->tag != DW_TAG_module
18775 && abbrev->tag != DW_TAG_member
18776 && abbrev->tag != DW_TAG_imported_unit
18777 && abbrev->tag != DW_TAG_imported_declaration)
18778 {
18779 /* Otherwise we skip to the next sibling, if any. */
18780 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18781 continue;
18782 }
18783
18784 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18785 abbrev);
18786
18787 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18788
18789 /* This two-pass algorithm for processing partial symbols has a
18790 high cost in cache pressure. Thus, handle some simple cases
18791 here which cover the majority of C partial symbols. DIEs
18792 which neither have specification tags in them, nor could have
18793 specification tags elsewhere pointing at them, can simply be
18794 processed and discarded.
18795
18796 This segment is also optional; scan_partial_symbols and
18797 add_partial_symbol will handle these DIEs if we chain
18798 them in normally. When compilers which do not emit large
18799 quantities of duplicate debug information are more common,
18800 this code can probably be removed. */
18801
18802 /* Any complete simple types at the top level (pretty much all
18803 of them, for a language without namespaces), can be processed
18804 directly. */
18805 if (parent_die == NULL
18806 && pdi.has_specification == 0
18807 && pdi.is_declaration == 0
18808 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18809 || pdi.tag == DW_TAG_base_type
18810 || pdi.tag == DW_TAG_subrange_type))
18811 {
18812 if (building_psymtab && pdi.name != NULL)
18813 add_psymbol_to_list (pdi.name, false,
18814 VAR_DOMAIN, LOC_TYPEDEF, -1,
18815 psymbol_placement::STATIC,
18816 0, cu->language, objfile);
18817 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18818 continue;
18819 }
18820
18821 /* The exception for DW_TAG_typedef with has_children above is
18822 a workaround of GCC PR debug/47510. In the case of this complaint
18823 type_name_or_error will error on such types later.
18824
18825 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18826 it could not find the child DIEs referenced later, this is checked
18827 above. In correct DWARF DW_TAG_typedef should have no children. */
18828
18829 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18830 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18831 "- DIE at %s [in module %s]"),
18832 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18833
18834 /* If we're at the second level, and we're an enumerator, and
18835 our parent has no specification (meaning possibly lives in a
18836 namespace elsewhere), then we can add the partial symbol now
18837 instead of queueing it. */
18838 if (pdi.tag == DW_TAG_enumerator
18839 && parent_die != NULL
18840 && parent_die->die_parent == NULL
18841 && parent_die->tag == DW_TAG_enumeration_type
18842 && parent_die->has_specification == 0)
18843 {
18844 if (pdi.name == NULL)
18845 complaint (_("malformed enumerator DIE ignored"));
18846 else if (building_psymtab)
18847 add_psymbol_to_list (pdi.name, false,
18848 VAR_DOMAIN, LOC_CONST, -1,
18849 cu->language == language_cplus
18850 ? psymbol_placement::GLOBAL
18851 : psymbol_placement::STATIC,
18852 0, cu->language, objfile);
18853
18854 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18855 continue;
18856 }
18857
18858 struct partial_die_info *part_die
18859 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18860
18861 /* We'll save this DIE so link it in. */
18862 part_die->die_parent = parent_die;
18863 part_die->die_sibling = NULL;
18864 part_die->die_child = NULL;
18865
18866 if (last_die && last_die == parent_die)
18867 last_die->die_child = part_die;
18868 else if (last_die)
18869 last_die->die_sibling = part_die;
18870
18871 last_die = part_die;
18872
18873 if (first_die == NULL)
18874 first_die = part_die;
18875
18876 /* Maybe add the DIE to the hash table. Not all DIEs that we
18877 find interesting need to be in the hash table, because we
18878 also have the parent/sibling/child chains; only those that we
18879 might refer to by offset later during partial symbol reading.
18880
18881 For now this means things that might have be the target of a
18882 DW_AT_specification, DW_AT_abstract_origin, or
18883 DW_AT_extension. DW_AT_extension will refer only to
18884 namespaces; DW_AT_abstract_origin refers to functions (and
18885 many things under the function DIE, but we do not recurse
18886 into function DIEs during partial symbol reading) and
18887 possibly variables as well; DW_AT_specification refers to
18888 declarations. Declarations ought to have the DW_AT_declaration
18889 flag. It happens that GCC forgets to put it in sometimes, but
18890 only for functions, not for types.
18891
18892 Adding more things than necessary to the hash table is harmless
18893 except for the performance cost. Adding too few will result in
18894 wasted time in find_partial_die, when we reread the compilation
18895 unit with load_all_dies set. */
18896
18897 if (load_all
18898 || abbrev->tag == DW_TAG_constant
18899 || abbrev->tag == DW_TAG_subprogram
18900 || abbrev->tag == DW_TAG_variable
18901 || abbrev->tag == DW_TAG_namespace
18902 || part_die->is_declaration)
18903 {
18904 void **slot;
18905
18906 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18907 to_underlying (part_die->sect_off),
18908 INSERT);
18909 *slot = part_die;
18910 }
18911
18912 /* For some DIEs we want to follow their children (if any). For C
18913 we have no reason to follow the children of structures; for other
18914 languages we have to, so that we can get at method physnames
18915 to infer fully qualified class names, for DW_AT_specification,
18916 and for C++ template arguments. For C++, we also look one level
18917 inside functions to find template arguments (if the name of the
18918 function does not already contain the template arguments).
18919
18920 For Ada and Fortran, we need to scan the children of subprograms
18921 and lexical blocks as well because these languages allow the
18922 definition of nested entities that could be interesting for the
18923 debugger, such as nested subprograms for instance. */
18924 if (last_die->has_children
18925 && (load_all
18926 || last_die->tag == DW_TAG_namespace
18927 || last_die->tag == DW_TAG_module
18928 || last_die->tag == DW_TAG_enumeration_type
18929 || (cu->language == language_cplus
18930 && last_die->tag == DW_TAG_subprogram
18931 && (last_die->name == NULL
18932 || strchr (last_die->name, '<') == NULL))
18933 || (cu->language != language_c
18934 && (last_die->tag == DW_TAG_class_type
18935 || last_die->tag == DW_TAG_interface_type
18936 || last_die->tag == DW_TAG_structure_type
18937 || last_die->tag == DW_TAG_union_type))
18938 || ((cu->language == language_ada
18939 || cu->language == language_fortran)
18940 && (last_die->tag == DW_TAG_subprogram
18941 || last_die->tag == DW_TAG_lexical_block))))
18942 {
18943 nesting_level++;
18944 parent_die = last_die;
18945 continue;
18946 }
18947
18948 /* Otherwise we skip to the next sibling, if any. */
18949 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18950
18951 /* Back to the top, do it again. */
18952 }
18953 }
18954
18955 partial_die_info::partial_die_info (sect_offset sect_off_,
18956 struct abbrev_info *abbrev)
18957 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18958 {
18959 }
18960
18961 /* Read a minimal amount of information into the minimal die structure.
18962 INFO_PTR should point just after the initial uleb128 of a DIE. */
18963
18964 const gdb_byte *
18965 partial_die_info::read (const struct die_reader_specs *reader,
18966 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18967 {
18968 struct dwarf2_cu *cu = reader->cu;
18969 struct dwarf2_per_objfile *dwarf2_per_objfile
18970 = cu->per_cu->dwarf2_per_objfile;
18971 unsigned int i;
18972 int has_low_pc_attr = 0;
18973 int has_high_pc_attr = 0;
18974 int high_pc_relative = 0;
18975
18976 for (i = 0; i < abbrev.num_attrs; ++i)
18977 {
18978 struct attribute attr;
18979
18980 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18981
18982 /* Store the data if it is of an attribute we want to keep in a
18983 partial symbol table. */
18984 switch (attr.name)
18985 {
18986 case DW_AT_name:
18987 switch (tag)
18988 {
18989 case DW_TAG_compile_unit:
18990 case DW_TAG_partial_unit:
18991 case DW_TAG_type_unit:
18992 /* Compilation units have a DW_AT_name that is a filename, not
18993 a source language identifier. */
18994 case DW_TAG_enumeration_type:
18995 case DW_TAG_enumerator:
18996 /* These tags always have simple identifiers already; no need
18997 to canonicalize them. */
18998 name = DW_STRING (&attr);
18999 break;
19000 default:
19001 {
19002 struct objfile *objfile = dwarf2_per_objfile->objfile;
19003
19004 name
19005 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
19006 &objfile->per_bfd->storage_obstack);
19007 }
19008 break;
19009 }
19010 break;
19011 case DW_AT_linkage_name:
19012 case DW_AT_MIPS_linkage_name:
19013 /* Note that both forms of linkage name might appear. We
19014 assume they will be the same, and we only store the last
19015 one we see. */
19016 linkage_name = DW_STRING (&attr);
19017 break;
19018 case DW_AT_low_pc:
19019 has_low_pc_attr = 1;
19020 lowpc = attr_value_as_address (&attr);
19021 break;
19022 case DW_AT_high_pc:
19023 has_high_pc_attr = 1;
19024 highpc = attr_value_as_address (&attr);
19025 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
19026 high_pc_relative = 1;
19027 break;
19028 case DW_AT_location:
19029 /* Support the .debug_loc offsets. */
19030 if (attr_form_is_block (&attr))
19031 {
19032 d.locdesc = DW_BLOCK (&attr);
19033 }
19034 else if (attr_form_is_section_offset (&attr))
19035 {
19036 dwarf2_complex_location_expr_complaint ();
19037 }
19038 else
19039 {
19040 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19041 "partial symbol information");
19042 }
19043 break;
19044 case DW_AT_external:
19045 is_external = DW_UNSND (&attr);
19046 break;
19047 case DW_AT_declaration:
19048 is_declaration = DW_UNSND (&attr);
19049 break;
19050 case DW_AT_type:
19051 has_type = 1;
19052 break;
19053 case DW_AT_abstract_origin:
19054 case DW_AT_specification:
19055 case DW_AT_extension:
19056 has_specification = 1;
19057 spec_offset = dwarf2_get_ref_die_offset (&attr);
19058 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19059 || cu->per_cu->is_dwz);
19060 break;
19061 case DW_AT_sibling:
19062 /* Ignore absolute siblings, they might point outside of
19063 the current compile unit. */
19064 if (attr.form == DW_FORM_ref_addr)
19065 complaint (_("ignoring absolute DW_AT_sibling"));
19066 else
19067 {
19068 const gdb_byte *buffer = reader->buffer;
19069 sect_offset off = dwarf2_get_ref_die_offset (&attr);
19070 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19071
19072 if (sibling_ptr < info_ptr)
19073 complaint (_("DW_AT_sibling points backwards"));
19074 else if (sibling_ptr > reader->buffer_end)
19075 dwarf2_section_buffer_overflow_complaint (reader->die_section);
19076 else
19077 sibling = sibling_ptr;
19078 }
19079 break;
19080 case DW_AT_byte_size:
19081 has_byte_size = 1;
19082 break;
19083 case DW_AT_const_value:
19084 has_const_value = 1;
19085 break;
19086 case DW_AT_calling_convention:
19087 /* DWARF doesn't provide a way to identify a program's source-level
19088 entry point. DW_AT_calling_convention attributes are only meant
19089 to describe functions' calling conventions.
19090
19091 However, because it's a necessary piece of information in
19092 Fortran, and before DWARF 4 DW_CC_program was the only
19093 piece of debugging information whose definition refers to
19094 a 'main program' at all, several compilers marked Fortran
19095 main programs with DW_CC_program --- even when those
19096 functions use the standard calling conventions.
19097
19098 Although DWARF now specifies a way to provide this
19099 information, we support this practice for backward
19100 compatibility. */
19101 if (DW_UNSND (&attr) == DW_CC_program
19102 && cu->language == language_fortran)
19103 main_subprogram = 1;
19104 break;
19105 case DW_AT_inline:
19106 if (DW_UNSND (&attr) == DW_INL_inlined
19107 || DW_UNSND (&attr) == DW_INL_declared_inlined)
19108 may_be_inlined = 1;
19109 break;
19110
19111 case DW_AT_import:
19112 if (tag == DW_TAG_imported_unit)
19113 {
19114 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19115 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19116 || cu->per_cu->is_dwz);
19117 }
19118 break;
19119
19120 case DW_AT_main_subprogram:
19121 main_subprogram = DW_UNSND (&attr);
19122 break;
19123
19124 case DW_AT_ranges:
19125 {
19126 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19127 but that requires a full DIE, so instead we just
19128 reimplement it. */
19129 int need_ranges_base = tag != DW_TAG_compile_unit;
19130 unsigned int ranges_offset = (DW_UNSND (&attr)
19131 + (need_ranges_base
19132 ? cu->ranges_base
19133 : 0));
19134
19135 /* Value of the DW_AT_ranges attribute is the offset in the
19136 .debug_ranges section. */
19137 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19138 nullptr))
19139 has_pc_info = 1;
19140 }
19141 break;
19142
19143 default:
19144 break;
19145 }
19146 }
19147
19148 /* For Ada, if both the name and the linkage name appear, we prefer
19149 the latter. This lets "catch exception" work better, regardless
19150 of the order in which the name and linkage name were emitted.
19151 Really, though, this is just a workaround for the fact that gdb
19152 doesn't store both the name and the linkage name. */
19153 if (cu->language == language_ada && linkage_name != nullptr)
19154 name = linkage_name;
19155
19156 if (high_pc_relative)
19157 highpc += lowpc;
19158
19159 if (has_low_pc_attr && has_high_pc_attr)
19160 {
19161 /* When using the GNU linker, .gnu.linkonce. sections are used to
19162 eliminate duplicate copies of functions and vtables and such.
19163 The linker will arbitrarily choose one and discard the others.
19164 The AT_*_pc values for such functions refer to local labels in
19165 these sections. If the section from that file was discarded, the
19166 labels are not in the output, so the relocs get a value of 0.
19167 If this is a discarded function, mark the pc bounds as invalid,
19168 so that GDB will ignore it. */
19169 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
19170 {
19171 struct objfile *objfile = dwarf2_per_objfile->objfile;
19172 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19173
19174 complaint (_("DW_AT_low_pc %s is zero "
19175 "for DIE at %s [in module %s]"),
19176 paddress (gdbarch, lowpc),
19177 sect_offset_str (sect_off),
19178 objfile_name (objfile));
19179 }
19180 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19181 else if (lowpc >= highpc)
19182 {
19183 struct objfile *objfile = dwarf2_per_objfile->objfile;
19184 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19185
19186 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19187 "for DIE at %s [in module %s]"),
19188 paddress (gdbarch, lowpc),
19189 paddress (gdbarch, highpc),
19190 sect_offset_str (sect_off),
19191 objfile_name (objfile));
19192 }
19193 else
19194 has_pc_info = 1;
19195 }
19196
19197 return info_ptr;
19198 }
19199
19200 /* Find a cached partial DIE at OFFSET in CU. */
19201
19202 struct partial_die_info *
19203 dwarf2_cu::find_partial_die (sect_offset sect_off)
19204 {
19205 struct partial_die_info *lookup_die = NULL;
19206 struct partial_die_info part_die (sect_off);
19207
19208 lookup_die = ((struct partial_die_info *)
19209 htab_find_with_hash (partial_dies, &part_die,
19210 to_underlying (sect_off)));
19211
19212 return lookup_die;
19213 }
19214
19215 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19216 except in the case of .debug_types DIEs which do not reference
19217 outside their CU (they do however referencing other types via
19218 DW_FORM_ref_sig8). */
19219
19220 static const struct cu_partial_die_info
19221 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19222 {
19223 struct dwarf2_per_objfile *dwarf2_per_objfile
19224 = cu->per_cu->dwarf2_per_objfile;
19225 struct objfile *objfile = dwarf2_per_objfile->objfile;
19226 struct dwarf2_per_cu_data *per_cu = NULL;
19227 struct partial_die_info *pd = NULL;
19228
19229 if (offset_in_dwz == cu->per_cu->is_dwz
19230 && offset_in_cu_p (&cu->header, sect_off))
19231 {
19232 pd = cu->find_partial_die (sect_off);
19233 if (pd != NULL)
19234 return { cu, pd };
19235 /* We missed recording what we needed.
19236 Load all dies and try again. */
19237 per_cu = cu->per_cu;
19238 }
19239 else
19240 {
19241 /* TUs don't reference other CUs/TUs (except via type signatures). */
19242 if (cu->per_cu->is_debug_types)
19243 {
19244 error (_("Dwarf Error: Type Unit at offset %s contains"
19245 " external reference to offset %s [in module %s].\n"),
19246 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19247 bfd_get_filename (objfile->obfd));
19248 }
19249 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19250 dwarf2_per_objfile);
19251
19252 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19253 load_partial_comp_unit (per_cu);
19254
19255 per_cu->cu->last_used = 0;
19256 pd = per_cu->cu->find_partial_die (sect_off);
19257 }
19258
19259 /* If we didn't find it, and not all dies have been loaded,
19260 load them all and try again. */
19261
19262 if (pd == NULL && per_cu->load_all_dies == 0)
19263 {
19264 per_cu->load_all_dies = 1;
19265
19266 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19267 THIS_CU->cu may already be in use. So we can't just free it and
19268 replace its DIEs with the ones we read in. Instead, we leave those
19269 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19270 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19271 set. */
19272 load_partial_comp_unit (per_cu);
19273
19274 pd = per_cu->cu->find_partial_die (sect_off);
19275 }
19276
19277 if (pd == NULL)
19278 internal_error (__FILE__, __LINE__,
19279 _("could not find partial DIE %s "
19280 "in cache [from module %s]\n"),
19281 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19282 return { per_cu->cu, pd };
19283 }
19284
19285 /* See if we can figure out if the class lives in a namespace. We do
19286 this by looking for a member function; its demangled name will
19287 contain namespace info, if there is any. */
19288
19289 static void
19290 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19291 struct dwarf2_cu *cu)
19292 {
19293 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19294 what template types look like, because the demangler
19295 frequently doesn't give the same name as the debug info. We
19296 could fix this by only using the demangled name to get the
19297 prefix (but see comment in read_structure_type). */
19298
19299 struct partial_die_info *real_pdi;
19300 struct partial_die_info *child_pdi;
19301
19302 /* If this DIE (this DIE's specification, if any) has a parent, then
19303 we should not do this. We'll prepend the parent's fully qualified
19304 name when we create the partial symbol. */
19305
19306 real_pdi = struct_pdi;
19307 while (real_pdi->has_specification)
19308 {
19309 auto res = find_partial_die (real_pdi->spec_offset,
19310 real_pdi->spec_is_dwz, cu);
19311 real_pdi = res.pdi;
19312 cu = res.cu;
19313 }
19314
19315 if (real_pdi->die_parent != NULL)
19316 return;
19317
19318 for (child_pdi = struct_pdi->die_child;
19319 child_pdi != NULL;
19320 child_pdi = child_pdi->die_sibling)
19321 {
19322 if (child_pdi->tag == DW_TAG_subprogram
19323 && child_pdi->linkage_name != NULL)
19324 {
19325 gdb::unique_xmalloc_ptr<char> actual_class_name
19326 (language_class_name_from_physname (cu->language_defn,
19327 child_pdi->linkage_name));
19328 if (actual_class_name != NULL)
19329 {
19330 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19331 struct_pdi->name
19332 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19333 actual_class_name.get ());
19334 }
19335 break;
19336 }
19337 }
19338 }
19339
19340 void
19341 partial_die_info::fixup (struct dwarf2_cu *cu)
19342 {
19343 /* Once we've fixed up a die, there's no point in doing so again.
19344 This also avoids a memory leak if we were to call
19345 guess_partial_die_structure_name multiple times. */
19346 if (fixup_called)
19347 return;
19348
19349 /* If we found a reference attribute and the DIE has no name, try
19350 to find a name in the referred to DIE. */
19351
19352 if (name == NULL && has_specification)
19353 {
19354 struct partial_die_info *spec_die;
19355
19356 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19357 spec_die = res.pdi;
19358 cu = res.cu;
19359
19360 spec_die->fixup (cu);
19361
19362 if (spec_die->name)
19363 {
19364 name = spec_die->name;
19365
19366 /* Copy DW_AT_external attribute if it is set. */
19367 if (spec_die->is_external)
19368 is_external = spec_die->is_external;
19369 }
19370 }
19371
19372 /* Set default names for some unnamed DIEs. */
19373
19374 if (name == NULL && tag == DW_TAG_namespace)
19375 name = CP_ANONYMOUS_NAMESPACE_STR;
19376
19377 /* If there is no parent die to provide a namespace, and there are
19378 children, see if we can determine the namespace from their linkage
19379 name. */
19380 if (cu->language == language_cplus
19381 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
19382 && die_parent == NULL
19383 && has_children
19384 && (tag == DW_TAG_class_type
19385 || tag == DW_TAG_structure_type
19386 || tag == DW_TAG_union_type))
19387 guess_partial_die_structure_name (this, cu);
19388
19389 /* GCC might emit a nameless struct or union that has a linkage
19390 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19391 if (name == NULL
19392 && (tag == DW_TAG_class_type
19393 || tag == DW_TAG_interface_type
19394 || tag == DW_TAG_structure_type
19395 || tag == DW_TAG_union_type)
19396 && linkage_name != NULL)
19397 {
19398 gdb::unique_xmalloc_ptr<char> demangled
19399 (gdb_demangle (linkage_name, DMGL_TYPES));
19400 if (demangled != nullptr)
19401 {
19402 const char *base;
19403
19404 /* Strip any leading namespaces/classes, keep only the base name.
19405 DW_AT_name for named DIEs does not contain the prefixes. */
19406 base = strrchr (demangled.get (), ':');
19407 if (base && base > demangled.get () && base[-1] == ':')
19408 base++;
19409 else
19410 base = demangled.get ();
19411
19412 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19413 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
19414 }
19415 }
19416
19417 fixup_called = 1;
19418 }
19419
19420 /* Read an attribute value described by an attribute form. */
19421
19422 static const gdb_byte *
19423 read_attribute_value (const struct die_reader_specs *reader,
19424 struct attribute *attr, unsigned form,
19425 LONGEST implicit_const, const gdb_byte *info_ptr)
19426 {
19427 struct dwarf2_cu *cu = reader->cu;
19428 struct dwarf2_per_objfile *dwarf2_per_objfile
19429 = cu->per_cu->dwarf2_per_objfile;
19430 struct objfile *objfile = dwarf2_per_objfile->objfile;
19431 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19432 bfd *abfd = reader->abfd;
19433 struct comp_unit_head *cu_header = &cu->header;
19434 unsigned int bytes_read;
19435 struct dwarf_block *blk;
19436
19437 attr->form = (enum dwarf_form) form;
19438 switch (form)
19439 {
19440 case DW_FORM_ref_addr:
19441 if (cu->header.version == 2)
19442 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19443 else
19444 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19445 &cu->header, &bytes_read);
19446 info_ptr += bytes_read;
19447 break;
19448 case DW_FORM_GNU_ref_alt:
19449 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19450 info_ptr += bytes_read;
19451 break;
19452 case DW_FORM_addr:
19453 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19454 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19455 info_ptr += bytes_read;
19456 break;
19457 case DW_FORM_block2:
19458 blk = dwarf_alloc_block (cu);
19459 blk->size = read_2_bytes (abfd, info_ptr);
19460 info_ptr += 2;
19461 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19462 info_ptr += blk->size;
19463 DW_BLOCK (attr) = blk;
19464 break;
19465 case DW_FORM_block4:
19466 blk = dwarf_alloc_block (cu);
19467 blk->size = read_4_bytes (abfd, info_ptr);
19468 info_ptr += 4;
19469 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19470 info_ptr += blk->size;
19471 DW_BLOCK (attr) = blk;
19472 break;
19473 case DW_FORM_data2:
19474 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19475 info_ptr += 2;
19476 break;
19477 case DW_FORM_data4:
19478 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19479 info_ptr += 4;
19480 break;
19481 case DW_FORM_data8:
19482 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19483 info_ptr += 8;
19484 break;
19485 case DW_FORM_data16:
19486 blk = dwarf_alloc_block (cu);
19487 blk->size = 16;
19488 blk->data = read_n_bytes (abfd, info_ptr, 16);
19489 info_ptr += 16;
19490 DW_BLOCK (attr) = blk;
19491 break;
19492 case DW_FORM_sec_offset:
19493 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19494 info_ptr += bytes_read;
19495 break;
19496 case DW_FORM_string:
19497 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19498 DW_STRING_IS_CANONICAL (attr) = 0;
19499 info_ptr += bytes_read;
19500 break;
19501 case DW_FORM_strp:
19502 if (!cu->per_cu->is_dwz)
19503 {
19504 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19505 abfd, info_ptr, cu_header,
19506 &bytes_read);
19507 DW_STRING_IS_CANONICAL (attr) = 0;
19508 info_ptr += bytes_read;
19509 break;
19510 }
19511 /* FALLTHROUGH */
19512 case DW_FORM_line_strp:
19513 if (!cu->per_cu->is_dwz)
19514 {
19515 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19516 abfd, info_ptr,
19517 cu_header, &bytes_read);
19518 DW_STRING_IS_CANONICAL (attr) = 0;
19519 info_ptr += bytes_read;
19520 break;
19521 }
19522 /* FALLTHROUGH */
19523 case DW_FORM_GNU_strp_alt:
19524 {
19525 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19526 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19527 &bytes_read);
19528
19529 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19530 dwz, str_offset);
19531 DW_STRING_IS_CANONICAL (attr) = 0;
19532 info_ptr += bytes_read;
19533 }
19534 break;
19535 case DW_FORM_exprloc:
19536 case DW_FORM_block:
19537 blk = dwarf_alloc_block (cu);
19538 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19539 info_ptr += bytes_read;
19540 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19541 info_ptr += blk->size;
19542 DW_BLOCK (attr) = blk;
19543 break;
19544 case DW_FORM_block1:
19545 blk = dwarf_alloc_block (cu);
19546 blk->size = read_1_byte (abfd, info_ptr);
19547 info_ptr += 1;
19548 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19549 info_ptr += blk->size;
19550 DW_BLOCK (attr) = blk;
19551 break;
19552 case DW_FORM_data1:
19553 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19554 info_ptr += 1;
19555 break;
19556 case DW_FORM_flag:
19557 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19558 info_ptr += 1;
19559 break;
19560 case DW_FORM_flag_present:
19561 DW_UNSND (attr) = 1;
19562 break;
19563 case DW_FORM_sdata:
19564 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19565 info_ptr += bytes_read;
19566 break;
19567 case DW_FORM_udata:
19568 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19569 info_ptr += bytes_read;
19570 break;
19571 case DW_FORM_ref1:
19572 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19573 + read_1_byte (abfd, info_ptr));
19574 info_ptr += 1;
19575 break;
19576 case DW_FORM_ref2:
19577 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19578 + read_2_bytes (abfd, info_ptr));
19579 info_ptr += 2;
19580 break;
19581 case DW_FORM_ref4:
19582 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19583 + read_4_bytes (abfd, info_ptr));
19584 info_ptr += 4;
19585 break;
19586 case DW_FORM_ref8:
19587 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19588 + read_8_bytes (abfd, info_ptr));
19589 info_ptr += 8;
19590 break;
19591 case DW_FORM_ref_sig8:
19592 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19593 info_ptr += 8;
19594 break;
19595 case DW_FORM_ref_udata:
19596 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19597 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19598 info_ptr += bytes_read;
19599 break;
19600 case DW_FORM_indirect:
19601 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19602 info_ptr += bytes_read;
19603 if (form == DW_FORM_implicit_const)
19604 {
19605 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19606 info_ptr += bytes_read;
19607 }
19608 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19609 info_ptr);
19610 break;
19611 case DW_FORM_implicit_const:
19612 DW_SND (attr) = implicit_const;
19613 break;
19614 case DW_FORM_addrx:
19615 case DW_FORM_GNU_addr_index:
19616 if (reader->dwo_file == NULL)
19617 {
19618 /* For now flag a hard error.
19619 Later we can turn this into a complaint. */
19620 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19621 dwarf_form_name (form),
19622 bfd_get_filename (abfd));
19623 }
19624 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19625 info_ptr += bytes_read;
19626 break;
19627 case DW_FORM_strx:
19628 case DW_FORM_strx1:
19629 case DW_FORM_strx2:
19630 case DW_FORM_strx3:
19631 case DW_FORM_strx4:
19632 case DW_FORM_GNU_str_index:
19633 if (reader->dwo_file == NULL)
19634 {
19635 /* For now flag a hard error.
19636 Later we can turn this into a complaint if warranted. */
19637 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19638 dwarf_form_name (form),
19639 bfd_get_filename (abfd));
19640 }
19641 {
19642 ULONGEST str_index;
19643 if (form == DW_FORM_strx1)
19644 {
19645 str_index = read_1_byte (abfd, info_ptr);
19646 info_ptr += 1;
19647 }
19648 else if (form == DW_FORM_strx2)
19649 {
19650 str_index = read_2_bytes (abfd, info_ptr);
19651 info_ptr += 2;
19652 }
19653 else if (form == DW_FORM_strx3)
19654 {
19655 str_index = read_3_bytes (abfd, info_ptr);
19656 info_ptr += 3;
19657 }
19658 else if (form == DW_FORM_strx4)
19659 {
19660 str_index = read_4_bytes (abfd, info_ptr);
19661 info_ptr += 4;
19662 }
19663 else
19664 {
19665 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19666 info_ptr += bytes_read;
19667 }
19668 DW_STRING (attr) = read_str_index (reader, str_index);
19669 DW_STRING_IS_CANONICAL (attr) = 0;
19670 }
19671 break;
19672 default:
19673 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19674 dwarf_form_name (form),
19675 bfd_get_filename (abfd));
19676 }
19677
19678 /* Super hack. */
19679 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19680 attr->form = DW_FORM_GNU_ref_alt;
19681
19682 /* We have seen instances where the compiler tried to emit a byte
19683 size attribute of -1 which ended up being encoded as an unsigned
19684 0xffffffff. Although 0xffffffff is technically a valid size value,
19685 an object of this size seems pretty unlikely so we can relatively
19686 safely treat these cases as if the size attribute was invalid and
19687 treat them as zero by default. */
19688 if (attr->name == DW_AT_byte_size
19689 && form == DW_FORM_data4
19690 && DW_UNSND (attr) >= 0xffffffff)
19691 {
19692 complaint
19693 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19694 hex_string (DW_UNSND (attr)));
19695 DW_UNSND (attr) = 0;
19696 }
19697
19698 return info_ptr;
19699 }
19700
19701 /* Read an attribute described by an abbreviated attribute. */
19702
19703 static const gdb_byte *
19704 read_attribute (const struct die_reader_specs *reader,
19705 struct attribute *attr, struct attr_abbrev *abbrev,
19706 const gdb_byte *info_ptr)
19707 {
19708 attr->name = abbrev->name;
19709 return read_attribute_value (reader, attr, abbrev->form,
19710 abbrev->implicit_const, info_ptr);
19711 }
19712
19713 /* Read dwarf information from a buffer. */
19714
19715 static unsigned int
19716 read_1_byte (bfd *abfd, const gdb_byte *buf)
19717 {
19718 return bfd_get_8 (abfd, buf);
19719 }
19720
19721 static int
19722 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19723 {
19724 return bfd_get_signed_8 (abfd, buf);
19725 }
19726
19727 static unsigned int
19728 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19729 {
19730 return bfd_get_16 (abfd, buf);
19731 }
19732
19733 static int
19734 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19735 {
19736 return bfd_get_signed_16 (abfd, buf);
19737 }
19738
19739 static unsigned int
19740 read_3_bytes (bfd *abfd, const gdb_byte *buf)
19741 {
19742 unsigned int result = 0;
19743 for (int i = 0; i < 3; ++i)
19744 {
19745 unsigned char byte = bfd_get_8 (abfd, buf);
19746 buf++;
19747 result |= ((unsigned int) byte << (i * 8));
19748 }
19749 return result;
19750 }
19751
19752 static unsigned int
19753 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19754 {
19755 return bfd_get_32 (abfd, buf);
19756 }
19757
19758 static int
19759 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19760 {
19761 return bfd_get_signed_32 (abfd, buf);
19762 }
19763
19764 static ULONGEST
19765 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19766 {
19767 return bfd_get_64 (abfd, buf);
19768 }
19769
19770 static CORE_ADDR
19771 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19772 unsigned int *bytes_read)
19773 {
19774 struct comp_unit_head *cu_header = &cu->header;
19775 CORE_ADDR retval = 0;
19776
19777 if (cu_header->signed_addr_p)
19778 {
19779 switch (cu_header->addr_size)
19780 {
19781 case 2:
19782 retval = bfd_get_signed_16 (abfd, buf);
19783 break;
19784 case 4:
19785 retval = bfd_get_signed_32 (abfd, buf);
19786 break;
19787 case 8:
19788 retval = bfd_get_signed_64 (abfd, buf);
19789 break;
19790 default:
19791 internal_error (__FILE__, __LINE__,
19792 _("read_address: bad switch, signed [in module %s]"),
19793 bfd_get_filename (abfd));
19794 }
19795 }
19796 else
19797 {
19798 switch (cu_header->addr_size)
19799 {
19800 case 2:
19801 retval = bfd_get_16 (abfd, buf);
19802 break;
19803 case 4:
19804 retval = bfd_get_32 (abfd, buf);
19805 break;
19806 case 8:
19807 retval = bfd_get_64 (abfd, buf);
19808 break;
19809 default:
19810 internal_error (__FILE__, __LINE__,
19811 _("read_address: bad switch, "
19812 "unsigned [in module %s]"),
19813 bfd_get_filename (abfd));
19814 }
19815 }
19816
19817 *bytes_read = cu_header->addr_size;
19818 return retval;
19819 }
19820
19821 /* Read the initial length from a section. The (draft) DWARF 3
19822 specification allows the initial length to take up either 4 bytes
19823 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19824 bytes describe the length and all offsets will be 8 bytes in length
19825 instead of 4.
19826
19827 An older, non-standard 64-bit format is also handled by this
19828 function. The older format in question stores the initial length
19829 as an 8-byte quantity without an escape value. Lengths greater
19830 than 2^32 aren't very common which means that the initial 4 bytes
19831 is almost always zero. Since a length value of zero doesn't make
19832 sense for the 32-bit format, this initial zero can be considered to
19833 be an escape value which indicates the presence of the older 64-bit
19834 format. As written, the code can't detect (old format) lengths
19835 greater than 4GB. If it becomes necessary to handle lengths
19836 somewhat larger than 4GB, we could allow other small values (such
19837 as the non-sensical values of 1, 2, and 3) to also be used as
19838 escape values indicating the presence of the old format.
19839
19840 The value returned via bytes_read should be used to increment the
19841 relevant pointer after calling read_initial_length().
19842
19843 [ Note: read_initial_length() and read_offset() are based on the
19844 document entitled "DWARF Debugging Information Format", revision
19845 3, draft 8, dated November 19, 2001. This document was obtained
19846 from:
19847
19848 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19849
19850 This document is only a draft and is subject to change. (So beware.)
19851
19852 Details regarding the older, non-standard 64-bit format were
19853 determined empirically by examining 64-bit ELF files produced by
19854 the SGI toolchain on an IRIX 6.5 machine.
19855
19856 - Kevin, July 16, 2002
19857 ] */
19858
19859 static LONGEST
19860 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19861 {
19862 LONGEST length = bfd_get_32 (abfd, buf);
19863
19864 if (length == 0xffffffff)
19865 {
19866 length = bfd_get_64 (abfd, buf + 4);
19867 *bytes_read = 12;
19868 }
19869 else if (length == 0)
19870 {
19871 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19872 length = bfd_get_64 (abfd, buf);
19873 *bytes_read = 8;
19874 }
19875 else
19876 {
19877 *bytes_read = 4;
19878 }
19879
19880 return length;
19881 }
19882
19883 /* Cover function for read_initial_length.
19884 Returns the length of the object at BUF, and stores the size of the
19885 initial length in *BYTES_READ and stores the size that offsets will be in
19886 *OFFSET_SIZE.
19887 If the initial length size is not equivalent to that specified in
19888 CU_HEADER then issue a complaint.
19889 This is useful when reading non-comp-unit headers. */
19890
19891 static LONGEST
19892 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19893 const struct comp_unit_head *cu_header,
19894 unsigned int *bytes_read,
19895 unsigned int *offset_size)
19896 {
19897 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19898
19899 gdb_assert (cu_header->initial_length_size == 4
19900 || cu_header->initial_length_size == 8
19901 || cu_header->initial_length_size == 12);
19902
19903 if (cu_header->initial_length_size != *bytes_read)
19904 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19905
19906 *offset_size = (*bytes_read == 4) ? 4 : 8;
19907 return length;
19908 }
19909
19910 /* Read an offset from the data stream. The size of the offset is
19911 given by cu_header->offset_size. */
19912
19913 static LONGEST
19914 read_offset (bfd *abfd, const gdb_byte *buf,
19915 const struct comp_unit_head *cu_header,
19916 unsigned int *bytes_read)
19917 {
19918 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19919
19920 *bytes_read = cu_header->offset_size;
19921 return offset;
19922 }
19923
19924 /* Read an offset from the data stream. */
19925
19926 static LONGEST
19927 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19928 {
19929 LONGEST retval = 0;
19930
19931 switch (offset_size)
19932 {
19933 case 4:
19934 retval = bfd_get_32 (abfd, buf);
19935 break;
19936 case 8:
19937 retval = bfd_get_64 (abfd, buf);
19938 break;
19939 default:
19940 internal_error (__FILE__, __LINE__,
19941 _("read_offset_1: bad switch [in module %s]"),
19942 bfd_get_filename (abfd));
19943 }
19944
19945 return retval;
19946 }
19947
19948 static const gdb_byte *
19949 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19950 {
19951 /* If the size of a host char is 8 bits, we can return a pointer
19952 to the buffer, otherwise we have to copy the data to a buffer
19953 allocated on the temporary obstack. */
19954 gdb_assert (HOST_CHAR_BIT == 8);
19955 return buf;
19956 }
19957
19958 static const char *
19959 read_direct_string (bfd *abfd, const gdb_byte *buf,
19960 unsigned int *bytes_read_ptr)
19961 {
19962 /* If the size of a host char is 8 bits, we can return a pointer
19963 to the string, otherwise we have to copy the string to a buffer
19964 allocated on the temporary obstack. */
19965 gdb_assert (HOST_CHAR_BIT == 8);
19966 if (*buf == '\0')
19967 {
19968 *bytes_read_ptr = 1;
19969 return NULL;
19970 }
19971 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19972 return (const char *) buf;
19973 }
19974
19975 /* Return pointer to string at section SECT offset STR_OFFSET with error
19976 reporting strings FORM_NAME and SECT_NAME. */
19977
19978 static const char *
19979 read_indirect_string_at_offset_from (struct objfile *objfile,
19980 bfd *abfd, LONGEST str_offset,
19981 struct dwarf2_section_info *sect,
19982 const char *form_name,
19983 const char *sect_name)
19984 {
19985 dwarf2_read_section (objfile, sect);
19986 if (sect->buffer == NULL)
19987 error (_("%s used without %s section [in module %s]"),
19988 form_name, sect_name, bfd_get_filename (abfd));
19989 if (str_offset >= sect->size)
19990 error (_("%s pointing outside of %s section [in module %s]"),
19991 form_name, sect_name, bfd_get_filename (abfd));
19992 gdb_assert (HOST_CHAR_BIT == 8);
19993 if (sect->buffer[str_offset] == '\0')
19994 return NULL;
19995 return (const char *) (sect->buffer + str_offset);
19996 }
19997
19998 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19999
20000 static const char *
20001 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
20002 bfd *abfd, LONGEST str_offset)
20003 {
20004 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
20005 abfd, str_offset,
20006 &dwarf2_per_objfile->str,
20007 "DW_FORM_strp", ".debug_str");
20008 }
20009
20010 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
20011
20012 static const char *
20013 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
20014 bfd *abfd, LONGEST str_offset)
20015 {
20016 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
20017 abfd, str_offset,
20018 &dwarf2_per_objfile->line_str,
20019 "DW_FORM_line_strp",
20020 ".debug_line_str");
20021 }
20022
20023 /* Read a string at offset STR_OFFSET in the .debug_str section from
20024 the .dwz file DWZ. Throw an error if the offset is too large. If
20025 the string consists of a single NUL byte, return NULL; otherwise
20026 return a pointer to the string. */
20027
20028 static const char *
20029 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
20030 LONGEST str_offset)
20031 {
20032 dwarf2_read_section (objfile, &dwz->str);
20033
20034 if (dwz->str.buffer == NULL)
20035 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
20036 "section [in module %s]"),
20037 bfd_get_filename (dwz->dwz_bfd.get ()));
20038 if (str_offset >= dwz->str.size)
20039 error (_("DW_FORM_GNU_strp_alt pointing outside of "
20040 ".debug_str section [in module %s]"),
20041 bfd_get_filename (dwz->dwz_bfd.get ()));
20042 gdb_assert (HOST_CHAR_BIT == 8);
20043 if (dwz->str.buffer[str_offset] == '\0')
20044 return NULL;
20045 return (const char *) (dwz->str.buffer + str_offset);
20046 }
20047
20048 /* Return pointer to string at .debug_str offset as read from BUF.
20049 BUF is assumed to be in a compilation unit described by CU_HEADER.
20050 Return *BYTES_READ_PTR count of bytes read from BUF. */
20051
20052 static const char *
20053 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
20054 const gdb_byte *buf,
20055 const struct comp_unit_head *cu_header,
20056 unsigned int *bytes_read_ptr)
20057 {
20058 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20059
20060 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
20061 }
20062
20063 /* Return pointer to string at .debug_line_str offset as read from BUF.
20064 BUF is assumed to be in a compilation unit described by CU_HEADER.
20065 Return *BYTES_READ_PTR count of bytes read from BUF. */
20066
20067 static const char *
20068 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
20069 bfd *abfd, const gdb_byte *buf,
20070 const struct comp_unit_head *cu_header,
20071 unsigned int *bytes_read_ptr)
20072 {
20073 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20074
20075 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
20076 str_offset);
20077 }
20078
20079 ULONGEST
20080 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
20081 unsigned int *bytes_read_ptr)
20082 {
20083 ULONGEST result;
20084 unsigned int num_read;
20085 int shift;
20086 unsigned char byte;
20087
20088 result = 0;
20089 shift = 0;
20090 num_read = 0;
20091 while (1)
20092 {
20093 byte = bfd_get_8 (abfd, buf);
20094 buf++;
20095 num_read++;
20096 result |= ((ULONGEST) (byte & 127) << shift);
20097 if ((byte & 128) == 0)
20098 {
20099 break;
20100 }
20101 shift += 7;
20102 }
20103 *bytes_read_ptr = num_read;
20104 return result;
20105 }
20106
20107 static LONGEST
20108 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
20109 unsigned int *bytes_read_ptr)
20110 {
20111 ULONGEST result;
20112 int shift, num_read;
20113 unsigned char byte;
20114
20115 result = 0;
20116 shift = 0;
20117 num_read = 0;
20118 while (1)
20119 {
20120 byte = bfd_get_8 (abfd, buf);
20121 buf++;
20122 num_read++;
20123 result |= ((ULONGEST) (byte & 127) << shift);
20124 shift += 7;
20125 if ((byte & 128) == 0)
20126 {
20127 break;
20128 }
20129 }
20130 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
20131 result |= -(((ULONGEST) 1) << shift);
20132 *bytes_read_ptr = num_read;
20133 return result;
20134 }
20135
20136 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20137 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
20138 ADDR_SIZE is the size of addresses from the CU header. */
20139
20140 static CORE_ADDR
20141 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20142 unsigned int addr_index, ULONGEST addr_base, int addr_size)
20143 {
20144 struct objfile *objfile = dwarf2_per_objfile->objfile;
20145 bfd *abfd = objfile->obfd;
20146 const gdb_byte *info_ptr;
20147
20148 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20149 if (dwarf2_per_objfile->addr.buffer == NULL)
20150 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20151 objfile_name (objfile));
20152 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
20153 error (_("DW_FORM_addr_index pointing outside of "
20154 ".debug_addr section [in module %s]"),
20155 objfile_name (objfile));
20156 info_ptr = (dwarf2_per_objfile->addr.buffer
20157 + addr_base + addr_index * addr_size);
20158 if (addr_size == 4)
20159 return bfd_get_32 (abfd, info_ptr);
20160 else
20161 return bfd_get_64 (abfd, info_ptr);
20162 }
20163
20164 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20165
20166 static CORE_ADDR
20167 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20168 {
20169 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20170 cu->addr_base, cu->header.addr_size);
20171 }
20172
20173 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20174
20175 static CORE_ADDR
20176 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20177 unsigned int *bytes_read)
20178 {
20179 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
20180 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20181
20182 return read_addr_index (cu, addr_index);
20183 }
20184
20185 /* Data structure to pass results from dwarf2_read_addr_index_reader
20186 back to dwarf2_read_addr_index. */
20187
20188 struct dwarf2_read_addr_index_data
20189 {
20190 ULONGEST addr_base;
20191 int addr_size;
20192 };
20193
20194 /* die_reader_func for dwarf2_read_addr_index. */
20195
20196 static void
20197 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
20198 const gdb_byte *info_ptr,
20199 struct die_info *comp_unit_die,
20200 int has_children,
20201 void *data)
20202 {
20203 struct dwarf2_cu *cu = reader->cu;
20204 struct dwarf2_read_addr_index_data *aidata =
20205 (struct dwarf2_read_addr_index_data *) data;
20206
20207 aidata->addr_base = cu->addr_base;
20208 aidata->addr_size = cu->header.addr_size;
20209 }
20210
20211 /* Given an index in .debug_addr, fetch the value.
20212 NOTE: This can be called during dwarf expression evaluation,
20213 long after the debug information has been read, and thus per_cu->cu
20214 may no longer exist. */
20215
20216 CORE_ADDR
20217 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20218 unsigned int addr_index)
20219 {
20220 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
20221 struct dwarf2_cu *cu = per_cu->cu;
20222 ULONGEST addr_base;
20223 int addr_size;
20224
20225 /* We need addr_base and addr_size.
20226 If we don't have PER_CU->cu, we have to get it.
20227 Nasty, but the alternative is storing the needed info in PER_CU,
20228 which at this point doesn't seem justified: it's not clear how frequently
20229 it would get used and it would increase the size of every PER_CU.
20230 Entry points like dwarf2_per_cu_addr_size do a similar thing
20231 so we're not in uncharted territory here.
20232 Alas we need to be a bit more complicated as addr_base is contained
20233 in the DIE.
20234
20235 We don't need to read the entire CU(/TU).
20236 We just need the header and top level die.
20237
20238 IWBN to use the aging mechanism to let us lazily later discard the CU.
20239 For now we skip this optimization. */
20240
20241 if (cu != NULL)
20242 {
20243 addr_base = cu->addr_base;
20244 addr_size = cu->header.addr_size;
20245 }
20246 else
20247 {
20248 struct dwarf2_read_addr_index_data aidata;
20249
20250 /* Note: We can't use init_cutu_and_read_dies_simple here,
20251 we need addr_base. */
20252 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
20253 dwarf2_read_addr_index_reader, &aidata);
20254 addr_base = aidata.addr_base;
20255 addr_size = aidata.addr_size;
20256 }
20257
20258 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20259 addr_size);
20260 }
20261
20262 /* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
20263 This is only used by the Fission support. */
20264
20265 static const char *
20266 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20267 {
20268 struct dwarf2_cu *cu = reader->cu;
20269 struct dwarf2_per_objfile *dwarf2_per_objfile
20270 = cu->per_cu->dwarf2_per_objfile;
20271 struct objfile *objfile = dwarf2_per_objfile->objfile;
20272 const char *objf_name = objfile_name (objfile);
20273 bfd *abfd = objfile->obfd;
20274 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20275 struct dwarf2_section_info *str_offsets_section =
20276 &reader->dwo_file->sections.str_offsets;
20277 const gdb_byte *info_ptr;
20278 ULONGEST str_offset;
20279 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20280
20281 dwarf2_read_section (objfile, str_section);
20282 dwarf2_read_section (objfile, str_offsets_section);
20283 if (str_section->buffer == NULL)
20284 error (_("%s used without .debug_str.dwo section"
20285 " in CU at offset %s [in module %s]"),
20286 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20287 if (str_offsets_section->buffer == NULL)
20288 error (_("%s used without .debug_str_offsets.dwo section"
20289 " in CU at offset %s [in module %s]"),
20290 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20291 if (str_index * cu->header.offset_size >= str_offsets_section->size)
20292 error (_("%s pointing outside of .debug_str_offsets.dwo"
20293 " section in CU at offset %s [in module %s]"),
20294 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20295 info_ptr = (str_offsets_section->buffer
20296 + str_index * cu->header.offset_size);
20297 if (cu->header.offset_size == 4)
20298 str_offset = bfd_get_32 (abfd, info_ptr);
20299 else
20300 str_offset = bfd_get_64 (abfd, info_ptr);
20301 if (str_offset >= str_section->size)
20302 error (_("Offset from %s pointing outside of"
20303 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20304 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20305 return (const char *) (str_section->buffer + str_offset);
20306 }
20307
20308 /* Return the length of an LEB128 number in BUF. */
20309
20310 static int
20311 leb128_size (const gdb_byte *buf)
20312 {
20313 const gdb_byte *begin = buf;
20314 gdb_byte byte;
20315
20316 while (1)
20317 {
20318 byte = *buf++;
20319 if ((byte & 128) == 0)
20320 return buf - begin;
20321 }
20322 }
20323
20324 static void
20325 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20326 {
20327 switch (lang)
20328 {
20329 case DW_LANG_C89:
20330 case DW_LANG_C99:
20331 case DW_LANG_C11:
20332 case DW_LANG_C:
20333 case DW_LANG_UPC:
20334 cu->language = language_c;
20335 break;
20336 case DW_LANG_Java:
20337 case DW_LANG_C_plus_plus:
20338 case DW_LANG_C_plus_plus_11:
20339 case DW_LANG_C_plus_plus_14:
20340 cu->language = language_cplus;
20341 break;
20342 case DW_LANG_D:
20343 cu->language = language_d;
20344 break;
20345 case DW_LANG_Fortran77:
20346 case DW_LANG_Fortran90:
20347 case DW_LANG_Fortran95:
20348 case DW_LANG_Fortran03:
20349 case DW_LANG_Fortran08:
20350 cu->language = language_fortran;
20351 break;
20352 case DW_LANG_Go:
20353 cu->language = language_go;
20354 break;
20355 case DW_LANG_Mips_Assembler:
20356 cu->language = language_asm;
20357 break;
20358 case DW_LANG_Ada83:
20359 case DW_LANG_Ada95:
20360 cu->language = language_ada;
20361 break;
20362 case DW_LANG_Modula2:
20363 cu->language = language_m2;
20364 break;
20365 case DW_LANG_Pascal83:
20366 cu->language = language_pascal;
20367 break;
20368 case DW_LANG_ObjC:
20369 cu->language = language_objc;
20370 break;
20371 case DW_LANG_Rust:
20372 case DW_LANG_Rust_old:
20373 cu->language = language_rust;
20374 break;
20375 case DW_LANG_Cobol74:
20376 case DW_LANG_Cobol85:
20377 default:
20378 cu->language = language_minimal;
20379 break;
20380 }
20381 cu->language_defn = language_def (cu->language);
20382 }
20383
20384 /* Return the named attribute or NULL if not there. */
20385
20386 static struct attribute *
20387 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20388 {
20389 for (;;)
20390 {
20391 unsigned int i;
20392 struct attribute *spec = NULL;
20393
20394 for (i = 0; i < die->num_attrs; ++i)
20395 {
20396 if (die->attrs[i].name == name)
20397 return &die->attrs[i];
20398 if (die->attrs[i].name == DW_AT_specification
20399 || die->attrs[i].name == DW_AT_abstract_origin)
20400 spec = &die->attrs[i];
20401 }
20402
20403 if (!spec)
20404 break;
20405
20406 die = follow_die_ref (die, spec, &cu);
20407 }
20408
20409 return NULL;
20410 }
20411
20412 /* Return the named attribute or NULL if not there,
20413 but do not follow DW_AT_specification, etc.
20414 This is for use in contexts where we're reading .debug_types dies.
20415 Following DW_AT_specification, DW_AT_abstract_origin will take us
20416 back up the chain, and we want to go down. */
20417
20418 static struct attribute *
20419 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20420 {
20421 unsigned int i;
20422
20423 for (i = 0; i < die->num_attrs; ++i)
20424 if (die->attrs[i].name == name)
20425 return &die->attrs[i];
20426
20427 return NULL;
20428 }
20429
20430 /* Return the string associated with a string-typed attribute, or NULL if it
20431 is either not found or is of an incorrect type. */
20432
20433 static const char *
20434 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20435 {
20436 struct attribute *attr;
20437 const char *str = NULL;
20438
20439 attr = dwarf2_attr (die, name, cu);
20440
20441 if (attr != NULL)
20442 {
20443 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20444 || attr->form == DW_FORM_string
20445 || attr->form == DW_FORM_strx
20446 || attr->form == DW_FORM_strx1
20447 || attr->form == DW_FORM_strx2
20448 || attr->form == DW_FORM_strx3
20449 || attr->form == DW_FORM_strx4
20450 || attr->form == DW_FORM_GNU_str_index
20451 || attr->form == DW_FORM_GNU_strp_alt)
20452 str = DW_STRING (attr);
20453 else
20454 complaint (_("string type expected for attribute %s for "
20455 "DIE at %s in module %s"),
20456 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20457 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20458 }
20459
20460 return str;
20461 }
20462
20463 /* Return the dwo name or NULL if not present. If present, it is in either
20464 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20465 static const char *
20466 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20467 {
20468 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20469 if (dwo_name == nullptr)
20470 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20471 return dwo_name;
20472 }
20473
20474 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20475 and holds a non-zero value. This function should only be used for
20476 DW_FORM_flag or DW_FORM_flag_present attributes. */
20477
20478 static int
20479 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20480 {
20481 struct attribute *attr = dwarf2_attr (die, name, cu);
20482
20483 return (attr && DW_UNSND (attr));
20484 }
20485
20486 static int
20487 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20488 {
20489 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20490 which value is non-zero. However, we have to be careful with
20491 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20492 (via dwarf2_flag_true_p) follows this attribute. So we may
20493 end up accidently finding a declaration attribute that belongs
20494 to a different DIE referenced by the specification attribute,
20495 even though the given DIE does not have a declaration attribute. */
20496 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20497 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20498 }
20499
20500 /* Return the die giving the specification for DIE, if there is
20501 one. *SPEC_CU is the CU containing DIE on input, and the CU
20502 containing the return value on output. If there is no
20503 specification, but there is an abstract origin, that is
20504 returned. */
20505
20506 static struct die_info *
20507 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20508 {
20509 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20510 *spec_cu);
20511
20512 if (spec_attr == NULL)
20513 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20514
20515 if (spec_attr == NULL)
20516 return NULL;
20517 else
20518 return follow_die_ref (die, spec_attr, spec_cu);
20519 }
20520
20521 /* Stub for free_line_header to match void * callback types. */
20522
20523 static void
20524 free_line_header_voidp (void *arg)
20525 {
20526 struct line_header *lh = (struct line_header *) arg;
20527
20528 delete lh;
20529 }
20530
20531 void
20532 line_header::add_include_dir (const char *include_dir)
20533 {
20534 if (dwarf_line_debug >= 2)
20535 {
20536 size_t new_size;
20537 if (version >= 5)
20538 new_size = m_include_dirs.size ();
20539 else
20540 new_size = m_include_dirs.size () + 1;
20541 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20542 new_size, include_dir);
20543 }
20544 m_include_dirs.push_back (include_dir);
20545 }
20546
20547 void
20548 line_header::add_file_name (const char *name,
20549 dir_index d_index,
20550 unsigned int mod_time,
20551 unsigned int length)
20552 {
20553 if (dwarf_line_debug >= 2)
20554 {
20555 size_t new_size;
20556 if (version >= 5)
20557 new_size = file_names_size ();
20558 else
20559 new_size = file_names_size () + 1;
20560 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20561 new_size, name);
20562 }
20563 m_file_names.emplace_back (name, d_index, mod_time, length);
20564 }
20565
20566 /* A convenience function to find the proper .debug_line section for a CU. */
20567
20568 static struct dwarf2_section_info *
20569 get_debug_line_section (struct dwarf2_cu *cu)
20570 {
20571 struct dwarf2_section_info *section;
20572 struct dwarf2_per_objfile *dwarf2_per_objfile
20573 = cu->per_cu->dwarf2_per_objfile;
20574
20575 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20576 DWO file. */
20577 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20578 section = &cu->dwo_unit->dwo_file->sections.line;
20579 else if (cu->per_cu->is_dwz)
20580 {
20581 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20582
20583 section = &dwz->line;
20584 }
20585 else
20586 section = &dwarf2_per_objfile->line;
20587
20588 return section;
20589 }
20590
20591 /* Read directory or file name entry format, starting with byte of
20592 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20593 entries count and the entries themselves in the described entry
20594 format. */
20595
20596 static void
20597 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20598 bfd *abfd, const gdb_byte **bufp,
20599 struct line_header *lh,
20600 const struct comp_unit_head *cu_header,
20601 void (*callback) (struct line_header *lh,
20602 const char *name,
20603 dir_index d_index,
20604 unsigned int mod_time,
20605 unsigned int length))
20606 {
20607 gdb_byte format_count, formati;
20608 ULONGEST data_count, datai;
20609 const gdb_byte *buf = *bufp;
20610 const gdb_byte *format_header_data;
20611 unsigned int bytes_read;
20612
20613 format_count = read_1_byte (abfd, buf);
20614 buf += 1;
20615 format_header_data = buf;
20616 for (formati = 0; formati < format_count; formati++)
20617 {
20618 read_unsigned_leb128 (abfd, buf, &bytes_read);
20619 buf += bytes_read;
20620 read_unsigned_leb128 (abfd, buf, &bytes_read);
20621 buf += bytes_read;
20622 }
20623
20624 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20625 buf += bytes_read;
20626 for (datai = 0; datai < data_count; datai++)
20627 {
20628 const gdb_byte *format = format_header_data;
20629 struct file_entry fe;
20630
20631 for (formati = 0; formati < format_count; formati++)
20632 {
20633 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20634 format += bytes_read;
20635
20636 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20637 format += bytes_read;
20638
20639 gdb::optional<const char *> string;
20640 gdb::optional<unsigned int> uint;
20641
20642 switch (form)
20643 {
20644 case DW_FORM_string:
20645 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20646 buf += bytes_read;
20647 break;
20648
20649 case DW_FORM_line_strp:
20650 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20651 abfd, buf,
20652 cu_header,
20653 &bytes_read));
20654 buf += bytes_read;
20655 break;
20656
20657 case DW_FORM_data1:
20658 uint.emplace (read_1_byte (abfd, buf));
20659 buf += 1;
20660 break;
20661
20662 case DW_FORM_data2:
20663 uint.emplace (read_2_bytes (abfd, buf));
20664 buf += 2;
20665 break;
20666
20667 case DW_FORM_data4:
20668 uint.emplace (read_4_bytes (abfd, buf));
20669 buf += 4;
20670 break;
20671
20672 case DW_FORM_data8:
20673 uint.emplace (read_8_bytes (abfd, buf));
20674 buf += 8;
20675 break;
20676
20677 case DW_FORM_data16:
20678 /* This is used for MD5, but file_entry does not record MD5s. */
20679 buf += 16;
20680 break;
20681
20682 case DW_FORM_udata:
20683 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20684 buf += bytes_read;
20685 break;
20686
20687 case DW_FORM_block:
20688 /* It is valid only for DW_LNCT_timestamp which is ignored by
20689 current GDB. */
20690 break;
20691 }
20692
20693 switch (content_type)
20694 {
20695 case DW_LNCT_path:
20696 if (string.has_value ())
20697 fe.name = *string;
20698 break;
20699 case DW_LNCT_directory_index:
20700 if (uint.has_value ())
20701 fe.d_index = (dir_index) *uint;
20702 break;
20703 case DW_LNCT_timestamp:
20704 if (uint.has_value ())
20705 fe.mod_time = *uint;
20706 break;
20707 case DW_LNCT_size:
20708 if (uint.has_value ())
20709 fe.length = *uint;
20710 break;
20711 case DW_LNCT_MD5:
20712 break;
20713 default:
20714 complaint (_("Unknown format content type %s"),
20715 pulongest (content_type));
20716 }
20717 }
20718
20719 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20720 }
20721
20722 *bufp = buf;
20723 }
20724
20725 /* Read the statement program header starting at OFFSET in
20726 .debug_line, or .debug_line.dwo. Return a pointer
20727 to a struct line_header, allocated using xmalloc.
20728 Returns NULL if there is a problem reading the header, e.g., if it
20729 has a version we don't understand.
20730
20731 NOTE: the strings in the include directory and file name tables of
20732 the returned object point into the dwarf line section buffer,
20733 and must not be freed. */
20734
20735 static line_header_up
20736 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20737 {
20738 const gdb_byte *line_ptr;
20739 unsigned int bytes_read, offset_size;
20740 int i;
20741 const char *cur_dir, *cur_file;
20742 struct dwarf2_section_info *section;
20743 bfd *abfd;
20744 struct dwarf2_per_objfile *dwarf2_per_objfile
20745 = cu->per_cu->dwarf2_per_objfile;
20746
20747 section = get_debug_line_section (cu);
20748 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20749 if (section->buffer == NULL)
20750 {
20751 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20752 complaint (_("missing .debug_line.dwo section"));
20753 else
20754 complaint (_("missing .debug_line section"));
20755 return 0;
20756 }
20757
20758 /* We can't do this until we know the section is non-empty.
20759 Only then do we know we have such a section. */
20760 abfd = get_section_bfd_owner (section);
20761
20762 /* Make sure that at least there's room for the total_length field.
20763 That could be 12 bytes long, but we're just going to fudge that. */
20764 if (to_underlying (sect_off) + 4 >= section->size)
20765 {
20766 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20767 return 0;
20768 }
20769
20770 line_header_up lh (new line_header ());
20771
20772 lh->sect_off = sect_off;
20773 lh->offset_in_dwz = cu->per_cu->is_dwz;
20774
20775 line_ptr = section->buffer + to_underlying (sect_off);
20776
20777 /* Read in the header. */
20778 lh->total_length =
20779 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20780 &bytes_read, &offset_size);
20781 line_ptr += bytes_read;
20782
20783 const gdb_byte *start_here = line_ptr;
20784
20785 if (line_ptr + lh->total_length > (section->buffer + section->size))
20786 {
20787 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20788 return 0;
20789 }
20790 lh->statement_program_end = start_here + lh->total_length;
20791 lh->version = read_2_bytes (abfd, line_ptr);
20792 line_ptr += 2;
20793 if (lh->version > 5)
20794 {
20795 /* This is a version we don't understand. The format could have
20796 changed in ways we don't handle properly so just punt. */
20797 complaint (_("unsupported version in .debug_line section"));
20798 return NULL;
20799 }
20800 if (lh->version >= 5)
20801 {
20802 gdb_byte segment_selector_size;
20803
20804 /* Skip address size. */
20805 read_1_byte (abfd, line_ptr);
20806 line_ptr += 1;
20807
20808 segment_selector_size = read_1_byte (abfd, line_ptr);
20809 line_ptr += 1;
20810 if (segment_selector_size != 0)
20811 {
20812 complaint (_("unsupported segment selector size %u "
20813 "in .debug_line section"),
20814 segment_selector_size);
20815 return NULL;
20816 }
20817 }
20818 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20819 line_ptr += offset_size;
20820 lh->statement_program_start = line_ptr + lh->header_length;
20821 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20822 line_ptr += 1;
20823 if (lh->version >= 4)
20824 {
20825 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20826 line_ptr += 1;
20827 }
20828 else
20829 lh->maximum_ops_per_instruction = 1;
20830
20831 if (lh->maximum_ops_per_instruction == 0)
20832 {
20833 lh->maximum_ops_per_instruction = 1;
20834 complaint (_("invalid maximum_ops_per_instruction "
20835 "in `.debug_line' section"));
20836 }
20837
20838 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20839 line_ptr += 1;
20840 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20841 line_ptr += 1;
20842 lh->line_range = read_1_byte (abfd, line_ptr);
20843 line_ptr += 1;
20844 lh->opcode_base = read_1_byte (abfd, line_ptr);
20845 line_ptr += 1;
20846 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20847
20848 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20849 for (i = 1; i < lh->opcode_base; ++i)
20850 {
20851 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20852 line_ptr += 1;
20853 }
20854
20855 if (lh->version >= 5)
20856 {
20857 /* Read directory table. */
20858 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20859 &cu->header,
20860 [] (struct line_header *header, const char *name,
20861 dir_index d_index, unsigned int mod_time,
20862 unsigned int length)
20863 {
20864 header->add_include_dir (name);
20865 });
20866
20867 /* Read file name table. */
20868 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20869 &cu->header,
20870 [] (struct line_header *header, const char *name,
20871 dir_index d_index, unsigned int mod_time,
20872 unsigned int length)
20873 {
20874 header->add_file_name (name, d_index, mod_time, length);
20875 });
20876 }
20877 else
20878 {
20879 /* Read directory table. */
20880 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20881 {
20882 line_ptr += bytes_read;
20883 lh->add_include_dir (cur_dir);
20884 }
20885 line_ptr += bytes_read;
20886
20887 /* Read file name table. */
20888 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20889 {
20890 unsigned int mod_time, length;
20891 dir_index d_index;
20892
20893 line_ptr += bytes_read;
20894 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20895 line_ptr += bytes_read;
20896 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20897 line_ptr += bytes_read;
20898 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20899 line_ptr += bytes_read;
20900
20901 lh->add_file_name (cur_file, d_index, mod_time, length);
20902 }
20903 line_ptr += bytes_read;
20904 }
20905
20906 if (line_ptr > (section->buffer + section->size))
20907 complaint (_("line number info header doesn't "
20908 "fit in `.debug_line' section"));
20909
20910 return lh;
20911 }
20912
20913 /* Subroutine of dwarf_decode_lines to simplify it.
20914 Return the file name of the psymtab for the given file_entry.
20915 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20916 If space for the result is malloc'd, *NAME_HOLDER will be set.
20917 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20918
20919 static const char *
20920 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20921 const struct partial_symtab *pst,
20922 const char *comp_dir,
20923 gdb::unique_xmalloc_ptr<char> *name_holder)
20924 {
20925 const char *include_name = fe.name;
20926 const char *include_name_to_compare = include_name;
20927 const char *pst_filename;
20928 int file_is_pst;
20929
20930 const char *dir_name = fe.include_dir (lh);
20931
20932 gdb::unique_xmalloc_ptr<char> hold_compare;
20933 if (!IS_ABSOLUTE_PATH (include_name)
20934 && (dir_name != NULL || comp_dir != NULL))
20935 {
20936 /* Avoid creating a duplicate psymtab for PST.
20937 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20938 Before we do the comparison, however, we need to account
20939 for DIR_NAME and COMP_DIR.
20940 First prepend dir_name (if non-NULL). If we still don't
20941 have an absolute path prepend comp_dir (if non-NULL).
20942 However, the directory we record in the include-file's
20943 psymtab does not contain COMP_DIR (to match the
20944 corresponding symtab(s)).
20945
20946 Example:
20947
20948 bash$ cd /tmp
20949 bash$ gcc -g ./hello.c
20950 include_name = "hello.c"
20951 dir_name = "."
20952 DW_AT_comp_dir = comp_dir = "/tmp"
20953 DW_AT_name = "./hello.c"
20954
20955 */
20956
20957 if (dir_name != NULL)
20958 {
20959 name_holder->reset (concat (dir_name, SLASH_STRING,
20960 include_name, (char *) NULL));
20961 include_name = name_holder->get ();
20962 include_name_to_compare = include_name;
20963 }
20964 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20965 {
20966 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20967 include_name, (char *) NULL));
20968 include_name_to_compare = hold_compare.get ();
20969 }
20970 }
20971
20972 pst_filename = pst->filename;
20973 gdb::unique_xmalloc_ptr<char> copied_name;
20974 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20975 {
20976 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20977 pst_filename, (char *) NULL));
20978 pst_filename = copied_name.get ();
20979 }
20980
20981 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20982
20983 if (file_is_pst)
20984 return NULL;
20985 return include_name;
20986 }
20987
20988 /* State machine to track the state of the line number program. */
20989
20990 class lnp_state_machine
20991 {
20992 public:
20993 /* Initialize a machine state for the start of a line number
20994 program. */
20995 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20996 bool record_lines_p);
20997
20998 file_entry *current_file ()
20999 {
21000 /* lh->file_names is 0-based, but the file name numbers in the
21001 statement program are 1-based. */
21002 return m_line_header->file_name_at (m_file);
21003 }
21004
21005 /* Record the line in the state machine. END_SEQUENCE is true if
21006 we're processing the end of a sequence. */
21007 void record_line (bool end_sequence);
21008
21009 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
21010 nop-out rest of the lines in this sequence. */
21011 void check_line_address (struct dwarf2_cu *cu,
21012 const gdb_byte *line_ptr,
21013 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
21014
21015 void handle_set_discriminator (unsigned int discriminator)
21016 {
21017 m_discriminator = discriminator;
21018 m_line_has_non_zero_discriminator |= discriminator != 0;
21019 }
21020
21021 /* Handle DW_LNE_set_address. */
21022 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21023 {
21024 m_op_index = 0;
21025 address += baseaddr;
21026 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21027 }
21028
21029 /* Handle DW_LNS_advance_pc. */
21030 void handle_advance_pc (CORE_ADDR adjust);
21031
21032 /* Handle a special opcode. */
21033 void handle_special_opcode (unsigned char op_code);
21034
21035 /* Handle DW_LNS_advance_line. */
21036 void handle_advance_line (int line_delta)
21037 {
21038 advance_line (line_delta);
21039 }
21040
21041 /* Handle DW_LNS_set_file. */
21042 void handle_set_file (file_name_index file);
21043
21044 /* Handle DW_LNS_negate_stmt. */
21045 void handle_negate_stmt ()
21046 {
21047 m_is_stmt = !m_is_stmt;
21048 }
21049
21050 /* Handle DW_LNS_const_add_pc. */
21051 void handle_const_add_pc ();
21052
21053 /* Handle DW_LNS_fixed_advance_pc. */
21054 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21055 {
21056 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21057 m_op_index = 0;
21058 }
21059
21060 /* Handle DW_LNS_copy. */
21061 void handle_copy ()
21062 {
21063 record_line (false);
21064 m_discriminator = 0;
21065 }
21066
21067 /* Handle DW_LNE_end_sequence. */
21068 void handle_end_sequence ()
21069 {
21070 m_currently_recording_lines = true;
21071 }
21072
21073 private:
21074 /* Advance the line by LINE_DELTA. */
21075 void advance_line (int line_delta)
21076 {
21077 m_line += line_delta;
21078
21079 if (line_delta != 0)
21080 m_line_has_non_zero_discriminator = m_discriminator != 0;
21081 }
21082
21083 struct dwarf2_cu *m_cu;
21084
21085 gdbarch *m_gdbarch;
21086
21087 /* True if we're recording lines.
21088 Otherwise we're building partial symtabs and are just interested in
21089 finding include files mentioned by the line number program. */
21090 bool m_record_lines_p;
21091
21092 /* The line number header. */
21093 line_header *m_line_header;
21094
21095 /* These are part of the standard DWARF line number state machine,
21096 and initialized according to the DWARF spec. */
21097
21098 unsigned char m_op_index = 0;
21099 /* The line table index of the current file. */
21100 file_name_index m_file = 1;
21101 unsigned int m_line = 1;
21102
21103 /* These are initialized in the constructor. */
21104
21105 CORE_ADDR m_address;
21106 bool m_is_stmt;
21107 unsigned int m_discriminator;
21108
21109 /* Additional bits of state we need to track. */
21110
21111 /* The last file that we called dwarf2_start_subfile for.
21112 This is only used for TLLs. */
21113 unsigned int m_last_file = 0;
21114 /* The last file a line number was recorded for. */
21115 struct subfile *m_last_subfile = NULL;
21116
21117 /* When true, record the lines we decode. */
21118 bool m_currently_recording_lines = false;
21119
21120 /* The last line number that was recorded, used to coalesce
21121 consecutive entries for the same line. This can happen, for
21122 example, when discriminators are present. PR 17276. */
21123 unsigned int m_last_line = 0;
21124 bool m_line_has_non_zero_discriminator = false;
21125 };
21126
21127 void
21128 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21129 {
21130 CORE_ADDR addr_adj = (((m_op_index + adjust)
21131 / m_line_header->maximum_ops_per_instruction)
21132 * m_line_header->minimum_instruction_length);
21133 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21134 m_op_index = ((m_op_index + adjust)
21135 % m_line_header->maximum_ops_per_instruction);
21136 }
21137
21138 void
21139 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21140 {
21141 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21142 CORE_ADDR addr_adj = (((m_op_index
21143 + (adj_opcode / m_line_header->line_range))
21144 / m_line_header->maximum_ops_per_instruction)
21145 * m_line_header->minimum_instruction_length);
21146 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21147 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
21148 % m_line_header->maximum_ops_per_instruction);
21149
21150 int line_delta = (m_line_header->line_base
21151 + (adj_opcode % m_line_header->line_range));
21152 advance_line (line_delta);
21153 record_line (false);
21154 m_discriminator = 0;
21155 }
21156
21157 void
21158 lnp_state_machine::handle_set_file (file_name_index file)
21159 {
21160 m_file = file;
21161
21162 const file_entry *fe = current_file ();
21163 if (fe == NULL)
21164 dwarf2_debug_line_missing_file_complaint ();
21165 else if (m_record_lines_p)
21166 {
21167 const char *dir = fe->include_dir (m_line_header);
21168
21169 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21170 m_line_has_non_zero_discriminator = m_discriminator != 0;
21171 dwarf2_start_subfile (m_cu, fe->name, dir);
21172 }
21173 }
21174
21175 void
21176 lnp_state_machine::handle_const_add_pc ()
21177 {
21178 CORE_ADDR adjust
21179 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21180
21181 CORE_ADDR addr_adj
21182 = (((m_op_index + adjust)
21183 / m_line_header->maximum_ops_per_instruction)
21184 * m_line_header->minimum_instruction_length);
21185
21186 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21187 m_op_index = ((m_op_index + adjust)
21188 % m_line_header->maximum_ops_per_instruction);
21189 }
21190
21191 /* Return non-zero if we should add LINE to the line number table.
21192 LINE is the line to add, LAST_LINE is the last line that was added,
21193 LAST_SUBFILE is the subfile for LAST_LINE.
21194 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21195 had a non-zero discriminator.
21196
21197 We have to be careful in the presence of discriminators.
21198 E.g., for this line:
21199
21200 for (i = 0; i < 100000; i++);
21201
21202 clang can emit four line number entries for that one line,
21203 each with a different discriminator.
21204 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21205
21206 However, we want gdb to coalesce all four entries into one.
21207 Otherwise the user could stepi into the middle of the line and
21208 gdb would get confused about whether the pc really was in the
21209 middle of the line.
21210
21211 Things are further complicated by the fact that two consecutive
21212 line number entries for the same line is a heuristic used by gcc
21213 to denote the end of the prologue. So we can't just discard duplicate
21214 entries, we have to be selective about it. The heuristic we use is
21215 that we only collapse consecutive entries for the same line if at least
21216 one of those entries has a non-zero discriminator. PR 17276.
21217
21218 Note: Addresses in the line number state machine can never go backwards
21219 within one sequence, thus this coalescing is ok. */
21220
21221 static int
21222 dwarf_record_line_p (struct dwarf2_cu *cu,
21223 unsigned int line, unsigned int last_line,
21224 int line_has_non_zero_discriminator,
21225 struct subfile *last_subfile)
21226 {
21227 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21228 return 1;
21229 if (line != last_line)
21230 return 1;
21231 /* Same line for the same file that we've seen already.
21232 As a last check, for pr 17276, only record the line if the line
21233 has never had a non-zero discriminator. */
21234 if (!line_has_non_zero_discriminator)
21235 return 1;
21236 return 0;
21237 }
21238
21239 /* Use the CU's builder to record line number LINE beginning at
21240 address ADDRESS in the line table of subfile SUBFILE. */
21241
21242 static void
21243 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21244 unsigned int line, CORE_ADDR address,
21245 struct dwarf2_cu *cu)
21246 {
21247 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21248
21249 if (dwarf_line_debug)
21250 {
21251 fprintf_unfiltered (gdb_stdlog,
21252 "Recording line %u, file %s, address %s\n",
21253 line, lbasename (subfile->name),
21254 paddress (gdbarch, address));
21255 }
21256
21257 if (cu != nullptr)
21258 cu->get_builder ()->record_line (subfile, line, addr);
21259 }
21260
21261 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21262 Mark the end of a set of line number records.
21263 The arguments are the same as for dwarf_record_line_1.
21264 If SUBFILE is NULL the request is ignored. */
21265
21266 static void
21267 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21268 CORE_ADDR address, struct dwarf2_cu *cu)
21269 {
21270 if (subfile == NULL)
21271 return;
21272
21273 if (dwarf_line_debug)
21274 {
21275 fprintf_unfiltered (gdb_stdlog,
21276 "Finishing current line, file %s, address %s\n",
21277 lbasename (subfile->name),
21278 paddress (gdbarch, address));
21279 }
21280
21281 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
21282 }
21283
21284 void
21285 lnp_state_machine::record_line (bool end_sequence)
21286 {
21287 if (dwarf_line_debug)
21288 {
21289 fprintf_unfiltered (gdb_stdlog,
21290 "Processing actual line %u: file %u,"
21291 " address %s, is_stmt %u, discrim %u\n",
21292 m_line, m_file,
21293 paddress (m_gdbarch, m_address),
21294 m_is_stmt, m_discriminator);
21295 }
21296
21297 file_entry *fe = current_file ();
21298
21299 if (fe == NULL)
21300 dwarf2_debug_line_missing_file_complaint ();
21301 /* For now we ignore lines not starting on an instruction boundary.
21302 But not when processing end_sequence for compatibility with the
21303 previous version of the code. */
21304 else if (m_op_index == 0 || end_sequence)
21305 {
21306 fe->included_p = 1;
21307 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
21308 {
21309 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
21310 || end_sequence)
21311 {
21312 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21313 m_currently_recording_lines ? m_cu : nullptr);
21314 }
21315
21316 if (!end_sequence)
21317 {
21318 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21319 m_line_has_non_zero_discriminator,
21320 m_last_subfile))
21321 {
21322 buildsym_compunit *builder = m_cu->get_builder ();
21323 dwarf_record_line_1 (m_gdbarch,
21324 builder->get_current_subfile (),
21325 m_line, m_address,
21326 m_currently_recording_lines ? m_cu : nullptr);
21327 }
21328 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21329 m_last_line = m_line;
21330 }
21331 }
21332 }
21333 }
21334
21335 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21336 line_header *lh, bool record_lines_p)
21337 {
21338 m_cu = cu;
21339 m_gdbarch = arch;
21340 m_record_lines_p = record_lines_p;
21341 m_line_header = lh;
21342
21343 m_currently_recording_lines = true;
21344
21345 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21346 was a line entry for it so that the backend has a chance to adjust it
21347 and also record it in case it needs it. This is currently used by MIPS
21348 code, cf. `mips_adjust_dwarf2_line'. */
21349 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21350 m_is_stmt = lh->default_is_stmt;
21351 m_discriminator = 0;
21352 }
21353
21354 void
21355 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21356 const gdb_byte *line_ptr,
21357 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21358 {
21359 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21360 the pc range of the CU. However, we restrict the test to only ADDRESS
21361 values of zero to preserve GDB's previous behaviour which is to handle
21362 the specific case of a function being GC'd by the linker. */
21363
21364 if (address == 0 && address < unrelocated_lowpc)
21365 {
21366 /* This line table is for a function which has been
21367 GCd by the linker. Ignore it. PR gdb/12528 */
21368
21369 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21370 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21371
21372 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21373 line_offset, objfile_name (objfile));
21374 m_currently_recording_lines = false;
21375 /* Note: m_currently_recording_lines is left as false until we see
21376 DW_LNE_end_sequence. */
21377 }
21378 }
21379
21380 /* Subroutine of dwarf_decode_lines to simplify it.
21381 Process the line number information in LH.
21382 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21383 program in order to set included_p for every referenced header. */
21384
21385 static void
21386 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21387 const int decode_for_pst_p, CORE_ADDR lowpc)
21388 {
21389 const gdb_byte *line_ptr, *extended_end;
21390 const gdb_byte *line_end;
21391 unsigned int bytes_read, extended_len;
21392 unsigned char op_code, extended_op;
21393 CORE_ADDR baseaddr;
21394 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21395 bfd *abfd = objfile->obfd;
21396 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21397 /* True if we're recording line info (as opposed to building partial
21398 symtabs and just interested in finding include files mentioned by
21399 the line number program). */
21400 bool record_lines_p = !decode_for_pst_p;
21401
21402 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21403
21404 line_ptr = lh->statement_program_start;
21405 line_end = lh->statement_program_end;
21406
21407 /* Read the statement sequences until there's nothing left. */
21408 while (line_ptr < line_end)
21409 {
21410 /* The DWARF line number program state machine. Reset the state
21411 machine at the start of each sequence. */
21412 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21413 bool end_sequence = false;
21414
21415 if (record_lines_p)
21416 {
21417 /* Start a subfile for the current file of the state
21418 machine. */
21419 const file_entry *fe = state_machine.current_file ();
21420
21421 if (fe != NULL)
21422 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21423 }
21424
21425 /* Decode the table. */
21426 while (line_ptr < line_end && !end_sequence)
21427 {
21428 op_code = read_1_byte (abfd, line_ptr);
21429 line_ptr += 1;
21430
21431 if (op_code >= lh->opcode_base)
21432 {
21433 /* Special opcode. */
21434 state_machine.handle_special_opcode (op_code);
21435 }
21436 else switch (op_code)
21437 {
21438 case DW_LNS_extended_op:
21439 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21440 &bytes_read);
21441 line_ptr += bytes_read;
21442 extended_end = line_ptr + extended_len;
21443 extended_op = read_1_byte (abfd, line_ptr);
21444 line_ptr += 1;
21445 switch (extended_op)
21446 {
21447 case DW_LNE_end_sequence:
21448 state_machine.handle_end_sequence ();
21449 end_sequence = true;
21450 break;
21451 case DW_LNE_set_address:
21452 {
21453 CORE_ADDR address
21454 = read_address (abfd, line_ptr, cu, &bytes_read);
21455 line_ptr += bytes_read;
21456
21457 state_machine.check_line_address (cu, line_ptr,
21458 lowpc - baseaddr, address);
21459 state_machine.handle_set_address (baseaddr, address);
21460 }
21461 break;
21462 case DW_LNE_define_file:
21463 {
21464 const char *cur_file;
21465 unsigned int mod_time, length;
21466 dir_index dindex;
21467
21468 cur_file = read_direct_string (abfd, line_ptr,
21469 &bytes_read);
21470 line_ptr += bytes_read;
21471 dindex = (dir_index)
21472 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21473 line_ptr += bytes_read;
21474 mod_time =
21475 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21476 line_ptr += bytes_read;
21477 length =
21478 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21479 line_ptr += bytes_read;
21480 lh->add_file_name (cur_file, dindex, mod_time, length);
21481 }
21482 break;
21483 case DW_LNE_set_discriminator:
21484 {
21485 /* The discriminator is not interesting to the
21486 debugger; just ignore it. We still need to
21487 check its value though:
21488 if there are consecutive entries for the same
21489 (non-prologue) line we want to coalesce them.
21490 PR 17276. */
21491 unsigned int discr
21492 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21493 line_ptr += bytes_read;
21494
21495 state_machine.handle_set_discriminator (discr);
21496 }
21497 break;
21498 default:
21499 complaint (_("mangled .debug_line section"));
21500 return;
21501 }
21502 /* Make sure that we parsed the extended op correctly. If e.g.
21503 we expected a different address size than the producer used,
21504 we may have read the wrong number of bytes. */
21505 if (line_ptr != extended_end)
21506 {
21507 complaint (_("mangled .debug_line section"));
21508 return;
21509 }
21510 break;
21511 case DW_LNS_copy:
21512 state_machine.handle_copy ();
21513 break;
21514 case DW_LNS_advance_pc:
21515 {
21516 CORE_ADDR adjust
21517 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21518 line_ptr += bytes_read;
21519
21520 state_machine.handle_advance_pc (adjust);
21521 }
21522 break;
21523 case DW_LNS_advance_line:
21524 {
21525 int line_delta
21526 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21527 line_ptr += bytes_read;
21528
21529 state_machine.handle_advance_line (line_delta);
21530 }
21531 break;
21532 case DW_LNS_set_file:
21533 {
21534 file_name_index file
21535 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21536 &bytes_read);
21537 line_ptr += bytes_read;
21538
21539 state_machine.handle_set_file (file);
21540 }
21541 break;
21542 case DW_LNS_set_column:
21543 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21544 line_ptr += bytes_read;
21545 break;
21546 case DW_LNS_negate_stmt:
21547 state_machine.handle_negate_stmt ();
21548 break;
21549 case DW_LNS_set_basic_block:
21550 break;
21551 /* Add to the address register of the state machine the
21552 address increment value corresponding to special opcode
21553 255. I.e., this value is scaled by the minimum
21554 instruction length since special opcode 255 would have
21555 scaled the increment. */
21556 case DW_LNS_const_add_pc:
21557 state_machine.handle_const_add_pc ();
21558 break;
21559 case DW_LNS_fixed_advance_pc:
21560 {
21561 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21562 line_ptr += 2;
21563
21564 state_machine.handle_fixed_advance_pc (addr_adj);
21565 }
21566 break;
21567 default:
21568 {
21569 /* Unknown standard opcode, ignore it. */
21570 int i;
21571
21572 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21573 {
21574 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21575 line_ptr += bytes_read;
21576 }
21577 }
21578 }
21579 }
21580
21581 if (!end_sequence)
21582 dwarf2_debug_line_missing_end_sequence_complaint ();
21583
21584 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21585 in which case we still finish recording the last line). */
21586 state_machine.record_line (true);
21587 }
21588 }
21589
21590 /* Decode the Line Number Program (LNP) for the given line_header
21591 structure and CU. The actual information extracted and the type
21592 of structures created from the LNP depends on the value of PST.
21593
21594 1. If PST is NULL, then this procedure uses the data from the program
21595 to create all necessary symbol tables, and their linetables.
21596
21597 2. If PST is not NULL, this procedure reads the program to determine
21598 the list of files included by the unit represented by PST, and
21599 builds all the associated partial symbol tables.
21600
21601 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21602 It is used for relative paths in the line table.
21603 NOTE: When processing partial symtabs (pst != NULL),
21604 comp_dir == pst->dirname.
21605
21606 NOTE: It is important that psymtabs have the same file name (via strcmp)
21607 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21608 symtab we don't use it in the name of the psymtabs we create.
21609 E.g. expand_line_sal requires this when finding psymtabs to expand.
21610 A good testcase for this is mb-inline.exp.
21611
21612 LOWPC is the lowest address in CU (or 0 if not known).
21613
21614 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21615 for its PC<->lines mapping information. Otherwise only the filename
21616 table is read in. */
21617
21618 static void
21619 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21620 struct dwarf2_cu *cu, struct partial_symtab *pst,
21621 CORE_ADDR lowpc, int decode_mapping)
21622 {
21623 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21624 const int decode_for_pst_p = (pst != NULL);
21625
21626 if (decode_mapping)
21627 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21628
21629 if (decode_for_pst_p)
21630 {
21631 /* Now that we're done scanning the Line Header Program, we can
21632 create the psymtab of each included file. */
21633 for (auto &file_entry : lh->file_names ())
21634 if (file_entry.included_p == 1)
21635 {
21636 gdb::unique_xmalloc_ptr<char> name_holder;
21637 const char *include_name =
21638 psymtab_include_file_name (lh, file_entry, pst,
21639 comp_dir, &name_holder);
21640 if (include_name != NULL)
21641 dwarf2_create_include_psymtab (include_name, pst, objfile);
21642 }
21643 }
21644 else
21645 {
21646 /* Make sure a symtab is created for every file, even files
21647 which contain only variables (i.e. no code with associated
21648 line numbers). */
21649 buildsym_compunit *builder = cu->get_builder ();
21650 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21651
21652 for (auto &fe : lh->file_names ())
21653 {
21654 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21655 if (builder->get_current_subfile ()->symtab == NULL)
21656 {
21657 builder->get_current_subfile ()->symtab
21658 = allocate_symtab (cust,
21659 builder->get_current_subfile ()->name);
21660 }
21661 fe.symtab = builder->get_current_subfile ()->symtab;
21662 }
21663 }
21664 }
21665
21666 /* Start a subfile for DWARF. FILENAME is the name of the file and
21667 DIRNAME the name of the source directory which contains FILENAME
21668 or NULL if not known.
21669 This routine tries to keep line numbers from identical absolute and
21670 relative file names in a common subfile.
21671
21672 Using the `list' example from the GDB testsuite, which resides in
21673 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21674 of /srcdir/list0.c yields the following debugging information for list0.c:
21675
21676 DW_AT_name: /srcdir/list0.c
21677 DW_AT_comp_dir: /compdir
21678 files.files[0].name: list0.h
21679 files.files[0].dir: /srcdir
21680 files.files[1].name: list0.c
21681 files.files[1].dir: /srcdir
21682
21683 The line number information for list0.c has to end up in a single
21684 subfile, so that `break /srcdir/list0.c:1' works as expected.
21685 start_subfile will ensure that this happens provided that we pass the
21686 concatenation of files.files[1].dir and files.files[1].name as the
21687 subfile's name. */
21688
21689 static void
21690 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21691 const char *dirname)
21692 {
21693 gdb::unique_xmalloc_ptr<char> copy;
21694
21695 /* In order not to lose the line information directory,
21696 we concatenate it to the filename when it makes sense.
21697 Note that the Dwarf3 standard says (speaking of filenames in line
21698 information): ``The directory index is ignored for file names
21699 that represent full path names''. Thus ignoring dirname in the
21700 `else' branch below isn't an issue. */
21701
21702 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21703 {
21704 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21705 filename = copy.get ();
21706 }
21707
21708 cu->get_builder ()->start_subfile (filename);
21709 }
21710
21711 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21712 buildsym_compunit constructor. */
21713
21714 struct compunit_symtab *
21715 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21716 CORE_ADDR low_pc)
21717 {
21718 gdb_assert (m_builder == nullptr);
21719
21720 m_builder.reset (new struct buildsym_compunit
21721 (per_cu->dwarf2_per_objfile->objfile,
21722 name, comp_dir, language, low_pc));
21723
21724 list_in_scope = get_builder ()->get_file_symbols ();
21725
21726 get_builder ()->record_debugformat ("DWARF 2");
21727 get_builder ()->record_producer (producer);
21728
21729 processing_has_namespace_info = false;
21730
21731 return get_builder ()->get_compunit_symtab ();
21732 }
21733
21734 static void
21735 var_decode_location (struct attribute *attr, struct symbol *sym,
21736 struct dwarf2_cu *cu)
21737 {
21738 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21739 struct comp_unit_head *cu_header = &cu->header;
21740
21741 /* NOTE drow/2003-01-30: There used to be a comment and some special
21742 code here to turn a symbol with DW_AT_external and a
21743 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21744 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21745 with some versions of binutils) where shared libraries could have
21746 relocations against symbols in their debug information - the
21747 minimal symbol would have the right address, but the debug info
21748 would not. It's no longer necessary, because we will explicitly
21749 apply relocations when we read in the debug information now. */
21750
21751 /* A DW_AT_location attribute with no contents indicates that a
21752 variable has been optimized away. */
21753 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21754 {
21755 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21756 return;
21757 }
21758
21759 /* Handle one degenerate form of location expression specially, to
21760 preserve GDB's previous behavior when section offsets are
21761 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21762 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21763
21764 if (attr_form_is_block (attr)
21765 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21766 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21767 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21768 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21769 && (DW_BLOCK (attr)->size
21770 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21771 {
21772 unsigned int dummy;
21773
21774 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21775 SET_SYMBOL_VALUE_ADDRESS (sym,
21776 read_address (objfile->obfd,
21777 DW_BLOCK (attr)->data + 1,
21778 cu, &dummy));
21779 else
21780 SET_SYMBOL_VALUE_ADDRESS
21781 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21782 &dummy));
21783 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21784 fixup_symbol_section (sym, objfile);
21785 SET_SYMBOL_VALUE_ADDRESS (sym,
21786 SYMBOL_VALUE_ADDRESS (sym)
21787 + ANOFFSET (objfile->section_offsets,
21788 SYMBOL_SECTION (sym)));
21789 return;
21790 }
21791
21792 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21793 expression evaluator, and use LOC_COMPUTED only when necessary
21794 (i.e. when the value of a register or memory location is
21795 referenced, or a thread-local block, etc.). Then again, it might
21796 not be worthwhile. I'm assuming that it isn't unless performance
21797 or memory numbers show me otherwise. */
21798
21799 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21800
21801 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21802 cu->has_loclist = true;
21803 }
21804
21805 /* Given a pointer to a DWARF information entry, figure out if we need
21806 to make a symbol table entry for it, and if so, create a new entry
21807 and return a pointer to it.
21808 If TYPE is NULL, determine symbol type from the die, otherwise
21809 used the passed type.
21810 If SPACE is not NULL, use it to hold the new symbol. If it is
21811 NULL, allocate a new symbol on the objfile's obstack. */
21812
21813 static struct symbol *
21814 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21815 struct symbol *space)
21816 {
21817 struct dwarf2_per_objfile *dwarf2_per_objfile
21818 = cu->per_cu->dwarf2_per_objfile;
21819 struct objfile *objfile = dwarf2_per_objfile->objfile;
21820 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21821 struct symbol *sym = NULL;
21822 const char *name;
21823 struct attribute *attr = NULL;
21824 struct attribute *attr2 = NULL;
21825 CORE_ADDR baseaddr;
21826 struct pending **list_to_add = NULL;
21827
21828 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21829
21830 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21831
21832 name = dwarf2_name (die, cu);
21833 if (name)
21834 {
21835 const char *linkagename;
21836 int suppress_add = 0;
21837
21838 if (space)
21839 sym = space;
21840 else
21841 sym = allocate_symbol (objfile);
21842 OBJSTAT (objfile, n_syms++);
21843
21844 /* Cache this symbol's name and the name's demangled form (if any). */
21845 sym->set_language (cu->language, &objfile->objfile_obstack);
21846 linkagename = dwarf2_physname (name, die, cu);
21847 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
21848
21849 /* Fortran does not have mangling standard and the mangling does differ
21850 between gfortran, iFort etc. */
21851 if (cu->language == language_fortran
21852 && symbol_get_demangled_name (sym) == NULL)
21853 symbol_set_demangled_name (sym,
21854 dwarf2_full_name (name, die, cu),
21855 NULL);
21856
21857 /* Default assumptions.
21858 Use the passed type or decode it from the die. */
21859 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21860 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21861 if (type != NULL)
21862 SYMBOL_TYPE (sym) = type;
21863 else
21864 SYMBOL_TYPE (sym) = die_type (die, cu);
21865 attr = dwarf2_attr (die,
21866 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21867 cu);
21868 if (attr != nullptr)
21869 {
21870 SYMBOL_LINE (sym) = DW_UNSND (attr);
21871 }
21872
21873 attr = dwarf2_attr (die,
21874 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21875 cu);
21876 if (attr != nullptr)
21877 {
21878 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21879 struct file_entry *fe;
21880
21881 if (cu->line_header != NULL)
21882 fe = cu->line_header->file_name_at (file_index);
21883 else
21884 fe = NULL;
21885
21886 if (fe == NULL)
21887 complaint (_("file index out of range"));
21888 else
21889 symbol_set_symtab (sym, fe->symtab);
21890 }
21891
21892 switch (die->tag)
21893 {
21894 case DW_TAG_label:
21895 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21896 if (attr != nullptr)
21897 {
21898 CORE_ADDR addr;
21899
21900 addr = attr_value_as_address (attr);
21901 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21902 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21903 }
21904 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21905 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21906 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21907 add_symbol_to_list (sym, cu->list_in_scope);
21908 break;
21909 case DW_TAG_subprogram:
21910 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21911 finish_block. */
21912 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21913 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21914 if ((attr2 && (DW_UNSND (attr2) != 0))
21915 || cu->language == language_ada
21916 || cu->language == language_fortran)
21917 {
21918 /* Subprograms marked external are stored as a global symbol.
21919 Ada and Fortran subprograms, whether marked external or
21920 not, are always stored as a global symbol, because we want
21921 to be able to access them globally. For instance, we want
21922 to be able to break on a nested subprogram without having
21923 to specify the context. */
21924 list_to_add = cu->get_builder ()->get_global_symbols ();
21925 }
21926 else
21927 {
21928 list_to_add = cu->list_in_scope;
21929 }
21930 break;
21931 case DW_TAG_inlined_subroutine:
21932 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21933 finish_block. */
21934 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21935 SYMBOL_INLINED (sym) = 1;
21936 list_to_add = cu->list_in_scope;
21937 break;
21938 case DW_TAG_template_value_param:
21939 suppress_add = 1;
21940 /* Fall through. */
21941 case DW_TAG_constant:
21942 case DW_TAG_variable:
21943 case DW_TAG_member:
21944 /* Compilation with minimal debug info may result in
21945 variables with missing type entries. Change the
21946 misleading `void' type to something sensible. */
21947 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21948 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21949
21950 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21951 /* In the case of DW_TAG_member, we should only be called for
21952 static const members. */
21953 if (die->tag == DW_TAG_member)
21954 {
21955 /* dwarf2_add_field uses die_is_declaration,
21956 so we do the same. */
21957 gdb_assert (die_is_declaration (die, cu));
21958 gdb_assert (attr);
21959 }
21960 if (attr != nullptr)
21961 {
21962 dwarf2_const_value (attr, sym, cu);
21963 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21964 if (!suppress_add)
21965 {
21966 if (attr2 && (DW_UNSND (attr2) != 0))
21967 list_to_add = cu->get_builder ()->get_global_symbols ();
21968 else
21969 list_to_add = cu->list_in_scope;
21970 }
21971 break;
21972 }
21973 attr = dwarf2_attr (die, DW_AT_location, cu);
21974 if (attr != nullptr)
21975 {
21976 var_decode_location (attr, sym, cu);
21977 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21978
21979 /* Fortran explicitly imports any global symbols to the local
21980 scope by DW_TAG_common_block. */
21981 if (cu->language == language_fortran && die->parent
21982 && die->parent->tag == DW_TAG_common_block)
21983 attr2 = NULL;
21984
21985 if (SYMBOL_CLASS (sym) == LOC_STATIC
21986 && SYMBOL_VALUE_ADDRESS (sym) == 0
21987 && !dwarf2_per_objfile->has_section_at_zero)
21988 {
21989 /* When a static variable is eliminated by the linker,
21990 the corresponding debug information is not stripped
21991 out, but the variable address is set to null;
21992 do not add such variables into symbol table. */
21993 }
21994 else if (attr2 && (DW_UNSND (attr2) != 0))
21995 {
21996 if (SYMBOL_CLASS (sym) == LOC_STATIC
21997 && (objfile->flags & OBJF_MAINLINE) == 0
21998 && dwarf2_per_objfile->can_copy)
21999 {
22000 /* A global static variable might be subject to
22001 copy relocation. We first check for a local
22002 minsym, though, because maybe the symbol was
22003 marked hidden, in which case this would not
22004 apply. */
22005 bound_minimal_symbol found
22006 = (lookup_minimal_symbol_linkage
22007 (sym->linkage_name (), objfile));
22008 if (found.minsym != nullptr)
22009 sym->maybe_copied = 1;
22010 }
22011
22012 /* A variable with DW_AT_external is never static,
22013 but it may be block-scoped. */
22014 list_to_add
22015 = ((cu->list_in_scope
22016 == cu->get_builder ()->get_file_symbols ())
22017 ? cu->get_builder ()->get_global_symbols ()
22018 : cu->list_in_scope);
22019 }
22020 else
22021 list_to_add = cu->list_in_scope;
22022 }
22023 else
22024 {
22025 /* We do not know the address of this symbol.
22026 If it is an external symbol and we have type information
22027 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22028 The address of the variable will then be determined from
22029 the minimal symbol table whenever the variable is
22030 referenced. */
22031 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22032
22033 /* Fortran explicitly imports any global symbols to the local
22034 scope by DW_TAG_common_block. */
22035 if (cu->language == language_fortran && die->parent
22036 && die->parent->tag == DW_TAG_common_block)
22037 {
22038 /* SYMBOL_CLASS doesn't matter here because
22039 read_common_block is going to reset it. */
22040 if (!suppress_add)
22041 list_to_add = cu->list_in_scope;
22042 }
22043 else if (attr2 && (DW_UNSND (attr2) != 0)
22044 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22045 {
22046 /* A variable with DW_AT_external is never static, but it
22047 may be block-scoped. */
22048 list_to_add
22049 = ((cu->list_in_scope
22050 == cu->get_builder ()->get_file_symbols ())
22051 ? cu->get_builder ()->get_global_symbols ()
22052 : cu->list_in_scope);
22053
22054 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22055 }
22056 else if (!die_is_declaration (die, cu))
22057 {
22058 /* Use the default LOC_OPTIMIZED_OUT class. */
22059 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22060 if (!suppress_add)
22061 list_to_add = cu->list_in_scope;
22062 }
22063 }
22064 break;
22065 case DW_TAG_formal_parameter:
22066 {
22067 /* If we are inside a function, mark this as an argument. If
22068 not, we might be looking at an argument to an inlined function
22069 when we do not have enough information to show inlined frames;
22070 pretend it's a local variable in that case so that the user can
22071 still see it. */
22072 struct context_stack *curr
22073 = cu->get_builder ()->get_current_context_stack ();
22074 if (curr != nullptr && curr->name != nullptr)
22075 SYMBOL_IS_ARGUMENT (sym) = 1;
22076 attr = dwarf2_attr (die, DW_AT_location, cu);
22077 if (attr != nullptr)
22078 {
22079 var_decode_location (attr, sym, cu);
22080 }
22081 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22082 if (attr != nullptr)
22083 {
22084 dwarf2_const_value (attr, sym, cu);
22085 }
22086
22087 list_to_add = cu->list_in_scope;
22088 }
22089 break;
22090 case DW_TAG_unspecified_parameters:
22091 /* From varargs functions; gdb doesn't seem to have any
22092 interest in this information, so just ignore it for now.
22093 (FIXME?) */
22094 break;
22095 case DW_TAG_template_type_param:
22096 suppress_add = 1;
22097 /* Fall through. */
22098 case DW_TAG_class_type:
22099 case DW_TAG_interface_type:
22100 case DW_TAG_structure_type:
22101 case DW_TAG_union_type:
22102 case DW_TAG_set_type:
22103 case DW_TAG_enumeration_type:
22104 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22105 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22106
22107 {
22108 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22109 really ever be static objects: otherwise, if you try
22110 to, say, break of a class's method and you're in a file
22111 which doesn't mention that class, it won't work unless
22112 the check for all static symbols in lookup_symbol_aux
22113 saves you. See the OtherFileClass tests in
22114 gdb.c++/namespace.exp. */
22115
22116 if (!suppress_add)
22117 {
22118 buildsym_compunit *builder = cu->get_builder ();
22119 list_to_add
22120 = (cu->list_in_scope == builder->get_file_symbols ()
22121 && cu->language == language_cplus
22122 ? builder->get_global_symbols ()
22123 : cu->list_in_scope);
22124
22125 /* The semantics of C++ state that "struct foo {
22126 ... }" also defines a typedef for "foo". */
22127 if (cu->language == language_cplus
22128 || cu->language == language_ada
22129 || cu->language == language_d
22130 || cu->language == language_rust)
22131 {
22132 /* The symbol's name is already allocated along
22133 with this objfile, so we don't need to
22134 duplicate it for the type. */
22135 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
22136 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
22137 }
22138 }
22139 }
22140 break;
22141 case DW_TAG_typedef:
22142 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22143 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22144 list_to_add = cu->list_in_scope;
22145 break;
22146 case DW_TAG_base_type:
22147 case DW_TAG_subrange_type:
22148 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22149 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22150 list_to_add = cu->list_in_scope;
22151 break;
22152 case DW_TAG_enumerator:
22153 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22154 if (attr != nullptr)
22155 {
22156 dwarf2_const_value (attr, sym, cu);
22157 }
22158 {
22159 /* NOTE: carlton/2003-11-10: See comment above in the
22160 DW_TAG_class_type, etc. block. */
22161
22162 list_to_add
22163 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22164 && cu->language == language_cplus
22165 ? cu->get_builder ()->get_global_symbols ()
22166 : cu->list_in_scope);
22167 }
22168 break;
22169 case DW_TAG_imported_declaration:
22170 case DW_TAG_namespace:
22171 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22172 list_to_add = cu->get_builder ()->get_global_symbols ();
22173 break;
22174 case DW_TAG_module:
22175 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22176 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22177 list_to_add = cu->get_builder ()->get_global_symbols ();
22178 break;
22179 case DW_TAG_common_block:
22180 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22181 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22182 add_symbol_to_list (sym, cu->list_in_scope);
22183 break;
22184 default:
22185 /* Not a tag we recognize. Hopefully we aren't processing
22186 trash data, but since we must specifically ignore things
22187 we don't recognize, there is nothing else we should do at
22188 this point. */
22189 complaint (_("unsupported tag: '%s'"),
22190 dwarf_tag_name (die->tag));
22191 break;
22192 }
22193
22194 if (suppress_add)
22195 {
22196 sym->hash_next = objfile->template_symbols;
22197 objfile->template_symbols = sym;
22198 list_to_add = NULL;
22199 }
22200
22201 if (list_to_add != NULL)
22202 add_symbol_to_list (sym, list_to_add);
22203
22204 /* For the benefit of old versions of GCC, check for anonymous
22205 namespaces based on the demangled name. */
22206 if (!cu->processing_has_namespace_info
22207 && cu->language == language_cplus)
22208 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22209 }
22210 return (sym);
22211 }
22212
22213 /* Given an attr with a DW_FORM_dataN value in host byte order,
22214 zero-extend it as appropriate for the symbol's type. The DWARF
22215 standard (v4) is not entirely clear about the meaning of using
22216 DW_FORM_dataN for a constant with a signed type, where the type is
22217 wider than the data. The conclusion of a discussion on the DWARF
22218 list was that this is unspecified. We choose to always zero-extend
22219 because that is the interpretation long in use by GCC. */
22220
22221 static gdb_byte *
22222 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22223 struct dwarf2_cu *cu, LONGEST *value, int bits)
22224 {
22225 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22226 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22227 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22228 LONGEST l = DW_UNSND (attr);
22229
22230 if (bits < sizeof (*value) * 8)
22231 {
22232 l &= ((LONGEST) 1 << bits) - 1;
22233 *value = l;
22234 }
22235 else if (bits == sizeof (*value) * 8)
22236 *value = l;
22237 else
22238 {
22239 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22240 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22241 return bytes;
22242 }
22243
22244 return NULL;
22245 }
22246
22247 /* Read a constant value from an attribute. Either set *VALUE, or if
22248 the value does not fit in *VALUE, set *BYTES - either already
22249 allocated on the objfile obstack, or newly allocated on OBSTACK,
22250 or, set *BATON, if we translated the constant to a location
22251 expression. */
22252
22253 static void
22254 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22255 const char *name, struct obstack *obstack,
22256 struct dwarf2_cu *cu,
22257 LONGEST *value, const gdb_byte **bytes,
22258 struct dwarf2_locexpr_baton **baton)
22259 {
22260 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22261 struct comp_unit_head *cu_header = &cu->header;
22262 struct dwarf_block *blk;
22263 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22264 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22265
22266 *value = 0;
22267 *bytes = NULL;
22268 *baton = NULL;
22269
22270 switch (attr->form)
22271 {
22272 case DW_FORM_addr:
22273 case DW_FORM_addrx:
22274 case DW_FORM_GNU_addr_index:
22275 {
22276 gdb_byte *data;
22277
22278 if (TYPE_LENGTH (type) != cu_header->addr_size)
22279 dwarf2_const_value_length_mismatch_complaint (name,
22280 cu_header->addr_size,
22281 TYPE_LENGTH (type));
22282 /* Symbols of this form are reasonably rare, so we just
22283 piggyback on the existing location code rather than writing
22284 a new implementation of symbol_computed_ops. */
22285 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22286 (*baton)->per_cu = cu->per_cu;
22287 gdb_assert ((*baton)->per_cu);
22288
22289 (*baton)->size = 2 + cu_header->addr_size;
22290 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22291 (*baton)->data = data;
22292
22293 data[0] = DW_OP_addr;
22294 store_unsigned_integer (&data[1], cu_header->addr_size,
22295 byte_order, DW_ADDR (attr));
22296 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22297 }
22298 break;
22299 case DW_FORM_string:
22300 case DW_FORM_strp:
22301 case DW_FORM_strx:
22302 case DW_FORM_GNU_str_index:
22303 case DW_FORM_GNU_strp_alt:
22304 /* DW_STRING is already allocated on the objfile obstack, point
22305 directly to it. */
22306 *bytes = (const gdb_byte *) DW_STRING (attr);
22307 break;
22308 case DW_FORM_block1:
22309 case DW_FORM_block2:
22310 case DW_FORM_block4:
22311 case DW_FORM_block:
22312 case DW_FORM_exprloc:
22313 case DW_FORM_data16:
22314 blk = DW_BLOCK (attr);
22315 if (TYPE_LENGTH (type) != blk->size)
22316 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22317 TYPE_LENGTH (type));
22318 *bytes = blk->data;
22319 break;
22320
22321 /* The DW_AT_const_value attributes are supposed to carry the
22322 symbol's value "represented as it would be on the target
22323 architecture." By the time we get here, it's already been
22324 converted to host endianness, so we just need to sign- or
22325 zero-extend it as appropriate. */
22326 case DW_FORM_data1:
22327 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22328 break;
22329 case DW_FORM_data2:
22330 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22331 break;
22332 case DW_FORM_data4:
22333 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22334 break;
22335 case DW_FORM_data8:
22336 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22337 break;
22338
22339 case DW_FORM_sdata:
22340 case DW_FORM_implicit_const:
22341 *value = DW_SND (attr);
22342 break;
22343
22344 case DW_FORM_udata:
22345 *value = DW_UNSND (attr);
22346 break;
22347
22348 default:
22349 complaint (_("unsupported const value attribute form: '%s'"),
22350 dwarf_form_name (attr->form));
22351 *value = 0;
22352 break;
22353 }
22354 }
22355
22356
22357 /* Copy constant value from an attribute to a symbol. */
22358
22359 static void
22360 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22361 struct dwarf2_cu *cu)
22362 {
22363 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22364 LONGEST value;
22365 const gdb_byte *bytes;
22366 struct dwarf2_locexpr_baton *baton;
22367
22368 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22369 sym->print_name (),
22370 &objfile->objfile_obstack, cu,
22371 &value, &bytes, &baton);
22372
22373 if (baton != NULL)
22374 {
22375 SYMBOL_LOCATION_BATON (sym) = baton;
22376 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22377 }
22378 else if (bytes != NULL)
22379 {
22380 SYMBOL_VALUE_BYTES (sym) = bytes;
22381 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22382 }
22383 else
22384 {
22385 SYMBOL_VALUE (sym) = value;
22386 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22387 }
22388 }
22389
22390 /* Return the type of the die in question using its DW_AT_type attribute. */
22391
22392 static struct type *
22393 die_type (struct die_info *die, struct dwarf2_cu *cu)
22394 {
22395 struct attribute *type_attr;
22396
22397 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22398 if (!type_attr)
22399 {
22400 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22401 /* A missing DW_AT_type represents a void type. */
22402 return objfile_type (objfile)->builtin_void;
22403 }
22404
22405 return lookup_die_type (die, type_attr, cu);
22406 }
22407
22408 /* True iff CU's producer generates GNAT Ada auxiliary information
22409 that allows to find parallel types through that information instead
22410 of having to do expensive parallel lookups by type name. */
22411
22412 static int
22413 need_gnat_info (struct dwarf2_cu *cu)
22414 {
22415 /* Assume that the Ada compiler was GNAT, which always produces
22416 the auxiliary information. */
22417 return (cu->language == language_ada);
22418 }
22419
22420 /* Return the auxiliary type of the die in question using its
22421 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22422 attribute is not present. */
22423
22424 static struct type *
22425 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22426 {
22427 struct attribute *type_attr;
22428
22429 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22430 if (!type_attr)
22431 return NULL;
22432
22433 return lookup_die_type (die, type_attr, cu);
22434 }
22435
22436 /* If DIE has a descriptive_type attribute, then set the TYPE's
22437 descriptive type accordingly. */
22438
22439 static void
22440 set_descriptive_type (struct type *type, struct die_info *die,
22441 struct dwarf2_cu *cu)
22442 {
22443 struct type *descriptive_type = die_descriptive_type (die, cu);
22444
22445 if (descriptive_type)
22446 {
22447 ALLOCATE_GNAT_AUX_TYPE (type);
22448 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22449 }
22450 }
22451
22452 /* Return the containing type of the die in question using its
22453 DW_AT_containing_type attribute. */
22454
22455 static struct type *
22456 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22457 {
22458 struct attribute *type_attr;
22459 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22460
22461 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22462 if (!type_attr)
22463 error (_("Dwarf Error: Problem turning containing type into gdb type "
22464 "[in module %s]"), objfile_name (objfile));
22465
22466 return lookup_die_type (die, type_attr, cu);
22467 }
22468
22469 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22470
22471 static struct type *
22472 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22473 {
22474 struct dwarf2_per_objfile *dwarf2_per_objfile
22475 = cu->per_cu->dwarf2_per_objfile;
22476 struct objfile *objfile = dwarf2_per_objfile->objfile;
22477 char *saved;
22478
22479 std::string message
22480 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22481 objfile_name (objfile),
22482 sect_offset_str (cu->header.sect_off),
22483 sect_offset_str (die->sect_off));
22484 saved = obstack_strdup (&objfile->objfile_obstack, message);
22485
22486 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22487 }
22488
22489 /* Look up the type of DIE in CU using its type attribute ATTR.
22490 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22491 DW_AT_containing_type.
22492 If there is no type substitute an error marker. */
22493
22494 static struct type *
22495 lookup_die_type (struct die_info *die, const struct attribute *attr,
22496 struct dwarf2_cu *cu)
22497 {
22498 struct dwarf2_per_objfile *dwarf2_per_objfile
22499 = cu->per_cu->dwarf2_per_objfile;
22500 struct objfile *objfile = dwarf2_per_objfile->objfile;
22501 struct type *this_type;
22502
22503 gdb_assert (attr->name == DW_AT_type
22504 || attr->name == DW_AT_GNAT_descriptive_type
22505 || attr->name == DW_AT_containing_type);
22506
22507 /* First see if we have it cached. */
22508
22509 if (attr->form == DW_FORM_GNU_ref_alt)
22510 {
22511 struct dwarf2_per_cu_data *per_cu;
22512 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22513
22514 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22515 dwarf2_per_objfile);
22516 this_type = get_die_type_at_offset (sect_off, per_cu);
22517 }
22518 else if (attr_form_is_ref (attr))
22519 {
22520 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22521
22522 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22523 }
22524 else if (attr->form == DW_FORM_ref_sig8)
22525 {
22526 ULONGEST signature = DW_SIGNATURE (attr);
22527
22528 return get_signatured_type (die, signature, cu);
22529 }
22530 else
22531 {
22532 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22533 " at %s [in module %s]"),
22534 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22535 objfile_name (objfile));
22536 return build_error_marker_type (cu, die);
22537 }
22538
22539 /* If not cached we need to read it in. */
22540
22541 if (this_type == NULL)
22542 {
22543 struct die_info *type_die = NULL;
22544 struct dwarf2_cu *type_cu = cu;
22545
22546 if (attr_form_is_ref (attr))
22547 type_die = follow_die_ref (die, attr, &type_cu);
22548 if (type_die == NULL)
22549 return build_error_marker_type (cu, die);
22550 /* If we find the type now, it's probably because the type came
22551 from an inter-CU reference and the type's CU got expanded before
22552 ours. */
22553 this_type = read_type_die (type_die, type_cu);
22554 }
22555
22556 /* If we still don't have a type use an error marker. */
22557
22558 if (this_type == NULL)
22559 return build_error_marker_type (cu, die);
22560
22561 return this_type;
22562 }
22563
22564 /* Return the type in DIE, CU.
22565 Returns NULL for invalid types.
22566
22567 This first does a lookup in die_type_hash,
22568 and only reads the die in if necessary.
22569
22570 NOTE: This can be called when reading in partial or full symbols. */
22571
22572 static struct type *
22573 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22574 {
22575 struct type *this_type;
22576
22577 this_type = get_die_type (die, cu);
22578 if (this_type)
22579 return this_type;
22580
22581 return read_type_die_1 (die, cu);
22582 }
22583
22584 /* Read the type in DIE, CU.
22585 Returns NULL for invalid types. */
22586
22587 static struct type *
22588 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22589 {
22590 struct type *this_type = NULL;
22591
22592 switch (die->tag)
22593 {
22594 case DW_TAG_class_type:
22595 case DW_TAG_interface_type:
22596 case DW_TAG_structure_type:
22597 case DW_TAG_union_type:
22598 this_type = read_structure_type (die, cu);
22599 break;
22600 case DW_TAG_enumeration_type:
22601 this_type = read_enumeration_type (die, cu);
22602 break;
22603 case DW_TAG_subprogram:
22604 case DW_TAG_subroutine_type:
22605 case DW_TAG_inlined_subroutine:
22606 this_type = read_subroutine_type (die, cu);
22607 break;
22608 case DW_TAG_array_type:
22609 this_type = read_array_type (die, cu);
22610 break;
22611 case DW_TAG_set_type:
22612 this_type = read_set_type (die, cu);
22613 break;
22614 case DW_TAG_pointer_type:
22615 this_type = read_tag_pointer_type (die, cu);
22616 break;
22617 case DW_TAG_ptr_to_member_type:
22618 this_type = read_tag_ptr_to_member_type (die, cu);
22619 break;
22620 case DW_TAG_reference_type:
22621 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22622 break;
22623 case DW_TAG_rvalue_reference_type:
22624 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22625 break;
22626 case DW_TAG_const_type:
22627 this_type = read_tag_const_type (die, cu);
22628 break;
22629 case DW_TAG_volatile_type:
22630 this_type = read_tag_volatile_type (die, cu);
22631 break;
22632 case DW_TAG_restrict_type:
22633 this_type = read_tag_restrict_type (die, cu);
22634 break;
22635 case DW_TAG_string_type:
22636 this_type = read_tag_string_type (die, cu);
22637 break;
22638 case DW_TAG_typedef:
22639 this_type = read_typedef (die, cu);
22640 break;
22641 case DW_TAG_subrange_type:
22642 this_type = read_subrange_type (die, cu);
22643 break;
22644 case DW_TAG_base_type:
22645 this_type = read_base_type (die, cu);
22646 break;
22647 case DW_TAG_unspecified_type:
22648 this_type = read_unspecified_type (die, cu);
22649 break;
22650 case DW_TAG_namespace:
22651 this_type = read_namespace_type (die, cu);
22652 break;
22653 case DW_TAG_module:
22654 this_type = read_module_type (die, cu);
22655 break;
22656 case DW_TAG_atomic_type:
22657 this_type = read_tag_atomic_type (die, cu);
22658 break;
22659 default:
22660 complaint (_("unexpected tag in read_type_die: '%s'"),
22661 dwarf_tag_name (die->tag));
22662 break;
22663 }
22664
22665 return this_type;
22666 }
22667
22668 /* See if we can figure out if the class lives in a namespace. We do
22669 this by looking for a member function; its demangled name will
22670 contain namespace info, if there is any.
22671 Return the computed name or NULL.
22672 Space for the result is allocated on the objfile's obstack.
22673 This is the full-die version of guess_partial_die_structure_name.
22674 In this case we know DIE has no useful parent. */
22675
22676 static const char *
22677 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22678 {
22679 struct die_info *spec_die;
22680 struct dwarf2_cu *spec_cu;
22681 struct die_info *child;
22682 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22683
22684 spec_cu = cu;
22685 spec_die = die_specification (die, &spec_cu);
22686 if (spec_die != NULL)
22687 {
22688 die = spec_die;
22689 cu = spec_cu;
22690 }
22691
22692 for (child = die->child;
22693 child != NULL;
22694 child = child->sibling)
22695 {
22696 if (child->tag == DW_TAG_subprogram)
22697 {
22698 const char *linkage_name = dw2_linkage_name (child, cu);
22699
22700 if (linkage_name != NULL)
22701 {
22702 gdb::unique_xmalloc_ptr<char> actual_name
22703 (language_class_name_from_physname (cu->language_defn,
22704 linkage_name));
22705 const char *name = NULL;
22706
22707 if (actual_name != NULL)
22708 {
22709 const char *die_name = dwarf2_name (die, cu);
22710
22711 if (die_name != NULL
22712 && strcmp (die_name, actual_name.get ()) != 0)
22713 {
22714 /* Strip off the class name from the full name.
22715 We want the prefix. */
22716 int die_name_len = strlen (die_name);
22717 int actual_name_len = strlen (actual_name.get ());
22718 const char *ptr = actual_name.get ();
22719
22720 /* Test for '::' as a sanity check. */
22721 if (actual_name_len > die_name_len + 2
22722 && ptr[actual_name_len - die_name_len - 1] == ':')
22723 name = obstack_strndup (
22724 &objfile->per_bfd->storage_obstack,
22725 ptr, actual_name_len - die_name_len - 2);
22726 }
22727 }
22728 return name;
22729 }
22730 }
22731 }
22732
22733 return NULL;
22734 }
22735
22736 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22737 prefix part in such case. See
22738 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22739
22740 static const char *
22741 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22742 {
22743 struct attribute *attr;
22744 const char *base;
22745
22746 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22747 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22748 return NULL;
22749
22750 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22751 return NULL;
22752
22753 attr = dw2_linkage_name_attr (die, cu);
22754 if (attr == NULL || DW_STRING (attr) == NULL)
22755 return NULL;
22756
22757 /* dwarf2_name had to be already called. */
22758 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22759
22760 /* Strip the base name, keep any leading namespaces/classes. */
22761 base = strrchr (DW_STRING (attr), ':');
22762 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22763 return "";
22764
22765 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22766 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22767 DW_STRING (attr),
22768 &base[-1] - DW_STRING (attr));
22769 }
22770
22771 /* Return the name of the namespace/class that DIE is defined within,
22772 or "" if we can't tell. The caller should not xfree the result.
22773
22774 For example, if we're within the method foo() in the following
22775 code:
22776
22777 namespace N {
22778 class C {
22779 void foo () {
22780 }
22781 };
22782 }
22783
22784 then determine_prefix on foo's die will return "N::C". */
22785
22786 static const char *
22787 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22788 {
22789 struct dwarf2_per_objfile *dwarf2_per_objfile
22790 = cu->per_cu->dwarf2_per_objfile;
22791 struct die_info *parent, *spec_die;
22792 struct dwarf2_cu *spec_cu;
22793 struct type *parent_type;
22794 const char *retval;
22795
22796 if (cu->language != language_cplus
22797 && cu->language != language_fortran && cu->language != language_d
22798 && cu->language != language_rust)
22799 return "";
22800
22801 retval = anonymous_struct_prefix (die, cu);
22802 if (retval)
22803 return retval;
22804
22805 /* We have to be careful in the presence of DW_AT_specification.
22806 For example, with GCC 3.4, given the code
22807
22808 namespace N {
22809 void foo() {
22810 // Definition of N::foo.
22811 }
22812 }
22813
22814 then we'll have a tree of DIEs like this:
22815
22816 1: DW_TAG_compile_unit
22817 2: DW_TAG_namespace // N
22818 3: DW_TAG_subprogram // declaration of N::foo
22819 4: DW_TAG_subprogram // definition of N::foo
22820 DW_AT_specification // refers to die #3
22821
22822 Thus, when processing die #4, we have to pretend that we're in
22823 the context of its DW_AT_specification, namely the contex of die
22824 #3. */
22825 spec_cu = cu;
22826 spec_die = die_specification (die, &spec_cu);
22827 if (spec_die == NULL)
22828 parent = die->parent;
22829 else
22830 {
22831 parent = spec_die->parent;
22832 cu = spec_cu;
22833 }
22834
22835 if (parent == NULL)
22836 return "";
22837 else if (parent->building_fullname)
22838 {
22839 const char *name;
22840 const char *parent_name;
22841
22842 /* It has been seen on RealView 2.2 built binaries,
22843 DW_TAG_template_type_param types actually _defined_ as
22844 children of the parent class:
22845
22846 enum E {};
22847 template class <class Enum> Class{};
22848 Class<enum E> class_e;
22849
22850 1: DW_TAG_class_type (Class)
22851 2: DW_TAG_enumeration_type (E)
22852 3: DW_TAG_enumerator (enum1:0)
22853 3: DW_TAG_enumerator (enum2:1)
22854 ...
22855 2: DW_TAG_template_type_param
22856 DW_AT_type DW_FORM_ref_udata (E)
22857
22858 Besides being broken debug info, it can put GDB into an
22859 infinite loop. Consider:
22860
22861 When we're building the full name for Class<E>, we'll start
22862 at Class, and go look over its template type parameters,
22863 finding E. We'll then try to build the full name of E, and
22864 reach here. We're now trying to build the full name of E,
22865 and look over the parent DIE for containing scope. In the
22866 broken case, if we followed the parent DIE of E, we'd again
22867 find Class, and once again go look at its template type
22868 arguments, etc., etc. Simply don't consider such parent die
22869 as source-level parent of this die (it can't be, the language
22870 doesn't allow it), and break the loop here. */
22871 name = dwarf2_name (die, cu);
22872 parent_name = dwarf2_name (parent, cu);
22873 complaint (_("template param type '%s' defined within parent '%s'"),
22874 name ? name : "<unknown>",
22875 parent_name ? parent_name : "<unknown>");
22876 return "";
22877 }
22878 else
22879 switch (parent->tag)
22880 {
22881 case DW_TAG_namespace:
22882 parent_type = read_type_die (parent, cu);
22883 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22884 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22885 Work around this problem here. */
22886 if (cu->language == language_cplus
22887 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22888 return "";
22889 /* We give a name to even anonymous namespaces. */
22890 return TYPE_NAME (parent_type);
22891 case DW_TAG_class_type:
22892 case DW_TAG_interface_type:
22893 case DW_TAG_structure_type:
22894 case DW_TAG_union_type:
22895 case DW_TAG_module:
22896 parent_type = read_type_die (parent, cu);
22897 if (TYPE_NAME (parent_type) != NULL)
22898 return TYPE_NAME (parent_type);
22899 else
22900 /* An anonymous structure is only allowed non-static data
22901 members; no typedefs, no member functions, et cetera.
22902 So it does not need a prefix. */
22903 return "";
22904 case DW_TAG_compile_unit:
22905 case DW_TAG_partial_unit:
22906 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22907 if (cu->language == language_cplus
22908 && !dwarf2_per_objfile->types.empty ()
22909 && die->child != NULL
22910 && (die->tag == DW_TAG_class_type
22911 || die->tag == DW_TAG_structure_type
22912 || die->tag == DW_TAG_union_type))
22913 {
22914 const char *name = guess_full_die_structure_name (die, cu);
22915 if (name != NULL)
22916 return name;
22917 }
22918 return "";
22919 case DW_TAG_subprogram:
22920 /* Nested subroutines in Fortran get a prefix with the name
22921 of the parent's subroutine. */
22922 if (cu->language == language_fortran)
22923 {
22924 if ((die->tag == DW_TAG_subprogram)
22925 && (dwarf2_name (parent, cu) != NULL))
22926 return dwarf2_name (parent, cu);
22927 }
22928 return determine_prefix (parent, cu);
22929 case DW_TAG_enumeration_type:
22930 parent_type = read_type_die (parent, cu);
22931 if (TYPE_DECLARED_CLASS (parent_type))
22932 {
22933 if (TYPE_NAME (parent_type) != NULL)
22934 return TYPE_NAME (parent_type);
22935 return "";
22936 }
22937 /* Fall through. */
22938 default:
22939 return determine_prefix (parent, cu);
22940 }
22941 }
22942
22943 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22944 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22945 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22946 an obconcat, otherwise allocate storage for the result. The CU argument is
22947 used to determine the language and hence, the appropriate separator. */
22948
22949 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22950
22951 static char *
22952 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22953 int physname, struct dwarf2_cu *cu)
22954 {
22955 const char *lead = "";
22956 const char *sep;
22957
22958 if (suffix == NULL || suffix[0] == '\0'
22959 || prefix == NULL || prefix[0] == '\0')
22960 sep = "";
22961 else if (cu->language == language_d)
22962 {
22963 /* For D, the 'main' function could be defined in any module, but it
22964 should never be prefixed. */
22965 if (strcmp (suffix, "D main") == 0)
22966 {
22967 prefix = "";
22968 sep = "";
22969 }
22970 else
22971 sep = ".";
22972 }
22973 else if (cu->language == language_fortran && physname)
22974 {
22975 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22976 DW_AT_MIPS_linkage_name is preferred and used instead. */
22977
22978 lead = "__";
22979 sep = "_MOD_";
22980 }
22981 else
22982 sep = "::";
22983
22984 if (prefix == NULL)
22985 prefix = "";
22986 if (suffix == NULL)
22987 suffix = "";
22988
22989 if (obs == NULL)
22990 {
22991 char *retval
22992 = ((char *)
22993 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22994
22995 strcpy (retval, lead);
22996 strcat (retval, prefix);
22997 strcat (retval, sep);
22998 strcat (retval, suffix);
22999 return retval;
23000 }
23001 else
23002 {
23003 /* We have an obstack. */
23004 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
23005 }
23006 }
23007
23008 /* Return sibling of die, NULL if no sibling. */
23009
23010 static struct die_info *
23011 sibling_die (struct die_info *die)
23012 {
23013 return die->sibling;
23014 }
23015
23016 /* Get name of a die, return NULL if not found. */
23017
23018 static const char *
23019 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23020 struct obstack *obstack)
23021 {
23022 if (name && cu->language == language_cplus)
23023 {
23024 std::string canon_name = cp_canonicalize_string (name);
23025
23026 if (!canon_name.empty ())
23027 {
23028 if (canon_name != name)
23029 name = obstack_strdup (obstack, canon_name);
23030 }
23031 }
23032
23033 return name;
23034 }
23035
23036 /* Get name of a die, return NULL if not found.
23037 Anonymous namespaces are converted to their magic string. */
23038
23039 static const char *
23040 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23041 {
23042 struct attribute *attr;
23043 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23044
23045 attr = dwarf2_attr (die, DW_AT_name, cu);
23046 if ((!attr || !DW_STRING (attr))
23047 && die->tag != DW_TAG_namespace
23048 && die->tag != DW_TAG_class_type
23049 && die->tag != DW_TAG_interface_type
23050 && die->tag != DW_TAG_structure_type
23051 && die->tag != DW_TAG_union_type)
23052 return NULL;
23053
23054 switch (die->tag)
23055 {
23056 case DW_TAG_compile_unit:
23057 case DW_TAG_partial_unit:
23058 /* Compilation units have a DW_AT_name that is a filename, not
23059 a source language identifier. */
23060 case DW_TAG_enumeration_type:
23061 case DW_TAG_enumerator:
23062 /* These tags always have simple identifiers already; no need
23063 to canonicalize them. */
23064 return DW_STRING (attr);
23065
23066 case DW_TAG_namespace:
23067 if (attr != NULL && DW_STRING (attr) != NULL)
23068 return DW_STRING (attr);
23069 return CP_ANONYMOUS_NAMESPACE_STR;
23070
23071 case DW_TAG_class_type:
23072 case DW_TAG_interface_type:
23073 case DW_TAG_structure_type:
23074 case DW_TAG_union_type:
23075 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23076 structures or unions. These were of the form "._%d" in GCC 4.1,
23077 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23078 and GCC 4.4. We work around this problem by ignoring these. */
23079 if (attr && DW_STRING (attr)
23080 && (startswith (DW_STRING (attr), "._")
23081 || startswith (DW_STRING (attr), "<anonymous")))
23082 return NULL;
23083
23084 /* GCC might emit a nameless typedef that has a linkage name. See
23085 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23086 if (!attr || DW_STRING (attr) == NULL)
23087 {
23088 attr = dw2_linkage_name_attr (die, cu);
23089 if (attr == NULL || DW_STRING (attr) == NULL)
23090 return NULL;
23091
23092 /* Avoid demangling DW_STRING (attr) the second time on a second
23093 call for the same DIE. */
23094 if (!DW_STRING_IS_CANONICAL (attr))
23095 {
23096 gdb::unique_xmalloc_ptr<char> demangled
23097 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
23098
23099 const char *base;
23100
23101 /* FIXME: we already did this for the partial symbol... */
23102 DW_STRING (attr)
23103 = obstack_strdup (&objfile->per_bfd->storage_obstack,
23104 demangled.get ());
23105 DW_STRING_IS_CANONICAL (attr) = 1;
23106
23107 /* Strip any leading namespaces/classes, keep only the base name.
23108 DW_AT_name for named DIEs does not contain the prefixes. */
23109 base = strrchr (DW_STRING (attr), ':');
23110 if (base && base > DW_STRING (attr) && base[-1] == ':')
23111 return &base[1];
23112 else
23113 return DW_STRING (attr);
23114 }
23115 }
23116 break;
23117
23118 default:
23119 break;
23120 }
23121
23122 if (!DW_STRING_IS_CANONICAL (attr))
23123 {
23124 DW_STRING (attr)
23125 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
23126 &objfile->per_bfd->storage_obstack);
23127 DW_STRING_IS_CANONICAL (attr) = 1;
23128 }
23129 return DW_STRING (attr);
23130 }
23131
23132 /* Return the die that this die in an extension of, or NULL if there
23133 is none. *EXT_CU is the CU containing DIE on input, and the CU
23134 containing the return value on output. */
23135
23136 static struct die_info *
23137 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23138 {
23139 struct attribute *attr;
23140
23141 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23142 if (attr == NULL)
23143 return NULL;
23144
23145 return follow_die_ref (die, attr, ext_cu);
23146 }
23147
23148 /* A convenience function that returns an "unknown" DWARF name,
23149 including the value of V. STR is the name of the entity being
23150 printed, e.g., "TAG". */
23151
23152 static const char *
23153 dwarf_unknown (const char *str, unsigned v)
23154 {
23155 char *cell = get_print_cell ();
23156 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
23157 return cell;
23158 }
23159
23160 /* Convert a DIE tag into its string name. */
23161
23162 static const char *
23163 dwarf_tag_name (unsigned tag)
23164 {
23165 const char *name = get_DW_TAG_name (tag);
23166
23167 if (name == NULL)
23168 return dwarf_unknown ("TAG", tag);
23169
23170 return name;
23171 }
23172
23173 /* Convert a DWARF attribute code into its string name. */
23174
23175 static const char *
23176 dwarf_attr_name (unsigned attr)
23177 {
23178 const char *name;
23179
23180 #ifdef MIPS /* collides with DW_AT_HP_block_index */
23181 if (attr == DW_AT_MIPS_fde)
23182 return "DW_AT_MIPS_fde";
23183 #else
23184 if (attr == DW_AT_HP_block_index)
23185 return "DW_AT_HP_block_index";
23186 #endif
23187
23188 name = get_DW_AT_name (attr);
23189
23190 if (name == NULL)
23191 return dwarf_unknown ("AT", attr);
23192
23193 return name;
23194 }
23195
23196 /* Convert a unit type to corresponding DW_UT name. */
23197
23198 static const char *
23199 dwarf_unit_type_name (int unit_type) {
23200 switch (unit_type)
23201 {
23202 case 0x01:
23203 return "DW_UT_compile (0x01)";
23204 case 0x02:
23205 return "DW_UT_type (0x02)";
23206 case 0x03:
23207 return "DW_UT_partial (0x03)";
23208 case 0x04:
23209 return "DW_UT_skeleton (0x04)";
23210 case 0x05:
23211 return "DW_UT_split_compile (0x05)";
23212 case 0x06:
23213 return "DW_UT_split_type (0x06)";
23214 case 0x80:
23215 return "DW_UT_lo_user (0x80)";
23216 case 0xff:
23217 return "DW_UT_hi_user (0xff)";
23218 default:
23219 return nullptr;
23220 }
23221 }
23222
23223 /* Convert a DWARF value form code into its string name. */
23224
23225 static const char *
23226 dwarf_form_name (unsigned form)
23227 {
23228 const char *name = get_DW_FORM_name (form);
23229
23230 if (name == NULL)
23231 return dwarf_unknown ("FORM", form);
23232
23233 return name;
23234 }
23235
23236 static const char *
23237 dwarf_bool_name (unsigned mybool)
23238 {
23239 if (mybool)
23240 return "TRUE";
23241 else
23242 return "FALSE";
23243 }
23244
23245 /* Convert a DWARF type code into its string name. */
23246
23247 static const char *
23248 dwarf_type_encoding_name (unsigned enc)
23249 {
23250 const char *name = get_DW_ATE_name (enc);
23251
23252 if (name == NULL)
23253 return dwarf_unknown ("ATE", enc);
23254
23255 return name;
23256 }
23257
23258 static void
23259 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23260 {
23261 unsigned int i;
23262
23263 print_spaces (indent, f);
23264 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23265 dwarf_tag_name (die->tag), die->abbrev,
23266 sect_offset_str (die->sect_off));
23267
23268 if (die->parent != NULL)
23269 {
23270 print_spaces (indent, f);
23271 fprintf_unfiltered (f, " parent at offset: %s\n",
23272 sect_offset_str (die->parent->sect_off));
23273 }
23274
23275 print_spaces (indent, f);
23276 fprintf_unfiltered (f, " has children: %s\n",
23277 dwarf_bool_name (die->child != NULL));
23278
23279 print_spaces (indent, f);
23280 fprintf_unfiltered (f, " attributes:\n");
23281
23282 for (i = 0; i < die->num_attrs; ++i)
23283 {
23284 print_spaces (indent, f);
23285 fprintf_unfiltered (f, " %s (%s) ",
23286 dwarf_attr_name (die->attrs[i].name),
23287 dwarf_form_name (die->attrs[i].form));
23288
23289 switch (die->attrs[i].form)
23290 {
23291 case DW_FORM_addr:
23292 case DW_FORM_addrx:
23293 case DW_FORM_GNU_addr_index:
23294 fprintf_unfiltered (f, "address: ");
23295 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
23296 break;
23297 case DW_FORM_block2:
23298 case DW_FORM_block4:
23299 case DW_FORM_block:
23300 case DW_FORM_block1:
23301 fprintf_unfiltered (f, "block: size %s",
23302 pulongest (DW_BLOCK (&die->attrs[i])->size));
23303 break;
23304 case DW_FORM_exprloc:
23305 fprintf_unfiltered (f, "expression: size %s",
23306 pulongest (DW_BLOCK (&die->attrs[i])->size));
23307 break;
23308 case DW_FORM_data16:
23309 fprintf_unfiltered (f, "constant of 16 bytes");
23310 break;
23311 case DW_FORM_ref_addr:
23312 fprintf_unfiltered (f, "ref address: ");
23313 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23314 break;
23315 case DW_FORM_GNU_ref_alt:
23316 fprintf_unfiltered (f, "alt ref address: ");
23317 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23318 break;
23319 case DW_FORM_ref1:
23320 case DW_FORM_ref2:
23321 case DW_FORM_ref4:
23322 case DW_FORM_ref8:
23323 case DW_FORM_ref_udata:
23324 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23325 (long) (DW_UNSND (&die->attrs[i])));
23326 break;
23327 case DW_FORM_data1:
23328 case DW_FORM_data2:
23329 case DW_FORM_data4:
23330 case DW_FORM_data8:
23331 case DW_FORM_udata:
23332 case DW_FORM_sdata:
23333 fprintf_unfiltered (f, "constant: %s",
23334 pulongest (DW_UNSND (&die->attrs[i])));
23335 break;
23336 case DW_FORM_sec_offset:
23337 fprintf_unfiltered (f, "section offset: %s",
23338 pulongest (DW_UNSND (&die->attrs[i])));
23339 break;
23340 case DW_FORM_ref_sig8:
23341 fprintf_unfiltered (f, "signature: %s",
23342 hex_string (DW_SIGNATURE (&die->attrs[i])));
23343 break;
23344 case DW_FORM_string:
23345 case DW_FORM_strp:
23346 case DW_FORM_line_strp:
23347 case DW_FORM_strx:
23348 case DW_FORM_GNU_str_index:
23349 case DW_FORM_GNU_strp_alt:
23350 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23351 DW_STRING (&die->attrs[i])
23352 ? DW_STRING (&die->attrs[i]) : "",
23353 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
23354 break;
23355 case DW_FORM_flag:
23356 if (DW_UNSND (&die->attrs[i]))
23357 fprintf_unfiltered (f, "flag: TRUE");
23358 else
23359 fprintf_unfiltered (f, "flag: FALSE");
23360 break;
23361 case DW_FORM_flag_present:
23362 fprintf_unfiltered (f, "flag: TRUE");
23363 break;
23364 case DW_FORM_indirect:
23365 /* The reader will have reduced the indirect form to
23366 the "base form" so this form should not occur. */
23367 fprintf_unfiltered (f,
23368 "unexpected attribute form: DW_FORM_indirect");
23369 break;
23370 case DW_FORM_implicit_const:
23371 fprintf_unfiltered (f, "constant: %s",
23372 plongest (DW_SND (&die->attrs[i])));
23373 break;
23374 default:
23375 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23376 die->attrs[i].form);
23377 break;
23378 }
23379 fprintf_unfiltered (f, "\n");
23380 }
23381 }
23382
23383 static void
23384 dump_die_for_error (struct die_info *die)
23385 {
23386 dump_die_shallow (gdb_stderr, 0, die);
23387 }
23388
23389 static void
23390 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23391 {
23392 int indent = level * 4;
23393
23394 gdb_assert (die != NULL);
23395
23396 if (level >= max_level)
23397 return;
23398
23399 dump_die_shallow (f, indent, die);
23400
23401 if (die->child != NULL)
23402 {
23403 print_spaces (indent, f);
23404 fprintf_unfiltered (f, " Children:");
23405 if (level + 1 < max_level)
23406 {
23407 fprintf_unfiltered (f, "\n");
23408 dump_die_1 (f, level + 1, max_level, die->child);
23409 }
23410 else
23411 {
23412 fprintf_unfiltered (f,
23413 " [not printed, max nesting level reached]\n");
23414 }
23415 }
23416
23417 if (die->sibling != NULL && level > 0)
23418 {
23419 dump_die_1 (f, level, max_level, die->sibling);
23420 }
23421 }
23422
23423 /* This is called from the pdie macro in gdbinit.in.
23424 It's not static so gcc will keep a copy callable from gdb. */
23425
23426 void
23427 dump_die (struct die_info *die, int max_level)
23428 {
23429 dump_die_1 (gdb_stdlog, 0, max_level, die);
23430 }
23431
23432 static void
23433 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23434 {
23435 void **slot;
23436
23437 slot = htab_find_slot_with_hash (cu->die_hash, die,
23438 to_underlying (die->sect_off),
23439 INSERT);
23440
23441 *slot = die;
23442 }
23443
23444 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23445 required kind. */
23446
23447 static sect_offset
23448 dwarf2_get_ref_die_offset (const struct attribute *attr)
23449 {
23450 if (attr_form_is_ref (attr))
23451 return (sect_offset) DW_UNSND (attr);
23452
23453 complaint (_("unsupported die ref attribute form: '%s'"),
23454 dwarf_form_name (attr->form));
23455 return {};
23456 }
23457
23458 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23459 * the value held by the attribute is not constant. */
23460
23461 static LONGEST
23462 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23463 {
23464 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23465 return DW_SND (attr);
23466 else if (attr->form == DW_FORM_udata
23467 || attr->form == DW_FORM_data1
23468 || attr->form == DW_FORM_data2
23469 || attr->form == DW_FORM_data4
23470 || attr->form == DW_FORM_data8)
23471 return DW_UNSND (attr);
23472 else
23473 {
23474 /* For DW_FORM_data16 see attr_form_is_constant. */
23475 complaint (_("Attribute value is not a constant (%s)"),
23476 dwarf_form_name (attr->form));
23477 return default_value;
23478 }
23479 }
23480
23481 /* Follow reference or signature attribute ATTR of SRC_DIE.
23482 On entry *REF_CU is the CU of SRC_DIE.
23483 On exit *REF_CU is the CU of the result. */
23484
23485 static struct die_info *
23486 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23487 struct dwarf2_cu **ref_cu)
23488 {
23489 struct die_info *die;
23490
23491 if (attr_form_is_ref (attr))
23492 die = follow_die_ref (src_die, attr, ref_cu);
23493 else if (attr->form == DW_FORM_ref_sig8)
23494 die = follow_die_sig (src_die, attr, ref_cu);
23495 else
23496 {
23497 dump_die_for_error (src_die);
23498 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23499 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23500 }
23501
23502 return die;
23503 }
23504
23505 /* Follow reference OFFSET.
23506 On entry *REF_CU is the CU of the source die referencing OFFSET.
23507 On exit *REF_CU is the CU of the result.
23508 Returns NULL if OFFSET is invalid. */
23509
23510 static struct die_info *
23511 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23512 struct dwarf2_cu **ref_cu)
23513 {
23514 struct die_info temp_die;
23515 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23516 struct dwarf2_per_objfile *dwarf2_per_objfile
23517 = cu->per_cu->dwarf2_per_objfile;
23518
23519 gdb_assert (cu->per_cu != NULL);
23520
23521 target_cu = cu;
23522
23523 if (cu->per_cu->is_debug_types)
23524 {
23525 /* .debug_types CUs cannot reference anything outside their CU.
23526 If they need to, they have to reference a signatured type via
23527 DW_FORM_ref_sig8. */
23528 if (!offset_in_cu_p (&cu->header, sect_off))
23529 return NULL;
23530 }
23531 else if (offset_in_dwz != cu->per_cu->is_dwz
23532 || !offset_in_cu_p (&cu->header, sect_off))
23533 {
23534 struct dwarf2_per_cu_data *per_cu;
23535
23536 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23537 dwarf2_per_objfile);
23538
23539 /* If necessary, add it to the queue and load its DIEs. */
23540 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23541 load_full_comp_unit (per_cu, false, cu->language);
23542
23543 target_cu = per_cu->cu;
23544 }
23545 else if (cu->dies == NULL)
23546 {
23547 /* We're loading full DIEs during partial symbol reading. */
23548 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23549 load_full_comp_unit (cu->per_cu, false, language_minimal);
23550 }
23551
23552 *ref_cu = target_cu;
23553 temp_die.sect_off = sect_off;
23554
23555 if (target_cu != cu)
23556 target_cu->ancestor = cu;
23557
23558 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23559 &temp_die,
23560 to_underlying (sect_off));
23561 }
23562
23563 /* Follow reference attribute ATTR of SRC_DIE.
23564 On entry *REF_CU is the CU of SRC_DIE.
23565 On exit *REF_CU is the CU of the result. */
23566
23567 static struct die_info *
23568 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23569 struct dwarf2_cu **ref_cu)
23570 {
23571 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23572 struct dwarf2_cu *cu = *ref_cu;
23573 struct die_info *die;
23574
23575 die = follow_die_offset (sect_off,
23576 (attr->form == DW_FORM_GNU_ref_alt
23577 || cu->per_cu->is_dwz),
23578 ref_cu);
23579 if (!die)
23580 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23581 "at %s [in module %s]"),
23582 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23583 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23584
23585 return die;
23586 }
23587
23588 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23589 Returned value is intended for DW_OP_call*. Returned
23590 dwarf2_locexpr_baton->data has lifetime of
23591 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23592
23593 struct dwarf2_locexpr_baton
23594 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23595 struct dwarf2_per_cu_data *per_cu,
23596 CORE_ADDR (*get_frame_pc) (void *baton),
23597 void *baton, bool resolve_abstract_p)
23598 {
23599 struct dwarf2_cu *cu;
23600 struct die_info *die;
23601 struct attribute *attr;
23602 struct dwarf2_locexpr_baton retval;
23603 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23604 struct objfile *objfile = dwarf2_per_objfile->objfile;
23605
23606 if (per_cu->cu == NULL)
23607 load_cu (per_cu, false);
23608 cu = per_cu->cu;
23609 if (cu == NULL)
23610 {
23611 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23612 Instead just throw an error, not much else we can do. */
23613 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23614 sect_offset_str (sect_off), objfile_name (objfile));
23615 }
23616
23617 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23618 if (!die)
23619 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23620 sect_offset_str (sect_off), objfile_name (objfile));
23621
23622 attr = dwarf2_attr (die, DW_AT_location, cu);
23623 if (!attr && resolve_abstract_p
23624 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
23625 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23626 {
23627 CORE_ADDR pc = (*get_frame_pc) (baton);
23628 CORE_ADDR baseaddr
23629 = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23630 struct gdbarch *gdbarch = get_objfile_arch (objfile);
23631
23632 for (const auto &cand_off
23633 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
23634 {
23635 struct dwarf2_cu *cand_cu = cu;
23636 struct die_info *cand
23637 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23638 if (!cand
23639 || !cand->parent
23640 || cand->parent->tag != DW_TAG_subprogram)
23641 continue;
23642
23643 CORE_ADDR pc_low, pc_high;
23644 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23645 if (pc_low == ((CORE_ADDR) -1))
23646 continue;
23647 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23648 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23649 if (!(pc_low <= pc && pc < pc_high))
23650 continue;
23651
23652 die = cand;
23653 attr = dwarf2_attr (die, DW_AT_location, cu);
23654 break;
23655 }
23656 }
23657
23658 if (!attr)
23659 {
23660 /* DWARF: "If there is no such attribute, then there is no effect.".
23661 DATA is ignored if SIZE is 0. */
23662
23663 retval.data = NULL;
23664 retval.size = 0;
23665 }
23666 else if (attr_form_is_section_offset (attr))
23667 {
23668 struct dwarf2_loclist_baton loclist_baton;
23669 CORE_ADDR pc = (*get_frame_pc) (baton);
23670 size_t size;
23671
23672 fill_in_loclist_baton (cu, &loclist_baton, attr);
23673
23674 retval.data = dwarf2_find_location_expression (&loclist_baton,
23675 &size, pc);
23676 retval.size = size;
23677 }
23678 else
23679 {
23680 if (!attr_form_is_block (attr))
23681 error (_("Dwarf Error: DIE at %s referenced in module %s "
23682 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23683 sect_offset_str (sect_off), objfile_name (objfile));
23684
23685 retval.data = DW_BLOCK (attr)->data;
23686 retval.size = DW_BLOCK (attr)->size;
23687 }
23688 retval.per_cu = cu->per_cu;
23689
23690 age_cached_comp_units (dwarf2_per_objfile);
23691
23692 return retval;
23693 }
23694
23695 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23696 offset. */
23697
23698 struct dwarf2_locexpr_baton
23699 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23700 struct dwarf2_per_cu_data *per_cu,
23701 CORE_ADDR (*get_frame_pc) (void *baton),
23702 void *baton)
23703 {
23704 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23705
23706 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23707 }
23708
23709 /* Write a constant of a given type as target-ordered bytes into
23710 OBSTACK. */
23711
23712 static const gdb_byte *
23713 write_constant_as_bytes (struct obstack *obstack,
23714 enum bfd_endian byte_order,
23715 struct type *type,
23716 ULONGEST value,
23717 LONGEST *len)
23718 {
23719 gdb_byte *result;
23720
23721 *len = TYPE_LENGTH (type);
23722 result = (gdb_byte *) obstack_alloc (obstack, *len);
23723 store_unsigned_integer (result, *len, byte_order, value);
23724
23725 return result;
23726 }
23727
23728 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23729 pointer to the constant bytes and set LEN to the length of the
23730 data. If memory is needed, allocate it on OBSTACK. If the DIE
23731 does not have a DW_AT_const_value, return NULL. */
23732
23733 const gdb_byte *
23734 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23735 struct dwarf2_per_cu_data *per_cu,
23736 struct obstack *obstack,
23737 LONGEST *len)
23738 {
23739 struct dwarf2_cu *cu;
23740 struct die_info *die;
23741 struct attribute *attr;
23742 const gdb_byte *result = NULL;
23743 struct type *type;
23744 LONGEST value;
23745 enum bfd_endian byte_order;
23746 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23747
23748 if (per_cu->cu == NULL)
23749 load_cu (per_cu, false);
23750 cu = per_cu->cu;
23751 if (cu == NULL)
23752 {
23753 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23754 Instead just throw an error, not much else we can do. */
23755 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23756 sect_offset_str (sect_off), objfile_name (objfile));
23757 }
23758
23759 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23760 if (!die)
23761 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23762 sect_offset_str (sect_off), objfile_name (objfile));
23763
23764 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23765 if (attr == NULL)
23766 return NULL;
23767
23768 byte_order = (bfd_big_endian (objfile->obfd)
23769 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23770
23771 switch (attr->form)
23772 {
23773 case DW_FORM_addr:
23774 case DW_FORM_addrx:
23775 case DW_FORM_GNU_addr_index:
23776 {
23777 gdb_byte *tem;
23778
23779 *len = cu->header.addr_size;
23780 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23781 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23782 result = tem;
23783 }
23784 break;
23785 case DW_FORM_string:
23786 case DW_FORM_strp:
23787 case DW_FORM_strx:
23788 case DW_FORM_GNU_str_index:
23789 case DW_FORM_GNU_strp_alt:
23790 /* DW_STRING is already allocated on the objfile obstack, point
23791 directly to it. */
23792 result = (const gdb_byte *) DW_STRING (attr);
23793 *len = strlen (DW_STRING (attr));
23794 break;
23795 case DW_FORM_block1:
23796 case DW_FORM_block2:
23797 case DW_FORM_block4:
23798 case DW_FORM_block:
23799 case DW_FORM_exprloc:
23800 case DW_FORM_data16:
23801 result = DW_BLOCK (attr)->data;
23802 *len = DW_BLOCK (attr)->size;
23803 break;
23804
23805 /* The DW_AT_const_value attributes are supposed to carry the
23806 symbol's value "represented as it would be on the target
23807 architecture." By the time we get here, it's already been
23808 converted to host endianness, so we just need to sign- or
23809 zero-extend it as appropriate. */
23810 case DW_FORM_data1:
23811 type = die_type (die, cu);
23812 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23813 if (result == NULL)
23814 result = write_constant_as_bytes (obstack, byte_order,
23815 type, value, len);
23816 break;
23817 case DW_FORM_data2:
23818 type = die_type (die, cu);
23819 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23820 if (result == NULL)
23821 result = write_constant_as_bytes (obstack, byte_order,
23822 type, value, len);
23823 break;
23824 case DW_FORM_data4:
23825 type = die_type (die, cu);
23826 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23827 if (result == NULL)
23828 result = write_constant_as_bytes (obstack, byte_order,
23829 type, value, len);
23830 break;
23831 case DW_FORM_data8:
23832 type = die_type (die, cu);
23833 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23834 if (result == NULL)
23835 result = write_constant_as_bytes (obstack, byte_order,
23836 type, value, len);
23837 break;
23838
23839 case DW_FORM_sdata:
23840 case DW_FORM_implicit_const:
23841 type = die_type (die, cu);
23842 result = write_constant_as_bytes (obstack, byte_order,
23843 type, DW_SND (attr), len);
23844 break;
23845
23846 case DW_FORM_udata:
23847 type = die_type (die, cu);
23848 result = write_constant_as_bytes (obstack, byte_order,
23849 type, DW_UNSND (attr), len);
23850 break;
23851
23852 default:
23853 complaint (_("unsupported const value attribute form: '%s'"),
23854 dwarf_form_name (attr->form));
23855 break;
23856 }
23857
23858 return result;
23859 }
23860
23861 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23862 valid type for this die is found. */
23863
23864 struct type *
23865 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23866 struct dwarf2_per_cu_data *per_cu)
23867 {
23868 struct dwarf2_cu *cu;
23869 struct die_info *die;
23870
23871 if (per_cu->cu == NULL)
23872 load_cu (per_cu, false);
23873 cu = per_cu->cu;
23874 if (!cu)
23875 return NULL;
23876
23877 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23878 if (!die)
23879 return NULL;
23880
23881 return die_type (die, cu);
23882 }
23883
23884 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23885 PER_CU. */
23886
23887 struct type *
23888 dwarf2_get_die_type (cu_offset die_offset,
23889 struct dwarf2_per_cu_data *per_cu)
23890 {
23891 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23892 return get_die_type_at_offset (die_offset_sect, per_cu);
23893 }
23894
23895 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23896 On entry *REF_CU is the CU of SRC_DIE.
23897 On exit *REF_CU is the CU of the result.
23898 Returns NULL if the referenced DIE isn't found. */
23899
23900 static struct die_info *
23901 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23902 struct dwarf2_cu **ref_cu)
23903 {
23904 struct die_info temp_die;
23905 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23906 struct die_info *die;
23907
23908 /* While it might be nice to assert sig_type->type == NULL here,
23909 we can get here for DW_AT_imported_declaration where we need
23910 the DIE not the type. */
23911
23912 /* If necessary, add it to the queue and load its DIEs. */
23913
23914 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23915 read_signatured_type (sig_type);
23916
23917 sig_cu = sig_type->per_cu.cu;
23918 gdb_assert (sig_cu != NULL);
23919 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23920 temp_die.sect_off = sig_type->type_offset_in_section;
23921 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23922 to_underlying (temp_die.sect_off));
23923 if (die)
23924 {
23925 struct dwarf2_per_objfile *dwarf2_per_objfile
23926 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23927
23928 /* For .gdb_index version 7 keep track of included TUs.
23929 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23930 if (dwarf2_per_objfile->index_table != NULL
23931 && dwarf2_per_objfile->index_table->version <= 7)
23932 {
23933 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23934 }
23935
23936 *ref_cu = sig_cu;
23937 if (sig_cu != cu)
23938 sig_cu->ancestor = cu;
23939
23940 return die;
23941 }
23942
23943 return NULL;
23944 }
23945
23946 /* Follow signatured type referenced by ATTR in SRC_DIE.
23947 On entry *REF_CU is the CU of SRC_DIE.
23948 On exit *REF_CU is the CU of the result.
23949 The result is the DIE of the type.
23950 If the referenced type cannot be found an error is thrown. */
23951
23952 static struct die_info *
23953 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23954 struct dwarf2_cu **ref_cu)
23955 {
23956 ULONGEST signature = DW_SIGNATURE (attr);
23957 struct signatured_type *sig_type;
23958 struct die_info *die;
23959
23960 gdb_assert (attr->form == DW_FORM_ref_sig8);
23961
23962 sig_type = lookup_signatured_type (*ref_cu, signature);
23963 /* sig_type will be NULL if the signatured type is missing from
23964 the debug info. */
23965 if (sig_type == NULL)
23966 {
23967 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23968 " from DIE at %s [in module %s]"),
23969 hex_string (signature), sect_offset_str (src_die->sect_off),
23970 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23971 }
23972
23973 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23974 if (die == NULL)
23975 {
23976 dump_die_for_error (src_die);
23977 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23978 " from DIE at %s [in module %s]"),
23979 hex_string (signature), sect_offset_str (src_die->sect_off),
23980 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23981 }
23982
23983 return die;
23984 }
23985
23986 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23987 reading in and processing the type unit if necessary. */
23988
23989 static struct type *
23990 get_signatured_type (struct die_info *die, ULONGEST signature,
23991 struct dwarf2_cu *cu)
23992 {
23993 struct dwarf2_per_objfile *dwarf2_per_objfile
23994 = cu->per_cu->dwarf2_per_objfile;
23995 struct signatured_type *sig_type;
23996 struct dwarf2_cu *type_cu;
23997 struct die_info *type_die;
23998 struct type *type;
23999
24000 sig_type = lookup_signatured_type (cu, signature);
24001 /* sig_type will be NULL if the signatured type is missing from
24002 the debug info. */
24003 if (sig_type == NULL)
24004 {
24005 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24006 " from DIE at %s [in module %s]"),
24007 hex_string (signature), sect_offset_str (die->sect_off),
24008 objfile_name (dwarf2_per_objfile->objfile));
24009 return build_error_marker_type (cu, die);
24010 }
24011
24012 /* If we already know the type we're done. */
24013 if (sig_type->type != NULL)
24014 return sig_type->type;
24015
24016 type_cu = cu;
24017 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24018 if (type_die != NULL)
24019 {
24020 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24021 is created. This is important, for example, because for c++ classes
24022 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24023 type = read_type_die (type_die, type_cu);
24024 if (type == NULL)
24025 {
24026 complaint (_("Dwarf Error: Cannot build signatured type %s"
24027 " referenced from DIE at %s [in module %s]"),
24028 hex_string (signature), sect_offset_str (die->sect_off),
24029 objfile_name (dwarf2_per_objfile->objfile));
24030 type = build_error_marker_type (cu, die);
24031 }
24032 }
24033 else
24034 {
24035 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24036 " from DIE at %s [in module %s]"),
24037 hex_string (signature), sect_offset_str (die->sect_off),
24038 objfile_name (dwarf2_per_objfile->objfile));
24039 type = build_error_marker_type (cu, die);
24040 }
24041 sig_type->type = type;
24042
24043 return type;
24044 }
24045
24046 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24047 reading in and processing the type unit if necessary. */
24048
24049 static struct type *
24050 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24051 struct dwarf2_cu *cu) /* ARI: editCase function */
24052 {
24053 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24054 if (attr_form_is_ref (attr))
24055 {
24056 struct dwarf2_cu *type_cu = cu;
24057 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24058
24059 return read_type_die (type_die, type_cu);
24060 }
24061 else if (attr->form == DW_FORM_ref_sig8)
24062 {
24063 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
24064 }
24065 else
24066 {
24067 struct dwarf2_per_objfile *dwarf2_per_objfile
24068 = cu->per_cu->dwarf2_per_objfile;
24069
24070 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24071 " at %s [in module %s]"),
24072 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24073 objfile_name (dwarf2_per_objfile->objfile));
24074 return build_error_marker_type (cu, die);
24075 }
24076 }
24077
24078 /* Load the DIEs associated with type unit PER_CU into memory. */
24079
24080 static void
24081 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
24082 {
24083 struct signatured_type *sig_type;
24084
24085 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24086 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
24087
24088 /* We have the per_cu, but we need the signatured_type.
24089 Fortunately this is an easy translation. */
24090 gdb_assert (per_cu->is_debug_types);
24091 sig_type = (struct signatured_type *) per_cu;
24092
24093 gdb_assert (per_cu->cu == NULL);
24094
24095 read_signatured_type (sig_type);
24096
24097 gdb_assert (per_cu->cu != NULL);
24098 }
24099
24100 /* die_reader_func for read_signatured_type.
24101 This is identical to load_full_comp_unit_reader,
24102 but is kept separate for now. */
24103
24104 static void
24105 read_signatured_type_reader (const struct die_reader_specs *reader,
24106 const gdb_byte *info_ptr,
24107 struct die_info *comp_unit_die,
24108 int has_children,
24109 void *data)
24110 {
24111 struct dwarf2_cu *cu = reader->cu;
24112
24113 gdb_assert (cu->die_hash == NULL);
24114 cu->die_hash =
24115 htab_create_alloc_ex (cu->header.length / 12,
24116 die_hash,
24117 die_eq,
24118 NULL,
24119 &cu->comp_unit_obstack,
24120 hashtab_obstack_allocate,
24121 dummy_obstack_deallocate);
24122
24123 if (has_children)
24124 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
24125 &info_ptr, comp_unit_die);
24126 cu->dies = comp_unit_die;
24127 /* comp_unit_die is not stored in die_hash, no need. */
24128
24129 /* We try not to read any attributes in this function, because not
24130 all CUs needed for references have been loaded yet, and symbol
24131 table processing isn't initialized. But we have to set the CU language,
24132 or we won't be able to build types correctly.
24133 Similarly, if we do not read the producer, we can not apply
24134 producer-specific interpretation. */
24135 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24136 }
24137
24138 /* Read in a signatured type and build its CU and DIEs.
24139 If the type is a stub for the real type in a DWO file,
24140 read in the real type from the DWO file as well. */
24141
24142 static void
24143 read_signatured_type (struct signatured_type *sig_type)
24144 {
24145 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24146
24147 gdb_assert (per_cu->is_debug_types);
24148 gdb_assert (per_cu->cu == NULL);
24149
24150 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
24151 read_signatured_type_reader, NULL);
24152 sig_type->per_cu.tu_read = 1;
24153 }
24154
24155 /* Decode simple location descriptions.
24156 Given a pointer to a dwarf block that defines a location, compute
24157 the location and return the value.
24158
24159 NOTE drow/2003-11-18: This function is called in two situations
24160 now: for the address of static or global variables (partial symbols
24161 only) and for offsets into structures which are expected to be
24162 (more or less) constant. The partial symbol case should go away,
24163 and only the constant case should remain. That will let this
24164 function complain more accurately. A few special modes are allowed
24165 without complaint for global variables (for instance, global
24166 register values and thread-local values).
24167
24168 A location description containing no operations indicates that the
24169 object is optimized out. The return value is 0 for that case.
24170 FIXME drow/2003-11-16: No callers check for this case any more; soon all
24171 callers will only want a very basic result and this can become a
24172 complaint.
24173
24174 Note that stack[0] is unused except as a default error return. */
24175
24176 static CORE_ADDR
24177 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
24178 {
24179 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
24180 size_t i;
24181 size_t size = blk->size;
24182 const gdb_byte *data = blk->data;
24183 CORE_ADDR stack[64];
24184 int stacki;
24185 unsigned int bytes_read, unsnd;
24186 gdb_byte op;
24187
24188 i = 0;
24189 stacki = 0;
24190 stack[stacki] = 0;
24191 stack[++stacki] = 0;
24192
24193 while (i < size)
24194 {
24195 op = data[i++];
24196 switch (op)
24197 {
24198 case DW_OP_lit0:
24199 case DW_OP_lit1:
24200 case DW_OP_lit2:
24201 case DW_OP_lit3:
24202 case DW_OP_lit4:
24203 case DW_OP_lit5:
24204 case DW_OP_lit6:
24205 case DW_OP_lit7:
24206 case DW_OP_lit8:
24207 case DW_OP_lit9:
24208 case DW_OP_lit10:
24209 case DW_OP_lit11:
24210 case DW_OP_lit12:
24211 case DW_OP_lit13:
24212 case DW_OP_lit14:
24213 case DW_OP_lit15:
24214 case DW_OP_lit16:
24215 case DW_OP_lit17:
24216 case DW_OP_lit18:
24217 case DW_OP_lit19:
24218 case DW_OP_lit20:
24219 case DW_OP_lit21:
24220 case DW_OP_lit22:
24221 case DW_OP_lit23:
24222 case DW_OP_lit24:
24223 case DW_OP_lit25:
24224 case DW_OP_lit26:
24225 case DW_OP_lit27:
24226 case DW_OP_lit28:
24227 case DW_OP_lit29:
24228 case DW_OP_lit30:
24229 case DW_OP_lit31:
24230 stack[++stacki] = op - DW_OP_lit0;
24231 break;
24232
24233 case DW_OP_reg0:
24234 case DW_OP_reg1:
24235 case DW_OP_reg2:
24236 case DW_OP_reg3:
24237 case DW_OP_reg4:
24238 case DW_OP_reg5:
24239 case DW_OP_reg6:
24240 case DW_OP_reg7:
24241 case DW_OP_reg8:
24242 case DW_OP_reg9:
24243 case DW_OP_reg10:
24244 case DW_OP_reg11:
24245 case DW_OP_reg12:
24246 case DW_OP_reg13:
24247 case DW_OP_reg14:
24248 case DW_OP_reg15:
24249 case DW_OP_reg16:
24250 case DW_OP_reg17:
24251 case DW_OP_reg18:
24252 case DW_OP_reg19:
24253 case DW_OP_reg20:
24254 case DW_OP_reg21:
24255 case DW_OP_reg22:
24256 case DW_OP_reg23:
24257 case DW_OP_reg24:
24258 case DW_OP_reg25:
24259 case DW_OP_reg26:
24260 case DW_OP_reg27:
24261 case DW_OP_reg28:
24262 case DW_OP_reg29:
24263 case DW_OP_reg30:
24264 case DW_OP_reg31:
24265 stack[++stacki] = op - DW_OP_reg0;
24266 if (i < size)
24267 dwarf2_complex_location_expr_complaint ();
24268 break;
24269
24270 case DW_OP_regx:
24271 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24272 i += bytes_read;
24273 stack[++stacki] = unsnd;
24274 if (i < size)
24275 dwarf2_complex_location_expr_complaint ();
24276 break;
24277
24278 case DW_OP_addr:
24279 stack[++stacki] = read_address (objfile->obfd, &data[i],
24280 cu, &bytes_read);
24281 i += bytes_read;
24282 break;
24283
24284 case DW_OP_const1u:
24285 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24286 i += 1;
24287 break;
24288
24289 case DW_OP_const1s:
24290 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24291 i += 1;
24292 break;
24293
24294 case DW_OP_const2u:
24295 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24296 i += 2;
24297 break;
24298
24299 case DW_OP_const2s:
24300 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24301 i += 2;
24302 break;
24303
24304 case DW_OP_const4u:
24305 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24306 i += 4;
24307 break;
24308
24309 case DW_OP_const4s:
24310 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24311 i += 4;
24312 break;
24313
24314 case DW_OP_const8u:
24315 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24316 i += 8;
24317 break;
24318
24319 case DW_OP_constu:
24320 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24321 &bytes_read);
24322 i += bytes_read;
24323 break;
24324
24325 case DW_OP_consts:
24326 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24327 i += bytes_read;
24328 break;
24329
24330 case DW_OP_dup:
24331 stack[stacki + 1] = stack[stacki];
24332 stacki++;
24333 break;
24334
24335 case DW_OP_plus:
24336 stack[stacki - 1] += stack[stacki];
24337 stacki--;
24338 break;
24339
24340 case DW_OP_plus_uconst:
24341 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24342 &bytes_read);
24343 i += bytes_read;
24344 break;
24345
24346 case DW_OP_minus:
24347 stack[stacki - 1] -= stack[stacki];
24348 stacki--;
24349 break;
24350
24351 case DW_OP_deref:
24352 /* If we're not the last op, then we definitely can't encode
24353 this using GDB's address_class enum. This is valid for partial
24354 global symbols, although the variable's address will be bogus
24355 in the psymtab. */
24356 if (i < size)
24357 dwarf2_complex_location_expr_complaint ();
24358 break;
24359
24360 case DW_OP_GNU_push_tls_address:
24361 case DW_OP_form_tls_address:
24362 /* The top of the stack has the offset from the beginning
24363 of the thread control block at which the variable is located. */
24364 /* Nothing should follow this operator, so the top of stack would
24365 be returned. */
24366 /* This is valid for partial global symbols, but the variable's
24367 address will be bogus in the psymtab. Make it always at least
24368 non-zero to not look as a variable garbage collected by linker
24369 which have DW_OP_addr 0. */
24370 if (i < size)
24371 dwarf2_complex_location_expr_complaint ();
24372 stack[stacki]++;
24373 break;
24374
24375 case DW_OP_GNU_uninit:
24376 break;
24377
24378 case DW_OP_addrx:
24379 case DW_OP_GNU_addr_index:
24380 case DW_OP_GNU_const_index:
24381 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24382 &bytes_read);
24383 i += bytes_read;
24384 break;
24385
24386 default:
24387 {
24388 const char *name = get_DW_OP_name (op);
24389
24390 if (name)
24391 complaint (_("unsupported stack op: '%s'"),
24392 name);
24393 else
24394 complaint (_("unsupported stack op: '%02x'"),
24395 op);
24396 }
24397
24398 return (stack[stacki]);
24399 }
24400
24401 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24402 outside of the allocated space. Also enforce minimum>0. */
24403 if (stacki >= ARRAY_SIZE (stack) - 1)
24404 {
24405 complaint (_("location description stack overflow"));
24406 return 0;
24407 }
24408
24409 if (stacki <= 0)
24410 {
24411 complaint (_("location description stack underflow"));
24412 return 0;
24413 }
24414 }
24415 return (stack[stacki]);
24416 }
24417
24418 /* memory allocation interface */
24419
24420 static struct dwarf_block *
24421 dwarf_alloc_block (struct dwarf2_cu *cu)
24422 {
24423 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24424 }
24425
24426 static struct die_info *
24427 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24428 {
24429 struct die_info *die;
24430 size_t size = sizeof (struct die_info);
24431
24432 if (num_attrs > 1)
24433 size += (num_attrs - 1) * sizeof (struct attribute);
24434
24435 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24436 memset (die, 0, sizeof (struct die_info));
24437 return (die);
24438 }
24439
24440 \f
24441 /* Macro support. */
24442
24443 /* Return file name relative to the compilation directory of file number I in
24444 *LH's file name table. The result is allocated using xmalloc; the caller is
24445 responsible for freeing it. */
24446
24447 static char *
24448 file_file_name (int file, struct line_header *lh)
24449 {
24450 /* Is the file number a valid index into the line header's file name
24451 table? Remember that file numbers start with one, not zero. */
24452 if (lh->is_valid_file_index (file))
24453 {
24454 const file_entry *fe = lh->file_name_at (file);
24455
24456 if (!IS_ABSOLUTE_PATH (fe->name))
24457 {
24458 const char *dir = fe->include_dir (lh);
24459 if (dir != NULL)
24460 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
24461 }
24462 return xstrdup (fe->name);
24463 }
24464 else
24465 {
24466 /* The compiler produced a bogus file number. We can at least
24467 record the macro definitions made in the file, even if we
24468 won't be able to find the file by name. */
24469 char fake_name[80];
24470
24471 xsnprintf (fake_name, sizeof (fake_name),
24472 "<bad macro file number %d>", file);
24473
24474 complaint (_("bad file number in macro information (%d)"),
24475 file);
24476
24477 return xstrdup (fake_name);
24478 }
24479 }
24480
24481 /* Return the full name of file number I in *LH's file name table.
24482 Use COMP_DIR as the name of the current directory of the
24483 compilation. The result is allocated using xmalloc; the caller is
24484 responsible for freeing it. */
24485 static char *
24486 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24487 {
24488 /* Is the file number a valid index into the line header's file name
24489 table? Remember that file numbers start with one, not zero. */
24490 if (lh->is_valid_file_index (file))
24491 {
24492 char *relative = file_file_name (file, lh);
24493
24494 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24495 return relative;
24496 return reconcat (relative, comp_dir, SLASH_STRING,
24497 relative, (char *) NULL);
24498 }
24499 else
24500 return file_file_name (file, lh);
24501 }
24502
24503
24504 static struct macro_source_file *
24505 macro_start_file (struct dwarf2_cu *cu,
24506 int file, int line,
24507 struct macro_source_file *current_file,
24508 struct line_header *lh)
24509 {
24510 /* File name relative to the compilation directory of this source file. */
24511 char *file_name = file_file_name (file, lh);
24512
24513 if (! current_file)
24514 {
24515 /* Note: We don't create a macro table for this compilation unit
24516 at all until we actually get a filename. */
24517 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
24518
24519 /* If we have no current file, then this must be the start_file
24520 directive for the compilation unit's main source file. */
24521 current_file = macro_set_main (macro_table, file_name);
24522 macro_define_special (macro_table);
24523 }
24524 else
24525 current_file = macro_include (current_file, line, file_name);
24526
24527 xfree (file_name);
24528
24529 return current_file;
24530 }
24531
24532 static const char *
24533 consume_improper_spaces (const char *p, const char *body)
24534 {
24535 if (*p == ' ')
24536 {
24537 complaint (_("macro definition contains spaces "
24538 "in formal argument list:\n`%s'"),
24539 body);
24540
24541 while (*p == ' ')
24542 p++;
24543 }
24544
24545 return p;
24546 }
24547
24548
24549 static void
24550 parse_macro_definition (struct macro_source_file *file, int line,
24551 const char *body)
24552 {
24553 const char *p;
24554
24555 /* The body string takes one of two forms. For object-like macro
24556 definitions, it should be:
24557
24558 <macro name> " " <definition>
24559
24560 For function-like macro definitions, it should be:
24561
24562 <macro name> "() " <definition>
24563 or
24564 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24565
24566 Spaces may appear only where explicitly indicated, and in the
24567 <definition>.
24568
24569 The Dwarf 2 spec says that an object-like macro's name is always
24570 followed by a space, but versions of GCC around March 2002 omit
24571 the space when the macro's definition is the empty string.
24572
24573 The Dwarf 2 spec says that there should be no spaces between the
24574 formal arguments in a function-like macro's formal argument list,
24575 but versions of GCC around March 2002 include spaces after the
24576 commas. */
24577
24578
24579 /* Find the extent of the macro name. The macro name is terminated
24580 by either a space or null character (for an object-like macro) or
24581 an opening paren (for a function-like macro). */
24582 for (p = body; *p; p++)
24583 if (*p == ' ' || *p == '(')
24584 break;
24585
24586 if (*p == ' ' || *p == '\0')
24587 {
24588 /* It's an object-like macro. */
24589 int name_len = p - body;
24590 char *name = savestring (body, name_len);
24591 const char *replacement;
24592
24593 if (*p == ' ')
24594 replacement = body + name_len + 1;
24595 else
24596 {
24597 dwarf2_macro_malformed_definition_complaint (body);
24598 replacement = body + name_len;
24599 }
24600
24601 macro_define_object (file, line, name, replacement);
24602
24603 xfree (name);
24604 }
24605 else if (*p == '(')
24606 {
24607 /* It's a function-like macro. */
24608 char *name = savestring (body, p - body);
24609 int argc = 0;
24610 int argv_size = 1;
24611 char **argv = XNEWVEC (char *, argv_size);
24612
24613 p++;
24614
24615 p = consume_improper_spaces (p, body);
24616
24617 /* Parse the formal argument list. */
24618 while (*p && *p != ')')
24619 {
24620 /* Find the extent of the current argument name. */
24621 const char *arg_start = p;
24622
24623 while (*p && *p != ',' && *p != ')' && *p != ' ')
24624 p++;
24625
24626 if (! *p || p == arg_start)
24627 dwarf2_macro_malformed_definition_complaint (body);
24628 else
24629 {
24630 /* Make sure argv has room for the new argument. */
24631 if (argc >= argv_size)
24632 {
24633 argv_size *= 2;
24634 argv = XRESIZEVEC (char *, argv, argv_size);
24635 }
24636
24637 argv[argc++] = savestring (arg_start, p - arg_start);
24638 }
24639
24640 p = consume_improper_spaces (p, body);
24641
24642 /* Consume the comma, if present. */
24643 if (*p == ',')
24644 {
24645 p++;
24646
24647 p = consume_improper_spaces (p, body);
24648 }
24649 }
24650
24651 if (*p == ')')
24652 {
24653 p++;
24654
24655 if (*p == ' ')
24656 /* Perfectly formed definition, no complaints. */
24657 macro_define_function (file, line, name,
24658 argc, (const char **) argv,
24659 p + 1);
24660 else if (*p == '\0')
24661 {
24662 /* Complain, but do define it. */
24663 dwarf2_macro_malformed_definition_complaint (body);
24664 macro_define_function (file, line, name,
24665 argc, (const char **) argv,
24666 p);
24667 }
24668 else
24669 /* Just complain. */
24670 dwarf2_macro_malformed_definition_complaint (body);
24671 }
24672 else
24673 /* Just complain. */
24674 dwarf2_macro_malformed_definition_complaint (body);
24675
24676 xfree (name);
24677 {
24678 int i;
24679
24680 for (i = 0; i < argc; i++)
24681 xfree (argv[i]);
24682 }
24683 xfree (argv);
24684 }
24685 else
24686 dwarf2_macro_malformed_definition_complaint (body);
24687 }
24688
24689 /* Skip some bytes from BYTES according to the form given in FORM.
24690 Returns the new pointer. */
24691
24692 static const gdb_byte *
24693 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24694 enum dwarf_form form,
24695 unsigned int offset_size,
24696 struct dwarf2_section_info *section)
24697 {
24698 unsigned int bytes_read;
24699
24700 switch (form)
24701 {
24702 case DW_FORM_data1:
24703 case DW_FORM_flag:
24704 ++bytes;
24705 break;
24706
24707 case DW_FORM_data2:
24708 bytes += 2;
24709 break;
24710
24711 case DW_FORM_data4:
24712 bytes += 4;
24713 break;
24714
24715 case DW_FORM_data8:
24716 bytes += 8;
24717 break;
24718
24719 case DW_FORM_data16:
24720 bytes += 16;
24721 break;
24722
24723 case DW_FORM_string:
24724 read_direct_string (abfd, bytes, &bytes_read);
24725 bytes += bytes_read;
24726 break;
24727
24728 case DW_FORM_sec_offset:
24729 case DW_FORM_strp:
24730 case DW_FORM_GNU_strp_alt:
24731 bytes += offset_size;
24732 break;
24733
24734 case DW_FORM_block:
24735 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24736 bytes += bytes_read;
24737 break;
24738
24739 case DW_FORM_block1:
24740 bytes += 1 + read_1_byte (abfd, bytes);
24741 break;
24742 case DW_FORM_block2:
24743 bytes += 2 + read_2_bytes (abfd, bytes);
24744 break;
24745 case DW_FORM_block4:
24746 bytes += 4 + read_4_bytes (abfd, bytes);
24747 break;
24748
24749 case DW_FORM_addrx:
24750 case DW_FORM_sdata:
24751 case DW_FORM_strx:
24752 case DW_FORM_udata:
24753 case DW_FORM_GNU_addr_index:
24754 case DW_FORM_GNU_str_index:
24755 bytes = gdb_skip_leb128 (bytes, buffer_end);
24756 if (bytes == NULL)
24757 {
24758 dwarf2_section_buffer_overflow_complaint (section);
24759 return NULL;
24760 }
24761 break;
24762
24763 case DW_FORM_implicit_const:
24764 break;
24765
24766 default:
24767 {
24768 complaint (_("invalid form 0x%x in `%s'"),
24769 form, get_section_name (section));
24770 return NULL;
24771 }
24772 }
24773
24774 return bytes;
24775 }
24776
24777 /* A helper for dwarf_decode_macros that handles skipping an unknown
24778 opcode. Returns an updated pointer to the macro data buffer; or,
24779 on error, issues a complaint and returns NULL. */
24780
24781 static const gdb_byte *
24782 skip_unknown_opcode (unsigned int opcode,
24783 const gdb_byte **opcode_definitions,
24784 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24785 bfd *abfd,
24786 unsigned int offset_size,
24787 struct dwarf2_section_info *section)
24788 {
24789 unsigned int bytes_read, i;
24790 unsigned long arg;
24791 const gdb_byte *defn;
24792
24793 if (opcode_definitions[opcode] == NULL)
24794 {
24795 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24796 opcode);
24797 return NULL;
24798 }
24799
24800 defn = opcode_definitions[opcode];
24801 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24802 defn += bytes_read;
24803
24804 for (i = 0; i < arg; ++i)
24805 {
24806 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24807 (enum dwarf_form) defn[i], offset_size,
24808 section);
24809 if (mac_ptr == NULL)
24810 {
24811 /* skip_form_bytes already issued the complaint. */
24812 return NULL;
24813 }
24814 }
24815
24816 return mac_ptr;
24817 }
24818
24819 /* A helper function which parses the header of a macro section.
24820 If the macro section is the extended (for now called "GNU") type,
24821 then this updates *OFFSET_SIZE. Returns a pointer to just after
24822 the header, or issues a complaint and returns NULL on error. */
24823
24824 static const gdb_byte *
24825 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24826 bfd *abfd,
24827 const gdb_byte *mac_ptr,
24828 unsigned int *offset_size,
24829 int section_is_gnu)
24830 {
24831 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24832
24833 if (section_is_gnu)
24834 {
24835 unsigned int version, flags;
24836
24837 version = read_2_bytes (abfd, mac_ptr);
24838 if (version != 4 && version != 5)
24839 {
24840 complaint (_("unrecognized version `%d' in .debug_macro section"),
24841 version);
24842 return NULL;
24843 }
24844 mac_ptr += 2;
24845
24846 flags = read_1_byte (abfd, mac_ptr);
24847 ++mac_ptr;
24848 *offset_size = (flags & 1) ? 8 : 4;
24849
24850 if ((flags & 2) != 0)
24851 /* We don't need the line table offset. */
24852 mac_ptr += *offset_size;
24853
24854 /* Vendor opcode descriptions. */
24855 if ((flags & 4) != 0)
24856 {
24857 unsigned int i, count;
24858
24859 count = read_1_byte (abfd, mac_ptr);
24860 ++mac_ptr;
24861 for (i = 0; i < count; ++i)
24862 {
24863 unsigned int opcode, bytes_read;
24864 unsigned long arg;
24865
24866 opcode = read_1_byte (abfd, mac_ptr);
24867 ++mac_ptr;
24868 opcode_definitions[opcode] = mac_ptr;
24869 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24870 mac_ptr += bytes_read;
24871 mac_ptr += arg;
24872 }
24873 }
24874 }
24875
24876 return mac_ptr;
24877 }
24878
24879 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24880 including DW_MACRO_import. */
24881
24882 static void
24883 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24884 bfd *abfd,
24885 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24886 struct macro_source_file *current_file,
24887 struct line_header *lh,
24888 struct dwarf2_section_info *section,
24889 int section_is_gnu, int section_is_dwz,
24890 unsigned int offset_size,
24891 htab_t include_hash)
24892 {
24893 struct dwarf2_per_objfile *dwarf2_per_objfile
24894 = cu->per_cu->dwarf2_per_objfile;
24895 struct objfile *objfile = dwarf2_per_objfile->objfile;
24896 enum dwarf_macro_record_type macinfo_type;
24897 int at_commandline;
24898 const gdb_byte *opcode_definitions[256];
24899
24900 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24901 &offset_size, section_is_gnu);
24902 if (mac_ptr == NULL)
24903 {
24904 /* We already issued a complaint. */
24905 return;
24906 }
24907
24908 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24909 GDB is still reading the definitions from command line. First
24910 DW_MACINFO_start_file will need to be ignored as it was already executed
24911 to create CURRENT_FILE for the main source holding also the command line
24912 definitions. On first met DW_MACINFO_start_file this flag is reset to
24913 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24914
24915 at_commandline = 1;
24916
24917 do
24918 {
24919 /* Do we at least have room for a macinfo type byte? */
24920 if (mac_ptr >= mac_end)
24921 {
24922 dwarf2_section_buffer_overflow_complaint (section);
24923 break;
24924 }
24925
24926 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24927 mac_ptr++;
24928
24929 /* Note that we rely on the fact that the corresponding GNU and
24930 DWARF constants are the same. */
24931 DIAGNOSTIC_PUSH
24932 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24933 switch (macinfo_type)
24934 {
24935 /* A zero macinfo type indicates the end of the macro
24936 information. */
24937 case 0:
24938 break;
24939
24940 case DW_MACRO_define:
24941 case DW_MACRO_undef:
24942 case DW_MACRO_define_strp:
24943 case DW_MACRO_undef_strp:
24944 case DW_MACRO_define_sup:
24945 case DW_MACRO_undef_sup:
24946 {
24947 unsigned int bytes_read;
24948 int line;
24949 const char *body;
24950 int is_define;
24951
24952 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24953 mac_ptr += bytes_read;
24954
24955 if (macinfo_type == DW_MACRO_define
24956 || macinfo_type == DW_MACRO_undef)
24957 {
24958 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24959 mac_ptr += bytes_read;
24960 }
24961 else
24962 {
24963 LONGEST str_offset;
24964
24965 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24966 mac_ptr += offset_size;
24967
24968 if (macinfo_type == DW_MACRO_define_sup
24969 || macinfo_type == DW_MACRO_undef_sup
24970 || section_is_dwz)
24971 {
24972 struct dwz_file *dwz
24973 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24974
24975 body = read_indirect_string_from_dwz (objfile,
24976 dwz, str_offset);
24977 }
24978 else
24979 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24980 abfd, str_offset);
24981 }
24982
24983 is_define = (macinfo_type == DW_MACRO_define
24984 || macinfo_type == DW_MACRO_define_strp
24985 || macinfo_type == DW_MACRO_define_sup);
24986 if (! current_file)
24987 {
24988 /* DWARF violation as no main source is present. */
24989 complaint (_("debug info with no main source gives macro %s "
24990 "on line %d: %s"),
24991 is_define ? _("definition") : _("undefinition"),
24992 line, body);
24993 break;
24994 }
24995 if ((line == 0 && !at_commandline)
24996 || (line != 0 && at_commandline))
24997 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
24998 at_commandline ? _("command-line") : _("in-file"),
24999 is_define ? _("definition") : _("undefinition"),
25000 line == 0 ? _("zero") : _("non-zero"), line, body);
25001
25002 if (body == NULL)
25003 {
25004 /* Fedora's rpm-build's "debugedit" binary
25005 corrupted .debug_macro sections.
25006
25007 For more info, see
25008 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
25009 complaint (_("debug info gives %s invalid macro %s "
25010 "without body (corrupted?) at line %d "
25011 "on file %s"),
25012 at_commandline ? _("command-line") : _("in-file"),
25013 is_define ? _("definition") : _("undefinition"),
25014 line, current_file->filename);
25015 }
25016 else if (is_define)
25017 parse_macro_definition (current_file, line, body);
25018 else
25019 {
25020 gdb_assert (macinfo_type == DW_MACRO_undef
25021 || macinfo_type == DW_MACRO_undef_strp
25022 || macinfo_type == DW_MACRO_undef_sup);
25023 macro_undef (current_file, line, body);
25024 }
25025 }
25026 break;
25027
25028 case DW_MACRO_start_file:
25029 {
25030 unsigned int bytes_read;
25031 int line, file;
25032
25033 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25034 mac_ptr += bytes_read;
25035 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25036 mac_ptr += bytes_read;
25037
25038 if ((line == 0 && !at_commandline)
25039 || (line != 0 && at_commandline))
25040 complaint (_("debug info gives source %d included "
25041 "from %s at %s line %d"),
25042 file, at_commandline ? _("command-line") : _("file"),
25043 line == 0 ? _("zero") : _("non-zero"), line);
25044
25045 if (at_commandline)
25046 {
25047 /* This DW_MACRO_start_file was executed in the
25048 pass one. */
25049 at_commandline = 0;
25050 }
25051 else
25052 current_file = macro_start_file (cu, file, line, current_file,
25053 lh);
25054 }
25055 break;
25056
25057 case DW_MACRO_end_file:
25058 if (! current_file)
25059 complaint (_("macro debug info has an unmatched "
25060 "`close_file' directive"));
25061 else
25062 {
25063 current_file = current_file->included_by;
25064 if (! current_file)
25065 {
25066 enum dwarf_macro_record_type next_type;
25067
25068 /* GCC circa March 2002 doesn't produce the zero
25069 type byte marking the end of the compilation
25070 unit. Complain if it's not there, but exit no
25071 matter what. */
25072
25073 /* Do we at least have room for a macinfo type byte? */
25074 if (mac_ptr >= mac_end)
25075 {
25076 dwarf2_section_buffer_overflow_complaint (section);
25077 return;
25078 }
25079
25080 /* We don't increment mac_ptr here, so this is just
25081 a look-ahead. */
25082 next_type
25083 = (enum dwarf_macro_record_type) read_1_byte (abfd,
25084 mac_ptr);
25085 if (next_type != 0)
25086 complaint (_("no terminating 0-type entry for "
25087 "macros in `.debug_macinfo' section"));
25088
25089 return;
25090 }
25091 }
25092 break;
25093
25094 case DW_MACRO_import:
25095 case DW_MACRO_import_sup:
25096 {
25097 LONGEST offset;
25098 void **slot;
25099 bfd *include_bfd = abfd;
25100 struct dwarf2_section_info *include_section = section;
25101 const gdb_byte *include_mac_end = mac_end;
25102 int is_dwz = section_is_dwz;
25103 const gdb_byte *new_mac_ptr;
25104
25105 offset = read_offset_1 (abfd, mac_ptr, offset_size);
25106 mac_ptr += offset_size;
25107
25108 if (macinfo_type == DW_MACRO_import_sup)
25109 {
25110 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
25111
25112 dwarf2_read_section (objfile, &dwz->macro);
25113
25114 include_section = &dwz->macro;
25115 include_bfd = get_section_bfd_owner (include_section);
25116 include_mac_end = dwz->macro.buffer + dwz->macro.size;
25117 is_dwz = 1;
25118 }
25119
25120 new_mac_ptr = include_section->buffer + offset;
25121 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
25122
25123 if (*slot != NULL)
25124 {
25125 /* This has actually happened; see
25126 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
25127 complaint (_("recursive DW_MACRO_import in "
25128 ".debug_macro section"));
25129 }
25130 else
25131 {
25132 *slot = (void *) new_mac_ptr;
25133
25134 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
25135 include_mac_end, current_file, lh,
25136 section, section_is_gnu, is_dwz,
25137 offset_size, include_hash);
25138
25139 htab_remove_elt (include_hash, (void *) new_mac_ptr);
25140 }
25141 }
25142 break;
25143
25144 case DW_MACINFO_vendor_ext:
25145 if (!section_is_gnu)
25146 {
25147 unsigned int bytes_read;
25148
25149 /* This reads the constant, but since we don't recognize
25150 any vendor extensions, we ignore it. */
25151 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25152 mac_ptr += bytes_read;
25153 read_direct_string (abfd, mac_ptr, &bytes_read);
25154 mac_ptr += bytes_read;
25155
25156 /* We don't recognize any vendor extensions. */
25157 break;
25158 }
25159 /* FALLTHROUGH */
25160
25161 default:
25162 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25163 mac_ptr, mac_end, abfd, offset_size,
25164 section);
25165 if (mac_ptr == NULL)
25166 return;
25167 break;
25168 }
25169 DIAGNOSTIC_POP
25170 } while (macinfo_type != 0);
25171 }
25172
25173 static void
25174 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
25175 int section_is_gnu)
25176 {
25177 struct dwarf2_per_objfile *dwarf2_per_objfile
25178 = cu->per_cu->dwarf2_per_objfile;
25179 struct objfile *objfile = dwarf2_per_objfile->objfile;
25180 struct line_header *lh = cu->line_header;
25181 bfd *abfd;
25182 const gdb_byte *mac_ptr, *mac_end;
25183 struct macro_source_file *current_file = 0;
25184 enum dwarf_macro_record_type macinfo_type;
25185 unsigned int offset_size = cu->header.offset_size;
25186 const gdb_byte *opcode_definitions[256];
25187 void **slot;
25188 struct dwarf2_section_info *section;
25189 const char *section_name;
25190
25191 if (cu->dwo_unit != NULL)
25192 {
25193 if (section_is_gnu)
25194 {
25195 section = &cu->dwo_unit->dwo_file->sections.macro;
25196 section_name = ".debug_macro.dwo";
25197 }
25198 else
25199 {
25200 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25201 section_name = ".debug_macinfo.dwo";
25202 }
25203 }
25204 else
25205 {
25206 if (section_is_gnu)
25207 {
25208 section = &dwarf2_per_objfile->macro;
25209 section_name = ".debug_macro";
25210 }
25211 else
25212 {
25213 section = &dwarf2_per_objfile->macinfo;
25214 section_name = ".debug_macinfo";
25215 }
25216 }
25217
25218 dwarf2_read_section (objfile, section);
25219 if (section->buffer == NULL)
25220 {
25221 complaint (_("missing %s section"), section_name);
25222 return;
25223 }
25224 abfd = get_section_bfd_owner (section);
25225
25226 /* First pass: Find the name of the base filename.
25227 This filename is needed in order to process all macros whose definition
25228 (or undefinition) comes from the command line. These macros are defined
25229 before the first DW_MACINFO_start_file entry, and yet still need to be
25230 associated to the base file.
25231
25232 To determine the base file name, we scan the macro definitions until we
25233 reach the first DW_MACINFO_start_file entry. We then initialize
25234 CURRENT_FILE accordingly so that any macro definition found before the
25235 first DW_MACINFO_start_file can still be associated to the base file. */
25236
25237 mac_ptr = section->buffer + offset;
25238 mac_end = section->buffer + section->size;
25239
25240 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25241 &offset_size, section_is_gnu);
25242 if (mac_ptr == NULL)
25243 {
25244 /* We already issued a complaint. */
25245 return;
25246 }
25247
25248 do
25249 {
25250 /* Do we at least have room for a macinfo type byte? */
25251 if (mac_ptr >= mac_end)
25252 {
25253 /* Complaint is printed during the second pass as GDB will probably
25254 stop the first pass earlier upon finding
25255 DW_MACINFO_start_file. */
25256 break;
25257 }
25258
25259 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
25260 mac_ptr++;
25261
25262 /* Note that we rely on the fact that the corresponding GNU and
25263 DWARF constants are the same. */
25264 DIAGNOSTIC_PUSH
25265 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
25266 switch (macinfo_type)
25267 {
25268 /* A zero macinfo type indicates the end of the macro
25269 information. */
25270 case 0:
25271 break;
25272
25273 case DW_MACRO_define:
25274 case DW_MACRO_undef:
25275 /* Only skip the data by MAC_PTR. */
25276 {
25277 unsigned int bytes_read;
25278
25279 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25280 mac_ptr += bytes_read;
25281 read_direct_string (abfd, mac_ptr, &bytes_read);
25282 mac_ptr += bytes_read;
25283 }
25284 break;
25285
25286 case DW_MACRO_start_file:
25287 {
25288 unsigned int bytes_read;
25289 int line, file;
25290
25291 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25292 mac_ptr += bytes_read;
25293 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25294 mac_ptr += bytes_read;
25295
25296 current_file = macro_start_file (cu, file, line, current_file, lh);
25297 }
25298 break;
25299
25300 case DW_MACRO_end_file:
25301 /* No data to skip by MAC_PTR. */
25302 break;
25303
25304 case DW_MACRO_define_strp:
25305 case DW_MACRO_undef_strp:
25306 case DW_MACRO_define_sup:
25307 case DW_MACRO_undef_sup:
25308 {
25309 unsigned int bytes_read;
25310
25311 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25312 mac_ptr += bytes_read;
25313 mac_ptr += offset_size;
25314 }
25315 break;
25316
25317 case DW_MACRO_import:
25318 case DW_MACRO_import_sup:
25319 /* Note that, according to the spec, a transparent include
25320 chain cannot call DW_MACRO_start_file. So, we can just
25321 skip this opcode. */
25322 mac_ptr += offset_size;
25323 break;
25324
25325 case DW_MACINFO_vendor_ext:
25326 /* Only skip the data by MAC_PTR. */
25327 if (!section_is_gnu)
25328 {
25329 unsigned int bytes_read;
25330
25331 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25332 mac_ptr += bytes_read;
25333 read_direct_string (abfd, mac_ptr, &bytes_read);
25334 mac_ptr += bytes_read;
25335 }
25336 /* FALLTHROUGH */
25337
25338 default:
25339 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25340 mac_ptr, mac_end, abfd, offset_size,
25341 section);
25342 if (mac_ptr == NULL)
25343 return;
25344 break;
25345 }
25346 DIAGNOSTIC_POP
25347 } while (macinfo_type != 0 && current_file == NULL);
25348
25349 /* Second pass: Process all entries.
25350
25351 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25352 command-line macro definitions/undefinitions. This flag is unset when we
25353 reach the first DW_MACINFO_start_file entry. */
25354
25355 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25356 htab_eq_pointer,
25357 NULL, xcalloc, xfree));
25358 mac_ptr = section->buffer + offset;
25359 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
25360 *slot = (void *) mac_ptr;
25361 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
25362 current_file, lh, section,
25363 section_is_gnu, 0, offset_size,
25364 include_hash.get ());
25365 }
25366
25367 /* Check if the attribute's form is a DW_FORM_block*
25368 if so return true else false. */
25369
25370 static int
25371 attr_form_is_block (const struct attribute *attr)
25372 {
25373 return (attr == NULL ? 0 :
25374 attr->form == DW_FORM_block1
25375 || attr->form == DW_FORM_block2
25376 || attr->form == DW_FORM_block4
25377 || attr->form == DW_FORM_block
25378 || attr->form == DW_FORM_exprloc);
25379 }
25380
25381 /* Return non-zero if ATTR's value is a section offset --- classes
25382 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25383 You may use DW_UNSND (attr) to retrieve such offsets.
25384
25385 Section 7.5.4, "Attribute Encodings", explains that no attribute
25386 may have a value that belongs to more than one of these classes; it
25387 would be ambiguous if we did, because we use the same forms for all
25388 of them. */
25389
25390 static int
25391 attr_form_is_section_offset (const struct attribute *attr)
25392 {
25393 return (attr->form == DW_FORM_data4
25394 || attr->form == DW_FORM_data8
25395 || attr->form == DW_FORM_sec_offset);
25396 }
25397
25398 /* Return non-zero if ATTR's value falls in the 'constant' class, or
25399 zero otherwise. When this function returns true, you can apply
25400 dwarf2_get_attr_constant_value to it.
25401
25402 However, note that for some attributes you must check
25403 attr_form_is_section_offset before using this test. DW_FORM_data4
25404 and DW_FORM_data8 are members of both the constant class, and of
25405 the classes that contain offsets into other debug sections
25406 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25407 that, if an attribute's can be either a constant or one of the
25408 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25409 taken as section offsets, not constants.
25410
25411 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25412 cannot handle that. */
25413
25414 static int
25415 attr_form_is_constant (const struct attribute *attr)
25416 {
25417 switch (attr->form)
25418 {
25419 case DW_FORM_sdata:
25420 case DW_FORM_udata:
25421 case DW_FORM_data1:
25422 case DW_FORM_data2:
25423 case DW_FORM_data4:
25424 case DW_FORM_data8:
25425 case DW_FORM_implicit_const:
25426 return 1;
25427 default:
25428 return 0;
25429 }
25430 }
25431
25432
25433 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25434 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25435
25436 static int
25437 attr_form_is_ref (const struct attribute *attr)
25438 {
25439 switch (attr->form)
25440 {
25441 case DW_FORM_ref_addr:
25442 case DW_FORM_ref1:
25443 case DW_FORM_ref2:
25444 case DW_FORM_ref4:
25445 case DW_FORM_ref8:
25446 case DW_FORM_ref_udata:
25447 case DW_FORM_GNU_ref_alt:
25448 return 1;
25449 default:
25450 return 0;
25451 }
25452 }
25453
25454 /* Return the .debug_loc section to use for CU.
25455 For DWO files use .debug_loc.dwo. */
25456
25457 static struct dwarf2_section_info *
25458 cu_debug_loc_section (struct dwarf2_cu *cu)
25459 {
25460 struct dwarf2_per_objfile *dwarf2_per_objfile
25461 = cu->per_cu->dwarf2_per_objfile;
25462
25463 if (cu->dwo_unit)
25464 {
25465 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25466
25467 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25468 }
25469 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25470 : &dwarf2_per_objfile->loc);
25471 }
25472
25473 /* A helper function that fills in a dwarf2_loclist_baton. */
25474
25475 static void
25476 fill_in_loclist_baton (struct dwarf2_cu *cu,
25477 struct dwarf2_loclist_baton *baton,
25478 const struct attribute *attr)
25479 {
25480 struct dwarf2_per_objfile *dwarf2_per_objfile
25481 = cu->per_cu->dwarf2_per_objfile;
25482 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25483
25484 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25485
25486 baton->per_cu = cu->per_cu;
25487 gdb_assert (baton->per_cu);
25488 /* We don't know how long the location list is, but make sure we
25489 don't run off the edge of the section. */
25490 baton->size = section->size - DW_UNSND (attr);
25491 baton->data = section->buffer + DW_UNSND (attr);
25492 baton->base_address = cu->base_address;
25493 baton->from_dwo = cu->dwo_unit != NULL;
25494 }
25495
25496 static void
25497 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25498 struct dwarf2_cu *cu, int is_block)
25499 {
25500 struct dwarf2_per_objfile *dwarf2_per_objfile
25501 = cu->per_cu->dwarf2_per_objfile;
25502 struct objfile *objfile = dwarf2_per_objfile->objfile;
25503 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25504
25505 if (attr_form_is_section_offset (attr)
25506 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25507 the section. If so, fall through to the complaint in the
25508 other branch. */
25509 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25510 {
25511 struct dwarf2_loclist_baton *baton;
25512
25513 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25514
25515 fill_in_loclist_baton (cu, baton, attr);
25516
25517 if (cu->base_known == 0)
25518 complaint (_("Location list used without "
25519 "specifying the CU base address."));
25520
25521 SYMBOL_ACLASS_INDEX (sym) = (is_block
25522 ? dwarf2_loclist_block_index
25523 : dwarf2_loclist_index);
25524 SYMBOL_LOCATION_BATON (sym) = baton;
25525 }
25526 else
25527 {
25528 struct dwarf2_locexpr_baton *baton;
25529
25530 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25531 baton->per_cu = cu->per_cu;
25532 gdb_assert (baton->per_cu);
25533
25534 if (attr_form_is_block (attr))
25535 {
25536 /* Note that we're just copying the block's data pointer
25537 here, not the actual data. We're still pointing into the
25538 info_buffer for SYM's objfile; right now we never release
25539 that buffer, but when we do clean up properly this may
25540 need to change. */
25541 baton->size = DW_BLOCK (attr)->size;
25542 baton->data = DW_BLOCK (attr)->data;
25543 }
25544 else
25545 {
25546 dwarf2_invalid_attrib_class_complaint ("location description",
25547 sym->natural_name ());
25548 baton->size = 0;
25549 }
25550
25551 SYMBOL_ACLASS_INDEX (sym) = (is_block
25552 ? dwarf2_locexpr_block_index
25553 : dwarf2_locexpr_index);
25554 SYMBOL_LOCATION_BATON (sym) = baton;
25555 }
25556 }
25557
25558 /* Return the OBJFILE associated with the compilation unit CU. If CU
25559 came from a separate debuginfo file, then the master objfile is
25560 returned. */
25561
25562 struct objfile *
25563 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25564 {
25565 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25566
25567 /* Return the master objfile, so that we can report and look up the
25568 correct file containing this variable. */
25569 if (objfile->separate_debug_objfile_backlink)
25570 objfile = objfile->separate_debug_objfile_backlink;
25571
25572 return objfile;
25573 }
25574
25575 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25576 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25577 CU_HEADERP first. */
25578
25579 static const struct comp_unit_head *
25580 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25581 struct dwarf2_per_cu_data *per_cu)
25582 {
25583 const gdb_byte *info_ptr;
25584
25585 if (per_cu->cu)
25586 return &per_cu->cu->header;
25587
25588 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25589
25590 memset (cu_headerp, 0, sizeof (*cu_headerp));
25591 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25592 rcuh_kind::COMPILE);
25593
25594 return cu_headerp;
25595 }
25596
25597 /* Return the address size given in the compilation unit header for CU. */
25598
25599 int
25600 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25601 {
25602 struct comp_unit_head cu_header_local;
25603 const struct comp_unit_head *cu_headerp;
25604
25605 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25606
25607 return cu_headerp->addr_size;
25608 }
25609
25610 /* Return the offset size given in the compilation unit header for CU. */
25611
25612 int
25613 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25614 {
25615 struct comp_unit_head cu_header_local;
25616 const struct comp_unit_head *cu_headerp;
25617
25618 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25619
25620 return cu_headerp->offset_size;
25621 }
25622
25623 /* See its dwarf2loc.h declaration. */
25624
25625 int
25626 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25627 {
25628 struct comp_unit_head cu_header_local;
25629 const struct comp_unit_head *cu_headerp;
25630
25631 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25632
25633 if (cu_headerp->version == 2)
25634 return cu_headerp->addr_size;
25635 else
25636 return cu_headerp->offset_size;
25637 }
25638
25639 /* Return the text offset of the CU. The returned offset comes from
25640 this CU's objfile. If this objfile came from a separate debuginfo
25641 file, then the offset may be different from the corresponding
25642 offset in the parent objfile. */
25643
25644 CORE_ADDR
25645 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25646 {
25647 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25648
25649 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25650 }
25651
25652 /* Return a type that is a generic pointer type, the size of which matches
25653 the address size given in the compilation unit header for PER_CU. */
25654 static struct type *
25655 dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25656 {
25657 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25658 struct type *void_type = objfile_type (objfile)->builtin_void;
25659 struct type *addr_type = lookup_pointer_type (void_type);
25660 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25661
25662 if (TYPE_LENGTH (addr_type) == addr_size)
25663 return addr_type;
25664
25665 addr_type
25666 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25667 return addr_type;
25668 }
25669
25670 /* Return DWARF version number of PER_CU. */
25671
25672 short
25673 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25674 {
25675 return per_cu->dwarf_version;
25676 }
25677
25678 /* Locate the .debug_info compilation unit from CU's objfile which contains
25679 the DIE at OFFSET. Raises an error on failure. */
25680
25681 static struct dwarf2_per_cu_data *
25682 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25683 unsigned int offset_in_dwz,
25684 struct dwarf2_per_objfile *dwarf2_per_objfile)
25685 {
25686 struct dwarf2_per_cu_data *this_cu;
25687 int low, high;
25688
25689 low = 0;
25690 high = dwarf2_per_objfile->all_comp_units.size () - 1;
25691 while (high > low)
25692 {
25693 struct dwarf2_per_cu_data *mid_cu;
25694 int mid = low + (high - low) / 2;
25695
25696 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25697 if (mid_cu->is_dwz > offset_in_dwz
25698 || (mid_cu->is_dwz == offset_in_dwz
25699 && mid_cu->sect_off + mid_cu->length >= sect_off))
25700 high = mid;
25701 else
25702 low = mid + 1;
25703 }
25704 gdb_assert (low == high);
25705 this_cu = dwarf2_per_objfile->all_comp_units[low];
25706 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25707 {
25708 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25709 error (_("Dwarf Error: could not find partial DIE containing "
25710 "offset %s [in module %s]"),
25711 sect_offset_str (sect_off),
25712 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25713
25714 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25715 <= sect_off);
25716 return dwarf2_per_objfile->all_comp_units[low-1];
25717 }
25718 else
25719 {
25720 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25721 && sect_off >= this_cu->sect_off + this_cu->length)
25722 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25723 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25724 return this_cu;
25725 }
25726 }
25727
25728 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25729
25730 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25731 : per_cu (per_cu_),
25732 mark (false),
25733 has_loclist (false),
25734 checked_producer (false),
25735 producer_is_gxx_lt_4_6 (false),
25736 producer_is_gcc_lt_4_3 (false),
25737 producer_is_icc (false),
25738 producer_is_icc_lt_14 (false),
25739 producer_is_codewarrior (false),
25740 processing_has_namespace_info (false)
25741 {
25742 per_cu->cu = this;
25743 }
25744
25745 /* Destroy a dwarf2_cu. */
25746
25747 dwarf2_cu::~dwarf2_cu ()
25748 {
25749 per_cu->cu = NULL;
25750 }
25751
25752 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25753
25754 static void
25755 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25756 enum language pretend_language)
25757 {
25758 struct attribute *attr;
25759
25760 /* Set the language we're debugging. */
25761 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25762 if (attr != nullptr)
25763 set_cu_language (DW_UNSND (attr), cu);
25764 else
25765 {
25766 cu->language = pretend_language;
25767 cu->language_defn = language_def (cu->language);
25768 }
25769
25770 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25771 }
25772
25773 /* Increase the age counter on each cached compilation unit, and free
25774 any that are too old. */
25775
25776 static void
25777 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25778 {
25779 struct dwarf2_per_cu_data *per_cu, **last_chain;
25780
25781 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25782 per_cu = dwarf2_per_objfile->read_in_chain;
25783 while (per_cu != NULL)
25784 {
25785 per_cu->cu->last_used ++;
25786 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25787 dwarf2_mark (per_cu->cu);
25788 per_cu = per_cu->cu->read_in_chain;
25789 }
25790
25791 per_cu = dwarf2_per_objfile->read_in_chain;
25792 last_chain = &dwarf2_per_objfile->read_in_chain;
25793 while (per_cu != NULL)
25794 {
25795 struct dwarf2_per_cu_data *next_cu;
25796
25797 next_cu = per_cu->cu->read_in_chain;
25798
25799 if (!per_cu->cu->mark)
25800 {
25801 delete per_cu->cu;
25802 *last_chain = next_cu;
25803 }
25804 else
25805 last_chain = &per_cu->cu->read_in_chain;
25806
25807 per_cu = next_cu;
25808 }
25809 }
25810
25811 /* Remove a single compilation unit from the cache. */
25812
25813 static void
25814 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25815 {
25816 struct dwarf2_per_cu_data *per_cu, **last_chain;
25817 struct dwarf2_per_objfile *dwarf2_per_objfile
25818 = target_per_cu->dwarf2_per_objfile;
25819
25820 per_cu = dwarf2_per_objfile->read_in_chain;
25821 last_chain = &dwarf2_per_objfile->read_in_chain;
25822 while (per_cu != NULL)
25823 {
25824 struct dwarf2_per_cu_data *next_cu;
25825
25826 next_cu = per_cu->cu->read_in_chain;
25827
25828 if (per_cu == target_per_cu)
25829 {
25830 delete per_cu->cu;
25831 per_cu->cu = NULL;
25832 *last_chain = next_cu;
25833 break;
25834 }
25835 else
25836 last_chain = &per_cu->cu->read_in_chain;
25837
25838 per_cu = next_cu;
25839 }
25840 }
25841
25842 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25843 We store these in a hash table separate from the DIEs, and preserve them
25844 when the DIEs are flushed out of cache.
25845
25846 The CU "per_cu" pointer is needed because offset alone is not enough to
25847 uniquely identify the type. A file may have multiple .debug_types sections,
25848 or the type may come from a DWO file. Furthermore, while it's more logical
25849 to use per_cu->section+offset, with Fission the section with the data is in
25850 the DWO file but we don't know that section at the point we need it.
25851 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25852 because we can enter the lookup routine, get_die_type_at_offset, from
25853 outside this file, and thus won't necessarily have PER_CU->cu.
25854 Fortunately, PER_CU is stable for the life of the objfile. */
25855
25856 struct dwarf2_per_cu_offset_and_type
25857 {
25858 const struct dwarf2_per_cu_data *per_cu;
25859 sect_offset sect_off;
25860 struct type *type;
25861 };
25862
25863 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25864
25865 static hashval_t
25866 per_cu_offset_and_type_hash (const void *item)
25867 {
25868 const struct dwarf2_per_cu_offset_and_type *ofs
25869 = (const struct dwarf2_per_cu_offset_and_type *) item;
25870
25871 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25872 }
25873
25874 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25875
25876 static int
25877 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25878 {
25879 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25880 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25881 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25882 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25883
25884 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25885 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25886 }
25887
25888 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25889 table if necessary. For convenience, return TYPE.
25890
25891 The DIEs reading must have careful ordering to:
25892 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25893 reading current DIE.
25894 * Not trying to dereference contents of still incompletely read in types
25895 while reading in other DIEs.
25896 * Enable referencing still incompletely read in types just by a pointer to
25897 the type without accessing its fields.
25898
25899 Therefore caller should follow these rules:
25900 * Try to fetch any prerequisite types we may need to build this DIE type
25901 before building the type and calling set_die_type.
25902 * After building type call set_die_type for current DIE as soon as
25903 possible before fetching more types to complete the current type.
25904 * Make the type as complete as possible before fetching more types. */
25905
25906 static struct type *
25907 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25908 {
25909 struct dwarf2_per_objfile *dwarf2_per_objfile
25910 = cu->per_cu->dwarf2_per_objfile;
25911 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25912 struct objfile *objfile = dwarf2_per_objfile->objfile;
25913 struct attribute *attr;
25914 struct dynamic_prop prop;
25915
25916 /* For Ada types, make sure that the gnat-specific data is always
25917 initialized (if not already set). There are a few types where
25918 we should not be doing so, because the type-specific area is
25919 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25920 where the type-specific area is used to store the floatformat).
25921 But this is not a problem, because the gnat-specific information
25922 is actually not needed for these types. */
25923 if (need_gnat_info (cu)
25924 && TYPE_CODE (type) != TYPE_CODE_FUNC
25925 && TYPE_CODE (type) != TYPE_CODE_FLT
25926 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25927 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25928 && TYPE_CODE (type) != TYPE_CODE_METHOD
25929 && !HAVE_GNAT_AUX_INFO (type))
25930 INIT_GNAT_SPECIFIC (type);
25931
25932 /* Read DW_AT_allocated and set in type. */
25933 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25934 if (attr_form_is_block (attr))
25935 {
25936 struct type *prop_type
25937 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25938 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25939 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25940 }
25941 else if (attr != NULL)
25942 {
25943 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25944 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25945 sect_offset_str (die->sect_off));
25946 }
25947
25948 /* Read DW_AT_associated and set in type. */
25949 attr = dwarf2_attr (die, DW_AT_associated, cu);
25950 if (attr_form_is_block (attr))
25951 {
25952 struct type *prop_type
25953 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25954 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25955 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25956 }
25957 else if (attr != NULL)
25958 {
25959 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25960 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25961 sect_offset_str (die->sect_off));
25962 }
25963
25964 /* Read DW_AT_data_location and set in type. */
25965 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25966 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25967 dwarf2_per_cu_addr_type (cu->per_cu)))
25968 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25969
25970 if (dwarf2_per_objfile->die_type_hash == NULL)
25971 {
25972 dwarf2_per_objfile->die_type_hash =
25973 htab_create_alloc_ex (127,
25974 per_cu_offset_and_type_hash,
25975 per_cu_offset_and_type_eq,
25976 NULL,
25977 &objfile->objfile_obstack,
25978 hashtab_obstack_allocate,
25979 dummy_obstack_deallocate);
25980 }
25981
25982 ofs.per_cu = cu->per_cu;
25983 ofs.sect_off = die->sect_off;
25984 ofs.type = type;
25985 slot = (struct dwarf2_per_cu_offset_and_type **)
25986 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25987 if (*slot)
25988 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25989 sect_offset_str (die->sect_off));
25990 *slot = XOBNEW (&objfile->objfile_obstack,
25991 struct dwarf2_per_cu_offset_and_type);
25992 **slot = ofs;
25993 return type;
25994 }
25995
25996 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25997 or return NULL if the die does not have a saved type. */
25998
25999 static struct type *
26000 get_die_type_at_offset (sect_offset sect_off,
26001 struct dwarf2_per_cu_data *per_cu)
26002 {
26003 struct dwarf2_per_cu_offset_and_type *slot, ofs;
26004 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
26005
26006 if (dwarf2_per_objfile->die_type_hash == NULL)
26007 return NULL;
26008
26009 ofs.per_cu = per_cu;
26010 ofs.sect_off = sect_off;
26011 slot = ((struct dwarf2_per_cu_offset_and_type *)
26012 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
26013 if (slot)
26014 return slot->type;
26015 else
26016 return NULL;
26017 }
26018
26019 /* Look up the type for DIE in CU in die_type_hash,
26020 or return NULL if DIE does not have a saved type. */
26021
26022 static struct type *
26023 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
26024 {
26025 return get_die_type_at_offset (die->sect_off, cu->per_cu);
26026 }
26027
26028 /* Add a dependence relationship from CU to REF_PER_CU. */
26029
26030 static void
26031 dwarf2_add_dependence (struct dwarf2_cu *cu,
26032 struct dwarf2_per_cu_data *ref_per_cu)
26033 {
26034 void **slot;
26035
26036 if (cu->dependencies == NULL)
26037 cu->dependencies
26038 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
26039 NULL, &cu->comp_unit_obstack,
26040 hashtab_obstack_allocate,
26041 dummy_obstack_deallocate);
26042
26043 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
26044 if (*slot == NULL)
26045 *slot = ref_per_cu;
26046 }
26047
26048 /* Subroutine of dwarf2_mark to pass to htab_traverse.
26049 Set the mark field in every compilation unit in the
26050 cache that we must keep because we are keeping CU. */
26051
26052 static int
26053 dwarf2_mark_helper (void **slot, void *data)
26054 {
26055 struct dwarf2_per_cu_data *per_cu;
26056
26057 per_cu = (struct dwarf2_per_cu_data *) *slot;
26058
26059 /* cu->dependencies references may not yet have been ever read if QUIT aborts
26060 reading of the chain. As such dependencies remain valid it is not much
26061 useful to track and undo them during QUIT cleanups. */
26062 if (per_cu->cu == NULL)
26063 return 1;
26064
26065 if (per_cu->cu->mark)
26066 return 1;
26067 per_cu->cu->mark = true;
26068
26069 if (per_cu->cu->dependencies != NULL)
26070 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
26071
26072 return 1;
26073 }
26074
26075 /* Set the mark field in CU and in every other compilation unit in the
26076 cache that we must keep because we are keeping CU. */
26077
26078 static void
26079 dwarf2_mark (struct dwarf2_cu *cu)
26080 {
26081 if (cu->mark)
26082 return;
26083 cu->mark = true;
26084 if (cu->dependencies != NULL)
26085 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
26086 }
26087
26088 static void
26089 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
26090 {
26091 while (per_cu)
26092 {
26093 per_cu->cu->mark = false;
26094 per_cu = per_cu->cu->read_in_chain;
26095 }
26096 }
26097
26098 /* Trivial hash function for partial_die_info: the hash value of a DIE
26099 is its offset in .debug_info for this objfile. */
26100
26101 static hashval_t
26102 partial_die_hash (const void *item)
26103 {
26104 const struct partial_die_info *part_die
26105 = (const struct partial_die_info *) item;
26106
26107 return to_underlying (part_die->sect_off);
26108 }
26109
26110 /* Trivial comparison function for partial_die_info structures: two DIEs
26111 are equal if they have the same offset. */
26112
26113 static int
26114 partial_die_eq (const void *item_lhs, const void *item_rhs)
26115 {
26116 const struct partial_die_info *part_die_lhs
26117 = (const struct partial_die_info *) item_lhs;
26118 const struct partial_die_info *part_die_rhs
26119 = (const struct partial_die_info *) item_rhs;
26120
26121 return part_die_lhs->sect_off == part_die_rhs->sect_off;
26122 }
26123
26124 struct cmd_list_element *set_dwarf_cmdlist;
26125 struct cmd_list_element *show_dwarf_cmdlist;
26126
26127 static void
26128 set_dwarf_cmd (const char *args, int from_tty)
26129 {
26130 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
26131 gdb_stdout);
26132 }
26133
26134 static void
26135 show_dwarf_cmd (const char *args, int from_tty)
26136 {
26137 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
26138 }
26139
26140 bool dwarf_always_disassemble;
26141
26142 static void
26143 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
26144 struct cmd_list_element *c, const char *value)
26145 {
26146 fprintf_filtered (file,
26147 _("Whether to always disassemble "
26148 "DWARF expressions is %s.\n"),
26149 value);
26150 }
26151
26152 static void
26153 show_check_physname (struct ui_file *file, int from_tty,
26154 struct cmd_list_element *c, const char *value)
26155 {
26156 fprintf_filtered (file,
26157 _("Whether to check \"physname\" is %s.\n"),
26158 value);
26159 }
26160
26161 void
26162 _initialize_dwarf2_read (void)
26163 {
26164 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26165 Set DWARF specific variables.\n\
26166 Configure DWARF variables such as the cache size."),
26167 &set_dwarf_cmdlist, "maintenance set dwarf ",
26168 0/*allow-unknown*/, &maintenance_set_cmdlist);
26169
26170 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
26171 Show DWARF specific variables.\n\
26172 Show DWARF variables such as the cache size."),
26173 &show_dwarf_cmdlist, "maintenance show dwarf ",
26174 0/*allow-unknown*/, &maintenance_show_cmdlist);
26175
26176 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26177 &dwarf_max_cache_age, _("\
26178 Set the upper bound on the age of cached DWARF compilation units."), _("\
26179 Show the upper bound on the age of cached DWARF compilation units."), _("\
26180 A higher limit means that cached compilation units will be stored\n\
26181 in memory longer, and more total memory will be used. Zero disables\n\
26182 caching, which can slow down startup."),
26183 NULL,
26184 show_dwarf_max_cache_age,
26185 &set_dwarf_cmdlist,
26186 &show_dwarf_cmdlist);
26187
26188 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
26189 &dwarf_always_disassemble, _("\
26190 Set whether `info address' always disassembles DWARF expressions."), _("\
26191 Show whether `info address' always disassembles DWARF expressions."), _("\
26192 When enabled, DWARF expressions are always printed in an assembly-like\n\
26193 syntax. When disabled, expressions will be printed in a more\n\
26194 conversational style, when possible."),
26195 NULL,
26196 show_dwarf_always_disassemble,
26197 &set_dwarf_cmdlist,
26198 &show_dwarf_cmdlist);
26199
26200 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26201 Set debugging of the DWARF reader."), _("\
26202 Show debugging of the DWARF reader."), _("\
26203 When enabled (non-zero), debugging messages are printed during DWARF\n\
26204 reading and symtab expansion. A value of 1 (one) provides basic\n\
26205 information. A value greater than 1 provides more verbose information."),
26206 NULL,
26207 NULL,
26208 &setdebuglist, &showdebuglist);
26209
26210 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26211 Set debugging of the DWARF DIE reader."), _("\
26212 Show debugging of the DWARF DIE reader."), _("\
26213 When enabled (non-zero), DIEs are dumped after they are read in.\n\
26214 The value is the maximum depth to print."),
26215 NULL,
26216 NULL,
26217 &setdebuglist, &showdebuglist);
26218
26219 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
26220 Set debugging of the dwarf line reader."), _("\
26221 Show debugging of the dwarf line reader."), _("\
26222 When enabled (non-zero), line number entries are dumped as they are read in.\n\
26223 A value of 1 (one) provides basic information.\n\
26224 A value greater than 1 provides more verbose information."),
26225 NULL,
26226 NULL,
26227 &setdebuglist, &showdebuglist);
26228
26229 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
26230 Set cross-checking of \"physname\" code against demangler."), _("\
26231 Show cross-checking of \"physname\" code against demangler."), _("\
26232 When enabled, GDB's internal \"physname\" code is checked against\n\
26233 the demangler."),
26234 NULL, show_check_physname,
26235 &setdebuglist, &showdebuglist);
26236
26237 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26238 no_class, &use_deprecated_index_sections, _("\
26239 Set whether to use deprecated gdb_index sections."), _("\
26240 Show whether to use deprecated gdb_index sections."), _("\
26241 When enabled, deprecated .gdb_index sections are used anyway.\n\
26242 Normally they are ignored either because of a missing feature or\n\
26243 performance issue.\n\
26244 Warning: This option must be enabled before gdb reads the file."),
26245 NULL,
26246 NULL,
26247 &setlist, &showlist);
26248
26249 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26250 &dwarf2_locexpr_funcs);
26251 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26252 &dwarf2_loclist_funcs);
26253
26254 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26255 &dwarf2_block_frame_base_locexpr_funcs);
26256 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26257 &dwarf2_block_frame_base_loclist_funcs);
26258
26259 #if GDB_SELF_TEST
26260 selftests::register_test ("dw2_expand_symtabs_matching",
26261 selftests::dw2_expand_symtabs_matching::run_test);
26262 #endif
26263 }
This page took 0.537913 seconds and 3 git commands to generate.