Change section_offsets to a std::vector
[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 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1343 but this would require a corresponding change in unpack_field_as_long
1344 and friends. */
1345 static int bits_per_byte = 8;
1346
1347 /* When reading a variant or variant part, we track a bit more
1348 information about the field, and store it in an object of this
1349 type. */
1350
1351 struct variant_field
1352 {
1353 /* If we see a DW_TAG_variant, then this will be the discriminant
1354 value. */
1355 ULONGEST discriminant_value;
1356 /* If we see a DW_TAG_variant, then this will be set if this is the
1357 default branch. */
1358 bool default_branch;
1359 /* While reading a DW_TAG_variant_part, this will be set if this
1360 field is the discriminant. */
1361 bool is_discriminant;
1362 };
1363
1364 struct nextfield
1365 {
1366 int accessibility = 0;
1367 int virtuality = 0;
1368 /* Extra information to describe a variant or variant part. */
1369 struct variant_field variant {};
1370 struct field field {};
1371 };
1372
1373 struct fnfieldlist
1374 {
1375 const char *name = nullptr;
1376 std::vector<struct fn_field> fnfields;
1377 };
1378
1379 /* The routines that read and process dies for a C struct or C++ class
1380 pass lists of data member fields and lists of member function fields
1381 in an instance of a field_info structure, as defined below. */
1382 struct field_info
1383 {
1384 /* List of data member and baseclasses fields. */
1385 std::vector<struct nextfield> fields;
1386 std::vector<struct nextfield> baseclasses;
1387
1388 /* Number of fields (including baseclasses). */
1389 int nfields = 0;
1390
1391 /* Set if the accessibility of one of the fields is not public. */
1392 int non_public_fields = 0;
1393
1394 /* Member function fieldlist array, contains name of possibly overloaded
1395 member function, number of overloaded member functions and a pointer
1396 to the head of the member function field chain. */
1397 std::vector<struct fnfieldlist> fnfieldlists;
1398
1399 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1400 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1401 std::vector<struct decl_field> typedef_field_list;
1402
1403 /* Nested types defined by this class and the number of elements in this
1404 list. */
1405 std::vector<struct decl_field> nested_types_list;
1406 };
1407
1408 /* One item on the queue of compilation units to read in full symbols
1409 for. */
1410 struct dwarf2_queue_item
1411 {
1412 struct dwarf2_per_cu_data *per_cu;
1413 enum language pretend_language;
1414 struct dwarf2_queue_item *next;
1415 };
1416
1417 /* The current queue. */
1418 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1419
1420 /* Loaded secondary compilation units are kept in memory until they
1421 have not been referenced for the processing of this many
1422 compilation units. Set this to zero to disable caching. Cache
1423 sizes of up to at least twenty will improve startup time for
1424 typical inter-CU-reference binaries, at an obvious memory cost. */
1425 static int dwarf_max_cache_age = 5;
1426 static void
1427 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1428 struct cmd_list_element *c, const char *value)
1429 {
1430 fprintf_filtered (file, _("The upper bound on the age of cached "
1431 "DWARF compilation units is %s.\n"),
1432 value);
1433 }
1434 \f
1435 /* local function prototypes */
1436
1437 static const char *get_section_name (const struct dwarf2_section_info *);
1438
1439 static const char *get_section_file_name (const struct dwarf2_section_info *);
1440
1441 static void dwarf2_find_base_address (struct die_info *die,
1442 struct dwarf2_cu *cu);
1443
1444 static struct partial_symtab *create_partial_symtab
1445 (struct dwarf2_per_cu_data *per_cu, const char *name);
1446
1447 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1448 const gdb_byte *info_ptr,
1449 struct die_info *type_unit_die,
1450 int has_children, void *data);
1451
1452 static void dwarf2_build_psymtabs_hard
1453 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1454
1455 static void scan_partial_symbols (struct partial_die_info *,
1456 CORE_ADDR *, CORE_ADDR *,
1457 int, struct dwarf2_cu *);
1458
1459 static void add_partial_symbol (struct partial_die_info *,
1460 struct dwarf2_cu *);
1461
1462 static void add_partial_namespace (struct partial_die_info *pdi,
1463 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1464 int set_addrmap, struct dwarf2_cu *cu);
1465
1466 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1467 CORE_ADDR *highpc, int set_addrmap,
1468 struct dwarf2_cu *cu);
1469
1470 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1471 struct dwarf2_cu *cu);
1472
1473 static void add_partial_subprogram (struct partial_die_info *pdi,
1474 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1475 int need_pc, struct dwarf2_cu *cu);
1476
1477 static void dwarf2_read_symtab (struct partial_symtab *,
1478 struct objfile *);
1479
1480 static void psymtab_to_symtab_1 (struct partial_symtab *);
1481
1482 static abbrev_table_up abbrev_table_read_table
1483 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1484 sect_offset);
1485
1486 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1487
1488 static struct partial_die_info *load_partial_dies
1489 (const struct die_reader_specs *, const gdb_byte *, int);
1490
1491 /* A pair of partial_die_info and compilation unit. */
1492 struct cu_partial_die_info
1493 {
1494 /* The compilation unit of the partial_die_info. */
1495 struct dwarf2_cu *cu;
1496 /* A partial_die_info. */
1497 struct partial_die_info *pdi;
1498
1499 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1500 : cu (cu),
1501 pdi (pdi)
1502 { /* Nothing. */ }
1503
1504 private:
1505 cu_partial_die_info () = delete;
1506 };
1507
1508 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1509 struct dwarf2_cu *);
1510
1511 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1512 struct attribute *, struct attr_abbrev *,
1513 const gdb_byte *);
1514
1515 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1516
1517 static int read_1_signed_byte (bfd *, const gdb_byte *);
1518
1519 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1520
1521 /* Read the next three bytes (little-endian order) as an unsigned integer. */
1522 static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1523
1524 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1525
1526 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1527
1528 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1529 unsigned int *);
1530
1531 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1532
1533 static LONGEST read_checked_initial_length_and_offset
1534 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1535 unsigned int *, unsigned int *);
1536
1537 static LONGEST read_offset (bfd *, const gdb_byte *,
1538 const struct comp_unit_head *,
1539 unsigned int *);
1540
1541 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1542
1543 static sect_offset read_abbrev_offset
1544 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1545 struct dwarf2_section_info *, sect_offset);
1546
1547 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1548
1549 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1550
1551 static const char *read_indirect_string
1552 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1553 const struct comp_unit_head *, unsigned int *);
1554
1555 static const char *read_indirect_line_string
1556 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1557 const struct comp_unit_head *, unsigned int *);
1558
1559 static const char *read_indirect_string_at_offset
1560 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1561 LONGEST str_offset);
1562
1563 static const char *read_indirect_string_from_dwz
1564 (struct objfile *objfile, struct dwz_file *, LONGEST);
1565
1566 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1567
1568 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1569 const gdb_byte *,
1570 unsigned int *);
1571
1572 static const char *read_str_index (const struct die_reader_specs *reader,
1573 ULONGEST str_index);
1574
1575 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1576
1577 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1578 struct dwarf2_cu *);
1579
1580 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1581 unsigned int);
1582
1583 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1584 struct dwarf2_cu *cu);
1585
1586 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1587
1588 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1589 struct dwarf2_cu *cu);
1590
1591 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1592
1593 static struct die_info *die_specification (struct die_info *die,
1594 struct dwarf2_cu **);
1595
1596 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1597 struct dwarf2_cu *cu);
1598
1599 static void dwarf_decode_lines (struct line_header *, const char *,
1600 struct dwarf2_cu *, struct partial_symtab *,
1601 CORE_ADDR, int decode_mapping);
1602
1603 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1604 const char *);
1605
1606 static struct symbol *new_symbol (struct die_info *, struct type *,
1607 struct dwarf2_cu *, struct symbol * = NULL);
1608
1609 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1610 struct dwarf2_cu *);
1611
1612 static void dwarf2_const_value_attr (const struct attribute *attr,
1613 struct type *type,
1614 const char *name,
1615 struct obstack *obstack,
1616 struct dwarf2_cu *cu, LONGEST *value,
1617 const gdb_byte **bytes,
1618 struct dwarf2_locexpr_baton **baton);
1619
1620 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1621
1622 static int need_gnat_info (struct dwarf2_cu *);
1623
1624 static struct type *die_descriptive_type (struct die_info *,
1625 struct dwarf2_cu *);
1626
1627 static void set_descriptive_type (struct type *, struct die_info *,
1628 struct dwarf2_cu *);
1629
1630 static struct type *die_containing_type (struct die_info *,
1631 struct dwarf2_cu *);
1632
1633 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1634 struct dwarf2_cu *);
1635
1636 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1637
1638 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1639
1640 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1641
1642 static char *typename_concat (struct obstack *obs, const char *prefix,
1643 const char *suffix, int physname,
1644 struct dwarf2_cu *cu);
1645
1646 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1647
1648 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1649
1650 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1651
1652 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1653
1654 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1655
1656 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1657
1658 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1659 struct dwarf2_cu *, struct partial_symtab *);
1660
1661 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1662 values. Keep the items ordered with increasing constraints compliance. */
1663 enum pc_bounds_kind
1664 {
1665 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1666 PC_BOUNDS_NOT_PRESENT,
1667
1668 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1669 were present but they do not form a valid range of PC addresses. */
1670 PC_BOUNDS_INVALID,
1671
1672 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1673 PC_BOUNDS_RANGES,
1674
1675 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1676 PC_BOUNDS_HIGH_LOW,
1677 };
1678
1679 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1680 CORE_ADDR *, CORE_ADDR *,
1681 struct dwarf2_cu *,
1682 struct partial_symtab *);
1683
1684 static void get_scope_pc_bounds (struct die_info *,
1685 CORE_ADDR *, CORE_ADDR *,
1686 struct dwarf2_cu *);
1687
1688 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1689 CORE_ADDR, struct dwarf2_cu *);
1690
1691 static void dwarf2_add_field (struct field_info *, struct die_info *,
1692 struct dwarf2_cu *);
1693
1694 static void dwarf2_attach_fields_to_type (struct field_info *,
1695 struct type *, struct dwarf2_cu *);
1696
1697 static void dwarf2_add_member_fn (struct field_info *,
1698 struct die_info *, struct type *,
1699 struct dwarf2_cu *);
1700
1701 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1702 struct type *,
1703 struct dwarf2_cu *);
1704
1705 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1706
1707 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1708
1709 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1710
1711 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1712
1713 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1714
1715 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1716
1717 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1718
1719 static struct type *read_module_type (struct die_info *die,
1720 struct dwarf2_cu *cu);
1721
1722 static const char *namespace_name (struct die_info *die,
1723 int *is_anonymous, struct dwarf2_cu *);
1724
1725 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1726
1727 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1728
1729 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1730 struct dwarf2_cu *);
1731
1732 static struct die_info *read_die_and_siblings_1
1733 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1734 struct die_info *);
1735
1736 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1737 const gdb_byte *info_ptr,
1738 const gdb_byte **new_info_ptr,
1739 struct die_info *parent);
1740
1741 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1742 struct die_info **, const gdb_byte *,
1743 int *, int);
1744
1745 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1746 struct die_info **, const gdb_byte *,
1747 int *);
1748
1749 static void process_die (struct die_info *, struct dwarf2_cu *);
1750
1751 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1752 struct obstack *);
1753
1754 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1755
1756 static const char *dwarf2_full_name (const char *name,
1757 struct die_info *die,
1758 struct dwarf2_cu *cu);
1759
1760 static const char *dwarf2_physname (const char *name, struct die_info *die,
1761 struct dwarf2_cu *cu);
1762
1763 static struct die_info *dwarf2_extension (struct die_info *die,
1764 struct dwarf2_cu **);
1765
1766 static const char *dwarf_tag_name (unsigned int);
1767
1768 static const char *dwarf_attr_name (unsigned int);
1769
1770 static const char *dwarf_unit_type_name (int unit_type);
1771
1772 static const char *dwarf_form_name (unsigned int);
1773
1774 static const char *dwarf_bool_name (unsigned int);
1775
1776 static const char *dwarf_type_encoding_name (unsigned int);
1777
1778 static struct die_info *sibling_die (struct die_info *);
1779
1780 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1781
1782 static void dump_die_for_error (struct die_info *);
1783
1784 static void dump_die_1 (struct ui_file *, int level, int max_level,
1785 struct die_info *);
1786
1787 /*static*/ void dump_die (struct die_info *, int max_level);
1788
1789 static void store_in_ref_table (struct die_info *,
1790 struct dwarf2_cu *);
1791
1792 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1793
1794 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1795
1796 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1797 const struct attribute *,
1798 struct dwarf2_cu **);
1799
1800 static struct die_info *follow_die_ref (struct die_info *,
1801 const struct attribute *,
1802 struct dwarf2_cu **);
1803
1804 static struct die_info *follow_die_sig (struct die_info *,
1805 const struct attribute *,
1806 struct dwarf2_cu **);
1807
1808 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1809 struct dwarf2_cu *);
1810
1811 static struct type *get_DW_AT_signature_type (struct die_info *,
1812 const struct attribute *,
1813 struct dwarf2_cu *);
1814
1815 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1816
1817 static void read_signatured_type (struct signatured_type *);
1818
1819 static int attr_to_dynamic_prop (const struct attribute *attr,
1820 struct die_info *die, struct dwarf2_cu *cu,
1821 struct dynamic_prop *prop, struct type *type);
1822
1823 /* memory allocation interface */
1824
1825 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1826
1827 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1828
1829 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1830
1831 static int attr_form_is_block (const struct attribute *);
1832
1833 static int attr_form_is_section_offset (const struct attribute *);
1834
1835 static int attr_form_is_constant (const struct attribute *);
1836
1837 static int attr_form_is_ref (const struct attribute *);
1838
1839 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1840 struct dwarf2_loclist_baton *baton,
1841 const struct attribute *attr);
1842
1843 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1844 struct symbol *sym,
1845 struct dwarf2_cu *cu,
1846 int is_block);
1847
1848 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1849 const gdb_byte *info_ptr,
1850 struct abbrev_info *abbrev);
1851
1852 static hashval_t partial_die_hash (const void *item);
1853
1854 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1855
1856 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1857 (sect_offset sect_off, unsigned int offset_in_dwz,
1858 struct dwarf2_per_objfile *dwarf2_per_objfile);
1859
1860 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1861 struct die_info *comp_unit_die,
1862 enum language pretend_language);
1863
1864 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1865
1866 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1867
1868 static struct type *set_die_type (struct die_info *, struct type *,
1869 struct dwarf2_cu *);
1870
1871 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1872
1873 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1874
1875 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1876 enum language);
1877
1878 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1879 enum language);
1880
1881 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1882 enum language);
1883
1884 static void dwarf2_add_dependence (struct dwarf2_cu *,
1885 struct dwarf2_per_cu_data *);
1886
1887 static void dwarf2_mark (struct dwarf2_cu *);
1888
1889 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1890
1891 static struct type *get_die_type_at_offset (sect_offset,
1892 struct dwarf2_per_cu_data *);
1893
1894 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1895
1896 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1897 enum language pretend_language);
1898
1899 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1900
1901 static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1902 static struct type *dwarf2_per_cu_addr_sized_int_type
1903 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1904 static struct type *dwarf2_per_cu_int_type
1905 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1906 bool unsigned_p);
1907
1908 /* Class, the destructor of which frees all allocated queue entries. This
1909 will only have work to do if an error was thrown while processing the
1910 dwarf. If no error was thrown then the queue entries should have all
1911 been processed, and freed, as we went along. */
1912
1913 class dwarf2_queue_guard
1914 {
1915 public:
1916 dwarf2_queue_guard () = default;
1917
1918 /* Free any entries remaining on the queue. There should only be
1919 entries left if we hit an error while processing the dwarf. */
1920 ~dwarf2_queue_guard ()
1921 {
1922 struct dwarf2_queue_item *item, *last;
1923
1924 item = dwarf2_queue;
1925 while (item)
1926 {
1927 /* Anything still marked queued is likely to be in an
1928 inconsistent state, so discard it. */
1929 if (item->per_cu->queued)
1930 {
1931 if (item->per_cu->cu != NULL)
1932 free_one_cached_comp_unit (item->per_cu);
1933 item->per_cu->queued = 0;
1934 }
1935
1936 last = item;
1937 item = item->next;
1938 xfree (last);
1939 }
1940
1941 dwarf2_queue = dwarf2_queue_tail = NULL;
1942 }
1943 };
1944
1945 /* The return type of find_file_and_directory. Note, the enclosed
1946 string pointers are only valid while this object is valid. */
1947
1948 struct file_and_directory
1949 {
1950 /* The filename. This is never NULL. */
1951 const char *name;
1952
1953 /* The compilation directory. NULL if not known. If we needed to
1954 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1955 points directly to the DW_AT_comp_dir string attribute owned by
1956 the obstack that owns the DIE. */
1957 const char *comp_dir;
1958
1959 /* If we needed to build a new string for comp_dir, this is what
1960 owns the storage. */
1961 std::string comp_dir_storage;
1962 };
1963
1964 static file_and_directory find_file_and_directory (struct die_info *die,
1965 struct dwarf2_cu *cu);
1966
1967 static char *file_full_name (int file, struct line_header *lh,
1968 const char *comp_dir);
1969
1970 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1971 enum class rcuh_kind { COMPILE, TYPE };
1972
1973 static const gdb_byte *read_and_check_comp_unit_head
1974 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1975 struct comp_unit_head *header,
1976 struct dwarf2_section_info *section,
1977 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1978 rcuh_kind section_kind);
1979
1980 static void init_cutu_and_read_dies
1981 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1982 int use_existing_cu, int keep, bool skip_partial,
1983 die_reader_func_ftype *die_reader_func, void *data);
1984
1985 static void init_cutu_and_read_dies_simple
1986 (struct dwarf2_per_cu_data *this_cu,
1987 die_reader_func_ftype *die_reader_func, void *data);
1988
1989 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1990
1991 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1992
1993 static struct dwo_unit *lookup_dwo_unit_in_dwp
1994 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1995 struct dwp_file *dwp_file, const char *comp_dir,
1996 ULONGEST signature, int is_debug_types);
1997
1998 static struct dwp_file *get_dwp_file
1999 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2000
2001 static struct dwo_unit *lookup_dwo_comp_unit
2002 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2003
2004 static struct dwo_unit *lookup_dwo_type_unit
2005 (struct signatured_type *, const char *, const char *);
2006
2007 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2008
2009 /* A unique pointer to a dwo_file. */
2010
2011 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
2012
2013 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2014
2015 static void check_producer (struct dwarf2_cu *cu);
2016
2017 static void free_line_header_voidp (void *arg);
2018 \f
2019 /* Various complaints about symbol reading that don't abort the process. */
2020
2021 static void
2022 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2023 {
2024 complaint (_("statement list doesn't fit in .debug_line section"));
2025 }
2026
2027 static void
2028 dwarf2_debug_line_missing_file_complaint (void)
2029 {
2030 complaint (_(".debug_line section has line data without a file"));
2031 }
2032
2033 static void
2034 dwarf2_debug_line_missing_end_sequence_complaint (void)
2035 {
2036 complaint (_(".debug_line section has line "
2037 "program sequence without an end"));
2038 }
2039
2040 static void
2041 dwarf2_complex_location_expr_complaint (void)
2042 {
2043 complaint (_("location expression too complex"));
2044 }
2045
2046 static void
2047 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2048 int arg3)
2049 {
2050 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
2051 arg1, arg2, arg3);
2052 }
2053
2054 static void
2055 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2056 {
2057 complaint (_("debug info runs off end of %s section"
2058 " [in module %s]"),
2059 get_section_name (section),
2060 get_section_file_name (section));
2061 }
2062
2063 static void
2064 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2065 {
2066 complaint (_("macro debug info contains a "
2067 "malformed macro definition:\n`%s'"),
2068 arg1);
2069 }
2070
2071 static void
2072 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2073 {
2074 complaint (_("invalid attribute class or form for '%s' in '%s'"),
2075 arg1, arg2);
2076 }
2077
2078 /* Hash function for line_header_hash. */
2079
2080 static hashval_t
2081 line_header_hash (const struct line_header *ofs)
2082 {
2083 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2084 }
2085
2086 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2087
2088 static hashval_t
2089 line_header_hash_voidp (const void *item)
2090 {
2091 const struct line_header *ofs = (const struct line_header *) item;
2092
2093 return line_header_hash (ofs);
2094 }
2095
2096 /* Equality function for line_header_hash. */
2097
2098 static int
2099 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2100 {
2101 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2102 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2103
2104 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2105 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2106 }
2107
2108 \f
2109
2110 /* Read the given attribute value as an address, taking the attribute's
2111 form into account. */
2112
2113 static CORE_ADDR
2114 attr_value_as_address (struct attribute *attr)
2115 {
2116 CORE_ADDR addr;
2117
2118 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2119 && attr->form != DW_FORM_GNU_addr_index)
2120 {
2121 /* Aside from a few clearly defined exceptions, attributes that
2122 contain an address must always be in DW_FORM_addr form.
2123 Unfortunately, some compilers happen to be violating this
2124 requirement by encoding addresses using other forms, such
2125 as DW_FORM_data4 for example. For those broken compilers,
2126 we try to do our best, without any guarantee of success,
2127 to interpret the address correctly. It would also be nice
2128 to generate a complaint, but that would require us to maintain
2129 a list of legitimate cases where a non-address form is allowed,
2130 as well as update callers to pass in at least the CU's DWARF
2131 version. This is more overhead than what we're willing to
2132 expand for a pretty rare case. */
2133 addr = DW_UNSND (attr);
2134 }
2135 else
2136 addr = DW_ADDR (attr);
2137
2138 return addr;
2139 }
2140
2141 /* See declaration. */
2142
2143 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2144 const dwarf2_debug_sections *names,
2145 bool can_copy_)
2146 : objfile (objfile_),
2147 can_copy (can_copy_)
2148 {
2149 if (names == NULL)
2150 names = &dwarf2_elf_names;
2151
2152 bfd *obfd = objfile->obfd;
2153
2154 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2155 locate_sections (obfd, sec, *names);
2156 }
2157
2158 dwarf2_per_objfile::~dwarf2_per_objfile ()
2159 {
2160 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2161 free_cached_comp_units ();
2162
2163 if (quick_file_names_table)
2164 htab_delete (quick_file_names_table);
2165
2166 if (line_header_hash)
2167 htab_delete (line_header_hash);
2168
2169 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2170 per_cu->imported_symtabs_free ();
2171
2172 for (signatured_type *sig_type : all_type_units)
2173 sig_type->per_cu.imported_symtabs_free ();
2174
2175 /* Everything else should be on the objfile obstack. */
2176 }
2177
2178 /* See declaration. */
2179
2180 void
2181 dwarf2_per_objfile::free_cached_comp_units ()
2182 {
2183 dwarf2_per_cu_data *per_cu = read_in_chain;
2184 dwarf2_per_cu_data **last_chain = &read_in_chain;
2185 while (per_cu != NULL)
2186 {
2187 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2188
2189 delete per_cu->cu;
2190 *last_chain = next_cu;
2191 per_cu = next_cu;
2192 }
2193 }
2194
2195 /* A helper class that calls free_cached_comp_units on
2196 destruction. */
2197
2198 class free_cached_comp_units
2199 {
2200 public:
2201
2202 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2203 : m_per_objfile (per_objfile)
2204 {
2205 }
2206
2207 ~free_cached_comp_units ()
2208 {
2209 m_per_objfile->free_cached_comp_units ();
2210 }
2211
2212 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2213
2214 private:
2215
2216 dwarf2_per_objfile *m_per_objfile;
2217 };
2218
2219 /* Try to locate the sections we need for DWARF 2 debugging
2220 information and return true if we have enough to do something.
2221 NAMES points to the dwarf2 section names, or is NULL if the standard
2222 ELF names are used. CAN_COPY is true for formats where symbol
2223 interposition is possible and so symbol values must follow copy
2224 relocation rules. */
2225
2226 int
2227 dwarf2_has_info (struct objfile *objfile,
2228 const struct dwarf2_debug_sections *names,
2229 bool can_copy)
2230 {
2231 if (objfile->flags & OBJF_READNEVER)
2232 return 0;
2233
2234 struct dwarf2_per_objfile *dwarf2_per_objfile
2235 = get_dwarf2_per_objfile (objfile);
2236
2237 if (dwarf2_per_objfile == NULL)
2238 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2239 names,
2240 can_copy);
2241
2242 return (!dwarf2_per_objfile->info.is_virtual
2243 && dwarf2_per_objfile->info.s.section != NULL
2244 && !dwarf2_per_objfile->abbrev.is_virtual
2245 && dwarf2_per_objfile->abbrev.s.section != NULL);
2246 }
2247
2248 /* Return the containing section of virtual section SECTION. */
2249
2250 static struct dwarf2_section_info *
2251 get_containing_section (const struct dwarf2_section_info *section)
2252 {
2253 gdb_assert (section->is_virtual);
2254 return section->s.containing_section;
2255 }
2256
2257 /* Return the bfd owner of SECTION. */
2258
2259 static struct bfd *
2260 get_section_bfd_owner (const struct dwarf2_section_info *section)
2261 {
2262 if (section->is_virtual)
2263 {
2264 section = get_containing_section (section);
2265 gdb_assert (!section->is_virtual);
2266 }
2267 return section->s.section->owner;
2268 }
2269
2270 /* Return the bfd section of SECTION.
2271 Returns NULL if the section is not present. */
2272
2273 static asection *
2274 get_section_bfd_section (const struct dwarf2_section_info *section)
2275 {
2276 if (section->is_virtual)
2277 {
2278 section = get_containing_section (section);
2279 gdb_assert (!section->is_virtual);
2280 }
2281 return section->s.section;
2282 }
2283
2284 /* Return the name of SECTION. */
2285
2286 static const char *
2287 get_section_name (const struct dwarf2_section_info *section)
2288 {
2289 asection *sectp = get_section_bfd_section (section);
2290
2291 gdb_assert (sectp != NULL);
2292 return bfd_section_name (sectp);
2293 }
2294
2295 /* Return the name of the file SECTION is in. */
2296
2297 static const char *
2298 get_section_file_name (const struct dwarf2_section_info *section)
2299 {
2300 bfd *abfd = get_section_bfd_owner (section);
2301
2302 return bfd_get_filename (abfd);
2303 }
2304
2305 /* Return the id of SECTION.
2306 Returns 0 if SECTION doesn't exist. */
2307
2308 static int
2309 get_section_id (const struct dwarf2_section_info *section)
2310 {
2311 asection *sectp = get_section_bfd_section (section);
2312
2313 if (sectp == NULL)
2314 return 0;
2315 return sectp->id;
2316 }
2317
2318 /* Return the flags of SECTION.
2319 SECTION (or containing section if this is a virtual section) must exist. */
2320
2321 static int
2322 get_section_flags (const struct dwarf2_section_info *section)
2323 {
2324 asection *sectp = get_section_bfd_section (section);
2325
2326 gdb_assert (sectp != NULL);
2327 return bfd_section_flags (sectp);
2328 }
2329
2330 /* When loading sections, we look either for uncompressed section or for
2331 compressed section names. */
2332
2333 static int
2334 section_is_p (const char *section_name,
2335 const struct dwarf2_section_names *names)
2336 {
2337 if (names->normal != NULL
2338 && strcmp (section_name, names->normal) == 0)
2339 return 1;
2340 if (names->compressed != NULL
2341 && strcmp (section_name, names->compressed) == 0)
2342 return 1;
2343 return 0;
2344 }
2345
2346 /* See declaration. */
2347
2348 void
2349 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2350 const dwarf2_debug_sections &names)
2351 {
2352 flagword aflag = bfd_section_flags (sectp);
2353
2354 if ((aflag & SEC_HAS_CONTENTS) == 0)
2355 {
2356 }
2357 else if (elf_section_data (sectp)->this_hdr.sh_size
2358 > bfd_get_file_size (abfd))
2359 {
2360 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2361 warning (_("Discarding section %s which has a section size (%s"
2362 ") larger than the file size [in module %s]"),
2363 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2364 bfd_get_filename (abfd));
2365 }
2366 else if (section_is_p (sectp->name, &names.info))
2367 {
2368 this->info.s.section = sectp;
2369 this->info.size = bfd_section_size (sectp);
2370 }
2371 else if (section_is_p (sectp->name, &names.abbrev))
2372 {
2373 this->abbrev.s.section = sectp;
2374 this->abbrev.size = bfd_section_size (sectp);
2375 }
2376 else if (section_is_p (sectp->name, &names.line))
2377 {
2378 this->line.s.section = sectp;
2379 this->line.size = bfd_section_size (sectp);
2380 }
2381 else if (section_is_p (sectp->name, &names.loc))
2382 {
2383 this->loc.s.section = sectp;
2384 this->loc.size = bfd_section_size (sectp);
2385 }
2386 else if (section_is_p (sectp->name, &names.loclists))
2387 {
2388 this->loclists.s.section = sectp;
2389 this->loclists.size = bfd_section_size (sectp);
2390 }
2391 else if (section_is_p (sectp->name, &names.macinfo))
2392 {
2393 this->macinfo.s.section = sectp;
2394 this->macinfo.size = bfd_section_size (sectp);
2395 }
2396 else if (section_is_p (sectp->name, &names.macro))
2397 {
2398 this->macro.s.section = sectp;
2399 this->macro.size = bfd_section_size (sectp);
2400 }
2401 else if (section_is_p (sectp->name, &names.str))
2402 {
2403 this->str.s.section = sectp;
2404 this->str.size = bfd_section_size (sectp);
2405 }
2406 else if (section_is_p (sectp->name, &names.line_str))
2407 {
2408 this->line_str.s.section = sectp;
2409 this->line_str.size = bfd_section_size (sectp);
2410 }
2411 else if (section_is_p (sectp->name, &names.addr))
2412 {
2413 this->addr.s.section = sectp;
2414 this->addr.size = bfd_section_size (sectp);
2415 }
2416 else if (section_is_p (sectp->name, &names.frame))
2417 {
2418 this->frame.s.section = sectp;
2419 this->frame.size = bfd_section_size (sectp);
2420 }
2421 else if (section_is_p (sectp->name, &names.eh_frame))
2422 {
2423 this->eh_frame.s.section = sectp;
2424 this->eh_frame.size = bfd_section_size (sectp);
2425 }
2426 else if (section_is_p (sectp->name, &names.ranges))
2427 {
2428 this->ranges.s.section = sectp;
2429 this->ranges.size = bfd_section_size (sectp);
2430 }
2431 else if (section_is_p (sectp->name, &names.rnglists))
2432 {
2433 this->rnglists.s.section = sectp;
2434 this->rnglists.size = bfd_section_size (sectp);
2435 }
2436 else if (section_is_p (sectp->name, &names.types))
2437 {
2438 struct dwarf2_section_info type_section;
2439
2440 memset (&type_section, 0, sizeof (type_section));
2441 type_section.s.section = sectp;
2442 type_section.size = bfd_section_size (sectp);
2443
2444 this->types.push_back (type_section);
2445 }
2446 else if (section_is_p (sectp->name, &names.gdb_index))
2447 {
2448 this->gdb_index.s.section = sectp;
2449 this->gdb_index.size = bfd_section_size (sectp);
2450 }
2451 else if (section_is_p (sectp->name, &names.debug_names))
2452 {
2453 this->debug_names.s.section = sectp;
2454 this->debug_names.size = bfd_section_size (sectp);
2455 }
2456 else if (section_is_p (sectp->name, &names.debug_aranges))
2457 {
2458 this->debug_aranges.s.section = sectp;
2459 this->debug_aranges.size = bfd_section_size (sectp);
2460 }
2461
2462 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2463 && bfd_section_vma (sectp) == 0)
2464 this->has_section_at_zero = true;
2465 }
2466
2467 /* A helper function that decides whether a section is empty,
2468 or not present. */
2469
2470 static int
2471 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2472 {
2473 if (section->is_virtual)
2474 return section->size == 0;
2475 return section->s.section == NULL || section->size == 0;
2476 }
2477
2478 /* See dwarf2read.h. */
2479
2480 void
2481 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2482 {
2483 asection *sectp;
2484 bfd *abfd;
2485 gdb_byte *buf, *retbuf;
2486
2487 if (info->readin)
2488 return;
2489 info->buffer = NULL;
2490 info->readin = true;
2491
2492 if (dwarf2_section_empty_p (info))
2493 return;
2494
2495 sectp = get_section_bfd_section (info);
2496
2497 /* If this is a virtual section we need to read in the real one first. */
2498 if (info->is_virtual)
2499 {
2500 struct dwarf2_section_info *containing_section =
2501 get_containing_section (info);
2502
2503 gdb_assert (sectp != NULL);
2504 if ((sectp->flags & SEC_RELOC) != 0)
2505 {
2506 error (_("Dwarf Error: DWP format V2 with relocations is not"
2507 " supported in section %s [in module %s]"),
2508 get_section_name (info), get_section_file_name (info));
2509 }
2510 dwarf2_read_section (objfile, containing_section);
2511 /* Other code should have already caught virtual sections that don't
2512 fit. */
2513 gdb_assert (info->virtual_offset + info->size
2514 <= containing_section->size);
2515 /* If the real section is empty or there was a problem reading the
2516 section we shouldn't get here. */
2517 gdb_assert (containing_section->buffer != NULL);
2518 info->buffer = containing_section->buffer + info->virtual_offset;
2519 return;
2520 }
2521
2522 /* If the section has relocations, we must read it ourselves.
2523 Otherwise we attach it to the BFD. */
2524 if ((sectp->flags & SEC_RELOC) == 0)
2525 {
2526 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2527 return;
2528 }
2529
2530 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2531 info->buffer = buf;
2532
2533 /* When debugging .o files, we may need to apply relocations; see
2534 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2535 We never compress sections in .o files, so we only need to
2536 try this when the section is not compressed. */
2537 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2538 if (retbuf != NULL)
2539 {
2540 info->buffer = retbuf;
2541 return;
2542 }
2543
2544 abfd = get_section_bfd_owner (info);
2545 gdb_assert (abfd != NULL);
2546
2547 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2548 || bfd_bread (buf, info->size, abfd) != info->size)
2549 {
2550 error (_("Dwarf Error: Can't read DWARF data"
2551 " in section %s [in module %s]"),
2552 bfd_section_name (sectp), bfd_get_filename (abfd));
2553 }
2554 }
2555
2556 /* A helper function that returns the size of a section in a safe way.
2557 If you are positive that the section has been read before using the
2558 size, then it is safe to refer to the dwarf2_section_info object's
2559 "size" field directly. In other cases, you must call this
2560 function, because for compressed sections the size field is not set
2561 correctly until the section has been read. */
2562
2563 static bfd_size_type
2564 dwarf2_section_size (struct objfile *objfile,
2565 struct dwarf2_section_info *info)
2566 {
2567 if (!info->readin)
2568 dwarf2_read_section (objfile, info);
2569 return info->size;
2570 }
2571
2572 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2573 SECTION_NAME. */
2574
2575 void
2576 dwarf2_get_section_info (struct objfile *objfile,
2577 enum dwarf2_section_enum sect,
2578 asection **sectp, const gdb_byte **bufp,
2579 bfd_size_type *sizep)
2580 {
2581 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2582 struct dwarf2_section_info *info;
2583
2584 /* We may see an objfile without any DWARF, in which case we just
2585 return nothing. */
2586 if (data == NULL)
2587 {
2588 *sectp = NULL;
2589 *bufp = NULL;
2590 *sizep = 0;
2591 return;
2592 }
2593 switch (sect)
2594 {
2595 case DWARF2_DEBUG_FRAME:
2596 info = &data->frame;
2597 break;
2598 case DWARF2_EH_FRAME:
2599 info = &data->eh_frame;
2600 break;
2601 default:
2602 gdb_assert_not_reached ("unexpected section");
2603 }
2604
2605 dwarf2_read_section (objfile, info);
2606
2607 *sectp = get_section_bfd_section (info);
2608 *bufp = info->buffer;
2609 *sizep = info->size;
2610 }
2611
2612 /* A helper function to find the sections for a .dwz file. */
2613
2614 static void
2615 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2616 {
2617 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2618
2619 /* Note that we only support the standard ELF names, because .dwz
2620 is ELF-only (at the time of writing). */
2621 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2622 {
2623 dwz_file->abbrev.s.section = sectp;
2624 dwz_file->abbrev.size = bfd_section_size (sectp);
2625 }
2626 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2627 {
2628 dwz_file->info.s.section = sectp;
2629 dwz_file->info.size = bfd_section_size (sectp);
2630 }
2631 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2632 {
2633 dwz_file->str.s.section = sectp;
2634 dwz_file->str.size = bfd_section_size (sectp);
2635 }
2636 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2637 {
2638 dwz_file->line.s.section = sectp;
2639 dwz_file->line.size = bfd_section_size (sectp);
2640 }
2641 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2642 {
2643 dwz_file->macro.s.section = sectp;
2644 dwz_file->macro.size = bfd_section_size (sectp);
2645 }
2646 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2647 {
2648 dwz_file->gdb_index.s.section = sectp;
2649 dwz_file->gdb_index.size = bfd_section_size (sectp);
2650 }
2651 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2652 {
2653 dwz_file->debug_names.s.section = sectp;
2654 dwz_file->debug_names.size = bfd_section_size (sectp);
2655 }
2656 }
2657
2658 /* See dwarf2read.h. */
2659
2660 struct dwz_file *
2661 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2662 {
2663 const char *filename;
2664 bfd_size_type buildid_len_arg;
2665 size_t buildid_len;
2666 bfd_byte *buildid;
2667
2668 if (dwarf2_per_objfile->dwz_file != NULL)
2669 return dwarf2_per_objfile->dwz_file.get ();
2670
2671 bfd_set_error (bfd_error_no_error);
2672 gdb::unique_xmalloc_ptr<char> data
2673 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2674 &buildid_len_arg, &buildid));
2675 if (data == NULL)
2676 {
2677 if (bfd_get_error () == bfd_error_no_error)
2678 return NULL;
2679 error (_("could not read '.gnu_debugaltlink' section: %s"),
2680 bfd_errmsg (bfd_get_error ()));
2681 }
2682
2683 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2684
2685 buildid_len = (size_t) buildid_len_arg;
2686
2687 filename = data.get ();
2688
2689 std::string abs_storage;
2690 if (!IS_ABSOLUTE_PATH (filename))
2691 {
2692 gdb::unique_xmalloc_ptr<char> abs
2693 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2694
2695 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2696 filename = abs_storage.c_str ();
2697 }
2698
2699 /* First try the file name given in the section. If that doesn't
2700 work, try to use the build-id instead. */
2701 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2702 if (dwz_bfd != NULL)
2703 {
2704 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2705 dwz_bfd.reset (nullptr);
2706 }
2707
2708 if (dwz_bfd == NULL)
2709 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2710
2711 if (dwz_bfd == NULL)
2712 error (_("could not find '.gnu_debugaltlink' file for %s"),
2713 objfile_name (dwarf2_per_objfile->objfile));
2714
2715 std::unique_ptr<struct dwz_file> result
2716 (new struct dwz_file (std::move (dwz_bfd)));
2717
2718 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2719 result.get ());
2720
2721 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2722 result->dwz_bfd.get ());
2723 dwarf2_per_objfile->dwz_file = std::move (result);
2724 return dwarf2_per_objfile->dwz_file.get ();
2725 }
2726 \f
2727 /* DWARF quick_symbols_functions support. */
2728
2729 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2730 unique line tables, so we maintain a separate table of all .debug_line
2731 derived entries to support the sharing.
2732 All the quick functions need is the list of file names. We discard the
2733 line_header when we're done and don't need to record it here. */
2734 struct quick_file_names
2735 {
2736 /* The data used to construct the hash key. */
2737 struct stmt_list_hash hash;
2738
2739 /* The number of entries in file_names, real_names. */
2740 unsigned int num_file_names;
2741
2742 /* The file names from the line table, after being run through
2743 file_full_name. */
2744 const char **file_names;
2745
2746 /* The file names from the line table after being run through
2747 gdb_realpath. These are computed lazily. */
2748 const char **real_names;
2749 };
2750
2751 /* When using the index (and thus not using psymtabs), each CU has an
2752 object of this type. This is used to hold information needed by
2753 the various "quick" methods. */
2754 struct dwarf2_per_cu_quick_data
2755 {
2756 /* The file table. This can be NULL if there was no file table
2757 or it's currently not read in.
2758 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2759 struct quick_file_names *file_names;
2760
2761 /* The corresponding symbol table. This is NULL if symbols for this
2762 CU have not yet been read. */
2763 struct compunit_symtab *compunit_symtab;
2764
2765 /* A temporary mark bit used when iterating over all CUs in
2766 expand_symtabs_matching. */
2767 unsigned int mark : 1;
2768
2769 /* True if we've tried to read the file table and found there isn't one.
2770 There will be no point in trying to read it again next time. */
2771 unsigned int no_file_data : 1;
2772 };
2773
2774 /* Utility hash function for a stmt_list_hash. */
2775
2776 static hashval_t
2777 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2778 {
2779 hashval_t v = 0;
2780
2781 if (stmt_list_hash->dwo_unit != NULL)
2782 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2783 v += to_underlying (stmt_list_hash->line_sect_off);
2784 return v;
2785 }
2786
2787 /* Utility equality function for a stmt_list_hash. */
2788
2789 static int
2790 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2791 const struct stmt_list_hash *rhs)
2792 {
2793 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2794 return 0;
2795 if (lhs->dwo_unit != NULL
2796 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2797 return 0;
2798
2799 return lhs->line_sect_off == rhs->line_sect_off;
2800 }
2801
2802 /* Hash function for a quick_file_names. */
2803
2804 static hashval_t
2805 hash_file_name_entry (const void *e)
2806 {
2807 const struct quick_file_names *file_data
2808 = (const struct quick_file_names *) e;
2809
2810 return hash_stmt_list_entry (&file_data->hash);
2811 }
2812
2813 /* Equality function for a quick_file_names. */
2814
2815 static int
2816 eq_file_name_entry (const void *a, const void *b)
2817 {
2818 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2819 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2820
2821 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2822 }
2823
2824 /* Delete function for a quick_file_names. */
2825
2826 static void
2827 delete_file_name_entry (void *e)
2828 {
2829 struct quick_file_names *file_data = (struct quick_file_names *) e;
2830 int i;
2831
2832 for (i = 0; i < file_data->num_file_names; ++i)
2833 {
2834 xfree ((void*) file_data->file_names[i]);
2835 if (file_data->real_names)
2836 xfree ((void*) file_data->real_names[i]);
2837 }
2838
2839 /* The space for the struct itself lives on objfile_obstack,
2840 so we don't free it here. */
2841 }
2842
2843 /* Create a quick_file_names hash table. */
2844
2845 static htab_t
2846 create_quick_file_names_table (unsigned int nr_initial_entries)
2847 {
2848 return htab_create_alloc (nr_initial_entries,
2849 hash_file_name_entry, eq_file_name_entry,
2850 delete_file_name_entry, xcalloc, xfree);
2851 }
2852
2853 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2854 have to be created afterwards. You should call age_cached_comp_units after
2855 processing PER_CU->CU. dw2_setup must have been already called. */
2856
2857 static void
2858 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2859 {
2860 if (per_cu->is_debug_types)
2861 load_full_type_unit (per_cu);
2862 else
2863 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2864
2865 if (per_cu->cu == NULL)
2866 return; /* Dummy CU. */
2867
2868 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2869 }
2870
2871 /* Read in the symbols for PER_CU. */
2872
2873 static void
2874 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2875 {
2876 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2877
2878 /* Skip type_unit_groups, reading the type units they contain
2879 is handled elsewhere. */
2880 if (IS_TYPE_UNIT_GROUP (per_cu))
2881 return;
2882
2883 /* The destructor of dwarf2_queue_guard frees any entries left on
2884 the queue. After this point we're guaranteed to leave this function
2885 with the dwarf queue empty. */
2886 dwarf2_queue_guard q_guard;
2887
2888 if (dwarf2_per_objfile->using_index
2889 ? per_cu->v.quick->compunit_symtab == NULL
2890 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2891 {
2892 queue_comp_unit (per_cu, language_minimal);
2893 load_cu (per_cu, skip_partial);
2894
2895 /* If we just loaded a CU from a DWO, and we're working with an index
2896 that may badly handle TUs, load all the TUs in that DWO as well.
2897 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2898 if (!per_cu->is_debug_types
2899 && per_cu->cu != NULL
2900 && per_cu->cu->dwo_unit != NULL
2901 && dwarf2_per_objfile->index_table != NULL
2902 && dwarf2_per_objfile->index_table->version <= 7
2903 /* DWP files aren't supported yet. */
2904 && get_dwp_file (dwarf2_per_objfile) == NULL)
2905 queue_and_load_all_dwo_tus (per_cu);
2906 }
2907
2908 process_queue (dwarf2_per_objfile);
2909
2910 /* Age the cache, releasing compilation units that have not
2911 been used recently. */
2912 age_cached_comp_units (dwarf2_per_objfile);
2913 }
2914
2915 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2916 the objfile from which this CU came. Returns the resulting symbol
2917 table. */
2918
2919 static struct compunit_symtab *
2920 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2921 {
2922 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2923
2924 gdb_assert (dwarf2_per_objfile->using_index);
2925 if (!per_cu->v.quick->compunit_symtab)
2926 {
2927 free_cached_comp_units freer (dwarf2_per_objfile);
2928 scoped_restore decrementer = increment_reading_symtab ();
2929 dw2_do_instantiate_symtab (per_cu, skip_partial);
2930 process_cu_includes (dwarf2_per_objfile);
2931 }
2932
2933 return per_cu->v.quick->compunit_symtab;
2934 }
2935
2936 /* See declaration. */
2937
2938 dwarf2_per_cu_data *
2939 dwarf2_per_objfile::get_cutu (int index)
2940 {
2941 if (index >= this->all_comp_units.size ())
2942 {
2943 index -= this->all_comp_units.size ();
2944 gdb_assert (index < this->all_type_units.size ());
2945 return &this->all_type_units[index]->per_cu;
2946 }
2947
2948 return this->all_comp_units[index];
2949 }
2950
2951 /* See declaration. */
2952
2953 dwarf2_per_cu_data *
2954 dwarf2_per_objfile::get_cu (int index)
2955 {
2956 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2957
2958 return this->all_comp_units[index];
2959 }
2960
2961 /* See declaration. */
2962
2963 signatured_type *
2964 dwarf2_per_objfile::get_tu (int index)
2965 {
2966 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2967
2968 return this->all_type_units[index];
2969 }
2970
2971 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2972 objfile_obstack, and constructed with the specified field
2973 values. */
2974
2975 static dwarf2_per_cu_data *
2976 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2977 struct dwarf2_section_info *section,
2978 int is_dwz,
2979 sect_offset sect_off, ULONGEST length)
2980 {
2981 struct objfile *objfile = dwarf2_per_objfile->objfile;
2982 dwarf2_per_cu_data *the_cu
2983 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2984 struct dwarf2_per_cu_data);
2985 the_cu->sect_off = sect_off;
2986 the_cu->length = length;
2987 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2988 the_cu->section = section;
2989 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2990 struct dwarf2_per_cu_quick_data);
2991 the_cu->is_dwz = is_dwz;
2992 return the_cu;
2993 }
2994
2995 /* A helper for create_cus_from_index that handles a given list of
2996 CUs. */
2997
2998 static void
2999 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3000 const gdb_byte *cu_list, offset_type n_elements,
3001 struct dwarf2_section_info *section,
3002 int is_dwz)
3003 {
3004 for (offset_type i = 0; i < n_elements; i += 2)
3005 {
3006 gdb_static_assert (sizeof (ULONGEST) >= 8);
3007
3008 sect_offset sect_off
3009 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3010 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3011 cu_list += 2 * 8;
3012
3013 dwarf2_per_cu_data *per_cu
3014 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3015 sect_off, length);
3016 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
3017 }
3018 }
3019
3020 /* Read the CU list from the mapped index, and use it to create all
3021 the CU objects for this objfile. */
3022
3023 static void
3024 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3025 const gdb_byte *cu_list, offset_type cu_list_elements,
3026 const gdb_byte *dwz_list, offset_type dwz_elements)
3027 {
3028 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3029 dwarf2_per_objfile->all_comp_units.reserve
3030 ((cu_list_elements + dwz_elements) / 2);
3031
3032 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3033 &dwarf2_per_objfile->info, 0);
3034
3035 if (dwz_elements == 0)
3036 return;
3037
3038 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3039 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3040 &dwz->info, 1);
3041 }
3042
3043 /* Create the signatured type hash table from the index. */
3044
3045 static void
3046 create_signatured_type_table_from_index
3047 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3048 struct dwarf2_section_info *section,
3049 const gdb_byte *bytes,
3050 offset_type elements)
3051 {
3052 struct objfile *objfile = dwarf2_per_objfile->objfile;
3053
3054 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3055 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
3056
3057 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3058
3059 for (offset_type i = 0; i < elements; i += 3)
3060 {
3061 struct signatured_type *sig_type;
3062 ULONGEST signature;
3063 void **slot;
3064 cu_offset type_offset_in_tu;
3065
3066 gdb_static_assert (sizeof (ULONGEST) >= 8);
3067 sect_offset sect_off
3068 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3069 type_offset_in_tu
3070 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3071 BFD_ENDIAN_LITTLE);
3072 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3073 bytes += 3 * 8;
3074
3075 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3076 struct signatured_type);
3077 sig_type->signature = signature;
3078 sig_type->type_offset_in_tu = type_offset_in_tu;
3079 sig_type->per_cu.is_debug_types = 1;
3080 sig_type->per_cu.section = section;
3081 sig_type->per_cu.sect_off = sect_off;
3082 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3083 sig_type->per_cu.v.quick
3084 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3085 struct dwarf2_per_cu_quick_data);
3086
3087 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3088 *slot = sig_type;
3089
3090 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3091 }
3092
3093 dwarf2_per_objfile->signatured_types = sig_types_hash;
3094 }
3095
3096 /* Create the signatured type hash table from .debug_names. */
3097
3098 static void
3099 create_signatured_type_table_from_debug_names
3100 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3101 const mapped_debug_names &map,
3102 struct dwarf2_section_info *section,
3103 struct dwarf2_section_info *abbrev_section)
3104 {
3105 struct objfile *objfile = dwarf2_per_objfile->objfile;
3106
3107 dwarf2_read_section (objfile, section);
3108 dwarf2_read_section (objfile, abbrev_section);
3109
3110 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3111 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
3112
3113 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3114
3115 for (uint32_t i = 0; i < map.tu_count; ++i)
3116 {
3117 struct signatured_type *sig_type;
3118 void **slot;
3119
3120 sect_offset sect_off
3121 = (sect_offset) (extract_unsigned_integer
3122 (map.tu_table_reordered + i * map.offset_size,
3123 map.offset_size,
3124 map.dwarf5_byte_order));
3125
3126 comp_unit_head cu_header;
3127 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3128 abbrev_section,
3129 section->buffer + to_underlying (sect_off),
3130 rcuh_kind::TYPE);
3131
3132 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3133 struct signatured_type);
3134 sig_type->signature = cu_header.signature;
3135 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3136 sig_type->per_cu.is_debug_types = 1;
3137 sig_type->per_cu.section = section;
3138 sig_type->per_cu.sect_off = sect_off;
3139 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3140 sig_type->per_cu.v.quick
3141 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3142 struct dwarf2_per_cu_quick_data);
3143
3144 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3145 *slot = sig_type;
3146
3147 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3148 }
3149
3150 dwarf2_per_objfile->signatured_types = sig_types_hash;
3151 }
3152
3153 /* Read the address map data from the mapped index, and use it to
3154 populate the objfile's psymtabs_addrmap. */
3155
3156 static void
3157 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3158 struct mapped_index *index)
3159 {
3160 struct objfile *objfile = dwarf2_per_objfile->objfile;
3161 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3162 const gdb_byte *iter, *end;
3163 struct addrmap *mutable_map;
3164 CORE_ADDR baseaddr;
3165
3166 auto_obstack temp_obstack;
3167
3168 mutable_map = addrmap_create_mutable (&temp_obstack);
3169
3170 iter = index->address_table.data ();
3171 end = iter + index->address_table.size ();
3172
3173 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
3174
3175 while (iter < end)
3176 {
3177 ULONGEST hi, lo, cu_index;
3178 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3179 iter += 8;
3180 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3181 iter += 8;
3182 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3183 iter += 4;
3184
3185 if (lo > hi)
3186 {
3187 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
3188 hex_string (lo), hex_string (hi));
3189 continue;
3190 }
3191
3192 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
3193 {
3194 complaint (_(".gdb_index address table has invalid CU number %u"),
3195 (unsigned) cu_index);
3196 continue;
3197 }
3198
3199 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3200 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
3201 addrmap_set_empty (mutable_map, lo, hi - 1,
3202 dwarf2_per_objfile->get_cu (cu_index));
3203 }
3204
3205 objfile->partial_symtabs->psymtabs_addrmap
3206 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3207 }
3208
3209 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3210 populate the objfile's psymtabs_addrmap. */
3211
3212 static void
3213 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3214 struct dwarf2_section_info *section)
3215 {
3216 struct objfile *objfile = dwarf2_per_objfile->objfile;
3217 bfd *abfd = objfile->obfd;
3218 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3219 const CORE_ADDR baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
3220
3221 auto_obstack temp_obstack;
3222 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3223
3224 std::unordered_map<sect_offset,
3225 dwarf2_per_cu_data *,
3226 gdb::hash_enum<sect_offset>>
3227 debug_info_offset_to_per_cu;
3228 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3229 {
3230 const auto insertpair
3231 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3232 if (!insertpair.second)
3233 {
3234 warning (_("Section .debug_aranges in %s has duplicate "
3235 "debug_info_offset %s, ignoring .debug_aranges."),
3236 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3237 return;
3238 }
3239 }
3240
3241 dwarf2_read_section (objfile, section);
3242
3243 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3244
3245 const gdb_byte *addr = section->buffer;
3246
3247 while (addr < section->buffer + section->size)
3248 {
3249 const gdb_byte *const entry_addr = addr;
3250 unsigned int bytes_read;
3251
3252 const LONGEST entry_length = read_initial_length (abfd, addr,
3253 &bytes_read);
3254 addr += bytes_read;
3255
3256 const gdb_byte *const entry_end = addr + entry_length;
3257 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3258 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3259 if (addr + entry_length > section->buffer + section->size)
3260 {
3261 warning (_("Section .debug_aranges in %s entry at offset %s "
3262 "length %s exceeds section length %s, "
3263 "ignoring .debug_aranges."),
3264 objfile_name (objfile),
3265 plongest (entry_addr - section->buffer),
3266 plongest (bytes_read + entry_length),
3267 pulongest (section->size));
3268 return;
3269 }
3270
3271 /* The version number. */
3272 const uint16_t version = read_2_bytes (abfd, addr);
3273 addr += 2;
3274 if (version != 2)
3275 {
3276 warning (_("Section .debug_aranges in %s entry at offset %s "
3277 "has unsupported version %d, ignoring .debug_aranges."),
3278 objfile_name (objfile),
3279 plongest (entry_addr - section->buffer), version);
3280 return;
3281 }
3282
3283 const uint64_t debug_info_offset
3284 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3285 addr += offset_size;
3286 const auto per_cu_it
3287 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3288 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3289 {
3290 warning (_("Section .debug_aranges in %s entry at offset %s "
3291 "debug_info_offset %s does not exists, "
3292 "ignoring .debug_aranges."),
3293 objfile_name (objfile),
3294 plongest (entry_addr - section->buffer),
3295 pulongest (debug_info_offset));
3296 return;
3297 }
3298 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3299
3300 const uint8_t address_size = *addr++;
3301 if (address_size < 1 || address_size > 8)
3302 {
3303 warning (_("Section .debug_aranges in %s entry at offset %s "
3304 "address_size %u is invalid, ignoring .debug_aranges."),
3305 objfile_name (objfile),
3306 plongest (entry_addr - section->buffer), address_size);
3307 return;
3308 }
3309
3310 const uint8_t segment_selector_size = *addr++;
3311 if (segment_selector_size != 0)
3312 {
3313 warning (_("Section .debug_aranges in %s entry at offset %s "
3314 "segment_selector_size %u is not supported, "
3315 "ignoring .debug_aranges."),
3316 objfile_name (objfile),
3317 plongest (entry_addr - section->buffer),
3318 segment_selector_size);
3319 return;
3320 }
3321
3322 /* Must pad to an alignment boundary that is twice the address
3323 size. It is undocumented by the DWARF standard but GCC does
3324 use it. */
3325 for (size_t padding = ((-(addr - section->buffer))
3326 & (2 * address_size - 1));
3327 padding > 0; padding--)
3328 if (*addr++ != 0)
3329 {
3330 warning (_("Section .debug_aranges in %s entry at offset %s "
3331 "padding is not zero, ignoring .debug_aranges."),
3332 objfile_name (objfile),
3333 plongest (entry_addr - section->buffer));
3334 return;
3335 }
3336
3337 for (;;)
3338 {
3339 if (addr + 2 * address_size > entry_end)
3340 {
3341 warning (_("Section .debug_aranges in %s entry at offset %s "
3342 "address list is not properly terminated, "
3343 "ignoring .debug_aranges."),
3344 objfile_name (objfile),
3345 plongest (entry_addr - section->buffer));
3346 return;
3347 }
3348 ULONGEST start = extract_unsigned_integer (addr, address_size,
3349 dwarf5_byte_order);
3350 addr += address_size;
3351 ULONGEST length = extract_unsigned_integer (addr, address_size,
3352 dwarf5_byte_order);
3353 addr += address_size;
3354 if (start == 0 && length == 0)
3355 break;
3356 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3357 {
3358 /* Symbol was eliminated due to a COMDAT group. */
3359 continue;
3360 }
3361 ULONGEST end = start + length;
3362 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3363 - baseaddr);
3364 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3365 - baseaddr);
3366 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3367 }
3368 }
3369
3370 objfile->partial_symtabs->psymtabs_addrmap
3371 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3372 }
3373
3374 /* Find a slot in the mapped index INDEX for the object named NAME.
3375 If NAME is found, set *VEC_OUT to point to the CU vector in the
3376 constant pool and return true. If NAME cannot be found, return
3377 false. */
3378
3379 static bool
3380 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3381 offset_type **vec_out)
3382 {
3383 offset_type hash;
3384 offset_type slot, step;
3385 int (*cmp) (const char *, const char *);
3386
3387 gdb::unique_xmalloc_ptr<char> without_params;
3388 if (current_language->la_language == language_cplus
3389 || current_language->la_language == language_fortran
3390 || current_language->la_language == language_d)
3391 {
3392 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3393 not contain any. */
3394
3395 if (strchr (name, '(') != NULL)
3396 {
3397 without_params = cp_remove_params (name);
3398
3399 if (without_params != NULL)
3400 name = without_params.get ();
3401 }
3402 }
3403
3404 /* Index version 4 did not support case insensitive searches. But the
3405 indices for case insensitive languages are built in lowercase, therefore
3406 simulate our NAME being searched is also lowercased. */
3407 hash = mapped_index_string_hash ((index->version == 4
3408 && case_sensitivity == case_sensitive_off
3409 ? 5 : index->version),
3410 name);
3411
3412 slot = hash & (index->symbol_table.size () - 1);
3413 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3414 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3415
3416 for (;;)
3417 {
3418 const char *str;
3419
3420 const auto &bucket = index->symbol_table[slot];
3421 if (bucket.name == 0 && bucket.vec == 0)
3422 return false;
3423
3424 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3425 if (!cmp (name, str))
3426 {
3427 *vec_out = (offset_type *) (index->constant_pool
3428 + MAYBE_SWAP (bucket.vec));
3429 return true;
3430 }
3431
3432 slot = (slot + step) & (index->symbol_table.size () - 1);
3433 }
3434 }
3435
3436 /* A helper function that reads the .gdb_index from BUFFER and fills
3437 in MAP. FILENAME is the name of the file containing the data;
3438 it is used for error reporting. DEPRECATED_OK is true if it is
3439 ok to use deprecated sections.
3440
3441 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3442 out parameters that are filled in with information about the CU and
3443 TU lists in the section.
3444
3445 Returns true if all went well, false otherwise. */
3446
3447 static bool
3448 read_gdb_index_from_buffer (struct objfile *objfile,
3449 const char *filename,
3450 bool deprecated_ok,
3451 gdb::array_view<const gdb_byte> buffer,
3452 struct mapped_index *map,
3453 const gdb_byte **cu_list,
3454 offset_type *cu_list_elements,
3455 const gdb_byte **types_list,
3456 offset_type *types_list_elements)
3457 {
3458 const gdb_byte *addr = &buffer[0];
3459
3460 /* Version check. */
3461 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3462 /* Versions earlier than 3 emitted every copy of a psymbol. This
3463 causes the index to behave very poorly for certain requests. Version 3
3464 contained incomplete addrmap. So, it seems better to just ignore such
3465 indices. */
3466 if (version < 4)
3467 {
3468 static int warning_printed = 0;
3469 if (!warning_printed)
3470 {
3471 warning (_("Skipping obsolete .gdb_index section in %s."),
3472 filename);
3473 warning_printed = 1;
3474 }
3475 return 0;
3476 }
3477 /* Index version 4 uses a different hash function than index version
3478 5 and later.
3479
3480 Versions earlier than 6 did not emit psymbols for inlined
3481 functions. Using these files will cause GDB not to be able to
3482 set breakpoints on inlined functions by name, so we ignore these
3483 indices unless the user has done
3484 "set use-deprecated-index-sections on". */
3485 if (version < 6 && !deprecated_ok)
3486 {
3487 static int warning_printed = 0;
3488 if (!warning_printed)
3489 {
3490 warning (_("\
3491 Skipping deprecated .gdb_index section in %s.\n\
3492 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3493 to use the section anyway."),
3494 filename);
3495 warning_printed = 1;
3496 }
3497 return 0;
3498 }
3499 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3500 of the TU (for symbols coming from TUs),
3501 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3502 Plus gold-generated indices can have duplicate entries for global symbols,
3503 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3504 These are just performance bugs, and we can't distinguish gdb-generated
3505 indices from gold-generated ones, so issue no warning here. */
3506
3507 /* Indexes with higher version than the one supported by GDB may be no
3508 longer backward compatible. */
3509 if (version > 8)
3510 return 0;
3511
3512 map->version = version;
3513
3514 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3515
3516 int i = 0;
3517 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3518 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3519 / 8);
3520 ++i;
3521
3522 *types_list = addr + MAYBE_SWAP (metadata[i]);
3523 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3524 - MAYBE_SWAP (metadata[i]))
3525 / 8);
3526 ++i;
3527
3528 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3529 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3530 map->address_table
3531 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3532 ++i;
3533
3534 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3535 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3536 map->symbol_table
3537 = gdb::array_view<mapped_index::symbol_table_slot>
3538 ((mapped_index::symbol_table_slot *) symbol_table,
3539 (mapped_index::symbol_table_slot *) symbol_table_end);
3540
3541 ++i;
3542 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3543
3544 return 1;
3545 }
3546
3547 /* Callback types for dwarf2_read_gdb_index. */
3548
3549 typedef gdb::function_view
3550 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3551 get_gdb_index_contents_ftype;
3552 typedef gdb::function_view
3553 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3554 get_gdb_index_contents_dwz_ftype;
3555
3556 /* Read .gdb_index. If everything went ok, initialize the "quick"
3557 elements of all the CUs and return 1. Otherwise, return 0. */
3558
3559 static int
3560 dwarf2_read_gdb_index
3561 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3562 get_gdb_index_contents_ftype get_gdb_index_contents,
3563 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3564 {
3565 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3566 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3567 struct dwz_file *dwz;
3568 struct objfile *objfile = dwarf2_per_objfile->objfile;
3569
3570 gdb::array_view<const gdb_byte> main_index_contents
3571 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3572
3573 if (main_index_contents.empty ())
3574 return 0;
3575
3576 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3577 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3578 use_deprecated_index_sections,
3579 main_index_contents, map.get (), &cu_list,
3580 &cu_list_elements, &types_list,
3581 &types_list_elements))
3582 return 0;
3583
3584 /* Don't use the index if it's empty. */
3585 if (map->symbol_table.empty ())
3586 return 0;
3587
3588 /* If there is a .dwz file, read it so we can get its CU list as
3589 well. */
3590 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3591 if (dwz != NULL)
3592 {
3593 struct mapped_index dwz_map;
3594 const gdb_byte *dwz_types_ignore;
3595 offset_type dwz_types_elements_ignore;
3596
3597 gdb::array_view<const gdb_byte> dwz_index_content
3598 = get_gdb_index_contents_dwz (objfile, dwz);
3599
3600 if (dwz_index_content.empty ())
3601 return 0;
3602
3603 if (!read_gdb_index_from_buffer (objfile,
3604 bfd_get_filename (dwz->dwz_bfd.get ()),
3605 1, dwz_index_content, &dwz_map,
3606 &dwz_list, &dwz_list_elements,
3607 &dwz_types_ignore,
3608 &dwz_types_elements_ignore))
3609 {
3610 warning (_("could not read '.gdb_index' section from %s; skipping"),
3611 bfd_get_filename (dwz->dwz_bfd.get ()));
3612 return 0;
3613 }
3614 }
3615
3616 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3617 dwz_list, dwz_list_elements);
3618
3619 if (types_list_elements)
3620 {
3621 /* We can only handle a single .debug_types when we have an
3622 index. */
3623 if (dwarf2_per_objfile->types.size () != 1)
3624 return 0;
3625
3626 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3627
3628 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3629 types_list, types_list_elements);
3630 }
3631
3632 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3633
3634 dwarf2_per_objfile->index_table = std::move (map);
3635 dwarf2_per_objfile->using_index = 1;
3636 dwarf2_per_objfile->quick_file_names_table =
3637 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3638
3639 return 1;
3640 }
3641
3642 /* die_reader_func for dw2_get_file_names. */
3643
3644 static void
3645 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3646 const gdb_byte *info_ptr,
3647 struct die_info *comp_unit_die,
3648 int has_children,
3649 void *data)
3650 {
3651 struct dwarf2_cu *cu = reader->cu;
3652 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3653 struct dwarf2_per_objfile *dwarf2_per_objfile
3654 = cu->per_cu->dwarf2_per_objfile;
3655 struct objfile *objfile = dwarf2_per_objfile->objfile;
3656 struct dwarf2_per_cu_data *lh_cu;
3657 struct attribute *attr;
3658 void **slot;
3659 struct quick_file_names *qfn;
3660
3661 gdb_assert (! this_cu->is_debug_types);
3662
3663 /* Our callers never want to match partial units -- instead they
3664 will match the enclosing full CU. */
3665 if (comp_unit_die->tag == DW_TAG_partial_unit)
3666 {
3667 this_cu->v.quick->no_file_data = 1;
3668 return;
3669 }
3670
3671 lh_cu = this_cu;
3672 slot = NULL;
3673
3674 line_header_up lh;
3675 sect_offset line_offset {};
3676
3677 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3678 if (attr != nullptr)
3679 {
3680 struct quick_file_names find_entry;
3681
3682 line_offset = (sect_offset) DW_UNSND (attr);
3683
3684 /* We may have already read in this line header (TU line header sharing).
3685 If we have we're done. */
3686 find_entry.hash.dwo_unit = cu->dwo_unit;
3687 find_entry.hash.line_sect_off = line_offset;
3688 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3689 &find_entry, INSERT);
3690 if (*slot != NULL)
3691 {
3692 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3693 return;
3694 }
3695
3696 lh = dwarf_decode_line_header (line_offset, cu);
3697 }
3698 if (lh == NULL)
3699 {
3700 lh_cu->v.quick->no_file_data = 1;
3701 return;
3702 }
3703
3704 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3705 qfn->hash.dwo_unit = cu->dwo_unit;
3706 qfn->hash.line_sect_off = line_offset;
3707 gdb_assert (slot != NULL);
3708 *slot = qfn;
3709
3710 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3711
3712 int offset = 0;
3713 if (strcmp (fnd.name, "<unknown>") != 0)
3714 ++offset;
3715
3716 qfn->num_file_names = offset + lh->file_names_size ();
3717 qfn->file_names =
3718 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3719 if (offset != 0)
3720 qfn->file_names[0] = xstrdup (fnd.name);
3721 for (int i = 0; i < lh->file_names_size (); ++i)
3722 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3723 qfn->real_names = NULL;
3724
3725 lh_cu->v.quick->file_names = qfn;
3726 }
3727
3728 /* A helper for the "quick" functions which attempts to read the line
3729 table for THIS_CU. */
3730
3731 static struct quick_file_names *
3732 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3733 {
3734 /* This should never be called for TUs. */
3735 gdb_assert (! this_cu->is_debug_types);
3736 /* Nor type unit groups. */
3737 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3738
3739 if (this_cu->v.quick->file_names != NULL)
3740 return this_cu->v.quick->file_names;
3741 /* If we know there is no line data, no point in looking again. */
3742 if (this_cu->v.quick->no_file_data)
3743 return NULL;
3744
3745 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3746
3747 if (this_cu->v.quick->no_file_data)
3748 return NULL;
3749 return this_cu->v.quick->file_names;
3750 }
3751
3752 /* A helper for the "quick" functions which computes and caches the
3753 real path for a given file name from the line table. */
3754
3755 static const char *
3756 dw2_get_real_path (struct objfile *objfile,
3757 struct quick_file_names *qfn, int index)
3758 {
3759 if (qfn->real_names == NULL)
3760 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3761 qfn->num_file_names, const char *);
3762
3763 if (qfn->real_names[index] == NULL)
3764 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3765
3766 return qfn->real_names[index];
3767 }
3768
3769 static struct symtab *
3770 dw2_find_last_source_symtab (struct objfile *objfile)
3771 {
3772 struct dwarf2_per_objfile *dwarf2_per_objfile
3773 = get_dwarf2_per_objfile (objfile);
3774 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3775 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3776
3777 if (cust == NULL)
3778 return NULL;
3779
3780 return compunit_primary_filetab (cust);
3781 }
3782
3783 /* Traversal function for dw2_forget_cached_source_info. */
3784
3785 static int
3786 dw2_free_cached_file_names (void **slot, void *info)
3787 {
3788 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3789
3790 if (file_data->real_names)
3791 {
3792 int i;
3793
3794 for (i = 0; i < file_data->num_file_names; ++i)
3795 {
3796 xfree ((void*) file_data->real_names[i]);
3797 file_data->real_names[i] = NULL;
3798 }
3799 }
3800
3801 return 1;
3802 }
3803
3804 static void
3805 dw2_forget_cached_source_info (struct objfile *objfile)
3806 {
3807 struct dwarf2_per_objfile *dwarf2_per_objfile
3808 = get_dwarf2_per_objfile (objfile);
3809
3810 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3811 dw2_free_cached_file_names, NULL);
3812 }
3813
3814 /* Helper function for dw2_map_symtabs_matching_filename that expands
3815 the symtabs and calls the iterator. */
3816
3817 static int
3818 dw2_map_expand_apply (struct objfile *objfile,
3819 struct dwarf2_per_cu_data *per_cu,
3820 const char *name, const char *real_path,
3821 gdb::function_view<bool (symtab *)> callback)
3822 {
3823 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3824
3825 /* Don't visit already-expanded CUs. */
3826 if (per_cu->v.quick->compunit_symtab)
3827 return 0;
3828
3829 /* This may expand more than one symtab, and we want to iterate over
3830 all of them. */
3831 dw2_instantiate_symtab (per_cu, false);
3832
3833 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3834 last_made, callback);
3835 }
3836
3837 /* Implementation of the map_symtabs_matching_filename method. */
3838
3839 static bool
3840 dw2_map_symtabs_matching_filename
3841 (struct objfile *objfile, const char *name, const char *real_path,
3842 gdb::function_view<bool (symtab *)> callback)
3843 {
3844 const char *name_basename = lbasename (name);
3845 struct dwarf2_per_objfile *dwarf2_per_objfile
3846 = get_dwarf2_per_objfile (objfile);
3847
3848 /* The rule is CUs specify all the files, including those used by
3849 any TU, so there's no need to scan TUs here. */
3850
3851 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3852 {
3853 /* We only need to look at symtabs not already expanded. */
3854 if (per_cu->v.quick->compunit_symtab)
3855 continue;
3856
3857 quick_file_names *file_data = dw2_get_file_names (per_cu);
3858 if (file_data == NULL)
3859 continue;
3860
3861 for (int j = 0; j < file_data->num_file_names; ++j)
3862 {
3863 const char *this_name = file_data->file_names[j];
3864 const char *this_real_name;
3865
3866 if (compare_filenames_for_search (this_name, name))
3867 {
3868 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3869 callback))
3870 return true;
3871 continue;
3872 }
3873
3874 /* Before we invoke realpath, which can get expensive when many
3875 files are involved, do a quick comparison of the basenames. */
3876 if (! basenames_may_differ
3877 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3878 continue;
3879
3880 this_real_name = dw2_get_real_path (objfile, file_data, j);
3881 if (compare_filenames_for_search (this_real_name, name))
3882 {
3883 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3884 callback))
3885 return true;
3886 continue;
3887 }
3888
3889 if (real_path != NULL)
3890 {
3891 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3892 gdb_assert (IS_ABSOLUTE_PATH (name));
3893 if (this_real_name != NULL
3894 && FILENAME_CMP (real_path, this_real_name) == 0)
3895 {
3896 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3897 callback))
3898 return true;
3899 continue;
3900 }
3901 }
3902 }
3903 }
3904
3905 return false;
3906 }
3907
3908 /* Struct used to manage iterating over all CUs looking for a symbol. */
3909
3910 struct dw2_symtab_iterator
3911 {
3912 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3913 struct dwarf2_per_objfile *dwarf2_per_objfile;
3914 /* If set, only look for symbols that match that block. Valid values are
3915 GLOBAL_BLOCK and STATIC_BLOCK. */
3916 gdb::optional<block_enum> block_index;
3917 /* The kind of symbol we're looking for. */
3918 domain_enum domain;
3919 /* The list of CUs from the index entry of the symbol,
3920 or NULL if not found. */
3921 offset_type *vec;
3922 /* The next element in VEC to look at. */
3923 int next;
3924 /* The number of elements in VEC, or zero if there is no match. */
3925 int length;
3926 /* Have we seen a global version of the symbol?
3927 If so we can ignore all further global instances.
3928 This is to work around gold/15646, inefficient gold-generated
3929 indices. */
3930 int global_seen;
3931 };
3932
3933 /* Initialize the index symtab iterator ITER. */
3934
3935 static void
3936 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3937 struct dwarf2_per_objfile *dwarf2_per_objfile,
3938 gdb::optional<block_enum> block_index,
3939 domain_enum domain,
3940 const char *name)
3941 {
3942 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3943 iter->block_index = block_index;
3944 iter->domain = domain;
3945 iter->next = 0;
3946 iter->global_seen = 0;
3947
3948 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3949
3950 /* index is NULL if OBJF_READNOW. */
3951 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3952 iter->length = MAYBE_SWAP (*iter->vec);
3953 else
3954 {
3955 iter->vec = NULL;
3956 iter->length = 0;
3957 }
3958 }
3959
3960 /* Return the next matching CU or NULL if there are no more. */
3961
3962 static struct dwarf2_per_cu_data *
3963 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3964 {
3965 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3966
3967 for ( ; iter->next < iter->length; ++iter->next)
3968 {
3969 offset_type cu_index_and_attrs =
3970 MAYBE_SWAP (iter->vec[iter->next + 1]);
3971 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3972 gdb_index_symbol_kind symbol_kind =
3973 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3974 /* Only check the symbol attributes if they're present.
3975 Indices prior to version 7 don't record them,
3976 and indices >= 7 may elide them for certain symbols
3977 (gold does this). */
3978 int attrs_valid =
3979 (dwarf2_per_objfile->index_table->version >= 7
3980 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3981
3982 /* Don't crash on bad data. */
3983 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3984 + dwarf2_per_objfile->all_type_units.size ()))
3985 {
3986 complaint (_(".gdb_index entry has bad CU index"
3987 " [in module %s]"),
3988 objfile_name (dwarf2_per_objfile->objfile));
3989 continue;
3990 }
3991
3992 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3993
3994 /* Skip if already read in. */
3995 if (per_cu->v.quick->compunit_symtab)
3996 continue;
3997
3998 /* Check static vs global. */
3999 if (attrs_valid)
4000 {
4001 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4002
4003 if (iter->block_index.has_value ())
4004 {
4005 bool want_static = *iter->block_index == STATIC_BLOCK;
4006
4007 if (is_static != want_static)
4008 continue;
4009 }
4010
4011 /* Work around gold/15646. */
4012 if (!is_static && iter->global_seen)
4013 continue;
4014 if (!is_static)
4015 iter->global_seen = 1;
4016 }
4017
4018 /* Only check the symbol's kind if it has one. */
4019 if (attrs_valid)
4020 {
4021 switch (iter->domain)
4022 {
4023 case VAR_DOMAIN:
4024 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4025 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4026 /* Some types are also in VAR_DOMAIN. */
4027 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4028 continue;
4029 break;
4030 case STRUCT_DOMAIN:
4031 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4032 continue;
4033 break;
4034 case LABEL_DOMAIN:
4035 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4036 continue;
4037 break;
4038 case MODULE_DOMAIN:
4039 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4040 continue;
4041 break;
4042 default:
4043 break;
4044 }
4045 }
4046
4047 ++iter->next;
4048 return per_cu;
4049 }
4050
4051 return NULL;
4052 }
4053
4054 static struct compunit_symtab *
4055 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
4056 const char *name, domain_enum domain)
4057 {
4058 struct compunit_symtab *stab_best = NULL;
4059 struct dwarf2_per_objfile *dwarf2_per_objfile
4060 = get_dwarf2_per_objfile (objfile);
4061
4062 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4063
4064 struct dw2_symtab_iterator iter;
4065 struct dwarf2_per_cu_data *per_cu;
4066
4067 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
4068
4069 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4070 {
4071 struct symbol *sym, *with_opaque = NULL;
4072 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
4073 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4074 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4075
4076 sym = block_find_symbol (block, name, domain,
4077 block_find_non_opaque_type_preferred,
4078 &with_opaque);
4079
4080 /* Some caution must be observed with overloaded functions
4081 and methods, since the index will not contain any overload
4082 information (but NAME might contain it). */
4083
4084 if (sym != NULL
4085 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4086 return stab;
4087 if (with_opaque != NULL
4088 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4089 stab_best = stab;
4090
4091 /* Keep looking through other CUs. */
4092 }
4093
4094 return stab_best;
4095 }
4096
4097 static void
4098 dw2_print_stats (struct objfile *objfile)
4099 {
4100 struct dwarf2_per_objfile *dwarf2_per_objfile
4101 = get_dwarf2_per_objfile (objfile);
4102 int total = (dwarf2_per_objfile->all_comp_units.size ()
4103 + dwarf2_per_objfile->all_type_units.size ());
4104 int count = 0;
4105
4106 for (int i = 0; i < total; ++i)
4107 {
4108 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4109
4110 if (!per_cu->v.quick->compunit_symtab)
4111 ++count;
4112 }
4113 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4114 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4115 }
4116
4117 /* This dumps minimal information about the index.
4118 It is called via "mt print objfiles".
4119 One use is to verify .gdb_index has been loaded by the
4120 gdb.dwarf2/gdb-index.exp testcase. */
4121
4122 static void
4123 dw2_dump (struct objfile *objfile)
4124 {
4125 struct dwarf2_per_objfile *dwarf2_per_objfile
4126 = get_dwarf2_per_objfile (objfile);
4127
4128 gdb_assert (dwarf2_per_objfile->using_index);
4129 printf_filtered (".gdb_index:");
4130 if (dwarf2_per_objfile->index_table != NULL)
4131 {
4132 printf_filtered (" version %d\n",
4133 dwarf2_per_objfile->index_table->version);
4134 }
4135 else
4136 printf_filtered (" faked for \"readnow\"\n");
4137 printf_filtered ("\n");
4138 }
4139
4140 static void
4141 dw2_expand_symtabs_for_function (struct objfile *objfile,
4142 const char *func_name)
4143 {
4144 struct dwarf2_per_objfile *dwarf2_per_objfile
4145 = get_dwarf2_per_objfile (objfile);
4146
4147 struct dw2_symtab_iterator iter;
4148 struct dwarf2_per_cu_data *per_cu;
4149
4150 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
4151
4152 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4153 dw2_instantiate_symtab (per_cu, false);
4154
4155 }
4156
4157 static void
4158 dw2_expand_all_symtabs (struct objfile *objfile)
4159 {
4160 struct dwarf2_per_objfile *dwarf2_per_objfile
4161 = get_dwarf2_per_objfile (objfile);
4162 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4163 + dwarf2_per_objfile->all_type_units.size ());
4164
4165 for (int i = 0; i < total_units; ++i)
4166 {
4167 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4168
4169 /* We don't want to directly expand a partial CU, because if we
4170 read it with the wrong language, then assertion failures can
4171 be triggered later on. See PR symtab/23010. So, tell
4172 dw2_instantiate_symtab to skip partial CUs -- any important
4173 partial CU will be read via DW_TAG_imported_unit anyway. */
4174 dw2_instantiate_symtab (per_cu, true);
4175 }
4176 }
4177
4178 static void
4179 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4180 const char *fullname)
4181 {
4182 struct dwarf2_per_objfile *dwarf2_per_objfile
4183 = get_dwarf2_per_objfile (objfile);
4184
4185 /* We don't need to consider type units here.
4186 This is only called for examining code, e.g. expand_line_sal.
4187 There can be an order of magnitude (or more) more type units
4188 than comp units, and we avoid them if we can. */
4189
4190 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4191 {
4192 /* We only need to look at symtabs not already expanded. */
4193 if (per_cu->v.quick->compunit_symtab)
4194 continue;
4195
4196 quick_file_names *file_data = dw2_get_file_names (per_cu);
4197 if (file_data == NULL)
4198 continue;
4199
4200 for (int j = 0; j < file_data->num_file_names; ++j)
4201 {
4202 const char *this_fullname = file_data->file_names[j];
4203
4204 if (filename_cmp (this_fullname, fullname) == 0)
4205 {
4206 dw2_instantiate_symtab (per_cu, false);
4207 break;
4208 }
4209 }
4210 }
4211 }
4212
4213 static void
4214 dw2_map_matching_symbols
4215 (struct objfile *objfile,
4216 const lookup_name_info &name, domain_enum domain,
4217 int global,
4218 gdb::function_view<symbol_found_callback_ftype> callback,
4219 symbol_compare_ftype *ordered_compare)
4220 {
4221 /* Currently unimplemented; used for Ada. The function can be called if the
4222 current language is Ada for a non-Ada objfile using GNU index. As Ada
4223 does not look for non-Ada symbols this function should just return. */
4224 }
4225
4226 /* Starting from a search name, return the string that finds the upper
4227 bound of all strings that start with SEARCH_NAME in a sorted name
4228 list. Returns the empty string to indicate that the upper bound is
4229 the end of the list. */
4230
4231 static std::string
4232 make_sort_after_prefix_name (const char *search_name)
4233 {
4234 /* When looking to complete "func", we find the upper bound of all
4235 symbols that start with "func" by looking for where we'd insert
4236 the closest string that would follow "func" in lexicographical
4237 order. Usually, that's "func"-with-last-character-incremented,
4238 i.e. "fund". Mind non-ASCII characters, though. Usually those
4239 will be UTF-8 multi-byte sequences, but we can't be certain.
4240 Especially mind the 0xff character, which is a valid character in
4241 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4242 rule out compilers allowing it in identifiers. Note that
4243 conveniently, strcmp/strcasecmp are specified to compare
4244 characters interpreted as unsigned char. So what we do is treat
4245 the whole string as a base 256 number composed of a sequence of
4246 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4247 to 0, and carries 1 to the following more-significant position.
4248 If the very first character in SEARCH_NAME ends up incremented
4249 and carries/overflows, then the upper bound is the end of the
4250 list. The string after the empty string is also the empty
4251 string.
4252
4253 Some examples of this operation:
4254
4255 SEARCH_NAME => "+1" RESULT
4256
4257 "abc" => "abd"
4258 "ab\xff" => "ac"
4259 "\xff" "a" "\xff" => "\xff" "b"
4260 "\xff" => ""
4261 "\xff\xff" => ""
4262 "" => ""
4263
4264 Then, with these symbols for example:
4265
4266 func
4267 func1
4268 fund
4269
4270 completing "func" looks for symbols between "func" and
4271 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4272 which finds "func" and "func1", but not "fund".
4273
4274 And with:
4275
4276 funcÿ (Latin1 'ÿ' [0xff])
4277 funcÿ1
4278 fund
4279
4280 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4281 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4282
4283 And with:
4284
4285 ÿÿ (Latin1 'ÿ' [0xff])
4286 ÿÿ1
4287
4288 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4289 the end of the list.
4290 */
4291 std::string after = search_name;
4292 while (!after.empty () && (unsigned char) after.back () == 0xff)
4293 after.pop_back ();
4294 if (!after.empty ())
4295 after.back () = (unsigned char) after.back () + 1;
4296 return after;
4297 }
4298
4299 /* See declaration. */
4300
4301 std::pair<std::vector<name_component>::const_iterator,
4302 std::vector<name_component>::const_iterator>
4303 mapped_index_base::find_name_components_bounds
4304 (const lookup_name_info &lookup_name_without_params, language lang) const
4305 {
4306 auto *name_cmp
4307 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4308
4309 const char *lang_name
4310 = lookup_name_without_params.language_lookup_name (lang).c_str ();
4311
4312 /* Comparison function object for lower_bound that matches against a
4313 given symbol name. */
4314 auto lookup_compare_lower = [&] (const name_component &elem,
4315 const char *name)
4316 {
4317 const char *elem_qualified = this->symbol_name_at (elem.idx);
4318 const char *elem_name = elem_qualified + elem.name_offset;
4319 return name_cmp (elem_name, name) < 0;
4320 };
4321
4322 /* Comparison function object for upper_bound that matches against a
4323 given symbol name. */
4324 auto lookup_compare_upper = [&] (const char *name,
4325 const name_component &elem)
4326 {
4327 const char *elem_qualified = this->symbol_name_at (elem.idx);
4328 const char *elem_name = elem_qualified + elem.name_offset;
4329 return name_cmp (name, elem_name) < 0;
4330 };
4331
4332 auto begin = this->name_components.begin ();
4333 auto end = this->name_components.end ();
4334
4335 /* Find the lower bound. */
4336 auto lower = [&] ()
4337 {
4338 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4339 return begin;
4340 else
4341 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4342 } ();
4343
4344 /* Find the upper bound. */
4345 auto upper = [&] ()
4346 {
4347 if (lookup_name_without_params.completion_mode ())
4348 {
4349 /* In completion mode, we want UPPER to point past all
4350 symbols names that have the same prefix. I.e., with
4351 these symbols, and completing "func":
4352
4353 function << lower bound
4354 function1
4355 other_function << upper bound
4356
4357 We find the upper bound by looking for the insertion
4358 point of "func"-with-last-character-incremented,
4359 i.e. "fund". */
4360 std::string after = make_sort_after_prefix_name (lang_name);
4361 if (after.empty ())
4362 return end;
4363 return std::lower_bound (lower, end, after.c_str (),
4364 lookup_compare_lower);
4365 }
4366 else
4367 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4368 } ();
4369
4370 return {lower, upper};
4371 }
4372
4373 /* See declaration. */
4374
4375 void
4376 mapped_index_base::build_name_components ()
4377 {
4378 if (!this->name_components.empty ())
4379 return;
4380
4381 this->name_components_casing = case_sensitivity;
4382 auto *name_cmp
4383 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4384
4385 /* The code below only knows how to break apart components of C++
4386 symbol names (and other languages that use '::' as
4387 namespace/module separator) and Ada symbol names. */
4388 auto count = this->symbol_name_count ();
4389 for (offset_type idx = 0; idx < count; idx++)
4390 {
4391 if (this->symbol_name_slot_invalid (idx))
4392 continue;
4393
4394 const char *name = this->symbol_name_at (idx);
4395
4396 /* Add each name component to the name component table. */
4397 unsigned int previous_len = 0;
4398
4399 if (strstr (name, "::") != nullptr)
4400 {
4401 for (unsigned int current_len = cp_find_first_component (name);
4402 name[current_len] != '\0';
4403 current_len += cp_find_first_component (name + current_len))
4404 {
4405 gdb_assert (name[current_len] == ':');
4406 this->name_components.push_back ({previous_len, idx});
4407 /* Skip the '::'. */
4408 current_len += 2;
4409 previous_len = current_len;
4410 }
4411 }
4412 else
4413 {
4414 /* Handle the Ada encoded (aka mangled) form here. */
4415 for (const char *iter = strstr (name, "__");
4416 iter != nullptr;
4417 iter = strstr (iter, "__"))
4418 {
4419 this->name_components.push_back ({previous_len, idx});
4420 iter += 2;
4421 previous_len = iter - name;
4422 }
4423 }
4424
4425 this->name_components.push_back ({previous_len, idx});
4426 }
4427
4428 /* Sort name_components elements by name. */
4429 auto name_comp_compare = [&] (const name_component &left,
4430 const name_component &right)
4431 {
4432 const char *left_qualified = this->symbol_name_at (left.idx);
4433 const char *right_qualified = this->symbol_name_at (right.idx);
4434
4435 const char *left_name = left_qualified + left.name_offset;
4436 const char *right_name = right_qualified + right.name_offset;
4437
4438 return name_cmp (left_name, right_name) < 0;
4439 };
4440
4441 std::sort (this->name_components.begin (),
4442 this->name_components.end (),
4443 name_comp_compare);
4444 }
4445
4446 /* Helper for dw2_expand_symtabs_matching that works with a
4447 mapped_index_base instead of the containing objfile. This is split
4448 to a separate function in order to be able to unit test the
4449 name_components matching using a mock mapped_index_base. For each
4450 symbol name that matches, calls MATCH_CALLBACK, passing it the
4451 symbol's index in the mapped_index_base symbol table. */
4452
4453 static void
4454 dw2_expand_symtabs_matching_symbol
4455 (mapped_index_base &index,
4456 const lookup_name_info &lookup_name_in,
4457 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4458 enum search_domain kind,
4459 gdb::function_view<bool (offset_type)> match_callback)
4460 {
4461 lookup_name_info lookup_name_without_params
4462 = lookup_name_in.make_ignore_params ();
4463
4464 /* Build the symbol name component sorted vector, if we haven't
4465 yet. */
4466 index.build_name_components ();
4467
4468 /* The same symbol may appear more than once in the range though.
4469 E.g., if we're looking for symbols that complete "w", and we have
4470 a symbol named "w1::w2", we'll find the two name components for
4471 that same symbol in the range. To be sure we only call the
4472 callback once per symbol, we first collect the symbol name
4473 indexes that matched in a temporary vector and ignore
4474 duplicates. */
4475 std::vector<offset_type> matches;
4476
4477 struct name_and_matcher
4478 {
4479 symbol_name_matcher_ftype *matcher;
4480 const std::string &name;
4481
4482 bool operator== (const name_and_matcher &other) const
4483 {
4484 return matcher == other.matcher && name == other.name;
4485 }
4486 };
4487
4488 /* A vector holding all the different symbol name matchers, for all
4489 languages. */
4490 std::vector<name_and_matcher> matchers;
4491
4492 for (int i = 0; i < nr_languages; i++)
4493 {
4494 enum language lang_e = (enum language) i;
4495
4496 const language_defn *lang = language_def (lang_e);
4497 symbol_name_matcher_ftype *name_matcher
4498 = get_symbol_name_matcher (lang, lookup_name_without_params);
4499
4500 name_and_matcher key {
4501 name_matcher,
4502 lookup_name_without_params.language_lookup_name (lang_e)
4503 };
4504
4505 /* Don't insert the same comparison routine more than once.
4506 Note that we do this linear walk. This is not a problem in
4507 practice because the number of supported languages is
4508 low. */
4509 if (std::find (matchers.begin (), matchers.end (), key)
4510 != matchers.end ())
4511 continue;
4512 matchers.push_back (std::move (key));
4513
4514 auto bounds
4515 = index.find_name_components_bounds (lookup_name_without_params,
4516 lang_e);
4517
4518 /* Now for each symbol name in range, check to see if we have a name
4519 match, and if so, call the MATCH_CALLBACK callback. */
4520
4521 for (; bounds.first != bounds.second; ++bounds.first)
4522 {
4523 const char *qualified = index.symbol_name_at (bounds.first->idx);
4524
4525 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4526 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4527 continue;
4528
4529 matches.push_back (bounds.first->idx);
4530 }
4531 }
4532
4533 std::sort (matches.begin (), matches.end ());
4534
4535 /* Finally call the callback, once per match. */
4536 ULONGEST prev = -1;
4537 for (offset_type idx : matches)
4538 {
4539 if (prev != idx)
4540 {
4541 if (!match_callback (idx))
4542 break;
4543 prev = idx;
4544 }
4545 }
4546
4547 /* Above we use a type wider than idx's for 'prev', since 0 and
4548 (offset_type)-1 are both possible values. */
4549 static_assert (sizeof (prev) > sizeof (offset_type), "");
4550 }
4551
4552 #if GDB_SELF_TEST
4553
4554 namespace selftests { namespace dw2_expand_symtabs_matching {
4555
4556 /* A mock .gdb_index/.debug_names-like name index table, enough to
4557 exercise dw2_expand_symtabs_matching_symbol, which works with the
4558 mapped_index_base interface. Builds an index from the symbol list
4559 passed as parameter to the constructor. */
4560 class mock_mapped_index : public mapped_index_base
4561 {
4562 public:
4563 mock_mapped_index (gdb::array_view<const char *> symbols)
4564 : m_symbol_table (symbols)
4565 {}
4566
4567 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4568
4569 /* Return the number of names in the symbol table. */
4570 size_t symbol_name_count () const override
4571 {
4572 return m_symbol_table.size ();
4573 }
4574
4575 /* Get the name of the symbol at IDX in the symbol table. */
4576 const char *symbol_name_at (offset_type idx) const override
4577 {
4578 return m_symbol_table[idx];
4579 }
4580
4581 private:
4582 gdb::array_view<const char *> m_symbol_table;
4583 };
4584
4585 /* Convenience function that converts a NULL pointer to a "<null>"
4586 string, to pass to print routines. */
4587
4588 static const char *
4589 string_or_null (const char *str)
4590 {
4591 return str != NULL ? str : "<null>";
4592 }
4593
4594 /* Check if a lookup_name_info built from
4595 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4596 index. EXPECTED_LIST is the list of expected matches, in expected
4597 matching order. If no match expected, then an empty list is
4598 specified. Returns true on success. On failure prints a warning
4599 indicating the file:line that failed, and returns false. */
4600
4601 static bool
4602 check_match (const char *file, int line,
4603 mock_mapped_index &mock_index,
4604 const char *name, symbol_name_match_type match_type,
4605 bool completion_mode,
4606 std::initializer_list<const char *> expected_list)
4607 {
4608 lookup_name_info lookup_name (name, match_type, completion_mode);
4609
4610 bool matched = true;
4611
4612 auto mismatch = [&] (const char *expected_str,
4613 const char *got)
4614 {
4615 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4616 "expected=\"%s\", got=\"%s\"\n"),
4617 file, line,
4618 (match_type == symbol_name_match_type::FULL
4619 ? "FULL" : "WILD"),
4620 name, string_or_null (expected_str), string_or_null (got));
4621 matched = false;
4622 };
4623
4624 auto expected_it = expected_list.begin ();
4625 auto expected_end = expected_list.end ();
4626
4627 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4628 NULL, ALL_DOMAIN,
4629 [&] (offset_type idx)
4630 {
4631 const char *matched_name = mock_index.symbol_name_at (idx);
4632 const char *expected_str
4633 = expected_it == expected_end ? NULL : *expected_it++;
4634
4635 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4636 mismatch (expected_str, matched_name);
4637 return true;
4638 });
4639
4640 const char *expected_str
4641 = expected_it == expected_end ? NULL : *expected_it++;
4642 if (expected_str != NULL)
4643 mismatch (expected_str, NULL);
4644
4645 return matched;
4646 }
4647
4648 /* The symbols added to the mock mapped_index for testing (in
4649 canonical form). */
4650 static const char *test_symbols[] = {
4651 "function",
4652 "std::bar",
4653 "std::zfunction",
4654 "std::zfunction2",
4655 "w1::w2",
4656 "ns::foo<char*>",
4657 "ns::foo<int>",
4658 "ns::foo<long>",
4659 "ns2::tmpl<int>::foo2",
4660 "(anonymous namespace)::A::B::C",
4661
4662 /* These are used to check that the increment-last-char in the
4663 matching algorithm for completion doesn't match "t1_fund" when
4664 completing "t1_func". */
4665 "t1_func",
4666 "t1_func1",
4667 "t1_fund",
4668 "t1_fund1",
4669
4670 /* A UTF-8 name with multi-byte sequences to make sure that
4671 cp-name-parser understands this as a single identifier ("função"
4672 is "function" in PT). */
4673 u8"u8função",
4674
4675 /* \377 (0xff) is Latin1 'ÿ'. */
4676 "yfunc\377",
4677
4678 /* \377 (0xff) is Latin1 'ÿ'. */
4679 "\377",
4680 "\377\377123",
4681
4682 /* A name with all sorts of complications. Starts with "z" to make
4683 it easier for the completion tests below. */
4684 #define Z_SYM_NAME \
4685 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4686 "::tuple<(anonymous namespace)::ui*, " \
4687 "std::default_delete<(anonymous namespace)::ui>, void>"
4688
4689 Z_SYM_NAME
4690 };
4691
4692 /* Returns true if the mapped_index_base::find_name_component_bounds
4693 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4694 in completion mode. */
4695
4696 static bool
4697 check_find_bounds_finds (mapped_index_base &index,
4698 const char *search_name,
4699 gdb::array_view<const char *> expected_syms)
4700 {
4701 lookup_name_info lookup_name (search_name,
4702 symbol_name_match_type::FULL, true);
4703
4704 auto bounds = index.find_name_components_bounds (lookup_name,
4705 language_cplus);
4706
4707 size_t distance = std::distance (bounds.first, bounds.second);
4708 if (distance != expected_syms.size ())
4709 return false;
4710
4711 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4712 {
4713 auto nc_elem = bounds.first + exp_elem;
4714 const char *qualified = index.symbol_name_at (nc_elem->idx);
4715 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4716 return false;
4717 }
4718
4719 return true;
4720 }
4721
4722 /* Test the lower-level mapped_index::find_name_component_bounds
4723 method. */
4724
4725 static void
4726 test_mapped_index_find_name_component_bounds ()
4727 {
4728 mock_mapped_index mock_index (test_symbols);
4729
4730 mock_index.build_name_components ();
4731
4732 /* Test the lower-level mapped_index::find_name_component_bounds
4733 method in completion mode. */
4734 {
4735 static const char *expected_syms[] = {
4736 "t1_func",
4737 "t1_func1",
4738 };
4739
4740 SELF_CHECK (check_find_bounds_finds (mock_index,
4741 "t1_func", expected_syms));
4742 }
4743
4744 /* Check that the increment-last-char in the name matching algorithm
4745 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4746 {
4747 static const char *expected_syms1[] = {
4748 "\377",
4749 "\377\377123",
4750 };
4751 SELF_CHECK (check_find_bounds_finds (mock_index,
4752 "\377", expected_syms1));
4753
4754 static const char *expected_syms2[] = {
4755 "\377\377123",
4756 };
4757 SELF_CHECK (check_find_bounds_finds (mock_index,
4758 "\377\377", expected_syms2));
4759 }
4760 }
4761
4762 /* Test dw2_expand_symtabs_matching_symbol. */
4763
4764 static void
4765 test_dw2_expand_symtabs_matching_symbol ()
4766 {
4767 mock_mapped_index mock_index (test_symbols);
4768
4769 /* We let all tests run until the end even if some fails, for debug
4770 convenience. */
4771 bool any_mismatch = false;
4772
4773 /* Create the expected symbols list (an initializer_list). Needed
4774 because lists have commas, and we need to pass them to CHECK,
4775 which is a macro. */
4776 #define EXPECT(...) { __VA_ARGS__ }
4777
4778 /* Wrapper for check_match that passes down the current
4779 __FILE__/__LINE__. */
4780 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4781 any_mismatch |= !check_match (__FILE__, __LINE__, \
4782 mock_index, \
4783 NAME, MATCH_TYPE, COMPLETION_MODE, \
4784 EXPECTED_LIST)
4785
4786 /* Identity checks. */
4787 for (const char *sym : test_symbols)
4788 {
4789 /* Should be able to match all existing symbols. */
4790 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4791 EXPECT (sym));
4792
4793 /* Should be able to match all existing symbols with
4794 parameters. */
4795 std::string with_params = std::string (sym) + "(int)";
4796 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4797 EXPECT (sym));
4798
4799 /* Should be able to match all existing symbols with
4800 parameters and qualifiers. */
4801 with_params = std::string (sym) + " ( int ) const";
4802 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4803 EXPECT (sym));
4804
4805 /* This should really find sym, but cp-name-parser.y doesn't
4806 know about lvalue/rvalue qualifiers yet. */
4807 with_params = std::string (sym) + " ( int ) &&";
4808 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4809 {});
4810 }
4811
4812 /* Check that the name matching algorithm for completion doesn't get
4813 confused with Latin1 'ÿ' / 0xff. */
4814 {
4815 static const char str[] = "\377";
4816 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4817 EXPECT ("\377", "\377\377123"));
4818 }
4819
4820 /* Check that the increment-last-char in the matching algorithm for
4821 completion doesn't match "t1_fund" when completing "t1_func". */
4822 {
4823 static const char str[] = "t1_func";
4824 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4825 EXPECT ("t1_func", "t1_func1"));
4826 }
4827
4828 /* Check that completion mode works at each prefix of the expected
4829 symbol name. */
4830 {
4831 static const char str[] = "function(int)";
4832 size_t len = strlen (str);
4833 std::string lookup;
4834
4835 for (size_t i = 1; i < len; i++)
4836 {
4837 lookup.assign (str, i);
4838 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4839 EXPECT ("function"));
4840 }
4841 }
4842
4843 /* While "w" is a prefix of both components, the match function
4844 should still only be called once. */
4845 {
4846 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4847 EXPECT ("w1::w2"));
4848 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4849 EXPECT ("w1::w2"));
4850 }
4851
4852 /* Same, with a "complicated" symbol. */
4853 {
4854 static const char str[] = Z_SYM_NAME;
4855 size_t len = strlen (str);
4856 std::string lookup;
4857
4858 for (size_t i = 1; i < len; i++)
4859 {
4860 lookup.assign (str, i);
4861 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4862 EXPECT (Z_SYM_NAME));
4863 }
4864 }
4865
4866 /* In FULL mode, an incomplete symbol doesn't match. */
4867 {
4868 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4869 {});
4870 }
4871
4872 /* A complete symbol with parameters matches any overload, since the
4873 index has no overload info. */
4874 {
4875 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4876 EXPECT ("std::zfunction", "std::zfunction2"));
4877 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4878 EXPECT ("std::zfunction", "std::zfunction2"));
4879 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4880 EXPECT ("std::zfunction", "std::zfunction2"));
4881 }
4882
4883 /* Check that whitespace is ignored appropriately. A symbol with a
4884 template argument list. */
4885 {
4886 static const char expected[] = "ns::foo<int>";
4887 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4888 EXPECT (expected));
4889 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4890 EXPECT (expected));
4891 }
4892
4893 /* Check that whitespace is ignored appropriately. A symbol with a
4894 template argument list that includes a pointer. */
4895 {
4896 static const char expected[] = "ns::foo<char*>";
4897 /* Try both completion and non-completion modes. */
4898 static const bool completion_mode[2] = {false, true};
4899 for (size_t i = 0; i < 2; i++)
4900 {
4901 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4902 completion_mode[i], EXPECT (expected));
4903 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4904 completion_mode[i], EXPECT (expected));
4905
4906 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4907 completion_mode[i], EXPECT (expected));
4908 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4909 completion_mode[i], EXPECT (expected));
4910 }
4911 }
4912
4913 {
4914 /* Check method qualifiers are ignored. */
4915 static const char expected[] = "ns::foo<char*>";
4916 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4917 symbol_name_match_type::FULL, true, EXPECT (expected));
4918 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4919 symbol_name_match_type::FULL, true, EXPECT (expected));
4920 CHECK_MATCH ("foo < char * > ( int ) const",
4921 symbol_name_match_type::WILD, true, EXPECT (expected));
4922 CHECK_MATCH ("foo < char * > ( int ) &&",
4923 symbol_name_match_type::WILD, true, EXPECT (expected));
4924 }
4925
4926 /* Test lookup names that don't match anything. */
4927 {
4928 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4929 {});
4930
4931 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4932 {});
4933 }
4934
4935 /* Some wild matching tests, exercising "(anonymous namespace)",
4936 which should not be confused with a parameter list. */
4937 {
4938 static const char *syms[] = {
4939 "A::B::C",
4940 "B::C",
4941 "C",
4942 "A :: B :: C ( int )",
4943 "B :: C ( int )",
4944 "C ( int )",
4945 };
4946
4947 for (const char *s : syms)
4948 {
4949 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4950 EXPECT ("(anonymous namespace)::A::B::C"));
4951 }
4952 }
4953
4954 {
4955 static const char expected[] = "ns2::tmpl<int>::foo2";
4956 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4957 EXPECT (expected));
4958 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4959 EXPECT (expected));
4960 }
4961
4962 SELF_CHECK (!any_mismatch);
4963
4964 #undef EXPECT
4965 #undef CHECK_MATCH
4966 }
4967
4968 static void
4969 run_test ()
4970 {
4971 test_mapped_index_find_name_component_bounds ();
4972 test_dw2_expand_symtabs_matching_symbol ();
4973 }
4974
4975 }} // namespace selftests::dw2_expand_symtabs_matching
4976
4977 #endif /* GDB_SELF_TEST */
4978
4979 /* If FILE_MATCHER is NULL or if PER_CU has
4980 dwarf2_per_cu_quick_data::MARK set (see
4981 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4982 EXPANSION_NOTIFY on it. */
4983
4984 static void
4985 dw2_expand_symtabs_matching_one
4986 (struct dwarf2_per_cu_data *per_cu,
4987 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4988 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4989 {
4990 if (file_matcher == NULL || per_cu->v.quick->mark)
4991 {
4992 bool symtab_was_null
4993 = (per_cu->v.quick->compunit_symtab == NULL);
4994
4995 dw2_instantiate_symtab (per_cu, false);
4996
4997 if (expansion_notify != NULL
4998 && symtab_was_null
4999 && per_cu->v.quick->compunit_symtab != NULL)
5000 expansion_notify (per_cu->v.quick->compunit_symtab);
5001 }
5002 }
5003
5004 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5005 matched, to expand corresponding CUs that were marked. IDX is the
5006 index of the symbol name that matched. */
5007
5008 static void
5009 dw2_expand_marked_cus
5010 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5011 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5012 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5013 search_domain kind)
5014 {
5015 offset_type *vec, vec_len, vec_idx;
5016 bool global_seen = false;
5017 mapped_index &index = *dwarf2_per_objfile->index_table;
5018
5019 vec = (offset_type *) (index.constant_pool
5020 + MAYBE_SWAP (index.symbol_table[idx].vec));
5021 vec_len = MAYBE_SWAP (vec[0]);
5022 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5023 {
5024 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5025 /* This value is only valid for index versions >= 7. */
5026 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5027 gdb_index_symbol_kind symbol_kind =
5028 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5029 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5030 /* Only check the symbol attributes if they're present.
5031 Indices prior to version 7 don't record them,
5032 and indices >= 7 may elide them for certain symbols
5033 (gold does this). */
5034 int attrs_valid =
5035 (index.version >= 7
5036 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5037
5038 /* Work around gold/15646. */
5039 if (attrs_valid)
5040 {
5041 if (!is_static && global_seen)
5042 continue;
5043 if (!is_static)
5044 global_seen = true;
5045 }
5046
5047 /* Only check the symbol's kind if it has one. */
5048 if (attrs_valid)
5049 {
5050 switch (kind)
5051 {
5052 case VARIABLES_DOMAIN:
5053 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5054 continue;
5055 break;
5056 case FUNCTIONS_DOMAIN:
5057 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5058 continue;
5059 break;
5060 case TYPES_DOMAIN:
5061 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5062 continue;
5063 break;
5064 case MODULES_DOMAIN:
5065 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
5066 continue;
5067 break;
5068 default:
5069 break;
5070 }
5071 }
5072
5073 /* Don't crash on bad data. */
5074 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5075 + dwarf2_per_objfile->all_type_units.size ()))
5076 {
5077 complaint (_(".gdb_index entry has bad CU index"
5078 " [in module %s]"),
5079 objfile_name (dwarf2_per_objfile->objfile));
5080 continue;
5081 }
5082
5083 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5084 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5085 expansion_notify);
5086 }
5087 }
5088
5089 /* If FILE_MATCHER is non-NULL, set all the
5090 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5091 that match FILE_MATCHER. */
5092
5093 static void
5094 dw_expand_symtabs_matching_file_matcher
5095 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5096 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5097 {
5098 if (file_matcher == NULL)
5099 return;
5100
5101 objfile *const objfile = dwarf2_per_objfile->objfile;
5102
5103 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5104 htab_eq_pointer,
5105 NULL, xcalloc, xfree));
5106 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5107 htab_eq_pointer,
5108 NULL, xcalloc, xfree));
5109
5110 /* The rule is CUs specify all the files, including those used by
5111 any TU, so there's no need to scan TUs here. */
5112
5113 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5114 {
5115 QUIT;
5116
5117 per_cu->v.quick->mark = 0;
5118
5119 /* We only need to look at symtabs not already expanded. */
5120 if (per_cu->v.quick->compunit_symtab)
5121 continue;
5122
5123 quick_file_names *file_data = dw2_get_file_names (per_cu);
5124 if (file_data == NULL)
5125 continue;
5126
5127 if (htab_find (visited_not_found.get (), file_data) != NULL)
5128 continue;
5129 else if (htab_find (visited_found.get (), file_data) != NULL)
5130 {
5131 per_cu->v.quick->mark = 1;
5132 continue;
5133 }
5134
5135 for (int j = 0; j < file_data->num_file_names; ++j)
5136 {
5137 const char *this_real_name;
5138
5139 if (file_matcher (file_data->file_names[j], false))
5140 {
5141 per_cu->v.quick->mark = 1;
5142 break;
5143 }
5144
5145 /* Before we invoke realpath, which can get expensive when many
5146 files are involved, do a quick comparison of the basenames. */
5147 if (!basenames_may_differ
5148 && !file_matcher (lbasename (file_data->file_names[j]),
5149 true))
5150 continue;
5151
5152 this_real_name = dw2_get_real_path (objfile, file_data, j);
5153 if (file_matcher (this_real_name, false))
5154 {
5155 per_cu->v.quick->mark = 1;
5156 break;
5157 }
5158 }
5159
5160 void **slot = htab_find_slot (per_cu->v.quick->mark
5161 ? visited_found.get ()
5162 : visited_not_found.get (),
5163 file_data, INSERT);
5164 *slot = file_data;
5165 }
5166 }
5167
5168 static void
5169 dw2_expand_symtabs_matching
5170 (struct objfile *objfile,
5171 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5172 const lookup_name_info &lookup_name,
5173 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5174 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5175 enum search_domain kind)
5176 {
5177 struct dwarf2_per_objfile *dwarf2_per_objfile
5178 = get_dwarf2_per_objfile (objfile);
5179
5180 /* index_table is NULL if OBJF_READNOW. */
5181 if (!dwarf2_per_objfile->index_table)
5182 return;
5183
5184 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5185
5186 mapped_index &index = *dwarf2_per_objfile->index_table;
5187
5188 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5189 symbol_matcher,
5190 kind, [&] (offset_type idx)
5191 {
5192 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5193 expansion_notify, kind);
5194 return true;
5195 });
5196 }
5197
5198 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5199 symtab. */
5200
5201 static struct compunit_symtab *
5202 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5203 CORE_ADDR pc)
5204 {
5205 int i;
5206
5207 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5208 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5209 return cust;
5210
5211 if (cust->includes == NULL)
5212 return NULL;
5213
5214 for (i = 0; cust->includes[i]; ++i)
5215 {
5216 struct compunit_symtab *s = cust->includes[i];
5217
5218 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5219 if (s != NULL)
5220 return s;
5221 }
5222
5223 return NULL;
5224 }
5225
5226 static struct compunit_symtab *
5227 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5228 struct bound_minimal_symbol msymbol,
5229 CORE_ADDR pc,
5230 struct obj_section *section,
5231 int warn_if_readin)
5232 {
5233 struct dwarf2_per_cu_data *data;
5234 struct compunit_symtab *result;
5235
5236 if (!objfile->partial_symtabs->psymtabs_addrmap)
5237 return NULL;
5238
5239 CORE_ADDR baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
5240 data = (struct dwarf2_per_cu_data *) addrmap_find
5241 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
5242 if (!data)
5243 return NULL;
5244
5245 if (warn_if_readin && data->v.quick->compunit_symtab)
5246 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5247 paddress (get_objfile_arch (objfile), pc));
5248
5249 result
5250 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5251 false),
5252 pc);
5253 gdb_assert (result != NULL);
5254 return result;
5255 }
5256
5257 static void
5258 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5259 void *data, int need_fullname)
5260 {
5261 struct dwarf2_per_objfile *dwarf2_per_objfile
5262 = get_dwarf2_per_objfile (objfile);
5263
5264 if (!dwarf2_per_objfile->filenames_cache)
5265 {
5266 dwarf2_per_objfile->filenames_cache.emplace ();
5267
5268 htab_up visited (htab_create_alloc (10,
5269 htab_hash_pointer, htab_eq_pointer,
5270 NULL, xcalloc, xfree));
5271
5272 /* The rule is CUs specify all the files, including those used
5273 by any TU, so there's no need to scan TUs here. We can
5274 ignore file names coming from already-expanded CUs. */
5275
5276 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5277 {
5278 if (per_cu->v.quick->compunit_symtab)
5279 {
5280 void **slot = htab_find_slot (visited.get (),
5281 per_cu->v.quick->file_names,
5282 INSERT);
5283
5284 *slot = per_cu->v.quick->file_names;
5285 }
5286 }
5287
5288 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5289 {
5290 /* We only need to look at symtabs not already expanded. */
5291 if (per_cu->v.quick->compunit_symtab)
5292 continue;
5293
5294 quick_file_names *file_data = dw2_get_file_names (per_cu);
5295 if (file_data == NULL)
5296 continue;
5297
5298 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5299 if (*slot)
5300 {
5301 /* Already visited. */
5302 continue;
5303 }
5304 *slot = file_data;
5305
5306 for (int j = 0; j < file_data->num_file_names; ++j)
5307 {
5308 const char *filename = file_data->file_names[j];
5309 dwarf2_per_objfile->filenames_cache->seen (filename);
5310 }
5311 }
5312 }
5313
5314 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5315 {
5316 gdb::unique_xmalloc_ptr<char> this_real_name;
5317
5318 if (need_fullname)
5319 this_real_name = gdb_realpath (filename);
5320 (*fun) (filename, this_real_name.get (), data);
5321 });
5322 }
5323
5324 static int
5325 dw2_has_symbols (struct objfile *objfile)
5326 {
5327 return 1;
5328 }
5329
5330 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5331 {
5332 dw2_has_symbols,
5333 dw2_find_last_source_symtab,
5334 dw2_forget_cached_source_info,
5335 dw2_map_symtabs_matching_filename,
5336 dw2_lookup_symbol,
5337 dw2_print_stats,
5338 dw2_dump,
5339 dw2_expand_symtabs_for_function,
5340 dw2_expand_all_symtabs,
5341 dw2_expand_symtabs_with_fullname,
5342 dw2_map_matching_symbols,
5343 dw2_expand_symtabs_matching,
5344 dw2_find_pc_sect_compunit_symtab,
5345 NULL,
5346 dw2_map_symbol_filenames
5347 };
5348
5349 /* DWARF-5 debug_names reader. */
5350
5351 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5352 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5353
5354 /* A helper function that reads the .debug_names section in SECTION
5355 and fills in MAP. FILENAME is the name of the file containing the
5356 section; it is used for error reporting.
5357
5358 Returns true if all went well, false otherwise. */
5359
5360 static bool
5361 read_debug_names_from_section (struct objfile *objfile,
5362 const char *filename,
5363 struct dwarf2_section_info *section,
5364 mapped_debug_names &map)
5365 {
5366 if (dwarf2_section_empty_p (section))
5367 return false;
5368
5369 /* Older elfutils strip versions could keep the section in the main
5370 executable while splitting it for the separate debug info file. */
5371 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5372 return false;
5373
5374 dwarf2_read_section (objfile, section);
5375
5376 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5377
5378 const gdb_byte *addr = section->buffer;
5379
5380 bfd *const abfd = get_section_bfd_owner (section);
5381
5382 unsigned int bytes_read;
5383 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5384 addr += bytes_read;
5385
5386 map.dwarf5_is_dwarf64 = bytes_read != 4;
5387 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5388 if (bytes_read + length != section->size)
5389 {
5390 /* There may be multiple per-CU indices. */
5391 warning (_("Section .debug_names in %s length %s does not match "
5392 "section length %s, ignoring .debug_names."),
5393 filename, plongest (bytes_read + length),
5394 pulongest (section->size));
5395 return false;
5396 }
5397
5398 /* The version number. */
5399 uint16_t version = read_2_bytes (abfd, addr);
5400 addr += 2;
5401 if (version != 5)
5402 {
5403 warning (_("Section .debug_names in %s has unsupported version %d, "
5404 "ignoring .debug_names."),
5405 filename, version);
5406 return false;
5407 }
5408
5409 /* Padding. */
5410 uint16_t padding = read_2_bytes (abfd, addr);
5411 addr += 2;
5412 if (padding != 0)
5413 {
5414 warning (_("Section .debug_names in %s has unsupported padding %d, "
5415 "ignoring .debug_names."),
5416 filename, padding);
5417 return false;
5418 }
5419
5420 /* comp_unit_count - The number of CUs in the CU list. */
5421 map.cu_count = read_4_bytes (abfd, addr);
5422 addr += 4;
5423
5424 /* local_type_unit_count - The number of TUs in the local TU
5425 list. */
5426 map.tu_count = read_4_bytes (abfd, addr);
5427 addr += 4;
5428
5429 /* foreign_type_unit_count - The number of TUs in the foreign TU
5430 list. */
5431 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5432 addr += 4;
5433 if (foreign_tu_count != 0)
5434 {
5435 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5436 "ignoring .debug_names."),
5437 filename, static_cast<unsigned long> (foreign_tu_count));
5438 return false;
5439 }
5440
5441 /* bucket_count - The number of hash buckets in the hash lookup
5442 table. */
5443 map.bucket_count = read_4_bytes (abfd, addr);
5444 addr += 4;
5445
5446 /* name_count - The number of unique names in the index. */
5447 map.name_count = read_4_bytes (abfd, addr);
5448 addr += 4;
5449
5450 /* abbrev_table_size - The size in bytes of the abbreviations
5451 table. */
5452 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5453 addr += 4;
5454
5455 /* augmentation_string_size - The size in bytes of the augmentation
5456 string. This value is rounded up to a multiple of 4. */
5457 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5458 addr += 4;
5459 map.augmentation_is_gdb = ((augmentation_string_size
5460 == sizeof (dwarf5_augmentation))
5461 && memcmp (addr, dwarf5_augmentation,
5462 sizeof (dwarf5_augmentation)) == 0);
5463 augmentation_string_size += (-augmentation_string_size) & 3;
5464 addr += augmentation_string_size;
5465
5466 /* List of CUs */
5467 map.cu_table_reordered = addr;
5468 addr += map.cu_count * map.offset_size;
5469
5470 /* List of Local TUs */
5471 map.tu_table_reordered = addr;
5472 addr += map.tu_count * map.offset_size;
5473
5474 /* Hash Lookup Table */
5475 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5476 addr += map.bucket_count * 4;
5477 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5478 addr += map.name_count * 4;
5479
5480 /* Name Table */
5481 map.name_table_string_offs_reordered = addr;
5482 addr += map.name_count * map.offset_size;
5483 map.name_table_entry_offs_reordered = addr;
5484 addr += map.name_count * map.offset_size;
5485
5486 const gdb_byte *abbrev_table_start = addr;
5487 for (;;)
5488 {
5489 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5490 addr += bytes_read;
5491 if (index_num == 0)
5492 break;
5493
5494 const auto insertpair
5495 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5496 if (!insertpair.second)
5497 {
5498 warning (_("Section .debug_names in %s has duplicate index %s, "
5499 "ignoring .debug_names."),
5500 filename, pulongest (index_num));
5501 return false;
5502 }
5503 mapped_debug_names::index_val &indexval = insertpair.first->second;
5504 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5505 addr += bytes_read;
5506
5507 for (;;)
5508 {
5509 mapped_debug_names::index_val::attr attr;
5510 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5511 addr += bytes_read;
5512 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5513 addr += bytes_read;
5514 if (attr.form == DW_FORM_implicit_const)
5515 {
5516 attr.implicit_const = read_signed_leb128 (abfd, addr,
5517 &bytes_read);
5518 addr += bytes_read;
5519 }
5520 if (attr.dw_idx == 0 && attr.form == 0)
5521 break;
5522 indexval.attr_vec.push_back (std::move (attr));
5523 }
5524 }
5525 if (addr != abbrev_table_start + abbrev_table_size)
5526 {
5527 warning (_("Section .debug_names in %s has abbreviation_table "
5528 "of size %s vs. written as %u, ignoring .debug_names."),
5529 filename, plongest (addr - abbrev_table_start),
5530 abbrev_table_size);
5531 return false;
5532 }
5533 map.entry_pool = addr;
5534
5535 return true;
5536 }
5537
5538 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5539 list. */
5540
5541 static void
5542 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5543 const mapped_debug_names &map,
5544 dwarf2_section_info &section,
5545 bool is_dwz)
5546 {
5547 sect_offset sect_off_prev;
5548 for (uint32_t i = 0; i <= map.cu_count; ++i)
5549 {
5550 sect_offset sect_off_next;
5551 if (i < map.cu_count)
5552 {
5553 sect_off_next
5554 = (sect_offset) (extract_unsigned_integer
5555 (map.cu_table_reordered + i * map.offset_size,
5556 map.offset_size,
5557 map.dwarf5_byte_order));
5558 }
5559 else
5560 sect_off_next = (sect_offset) section.size;
5561 if (i >= 1)
5562 {
5563 const ULONGEST length = sect_off_next - sect_off_prev;
5564 dwarf2_per_cu_data *per_cu
5565 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5566 sect_off_prev, length);
5567 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5568 }
5569 sect_off_prev = sect_off_next;
5570 }
5571 }
5572
5573 /* Read the CU list from the mapped index, and use it to create all
5574 the CU objects for this dwarf2_per_objfile. */
5575
5576 static void
5577 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5578 const mapped_debug_names &map,
5579 const mapped_debug_names &dwz_map)
5580 {
5581 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5582 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5583
5584 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5585 dwarf2_per_objfile->info,
5586 false /* is_dwz */);
5587
5588 if (dwz_map.cu_count == 0)
5589 return;
5590
5591 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5592 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5593 true /* is_dwz */);
5594 }
5595
5596 /* Read .debug_names. If everything went ok, initialize the "quick"
5597 elements of all the CUs and return true. Otherwise, return false. */
5598
5599 static bool
5600 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5601 {
5602 std::unique_ptr<mapped_debug_names> map
5603 (new mapped_debug_names (dwarf2_per_objfile));
5604 mapped_debug_names dwz_map (dwarf2_per_objfile);
5605 struct objfile *objfile = dwarf2_per_objfile->objfile;
5606
5607 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5608 &dwarf2_per_objfile->debug_names,
5609 *map))
5610 return false;
5611
5612 /* Don't use the index if it's empty. */
5613 if (map->name_count == 0)
5614 return false;
5615
5616 /* If there is a .dwz file, read it so we can get its CU list as
5617 well. */
5618 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5619 if (dwz != NULL)
5620 {
5621 if (!read_debug_names_from_section (objfile,
5622 bfd_get_filename (dwz->dwz_bfd.get ()),
5623 &dwz->debug_names, dwz_map))
5624 {
5625 warning (_("could not read '.debug_names' section from %s; skipping"),
5626 bfd_get_filename (dwz->dwz_bfd.get ()));
5627 return false;
5628 }
5629 }
5630
5631 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5632
5633 if (map->tu_count != 0)
5634 {
5635 /* We can only handle a single .debug_types when we have an
5636 index. */
5637 if (dwarf2_per_objfile->types.size () != 1)
5638 return false;
5639
5640 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5641
5642 create_signatured_type_table_from_debug_names
5643 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5644 }
5645
5646 create_addrmap_from_aranges (dwarf2_per_objfile,
5647 &dwarf2_per_objfile->debug_aranges);
5648
5649 dwarf2_per_objfile->debug_names_table = std::move (map);
5650 dwarf2_per_objfile->using_index = 1;
5651 dwarf2_per_objfile->quick_file_names_table =
5652 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5653
5654 return true;
5655 }
5656
5657 /* Type used to manage iterating over all CUs looking for a symbol for
5658 .debug_names. */
5659
5660 class dw2_debug_names_iterator
5661 {
5662 public:
5663 dw2_debug_names_iterator (const mapped_debug_names &map,
5664 gdb::optional<block_enum> block_index,
5665 domain_enum domain,
5666 const char *name)
5667 : m_map (map), m_block_index (block_index), m_domain (domain),
5668 m_addr (find_vec_in_debug_names (map, name))
5669 {}
5670
5671 dw2_debug_names_iterator (const mapped_debug_names &map,
5672 search_domain search, uint32_t namei)
5673 : m_map (map),
5674 m_search (search),
5675 m_addr (find_vec_in_debug_names (map, namei))
5676 {}
5677
5678 dw2_debug_names_iterator (const mapped_debug_names &map,
5679 block_enum block_index, domain_enum domain,
5680 uint32_t namei)
5681 : m_map (map), m_block_index (block_index), m_domain (domain),
5682 m_addr (find_vec_in_debug_names (map, namei))
5683 {}
5684
5685 /* Return the next matching CU or NULL if there are no more. */
5686 dwarf2_per_cu_data *next ();
5687
5688 private:
5689 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5690 const char *name);
5691 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5692 uint32_t namei);
5693
5694 /* The internalized form of .debug_names. */
5695 const mapped_debug_names &m_map;
5696
5697 /* If set, only look for symbols that match that block. Valid values are
5698 GLOBAL_BLOCK and STATIC_BLOCK. */
5699 const gdb::optional<block_enum> m_block_index;
5700
5701 /* The kind of symbol we're looking for. */
5702 const domain_enum m_domain = UNDEF_DOMAIN;
5703 const search_domain m_search = ALL_DOMAIN;
5704
5705 /* The list of CUs from the index entry of the symbol, or NULL if
5706 not found. */
5707 const gdb_byte *m_addr;
5708 };
5709
5710 const char *
5711 mapped_debug_names::namei_to_name (uint32_t namei) const
5712 {
5713 const ULONGEST namei_string_offs
5714 = extract_unsigned_integer ((name_table_string_offs_reordered
5715 + namei * offset_size),
5716 offset_size,
5717 dwarf5_byte_order);
5718 return read_indirect_string_at_offset
5719 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5720 }
5721
5722 /* Find a slot in .debug_names for the object named NAME. If NAME is
5723 found, return pointer to its pool data. If NAME cannot be found,
5724 return NULL. */
5725
5726 const gdb_byte *
5727 dw2_debug_names_iterator::find_vec_in_debug_names
5728 (const mapped_debug_names &map, const char *name)
5729 {
5730 int (*cmp) (const char *, const char *);
5731
5732 gdb::unique_xmalloc_ptr<char> without_params;
5733 if (current_language->la_language == language_cplus
5734 || current_language->la_language == language_fortran
5735 || current_language->la_language == language_d)
5736 {
5737 /* NAME is already canonical. Drop any qualifiers as
5738 .debug_names does not contain any. */
5739
5740 if (strchr (name, '(') != NULL)
5741 {
5742 without_params = cp_remove_params (name);
5743 if (without_params != NULL)
5744 name = without_params.get ();
5745 }
5746 }
5747
5748 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5749
5750 const uint32_t full_hash = dwarf5_djb_hash (name);
5751 uint32_t namei
5752 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5753 (map.bucket_table_reordered
5754 + (full_hash % map.bucket_count)), 4,
5755 map.dwarf5_byte_order);
5756 if (namei == 0)
5757 return NULL;
5758 --namei;
5759 if (namei >= map.name_count)
5760 {
5761 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5762 "[in module %s]"),
5763 namei, map.name_count,
5764 objfile_name (map.dwarf2_per_objfile->objfile));
5765 return NULL;
5766 }
5767
5768 for (;;)
5769 {
5770 const uint32_t namei_full_hash
5771 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5772 (map.hash_table_reordered + namei), 4,
5773 map.dwarf5_byte_order);
5774 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5775 return NULL;
5776
5777 if (full_hash == namei_full_hash)
5778 {
5779 const char *const namei_string = map.namei_to_name (namei);
5780
5781 #if 0 /* An expensive sanity check. */
5782 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5783 {
5784 complaint (_("Wrong .debug_names hash for string at index %u "
5785 "[in module %s]"),
5786 namei, objfile_name (dwarf2_per_objfile->objfile));
5787 return NULL;
5788 }
5789 #endif
5790
5791 if (cmp (namei_string, name) == 0)
5792 {
5793 const ULONGEST namei_entry_offs
5794 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5795 + namei * map.offset_size),
5796 map.offset_size, map.dwarf5_byte_order);
5797 return map.entry_pool + namei_entry_offs;
5798 }
5799 }
5800
5801 ++namei;
5802 if (namei >= map.name_count)
5803 return NULL;
5804 }
5805 }
5806
5807 const gdb_byte *
5808 dw2_debug_names_iterator::find_vec_in_debug_names
5809 (const mapped_debug_names &map, uint32_t namei)
5810 {
5811 if (namei >= map.name_count)
5812 {
5813 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5814 "[in module %s]"),
5815 namei, map.name_count,
5816 objfile_name (map.dwarf2_per_objfile->objfile));
5817 return NULL;
5818 }
5819
5820 const ULONGEST namei_entry_offs
5821 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5822 + namei * map.offset_size),
5823 map.offset_size, map.dwarf5_byte_order);
5824 return map.entry_pool + namei_entry_offs;
5825 }
5826
5827 /* See dw2_debug_names_iterator. */
5828
5829 dwarf2_per_cu_data *
5830 dw2_debug_names_iterator::next ()
5831 {
5832 if (m_addr == NULL)
5833 return NULL;
5834
5835 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5836 struct objfile *objfile = dwarf2_per_objfile->objfile;
5837 bfd *const abfd = objfile->obfd;
5838
5839 again:
5840
5841 unsigned int bytes_read;
5842 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5843 m_addr += bytes_read;
5844 if (abbrev == 0)
5845 return NULL;
5846
5847 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5848 if (indexval_it == m_map.abbrev_map.cend ())
5849 {
5850 complaint (_("Wrong .debug_names undefined abbrev code %s "
5851 "[in module %s]"),
5852 pulongest (abbrev), objfile_name (objfile));
5853 return NULL;
5854 }
5855 const mapped_debug_names::index_val &indexval = indexval_it->second;
5856 enum class symbol_linkage {
5857 unknown,
5858 static_,
5859 extern_,
5860 } symbol_linkage_ = symbol_linkage::unknown;
5861 dwarf2_per_cu_data *per_cu = NULL;
5862 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5863 {
5864 ULONGEST ull;
5865 switch (attr.form)
5866 {
5867 case DW_FORM_implicit_const:
5868 ull = attr.implicit_const;
5869 break;
5870 case DW_FORM_flag_present:
5871 ull = 1;
5872 break;
5873 case DW_FORM_udata:
5874 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5875 m_addr += bytes_read;
5876 break;
5877 default:
5878 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5879 dwarf_form_name (attr.form),
5880 objfile_name (objfile));
5881 return NULL;
5882 }
5883 switch (attr.dw_idx)
5884 {
5885 case DW_IDX_compile_unit:
5886 /* Don't crash on bad data. */
5887 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5888 {
5889 complaint (_(".debug_names entry has bad CU index %s"
5890 " [in module %s]"),
5891 pulongest (ull),
5892 objfile_name (dwarf2_per_objfile->objfile));
5893 continue;
5894 }
5895 per_cu = dwarf2_per_objfile->get_cutu (ull);
5896 break;
5897 case DW_IDX_type_unit:
5898 /* Don't crash on bad data. */
5899 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5900 {
5901 complaint (_(".debug_names entry has bad TU index %s"
5902 " [in module %s]"),
5903 pulongest (ull),
5904 objfile_name (dwarf2_per_objfile->objfile));
5905 continue;
5906 }
5907 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5908 break;
5909 case DW_IDX_GNU_internal:
5910 if (!m_map.augmentation_is_gdb)
5911 break;
5912 symbol_linkage_ = symbol_linkage::static_;
5913 break;
5914 case DW_IDX_GNU_external:
5915 if (!m_map.augmentation_is_gdb)
5916 break;
5917 symbol_linkage_ = symbol_linkage::extern_;
5918 break;
5919 }
5920 }
5921
5922 /* Skip if already read in. */
5923 if (per_cu->v.quick->compunit_symtab)
5924 goto again;
5925
5926 /* Check static vs global. */
5927 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5928 {
5929 const bool want_static = *m_block_index == STATIC_BLOCK;
5930 const bool symbol_is_static =
5931 symbol_linkage_ == symbol_linkage::static_;
5932 if (want_static != symbol_is_static)
5933 goto again;
5934 }
5935
5936 /* Match dw2_symtab_iter_next, symbol_kind
5937 and debug_names::psymbol_tag. */
5938 switch (m_domain)
5939 {
5940 case VAR_DOMAIN:
5941 switch (indexval.dwarf_tag)
5942 {
5943 case DW_TAG_variable:
5944 case DW_TAG_subprogram:
5945 /* Some types are also in VAR_DOMAIN. */
5946 case DW_TAG_typedef:
5947 case DW_TAG_structure_type:
5948 break;
5949 default:
5950 goto again;
5951 }
5952 break;
5953 case STRUCT_DOMAIN:
5954 switch (indexval.dwarf_tag)
5955 {
5956 case DW_TAG_typedef:
5957 case DW_TAG_structure_type:
5958 break;
5959 default:
5960 goto again;
5961 }
5962 break;
5963 case LABEL_DOMAIN:
5964 switch (indexval.dwarf_tag)
5965 {
5966 case 0:
5967 case DW_TAG_variable:
5968 break;
5969 default:
5970 goto again;
5971 }
5972 break;
5973 case MODULE_DOMAIN:
5974 switch (indexval.dwarf_tag)
5975 {
5976 case DW_TAG_module:
5977 break;
5978 default:
5979 goto again;
5980 }
5981 break;
5982 default:
5983 break;
5984 }
5985
5986 /* Match dw2_expand_symtabs_matching, symbol_kind and
5987 debug_names::psymbol_tag. */
5988 switch (m_search)
5989 {
5990 case VARIABLES_DOMAIN:
5991 switch (indexval.dwarf_tag)
5992 {
5993 case DW_TAG_variable:
5994 break;
5995 default:
5996 goto again;
5997 }
5998 break;
5999 case FUNCTIONS_DOMAIN:
6000 switch (indexval.dwarf_tag)
6001 {
6002 case DW_TAG_subprogram:
6003 break;
6004 default:
6005 goto again;
6006 }
6007 break;
6008 case TYPES_DOMAIN:
6009 switch (indexval.dwarf_tag)
6010 {
6011 case DW_TAG_typedef:
6012 case DW_TAG_structure_type:
6013 break;
6014 default:
6015 goto again;
6016 }
6017 break;
6018 case MODULES_DOMAIN:
6019 switch (indexval.dwarf_tag)
6020 {
6021 case DW_TAG_module:
6022 break;
6023 default:
6024 goto again;
6025 }
6026 default:
6027 break;
6028 }
6029
6030 return per_cu;
6031 }
6032
6033 static struct compunit_symtab *
6034 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
6035 const char *name, domain_enum domain)
6036 {
6037 struct dwarf2_per_objfile *dwarf2_per_objfile
6038 = get_dwarf2_per_objfile (objfile);
6039
6040 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6041 if (!mapp)
6042 {
6043 /* index is NULL if OBJF_READNOW. */
6044 return NULL;
6045 }
6046 const auto &map = *mapp;
6047
6048 dw2_debug_names_iterator iter (map, block_index, domain, name);
6049
6050 struct compunit_symtab *stab_best = NULL;
6051 struct dwarf2_per_cu_data *per_cu;
6052 while ((per_cu = iter.next ()) != NULL)
6053 {
6054 struct symbol *sym, *with_opaque = NULL;
6055 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
6056 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6057 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6058
6059 sym = block_find_symbol (block, name, domain,
6060 block_find_non_opaque_type_preferred,
6061 &with_opaque);
6062
6063 /* Some caution must be observed with overloaded functions and
6064 methods, since the index will not contain any overload
6065 information (but NAME might contain it). */
6066
6067 if (sym != NULL
6068 && strcmp_iw (sym->search_name (), name) == 0)
6069 return stab;
6070 if (with_opaque != NULL
6071 && strcmp_iw (with_opaque->search_name (), name) == 0)
6072 stab_best = stab;
6073
6074 /* Keep looking through other CUs. */
6075 }
6076
6077 return stab_best;
6078 }
6079
6080 /* This dumps minimal information about .debug_names. It is called
6081 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6082 uses this to verify that .debug_names has been loaded. */
6083
6084 static void
6085 dw2_debug_names_dump (struct objfile *objfile)
6086 {
6087 struct dwarf2_per_objfile *dwarf2_per_objfile
6088 = get_dwarf2_per_objfile (objfile);
6089
6090 gdb_assert (dwarf2_per_objfile->using_index);
6091 printf_filtered (".debug_names:");
6092 if (dwarf2_per_objfile->debug_names_table)
6093 printf_filtered (" exists\n");
6094 else
6095 printf_filtered (" faked for \"readnow\"\n");
6096 printf_filtered ("\n");
6097 }
6098
6099 static void
6100 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6101 const char *func_name)
6102 {
6103 struct dwarf2_per_objfile *dwarf2_per_objfile
6104 = get_dwarf2_per_objfile (objfile);
6105
6106 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6107 if (dwarf2_per_objfile->debug_names_table)
6108 {
6109 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6110
6111 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
6112
6113 struct dwarf2_per_cu_data *per_cu;
6114 while ((per_cu = iter.next ()) != NULL)
6115 dw2_instantiate_symtab (per_cu, false);
6116 }
6117 }
6118
6119 static void
6120 dw2_debug_names_map_matching_symbols
6121 (struct objfile *objfile,
6122 const lookup_name_info &name, domain_enum domain,
6123 int global,
6124 gdb::function_view<symbol_found_callback_ftype> callback,
6125 symbol_compare_ftype *ordered_compare)
6126 {
6127 struct dwarf2_per_objfile *dwarf2_per_objfile
6128 = get_dwarf2_per_objfile (objfile);
6129
6130 /* debug_names_table is NULL if OBJF_READNOW. */
6131 if (!dwarf2_per_objfile->debug_names_table)
6132 return;
6133
6134 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6135 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6136
6137 const char *match_name = name.ada ().lookup_name ().c_str ();
6138 auto matcher = [&] (const char *symname)
6139 {
6140 if (ordered_compare == nullptr)
6141 return true;
6142 return ordered_compare (symname, match_name) == 0;
6143 };
6144
6145 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6146 [&] (offset_type namei)
6147 {
6148 /* The name was matched, now expand corresponding CUs that were
6149 marked. */
6150 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6151
6152 struct dwarf2_per_cu_data *per_cu;
6153 while ((per_cu = iter.next ()) != NULL)
6154 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6155 return true;
6156 });
6157
6158 /* It's a shame we couldn't do this inside the
6159 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6160 that have already been expanded. Instead, this loop matches what
6161 the psymtab code does. */
6162 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6163 {
6164 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6165 if (cust != nullptr)
6166 {
6167 const struct block *block
6168 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6169 if (!iterate_over_symbols_terminated (block, name,
6170 domain, callback))
6171 break;
6172 }
6173 }
6174 }
6175
6176 static void
6177 dw2_debug_names_expand_symtabs_matching
6178 (struct objfile *objfile,
6179 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6180 const lookup_name_info &lookup_name,
6181 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6182 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6183 enum search_domain kind)
6184 {
6185 struct dwarf2_per_objfile *dwarf2_per_objfile
6186 = get_dwarf2_per_objfile (objfile);
6187
6188 /* debug_names_table is NULL if OBJF_READNOW. */
6189 if (!dwarf2_per_objfile->debug_names_table)
6190 return;
6191
6192 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6193
6194 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6195
6196 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6197 symbol_matcher,
6198 kind, [&] (offset_type namei)
6199 {
6200 /* The name was matched, now expand corresponding CUs that were
6201 marked. */
6202 dw2_debug_names_iterator iter (map, kind, namei);
6203
6204 struct dwarf2_per_cu_data *per_cu;
6205 while ((per_cu = iter.next ()) != NULL)
6206 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6207 expansion_notify);
6208 return true;
6209 });
6210 }
6211
6212 const struct quick_symbol_functions dwarf2_debug_names_functions =
6213 {
6214 dw2_has_symbols,
6215 dw2_find_last_source_symtab,
6216 dw2_forget_cached_source_info,
6217 dw2_map_symtabs_matching_filename,
6218 dw2_debug_names_lookup_symbol,
6219 dw2_print_stats,
6220 dw2_debug_names_dump,
6221 dw2_debug_names_expand_symtabs_for_function,
6222 dw2_expand_all_symtabs,
6223 dw2_expand_symtabs_with_fullname,
6224 dw2_debug_names_map_matching_symbols,
6225 dw2_debug_names_expand_symtabs_matching,
6226 dw2_find_pc_sect_compunit_symtab,
6227 NULL,
6228 dw2_map_symbol_filenames
6229 };
6230
6231 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6232 to either a dwarf2_per_objfile or dwz_file object. */
6233
6234 template <typename T>
6235 static gdb::array_view<const gdb_byte>
6236 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6237 {
6238 dwarf2_section_info *section = &section_owner->gdb_index;
6239
6240 if (dwarf2_section_empty_p (section))
6241 return {};
6242
6243 /* Older elfutils strip versions could keep the section in the main
6244 executable while splitting it for the separate debug info file. */
6245 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6246 return {};
6247
6248 dwarf2_read_section (obj, section);
6249
6250 /* dwarf2_section_info::size is a bfd_size_type, while
6251 gdb::array_view works with size_t. On 32-bit hosts, with
6252 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6253 is 32-bit. So we need an explicit narrowing conversion here.
6254 This is fine, because it's impossible to allocate or mmap an
6255 array/buffer larger than what size_t can represent. */
6256 return gdb::make_array_view (section->buffer, section->size);
6257 }
6258
6259 /* Lookup the index cache for the contents of the index associated to
6260 DWARF2_OBJ. */
6261
6262 static gdb::array_view<const gdb_byte>
6263 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6264 {
6265 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6266 if (build_id == nullptr)
6267 return {};
6268
6269 return global_index_cache.lookup_gdb_index (build_id,
6270 &dwarf2_obj->index_cache_res);
6271 }
6272
6273 /* Same as the above, but for DWZ. */
6274
6275 static gdb::array_view<const gdb_byte>
6276 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6277 {
6278 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6279 if (build_id == nullptr)
6280 return {};
6281
6282 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6283 }
6284
6285 /* See symfile.h. */
6286
6287 bool
6288 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6289 {
6290 struct dwarf2_per_objfile *dwarf2_per_objfile
6291 = get_dwarf2_per_objfile (objfile);
6292
6293 /* If we're about to read full symbols, don't bother with the
6294 indices. In this case we also don't care if some other debug
6295 format is making psymtabs, because they are all about to be
6296 expanded anyway. */
6297 if ((objfile->flags & OBJF_READNOW))
6298 {
6299 dwarf2_per_objfile->using_index = 1;
6300 create_all_comp_units (dwarf2_per_objfile);
6301 create_all_type_units (dwarf2_per_objfile);
6302 dwarf2_per_objfile->quick_file_names_table
6303 = create_quick_file_names_table
6304 (dwarf2_per_objfile->all_comp_units.size ());
6305
6306 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6307 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6308 {
6309 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6310
6311 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6312 struct dwarf2_per_cu_quick_data);
6313 }
6314
6315 /* Return 1 so that gdb sees the "quick" functions. However,
6316 these functions will be no-ops because we will have expanded
6317 all symtabs. */
6318 *index_kind = dw_index_kind::GDB_INDEX;
6319 return true;
6320 }
6321
6322 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6323 {
6324 *index_kind = dw_index_kind::DEBUG_NAMES;
6325 return true;
6326 }
6327
6328 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6329 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6330 get_gdb_index_contents_from_section<dwz_file>))
6331 {
6332 *index_kind = dw_index_kind::GDB_INDEX;
6333 return true;
6334 }
6335
6336 /* ... otherwise, try to find the index in the index cache. */
6337 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6338 get_gdb_index_contents_from_cache,
6339 get_gdb_index_contents_from_cache_dwz))
6340 {
6341 global_index_cache.hit ();
6342 *index_kind = dw_index_kind::GDB_INDEX;
6343 return true;
6344 }
6345
6346 global_index_cache.miss ();
6347 return false;
6348 }
6349
6350 \f
6351
6352 /* Build a partial symbol table. */
6353
6354 void
6355 dwarf2_build_psymtabs (struct objfile *objfile)
6356 {
6357 struct dwarf2_per_objfile *dwarf2_per_objfile
6358 = get_dwarf2_per_objfile (objfile);
6359
6360 init_psymbol_list (objfile, 1024);
6361
6362 try
6363 {
6364 /* This isn't really ideal: all the data we allocate on the
6365 objfile's obstack is still uselessly kept around. However,
6366 freeing it seems unsafe. */
6367 psymtab_discarder psymtabs (objfile);
6368 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6369 psymtabs.keep ();
6370
6371 /* (maybe) store an index in the cache. */
6372 global_index_cache.store (dwarf2_per_objfile);
6373 }
6374 catch (const gdb_exception_error &except)
6375 {
6376 exception_print (gdb_stderr, except);
6377 }
6378 }
6379
6380 /* Return the total length of the CU described by HEADER. */
6381
6382 static unsigned int
6383 get_cu_length (const struct comp_unit_head *header)
6384 {
6385 return header->initial_length_size + header->length;
6386 }
6387
6388 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6389
6390 static inline bool
6391 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6392 {
6393 sect_offset bottom = cu_header->sect_off;
6394 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6395
6396 return sect_off >= bottom && sect_off < top;
6397 }
6398
6399 /* Find the base address of the compilation unit for range lists and
6400 location lists. It will normally be specified by DW_AT_low_pc.
6401 In DWARF-3 draft 4, the base address could be overridden by
6402 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6403 compilation units with discontinuous ranges. */
6404
6405 static void
6406 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6407 {
6408 struct attribute *attr;
6409
6410 cu->base_known = 0;
6411 cu->base_address = 0;
6412
6413 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6414 if (attr != nullptr)
6415 {
6416 cu->base_address = attr_value_as_address (attr);
6417 cu->base_known = 1;
6418 }
6419 else
6420 {
6421 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6422 if (attr != nullptr)
6423 {
6424 cu->base_address = attr_value_as_address (attr);
6425 cu->base_known = 1;
6426 }
6427 }
6428 }
6429
6430 /* Read in the comp unit header information from the debug_info at info_ptr.
6431 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6432 NOTE: This leaves members offset, first_die_offset to be filled in
6433 by the caller. */
6434
6435 static const gdb_byte *
6436 read_comp_unit_head (struct comp_unit_head *cu_header,
6437 const gdb_byte *info_ptr,
6438 struct dwarf2_section_info *section,
6439 rcuh_kind section_kind)
6440 {
6441 int signed_addr;
6442 unsigned int bytes_read;
6443 const char *filename = get_section_file_name (section);
6444 bfd *abfd = get_section_bfd_owner (section);
6445
6446 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6447 cu_header->initial_length_size = bytes_read;
6448 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6449 info_ptr += bytes_read;
6450 cu_header->version = read_2_bytes (abfd, info_ptr);
6451 if (cu_header->version < 2 || cu_header->version > 5)
6452 error (_("Dwarf Error: wrong version in compilation unit header "
6453 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6454 cu_header->version, filename);
6455 info_ptr += 2;
6456 if (cu_header->version < 5)
6457 switch (section_kind)
6458 {
6459 case rcuh_kind::COMPILE:
6460 cu_header->unit_type = DW_UT_compile;
6461 break;
6462 case rcuh_kind::TYPE:
6463 cu_header->unit_type = DW_UT_type;
6464 break;
6465 default:
6466 internal_error (__FILE__, __LINE__,
6467 _("read_comp_unit_head: invalid section_kind"));
6468 }
6469 else
6470 {
6471 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6472 (read_1_byte (abfd, info_ptr));
6473 info_ptr += 1;
6474 switch (cu_header->unit_type)
6475 {
6476 case DW_UT_compile:
6477 case DW_UT_partial:
6478 case DW_UT_skeleton:
6479 case DW_UT_split_compile:
6480 if (section_kind != rcuh_kind::COMPILE)
6481 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6482 "(is %s, should be %s) [in module %s]"),
6483 dwarf_unit_type_name (cu_header->unit_type),
6484 dwarf_unit_type_name (DW_UT_type), filename);
6485 break;
6486 case DW_UT_type:
6487 case DW_UT_split_type:
6488 section_kind = rcuh_kind::TYPE;
6489 break;
6490 default:
6491 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6492 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6493 "[in module %s]"), cu_header->unit_type,
6494 dwarf_unit_type_name (DW_UT_compile),
6495 dwarf_unit_type_name (DW_UT_skeleton),
6496 dwarf_unit_type_name (DW_UT_split_compile),
6497 dwarf_unit_type_name (DW_UT_type),
6498 dwarf_unit_type_name (DW_UT_split_type), filename);
6499 }
6500
6501 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6502 info_ptr += 1;
6503 }
6504 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6505 cu_header,
6506 &bytes_read);
6507 info_ptr += bytes_read;
6508 if (cu_header->version < 5)
6509 {
6510 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6511 info_ptr += 1;
6512 }
6513 signed_addr = bfd_get_sign_extend_vma (abfd);
6514 if (signed_addr < 0)
6515 internal_error (__FILE__, __LINE__,
6516 _("read_comp_unit_head: dwarf from non elf file"));
6517 cu_header->signed_addr_p = signed_addr;
6518
6519 bool header_has_signature = section_kind == rcuh_kind::TYPE
6520 || cu_header->unit_type == DW_UT_skeleton
6521 || cu_header->unit_type == DW_UT_split_compile;
6522
6523 if (header_has_signature)
6524 {
6525 cu_header->signature = read_8_bytes (abfd, info_ptr);
6526 info_ptr += 8;
6527 }
6528
6529 if (section_kind == rcuh_kind::TYPE)
6530 {
6531 LONGEST type_offset;
6532 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6533 info_ptr += bytes_read;
6534 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6535 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6536 error (_("Dwarf Error: Too big type_offset in compilation unit "
6537 "header (is %s) [in module %s]"), plongest (type_offset),
6538 filename);
6539 }
6540
6541 return info_ptr;
6542 }
6543
6544 /* Helper function that returns the proper abbrev section for
6545 THIS_CU. */
6546
6547 static struct dwarf2_section_info *
6548 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6549 {
6550 struct dwarf2_section_info *abbrev;
6551 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6552
6553 if (this_cu->is_dwz)
6554 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6555 else
6556 abbrev = &dwarf2_per_objfile->abbrev;
6557
6558 return abbrev;
6559 }
6560
6561 /* Subroutine of read_and_check_comp_unit_head and
6562 read_and_check_type_unit_head to simplify them.
6563 Perform various error checking on the header. */
6564
6565 static void
6566 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6567 struct comp_unit_head *header,
6568 struct dwarf2_section_info *section,
6569 struct dwarf2_section_info *abbrev_section)
6570 {
6571 const char *filename = get_section_file_name (section);
6572
6573 if (to_underlying (header->abbrev_sect_off)
6574 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6575 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6576 "(offset %s + 6) [in module %s]"),
6577 sect_offset_str (header->abbrev_sect_off),
6578 sect_offset_str (header->sect_off),
6579 filename);
6580
6581 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6582 avoid potential 32-bit overflow. */
6583 if (((ULONGEST) header->sect_off + get_cu_length (header))
6584 > section->size)
6585 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6586 "(offset %s + 0) [in module %s]"),
6587 header->length, sect_offset_str (header->sect_off),
6588 filename);
6589 }
6590
6591 /* Read in a CU/TU header and perform some basic error checking.
6592 The contents of the header are stored in HEADER.
6593 The result is a pointer to the start of the first DIE. */
6594
6595 static const gdb_byte *
6596 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6597 struct comp_unit_head *header,
6598 struct dwarf2_section_info *section,
6599 struct dwarf2_section_info *abbrev_section,
6600 const gdb_byte *info_ptr,
6601 rcuh_kind section_kind)
6602 {
6603 const gdb_byte *beg_of_comp_unit = info_ptr;
6604
6605 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6606
6607 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6608
6609 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6610
6611 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6612 abbrev_section);
6613
6614 return info_ptr;
6615 }
6616
6617 /* Fetch the abbreviation table offset from a comp or type unit header. */
6618
6619 static sect_offset
6620 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6621 struct dwarf2_section_info *section,
6622 sect_offset sect_off)
6623 {
6624 bfd *abfd = get_section_bfd_owner (section);
6625 const gdb_byte *info_ptr;
6626 unsigned int initial_length_size, offset_size;
6627 uint16_t version;
6628
6629 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6630 info_ptr = section->buffer + to_underlying (sect_off);
6631 read_initial_length (abfd, info_ptr, &initial_length_size);
6632 offset_size = initial_length_size == 4 ? 4 : 8;
6633 info_ptr += initial_length_size;
6634
6635 version = read_2_bytes (abfd, info_ptr);
6636 info_ptr += 2;
6637 if (version >= 5)
6638 {
6639 /* Skip unit type and address size. */
6640 info_ptr += 2;
6641 }
6642
6643 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6644 }
6645
6646 /* Allocate a new partial symtab for file named NAME and mark this new
6647 partial symtab as being an include of PST. */
6648
6649 static void
6650 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6651 struct objfile *objfile)
6652 {
6653 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6654
6655 if (!IS_ABSOLUTE_PATH (subpst->filename))
6656 {
6657 /* It shares objfile->objfile_obstack. */
6658 subpst->dirname = pst->dirname;
6659 }
6660
6661 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6662 subpst->dependencies[0] = pst;
6663 subpst->number_of_dependencies = 1;
6664
6665 subpst->read_symtab = pst->read_symtab;
6666
6667 /* No private part is necessary for include psymtabs. This property
6668 can be used to differentiate between such include psymtabs and
6669 the regular ones. */
6670 subpst->read_symtab_private = NULL;
6671 }
6672
6673 /* Read the Line Number Program data and extract the list of files
6674 included by the source file represented by PST. Build an include
6675 partial symtab for each of these included files. */
6676
6677 static void
6678 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6679 struct die_info *die,
6680 struct partial_symtab *pst)
6681 {
6682 line_header_up lh;
6683 struct attribute *attr;
6684
6685 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6686 if (attr != nullptr)
6687 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6688 if (lh == NULL)
6689 return; /* No linetable, so no includes. */
6690
6691 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6692 that we pass in the raw text_low here; that is ok because we're
6693 only decoding the line table to make include partial symtabs, and
6694 so the addresses aren't really used. */
6695 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6696 pst->raw_text_low (), 1);
6697 }
6698
6699 static hashval_t
6700 hash_signatured_type (const void *item)
6701 {
6702 const struct signatured_type *sig_type
6703 = (const struct signatured_type *) item;
6704
6705 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6706 return sig_type->signature;
6707 }
6708
6709 static int
6710 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6711 {
6712 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6713 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6714
6715 return lhs->signature == rhs->signature;
6716 }
6717
6718 /* Allocate a hash table for signatured types. */
6719
6720 static htab_t
6721 allocate_signatured_type_table (struct objfile *objfile)
6722 {
6723 return htab_create_alloc_ex (41,
6724 hash_signatured_type,
6725 eq_signatured_type,
6726 NULL,
6727 &objfile->objfile_obstack,
6728 hashtab_obstack_allocate,
6729 dummy_obstack_deallocate);
6730 }
6731
6732 /* A helper function to add a signatured type CU to a table. */
6733
6734 static int
6735 add_signatured_type_cu_to_table (void **slot, void *datum)
6736 {
6737 struct signatured_type *sigt = (struct signatured_type *) *slot;
6738 std::vector<signatured_type *> *all_type_units
6739 = (std::vector<signatured_type *> *) datum;
6740
6741 all_type_units->push_back (sigt);
6742
6743 return 1;
6744 }
6745
6746 /* A helper for create_debug_types_hash_table. Read types from SECTION
6747 and fill them into TYPES_HTAB. It will process only type units,
6748 therefore DW_UT_type. */
6749
6750 static void
6751 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6752 struct dwo_file *dwo_file,
6753 dwarf2_section_info *section, htab_t &types_htab,
6754 rcuh_kind section_kind)
6755 {
6756 struct objfile *objfile = dwarf2_per_objfile->objfile;
6757 struct dwarf2_section_info *abbrev_section;
6758 bfd *abfd;
6759 const gdb_byte *info_ptr, *end_ptr;
6760
6761 abbrev_section = (dwo_file != NULL
6762 ? &dwo_file->sections.abbrev
6763 : &dwarf2_per_objfile->abbrev);
6764
6765 if (dwarf_read_debug)
6766 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6767 get_section_name (section),
6768 get_section_file_name (abbrev_section));
6769
6770 dwarf2_read_section (objfile, section);
6771 info_ptr = section->buffer;
6772
6773 if (info_ptr == NULL)
6774 return;
6775
6776 /* We can't set abfd until now because the section may be empty or
6777 not present, in which case the bfd is unknown. */
6778 abfd = get_section_bfd_owner (section);
6779
6780 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6781 because we don't need to read any dies: the signature is in the
6782 header. */
6783
6784 end_ptr = info_ptr + section->size;
6785 while (info_ptr < end_ptr)
6786 {
6787 struct signatured_type *sig_type;
6788 struct dwo_unit *dwo_tu;
6789 void **slot;
6790 const gdb_byte *ptr = info_ptr;
6791 struct comp_unit_head header;
6792 unsigned int length;
6793
6794 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6795
6796 /* Initialize it due to a false compiler warning. */
6797 header.signature = -1;
6798 header.type_cu_offset_in_tu = (cu_offset) -1;
6799
6800 /* We need to read the type's signature in order to build the hash
6801 table, but we don't need anything else just yet. */
6802
6803 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6804 abbrev_section, ptr, section_kind);
6805
6806 length = get_cu_length (&header);
6807
6808 /* Skip dummy type units. */
6809 if (ptr >= info_ptr + length
6810 || peek_abbrev_code (abfd, ptr) == 0
6811 || header.unit_type != DW_UT_type)
6812 {
6813 info_ptr += length;
6814 continue;
6815 }
6816
6817 if (types_htab == NULL)
6818 {
6819 if (dwo_file)
6820 types_htab = allocate_dwo_unit_table (objfile);
6821 else
6822 types_htab = allocate_signatured_type_table (objfile);
6823 }
6824
6825 if (dwo_file)
6826 {
6827 sig_type = NULL;
6828 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6829 struct dwo_unit);
6830 dwo_tu->dwo_file = dwo_file;
6831 dwo_tu->signature = header.signature;
6832 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6833 dwo_tu->section = section;
6834 dwo_tu->sect_off = sect_off;
6835 dwo_tu->length = length;
6836 }
6837 else
6838 {
6839 /* N.B.: type_offset is not usable if this type uses a DWO file.
6840 The real type_offset is in the DWO file. */
6841 dwo_tu = NULL;
6842 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6843 struct signatured_type);
6844 sig_type->signature = header.signature;
6845 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6846 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6847 sig_type->per_cu.is_debug_types = 1;
6848 sig_type->per_cu.section = section;
6849 sig_type->per_cu.sect_off = sect_off;
6850 sig_type->per_cu.length = length;
6851 }
6852
6853 slot = htab_find_slot (types_htab,
6854 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6855 INSERT);
6856 gdb_assert (slot != NULL);
6857 if (*slot != NULL)
6858 {
6859 sect_offset dup_sect_off;
6860
6861 if (dwo_file)
6862 {
6863 const struct dwo_unit *dup_tu
6864 = (const struct dwo_unit *) *slot;
6865
6866 dup_sect_off = dup_tu->sect_off;
6867 }
6868 else
6869 {
6870 const struct signatured_type *dup_tu
6871 = (const struct signatured_type *) *slot;
6872
6873 dup_sect_off = dup_tu->per_cu.sect_off;
6874 }
6875
6876 complaint (_("debug type entry at offset %s is duplicate to"
6877 " the entry at offset %s, signature %s"),
6878 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6879 hex_string (header.signature));
6880 }
6881 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6882
6883 if (dwarf_read_debug > 1)
6884 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6885 sect_offset_str (sect_off),
6886 hex_string (header.signature));
6887
6888 info_ptr += length;
6889 }
6890 }
6891
6892 /* Create the hash table of all entries in the .debug_types
6893 (or .debug_types.dwo) section(s).
6894 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6895 otherwise it is NULL.
6896
6897 The result is a pointer to the hash table or NULL if there are no types.
6898
6899 Note: This function processes DWO files only, not DWP files. */
6900
6901 static void
6902 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6903 struct dwo_file *dwo_file,
6904 gdb::array_view<dwarf2_section_info> type_sections,
6905 htab_t &types_htab)
6906 {
6907 for (dwarf2_section_info &section : type_sections)
6908 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6909 types_htab, rcuh_kind::TYPE);
6910 }
6911
6912 /* Create the hash table of all entries in the .debug_types section,
6913 and initialize all_type_units.
6914 The result is zero if there is an error (e.g. missing .debug_types section),
6915 otherwise non-zero. */
6916
6917 static int
6918 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6919 {
6920 htab_t types_htab = NULL;
6921
6922 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6923 &dwarf2_per_objfile->info, types_htab,
6924 rcuh_kind::COMPILE);
6925 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6926 dwarf2_per_objfile->types, types_htab);
6927 if (types_htab == NULL)
6928 {
6929 dwarf2_per_objfile->signatured_types = NULL;
6930 return 0;
6931 }
6932
6933 dwarf2_per_objfile->signatured_types = types_htab;
6934
6935 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6936 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6937
6938 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6939 &dwarf2_per_objfile->all_type_units);
6940
6941 return 1;
6942 }
6943
6944 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6945 If SLOT is non-NULL, it is the entry to use in the hash table.
6946 Otherwise we find one. */
6947
6948 static struct signatured_type *
6949 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6950 void **slot)
6951 {
6952 struct objfile *objfile = dwarf2_per_objfile->objfile;
6953
6954 if (dwarf2_per_objfile->all_type_units.size ()
6955 == dwarf2_per_objfile->all_type_units.capacity ())
6956 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6957
6958 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6959 struct signatured_type);
6960
6961 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6962 sig_type->signature = sig;
6963 sig_type->per_cu.is_debug_types = 1;
6964 if (dwarf2_per_objfile->using_index)
6965 {
6966 sig_type->per_cu.v.quick =
6967 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6968 struct dwarf2_per_cu_quick_data);
6969 }
6970
6971 if (slot == NULL)
6972 {
6973 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6974 sig_type, INSERT);
6975 }
6976 gdb_assert (*slot == NULL);
6977 *slot = sig_type;
6978 /* The rest of sig_type must be filled in by the caller. */
6979 return sig_type;
6980 }
6981
6982 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6983 Fill in SIG_ENTRY with DWO_ENTRY. */
6984
6985 static void
6986 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6987 struct signatured_type *sig_entry,
6988 struct dwo_unit *dwo_entry)
6989 {
6990 /* Make sure we're not clobbering something we don't expect to. */
6991 gdb_assert (! sig_entry->per_cu.queued);
6992 gdb_assert (sig_entry->per_cu.cu == NULL);
6993 if (dwarf2_per_objfile->using_index)
6994 {
6995 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6996 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6997 }
6998 else
6999 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7000 gdb_assert (sig_entry->signature == dwo_entry->signature);
7001 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7002 gdb_assert (sig_entry->type_unit_group == NULL);
7003 gdb_assert (sig_entry->dwo_unit == NULL);
7004
7005 sig_entry->per_cu.section = dwo_entry->section;
7006 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7007 sig_entry->per_cu.length = dwo_entry->length;
7008 sig_entry->per_cu.reading_dwo_directly = 1;
7009 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7010 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7011 sig_entry->dwo_unit = dwo_entry;
7012 }
7013
7014 /* Subroutine of lookup_signatured_type.
7015 If we haven't read the TU yet, create the signatured_type data structure
7016 for a TU to be read in directly from a DWO file, bypassing the stub.
7017 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7018 using .gdb_index, then when reading a CU we want to stay in the DWO file
7019 containing that CU. Otherwise we could end up reading several other DWO
7020 files (due to comdat folding) to process the transitive closure of all the
7021 mentioned TUs, and that can be slow. The current DWO file will have every
7022 type signature that it needs.
7023 We only do this for .gdb_index because in the psymtab case we already have
7024 to read all the DWOs to build the type unit groups. */
7025
7026 static struct signatured_type *
7027 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7028 {
7029 struct dwarf2_per_objfile *dwarf2_per_objfile
7030 = cu->per_cu->dwarf2_per_objfile;
7031 struct objfile *objfile = dwarf2_per_objfile->objfile;
7032 struct dwo_file *dwo_file;
7033 struct dwo_unit find_dwo_entry, *dwo_entry;
7034 struct signatured_type find_sig_entry, *sig_entry;
7035 void **slot;
7036
7037 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7038
7039 /* If TU skeletons have been removed then we may not have read in any
7040 TUs yet. */
7041 if (dwarf2_per_objfile->signatured_types == NULL)
7042 {
7043 dwarf2_per_objfile->signatured_types
7044 = allocate_signatured_type_table (objfile);
7045 }
7046
7047 /* We only ever need to read in one copy of a signatured type.
7048 Use the global signatured_types array to do our own comdat-folding
7049 of types. If this is the first time we're reading this TU, and
7050 the TU has an entry in .gdb_index, replace the recorded data from
7051 .gdb_index with this TU. */
7052
7053 find_sig_entry.signature = sig;
7054 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7055 &find_sig_entry, INSERT);
7056 sig_entry = (struct signatured_type *) *slot;
7057
7058 /* We can get here with the TU already read, *or* in the process of being
7059 read. Don't reassign the global entry to point to this DWO if that's
7060 the case. Also note that if the TU is already being read, it may not
7061 have come from a DWO, the program may be a mix of Fission-compiled
7062 code and non-Fission-compiled code. */
7063
7064 /* Have we already tried to read this TU?
7065 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7066 needn't exist in the global table yet). */
7067 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7068 return sig_entry;
7069
7070 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7071 dwo_unit of the TU itself. */
7072 dwo_file = cu->dwo_unit->dwo_file;
7073
7074 /* Ok, this is the first time we're reading this TU. */
7075 if (dwo_file->tus == NULL)
7076 return NULL;
7077 find_dwo_entry.signature = sig;
7078 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7079 if (dwo_entry == NULL)
7080 return NULL;
7081
7082 /* If the global table doesn't have an entry for this TU, add one. */
7083 if (sig_entry == NULL)
7084 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7085
7086 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7087 sig_entry->per_cu.tu_read = 1;
7088 return sig_entry;
7089 }
7090
7091 /* Subroutine of lookup_signatured_type.
7092 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7093 then try the DWP file. If the TU stub (skeleton) has been removed then
7094 it won't be in .gdb_index. */
7095
7096 static struct signatured_type *
7097 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7098 {
7099 struct dwarf2_per_objfile *dwarf2_per_objfile
7100 = cu->per_cu->dwarf2_per_objfile;
7101 struct objfile *objfile = dwarf2_per_objfile->objfile;
7102 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7103 struct dwo_unit *dwo_entry;
7104 struct signatured_type find_sig_entry, *sig_entry;
7105 void **slot;
7106
7107 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7108 gdb_assert (dwp_file != NULL);
7109
7110 /* If TU skeletons have been removed then we may not have read in any
7111 TUs yet. */
7112 if (dwarf2_per_objfile->signatured_types == NULL)
7113 {
7114 dwarf2_per_objfile->signatured_types
7115 = allocate_signatured_type_table (objfile);
7116 }
7117
7118 find_sig_entry.signature = sig;
7119 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7120 &find_sig_entry, INSERT);
7121 sig_entry = (struct signatured_type *) *slot;
7122
7123 /* Have we already tried to read this TU?
7124 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7125 needn't exist in the global table yet). */
7126 if (sig_entry != NULL)
7127 return sig_entry;
7128
7129 if (dwp_file->tus == NULL)
7130 return NULL;
7131 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7132 sig, 1 /* is_debug_types */);
7133 if (dwo_entry == NULL)
7134 return NULL;
7135
7136 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7137 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7138
7139 return sig_entry;
7140 }
7141
7142 /* Lookup a signature based type for DW_FORM_ref_sig8.
7143 Returns NULL if signature SIG is not present in the table.
7144 It is up to the caller to complain about this. */
7145
7146 static struct signatured_type *
7147 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7148 {
7149 struct dwarf2_per_objfile *dwarf2_per_objfile
7150 = cu->per_cu->dwarf2_per_objfile;
7151
7152 if (cu->dwo_unit
7153 && dwarf2_per_objfile->using_index)
7154 {
7155 /* We're in a DWO/DWP file, and we're using .gdb_index.
7156 These cases require special processing. */
7157 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7158 return lookup_dwo_signatured_type (cu, sig);
7159 else
7160 return lookup_dwp_signatured_type (cu, sig);
7161 }
7162 else
7163 {
7164 struct signatured_type find_entry, *entry;
7165
7166 if (dwarf2_per_objfile->signatured_types == NULL)
7167 return NULL;
7168 find_entry.signature = sig;
7169 entry = ((struct signatured_type *)
7170 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7171 return entry;
7172 }
7173 }
7174 \f
7175 /* Low level DIE reading support. */
7176
7177 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7178
7179 static void
7180 init_cu_die_reader (struct die_reader_specs *reader,
7181 struct dwarf2_cu *cu,
7182 struct dwarf2_section_info *section,
7183 struct dwo_file *dwo_file,
7184 struct abbrev_table *abbrev_table)
7185 {
7186 gdb_assert (section->readin && section->buffer != NULL);
7187 reader->abfd = get_section_bfd_owner (section);
7188 reader->cu = cu;
7189 reader->dwo_file = dwo_file;
7190 reader->die_section = section;
7191 reader->buffer = section->buffer;
7192 reader->buffer_end = section->buffer + section->size;
7193 reader->comp_dir = NULL;
7194 reader->abbrev_table = abbrev_table;
7195 }
7196
7197 /* Subroutine of init_cutu_and_read_dies to simplify it.
7198 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7199 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7200 already.
7201
7202 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7203 from it to the DIE in the DWO. If NULL we are skipping the stub.
7204 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7205 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7206 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7207 STUB_COMP_DIR may be non-NULL.
7208 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7209 are filled in with the info of the DIE from the DWO file.
7210 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7211 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7212 kept around for at least as long as *RESULT_READER.
7213
7214 The result is non-zero if a valid (non-dummy) DIE was found. */
7215
7216 static int
7217 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7218 struct dwo_unit *dwo_unit,
7219 struct die_info *stub_comp_unit_die,
7220 const char *stub_comp_dir,
7221 struct die_reader_specs *result_reader,
7222 const gdb_byte **result_info_ptr,
7223 struct die_info **result_comp_unit_die,
7224 int *result_has_children,
7225 abbrev_table_up *result_dwo_abbrev_table)
7226 {
7227 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7228 struct objfile *objfile = dwarf2_per_objfile->objfile;
7229 struct dwarf2_cu *cu = this_cu->cu;
7230 bfd *abfd;
7231 const gdb_byte *begin_info_ptr, *info_ptr;
7232 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7233 int i,num_extra_attrs;
7234 struct dwarf2_section_info *dwo_abbrev_section;
7235 struct attribute *attr;
7236 struct die_info *comp_unit_die;
7237
7238 /* At most one of these may be provided. */
7239 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7240
7241 /* These attributes aren't processed until later:
7242 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7243 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7244 referenced later. However, these attributes are found in the stub
7245 which we won't have later. In order to not impose this complication
7246 on the rest of the code, we read them here and copy them to the
7247 DWO CU/TU die. */
7248
7249 stmt_list = NULL;
7250 low_pc = NULL;
7251 high_pc = NULL;
7252 ranges = NULL;
7253 comp_dir = NULL;
7254
7255 if (stub_comp_unit_die != NULL)
7256 {
7257 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7258 DWO file. */
7259 if (! this_cu->is_debug_types)
7260 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7261 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7262 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7263 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7264 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7265
7266 /* There should be a DW_AT_addr_base attribute here (if needed).
7267 We need the value before we can process DW_FORM_GNU_addr_index
7268 or DW_FORM_addrx. */
7269 cu->addr_base = 0;
7270 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7271 if (attr != nullptr)
7272 cu->addr_base = DW_UNSND (attr);
7273
7274 /* There should be a DW_AT_ranges_base attribute here (if needed).
7275 We need the value before we can process DW_AT_ranges. */
7276 cu->ranges_base = 0;
7277 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7278 if (attr != nullptr)
7279 cu->ranges_base = DW_UNSND (attr);
7280 }
7281 else if (stub_comp_dir != NULL)
7282 {
7283 /* Reconstruct the comp_dir attribute to simplify the code below. */
7284 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7285 comp_dir->name = DW_AT_comp_dir;
7286 comp_dir->form = DW_FORM_string;
7287 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7288 DW_STRING (comp_dir) = stub_comp_dir;
7289 }
7290
7291 /* Set up for reading the DWO CU/TU. */
7292 cu->dwo_unit = dwo_unit;
7293 dwarf2_section_info *section = dwo_unit->section;
7294 dwarf2_read_section (objfile, section);
7295 abfd = get_section_bfd_owner (section);
7296 begin_info_ptr = info_ptr = (section->buffer
7297 + to_underlying (dwo_unit->sect_off));
7298 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7299
7300 if (this_cu->is_debug_types)
7301 {
7302 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7303
7304 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7305 &cu->header, section,
7306 dwo_abbrev_section,
7307 info_ptr, rcuh_kind::TYPE);
7308 /* This is not an assert because it can be caused by bad debug info. */
7309 if (sig_type->signature != cu->header.signature)
7310 {
7311 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7312 " TU at offset %s [in module %s]"),
7313 hex_string (sig_type->signature),
7314 hex_string (cu->header.signature),
7315 sect_offset_str (dwo_unit->sect_off),
7316 bfd_get_filename (abfd));
7317 }
7318 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7319 /* For DWOs coming from DWP files, we don't know the CU length
7320 nor the type's offset in the TU until now. */
7321 dwo_unit->length = get_cu_length (&cu->header);
7322 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7323
7324 /* Establish the type offset that can be used to lookup the type.
7325 For DWO files, we don't know it until now. */
7326 sig_type->type_offset_in_section
7327 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7328 }
7329 else
7330 {
7331 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7332 &cu->header, section,
7333 dwo_abbrev_section,
7334 info_ptr, rcuh_kind::COMPILE);
7335 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7336 /* For DWOs coming from DWP files, we don't know the CU length
7337 until now. */
7338 dwo_unit->length = get_cu_length (&cu->header);
7339 }
7340
7341 *result_dwo_abbrev_table
7342 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7343 cu->header.abbrev_sect_off);
7344 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7345 result_dwo_abbrev_table->get ());
7346
7347 /* Read in the die, but leave space to copy over the attributes
7348 from the stub. This has the benefit of simplifying the rest of
7349 the code - all the work to maintain the illusion of a single
7350 DW_TAG_{compile,type}_unit DIE is done here. */
7351 num_extra_attrs = ((stmt_list != NULL)
7352 + (low_pc != NULL)
7353 + (high_pc != NULL)
7354 + (ranges != NULL)
7355 + (comp_dir != NULL));
7356 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7357 result_has_children, num_extra_attrs);
7358
7359 /* Copy over the attributes from the stub to the DIE we just read in. */
7360 comp_unit_die = *result_comp_unit_die;
7361 i = comp_unit_die->num_attrs;
7362 if (stmt_list != NULL)
7363 comp_unit_die->attrs[i++] = *stmt_list;
7364 if (low_pc != NULL)
7365 comp_unit_die->attrs[i++] = *low_pc;
7366 if (high_pc != NULL)
7367 comp_unit_die->attrs[i++] = *high_pc;
7368 if (ranges != NULL)
7369 comp_unit_die->attrs[i++] = *ranges;
7370 if (comp_dir != NULL)
7371 comp_unit_die->attrs[i++] = *comp_dir;
7372 comp_unit_die->num_attrs += num_extra_attrs;
7373
7374 if (dwarf_die_debug)
7375 {
7376 fprintf_unfiltered (gdb_stdlog,
7377 "Read die from %s@0x%x of %s:\n",
7378 get_section_name (section),
7379 (unsigned) (begin_info_ptr - section->buffer),
7380 bfd_get_filename (abfd));
7381 dump_die (comp_unit_die, dwarf_die_debug);
7382 }
7383
7384 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7385 TUs by skipping the stub and going directly to the entry in the DWO file.
7386 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7387 to get it via circuitous means. Blech. */
7388 if (comp_dir != NULL)
7389 result_reader->comp_dir = DW_STRING (comp_dir);
7390
7391 /* Skip dummy compilation units. */
7392 if (info_ptr >= begin_info_ptr + dwo_unit->length
7393 || peek_abbrev_code (abfd, info_ptr) == 0)
7394 return 0;
7395
7396 *result_info_ptr = info_ptr;
7397 return 1;
7398 }
7399
7400 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7401 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7402 signature is part of the header. */
7403 static gdb::optional<ULONGEST>
7404 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7405 {
7406 if (cu->header.version >= 5)
7407 return cu->header.signature;
7408 struct attribute *attr;
7409 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7410 if (attr == nullptr)
7411 return gdb::optional<ULONGEST> ();
7412 return DW_UNSND (attr);
7413 }
7414
7415 /* Subroutine of init_cutu_and_read_dies to simplify it.
7416 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7417 Returns NULL if the specified DWO unit cannot be found. */
7418
7419 static struct dwo_unit *
7420 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7421 struct die_info *comp_unit_die)
7422 {
7423 struct dwarf2_cu *cu = this_cu->cu;
7424 struct dwo_unit *dwo_unit;
7425 const char *comp_dir, *dwo_name;
7426
7427 gdb_assert (cu != NULL);
7428
7429 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7430 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7431 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7432
7433 if (this_cu->is_debug_types)
7434 {
7435 struct signatured_type *sig_type;
7436
7437 /* Since this_cu is the first member of struct signatured_type,
7438 we can go from a pointer to one to a pointer to the other. */
7439 sig_type = (struct signatured_type *) this_cu;
7440 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7441 }
7442 else
7443 {
7444 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7445 if (!signature.has_value ())
7446 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7447 " [in module %s]"),
7448 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7449 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7450 *signature);
7451 }
7452
7453 return dwo_unit;
7454 }
7455
7456 /* Subroutine of init_cutu_and_read_dies to simplify it.
7457 See it for a description of the parameters.
7458 Read a TU directly from a DWO file, bypassing the stub. */
7459
7460 static void
7461 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7462 int use_existing_cu, int keep,
7463 die_reader_func_ftype *die_reader_func,
7464 void *data)
7465 {
7466 std::unique_ptr<dwarf2_cu> new_cu;
7467 struct signatured_type *sig_type;
7468 struct die_reader_specs reader;
7469 const gdb_byte *info_ptr;
7470 struct die_info *comp_unit_die;
7471 int has_children;
7472 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7473
7474 /* Verify we can do the following downcast, and that we have the
7475 data we need. */
7476 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7477 sig_type = (struct signatured_type *) this_cu;
7478 gdb_assert (sig_type->dwo_unit != NULL);
7479
7480 if (use_existing_cu && this_cu->cu != NULL)
7481 {
7482 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7483 /* There's no need to do the rereading_dwo_cu handling that
7484 init_cutu_and_read_dies does since we don't read the stub. */
7485 }
7486 else
7487 {
7488 /* If !use_existing_cu, this_cu->cu must be NULL. */
7489 gdb_assert (this_cu->cu == NULL);
7490 new_cu.reset (new dwarf2_cu (this_cu));
7491 }
7492
7493 /* A future optimization, if needed, would be to use an existing
7494 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7495 could share abbrev tables. */
7496
7497 /* The abbreviation table used by READER, this must live at least as long as
7498 READER. */
7499 abbrev_table_up dwo_abbrev_table;
7500
7501 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7502 NULL /* stub_comp_unit_die */,
7503 sig_type->dwo_unit->dwo_file->comp_dir,
7504 &reader, &info_ptr,
7505 &comp_unit_die, &has_children,
7506 &dwo_abbrev_table) == 0)
7507 {
7508 /* Dummy die. */
7509 return;
7510 }
7511
7512 /* All the "real" work is done here. */
7513 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7514
7515 /* This duplicates the code in init_cutu_and_read_dies,
7516 but the alternative is making the latter more complex.
7517 This function is only for the special case of using DWO files directly:
7518 no point in overly complicating the general case just to handle this. */
7519 if (new_cu != NULL && keep)
7520 {
7521 /* Link this CU into read_in_chain. */
7522 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7523 dwarf2_per_objfile->read_in_chain = this_cu;
7524 /* The chain owns it now. */
7525 new_cu.release ();
7526 }
7527 }
7528
7529 /* Initialize a CU (or TU) and read its DIEs.
7530 If the CU defers to a DWO file, read the DWO file as well.
7531
7532 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7533 Otherwise the table specified in the comp unit header is read in and used.
7534 This is an optimization for when we already have the abbrev table.
7535
7536 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7537 Otherwise, a new CU is allocated with xmalloc.
7538
7539 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7540 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7541
7542 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7543 linker) then DIE_READER_FUNC will not get called. */
7544
7545 static void
7546 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7547 struct abbrev_table *abbrev_table,
7548 int use_existing_cu, int keep,
7549 bool skip_partial,
7550 die_reader_func_ftype *die_reader_func,
7551 void *data)
7552 {
7553 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7554 struct objfile *objfile = dwarf2_per_objfile->objfile;
7555 struct dwarf2_section_info *section = this_cu->section;
7556 bfd *abfd = get_section_bfd_owner (section);
7557 struct dwarf2_cu *cu;
7558 const gdb_byte *begin_info_ptr, *info_ptr;
7559 struct die_reader_specs reader;
7560 struct die_info *comp_unit_die;
7561 int has_children;
7562 struct signatured_type *sig_type = NULL;
7563 struct dwarf2_section_info *abbrev_section;
7564 /* Non-zero if CU currently points to a DWO file and we need to
7565 reread it. When this happens we need to reread the skeleton die
7566 before we can reread the DWO file (this only applies to CUs, not TUs). */
7567 int rereading_dwo_cu = 0;
7568
7569 if (dwarf_die_debug)
7570 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7571 this_cu->is_debug_types ? "type" : "comp",
7572 sect_offset_str (this_cu->sect_off));
7573
7574 if (use_existing_cu)
7575 gdb_assert (keep);
7576
7577 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7578 file (instead of going through the stub), short-circuit all of this. */
7579 if (this_cu->reading_dwo_directly)
7580 {
7581 /* Narrow down the scope of possibilities to have to understand. */
7582 gdb_assert (this_cu->is_debug_types);
7583 gdb_assert (abbrev_table == NULL);
7584 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7585 die_reader_func, data);
7586 return;
7587 }
7588
7589 /* This is cheap if the section is already read in. */
7590 dwarf2_read_section (objfile, section);
7591
7592 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7593
7594 abbrev_section = get_abbrev_section_for_cu (this_cu);
7595
7596 std::unique_ptr<dwarf2_cu> new_cu;
7597 if (use_existing_cu && this_cu->cu != NULL)
7598 {
7599 cu = this_cu->cu;
7600 /* If this CU is from a DWO file we need to start over, we need to
7601 refetch the attributes from the skeleton CU.
7602 This could be optimized by retrieving those attributes from when we
7603 were here the first time: the previous comp_unit_die was stored in
7604 comp_unit_obstack. But there's no data yet that we need this
7605 optimization. */
7606 if (cu->dwo_unit != NULL)
7607 rereading_dwo_cu = 1;
7608 }
7609 else
7610 {
7611 /* If !use_existing_cu, this_cu->cu must be NULL. */
7612 gdb_assert (this_cu->cu == NULL);
7613 new_cu.reset (new dwarf2_cu (this_cu));
7614 cu = new_cu.get ();
7615 }
7616
7617 /* Get the header. */
7618 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7619 {
7620 /* We already have the header, there's no need to read it in again. */
7621 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7622 }
7623 else
7624 {
7625 if (this_cu->is_debug_types)
7626 {
7627 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7628 &cu->header, section,
7629 abbrev_section, info_ptr,
7630 rcuh_kind::TYPE);
7631
7632 /* Since per_cu is the first member of struct signatured_type,
7633 we can go from a pointer to one to a pointer to the other. */
7634 sig_type = (struct signatured_type *) this_cu;
7635 gdb_assert (sig_type->signature == cu->header.signature);
7636 gdb_assert (sig_type->type_offset_in_tu
7637 == cu->header.type_cu_offset_in_tu);
7638 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7639
7640 /* LENGTH has not been set yet for type units if we're
7641 using .gdb_index. */
7642 this_cu->length = get_cu_length (&cu->header);
7643
7644 /* Establish the type offset that can be used to lookup the type. */
7645 sig_type->type_offset_in_section =
7646 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7647
7648 this_cu->dwarf_version = cu->header.version;
7649 }
7650 else
7651 {
7652 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7653 &cu->header, section,
7654 abbrev_section,
7655 info_ptr,
7656 rcuh_kind::COMPILE);
7657
7658 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7659 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7660 this_cu->dwarf_version = cu->header.version;
7661 }
7662 }
7663
7664 /* Skip dummy compilation units. */
7665 if (info_ptr >= begin_info_ptr + this_cu->length
7666 || peek_abbrev_code (abfd, info_ptr) == 0)
7667 return;
7668
7669 /* If we don't have them yet, read the abbrevs for this compilation unit.
7670 And if we need to read them now, make sure they're freed when we're
7671 done (own the table through ABBREV_TABLE_HOLDER). */
7672 abbrev_table_up abbrev_table_holder;
7673 if (abbrev_table != NULL)
7674 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7675 else
7676 {
7677 abbrev_table_holder
7678 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7679 cu->header.abbrev_sect_off);
7680 abbrev_table = abbrev_table_holder.get ();
7681 }
7682
7683 /* Read the top level CU/TU die. */
7684 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7685 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7686
7687 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7688 return;
7689
7690 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7691 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7692 table from the DWO file and pass the ownership over to us. It will be
7693 referenced from READER, so we must make sure to free it after we're done
7694 with READER.
7695
7696 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7697 DWO CU, that this test will fail (the attribute will not be present). */
7698 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7699 abbrev_table_up dwo_abbrev_table;
7700 if (dwo_name != nullptr)
7701 {
7702 struct dwo_unit *dwo_unit;
7703 struct die_info *dwo_comp_unit_die;
7704
7705 if (has_children)
7706 {
7707 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7708 " has children (offset %s) [in module %s]"),
7709 sect_offset_str (this_cu->sect_off),
7710 bfd_get_filename (abfd));
7711 }
7712 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7713 if (dwo_unit != NULL)
7714 {
7715 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7716 comp_unit_die, NULL,
7717 &reader, &info_ptr,
7718 &dwo_comp_unit_die, &has_children,
7719 &dwo_abbrev_table) == 0)
7720 {
7721 /* Dummy die. */
7722 return;
7723 }
7724 comp_unit_die = dwo_comp_unit_die;
7725 }
7726 else
7727 {
7728 /* Yikes, we couldn't find the rest of the DIE, we only have
7729 the stub. A complaint has already been logged. There's
7730 not much more we can do except pass on the stub DIE to
7731 die_reader_func. We don't want to throw an error on bad
7732 debug info. */
7733 }
7734 }
7735
7736 /* All of the above is setup for this call. Yikes. */
7737 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7738
7739 /* Done, clean up. */
7740 if (new_cu != NULL && keep)
7741 {
7742 /* Link this CU into read_in_chain. */
7743 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7744 dwarf2_per_objfile->read_in_chain = this_cu;
7745 /* The chain owns it now. */
7746 new_cu.release ();
7747 }
7748 }
7749
7750 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7751 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7752 to have already done the lookup to find the DWO file).
7753
7754 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7755 THIS_CU->is_debug_types, but nothing else.
7756
7757 We fill in THIS_CU->length.
7758
7759 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7760 linker) then DIE_READER_FUNC will not get called.
7761
7762 THIS_CU->cu is always freed when done.
7763 This is done in order to not leave THIS_CU->cu in a state where we have
7764 to care whether it refers to the "main" CU or the DWO CU. */
7765
7766 static void
7767 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7768 struct dwo_file *dwo_file,
7769 die_reader_func_ftype *die_reader_func,
7770 void *data)
7771 {
7772 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7773 struct objfile *objfile = dwarf2_per_objfile->objfile;
7774 struct dwarf2_section_info *section = this_cu->section;
7775 bfd *abfd = get_section_bfd_owner (section);
7776 struct dwarf2_section_info *abbrev_section;
7777 const gdb_byte *begin_info_ptr, *info_ptr;
7778 struct die_reader_specs reader;
7779 struct die_info *comp_unit_die;
7780 int has_children;
7781
7782 if (dwarf_die_debug)
7783 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7784 this_cu->is_debug_types ? "type" : "comp",
7785 sect_offset_str (this_cu->sect_off));
7786
7787 gdb_assert (this_cu->cu == NULL);
7788
7789 abbrev_section = (dwo_file != NULL
7790 ? &dwo_file->sections.abbrev
7791 : get_abbrev_section_for_cu (this_cu));
7792
7793 /* This is cheap if the section is already read in. */
7794 dwarf2_read_section (objfile, section);
7795
7796 struct dwarf2_cu cu (this_cu);
7797
7798 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7799 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7800 &cu.header, section,
7801 abbrev_section, info_ptr,
7802 (this_cu->is_debug_types
7803 ? rcuh_kind::TYPE
7804 : rcuh_kind::COMPILE));
7805
7806 this_cu->length = get_cu_length (&cu.header);
7807
7808 /* Skip dummy compilation units. */
7809 if (info_ptr >= begin_info_ptr + this_cu->length
7810 || peek_abbrev_code (abfd, info_ptr) == 0)
7811 return;
7812
7813 abbrev_table_up abbrev_table
7814 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7815 cu.header.abbrev_sect_off);
7816
7817 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
7818 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7819
7820 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7821 }
7822
7823 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7824 does not lookup the specified DWO file.
7825 This cannot be used to read DWO files.
7826
7827 THIS_CU->cu is always freed when done.
7828 This is done in order to not leave THIS_CU->cu in a state where we have
7829 to care whether it refers to the "main" CU or the DWO CU.
7830 We can revisit this if the data shows there's a performance issue. */
7831
7832 static void
7833 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7834 die_reader_func_ftype *die_reader_func,
7835 void *data)
7836 {
7837 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
7838 }
7839 \f
7840 /* Type Unit Groups.
7841
7842 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7843 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7844 so that all types coming from the same compilation (.o file) are grouped
7845 together. A future step could be to put the types in the same symtab as
7846 the CU the types ultimately came from. */
7847
7848 static hashval_t
7849 hash_type_unit_group (const void *item)
7850 {
7851 const struct type_unit_group *tu_group
7852 = (const struct type_unit_group *) item;
7853
7854 return hash_stmt_list_entry (&tu_group->hash);
7855 }
7856
7857 static int
7858 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7859 {
7860 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7861 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7862
7863 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7864 }
7865
7866 /* Allocate a hash table for type unit groups. */
7867
7868 static htab_t
7869 allocate_type_unit_groups_table (struct objfile *objfile)
7870 {
7871 return htab_create_alloc_ex (3,
7872 hash_type_unit_group,
7873 eq_type_unit_group,
7874 NULL,
7875 &objfile->objfile_obstack,
7876 hashtab_obstack_allocate,
7877 dummy_obstack_deallocate);
7878 }
7879
7880 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7881 partial symtabs. We combine several TUs per psymtab to not let the size
7882 of any one psymtab grow too big. */
7883 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7884 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7885
7886 /* Helper routine for get_type_unit_group.
7887 Create the type_unit_group object used to hold one or more TUs. */
7888
7889 static struct type_unit_group *
7890 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7891 {
7892 struct dwarf2_per_objfile *dwarf2_per_objfile
7893 = cu->per_cu->dwarf2_per_objfile;
7894 struct objfile *objfile = dwarf2_per_objfile->objfile;
7895 struct dwarf2_per_cu_data *per_cu;
7896 struct type_unit_group *tu_group;
7897
7898 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7899 struct type_unit_group);
7900 per_cu = &tu_group->per_cu;
7901 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7902
7903 if (dwarf2_per_objfile->using_index)
7904 {
7905 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7906 struct dwarf2_per_cu_quick_data);
7907 }
7908 else
7909 {
7910 unsigned int line_offset = to_underlying (line_offset_struct);
7911 struct partial_symtab *pst;
7912 std::string name;
7913
7914 /* Give the symtab a useful name for debug purposes. */
7915 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7916 name = string_printf ("<type_units_%d>",
7917 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7918 else
7919 name = string_printf ("<type_units_at_0x%x>", line_offset);
7920
7921 pst = create_partial_symtab (per_cu, name.c_str ());
7922 pst->anonymous = 1;
7923 }
7924
7925 tu_group->hash.dwo_unit = cu->dwo_unit;
7926 tu_group->hash.line_sect_off = line_offset_struct;
7927
7928 return tu_group;
7929 }
7930
7931 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7932 STMT_LIST is a DW_AT_stmt_list attribute. */
7933
7934 static struct type_unit_group *
7935 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7936 {
7937 struct dwarf2_per_objfile *dwarf2_per_objfile
7938 = cu->per_cu->dwarf2_per_objfile;
7939 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7940 struct type_unit_group *tu_group;
7941 void **slot;
7942 unsigned int line_offset;
7943 struct type_unit_group type_unit_group_for_lookup;
7944
7945 if (dwarf2_per_objfile->type_unit_groups == NULL)
7946 {
7947 dwarf2_per_objfile->type_unit_groups =
7948 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7949 }
7950
7951 /* Do we need to create a new group, or can we use an existing one? */
7952
7953 if (stmt_list)
7954 {
7955 line_offset = DW_UNSND (stmt_list);
7956 ++tu_stats->nr_symtab_sharers;
7957 }
7958 else
7959 {
7960 /* Ugh, no stmt_list. Rare, but we have to handle it.
7961 We can do various things here like create one group per TU or
7962 spread them over multiple groups to split up the expansion work.
7963 To avoid worst case scenarios (too many groups or too large groups)
7964 we, umm, group them in bunches. */
7965 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7966 | (tu_stats->nr_stmt_less_type_units
7967 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7968 ++tu_stats->nr_stmt_less_type_units;
7969 }
7970
7971 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7972 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7973 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7974 &type_unit_group_for_lookup, INSERT);
7975 if (*slot != NULL)
7976 {
7977 tu_group = (struct type_unit_group *) *slot;
7978 gdb_assert (tu_group != NULL);
7979 }
7980 else
7981 {
7982 sect_offset line_offset_struct = (sect_offset) line_offset;
7983 tu_group = create_type_unit_group (cu, line_offset_struct);
7984 *slot = tu_group;
7985 ++tu_stats->nr_symtabs;
7986 }
7987
7988 return tu_group;
7989 }
7990 \f
7991 /* Partial symbol tables. */
7992
7993 /* Create a psymtab named NAME and assign it to PER_CU.
7994
7995 The caller must fill in the following details:
7996 dirname, textlow, texthigh. */
7997
7998 static struct partial_symtab *
7999 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8000 {
8001 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
8002 struct partial_symtab *pst;
8003
8004 pst = start_psymtab_common (objfile, name, 0);
8005
8006 pst->psymtabs_addrmap_supported = 1;
8007
8008 /* This is the glue that links PST into GDB's symbol API. */
8009 pst->read_symtab_private = per_cu;
8010 pst->read_symtab = dwarf2_read_symtab;
8011 per_cu->v.psymtab = pst;
8012
8013 return pst;
8014 }
8015
8016 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8017 type. */
8018
8019 struct process_psymtab_comp_unit_data
8020 {
8021 /* True if we are reading a DW_TAG_partial_unit. */
8022
8023 int want_partial_unit;
8024
8025 /* The "pretend" language that is used if the CU doesn't declare a
8026 language. */
8027
8028 enum language pretend_language;
8029 };
8030
8031 /* die_reader_func for process_psymtab_comp_unit. */
8032
8033 static void
8034 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8035 const gdb_byte *info_ptr,
8036 struct die_info *comp_unit_die,
8037 int has_children,
8038 void *data)
8039 {
8040 struct dwarf2_cu *cu = reader->cu;
8041 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8042 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8043 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8044 CORE_ADDR baseaddr;
8045 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8046 struct partial_symtab *pst;
8047 enum pc_bounds_kind cu_bounds_kind;
8048 const char *filename;
8049 struct process_psymtab_comp_unit_data *info
8050 = (struct process_psymtab_comp_unit_data *) data;
8051
8052 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8053 return;
8054
8055 gdb_assert (! per_cu->is_debug_types);
8056
8057 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8058
8059 /* Allocate a new partial symbol table structure. */
8060 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8061 if (filename == NULL)
8062 filename = "";
8063
8064 pst = create_partial_symtab (per_cu, filename);
8065
8066 /* This must be done before calling dwarf2_build_include_psymtabs. */
8067 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8068
8069 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
8070
8071 dwarf2_find_base_address (comp_unit_die, cu);
8072
8073 /* Possibly set the default values of LOWPC and HIGHPC from
8074 `DW_AT_ranges'. */
8075 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8076 &best_highpc, cu, pst);
8077 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8078 {
8079 CORE_ADDR low
8080 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8081 - baseaddr);
8082 CORE_ADDR high
8083 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8084 - baseaddr - 1);
8085 /* Store the contiguous range if it is not empty; it can be
8086 empty for CUs with no code. */
8087 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8088 low, high, pst);
8089 }
8090
8091 /* Check if comp unit has_children.
8092 If so, read the rest of the partial symbols from this comp unit.
8093 If not, there's no more debug_info for this comp unit. */
8094 if (has_children)
8095 {
8096 struct partial_die_info *first_die;
8097 CORE_ADDR lowpc, highpc;
8098
8099 lowpc = ((CORE_ADDR) -1);
8100 highpc = ((CORE_ADDR) 0);
8101
8102 first_die = load_partial_dies (reader, info_ptr, 1);
8103
8104 scan_partial_symbols (first_die, &lowpc, &highpc,
8105 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8106
8107 /* If we didn't find a lowpc, set it to highpc to avoid
8108 complaints from `maint check'. */
8109 if (lowpc == ((CORE_ADDR) -1))
8110 lowpc = highpc;
8111
8112 /* If the compilation unit didn't have an explicit address range,
8113 then use the information extracted from its child dies. */
8114 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8115 {
8116 best_lowpc = lowpc;
8117 best_highpc = highpc;
8118 }
8119 }
8120 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
8121 best_lowpc + baseaddr)
8122 - baseaddr);
8123 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
8124 best_highpc + baseaddr)
8125 - baseaddr);
8126
8127 end_psymtab_common (objfile, pst);
8128
8129 if (!cu->per_cu->imported_symtabs_empty ())
8130 {
8131 int i;
8132 int len = cu->per_cu->imported_symtabs_size ();
8133
8134 /* Fill in 'dependencies' here; we fill in 'users' in a
8135 post-pass. */
8136 pst->number_of_dependencies = len;
8137 pst->dependencies
8138 = objfile->partial_symtabs->allocate_dependencies (len);
8139 for (i = 0; i < len; ++i)
8140 {
8141 pst->dependencies[i]
8142 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8143 }
8144
8145 cu->per_cu->imported_symtabs_free ();
8146 }
8147
8148 /* Get the list of files included in the current compilation unit,
8149 and build a psymtab for each of them. */
8150 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8151
8152 if (dwarf_read_debug)
8153 fprintf_unfiltered (gdb_stdlog,
8154 "Psymtab for %s unit @%s: %s - %s"
8155 ", %d global, %d static syms\n",
8156 per_cu->is_debug_types ? "type" : "comp",
8157 sect_offset_str (per_cu->sect_off),
8158 paddress (gdbarch, pst->text_low (objfile)),
8159 paddress (gdbarch, pst->text_high (objfile)),
8160 pst->n_global_syms, pst->n_static_syms);
8161 }
8162
8163 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8164 Process compilation unit THIS_CU for a psymtab. */
8165
8166 static void
8167 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8168 int want_partial_unit,
8169 enum language pretend_language)
8170 {
8171 /* If this compilation unit was already read in, free the
8172 cached copy in order to read it in again. This is
8173 necessary because we skipped some symbols when we first
8174 read in the compilation unit (see load_partial_dies).
8175 This problem could be avoided, but the benefit is unclear. */
8176 if (this_cu->cu != NULL)
8177 free_one_cached_comp_unit (this_cu);
8178
8179 if (this_cu->is_debug_types)
8180 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8181 build_type_psymtabs_reader, NULL);
8182 else
8183 {
8184 process_psymtab_comp_unit_data info;
8185 info.want_partial_unit = want_partial_unit;
8186 info.pretend_language = pretend_language;
8187 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8188 process_psymtab_comp_unit_reader, &info);
8189 }
8190
8191 /* Age out any secondary CUs. */
8192 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8193 }
8194
8195 /* Reader function for build_type_psymtabs. */
8196
8197 static void
8198 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8199 const gdb_byte *info_ptr,
8200 struct die_info *type_unit_die,
8201 int has_children,
8202 void *data)
8203 {
8204 struct dwarf2_per_objfile *dwarf2_per_objfile
8205 = reader->cu->per_cu->dwarf2_per_objfile;
8206 struct objfile *objfile = dwarf2_per_objfile->objfile;
8207 struct dwarf2_cu *cu = reader->cu;
8208 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8209 struct signatured_type *sig_type;
8210 struct type_unit_group *tu_group;
8211 struct attribute *attr;
8212 struct partial_die_info *first_die;
8213 CORE_ADDR lowpc, highpc;
8214 struct partial_symtab *pst;
8215
8216 gdb_assert (data == NULL);
8217 gdb_assert (per_cu->is_debug_types);
8218 sig_type = (struct signatured_type *) per_cu;
8219
8220 if (! has_children)
8221 return;
8222
8223 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8224 tu_group = get_type_unit_group (cu, attr);
8225
8226 if (tu_group->tus == nullptr)
8227 tu_group->tus = new std::vector<signatured_type *>;
8228 tu_group->tus->push_back (sig_type);
8229
8230 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8231 pst = create_partial_symtab (per_cu, "");
8232 pst->anonymous = 1;
8233
8234 first_die = load_partial_dies (reader, info_ptr, 1);
8235
8236 lowpc = (CORE_ADDR) -1;
8237 highpc = (CORE_ADDR) 0;
8238 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8239
8240 end_psymtab_common (objfile, pst);
8241 }
8242
8243 /* Struct used to sort TUs by their abbreviation table offset. */
8244
8245 struct tu_abbrev_offset
8246 {
8247 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8248 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8249 {}
8250
8251 signatured_type *sig_type;
8252 sect_offset abbrev_offset;
8253 };
8254
8255 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8256
8257 static bool
8258 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8259 const struct tu_abbrev_offset &b)
8260 {
8261 return a.abbrev_offset < b.abbrev_offset;
8262 }
8263
8264 /* Efficiently read all the type units.
8265 This does the bulk of the work for build_type_psymtabs.
8266
8267 The efficiency is because we sort TUs by the abbrev table they use and
8268 only read each abbrev table once. In one program there are 200K TUs
8269 sharing 8K abbrev tables.
8270
8271 The main purpose of this function is to support building the
8272 dwarf2_per_objfile->type_unit_groups table.
8273 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8274 can collapse the search space by grouping them by stmt_list.
8275 The savings can be significant, in the same program from above the 200K TUs
8276 share 8K stmt_list tables.
8277
8278 FUNC is expected to call get_type_unit_group, which will create the
8279 struct type_unit_group if necessary and add it to
8280 dwarf2_per_objfile->type_unit_groups. */
8281
8282 static void
8283 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8284 {
8285 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8286 abbrev_table_up abbrev_table;
8287 sect_offset abbrev_offset;
8288
8289 /* It's up to the caller to not call us multiple times. */
8290 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8291
8292 if (dwarf2_per_objfile->all_type_units.empty ())
8293 return;
8294
8295 /* TUs typically share abbrev tables, and there can be way more TUs than
8296 abbrev tables. Sort by abbrev table to reduce the number of times we
8297 read each abbrev table in.
8298 Alternatives are to punt or to maintain a cache of abbrev tables.
8299 This is simpler and efficient enough for now.
8300
8301 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8302 symtab to use). Typically TUs with the same abbrev offset have the same
8303 stmt_list value too so in practice this should work well.
8304
8305 The basic algorithm here is:
8306
8307 sort TUs by abbrev table
8308 for each TU with same abbrev table:
8309 read abbrev table if first user
8310 read TU top level DIE
8311 [IWBN if DWO skeletons had DW_AT_stmt_list]
8312 call FUNC */
8313
8314 if (dwarf_read_debug)
8315 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8316
8317 /* Sort in a separate table to maintain the order of all_type_units
8318 for .gdb_index: TU indices directly index all_type_units. */
8319 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8320 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8321
8322 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8323 sorted_by_abbrev.emplace_back
8324 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8325 sig_type->per_cu.section,
8326 sig_type->per_cu.sect_off));
8327
8328 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8329 sort_tu_by_abbrev_offset);
8330
8331 abbrev_offset = (sect_offset) ~(unsigned) 0;
8332
8333 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8334 {
8335 /* Switch to the next abbrev table if necessary. */
8336 if (abbrev_table == NULL
8337 || tu.abbrev_offset != abbrev_offset)
8338 {
8339 abbrev_offset = tu.abbrev_offset;
8340 abbrev_table =
8341 abbrev_table_read_table (dwarf2_per_objfile,
8342 &dwarf2_per_objfile->abbrev,
8343 abbrev_offset);
8344 ++tu_stats->nr_uniq_abbrev_tables;
8345 }
8346
8347 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
8348 0, 0, false, build_type_psymtabs_reader, NULL);
8349 }
8350 }
8351
8352 /* Print collected type unit statistics. */
8353
8354 static void
8355 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8356 {
8357 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8358
8359 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8360 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8361 dwarf2_per_objfile->all_type_units.size ());
8362 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8363 tu_stats->nr_uniq_abbrev_tables);
8364 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8365 tu_stats->nr_symtabs);
8366 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8367 tu_stats->nr_symtab_sharers);
8368 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8369 tu_stats->nr_stmt_less_type_units);
8370 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8371 tu_stats->nr_all_type_units_reallocs);
8372 }
8373
8374 /* Traversal function for build_type_psymtabs. */
8375
8376 static int
8377 build_type_psymtab_dependencies (void **slot, void *info)
8378 {
8379 struct dwarf2_per_objfile *dwarf2_per_objfile
8380 = (struct dwarf2_per_objfile *) info;
8381 struct objfile *objfile = dwarf2_per_objfile->objfile;
8382 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8383 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8384 struct partial_symtab *pst = per_cu->v.psymtab;
8385 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8386 int i;
8387
8388 gdb_assert (len > 0);
8389 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8390
8391 pst->number_of_dependencies = len;
8392 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8393 for (i = 0; i < len; ++i)
8394 {
8395 struct signatured_type *iter = tu_group->tus->at (i);
8396 gdb_assert (iter->per_cu.is_debug_types);
8397 pst->dependencies[i] = iter->per_cu.v.psymtab;
8398 iter->type_unit_group = tu_group;
8399 }
8400
8401 delete tu_group->tus;
8402 tu_group->tus = nullptr;
8403
8404 return 1;
8405 }
8406
8407 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8408 Build partial symbol tables for the .debug_types comp-units. */
8409
8410 static void
8411 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8412 {
8413 if (! create_all_type_units (dwarf2_per_objfile))
8414 return;
8415
8416 build_type_psymtabs_1 (dwarf2_per_objfile);
8417 }
8418
8419 /* Traversal function for process_skeletonless_type_unit.
8420 Read a TU in a DWO file and build partial symbols for it. */
8421
8422 static int
8423 process_skeletonless_type_unit (void **slot, void *info)
8424 {
8425 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8426 struct dwarf2_per_objfile *dwarf2_per_objfile
8427 = (struct dwarf2_per_objfile *) info;
8428 struct signatured_type find_entry, *entry;
8429
8430 /* If this TU doesn't exist in the global table, add it and read it in. */
8431
8432 if (dwarf2_per_objfile->signatured_types == NULL)
8433 {
8434 dwarf2_per_objfile->signatured_types
8435 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8436 }
8437
8438 find_entry.signature = dwo_unit->signature;
8439 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8440 INSERT);
8441 /* If we've already seen this type there's nothing to do. What's happening
8442 is we're doing our own version of comdat-folding here. */
8443 if (*slot != NULL)
8444 return 1;
8445
8446 /* This does the job that create_all_type_units would have done for
8447 this TU. */
8448 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8449 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8450 *slot = entry;
8451
8452 /* This does the job that build_type_psymtabs_1 would have done. */
8453 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
8454 build_type_psymtabs_reader, NULL);
8455
8456 return 1;
8457 }
8458
8459 /* Traversal function for process_skeletonless_type_units. */
8460
8461 static int
8462 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8463 {
8464 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8465
8466 if (dwo_file->tus != NULL)
8467 {
8468 htab_traverse_noresize (dwo_file->tus,
8469 process_skeletonless_type_unit, info);
8470 }
8471
8472 return 1;
8473 }
8474
8475 /* Scan all TUs of DWO files, verifying we've processed them.
8476 This is needed in case a TU was emitted without its skeleton.
8477 Note: This can't be done until we know what all the DWO files are. */
8478
8479 static void
8480 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8481 {
8482 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8483 if (get_dwp_file (dwarf2_per_objfile) == NULL
8484 && dwarf2_per_objfile->dwo_files != NULL)
8485 {
8486 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
8487 process_dwo_file_for_skeletonless_type_units,
8488 dwarf2_per_objfile);
8489 }
8490 }
8491
8492 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8493
8494 static void
8495 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8496 {
8497 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8498 {
8499 struct partial_symtab *pst = per_cu->v.psymtab;
8500
8501 if (pst == NULL)
8502 continue;
8503
8504 for (int j = 0; j < pst->number_of_dependencies; ++j)
8505 {
8506 /* Set the 'user' field only if it is not already set. */
8507 if (pst->dependencies[j]->user == NULL)
8508 pst->dependencies[j]->user = pst;
8509 }
8510 }
8511 }
8512
8513 /* Build the partial symbol table by doing a quick pass through the
8514 .debug_info and .debug_abbrev sections. */
8515
8516 static void
8517 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8518 {
8519 struct objfile *objfile = dwarf2_per_objfile->objfile;
8520
8521 if (dwarf_read_debug)
8522 {
8523 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8524 objfile_name (objfile));
8525 }
8526
8527 dwarf2_per_objfile->reading_partial_symbols = 1;
8528
8529 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8530
8531 /* Any cached compilation units will be linked by the per-objfile
8532 read_in_chain. Make sure to free them when we're done. */
8533 free_cached_comp_units freer (dwarf2_per_objfile);
8534
8535 build_type_psymtabs (dwarf2_per_objfile);
8536
8537 create_all_comp_units (dwarf2_per_objfile);
8538
8539 /* Create a temporary address map on a temporary obstack. We later
8540 copy this to the final obstack. */
8541 auto_obstack temp_obstack;
8542
8543 scoped_restore save_psymtabs_addrmap
8544 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8545 addrmap_create_mutable (&temp_obstack));
8546
8547 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8548 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8549
8550 /* This has to wait until we read the CUs, we need the list of DWOs. */
8551 process_skeletonless_type_units (dwarf2_per_objfile);
8552
8553 /* Now that all TUs have been processed we can fill in the dependencies. */
8554 if (dwarf2_per_objfile->type_unit_groups != NULL)
8555 {
8556 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8557 build_type_psymtab_dependencies, dwarf2_per_objfile);
8558 }
8559
8560 if (dwarf_read_debug)
8561 print_tu_stats (dwarf2_per_objfile);
8562
8563 set_partial_user (dwarf2_per_objfile);
8564
8565 objfile->partial_symtabs->psymtabs_addrmap
8566 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8567 objfile->partial_symtabs->obstack ());
8568 /* At this point we want to keep the address map. */
8569 save_psymtabs_addrmap.release ();
8570
8571 if (dwarf_read_debug)
8572 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8573 objfile_name (objfile));
8574 }
8575
8576 /* die_reader_func for load_partial_comp_unit. */
8577
8578 static void
8579 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8580 const gdb_byte *info_ptr,
8581 struct die_info *comp_unit_die,
8582 int has_children,
8583 void *data)
8584 {
8585 struct dwarf2_cu *cu = reader->cu;
8586
8587 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8588
8589 /* Check if comp unit has_children.
8590 If so, read the rest of the partial symbols from this comp unit.
8591 If not, there's no more debug_info for this comp unit. */
8592 if (has_children)
8593 load_partial_dies (reader, info_ptr, 0);
8594 }
8595
8596 /* Load the partial DIEs for a secondary CU into memory.
8597 This is also used when rereading a primary CU with load_all_dies. */
8598
8599 static void
8600 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8601 {
8602 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
8603 load_partial_comp_unit_reader, NULL);
8604 }
8605
8606 static void
8607 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8608 struct dwarf2_section_info *section,
8609 struct dwarf2_section_info *abbrev_section,
8610 unsigned int is_dwz)
8611 {
8612 const gdb_byte *info_ptr;
8613 struct objfile *objfile = dwarf2_per_objfile->objfile;
8614
8615 if (dwarf_read_debug)
8616 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8617 get_section_name (section),
8618 get_section_file_name (section));
8619
8620 dwarf2_read_section (objfile, section);
8621
8622 info_ptr = section->buffer;
8623
8624 while (info_ptr < section->buffer + section->size)
8625 {
8626 struct dwarf2_per_cu_data *this_cu;
8627
8628 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8629
8630 comp_unit_head cu_header;
8631 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8632 abbrev_section, info_ptr,
8633 rcuh_kind::COMPILE);
8634
8635 /* Save the compilation unit for later lookup. */
8636 if (cu_header.unit_type != DW_UT_type)
8637 {
8638 this_cu = XOBNEW (&objfile->objfile_obstack,
8639 struct dwarf2_per_cu_data);
8640 memset (this_cu, 0, sizeof (*this_cu));
8641 }
8642 else
8643 {
8644 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8645 struct signatured_type);
8646 memset (sig_type, 0, sizeof (*sig_type));
8647 sig_type->signature = cu_header.signature;
8648 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8649 this_cu = &sig_type->per_cu;
8650 }
8651 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8652 this_cu->sect_off = sect_off;
8653 this_cu->length = cu_header.length + cu_header.initial_length_size;
8654 this_cu->is_dwz = is_dwz;
8655 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8656 this_cu->section = section;
8657
8658 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8659
8660 info_ptr = info_ptr + this_cu->length;
8661 }
8662 }
8663
8664 /* Create a list of all compilation units in OBJFILE.
8665 This is only done for -readnow and building partial symtabs. */
8666
8667 static void
8668 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8669 {
8670 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8671 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8672 &dwarf2_per_objfile->abbrev, 0);
8673
8674 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8675 if (dwz != NULL)
8676 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8677 1);
8678 }
8679
8680 /* Process all loaded DIEs for compilation unit CU, starting at
8681 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8682 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8683 DW_AT_ranges). See the comments of add_partial_subprogram on how
8684 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8685
8686 static void
8687 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8688 CORE_ADDR *highpc, int set_addrmap,
8689 struct dwarf2_cu *cu)
8690 {
8691 struct partial_die_info *pdi;
8692
8693 /* Now, march along the PDI's, descending into ones which have
8694 interesting children but skipping the children of the other ones,
8695 until we reach the end of the compilation unit. */
8696
8697 pdi = first_die;
8698
8699 while (pdi != NULL)
8700 {
8701 pdi->fixup (cu);
8702
8703 /* Anonymous namespaces or modules have no name but have interesting
8704 children, so we need to look at them. Ditto for anonymous
8705 enums. */
8706
8707 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8708 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8709 || pdi->tag == DW_TAG_imported_unit
8710 || pdi->tag == DW_TAG_inlined_subroutine)
8711 {
8712 switch (pdi->tag)
8713 {
8714 case DW_TAG_subprogram:
8715 case DW_TAG_inlined_subroutine:
8716 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8717 break;
8718 case DW_TAG_constant:
8719 case DW_TAG_variable:
8720 case DW_TAG_typedef:
8721 case DW_TAG_union_type:
8722 if (!pdi->is_declaration)
8723 {
8724 add_partial_symbol (pdi, cu);
8725 }
8726 break;
8727 case DW_TAG_class_type:
8728 case DW_TAG_interface_type:
8729 case DW_TAG_structure_type:
8730 if (!pdi->is_declaration)
8731 {
8732 add_partial_symbol (pdi, cu);
8733 }
8734 if ((cu->language == language_rust
8735 || cu->language == language_cplus) && pdi->has_children)
8736 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8737 set_addrmap, cu);
8738 break;
8739 case DW_TAG_enumeration_type:
8740 if (!pdi->is_declaration)
8741 add_partial_enumeration (pdi, cu);
8742 break;
8743 case DW_TAG_base_type:
8744 case DW_TAG_subrange_type:
8745 /* File scope base type definitions are added to the partial
8746 symbol table. */
8747 add_partial_symbol (pdi, cu);
8748 break;
8749 case DW_TAG_namespace:
8750 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8751 break;
8752 case DW_TAG_module:
8753 if (!pdi->is_declaration)
8754 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8755 break;
8756 case DW_TAG_imported_unit:
8757 {
8758 struct dwarf2_per_cu_data *per_cu;
8759
8760 /* For now we don't handle imported units in type units. */
8761 if (cu->per_cu->is_debug_types)
8762 {
8763 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8764 " supported in type units [in module %s]"),
8765 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8766 }
8767
8768 per_cu = dwarf2_find_containing_comp_unit
8769 (pdi->d.sect_off, pdi->is_dwz,
8770 cu->per_cu->dwarf2_per_objfile);
8771
8772 /* Go read the partial unit, if needed. */
8773 if (per_cu->v.psymtab == NULL)
8774 process_psymtab_comp_unit (per_cu, 1, cu->language);
8775
8776 cu->per_cu->imported_symtabs_push (per_cu);
8777 }
8778 break;
8779 case DW_TAG_imported_declaration:
8780 add_partial_symbol (pdi, cu);
8781 break;
8782 default:
8783 break;
8784 }
8785 }
8786
8787 /* If the die has a sibling, skip to the sibling. */
8788
8789 pdi = pdi->die_sibling;
8790 }
8791 }
8792
8793 /* Functions used to compute the fully scoped name of a partial DIE.
8794
8795 Normally, this is simple. For C++, the parent DIE's fully scoped
8796 name is concatenated with "::" and the partial DIE's name.
8797 Enumerators are an exception; they use the scope of their parent
8798 enumeration type, i.e. the name of the enumeration type is not
8799 prepended to the enumerator.
8800
8801 There are two complexities. One is DW_AT_specification; in this
8802 case "parent" means the parent of the target of the specification,
8803 instead of the direct parent of the DIE. The other is compilers
8804 which do not emit DW_TAG_namespace; in this case we try to guess
8805 the fully qualified name of structure types from their members'
8806 linkage names. This must be done using the DIE's children rather
8807 than the children of any DW_AT_specification target. We only need
8808 to do this for structures at the top level, i.e. if the target of
8809 any DW_AT_specification (if any; otherwise the DIE itself) does not
8810 have a parent. */
8811
8812 /* Compute the scope prefix associated with PDI's parent, in
8813 compilation unit CU. The result will be allocated on CU's
8814 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8815 field. NULL is returned if no prefix is necessary. */
8816 static const char *
8817 partial_die_parent_scope (struct partial_die_info *pdi,
8818 struct dwarf2_cu *cu)
8819 {
8820 const char *grandparent_scope;
8821 struct partial_die_info *parent, *real_pdi;
8822
8823 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8824 then this means the parent of the specification DIE. */
8825
8826 real_pdi = pdi;
8827 while (real_pdi->has_specification)
8828 {
8829 auto res = find_partial_die (real_pdi->spec_offset,
8830 real_pdi->spec_is_dwz, cu);
8831 real_pdi = res.pdi;
8832 cu = res.cu;
8833 }
8834
8835 parent = real_pdi->die_parent;
8836 if (parent == NULL)
8837 return NULL;
8838
8839 if (parent->scope_set)
8840 return parent->scope;
8841
8842 parent->fixup (cu);
8843
8844 grandparent_scope = partial_die_parent_scope (parent, cu);
8845
8846 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8847 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8848 Work around this problem here. */
8849 if (cu->language == language_cplus
8850 && parent->tag == DW_TAG_namespace
8851 && strcmp (parent->name, "::") == 0
8852 && grandparent_scope == NULL)
8853 {
8854 parent->scope = NULL;
8855 parent->scope_set = 1;
8856 return NULL;
8857 }
8858
8859 /* Nested subroutines in Fortran get a prefix. */
8860 if (pdi->tag == DW_TAG_enumerator)
8861 /* Enumerators should not get the name of the enumeration as a prefix. */
8862 parent->scope = grandparent_scope;
8863 else if (parent->tag == DW_TAG_namespace
8864 || parent->tag == DW_TAG_module
8865 || parent->tag == DW_TAG_structure_type
8866 || parent->tag == DW_TAG_class_type
8867 || parent->tag == DW_TAG_interface_type
8868 || parent->tag == DW_TAG_union_type
8869 || parent->tag == DW_TAG_enumeration_type
8870 || (cu->language == language_fortran
8871 && parent->tag == DW_TAG_subprogram
8872 && pdi->tag == DW_TAG_subprogram))
8873 {
8874 if (grandparent_scope == NULL)
8875 parent->scope = parent->name;
8876 else
8877 parent->scope = typename_concat (&cu->comp_unit_obstack,
8878 grandparent_scope,
8879 parent->name, 0, cu);
8880 }
8881 else
8882 {
8883 /* FIXME drow/2004-04-01: What should we be doing with
8884 function-local names? For partial symbols, we should probably be
8885 ignoring them. */
8886 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8887 dwarf_tag_name (parent->tag),
8888 sect_offset_str (pdi->sect_off));
8889 parent->scope = grandparent_scope;
8890 }
8891
8892 parent->scope_set = 1;
8893 return parent->scope;
8894 }
8895
8896 /* Return the fully scoped name associated with PDI, from compilation unit
8897 CU. The result will be allocated with malloc. */
8898
8899 static gdb::unique_xmalloc_ptr<char>
8900 partial_die_full_name (struct partial_die_info *pdi,
8901 struct dwarf2_cu *cu)
8902 {
8903 const char *parent_scope;
8904
8905 /* If this is a template instantiation, we can not work out the
8906 template arguments from partial DIEs. So, unfortunately, we have
8907 to go through the full DIEs. At least any work we do building
8908 types here will be reused if full symbols are loaded later. */
8909 if (pdi->has_template_arguments)
8910 {
8911 pdi->fixup (cu);
8912
8913 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8914 {
8915 struct die_info *die;
8916 struct attribute attr;
8917 struct dwarf2_cu *ref_cu = cu;
8918
8919 /* DW_FORM_ref_addr is using section offset. */
8920 attr.name = (enum dwarf_attribute) 0;
8921 attr.form = DW_FORM_ref_addr;
8922 attr.u.unsnd = to_underlying (pdi->sect_off);
8923 die = follow_die_ref (NULL, &attr, &ref_cu);
8924
8925 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8926 }
8927 }
8928
8929 parent_scope = partial_die_parent_scope (pdi, cu);
8930 if (parent_scope == NULL)
8931 return NULL;
8932 else
8933 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8934 pdi->name, 0, cu));
8935 }
8936
8937 static void
8938 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8939 {
8940 struct dwarf2_per_objfile *dwarf2_per_objfile
8941 = cu->per_cu->dwarf2_per_objfile;
8942 struct objfile *objfile = dwarf2_per_objfile->objfile;
8943 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8944 CORE_ADDR addr = 0;
8945 const char *actual_name = NULL;
8946 CORE_ADDR baseaddr;
8947
8948 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
8949
8950 gdb::unique_xmalloc_ptr<char> built_actual_name
8951 = partial_die_full_name (pdi, cu);
8952 if (built_actual_name != NULL)
8953 actual_name = built_actual_name.get ();
8954
8955 if (actual_name == NULL)
8956 actual_name = pdi->name;
8957
8958 switch (pdi->tag)
8959 {
8960 case DW_TAG_inlined_subroutine:
8961 case DW_TAG_subprogram:
8962 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8963 - baseaddr);
8964 if (pdi->is_external
8965 || cu->language == language_ada
8966 || (cu->language == language_fortran
8967 && pdi->die_parent != NULL
8968 && pdi->die_parent->tag == DW_TAG_subprogram))
8969 {
8970 /* Normally, only "external" DIEs are part of the global scope.
8971 But in Ada and Fortran, we want to be able to access nested
8972 procedures globally. So all Ada and Fortran subprograms are
8973 stored in the global scope. */
8974 add_psymbol_to_list (actual_name,
8975 built_actual_name != NULL,
8976 VAR_DOMAIN, LOC_BLOCK,
8977 SECT_OFF_TEXT (objfile),
8978 psymbol_placement::GLOBAL,
8979 addr,
8980 cu->language, objfile);
8981 }
8982 else
8983 {
8984 add_psymbol_to_list (actual_name,
8985 built_actual_name != NULL,
8986 VAR_DOMAIN, LOC_BLOCK,
8987 SECT_OFF_TEXT (objfile),
8988 psymbol_placement::STATIC,
8989 addr, cu->language, objfile);
8990 }
8991
8992 if (pdi->main_subprogram && actual_name != NULL)
8993 set_objfile_main_name (objfile, actual_name, cu->language);
8994 break;
8995 case DW_TAG_constant:
8996 add_psymbol_to_list (actual_name,
8997 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8998 -1, (pdi->is_external
8999 ? psymbol_placement::GLOBAL
9000 : psymbol_placement::STATIC),
9001 0, cu->language, objfile);
9002 break;
9003 case DW_TAG_variable:
9004 if (pdi->d.locdesc)
9005 addr = decode_locdesc (pdi->d.locdesc, cu);
9006
9007 if (pdi->d.locdesc
9008 && addr == 0
9009 && !dwarf2_per_objfile->has_section_at_zero)
9010 {
9011 /* A global or static variable may also have been stripped
9012 out by the linker if unused, in which case its address
9013 will be nullified; do not add such variables into partial
9014 symbol table then. */
9015 }
9016 else if (pdi->is_external)
9017 {
9018 /* Global Variable.
9019 Don't enter into the minimal symbol tables as there is
9020 a minimal symbol table entry from the ELF symbols already.
9021 Enter into partial symbol table if it has a location
9022 descriptor or a type.
9023 If the location descriptor is missing, new_symbol will create
9024 a LOC_UNRESOLVED symbol, the address of the variable will then
9025 be determined from the minimal symbol table whenever the variable
9026 is referenced.
9027 The address for the partial symbol table entry is not
9028 used by GDB, but it comes in handy for debugging partial symbol
9029 table building. */
9030
9031 if (pdi->d.locdesc || pdi->has_type)
9032 add_psymbol_to_list (actual_name,
9033 built_actual_name != NULL,
9034 VAR_DOMAIN, LOC_STATIC,
9035 SECT_OFF_TEXT (objfile),
9036 psymbol_placement::GLOBAL,
9037 addr, cu->language, objfile);
9038 }
9039 else
9040 {
9041 int has_loc = pdi->d.locdesc != NULL;
9042
9043 /* Static Variable. Skip symbols whose value we cannot know (those
9044 without location descriptors or constant values). */
9045 if (!has_loc && !pdi->has_const_value)
9046 return;
9047
9048 add_psymbol_to_list (actual_name,
9049 built_actual_name != NULL,
9050 VAR_DOMAIN, LOC_STATIC,
9051 SECT_OFF_TEXT (objfile),
9052 psymbol_placement::STATIC,
9053 has_loc ? addr : 0,
9054 cu->language, objfile);
9055 }
9056 break;
9057 case DW_TAG_typedef:
9058 case DW_TAG_base_type:
9059 case DW_TAG_subrange_type:
9060 add_psymbol_to_list (actual_name,
9061 built_actual_name != NULL,
9062 VAR_DOMAIN, LOC_TYPEDEF, -1,
9063 psymbol_placement::STATIC,
9064 0, cu->language, objfile);
9065 break;
9066 case DW_TAG_imported_declaration:
9067 case DW_TAG_namespace:
9068 add_psymbol_to_list (actual_name,
9069 built_actual_name != NULL,
9070 VAR_DOMAIN, LOC_TYPEDEF, -1,
9071 psymbol_placement::GLOBAL,
9072 0, cu->language, objfile);
9073 break;
9074 case DW_TAG_module:
9075 /* With Fortran 77 there might be a "BLOCK DATA" module
9076 available without any name. If so, we skip the module as it
9077 doesn't bring any value. */
9078 if (actual_name != nullptr)
9079 add_psymbol_to_list (actual_name,
9080 built_actual_name != NULL,
9081 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9082 psymbol_placement::GLOBAL,
9083 0, cu->language, objfile);
9084 break;
9085 case DW_TAG_class_type:
9086 case DW_TAG_interface_type:
9087 case DW_TAG_structure_type:
9088 case DW_TAG_union_type:
9089 case DW_TAG_enumeration_type:
9090 /* Skip external references. The DWARF standard says in the section
9091 about "Structure, Union, and Class Type Entries": "An incomplete
9092 structure, union or class type is represented by a structure,
9093 union or class entry that does not have a byte size attribute
9094 and that has a DW_AT_declaration attribute." */
9095 if (!pdi->has_byte_size && pdi->is_declaration)
9096 return;
9097
9098 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9099 static vs. global. */
9100 add_psymbol_to_list (actual_name,
9101 built_actual_name != NULL,
9102 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9103 cu->language == language_cplus
9104 ? psymbol_placement::GLOBAL
9105 : psymbol_placement::STATIC,
9106 0, cu->language, objfile);
9107
9108 break;
9109 case DW_TAG_enumerator:
9110 add_psymbol_to_list (actual_name,
9111 built_actual_name != NULL,
9112 VAR_DOMAIN, LOC_CONST, -1,
9113 cu->language == language_cplus
9114 ? psymbol_placement::GLOBAL
9115 : psymbol_placement::STATIC,
9116 0, cu->language, objfile);
9117 break;
9118 default:
9119 break;
9120 }
9121 }
9122
9123 /* Read a partial die corresponding to a namespace; also, add a symbol
9124 corresponding to that namespace to the symbol table. NAMESPACE is
9125 the name of the enclosing namespace. */
9126
9127 static void
9128 add_partial_namespace (struct partial_die_info *pdi,
9129 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9130 int set_addrmap, struct dwarf2_cu *cu)
9131 {
9132 /* Add a symbol for the namespace. */
9133
9134 add_partial_symbol (pdi, cu);
9135
9136 /* Now scan partial symbols in that namespace. */
9137
9138 if (pdi->has_children)
9139 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9140 }
9141
9142 /* Read a partial die corresponding to a Fortran module. */
9143
9144 static void
9145 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9146 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9147 {
9148 /* Add a symbol for the namespace. */
9149
9150 add_partial_symbol (pdi, cu);
9151
9152 /* Now scan partial symbols in that module. */
9153
9154 if (pdi->has_children)
9155 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9156 }
9157
9158 /* Read a partial die corresponding to a subprogram or an inlined
9159 subprogram and create a partial symbol for that subprogram.
9160 When the CU language allows it, this routine also defines a partial
9161 symbol for each nested subprogram that this subprogram contains.
9162 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9163 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9164
9165 PDI may also be a lexical block, in which case we simply search
9166 recursively for subprograms defined inside that lexical block.
9167 Again, this is only performed when the CU language allows this
9168 type of definitions. */
9169
9170 static void
9171 add_partial_subprogram (struct partial_die_info *pdi,
9172 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9173 int set_addrmap, struct dwarf2_cu *cu)
9174 {
9175 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9176 {
9177 if (pdi->has_pc_info)
9178 {
9179 if (pdi->lowpc < *lowpc)
9180 *lowpc = pdi->lowpc;
9181 if (pdi->highpc > *highpc)
9182 *highpc = pdi->highpc;
9183 if (set_addrmap)
9184 {
9185 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9186 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9187 CORE_ADDR baseaddr;
9188 CORE_ADDR this_highpc;
9189 CORE_ADDR this_lowpc;
9190
9191 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
9192 this_lowpc
9193 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9194 pdi->lowpc + baseaddr)
9195 - baseaddr);
9196 this_highpc
9197 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9198 pdi->highpc + baseaddr)
9199 - baseaddr);
9200 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
9201 this_lowpc, this_highpc - 1,
9202 cu->per_cu->v.psymtab);
9203 }
9204 }
9205
9206 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9207 {
9208 if (!pdi->is_declaration)
9209 /* Ignore subprogram DIEs that do not have a name, they are
9210 illegal. Do not emit a complaint at this point, we will
9211 do so when we convert this psymtab into a symtab. */
9212 if (pdi->name)
9213 add_partial_symbol (pdi, cu);
9214 }
9215 }
9216
9217 if (! pdi->has_children)
9218 return;
9219
9220 if (cu->language == language_ada || cu->language == language_fortran)
9221 {
9222 pdi = pdi->die_child;
9223 while (pdi != NULL)
9224 {
9225 pdi->fixup (cu);
9226 if (pdi->tag == DW_TAG_subprogram
9227 || pdi->tag == DW_TAG_inlined_subroutine
9228 || pdi->tag == DW_TAG_lexical_block)
9229 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9230 pdi = pdi->die_sibling;
9231 }
9232 }
9233 }
9234
9235 /* Read a partial die corresponding to an enumeration type. */
9236
9237 static void
9238 add_partial_enumeration (struct partial_die_info *enum_pdi,
9239 struct dwarf2_cu *cu)
9240 {
9241 struct partial_die_info *pdi;
9242
9243 if (enum_pdi->name != NULL)
9244 add_partial_symbol (enum_pdi, cu);
9245
9246 pdi = enum_pdi->die_child;
9247 while (pdi)
9248 {
9249 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9250 complaint (_("malformed enumerator DIE ignored"));
9251 else
9252 add_partial_symbol (pdi, cu);
9253 pdi = pdi->die_sibling;
9254 }
9255 }
9256
9257 /* Return the initial uleb128 in the die at INFO_PTR. */
9258
9259 static unsigned int
9260 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9261 {
9262 unsigned int bytes_read;
9263
9264 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9265 }
9266
9267 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9268 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9269
9270 Return the corresponding abbrev, or NULL if the number is zero (indicating
9271 an empty DIE). In either case *BYTES_READ will be set to the length of
9272 the initial number. */
9273
9274 static struct abbrev_info *
9275 peek_die_abbrev (const die_reader_specs &reader,
9276 const gdb_byte *info_ptr, unsigned int *bytes_read)
9277 {
9278 dwarf2_cu *cu = reader.cu;
9279 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9280 unsigned int abbrev_number
9281 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9282
9283 if (abbrev_number == 0)
9284 return NULL;
9285
9286 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9287 if (!abbrev)
9288 {
9289 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9290 " at offset %s [in module %s]"),
9291 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9292 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9293 }
9294
9295 return abbrev;
9296 }
9297
9298 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9299 Returns a pointer to the end of a series of DIEs, terminated by an empty
9300 DIE. Any children of the skipped DIEs will also be skipped. */
9301
9302 static const gdb_byte *
9303 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9304 {
9305 while (1)
9306 {
9307 unsigned int bytes_read;
9308 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9309
9310 if (abbrev == NULL)
9311 return info_ptr + bytes_read;
9312 else
9313 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9314 }
9315 }
9316
9317 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9318 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9319 abbrev corresponding to that skipped uleb128 should be passed in
9320 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9321 children. */
9322
9323 static const gdb_byte *
9324 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9325 struct abbrev_info *abbrev)
9326 {
9327 unsigned int bytes_read;
9328 struct attribute attr;
9329 bfd *abfd = reader->abfd;
9330 struct dwarf2_cu *cu = reader->cu;
9331 const gdb_byte *buffer = reader->buffer;
9332 const gdb_byte *buffer_end = reader->buffer_end;
9333 unsigned int form, i;
9334
9335 for (i = 0; i < abbrev->num_attrs; i++)
9336 {
9337 /* The only abbrev we care about is DW_AT_sibling. */
9338 if (abbrev->attrs[i].name == DW_AT_sibling)
9339 {
9340 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9341 if (attr.form == DW_FORM_ref_addr)
9342 complaint (_("ignoring absolute DW_AT_sibling"));
9343 else
9344 {
9345 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9346 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9347
9348 if (sibling_ptr < info_ptr)
9349 complaint (_("DW_AT_sibling points backwards"));
9350 else if (sibling_ptr > reader->buffer_end)
9351 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9352 else
9353 return sibling_ptr;
9354 }
9355 }
9356
9357 /* If it isn't DW_AT_sibling, skip this attribute. */
9358 form = abbrev->attrs[i].form;
9359 skip_attribute:
9360 switch (form)
9361 {
9362 case DW_FORM_ref_addr:
9363 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9364 and later it is offset sized. */
9365 if (cu->header.version == 2)
9366 info_ptr += cu->header.addr_size;
9367 else
9368 info_ptr += cu->header.offset_size;
9369 break;
9370 case DW_FORM_GNU_ref_alt:
9371 info_ptr += cu->header.offset_size;
9372 break;
9373 case DW_FORM_addr:
9374 info_ptr += cu->header.addr_size;
9375 break;
9376 case DW_FORM_data1:
9377 case DW_FORM_ref1:
9378 case DW_FORM_flag:
9379 case DW_FORM_strx1:
9380 info_ptr += 1;
9381 break;
9382 case DW_FORM_flag_present:
9383 case DW_FORM_implicit_const:
9384 break;
9385 case DW_FORM_data2:
9386 case DW_FORM_ref2:
9387 case DW_FORM_strx2:
9388 info_ptr += 2;
9389 break;
9390 case DW_FORM_strx3:
9391 info_ptr += 3;
9392 break;
9393 case DW_FORM_data4:
9394 case DW_FORM_ref4:
9395 case DW_FORM_strx4:
9396 info_ptr += 4;
9397 break;
9398 case DW_FORM_data8:
9399 case DW_FORM_ref8:
9400 case DW_FORM_ref_sig8:
9401 info_ptr += 8;
9402 break;
9403 case DW_FORM_data16:
9404 info_ptr += 16;
9405 break;
9406 case DW_FORM_string:
9407 read_direct_string (abfd, info_ptr, &bytes_read);
9408 info_ptr += bytes_read;
9409 break;
9410 case DW_FORM_sec_offset:
9411 case DW_FORM_strp:
9412 case DW_FORM_GNU_strp_alt:
9413 info_ptr += cu->header.offset_size;
9414 break;
9415 case DW_FORM_exprloc:
9416 case DW_FORM_block:
9417 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9418 info_ptr += bytes_read;
9419 break;
9420 case DW_FORM_block1:
9421 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9422 break;
9423 case DW_FORM_block2:
9424 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9425 break;
9426 case DW_FORM_block4:
9427 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9428 break;
9429 case DW_FORM_addrx:
9430 case DW_FORM_strx:
9431 case DW_FORM_sdata:
9432 case DW_FORM_udata:
9433 case DW_FORM_ref_udata:
9434 case DW_FORM_GNU_addr_index:
9435 case DW_FORM_GNU_str_index:
9436 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9437 break;
9438 case DW_FORM_indirect:
9439 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9440 info_ptr += bytes_read;
9441 /* We need to continue parsing from here, so just go back to
9442 the top. */
9443 goto skip_attribute;
9444
9445 default:
9446 error (_("Dwarf Error: Cannot handle %s "
9447 "in DWARF reader [in module %s]"),
9448 dwarf_form_name (form),
9449 bfd_get_filename (abfd));
9450 }
9451 }
9452
9453 if (abbrev->has_children)
9454 return skip_children (reader, info_ptr);
9455 else
9456 return info_ptr;
9457 }
9458
9459 /* Locate ORIG_PDI's sibling.
9460 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9461
9462 static const gdb_byte *
9463 locate_pdi_sibling (const struct die_reader_specs *reader,
9464 struct partial_die_info *orig_pdi,
9465 const gdb_byte *info_ptr)
9466 {
9467 /* Do we know the sibling already? */
9468
9469 if (orig_pdi->sibling)
9470 return orig_pdi->sibling;
9471
9472 /* Are there any children to deal with? */
9473
9474 if (!orig_pdi->has_children)
9475 return info_ptr;
9476
9477 /* Skip the children the long way. */
9478
9479 return skip_children (reader, info_ptr);
9480 }
9481
9482 /* Expand this partial symbol table into a full symbol table. SELF is
9483 not NULL. */
9484
9485 static void
9486 dwarf2_read_symtab (struct partial_symtab *self,
9487 struct objfile *objfile)
9488 {
9489 struct dwarf2_per_objfile *dwarf2_per_objfile
9490 = get_dwarf2_per_objfile (objfile);
9491
9492 if (self->readin)
9493 {
9494 warning (_("bug: psymtab for %s is already read in."),
9495 self->filename);
9496 }
9497 else
9498 {
9499 if (info_verbose)
9500 {
9501 printf_filtered (_("Reading in symbols for %s..."),
9502 self->filename);
9503 gdb_flush (gdb_stdout);
9504 }
9505
9506 /* If this psymtab is constructed from a debug-only objfile, the
9507 has_section_at_zero flag will not necessarily be correct. We
9508 can get the correct value for this flag by looking at the data
9509 associated with the (presumably stripped) associated objfile. */
9510 if (objfile->separate_debug_objfile_backlink)
9511 {
9512 struct dwarf2_per_objfile *dpo_backlink
9513 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9514
9515 dwarf2_per_objfile->has_section_at_zero
9516 = dpo_backlink->has_section_at_zero;
9517 }
9518
9519 dwarf2_per_objfile->reading_partial_symbols = 0;
9520
9521 psymtab_to_symtab_1 (self);
9522
9523 /* Finish up the debug error message. */
9524 if (info_verbose)
9525 printf_filtered (_("done.\n"));
9526 }
9527
9528 process_cu_includes (dwarf2_per_objfile);
9529 }
9530 \f
9531 /* Reading in full CUs. */
9532
9533 /* Add PER_CU to the queue. */
9534
9535 static void
9536 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9537 enum language pretend_language)
9538 {
9539 struct dwarf2_queue_item *item;
9540
9541 per_cu->queued = 1;
9542 item = XNEW (struct dwarf2_queue_item);
9543 item->per_cu = per_cu;
9544 item->pretend_language = pretend_language;
9545 item->next = NULL;
9546
9547 if (dwarf2_queue == NULL)
9548 dwarf2_queue = item;
9549 else
9550 dwarf2_queue_tail->next = item;
9551
9552 dwarf2_queue_tail = item;
9553 }
9554
9555 /* If PER_CU is not yet queued, add it to the queue.
9556 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9557 dependency.
9558 The result is non-zero if PER_CU was queued, otherwise the result is zero
9559 meaning either PER_CU is already queued or it is already loaded.
9560
9561 N.B. There is an invariant here that if a CU is queued then it is loaded.
9562 The caller is required to load PER_CU if we return non-zero. */
9563
9564 static int
9565 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9566 struct dwarf2_per_cu_data *per_cu,
9567 enum language pretend_language)
9568 {
9569 /* We may arrive here during partial symbol reading, if we need full
9570 DIEs to process an unusual case (e.g. template arguments). Do
9571 not queue PER_CU, just tell our caller to load its DIEs. */
9572 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9573 {
9574 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9575 return 1;
9576 return 0;
9577 }
9578
9579 /* Mark the dependence relation so that we don't flush PER_CU
9580 too early. */
9581 if (dependent_cu != NULL)
9582 dwarf2_add_dependence (dependent_cu, per_cu);
9583
9584 /* If it's already on the queue, we have nothing to do. */
9585 if (per_cu->queued)
9586 return 0;
9587
9588 /* If the compilation unit is already loaded, just mark it as
9589 used. */
9590 if (per_cu->cu != NULL)
9591 {
9592 per_cu->cu->last_used = 0;
9593 return 0;
9594 }
9595
9596 /* Add it to the queue. */
9597 queue_comp_unit (per_cu, pretend_language);
9598
9599 return 1;
9600 }
9601
9602 /* Process the queue. */
9603
9604 static void
9605 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9606 {
9607 struct dwarf2_queue_item *item, *next_item;
9608
9609 if (dwarf_read_debug)
9610 {
9611 fprintf_unfiltered (gdb_stdlog,
9612 "Expanding one or more symtabs of objfile %s ...\n",
9613 objfile_name (dwarf2_per_objfile->objfile));
9614 }
9615
9616 /* The queue starts out with one item, but following a DIE reference
9617 may load a new CU, adding it to the end of the queue. */
9618 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9619 {
9620 if ((dwarf2_per_objfile->using_index
9621 ? !item->per_cu->v.quick->compunit_symtab
9622 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9623 /* Skip dummy CUs. */
9624 && item->per_cu->cu != NULL)
9625 {
9626 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9627 unsigned int debug_print_threshold;
9628 char buf[100];
9629
9630 if (per_cu->is_debug_types)
9631 {
9632 struct signatured_type *sig_type =
9633 (struct signatured_type *) per_cu;
9634
9635 sprintf (buf, "TU %s at offset %s",
9636 hex_string (sig_type->signature),
9637 sect_offset_str (per_cu->sect_off));
9638 /* There can be 100s of TUs.
9639 Only print them in verbose mode. */
9640 debug_print_threshold = 2;
9641 }
9642 else
9643 {
9644 sprintf (buf, "CU at offset %s",
9645 sect_offset_str (per_cu->sect_off));
9646 debug_print_threshold = 1;
9647 }
9648
9649 if (dwarf_read_debug >= debug_print_threshold)
9650 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9651
9652 if (per_cu->is_debug_types)
9653 process_full_type_unit (per_cu, item->pretend_language);
9654 else
9655 process_full_comp_unit (per_cu, item->pretend_language);
9656
9657 if (dwarf_read_debug >= debug_print_threshold)
9658 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9659 }
9660
9661 item->per_cu->queued = 0;
9662 next_item = item->next;
9663 xfree (item);
9664 }
9665
9666 dwarf2_queue_tail = NULL;
9667
9668 if (dwarf_read_debug)
9669 {
9670 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9671 objfile_name (dwarf2_per_objfile->objfile));
9672 }
9673 }
9674
9675 /* Read in full symbols for PST, and anything it depends on. */
9676
9677 static void
9678 psymtab_to_symtab_1 (struct partial_symtab *pst)
9679 {
9680 struct dwarf2_per_cu_data *per_cu;
9681 int i;
9682
9683 if (pst->readin)
9684 return;
9685
9686 for (i = 0; i < pst->number_of_dependencies; i++)
9687 if (!pst->dependencies[i]->readin
9688 && pst->dependencies[i]->user == NULL)
9689 {
9690 /* Inform about additional files that need to be read in. */
9691 if (info_verbose)
9692 {
9693 /* FIXME: i18n: Need to make this a single string. */
9694 fputs_filtered (" ", gdb_stdout);
9695 wrap_here ("");
9696 fputs_filtered ("and ", gdb_stdout);
9697 wrap_here ("");
9698 printf_filtered ("%s...", pst->dependencies[i]->filename);
9699 wrap_here (""); /* Flush output. */
9700 gdb_flush (gdb_stdout);
9701 }
9702 psymtab_to_symtab_1 (pst->dependencies[i]);
9703 }
9704
9705 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9706
9707 if (per_cu == NULL)
9708 {
9709 /* It's an include file, no symbols to read for it.
9710 Everything is in the parent symtab. */
9711 pst->readin = 1;
9712 return;
9713 }
9714
9715 dw2_do_instantiate_symtab (per_cu, false);
9716 }
9717
9718 /* Trivial hash function for die_info: the hash value of a DIE
9719 is its offset in .debug_info for this objfile. */
9720
9721 static hashval_t
9722 die_hash (const void *item)
9723 {
9724 const struct die_info *die = (const struct die_info *) item;
9725
9726 return to_underlying (die->sect_off);
9727 }
9728
9729 /* Trivial comparison function for die_info structures: two DIEs
9730 are equal if they have the same offset. */
9731
9732 static int
9733 die_eq (const void *item_lhs, const void *item_rhs)
9734 {
9735 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9736 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9737
9738 return die_lhs->sect_off == die_rhs->sect_off;
9739 }
9740
9741 /* die_reader_func for load_full_comp_unit.
9742 This is identical to read_signatured_type_reader,
9743 but is kept separate for now. */
9744
9745 static void
9746 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9747 const gdb_byte *info_ptr,
9748 struct die_info *comp_unit_die,
9749 int has_children,
9750 void *data)
9751 {
9752 struct dwarf2_cu *cu = reader->cu;
9753 enum language *language_ptr = (enum language *) data;
9754
9755 gdb_assert (cu->die_hash == NULL);
9756 cu->die_hash =
9757 htab_create_alloc_ex (cu->header.length / 12,
9758 die_hash,
9759 die_eq,
9760 NULL,
9761 &cu->comp_unit_obstack,
9762 hashtab_obstack_allocate,
9763 dummy_obstack_deallocate);
9764
9765 if (has_children)
9766 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9767 &info_ptr, comp_unit_die);
9768 cu->dies = comp_unit_die;
9769 /* comp_unit_die is not stored in die_hash, no need. */
9770
9771 /* We try not to read any attributes in this function, because not
9772 all CUs needed for references have been loaded yet, and symbol
9773 table processing isn't initialized. But we have to set the CU language,
9774 or we won't be able to build types correctly.
9775 Similarly, if we do not read the producer, we can not apply
9776 producer-specific interpretation. */
9777 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
9778 }
9779
9780 /* Load the DIEs associated with PER_CU into memory. */
9781
9782 static void
9783 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9784 bool skip_partial,
9785 enum language pretend_language)
9786 {
9787 gdb_assert (! this_cu->is_debug_types);
9788
9789 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
9790 load_full_comp_unit_reader, &pretend_language);
9791 }
9792
9793 /* Add a DIE to the delayed physname list. */
9794
9795 static void
9796 add_to_method_list (struct type *type, int fnfield_index, int index,
9797 const char *name, struct die_info *die,
9798 struct dwarf2_cu *cu)
9799 {
9800 struct delayed_method_info mi;
9801 mi.type = type;
9802 mi.fnfield_index = fnfield_index;
9803 mi.index = index;
9804 mi.name = name;
9805 mi.die = die;
9806 cu->method_list.push_back (mi);
9807 }
9808
9809 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9810 "const" / "volatile". If so, decrements LEN by the length of the
9811 modifier and return true. Otherwise return false. */
9812
9813 template<size_t N>
9814 static bool
9815 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9816 {
9817 size_t mod_len = sizeof (mod) - 1;
9818 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9819 {
9820 len -= mod_len;
9821 return true;
9822 }
9823 return false;
9824 }
9825
9826 /* Compute the physnames of any methods on the CU's method list.
9827
9828 The computation of method physnames is delayed in order to avoid the
9829 (bad) condition that one of the method's formal parameters is of an as yet
9830 incomplete type. */
9831
9832 static void
9833 compute_delayed_physnames (struct dwarf2_cu *cu)
9834 {
9835 /* Only C++ delays computing physnames. */
9836 if (cu->method_list.empty ())
9837 return;
9838 gdb_assert (cu->language == language_cplus);
9839
9840 for (const delayed_method_info &mi : cu->method_list)
9841 {
9842 const char *physname;
9843 struct fn_fieldlist *fn_flp
9844 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9845 physname = dwarf2_physname (mi.name, mi.die, cu);
9846 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9847 = physname ? physname : "";
9848
9849 /* Since there's no tag to indicate whether a method is a
9850 const/volatile overload, extract that information out of the
9851 demangled name. */
9852 if (physname != NULL)
9853 {
9854 size_t len = strlen (physname);
9855
9856 while (1)
9857 {
9858 if (physname[len] == ')') /* shortcut */
9859 break;
9860 else if (check_modifier (physname, len, " const"))
9861 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9862 else if (check_modifier (physname, len, " volatile"))
9863 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9864 else
9865 break;
9866 }
9867 }
9868 }
9869
9870 /* The list is no longer needed. */
9871 cu->method_list.clear ();
9872 }
9873
9874 /* Go objects should be embedded in a DW_TAG_module DIE,
9875 and it's not clear if/how imported objects will appear.
9876 To keep Go support simple until that's worked out,
9877 go back through what we've read and create something usable.
9878 We could do this while processing each DIE, and feels kinda cleaner,
9879 but that way is more invasive.
9880 This is to, for example, allow the user to type "p var" or "b main"
9881 without having to specify the package name, and allow lookups
9882 of module.object to work in contexts that use the expression
9883 parser. */
9884
9885 static void
9886 fixup_go_packaging (struct dwarf2_cu *cu)
9887 {
9888 gdb::unique_xmalloc_ptr<char> package_name;
9889 struct pending *list;
9890 int i;
9891
9892 for (list = *cu->get_builder ()->get_global_symbols ();
9893 list != NULL;
9894 list = list->next)
9895 {
9896 for (i = 0; i < list->nsyms; ++i)
9897 {
9898 struct symbol *sym = list->symbol[i];
9899
9900 if (sym->language () == language_go
9901 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9902 {
9903 gdb::unique_xmalloc_ptr<char> this_package_name
9904 (go_symbol_package_name (sym));
9905
9906 if (this_package_name == NULL)
9907 continue;
9908 if (package_name == NULL)
9909 package_name = std::move (this_package_name);
9910 else
9911 {
9912 struct objfile *objfile
9913 = cu->per_cu->dwarf2_per_objfile->objfile;
9914 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9915 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9916 (symbol_symtab (sym) != NULL
9917 ? symtab_to_filename_for_display
9918 (symbol_symtab (sym))
9919 : objfile_name (objfile)),
9920 this_package_name.get (), package_name.get ());
9921 }
9922 }
9923 }
9924 }
9925
9926 if (package_name != NULL)
9927 {
9928 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9929 const char *saved_package_name
9930 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
9931 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9932 saved_package_name);
9933 struct symbol *sym;
9934
9935 sym = allocate_symbol (objfile);
9936 sym->set_language (language_go, &objfile->objfile_obstack);
9937 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9938 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9939 e.g., "main" finds the "main" module and not C's main(). */
9940 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9941 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9942 SYMBOL_TYPE (sym) = type;
9943
9944 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9945 }
9946 }
9947
9948 /* Allocate a fully-qualified name consisting of the two parts on the
9949 obstack. */
9950
9951 static const char *
9952 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9953 {
9954 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9955 }
9956
9957 /* A helper that allocates a struct discriminant_info to attach to a
9958 union type. */
9959
9960 static struct discriminant_info *
9961 alloc_discriminant_info (struct type *type, int discriminant_index,
9962 int default_index)
9963 {
9964 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9965 gdb_assert (discriminant_index == -1
9966 || (discriminant_index >= 0
9967 && discriminant_index < TYPE_NFIELDS (type)));
9968 gdb_assert (default_index == -1
9969 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9970
9971 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9972
9973 struct discriminant_info *disc
9974 = ((struct discriminant_info *)
9975 TYPE_ZALLOC (type,
9976 offsetof (struct discriminant_info, discriminants)
9977 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9978 disc->default_index = default_index;
9979 disc->discriminant_index = discriminant_index;
9980
9981 struct dynamic_prop prop;
9982 prop.kind = PROP_UNDEFINED;
9983 prop.data.baton = disc;
9984
9985 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9986
9987 return disc;
9988 }
9989
9990 /* Some versions of rustc emitted enums in an unusual way.
9991
9992 Ordinary enums were emitted as unions. The first element of each
9993 structure in the union was named "RUST$ENUM$DISR". This element
9994 held the discriminant.
9995
9996 These versions of Rust also implemented the "non-zero"
9997 optimization. When the enum had two values, and one is empty and
9998 the other holds a pointer that cannot be zero, the pointer is used
9999 as the discriminant, with a zero value meaning the empty variant.
10000 Here, the union's first member is of the form
10001 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
10002 where the fieldnos are the indices of the fields that should be
10003 traversed in order to find the field (which may be several fields deep)
10004 and the variantname is the name of the variant of the case when the
10005 field is zero.
10006
10007 This function recognizes whether TYPE is of one of these forms,
10008 and, if so, smashes it to be a variant type. */
10009
10010 static void
10011 quirk_rust_enum (struct type *type, struct objfile *objfile)
10012 {
10013 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10014
10015 /* We don't need to deal with empty enums. */
10016 if (TYPE_NFIELDS (type) == 0)
10017 return;
10018
10019 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10020 if (TYPE_NFIELDS (type) == 1
10021 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10022 {
10023 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10024
10025 /* Decode the field name to find the offset of the
10026 discriminant. */
10027 ULONGEST bit_offset = 0;
10028 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10029 while (name[0] >= '0' && name[0] <= '9')
10030 {
10031 char *tail;
10032 unsigned long index = strtoul (name, &tail, 10);
10033 name = tail;
10034 if (*name != '$'
10035 || index >= TYPE_NFIELDS (field_type)
10036 || (TYPE_FIELD_LOC_KIND (field_type, index)
10037 != FIELD_LOC_KIND_BITPOS))
10038 {
10039 complaint (_("Could not parse Rust enum encoding string \"%s\""
10040 "[in module %s]"),
10041 TYPE_FIELD_NAME (type, 0),
10042 objfile_name (objfile));
10043 return;
10044 }
10045 ++name;
10046
10047 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10048 field_type = TYPE_FIELD_TYPE (field_type, index);
10049 }
10050
10051 /* Make a union to hold the variants. */
10052 struct type *union_type = alloc_type (objfile);
10053 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10054 TYPE_NFIELDS (union_type) = 3;
10055 TYPE_FIELDS (union_type)
10056 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10057 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10058 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10059
10060 /* Put the discriminant must at index 0. */
10061 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10062 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10063 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10064 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10065
10066 /* The order of fields doesn't really matter, so put the real
10067 field at index 1 and the data-less field at index 2. */
10068 struct discriminant_info *disc
10069 = alloc_discriminant_info (union_type, 0, 1);
10070 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10071 TYPE_FIELD_NAME (union_type, 1)
10072 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10073 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10074 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10075 TYPE_FIELD_NAME (union_type, 1));
10076
10077 const char *dataless_name
10078 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10079 name);
10080 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10081 dataless_name);
10082 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10083 /* NAME points into the original discriminant name, which
10084 already has the correct lifetime. */
10085 TYPE_FIELD_NAME (union_type, 2) = name;
10086 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10087 disc->discriminants[2] = 0;
10088
10089 /* Smash this type to be a structure type. We have to do this
10090 because the type has already been recorded. */
10091 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10092 TYPE_NFIELDS (type) = 1;
10093 TYPE_FIELDS (type)
10094 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10095
10096 /* Install the variant part. */
10097 TYPE_FIELD_TYPE (type, 0) = union_type;
10098 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10099 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10100 }
10101 /* A union with a single anonymous field is probably an old-style
10102 univariant enum. */
10103 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
10104 {
10105 /* Smash this type to be a structure type. We have to do this
10106 because the type has already been recorded. */
10107 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10108
10109 /* Make a union to hold the variants. */
10110 struct type *union_type = alloc_type (objfile);
10111 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10112 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10113 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10114 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10115 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10116
10117 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10118 const char *variant_name
10119 = rust_last_path_segment (TYPE_NAME (field_type));
10120 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10121 TYPE_NAME (field_type)
10122 = rust_fully_qualify (&objfile->objfile_obstack,
10123 TYPE_NAME (type), variant_name);
10124
10125 /* Install the union in the outer struct type. */
10126 TYPE_NFIELDS (type) = 1;
10127 TYPE_FIELDS (type)
10128 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10129 TYPE_FIELD_TYPE (type, 0) = union_type;
10130 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10131 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10132
10133 alloc_discriminant_info (union_type, -1, 0);
10134 }
10135 else
10136 {
10137 struct type *disr_type = nullptr;
10138 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10139 {
10140 disr_type = TYPE_FIELD_TYPE (type, i);
10141
10142 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10143 {
10144 /* All fields of a true enum will be structs. */
10145 return;
10146 }
10147 else if (TYPE_NFIELDS (disr_type) == 0)
10148 {
10149 /* Could be data-less variant, so keep going. */
10150 disr_type = nullptr;
10151 }
10152 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10153 "RUST$ENUM$DISR") != 0)
10154 {
10155 /* Not a Rust enum. */
10156 return;
10157 }
10158 else
10159 {
10160 /* Found one. */
10161 break;
10162 }
10163 }
10164
10165 /* If we got here without a discriminant, then it's probably
10166 just a union. */
10167 if (disr_type == nullptr)
10168 return;
10169
10170 /* Smash this type to be a structure type. We have to do this
10171 because the type has already been recorded. */
10172 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10173
10174 /* Make a union to hold the variants. */
10175 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10176 struct type *union_type = alloc_type (objfile);
10177 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10178 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10179 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10180 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10181 TYPE_FIELDS (union_type)
10182 = (struct field *) TYPE_ZALLOC (union_type,
10183 (TYPE_NFIELDS (union_type)
10184 * sizeof (struct field)));
10185
10186 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10187 TYPE_NFIELDS (type) * sizeof (struct field));
10188
10189 /* Install the discriminant at index 0 in the union. */
10190 TYPE_FIELD (union_type, 0) = *disr_field;
10191 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10192 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10193
10194 /* Install the union in the outer struct type. */
10195 TYPE_FIELD_TYPE (type, 0) = union_type;
10196 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10197 TYPE_NFIELDS (type) = 1;
10198
10199 /* Set the size and offset of the union type. */
10200 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10201
10202 /* We need a way to find the correct discriminant given a
10203 variant name. For convenience we build a map here. */
10204 struct type *enum_type = FIELD_TYPE (*disr_field);
10205 std::unordered_map<std::string, ULONGEST> discriminant_map;
10206 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10207 {
10208 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10209 {
10210 const char *name
10211 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10212 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10213 }
10214 }
10215
10216 int n_fields = TYPE_NFIELDS (union_type);
10217 struct discriminant_info *disc
10218 = alloc_discriminant_info (union_type, 0, -1);
10219 /* Skip the discriminant here. */
10220 for (int i = 1; i < n_fields; ++i)
10221 {
10222 /* Find the final word in the name of this variant's type.
10223 That name can be used to look up the correct
10224 discriminant. */
10225 const char *variant_name
10226 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10227 i)));
10228
10229 auto iter = discriminant_map.find (variant_name);
10230 if (iter != discriminant_map.end ())
10231 disc->discriminants[i] = iter->second;
10232
10233 /* Remove the discriminant field, if it exists. */
10234 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10235 if (TYPE_NFIELDS (sub_type) > 0)
10236 {
10237 --TYPE_NFIELDS (sub_type);
10238 ++TYPE_FIELDS (sub_type);
10239 }
10240 TYPE_FIELD_NAME (union_type, i) = variant_name;
10241 TYPE_NAME (sub_type)
10242 = rust_fully_qualify (&objfile->objfile_obstack,
10243 TYPE_NAME (type), variant_name);
10244 }
10245 }
10246 }
10247
10248 /* Rewrite some Rust unions to be structures with variants parts. */
10249
10250 static void
10251 rust_union_quirks (struct dwarf2_cu *cu)
10252 {
10253 gdb_assert (cu->language == language_rust);
10254 for (type *type_ : cu->rust_unions)
10255 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
10256 /* We don't need this any more. */
10257 cu->rust_unions.clear ();
10258 }
10259
10260 /* Return the symtab for PER_CU. This works properly regardless of
10261 whether we're using the index or psymtabs. */
10262
10263 static struct compunit_symtab *
10264 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10265 {
10266 return (per_cu->dwarf2_per_objfile->using_index
10267 ? per_cu->v.quick->compunit_symtab
10268 : per_cu->v.psymtab->compunit_symtab);
10269 }
10270
10271 /* A helper function for computing the list of all symbol tables
10272 included by PER_CU. */
10273
10274 static void
10275 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
10276 htab_t all_children, htab_t all_type_symtabs,
10277 struct dwarf2_per_cu_data *per_cu,
10278 struct compunit_symtab *immediate_parent)
10279 {
10280 void **slot;
10281 struct compunit_symtab *cust;
10282
10283 slot = htab_find_slot (all_children, per_cu, INSERT);
10284 if (*slot != NULL)
10285 {
10286 /* This inclusion and its children have been processed. */
10287 return;
10288 }
10289
10290 *slot = per_cu;
10291 /* Only add a CU if it has a symbol table. */
10292 cust = get_compunit_symtab (per_cu);
10293 if (cust != NULL)
10294 {
10295 /* If this is a type unit only add its symbol table if we haven't
10296 seen it yet (type unit per_cu's can share symtabs). */
10297 if (per_cu->is_debug_types)
10298 {
10299 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10300 if (*slot == NULL)
10301 {
10302 *slot = cust;
10303 result->push_back (cust);
10304 if (cust->user == NULL)
10305 cust->user = immediate_parent;
10306 }
10307 }
10308 else
10309 {
10310 result->push_back (cust);
10311 if (cust->user == NULL)
10312 cust->user = immediate_parent;
10313 }
10314 }
10315
10316 if (!per_cu->imported_symtabs_empty ())
10317 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10318 {
10319 recursively_compute_inclusions (result, all_children,
10320 all_type_symtabs, ptr, cust);
10321 }
10322 }
10323
10324 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10325 PER_CU. */
10326
10327 static void
10328 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10329 {
10330 gdb_assert (! per_cu->is_debug_types);
10331
10332 if (!per_cu->imported_symtabs_empty ())
10333 {
10334 int len;
10335 std::vector<compunit_symtab *> result_symtabs;
10336 htab_t all_children, all_type_symtabs;
10337 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10338
10339 /* If we don't have a symtab, we can just skip this case. */
10340 if (cust == NULL)
10341 return;
10342
10343 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10344 NULL, xcalloc, xfree);
10345 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10346 NULL, xcalloc, xfree);
10347
10348 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10349 {
10350 recursively_compute_inclusions (&result_symtabs, all_children,
10351 all_type_symtabs, ptr, cust);
10352 }
10353
10354 /* Now we have a transitive closure of all the included symtabs. */
10355 len = result_symtabs.size ();
10356 cust->includes
10357 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10358 struct compunit_symtab *, len + 1);
10359 memcpy (cust->includes, result_symtabs.data (),
10360 len * sizeof (compunit_symtab *));
10361 cust->includes[len] = NULL;
10362
10363 htab_delete (all_children);
10364 htab_delete (all_type_symtabs);
10365 }
10366 }
10367
10368 /* Compute the 'includes' field for the symtabs of all the CUs we just
10369 read. */
10370
10371 static void
10372 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10373 {
10374 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
10375 {
10376 if (! iter->is_debug_types)
10377 compute_compunit_symtab_includes (iter);
10378 }
10379
10380 dwarf2_per_objfile->just_read_cus.clear ();
10381 }
10382
10383 /* Generate full symbol information for PER_CU, whose DIEs have
10384 already been loaded into memory. */
10385
10386 static void
10387 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10388 enum language pretend_language)
10389 {
10390 struct dwarf2_cu *cu = per_cu->cu;
10391 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10392 struct objfile *objfile = dwarf2_per_objfile->objfile;
10393 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10394 CORE_ADDR lowpc, highpc;
10395 struct compunit_symtab *cust;
10396 CORE_ADDR baseaddr;
10397 struct block *static_block;
10398 CORE_ADDR addr;
10399
10400 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
10401
10402 /* Clear the list here in case something was left over. */
10403 cu->method_list.clear ();
10404
10405 cu->language = pretend_language;
10406 cu->language_defn = language_def (cu->language);
10407
10408 /* Do line number decoding in read_file_scope () */
10409 process_die (cu->dies, cu);
10410
10411 /* For now fudge the Go package. */
10412 if (cu->language == language_go)
10413 fixup_go_packaging (cu);
10414
10415 /* Now that we have processed all the DIEs in the CU, all the types
10416 should be complete, and it should now be safe to compute all of the
10417 physnames. */
10418 compute_delayed_physnames (cu);
10419
10420 if (cu->language == language_rust)
10421 rust_union_quirks (cu);
10422
10423 /* Some compilers don't define a DW_AT_high_pc attribute for the
10424 compilation unit. If the DW_AT_high_pc is missing, synthesize
10425 it, by scanning the DIE's below the compilation unit. */
10426 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10427
10428 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10429 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10430
10431 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10432 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10433 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10434 addrmap to help ensure it has an accurate map of pc values belonging to
10435 this comp unit. */
10436 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10437
10438 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10439 SECT_OFF_TEXT (objfile),
10440 0);
10441
10442 if (cust != NULL)
10443 {
10444 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10445
10446 /* Set symtab language to language from DW_AT_language. If the
10447 compilation is from a C file generated by language preprocessors, do
10448 not set the language if it was already deduced by start_subfile. */
10449 if (!(cu->language == language_c
10450 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10451 COMPUNIT_FILETABS (cust)->language = cu->language;
10452
10453 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10454 produce DW_AT_location with location lists but it can be possibly
10455 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10456 there were bugs in prologue debug info, fixed later in GCC-4.5
10457 by "unwind info for epilogues" patch (which is not directly related).
10458
10459 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10460 needed, it would be wrong due to missing DW_AT_producer there.
10461
10462 Still one can confuse GDB by using non-standard GCC compilation
10463 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10464 */
10465 if (cu->has_loclist && gcc_4_minor >= 5)
10466 cust->locations_valid = 1;
10467
10468 if (gcc_4_minor >= 5)
10469 cust->epilogue_unwind_valid = 1;
10470
10471 cust->call_site_htab = cu->call_site_htab;
10472 }
10473
10474 if (dwarf2_per_objfile->using_index)
10475 per_cu->v.quick->compunit_symtab = cust;
10476 else
10477 {
10478 struct partial_symtab *pst = per_cu->v.psymtab;
10479 pst->compunit_symtab = cust;
10480 pst->readin = 1;
10481 }
10482
10483 /* Push it for inclusion processing later. */
10484 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10485
10486 /* Not needed any more. */
10487 cu->reset_builder ();
10488 }
10489
10490 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10491 already been loaded into memory. */
10492
10493 static void
10494 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10495 enum language pretend_language)
10496 {
10497 struct dwarf2_cu *cu = per_cu->cu;
10498 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10499 struct objfile *objfile = dwarf2_per_objfile->objfile;
10500 struct compunit_symtab *cust;
10501 struct signatured_type *sig_type;
10502
10503 gdb_assert (per_cu->is_debug_types);
10504 sig_type = (struct signatured_type *) per_cu;
10505
10506 /* Clear the list here in case something was left over. */
10507 cu->method_list.clear ();
10508
10509 cu->language = pretend_language;
10510 cu->language_defn = language_def (cu->language);
10511
10512 /* The symbol tables are set up in read_type_unit_scope. */
10513 process_die (cu->dies, cu);
10514
10515 /* For now fudge the Go package. */
10516 if (cu->language == language_go)
10517 fixup_go_packaging (cu);
10518
10519 /* Now that we have processed all the DIEs in the CU, all the types
10520 should be complete, and it should now be safe to compute all of the
10521 physnames. */
10522 compute_delayed_physnames (cu);
10523
10524 if (cu->language == language_rust)
10525 rust_union_quirks (cu);
10526
10527 /* TUs share symbol tables.
10528 If this is the first TU to use this symtab, complete the construction
10529 of it with end_expandable_symtab. Otherwise, complete the addition of
10530 this TU's symbols to the existing symtab. */
10531 if (sig_type->type_unit_group->compunit_symtab == NULL)
10532 {
10533 buildsym_compunit *builder = cu->get_builder ();
10534 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10535 sig_type->type_unit_group->compunit_symtab = cust;
10536
10537 if (cust != NULL)
10538 {
10539 /* Set symtab language to language from DW_AT_language. If the
10540 compilation is from a C file generated by language preprocessors,
10541 do not set the language if it was already deduced by
10542 start_subfile. */
10543 if (!(cu->language == language_c
10544 && COMPUNIT_FILETABS (cust)->language != language_c))
10545 COMPUNIT_FILETABS (cust)->language = cu->language;
10546 }
10547 }
10548 else
10549 {
10550 cu->get_builder ()->augment_type_symtab ();
10551 cust = sig_type->type_unit_group->compunit_symtab;
10552 }
10553
10554 if (dwarf2_per_objfile->using_index)
10555 per_cu->v.quick->compunit_symtab = cust;
10556 else
10557 {
10558 struct partial_symtab *pst = per_cu->v.psymtab;
10559 pst->compunit_symtab = cust;
10560 pst->readin = 1;
10561 }
10562
10563 /* Not needed any more. */
10564 cu->reset_builder ();
10565 }
10566
10567 /* Process an imported unit DIE. */
10568
10569 static void
10570 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10571 {
10572 struct attribute *attr;
10573
10574 /* For now we don't handle imported units in type units. */
10575 if (cu->per_cu->is_debug_types)
10576 {
10577 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10578 " supported in type units [in module %s]"),
10579 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10580 }
10581
10582 attr = dwarf2_attr (die, DW_AT_import, cu);
10583 if (attr != NULL)
10584 {
10585 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10586 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10587 dwarf2_per_cu_data *per_cu
10588 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10589 cu->per_cu->dwarf2_per_objfile);
10590
10591 /* If necessary, add it to the queue and load its DIEs. */
10592 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10593 load_full_comp_unit (per_cu, false, cu->language);
10594
10595 cu->per_cu->imported_symtabs_push (per_cu);
10596 }
10597 }
10598
10599 /* RAII object that represents a process_die scope: i.e.,
10600 starts/finishes processing a DIE. */
10601 class process_die_scope
10602 {
10603 public:
10604 process_die_scope (die_info *die, dwarf2_cu *cu)
10605 : m_die (die), m_cu (cu)
10606 {
10607 /* We should only be processing DIEs not already in process. */
10608 gdb_assert (!m_die->in_process);
10609 m_die->in_process = true;
10610 }
10611
10612 ~process_die_scope ()
10613 {
10614 m_die->in_process = false;
10615
10616 /* If we're done processing the DIE for the CU that owns the line
10617 header, we don't need the line header anymore. */
10618 if (m_cu->line_header_die_owner == m_die)
10619 {
10620 delete m_cu->line_header;
10621 m_cu->line_header = NULL;
10622 m_cu->line_header_die_owner = NULL;
10623 }
10624 }
10625
10626 private:
10627 die_info *m_die;
10628 dwarf2_cu *m_cu;
10629 };
10630
10631 /* Process a die and its children. */
10632
10633 static void
10634 process_die (struct die_info *die, struct dwarf2_cu *cu)
10635 {
10636 process_die_scope scope (die, cu);
10637
10638 switch (die->tag)
10639 {
10640 case DW_TAG_padding:
10641 break;
10642 case DW_TAG_compile_unit:
10643 case DW_TAG_partial_unit:
10644 read_file_scope (die, cu);
10645 break;
10646 case DW_TAG_type_unit:
10647 read_type_unit_scope (die, cu);
10648 break;
10649 case DW_TAG_subprogram:
10650 /* Nested subprograms in Fortran get a prefix. */
10651 if (cu->language == language_fortran
10652 && die->parent != NULL
10653 && die->parent->tag == DW_TAG_subprogram)
10654 cu->processing_has_namespace_info = true;
10655 /* Fall through. */
10656 case DW_TAG_inlined_subroutine:
10657 read_func_scope (die, cu);
10658 break;
10659 case DW_TAG_lexical_block:
10660 case DW_TAG_try_block:
10661 case DW_TAG_catch_block:
10662 read_lexical_block_scope (die, cu);
10663 break;
10664 case DW_TAG_call_site:
10665 case DW_TAG_GNU_call_site:
10666 read_call_site_scope (die, cu);
10667 break;
10668 case DW_TAG_class_type:
10669 case DW_TAG_interface_type:
10670 case DW_TAG_structure_type:
10671 case DW_TAG_union_type:
10672 process_structure_scope (die, cu);
10673 break;
10674 case DW_TAG_enumeration_type:
10675 process_enumeration_scope (die, cu);
10676 break;
10677
10678 /* These dies have a type, but processing them does not create
10679 a symbol or recurse to process the children. Therefore we can
10680 read them on-demand through read_type_die. */
10681 case DW_TAG_subroutine_type:
10682 case DW_TAG_set_type:
10683 case DW_TAG_array_type:
10684 case DW_TAG_pointer_type:
10685 case DW_TAG_ptr_to_member_type:
10686 case DW_TAG_reference_type:
10687 case DW_TAG_rvalue_reference_type:
10688 case DW_TAG_string_type:
10689 break;
10690
10691 case DW_TAG_base_type:
10692 case DW_TAG_subrange_type:
10693 case DW_TAG_typedef:
10694 /* Add a typedef symbol for the type definition, if it has a
10695 DW_AT_name. */
10696 new_symbol (die, read_type_die (die, cu), cu);
10697 break;
10698 case DW_TAG_common_block:
10699 read_common_block (die, cu);
10700 break;
10701 case DW_TAG_common_inclusion:
10702 break;
10703 case DW_TAG_namespace:
10704 cu->processing_has_namespace_info = true;
10705 read_namespace (die, cu);
10706 break;
10707 case DW_TAG_module:
10708 cu->processing_has_namespace_info = true;
10709 read_module (die, cu);
10710 break;
10711 case DW_TAG_imported_declaration:
10712 cu->processing_has_namespace_info = true;
10713 if (read_namespace_alias (die, cu))
10714 break;
10715 /* The declaration is not a global namespace alias. */
10716 /* Fall through. */
10717 case DW_TAG_imported_module:
10718 cu->processing_has_namespace_info = true;
10719 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10720 || cu->language != language_fortran))
10721 complaint (_("Tag '%s' has unexpected children"),
10722 dwarf_tag_name (die->tag));
10723 read_import_statement (die, cu);
10724 break;
10725
10726 case DW_TAG_imported_unit:
10727 process_imported_unit_die (die, cu);
10728 break;
10729
10730 case DW_TAG_variable:
10731 read_variable (die, cu);
10732 break;
10733
10734 default:
10735 new_symbol (die, NULL, cu);
10736 break;
10737 }
10738 }
10739 \f
10740 /* DWARF name computation. */
10741
10742 /* A helper function for dwarf2_compute_name which determines whether DIE
10743 needs to have the name of the scope prepended to the name listed in the
10744 die. */
10745
10746 static int
10747 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10748 {
10749 struct attribute *attr;
10750
10751 switch (die->tag)
10752 {
10753 case DW_TAG_namespace:
10754 case DW_TAG_typedef:
10755 case DW_TAG_class_type:
10756 case DW_TAG_interface_type:
10757 case DW_TAG_structure_type:
10758 case DW_TAG_union_type:
10759 case DW_TAG_enumeration_type:
10760 case DW_TAG_enumerator:
10761 case DW_TAG_subprogram:
10762 case DW_TAG_inlined_subroutine:
10763 case DW_TAG_member:
10764 case DW_TAG_imported_declaration:
10765 return 1;
10766
10767 case DW_TAG_variable:
10768 case DW_TAG_constant:
10769 /* We only need to prefix "globally" visible variables. These include
10770 any variable marked with DW_AT_external or any variable that
10771 lives in a namespace. [Variables in anonymous namespaces
10772 require prefixing, but they are not DW_AT_external.] */
10773
10774 if (dwarf2_attr (die, DW_AT_specification, cu))
10775 {
10776 struct dwarf2_cu *spec_cu = cu;
10777
10778 return die_needs_namespace (die_specification (die, &spec_cu),
10779 spec_cu);
10780 }
10781
10782 attr = dwarf2_attr (die, DW_AT_external, cu);
10783 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10784 && die->parent->tag != DW_TAG_module)
10785 return 0;
10786 /* A variable in a lexical block of some kind does not need a
10787 namespace, even though in C++ such variables may be external
10788 and have a mangled name. */
10789 if (die->parent->tag == DW_TAG_lexical_block
10790 || die->parent->tag == DW_TAG_try_block
10791 || die->parent->tag == DW_TAG_catch_block
10792 || die->parent->tag == DW_TAG_subprogram)
10793 return 0;
10794 return 1;
10795
10796 default:
10797 return 0;
10798 }
10799 }
10800
10801 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10802 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10803 defined for the given DIE. */
10804
10805 static struct attribute *
10806 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10807 {
10808 struct attribute *attr;
10809
10810 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10811 if (attr == NULL)
10812 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10813
10814 return attr;
10815 }
10816
10817 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10818 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10819 defined for the given DIE. */
10820
10821 static const char *
10822 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10823 {
10824 const char *linkage_name;
10825
10826 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10827 if (linkage_name == NULL)
10828 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10829
10830 return linkage_name;
10831 }
10832
10833 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10834 compute the physname for the object, which include a method's:
10835 - formal parameters (C++),
10836 - receiver type (Go),
10837
10838 The term "physname" is a bit confusing.
10839 For C++, for example, it is the demangled name.
10840 For Go, for example, it's the mangled name.
10841
10842 For Ada, return the DIE's linkage name rather than the fully qualified
10843 name. PHYSNAME is ignored..
10844
10845 The result is allocated on the objfile_obstack and canonicalized. */
10846
10847 static const char *
10848 dwarf2_compute_name (const char *name,
10849 struct die_info *die, struct dwarf2_cu *cu,
10850 int physname)
10851 {
10852 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10853
10854 if (name == NULL)
10855 name = dwarf2_name (die, cu);
10856
10857 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10858 but otherwise compute it by typename_concat inside GDB.
10859 FIXME: Actually this is not really true, or at least not always true.
10860 It's all very confusing. compute_and_set_names doesn't try to demangle
10861 Fortran names because there is no mangling standard. So new_symbol
10862 will set the demangled name to the result of dwarf2_full_name, and it is
10863 the demangled name that GDB uses if it exists. */
10864 if (cu->language == language_ada
10865 || (cu->language == language_fortran && physname))
10866 {
10867 /* For Ada unit, we prefer the linkage name over the name, as
10868 the former contains the exported name, which the user expects
10869 to be able to reference. Ideally, we want the user to be able
10870 to reference this entity using either natural or linkage name,
10871 but we haven't started looking at this enhancement yet. */
10872 const char *linkage_name = dw2_linkage_name (die, cu);
10873
10874 if (linkage_name != NULL)
10875 return linkage_name;
10876 }
10877
10878 /* These are the only languages we know how to qualify names in. */
10879 if (name != NULL
10880 && (cu->language == language_cplus
10881 || cu->language == language_fortran || cu->language == language_d
10882 || cu->language == language_rust))
10883 {
10884 if (die_needs_namespace (die, cu))
10885 {
10886 const char *prefix;
10887 const char *canonical_name = NULL;
10888
10889 string_file buf;
10890
10891 prefix = determine_prefix (die, cu);
10892 if (*prefix != '\0')
10893 {
10894 gdb::unique_xmalloc_ptr<char> prefixed_name
10895 (typename_concat (NULL, prefix, name, physname, cu));
10896
10897 buf.puts (prefixed_name.get ());
10898 }
10899 else
10900 buf.puts (name);
10901
10902 /* Template parameters may be specified in the DIE's DW_AT_name, or
10903 as children with DW_TAG_template_type_param or
10904 DW_TAG_value_type_param. If the latter, add them to the name
10905 here. If the name already has template parameters, then
10906 skip this step; some versions of GCC emit both, and
10907 it is more efficient to use the pre-computed name.
10908
10909 Something to keep in mind about this process: it is very
10910 unlikely, or in some cases downright impossible, to produce
10911 something that will match the mangled name of a function.
10912 If the definition of the function has the same debug info,
10913 we should be able to match up with it anyway. But fallbacks
10914 using the minimal symbol, for instance to find a method
10915 implemented in a stripped copy of libstdc++, will not work.
10916 If we do not have debug info for the definition, we will have to
10917 match them up some other way.
10918
10919 When we do name matching there is a related problem with function
10920 templates; two instantiated function templates are allowed to
10921 differ only by their return types, which we do not add here. */
10922
10923 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10924 {
10925 struct attribute *attr;
10926 struct die_info *child;
10927 int first = 1;
10928
10929 die->building_fullname = 1;
10930
10931 for (child = die->child; child != NULL; child = child->sibling)
10932 {
10933 struct type *type;
10934 LONGEST value;
10935 const gdb_byte *bytes;
10936 struct dwarf2_locexpr_baton *baton;
10937 struct value *v;
10938
10939 if (child->tag != DW_TAG_template_type_param
10940 && child->tag != DW_TAG_template_value_param)
10941 continue;
10942
10943 if (first)
10944 {
10945 buf.puts ("<");
10946 first = 0;
10947 }
10948 else
10949 buf.puts (", ");
10950
10951 attr = dwarf2_attr (child, DW_AT_type, cu);
10952 if (attr == NULL)
10953 {
10954 complaint (_("template parameter missing DW_AT_type"));
10955 buf.puts ("UNKNOWN_TYPE");
10956 continue;
10957 }
10958 type = die_type (child, cu);
10959
10960 if (child->tag == DW_TAG_template_type_param)
10961 {
10962 c_print_type (type, "", &buf, -1, 0, cu->language,
10963 &type_print_raw_options);
10964 continue;
10965 }
10966
10967 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10968 if (attr == NULL)
10969 {
10970 complaint (_("template parameter missing "
10971 "DW_AT_const_value"));
10972 buf.puts ("UNKNOWN_VALUE");
10973 continue;
10974 }
10975
10976 dwarf2_const_value_attr (attr, type, name,
10977 &cu->comp_unit_obstack, cu,
10978 &value, &bytes, &baton);
10979
10980 if (TYPE_NOSIGN (type))
10981 /* GDB prints characters as NUMBER 'CHAR'. If that's
10982 changed, this can use value_print instead. */
10983 c_printchar (value, type, &buf);
10984 else
10985 {
10986 struct value_print_options opts;
10987
10988 if (baton != NULL)
10989 v = dwarf2_evaluate_loc_desc (type, NULL,
10990 baton->data,
10991 baton->size,
10992 baton->per_cu);
10993 else if (bytes != NULL)
10994 {
10995 v = allocate_value (type);
10996 memcpy (value_contents_writeable (v), bytes,
10997 TYPE_LENGTH (type));
10998 }
10999 else
11000 v = value_from_longest (type, value);
11001
11002 /* Specify decimal so that we do not depend on
11003 the radix. */
11004 get_formatted_print_options (&opts, 'd');
11005 opts.raw = 1;
11006 value_print (v, &buf, &opts);
11007 release_value (v);
11008 }
11009 }
11010
11011 die->building_fullname = 0;
11012
11013 if (!first)
11014 {
11015 /* Close the argument list, with a space if necessary
11016 (nested templates). */
11017 if (!buf.empty () && buf.string ().back () == '>')
11018 buf.puts (" >");
11019 else
11020 buf.puts (">");
11021 }
11022 }
11023
11024 /* For C++ methods, append formal parameter type
11025 information, if PHYSNAME. */
11026
11027 if (physname && die->tag == DW_TAG_subprogram
11028 && cu->language == language_cplus)
11029 {
11030 struct type *type = read_type_die (die, cu);
11031
11032 c_type_print_args (type, &buf, 1, cu->language,
11033 &type_print_raw_options);
11034
11035 if (cu->language == language_cplus)
11036 {
11037 /* Assume that an artificial first parameter is
11038 "this", but do not crash if it is not. RealView
11039 marks unnamed (and thus unused) parameters as
11040 artificial; there is no way to differentiate
11041 the two cases. */
11042 if (TYPE_NFIELDS (type) > 0
11043 && TYPE_FIELD_ARTIFICIAL (type, 0)
11044 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11045 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11046 0))))
11047 buf.puts (" const");
11048 }
11049 }
11050
11051 const std::string &intermediate_name = buf.string ();
11052
11053 if (cu->language == language_cplus)
11054 canonical_name
11055 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11056 &objfile->per_bfd->storage_obstack);
11057
11058 /* If we only computed INTERMEDIATE_NAME, or if
11059 INTERMEDIATE_NAME is already canonical, then we need to
11060 copy it to the appropriate obstack. */
11061 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11062 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11063 intermediate_name);
11064 else
11065 name = canonical_name;
11066 }
11067 }
11068
11069 return name;
11070 }
11071
11072 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11073 If scope qualifiers are appropriate they will be added. The result
11074 will be allocated on the storage_obstack, or NULL if the DIE does
11075 not have a name. NAME may either be from a previous call to
11076 dwarf2_name or NULL.
11077
11078 The output string will be canonicalized (if C++). */
11079
11080 static const char *
11081 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11082 {
11083 return dwarf2_compute_name (name, die, cu, 0);
11084 }
11085
11086 /* Construct a physname for the given DIE in CU. NAME may either be
11087 from a previous call to dwarf2_name or NULL. The result will be
11088 allocated on the objfile_objstack or NULL if the DIE does not have a
11089 name.
11090
11091 The output string will be canonicalized (if C++). */
11092
11093 static const char *
11094 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11095 {
11096 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11097 const char *retval, *mangled = NULL, *canon = NULL;
11098 int need_copy = 1;
11099
11100 /* In this case dwarf2_compute_name is just a shortcut not building anything
11101 on its own. */
11102 if (!die_needs_namespace (die, cu))
11103 return dwarf2_compute_name (name, die, cu, 1);
11104
11105 mangled = dw2_linkage_name (die, cu);
11106
11107 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11108 See https://github.com/rust-lang/rust/issues/32925. */
11109 if (cu->language == language_rust && mangled != NULL
11110 && strchr (mangled, '{') != NULL)
11111 mangled = NULL;
11112
11113 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11114 has computed. */
11115 gdb::unique_xmalloc_ptr<char> demangled;
11116 if (mangled != NULL)
11117 {
11118
11119 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11120 {
11121 /* Do nothing (do not demangle the symbol name). */
11122 }
11123 else if (cu->language == language_go)
11124 {
11125 /* This is a lie, but we already lie to the caller new_symbol.
11126 new_symbol assumes we return the mangled name.
11127 This just undoes that lie until things are cleaned up. */
11128 }
11129 else
11130 {
11131 /* Use DMGL_RET_DROP for C++ template functions to suppress
11132 their return type. It is easier for GDB users to search
11133 for such functions as `name(params)' than `long name(params)'.
11134 In such case the minimal symbol names do not match the full
11135 symbol names but for template functions there is never a need
11136 to look up their definition from their declaration so
11137 the only disadvantage remains the minimal symbol variant
11138 `long name(params)' does not have the proper inferior type. */
11139 demangled.reset (gdb_demangle (mangled,
11140 (DMGL_PARAMS | DMGL_ANSI
11141 | DMGL_RET_DROP)));
11142 }
11143 if (demangled)
11144 canon = demangled.get ();
11145 else
11146 {
11147 canon = mangled;
11148 need_copy = 0;
11149 }
11150 }
11151
11152 if (canon == NULL || check_physname)
11153 {
11154 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11155
11156 if (canon != NULL && strcmp (physname, canon) != 0)
11157 {
11158 /* It may not mean a bug in GDB. The compiler could also
11159 compute DW_AT_linkage_name incorrectly. But in such case
11160 GDB would need to be bug-to-bug compatible. */
11161
11162 complaint (_("Computed physname <%s> does not match demangled <%s> "
11163 "(from linkage <%s>) - DIE at %s [in module %s]"),
11164 physname, canon, mangled, sect_offset_str (die->sect_off),
11165 objfile_name (objfile));
11166
11167 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11168 is available here - over computed PHYSNAME. It is safer
11169 against both buggy GDB and buggy compilers. */
11170
11171 retval = canon;
11172 }
11173 else
11174 {
11175 retval = physname;
11176 need_copy = 0;
11177 }
11178 }
11179 else
11180 retval = canon;
11181
11182 if (need_copy)
11183 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
11184
11185 return retval;
11186 }
11187
11188 /* Inspect DIE in CU for a namespace alias. If one exists, record
11189 a new symbol for it.
11190
11191 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11192
11193 static int
11194 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11195 {
11196 struct attribute *attr;
11197
11198 /* If the die does not have a name, this is not a namespace
11199 alias. */
11200 attr = dwarf2_attr (die, DW_AT_name, cu);
11201 if (attr != NULL)
11202 {
11203 int num;
11204 struct die_info *d = die;
11205 struct dwarf2_cu *imported_cu = cu;
11206
11207 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11208 keep inspecting DIEs until we hit the underlying import. */
11209 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11210 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11211 {
11212 attr = dwarf2_attr (d, DW_AT_import, cu);
11213 if (attr == NULL)
11214 break;
11215
11216 d = follow_die_ref (d, attr, &imported_cu);
11217 if (d->tag != DW_TAG_imported_declaration)
11218 break;
11219 }
11220
11221 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11222 {
11223 complaint (_("DIE at %s has too many recursively imported "
11224 "declarations"), sect_offset_str (d->sect_off));
11225 return 0;
11226 }
11227
11228 if (attr != NULL)
11229 {
11230 struct type *type;
11231 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11232
11233 type = get_die_type_at_offset (sect_off, cu->per_cu);
11234 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11235 {
11236 /* This declaration is a global namespace alias. Add
11237 a symbol for it whose type is the aliased namespace. */
11238 new_symbol (die, type, cu);
11239 return 1;
11240 }
11241 }
11242 }
11243
11244 return 0;
11245 }
11246
11247 /* Return the using directives repository (global or local?) to use in the
11248 current context for CU.
11249
11250 For Ada, imported declarations can materialize renamings, which *may* be
11251 global. However it is impossible (for now?) in DWARF to distinguish
11252 "external" imported declarations and "static" ones. As all imported
11253 declarations seem to be static in all other languages, make them all CU-wide
11254 global only in Ada. */
11255
11256 static struct using_direct **
11257 using_directives (struct dwarf2_cu *cu)
11258 {
11259 if (cu->language == language_ada
11260 && cu->get_builder ()->outermost_context_p ())
11261 return cu->get_builder ()->get_global_using_directives ();
11262 else
11263 return cu->get_builder ()->get_local_using_directives ();
11264 }
11265
11266 /* Read the import statement specified by the given die and record it. */
11267
11268 static void
11269 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11270 {
11271 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11272 struct attribute *import_attr;
11273 struct die_info *imported_die, *child_die;
11274 struct dwarf2_cu *imported_cu;
11275 const char *imported_name;
11276 const char *imported_name_prefix;
11277 const char *canonical_name;
11278 const char *import_alias;
11279 const char *imported_declaration = NULL;
11280 const char *import_prefix;
11281 std::vector<const char *> excludes;
11282
11283 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11284 if (import_attr == NULL)
11285 {
11286 complaint (_("Tag '%s' has no DW_AT_import"),
11287 dwarf_tag_name (die->tag));
11288 return;
11289 }
11290
11291 imported_cu = cu;
11292 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11293 imported_name = dwarf2_name (imported_die, imported_cu);
11294 if (imported_name == NULL)
11295 {
11296 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11297
11298 The import in the following code:
11299 namespace A
11300 {
11301 typedef int B;
11302 }
11303
11304 int main ()
11305 {
11306 using A::B;
11307 B b;
11308 return b;
11309 }
11310
11311 ...
11312 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11313 <52> DW_AT_decl_file : 1
11314 <53> DW_AT_decl_line : 6
11315 <54> DW_AT_import : <0x75>
11316 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11317 <59> DW_AT_name : B
11318 <5b> DW_AT_decl_file : 1
11319 <5c> DW_AT_decl_line : 2
11320 <5d> DW_AT_type : <0x6e>
11321 ...
11322 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11323 <76> DW_AT_byte_size : 4
11324 <77> DW_AT_encoding : 5 (signed)
11325
11326 imports the wrong die ( 0x75 instead of 0x58 ).
11327 This case will be ignored until the gcc bug is fixed. */
11328 return;
11329 }
11330
11331 /* Figure out the local name after import. */
11332 import_alias = dwarf2_name (die, cu);
11333
11334 /* Figure out where the statement is being imported to. */
11335 import_prefix = determine_prefix (die, cu);
11336
11337 /* Figure out what the scope of the imported die is and prepend it
11338 to the name of the imported die. */
11339 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11340
11341 if (imported_die->tag != DW_TAG_namespace
11342 && imported_die->tag != DW_TAG_module)
11343 {
11344 imported_declaration = imported_name;
11345 canonical_name = imported_name_prefix;
11346 }
11347 else if (strlen (imported_name_prefix) > 0)
11348 canonical_name = obconcat (&objfile->objfile_obstack,
11349 imported_name_prefix,
11350 (cu->language == language_d ? "." : "::"),
11351 imported_name, (char *) NULL);
11352 else
11353 canonical_name = imported_name;
11354
11355 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11356 for (child_die = die->child; child_die && child_die->tag;
11357 child_die = sibling_die (child_die))
11358 {
11359 /* DWARF-4: A Fortran use statement with a “rename list” may be
11360 represented by an imported module entry with an import attribute
11361 referring to the module and owned entries corresponding to those
11362 entities that are renamed as part of being imported. */
11363
11364 if (child_die->tag != DW_TAG_imported_declaration)
11365 {
11366 complaint (_("child DW_TAG_imported_declaration expected "
11367 "- DIE at %s [in module %s]"),
11368 sect_offset_str (child_die->sect_off),
11369 objfile_name (objfile));
11370 continue;
11371 }
11372
11373 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11374 if (import_attr == NULL)
11375 {
11376 complaint (_("Tag '%s' has no DW_AT_import"),
11377 dwarf_tag_name (child_die->tag));
11378 continue;
11379 }
11380
11381 imported_cu = cu;
11382 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11383 &imported_cu);
11384 imported_name = dwarf2_name (imported_die, imported_cu);
11385 if (imported_name == NULL)
11386 {
11387 complaint (_("child DW_TAG_imported_declaration has unknown "
11388 "imported name - DIE at %s [in module %s]"),
11389 sect_offset_str (child_die->sect_off),
11390 objfile_name (objfile));
11391 continue;
11392 }
11393
11394 excludes.push_back (imported_name);
11395
11396 process_die (child_die, cu);
11397 }
11398
11399 add_using_directive (using_directives (cu),
11400 import_prefix,
11401 canonical_name,
11402 import_alias,
11403 imported_declaration,
11404 excludes,
11405 0,
11406 &objfile->objfile_obstack);
11407 }
11408
11409 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11410 types, but gives them a size of zero. Starting with version 14,
11411 ICC is compatible with GCC. */
11412
11413 static bool
11414 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11415 {
11416 if (!cu->checked_producer)
11417 check_producer (cu);
11418
11419 return cu->producer_is_icc_lt_14;
11420 }
11421
11422 /* ICC generates a DW_AT_type for C void functions. This was observed on
11423 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11424 which says that void functions should not have a DW_AT_type. */
11425
11426 static bool
11427 producer_is_icc (struct dwarf2_cu *cu)
11428 {
11429 if (!cu->checked_producer)
11430 check_producer (cu);
11431
11432 return cu->producer_is_icc;
11433 }
11434
11435 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11436 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11437 this, it was first present in GCC release 4.3.0. */
11438
11439 static bool
11440 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11441 {
11442 if (!cu->checked_producer)
11443 check_producer (cu);
11444
11445 return cu->producer_is_gcc_lt_4_3;
11446 }
11447
11448 static file_and_directory
11449 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11450 {
11451 file_and_directory res;
11452
11453 /* Find the filename. Do not use dwarf2_name here, since the filename
11454 is not a source language identifier. */
11455 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11456 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11457
11458 if (res.comp_dir == NULL
11459 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11460 && IS_ABSOLUTE_PATH (res.name))
11461 {
11462 res.comp_dir_storage = ldirname (res.name);
11463 if (!res.comp_dir_storage.empty ())
11464 res.comp_dir = res.comp_dir_storage.c_str ();
11465 }
11466 if (res.comp_dir != NULL)
11467 {
11468 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11469 directory, get rid of it. */
11470 const char *cp = strchr (res.comp_dir, ':');
11471
11472 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11473 res.comp_dir = cp + 1;
11474 }
11475
11476 if (res.name == NULL)
11477 res.name = "<unknown>";
11478
11479 return res;
11480 }
11481
11482 /* Handle DW_AT_stmt_list for a compilation unit.
11483 DIE is the DW_TAG_compile_unit die for CU.
11484 COMP_DIR is the compilation directory. LOWPC is passed to
11485 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11486
11487 static void
11488 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11489 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11490 {
11491 struct dwarf2_per_objfile *dwarf2_per_objfile
11492 = cu->per_cu->dwarf2_per_objfile;
11493 struct objfile *objfile = dwarf2_per_objfile->objfile;
11494 struct attribute *attr;
11495 struct line_header line_header_local;
11496 hashval_t line_header_local_hash;
11497 void **slot;
11498 int decode_mapping;
11499
11500 gdb_assert (! cu->per_cu->is_debug_types);
11501
11502 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11503 if (attr == NULL)
11504 return;
11505
11506 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11507
11508 /* The line header hash table is only created if needed (it exists to
11509 prevent redundant reading of the line table for partial_units).
11510 If we're given a partial_unit, we'll need it. If we're given a
11511 compile_unit, then use the line header hash table if it's already
11512 created, but don't create one just yet. */
11513
11514 if (dwarf2_per_objfile->line_header_hash == NULL
11515 && die->tag == DW_TAG_partial_unit)
11516 {
11517 dwarf2_per_objfile->line_header_hash
11518 = htab_create_alloc_ex (127, line_header_hash_voidp,
11519 line_header_eq_voidp,
11520 free_line_header_voidp,
11521 &objfile->objfile_obstack,
11522 hashtab_obstack_allocate,
11523 dummy_obstack_deallocate);
11524 }
11525
11526 line_header_local.sect_off = line_offset;
11527 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11528 line_header_local_hash = line_header_hash (&line_header_local);
11529 if (dwarf2_per_objfile->line_header_hash != NULL)
11530 {
11531 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11532 &line_header_local,
11533 line_header_local_hash, NO_INSERT);
11534
11535 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11536 is not present in *SLOT (since if there is something in *SLOT then
11537 it will be for a partial_unit). */
11538 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11539 {
11540 gdb_assert (*slot != NULL);
11541 cu->line_header = (struct line_header *) *slot;
11542 return;
11543 }
11544 }
11545
11546 /* dwarf_decode_line_header does not yet provide sufficient information.
11547 We always have to call also dwarf_decode_lines for it. */
11548 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11549 if (lh == NULL)
11550 return;
11551
11552 cu->line_header = lh.release ();
11553 cu->line_header_die_owner = die;
11554
11555 if (dwarf2_per_objfile->line_header_hash == NULL)
11556 slot = NULL;
11557 else
11558 {
11559 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11560 &line_header_local,
11561 line_header_local_hash, INSERT);
11562 gdb_assert (slot != NULL);
11563 }
11564 if (slot != NULL && *slot == NULL)
11565 {
11566 /* This newly decoded line number information unit will be owned
11567 by line_header_hash hash table. */
11568 *slot = cu->line_header;
11569 cu->line_header_die_owner = NULL;
11570 }
11571 else
11572 {
11573 /* We cannot free any current entry in (*slot) as that struct line_header
11574 may be already used by multiple CUs. Create only temporary decoded
11575 line_header for this CU - it may happen at most once for each line
11576 number information unit. And if we're not using line_header_hash
11577 then this is what we want as well. */
11578 gdb_assert (die->tag != DW_TAG_partial_unit);
11579 }
11580 decode_mapping = (die->tag != DW_TAG_partial_unit);
11581 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11582 decode_mapping);
11583
11584 }
11585
11586 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11587
11588 static void
11589 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11590 {
11591 struct dwarf2_per_objfile *dwarf2_per_objfile
11592 = cu->per_cu->dwarf2_per_objfile;
11593 struct objfile *objfile = dwarf2_per_objfile->objfile;
11594 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11595 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11596 CORE_ADDR highpc = ((CORE_ADDR) 0);
11597 struct attribute *attr;
11598 struct die_info *child_die;
11599 CORE_ADDR baseaddr;
11600
11601 prepare_one_comp_unit (cu, die, cu->language);
11602 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
11603
11604 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11605
11606 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11607 from finish_block. */
11608 if (lowpc == ((CORE_ADDR) -1))
11609 lowpc = highpc;
11610 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11611
11612 file_and_directory fnd = find_file_and_directory (die, cu);
11613
11614 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11615 standardised yet. As a workaround for the language detection we fall
11616 back to the DW_AT_producer string. */
11617 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11618 cu->language = language_opencl;
11619
11620 /* Similar hack for Go. */
11621 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11622 set_cu_language (DW_LANG_Go, cu);
11623
11624 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11625
11626 /* Decode line number information if present. We do this before
11627 processing child DIEs, so that the line header table is available
11628 for DW_AT_decl_file. */
11629 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11630
11631 /* Process all dies in compilation unit. */
11632 if (die->child != NULL)
11633 {
11634 child_die = die->child;
11635 while (child_die && child_die->tag)
11636 {
11637 process_die (child_die, cu);
11638 child_die = sibling_die (child_die);
11639 }
11640 }
11641
11642 /* Decode macro information, if present. Dwarf 2 macro information
11643 refers to information in the line number info statement program
11644 header, so we can only read it if we've read the header
11645 successfully. */
11646 attr = dwarf2_attr (die, DW_AT_macros, cu);
11647 if (attr == NULL)
11648 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11649 if (attr && cu->line_header)
11650 {
11651 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11652 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11653
11654 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11655 }
11656 else
11657 {
11658 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11659 if (attr && cu->line_header)
11660 {
11661 unsigned int macro_offset = DW_UNSND (attr);
11662
11663 dwarf_decode_macros (cu, macro_offset, 0);
11664 }
11665 }
11666 }
11667
11668 void
11669 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11670 {
11671 struct type_unit_group *tu_group;
11672 int first_time;
11673 struct attribute *attr;
11674 unsigned int i;
11675 struct signatured_type *sig_type;
11676
11677 gdb_assert (per_cu->is_debug_types);
11678 sig_type = (struct signatured_type *) per_cu;
11679
11680 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11681
11682 /* If we're using .gdb_index (includes -readnow) then
11683 per_cu->type_unit_group may not have been set up yet. */
11684 if (sig_type->type_unit_group == NULL)
11685 sig_type->type_unit_group = get_type_unit_group (this, attr);
11686 tu_group = sig_type->type_unit_group;
11687
11688 /* If we've already processed this stmt_list there's no real need to
11689 do it again, we could fake it and just recreate the part we need
11690 (file name,index -> symtab mapping). If data shows this optimization
11691 is useful we can do it then. */
11692 first_time = tu_group->compunit_symtab == NULL;
11693
11694 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11695 debug info. */
11696 line_header_up lh;
11697 if (attr != NULL)
11698 {
11699 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11700 lh = dwarf_decode_line_header (line_offset, this);
11701 }
11702 if (lh == NULL)
11703 {
11704 if (first_time)
11705 start_symtab ("", NULL, 0);
11706 else
11707 {
11708 gdb_assert (tu_group->symtabs == NULL);
11709 gdb_assert (m_builder == nullptr);
11710 struct compunit_symtab *cust = tu_group->compunit_symtab;
11711 m_builder.reset (new struct buildsym_compunit
11712 (COMPUNIT_OBJFILE (cust), "",
11713 COMPUNIT_DIRNAME (cust),
11714 compunit_language (cust),
11715 0, cust));
11716 }
11717 return;
11718 }
11719
11720 line_header = lh.release ();
11721 line_header_die_owner = die;
11722
11723 if (first_time)
11724 {
11725 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11726
11727 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11728 still initializing it, and our caller (a few levels up)
11729 process_full_type_unit still needs to know if this is the first
11730 time. */
11731
11732 tu_group->num_symtabs = line_header->file_names_size ();
11733 tu_group->symtabs = XNEWVEC (struct symtab *,
11734 line_header->file_names_size ());
11735
11736 auto &file_names = line_header->file_names ();
11737 for (i = 0; i < file_names.size (); ++i)
11738 {
11739 file_entry &fe = file_names[i];
11740 dwarf2_start_subfile (this, fe.name,
11741 fe.include_dir (line_header));
11742 buildsym_compunit *b = get_builder ();
11743 if (b->get_current_subfile ()->symtab == NULL)
11744 {
11745 /* NOTE: start_subfile will recognize when it's been
11746 passed a file it has already seen. So we can't
11747 assume there's a simple mapping from
11748 cu->line_header->file_names to subfiles, plus
11749 cu->line_header->file_names may contain dups. */
11750 b->get_current_subfile ()->symtab
11751 = allocate_symtab (cust, b->get_current_subfile ()->name);
11752 }
11753
11754 fe.symtab = b->get_current_subfile ()->symtab;
11755 tu_group->symtabs[i] = fe.symtab;
11756 }
11757 }
11758 else
11759 {
11760 gdb_assert (m_builder == nullptr);
11761 struct compunit_symtab *cust = tu_group->compunit_symtab;
11762 m_builder.reset (new struct buildsym_compunit
11763 (COMPUNIT_OBJFILE (cust), "",
11764 COMPUNIT_DIRNAME (cust),
11765 compunit_language (cust),
11766 0, cust));
11767
11768 auto &file_names = line_header->file_names ();
11769 for (i = 0; i < file_names.size (); ++i)
11770 {
11771 file_entry &fe = file_names[i];
11772 fe.symtab = tu_group->symtabs[i];
11773 }
11774 }
11775
11776 /* The main symtab is allocated last. Type units don't have DW_AT_name
11777 so they don't have a "real" (so to speak) symtab anyway.
11778 There is later code that will assign the main symtab to all symbols
11779 that don't have one. We need to handle the case of a symbol with a
11780 missing symtab (DW_AT_decl_file) anyway. */
11781 }
11782
11783 /* Process DW_TAG_type_unit.
11784 For TUs we want to skip the first top level sibling if it's not the
11785 actual type being defined by this TU. In this case the first top
11786 level sibling is there to provide context only. */
11787
11788 static void
11789 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11790 {
11791 struct die_info *child_die;
11792
11793 prepare_one_comp_unit (cu, die, language_minimal);
11794
11795 /* Initialize (or reinitialize) the machinery for building symtabs.
11796 We do this before processing child DIEs, so that the line header table
11797 is available for DW_AT_decl_file. */
11798 cu->setup_type_unit_groups (die);
11799
11800 if (die->child != NULL)
11801 {
11802 child_die = die->child;
11803 while (child_die && child_die->tag)
11804 {
11805 process_die (child_die, cu);
11806 child_die = sibling_die (child_die);
11807 }
11808 }
11809 }
11810 \f
11811 /* DWO/DWP files.
11812
11813 http://gcc.gnu.org/wiki/DebugFission
11814 http://gcc.gnu.org/wiki/DebugFissionDWP
11815
11816 To simplify handling of both DWO files ("object" files with the DWARF info)
11817 and DWP files (a file with the DWOs packaged up into one file), we treat
11818 DWP files as having a collection of virtual DWO files. */
11819
11820 static hashval_t
11821 hash_dwo_file (const void *item)
11822 {
11823 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11824 hashval_t hash;
11825
11826 hash = htab_hash_string (dwo_file->dwo_name);
11827 if (dwo_file->comp_dir != NULL)
11828 hash += htab_hash_string (dwo_file->comp_dir);
11829 return hash;
11830 }
11831
11832 static int
11833 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11834 {
11835 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11836 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11837
11838 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11839 return 0;
11840 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11841 return lhs->comp_dir == rhs->comp_dir;
11842 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11843 }
11844
11845 /* Allocate a hash table for DWO files. */
11846
11847 static htab_up
11848 allocate_dwo_file_hash_table (struct objfile *objfile)
11849 {
11850 auto delete_dwo_file = [] (void *item)
11851 {
11852 struct dwo_file *dwo_file = (struct dwo_file *) item;
11853
11854 delete dwo_file;
11855 };
11856
11857 return htab_up (htab_create_alloc_ex (41,
11858 hash_dwo_file,
11859 eq_dwo_file,
11860 delete_dwo_file,
11861 &objfile->objfile_obstack,
11862 hashtab_obstack_allocate,
11863 dummy_obstack_deallocate));
11864 }
11865
11866 /* Lookup DWO file DWO_NAME. */
11867
11868 static void **
11869 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11870 const char *dwo_name,
11871 const char *comp_dir)
11872 {
11873 struct dwo_file find_entry;
11874 void **slot;
11875
11876 if (dwarf2_per_objfile->dwo_files == NULL)
11877 dwarf2_per_objfile->dwo_files
11878 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11879
11880 find_entry.dwo_name = dwo_name;
11881 find_entry.comp_dir = comp_dir;
11882 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11883 INSERT);
11884
11885 return slot;
11886 }
11887
11888 static hashval_t
11889 hash_dwo_unit (const void *item)
11890 {
11891 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11892
11893 /* This drops the top 32 bits of the id, but is ok for a hash. */
11894 return dwo_unit->signature;
11895 }
11896
11897 static int
11898 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11899 {
11900 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11901 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11902
11903 /* The signature is assumed to be unique within the DWO file.
11904 So while object file CU dwo_id's always have the value zero,
11905 that's OK, assuming each object file DWO file has only one CU,
11906 and that's the rule for now. */
11907 return lhs->signature == rhs->signature;
11908 }
11909
11910 /* Allocate a hash table for DWO CUs,TUs.
11911 There is one of these tables for each of CUs,TUs for each DWO file. */
11912
11913 static htab_t
11914 allocate_dwo_unit_table (struct objfile *objfile)
11915 {
11916 /* Start out with a pretty small number.
11917 Generally DWO files contain only one CU and maybe some TUs. */
11918 return htab_create_alloc_ex (3,
11919 hash_dwo_unit,
11920 eq_dwo_unit,
11921 NULL,
11922 &objfile->objfile_obstack,
11923 hashtab_obstack_allocate,
11924 dummy_obstack_deallocate);
11925 }
11926
11927 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11928
11929 struct create_dwo_cu_data
11930 {
11931 struct dwo_file *dwo_file;
11932 struct dwo_unit dwo_unit;
11933 };
11934
11935 /* die_reader_func for create_dwo_cu. */
11936
11937 static void
11938 create_dwo_cu_reader (const struct die_reader_specs *reader,
11939 const gdb_byte *info_ptr,
11940 struct die_info *comp_unit_die,
11941 int has_children,
11942 void *datap)
11943 {
11944 struct dwarf2_cu *cu = reader->cu;
11945 sect_offset sect_off = cu->per_cu->sect_off;
11946 struct dwarf2_section_info *section = cu->per_cu->section;
11947 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11948 struct dwo_file *dwo_file = data->dwo_file;
11949 struct dwo_unit *dwo_unit = &data->dwo_unit;
11950
11951 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11952 if (!signature.has_value ())
11953 {
11954 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11955 " its dwo_id [in module %s]"),
11956 sect_offset_str (sect_off), dwo_file->dwo_name);
11957 return;
11958 }
11959
11960 dwo_unit->dwo_file = dwo_file;
11961 dwo_unit->signature = *signature;
11962 dwo_unit->section = section;
11963 dwo_unit->sect_off = sect_off;
11964 dwo_unit->length = cu->per_cu->length;
11965
11966 if (dwarf_read_debug)
11967 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11968 sect_offset_str (sect_off),
11969 hex_string (dwo_unit->signature));
11970 }
11971
11972 /* Create the dwo_units for the CUs in a DWO_FILE.
11973 Note: This function processes DWO files only, not DWP files. */
11974
11975 static void
11976 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11977 struct dwo_file &dwo_file, dwarf2_section_info &section,
11978 htab_t &cus_htab)
11979 {
11980 struct objfile *objfile = dwarf2_per_objfile->objfile;
11981 const gdb_byte *info_ptr, *end_ptr;
11982
11983 dwarf2_read_section (objfile, &section);
11984 info_ptr = section.buffer;
11985
11986 if (info_ptr == NULL)
11987 return;
11988
11989 if (dwarf_read_debug)
11990 {
11991 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11992 get_section_name (&section),
11993 get_section_file_name (&section));
11994 }
11995
11996 end_ptr = info_ptr + section.size;
11997 while (info_ptr < end_ptr)
11998 {
11999 struct dwarf2_per_cu_data per_cu;
12000 struct create_dwo_cu_data create_dwo_cu_data;
12001 struct dwo_unit *dwo_unit;
12002 void **slot;
12003 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
12004
12005 memset (&create_dwo_cu_data.dwo_unit, 0,
12006 sizeof (create_dwo_cu_data.dwo_unit));
12007 memset (&per_cu, 0, sizeof (per_cu));
12008 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
12009 per_cu.is_debug_types = 0;
12010 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12011 per_cu.section = &section;
12012 create_dwo_cu_data.dwo_file = &dwo_file;
12013
12014 init_cutu_and_read_dies_no_follow (
12015 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12016 info_ptr += per_cu.length;
12017
12018 // If the unit could not be parsed, skip it.
12019 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12020 continue;
12021
12022 if (cus_htab == NULL)
12023 cus_htab = allocate_dwo_unit_table (objfile);
12024
12025 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12026 *dwo_unit = create_dwo_cu_data.dwo_unit;
12027 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12028 gdb_assert (slot != NULL);
12029 if (*slot != NULL)
12030 {
12031 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12032 sect_offset dup_sect_off = dup_cu->sect_off;
12033
12034 complaint (_("debug cu entry at offset %s is duplicate to"
12035 " the entry at offset %s, signature %s"),
12036 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
12037 hex_string (dwo_unit->signature));
12038 }
12039 *slot = (void *)dwo_unit;
12040 }
12041 }
12042
12043 /* DWP file .debug_{cu,tu}_index section format:
12044 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12045
12046 DWP Version 1:
12047
12048 Both index sections have the same format, and serve to map a 64-bit
12049 signature to a set of section numbers. Each section begins with a header,
12050 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12051 indexes, and a pool of 32-bit section numbers. The index sections will be
12052 aligned at 8-byte boundaries in the file.
12053
12054 The index section header consists of:
12055
12056 V, 32 bit version number
12057 -, 32 bits unused
12058 N, 32 bit number of compilation units or type units in the index
12059 M, 32 bit number of slots in the hash table
12060
12061 Numbers are recorded using the byte order of the application binary.
12062
12063 The hash table begins at offset 16 in the section, and consists of an array
12064 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12065 order of the application binary). Unused slots in the hash table are 0.
12066 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12067
12068 The parallel table begins immediately after the hash table
12069 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12070 array of 32-bit indexes (using the byte order of the application binary),
12071 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12072 table contains a 32-bit index into the pool of section numbers. For unused
12073 hash table slots, the corresponding entry in the parallel table will be 0.
12074
12075 The pool of section numbers begins immediately following the hash table
12076 (at offset 16 + 12 * M from the beginning of the section). The pool of
12077 section numbers consists of an array of 32-bit words (using the byte order
12078 of the application binary). Each item in the array is indexed starting
12079 from 0. The hash table entry provides the index of the first section
12080 number in the set. Additional section numbers in the set follow, and the
12081 set is terminated by a 0 entry (section number 0 is not used in ELF).
12082
12083 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12084 section must be the first entry in the set, and the .debug_abbrev.dwo must
12085 be the second entry. Other members of the set may follow in any order.
12086
12087 ---
12088
12089 DWP Version 2:
12090
12091 DWP Version 2 combines all the .debug_info, etc. sections into one,
12092 and the entries in the index tables are now offsets into these sections.
12093 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12094 section.
12095
12096 Index Section Contents:
12097 Header
12098 Hash Table of Signatures dwp_hash_table.hash_table
12099 Parallel Table of Indices dwp_hash_table.unit_table
12100 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12101 Table of Section Sizes dwp_hash_table.v2.sizes
12102
12103 The index section header consists of:
12104
12105 V, 32 bit version number
12106 L, 32 bit number of columns in the table of section offsets
12107 N, 32 bit number of compilation units or type units in the index
12108 M, 32 bit number of slots in the hash table
12109
12110 Numbers are recorded using the byte order of the application binary.
12111
12112 The hash table has the same format as version 1.
12113 The parallel table of indices has the same format as version 1,
12114 except that the entries are origin-1 indices into the table of sections
12115 offsets and the table of section sizes.
12116
12117 The table of offsets begins immediately following the parallel table
12118 (at offset 16 + 12 * M from the beginning of the section). The table is
12119 a two-dimensional array of 32-bit words (using the byte order of the
12120 application binary), with L columns and N+1 rows, in row-major order.
12121 Each row in the array is indexed starting from 0. The first row provides
12122 a key to the remaining rows: each column in this row provides an identifier
12123 for a debug section, and the offsets in the same column of subsequent rows
12124 refer to that section. The section identifiers are:
12125
12126 DW_SECT_INFO 1 .debug_info.dwo
12127 DW_SECT_TYPES 2 .debug_types.dwo
12128 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12129 DW_SECT_LINE 4 .debug_line.dwo
12130 DW_SECT_LOC 5 .debug_loc.dwo
12131 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12132 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12133 DW_SECT_MACRO 8 .debug_macro.dwo
12134
12135 The offsets provided by the CU and TU index sections are the base offsets
12136 for the contributions made by each CU or TU to the corresponding section
12137 in the package file. Each CU and TU header contains an abbrev_offset
12138 field, used to find the abbreviations table for that CU or TU within the
12139 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12140 be interpreted as relative to the base offset given in the index section.
12141 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12142 should be interpreted as relative to the base offset for .debug_line.dwo,
12143 and offsets into other debug sections obtained from DWARF attributes should
12144 also be interpreted as relative to the corresponding base offset.
12145
12146 The table of sizes begins immediately following the table of offsets.
12147 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12148 with L columns and N rows, in row-major order. Each row in the array is
12149 indexed starting from 1 (row 0 is shared by the two tables).
12150
12151 ---
12152
12153 Hash table lookup is handled the same in version 1 and 2:
12154
12155 We assume that N and M will not exceed 2^32 - 1.
12156 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12157
12158 Given a 64-bit compilation unit signature or a type signature S, an entry
12159 in the hash table is located as follows:
12160
12161 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12162 the low-order k bits all set to 1.
12163
12164 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12165
12166 3) If the hash table entry at index H matches the signature, use that
12167 entry. If the hash table entry at index H is unused (all zeroes),
12168 terminate the search: the signature is not present in the table.
12169
12170 4) Let H = (H + H') modulo M. Repeat at Step 3.
12171
12172 Because M > N and H' and M are relatively prime, the search is guaranteed
12173 to stop at an unused slot or find the match. */
12174
12175 /* Create a hash table to map DWO IDs to their CU/TU entry in
12176 .debug_{info,types}.dwo in DWP_FILE.
12177 Returns NULL if there isn't one.
12178 Note: This function processes DWP files only, not DWO files. */
12179
12180 static struct dwp_hash_table *
12181 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12182 struct dwp_file *dwp_file, int is_debug_types)
12183 {
12184 struct objfile *objfile = dwarf2_per_objfile->objfile;
12185 bfd *dbfd = dwp_file->dbfd.get ();
12186 const gdb_byte *index_ptr, *index_end;
12187 struct dwarf2_section_info *index;
12188 uint32_t version, nr_columns, nr_units, nr_slots;
12189 struct dwp_hash_table *htab;
12190
12191 if (is_debug_types)
12192 index = &dwp_file->sections.tu_index;
12193 else
12194 index = &dwp_file->sections.cu_index;
12195
12196 if (dwarf2_section_empty_p (index))
12197 return NULL;
12198 dwarf2_read_section (objfile, index);
12199
12200 index_ptr = index->buffer;
12201 index_end = index_ptr + index->size;
12202
12203 version = read_4_bytes (dbfd, index_ptr);
12204 index_ptr += 4;
12205 if (version == 2)
12206 nr_columns = read_4_bytes (dbfd, index_ptr);
12207 else
12208 nr_columns = 0;
12209 index_ptr += 4;
12210 nr_units = read_4_bytes (dbfd, index_ptr);
12211 index_ptr += 4;
12212 nr_slots = read_4_bytes (dbfd, index_ptr);
12213 index_ptr += 4;
12214
12215 if (version != 1 && version != 2)
12216 {
12217 error (_("Dwarf Error: unsupported DWP file version (%s)"
12218 " [in module %s]"),
12219 pulongest (version), dwp_file->name);
12220 }
12221 if (nr_slots != (nr_slots & -nr_slots))
12222 {
12223 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12224 " is not power of 2 [in module %s]"),
12225 pulongest (nr_slots), dwp_file->name);
12226 }
12227
12228 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12229 htab->version = version;
12230 htab->nr_columns = nr_columns;
12231 htab->nr_units = nr_units;
12232 htab->nr_slots = nr_slots;
12233 htab->hash_table = index_ptr;
12234 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12235
12236 /* Exit early if the table is empty. */
12237 if (nr_slots == 0 || nr_units == 0
12238 || (version == 2 && nr_columns == 0))
12239 {
12240 /* All must be zero. */
12241 if (nr_slots != 0 || nr_units != 0
12242 || (version == 2 && nr_columns != 0))
12243 {
12244 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
12245 " all zero [in modules %s]"),
12246 dwp_file->name);
12247 }
12248 return htab;
12249 }
12250
12251 if (version == 1)
12252 {
12253 htab->section_pool.v1.indices =
12254 htab->unit_table + sizeof (uint32_t) * nr_slots;
12255 /* It's harder to decide whether the section is too small in v1.
12256 V1 is deprecated anyway so we punt. */
12257 }
12258 else
12259 {
12260 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12261 int *ids = htab->section_pool.v2.section_ids;
12262 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
12263 /* Reverse map for error checking. */
12264 int ids_seen[DW_SECT_MAX + 1];
12265 int i;
12266
12267 if (nr_columns < 2)
12268 {
12269 error (_("Dwarf Error: bad DWP hash table, too few columns"
12270 " in section table [in module %s]"),
12271 dwp_file->name);
12272 }
12273 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12274 {
12275 error (_("Dwarf Error: bad DWP hash table, too many columns"
12276 " in section table [in module %s]"),
12277 dwp_file->name);
12278 }
12279 memset (ids, 255, sizeof_ids);
12280 memset (ids_seen, 255, sizeof (ids_seen));
12281 for (i = 0; i < nr_columns; ++i)
12282 {
12283 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12284
12285 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12286 {
12287 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12288 " in section table [in module %s]"),
12289 id, dwp_file->name);
12290 }
12291 if (ids_seen[id] != -1)
12292 {
12293 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12294 " id %d in section table [in module %s]"),
12295 id, dwp_file->name);
12296 }
12297 ids_seen[id] = i;
12298 ids[i] = id;
12299 }
12300 /* Must have exactly one info or types section. */
12301 if (((ids_seen[DW_SECT_INFO] != -1)
12302 + (ids_seen[DW_SECT_TYPES] != -1))
12303 != 1)
12304 {
12305 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12306 " DWO info/types section [in module %s]"),
12307 dwp_file->name);
12308 }
12309 /* Must have an abbrev section. */
12310 if (ids_seen[DW_SECT_ABBREV] == -1)
12311 {
12312 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12313 " section [in module %s]"),
12314 dwp_file->name);
12315 }
12316 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12317 htab->section_pool.v2.sizes =
12318 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12319 * nr_units * nr_columns);
12320 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12321 * nr_units * nr_columns))
12322 > index_end)
12323 {
12324 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12325 " [in module %s]"),
12326 dwp_file->name);
12327 }
12328 }
12329
12330 return htab;
12331 }
12332
12333 /* Update SECTIONS with the data from SECTP.
12334
12335 This function is like the other "locate" section routines that are
12336 passed to bfd_map_over_sections, but in this context the sections to
12337 read comes from the DWP V1 hash table, not the full ELF section table.
12338
12339 The result is non-zero for success, or zero if an error was found. */
12340
12341 static int
12342 locate_v1_virtual_dwo_sections (asection *sectp,
12343 struct virtual_v1_dwo_sections *sections)
12344 {
12345 const struct dwop_section_names *names = &dwop_section_names;
12346
12347 if (section_is_p (sectp->name, &names->abbrev_dwo))
12348 {
12349 /* There can be only one. */
12350 if (sections->abbrev.s.section != NULL)
12351 return 0;
12352 sections->abbrev.s.section = sectp;
12353 sections->abbrev.size = bfd_section_size (sectp);
12354 }
12355 else if (section_is_p (sectp->name, &names->info_dwo)
12356 || section_is_p (sectp->name, &names->types_dwo))
12357 {
12358 /* There can be only one. */
12359 if (sections->info_or_types.s.section != NULL)
12360 return 0;
12361 sections->info_or_types.s.section = sectp;
12362 sections->info_or_types.size = bfd_section_size (sectp);
12363 }
12364 else if (section_is_p (sectp->name, &names->line_dwo))
12365 {
12366 /* There can be only one. */
12367 if (sections->line.s.section != NULL)
12368 return 0;
12369 sections->line.s.section = sectp;
12370 sections->line.size = bfd_section_size (sectp);
12371 }
12372 else if (section_is_p (sectp->name, &names->loc_dwo))
12373 {
12374 /* There can be only one. */
12375 if (sections->loc.s.section != NULL)
12376 return 0;
12377 sections->loc.s.section = sectp;
12378 sections->loc.size = bfd_section_size (sectp);
12379 }
12380 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12381 {
12382 /* There can be only one. */
12383 if (sections->macinfo.s.section != NULL)
12384 return 0;
12385 sections->macinfo.s.section = sectp;
12386 sections->macinfo.size = bfd_section_size (sectp);
12387 }
12388 else if (section_is_p (sectp->name, &names->macro_dwo))
12389 {
12390 /* There can be only one. */
12391 if (sections->macro.s.section != NULL)
12392 return 0;
12393 sections->macro.s.section = sectp;
12394 sections->macro.size = bfd_section_size (sectp);
12395 }
12396 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12397 {
12398 /* There can be only one. */
12399 if (sections->str_offsets.s.section != NULL)
12400 return 0;
12401 sections->str_offsets.s.section = sectp;
12402 sections->str_offsets.size = bfd_section_size (sectp);
12403 }
12404 else
12405 {
12406 /* No other kind of section is valid. */
12407 return 0;
12408 }
12409
12410 return 1;
12411 }
12412
12413 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12414 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12415 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12416 This is for DWP version 1 files. */
12417
12418 static struct dwo_unit *
12419 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12420 struct dwp_file *dwp_file,
12421 uint32_t unit_index,
12422 const char *comp_dir,
12423 ULONGEST signature, int is_debug_types)
12424 {
12425 struct objfile *objfile = dwarf2_per_objfile->objfile;
12426 const struct dwp_hash_table *dwp_htab =
12427 is_debug_types ? dwp_file->tus : dwp_file->cus;
12428 bfd *dbfd = dwp_file->dbfd.get ();
12429 const char *kind = is_debug_types ? "TU" : "CU";
12430 struct dwo_file *dwo_file;
12431 struct dwo_unit *dwo_unit;
12432 struct virtual_v1_dwo_sections sections;
12433 void **dwo_file_slot;
12434 int i;
12435
12436 gdb_assert (dwp_file->version == 1);
12437
12438 if (dwarf_read_debug)
12439 {
12440 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12441 kind,
12442 pulongest (unit_index), hex_string (signature),
12443 dwp_file->name);
12444 }
12445
12446 /* Fetch the sections of this DWO unit.
12447 Put a limit on the number of sections we look for so that bad data
12448 doesn't cause us to loop forever. */
12449
12450 #define MAX_NR_V1_DWO_SECTIONS \
12451 (1 /* .debug_info or .debug_types */ \
12452 + 1 /* .debug_abbrev */ \
12453 + 1 /* .debug_line */ \
12454 + 1 /* .debug_loc */ \
12455 + 1 /* .debug_str_offsets */ \
12456 + 1 /* .debug_macro or .debug_macinfo */ \
12457 + 1 /* trailing zero */)
12458
12459 memset (&sections, 0, sizeof (sections));
12460
12461 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12462 {
12463 asection *sectp;
12464 uint32_t section_nr =
12465 read_4_bytes (dbfd,
12466 dwp_htab->section_pool.v1.indices
12467 + (unit_index + i) * sizeof (uint32_t));
12468
12469 if (section_nr == 0)
12470 break;
12471 if (section_nr >= dwp_file->num_sections)
12472 {
12473 error (_("Dwarf Error: bad DWP hash table, section number too large"
12474 " [in module %s]"),
12475 dwp_file->name);
12476 }
12477
12478 sectp = dwp_file->elf_sections[section_nr];
12479 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12480 {
12481 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12482 " [in module %s]"),
12483 dwp_file->name);
12484 }
12485 }
12486
12487 if (i < 2
12488 || dwarf2_section_empty_p (&sections.info_or_types)
12489 || dwarf2_section_empty_p (&sections.abbrev))
12490 {
12491 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12492 " [in module %s]"),
12493 dwp_file->name);
12494 }
12495 if (i == MAX_NR_V1_DWO_SECTIONS)
12496 {
12497 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12498 " [in module %s]"),
12499 dwp_file->name);
12500 }
12501
12502 /* It's easier for the rest of the code if we fake a struct dwo_file and
12503 have dwo_unit "live" in that. At least for now.
12504
12505 The DWP file can be made up of a random collection of CUs and TUs.
12506 However, for each CU + set of TUs that came from the same original DWO
12507 file, we can combine them back into a virtual DWO file to save space
12508 (fewer struct dwo_file objects to allocate). Remember that for really
12509 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12510
12511 std::string virtual_dwo_name =
12512 string_printf ("virtual-dwo/%d-%d-%d-%d",
12513 get_section_id (&sections.abbrev),
12514 get_section_id (&sections.line),
12515 get_section_id (&sections.loc),
12516 get_section_id (&sections.str_offsets));
12517 /* Can we use an existing virtual DWO file? */
12518 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12519 virtual_dwo_name.c_str (),
12520 comp_dir);
12521 /* Create one if necessary. */
12522 if (*dwo_file_slot == NULL)
12523 {
12524 if (dwarf_read_debug)
12525 {
12526 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12527 virtual_dwo_name.c_str ());
12528 }
12529 dwo_file = new struct dwo_file;
12530 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12531 virtual_dwo_name);
12532 dwo_file->comp_dir = comp_dir;
12533 dwo_file->sections.abbrev = sections.abbrev;
12534 dwo_file->sections.line = sections.line;
12535 dwo_file->sections.loc = sections.loc;
12536 dwo_file->sections.macinfo = sections.macinfo;
12537 dwo_file->sections.macro = sections.macro;
12538 dwo_file->sections.str_offsets = sections.str_offsets;
12539 /* The "str" section is global to the entire DWP file. */
12540 dwo_file->sections.str = dwp_file->sections.str;
12541 /* The info or types section is assigned below to dwo_unit,
12542 there's no need to record it in dwo_file.
12543 Also, we can't simply record type sections in dwo_file because
12544 we record a pointer into the vector in dwo_unit. As we collect more
12545 types we'll grow the vector and eventually have to reallocate space
12546 for it, invalidating all copies of pointers into the previous
12547 contents. */
12548 *dwo_file_slot = dwo_file;
12549 }
12550 else
12551 {
12552 if (dwarf_read_debug)
12553 {
12554 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12555 virtual_dwo_name.c_str ());
12556 }
12557 dwo_file = (struct dwo_file *) *dwo_file_slot;
12558 }
12559
12560 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12561 dwo_unit->dwo_file = dwo_file;
12562 dwo_unit->signature = signature;
12563 dwo_unit->section =
12564 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12565 *dwo_unit->section = sections.info_or_types;
12566 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12567
12568 return dwo_unit;
12569 }
12570
12571 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12572 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12573 piece within that section used by a TU/CU, return a virtual section
12574 of just that piece. */
12575
12576 static struct dwarf2_section_info
12577 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12578 struct dwarf2_section_info *section,
12579 bfd_size_type offset, bfd_size_type size)
12580 {
12581 struct dwarf2_section_info result;
12582 asection *sectp;
12583
12584 gdb_assert (section != NULL);
12585 gdb_assert (!section->is_virtual);
12586
12587 memset (&result, 0, sizeof (result));
12588 result.s.containing_section = section;
12589 result.is_virtual = true;
12590
12591 if (size == 0)
12592 return result;
12593
12594 sectp = get_section_bfd_section (section);
12595
12596 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12597 bounds of the real section. This is a pretty-rare event, so just
12598 flag an error (easier) instead of a warning and trying to cope. */
12599 if (sectp == NULL
12600 || offset + size > bfd_section_size (sectp))
12601 {
12602 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12603 " in section %s [in module %s]"),
12604 sectp ? bfd_section_name (sectp) : "<unknown>",
12605 objfile_name (dwarf2_per_objfile->objfile));
12606 }
12607
12608 result.virtual_offset = offset;
12609 result.size = size;
12610 return result;
12611 }
12612
12613 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12614 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12615 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12616 This is for DWP version 2 files. */
12617
12618 static struct dwo_unit *
12619 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12620 struct dwp_file *dwp_file,
12621 uint32_t unit_index,
12622 const char *comp_dir,
12623 ULONGEST signature, int is_debug_types)
12624 {
12625 struct objfile *objfile = dwarf2_per_objfile->objfile;
12626 const struct dwp_hash_table *dwp_htab =
12627 is_debug_types ? dwp_file->tus : dwp_file->cus;
12628 bfd *dbfd = dwp_file->dbfd.get ();
12629 const char *kind = is_debug_types ? "TU" : "CU";
12630 struct dwo_file *dwo_file;
12631 struct dwo_unit *dwo_unit;
12632 struct virtual_v2_dwo_sections sections;
12633 void **dwo_file_slot;
12634 int i;
12635
12636 gdb_assert (dwp_file->version == 2);
12637
12638 if (dwarf_read_debug)
12639 {
12640 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12641 kind,
12642 pulongest (unit_index), hex_string (signature),
12643 dwp_file->name);
12644 }
12645
12646 /* Fetch the section offsets of this DWO unit. */
12647
12648 memset (&sections, 0, sizeof (sections));
12649
12650 for (i = 0; i < dwp_htab->nr_columns; ++i)
12651 {
12652 uint32_t offset = read_4_bytes (dbfd,
12653 dwp_htab->section_pool.v2.offsets
12654 + (((unit_index - 1) * dwp_htab->nr_columns
12655 + i)
12656 * sizeof (uint32_t)));
12657 uint32_t size = read_4_bytes (dbfd,
12658 dwp_htab->section_pool.v2.sizes
12659 + (((unit_index - 1) * dwp_htab->nr_columns
12660 + i)
12661 * sizeof (uint32_t)));
12662
12663 switch (dwp_htab->section_pool.v2.section_ids[i])
12664 {
12665 case DW_SECT_INFO:
12666 case DW_SECT_TYPES:
12667 sections.info_or_types_offset = offset;
12668 sections.info_or_types_size = size;
12669 break;
12670 case DW_SECT_ABBREV:
12671 sections.abbrev_offset = offset;
12672 sections.abbrev_size = size;
12673 break;
12674 case DW_SECT_LINE:
12675 sections.line_offset = offset;
12676 sections.line_size = size;
12677 break;
12678 case DW_SECT_LOC:
12679 sections.loc_offset = offset;
12680 sections.loc_size = size;
12681 break;
12682 case DW_SECT_STR_OFFSETS:
12683 sections.str_offsets_offset = offset;
12684 sections.str_offsets_size = size;
12685 break;
12686 case DW_SECT_MACINFO:
12687 sections.macinfo_offset = offset;
12688 sections.macinfo_size = size;
12689 break;
12690 case DW_SECT_MACRO:
12691 sections.macro_offset = offset;
12692 sections.macro_size = size;
12693 break;
12694 }
12695 }
12696
12697 /* It's easier for the rest of the code if we fake a struct dwo_file and
12698 have dwo_unit "live" in that. At least for now.
12699
12700 The DWP file can be made up of a random collection of CUs and TUs.
12701 However, for each CU + set of TUs that came from the same original DWO
12702 file, we can combine them back into a virtual DWO file to save space
12703 (fewer struct dwo_file objects to allocate). Remember that for really
12704 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12705
12706 std::string virtual_dwo_name =
12707 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12708 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12709 (long) (sections.line_size ? sections.line_offset : 0),
12710 (long) (sections.loc_size ? sections.loc_offset : 0),
12711 (long) (sections.str_offsets_size
12712 ? sections.str_offsets_offset : 0));
12713 /* Can we use an existing virtual DWO file? */
12714 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12715 virtual_dwo_name.c_str (),
12716 comp_dir);
12717 /* Create one if necessary. */
12718 if (*dwo_file_slot == NULL)
12719 {
12720 if (dwarf_read_debug)
12721 {
12722 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12723 virtual_dwo_name.c_str ());
12724 }
12725 dwo_file = new struct dwo_file;
12726 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12727 virtual_dwo_name);
12728 dwo_file->comp_dir = comp_dir;
12729 dwo_file->sections.abbrev =
12730 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12731 sections.abbrev_offset, sections.abbrev_size);
12732 dwo_file->sections.line =
12733 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12734 sections.line_offset, sections.line_size);
12735 dwo_file->sections.loc =
12736 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12737 sections.loc_offset, sections.loc_size);
12738 dwo_file->sections.macinfo =
12739 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12740 sections.macinfo_offset, sections.macinfo_size);
12741 dwo_file->sections.macro =
12742 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12743 sections.macro_offset, sections.macro_size);
12744 dwo_file->sections.str_offsets =
12745 create_dwp_v2_section (dwarf2_per_objfile,
12746 &dwp_file->sections.str_offsets,
12747 sections.str_offsets_offset,
12748 sections.str_offsets_size);
12749 /* The "str" section is global to the entire DWP file. */
12750 dwo_file->sections.str = dwp_file->sections.str;
12751 /* The info or types section is assigned below to dwo_unit,
12752 there's no need to record it in dwo_file.
12753 Also, we can't simply record type sections in dwo_file because
12754 we record a pointer into the vector in dwo_unit. As we collect more
12755 types we'll grow the vector and eventually have to reallocate space
12756 for it, invalidating all copies of pointers into the previous
12757 contents. */
12758 *dwo_file_slot = dwo_file;
12759 }
12760 else
12761 {
12762 if (dwarf_read_debug)
12763 {
12764 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12765 virtual_dwo_name.c_str ());
12766 }
12767 dwo_file = (struct dwo_file *) *dwo_file_slot;
12768 }
12769
12770 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12771 dwo_unit->dwo_file = dwo_file;
12772 dwo_unit->signature = signature;
12773 dwo_unit->section =
12774 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12775 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12776 is_debug_types
12777 ? &dwp_file->sections.types
12778 : &dwp_file->sections.info,
12779 sections.info_or_types_offset,
12780 sections.info_or_types_size);
12781 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12782
12783 return dwo_unit;
12784 }
12785
12786 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12787 Returns NULL if the signature isn't found. */
12788
12789 static struct dwo_unit *
12790 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12791 struct dwp_file *dwp_file, const char *comp_dir,
12792 ULONGEST signature, int is_debug_types)
12793 {
12794 const struct dwp_hash_table *dwp_htab =
12795 is_debug_types ? dwp_file->tus : dwp_file->cus;
12796 bfd *dbfd = dwp_file->dbfd.get ();
12797 uint32_t mask = dwp_htab->nr_slots - 1;
12798 uint32_t hash = signature & mask;
12799 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12800 unsigned int i;
12801 void **slot;
12802 struct dwo_unit find_dwo_cu;
12803
12804 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12805 find_dwo_cu.signature = signature;
12806 slot = htab_find_slot (is_debug_types
12807 ? dwp_file->loaded_tus
12808 : dwp_file->loaded_cus,
12809 &find_dwo_cu, INSERT);
12810
12811 if (*slot != NULL)
12812 return (struct dwo_unit *) *slot;
12813
12814 /* Use a for loop so that we don't loop forever on bad debug info. */
12815 for (i = 0; i < dwp_htab->nr_slots; ++i)
12816 {
12817 ULONGEST signature_in_table;
12818
12819 signature_in_table =
12820 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12821 if (signature_in_table == signature)
12822 {
12823 uint32_t unit_index =
12824 read_4_bytes (dbfd,
12825 dwp_htab->unit_table + hash * sizeof (uint32_t));
12826
12827 if (dwp_file->version == 1)
12828 {
12829 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12830 dwp_file, unit_index,
12831 comp_dir, signature,
12832 is_debug_types);
12833 }
12834 else
12835 {
12836 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12837 dwp_file, unit_index,
12838 comp_dir, signature,
12839 is_debug_types);
12840 }
12841 return (struct dwo_unit *) *slot;
12842 }
12843 if (signature_in_table == 0)
12844 return NULL;
12845 hash = (hash + hash2) & mask;
12846 }
12847
12848 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12849 " [in module %s]"),
12850 dwp_file->name);
12851 }
12852
12853 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12854 Open the file specified by FILE_NAME and hand it off to BFD for
12855 preliminary analysis. Return a newly initialized bfd *, which
12856 includes a canonicalized copy of FILE_NAME.
12857 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12858 SEARCH_CWD is true if the current directory is to be searched.
12859 It will be searched before debug-file-directory.
12860 If successful, the file is added to the bfd include table of the
12861 objfile's bfd (see gdb_bfd_record_inclusion).
12862 If unable to find/open the file, return NULL.
12863 NOTE: This function is derived from symfile_bfd_open. */
12864
12865 static gdb_bfd_ref_ptr
12866 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12867 const char *file_name, int is_dwp, int search_cwd)
12868 {
12869 int desc;
12870 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12871 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12872 to debug_file_directory. */
12873 const char *search_path;
12874 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12875
12876 gdb::unique_xmalloc_ptr<char> search_path_holder;
12877 if (search_cwd)
12878 {
12879 if (*debug_file_directory != '\0')
12880 {
12881 search_path_holder.reset (concat (".", dirname_separator_string,
12882 debug_file_directory,
12883 (char *) NULL));
12884 search_path = search_path_holder.get ();
12885 }
12886 else
12887 search_path = ".";
12888 }
12889 else
12890 search_path = debug_file_directory;
12891
12892 openp_flags flags = OPF_RETURN_REALPATH;
12893 if (is_dwp)
12894 flags |= OPF_SEARCH_IN_PATH;
12895
12896 gdb::unique_xmalloc_ptr<char> absolute_name;
12897 desc = openp (search_path, flags, file_name,
12898 O_RDONLY | O_BINARY, &absolute_name);
12899 if (desc < 0)
12900 return NULL;
12901
12902 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12903 gnutarget, desc));
12904 if (sym_bfd == NULL)
12905 return NULL;
12906 bfd_set_cacheable (sym_bfd.get (), 1);
12907
12908 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12909 return NULL;
12910
12911 /* Success. Record the bfd as having been included by the objfile's bfd.
12912 This is important because things like demangled_names_hash lives in the
12913 objfile's per_bfd space and may have references to things like symbol
12914 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12915 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12916
12917 return sym_bfd;
12918 }
12919
12920 /* Try to open DWO file FILE_NAME.
12921 COMP_DIR is the DW_AT_comp_dir attribute.
12922 The result is the bfd handle of the file.
12923 If there is a problem finding or opening the file, return NULL.
12924 Upon success, the canonicalized path of the file is stored in the bfd,
12925 same as symfile_bfd_open. */
12926
12927 static gdb_bfd_ref_ptr
12928 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12929 const char *file_name, const char *comp_dir)
12930 {
12931 if (IS_ABSOLUTE_PATH (file_name))
12932 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12933 0 /*is_dwp*/, 0 /*search_cwd*/);
12934
12935 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12936
12937 if (comp_dir != NULL)
12938 {
12939 gdb::unique_xmalloc_ptr<char> path_to_try
12940 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12941
12942 /* NOTE: If comp_dir is a relative path, this will also try the
12943 search path, which seems useful. */
12944 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12945 path_to_try.get (),
12946 0 /*is_dwp*/,
12947 1 /*search_cwd*/));
12948 if (abfd != NULL)
12949 return abfd;
12950 }
12951
12952 /* That didn't work, try debug-file-directory, which, despite its name,
12953 is a list of paths. */
12954
12955 if (*debug_file_directory == '\0')
12956 return NULL;
12957
12958 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12959 0 /*is_dwp*/, 1 /*search_cwd*/);
12960 }
12961
12962 /* This function is mapped across the sections and remembers the offset and
12963 size of each of the DWO debugging sections we are interested in. */
12964
12965 static void
12966 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12967 {
12968 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12969 const struct dwop_section_names *names = &dwop_section_names;
12970
12971 if (section_is_p (sectp->name, &names->abbrev_dwo))
12972 {
12973 dwo_sections->abbrev.s.section = sectp;
12974 dwo_sections->abbrev.size = bfd_section_size (sectp);
12975 }
12976 else if (section_is_p (sectp->name, &names->info_dwo))
12977 {
12978 dwo_sections->info.s.section = sectp;
12979 dwo_sections->info.size = bfd_section_size (sectp);
12980 }
12981 else if (section_is_p (sectp->name, &names->line_dwo))
12982 {
12983 dwo_sections->line.s.section = sectp;
12984 dwo_sections->line.size = bfd_section_size (sectp);
12985 }
12986 else if (section_is_p (sectp->name, &names->loc_dwo))
12987 {
12988 dwo_sections->loc.s.section = sectp;
12989 dwo_sections->loc.size = bfd_section_size (sectp);
12990 }
12991 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12992 {
12993 dwo_sections->macinfo.s.section = sectp;
12994 dwo_sections->macinfo.size = bfd_section_size (sectp);
12995 }
12996 else if (section_is_p (sectp->name, &names->macro_dwo))
12997 {
12998 dwo_sections->macro.s.section = sectp;
12999 dwo_sections->macro.size = bfd_section_size (sectp);
13000 }
13001 else if (section_is_p (sectp->name, &names->str_dwo))
13002 {
13003 dwo_sections->str.s.section = sectp;
13004 dwo_sections->str.size = bfd_section_size (sectp);
13005 }
13006 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13007 {
13008 dwo_sections->str_offsets.s.section = sectp;
13009 dwo_sections->str_offsets.size = bfd_section_size (sectp);
13010 }
13011 else if (section_is_p (sectp->name, &names->types_dwo))
13012 {
13013 struct dwarf2_section_info type_section;
13014
13015 memset (&type_section, 0, sizeof (type_section));
13016 type_section.s.section = sectp;
13017 type_section.size = bfd_section_size (sectp);
13018 dwo_sections->types.push_back (type_section);
13019 }
13020 }
13021
13022 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
13023 by PER_CU. This is for the non-DWP case.
13024 The result is NULL if DWO_NAME can't be found. */
13025
13026 static struct dwo_file *
13027 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13028 const char *dwo_name, const char *comp_dir)
13029 {
13030 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13031
13032 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
13033 if (dbfd == NULL)
13034 {
13035 if (dwarf_read_debug)
13036 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13037 return NULL;
13038 }
13039
13040 dwo_file_up dwo_file (new struct dwo_file);
13041 dwo_file->dwo_name = dwo_name;
13042 dwo_file->comp_dir = comp_dir;
13043 dwo_file->dbfd = std::move (dbfd);
13044
13045 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
13046 &dwo_file->sections);
13047
13048 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13049 dwo_file->cus);
13050
13051 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
13052 dwo_file->sections.types, dwo_file->tus);
13053
13054 if (dwarf_read_debug)
13055 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13056
13057 return dwo_file.release ();
13058 }
13059
13060 /* This function is mapped across the sections and remembers the offset and
13061 size of each of the DWP debugging sections common to version 1 and 2 that
13062 we are interested in. */
13063
13064 static void
13065 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13066 void *dwp_file_ptr)
13067 {
13068 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13069 const struct dwop_section_names *names = &dwop_section_names;
13070 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13071
13072 /* Record the ELF section number for later lookup: this is what the
13073 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13074 gdb_assert (elf_section_nr < dwp_file->num_sections);
13075 dwp_file->elf_sections[elf_section_nr] = sectp;
13076
13077 /* Look for specific sections that we need. */
13078 if (section_is_p (sectp->name, &names->str_dwo))
13079 {
13080 dwp_file->sections.str.s.section = sectp;
13081 dwp_file->sections.str.size = bfd_section_size (sectp);
13082 }
13083 else if (section_is_p (sectp->name, &names->cu_index))
13084 {
13085 dwp_file->sections.cu_index.s.section = sectp;
13086 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
13087 }
13088 else if (section_is_p (sectp->name, &names->tu_index))
13089 {
13090 dwp_file->sections.tu_index.s.section = sectp;
13091 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
13092 }
13093 }
13094
13095 /* This function is mapped across the sections and remembers the offset and
13096 size of each of the DWP version 2 debugging sections that we are interested
13097 in. This is split into a separate function because we don't know if we
13098 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13099
13100 static void
13101 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13102 {
13103 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13104 const struct dwop_section_names *names = &dwop_section_names;
13105 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13106
13107 /* Record the ELF section number for later lookup: this is what the
13108 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13109 gdb_assert (elf_section_nr < dwp_file->num_sections);
13110 dwp_file->elf_sections[elf_section_nr] = sectp;
13111
13112 /* Look for specific sections that we need. */
13113 if (section_is_p (sectp->name, &names->abbrev_dwo))
13114 {
13115 dwp_file->sections.abbrev.s.section = sectp;
13116 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13117 }
13118 else if (section_is_p (sectp->name, &names->info_dwo))
13119 {
13120 dwp_file->sections.info.s.section = sectp;
13121 dwp_file->sections.info.size = bfd_section_size (sectp);
13122 }
13123 else if (section_is_p (sectp->name, &names->line_dwo))
13124 {
13125 dwp_file->sections.line.s.section = sectp;
13126 dwp_file->sections.line.size = bfd_section_size (sectp);
13127 }
13128 else if (section_is_p (sectp->name, &names->loc_dwo))
13129 {
13130 dwp_file->sections.loc.s.section = sectp;
13131 dwp_file->sections.loc.size = bfd_section_size (sectp);
13132 }
13133 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13134 {
13135 dwp_file->sections.macinfo.s.section = sectp;
13136 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
13137 }
13138 else if (section_is_p (sectp->name, &names->macro_dwo))
13139 {
13140 dwp_file->sections.macro.s.section = sectp;
13141 dwp_file->sections.macro.size = bfd_section_size (sectp);
13142 }
13143 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13144 {
13145 dwp_file->sections.str_offsets.s.section = sectp;
13146 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13147 }
13148 else if (section_is_p (sectp->name, &names->types_dwo))
13149 {
13150 dwp_file->sections.types.s.section = sectp;
13151 dwp_file->sections.types.size = bfd_section_size (sectp);
13152 }
13153 }
13154
13155 /* Hash function for dwp_file loaded CUs/TUs. */
13156
13157 static hashval_t
13158 hash_dwp_loaded_cutus (const void *item)
13159 {
13160 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13161
13162 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13163 return dwo_unit->signature;
13164 }
13165
13166 /* Equality function for dwp_file loaded CUs/TUs. */
13167
13168 static int
13169 eq_dwp_loaded_cutus (const void *a, const void *b)
13170 {
13171 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13172 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13173
13174 return dua->signature == dub->signature;
13175 }
13176
13177 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13178
13179 static htab_t
13180 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13181 {
13182 return htab_create_alloc_ex (3,
13183 hash_dwp_loaded_cutus,
13184 eq_dwp_loaded_cutus,
13185 NULL,
13186 &objfile->objfile_obstack,
13187 hashtab_obstack_allocate,
13188 dummy_obstack_deallocate);
13189 }
13190
13191 /* Try to open DWP file FILE_NAME.
13192 The result is the bfd handle of the file.
13193 If there is a problem finding or opening the file, return NULL.
13194 Upon success, the canonicalized path of the file is stored in the bfd,
13195 same as symfile_bfd_open. */
13196
13197 static gdb_bfd_ref_ptr
13198 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13199 const char *file_name)
13200 {
13201 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13202 1 /*is_dwp*/,
13203 1 /*search_cwd*/));
13204 if (abfd != NULL)
13205 return abfd;
13206
13207 /* Work around upstream bug 15652.
13208 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13209 [Whether that's a "bug" is debatable, but it is getting in our way.]
13210 We have no real idea where the dwp file is, because gdb's realpath-ing
13211 of the executable's path may have discarded the needed info.
13212 [IWBN if the dwp file name was recorded in the executable, akin to
13213 .gnu_debuglink, but that doesn't exist yet.]
13214 Strip the directory from FILE_NAME and search again. */
13215 if (*debug_file_directory != '\0')
13216 {
13217 /* Don't implicitly search the current directory here.
13218 If the user wants to search "." to handle this case,
13219 it must be added to debug-file-directory. */
13220 return try_open_dwop_file (dwarf2_per_objfile,
13221 lbasename (file_name), 1 /*is_dwp*/,
13222 0 /*search_cwd*/);
13223 }
13224
13225 return NULL;
13226 }
13227
13228 /* Initialize the use of the DWP file for the current objfile.
13229 By convention the name of the DWP file is ${objfile}.dwp.
13230 The result is NULL if it can't be found. */
13231
13232 static std::unique_ptr<struct dwp_file>
13233 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13234 {
13235 struct objfile *objfile = dwarf2_per_objfile->objfile;
13236
13237 /* Try to find first .dwp for the binary file before any symbolic links
13238 resolving. */
13239
13240 /* If the objfile is a debug file, find the name of the real binary
13241 file and get the name of dwp file from there. */
13242 std::string dwp_name;
13243 if (objfile->separate_debug_objfile_backlink != NULL)
13244 {
13245 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13246 const char *backlink_basename = lbasename (backlink->original_name);
13247
13248 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13249 }
13250 else
13251 dwp_name = objfile->original_name;
13252
13253 dwp_name += ".dwp";
13254
13255 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13256 if (dbfd == NULL
13257 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13258 {
13259 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13260 dwp_name = objfile_name (objfile);
13261 dwp_name += ".dwp";
13262 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13263 }
13264
13265 if (dbfd == NULL)
13266 {
13267 if (dwarf_read_debug)
13268 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13269 return std::unique_ptr<dwp_file> ();
13270 }
13271
13272 const char *name = bfd_get_filename (dbfd.get ());
13273 std::unique_ptr<struct dwp_file> dwp_file
13274 (new struct dwp_file (name, std::move (dbfd)));
13275
13276 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13277 dwp_file->elf_sections =
13278 OBSTACK_CALLOC (&objfile->objfile_obstack,
13279 dwp_file->num_sections, asection *);
13280
13281 bfd_map_over_sections (dwp_file->dbfd.get (),
13282 dwarf2_locate_common_dwp_sections,
13283 dwp_file.get ());
13284
13285 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13286 0);
13287
13288 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13289 1);
13290
13291 /* The DWP file version is stored in the hash table. Oh well. */
13292 if (dwp_file->cus && dwp_file->tus
13293 && dwp_file->cus->version != dwp_file->tus->version)
13294 {
13295 /* Technically speaking, we should try to limp along, but this is
13296 pretty bizarre. We use pulongest here because that's the established
13297 portability solution (e.g, we cannot use %u for uint32_t). */
13298 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13299 " TU version %s [in DWP file %s]"),
13300 pulongest (dwp_file->cus->version),
13301 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13302 }
13303
13304 if (dwp_file->cus)
13305 dwp_file->version = dwp_file->cus->version;
13306 else if (dwp_file->tus)
13307 dwp_file->version = dwp_file->tus->version;
13308 else
13309 dwp_file->version = 2;
13310
13311 if (dwp_file->version == 2)
13312 bfd_map_over_sections (dwp_file->dbfd.get (),
13313 dwarf2_locate_v2_dwp_sections,
13314 dwp_file.get ());
13315
13316 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13317 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13318
13319 if (dwarf_read_debug)
13320 {
13321 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13322 fprintf_unfiltered (gdb_stdlog,
13323 " %s CUs, %s TUs\n",
13324 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13325 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13326 }
13327
13328 return dwp_file;
13329 }
13330
13331 /* Wrapper around open_and_init_dwp_file, only open it once. */
13332
13333 static struct dwp_file *
13334 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13335 {
13336 if (! dwarf2_per_objfile->dwp_checked)
13337 {
13338 dwarf2_per_objfile->dwp_file
13339 = open_and_init_dwp_file (dwarf2_per_objfile);
13340 dwarf2_per_objfile->dwp_checked = 1;
13341 }
13342 return dwarf2_per_objfile->dwp_file.get ();
13343 }
13344
13345 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13346 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13347 or in the DWP file for the objfile, referenced by THIS_UNIT.
13348 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13349 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13350
13351 This is called, for example, when wanting to read a variable with a
13352 complex location. Therefore we don't want to do file i/o for every call.
13353 Therefore we don't want to look for a DWO file on every call.
13354 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13355 then we check if we've already seen DWO_NAME, and only THEN do we check
13356 for a DWO file.
13357
13358 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13359 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13360
13361 static struct dwo_unit *
13362 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13363 const char *dwo_name, const char *comp_dir,
13364 ULONGEST signature, int is_debug_types)
13365 {
13366 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13367 struct objfile *objfile = dwarf2_per_objfile->objfile;
13368 const char *kind = is_debug_types ? "TU" : "CU";
13369 void **dwo_file_slot;
13370 struct dwo_file *dwo_file;
13371 struct dwp_file *dwp_file;
13372
13373 /* First see if there's a DWP file.
13374 If we have a DWP file but didn't find the DWO inside it, don't
13375 look for the original DWO file. It makes gdb behave differently
13376 depending on whether one is debugging in the build tree. */
13377
13378 dwp_file = get_dwp_file (dwarf2_per_objfile);
13379 if (dwp_file != NULL)
13380 {
13381 const struct dwp_hash_table *dwp_htab =
13382 is_debug_types ? dwp_file->tus : dwp_file->cus;
13383
13384 if (dwp_htab != NULL)
13385 {
13386 struct dwo_unit *dwo_cutu =
13387 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13388 signature, is_debug_types);
13389
13390 if (dwo_cutu != NULL)
13391 {
13392 if (dwarf_read_debug)
13393 {
13394 fprintf_unfiltered (gdb_stdlog,
13395 "Virtual DWO %s %s found: @%s\n",
13396 kind, hex_string (signature),
13397 host_address_to_string (dwo_cutu));
13398 }
13399 return dwo_cutu;
13400 }
13401 }
13402 }
13403 else
13404 {
13405 /* No DWP file, look for the DWO file. */
13406
13407 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13408 dwo_name, comp_dir);
13409 if (*dwo_file_slot == NULL)
13410 {
13411 /* Read in the file and build a table of the CUs/TUs it contains. */
13412 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13413 }
13414 /* NOTE: This will be NULL if unable to open the file. */
13415 dwo_file = (struct dwo_file *) *dwo_file_slot;
13416
13417 if (dwo_file != NULL)
13418 {
13419 struct dwo_unit *dwo_cutu = NULL;
13420
13421 if (is_debug_types && dwo_file->tus)
13422 {
13423 struct dwo_unit find_dwo_cutu;
13424
13425 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13426 find_dwo_cutu.signature = signature;
13427 dwo_cutu
13428 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13429 }
13430 else if (!is_debug_types && dwo_file->cus)
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 = (struct dwo_unit *)htab_find (dwo_file->cus,
13437 &find_dwo_cutu);
13438 }
13439
13440 if (dwo_cutu != NULL)
13441 {
13442 if (dwarf_read_debug)
13443 {
13444 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13445 kind, dwo_name, hex_string (signature),
13446 host_address_to_string (dwo_cutu));
13447 }
13448 return dwo_cutu;
13449 }
13450 }
13451 }
13452
13453 /* We didn't find it. This could mean a dwo_id mismatch, or
13454 someone deleted the DWO/DWP file, or the search path isn't set up
13455 correctly to find the file. */
13456
13457 if (dwarf_read_debug)
13458 {
13459 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13460 kind, dwo_name, hex_string (signature));
13461 }
13462
13463 /* This is a warning and not a complaint because it can be caused by
13464 pilot error (e.g., user accidentally deleting the DWO). */
13465 {
13466 /* Print the name of the DWP file if we looked there, helps the user
13467 better diagnose the problem. */
13468 std::string dwp_text;
13469
13470 if (dwp_file != NULL)
13471 dwp_text = string_printf (" [in DWP file %s]",
13472 lbasename (dwp_file->name));
13473
13474 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13475 " [in module %s]"),
13476 kind, dwo_name, hex_string (signature),
13477 dwp_text.c_str (),
13478 this_unit->is_debug_types ? "TU" : "CU",
13479 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13480 }
13481 return NULL;
13482 }
13483
13484 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13485 See lookup_dwo_cutu_unit for details. */
13486
13487 static struct dwo_unit *
13488 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13489 const char *dwo_name, const char *comp_dir,
13490 ULONGEST signature)
13491 {
13492 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13493 }
13494
13495 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13496 See lookup_dwo_cutu_unit for details. */
13497
13498 static struct dwo_unit *
13499 lookup_dwo_type_unit (struct signatured_type *this_tu,
13500 const char *dwo_name, const char *comp_dir)
13501 {
13502 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13503 }
13504
13505 /* Traversal function for queue_and_load_all_dwo_tus. */
13506
13507 static int
13508 queue_and_load_dwo_tu (void **slot, void *info)
13509 {
13510 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13511 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13512 ULONGEST signature = dwo_unit->signature;
13513 struct signatured_type *sig_type =
13514 lookup_dwo_signatured_type (per_cu->cu, signature);
13515
13516 if (sig_type != NULL)
13517 {
13518 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13519
13520 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13521 a real dependency of PER_CU on SIG_TYPE. That is detected later
13522 while processing PER_CU. */
13523 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13524 load_full_type_unit (sig_cu);
13525 per_cu->imported_symtabs_push (sig_cu);
13526 }
13527
13528 return 1;
13529 }
13530
13531 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13532 The DWO may have the only definition of the type, though it may not be
13533 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13534 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13535
13536 static void
13537 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13538 {
13539 struct dwo_unit *dwo_unit;
13540 struct dwo_file *dwo_file;
13541
13542 gdb_assert (!per_cu->is_debug_types);
13543 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13544 gdb_assert (per_cu->cu != NULL);
13545
13546 dwo_unit = per_cu->cu->dwo_unit;
13547 gdb_assert (dwo_unit != NULL);
13548
13549 dwo_file = dwo_unit->dwo_file;
13550 if (dwo_file->tus != NULL)
13551 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13552 }
13553
13554 /* Read in various DIEs. */
13555
13556 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13557 Inherit only the children of the DW_AT_abstract_origin DIE not being
13558 already referenced by DW_AT_abstract_origin from the children of the
13559 current DIE. */
13560
13561 static void
13562 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13563 {
13564 struct die_info *child_die;
13565 sect_offset *offsetp;
13566 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13567 struct die_info *origin_die;
13568 /* Iterator of the ORIGIN_DIE children. */
13569 struct die_info *origin_child_die;
13570 struct attribute *attr;
13571 struct dwarf2_cu *origin_cu;
13572 struct pending **origin_previous_list_in_scope;
13573
13574 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13575 if (!attr)
13576 return;
13577
13578 /* Note that following die references may follow to a die in a
13579 different cu. */
13580
13581 origin_cu = cu;
13582 origin_die = follow_die_ref (die, attr, &origin_cu);
13583
13584 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13585 symbols in. */
13586 origin_previous_list_in_scope = origin_cu->list_in_scope;
13587 origin_cu->list_in_scope = cu->list_in_scope;
13588
13589 if (die->tag != origin_die->tag
13590 && !(die->tag == DW_TAG_inlined_subroutine
13591 && origin_die->tag == DW_TAG_subprogram))
13592 complaint (_("DIE %s and its abstract origin %s have different tags"),
13593 sect_offset_str (die->sect_off),
13594 sect_offset_str (origin_die->sect_off));
13595
13596 std::vector<sect_offset> offsets;
13597
13598 for (child_die = die->child;
13599 child_die && child_die->tag;
13600 child_die = sibling_die (child_die))
13601 {
13602 struct die_info *child_origin_die;
13603 struct dwarf2_cu *child_origin_cu;
13604
13605 /* We are trying to process concrete instance entries:
13606 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13607 it's not relevant to our analysis here. i.e. detecting DIEs that are
13608 present in the abstract instance but not referenced in the concrete
13609 one. */
13610 if (child_die->tag == DW_TAG_call_site
13611 || child_die->tag == DW_TAG_GNU_call_site)
13612 continue;
13613
13614 /* For each CHILD_DIE, find the corresponding child of
13615 ORIGIN_DIE. If there is more than one layer of
13616 DW_AT_abstract_origin, follow them all; there shouldn't be,
13617 but GCC versions at least through 4.4 generate this (GCC PR
13618 40573). */
13619 child_origin_die = child_die;
13620 child_origin_cu = cu;
13621 while (1)
13622 {
13623 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13624 child_origin_cu);
13625 if (attr == NULL)
13626 break;
13627 child_origin_die = follow_die_ref (child_origin_die, attr,
13628 &child_origin_cu);
13629 }
13630
13631 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13632 counterpart may exist. */
13633 if (child_origin_die != child_die)
13634 {
13635 if (child_die->tag != child_origin_die->tag
13636 && !(child_die->tag == DW_TAG_inlined_subroutine
13637 && child_origin_die->tag == DW_TAG_subprogram))
13638 complaint (_("Child DIE %s and its abstract origin %s have "
13639 "different tags"),
13640 sect_offset_str (child_die->sect_off),
13641 sect_offset_str (child_origin_die->sect_off));
13642 if (child_origin_die->parent != origin_die)
13643 complaint (_("Child DIE %s and its abstract origin %s have "
13644 "different parents"),
13645 sect_offset_str (child_die->sect_off),
13646 sect_offset_str (child_origin_die->sect_off));
13647 else
13648 offsets.push_back (child_origin_die->sect_off);
13649 }
13650 }
13651 std::sort (offsets.begin (), offsets.end ());
13652 sect_offset *offsets_end = offsets.data () + offsets.size ();
13653 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13654 if (offsetp[-1] == *offsetp)
13655 complaint (_("Multiple children of DIE %s refer "
13656 "to DIE %s as their abstract origin"),
13657 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13658
13659 offsetp = offsets.data ();
13660 origin_child_die = origin_die->child;
13661 while (origin_child_die && origin_child_die->tag)
13662 {
13663 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13664 while (offsetp < offsets_end
13665 && *offsetp < origin_child_die->sect_off)
13666 offsetp++;
13667 if (offsetp >= offsets_end
13668 || *offsetp > origin_child_die->sect_off)
13669 {
13670 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13671 Check whether we're already processing ORIGIN_CHILD_DIE.
13672 This can happen with mutually referenced abstract_origins.
13673 PR 16581. */
13674 if (!origin_child_die->in_process)
13675 process_die (origin_child_die, origin_cu);
13676 }
13677 origin_child_die = sibling_die (origin_child_die);
13678 }
13679 origin_cu->list_in_scope = origin_previous_list_in_scope;
13680
13681 if (cu != origin_cu)
13682 compute_delayed_physnames (origin_cu);
13683 }
13684
13685 static void
13686 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13687 {
13688 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13689 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13690 struct context_stack *newobj;
13691 CORE_ADDR lowpc;
13692 CORE_ADDR highpc;
13693 struct die_info *child_die;
13694 struct attribute *attr, *call_line, *call_file;
13695 const char *name;
13696 CORE_ADDR baseaddr;
13697 struct block *block;
13698 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13699 std::vector<struct symbol *> template_args;
13700 struct template_symbol *templ_func = NULL;
13701
13702 if (inlined_func)
13703 {
13704 /* If we do not have call site information, we can't show the
13705 caller of this inlined function. That's too confusing, so
13706 only use the scope for local variables. */
13707 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13708 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13709 if (call_line == NULL || call_file == NULL)
13710 {
13711 read_lexical_block_scope (die, cu);
13712 return;
13713 }
13714 }
13715
13716 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
13717
13718 name = dwarf2_name (die, cu);
13719
13720 /* Ignore functions with missing or empty names. These are actually
13721 illegal according to the DWARF standard. */
13722 if (name == NULL)
13723 {
13724 complaint (_("missing name for subprogram DIE at %s"),
13725 sect_offset_str (die->sect_off));
13726 return;
13727 }
13728
13729 /* Ignore functions with missing or invalid low and high pc attributes. */
13730 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13731 <= PC_BOUNDS_INVALID)
13732 {
13733 attr = dwarf2_attr (die, DW_AT_external, cu);
13734 if (!attr || !DW_UNSND (attr))
13735 complaint (_("cannot get low and high bounds "
13736 "for subprogram DIE at %s"),
13737 sect_offset_str (die->sect_off));
13738 return;
13739 }
13740
13741 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13742 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13743
13744 /* If we have any template arguments, then we must allocate a
13745 different sort of symbol. */
13746 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13747 {
13748 if (child_die->tag == DW_TAG_template_type_param
13749 || child_die->tag == DW_TAG_template_value_param)
13750 {
13751 templ_func = allocate_template_symbol (objfile);
13752 templ_func->subclass = SYMBOL_TEMPLATE;
13753 break;
13754 }
13755 }
13756
13757 newobj = cu->get_builder ()->push_context (0, lowpc);
13758 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13759 (struct symbol *) templ_func);
13760
13761 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13762 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13763 cu->language);
13764
13765 /* If there is a location expression for DW_AT_frame_base, record
13766 it. */
13767 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13768 if (attr != nullptr)
13769 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13770
13771 /* If there is a location for the static link, record it. */
13772 newobj->static_link = NULL;
13773 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13774 if (attr != nullptr)
13775 {
13776 newobj->static_link
13777 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13778 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13779 dwarf2_per_cu_addr_type (cu->per_cu));
13780 }
13781
13782 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13783
13784 if (die->child != NULL)
13785 {
13786 child_die = die->child;
13787 while (child_die && child_die->tag)
13788 {
13789 if (child_die->tag == DW_TAG_template_type_param
13790 || child_die->tag == DW_TAG_template_value_param)
13791 {
13792 struct symbol *arg = new_symbol (child_die, NULL, cu);
13793
13794 if (arg != NULL)
13795 template_args.push_back (arg);
13796 }
13797 else
13798 process_die (child_die, cu);
13799 child_die = sibling_die (child_die);
13800 }
13801 }
13802
13803 inherit_abstract_dies (die, cu);
13804
13805 /* If we have a DW_AT_specification, we might need to import using
13806 directives from the context of the specification DIE. See the
13807 comment in determine_prefix. */
13808 if (cu->language == language_cplus
13809 && dwarf2_attr (die, DW_AT_specification, cu))
13810 {
13811 struct dwarf2_cu *spec_cu = cu;
13812 struct die_info *spec_die = die_specification (die, &spec_cu);
13813
13814 while (spec_die)
13815 {
13816 child_die = spec_die->child;
13817 while (child_die && child_die->tag)
13818 {
13819 if (child_die->tag == DW_TAG_imported_module)
13820 process_die (child_die, spec_cu);
13821 child_die = sibling_die (child_die);
13822 }
13823
13824 /* In some cases, GCC generates specification DIEs that
13825 themselves contain DW_AT_specification attributes. */
13826 spec_die = die_specification (spec_die, &spec_cu);
13827 }
13828 }
13829
13830 struct context_stack cstk = cu->get_builder ()->pop_context ();
13831 /* Make a block for the local symbols within. */
13832 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13833 cstk.static_link, lowpc, highpc);
13834
13835 /* For C++, set the block's scope. */
13836 if ((cu->language == language_cplus
13837 || cu->language == language_fortran
13838 || cu->language == language_d
13839 || cu->language == language_rust)
13840 && cu->processing_has_namespace_info)
13841 block_set_scope (block, determine_prefix (die, cu),
13842 &objfile->objfile_obstack);
13843
13844 /* If we have address ranges, record them. */
13845 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13846
13847 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13848
13849 /* Attach template arguments to function. */
13850 if (!template_args.empty ())
13851 {
13852 gdb_assert (templ_func != NULL);
13853
13854 templ_func->n_template_arguments = template_args.size ();
13855 templ_func->template_arguments
13856 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13857 templ_func->n_template_arguments);
13858 memcpy (templ_func->template_arguments,
13859 template_args.data (),
13860 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13861
13862 /* Make sure that the symtab is set on the new symbols. Even
13863 though they don't appear in this symtab directly, other parts
13864 of gdb assume that symbols do, and this is reasonably
13865 true. */
13866 for (symbol *sym : template_args)
13867 symbol_set_symtab (sym, symbol_symtab (templ_func));
13868 }
13869
13870 /* In C++, we can have functions nested inside functions (e.g., when
13871 a function declares a class that has methods). This means that
13872 when we finish processing a function scope, we may need to go
13873 back to building a containing block's symbol lists. */
13874 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13875 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13876
13877 /* If we've finished processing a top-level function, subsequent
13878 symbols go in the file symbol list. */
13879 if (cu->get_builder ()->outermost_context_p ())
13880 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13881 }
13882
13883 /* Process all the DIES contained within a lexical block scope. Start
13884 a new scope, process the dies, and then close the scope. */
13885
13886 static void
13887 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13888 {
13889 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13890 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13891 CORE_ADDR lowpc, highpc;
13892 struct die_info *child_die;
13893 CORE_ADDR baseaddr;
13894
13895 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
13896
13897 /* Ignore blocks with missing or invalid low and high pc attributes. */
13898 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13899 as multiple lexical blocks? Handling children in a sane way would
13900 be nasty. Might be easier to properly extend generic blocks to
13901 describe ranges. */
13902 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13903 {
13904 case PC_BOUNDS_NOT_PRESENT:
13905 /* DW_TAG_lexical_block has no attributes, process its children as if
13906 there was no wrapping by that DW_TAG_lexical_block.
13907 GCC does no longer produces such DWARF since GCC r224161. */
13908 for (child_die = die->child;
13909 child_die != NULL && child_die->tag;
13910 child_die = sibling_die (child_die))
13911 process_die (child_die, cu);
13912 return;
13913 case PC_BOUNDS_INVALID:
13914 return;
13915 }
13916 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13917 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13918
13919 cu->get_builder ()->push_context (0, lowpc);
13920 if (die->child != NULL)
13921 {
13922 child_die = die->child;
13923 while (child_die && child_die->tag)
13924 {
13925 process_die (child_die, cu);
13926 child_die = sibling_die (child_die);
13927 }
13928 }
13929 inherit_abstract_dies (die, cu);
13930 struct context_stack cstk = cu->get_builder ()->pop_context ();
13931
13932 if (*cu->get_builder ()->get_local_symbols () != NULL
13933 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13934 {
13935 struct block *block
13936 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13937 cstk.start_addr, highpc);
13938
13939 /* Note that recording ranges after traversing children, as we
13940 do here, means that recording a parent's ranges entails
13941 walking across all its children's ranges as they appear in
13942 the address map, which is quadratic behavior.
13943
13944 It would be nicer to record the parent's ranges before
13945 traversing its children, simply overriding whatever you find
13946 there. But since we don't even decide whether to create a
13947 block until after we've traversed its children, that's hard
13948 to do. */
13949 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13950 }
13951 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13952 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13953 }
13954
13955 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13956
13957 static void
13958 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13959 {
13960 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13961 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13962 CORE_ADDR pc, baseaddr;
13963 struct attribute *attr;
13964 struct call_site *call_site, call_site_local;
13965 void **slot;
13966 int nparams;
13967 struct die_info *child_die;
13968
13969 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
13970
13971 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13972 if (attr == NULL)
13973 {
13974 /* This was a pre-DWARF-5 GNU extension alias
13975 for DW_AT_call_return_pc. */
13976 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13977 }
13978 if (!attr)
13979 {
13980 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13981 "DIE %s [in module %s]"),
13982 sect_offset_str (die->sect_off), objfile_name (objfile));
13983 return;
13984 }
13985 pc = attr_value_as_address (attr) + baseaddr;
13986 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13987
13988 if (cu->call_site_htab == NULL)
13989 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13990 NULL, &objfile->objfile_obstack,
13991 hashtab_obstack_allocate, NULL);
13992 call_site_local.pc = pc;
13993 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13994 if (*slot != NULL)
13995 {
13996 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13997 "DIE %s [in module %s]"),
13998 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13999 objfile_name (objfile));
14000 return;
14001 }
14002
14003 /* Count parameters at the caller. */
14004
14005 nparams = 0;
14006 for (child_die = die->child; child_die && child_die->tag;
14007 child_die = sibling_die (child_die))
14008 {
14009 if (child_die->tag != DW_TAG_call_site_parameter
14010 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14011 {
14012 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
14013 "DW_TAG_call_site child DIE %s [in module %s]"),
14014 child_die->tag, sect_offset_str (child_die->sect_off),
14015 objfile_name (objfile));
14016 continue;
14017 }
14018
14019 nparams++;
14020 }
14021
14022 call_site
14023 = ((struct call_site *)
14024 obstack_alloc (&objfile->objfile_obstack,
14025 sizeof (*call_site)
14026 + (sizeof (*call_site->parameter) * (nparams - 1))));
14027 *slot = call_site;
14028 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14029 call_site->pc = pc;
14030
14031 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14032 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14033 {
14034 struct die_info *func_die;
14035
14036 /* Skip also over DW_TAG_inlined_subroutine. */
14037 for (func_die = die->parent;
14038 func_die && func_die->tag != DW_TAG_subprogram
14039 && func_die->tag != DW_TAG_subroutine_type;
14040 func_die = func_die->parent);
14041
14042 /* DW_AT_call_all_calls is a superset
14043 of DW_AT_call_all_tail_calls. */
14044 if (func_die
14045 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14046 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14047 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14048 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14049 {
14050 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14051 not complete. But keep CALL_SITE for look ups via call_site_htab,
14052 both the initial caller containing the real return address PC and
14053 the final callee containing the current PC of a chain of tail
14054 calls do not need to have the tail call list complete. But any
14055 function candidate for a virtual tail call frame searched via
14056 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14057 determined unambiguously. */
14058 }
14059 else
14060 {
14061 struct type *func_type = NULL;
14062
14063 if (func_die)
14064 func_type = get_die_type (func_die, cu);
14065 if (func_type != NULL)
14066 {
14067 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14068
14069 /* Enlist this call site to the function. */
14070 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14071 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14072 }
14073 else
14074 complaint (_("Cannot find function owning DW_TAG_call_site "
14075 "DIE %s [in module %s]"),
14076 sect_offset_str (die->sect_off), objfile_name (objfile));
14077 }
14078 }
14079
14080 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14081 if (attr == NULL)
14082 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14083 if (attr == NULL)
14084 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14085 if (attr == NULL)
14086 {
14087 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14088 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14089 }
14090 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14091 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14092 /* Keep NULL DWARF_BLOCK. */;
14093 else if (attr_form_is_block (attr))
14094 {
14095 struct dwarf2_locexpr_baton *dlbaton;
14096
14097 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14098 dlbaton->data = DW_BLOCK (attr)->data;
14099 dlbaton->size = DW_BLOCK (attr)->size;
14100 dlbaton->per_cu = cu->per_cu;
14101
14102 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14103 }
14104 else if (attr_form_is_ref (attr))
14105 {
14106 struct dwarf2_cu *target_cu = cu;
14107 struct die_info *target_die;
14108
14109 target_die = follow_die_ref (die, attr, &target_cu);
14110 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14111 if (die_is_declaration (target_die, target_cu))
14112 {
14113 const char *target_physname;
14114
14115 /* Prefer the mangled name; otherwise compute the demangled one. */
14116 target_physname = dw2_linkage_name (target_die, target_cu);
14117 if (target_physname == NULL)
14118 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14119 if (target_physname == NULL)
14120 complaint (_("DW_AT_call_target target DIE has invalid "
14121 "physname, for referencing DIE %s [in module %s]"),
14122 sect_offset_str (die->sect_off), objfile_name (objfile));
14123 else
14124 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14125 }
14126 else
14127 {
14128 CORE_ADDR lowpc;
14129
14130 /* DW_AT_entry_pc should be preferred. */
14131 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14132 <= PC_BOUNDS_INVALID)
14133 complaint (_("DW_AT_call_target target DIE has invalid "
14134 "low pc, for referencing DIE %s [in module %s]"),
14135 sect_offset_str (die->sect_off), objfile_name (objfile));
14136 else
14137 {
14138 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14139 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14140 }
14141 }
14142 }
14143 else
14144 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14145 "block nor reference, for DIE %s [in module %s]"),
14146 sect_offset_str (die->sect_off), objfile_name (objfile));
14147
14148 call_site->per_cu = cu->per_cu;
14149
14150 for (child_die = die->child;
14151 child_die && child_die->tag;
14152 child_die = sibling_die (child_die))
14153 {
14154 struct call_site_parameter *parameter;
14155 struct attribute *loc, *origin;
14156
14157 if (child_die->tag != DW_TAG_call_site_parameter
14158 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14159 {
14160 /* Already printed the complaint above. */
14161 continue;
14162 }
14163
14164 gdb_assert (call_site->parameter_count < nparams);
14165 parameter = &call_site->parameter[call_site->parameter_count];
14166
14167 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14168 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14169 register is contained in DW_AT_call_value. */
14170
14171 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14172 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14173 if (origin == NULL)
14174 {
14175 /* This was a pre-DWARF-5 GNU extension alias
14176 for DW_AT_call_parameter. */
14177 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14178 }
14179 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14180 {
14181 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14182
14183 sect_offset sect_off
14184 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14185 if (!offset_in_cu_p (&cu->header, sect_off))
14186 {
14187 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14188 binding can be done only inside one CU. Such referenced DIE
14189 therefore cannot be even moved to DW_TAG_partial_unit. */
14190 complaint (_("DW_AT_call_parameter offset is not in CU for "
14191 "DW_TAG_call_site child DIE %s [in module %s]"),
14192 sect_offset_str (child_die->sect_off),
14193 objfile_name (objfile));
14194 continue;
14195 }
14196 parameter->u.param_cu_off
14197 = (cu_offset) (sect_off - cu->header.sect_off);
14198 }
14199 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14200 {
14201 complaint (_("No DW_FORM_block* DW_AT_location for "
14202 "DW_TAG_call_site child DIE %s [in module %s]"),
14203 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14204 continue;
14205 }
14206 else
14207 {
14208 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14209 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14210 if (parameter->u.dwarf_reg != -1)
14211 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14212 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14213 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14214 &parameter->u.fb_offset))
14215 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14216 else
14217 {
14218 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14219 "for DW_FORM_block* DW_AT_location is supported for "
14220 "DW_TAG_call_site child DIE %s "
14221 "[in module %s]"),
14222 sect_offset_str (child_die->sect_off),
14223 objfile_name (objfile));
14224 continue;
14225 }
14226 }
14227
14228 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14229 if (attr == NULL)
14230 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14231 if (!attr_form_is_block (attr))
14232 {
14233 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14234 "DW_TAG_call_site child DIE %s [in module %s]"),
14235 sect_offset_str (child_die->sect_off),
14236 objfile_name (objfile));
14237 continue;
14238 }
14239 parameter->value = DW_BLOCK (attr)->data;
14240 parameter->value_size = DW_BLOCK (attr)->size;
14241
14242 /* Parameters are not pre-cleared by memset above. */
14243 parameter->data_value = NULL;
14244 parameter->data_value_size = 0;
14245 call_site->parameter_count++;
14246
14247 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14248 if (attr == NULL)
14249 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14250 if (attr != nullptr)
14251 {
14252 if (!attr_form_is_block (attr))
14253 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14254 "DW_TAG_call_site child DIE %s [in module %s]"),
14255 sect_offset_str (child_die->sect_off),
14256 objfile_name (objfile));
14257 else
14258 {
14259 parameter->data_value = DW_BLOCK (attr)->data;
14260 parameter->data_value_size = DW_BLOCK (attr)->size;
14261 }
14262 }
14263 }
14264 }
14265
14266 /* Helper function for read_variable. If DIE represents a virtual
14267 table, then return the type of the concrete object that is
14268 associated with the virtual table. Otherwise, return NULL. */
14269
14270 static struct type *
14271 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14272 {
14273 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14274 if (attr == NULL)
14275 return NULL;
14276
14277 /* Find the type DIE. */
14278 struct die_info *type_die = NULL;
14279 struct dwarf2_cu *type_cu = cu;
14280
14281 if (attr_form_is_ref (attr))
14282 type_die = follow_die_ref (die, attr, &type_cu);
14283 if (type_die == NULL)
14284 return NULL;
14285
14286 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14287 return NULL;
14288 return die_containing_type (type_die, type_cu);
14289 }
14290
14291 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14292
14293 static void
14294 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14295 {
14296 struct rust_vtable_symbol *storage = NULL;
14297
14298 if (cu->language == language_rust)
14299 {
14300 struct type *containing_type = rust_containing_type (die, cu);
14301
14302 if (containing_type != NULL)
14303 {
14304 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14305
14306 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
14307 initialize_objfile_symbol (storage);
14308 storage->concrete_type = containing_type;
14309 storage->subclass = SYMBOL_RUST_VTABLE;
14310 }
14311 }
14312
14313 struct symbol *res = new_symbol (die, NULL, cu, storage);
14314 struct attribute *abstract_origin
14315 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14316 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14317 if (res == NULL && loc && abstract_origin)
14318 {
14319 /* We have a variable without a name, but with a location and an abstract
14320 origin. This may be a concrete instance of an abstract variable
14321 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14322 later. */
14323 struct dwarf2_cu *origin_cu = cu;
14324 struct die_info *origin_die
14325 = follow_die_ref (die, abstract_origin, &origin_cu);
14326 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
14327 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
14328 }
14329 }
14330
14331 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14332 reading .debug_rnglists.
14333 Callback's type should be:
14334 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14335 Return true if the attributes are present and valid, otherwise,
14336 return false. */
14337
14338 template <typename Callback>
14339 static bool
14340 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14341 Callback &&callback)
14342 {
14343 struct dwarf2_per_objfile *dwarf2_per_objfile
14344 = cu->per_cu->dwarf2_per_objfile;
14345 struct objfile *objfile = dwarf2_per_objfile->objfile;
14346 bfd *obfd = objfile->obfd;
14347 /* Base address selection entry. */
14348 CORE_ADDR base;
14349 int found_base;
14350 const gdb_byte *buffer;
14351 CORE_ADDR baseaddr;
14352 bool overflow = false;
14353
14354 found_base = cu->base_known;
14355 base = cu->base_address;
14356
14357 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14358 if (offset >= dwarf2_per_objfile->rnglists.size)
14359 {
14360 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14361 offset);
14362 return false;
14363 }
14364 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14365
14366 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
14367
14368 while (1)
14369 {
14370 /* Initialize it due to a false compiler warning. */
14371 CORE_ADDR range_beginning = 0, range_end = 0;
14372 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14373 + dwarf2_per_objfile->rnglists.size);
14374 unsigned int bytes_read;
14375
14376 if (buffer == buf_end)
14377 {
14378 overflow = true;
14379 break;
14380 }
14381 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14382 switch (rlet)
14383 {
14384 case DW_RLE_end_of_list:
14385 break;
14386 case DW_RLE_base_address:
14387 if (buffer + cu->header.addr_size > buf_end)
14388 {
14389 overflow = true;
14390 break;
14391 }
14392 base = read_address (obfd, buffer, cu, &bytes_read);
14393 found_base = 1;
14394 buffer += bytes_read;
14395 break;
14396 case DW_RLE_start_length:
14397 if (buffer + cu->header.addr_size > buf_end)
14398 {
14399 overflow = true;
14400 break;
14401 }
14402 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14403 buffer += bytes_read;
14404 range_end = (range_beginning
14405 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14406 buffer += bytes_read;
14407 if (buffer > buf_end)
14408 {
14409 overflow = true;
14410 break;
14411 }
14412 break;
14413 case DW_RLE_offset_pair:
14414 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14415 buffer += bytes_read;
14416 if (buffer > buf_end)
14417 {
14418 overflow = true;
14419 break;
14420 }
14421 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14422 buffer += bytes_read;
14423 if (buffer > buf_end)
14424 {
14425 overflow = true;
14426 break;
14427 }
14428 break;
14429 case DW_RLE_start_end:
14430 if (buffer + 2 * cu->header.addr_size > buf_end)
14431 {
14432 overflow = true;
14433 break;
14434 }
14435 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14436 buffer += bytes_read;
14437 range_end = read_address (obfd, buffer, cu, &bytes_read);
14438 buffer += bytes_read;
14439 break;
14440 default:
14441 complaint (_("Invalid .debug_rnglists data (no base address)"));
14442 return false;
14443 }
14444 if (rlet == DW_RLE_end_of_list || overflow)
14445 break;
14446 if (rlet == DW_RLE_base_address)
14447 continue;
14448
14449 if (!found_base)
14450 {
14451 /* We have no valid base address for the ranges
14452 data. */
14453 complaint (_("Invalid .debug_rnglists data (no base address)"));
14454 return false;
14455 }
14456
14457 if (range_beginning > range_end)
14458 {
14459 /* Inverted range entries are invalid. */
14460 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14461 return false;
14462 }
14463
14464 /* Empty range entries have no effect. */
14465 if (range_beginning == range_end)
14466 continue;
14467
14468 range_beginning += base;
14469 range_end += base;
14470
14471 /* A not-uncommon case of bad debug info.
14472 Don't pollute the addrmap with bad data. */
14473 if (range_beginning + baseaddr == 0
14474 && !dwarf2_per_objfile->has_section_at_zero)
14475 {
14476 complaint (_(".debug_rnglists entry has start address of zero"
14477 " [in module %s]"), objfile_name (objfile));
14478 continue;
14479 }
14480
14481 callback (range_beginning, range_end);
14482 }
14483
14484 if (overflow)
14485 {
14486 complaint (_("Offset %d is not terminated "
14487 "for DW_AT_ranges attribute"),
14488 offset);
14489 return false;
14490 }
14491
14492 return true;
14493 }
14494
14495 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14496 Callback's type should be:
14497 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14498 Return 1 if the attributes are present and valid, otherwise, return 0. */
14499
14500 template <typename Callback>
14501 static int
14502 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14503 Callback &&callback)
14504 {
14505 struct dwarf2_per_objfile *dwarf2_per_objfile
14506 = cu->per_cu->dwarf2_per_objfile;
14507 struct objfile *objfile = dwarf2_per_objfile->objfile;
14508 struct comp_unit_head *cu_header = &cu->header;
14509 bfd *obfd = objfile->obfd;
14510 unsigned int addr_size = cu_header->addr_size;
14511 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14512 /* Base address selection entry. */
14513 CORE_ADDR base;
14514 int found_base;
14515 unsigned int dummy;
14516 const gdb_byte *buffer;
14517 CORE_ADDR baseaddr;
14518
14519 if (cu_header->version >= 5)
14520 return dwarf2_rnglists_process (offset, cu, callback);
14521
14522 found_base = cu->base_known;
14523 base = cu->base_address;
14524
14525 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14526 if (offset >= dwarf2_per_objfile->ranges.size)
14527 {
14528 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14529 offset);
14530 return 0;
14531 }
14532 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14533
14534 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
14535
14536 while (1)
14537 {
14538 CORE_ADDR range_beginning, range_end;
14539
14540 range_beginning = read_address (obfd, buffer, cu, &dummy);
14541 buffer += addr_size;
14542 range_end = read_address (obfd, buffer, cu, &dummy);
14543 buffer += addr_size;
14544 offset += 2 * addr_size;
14545
14546 /* An end of list marker is a pair of zero addresses. */
14547 if (range_beginning == 0 && range_end == 0)
14548 /* Found the end of list entry. */
14549 break;
14550
14551 /* Each base address selection entry is a pair of 2 values.
14552 The first is the largest possible address, the second is
14553 the base address. Check for a base address here. */
14554 if ((range_beginning & mask) == mask)
14555 {
14556 /* If we found the largest possible address, then we already
14557 have the base address in range_end. */
14558 base = range_end;
14559 found_base = 1;
14560 continue;
14561 }
14562
14563 if (!found_base)
14564 {
14565 /* We have no valid base address for the ranges
14566 data. */
14567 complaint (_("Invalid .debug_ranges data (no base address)"));
14568 return 0;
14569 }
14570
14571 if (range_beginning > range_end)
14572 {
14573 /* Inverted range entries are invalid. */
14574 complaint (_("Invalid .debug_ranges data (inverted range)"));
14575 return 0;
14576 }
14577
14578 /* Empty range entries have no effect. */
14579 if (range_beginning == range_end)
14580 continue;
14581
14582 range_beginning += base;
14583 range_end += base;
14584
14585 /* A not-uncommon case of bad debug info.
14586 Don't pollute the addrmap with bad data. */
14587 if (range_beginning + baseaddr == 0
14588 && !dwarf2_per_objfile->has_section_at_zero)
14589 {
14590 complaint (_(".debug_ranges entry has start address of zero"
14591 " [in module %s]"), objfile_name (objfile));
14592 continue;
14593 }
14594
14595 callback (range_beginning, range_end);
14596 }
14597
14598 return 1;
14599 }
14600
14601 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14602 Return 1 if the attributes are present and valid, otherwise, return 0.
14603 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14604
14605 static int
14606 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14607 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14608 struct partial_symtab *ranges_pst)
14609 {
14610 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14611 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14612 const CORE_ADDR baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
14613 int low_set = 0;
14614 CORE_ADDR low = 0;
14615 CORE_ADDR high = 0;
14616 int retval;
14617
14618 retval = dwarf2_ranges_process (offset, cu,
14619 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14620 {
14621 if (ranges_pst != NULL)
14622 {
14623 CORE_ADDR lowpc;
14624 CORE_ADDR highpc;
14625
14626 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14627 range_beginning + baseaddr)
14628 - baseaddr);
14629 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14630 range_end + baseaddr)
14631 - baseaddr);
14632 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14633 lowpc, highpc - 1, ranges_pst);
14634 }
14635
14636 /* FIXME: This is recording everything as a low-high
14637 segment of consecutive addresses. We should have a
14638 data structure for discontiguous block ranges
14639 instead. */
14640 if (! low_set)
14641 {
14642 low = range_beginning;
14643 high = range_end;
14644 low_set = 1;
14645 }
14646 else
14647 {
14648 if (range_beginning < low)
14649 low = range_beginning;
14650 if (range_end > high)
14651 high = range_end;
14652 }
14653 });
14654 if (!retval)
14655 return 0;
14656
14657 if (! low_set)
14658 /* If the first entry is an end-of-list marker, the range
14659 describes an empty scope, i.e. no instructions. */
14660 return 0;
14661
14662 if (low_return)
14663 *low_return = low;
14664 if (high_return)
14665 *high_return = high;
14666 return 1;
14667 }
14668
14669 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14670 definition for the return value. *LOWPC and *HIGHPC are set iff
14671 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14672
14673 static enum pc_bounds_kind
14674 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14675 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14676 struct partial_symtab *pst)
14677 {
14678 struct dwarf2_per_objfile *dwarf2_per_objfile
14679 = cu->per_cu->dwarf2_per_objfile;
14680 struct attribute *attr;
14681 struct attribute *attr_high;
14682 CORE_ADDR low = 0;
14683 CORE_ADDR high = 0;
14684 enum pc_bounds_kind ret;
14685
14686 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14687 if (attr_high)
14688 {
14689 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14690 if (attr != nullptr)
14691 {
14692 low = attr_value_as_address (attr);
14693 high = attr_value_as_address (attr_high);
14694 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14695 high += low;
14696 }
14697 else
14698 /* Found high w/o low attribute. */
14699 return PC_BOUNDS_INVALID;
14700
14701 /* Found consecutive range of addresses. */
14702 ret = PC_BOUNDS_HIGH_LOW;
14703 }
14704 else
14705 {
14706 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14707 if (attr != NULL)
14708 {
14709 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14710 We take advantage of the fact that DW_AT_ranges does not appear
14711 in DW_TAG_compile_unit of DWO files. */
14712 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14713 unsigned int ranges_offset = (DW_UNSND (attr)
14714 + (need_ranges_base
14715 ? cu->ranges_base
14716 : 0));
14717
14718 /* Value of the DW_AT_ranges attribute is the offset in the
14719 .debug_ranges section. */
14720 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14721 return PC_BOUNDS_INVALID;
14722 /* Found discontinuous range of addresses. */
14723 ret = PC_BOUNDS_RANGES;
14724 }
14725 else
14726 return PC_BOUNDS_NOT_PRESENT;
14727 }
14728
14729 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14730 if (high <= low)
14731 return PC_BOUNDS_INVALID;
14732
14733 /* When using the GNU linker, .gnu.linkonce. sections are used to
14734 eliminate duplicate copies of functions and vtables and such.
14735 The linker will arbitrarily choose one and discard the others.
14736 The AT_*_pc values for such functions refer to local labels in
14737 these sections. If the section from that file was discarded, the
14738 labels are not in the output, so the relocs get a value of 0.
14739 If this is a discarded function, mark the pc bounds as invalid,
14740 so that GDB will ignore it. */
14741 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14742 return PC_BOUNDS_INVALID;
14743
14744 *lowpc = low;
14745 if (highpc)
14746 *highpc = high;
14747 return ret;
14748 }
14749
14750 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14751 its low and high PC addresses. Do nothing if these addresses could not
14752 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14753 and HIGHPC to the high address if greater than HIGHPC. */
14754
14755 static void
14756 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14757 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14758 struct dwarf2_cu *cu)
14759 {
14760 CORE_ADDR low, high;
14761 struct die_info *child = die->child;
14762
14763 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14764 {
14765 *lowpc = std::min (*lowpc, low);
14766 *highpc = std::max (*highpc, high);
14767 }
14768
14769 /* If the language does not allow nested subprograms (either inside
14770 subprograms or lexical blocks), we're done. */
14771 if (cu->language != language_ada)
14772 return;
14773
14774 /* Check all the children of the given DIE. If it contains nested
14775 subprograms, then check their pc bounds. Likewise, we need to
14776 check lexical blocks as well, as they may also contain subprogram
14777 definitions. */
14778 while (child && child->tag)
14779 {
14780 if (child->tag == DW_TAG_subprogram
14781 || child->tag == DW_TAG_lexical_block)
14782 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14783 child = sibling_die (child);
14784 }
14785 }
14786
14787 /* Get the low and high pc's represented by the scope DIE, and store
14788 them in *LOWPC and *HIGHPC. If the correct values can't be
14789 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14790
14791 static void
14792 get_scope_pc_bounds (struct die_info *die,
14793 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14794 struct dwarf2_cu *cu)
14795 {
14796 CORE_ADDR best_low = (CORE_ADDR) -1;
14797 CORE_ADDR best_high = (CORE_ADDR) 0;
14798 CORE_ADDR current_low, current_high;
14799
14800 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14801 >= PC_BOUNDS_RANGES)
14802 {
14803 best_low = current_low;
14804 best_high = current_high;
14805 }
14806 else
14807 {
14808 struct die_info *child = die->child;
14809
14810 while (child && child->tag)
14811 {
14812 switch (child->tag) {
14813 case DW_TAG_subprogram:
14814 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14815 break;
14816 case DW_TAG_namespace:
14817 case DW_TAG_module:
14818 /* FIXME: carlton/2004-01-16: Should we do this for
14819 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14820 that current GCC's always emit the DIEs corresponding
14821 to definitions of methods of classes as children of a
14822 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14823 the DIEs giving the declarations, which could be
14824 anywhere). But I don't see any reason why the
14825 standards says that they have to be there. */
14826 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14827
14828 if (current_low != ((CORE_ADDR) -1))
14829 {
14830 best_low = std::min (best_low, current_low);
14831 best_high = std::max (best_high, current_high);
14832 }
14833 break;
14834 default:
14835 /* Ignore. */
14836 break;
14837 }
14838
14839 child = sibling_die (child);
14840 }
14841 }
14842
14843 *lowpc = best_low;
14844 *highpc = best_high;
14845 }
14846
14847 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14848 in DIE. */
14849
14850 static void
14851 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14852 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14853 {
14854 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14855 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14856 struct attribute *attr;
14857 struct attribute *attr_high;
14858
14859 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14860 if (attr_high)
14861 {
14862 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14863 if (attr != nullptr)
14864 {
14865 CORE_ADDR low = attr_value_as_address (attr);
14866 CORE_ADDR high = attr_value_as_address (attr_high);
14867
14868 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14869 high += low;
14870
14871 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14872 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14873 cu->get_builder ()->record_block_range (block, low, high - 1);
14874 }
14875 }
14876
14877 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14878 if (attr != nullptr)
14879 {
14880 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14881 We take advantage of the fact that DW_AT_ranges does not appear
14882 in DW_TAG_compile_unit of DWO files. */
14883 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14884
14885 /* The value of the DW_AT_ranges attribute is the offset of the
14886 address range list in the .debug_ranges section. */
14887 unsigned long offset = (DW_UNSND (attr)
14888 + (need_ranges_base ? cu->ranges_base : 0));
14889
14890 std::vector<blockrange> blockvec;
14891 dwarf2_ranges_process (offset, cu,
14892 [&] (CORE_ADDR start, CORE_ADDR end)
14893 {
14894 start += baseaddr;
14895 end += baseaddr;
14896 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14897 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14898 cu->get_builder ()->record_block_range (block, start, end - 1);
14899 blockvec.emplace_back (start, end);
14900 });
14901
14902 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14903 }
14904 }
14905
14906 /* Check whether the producer field indicates either of GCC < 4.6, or the
14907 Intel C/C++ compiler, and cache the result in CU. */
14908
14909 static void
14910 check_producer (struct dwarf2_cu *cu)
14911 {
14912 int major, minor;
14913
14914 if (cu->producer == NULL)
14915 {
14916 /* For unknown compilers expect their behavior is DWARF version
14917 compliant.
14918
14919 GCC started to support .debug_types sections by -gdwarf-4 since
14920 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14921 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14922 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14923 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14924 }
14925 else if (producer_is_gcc (cu->producer, &major, &minor))
14926 {
14927 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14928 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14929 }
14930 else if (producer_is_icc (cu->producer, &major, &minor))
14931 {
14932 cu->producer_is_icc = true;
14933 cu->producer_is_icc_lt_14 = major < 14;
14934 }
14935 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14936 cu->producer_is_codewarrior = true;
14937 else
14938 {
14939 /* For other non-GCC compilers, expect their behavior is DWARF version
14940 compliant. */
14941 }
14942
14943 cu->checked_producer = true;
14944 }
14945
14946 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14947 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14948 during 4.6.0 experimental. */
14949
14950 static bool
14951 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14952 {
14953 if (!cu->checked_producer)
14954 check_producer (cu);
14955
14956 return cu->producer_is_gxx_lt_4_6;
14957 }
14958
14959
14960 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14961 with incorrect is_stmt attributes. */
14962
14963 static bool
14964 producer_is_codewarrior (struct dwarf2_cu *cu)
14965 {
14966 if (!cu->checked_producer)
14967 check_producer (cu);
14968
14969 return cu->producer_is_codewarrior;
14970 }
14971
14972 /* Return the default accessibility type if it is not overridden by
14973 DW_AT_accessibility. */
14974
14975 static enum dwarf_access_attribute
14976 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14977 {
14978 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14979 {
14980 /* The default DWARF 2 accessibility for members is public, the default
14981 accessibility for inheritance is private. */
14982
14983 if (die->tag != DW_TAG_inheritance)
14984 return DW_ACCESS_public;
14985 else
14986 return DW_ACCESS_private;
14987 }
14988 else
14989 {
14990 /* DWARF 3+ defines the default accessibility a different way. The same
14991 rules apply now for DW_TAG_inheritance as for the members and it only
14992 depends on the container kind. */
14993
14994 if (die->parent->tag == DW_TAG_class_type)
14995 return DW_ACCESS_private;
14996 else
14997 return DW_ACCESS_public;
14998 }
14999 }
15000
15001 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15002 offset. If the attribute was not found return 0, otherwise return
15003 1. If it was found but could not properly be handled, set *OFFSET
15004 to 0. */
15005
15006 static int
15007 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15008 LONGEST *offset)
15009 {
15010 struct attribute *attr;
15011
15012 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15013 if (attr != NULL)
15014 {
15015 *offset = 0;
15016
15017 /* Note that we do not check for a section offset first here.
15018 This is because DW_AT_data_member_location is new in DWARF 4,
15019 so if we see it, we can assume that a constant form is really
15020 a constant and not a section offset. */
15021 if (attr_form_is_constant (attr))
15022 *offset = dwarf2_get_attr_constant_value (attr, 0);
15023 else if (attr_form_is_section_offset (attr))
15024 dwarf2_complex_location_expr_complaint ();
15025 else if (attr_form_is_block (attr))
15026 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15027 else
15028 dwarf2_complex_location_expr_complaint ();
15029
15030 return 1;
15031 }
15032
15033 return 0;
15034 }
15035
15036 /* Add an aggregate field to the field list. */
15037
15038 static void
15039 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15040 struct dwarf2_cu *cu)
15041 {
15042 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15043 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15044 struct nextfield *new_field;
15045 struct attribute *attr;
15046 struct field *fp;
15047 const char *fieldname = "";
15048
15049 if (die->tag == DW_TAG_inheritance)
15050 {
15051 fip->baseclasses.emplace_back ();
15052 new_field = &fip->baseclasses.back ();
15053 }
15054 else
15055 {
15056 fip->fields.emplace_back ();
15057 new_field = &fip->fields.back ();
15058 }
15059
15060 fip->nfields++;
15061
15062 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15063 if (attr != nullptr)
15064 new_field->accessibility = DW_UNSND (attr);
15065 else
15066 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15067 if (new_field->accessibility != DW_ACCESS_public)
15068 fip->non_public_fields = 1;
15069
15070 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15071 if (attr != nullptr)
15072 new_field->virtuality = DW_UNSND (attr);
15073 else
15074 new_field->virtuality = DW_VIRTUALITY_none;
15075
15076 fp = &new_field->field;
15077
15078 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15079 {
15080 LONGEST offset;
15081
15082 /* Data member other than a C++ static data member. */
15083
15084 /* Get type of field. */
15085 fp->type = die_type (die, cu);
15086
15087 SET_FIELD_BITPOS (*fp, 0);
15088
15089 /* Get bit size of field (zero if none). */
15090 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15091 if (attr != nullptr)
15092 {
15093 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15094 }
15095 else
15096 {
15097 FIELD_BITSIZE (*fp) = 0;
15098 }
15099
15100 /* Get bit offset of field. */
15101 if (handle_data_member_location (die, cu, &offset))
15102 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15103 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15104 if (attr != nullptr)
15105 {
15106 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15107 {
15108 /* For big endian bits, the DW_AT_bit_offset gives the
15109 additional bit offset from the MSB of the containing
15110 anonymous object to the MSB of the field. We don't
15111 have to do anything special since we don't need to
15112 know the size of the anonymous object. */
15113 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15114 }
15115 else
15116 {
15117 /* For little endian bits, compute the bit offset to the
15118 MSB of the anonymous object, subtract off the number of
15119 bits from the MSB of the field to the MSB of the
15120 object, and then subtract off the number of bits of
15121 the field itself. The result is the bit offset of
15122 the LSB of the field. */
15123 int anonymous_size;
15124 int bit_offset = DW_UNSND (attr);
15125
15126 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15127 if (attr != nullptr)
15128 {
15129 /* The size of the anonymous object containing
15130 the bit field is explicit, so use the
15131 indicated size (in bytes). */
15132 anonymous_size = DW_UNSND (attr);
15133 }
15134 else
15135 {
15136 /* The size of the anonymous object containing
15137 the bit field must be inferred from the type
15138 attribute of the data member containing the
15139 bit field. */
15140 anonymous_size = TYPE_LENGTH (fp->type);
15141 }
15142 SET_FIELD_BITPOS (*fp,
15143 (FIELD_BITPOS (*fp)
15144 + anonymous_size * bits_per_byte
15145 - bit_offset - FIELD_BITSIZE (*fp)));
15146 }
15147 }
15148 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15149 if (attr != NULL)
15150 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15151 + dwarf2_get_attr_constant_value (attr, 0)));
15152
15153 /* Get name of field. */
15154 fieldname = dwarf2_name (die, cu);
15155 if (fieldname == NULL)
15156 fieldname = "";
15157
15158 /* The name is already allocated along with this objfile, so we don't
15159 need to duplicate it for the type. */
15160 fp->name = fieldname;
15161
15162 /* Change accessibility for artificial fields (e.g. virtual table
15163 pointer or virtual base class pointer) to private. */
15164 if (dwarf2_attr (die, DW_AT_artificial, cu))
15165 {
15166 FIELD_ARTIFICIAL (*fp) = 1;
15167 new_field->accessibility = DW_ACCESS_private;
15168 fip->non_public_fields = 1;
15169 }
15170 }
15171 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15172 {
15173 /* C++ static member. */
15174
15175 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15176 is a declaration, but all versions of G++ as of this writing
15177 (so through at least 3.2.1) incorrectly generate
15178 DW_TAG_variable tags. */
15179
15180 const char *physname;
15181
15182 /* Get name of field. */
15183 fieldname = dwarf2_name (die, cu);
15184 if (fieldname == NULL)
15185 return;
15186
15187 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15188 if (attr
15189 /* Only create a symbol if this is an external value.
15190 new_symbol checks this and puts the value in the global symbol
15191 table, which we want. If it is not external, new_symbol
15192 will try to put the value in cu->list_in_scope which is wrong. */
15193 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15194 {
15195 /* A static const member, not much different than an enum as far as
15196 we're concerned, except that we can support more types. */
15197 new_symbol (die, NULL, cu);
15198 }
15199
15200 /* Get physical name. */
15201 physname = dwarf2_physname (fieldname, die, cu);
15202
15203 /* The name is already allocated along with this objfile, so we don't
15204 need to duplicate it for the type. */
15205 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15206 FIELD_TYPE (*fp) = die_type (die, cu);
15207 FIELD_NAME (*fp) = fieldname;
15208 }
15209 else if (die->tag == DW_TAG_inheritance)
15210 {
15211 LONGEST offset;
15212
15213 /* C++ base class field. */
15214 if (handle_data_member_location (die, cu, &offset))
15215 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15216 FIELD_BITSIZE (*fp) = 0;
15217 FIELD_TYPE (*fp) = die_type (die, cu);
15218 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
15219 }
15220 else if (die->tag == DW_TAG_variant_part)
15221 {
15222 /* process_structure_scope will treat this DIE as a union. */
15223 process_structure_scope (die, cu);
15224
15225 /* The variant part is relative to the start of the enclosing
15226 structure. */
15227 SET_FIELD_BITPOS (*fp, 0);
15228 fp->type = get_die_type (die, cu);
15229 fp->artificial = 1;
15230 fp->name = "<<variant>>";
15231
15232 /* Normally a DW_TAG_variant_part won't have a size, but our
15233 representation requires one, so set it to the maximum of the
15234 child sizes, being sure to account for the offset at which
15235 each child is seen. */
15236 if (TYPE_LENGTH (fp->type) == 0)
15237 {
15238 unsigned max = 0;
15239 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15240 {
15241 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
15242 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
15243 if (len > max)
15244 max = len;
15245 }
15246 TYPE_LENGTH (fp->type) = max;
15247 }
15248 }
15249 else
15250 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15251 }
15252
15253 /* Can the type given by DIE define another type? */
15254
15255 static bool
15256 type_can_define_types (const struct die_info *die)
15257 {
15258 switch (die->tag)
15259 {
15260 case DW_TAG_typedef:
15261 case DW_TAG_class_type:
15262 case DW_TAG_structure_type:
15263 case DW_TAG_union_type:
15264 case DW_TAG_enumeration_type:
15265 return true;
15266
15267 default:
15268 return false;
15269 }
15270 }
15271
15272 /* Add a type definition defined in the scope of the FIP's class. */
15273
15274 static void
15275 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15276 struct dwarf2_cu *cu)
15277 {
15278 struct decl_field fp;
15279 memset (&fp, 0, sizeof (fp));
15280
15281 gdb_assert (type_can_define_types (die));
15282
15283 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15284 fp.name = dwarf2_name (die, cu);
15285 fp.type = read_type_die (die, cu);
15286
15287 /* Save accessibility. */
15288 enum dwarf_access_attribute accessibility;
15289 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15290 if (attr != NULL)
15291 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15292 else
15293 accessibility = dwarf2_default_access_attribute (die, cu);
15294 switch (accessibility)
15295 {
15296 case DW_ACCESS_public:
15297 /* The assumed value if neither private nor protected. */
15298 break;
15299 case DW_ACCESS_private:
15300 fp.is_private = 1;
15301 break;
15302 case DW_ACCESS_protected:
15303 fp.is_protected = 1;
15304 break;
15305 default:
15306 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15307 }
15308
15309 if (die->tag == DW_TAG_typedef)
15310 fip->typedef_field_list.push_back (fp);
15311 else
15312 fip->nested_types_list.push_back (fp);
15313 }
15314
15315 /* Create the vector of fields, and attach it to the type. */
15316
15317 static void
15318 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15319 struct dwarf2_cu *cu)
15320 {
15321 int nfields = fip->nfields;
15322
15323 /* Record the field count, allocate space for the array of fields,
15324 and create blank accessibility bitfields if necessary. */
15325 TYPE_NFIELDS (type) = nfields;
15326 TYPE_FIELDS (type) = (struct field *)
15327 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15328
15329 if (fip->non_public_fields && cu->language != language_ada)
15330 {
15331 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15332
15333 TYPE_FIELD_PRIVATE_BITS (type) =
15334 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15335 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15336
15337 TYPE_FIELD_PROTECTED_BITS (type) =
15338 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15339 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15340
15341 TYPE_FIELD_IGNORE_BITS (type) =
15342 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15343 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15344 }
15345
15346 /* If the type has baseclasses, allocate and clear a bit vector for
15347 TYPE_FIELD_VIRTUAL_BITS. */
15348 if (!fip->baseclasses.empty () && cu->language != language_ada)
15349 {
15350 int num_bytes = B_BYTES (fip->baseclasses.size ());
15351 unsigned char *pointer;
15352
15353 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15354 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15355 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15356 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15357 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15358 }
15359
15360 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15361 {
15362 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15363
15364 for (int index = 0; index < nfields; ++index)
15365 {
15366 struct nextfield &field = fip->fields[index];
15367
15368 if (field.variant.is_discriminant)
15369 di->discriminant_index = index;
15370 else if (field.variant.default_branch)
15371 di->default_index = index;
15372 else
15373 di->discriminants[index] = field.variant.discriminant_value;
15374 }
15375 }
15376
15377 /* Copy the saved-up fields into the field vector. */
15378 for (int i = 0; i < nfields; ++i)
15379 {
15380 struct nextfield &field
15381 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15382 : fip->fields[i - fip->baseclasses.size ()]);
15383
15384 TYPE_FIELD (type, i) = field.field;
15385 switch (field.accessibility)
15386 {
15387 case DW_ACCESS_private:
15388 if (cu->language != language_ada)
15389 SET_TYPE_FIELD_PRIVATE (type, i);
15390 break;
15391
15392 case DW_ACCESS_protected:
15393 if (cu->language != language_ada)
15394 SET_TYPE_FIELD_PROTECTED (type, i);
15395 break;
15396
15397 case DW_ACCESS_public:
15398 break;
15399
15400 default:
15401 /* Unknown accessibility. Complain and treat it as public. */
15402 {
15403 complaint (_("unsupported accessibility %d"),
15404 field.accessibility);
15405 }
15406 break;
15407 }
15408 if (i < fip->baseclasses.size ())
15409 {
15410 switch (field.virtuality)
15411 {
15412 case DW_VIRTUALITY_virtual:
15413 case DW_VIRTUALITY_pure_virtual:
15414 if (cu->language == language_ada)
15415 error (_("unexpected virtuality in component of Ada type"));
15416 SET_TYPE_FIELD_VIRTUAL (type, i);
15417 break;
15418 }
15419 }
15420 }
15421 }
15422
15423 /* Return true if this member function is a constructor, false
15424 otherwise. */
15425
15426 static int
15427 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15428 {
15429 const char *fieldname;
15430 const char *type_name;
15431 int len;
15432
15433 if (die->parent == NULL)
15434 return 0;
15435
15436 if (die->parent->tag != DW_TAG_structure_type
15437 && die->parent->tag != DW_TAG_union_type
15438 && die->parent->tag != DW_TAG_class_type)
15439 return 0;
15440
15441 fieldname = dwarf2_name (die, cu);
15442 type_name = dwarf2_name (die->parent, cu);
15443 if (fieldname == NULL || type_name == NULL)
15444 return 0;
15445
15446 len = strlen (fieldname);
15447 return (strncmp (fieldname, type_name, len) == 0
15448 && (type_name[len] == '\0' || type_name[len] == '<'));
15449 }
15450
15451 /* Check if the given VALUE is a recognized enum
15452 dwarf_defaulted_attribute constant according to DWARF5 spec,
15453 Table 7.24. */
15454
15455 static bool
15456 is_valid_DW_AT_defaulted (ULONGEST value)
15457 {
15458 switch (value)
15459 {
15460 case DW_DEFAULTED_no:
15461 case DW_DEFAULTED_in_class:
15462 case DW_DEFAULTED_out_of_class:
15463 return true;
15464 }
15465
15466 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15467 return false;
15468 }
15469
15470 /* Add a member function to the proper fieldlist. */
15471
15472 static void
15473 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15474 struct type *type, struct dwarf2_cu *cu)
15475 {
15476 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15477 struct attribute *attr;
15478 int i;
15479 struct fnfieldlist *flp = nullptr;
15480 struct fn_field *fnp;
15481 const char *fieldname;
15482 struct type *this_type;
15483 enum dwarf_access_attribute accessibility;
15484
15485 if (cu->language == language_ada)
15486 error (_("unexpected member function in Ada type"));
15487
15488 /* Get name of member function. */
15489 fieldname = dwarf2_name (die, cu);
15490 if (fieldname == NULL)
15491 return;
15492
15493 /* Look up member function name in fieldlist. */
15494 for (i = 0; i < fip->fnfieldlists.size (); i++)
15495 {
15496 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15497 {
15498 flp = &fip->fnfieldlists[i];
15499 break;
15500 }
15501 }
15502
15503 /* Create a new fnfieldlist if necessary. */
15504 if (flp == nullptr)
15505 {
15506 fip->fnfieldlists.emplace_back ();
15507 flp = &fip->fnfieldlists.back ();
15508 flp->name = fieldname;
15509 i = fip->fnfieldlists.size () - 1;
15510 }
15511
15512 /* Create a new member function field and add it to the vector of
15513 fnfieldlists. */
15514 flp->fnfields.emplace_back ();
15515 fnp = &flp->fnfields.back ();
15516
15517 /* Delay processing of the physname until later. */
15518 if (cu->language == language_cplus)
15519 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15520 die, cu);
15521 else
15522 {
15523 const char *physname = dwarf2_physname (fieldname, die, cu);
15524 fnp->physname = physname ? physname : "";
15525 }
15526
15527 fnp->type = alloc_type (objfile);
15528 this_type = read_type_die (die, cu);
15529 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15530 {
15531 int nparams = TYPE_NFIELDS (this_type);
15532
15533 /* TYPE is the domain of this method, and THIS_TYPE is the type
15534 of the method itself (TYPE_CODE_METHOD). */
15535 smash_to_method_type (fnp->type, type,
15536 TYPE_TARGET_TYPE (this_type),
15537 TYPE_FIELDS (this_type),
15538 TYPE_NFIELDS (this_type),
15539 TYPE_VARARGS (this_type));
15540
15541 /* Handle static member functions.
15542 Dwarf2 has no clean way to discern C++ static and non-static
15543 member functions. G++ helps GDB by marking the first
15544 parameter for non-static member functions (which is the this
15545 pointer) as artificial. We obtain this information from
15546 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15547 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15548 fnp->voffset = VOFFSET_STATIC;
15549 }
15550 else
15551 complaint (_("member function type missing for '%s'"),
15552 dwarf2_full_name (fieldname, die, cu));
15553
15554 /* Get fcontext from DW_AT_containing_type if present. */
15555 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15556 fnp->fcontext = die_containing_type (die, cu);
15557
15558 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15559 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15560
15561 /* Get accessibility. */
15562 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15563 if (attr != nullptr)
15564 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15565 else
15566 accessibility = dwarf2_default_access_attribute (die, cu);
15567 switch (accessibility)
15568 {
15569 case DW_ACCESS_private:
15570 fnp->is_private = 1;
15571 break;
15572 case DW_ACCESS_protected:
15573 fnp->is_protected = 1;
15574 break;
15575 }
15576
15577 /* Check for artificial methods. */
15578 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15579 if (attr && DW_UNSND (attr) != 0)
15580 fnp->is_artificial = 1;
15581
15582 /* Check for defaulted methods. */
15583 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15584 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15585 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15586
15587 /* Check for deleted methods. */
15588 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15589 if (attr != nullptr && DW_UNSND (attr) != 0)
15590 fnp->is_deleted = 1;
15591
15592 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15593
15594 /* Get index in virtual function table if it is a virtual member
15595 function. For older versions of GCC, this is an offset in the
15596 appropriate virtual table, as specified by DW_AT_containing_type.
15597 For everyone else, it is an expression to be evaluated relative
15598 to the object address. */
15599
15600 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15601 if (attr != nullptr)
15602 {
15603 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15604 {
15605 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15606 {
15607 /* Old-style GCC. */
15608 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15609 }
15610 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15611 || (DW_BLOCK (attr)->size > 1
15612 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15613 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15614 {
15615 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15616 if ((fnp->voffset % cu->header.addr_size) != 0)
15617 dwarf2_complex_location_expr_complaint ();
15618 else
15619 fnp->voffset /= cu->header.addr_size;
15620 fnp->voffset += 2;
15621 }
15622 else
15623 dwarf2_complex_location_expr_complaint ();
15624
15625 if (!fnp->fcontext)
15626 {
15627 /* If there is no `this' field and no DW_AT_containing_type,
15628 we cannot actually find a base class context for the
15629 vtable! */
15630 if (TYPE_NFIELDS (this_type) == 0
15631 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15632 {
15633 complaint (_("cannot determine context for virtual member "
15634 "function \"%s\" (offset %s)"),
15635 fieldname, sect_offset_str (die->sect_off));
15636 }
15637 else
15638 {
15639 fnp->fcontext
15640 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15641 }
15642 }
15643 }
15644 else if (attr_form_is_section_offset (attr))
15645 {
15646 dwarf2_complex_location_expr_complaint ();
15647 }
15648 else
15649 {
15650 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15651 fieldname);
15652 }
15653 }
15654 else
15655 {
15656 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15657 if (attr && DW_UNSND (attr))
15658 {
15659 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15660 complaint (_("Member function \"%s\" (offset %s) is virtual "
15661 "but the vtable offset is not specified"),
15662 fieldname, sect_offset_str (die->sect_off));
15663 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15664 TYPE_CPLUS_DYNAMIC (type) = 1;
15665 }
15666 }
15667 }
15668
15669 /* Create the vector of member function fields, and attach it to the type. */
15670
15671 static void
15672 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15673 struct dwarf2_cu *cu)
15674 {
15675 if (cu->language == language_ada)
15676 error (_("unexpected member functions in Ada type"));
15677
15678 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15679 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15680 TYPE_ALLOC (type,
15681 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15682
15683 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15684 {
15685 struct fnfieldlist &nf = fip->fnfieldlists[i];
15686 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15687
15688 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15689 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15690 fn_flp->fn_fields = (struct fn_field *)
15691 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15692
15693 for (int k = 0; k < nf.fnfields.size (); ++k)
15694 fn_flp->fn_fields[k] = nf.fnfields[k];
15695 }
15696
15697 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15698 }
15699
15700 /* Returns non-zero if NAME is the name of a vtable member in CU's
15701 language, zero otherwise. */
15702 static int
15703 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15704 {
15705 static const char vptr[] = "_vptr";
15706
15707 /* Look for the C++ form of the vtable. */
15708 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15709 return 1;
15710
15711 return 0;
15712 }
15713
15714 /* GCC outputs unnamed structures that are really pointers to member
15715 functions, with the ABI-specified layout. If TYPE describes
15716 such a structure, smash it into a member function type.
15717
15718 GCC shouldn't do this; it should just output pointer to member DIEs.
15719 This is GCC PR debug/28767. */
15720
15721 static void
15722 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15723 {
15724 struct type *pfn_type, *self_type, *new_type;
15725
15726 /* Check for a structure with no name and two children. */
15727 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15728 return;
15729
15730 /* Check for __pfn and __delta members. */
15731 if (TYPE_FIELD_NAME (type, 0) == NULL
15732 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15733 || TYPE_FIELD_NAME (type, 1) == NULL
15734 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15735 return;
15736
15737 /* Find the type of the method. */
15738 pfn_type = TYPE_FIELD_TYPE (type, 0);
15739 if (pfn_type == NULL
15740 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15741 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15742 return;
15743
15744 /* Look for the "this" argument. */
15745 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15746 if (TYPE_NFIELDS (pfn_type) == 0
15747 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15748 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15749 return;
15750
15751 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15752 new_type = alloc_type (objfile);
15753 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15754 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15755 TYPE_VARARGS (pfn_type));
15756 smash_to_methodptr_type (type, new_type);
15757 }
15758
15759 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15760 appropriate error checking and issuing complaints if there is a
15761 problem. */
15762
15763 static ULONGEST
15764 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15765 {
15766 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15767
15768 if (attr == nullptr)
15769 return 0;
15770
15771 if (!attr_form_is_constant (attr))
15772 {
15773 complaint (_("DW_AT_alignment must have constant form"
15774 " - DIE at %s [in module %s]"),
15775 sect_offset_str (die->sect_off),
15776 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15777 return 0;
15778 }
15779
15780 ULONGEST align;
15781 if (attr->form == DW_FORM_sdata)
15782 {
15783 LONGEST val = DW_SND (attr);
15784 if (val < 0)
15785 {
15786 complaint (_("DW_AT_alignment value must not be negative"
15787 " - DIE at %s [in module %s]"),
15788 sect_offset_str (die->sect_off),
15789 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15790 return 0;
15791 }
15792 align = val;
15793 }
15794 else
15795 align = DW_UNSND (attr);
15796
15797 if (align == 0)
15798 {
15799 complaint (_("DW_AT_alignment value must not be zero"
15800 " - DIE at %s [in module %s]"),
15801 sect_offset_str (die->sect_off),
15802 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15803 return 0;
15804 }
15805 if ((align & (align - 1)) != 0)
15806 {
15807 complaint (_("DW_AT_alignment value must be a power of 2"
15808 " - DIE at %s [in module %s]"),
15809 sect_offset_str (die->sect_off),
15810 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15811 return 0;
15812 }
15813
15814 return align;
15815 }
15816
15817 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15818 the alignment for TYPE. */
15819
15820 static void
15821 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15822 struct type *type)
15823 {
15824 if (!set_type_align (type, get_alignment (cu, die)))
15825 complaint (_("DW_AT_alignment value too large"
15826 " - DIE at %s [in module %s]"),
15827 sect_offset_str (die->sect_off),
15828 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15829 }
15830
15831 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15832 constant for a type, according to DWARF5 spec, Table 5.5. */
15833
15834 static bool
15835 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15836 {
15837 switch (value)
15838 {
15839 case DW_CC_normal:
15840 case DW_CC_pass_by_reference:
15841 case DW_CC_pass_by_value:
15842 return true;
15843
15844 default:
15845 complaint (_("unrecognized DW_AT_calling_convention value "
15846 "(%s) for a type"), pulongest (value));
15847 return false;
15848 }
15849 }
15850
15851 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15852 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15853 also according to GNU-specific values (see include/dwarf2.h). */
15854
15855 static bool
15856 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15857 {
15858 switch (value)
15859 {
15860 case DW_CC_normal:
15861 case DW_CC_program:
15862 case DW_CC_nocall:
15863 return true;
15864
15865 case DW_CC_GNU_renesas_sh:
15866 case DW_CC_GNU_borland_fastcall_i386:
15867 case DW_CC_GDB_IBM_OpenCL:
15868 return true;
15869
15870 default:
15871 complaint (_("unrecognized DW_AT_calling_convention value "
15872 "(%s) for a subroutine"), pulongest (value));
15873 return false;
15874 }
15875 }
15876
15877 /* Called when we find the DIE that starts a structure or union scope
15878 (definition) to create a type for the structure or union. Fill in
15879 the type's name and general properties; the members will not be
15880 processed until process_structure_scope. A symbol table entry for
15881 the type will also not be done until process_structure_scope (assuming
15882 the type has a name).
15883
15884 NOTE: we need to call these functions regardless of whether or not the
15885 DIE has a DW_AT_name attribute, since it might be an anonymous
15886 structure or union. This gets the type entered into our set of
15887 user defined types. */
15888
15889 static struct type *
15890 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15891 {
15892 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15893 struct type *type;
15894 struct attribute *attr;
15895 const char *name;
15896
15897 /* If the definition of this type lives in .debug_types, read that type.
15898 Don't follow DW_AT_specification though, that will take us back up
15899 the chain and we want to go down. */
15900 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15901 if (attr != nullptr)
15902 {
15903 type = get_DW_AT_signature_type (die, attr, cu);
15904
15905 /* The type's CU may not be the same as CU.
15906 Ensure TYPE is recorded with CU in die_type_hash. */
15907 return set_die_type (die, type, cu);
15908 }
15909
15910 type = alloc_type (objfile);
15911 INIT_CPLUS_SPECIFIC (type);
15912
15913 name = dwarf2_name (die, cu);
15914 if (name != NULL)
15915 {
15916 if (cu->language == language_cplus
15917 || cu->language == language_d
15918 || cu->language == language_rust)
15919 {
15920 const char *full_name = dwarf2_full_name (name, die, cu);
15921
15922 /* dwarf2_full_name might have already finished building the DIE's
15923 type. If so, there is no need to continue. */
15924 if (get_die_type (die, cu) != NULL)
15925 return get_die_type (die, cu);
15926
15927 TYPE_NAME (type) = full_name;
15928 }
15929 else
15930 {
15931 /* The name is already allocated along with this objfile, so
15932 we don't need to duplicate it for the type. */
15933 TYPE_NAME (type) = name;
15934 }
15935 }
15936
15937 if (die->tag == DW_TAG_structure_type)
15938 {
15939 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15940 }
15941 else if (die->tag == DW_TAG_union_type)
15942 {
15943 TYPE_CODE (type) = TYPE_CODE_UNION;
15944 }
15945 else if (die->tag == DW_TAG_variant_part)
15946 {
15947 TYPE_CODE (type) = TYPE_CODE_UNION;
15948 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15949 }
15950 else
15951 {
15952 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15953 }
15954
15955 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15956 TYPE_DECLARED_CLASS (type) = 1;
15957
15958 /* Store the calling convention in the type if it's available in
15959 the die. Otherwise the calling convention remains set to
15960 the default value DW_CC_normal. */
15961 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15962 if (attr != nullptr
15963 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15964 {
15965 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15966 TYPE_CPLUS_CALLING_CONVENTION (type)
15967 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15968 }
15969
15970 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15971 if (attr != nullptr)
15972 {
15973 if (attr_form_is_constant (attr))
15974 TYPE_LENGTH (type) = DW_UNSND (attr);
15975 else
15976 {
15977 /* For the moment, dynamic type sizes are not supported
15978 by GDB's struct type. The actual size is determined
15979 on-demand when resolving the type of a given object,
15980 so set the type's length to zero for now. Otherwise,
15981 we record an expression as the length, and that expression
15982 could lead to a very large value, which could eventually
15983 lead to us trying to allocate that much memory when creating
15984 a value of that type. */
15985 TYPE_LENGTH (type) = 0;
15986 }
15987 }
15988 else
15989 {
15990 TYPE_LENGTH (type) = 0;
15991 }
15992
15993 maybe_set_alignment (cu, die, type);
15994
15995 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15996 {
15997 /* ICC<14 does not output the required DW_AT_declaration on
15998 incomplete types, but gives them a size of zero. */
15999 TYPE_STUB (type) = 1;
16000 }
16001 else
16002 TYPE_STUB_SUPPORTED (type) = 1;
16003
16004 if (die_is_declaration (die, cu))
16005 TYPE_STUB (type) = 1;
16006 else if (attr == NULL && die->child == NULL
16007 && producer_is_realview (cu->producer))
16008 /* RealView does not output the required DW_AT_declaration
16009 on incomplete types. */
16010 TYPE_STUB (type) = 1;
16011
16012 /* We need to add the type field to the die immediately so we don't
16013 infinitely recurse when dealing with pointers to the structure
16014 type within the structure itself. */
16015 set_die_type (die, type, cu);
16016
16017 /* set_die_type should be already done. */
16018 set_descriptive_type (type, die, cu);
16019
16020 return type;
16021 }
16022
16023 /* A helper for process_structure_scope that handles a single member
16024 DIE. */
16025
16026 static void
16027 handle_struct_member_die (struct die_info *child_die, struct type *type,
16028 struct field_info *fi,
16029 std::vector<struct symbol *> *template_args,
16030 struct dwarf2_cu *cu)
16031 {
16032 if (child_die->tag == DW_TAG_member
16033 || child_die->tag == DW_TAG_variable
16034 || child_die->tag == DW_TAG_variant_part)
16035 {
16036 /* NOTE: carlton/2002-11-05: A C++ static data member
16037 should be a DW_TAG_member that is a declaration, but
16038 all versions of G++ as of this writing (so through at
16039 least 3.2.1) incorrectly generate DW_TAG_variable
16040 tags for them instead. */
16041 dwarf2_add_field (fi, child_die, cu);
16042 }
16043 else if (child_die->tag == DW_TAG_subprogram)
16044 {
16045 /* Rust doesn't have member functions in the C++ sense.
16046 However, it does emit ordinary functions as children
16047 of a struct DIE. */
16048 if (cu->language == language_rust)
16049 read_func_scope (child_die, cu);
16050 else
16051 {
16052 /* C++ member function. */
16053 dwarf2_add_member_fn (fi, child_die, type, cu);
16054 }
16055 }
16056 else if (child_die->tag == DW_TAG_inheritance)
16057 {
16058 /* C++ base class field. */
16059 dwarf2_add_field (fi, child_die, cu);
16060 }
16061 else if (type_can_define_types (child_die))
16062 dwarf2_add_type_defn (fi, child_die, cu);
16063 else if (child_die->tag == DW_TAG_template_type_param
16064 || child_die->tag == DW_TAG_template_value_param)
16065 {
16066 struct symbol *arg = new_symbol (child_die, NULL, cu);
16067
16068 if (arg != NULL)
16069 template_args->push_back (arg);
16070 }
16071 else if (child_die->tag == DW_TAG_variant)
16072 {
16073 /* In a variant we want to get the discriminant and also add a
16074 field for our sole member child. */
16075 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16076
16077 for (die_info *variant_child = child_die->child;
16078 variant_child != NULL;
16079 variant_child = sibling_die (variant_child))
16080 {
16081 if (variant_child->tag == DW_TAG_member)
16082 {
16083 handle_struct_member_die (variant_child, type, fi,
16084 template_args, cu);
16085 /* Only handle the one. */
16086 break;
16087 }
16088 }
16089
16090 /* We don't handle this but we might as well report it if we see
16091 it. */
16092 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
16093 complaint (_("DW_AT_discr_list is not supported yet"
16094 " - DIE at %s [in module %s]"),
16095 sect_offset_str (child_die->sect_off),
16096 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16097
16098 /* The first field was just added, so we can stash the
16099 discriminant there. */
16100 gdb_assert (!fi->fields.empty ());
16101 if (discr == NULL)
16102 fi->fields.back ().variant.default_branch = true;
16103 else
16104 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
16105 }
16106 }
16107
16108 /* Finish creating a structure or union type, including filling in
16109 its members and creating a symbol for it. */
16110
16111 static void
16112 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16113 {
16114 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16115 struct die_info *child_die;
16116 struct type *type;
16117
16118 type = get_die_type (die, cu);
16119 if (type == NULL)
16120 type = read_structure_type (die, cu);
16121
16122 /* When reading a DW_TAG_variant_part, we need to notice when we
16123 read the discriminant member, so we can record it later in the
16124 discriminant_info. */
16125 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16126 sect_offset discr_offset {};
16127 bool has_template_parameters = false;
16128
16129 if (is_variant_part)
16130 {
16131 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16132 if (discr == NULL)
16133 {
16134 /* Maybe it's a univariant form, an extension we support.
16135 In this case arrange not to check the offset. */
16136 is_variant_part = false;
16137 }
16138 else if (attr_form_is_ref (discr))
16139 {
16140 struct dwarf2_cu *target_cu = cu;
16141 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16142
16143 discr_offset = target_die->sect_off;
16144 }
16145 else
16146 {
16147 complaint (_("DW_AT_discr does not have DIE reference form"
16148 " - DIE at %s [in module %s]"),
16149 sect_offset_str (die->sect_off),
16150 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16151 is_variant_part = false;
16152 }
16153 }
16154
16155 if (die->child != NULL && ! die_is_declaration (die, cu))
16156 {
16157 struct field_info fi;
16158 std::vector<struct symbol *> template_args;
16159
16160 child_die = die->child;
16161
16162 while (child_die && child_die->tag)
16163 {
16164 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16165
16166 if (is_variant_part && discr_offset == child_die->sect_off)
16167 fi.fields.back ().variant.is_discriminant = true;
16168
16169 child_die = sibling_die (child_die);
16170 }
16171
16172 /* Attach template arguments to type. */
16173 if (!template_args.empty ())
16174 {
16175 has_template_parameters = true;
16176 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16177 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16178 TYPE_TEMPLATE_ARGUMENTS (type)
16179 = XOBNEWVEC (&objfile->objfile_obstack,
16180 struct symbol *,
16181 TYPE_N_TEMPLATE_ARGUMENTS (type));
16182 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16183 template_args.data (),
16184 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16185 * sizeof (struct symbol *)));
16186 }
16187
16188 /* Attach fields and member functions to the type. */
16189 if (fi.nfields)
16190 dwarf2_attach_fields_to_type (&fi, type, cu);
16191 if (!fi.fnfieldlists.empty ())
16192 {
16193 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16194
16195 /* Get the type which refers to the base class (possibly this
16196 class itself) which contains the vtable pointer for the current
16197 class from the DW_AT_containing_type attribute. This use of
16198 DW_AT_containing_type is a GNU extension. */
16199
16200 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16201 {
16202 struct type *t = die_containing_type (die, cu);
16203
16204 set_type_vptr_basetype (type, t);
16205 if (type == t)
16206 {
16207 int i;
16208
16209 /* Our own class provides vtbl ptr. */
16210 for (i = TYPE_NFIELDS (t) - 1;
16211 i >= TYPE_N_BASECLASSES (t);
16212 --i)
16213 {
16214 const char *fieldname = TYPE_FIELD_NAME (t, i);
16215
16216 if (is_vtable_name (fieldname, cu))
16217 {
16218 set_type_vptr_fieldno (type, i);
16219 break;
16220 }
16221 }
16222
16223 /* Complain if virtual function table field not found. */
16224 if (i < TYPE_N_BASECLASSES (t))
16225 complaint (_("virtual function table pointer "
16226 "not found when defining class '%s'"),
16227 TYPE_NAME (type) ? TYPE_NAME (type) : "");
16228 }
16229 else
16230 {
16231 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16232 }
16233 }
16234 else if (cu->producer
16235 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16236 {
16237 /* The IBM XLC compiler does not provide direct indication
16238 of the containing type, but the vtable pointer is
16239 always named __vfp. */
16240
16241 int i;
16242
16243 for (i = TYPE_NFIELDS (type) - 1;
16244 i >= TYPE_N_BASECLASSES (type);
16245 --i)
16246 {
16247 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16248 {
16249 set_type_vptr_fieldno (type, i);
16250 set_type_vptr_basetype (type, type);
16251 break;
16252 }
16253 }
16254 }
16255 }
16256
16257 /* Copy fi.typedef_field_list linked list elements content into the
16258 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16259 if (!fi.typedef_field_list.empty ())
16260 {
16261 int count = fi.typedef_field_list.size ();
16262
16263 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16264 TYPE_TYPEDEF_FIELD_ARRAY (type)
16265 = ((struct decl_field *)
16266 TYPE_ALLOC (type,
16267 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16268 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16269
16270 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16271 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16272 }
16273
16274 /* Copy fi.nested_types_list linked list elements content into the
16275 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16276 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16277 {
16278 int count = fi.nested_types_list.size ();
16279
16280 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16281 TYPE_NESTED_TYPES_ARRAY (type)
16282 = ((struct decl_field *)
16283 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16284 TYPE_NESTED_TYPES_COUNT (type) = count;
16285
16286 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16287 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16288 }
16289 }
16290
16291 quirk_gcc_member_function_pointer (type, objfile);
16292 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16293 cu->rust_unions.push_back (type);
16294
16295 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16296 snapshots) has been known to create a die giving a declaration
16297 for a class that has, as a child, a die giving a definition for a
16298 nested class. So we have to process our children even if the
16299 current die is a declaration. Normally, of course, a declaration
16300 won't have any children at all. */
16301
16302 child_die = die->child;
16303
16304 while (child_die != NULL && child_die->tag)
16305 {
16306 if (child_die->tag == DW_TAG_member
16307 || child_die->tag == DW_TAG_variable
16308 || child_die->tag == DW_TAG_inheritance
16309 || child_die->tag == DW_TAG_template_value_param
16310 || child_die->tag == DW_TAG_template_type_param)
16311 {
16312 /* Do nothing. */
16313 }
16314 else
16315 process_die (child_die, cu);
16316
16317 child_die = sibling_die (child_die);
16318 }
16319
16320 /* Do not consider external references. According to the DWARF standard,
16321 these DIEs are identified by the fact that they have no byte_size
16322 attribute, and a declaration attribute. */
16323 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16324 || !die_is_declaration (die, cu))
16325 {
16326 struct symbol *sym = new_symbol (die, type, cu);
16327
16328 if (has_template_parameters)
16329 {
16330 struct symtab *symtab;
16331 if (sym != nullptr)
16332 symtab = symbol_symtab (sym);
16333 else if (cu->line_header != nullptr)
16334 {
16335 /* Any related symtab will do. */
16336 symtab
16337 = cu->line_header->file_names ()[0].symtab;
16338 }
16339 else
16340 {
16341 symtab = nullptr;
16342 complaint (_("could not find suitable "
16343 "symtab for template parameter"
16344 " - DIE at %s [in module %s]"),
16345 sect_offset_str (die->sect_off),
16346 objfile_name (objfile));
16347 }
16348
16349 if (symtab != nullptr)
16350 {
16351 /* Make sure that the symtab is set on the new symbols.
16352 Even though they don't appear in this symtab directly,
16353 other parts of gdb assume that symbols do, and this is
16354 reasonably true. */
16355 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16356 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16357 }
16358 }
16359 }
16360 }
16361
16362 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16363 update TYPE using some information only available in DIE's children. */
16364
16365 static void
16366 update_enumeration_type_from_children (struct die_info *die,
16367 struct type *type,
16368 struct dwarf2_cu *cu)
16369 {
16370 struct die_info *child_die;
16371 int unsigned_enum = 1;
16372 int flag_enum = 1;
16373 ULONGEST mask = 0;
16374
16375 auto_obstack obstack;
16376
16377 for (child_die = die->child;
16378 child_die != NULL && child_die->tag;
16379 child_die = sibling_die (child_die))
16380 {
16381 struct attribute *attr;
16382 LONGEST value;
16383 const gdb_byte *bytes;
16384 struct dwarf2_locexpr_baton *baton;
16385 const char *name;
16386
16387 if (child_die->tag != DW_TAG_enumerator)
16388 continue;
16389
16390 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16391 if (attr == NULL)
16392 continue;
16393
16394 name = dwarf2_name (child_die, cu);
16395 if (name == NULL)
16396 name = "<anonymous enumerator>";
16397
16398 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16399 &value, &bytes, &baton);
16400 if (value < 0)
16401 {
16402 unsigned_enum = 0;
16403 flag_enum = 0;
16404 }
16405 else if ((mask & value) != 0)
16406 flag_enum = 0;
16407 else
16408 mask |= value;
16409
16410 /* If we already know that the enum type is neither unsigned, nor
16411 a flag type, no need to look at the rest of the enumerates. */
16412 if (!unsigned_enum && !flag_enum)
16413 break;
16414 }
16415
16416 if (unsigned_enum)
16417 TYPE_UNSIGNED (type) = 1;
16418 if (flag_enum)
16419 TYPE_FLAG_ENUM (type) = 1;
16420 }
16421
16422 /* Given a DW_AT_enumeration_type die, set its type. We do not
16423 complete the type's fields yet, or create any symbols. */
16424
16425 static struct type *
16426 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16427 {
16428 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16429 struct type *type;
16430 struct attribute *attr;
16431 const char *name;
16432
16433 /* If the definition of this type lives in .debug_types, read that type.
16434 Don't follow DW_AT_specification though, that will take us back up
16435 the chain and we want to go down. */
16436 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16437 if (attr != nullptr)
16438 {
16439 type = get_DW_AT_signature_type (die, attr, cu);
16440
16441 /* The type's CU may not be the same as CU.
16442 Ensure TYPE is recorded with CU in die_type_hash. */
16443 return set_die_type (die, type, cu);
16444 }
16445
16446 type = alloc_type (objfile);
16447
16448 TYPE_CODE (type) = TYPE_CODE_ENUM;
16449 name = dwarf2_full_name (NULL, die, cu);
16450 if (name != NULL)
16451 TYPE_NAME (type) = name;
16452
16453 attr = dwarf2_attr (die, DW_AT_type, cu);
16454 if (attr != NULL)
16455 {
16456 struct type *underlying_type = die_type (die, cu);
16457
16458 TYPE_TARGET_TYPE (type) = underlying_type;
16459 }
16460
16461 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16462 if (attr != nullptr)
16463 {
16464 TYPE_LENGTH (type) = DW_UNSND (attr);
16465 }
16466 else
16467 {
16468 TYPE_LENGTH (type) = 0;
16469 }
16470
16471 maybe_set_alignment (cu, die, type);
16472
16473 /* The enumeration DIE can be incomplete. In Ada, any type can be
16474 declared as private in the package spec, and then defined only
16475 inside the package body. Such types are known as Taft Amendment
16476 Types. When another package uses such a type, an incomplete DIE
16477 may be generated by the compiler. */
16478 if (die_is_declaration (die, cu))
16479 TYPE_STUB (type) = 1;
16480
16481 /* Finish the creation of this type by using the enum's children.
16482 We must call this even when the underlying type has been provided
16483 so that we can determine if we're looking at a "flag" enum. */
16484 update_enumeration_type_from_children (die, type, cu);
16485
16486 /* If this type has an underlying type that is not a stub, then we
16487 may use its attributes. We always use the "unsigned" attribute
16488 in this situation, because ordinarily we guess whether the type
16489 is unsigned -- but the guess can be wrong and the underlying type
16490 can tell us the reality. However, we defer to a local size
16491 attribute if one exists, because this lets the compiler override
16492 the underlying type if needed. */
16493 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16494 {
16495 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16496 if (TYPE_LENGTH (type) == 0)
16497 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16498 if (TYPE_RAW_ALIGN (type) == 0
16499 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16500 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16501 }
16502
16503 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16504
16505 return set_die_type (die, type, cu);
16506 }
16507
16508 /* Given a pointer to a die which begins an enumeration, process all
16509 the dies that define the members of the enumeration, and create the
16510 symbol for the enumeration type.
16511
16512 NOTE: We reverse the order of the element list. */
16513
16514 static void
16515 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16516 {
16517 struct type *this_type;
16518
16519 this_type = get_die_type (die, cu);
16520 if (this_type == NULL)
16521 this_type = read_enumeration_type (die, cu);
16522
16523 if (die->child != NULL)
16524 {
16525 struct die_info *child_die;
16526 struct symbol *sym;
16527 std::vector<struct field> fields;
16528 const char *name;
16529
16530 child_die = die->child;
16531 while (child_die && child_die->tag)
16532 {
16533 if (child_die->tag != DW_TAG_enumerator)
16534 {
16535 process_die (child_die, cu);
16536 }
16537 else
16538 {
16539 name = dwarf2_name (child_die, cu);
16540 if (name)
16541 {
16542 sym = new_symbol (child_die, this_type, cu);
16543
16544 fields.emplace_back ();
16545 struct field &field = fields.back ();
16546
16547 FIELD_NAME (field) = sym->linkage_name ();
16548 FIELD_TYPE (field) = NULL;
16549 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
16550 FIELD_BITSIZE (field) = 0;
16551 }
16552 }
16553
16554 child_die = sibling_die (child_die);
16555 }
16556
16557 if (!fields.empty ())
16558 {
16559 TYPE_NFIELDS (this_type) = fields.size ();
16560 TYPE_FIELDS (this_type) = (struct field *)
16561 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
16562 memcpy (TYPE_FIELDS (this_type), fields.data (),
16563 sizeof (struct field) * fields.size ());
16564 }
16565 }
16566
16567 /* If we are reading an enum from a .debug_types unit, and the enum
16568 is a declaration, and the enum is not the signatured type in the
16569 unit, then we do not want to add a symbol for it. Adding a
16570 symbol would in some cases obscure the true definition of the
16571 enum, giving users an incomplete type when the definition is
16572 actually available. Note that we do not want to do this for all
16573 enums which are just declarations, because C++0x allows forward
16574 enum declarations. */
16575 if (cu->per_cu->is_debug_types
16576 && die_is_declaration (die, cu))
16577 {
16578 struct signatured_type *sig_type;
16579
16580 sig_type = (struct signatured_type *) cu->per_cu;
16581 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16582 if (sig_type->type_offset_in_section != die->sect_off)
16583 return;
16584 }
16585
16586 new_symbol (die, this_type, cu);
16587 }
16588
16589 /* Extract all information from a DW_TAG_array_type DIE and put it in
16590 the DIE's type field. For now, this only handles one dimensional
16591 arrays. */
16592
16593 static struct type *
16594 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16595 {
16596 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16597 struct die_info *child_die;
16598 struct type *type;
16599 struct type *element_type, *range_type, *index_type;
16600 struct attribute *attr;
16601 const char *name;
16602 struct dynamic_prop *byte_stride_prop = NULL;
16603 unsigned int bit_stride = 0;
16604
16605 element_type = die_type (die, cu);
16606
16607 /* The die_type call above may have already set the type for this DIE. */
16608 type = get_die_type (die, cu);
16609 if (type)
16610 return type;
16611
16612 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16613 if (attr != NULL)
16614 {
16615 int stride_ok;
16616 struct type *prop_type
16617 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
16618
16619 byte_stride_prop
16620 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16621 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16622 prop_type);
16623 if (!stride_ok)
16624 {
16625 complaint (_("unable to read array DW_AT_byte_stride "
16626 " - DIE at %s [in module %s]"),
16627 sect_offset_str (die->sect_off),
16628 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16629 /* Ignore this attribute. We will likely not be able to print
16630 arrays of this type correctly, but there is little we can do
16631 to help if we cannot read the attribute's value. */
16632 byte_stride_prop = NULL;
16633 }
16634 }
16635
16636 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16637 if (attr != NULL)
16638 bit_stride = DW_UNSND (attr);
16639
16640 /* Irix 6.2 native cc creates array types without children for
16641 arrays with unspecified length. */
16642 if (die->child == NULL)
16643 {
16644 index_type = objfile_type (objfile)->builtin_int;
16645 range_type = create_static_range_type (NULL, index_type, 0, -1);
16646 type = create_array_type_with_stride (NULL, element_type, range_type,
16647 byte_stride_prop, bit_stride);
16648 return set_die_type (die, type, cu);
16649 }
16650
16651 std::vector<struct type *> range_types;
16652 child_die = die->child;
16653 while (child_die && child_die->tag)
16654 {
16655 if (child_die->tag == DW_TAG_subrange_type)
16656 {
16657 struct type *child_type = read_type_die (child_die, cu);
16658
16659 if (child_type != NULL)
16660 {
16661 /* The range type was succesfully read. Save it for the
16662 array type creation. */
16663 range_types.push_back (child_type);
16664 }
16665 }
16666 child_die = sibling_die (child_die);
16667 }
16668
16669 /* Dwarf2 dimensions are output from left to right, create the
16670 necessary array types in backwards order. */
16671
16672 type = element_type;
16673
16674 if (read_array_order (die, cu) == DW_ORD_col_major)
16675 {
16676 int i = 0;
16677
16678 while (i < range_types.size ())
16679 type = create_array_type_with_stride (NULL, type, range_types[i++],
16680 byte_stride_prop, bit_stride);
16681 }
16682 else
16683 {
16684 size_t ndim = range_types.size ();
16685 while (ndim-- > 0)
16686 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16687 byte_stride_prop, bit_stride);
16688 }
16689
16690 /* Understand Dwarf2 support for vector types (like they occur on
16691 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16692 array type. This is not part of the Dwarf2/3 standard yet, but a
16693 custom vendor extension. The main difference between a regular
16694 array and the vector variant is that vectors are passed by value
16695 to functions. */
16696 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16697 if (attr != nullptr)
16698 make_vector_type (type);
16699
16700 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16701 implementation may choose to implement triple vectors using this
16702 attribute. */
16703 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16704 if (attr != nullptr)
16705 {
16706 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16707 TYPE_LENGTH (type) = DW_UNSND (attr);
16708 else
16709 complaint (_("DW_AT_byte_size for array type smaller "
16710 "than the total size of elements"));
16711 }
16712
16713 name = dwarf2_name (die, cu);
16714 if (name)
16715 TYPE_NAME (type) = name;
16716
16717 maybe_set_alignment (cu, die, type);
16718
16719 /* Install the type in the die. */
16720 set_die_type (die, type, cu);
16721
16722 /* set_die_type should be already done. */
16723 set_descriptive_type (type, die, cu);
16724
16725 return type;
16726 }
16727
16728 static enum dwarf_array_dim_ordering
16729 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16730 {
16731 struct attribute *attr;
16732
16733 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16734
16735 if (attr != nullptr)
16736 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16737
16738 /* GNU F77 is a special case, as at 08/2004 array type info is the
16739 opposite order to the dwarf2 specification, but data is still
16740 laid out as per normal fortran.
16741
16742 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16743 version checking. */
16744
16745 if (cu->language == language_fortran
16746 && cu->producer && strstr (cu->producer, "GNU F77"))
16747 {
16748 return DW_ORD_row_major;
16749 }
16750
16751 switch (cu->language_defn->la_array_ordering)
16752 {
16753 case array_column_major:
16754 return DW_ORD_col_major;
16755 case array_row_major:
16756 default:
16757 return DW_ORD_row_major;
16758 };
16759 }
16760
16761 /* Extract all information from a DW_TAG_set_type DIE and put it in
16762 the DIE's type field. */
16763
16764 static struct type *
16765 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16766 {
16767 struct type *domain_type, *set_type;
16768 struct attribute *attr;
16769
16770 domain_type = die_type (die, cu);
16771
16772 /* The die_type call above may have already set the type for this DIE. */
16773 set_type = get_die_type (die, cu);
16774 if (set_type)
16775 return set_type;
16776
16777 set_type = create_set_type (NULL, domain_type);
16778
16779 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16780 if (attr != nullptr)
16781 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16782
16783 maybe_set_alignment (cu, die, set_type);
16784
16785 return set_die_type (die, set_type, cu);
16786 }
16787
16788 /* A helper for read_common_block that creates a locexpr baton.
16789 SYM is the symbol which we are marking as computed.
16790 COMMON_DIE is the DIE for the common block.
16791 COMMON_LOC is the location expression attribute for the common
16792 block itself.
16793 MEMBER_LOC is the location expression attribute for the particular
16794 member of the common block that we are processing.
16795 CU is the CU from which the above come. */
16796
16797 static void
16798 mark_common_block_symbol_computed (struct symbol *sym,
16799 struct die_info *common_die,
16800 struct attribute *common_loc,
16801 struct attribute *member_loc,
16802 struct dwarf2_cu *cu)
16803 {
16804 struct dwarf2_per_objfile *dwarf2_per_objfile
16805 = cu->per_cu->dwarf2_per_objfile;
16806 struct objfile *objfile = dwarf2_per_objfile->objfile;
16807 struct dwarf2_locexpr_baton *baton;
16808 gdb_byte *ptr;
16809 unsigned int cu_off;
16810 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16811 LONGEST offset = 0;
16812
16813 gdb_assert (common_loc && member_loc);
16814 gdb_assert (attr_form_is_block (common_loc));
16815 gdb_assert (attr_form_is_block (member_loc)
16816 || attr_form_is_constant (member_loc));
16817
16818 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16819 baton->per_cu = cu->per_cu;
16820 gdb_assert (baton->per_cu);
16821
16822 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16823
16824 if (attr_form_is_constant (member_loc))
16825 {
16826 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16827 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16828 }
16829 else
16830 baton->size += DW_BLOCK (member_loc)->size;
16831
16832 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16833 baton->data = ptr;
16834
16835 *ptr++ = DW_OP_call4;
16836 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16837 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16838 ptr += 4;
16839
16840 if (attr_form_is_constant (member_loc))
16841 {
16842 *ptr++ = DW_OP_addr;
16843 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16844 ptr += cu->header.addr_size;
16845 }
16846 else
16847 {
16848 /* We have to copy the data here, because DW_OP_call4 will only
16849 use a DW_AT_location attribute. */
16850 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16851 ptr += DW_BLOCK (member_loc)->size;
16852 }
16853
16854 *ptr++ = DW_OP_plus;
16855 gdb_assert (ptr - baton->data == baton->size);
16856
16857 SYMBOL_LOCATION_BATON (sym) = baton;
16858 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16859 }
16860
16861 /* Create appropriate locally-scoped variables for all the
16862 DW_TAG_common_block entries. Also create a struct common_block
16863 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16864 is used to separate the common blocks name namespace from regular
16865 variable names. */
16866
16867 static void
16868 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16869 {
16870 struct attribute *attr;
16871
16872 attr = dwarf2_attr (die, DW_AT_location, cu);
16873 if (attr != nullptr)
16874 {
16875 /* Support the .debug_loc offsets. */
16876 if (attr_form_is_block (attr))
16877 {
16878 /* Ok. */
16879 }
16880 else if (attr_form_is_section_offset (attr))
16881 {
16882 dwarf2_complex_location_expr_complaint ();
16883 attr = NULL;
16884 }
16885 else
16886 {
16887 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16888 "common block member");
16889 attr = NULL;
16890 }
16891 }
16892
16893 if (die->child != NULL)
16894 {
16895 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16896 struct die_info *child_die;
16897 size_t n_entries = 0, size;
16898 struct common_block *common_block;
16899 struct symbol *sym;
16900
16901 for (child_die = die->child;
16902 child_die && child_die->tag;
16903 child_die = sibling_die (child_die))
16904 ++n_entries;
16905
16906 size = (sizeof (struct common_block)
16907 + (n_entries - 1) * sizeof (struct symbol *));
16908 common_block
16909 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16910 size);
16911 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16912 common_block->n_entries = 0;
16913
16914 for (child_die = die->child;
16915 child_die && child_die->tag;
16916 child_die = sibling_die (child_die))
16917 {
16918 /* Create the symbol in the DW_TAG_common_block block in the current
16919 symbol scope. */
16920 sym = new_symbol (child_die, NULL, cu);
16921 if (sym != NULL)
16922 {
16923 struct attribute *member_loc;
16924
16925 common_block->contents[common_block->n_entries++] = sym;
16926
16927 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16928 cu);
16929 if (member_loc)
16930 {
16931 /* GDB has handled this for a long time, but it is
16932 not specified by DWARF. It seems to have been
16933 emitted by gfortran at least as recently as:
16934 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16935 complaint (_("Variable in common block has "
16936 "DW_AT_data_member_location "
16937 "- DIE at %s [in module %s]"),
16938 sect_offset_str (child_die->sect_off),
16939 objfile_name (objfile));
16940
16941 if (attr_form_is_section_offset (member_loc))
16942 dwarf2_complex_location_expr_complaint ();
16943 else if (attr_form_is_constant (member_loc)
16944 || attr_form_is_block (member_loc))
16945 {
16946 if (attr != nullptr)
16947 mark_common_block_symbol_computed (sym, die, attr,
16948 member_loc, cu);
16949 }
16950 else
16951 dwarf2_complex_location_expr_complaint ();
16952 }
16953 }
16954 }
16955
16956 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16957 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16958 }
16959 }
16960
16961 /* Create a type for a C++ namespace. */
16962
16963 static struct type *
16964 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16965 {
16966 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16967 const char *previous_prefix, *name;
16968 int is_anonymous;
16969 struct type *type;
16970
16971 /* For extensions, reuse the type of the original namespace. */
16972 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16973 {
16974 struct die_info *ext_die;
16975 struct dwarf2_cu *ext_cu = cu;
16976
16977 ext_die = dwarf2_extension (die, &ext_cu);
16978 type = read_type_die (ext_die, ext_cu);
16979
16980 /* EXT_CU may not be the same as CU.
16981 Ensure TYPE is recorded with CU in die_type_hash. */
16982 return set_die_type (die, type, cu);
16983 }
16984
16985 name = namespace_name (die, &is_anonymous, cu);
16986
16987 /* Now build the name of the current namespace. */
16988
16989 previous_prefix = determine_prefix (die, cu);
16990 if (previous_prefix[0] != '\0')
16991 name = typename_concat (&objfile->objfile_obstack,
16992 previous_prefix, name, 0, cu);
16993
16994 /* Create the type. */
16995 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16996
16997 return set_die_type (die, type, cu);
16998 }
16999
17000 /* Read a namespace scope. */
17001
17002 static void
17003 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17004 {
17005 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17006 int is_anonymous;
17007
17008 /* Add a symbol associated to this if we haven't seen the namespace
17009 before. Also, add a using directive if it's an anonymous
17010 namespace. */
17011
17012 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17013 {
17014 struct type *type;
17015
17016 type = read_type_die (die, cu);
17017 new_symbol (die, type, cu);
17018
17019 namespace_name (die, &is_anonymous, cu);
17020 if (is_anonymous)
17021 {
17022 const char *previous_prefix = determine_prefix (die, cu);
17023
17024 std::vector<const char *> excludes;
17025 add_using_directive (using_directives (cu),
17026 previous_prefix, TYPE_NAME (type), NULL,
17027 NULL, excludes, 0, &objfile->objfile_obstack);
17028 }
17029 }
17030
17031 if (die->child != NULL)
17032 {
17033 struct die_info *child_die = die->child;
17034
17035 while (child_die && child_die->tag)
17036 {
17037 process_die (child_die, cu);
17038 child_die = sibling_die (child_die);
17039 }
17040 }
17041 }
17042
17043 /* Read a Fortran module as type. This DIE can be only a declaration used for
17044 imported module. Still we need that type as local Fortran "use ... only"
17045 declaration imports depend on the created type in determine_prefix. */
17046
17047 static struct type *
17048 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17049 {
17050 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17051 const char *module_name;
17052 struct type *type;
17053
17054 module_name = dwarf2_name (die, cu);
17055 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17056
17057 return set_die_type (die, type, cu);
17058 }
17059
17060 /* Read a Fortran module. */
17061
17062 static void
17063 read_module (struct die_info *die, struct dwarf2_cu *cu)
17064 {
17065 struct die_info *child_die = die->child;
17066 struct type *type;
17067
17068 type = read_type_die (die, cu);
17069 new_symbol (die, type, cu);
17070
17071 while (child_die && child_die->tag)
17072 {
17073 process_die (child_die, cu);
17074 child_die = sibling_die (child_die);
17075 }
17076 }
17077
17078 /* Return the name of the namespace represented by DIE. Set
17079 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17080 namespace. */
17081
17082 static const char *
17083 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17084 {
17085 struct die_info *current_die;
17086 const char *name = NULL;
17087
17088 /* Loop through the extensions until we find a name. */
17089
17090 for (current_die = die;
17091 current_die != NULL;
17092 current_die = dwarf2_extension (die, &cu))
17093 {
17094 /* We don't use dwarf2_name here so that we can detect the absence
17095 of a name -> anonymous namespace. */
17096 name = dwarf2_string_attr (die, DW_AT_name, cu);
17097
17098 if (name != NULL)
17099 break;
17100 }
17101
17102 /* Is it an anonymous namespace? */
17103
17104 *is_anonymous = (name == NULL);
17105 if (*is_anonymous)
17106 name = CP_ANONYMOUS_NAMESPACE_STR;
17107
17108 return name;
17109 }
17110
17111 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17112 the user defined type vector. */
17113
17114 static struct type *
17115 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17116 {
17117 struct gdbarch *gdbarch
17118 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
17119 struct comp_unit_head *cu_header = &cu->header;
17120 struct type *type;
17121 struct attribute *attr_byte_size;
17122 struct attribute *attr_address_class;
17123 int byte_size, addr_class;
17124 struct type *target_type;
17125
17126 target_type = die_type (die, cu);
17127
17128 /* The die_type call above may have already set the type for this DIE. */
17129 type = get_die_type (die, cu);
17130 if (type)
17131 return type;
17132
17133 type = lookup_pointer_type (target_type);
17134
17135 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17136 if (attr_byte_size)
17137 byte_size = DW_UNSND (attr_byte_size);
17138 else
17139 byte_size = cu_header->addr_size;
17140
17141 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17142 if (attr_address_class)
17143 addr_class = DW_UNSND (attr_address_class);
17144 else
17145 addr_class = DW_ADDR_none;
17146
17147 ULONGEST alignment = get_alignment (cu, die);
17148
17149 /* If the pointer size, alignment, or address class is different
17150 than the default, create a type variant marked as such and set
17151 the length accordingly. */
17152 if (TYPE_LENGTH (type) != byte_size
17153 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17154 && alignment != TYPE_RAW_ALIGN (type))
17155 || addr_class != DW_ADDR_none)
17156 {
17157 if (gdbarch_address_class_type_flags_p (gdbarch))
17158 {
17159 int type_flags;
17160
17161 type_flags = gdbarch_address_class_type_flags
17162 (gdbarch, byte_size, addr_class);
17163 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17164 == 0);
17165 type = make_type_with_address_space (type, type_flags);
17166 }
17167 else if (TYPE_LENGTH (type) != byte_size)
17168 {
17169 complaint (_("invalid pointer size %d"), byte_size);
17170 }
17171 else if (TYPE_RAW_ALIGN (type) != alignment)
17172 {
17173 complaint (_("Invalid DW_AT_alignment"
17174 " - DIE at %s [in module %s]"),
17175 sect_offset_str (die->sect_off),
17176 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17177 }
17178 else
17179 {
17180 /* Should we also complain about unhandled address classes? */
17181 }
17182 }
17183
17184 TYPE_LENGTH (type) = byte_size;
17185 set_type_align (type, alignment);
17186 return set_die_type (die, type, cu);
17187 }
17188
17189 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17190 the user defined type vector. */
17191
17192 static struct type *
17193 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17194 {
17195 struct type *type;
17196 struct type *to_type;
17197 struct type *domain;
17198
17199 to_type = die_type (die, cu);
17200 domain = die_containing_type (die, cu);
17201
17202 /* The calls above may have already set the type for this DIE. */
17203 type = get_die_type (die, cu);
17204 if (type)
17205 return type;
17206
17207 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17208 type = lookup_methodptr_type (to_type);
17209 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17210 {
17211 struct type *new_type
17212 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17213
17214 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17215 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17216 TYPE_VARARGS (to_type));
17217 type = lookup_methodptr_type (new_type);
17218 }
17219 else
17220 type = lookup_memberptr_type (to_type, domain);
17221
17222 return set_die_type (die, type, cu);
17223 }
17224
17225 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17226 the user defined type vector. */
17227
17228 static struct type *
17229 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17230 enum type_code refcode)
17231 {
17232 struct comp_unit_head *cu_header = &cu->header;
17233 struct type *type, *target_type;
17234 struct attribute *attr;
17235
17236 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17237
17238 target_type = die_type (die, cu);
17239
17240 /* The die_type call above may have already set the type for this DIE. */
17241 type = get_die_type (die, cu);
17242 if (type)
17243 return type;
17244
17245 type = lookup_reference_type (target_type, refcode);
17246 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17247 if (attr != nullptr)
17248 {
17249 TYPE_LENGTH (type) = DW_UNSND (attr);
17250 }
17251 else
17252 {
17253 TYPE_LENGTH (type) = cu_header->addr_size;
17254 }
17255 maybe_set_alignment (cu, die, type);
17256 return set_die_type (die, type, cu);
17257 }
17258
17259 /* Add the given cv-qualifiers to the element type of the array. GCC
17260 outputs DWARF type qualifiers that apply to an array, not the
17261 element type. But GDB relies on the array element type to carry
17262 the cv-qualifiers. This mimics section 6.7.3 of the C99
17263 specification. */
17264
17265 static struct type *
17266 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17267 struct type *base_type, int cnst, int voltl)
17268 {
17269 struct type *el_type, *inner_array;
17270
17271 base_type = copy_type (base_type);
17272 inner_array = base_type;
17273
17274 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17275 {
17276 TYPE_TARGET_TYPE (inner_array) =
17277 copy_type (TYPE_TARGET_TYPE (inner_array));
17278 inner_array = TYPE_TARGET_TYPE (inner_array);
17279 }
17280
17281 el_type = TYPE_TARGET_TYPE (inner_array);
17282 cnst |= TYPE_CONST (el_type);
17283 voltl |= TYPE_VOLATILE (el_type);
17284 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17285
17286 return set_die_type (die, base_type, cu);
17287 }
17288
17289 static struct type *
17290 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17291 {
17292 struct type *base_type, *cv_type;
17293
17294 base_type = die_type (die, cu);
17295
17296 /* The die_type call above may have already set the type for this DIE. */
17297 cv_type = get_die_type (die, cu);
17298 if (cv_type)
17299 return cv_type;
17300
17301 /* In case the const qualifier is applied to an array type, the element type
17302 is so qualified, not the array type (section 6.7.3 of C99). */
17303 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17304 return add_array_cv_type (die, cu, base_type, 1, 0);
17305
17306 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17307 return set_die_type (die, cv_type, cu);
17308 }
17309
17310 static struct type *
17311 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17312 {
17313 struct type *base_type, *cv_type;
17314
17315 base_type = die_type (die, cu);
17316
17317 /* The die_type call above may have already set the type for this DIE. */
17318 cv_type = get_die_type (die, cu);
17319 if (cv_type)
17320 return cv_type;
17321
17322 /* In case the volatile qualifier is applied to an array type, the
17323 element type is so qualified, not the array type (section 6.7.3
17324 of C99). */
17325 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17326 return add_array_cv_type (die, cu, base_type, 0, 1);
17327
17328 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17329 return set_die_type (die, cv_type, cu);
17330 }
17331
17332 /* Handle DW_TAG_restrict_type. */
17333
17334 static struct type *
17335 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17336 {
17337 struct type *base_type, *cv_type;
17338
17339 base_type = die_type (die, cu);
17340
17341 /* The die_type call above may have already set the type for this DIE. */
17342 cv_type = get_die_type (die, cu);
17343 if (cv_type)
17344 return cv_type;
17345
17346 cv_type = make_restrict_type (base_type);
17347 return set_die_type (die, cv_type, cu);
17348 }
17349
17350 /* Handle DW_TAG_atomic_type. */
17351
17352 static struct type *
17353 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17354 {
17355 struct type *base_type, *cv_type;
17356
17357 base_type = die_type (die, cu);
17358
17359 /* The die_type call above may have already set the type for this DIE. */
17360 cv_type = get_die_type (die, cu);
17361 if (cv_type)
17362 return cv_type;
17363
17364 cv_type = make_atomic_type (base_type);
17365 return set_die_type (die, cv_type, cu);
17366 }
17367
17368 /* Extract all information from a DW_TAG_string_type DIE and add to
17369 the user defined type vector. It isn't really a user defined type,
17370 but it behaves like one, with other DIE's using an AT_user_def_type
17371 attribute to reference it. */
17372
17373 static struct type *
17374 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17375 {
17376 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17377 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17378 struct type *type, *range_type, *index_type, *char_type;
17379 struct attribute *attr;
17380 struct dynamic_prop prop;
17381 bool length_is_constant = true;
17382 LONGEST length;
17383
17384 /* There are a couple of places where bit sizes might be made use of
17385 when parsing a DW_TAG_string_type, however, no producer that we know
17386 of make use of these. Handling bit sizes that are a multiple of the
17387 byte size is easy enough, but what about other bit sizes? Lets deal
17388 with that problem when we have to. Warn about these attributes being
17389 unsupported, then parse the type and ignore them like we always
17390 have. */
17391 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17392 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17393 {
17394 static bool warning_printed = false;
17395 if (!warning_printed)
17396 {
17397 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17398 "currently supported on DW_TAG_string_type."));
17399 warning_printed = true;
17400 }
17401 }
17402
17403 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17404 if (attr != nullptr && !attr_form_is_constant (attr))
17405 {
17406 /* The string length describes the location at which the length of
17407 the string can be found. The size of the length field can be
17408 specified with one of the attributes below. */
17409 struct type *prop_type;
17410 struct attribute *len
17411 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17412 if (len == nullptr)
17413 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17414 if (len != nullptr && attr_form_is_constant (len))
17415 {
17416 /* Pass 0 as the default as we know this attribute is constant
17417 and the default value will not be returned. */
17418 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17419 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17420 }
17421 else
17422 {
17423 /* If the size is not specified then we assume it is the size of
17424 an address on this target. */
17425 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17426 }
17427
17428 /* Convert the attribute into a dynamic property. */
17429 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17430 length = 1;
17431 else
17432 length_is_constant = false;
17433 }
17434 else if (attr != nullptr)
17435 {
17436 /* This DW_AT_string_length just contains the length with no
17437 indirection. There's no need to create a dynamic property in this
17438 case. Pass 0 for the default value as we know it will not be
17439 returned in this case. */
17440 length = dwarf2_get_attr_constant_value (attr, 0);
17441 }
17442 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17443 {
17444 /* We don't currently support non-constant byte sizes for strings. */
17445 length = dwarf2_get_attr_constant_value (attr, 1);
17446 }
17447 else
17448 {
17449 /* Use 1 as a fallback length if we have nothing else. */
17450 length = 1;
17451 }
17452
17453 index_type = objfile_type (objfile)->builtin_int;
17454 if (length_is_constant)
17455 range_type = create_static_range_type (NULL, index_type, 1, length);
17456 else
17457 {
17458 struct dynamic_prop low_bound;
17459
17460 low_bound.kind = PROP_CONST;
17461 low_bound.data.const_val = 1;
17462 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17463 }
17464 char_type = language_string_char_type (cu->language_defn, gdbarch);
17465 type = create_string_type (NULL, char_type, range_type);
17466
17467 return set_die_type (die, type, cu);
17468 }
17469
17470 /* Assuming that DIE corresponds to a function, returns nonzero
17471 if the function is prototyped. */
17472
17473 static int
17474 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17475 {
17476 struct attribute *attr;
17477
17478 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17479 if (attr && (DW_UNSND (attr) != 0))
17480 return 1;
17481
17482 /* The DWARF standard implies that the DW_AT_prototyped attribute
17483 is only meaningful for C, but the concept also extends to other
17484 languages that allow unprototyped functions (Eg: Objective C).
17485 For all other languages, assume that functions are always
17486 prototyped. */
17487 if (cu->language != language_c
17488 && cu->language != language_objc
17489 && cu->language != language_opencl)
17490 return 1;
17491
17492 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17493 prototyped and unprototyped functions; default to prototyped,
17494 since that is more common in modern code (and RealView warns
17495 about unprototyped functions). */
17496 if (producer_is_realview (cu->producer))
17497 return 1;
17498
17499 return 0;
17500 }
17501
17502 /* Handle DIES due to C code like:
17503
17504 struct foo
17505 {
17506 int (*funcp)(int a, long l);
17507 int b;
17508 };
17509
17510 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17511
17512 static struct type *
17513 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17514 {
17515 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17516 struct type *type; /* Type that this function returns. */
17517 struct type *ftype; /* Function that returns above type. */
17518 struct attribute *attr;
17519
17520 type = die_type (die, cu);
17521
17522 /* The die_type call above may have already set the type for this DIE. */
17523 ftype = get_die_type (die, cu);
17524 if (ftype)
17525 return ftype;
17526
17527 ftype = lookup_function_type (type);
17528
17529 if (prototyped_function_p (die, cu))
17530 TYPE_PROTOTYPED (ftype) = 1;
17531
17532 /* Store the calling convention in the type if it's available in
17533 the subroutine die. Otherwise set the calling convention to
17534 the default value DW_CC_normal. */
17535 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17536 if (attr != nullptr
17537 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17538 TYPE_CALLING_CONVENTION (ftype)
17539 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17540 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17541 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17542 else
17543 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17544
17545 /* Record whether the function returns normally to its caller or not
17546 if the DWARF producer set that information. */
17547 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17548 if (attr && (DW_UNSND (attr) != 0))
17549 TYPE_NO_RETURN (ftype) = 1;
17550
17551 /* We need to add the subroutine type to the die immediately so
17552 we don't infinitely recurse when dealing with parameters
17553 declared as the same subroutine type. */
17554 set_die_type (die, ftype, cu);
17555
17556 if (die->child != NULL)
17557 {
17558 struct type *void_type = objfile_type (objfile)->builtin_void;
17559 struct die_info *child_die;
17560 int nparams, iparams;
17561
17562 /* Count the number of parameters.
17563 FIXME: GDB currently ignores vararg functions, but knows about
17564 vararg member functions. */
17565 nparams = 0;
17566 child_die = die->child;
17567 while (child_die && child_die->tag)
17568 {
17569 if (child_die->tag == DW_TAG_formal_parameter)
17570 nparams++;
17571 else if (child_die->tag == DW_TAG_unspecified_parameters)
17572 TYPE_VARARGS (ftype) = 1;
17573 child_die = sibling_die (child_die);
17574 }
17575
17576 /* Allocate storage for parameters and fill them in. */
17577 TYPE_NFIELDS (ftype) = nparams;
17578 TYPE_FIELDS (ftype) = (struct field *)
17579 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17580
17581 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17582 even if we error out during the parameters reading below. */
17583 for (iparams = 0; iparams < nparams; iparams++)
17584 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17585
17586 iparams = 0;
17587 child_die = die->child;
17588 while (child_die && child_die->tag)
17589 {
17590 if (child_die->tag == DW_TAG_formal_parameter)
17591 {
17592 struct type *arg_type;
17593
17594 /* DWARF version 2 has no clean way to discern C++
17595 static and non-static member functions. G++ helps
17596 GDB by marking the first parameter for non-static
17597 member functions (which is the this pointer) as
17598 artificial. We pass this information to
17599 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17600
17601 DWARF version 3 added DW_AT_object_pointer, which GCC
17602 4.5 does not yet generate. */
17603 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17604 if (attr != nullptr)
17605 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17606 else
17607 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17608 arg_type = die_type (child_die, cu);
17609
17610 /* RealView does not mark THIS as const, which the testsuite
17611 expects. GCC marks THIS as const in method definitions,
17612 but not in the class specifications (GCC PR 43053). */
17613 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17614 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17615 {
17616 int is_this = 0;
17617 struct dwarf2_cu *arg_cu = cu;
17618 const char *name = dwarf2_name (child_die, cu);
17619
17620 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17621 if (attr != nullptr)
17622 {
17623 /* If the compiler emits this, use it. */
17624 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17625 is_this = 1;
17626 }
17627 else if (name && strcmp (name, "this") == 0)
17628 /* Function definitions will have the argument names. */
17629 is_this = 1;
17630 else if (name == NULL && iparams == 0)
17631 /* Declarations may not have the names, so like
17632 elsewhere in GDB, assume an artificial first
17633 argument is "this". */
17634 is_this = 1;
17635
17636 if (is_this)
17637 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17638 arg_type, 0);
17639 }
17640
17641 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17642 iparams++;
17643 }
17644 child_die = sibling_die (child_die);
17645 }
17646 }
17647
17648 return ftype;
17649 }
17650
17651 static struct type *
17652 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17653 {
17654 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17655 const char *name = NULL;
17656 struct type *this_type, *target_type;
17657
17658 name = dwarf2_full_name (NULL, die, cu);
17659 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17660 TYPE_TARGET_STUB (this_type) = 1;
17661 set_die_type (die, this_type, cu);
17662 target_type = die_type (die, cu);
17663 if (target_type != this_type)
17664 TYPE_TARGET_TYPE (this_type) = target_type;
17665 else
17666 {
17667 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17668 spec and cause infinite loops in GDB. */
17669 complaint (_("Self-referential DW_TAG_typedef "
17670 "- DIE at %s [in module %s]"),
17671 sect_offset_str (die->sect_off), objfile_name (objfile));
17672 TYPE_TARGET_TYPE (this_type) = NULL;
17673 }
17674 return this_type;
17675 }
17676
17677 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17678 (which may be different from NAME) to the architecture back-end to allow
17679 it to guess the correct format if necessary. */
17680
17681 static struct type *
17682 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17683 const char *name_hint, enum bfd_endian byte_order)
17684 {
17685 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17686 const struct floatformat **format;
17687 struct type *type;
17688
17689 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17690 if (format)
17691 type = init_float_type (objfile, bits, name, format, byte_order);
17692 else
17693 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17694
17695 return type;
17696 }
17697
17698 /* Allocate an integer type of size BITS and name NAME. */
17699
17700 static struct type *
17701 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17702 int bits, int unsigned_p, const char *name)
17703 {
17704 struct type *type;
17705
17706 /* Versions of Intel's C Compiler generate an integer type called "void"
17707 instead of using DW_TAG_unspecified_type. This has been seen on
17708 at least versions 14, 17, and 18. */
17709 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17710 && strcmp (name, "void") == 0)
17711 type = objfile_type (objfile)->builtin_void;
17712 else
17713 type = init_integer_type (objfile, bits, unsigned_p, name);
17714
17715 return type;
17716 }
17717
17718 /* Initialise and return a floating point type of size BITS suitable for
17719 use as a component of a complex number. The NAME_HINT is passed through
17720 when initialising the floating point type and is the name of the complex
17721 type.
17722
17723 As DWARF doesn't currently provide an explicit name for the components
17724 of a complex number, but it can be helpful to have these components
17725 named, we try to select a suitable name based on the size of the
17726 component. */
17727 static struct type *
17728 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17729 struct objfile *objfile,
17730 int bits, const char *name_hint,
17731 enum bfd_endian byte_order)
17732 {
17733 gdbarch *gdbarch = get_objfile_arch (objfile);
17734 struct type *tt = nullptr;
17735
17736 /* Try to find a suitable floating point builtin type of size BITS.
17737 We're going to use the name of this type as the name for the complex
17738 target type that we are about to create. */
17739 switch (cu->language)
17740 {
17741 case language_fortran:
17742 switch (bits)
17743 {
17744 case 32:
17745 tt = builtin_f_type (gdbarch)->builtin_real;
17746 break;
17747 case 64:
17748 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17749 break;
17750 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17751 case 128:
17752 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17753 break;
17754 }
17755 break;
17756 default:
17757 switch (bits)
17758 {
17759 case 32:
17760 tt = builtin_type (gdbarch)->builtin_float;
17761 break;
17762 case 64:
17763 tt = builtin_type (gdbarch)->builtin_double;
17764 break;
17765 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17766 case 128:
17767 tt = builtin_type (gdbarch)->builtin_long_double;
17768 break;
17769 }
17770 break;
17771 }
17772
17773 /* If the type we found doesn't match the size we were looking for, then
17774 pretend we didn't find a type at all, the complex target type we
17775 create will then be nameless. */
17776 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17777 tt = nullptr;
17778
17779 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17780 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17781 }
17782
17783 /* Find a representation of a given base type and install
17784 it in the TYPE field of the die. */
17785
17786 static struct type *
17787 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17788 {
17789 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17790 struct type *type;
17791 struct attribute *attr;
17792 int encoding = 0, bits = 0;
17793 const char *name;
17794 gdbarch *arch;
17795
17796 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17797 if (attr != nullptr)
17798 encoding = DW_UNSND (attr);
17799 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17800 if (attr != nullptr)
17801 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17802 name = dwarf2_name (die, cu);
17803 if (!name)
17804 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17805
17806 arch = get_objfile_arch (objfile);
17807 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17808
17809 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17810 if (attr)
17811 {
17812 int endianity = DW_UNSND (attr);
17813
17814 switch (endianity)
17815 {
17816 case DW_END_big:
17817 byte_order = BFD_ENDIAN_BIG;
17818 break;
17819 case DW_END_little:
17820 byte_order = BFD_ENDIAN_LITTLE;
17821 break;
17822 default:
17823 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17824 break;
17825 }
17826 }
17827
17828 switch (encoding)
17829 {
17830 case DW_ATE_address:
17831 /* Turn DW_ATE_address into a void * pointer. */
17832 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17833 type = init_pointer_type (objfile, bits, name, type);
17834 break;
17835 case DW_ATE_boolean:
17836 type = init_boolean_type (objfile, bits, 1, name);
17837 break;
17838 case DW_ATE_complex_float:
17839 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17840 byte_order);
17841 type = init_complex_type (objfile, name, type);
17842 break;
17843 case DW_ATE_decimal_float:
17844 type = init_decfloat_type (objfile, bits, name);
17845 break;
17846 case DW_ATE_float:
17847 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17848 break;
17849 case DW_ATE_signed:
17850 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17851 break;
17852 case DW_ATE_unsigned:
17853 if (cu->language == language_fortran
17854 && name
17855 && startswith (name, "character("))
17856 type = init_character_type (objfile, bits, 1, name);
17857 else
17858 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17859 break;
17860 case DW_ATE_signed_char:
17861 if (cu->language == language_ada || cu->language == language_m2
17862 || cu->language == language_pascal
17863 || cu->language == language_fortran)
17864 type = init_character_type (objfile, bits, 0, name);
17865 else
17866 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17867 break;
17868 case DW_ATE_unsigned_char:
17869 if (cu->language == language_ada || cu->language == language_m2
17870 || cu->language == language_pascal
17871 || cu->language == language_fortran
17872 || cu->language == language_rust)
17873 type = init_character_type (objfile, bits, 1, name);
17874 else
17875 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17876 break;
17877 case DW_ATE_UTF:
17878 {
17879 if (bits == 16)
17880 type = builtin_type (arch)->builtin_char16;
17881 else if (bits == 32)
17882 type = builtin_type (arch)->builtin_char32;
17883 else
17884 {
17885 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17886 bits);
17887 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17888 }
17889 return set_die_type (die, type, cu);
17890 }
17891 break;
17892
17893 default:
17894 complaint (_("unsupported DW_AT_encoding: '%s'"),
17895 dwarf_type_encoding_name (encoding));
17896 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17897 break;
17898 }
17899
17900 if (name && strcmp (name, "char") == 0)
17901 TYPE_NOSIGN (type) = 1;
17902
17903 maybe_set_alignment (cu, die, type);
17904
17905 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17906
17907 return set_die_type (die, type, cu);
17908 }
17909
17910 /* Parse dwarf attribute if it's a block, reference or constant and put the
17911 resulting value of the attribute into struct bound_prop.
17912 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17913
17914 static int
17915 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17916 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17917 struct type *default_type)
17918 {
17919 struct dwarf2_property_baton *baton;
17920 struct obstack *obstack
17921 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17922
17923 gdb_assert (default_type != NULL);
17924
17925 if (attr == NULL || prop == NULL)
17926 return 0;
17927
17928 if (attr_form_is_block (attr))
17929 {
17930 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17931 baton->property_type = default_type;
17932 baton->locexpr.per_cu = cu->per_cu;
17933 baton->locexpr.size = DW_BLOCK (attr)->size;
17934 baton->locexpr.data = DW_BLOCK (attr)->data;
17935 switch (attr->name)
17936 {
17937 case DW_AT_string_length:
17938 baton->locexpr.is_reference = true;
17939 break;
17940 default:
17941 baton->locexpr.is_reference = false;
17942 break;
17943 }
17944 prop->data.baton = baton;
17945 prop->kind = PROP_LOCEXPR;
17946 gdb_assert (prop->data.baton != NULL);
17947 }
17948 else if (attr_form_is_ref (attr))
17949 {
17950 struct dwarf2_cu *target_cu = cu;
17951 struct die_info *target_die;
17952 struct attribute *target_attr;
17953
17954 target_die = follow_die_ref (die, attr, &target_cu);
17955 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17956 if (target_attr == NULL)
17957 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17958 target_cu);
17959 if (target_attr == NULL)
17960 return 0;
17961
17962 switch (target_attr->name)
17963 {
17964 case DW_AT_location:
17965 if (attr_form_is_section_offset (target_attr))
17966 {
17967 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17968 baton->property_type = die_type (target_die, target_cu);
17969 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17970 prop->data.baton = baton;
17971 prop->kind = PROP_LOCLIST;
17972 gdb_assert (prop->data.baton != NULL);
17973 }
17974 else if (attr_form_is_block (target_attr))
17975 {
17976 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17977 baton->property_type = die_type (target_die, target_cu);
17978 baton->locexpr.per_cu = cu->per_cu;
17979 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17980 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17981 baton->locexpr.is_reference = true;
17982 prop->data.baton = baton;
17983 prop->kind = PROP_LOCEXPR;
17984 gdb_assert (prop->data.baton != NULL);
17985 }
17986 else
17987 {
17988 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17989 "dynamic property");
17990 return 0;
17991 }
17992 break;
17993 case DW_AT_data_member_location:
17994 {
17995 LONGEST offset;
17996
17997 if (!handle_data_member_location (target_die, target_cu,
17998 &offset))
17999 return 0;
18000
18001 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18002 baton->property_type = read_type_die (target_die->parent,
18003 target_cu);
18004 baton->offset_info.offset = offset;
18005 baton->offset_info.type = die_type (target_die, target_cu);
18006 prop->data.baton = baton;
18007 prop->kind = PROP_ADDR_OFFSET;
18008 break;
18009 }
18010 }
18011 }
18012 else if (attr_form_is_constant (attr))
18013 {
18014 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
18015 prop->kind = PROP_CONST;
18016 }
18017 else
18018 {
18019 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18020 dwarf2_name (die, cu));
18021 return 0;
18022 }
18023
18024 return 1;
18025 }
18026
18027 /* Find an integer type SIZE_IN_BYTES bytes in size and return it.
18028 UNSIGNED_P controls if the integer is unsigned or not. */
18029
18030 static struct type *
18031 dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
18032 int size_in_bytes, bool unsigned_p)
18033 {
18034 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
18035 struct type *int_type;
18036
18037 /* Helper macro to examine the various builtin types. */
18038 #define TRY_TYPE(F) \
18039 int_type = (unsigned_p \
18040 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18041 : objfile_type (objfile)->builtin_ ## F); \
18042 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18043 return int_type
18044
18045 TRY_TYPE (char);
18046 TRY_TYPE (short);
18047 TRY_TYPE (int);
18048 TRY_TYPE (long);
18049 TRY_TYPE (long_long);
18050
18051 #undef TRY_TYPE
18052
18053 gdb_assert_not_reached ("unable to find suitable integer type");
18054 }
18055
18056 /* Find an integer type the same size as the address size given in the
18057 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
18058 is unsigned or not. */
18059
18060 static struct type *
18061 dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
18062 bool unsigned_p)
18063 {
18064 int addr_size = dwarf2_per_cu_addr_size (per_cu);
18065 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
18066 }
18067
18068 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18069 present (which is valid) then compute the default type based on the
18070 compilation units address size. */
18071
18072 static struct type *
18073 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18074 {
18075 struct type *index_type = die_type (die, cu);
18076
18077 /* Dwarf-2 specifications explicitly allows to create subrange types
18078 without specifying a base type.
18079 In that case, the base type must be set to the type of
18080 the lower bound, upper bound or count, in that order, if any of these
18081 three attributes references an object that has a type.
18082 If no base type is found, the Dwarf-2 specifications say that
18083 a signed integer type of size equal to the size of an address should
18084 be used.
18085 For the following C code: `extern char gdb_int [];'
18086 GCC produces an empty range DIE.
18087 FIXME: muller/2010-05-28: Possible references to object for low bound,
18088 high bound or count are not yet handled by this code. */
18089 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
18090 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18091
18092 return index_type;
18093 }
18094
18095 /* Read the given DW_AT_subrange DIE. */
18096
18097 static struct type *
18098 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18099 {
18100 struct type *base_type, *orig_base_type;
18101 struct type *range_type;
18102 struct attribute *attr;
18103 struct dynamic_prop low, high;
18104 int low_default_is_valid;
18105 int high_bound_is_count = 0;
18106 const char *name;
18107 ULONGEST negative_mask;
18108
18109 orig_base_type = read_subrange_index_type (die, cu);
18110
18111 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18112 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18113 creating the range type, but we use the result of check_typedef
18114 when examining properties of the type. */
18115 base_type = check_typedef (orig_base_type);
18116
18117 /* The die_type call above may have already set the type for this DIE. */
18118 range_type = get_die_type (die, cu);
18119 if (range_type)
18120 return range_type;
18121
18122 low.kind = PROP_CONST;
18123 high.kind = PROP_CONST;
18124 high.data.const_val = 0;
18125
18126 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18127 omitting DW_AT_lower_bound. */
18128 switch (cu->language)
18129 {
18130 case language_c:
18131 case language_cplus:
18132 low.data.const_val = 0;
18133 low_default_is_valid = 1;
18134 break;
18135 case language_fortran:
18136 low.data.const_val = 1;
18137 low_default_is_valid = 1;
18138 break;
18139 case language_d:
18140 case language_objc:
18141 case language_rust:
18142 low.data.const_val = 0;
18143 low_default_is_valid = (cu->header.version >= 4);
18144 break;
18145 case language_ada:
18146 case language_m2:
18147 case language_pascal:
18148 low.data.const_val = 1;
18149 low_default_is_valid = (cu->header.version >= 4);
18150 break;
18151 default:
18152 low.data.const_val = 0;
18153 low_default_is_valid = 0;
18154 break;
18155 }
18156
18157 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18158 if (attr != nullptr)
18159 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18160 else if (!low_default_is_valid)
18161 complaint (_("Missing DW_AT_lower_bound "
18162 "- DIE at %s [in module %s]"),
18163 sect_offset_str (die->sect_off),
18164 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18165
18166 struct attribute *attr_ub, *attr_count;
18167 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18168 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18169 {
18170 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18171 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18172 {
18173 /* If bounds are constant do the final calculation here. */
18174 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18175 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18176 else
18177 high_bound_is_count = 1;
18178 }
18179 else
18180 {
18181 if (attr_ub != NULL)
18182 complaint (_("Unresolved DW_AT_upper_bound "
18183 "- DIE at %s [in module %s]"),
18184 sect_offset_str (die->sect_off),
18185 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18186 if (attr_count != NULL)
18187 complaint (_("Unresolved DW_AT_count "
18188 "- DIE at %s [in module %s]"),
18189 sect_offset_str (die->sect_off),
18190 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18191 }
18192 }
18193
18194 LONGEST bias = 0;
18195 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18196 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18197 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18198
18199 /* Normally, the DWARF producers are expected to use a signed
18200 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18201 But this is unfortunately not always the case, as witnessed
18202 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18203 is used instead. To work around that ambiguity, we treat
18204 the bounds as signed, and thus sign-extend their values, when
18205 the base type is signed. */
18206 negative_mask =
18207 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18208 if (low.kind == PROP_CONST
18209 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18210 low.data.const_val |= negative_mask;
18211 if (high.kind == PROP_CONST
18212 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18213 high.data.const_val |= negative_mask;
18214
18215 /* Check for bit and byte strides. */
18216 struct dynamic_prop byte_stride_prop;
18217 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18218 if (attr_byte_stride != nullptr)
18219 {
18220 struct type *prop_type
18221 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18222 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18223 prop_type);
18224 }
18225
18226 struct dynamic_prop bit_stride_prop;
18227 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18228 if (attr_bit_stride != nullptr)
18229 {
18230 /* It only makes sense to have either a bit or byte stride. */
18231 if (attr_byte_stride != nullptr)
18232 {
18233 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18234 "- DIE at %s [in module %s]"),
18235 sect_offset_str (die->sect_off),
18236 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18237 attr_bit_stride = nullptr;
18238 }
18239 else
18240 {
18241 struct type *prop_type
18242 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18243 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18244 prop_type);
18245 }
18246 }
18247
18248 if (attr_byte_stride != nullptr
18249 || attr_bit_stride != nullptr)
18250 {
18251 bool byte_stride_p = (attr_byte_stride != nullptr);
18252 struct dynamic_prop *stride
18253 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18254
18255 range_type
18256 = create_range_type_with_stride (NULL, orig_base_type, &low,
18257 &high, bias, stride, byte_stride_p);
18258 }
18259 else
18260 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18261
18262 if (high_bound_is_count)
18263 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18264
18265 /* Ada expects an empty array on no boundary attributes. */
18266 if (attr == NULL && cu->language != language_ada)
18267 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
18268
18269 name = dwarf2_name (die, cu);
18270 if (name)
18271 TYPE_NAME (range_type) = name;
18272
18273 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18274 if (attr != nullptr)
18275 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18276
18277 maybe_set_alignment (cu, die, range_type);
18278
18279 set_die_type (die, range_type, cu);
18280
18281 /* set_die_type should be already done. */
18282 set_descriptive_type (range_type, die, cu);
18283
18284 return range_type;
18285 }
18286
18287 static struct type *
18288 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18289 {
18290 struct type *type;
18291
18292 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18293 NULL);
18294 TYPE_NAME (type) = dwarf2_name (die, cu);
18295
18296 /* In Ada, an unspecified type is typically used when the description
18297 of the type is deferred to a different unit. When encountering
18298 such a type, we treat it as a stub, and try to resolve it later on,
18299 when needed. */
18300 if (cu->language == language_ada)
18301 TYPE_STUB (type) = 1;
18302
18303 return set_die_type (die, type, cu);
18304 }
18305
18306 /* Read a single die and all its descendents. Set the die's sibling
18307 field to NULL; set other fields in the die correctly, and set all
18308 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18309 location of the info_ptr after reading all of those dies. PARENT
18310 is the parent of the die in question. */
18311
18312 static struct die_info *
18313 read_die_and_children (const struct die_reader_specs *reader,
18314 const gdb_byte *info_ptr,
18315 const gdb_byte **new_info_ptr,
18316 struct die_info *parent)
18317 {
18318 struct die_info *die;
18319 const gdb_byte *cur_ptr;
18320 int has_children;
18321
18322 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18323 if (die == NULL)
18324 {
18325 *new_info_ptr = cur_ptr;
18326 return NULL;
18327 }
18328 store_in_ref_table (die, reader->cu);
18329
18330 if (has_children)
18331 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18332 else
18333 {
18334 die->child = NULL;
18335 *new_info_ptr = cur_ptr;
18336 }
18337
18338 die->sibling = NULL;
18339 die->parent = parent;
18340 return die;
18341 }
18342
18343 /* Read a die, all of its descendents, and all of its siblings; set
18344 all of the fields of all of the dies correctly. Arguments are as
18345 in read_die_and_children. */
18346
18347 static struct die_info *
18348 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18349 const gdb_byte *info_ptr,
18350 const gdb_byte **new_info_ptr,
18351 struct die_info *parent)
18352 {
18353 struct die_info *first_die, *last_sibling;
18354 const gdb_byte *cur_ptr;
18355
18356 cur_ptr = info_ptr;
18357 first_die = last_sibling = NULL;
18358
18359 while (1)
18360 {
18361 struct die_info *die
18362 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18363
18364 if (die == NULL)
18365 {
18366 *new_info_ptr = cur_ptr;
18367 return first_die;
18368 }
18369
18370 if (!first_die)
18371 first_die = die;
18372 else
18373 last_sibling->sibling = die;
18374
18375 last_sibling = die;
18376 }
18377 }
18378
18379 /* Read a die, all of its descendents, and all of its siblings; set
18380 all of the fields of all of the dies correctly. Arguments are as
18381 in read_die_and_children.
18382 This the main entry point for reading a DIE and all its children. */
18383
18384 static struct die_info *
18385 read_die_and_siblings (const struct die_reader_specs *reader,
18386 const gdb_byte *info_ptr,
18387 const gdb_byte **new_info_ptr,
18388 struct die_info *parent)
18389 {
18390 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18391 new_info_ptr, parent);
18392
18393 if (dwarf_die_debug)
18394 {
18395 fprintf_unfiltered (gdb_stdlog,
18396 "Read die from %s@0x%x of %s:\n",
18397 get_section_name (reader->die_section),
18398 (unsigned) (info_ptr - reader->die_section->buffer),
18399 bfd_get_filename (reader->abfd));
18400 dump_die (die, dwarf_die_debug);
18401 }
18402
18403 return die;
18404 }
18405
18406 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18407 attributes.
18408 The caller is responsible for filling in the extra attributes
18409 and updating (*DIEP)->num_attrs.
18410 Set DIEP to point to a newly allocated die with its information,
18411 except for its child, sibling, and parent fields.
18412 Set HAS_CHILDREN to tell whether the die has children or not. */
18413
18414 static const gdb_byte *
18415 read_full_die_1 (const struct die_reader_specs *reader,
18416 struct die_info **diep, const gdb_byte *info_ptr,
18417 int *has_children, int num_extra_attrs)
18418 {
18419 unsigned int abbrev_number, bytes_read, i;
18420 struct abbrev_info *abbrev;
18421 struct die_info *die;
18422 struct dwarf2_cu *cu = reader->cu;
18423 bfd *abfd = reader->abfd;
18424
18425 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18426 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18427 info_ptr += bytes_read;
18428 if (!abbrev_number)
18429 {
18430 *diep = NULL;
18431 *has_children = 0;
18432 return info_ptr;
18433 }
18434
18435 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18436 if (!abbrev)
18437 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18438 abbrev_number,
18439 bfd_get_filename (abfd));
18440
18441 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18442 die->sect_off = sect_off;
18443 die->tag = abbrev->tag;
18444 die->abbrev = abbrev_number;
18445
18446 /* Make the result usable.
18447 The caller needs to update num_attrs after adding the extra
18448 attributes. */
18449 die->num_attrs = abbrev->num_attrs;
18450
18451 for (i = 0; i < abbrev->num_attrs; ++i)
18452 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18453 info_ptr);
18454
18455 *diep = die;
18456 *has_children = abbrev->has_children;
18457 return info_ptr;
18458 }
18459
18460 /* Read a die and all its attributes.
18461 Set DIEP to point to a newly allocated die with its information,
18462 except for its child, sibling, and parent fields.
18463 Set HAS_CHILDREN to tell whether the die has children or not. */
18464
18465 static const gdb_byte *
18466 read_full_die (const struct die_reader_specs *reader,
18467 struct die_info **diep, const gdb_byte *info_ptr,
18468 int *has_children)
18469 {
18470 const gdb_byte *result;
18471
18472 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18473
18474 if (dwarf_die_debug)
18475 {
18476 fprintf_unfiltered (gdb_stdlog,
18477 "Read die from %s@0x%x of %s:\n",
18478 get_section_name (reader->die_section),
18479 (unsigned) (info_ptr - reader->die_section->buffer),
18480 bfd_get_filename (reader->abfd));
18481 dump_die (*diep, dwarf_die_debug);
18482 }
18483
18484 return result;
18485 }
18486 \f
18487 /* Abbreviation tables.
18488
18489 In DWARF version 2, the description of the debugging information is
18490 stored in a separate .debug_abbrev section. Before we read any
18491 dies from a section we read in all abbreviations and install them
18492 in a hash table. */
18493
18494 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18495
18496 struct abbrev_info *
18497 abbrev_table::alloc_abbrev ()
18498 {
18499 struct abbrev_info *abbrev;
18500
18501 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18502 memset (abbrev, 0, sizeof (struct abbrev_info));
18503
18504 return abbrev;
18505 }
18506
18507 /* Add an abbreviation to the table. */
18508
18509 void
18510 abbrev_table::add_abbrev (unsigned int abbrev_number,
18511 struct abbrev_info *abbrev)
18512 {
18513 unsigned int hash_number;
18514
18515 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18516 abbrev->next = m_abbrevs[hash_number];
18517 m_abbrevs[hash_number] = abbrev;
18518 }
18519
18520 /* Look up an abbrev in the table.
18521 Returns NULL if the abbrev is not found. */
18522
18523 struct abbrev_info *
18524 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18525 {
18526 unsigned int hash_number;
18527 struct abbrev_info *abbrev;
18528
18529 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18530 abbrev = m_abbrevs[hash_number];
18531
18532 while (abbrev)
18533 {
18534 if (abbrev->number == abbrev_number)
18535 return abbrev;
18536 abbrev = abbrev->next;
18537 }
18538 return NULL;
18539 }
18540
18541 /* Read in an abbrev table. */
18542
18543 static abbrev_table_up
18544 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18545 struct dwarf2_section_info *section,
18546 sect_offset sect_off)
18547 {
18548 struct objfile *objfile = dwarf2_per_objfile->objfile;
18549 bfd *abfd = get_section_bfd_owner (section);
18550 const gdb_byte *abbrev_ptr;
18551 struct abbrev_info *cur_abbrev;
18552 unsigned int abbrev_number, bytes_read, abbrev_name;
18553 unsigned int abbrev_form;
18554 std::vector<struct attr_abbrev> cur_attrs;
18555
18556 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18557
18558 dwarf2_read_section (objfile, section);
18559 abbrev_ptr = section->buffer + to_underlying (sect_off);
18560 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18561 abbrev_ptr += bytes_read;
18562
18563 /* Loop until we reach an abbrev number of 0. */
18564 while (abbrev_number)
18565 {
18566 cur_attrs.clear ();
18567 cur_abbrev = abbrev_table->alloc_abbrev ();
18568
18569 /* read in abbrev header */
18570 cur_abbrev->number = abbrev_number;
18571 cur_abbrev->tag
18572 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18573 abbrev_ptr += bytes_read;
18574 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18575 abbrev_ptr += 1;
18576
18577 /* now read in declarations */
18578 for (;;)
18579 {
18580 LONGEST implicit_const;
18581
18582 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18583 abbrev_ptr += bytes_read;
18584 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18585 abbrev_ptr += bytes_read;
18586 if (abbrev_form == DW_FORM_implicit_const)
18587 {
18588 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18589 &bytes_read);
18590 abbrev_ptr += bytes_read;
18591 }
18592 else
18593 {
18594 /* Initialize it due to a false compiler warning. */
18595 implicit_const = -1;
18596 }
18597
18598 if (abbrev_name == 0)
18599 break;
18600
18601 cur_attrs.emplace_back ();
18602 struct attr_abbrev &cur_attr = cur_attrs.back ();
18603 cur_attr.name = (enum dwarf_attribute) abbrev_name;
18604 cur_attr.form = (enum dwarf_form) abbrev_form;
18605 cur_attr.implicit_const = implicit_const;
18606 ++cur_abbrev->num_attrs;
18607 }
18608
18609 cur_abbrev->attrs =
18610 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18611 cur_abbrev->num_attrs);
18612 memcpy (cur_abbrev->attrs, cur_attrs.data (),
18613 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18614
18615 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18616
18617 /* Get next abbreviation.
18618 Under Irix6 the abbreviations for a compilation unit are not
18619 always properly terminated with an abbrev number of 0.
18620 Exit loop if we encounter an abbreviation which we have
18621 already read (which means we are about to read the abbreviations
18622 for the next compile unit) or if the end of the abbreviation
18623 table is reached. */
18624 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18625 break;
18626 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18627 abbrev_ptr += bytes_read;
18628 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18629 break;
18630 }
18631
18632 return abbrev_table;
18633 }
18634
18635 /* Returns nonzero if TAG represents a type that we might generate a partial
18636 symbol for. */
18637
18638 static int
18639 is_type_tag_for_partial (int tag)
18640 {
18641 switch (tag)
18642 {
18643 #if 0
18644 /* Some types that would be reasonable to generate partial symbols for,
18645 that we don't at present. */
18646 case DW_TAG_array_type:
18647 case DW_TAG_file_type:
18648 case DW_TAG_ptr_to_member_type:
18649 case DW_TAG_set_type:
18650 case DW_TAG_string_type:
18651 case DW_TAG_subroutine_type:
18652 #endif
18653 case DW_TAG_base_type:
18654 case DW_TAG_class_type:
18655 case DW_TAG_interface_type:
18656 case DW_TAG_enumeration_type:
18657 case DW_TAG_structure_type:
18658 case DW_TAG_subrange_type:
18659 case DW_TAG_typedef:
18660 case DW_TAG_union_type:
18661 return 1;
18662 default:
18663 return 0;
18664 }
18665 }
18666
18667 /* Load all DIEs that are interesting for partial symbols into memory. */
18668
18669 static struct partial_die_info *
18670 load_partial_dies (const struct die_reader_specs *reader,
18671 const gdb_byte *info_ptr, int building_psymtab)
18672 {
18673 struct dwarf2_cu *cu = reader->cu;
18674 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18675 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18676 unsigned int bytes_read;
18677 unsigned int load_all = 0;
18678 int nesting_level = 1;
18679
18680 parent_die = NULL;
18681 last_die = NULL;
18682
18683 gdb_assert (cu->per_cu != NULL);
18684 if (cu->per_cu->load_all_dies)
18685 load_all = 1;
18686
18687 cu->partial_dies
18688 = htab_create_alloc_ex (cu->header.length / 12,
18689 partial_die_hash,
18690 partial_die_eq,
18691 NULL,
18692 &cu->comp_unit_obstack,
18693 hashtab_obstack_allocate,
18694 dummy_obstack_deallocate);
18695
18696 while (1)
18697 {
18698 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18699
18700 /* A NULL abbrev means the end of a series of children. */
18701 if (abbrev == NULL)
18702 {
18703 if (--nesting_level == 0)
18704 return first_die;
18705
18706 info_ptr += bytes_read;
18707 last_die = parent_die;
18708 parent_die = parent_die->die_parent;
18709 continue;
18710 }
18711
18712 /* Check for template arguments. We never save these; if
18713 they're seen, we just mark the parent, and go on our way. */
18714 if (parent_die != NULL
18715 && cu->language == language_cplus
18716 && (abbrev->tag == DW_TAG_template_type_param
18717 || abbrev->tag == DW_TAG_template_value_param))
18718 {
18719 parent_die->has_template_arguments = 1;
18720
18721 if (!load_all)
18722 {
18723 /* We don't need a partial DIE for the template argument. */
18724 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18725 continue;
18726 }
18727 }
18728
18729 /* We only recurse into c++ subprograms looking for template arguments.
18730 Skip their other children. */
18731 if (!load_all
18732 && cu->language == language_cplus
18733 && parent_die != NULL
18734 && parent_die->tag == DW_TAG_subprogram)
18735 {
18736 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18737 continue;
18738 }
18739
18740 /* Check whether this DIE is interesting enough to save. Normally
18741 we would not be interested in members here, but there may be
18742 later variables referencing them via DW_AT_specification (for
18743 static members). */
18744 if (!load_all
18745 && !is_type_tag_for_partial (abbrev->tag)
18746 && abbrev->tag != DW_TAG_constant
18747 && abbrev->tag != DW_TAG_enumerator
18748 && abbrev->tag != DW_TAG_subprogram
18749 && abbrev->tag != DW_TAG_inlined_subroutine
18750 && abbrev->tag != DW_TAG_lexical_block
18751 && abbrev->tag != DW_TAG_variable
18752 && abbrev->tag != DW_TAG_namespace
18753 && abbrev->tag != DW_TAG_module
18754 && abbrev->tag != DW_TAG_member
18755 && abbrev->tag != DW_TAG_imported_unit
18756 && abbrev->tag != DW_TAG_imported_declaration)
18757 {
18758 /* Otherwise we skip to the next sibling, if any. */
18759 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18760 continue;
18761 }
18762
18763 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18764 abbrev);
18765
18766 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18767
18768 /* This two-pass algorithm for processing partial symbols has a
18769 high cost in cache pressure. Thus, handle some simple cases
18770 here which cover the majority of C partial symbols. DIEs
18771 which neither have specification tags in them, nor could have
18772 specification tags elsewhere pointing at them, can simply be
18773 processed and discarded.
18774
18775 This segment is also optional; scan_partial_symbols and
18776 add_partial_symbol will handle these DIEs if we chain
18777 them in normally. When compilers which do not emit large
18778 quantities of duplicate debug information are more common,
18779 this code can probably be removed. */
18780
18781 /* Any complete simple types at the top level (pretty much all
18782 of them, for a language without namespaces), can be processed
18783 directly. */
18784 if (parent_die == NULL
18785 && pdi.has_specification == 0
18786 && pdi.is_declaration == 0
18787 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18788 || pdi.tag == DW_TAG_base_type
18789 || pdi.tag == DW_TAG_subrange_type))
18790 {
18791 if (building_psymtab && pdi.name != NULL)
18792 add_psymbol_to_list (pdi.name, false,
18793 VAR_DOMAIN, LOC_TYPEDEF, -1,
18794 psymbol_placement::STATIC,
18795 0, cu->language, objfile);
18796 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18797 continue;
18798 }
18799
18800 /* The exception for DW_TAG_typedef with has_children above is
18801 a workaround of GCC PR debug/47510. In the case of this complaint
18802 type_name_or_error will error on such types later.
18803
18804 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18805 it could not find the child DIEs referenced later, this is checked
18806 above. In correct DWARF DW_TAG_typedef should have no children. */
18807
18808 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18809 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18810 "- DIE at %s [in module %s]"),
18811 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18812
18813 /* If we're at the second level, and we're an enumerator, and
18814 our parent has no specification (meaning possibly lives in a
18815 namespace elsewhere), then we can add the partial symbol now
18816 instead of queueing it. */
18817 if (pdi.tag == DW_TAG_enumerator
18818 && parent_die != NULL
18819 && parent_die->die_parent == NULL
18820 && parent_die->tag == DW_TAG_enumeration_type
18821 && parent_die->has_specification == 0)
18822 {
18823 if (pdi.name == NULL)
18824 complaint (_("malformed enumerator DIE ignored"));
18825 else if (building_psymtab)
18826 add_psymbol_to_list (pdi.name, false,
18827 VAR_DOMAIN, LOC_CONST, -1,
18828 cu->language == language_cplus
18829 ? psymbol_placement::GLOBAL
18830 : psymbol_placement::STATIC,
18831 0, cu->language, objfile);
18832
18833 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18834 continue;
18835 }
18836
18837 struct partial_die_info *part_die
18838 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18839
18840 /* We'll save this DIE so link it in. */
18841 part_die->die_parent = parent_die;
18842 part_die->die_sibling = NULL;
18843 part_die->die_child = NULL;
18844
18845 if (last_die && last_die == parent_die)
18846 last_die->die_child = part_die;
18847 else if (last_die)
18848 last_die->die_sibling = part_die;
18849
18850 last_die = part_die;
18851
18852 if (first_die == NULL)
18853 first_die = part_die;
18854
18855 /* Maybe add the DIE to the hash table. Not all DIEs that we
18856 find interesting need to be in the hash table, because we
18857 also have the parent/sibling/child chains; only those that we
18858 might refer to by offset later during partial symbol reading.
18859
18860 For now this means things that might have be the target of a
18861 DW_AT_specification, DW_AT_abstract_origin, or
18862 DW_AT_extension. DW_AT_extension will refer only to
18863 namespaces; DW_AT_abstract_origin refers to functions (and
18864 many things under the function DIE, but we do not recurse
18865 into function DIEs during partial symbol reading) and
18866 possibly variables as well; DW_AT_specification refers to
18867 declarations. Declarations ought to have the DW_AT_declaration
18868 flag. It happens that GCC forgets to put it in sometimes, but
18869 only for functions, not for types.
18870
18871 Adding more things than necessary to the hash table is harmless
18872 except for the performance cost. Adding too few will result in
18873 wasted time in find_partial_die, when we reread the compilation
18874 unit with load_all_dies set. */
18875
18876 if (load_all
18877 || abbrev->tag == DW_TAG_constant
18878 || abbrev->tag == DW_TAG_subprogram
18879 || abbrev->tag == DW_TAG_variable
18880 || abbrev->tag == DW_TAG_namespace
18881 || part_die->is_declaration)
18882 {
18883 void **slot;
18884
18885 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18886 to_underlying (part_die->sect_off),
18887 INSERT);
18888 *slot = part_die;
18889 }
18890
18891 /* For some DIEs we want to follow their children (if any). For C
18892 we have no reason to follow the children of structures; for other
18893 languages we have to, so that we can get at method physnames
18894 to infer fully qualified class names, for DW_AT_specification,
18895 and for C++ template arguments. For C++, we also look one level
18896 inside functions to find template arguments (if the name of the
18897 function does not already contain the template arguments).
18898
18899 For Ada and Fortran, we need to scan the children of subprograms
18900 and lexical blocks as well because these languages allow the
18901 definition of nested entities that could be interesting for the
18902 debugger, such as nested subprograms for instance. */
18903 if (last_die->has_children
18904 && (load_all
18905 || last_die->tag == DW_TAG_namespace
18906 || last_die->tag == DW_TAG_module
18907 || last_die->tag == DW_TAG_enumeration_type
18908 || (cu->language == language_cplus
18909 && last_die->tag == DW_TAG_subprogram
18910 && (last_die->name == NULL
18911 || strchr (last_die->name, '<') == NULL))
18912 || (cu->language != language_c
18913 && (last_die->tag == DW_TAG_class_type
18914 || last_die->tag == DW_TAG_interface_type
18915 || last_die->tag == DW_TAG_structure_type
18916 || last_die->tag == DW_TAG_union_type))
18917 || ((cu->language == language_ada
18918 || cu->language == language_fortran)
18919 && (last_die->tag == DW_TAG_subprogram
18920 || last_die->tag == DW_TAG_lexical_block))))
18921 {
18922 nesting_level++;
18923 parent_die = last_die;
18924 continue;
18925 }
18926
18927 /* Otherwise we skip to the next sibling, if any. */
18928 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18929
18930 /* Back to the top, do it again. */
18931 }
18932 }
18933
18934 partial_die_info::partial_die_info (sect_offset sect_off_,
18935 struct abbrev_info *abbrev)
18936 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18937 {
18938 }
18939
18940 /* Read a minimal amount of information into the minimal die structure.
18941 INFO_PTR should point just after the initial uleb128 of a DIE. */
18942
18943 const gdb_byte *
18944 partial_die_info::read (const struct die_reader_specs *reader,
18945 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18946 {
18947 struct dwarf2_cu *cu = reader->cu;
18948 struct dwarf2_per_objfile *dwarf2_per_objfile
18949 = cu->per_cu->dwarf2_per_objfile;
18950 unsigned int i;
18951 int has_low_pc_attr = 0;
18952 int has_high_pc_attr = 0;
18953 int high_pc_relative = 0;
18954
18955 for (i = 0; i < abbrev.num_attrs; ++i)
18956 {
18957 struct attribute attr;
18958
18959 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18960
18961 /* Store the data if it is of an attribute we want to keep in a
18962 partial symbol table. */
18963 switch (attr.name)
18964 {
18965 case DW_AT_name:
18966 switch (tag)
18967 {
18968 case DW_TAG_compile_unit:
18969 case DW_TAG_partial_unit:
18970 case DW_TAG_type_unit:
18971 /* Compilation units have a DW_AT_name that is a filename, not
18972 a source language identifier. */
18973 case DW_TAG_enumeration_type:
18974 case DW_TAG_enumerator:
18975 /* These tags always have simple identifiers already; no need
18976 to canonicalize them. */
18977 name = DW_STRING (&attr);
18978 break;
18979 default:
18980 {
18981 struct objfile *objfile = dwarf2_per_objfile->objfile;
18982
18983 name
18984 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18985 &objfile->per_bfd->storage_obstack);
18986 }
18987 break;
18988 }
18989 break;
18990 case DW_AT_linkage_name:
18991 case DW_AT_MIPS_linkage_name:
18992 /* Note that both forms of linkage name might appear. We
18993 assume they will be the same, and we only store the last
18994 one we see. */
18995 linkage_name = DW_STRING (&attr);
18996 break;
18997 case DW_AT_low_pc:
18998 has_low_pc_attr = 1;
18999 lowpc = attr_value_as_address (&attr);
19000 break;
19001 case DW_AT_high_pc:
19002 has_high_pc_attr = 1;
19003 highpc = attr_value_as_address (&attr);
19004 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
19005 high_pc_relative = 1;
19006 break;
19007 case DW_AT_location:
19008 /* Support the .debug_loc offsets. */
19009 if (attr_form_is_block (&attr))
19010 {
19011 d.locdesc = DW_BLOCK (&attr);
19012 }
19013 else if (attr_form_is_section_offset (&attr))
19014 {
19015 dwarf2_complex_location_expr_complaint ();
19016 }
19017 else
19018 {
19019 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19020 "partial symbol information");
19021 }
19022 break;
19023 case DW_AT_external:
19024 is_external = DW_UNSND (&attr);
19025 break;
19026 case DW_AT_declaration:
19027 is_declaration = DW_UNSND (&attr);
19028 break;
19029 case DW_AT_type:
19030 has_type = 1;
19031 break;
19032 case DW_AT_abstract_origin:
19033 case DW_AT_specification:
19034 case DW_AT_extension:
19035 has_specification = 1;
19036 spec_offset = dwarf2_get_ref_die_offset (&attr);
19037 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19038 || cu->per_cu->is_dwz);
19039 break;
19040 case DW_AT_sibling:
19041 /* Ignore absolute siblings, they might point outside of
19042 the current compile unit. */
19043 if (attr.form == DW_FORM_ref_addr)
19044 complaint (_("ignoring absolute DW_AT_sibling"));
19045 else
19046 {
19047 const gdb_byte *buffer = reader->buffer;
19048 sect_offset off = dwarf2_get_ref_die_offset (&attr);
19049 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19050
19051 if (sibling_ptr < info_ptr)
19052 complaint (_("DW_AT_sibling points backwards"));
19053 else if (sibling_ptr > reader->buffer_end)
19054 dwarf2_section_buffer_overflow_complaint (reader->die_section);
19055 else
19056 sibling = sibling_ptr;
19057 }
19058 break;
19059 case DW_AT_byte_size:
19060 has_byte_size = 1;
19061 break;
19062 case DW_AT_const_value:
19063 has_const_value = 1;
19064 break;
19065 case DW_AT_calling_convention:
19066 /* DWARF doesn't provide a way to identify a program's source-level
19067 entry point. DW_AT_calling_convention attributes are only meant
19068 to describe functions' calling conventions.
19069
19070 However, because it's a necessary piece of information in
19071 Fortran, and before DWARF 4 DW_CC_program was the only
19072 piece of debugging information whose definition refers to
19073 a 'main program' at all, several compilers marked Fortran
19074 main programs with DW_CC_program --- even when those
19075 functions use the standard calling conventions.
19076
19077 Although DWARF now specifies a way to provide this
19078 information, we support this practice for backward
19079 compatibility. */
19080 if (DW_UNSND (&attr) == DW_CC_program
19081 && cu->language == language_fortran)
19082 main_subprogram = 1;
19083 break;
19084 case DW_AT_inline:
19085 if (DW_UNSND (&attr) == DW_INL_inlined
19086 || DW_UNSND (&attr) == DW_INL_declared_inlined)
19087 may_be_inlined = 1;
19088 break;
19089
19090 case DW_AT_import:
19091 if (tag == DW_TAG_imported_unit)
19092 {
19093 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19094 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19095 || cu->per_cu->is_dwz);
19096 }
19097 break;
19098
19099 case DW_AT_main_subprogram:
19100 main_subprogram = DW_UNSND (&attr);
19101 break;
19102
19103 case DW_AT_ranges:
19104 {
19105 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19106 but that requires a full DIE, so instead we just
19107 reimplement it. */
19108 int need_ranges_base = tag != DW_TAG_compile_unit;
19109 unsigned int ranges_offset = (DW_UNSND (&attr)
19110 + (need_ranges_base
19111 ? cu->ranges_base
19112 : 0));
19113
19114 /* Value of the DW_AT_ranges attribute is the offset in the
19115 .debug_ranges section. */
19116 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19117 nullptr))
19118 has_pc_info = 1;
19119 }
19120 break;
19121
19122 default:
19123 break;
19124 }
19125 }
19126
19127 /* For Ada, if both the name and the linkage name appear, we prefer
19128 the latter. This lets "catch exception" work better, regardless
19129 of the order in which the name and linkage name were emitted.
19130 Really, though, this is just a workaround for the fact that gdb
19131 doesn't store both the name and the linkage name. */
19132 if (cu->language == language_ada && linkage_name != nullptr)
19133 name = linkage_name;
19134
19135 if (high_pc_relative)
19136 highpc += lowpc;
19137
19138 if (has_low_pc_attr && has_high_pc_attr)
19139 {
19140 /* When using the GNU linker, .gnu.linkonce. sections are used to
19141 eliminate duplicate copies of functions and vtables and such.
19142 The linker will arbitrarily choose one and discard the others.
19143 The AT_*_pc values for such functions refer to local labels in
19144 these sections. If the section from that file was discarded, the
19145 labels are not in the output, so the relocs get a value of 0.
19146 If this is a discarded function, mark the pc bounds as invalid,
19147 so that GDB will ignore it. */
19148 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
19149 {
19150 struct objfile *objfile = dwarf2_per_objfile->objfile;
19151 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19152
19153 complaint (_("DW_AT_low_pc %s is zero "
19154 "for DIE at %s [in module %s]"),
19155 paddress (gdbarch, lowpc),
19156 sect_offset_str (sect_off),
19157 objfile_name (objfile));
19158 }
19159 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19160 else if (lowpc >= highpc)
19161 {
19162 struct objfile *objfile = dwarf2_per_objfile->objfile;
19163 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19164
19165 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19166 "for DIE at %s [in module %s]"),
19167 paddress (gdbarch, lowpc),
19168 paddress (gdbarch, highpc),
19169 sect_offset_str (sect_off),
19170 objfile_name (objfile));
19171 }
19172 else
19173 has_pc_info = 1;
19174 }
19175
19176 return info_ptr;
19177 }
19178
19179 /* Find a cached partial DIE at OFFSET in CU. */
19180
19181 struct partial_die_info *
19182 dwarf2_cu::find_partial_die (sect_offset sect_off)
19183 {
19184 struct partial_die_info *lookup_die = NULL;
19185 struct partial_die_info part_die (sect_off);
19186
19187 lookup_die = ((struct partial_die_info *)
19188 htab_find_with_hash (partial_dies, &part_die,
19189 to_underlying (sect_off)));
19190
19191 return lookup_die;
19192 }
19193
19194 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19195 except in the case of .debug_types DIEs which do not reference
19196 outside their CU (they do however referencing other types via
19197 DW_FORM_ref_sig8). */
19198
19199 static const struct cu_partial_die_info
19200 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19201 {
19202 struct dwarf2_per_objfile *dwarf2_per_objfile
19203 = cu->per_cu->dwarf2_per_objfile;
19204 struct objfile *objfile = dwarf2_per_objfile->objfile;
19205 struct dwarf2_per_cu_data *per_cu = NULL;
19206 struct partial_die_info *pd = NULL;
19207
19208 if (offset_in_dwz == cu->per_cu->is_dwz
19209 && offset_in_cu_p (&cu->header, sect_off))
19210 {
19211 pd = cu->find_partial_die (sect_off);
19212 if (pd != NULL)
19213 return { cu, pd };
19214 /* We missed recording what we needed.
19215 Load all dies and try again. */
19216 per_cu = cu->per_cu;
19217 }
19218 else
19219 {
19220 /* TUs don't reference other CUs/TUs (except via type signatures). */
19221 if (cu->per_cu->is_debug_types)
19222 {
19223 error (_("Dwarf Error: Type Unit at offset %s contains"
19224 " external reference to offset %s [in module %s].\n"),
19225 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19226 bfd_get_filename (objfile->obfd));
19227 }
19228 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19229 dwarf2_per_objfile);
19230
19231 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19232 load_partial_comp_unit (per_cu);
19233
19234 per_cu->cu->last_used = 0;
19235 pd = per_cu->cu->find_partial_die (sect_off);
19236 }
19237
19238 /* If we didn't find it, and not all dies have been loaded,
19239 load them all and try again. */
19240
19241 if (pd == NULL && per_cu->load_all_dies == 0)
19242 {
19243 per_cu->load_all_dies = 1;
19244
19245 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19246 THIS_CU->cu may already be in use. So we can't just free it and
19247 replace its DIEs with the ones we read in. Instead, we leave those
19248 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19249 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19250 set. */
19251 load_partial_comp_unit (per_cu);
19252
19253 pd = per_cu->cu->find_partial_die (sect_off);
19254 }
19255
19256 if (pd == NULL)
19257 internal_error (__FILE__, __LINE__,
19258 _("could not find partial DIE %s "
19259 "in cache [from module %s]\n"),
19260 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19261 return { per_cu->cu, pd };
19262 }
19263
19264 /* See if we can figure out if the class lives in a namespace. We do
19265 this by looking for a member function; its demangled name will
19266 contain namespace info, if there is any. */
19267
19268 static void
19269 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19270 struct dwarf2_cu *cu)
19271 {
19272 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19273 what template types look like, because the demangler
19274 frequently doesn't give the same name as the debug info. We
19275 could fix this by only using the demangled name to get the
19276 prefix (but see comment in read_structure_type). */
19277
19278 struct partial_die_info *real_pdi;
19279 struct partial_die_info *child_pdi;
19280
19281 /* If this DIE (this DIE's specification, if any) has a parent, then
19282 we should not do this. We'll prepend the parent's fully qualified
19283 name when we create the partial symbol. */
19284
19285 real_pdi = struct_pdi;
19286 while (real_pdi->has_specification)
19287 {
19288 auto res = find_partial_die (real_pdi->spec_offset,
19289 real_pdi->spec_is_dwz, cu);
19290 real_pdi = res.pdi;
19291 cu = res.cu;
19292 }
19293
19294 if (real_pdi->die_parent != NULL)
19295 return;
19296
19297 for (child_pdi = struct_pdi->die_child;
19298 child_pdi != NULL;
19299 child_pdi = child_pdi->die_sibling)
19300 {
19301 if (child_pdi->tag == DW_TAG_subprogram
19302 && child_pdi->linkage_name != NULL)
19303 {
19304 gdb::unique_xmalloc_ptr<char> actual_class_name
19305 (language_class_name_from_physname (cu->language_defn,
19306 child_pdi->linkage_name));
19307 if (actual_class_name != NULL)
19308 {
19309 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19310 struct_pdi->name
19311 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19312 actual_class_name.get ());
19313 }
19314 break;
19315 }
19316 }
19317 }
19318
19319 void
19320 partial_die_info::fixup (struct dwarf2_cu *cu)
19321 {
19322 /* Once we've fixed up a die, there's no point in doing so again.
19323 This also avoids a memory leak if we were to call
19324 guess_partial_die_structure_name multiple times. */
19325 if (fixup_called)
19326 return;
19327
19328 /* If we found a reference attribute and the DIE has no name, try
19329 to find a name in the referred to DIE. */
19330
19331 if (name == NULL && has_specification)
19332 {
19333 struct partial_die_info *spec_die;
19334
19335 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19336 spec_die = res.pdi;
19337 cu = res.cu;
19338
19339 spec_die->fixup (cu);
19340
19341 if (spec_die->name)
19342 {
19343 name = spec_die->name;
19344
19345 /* Copy DW_AT_external attribute if it is set. */
19346 if (spec_die->is_external)
19347 is_external = spec_die->is_external;
19348 }
19349 }
19350
19351 /* Set default names for some unnamed DIEs. */
19352
19353 if (name == NULL && tag == DW_TAG_namespace)
19354 name = CP_ANONYMOUS_NAMESPACE_STR;
19355
19356 /* If there is no parent die to provide a namespace, and there are
19357 children, see if we can determine the namespace from their linkage
19358 name. */
19359 if (cu->language == language_cplus
19360 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
19361 && die_parent == NULL
19362 && has_children
19363 && (tag == DW_TAG_class_type
19364 || tag == DW_TAG_structure_type
19365 || tag == DW_TAG_union_type))
19366 guess_partial_die_structure_name (this, cu);
19367
19368 /* GCC might emit a nameless struct or union that has a linkage
19369 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19370 if (name == NULL
19371 && (tag == DW_TAG_class_type
19372 || tag == DW_TAG_interface_type
19373 || tag == DW_TAG_structure_type
19374 || tag == DW_TAG_union_type)
19375 && linkage_name != NULL)
19376 {
19377 gdb::unique_xmalloc_ptr<char> demangled
19378 (gdb_demangle (linkage_name, DMGL_TYPES));
19379 if (demangled != nullptr)
19380 {
19381 const char *base;
19382
19383 /* Strip any leading namespaces/classes, keep only the base name.
19384 DW_AT_name for named DIEs does not contain the prefixes. */
19385 base = strrchr (demangled.get (), ':');
19386 if (base && base > demangled.get () && base[-1] == ':')
19387 base++;
19388 else
19389 base = demangled.get ();
19390
19391 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19392 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
19393 }
19394 }
19395
19396 fixup_called = 1;
19397 }
19398
19399 /* Read an attribute value described by an attribute form. */
19400
19401 static const gdb_byte *
19402 read_attribute_value (const struct die_reader_specs *reader,
19403 struct attribute *attr, unsigned form,
19404 LONGEST implicit_const, const gdb_byte *info_ptr)
19405 {
19406 struct dwarf2_cu *cu = reader->cu;
19407 struct dwarf2_per_objfile *dwarf2_per_objfile
19408 = cu->per_cu->dwarf2_per_objfile;
19409 struct objfile *objfile = dwarf2_per_objfile->objfile;
19410 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19411 bfd *abfd = reader->abfd;
19412 struct comp_unit_head *cu_header = &cu->header;
19413 unsigned int bytes_read;
19414 struct dwarf_block *blk;
19415
19416 attr->form = (enum dwarf_form) form;
19417 switch (form)
19418 {
19419 case DW_FORM_ref_addr:
19420 if (cu->header.version == 2)
19421 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19422 else
19423 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19424 &cu->header, &bytes_read);
19425 info_ptr += bytes_read;
19426 break;
19427 case DW_FORM_GNU_ref_alt:
19428 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19429 info_ptr += bytes_read;
19430 break;
19431 case DW_FORM_addr:
19432 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19433 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19434 info_ptr += bytes_read;
19435 break;
19436 case DW_FORM_block2:
19437 blk = dwarf_alloc_block (cu);
19438 blk->size = read_2_bytes (abfd, info_ptr);
19439 info_ptr += 2;
19440 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19441 info_ptr += blk->size;
19442 DW_BLOCK (attr) = blk;
19443 break;
19444 case DW_FORM_block4:
19445 blk = dwarf_alloc_block (cu);
19446 blk->size = read_4_bytes (abfd, info_ptr);
19447 info_ptr += 4;
19448 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19449 info_ptr += blk->size;
19450 DW_BLOCK (attr) = blk;
19451 break;
19452 case DW_FORM_data2:
19453 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19454 info_ptr += 2;
19455 break;
19456 case DW_FORM_data4:
19457 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19458 info_ptr += 4;
19459 break;
19460 case DW_FORM_data8:
19461 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19462 info_ptr += 8;
19463 break;
19464 case DW_FORM_data16:
19465 blk = dwarf_alloc_block (cu);
19466 blk->size = 16;
19467 blk->data = read_n_bytes (abfd, info_ptr, 16);
19468 info_ptr += 16;
19469 DW_BLOCK (attr) = blk;
19470 break;
19471 case DW_FORM_sec_offset:
19472 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19473 info_ptr += bytes_read;
19474 break;
19475 case DW_FORM_string:
19476 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19477 DW_STRING_IS_CANONICAL (attr) = 0;
19478 info_ptr += bytes_read;
19479 break;
19480 case DW_FORM_strp:
19481 if (!cu->per_cu->is_dwz)
19482 {
19483 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19484 abfd, info_ptr, cu_header,
19485 &bytes_read);
19486 DW_STRING_IS_CANONICAL (attr) = 0;
19487 info_ptr += bytes_read;
19488 break;
19489 }
19490 /* FALLTHROUGH */
19491 case DW_FORM_line_strp:
19492 if (!cu->per_cu->is_dwz)
19493 {
19494 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19495 abfd, info_ptr,
19496 cu_header, &bytes_read);
19497 DW_STRING_IS_CANONICAL (attr) = 0;
19498 info_ptr += bytes_read;
19499 break;
19500 }
19501 /* FALLTHROUGH */
19502 case DW_FORM_GNU_strp_alt:
19503 {
19504 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19505 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19506 &bytes_read);
19507
19508 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19509 dwz, str_offset);
19510 DW_STRING_IS_CANONICAL (attr) = 0;
19511 info_ptr += bytes_read;
19512 }
19513 break;
19514 case DW_FORM_exprloc:
19515 case DW_FORM_block:
19516 blk = dwarf_alloc_block (cu);
19517 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19518 info_ptr += bytes_read;
19519 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19520 info_ptr += blk->size;
19521 DW_BLOCK (attr) = blk;
19522 break;
19523 case DW_FORM_block1:
19524 blk = dwarf_alloc_block (cu);
19525 blk->size = read_1_byte (abfd, info_ptr);
19526 info_ptr += 1;
19527 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19528 info_ptr += blk->size;
19529 DW_BLOCK (attr) = blk;
19530 break;
19531 case DW_FORM_data1:
19532 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19533 info_ptr += 1;
19534 break;
19535 case DW_FORM_flag:
19536 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19537 info_ptr += 1;
19538 break;
19539 case DW_FORM_flag_present:
19540 DW_UNSND (attr) = 1;
19541 break;
19542 case DW_FORM_sdata:
19543 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19544 info_ptr += bytes_read;
19545 break;
19546 case DW_FORM_udata:
19547 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19548 info_ptr += bytes_read;
19549 break;
19550 case DW_FORM_ref1:
19551 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19552 + read_1_byte (abfd, info_ptr));
19553 info_ptr += 1;
19554 break;
19555 case DW_FORM_ref2:
19556 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19557 + read_2_bytes (abfd, info_ptr));
19558 info_ptr += 2;
19559 break;
19560 case DW_FORM_ref4:
19561 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19562 + read_4_bytes (abfd, info_ptr));
19563 info_ptr += 4;
19564 break;
19565 case DW_FORM_ref8:
19566 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19567 + read_8_bytes (abfd, info_ptr));
19568 info_ptr += 8;
19569 break;
19570 case DW_FORM_ref_sig8:
19571 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19572 info_ptr += 8;
19573 break;
19574 case DW_FORM_ref_udata:
19575 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19576 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19577 info_ptr += bytes_read;
19578 break;
19579 case DW_FORM_indirect:
19580 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19581 info_ptr += bytes_read;
19582 if (form == DW_FORM_implicit_const)
19583 {
19584 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19585 info_ptr += bytes_read;
19586 }
19587 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19588 info_ptr);
19589 break;
19590 case DW_FORM_implicit_const:
19591 DW_SND (attr) = implicit_const;
19592 break;
19593 case DW_FORM_addrx:
19594 case DW_FORM_GNU_addr_index:
19595 if (reader->dwo_file == NULL)
19596 {
19597 /* For now flag a hard error.
19598 Later we can turn this into a complaint. */
19599 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19600 dwarf_form_name (form),
19601 bfd_get_filename (abfd));
19602 }
19603 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19604 info_ptr += bytes_read;
19605 break;
19606 case DW_FORM_strx:
19607 case DW_FORM_strx1:
19608 case DW_FORM_strx2:
19609 case DW_FORM_strx3:
19610 case DW_FORM_strx4:
19611 case DW_FORM_GNU_str_index:
19612 if (reader->dwo_file == NULL)
19613 {
19614 /* For now flag a hard error.
19615 Later we can turn this into a complaint if warranted. */
19616 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19617 dwarf_form_name (form),
19618 bfd_get_filename (abfd));
19619 }
19620 {
19621 ULONGEST str_index;
19622 if (form == DW_FORM_strx1)
19623 {
19624 str_index = read_1_byte (abfd, info_ptr);
19625 info_ptr += 1;
19626 }
19627 else if (form == DW_FORM_strx2)
19628 {
19629 str_index = read_2_bytes (abfd, info_ptr);
19630 info_ptr += 2;
19631 }
19632 else if (form == DW_FORM_strx3)
19633 {
19634 str_index = read_3_bytes (abfd, info_ptr);
19635 info_ptr += 3;
19636 }
19637 else if (form == DW_FORM_strx4)
19638 {
19639 str_index = read_4_bytes (abfd, info_ptr);
19640 info_ptr += 4;
19641 }
19642 else
19643 {
19644 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19645 info_ptr += bytes_read;
19646 }
19647 DW_STRING (attr) = read_str_index (reader, str_index);
19648 DW_STRING_IS_CANONICAL (attr) = 0;
19649 }
19650 break;
19651 default:
19652 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19653 dwarf_form_name (form),
19654 bfd_get_filename (abfd));
19655 }
19656
19657 /* Super hack. */
19658 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19659 attr->form = DW_FORM_GNU_ref_alt;
19660
19661 /* We have seen instances where the compiler tried to emit a byte
19662 size attribute of -1 which ended up being encoded as an unsigned
19663 0xffffffff. Although 0xffffffff is technically a valid size value,
19664 an object of this size seems pretty unlikely so we can relatively
19665 safely treat these cases as if the size attribute was invalid and
19666 treat them as zero by default. */
19667 if (attr->name == DW_AT_byte_size
19668 && form == DW_FORM_data4
19669 && DW_UNSND (attr) >= 0xffffffff)
19670 {
19671 complaint
19672 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19673 hex_string (DW_UNSND (attr)));
19674 DW_UNSND (attr) = 0;
19675 }
19676
19677 return info_ptr;
19678 }
19679
19680 /* Read an attribute described by an abbreviated attribute. */
19681
19682 static const gdb_byte *
19683 read_attribute (const struct die_reader_specs *reader,
19684 struct attribute *attr, struct attr_abbrev *abbrev,
19685 const gdb_byte *info_ptr)
19686 {
19687 attr->name = abbrev->name;
19688 return read_attribute_value (reader, attr, abbrev->form,
19689 abbrev->implicit_const, info_ptr);
19690 }
19691
19692 /* Read dwarf information from a buffer. */
19693
19694 static unsigned int
19695 read_1_byte (bfd *abfd, const gdb_byte *buf)
19696 {
19697 return bfd_get_8 (abfd, buf);
19698 }
19699
19700 static int
19701 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19702 {
19703 return bfd_get_signed_8 (abfd, buf);
19704 }
19705
19706 static unsigned int
19707 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19708 {
19709 return bfd_get_16 (abfd, buf);
19710 }
19711
19712 static int
19713 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19714 {
19715 return bfd_get_signed_16 (abfd, buf);
19716 }
19717
19718 static unsigned int
19719 read_3_bytes (bfd *abfd, const gdb_byte *buf)
19720 {
19721 unsigned int result = 0;
19722 for (int i = 0; i < 3; ++i)
19723 {
19724 unsigned char byte = bfd_get_8 (abfd, buf);
19725 buf++;
19726 result |= ((unsigned int) byte << (i * 8));
19727 }
19728 return result;
19729 }
19730
19731 static unsigned int
19732 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19733 {
19734 return bfd_get_32 (abfd, buf);
19735 }
19736
19737 static int
19738 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19739 {
19740 return bfd_get_signed_32 (abfd, buf);
19741 }
19742
19743 static ULONGEST
19744 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19745 {
19746 return bfd_get_64 (abfd, buf);
19747 }
19748
19749 static CORE_ADDR
19750 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19751 unsigned int *bytes_read)
19752 {
19753 struct comp_unit_head *cu_header = &cu->header;
19754 CORE_ADDR retval = 0;
19755
19756 if (cu_header->signed_addr_p)
19757 {
19758 switch (cu_header->addr_size)
19759 {
19760 case 2:
19761 retval = bfd_get_signed_16 (abfd, buf);
19762 break;
19763 case 4:
19764 retval = bfd_get_signed_32 (abfd, buf);
19765 break;
19766 case 8:
19767 retval = bfd_get_signed_64 (abfd, buf);
19768 break;
19769 default:
19770 internal_error (__FILE__, __LINE__,
19771 _("read_address: bad switch, signed [in module %s]"),
19772 bfd_get_filename (abfd));
19773 }
19774 }
19775 else
19776 {
19777 switch (cu_header->addr_size)
19778 {
19779 case 2:
19780 retval = bfd_get_16 (abfd, buf);
19781 break;
19782 case 4:
19783 retval = bfd_get_32 (abfd, buf);
19784 break;
19785 case 8:
19786 retval = bfd_get_64 (abfd, buf);
19787 break;
19788 default:
19789 internal_error (__FILE__, __LINE__,
19790 _("read_address: bad switch, "
19791 "unsigned [in module %s]"),
19792 bfd_get_filename (abfd));
19793 }
19794 }
19795
19796 *bytes_read = cu_header->addr_size;
19797 return retval;
19798 }
19799
19800 /* Read the initial length from a section. The (draft) DWARF 3
19801 specification allows the initial length to take up either 4 bytes
19802 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19803 bytes describe the length and all offsets will be 8 bytes in length
19804 instead of 4.
19805
19806 An older, non-standard 64-bit format is also handled by this
19807 function. The older format in question stores the initial length
19808 as an 8-byte quantity without an escape value. Lengths greater
19809 than 2^32 aren't very common which means that the initial 4 bytes
19810 is almost always zero. Since a length value of zero doesn't make
19811 sense for the 32-bit format, this initial zero can be considered to
19812 be an escape value which indicates the presence of the older 64-bit
19813 format. As written, the code can't detect (old format) lengths
19814 greater than 4GB. If it becomes necessary to handle lengths
19815 somewhat larger than 4GB, we could allow other small values (such
19816 as the non-sensical values of 1, 2, and 3) to also be used as
19817 escape values indicating the presence of the old format.
19818
19819 The value returned via bytes_read should be used to increment the
19820 relevant pointer after calling read_initial_length().
19821
19822 [ Note: read_initial_length() and read_offset() are based on the
19823 document entitled "DWARF Debugging Information Format", revision
19824 3, draft 8, dated November 19, 2001. This document was obtained
19825 from:
19826
19827 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19828
19829 This document is only a draft and is subject to change. (So beware.)
19830
19831 Details regarding the older, non-standard 64-bit format were
19832 determined empirically by examining 64-bit ELF files produced by
19833 the SGI toolchain on an IRIX 6.5 machine.
19834
19835 - Kevin, July 16, 2002
19836 ] */
19837
19838 static LONGEST
19839 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19840 {
19841 LONGEST length = bfd_get_32 (abfd, buf);
19842
19843 if (length == 0xffffffff)
19844 {
19845 length = bfd_get_64 (abfd, buf + 4);
19846 *bytes_read = 12;
19847 }
19848 else if (length == 0)
19849 {
19850 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19851 length = bfd_get_64 (abfd, buf);
19852 *bytes_read = 8;
19853 }
19854 else
19855 {
19856 *bytes_read = 4;
19857 }
19858
19859 return length;
19860 }
19861
19862 /* Cover function for read_initial_length.
19863 Returns the length of the object at BUF, and stores the size of the
19864 initial length in *BYTES_READ and stores the size that offsets will be in
19865 *OFFSET_SIZE.
19866 If the initial length size is not equivalent to that specified in
19867 CU_HEADER then issue a complaint.
19868 This is useful when reading non-comp-unit headers. */
19869
19870 static LONGEST
19871 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19872 const struct comp_unit_head *cu_header,
19873 unsigned int *bytes_read,
19874 unsigned int *offset_size)
19875 {
19876 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19877
19878 gdb_assert (cu_header->initial_length_size == 4
19879 || cu_header->initial_length_size == 8
19880 || cu_header->initial_length_size == 12);
19881
19882 if (cu_header->initial_length_size != *bytes_read)
19883 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19884
19885 *offset_size = (*bytes_read == 4) ? 4 : 8;
19886 return length;
19887 }
19888
19889 /* Read an offset from the data stream. The size of the offset is
19890 given by cu_header->offset_size. */
19891
19892 static LONGEST
19893 read_offset (bfd *abfd, const gdb_byte *buf,
19894 const struct comp_unit_head *cu_header,
19895 unsigned int *bytes_read)
19896 {
19897 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19898
19899 *bytes_read = cu_header->offset_size;
19900 return offset;
19901 }
19902
19903 /* Read an offset from the data stream. */
19904
19905 static LONGEST
19906 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19907 {
19908 LONGEST retval = 0;
19909
19910 switch (offset_size)
19911 {
19912 case 4:
19913 retval = bfd_get_32 (abfd, buf);
19914 break;
19915 case 8:
19916 retval = bfd_get_64 (abfd, buf);
19917 break;
19918 default:
19919 internal_error (__FILE__, __LINE__,
19920 _("read_offset_1: bad switch [in module %s]"),
19921 bfd_get_filename (abfd));
19922 }
19923
19924 return retval;
19925 }
19926
19927 static const gdb_byte *
19928 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19929 {
19930 /* If the size of a host char is 8 bits, we can return a pointer
19931 to the buffer, otherwise we have to copy the data to a buffer
19932 allocated on the temporary obstack. */
19933 gdb_assert (HOST_CHAR_BIT == 8);
19934 return buf;
19935 }
19936
19937 static const char *
19938 read_direct_string (bfd *abfd, const gdb_byte *buf,
19939 unsigned int *bytes_read_ptr)
19940 {
19941 /* If the size of a host char is 8 bits, we can return a pointer
19942 to the string, otherwise we have to copy the string to a buffer
19943 allocated on the temporary obstack. */
19944 gdb_assert (HOST_CHAR_BIT == 8);
19945 if (*buf == '\0')
19946 {
19947 *bytes_read_ptr = 1;
19948 return NULL;
19949 }
19950 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19951 return (const char *) buf;
19952 }
19953
19954 /* Return pointer to string at section SECT offset STR_OFFSET with error
19955 reporting strings FORM_NAME and SECT_NAME. */
19956
19957 static const char *
19958 read_indirect_string_at_offset_from (struct objfile *objfile,
19959 bfd *abfd, LONGEST str_offset,
19960 struct dwarf2_section_info *sect,
19961 const char *form_name,
19962 const char *sect_name)
19963 {
19964 dwarf2_read_section (objfile, sect);
19965 if (sect->buffer == NULL)
19966 error (_("%s used without %s section [in module %s]"),
19967 form_name, sect_name, bfd_get_filename (abfd));
19968 if (str_offset >= sect->size)
19969 error (_("%s pointing outside of %s section [in module %s]"),
19970 form_name, sect_name, bfd_get_filename (abfd));
19971 gdb_assert (HOST_CHAR_BIT == 8);
19972 if (sect->buffer[str_offset] == '\0')
19973 return NULL;
19974 return (const char *) (sect->buffer + str_offset);
19975 }
19976
19977 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19978
19979 static const char *
19980 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19981 bfd *abfd, LONGEST str_offset)
19982 {
19983 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19984 abfd, str_offset,
19985 &dwarf2_per_objfile->str,
19986 "DW_FORM_strp", ".debug_str");
19987 }
19988
19989 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19990
19991 static const char *
19992 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19993 bfd *abfd, LONGEST str_offset)
19994 {
19995 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19996 abfd, str_offset,
19997 &dwarf2_per_objfile->line_str,
19998 "DW_FORM_line_strp",
19999 ".debug_line_str");
20000 }
20001
20002 /* Read a string at offset STR_OFFSET in the .debug_str section from
20003 the .dwz file DWZ. Throw an error if the offset is too large. If
20004 the string consists of a single NUL byte, return NULL; otherwise
20005 return a pointer to the string. */
20006
20007 static const char *
20008 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
20009 LONGEST str_offset)
20010 {
20011 dwarf2_read_section (objfile, &dwz->str);
20012
20013 if (dwz->str.buffer == NULL)
20014 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
20015 "section [in module %s]"),
20016 bfd_get_filename (dwz->dwz_bfd.get ()));
20017 if (str_offset >= dwz->str.size)
20018 error (_("DW_FORM_GNU_strp_alt pointing outside of "
20019 ".debug_str section [in module %s]"),
20020 bfd_get_filename (dwz->dwz_bfd.get ()));
20021 gdb_assert (HOST_CHAR_BIT == 8);
20022 if (dwz->str.buffer[str_offset] == '\0')
20023 return NULL;
20024 return (const char *) (dwz->str.buffer + str_offset);
20025 }
20026
20027 /* Return pointer to string at .debug_str offset as read from BUF.
20028 BUF is assumed to be in a compilation unit described by CU_HEADER.
20029 Return *BYTES_READ_PTR count of bytes read from BUF. */
20030
20031 static const char *
20032 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
20033 const gdb_byte *buf,
20034 const struct comp_unit_head *cu_header,
20035 unsigned int *bytes_read_ptr)
20036 {
20037 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20038
20039 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
20040 }
20041
20042 /* Return pointer to string at .debug_line_str offset as read from BUF.
20043 BUF is assumed to be in a compilation unit described by CU_HEADER.
20044 Return *BYTES_READ_PTR count of bytes read from BUF. */
20045
20046 static const char *
20047 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
20048 bfd *abfd, const gdb_byte *buf,
20049 const struct comp_unit_head *cu_header,
20050 unsigned int *bytes_read_ptr)
20051 {
20052 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20053
20054 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
20055 str_offset);
20056 }
20057
20058 ULONGEST
20059 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
20060 unsigned int *bytes_read_ptr)
20061 {
20062 ULONGEST result;
20063 unsigned int num_read;
20064 int shift;
20065 unsigned char byte;
20066
20067 result = 0;
20068 shift = 0;
20069 num_read = 0;
20070 while (1)
20071 {
20072 byte = bfd_get_8 (abfd, buf);
20073 buf++;
20074 num_read++;
20075 result |= ((ULONGEST) (byte & 127) << shift);
20076 if ((byte & 128) == 0)
20077 {
20078 break;
20079 }
20080 shift += 7;
20081 }
20082 *bytes_read_ptr = num_read;
20083 return result;
20084 }
20085
20086 static LONGEST
20087 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
20088 unsigned int *bytes_read_ptr)
20089 {
20090 ULONGEST result;
20091 int shift, num_read;
20092 unsigned char byte;
20093
20094 result = 0;
20095 shift = 0;
20096 num_read = 0;
20097 while (1)
20098 {
20099 byte = bfd_get_8 (abfd, buf);
20100 buf++;
20101 num_read++;
20102 result |= ((ULONGEST) (byte & 127) << shift);
20103 shift += 7;
20104 if ((byte & 128) == 0)
20105 {
20106 break;
20107 }
20108 }
20109 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
20110 result |= -(((ULONGEST) 1) << shift);
20111 *bytes_read_ptr = num_read;
20112 return result;
20113 }
20114
20115 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20116 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
20117 ADDR_SIZE is the size of addresses from the CU header. */
20118
20119 static CORE_ADDR
20120 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20121 unsigned int addr_index, ULONGEST addr_base, int addr_size)
20122 {
20123 struct objfile *objfile = dwarf2_per_objfile->objfile;
20124 bfd *abfd = objfile->obfd;
20125 const gdb_byte *info_ptr;
20126
20127 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20128 if (dwarf2_per_objfile->addr.buffer == NULL)
20129 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20130 objfile_name (objfile));
20131 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
20132 error (_("DW_FORM_addr_index pointing outside of "
20133 ".debug_addr section [in module %s]"),
20134 objfile_name (objfile));
20135 info_ptr = (dwarf2_per_objfile->addr.buffer
20136 + addr_base + addr_index * addr_size);
20137 if (addr_size == 4)
20138 return bfd_get_32 (abfd, info_ptr);
20139 else
20140 return bfd_get_64 (abfd, info_ptr);
20141 }
20142
20143 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20144
20145 static CORE_ADDR
20146 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20147 {
20148 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20149 cu->addr_base, cu->header.addr_size);
20150 }
20151
20152 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20153
20154 static CORE_ADDR
20155 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20156 unsigned int *bytes_read)
20157 {
20158 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
20159 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20160
20161 return read_addr_index (cu, addr_index);
20162 }
20163
20164 /* Data structure to pass results from dwarf2_read_addr_index_reader
20165 back to dwarf2_read_addr_index. */
20166
20167 struct dwarf2_read_addr_index_data
20168 {
20169 ULONGEST addr_base;
20170 int addr_size;
20171 };
20172
20173 /* die_reader_func for dwarf2_read_addr_index. */
20174
20175 static void
20176 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
20177 const gdb_byte *info_ptr,
20178 struct die_info *comp_unit_die,
20179 int has_children,
20180 void *data)
20181 {
20182 struct dwarf2_cu *cu = reader->cu;
20183 struct dwarf2_read_addr_index_data *aidata =
20184 (struct dwarf2_read_addr_index_data *) data;
20185
20186 aidata->addr_base = cu->addr_base;
20187 aidata->addr_size = cu->header.addr_size;
20188 }
20189
20190 /* Given an index in .debug_addr, fetch the value.
20191 NOTE: This can be called during dwarf expression evaluation,
20192 long after the debug information has been read, and thus per_cu->cu
20193 may no longer exist. */
20194
20195 CORE_ADDR
20196 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20197 unsigned int addr_index)
20198 {
20199 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
20200 struct dwarf2_cu *cu = per_cu->cu;
20201 ULONGEST addr_base;
20202 int addr_size;
20203
20204 /* We need addr_base and addr_size.
20205 If we don't have PER_CU->cu, we have to get it.
20206 Nasty, but the alternative is storing the needed info in PER_CU,
20207 which at this point doesn't seem justified: it's not clear how frequently
20208 it would get used and it would increase the size of every PER_CU.
20209 Entry points like dwarf2_per_cu_addr_size do a similar thing
20210 so we're not in uncharted territory here.
20211 Alas we need to be a bit more complicated as addr_base is contained
20212 in the DIE.
20213
20214 We don't need to read the entire CU(/TU).
20215 We just need the header and top level die.
20216
20217 IWBN to use the aging mechanism to let us lazily later discard the CU.
20218 For now we skip this optimization. */
20219
20220 if (cu != NULL)
20221 {
20222 addr_base = cu->addr_base;
20223 addr_size = cu->header.addr_size;
20224 }
20225 else
20226 {
20227 struct dwarf2_read_addr_index_data aidata;
20228
20229 /* Note: We can't use init_cutu_and_read_dies_simple here,
20230 we need addr_base. */
20231 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
20232 dwarf2_read_addr_index_reader, &aidata);
20233 addr_base = aidata.addr_base;
20234 addr_size = aidata.addr_size;
20235 }
20236
20237 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20238 addr_size);
20239 }
20240
20241 /* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
20242 This is only used by the Fission support. */
20243
20244 static const char *
20245 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20246 {
20247 struct dwarf2_cu *cu = reader->cu;
20248 struct dwarf2_per_objfile *dwarf2_per_objfile
20249 = cu->per_cu->dwarf2_per_objfile;
20250 struct objfile *objfile = dwarf2_per_objfile->objfile;
20251 const char *objf_name = objfile_name (objfile);
20252 bfd *abfd = objfile->obfd;
20253 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20254 struct dwarf2_section_info *str_offsets_section =
20255 &reader->dwo_file->sections.str_offsets;
20256 const gdb_byte *info_ptr;
20257 ULONGEST str_offset;
20258 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20259
20260 dwarf2_read_section (objfile, str_section);
20261 dwarf2_read_section (objfile, str_offsets_section);
20262 if (str_section->buffer == NULL)
20263 error (_("%s used without .debug_str.dwo section"
20264 " in CU at offset %s [in module %s]"),
20265 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20266 if (str_offsets_section->buffer == NULL)
20267 error (_("%s used without .debug_str_offsets.dwo section"
20268 " in CU at offset %s [in module %s]"),
20269 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20270 if (str_index * cu->header.offset_size >= str_offsets_section->size)
20271 error (_("%s pointing outside of .debug_str_offsets.dwo"
20272 " section in CU at offset %s [in module %s]"),
20273 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20274 info_ptr = (str_offsets_section->buffer
20275 + str_index * cu->header.offset_size);
20276 if (cu->header.offset_size == 4)
20277 str_offset = bfd_get_32 (abfd, info_ptr);
20278 else
20279 str_offset = bfd_get_64 (abfd, info_ptr);
20280 if (str_offset >= str_section->size)
20281 error (_("Offset from %s pointing outside of"
20282 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20283 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20284 return (const char *) (str_section->buffer + str_offset);
20285 }
20286
20287 /* Return the length of an LEB128 number in BUF. */
20288
20289 static int
20290 leb128_size (const gdb_byte *buf)
20291 {
20292 const gdb_byte *begin = buf;
20293 gdb_byte byte;
20294
20295 while (1)
20296 {
20297 byte = *buf++;
20298 if ((byte & 128) == 0)
20299 return buf - begin;
20300 }
20301 }
20302
20303 static void
20304 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20305 {
20306 switch (lang)
20307 {
20308 case DW_LANG_C89:
20309 case DW_LANG_C99:
20310 case DW_LANG_C11:
20311 case DW_LANG_C:
20312 case DW_LANG_UPC:
20313 cu->language = language_c;
20314 break;
20315 case DW_LANG_Java:
20316 case DW_LANG_C_plus_plus:
20317 case DW_LANG_C_plus_plus_11:
20318 case DW_LANG_C_plus_plus_14:
20319 cu->language = language_cplus;
20320 break;
20321 case DW_LANG_D:
20322 cu->language = language_d;
20323 break;
20324 case DW_LANG_Fortran77:
20325 case DW_LANG_Fortran90:
20326 case DW_LANG_Fortran95:
20327 case DW_LANG_Fortran03:
20328 case DW_LANG_Fortran08:
20329 cu->language = language_fortran;
20330 break;
20331 case DW_LANG_Go:
20332 cu->language = language_go;
20333 break;
20334 case DW_LANG_Mips_Assembler:
20335 cu->language = language_asm;
20336 break;
20337 case DW_LANG_Ada83:
20338 case DW_LANG_Ada95:
20339 cu->language = language_ada;
20340 break;
20341 case DW_LANG_Modula2:
20342 cu->language = language_m2;
20343 break;
20344 case DW_LANG_Pascal83:
20345 cu->language = language_pascal;
20346 break;
20347 case DW_LANG_ObjC:
20348 cu->language = language_objc;
20349 break;
20350 case DW_LANG_Rust:
20351 case DW_LANG_Rust_old:
20352 cu->language = language_rust;
20353 break;
20354 case DW_LANG_Cobol74:
20355 case DW_LANG_Cobol85:
20356 default:
20357 cu->language = language_minimal;
20358 break;
20359 }
20360 cu->language_defn = language_def (cu->language);
20361 }
20362
20363 /* Return the named attribute or NULL if not there. */
20364
20365 static struct attribute *
20366 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20367 {
20368 for (;;)
20369 {
20370 unsigned int i;
20371 struct attribute *spec = NULL;
20372
20373 for (i = 0; i < die->num_attrs; ++i)
20374 {
20375 if (die->attrs[i].name == name)
20376 return &die->attrs[i];
20377 if (die->attrs[i].name == DW_AT_specification
20378 || die->attrs[i].name == DW_AT_abstract_origin)
20379 spec = &die->attrs[i];
20380 }
20381
20382 if (!spec)
20383 break;
20384
20385 die = follow_die_ref (die, spec, &cu);
20386 }
20387
20388 return NULL;
20389 }
20390
20391 /* Return the named attribute or NULL if not there,
20392 but do not follow DW_AT_specification, etc.
20393 This is for use in contexts where we're reading .debug_types dies.
20394 Following DW_AT_specification, DW_AT_abstract_origin will take us
20395 back up the chain, and we want to go down. */
20396
20397 static struct attribute *
20398 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20399 {
20400 unsigned int i;
20401
20402 for (i = 0; i < die->num_attrs; ++i)
20403 if (die->attrs[i].name == name)
20404 return &die->attrs[i];
20405
20406 return NULL;
20407 }
20408
20409 /* Return the string associated with a string-typed attribute, or NULL if it
20410 is either not found or is of an incorrect type. */
20411
20412 static const char *
20413 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20414 {
20415 struct attribute *attr;
20416 const char *str = NULL;
20417
20418 attr = dwarf2_attr (die, name, cu);
20419
20420 if (attr != NULL)
20421 {
20422 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20423 || attr->form == DW_FORM_string
20424 || attr->form == DW_FORM_strx
20425 || attr->form == DW_FORM_strx1
20426 || attr->form == DW_FORM_strx2
20427 || attr->form == DW_FORM_strx3
20428 || attr->form == DW_FORM_strx4
20429 || attr->form == DW_FORM_GNU_str_index
20430 || attr->form == DW_FORM_GNU_strp_alt)
20431 str = DW_STRING (attr);
20432 else
20433 complaint (_("string type expected for attribute %s for "
20434 "DIE at %s in module %s"),
20435 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20436 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20437 }
20438
20439 return str;
20440 }
20441
20442 /* Return the dwo name or NULL if not present. If present, it is in either
20443 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20444 static const char *
20445 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20446 {
20447 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20448 if (dwo_name == nullptr)
20449 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20450 return dwo_name;
20451 }
20452
20453 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20454 and holds a non-zero value. This function should only be used for
20455 DW_FORM_flag or DW_FORM_flag_present attributes. */
20456
20457 static int
20458 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20459 {
20460 struct attribute *attr = dwarf2_attr (die, name, cu);
20461
20462 return (attr && DW_UNSND (attr));
20463 }
20464
20465 static int
20466 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20467 {
20468 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20469 which value is non-zero. However, we have to be careful with
20470 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20471 (via dwarf2_flag_true_p) follows this attribute. So we may
20472 end up accidently finding a declaration attribute that belongs
20473 to a different DIE referenced by the specification attribute,
20474 even though the given DIE does not have a declaration attribute. */
20475 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20476 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20477 }
20478
20479 /* Return the die giving the specification for DIE, if there is
20480 one. *SPEC_CU is the CU containing DIE on input, and the CU
20481 containing the return value on output. If there is no
20482 specification, but there is an abstract origin, that is
20483 returned. */
20484
20485 static struct die_info *
20486 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20487 {
20488 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20489 *spec_cu);
20490
20491 if (spec_attr == NULL)
20492 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20493
20494 if (spec_attr == NULL)
20495 return NULL;
20496 else
20497 return follow_die_ref (die, spec_attr, spec_cu);
20498 }
20499
20500 /* Stub for free_line_header to match void * callback types. */
20501
20502 static void
20503 free_line_header_voidp (void *arg)
20504 {
20505 struct line_header *lh = (struct line_header *) arg;
20506
20507 delete lh;
20508 }
20509
20510 void
20511 line_header::add_include_dir (const char *include_dir)
20512 {
20513 if (dwarf_line_debug >= 2)
20514 {
20515 size_t new_size;
20516 if (version >= 5)
20517 new_size = m_include_dirs.size ();
20518 else
20519 new_size = m_include_dirs.size () + 1;
20520 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20521 new_size, include_dir);
20522 }
20523 m_include_dirs.push_back (include_dir);
20524 }
20525
20526 void
20527 line_header::add_file_name (const char *name,
20528 dir_index d_index,
20529 unsigned int mod_time,
20530 unsigned int length)
20531 {
20532 if (dwarf_line_debug >= 2)
20533 {
20534 size_t new_size;
20535 if (version >= 5)
20536 new_size = file_names_size ();
20537 else
20538 new_size = file_names_size () + 1;
20539 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20540 new_size, name);
20541 }
20542 m_file_names.emplace_back (name, d_index, mod_time, length);
20543 }
20544
20545 /* A convenience function to find the proper .debug_line section for a CU. */
20546
20547 static struct dwarf2_section_info *
20548 get_debug_line_section (struct dwarf2_cu *cu)
20549 {
20550 struct dwarf2_section_info *section;
20551 struct dwarf2_per_objfile *dwarf2_per_objfile
20552 = cu->per_cu->dwarf2_per_objfile;
20553
20554 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20555 DWO file. */
20556 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20557 section = &cu->dwo_unit->dwo_file->sections.line;
20558 else if (cu->per_cu->is_dwz)
20559 {
20560 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20561
20562 section = &dwz->line;
20563 }
20564 else
20565 section = &dwarf2_per_objfile->line;
20566
20567 return section;
20568 }
20569
20570 /* Read directory or file name entry format, starting with byte of
20571 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20572 entries count and the entries themselves in the described entry
20573 format. */
20574
20575 static void
20576 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20577 bfd *abfd, const gdb_byte **bufp,
20578 struct line_header *lh,
20579 const struct comp_unit_head *cu_header,
20580 void (*callback) (struct line_header *lh,
20581 const char *name,
20582 dir_index d_index,
20583 unsigned int mod_time,
20584 unsigned int length))
20585 {
20586 gdb_byte format_count, formati;
20587 ULONGEST data_count, datai;
20588 const gdb_byte *buf = *bufp;
20589 const gdb_byte *format_header_data;
20590 unsigned int bytes_read;
20591
20592 format_count = read_1_byte (abfd, buf);
20593 buf += 1;
20594 format_header_data = buf;
20595 for (formati = 0; formati < format_count; formati++)
20596 {
20597 read_unsigned_leb128 (abfd, buf, &bytes_read);
20598 buf += bytes_read;
20599 read_unsigned_leb128 (abfd, buf, &bytes_read);
20600 buf += bytes_read;
20601 }
20602
20603 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20604 buf += bytes_read;
20605 for (datai = 0; datai < data_count; datai++)
20606 {
20607 const gdb_byte *format = format_header_data;
20608 struct file_entry fe;
20609
20610 for (formati = 0; formati < format_count; formati++)
20611 {
20612 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20613 format += bytes_read;
20614
20615 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20616 format += bytes_read;
20617
20618 gdb::optional<const char *> string;
20619 gdb::optional<unsigned int> uint;
20620
20621 switch (form)
20622 {
20623 case DW_FORM_string:
20624 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20625 buf += bytes_read;
20626 break;
20627
20628 case DW_FORM_line_strp:
20629 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20630 abfd, buf,
20631 cu_header,
20632 &bytes_read));
20633 buf += bytes_read;
20634 break;
20635
20636 case DW_FORM_data1:
20637 uint.emplace (read_1_byte (abfd, buf));
20638 buf += 1;
20639 break;
20640
20641 case DW_FORM_data2:
20642 uint.emplace (read_2_bytes (abfd, buf));
20643 buf += 2;
20644 break;
20645
20646 case DW_FORM_data4:
20647 uint.emplace (read_4_bytes (abfd, buf));
20648 buf += 4;
20649 break;
20650
20651 case DW_FORM_data8:
20652 uint.emplace (read_8_bytes (abfd, buf));
20653 buf += 8;
20654 break;
20655
20656 case DW_FORM_data16:
20657 /* This is used for MD5, but file_entry does not record MD5s. */
20658 buf += 16;
20659 break;
20660
20661 case DW_FORM_udata:
20662 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20663 buf += bytes_read;
20664 break;
20665
20666 case DW_FORM_block:
20667 /* It is valid only for DW_LNCT_timestamp which is ignored by
20668 current GDB. */
20669 break;
20670 }
20671
20672 switch (content_type)
20673 {
20674 case DW_LNCT_path:
20675 if (string.has_value ())
20676 fe.name = *string;
20677 break;
20678 case DW_LNCT_directory_index:
20679 if (uint.has_value ())
20680 fe.d_index = (dir_index) *uint;
20681 break;
20682 case DW_LNCT_timestamp:
20683 if (uint.has_value ())
20684 fe.mod_time = *uint;
20685 break;
20686 case DW_LNCT_size:
20687 if (uint.has_value ())
20688 fe.length = *uint;
20689 break;
20690 case DW_LNCT_MD5:
20691 break;
20692 default:
20693 complaint (_("Unknown format content type %s"),
20694 pulongest (content_type));
20695 }
20696 }
20697
20698 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20699 }
20700
20701 *bufp = buf;
20702 }
20703
20704 /* Read the statement program header starting at OFFSET in
20705 .debug_line, or .debug_line.dwo. Return a pointer
20706 to a struct line_header, allocated using xmalloc.
20707 Returns NULL if there is a problem reading the header, e.g., if it
20708 has a version we don't understand.
20709
20710 NOTE: the strings in the include directory and file name tables of
20711 the returned object point into the dwarf line section buffer,
20712 and must not be freed. */
20713
20714 static line_header_up
20715 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20716 {
20717 const gdb_byte *line_ptr;
20718 unsigned int bytes_read, offset_size;
20719 int i;
20720 const char *cur_dir, *cur_file;
20721 struct dwarf2_section_info *section;
20722 bfd *abfd;
20723 struct dwarf2_per_objfile *dwarf2_per_objfile
20724 = cu->per_cu->dwarf2_per_objfile;
20725
20726 section = get_debug_line_section (cu);
20727 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20728 if (section->buffer == NULL)
20729 {
20730 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20731 complaint (_("missing .debug_line.dwo section"));
20732 else
20733 complaint (_("missing .debug_line section"));
20734 return 0;
20735 }
20736
20737 /* We can't do this until we know the section is non-empty.
20738 Only then do we know we have such a section. */
20739 abfd = get_section_bfd_owner (section);
20740
20741 /* Make sure that at least there's room for the total_length field.
20742 That could be 12 bytes long, but we're just going to fudge that. */
20743 if (to_underlying (sect_off) + 4 >= section->size)
20744 {
20745 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20746 return 0;
20747 }
20748
20749 line_header_up lh (new line_header ());
20750
20751 lh->sect_off = sect_off;
20752 lh->offset_in_dwz = cu->per_cu->is_dwz;
20753
20754 line_ptr = section->buffer + to_underlying (sect_off);
20755
20756 /* Read in the header. */
20757 lh->total_length =
20758 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20759 &bytes_read, &offset_size);
20760 line_ptr += bytes_read;
20761
20762 const gdb_byte *start_here = line_ptr;
20763
20764 if (line_ptr + lh->total_length > (section->buffer + section->size))
20765 {
20766 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20767 return 0;
20768 }
20769 lh->statement_program_end = start_here + lh->total_length;
20770 lh->version = read_2_bytes (abfd, line_ptr);
20771 line_ptr += 2;
20772 if (lh->version > 5)
20773 {
20774 /* This is a version we don't understand. The format could have
20775 changed in ways we don't handle properly so just punt. */
20776 complaint (_("unsupported version in .debug_line section"));
20777 return NULL;
20778 }
20779 if (lh->version >= 5)
20780 {
20781 gdb_byte segment_selector_size;
20782
20783 /* Skip address size. */
20784 read_1_byte (abfd, line_ptr);
20785 line_ptr += 1;
20786
20787 segment_selector_size = read_1_byte (abfd, line_ptr);
20788 line_ptr += 1;
20789 if (segment_selector_size != 0)
20790 {
20791 complaint (_("unsupported segment selector size %u "
20792 "in .debug_line section"),
20793 segment_selector_size);
20794 return NULL;
20795 }
20796 }
20797 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20798 line_ptr += offset_size;
20799 lh->statement_program_start = line_ptr + lh->header_length;
20800 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20801 line_ptr += 1;
20802 if (lh->version >= 4)
20803 {
20804 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20805 line_ptr += 1;
20806 }
20807 else
20808 lh->maximum_ops_per_instruction = 1;
20809
20810 if (lh->maximum_ops_per_instruction == 0)
20811 {
20812 lh->maximum_ops_per_instruction = 1;
20813 complaint (_("invalid maximum_ops_per_instruction "
20814 "in `.debug_line' section"));
20815 }
20816
20817 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20818 line_ptr += 1;
20819 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20820 line_ptr += 1;
20821 lh->line_range = read_1_byte (abfd, line_ptr);
20822 line_ptr += 1;
20823 lh->opcode_base = read_1_byte (abfd, line_ptr);
20824 line_ptr += 1;
20825 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20826
20827 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20828 for (i = 1; i < lh->opcode_base; ++i)
20829 {
20830 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20831 line_ptr += 1;
20832 }
20833
20834 if (lh->version >= 5)
20835 {
20836 /* Read directory table. */
20837 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20838 &cu->header,
20839 [] (struct line_header *header, const char *name,
20840 dir_index d_index, unsigned int mod_time,
20841 unsigned int length)
20842 {
20843 header->add_include_dir (name);
20844 });
20845
20846 /* Read file name table. */
20847 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20848 &cu->header,
20849 [] (struct line_header *header, const char *name,
20850 dir_index d_index, unsigned int mod_time,
20851 unsigned int length)
20852 {
20853 header->add_file_name (name, d_index, mod_time, length);
20854 });
20855 }
20856 else
20857 {
20858 /* Read directory table. */
20859 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20860 {
20861 line_ptr += bytes_read;
20862 lh->add_include_dir (cur_dir);
20863 }
20864 line_ptr += bytes_read;
20865
20866 /* Read file name table. */
20867 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20868 {
20869 unsigned int mod_time, length;
20870 dir_index d_index;
20871
20872 line_ptr += bytes_read;
20873 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20874 line_ptr += bytes_read;
20875 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20876 line_ptr += bytes_read;
20877 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20878 line_ptr += bytes_read;
20879
20880 lh->add_file_name (cur_file, d_index, mod_time, length);
20881 }
20882 line_ptr += bytes_read;
20883 }
20884
20885 if (line_ptr > (section->buffer + section->size))
20886 complaint (_("line number info header doesn't "
20887 "fit in `.debug_line' section"));
20888
20889 return lh;
20890 }
20891
20892 /* Subroutine of dwarf_decode_lines to simplify it.
20893 Return the file name of the psymtab for the given file_entry.
20894 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20895 If space for the result is malloc'd, *NAME_HOLDER will be set.
20896 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20897
20898 static const char *
20899 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20900 const struct partial_symtab *pst,
20901 const char *comp_dir,
20902 gdb::unique_xmalloc_ptr<char> *name_holder)
20903 {
20904 const char *include_name = fe.name;
20905 const char *include_name_to_compare = include_name;
20906 const char *pst_filename;
20907 int file_is_pst;
20908
20909 const char *dir_name = fe.include_dir (lh);
20910
20911 gdb::unique_xmalloc_ptr<char> hold_compare;
20912 if (!IS_ABSOLUTE_PATH (include_name)
20913 && (dir_name != NULL || comp_dir != NULL))
20914 {
20915 /* Avoid creating a duplicate psymtab for PST.
20916 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20917 Before we do the comparison, however, we need to account
20918 for DIR_NAME and COMP_DIR.
20919 First prepend dir_name (if non-NULL). If we still don't
20920 have an absolute path prepend comp_dir (if non-NULL).
20921 However, the directory we record in the include-file's
20922 psymtab does not contain COMP_DIR (to match the
20923 corresponding symtab(s)).
20924
20925 Example:
20926
20927 bash$ cd /tmp
20928 bash$ gcc -g ./hello.c
20929 include_name = "hello.c"
20930 dir_name = "."
20931 DW_AT_comp_dir = comp_dir = "/tmp"
20932 DW_AT_name = "./hello.c"
20933
20934 */
20935
20936 if (dir_name != NULL)
20937 {
20938 name_holder->reset (concat (dir_name, SLASH_STRING,
20939 include_name, (char *) NULL));
20940 include_name = name_holder->get ();
20941 include_name_to_compare = include_name;
20942 }
20943 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20944 {
20945 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20946 include_name, (char *) NULL));
20947 include_name_to_compare = hold_compare.get ();
20948 }
20949 }
20950
20951 pst_filename = pst->filename;
20952 gdb::unique_xmalloc_ptr<char> copied_name;
20953 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20954 {
20955 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20956 pst_filename, (char *) NULL));
20957 pst_filename = copied_name.get ();
20958 }
20959
20960 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20961
20962 if (file_is_pst)
20963 return NULL;
20964 return include_name;
20965 }
20966
20967 /* State machine to track the state of the line number program. */
20968
20969 class lnp_state_machine
20970 {
20971 public:
20972 /* Initialize a machine state for the start of a line number
20973 program. */
20974 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20975 bool record_lines_p);
20976
20977 file_entry *current_file ()
20978 {
20979 /* lh->file_names is 0-based, but the file name numbers in the
20980 statement program are 1-based. */
20981 return m_line_header->file_name_at (m_file);
20982 }
20983
20984 /* Record the line in the state machine. END_SEQUENCE is true if
20985 we're processing the end of a sequence. */
20986 void record_line (bool end_sequence);
20987
20988 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20989 nop-out rest of the lines in this sequence. */
20990 void check_line_address (struct dwarf2_cu *cu,
20991 const gdb_byte *line_ptr,
20992 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20993
20994 void handle_set_discriminator (unsigned int discriminator)
20995 {
20996 m_discriminator = discriminator;
20997 m_line_has_non_zero_discriminator |= discriminator != 0;
20998 }
20999
21000 /* Handle DW_LNE_set_address. */
21001 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21002 {
21003 m_op_index = 0;
21004 address += baseaddr;
21005 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21006 }
21007
21008 /* Handle DW_LNS_advance_pc. */
21009 void handle_advance_pc (CORE_ADDR adjust);
21010
21011 /* Handle a special opcode. */
21012 void handle_special_opcode (unsigned char op_code);
21013
21014 /* Handle DW_LNS_advance_line. */
21015 void handle_advance_line (int line_delta)
21016 {
21017 advance_line (line_delta);
21018 }
21019
21020 /* Handle DW_LNS_set_file. */
21021 void handle_set_file (file_name_index file);
21022
21023 /* Handle DW_LNS_negate_stmt. */
21024 void handle_negate_stmt ()
21025 {
21026 m_is_stmt = !m_is_stmt;
21027 }
21028
21029 /* Handle DW_LNS_const_add_pc. */
21030 void handle_const_add_pc ();
21031
21032 /* Handle DW_LNS_fixed_advance_pc. */
21033 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21034 {
21035 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21036 m_op_index = 0;
21037 }
21038
21039 /* Handle DW_LNS_copy. */
21040 void handle_copy ()
21041 {
21042 record_line (false);
21043 m_discriminator = 0;
21044 }
21045
21046 /* Handle DW_LNE_end_sequence. */
21047 void handle_end_sequence ()
21048 {
21049 m_currently_recording_lines = true;
21050 }
21051
21052 private:
21053 /* Advance the line by LINE_DELTA. */
21054 void advance_line (int line_delta)
21055 {
21056 m_line += line_delta;
21057
21058 if (line_delta != 0)
21059 m_line_has_non_zero_discriminator = m_discriminator != 0;
21060 }
21061
21062 struct dwarf2_cu *m_cu;
21063
21064 gdbarch *m_gdbarch;
21065
21066 /* True if we're recording lines.
21067 Otherwise we're building partial symtabs and are just interested in
21068 finding include files mentioned by the line number program. */
21069 bool m_record_lines_p;
21070
21071 /* The line number header. */
21072 line_header *m_line_header;
21073
21074 /* These are part of the standard DWARF line number state machine,
21075 and initialized according to the DWARF spec. */
21076
21077 unsigned char m_op_index = 0;
21078 /* The line table index of the current file. */
21079 file_name_index m_file = 1;
21080 unsigned int m_line = 1;
21081
21082 /* These are initialized in the constructor. */
21083
21084 CORE_ADDR m_address;
21085 bool m_is_stmt;
21086 unsigned int m_discriminator;
21087
21088 /* Additional bits of state we need to track. */
21089
21090 /* The last file that we called dwarf2_start_subfile for.
21091 This is only used for TLLs. */
21092 unsigned int m_last_file = 0;
21093 /* The last file a line number was recorded for. */
21094 struct subfile *m_last_subfile = NULL;
21095
21096 /* When true, record the lines we decode. */
21097 bool m_currently_recording_lines = false;
21098
21099 /* The last line number that was recorded, used to coalesce
21100 consecutive entries for the same line. This can happen, for
21101 example, when discriminators are present. PR 17276. */
21102 unsigned int m_last_line = 0;
21103 bool m_line_has_non_zero_discriminator = false;
21104 };
21105
21106 void
21107 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21108 {
21109 CORE_ADDR addr_adj = (((m_op_index + adjust)
21110 / m_line_header->maximum_ops_per_instruction)
21111 * m_line_header->minimum_instruction_length);
21112 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21113 m_op_index = ((m_op_index + adjust)
21114 % m_line_header->maximum_ops_per_instruction);
21115 }
21116
21117 void
21118 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21119 {
21120 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21121 CORE_ADDR addr_adj = (((m_op_index
21122 + (adj_opcode / m_line_header->line_range))
21123 / m_line_header->maximum_ops_per_instruction)
21124 * m_line_header->minimum_instruction_length);
21125 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21126 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
21127 % m_line_header->maximum_ops_per_instruction);
21128
21129 int line_delta = (m_line_header->line_base
21130 + (adj_opcode % m_line_header->line_range));
21131 advance_line (line_delta);
21132 record_line (false);
21133 m_discriminator = 0;
21134 }
21135
21136 void
21137 lnp_state_machine::handle_set_file (file_name_index file)
21138 {
21139 m_file = file;
21140
21141 const file_entry *fe = current_file ();
21142 if (fe == NULL)
21143 dwarf2_debug_line_missing_file_complaint ();
21144 else if (m_record_lines_p)
21145 {
21146 const char *dir = fe->include_dir (m_line_header);
21147
21148 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21149 m_line_has_non_zero_discriminator = m_discriminator != 0;
21150 dwarf2_start_subfile (m_cu, fe->name, dir);
21151 }
21152 }
21153
21154 void
21155 lnp_state_machine::handle_const_add_pc ()
21156 {
21157 CORE_ADDR adjust
21158 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21159
21160 CORE_ADDR addr_adj
21161 = (((m_op_index + adjust)
21162 / m_line_header->maximum_ops_per_instruction)
21163 * m_line_header->minimum_instruction_length);
21164
21165 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21166 m_op_index = ((m_op_index + adjust)
21167 % m_line_header->maximum_ops_per_instruction);
21168 }
21169
21170 /* Return non-zero if we should add LINE to the line number table.
21171 LINE is the line to add, LAST_LINE is the last line that was added,
21172 LAST_SUBFILE is the subfile for LAST_LINE.
21173 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21174 had a non-zero discriminator.
21175
21176 We have to be careful in the presence of discriminators.
21177 E.g., for this line:
21178
21179 for (i = 0; i < 100000; i++);
21180
21181 clang can emit four line number entries for that one line,
21182 each with a different discriminator.
21183 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21184
21185 However, we want gdb to coalesce all four entries into one.
21186 Otherwise the user could stepi into the middle of the line and
21187 gdb would get confused about whether the pc really was in the
21188 middle of the line.
21189
21190 Things are further complicated by the fact that two consecutive
21191 line number entries for the same line is a heuristic used by gcc
21192 to denote the end of the prologue. So we can't just discard duplicate
21193 entries, we have to be selective about it. The heuristic we use is
21194 that we only collapse consecutive entries for the same line if at least
21195 one of those entries has a non-zero discriminator. PR 17276.
21196
21197 Note: Addresses in the line number state machine can never go backwards
21198 within one sequence, thus this coalescing is ok. */
21199
21200 static int
21201 dwarf_record_line_p (struct dwarf2_cu *cu,
21202 unsigned int line, unsigned int last_line,
21203 int line_has_non_zero_discriminator,
21204 struct subfile *last_subfile)
21205 {
21206 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21207 return 1;
21208 if (line != last_line)
21209 return 1;
21210 /* Same line for the same file that we've seen already.
21211 As a last check, for pr 17276, only record the line if the line
21212 has never had a non-zero discriminator. */
21213 if (!line_has_non_zero_discriminator)
21214 return 1;
21215 return 0;
21216 }
21217
21218 /* Use the CU's builder to record line number LINE beginning at
21219 address ADDRESS in the line table of subfile SUBFILE. */
21220
21221 static void
21222 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21223 unsigned int line, CORE_ADDR address,
21224 struct dwarf2_cu *cu)
21225 {
21226 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21227
21228 if (dwarf_line_debug)
21229 {
21230 fprintf_unfiltered (gdb_stdlog,
21231 "Recording line %u, file %s, address %s\n",
21232 line, lbasename (subfile->name),
21233 paddress (gdbarch, address));
21234 }
21235
21236 if (cu != nullptr)
21237 cu->get_builder ()->record_line (subfile, line, addr);
21238 }
21239
21240 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21241 Mark the end of a set of line number records.
21242 The arguments are the same as for dwarf_record_line_1.
21243 If SUBFILE is NULL the request is ignored. */
21244
21245 static void
21246 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21247 CORE_ADDR address, struct dwarf2_cu *cu)
21248 {
21249 if (subfile == NULL)
21250 return;
21251
21252 if (dwarf_line_debug)
21253 {
21254 fprintf_unfiltered (gdb_stdlog,
21255 "Finishing current line, file %s, address %s\n",
21256 lbasename (subfile->name),
21257 paddress (gdbarch, address));
21258 }
21259
21260 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
21261 }
21262
21263 void
21264 lnp_state_machine::record_line (bool end_sequence)
21265 {
21266 if (dwarf_line_debug)
21267 {
21268 fprintf_unfiltered (gdb_stdlog,
21269 "Processing actual line %u: file %u,"
21270 " address %s, is_stmt %u, discrim %u\n",
21271 m_line, m_file,
21272 paddress (m_gdbarch, m_address),
21273 m_is_stmt, m_discriminator);
21274 }
21275
21276 file_entry *fe = current_file ();
21277
21278 if (fe == NULL)
21279 dwarf2_debug_line_missing_file_complaint ();
21280 /* For now we ignore lines not starting on an instruction boundary.
21281 But not when processing end_sequence for compatibility with the
21282 previous version of the code. */
21283 else if (m_op_index == 0 || end_sequence)
21284 {
21285 fe->included_p = 1;
21286 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
21287 {
21288 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
21289 || end_sequence)
21290 {
21291 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21292 m_currently_recording_lines ? m_cu : nullptr);
21293 }
21294
21295 if (!end_sequence)
21296 {
21297 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21298 m_line_has_non_zero_discriminator,
21299 m_last_subfile))
21300 {
21301 buildsym_compunit *builder = m_cu->get_builder ();
21302 dwarf_record_line_1 (m_gdbarch,
21303 builder->get_current_subfile (),
21304 m_line, m_address,
21305 m_currently_recording_lines ? m_cu : nullptr);
21306 }
21307 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21308 m_last_line = m_line;
21309 }
21310 }
21311 }
21312 }
21313
21314 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21315 line_header *lh, bool record_lines_p)
21316 {
21317 m_cu = cu;
21318 m_gdbarch = arch;
21319 m_record_lines_p = record_lines_p;
21320 m_line_header = lh;
21321
21322 m_currently_recording_lines = true;
21323
21324 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21325 was a line entry for it so that the backend has a chance to adjust it
21326 and also record it in case it needs it. This is currently used by MIPS
21327 code, cf. `mips_adjust_dwarf2_line'. */
21328 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21329 m_is_stmt = lh->default_is_stmt;
21330 m_discriminator = 0;
21331 }
21332
21333 void
21334 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21335 const gdb_byte *line_ptr,
21336 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21337 {
21338 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21339 the pc range of the CU. However, we restrict the test to only ADDRESS
21340 values of zero to preserve GDB's previous behaviour which is to handle
21341 the specific case of a function being GC'd by the linker. */
21342
21343 if (address == 0 && address < unrelocated_lowpc)
21344 {
21345 /* This line table is for a function which has been
21346 GCd by the linker. Ignore it. PR gdb/12528 */
21347
21348 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21349 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21350
21351 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21352 line_offset, objfile_name (objfile));
21353 m_currently_recording_lines = false;
21354 /* Note: m_currently_recording_lines is left as false until we see
21355 DW_LNE_end_sequence. */
21356 }
21357 }
21358
21359 /* Subroutine of dwarf_decode_lines to simplify it.
21360 Process the line number information in LH.
21361 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21362 program in order to set included_p for every referenced header. */
21363
21364 static void
21365 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21366 const int decode_for_pst_p, CORE_ADDR lowpc)
21367 {
21368 const gdb_byte *line_ptr, *extended_end;
21369 const gdb_byte *line_end;
21370 unsigned int bytes_read, extended_len;
21371 unsigned char op_code, extended_op;
21372 CORE_ADDR baseaddr;
21373 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21374 bfd *abfd = objfile->obfd;
21375 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21376 /* True if we're recording line info (as opposed to building partial
21377 symtabs and just interested in finding include files mentioned by
21378 the line number program). */
21379 bool record_lines_p = !decode_for_pst_p;
21380
21381 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
21382
21383 line_ptr = lh->statement_program_start;
21384 line_end = lh->statement_program_end;
21385
21386 /* Read the statement sequences until there's nothing left. */
21387 while (line_ptr < line_end)
21388 {
21389 /* The DWARF line number program state machine. Reset the state
21390 machine at the start of each sequence. */
21391 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21392 bool end_sequence = false;
21393
21394 if (record_lines_p)
21395 {
21396 /* Start a subfile for the current file of the state
21397 machine. */
21398 const file_entry *fe = state_machine.current_file ();
21399
21400 if (fe != NULL)
21401 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21402 }
21403
21404 /* Decode the table. */
21405 while (line_ptr < line_end && !end_sequence)
21406 {
21407 op_code = read_1_byte (abfd, line_ptr);
21408 line_ptr += 1;
21409
21410 if (op_code >= lh->opcode_base)
21411 {
21412 /* Special opcode. */
21413 state_machine.handle_special_opcode (op_code);
21414 }
21415 else switch (op_code)
21416 {
21417 case DW_LNS_extended_op:
21418 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21419 &bytes_read);
21420 line_ptr += bytes_read;
21421 extended_end = line_ptr + extended_len;
21422 extended_op = read_1_byte (abfd, line_ptr);
21423 line_ptr += 1;
21424 switch (extended_op)
21425 {
21426 case DW_LNE_end_sequence:
21427 state_machine.handle_end_sequence ();
21428 end_sequence = true;
21429 break;
21430 case DW_LNE_set_address:
21431 {
21432 CORE_ADDR address
21433 = read_address (abfd, line_ptr, cu, &bytes_read);
21434 line_ptr += bytes_read;
21435
21436 state_machine.check_line_address (cu, line_ptr,
21437 lowpc - baseaddr, address);
21438 state_machine.handle_set_address (baseaddr, address);
21439 }
21440 break;
21441 case DW_LNE_define_file:
21442 {
21443 const char *cur_file;
21444 unsigned int mod_time, length;
21445 dir_index dindex;
21446
21447 cur_file = read_direct_string (abfd, line_ptr,
21448 &bytes_read);
21449 line_ptr += bytes_read;
21450 dindex = (dir_index)
21451 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21452 line_ptr += bytes_read;
21453 mod_time =
21454 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21455 line_ptr += bytes_read;
21456 length =
21457 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21458 line_ptr += bytes_read;
21459 lh->add_file_name (cur_file, dindex, mod_time, length);
21460 }
21461 break;
21462 case DW_LNE_set_discriminator:
21463 {
21464 /* The discriminator is not interesting to the
21465 debugger; just ignore it. We still need to
21466 check its value though:
21467 if there are consecutive entries for the same
21468 (non-prologue) line we want to coalesce them.
21469 PR 17276. */
21470 unsigned int discr
21471 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21472 line_ptr += bytes_read;
21473
21474 state_machine.handle_set_discriminator (discr);
21475 }
21476 break;
21477 default:
21478 complaint (_("mangled .debug_line section"));
21479 return;
21480 }
21481 /* Make sure that we parsed the extended op correctly. If e.g.
21482 we expected a different address size than the producer used,
21483 we may have read the wrong number of bytes. */
21484 if (line_ptr != extended_end)
21485 {
21486 complaint (_("mangled .debug_line section"));
21487 return;
21488 }
21489 break;
21490 case DW_LNS_copy:
21491 state_machine.handle_copy ();
21492 break;
21493 case DW_LNS_advance_pc:
21494 {
21495 CORE_ADDR adjust
21496 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21497 line_ptr += bytes_read;
21498
21499 state_machine.handle_advance_pc (adjust);
21500 }
21501 break;
21502 case DW_LNS_advance_line:
21503 {
21504 int line_delta
21505 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21506 line_ptr += bytes_read;
21507
21508 state_machine.handle_advance_line (line_delta);
21509 }
21510 break;
21511 case DW_LNS_set_file:
21512 {
21513 file_name_index file
21514 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21515 &bytes_read);
21516 line_ptr += bytes_read;
21517
21518 state_machine.handle_set_file (file);
21519 }
21520 break;
21521 case DW_LNS_set_column:
21522 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21523 line_ptr += bytes_read;
21524 break;
21525 case DW_LNS_negate_stmt:
21526 state_machine.handle_negate_stmt ();
21527 break;
21528 case DW_LNS_set_basic_block:
21529 break;
21530 /* Add to the address register of the state machine the
21531 address increment value corresponding to special opcode
21532 255. I.e., this value is scaled by the minimum
21533 instruction length since special opcode 255 would have
21534 scaled the increment. */
21535 case DW_LNS_const_add_pc:
21536 state_machine.handle_const_add_pc ();
21537 break;
21538 case DW_LNS_fixed_advance_pc:
21539 {
21540 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21541 line_ptr += 2;
21542
21543 state_machine.handle_fixed_advance_pc (addr_adj);
21544 }
21545 break;
21546 default:
21547 {
21548 /* Unknown standard opcode, ignore it. */
21549 int i;
21550
21551 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21552 {
21553 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21554 line_ptr += bytes_read;
21555 }
21556 }
21557 }
21558 }
21559
21560 if (!end_sequence)
21561 dwarf2_debug_line_missing_end_sequence_complaint ();
21562
21563 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21564 in which case we still finish recording the last line). */
21565 state_machine.record_line (true);
21566 }
21567 }
21568
21569 /* Decode the Line Number Program (LNP) for the given line_header
21570 structure and CU. The actual information extracted and the type
21571 of structures created from the LNP depends on the value of PST.
21572
21573 1. If PST is NULL, then this procedure uses the data from the program
21574 to create all necessary symbol tables, and their linetables.
21575
21576 2. If PST is not NULL, this procedure reads the program to determine
21577 the list of files included by the unit represented by PST, and
21578 builds all the associated partial symbol tables.
21579
21580 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21581 It is used for relative paths in the line table.
21582 NOTE: When processing partial symtabs (pst != NULL),
21583 comp_dir == pst->dirname.
21584
21585 NOTE: It is important that psymtabs have the same file name (via strcmp)
21586 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21587 symtab we don't use it in the name of the psymtabs we create.
21588 E.g. expand_line_sal requires this when finding psymtabs to expand.
21589 A good testcase for this is mb-inline.exp.
21590
21591 LOWPC is the lowest address in CU (or 0 if not known).
21592
21593 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21594 for its PC<->lines mapping information. Otherwise only the filename
21595 table is read in. */
21596
21597 static void
21598 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21599 struct dwarf2_cu *cu, struct partial_symtab *pst,
21600 CORE_ADDR lowpc, int decode_mapping)
21601 {
21602 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21603 const int decode_for_pst_p = (pst != NULL);
21604
21605 if (decode_mapping)
21606 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21607
21608 if (decode_for_pst_p)
21609 {
21610 /* Now that we're done scanning the Line Header Program, we can
21611 create the psymtab of each included file. */
21612 for (auto &file_entry : lh->file_names ())
21613 if (file_entry.included_p == 1)
21614 {
21615 gdb::unique_xmalloc_ptr<char> name_holder;
21616 const char *include_name =
21617 psymtab_include_file_name (lh, file_entry, pst,
21618 comp_dir, &name_holder);
21619 if (include_name != NULL)
21620 dwarf2_create_include_psymtab (include_name, pst, objfile);
21621 }
21622 }
21623 else
21624 {
21625 /* Make sure a symtab is created for every file, even files
21626 which contain only variables (i.e. no code with associated
21627 line numbers). */
21628 buildsym_compunit *builder = cu->get_builder ();
21629 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21630
21631 for (auto &fe : lh->file_names ())
21632 {
21633 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21634 if (builder->get_current_subfile ()->symtab == NULL)
21635 {
21636 builder->get_current_subfile ()->symtab
21637 = allocate_symtab (cust,
21638 builder->get_current_subfile ()->name);
21639 }
21640 fe.symtab = builder->get_current_subfile ()->symtab;
21641 }
21642 }
21643 }
21644
21645 /* Start a subfile for DWARF. FILENAME is the name of the file and
21646 DIRNAME the name of the source directory which contains FILENAME
21647 or NULL if not known.
21648 This routine tries to keep line numbers from identical absolute and
21649 relative file names in a common subfile.
21650
21651 Using the `list' example from the GDB testsuite, which resides in
21652 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21653 of /srcdir/list0.c yields the following debugging information for list0.c:
21654
21655 DW_AT_name: /srcdir/list0.c
21656 DW_AT_comp_dir: /compdir
21657 files.files[0].name: list0.h
21658 files.files[0].dir: /srcdir
21659 files.files[1].name: list0.c
21660 files.files[1].dir: /srcdir
21661
21662 The line number information for list0.c has to end up in a single
21663 subfile, so that `break /srcdir/list0.c:1' works as expected.
21664 start_subfile will ensure that this happens provided that we pass the
21665 concatenation of files.files[1].dir and files.files[1].name as the
21666 subfile's name. */
21667
21668 static void
21669 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21670 const char *dirname)
21671 {
21672 gdb::unique_xmalloc_ptr<char> copy;
21673
21674 /* In order not to lose the line information directory,
21675 we concatenate it to the filename when it makes sense.
21676 Note that the Dwarf3 standard says (speaking of filenames in line
21677 information): ``The directory index is ignored for file names
21678 that represent full path names''. Thus ignoring dirname in the
21679 `else' branch below isn't an issue. */
21680
21681 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21682 {
21683 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21684 filename = copy.get ();
21685 }
21686
21687 cu->get_builder ()->start_subfile (filename);
21688 }
21689
21690 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21691 buildsym_compunit constructor. */
21692
21693 struct compunit_symtab *
21694 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21695 CORE_ADDR low_pc)
21696 {
21697 gdb_assert (m_builder == nullptr);
21698
21699 m_builder.reset (new struct buildsym_compunit
21700 (per_cu->dwarf2_per_objfile->objfile,
21701 name, comp_dir, language, low_pc));
21702
21703 list_in_scope = get_builder ()->get_file_symbols ();
21704
21705 get_builder ()->record_debugformat ("DWARF 2");
21706 get_builder ()->record_producer (producer);
21707
21708 processing_has_namespace_info = false;
21709
21710 return get_builder ()->get_compunit_symtab ();
21711 }
21712
21713 static void
21714 var_decode_location (struct attribute *attr, struct symbol *sym,
21715 struct dwarf2_cu *cu)
21716 {
21717 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21718 struct comp_unit_head *cu_header = &cu->header;
21719
21720 /* NOTE drow/2003-01-30: There used to be a comment and some special
21721 code here to turn a symbol with DW_AT_external and a
21722 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21723 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21724 with some versions of binutils) where shared libraries could have
21725 relocations against symbols in their debug information - the
21726 minimal symbol would have the right address, but the debug info
21727 would not. It's no longer necessary, because we will explicitly
21728 apply relocations when we read in the debug information now. */
21729
21730 /* A DW_AT_location attribute with no contents indicates that a
21731 variable has been optimized away. */
21732 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21733 {
21734 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21735 return;
21736 }
21737
21738 /* Handle one degenerate form of location expression specially, to
21739 preserve GDB's previous behavior when section offsets are
21740 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21741 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21742
21743 if (attr_form_is_block (attr)
21744 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21745 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21746 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21747 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21748 && (DW_BLOCK (attr)->size
21749 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21750 {
21751 unsigned int dummy;
21752
21753 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21754 SET_SYMBOL_VALUE_ADDRESS (sym,
21755 read_address (objfile->obfd,
21756 DW_BLOCK (attr)->data + 1,
21757 cu, &dummy));
21758 else
21759 SET_SYMBOL_VALUE_ADDRESS
21760 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21761 &dummy));
21762 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21763 fixup_symbol_section (sym, objfile);
21764 SET_SYMBOL_VALUE_ADDRESS
21765 (sym,
21766 SYMBOL_VALUE_ADDRESS (sym)
21767 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
21768 return;
21769 }
21770
21771 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21772 expression evaluator, and use LOC_COMPUTED only when necessary
21773 (i.e. when the value of a register or memory location is
21774 referenced, or a thread-local block, etc.). Then again, it might
21775 not be worthwhile. I'm assuming that it isn't unless performance
21776 or memory numbers show me otherwise. */
21777
21778 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21779
21780 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21781 cu->has_loclist = true;
21782 }
21783
21784 /* Given a pointer to a DWARF information entry, figure out if we need
21785 to make a symbol table entry for it, and if so, create a new entry
21786 and return a pointer to it.
21787 If TYPE is NULL, determine symbol type from the die, otherwise
21788 used the passed type.
21789 If SPACE is not NULL, use it to hold the new symbol. If it is
21790 NULL, allocate a new symbol on the objfile's obstack. */
21791
21792 static struct symbol *
21793 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21794 struct symbol *space)
21795 {
21796 struct dwarf2_per_objfile *dwarf2_per_objfile
21797 = cu->per_cu->dwarf2_per_objfile;
21798 struct objfile *objfile = dwarf2_per_objfile->objfile;
21799 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21800 struct symbol *sym = NULL;
21801 const char *name;
21802 struct attribute *attr = NULL;
21803 struct attribute *attr2 = NULL;
21804 CORE_ADDR baseaddr;
21805 struct pending **list_to_add = NULL;
21806
21807 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21808
21809 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
21810
21811 name = dwarf2_name (die, cu);
21812 if (name)
21813 {
21814 const char *linkagename;
21815 int suppress_add = 0;
21816
21817 if (space)
21818 sym = space;
21819 else
21820 sym = allocate_symbol (objfile);
21821 OBJSTAT (objfile, n_syms++);
21822
21823 /* Cache this symbol's name and the name's demangled form (if any). */
21824 sym->set_language (cu->language, &objfile->objfile_obstack);
21825 linkagename = dwarf2_physname (name, die, cu);
21826 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
21827
21828 /* Fortran does not have mangling standard and the mangling does differ
21829 between gfortran, iFort etc. */
21830 if (cu->language == language_fortran
21831 && symbol_get_demangled_name (sym) == NULL)
21832 symbol_set_demangled_name (sym,
21833 dwarf2_full_name (name, die, cu),
21834 NULL);
21835
21836 /* Default assumptions.
21837 Use the passed type or decode it from the die. */
21838 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21839 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21840 if (type != NULL)
21841 SYMBOL_TYPE (sym) = type;
21842 else
21843 SYMBOL_TYPE (sym) = die_type (die, cu);
21844 attr = dwarf2_attr (die,
21845 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21846 cu);
21847 if (attr != nullptr)
21848 {
21849 SYMBOL_LINE (sym) = DW_UNSND (attr);
21850 }
21851
21852 attr = dwarf2_attr (die,
21853 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21854 cu);
21855 if (attr != nullptr)
21856 {
21857 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21858 struct file_entry *fe;
21859
21860 if (cu->line_header != NULL)
21861 fe = cu->line_header->file_name_at (file_index);
21862 else
21863 fe = NULL;
21864
21865 if (fe == NULL)
21866 complaint (_("file index out of range"));
21867 else
21868 symbol_set_symtab (sym, fe->symtab);
21869 }
21870
21871 switch (die->tag)
21872 {
21873 case DW_TAG_label:
21874 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21875 if (attr != nullptr)
21876 {
21877 CORE_ADDR addr;
21878
21879 addr = attr_value_as_address (attr);
21880 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21881 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21882 }
21883 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21884 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21885 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21886 add_symbol_to_list (sym, cu->list_in_scope);
21887 break;
21888 case DW_TAG_subprogram:
21889 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21890 finish_block. */
21891 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21892 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21893 if ((attr2 && (DW_UNSND (attr2) != 0))
21894 || cu->language == language_ada
21895 || cu->language == language_fortran)
21896 {
21897 /* Subprograms marked external are stored as a global symbol.
21898 Ada and Fortran subprograms, whether marked external or
21899 not, are always stored as a global symbol, because we want
21900 to be able to access them globally. For instance, we want
21901 to be able to break on a nested subprogram without having
21902 to specify the context. */
21903 list_to_add = cu->get_builder ()->get_global_symbols ();
21904 }
21905 else
21906 {
21907 list_to_add = cu->list_in_scope;
21908 }
21909 break;
21910 case DW_TAG_inlined_subroutine:
21911 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21912 finish_block. */
21913 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21914 SYMBOL_INLINED (sym) = 1;
21915 list_to_add = cu->list_in_scope;
21916 break;
21917 case DW_TAG_template_value_param:
21918 suppress_add = 1;
21919 /* Fall through. */
21920 case DW_TAG_constant:
21921 case DW_TAG_variable:
21922 case DW_TAG_member:
21923 /* Compilation with minimal debug info may result in
21924 variables with missing type entries. Change the
21925 misleading `void' type to something sensible. */
21926 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21927 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21928
21929 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21930 /* In the case of DW_TAG_member, we should only be called for
21931 static const members. */
21932 if (die->tag == DW_TAG_member)
21933 {
21934 /* dwarf2_add_field uses die_is_declaration,
21935 so we do the same. */
21936 gdb_assert (die_is_declaration (die, cu));
21937 gdb_assert (attr);
21938 }
21939 if (attr != nullptr)
21940 {
21941 dwarf2_const_value (attr, sym, cu);
21942 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21943 if (!suppress_add)
21944 {
21945 if (attr2 && (DW_UNSND (attr2) != 0))
21946 list_to_add = cu->get_builder ()->get_global_symbols ();
21947 else
21948 list_to_add = cu->list_in_scope;
21949 }
21950 break;
21951 }
21952 attr = dwarf2_attr (die, DW_AT_location, cu);
21953 if (attr != nullptr)
21954 {
21955 var_decode_location (attr, sym, cu);
21956 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21957
21958 /* Fortran explicitly imports any global symbols to the local
21959 scope by DW_TAG_common_block. */
21960 if (cu->language == language_fortran && die->parent
21961 && die->parent->tag == DW_TAG_common_block)
21962 attr2 = NULL;
21963
21964 if (SYMBOL_CLASS (sym) == LOC_STATIC
21965 && SYMBOL_VALUE_ADDRESS (sym) == 0
21966 && !dwarf2_per_objfile->has_section_at_zero)
21967 {
21968 /* When a static variable is eliminated by the linker,
21969 the corresponding debug information is not stripped
21970 out, but the variable address is set to null;
21971 do not add such variables into symbol table. */
21972 }
21973 else if (attr2 && (DW_UNSND (attr2) != 0))
21974 {
21975 if (SYMBOL_CLASS (sym) == LOC_STATIC
21976 && (objfile->flags & OBJF_MAINLINE) == 0
21977 && dwarf2_per_objfile->can_copy)
21978 {
21979 /* A global static variable might be subject to
21980 copy relocation. We first check for a local
21981 minsym, though, because maybe the symbol was
21982 marked hidden, in which case this would not
21983 apply. */
21984 bound_minimal_symbol found
21985 = (lookup_minimal_symbol_linkage
21986 (sym->linkage_name (), objfile));
21987 if (found.minsym != nullptr)
21988 sym->maybe_copied = 1;
21989 }
21990
21991 /* A variable with DW_AT_external is never static,
21992 but it may be block-scoped. */
21993 list_to_add
21994 = ((cu->list_in_scope
21995 == cu->get_builder ()->get_file_symbols ())
21996 ? cu->get_builder ()->get_global_symbols ()
21997 : cu->list_in_scope);
21998 }
21999 else
22000 list_to_add = cu->list_in_scope;
22001 }
22002 else
22003 {
22004 /* We do not know the address of this symbol.
22005 If it is an external symbol and we have type information
22006 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22007 The address of the variable will then be determined from
22008 the minimal symbol table whenever the variable is
22009 referenced. */
22010 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22011
22012 /* Fortran explicitly imports any global symbols to the local
22013 scope by DW_TAG_common_block. */
22014 if (cu->language == language_fortran && die->parent
22015 && die->parent->tag == DW_TAG_common_block)
22016 {
22017 /* SYMBOL_CLASS doesn't matter here because
22018 read_common_block is going to reset it. */
22019 if (!suppress_add)
22020 list_to_add = cu->list_in_scope;
22021 }
22022 else if (attr2 && (DW_UNSND (attr2) != 0)
22023 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22024 {
22025 /* A variable with DW_AT_external is never static, but it
22026 may be block-scoped. */
22027 list_to_add
22028 = ((cu->list_in_scope
22029 == cu->get_builder ()->get_file_symbols ())
22030 ? cu->get_builder ()->get_global_symbols ()
22031 : cu->list_in_scope);
22032
22033 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22034 }
22035 else if (!die_is_declaration (die, cu))
22036 {
22037 /* Use the default LOC_OPTIMIZED_OUT class. */
22038 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22039 if (!suppress_add)
22040 list_to_add = cu->list_in_scope;
22041 }
22042 }
22043 break;
22044 case DW_TAG_formal_parameter:
22045 {
22046 /* If we are inside a function, mark this as an argument. If
22047 not, we might be looking at an argument to an inlined function
22048 when we do not have enough information to show inlined frames;
22049 pretend it's a local variable in that case so that the user can
22050 still see it. */
22051 struct context_stack *curr
22052 = cu->get_builder ()->get_current_context_stack ();
22053 if (curr != nullptr && curr->name != nullptr)
22054 SYMBOL_IS_ARGUMENT (sym) = 1;
22055 attr = dwarf2_attr (die, DW_AT_location, cu);
22056 if (attr != nullptr)
22057 {
22058 var_decode_location (attr, sym, cu);
22059 }
22060 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22061 if (attr != nullptr)
22062 {
22063 dwarf2_const_value (attr, sym, cu);
22064 }
22065
22066 list_to_add = cu->list_in_scope;
22067 }
22068 break;
22069 case DW_TAG_unspecified_parameters:
22070 /* From varargs functions; gdb doesn't seem to have any
22071 interest in this information, so just ignore it for now.
22072 (FIXME?) */
22073 break;
22074 case DW_TAG_template_type_param:
22075 suppress_add = 1;
22076 /* Fall through. */
22077 case DW_TAG_class_type:
22078 case DW_TAG_interface_type:
22079 case DW_TAG_structure_type:
22080 case DW_TAG_union_type:
22081 case DW_TAG_set_type:
22082 case DW_TAG_enumeration_type:
22083 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22084 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22085
22086 {
22087 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22088 really ever be static objects: otherwise, if you try
22089 to, say, break of a class's method and you're in a file
22090 which doesn't mention that class, it won't work unless
22091 the check for all static symbols in lookup_symbol_aux
22092 saves you. See the OtherFileClass tests in
22093 gdb.c++/namespace.exp. */
22094
22095 if (!suppress_add)
22096 {
22097 buildsym_compunit *builder = cu->get_builder ();
22098 list_to_add
22099 = (cu->list_in_scope == builder->get_file_symbols ()
22100 && cu->language == language_cplus
22101 ? builder->get_global_symbols ()
22102 : cu->list_in_scope);
22103
22104 /* The semantics of C++ state that "struct foo {
22105 ... }" also defines a typedef for "foo". */
22106 if (cu->language == language_cplus
22107 || cu->language == language_ada
22108 || cu->language == language_d
22109 || cu->language == language_rust)
22110 {
22111 /* The symbol's name is already allocated along
22112 with this objfile, so we don't need to
22113 duplicate it for the type. */
22114 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
22115 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
22116 }
22117 }
22118 }
22119 break;
22120 case DW_TAG_typedef:
22121 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22122 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22123 list_to_add = cu->list_in_scope;
22124 break;
22125 case DW_TAG_base_type:
22126 case DW_TAG_subrange_type:
22127 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22128 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22129 list_to_add = cu->list_in_scope;
22130 break;
22131 case DW_TAG_enumerator:
22132 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22133 if (attr != nullptr)
22134 {
22135 dwarf2_const_value (attr, sym, cu);
22136 }
22137 {
22138 /* NOTE: carlton/2003-11-10: See comment above in the
22139 DW_TAG_class_type, etc. block. */
22140
22141 list_to_add
22142 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22143 && cu->language == language_cplus
22144 ? cu->get_builder ()->get_global_symbols ()
22145 : cu->list_in_scope);
22146 }
22147 break;
22148 case DW_TAG_imported_declaration:
22149 case DW_TAG_namespace:
22150 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22151 list_to_add = cu->get_builder ()->get_global_symbols ();
22152 break;
22153 case DW_TAG_module:
22154 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22155 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22156 list_to_add = cu->get_builder ()->get_global_symbols ();
22157 break;
22158 case DW_TAG_common_block:
22159 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22160 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22161 add_symbol_to_list (sym, cu->list_in_scope);
22162 break;
22163 default:
22164 /* Not a tag we recognize. Hopefully we aren't processing
22165 trash data, but since we must specifically ignore things
22166 we don't recognize, there is nothing else we should do at
22167 this point. */
22168 complaint (_("unsupported tag: '%s'"),
22169 dwarf_tag_name (die->tag));
22170 break;
22171 }
22172
22173 if (suppress_add)
22174 {
22175 sym->hash_next = objfile->template_symbols;
22176 objfile->template_symbols = sym;
22177 list_to_add = NULL;
22178 }
22179
22180 if (list_to_add != NULL)
22181 add_symbol_to_list (sym, list_to_add);
22182
22183 /* For the benefit of old versions of GCC, check for anonymous
22184 namespaces based on the demangled name. */
22185 if (!cu->processing_has_namespace_info
22186 && cu->language == language_cplus)
22187 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22188 }
22189 return (sym);
22190 }
22191
22192 /* Given an attr with a DW_FORM_dataN value in host byte order,
22193 zero-extend it as appropriate for the symbol's type. The DWARF
22194 standard (v4) is not entirely clear about the meaning of using
22195 DW_FORM_dataN for a constant with a signed type, where the type is
22196 wider than the data. The conclusion of a discussion on the DWARF
22197 list was that this is unspecified. We choose to always zero-extend
22198 because that is the interpretation long in use by GCC. */
22199
22200 static gdb_byte *
22201 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22202 struct dwarf2_cu *cu, LONGEST *value, int bits)
22203 {
22204 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22205 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22206 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22207 LONGEST l = DW_UNSND (attr);
22208
22209 if (bits < sizeof (*value) * 8)
22210 {
22211 l &= ((LONGEST) 1 << bits) - 1;
22212 *value = l;
22213 }
22214 else if (bits == sizeof (*value) * 8)
22215 *value = l;
22216 else
22217 {
22218 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22219 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22220 return bytes;
22221 }
22222
22223 return NULL;
22224 }
22225
22226 /* Read a constant value from an attribute. Either set *VALUE, or if
22227 the value does not fit in *VALUE, set *BYTES - either already
22228 allocated on the objfile obstack, or newly allocated on OBSTACK,
22229 or, set *BATON, if we translated the constant to a location
22230 expression. */
22231
22232 static void
22233 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22234 const char *name, struct obstack *obstack,
22235 struct dwarf2_cu *cu,
22236 LONGEST *value, const gdb_byte **bytes,
22237 struct dwarf2_locexpr_baton **baton)
22238 {
22239 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22240 struct comp_unit_head *cu_header = &cu->header;
22241 struct dwarf_block *blk;
22242 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22243 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22244
22245 *value = 0;
22246 *bytes = NULL;
22247 *baton = NULL;
22248
22249 switch (attr->form)
22250 {
22251 case DW_FORM_addr:
22252 case DW_FORM_addrx:
22253 case DW_FORM_GNU_addr_index:
22254 {
22255 gdb_byte *data;
22256
22257 if (TYPE_LENGTH (type) != cu_header->addr_size)
22258 dwarf2_const_value_length_mismatch_complaint (name,
22259 cu_header->addr_size,
22260 TYPE_LENGTH (type));
22261 /* Symbols of this form are reasonably rare, so we just
22262 piggyback on the existing location code rather than writing
22263 a new implementation of symbol_computed_ops. */
22264 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22265 (*baton)->per_cu = cu->per_cu;
22266 gdb_assert ((*baton)->per_cu);
22267
22268 (*baton)->size = 2 + cu_header->addr_size;
22269 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22270 (*baton)->data = data;
22271
22272 data[0] = DW_OP_addr;
22273 store_unsigned_integer (&data[1], cu_header->addr_size,
22274 byte_order, DW_ADDR (attr));
22275 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22276 }
22277 break;
22278 case DW_FORM_string:
22279 case DW_FORM_strp:
22280 case DW_FORM_strx:
22281 case DW_FORM_GNU_str_index:
22282 case DW_FORM_GNU_strp_alt:
22283 /* DW_STRING is already allocated on the objfile obstack, point
22284 directly to it. */
22285 *bytes = (const gdb_byte *) DW_STRING (attr);
22286 break;
22287 case DW_FORM_block1:
22288 case DW_FORM_block2:
22289 case DW_FORM_block4:
22290 case DW_FORM_block:
22291 case DW_FORM_exprloc:
22292 case DW_FORM_data16:
22293 blk = DW_BLOCK (attr);
22294 if (TYPE_LENGTH (type) != blk->size)
22295 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22296 TYPE_LENGTH (type));
22297 *bytes = blk->data;
22298 break;
22299
22300 /* The DW_AT_const_value attributes are supposed to carry the
22301 symbol's value "represented as it would be on the target
22302 architecture." By the time we get here, it's already been
22303 converted to host endianness, so we just need to sign- or
22304 zero-extend it as appropriate. */
22305 case DW_FORM_data1:
22306 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22307 break;
22308 case DW_FORM_data2:
22309 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22310 break;
22311 case DW_FORM_data4:
22312 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22313 break;
22314 case DW_FORM_data8:
22315 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22316 break;
22317
22318 case DW_FORM_sdata:
22319 case DW_FORM_implicit_const:
22320 *value = DW_SND (attr);
22321 break;
22322
22323 case DW_FORM_udata:
22324 *value = DW_UNSND (attr);
22325 break;
22326
22327 default:
22328 complaint (_("unsupported const value attribute form: '%s'"),
22329 dwarf_form_name (attr->form));
22330 *value = 0;
22331 break;
22332 }
22333 }
22334
22335
22336 /* Copy constant value from an attribute to a symbol. */
22337
22338 static void
22339 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22340 struct dwarf2_cu *cu)
22341 {
22342 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22343 LONGEST value;
22344 const gdb_byte *bytes;
22345 struct dwarf2_locexpr_baton *baton;
22346
22347 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22348 sym->print_name (),
22349 &objfile->objfile_obstack, cu,
22350 &value, &bytes, &baton);
22351
22352 if (baton != NULL)
22353 {
22354 SYMBOL_LOCATION_BATON (sym) = baton;
22355 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22356 }
22357 else if (bytes != NULL)
22358 {
22359 SYMBOL_VALUE_BYTES (sym) = bytes;
22360 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22361 }
22362 else
22363 {
22364 SYMBOL_VALUE (sym) = value;
22365 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22366 }
22367 }
22368
22369 /* Return the type of the die in question using its DW_AT_type attribute. */
22370
22371 static struct type *
22372 die_type (struct die_info *die, struct dwarf2_cu *cu)
22373 {
22374 struct attribute *type_attr;
22375
22376 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22377 if (!type_attr)
22378 {
22379 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22380 /* A missing DW_AT_type represents a void type. */
22381 return objfile_type (objfile)->builtin_void;
22382 }
22383
22384 return lookup_die_type (die, type_attr, cu);
22385 }
22386
22387 /* True iff CU's producer generates GNAT Ada auxiliary information
22388 that allows to find parallel types through that information instead
22389 of having to do expensive parallel lookups by type name. */
22390
22391 static int
22392 need_gnat_info (struct dwarf2_cu *cu)
22393 {
22394 /* Assume that the Ada compiler was GNAT, which always produces
22395 the auxiliary information. */
22396 return (cu->language == language_ada);
22397 }
22398
22399 /* Return the auxiliary type of the die in question using its
22400 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22401 attribute is not present. */
22402
22403 static struct type *
22404 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22405 {
22406 struct attribute *type_attr;
22407
22408 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22409 if (!type_attr)
22410 return NULL;
22411
22412 return lookup_die_type (die, type_attr, cu);
22413 }
22414
22415 /* If DIE has a descriptive_type attribute, then set the TYPE's
22416 descriptive type accordingly. */
22417
22418 static void
22419 set_descriptive_type (struct type *type, struct die_info *die,
22420 struct dwarf2_cu *cu)
22421 {
22422 struct type *descriptive_type = die_descriptive_type (die, cu);
22423
22424 if (descriptive_type)
22425 {
22426 ALLOCATE_GNAT_AUX_TYPE (type);
22427 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22428 }
22429 }
22430
22431 /* Return the containing type of the die in question using its
22432 DW_AT_containing_type attribute. */
22433
22434 static struct type *
22435 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22436 {
22437 struct attribute *type_attr;
22438 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22439
22440 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22441 if (!type_attr)
22442 error (_("Dwarf Error: Problem turning containing type into gdb type "
22443 "[in module %s]"), objfile_name (objfile));
22444
22445 return lookup_die_type (die, type_attr, cu);
22446 }
22447
22448 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22449
22450 static struct type *
22451 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22452 {
22453 struct dwarf2_per_objfile *dwarf2_per_objfile
22454 = cu->per_cu->dwarf2_per_objfile;
22455 struct objfile *objfile = dwarf2_per_objfile->objfile;
22456 char *saved;
22457
22458 std::string message
22459 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22460 objfile_name (objfile),
22461 sect_offset_str (cu->header.sect_off),
22462 sect_offset_str (die->sect_off));
22463 saved = obstack_strdup (&objfile->objfile_obstack, message);
22464
22465 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22466 }
22467
22468 /* Look up the type of DIE in CU using its type attribute ATTR.
22469 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22470 DW_AT_containing_type.
22471 If there is no type substitute an error marker. */
22472
22473 static struct type *
22474 lookup_die_type (struct die_info *die, const struct attribute *attr,
22475 struct dwarf2_cu *cu)
22476 {
22477 struct dwarf2_per_objfile *dwarf2_per_objfile
22478 = cu->per_cu->dwarf2_per_objfile;
22479 struct objfile *objfile = dwarf2_per_objfile->objfile;
22480 struct type *this_type;
22481
22482 gdb_assert (attr->name == DW_AT_type
22483 || attr->name == DW_AT_GNAT_descriptive_type
22484 || attr->name == DW_AT_containing_type);
22485
22486 /* First see if we have it cached. */
22487
22488 if (attr->form == DW_FORM_GNU_ref_alt)
22489 {
22490 struct dwarf2_per_cu_data *per_cu;
22491 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22492
22493 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22494 dwarf2_per_objfile);
22495 this_type = get_die_type_at_offset (sect_off, per_cu);
22496 }
22497 else if (attr_form_is_ref (attr))
22498 {
22499 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22500
22501 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22502 }
22503 else if (attr->form == DW_FORM_ref_sig8)
22504 {
22505 ULONGEST signature = DW_SIGNATURE (attr);
22506
22507 return get_signatured_type (die, signature, cu);
22508 }
22509 else
22510 {
22511 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22512 " at %s [in module %s]"),
22513 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22514 objfile_name (objfile));
22515 return build_error_marker_type (cu, die);
22516 }
22517
22518 /* If not cached we need to read it in. */
22519
22520 if (this_type == NULL)
22521 {
22522 struct die_info *type_die = NULL;
22523 struct dwarf2_cu *type_cu = cu;
22524
22525 if (attr_form_is_ref (attr))
22526 type_die = follow_die_ref (die, attr, &type_cu);
22527 if (type_die == NULL)
22528 return build_error_marker_type (cu, die);
22529 /* If we find the type now, it's probably because the type came
22530 from an inter-CU reference and the type's CU got expanded before
22531 ours. */
22532 this_type = read_type_die (type_die, type_cu);
22533 }
22534
22535 /* If we still don't have a type use an error marker. */
22536
22537 if (this_type == NULL)
22538 return build_error_marker_type (cu, die);
22539
22540 return this_type;
22541 }
22542
22543 /* Return the type in DIE, CU.
22544 Returns NULL for invalid types.
22545
22546 This first does a lookup in die_type_hash,
22547 and only reads the die in if necessary.
22548
22549 NOTE: This can be called when reading in partial or full symbols. */
22550
22551 static struct type *
22552 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22553 {
22554 struct type *this_type;
22555
22556 this_type = get_die_type (die, cu);
22557 if (this_type)
22558 return this_type;
22559
22560 return read_type_die_1 (die, cu);
22561 }
22562
22563 /* Read the type in DIE, CU.
22564 Returns NULL for invalid types. */
22565
22566 static struct type *
22567 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22568 {
22569 struct type *this_type = NULL;
22570
22571 switch (die->tag)
22572 {
22573 case DW_TAG_class_type:
22574 case DW_TAG_interface_type:
22575 case DW_TAG_structure_type:
22576 case DW_TAG_union_type:
22577 this_type = read_structure_type (die, cu);
22578 break;
22579 case DW_TAG_enumeration_type:
22580 this_type = read_enumeration_type (die, cu);
22581 break;
22582 case DW_TAG_subprogram:
22583 case DW_TAG_subroutine_type:
22584 case DW_TAG_inlined_subroutine:
22585 this_type = read_subroutine_type (die, cu);
22586 break;
22587 case DW_TAG_array_type:
22588 this_type = read_array_type (die, cu);
22589 break;
22590 case DW_TAG_set_type:
22591 this_type = read_set_type (die, cu);
22592 break;
22593 case DW_TAG_pointer_type:
22594 this_type = read_tag_pointer_type (die, cu);
22595 break;
22596 case DW_TAG_ptr_to_member_type:
22597 this_type = read_tag_ptr_to_member_type (die, cu);
22598 break;
22599 case DW_TAG_reference_type:
22600 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22601 break;
22602 case DW_TAG_rvalue_reference_type:
22603 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22604 break;
22605 case DW_TAG_const_type:
22606 this_type = read_tag_const_type (die, cu);
22607 break;
22608 case DW_TAG_volatile_type:
22609 this_type = read_tag_volatile_type (die, cu);
22610 break;
22611 case DW_TAG_restrict_type:
22612 this_type = read_tag_restrict_type (die, cu);
22613 break;
22614 case DW_TAG_string_type:
22615 this_type = read_tag_string_type (die, cu);
22616 break;
22617 case DW_TAG_typedef:
22618 this_type = read_typedef (die, cu);
22619 break;
22620 case DW_TAG_subrange_type:
22621 this_type = read_subrange_type (die, cu);
22622 break;
22623 case DW_TAG_base_type:
22624 this_type = read_base_type (die, cu);
22625 break;
22626 case DW_TAG_unspecified_type:
22627 this_type = read_unspecified_type (die, cu);
22628 break;
22629 case DW_TAG_namespace:
22630 this_type = read_namespace_type (die, cu);
22631 break;
22632 case DW_TAG_module:
22633 this_type = read_module_type (die, cu);
22634 break;
22635 case DW_TAG_atomic_type:
22636 this_type = read_tag_atomic_type (die, cu);
22637 break;
22638 default:
22639 complaint (_("unexpected tag in read_type_die: '%s'"),
22640 dwarf_tag_name (die->tag));
22641 break;
22642 }
22643
22644 return this_type;
22645 }
22646
22647 /* See if we can figure out if the class lives in a namespace. We do
22648 this by looking for a member function; its demangled name will
22649 contain namespace info, if there is any.
22650 Return the computed name or NULL.
22651 Space for the result is allocated on the objfile's obstack.
22652 This is the full-die version of guess_partial_die_structure_name.
22653 In this case we know DIE has no useful parent. */
22654
22655 static const char *
22656 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22657 {
22658 struct die_info *spec_die;
22659 struct dwarf2_cu *spec_cu;
22660 struct die_info *child;
22661 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22662
22663 spec_cu = cu;
22664 spec_die = die_specification (die, &spec_cu);
22665 if (spec_die != NULL)
22666 {
22667 die = spec_die;
22668 cu = spec_cu;
22669 }
22670
22671 for (child = die->child;
22672 child != NULL;
22673 child = child->sibling)
22674 {
22675 if (child->tag == DW_TAG_subprogram)
22676 {
22677 const char *linkage_name = dw2_linkage_name (child, cu);
22678
22679 if (linkage_name != NULL)
22680 {
22681 gdb::unique_xmalloc_ptr<char> actual_name
22682 (language_class_name_from_physname (cu->language_defn,
22683 linkage_name));
22684 const char *name = NULL;
22685
22686 if (actual_name != NULL)
22687 {
22688 const char *die_name = dwarf2_name (die, cu);
22689
22690 if (die_name != NULL
22691 && strcmp (die_name, actual_name.get ()) != 0)
22692 {
22693 /* Strip off the class name from the full name.
22694 We want the prefix. */
22695 int die_name_len = strlen (die_name);
22696 int actual_name_len = strlen (actual_name.get ());
22697 const char *ptr = actual_name.get ();
22698
22699 /* Test for '::' as a sanity check. */
22700 if (actual_name_len > die_name_len + 2
22701 && ptr[actual_name_len - die_name_len - 1] == ':')
22702 name = obstack_strndup (
22703 &objfile->per_bfd->storage_obstack,
22704 ptr, actual_name_len - die_name_len - 2);
22705 }
22706 }
22707 return name;
22708 }
22709 }
22710 }
22711
22712 return NULL;
22713 }
22714
22715 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22716 prefix part in such case. See
22717 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22718
22719 static const char *
22720 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22721 {
22722 struct attribute *attr;
22723 const char *base;
22724
22725 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22726 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22727 return NULL;
22728
22729 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22730 return NULL;
22731
22732 attr = dw2_linkage_name_attr (die, cu);
22733 if (attr == NULL || DW_STRING (attr) == NULL)
22734 return NULL;
22735
22736 /* dwarf2_name had to be already called. */
22737 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22738
22739 /* Strip the base name, keep any leading namespaces/classes. */
22740 base = strrchr (DW_STRING (attr), ':');
22741 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22742 return "";
22743
22744 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22745 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22746 DW_STRING (attr),
22747 &base[-1] - DW_STRING (attr));
22748 }
22749
22750 /* Return the name of the namespace/class that DIE is defined within,
22751 or "" if we can't tell. The caller should not xfree the result.
22752
22753 For example, if we're within the method foo() in the following
22754 code:
22755
22756 namespace N {
22757 class C {
22758 void foo () {
22759 }
22760 };
22761 }
22762
22763 then determine_prefix on foo's die will return "N::C". */
22764
22765 static const char *
22766 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22767 {
22768 struct dwarf2_per_objfile *dwarf2_per_objfile
22769 = cu->per_cu->dwarf2_per_objfile;
22770 struct die_info *parent, *spec_die;
22771 struct dwarf2_cu *spec_cu;
22772 struct type *parent_type;
22773 const char *retval;
22774
22775 if (cu->language != language_cplus
22776 && cu->language != language_fortran && cu->language != language_d
22777 && cu->language != language_rust)
22778 return "";
22779
22780 retval = anonymous_struct_prefix (die, cu);
22781 if (retval)
22782 return retval;
22783
22784 /* We have to be careful in the presence of DW_AT_specification.
22785 For example, with GCC 3.4, given the code
22786
22787 namespace N {
22788 void foo() {
22789 // Definition of N::foo.
22790 }
22791 }
22792
22793 then we'll have a tree of DIEs like this:
22794
22795 1: DW_TAG_compile_unit
22796 2: DW_TAG_namespace // N
22797 3: DW_TAG_subprogram // declaration of N::foo
22798 4: DW_TAG_subprogram // definition of N::foo
22799 DW_AT_specification // refers to die #3
22800
22801 Thus, when processing die #4, we have to pretend that we're in
22802 the context of its DW_AT_specification, namely the contex of die
22803 #3. */
22804 spec_cu = cu;
22805 spec_die = die_specification (die, &spec_cu);
22806 if (spec_die == NULL)
22807 parent = die->parent;
22808 else
22809 {
22810 parent = spec_die->parent;
22811 cu = spec_cu;
22812 }
22813
22814 if (parent == NULL)
22815 return "";
22816 else if (parent->building_fullname)
22817 {
22818 const char *name;
22819 const char *parent_name;
22820
22821 /* It has been seen on RealView 2.2 built binaries,
22822 DW_TAG_template_type_param types actually _defined_ as
22823 children of the parent class:
22824
22825 enum E {};
22826 template class <class Enum> Class{};
22827 Class<enum E> class_e;
22828
22829 1: DW_TAG_class_type (Class)
22830 2: DW_TAG_enumeration_type (E)
22831 3: DW_TAG_enumerator (enum1:0)
22832 3: DW_TAG_enumerator (enum2:1)
22833 ...
22834 2: DW_TAG_template_type_param
22835 DW_AT_type DW_FORM_ref_udata (E)
22836
22837 Besides being broken debug info, it can put GDB into an
22838 infinite loop. Consider:
22839
22840 When we're building the full name for Class<E>, we'll start
22841 at Class, and go look over its template type parameters,
22842 finding E. We'll then try to build the full name of E, and
22843 reach here. We're now trying to build the full name of E,
22844 and look over the parent DIE for containing scope. In the
22845 broken case, if we followed the parent DIE of E, we'd again
22846 find Class, and once again go look at its template type
22847 arguments, etc., etc. Simply don't consider such parent die
22848 as source-level parent of this die (it can't be, the language
22849 doesn't allow it), and break the loop here. */
22850 name = dwarf2_name (die, cu);
22851 parent_name = dwarf2_name (parent, cu);
22852 complaint (_("template param type '%s' defined within parent '%s'"),
22853 name ? name : "<unknown>",
22854 parent_name ? parent_name : "<unknown>");
22855 return "";
22856 }
22857 else
22858 switch (parent->tag)
22859 {
22860 case DW_TAG_namespace:
22861 parent_type = read_type_die (parent, cu);
22862 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22863 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22864 Work around this problem here. */
22865 if (cu->language == language_cplus
22866 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22867 return "";
22868 /* We give a name to even anonymous namespaces. */
22869 return TYPE_NAME (parent_type);
22870 case DW_TAG_class_type:
22871 case DW_TAG_interface_type:
22872 case DW_TAG_structure_type:
22873 case DW_TAG_union_type:
22874 case DW_TAG_module:
22875 parent_type = read_type_die (parent, cu);
22876 if (TYPE_NAME (parent_type) != NULL)
22877 return TYPE_NAME (parent_type);
22878 else
22879 /* An anonymous structure is only allowed non-static data
22880 members; no typedefs, no member functions, et cetera.
22881 So it does not need a prefix. */
22882 return "";
22883 case DW_TAG_compile_unit:
22884 case DW_TAG_partial_unit:
22885 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22886 if (cu->language == language_cplus
22887 && !dwarf2_per_objfile->types.empty ()
22888 && die->child != NULL
22889 && (die->tag == DW_TAG_class_type
22890 || die->tag == DW_TAG_structure_type
22891 || die->tag == DW_TAG_union_type))
22892 {
22893 const char *name = guess_full_die_structure_name (die, cu);
22894 if (name != NULL)
22895 return name;
22896 }
22897 return "";
22898 case DW_TAG_subprogram:
22899 /* Nested subroutines in Fortran get a prefix with the name
22900 of the parent's subroutine. */
22901 if (cu->language == language_fortran)
22902 {
22903 if ((die->tag == DW_TAG_subprogram)
22904 && (dwarf2_name (parent, cu) != NULL))
22905 return dwarf2_name (parent, cu);
22906 }
22907 return determine_prefix (parent, cu);
22908 case DW_TAG_enumeration_type:
22909 parent_type = read_type_die (parent, cu);
22910 if (TYPE_DECLARED_CLASS (parent_type))
22911 {
22912 if (TYPE_NAME (parent_type) != NULL)
22913 return TYPE_NAME (parent_type);
22914 return "";
22915 }
22916 /* Fall through. */
22917 default:
22918 return determine_prefix (parent, cu);
22919 }
22920 }
22921
22922 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22923 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22924 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22925 an obconcat, otherwise allocate storage for the result. The CU argument is
22926 used to determine the language and hence, the appropriate separator. */
22927
22928 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22929
22930 static char *
22931 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22932 int physname, struct dwarf2_cu *cu)
22933 {
22934 const char *lead = "";
22935 const char *sep;
22936
22937 if (suffix == NULL || suffix[0] == '\0'
22938 || prefix == NULL || prefix[0] == '\0')
22939 sep = "";
22940 else if (cu->language == language_d)
22941 {
22942 /* For D, the 'main' function could be defined in any module, but it
22943 should never be prefixed. */
22944 if (strcmp (suffix, "D main") == 0)
22945 {
22946 prefix = "";
22947 sep = "";
22948 }
22949 else
22950 sep = ".";
22951 }
22952 else if (cu->language == language_fortran && physname)
22953 {
22954 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22955 DW_AT_MIPS_linkage_name is preferred and used instead. */
22956
22957 lead = "__";
22958 sep = "_MOD_";
22959 }
22960 else
22961 sep = "::";
22962
22963 if (prefix == NULL)
22964 prefix = "";
22965 if (suffix == NULL)
22966 suffix = "";
22967
22968 if (obs == NULL)
22969 {
22970 char *retval
22971 = ((char *)
22972 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22973
22974 strcpy (retval, lead);
22975 strcat (retval, prefix);
22976 strcat (retval, sep);
22977 strcat (retval, suffix);
22978 return retval;
22979 }
22980 else
22981 {
22982 /* We have an obstack. */
22983 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22984 }
22985 }
22986
22987 /* Return sibling of die, NULL if no sibling. */
22988
22989 static struct die_info *
22990 sibling_die (struct die_info *die)
22991 {
22992 return die->sibling;
22993 }
22994
22995 /* Get name of a die, return NULL if not found. */
22996
22997 static const char *
22998 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22999 struct obstack *obstack)
23000 {
23001 if (name && cu->language == language_cplus)
23002 {
23003 std::string canon_name = cp_canonicalize_string (name);
23004
23005 if (!canon_name.empty ())
23006 {
23007 if (canon_name != name)
23008 name = obstack_strdup (obstack, canon_name);
23009 }
23010 }
23011
23012 return name;
23013 }
23014
23015 /* Get name of a die, return NULL if not found.
23016 Anonymous namespaces are converted to their magic string. */
23017
23018 static const char *
23019 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23020 {
23021 struct attribute *attr;
23022 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23023
23024 attr = dwarf2_attr (die, DW_AT_name, cu);
23025 if ((!attr || !DW_STRING (attr))
23026 && die->tag != DW_TAG_namespace
23027 && die->tag != DW_TAG_class_type
23028 && die->tag != DW_TAG_interface_type
23029 && die->tag != DW_TAG_structure_type
23030 && die->tag != DW_TAG_union_type)
23031 return NULL;
23032
23033 switch (die->tag)
23034 {
23035 case DW_TAG_compile_unit:
23036 case DW_TAG_partial_unit:
23037 /* Compilation units have a DW_AT_name that is a filename, not
23038 a source language identifier. */
23039 case DW_TAG_enumeration_type:
23040 case DW_TAG_enumerator:
23041 /* These tags always have simple identifiers already; no need
23042 to canonicalize them. */
23043 return DW_STRING (attr);
23044
23045 case DW_TAG_namespace:
23046 if (attr != NULL && DW_STRING (attr) != NULL)
23047 return DW_STRING (attr);
23048 return CP_ANONYMOUS_NAMESPACE_STR;
23049
23050 case DW_TAG_class_type:
23051 case DW_TAG_interface_type:
23052 case DW_TAG_structure_type:
23053 case DW_TAG_union_type:
23054 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23055 structures or unions. These were of the form "._%d" in GCC 4.1,
23056 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23057 and GCC 4.4. We work around this problem by ignoring these. */
23058 if (attr && DW_STRING (attr)
23059 && (startswith (DW_STRING (attr), "._")
23060 || startswith (DW_STRING (attr), "<anonymous")))
23061 return NULL;
23062
23063 /* GCC might emit a nameless typedef that has a linkage name. See
23064 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23065 if (!attr || DW_STRING (attr) == NULL)
23066 {
23067 attr = dw2_linkage_name_attr (die, cu);
23068 if (attr == NULL || DW_STRING (attr) == NULL)
23069 return NULL;
23070
23071 /* Avoid demangling DW_STRING (attr) the second time on a second
23072 call for the same DIE. */
23073 if (!DW_STRING_IS_CANONICAL (attr))
23074 {
23075 gdb::unique_xmalloc_ptr<char> demangled
23076 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
23077
23078 const char *base;
23079
23080 /* FIXME: we already did this for the partial symbol... */
23081 DW_STRING (attr)
23082 = obstack_strdup (&objfile->per_bfd->storage_obstack,
23083 demangled.get ());
23084 DW_STRING_IS_CANONICAL (attr) = 1;
23085
23086 /* Strip any leading namespaces/classes, keep only the base name.
23087 DW_AT_name for named DIEs does not contain the prefixes. */
23088 base = strrchr (DW_STRING (attr), ':');
23089 if (base && base > DW_STRING (attr) && base[-1] == ':')
23090 return &base[1];
23091 else
23092 return DW_STRING (attr);
23093 }
23094 }
23095 break;
23096
23097 default:
23098 break;
23099 }
23100
23101 if (!DW_STRING_IS_CANONICAL (attr))
23102 {
23103 DW_STRING (attr)
23104 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
23105 &objfile->per_bfd->storage_obstack);
23106 DW_STRING_IS_CANONICAL (attr) = 1;
23107 }
23108 return DW_STRING (attr);
23109 }
23110
23111 /* Return the die that this die in an extension of, or NULL if there
23112 is none. *EXT_CU is the CU containing DIE on input, and the CU
23113 containing the return value on output. */
23114
23115 static struct die_info *
23116 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23117 {
23118 struct attribute *attr;
23119
23120 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23121 if (attr == NULL)
23122 return NULL;
23123
23124 return follow_die_ref (die, attr, ext_cu);
23125 }
23126
23127 /* A convenience function that returns an "unknown" DWARF name,
23128 including the value of V. STR is the name of the entity being
23129 printed, e.g., "TAG". */
23130
23131 static const char *
23132 dwarf_unknown (const char *str, unsigned v)
23133 {
23134 char *cell = get_print_cell ();
23135 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
23136 return cell;
23137 }
23138
23139 /* Convert a DIE tag into its string name. */
23140
23141 static const char *
23142 dwarf_tag_name (unsigned tag)
23143 {
23144 const char *name = get_DW_TAG_name (tag);
23145
23146 if (name == NULL)
23147 return dwarf_unknown ("TAG", tag);
23148
23149 return name;
23150 }
23151
23152 /* Convert a DWARF attribute code into its string name. */
23153
23154 static const char *
23155 dwarf_attr_name (unsigned attr)
23156 {
23157 const char *name;
23158
23159 #ifdef MIPS /* collides with DW_AT_HP_block_index */
23160 if (attr == DW_AT_MIPS_fde)
23161 return "DW_AT_MIPS_fde";
23162 #else
23163 if (attr == DW_AT_HP_block_index)
23164 return "DW_AT_HP_block_index";
23165 #endif
23166
23167 name = get_DW_AT_name (attr);
23168
23169 if (name == NULL)
23170 return dwarf_unknown ("AT", attr);
23171
23172 return name;
23173 }
23174
23175 /* Convert a unit type to corresponding DW_UT name. */
23176
23177 static const char *
23178 dwarf_unit_type_name (int unit_type) {
23179 switch (unit_type)
23180 {
23181 case 0x01:
23182 return "DW_UT_compile (0x01)";
23183 case 0x02:
23184 return "DW_UT_type (0x02)";
23185 case 0x03:
23186 return "DW_UT_partial (0x03)";
23187 case 0x04:
23188 return "DW_UT_skeleton (0x04)";
23189 case 0x05:
23190 return "DW_UT_split_compile (0x05)";
23191 case 0x06:
23192 return "DW_UT_split_type (0x06)";
23193 case 0x80:
23194 return "DW_UT_lo_user (0x80)";
23195 case 0xff:
23196 return "DW_UT_hi_user (0xff)";
23197 default:
23198 return nullptr;
23199 }
23200 }
23201
23202 /* Convert a DWARF value form code into its string name. */
23203
23204 static const char *
23205 dwarf_form_name (unsigned form)
23206 {
23207 const char *name = get_DW_FORM_name (form);
23208
23209 if (name == NULL)
23210 return dwarf_unknown ("FORM", form);
23211
23212 return name;
23213 }
23214
23215 static const char *
23216 dwarf_bool_name (unsigned mybool)
23217 {
23218 if (mybool)
23219 return "TRUE";
23220 else
23221 return "FALSE";
23222 }
23223
23224 /* Convert a DWARF type code into its string name. */
23225
23226 static const char *
23227 dwarf_type_encoding_name (unsigned enc)
23228 {
23229 const char *name = get_DW_ATE_name (enc);
23230
23231 if (name == NULL)
23232 return dwarf_unknown ("ATE", enc);
23233
23234 return name;
23235 }
23236
23237 static void
23238 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23239 {
23240 unsigned int i;
23241
23242 print_spaces (indent, f);
23243 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23244 dwarf_tag_name (die->tag), die->abbrev,
23245 sect_offset_str (die->sect_off));
23246
23247 if (die->parent != NULL)
23248 {
23249 print_spaces (indent, f);
23250 fprintf_unfiltered (f, " parent at offset: %s\n",
23251 sect_offset_str (die->parent->sect_off));
23252 }
23253
23254 print_spaces (indent, f);
23255 fprintf_unfiltered (f, " has children: %s\n",
23256 dwarf_bool_name (die->child != NULL));
23257
23258 print_spaces (indent, f);
23259 fprintf_unfiltered (f, " attributes:\n");
23260
23261 for (i = 0; i < die->num_attrs; ++i)
23262 {
23263 print_spaces (indent, f);
23264 fprintf_unfiltered (f, " %s (%s) ",
23265 dwarf_attr_name (die->attrs[i].name),
23266 dwarf_form_name (die->attrs[i].form));
23267
23268 switch (die->attrs[i].form)
23269 {
23270 case DW_FORM_addr:
23271 case DW_FORM_addrx:
23272 case DW_FORM_GNU_addr_index:
23273 fprintf_unfiltered (f, "address: ");
23274 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
23275 break;
23276 case DW_FORM_block2:
23277 case DW_FORM_block4:
23278 case DW_FORM_block:
23279 case DW_FORM_block1:
23280 fprintf_unfiltered (f, "block: size %s",
23281 pulongest (DW_BLOCK (&die->attrs[i])->size));
23282 break;
23283 case DW_FORM_exprloc:
23284 fprintf_unfiltered (f, "expression: size %s",
23285 pulongest (DW_BLOCK (&die->attrs[i])->size));
23286 break;
23287 case DW_FORM_data16:
23288 fprintf_unfiltered (f, "constant of 16 bytes");
23289 break;
23290 case DW_FORM_ref_addr:
23291 fprintf_unfiltered (f, "ref address: ");
23292 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23293 break;
23294 case DW_FORM_GNU_ref_alt:
23295 fprintf_unfiltered (f, "alt ref address: ");
23296 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23297 break;
23298 case DW_FORM_ref1:
23299 case DW_FORM_ref2:
23300 case DW_FORM_ref4:
23301 case DW_FORM_ref8:
23302 case DW_FORM_ref_udata:
23303 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23304 (long) (DW_UNSND (&die->attrs[i])));
23305 break;
23306 case DW_FORM_data1:
23307 case DW_FORM_data2:
23308 case DW_FORM_data4:
23309 case DW_FORM_data8:
23310 case DW_FORM_udata:
23311 case DW_FORM_sdata:
23312 fprintf_unfiltered (f, "constant: %s",
23313 pulongest (DW_UNSND (&die->attrs[i])));
23314 break;
23315 case DW_FORM_sec_offset:
23316 fprintf_unfiltered (f, "section offset: %s",
23317 pulongest (DW_UNSND (&die->attrs[i])));
23318 break;
23319 case DW_FORM_ref_sig8:
23320 fprintf_unfiltered (f, "signature: %s",
23321 hex_string (DW_SIGNATURE (&die->attrs[i])));
23322 break;
23323 case DW_FORM_string:
23324 case DW_FORM_strp:
23325 case DW_FORM_line_strp:
23326 case DW_FORM_strx:
23327 case DW_FORM_GNU_str_index:
23328 case DW_FORM_GNU_strp_alt:
23329 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23330 DW_STRING (&die->attrs[i])
23331 ? DW_STRING (&die->attrs[i]) : "",
23332 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
23333 break;
23334 case DW_FORM_flag:
23335 if (DW_UNSND (&die->attrs[i]))
23336 fprintf_unfiltered (f, "flag: TRUE");
23337 else
23338 fprintf_unfiltered (f, "flag: FALSE");
23339 break;
23340 case DW_FORM_flag_present:
23341 fprintf_unfiltered (f, "flag: TRUE");
23342 break;
23343 case DW_FORM_indirect:
23344 /* The reader will have reduced the indirect form to
23345 the "base form" so this form should not occur. */
23346 fprintf_unfiltered (f,
23347 "unexpected attribute form: DW_FORM_indirect");
23348 break;
23349 case DW_FORM_implicit_const:
23350 fprintf_unfiltered (f, "constant: %s",
23351 plongest (DW_SND (&die->attrs[i])));
23352 break;
23353 default:
23354 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23355 die->attrs[i].form);
23356 break;
23357 }
23358 fprintf_unfiltered (f, "\n");
23359 }
23360 }
23361
23362 static void
23363 dump_die_for_error (struct die_info *die)
23364 {
23365 dump_die_shallow (gdb_stderr, 0, die);
23366 }
23367
23368 static void
23369 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23370 {
23371 int indent = level * 4;
23372
23373 gdb_assert (die != NULL);
23374
23375 if (level >= max_level)
23376 return;
23377
23378 dump_die_shallow (f, indent, die);
23379
23380 if (die->child != NULL)
23381 {
23382 print_spaces (indent, f);
23383 fprintf_unfiltered (f, " Children:");
23384 if (level + 1 < max_level)
23385 {
23386 fprintf_unfiltered (f, "\n");
23387 dump_die_1 (f, level + 1, max_level, die->child);
23388 }
23389 else
23390 {
23391 fprintf_unfiltered (f,
23392 " [not printed, max nesting level reached]\n");
23393 }
23394 }
23395
23396 if (die->sibling != NULL && level > 0)
23397 {
23398 dump_die_1 (f, level, max_level, die->sibling);
23399 }
23400 }
23401
23402 /* This is called from the pdie macro in gdbinit.in.
23403 It's not static so gcc will keep a copy callable from gdb. */
23404
23405 void
23406 dump_die (struct die_info *die, int max_level)
23407 {
23408 dump_die_1 (gdb_stdlog, 0, max_level, die);
23409 }
23410
23411 static void
23412 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23413 {
23414 void **slot;
23415
23416 slot = htab_find_slot_with_hash (cu->die_hash, die,
23417 to_underlying (die->sect_off),
23418 INSERT);
23419
23420 *slot = die;
23421 }
23422
23423 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23424 required kind. */
23425
23426 static sect_offset
23427 dwarf2_get_ref_die_offset (const struct attribute *attr)
23428 {
23429 if (attr_form_is_ref (attr))
23430 return (sect_offset) DW_UNSND (attr);
23431
23432 complaint (_("unsupported die ref attribute form: '%s'"),
23433 dwarf_form_name (attr->form));
23434 return {};
23435 }
23436
23437 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23438 * the value held by the attribute is not constant. */
23439
23440 static LONGEST
23441 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23442 {
23443 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23444 return DW_SND (attr);
23445 else if (attr->form == DW_FORM_udata
23446 || attr->form == DW_FORM_data1
23447 || attr->form == DW_FORM_data2
23448 || attr->form == DW_FORM_data4
23449 || attr->form == DW_FORM_data8)
23450 return DW_UNSND (attr);
23451 else
23452 {
23453 /* For DW_FORM_data16 see attr_form_is_constant. */
23454 complaint (_("Attribute value is not a constant (%s)"),
23455 dwarf_form_name (attr->form));
23456 return default_value;
23457 }
23458 }
23459
23460 /* Follow reference or signature attribute ATTR of SRC_DIE.
23461 On entry *REF_CU is the CU of SRC_DIE.
23462 On exit *REF_CU is the CU of the result. */
23463
23464 static struct die_info *
23465 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23466 struct dwarf2_cu **ref_cu)
23467 {
23468 struct die_info *die;
23469
23470 if (attr_form_is_ref (attr))
23471 die = follow_die_ref (src_die, attr, ref_cu);
23472 else if (attr->form == DW_FORM_ref_sig8)
23473 die = follow_die_sig (src_die, attr, ref_cu);
23474 else
23475 {
23476 dump_die_for_error (src_die);
23477 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23478 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23479 }
23480
23481 return die;
23482 }
23483
23484 /* Follow reference OFFSET.
23485 On entry *REF_CU is the CU of the source die referencing OFFSET.
23486 On exit *REF_CU is the CU of the result.
23487 Returns NULL if OFFSET is invalid. */
23488
23489 static struct die_info *
23490 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23491 struct dwarf2_cu **ref_cu)
23492 {
23493 struct die_info temp_die;
23494 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23495 struct dwarf2_per_objfile *dwarf2_per_objfile
23496 = cu->per_cu->dwarf2_per_objfile;
23497
23498 gdb_assert (cu->per_cu != NULL);
23499
23500 target_cu = cu;
23501
23502 if (cu->per_cu->is_debug_types)
23503 {
23504 /* .debug_types CUs cannot reference anything outside their CU.
23505 If they need to, they have to reference a signatured type via
23506 DW_FORM_ref_sig8. */
23507 if (!offset_in_cu_p (&cu->header, sect_off))
23508 return NULL;
23509 }
23510 else if (offset_in_dwz != cu->per_cu->is_dwz
23511 || !offset_in_cu_p (&cu->header, sect_off))
23512 {
23513 struct dwarf2_per_cu_data *per_cu;
23514
23515 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23516 dwarf2_per_objfile);
23517
23518 /* If necessary, add it to the queue and load its DIEs. */
23519 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23520 load_full_comp_unit (per_cu, false, cu->language);
23521
23522 target_cu = per_cu->cu;
23523 }
23524 else if (cu->dies == NULL)
23525 {
23526 /* We're loading full DIEs during partial symbol reading. */
23527 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23528 load_full_comp_unit (cu->per_cu, false, language_minimal);
23529 }
23530
23531 *ref_cu = target_cu;
23532 temp_die.sect_off = sect_off;
23533
23534 if (target_cu != cu)
23535 target_cu->ancestor = cu;
23536
23537 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23538 &temp_die,
23539 to_underlying (sect_off));
23540 }
23541
23542 /* Follow reference attribute ATTR of SRC_DIE.
23543 On entry *REF_CU is the CU of SRC_DIE.
23544 On exit *REF_CU is the CU of the result. */
23545
23546 static struct die_info *
23547 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23548 struct dwarf2_cu **ref_cu)
23549 {
23550 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23551 struct dwarf2_cu *cu = *ref_cu;
23552 struct die_info *die;
23553
23554 die = follow_die_offset (sect_off,
23555 (attr->form == DW_FORM_GNU_ref_alt
23556 || cu->per_cu->is_dwz),
23557 ref_cu);
23558 if (!die)
23559 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23560 "at %s [in module %s]"),
23561 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23562 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23563
23564 return die;
23565 }
23566
23567 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23568 Returned value is intended for DW_OP_call*. Returned
23569 dwarf2_locexpr_baton->data has lifetime of
23570 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23571
23572 struct dwarf2_locexpr_baton
23573 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23574 struct dwarf2_per_cu_data *per_cu,
23575 CORE_ADDR (*get_frame_pc) (void *baton),
23576 void *baton, bool resolve_abstract_p)
23577 {
23578 struct dwarf2_cu *cu;
23579 struct die_info *die;
23580 struct attribute *attr;
23581 struct dwarf2_locexpr_baton retval;
23582 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23583 struct objfile *objfile = dwarf2_per_objfile->objfile;
23584
23585 if (per_cu->cu == NULL)
23586 load_cu (per_cu, false);
23587 cu = per_cu->cu;
23588 if (cu == NULL)
23589 {
23590 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23591 Instead just throw an error, not much else we can do. */
23592 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23593 sect_offset_str (sect_off), objfile_name (objfile));
23594 }
23595
23596 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23597 if (!die)
23598 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23599 sect_offset_str (sect_off), objfile_name (objfile));
23600
23601 attr = dwarf2_attr (die, DW_AT_location, cu);
23602 if (!attr && resolve_abstract_p
23603 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
23604 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23605 {
23606 CORE_ADDR pc = (*get_frame_pc) (baton);
23607 CORE_ADDR baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
23608 struct gdbarch *gdbarch = get_objfile_arch (objfile);
23609
23610 for (const auto &cand_off
23611 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
23612 {
23613 struct dwarf2_cu *cand_cu = cu;
23614 struct die_info *cand
23615 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23616 if (!cand
23617 || !cand->parent
23618 || cand->parent->tag != DW_TAG_subprogram)
23619 continue;
23620
23621 CORE_ADDR pc_low, pc_high;
23622 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23623 if (pc_low == ((CORE_ADDR) -1))
23624 continue;
23625 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23626 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23627 if (!(pc_low <= pc && pc < pc_high))
23628 continue;
23629
23630 die = cand;
23631 attr = dwarf2_attr (die, DW_AT_location, cu);
23632 break;
23633 }
23634 }
23635
23636 if (!attr)
23637 {
23638 /* DWARF: "If there is no such attribute, then there is no effect.".
23639 DATA is ignored if SIZE is 0. */
23640
23641 retval.data = NULL;
23642 retval.size = 0;
23643 }
23644 else if (attr_form_is_section_offset (attr))
23645 {
23646 struct dwarf2_loclist_baton loclist_baton;
23647 CORE_ADDR pc = (*get_frame_pc) (baton);
23648 size_t size;
23649
23650 fill_in_loclist_baton (cu, &loclist_baton, attr);
23651
23652 retval.data = dwarf2_find_location_expression (&loclist_baton,
23653 &size, pc);
23654 retval.size = size;
23655 }
23656 else
23657 {
23658 if (!attr_form_is_block (attr))
23659 error (_("Dwarf Error: DIE at %s referenced in module %s "
23660 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23661 sect_offset_str (sect_off), objfile_name (objfile));
23662
23663 retval.data = DW_BLOCK (attr)->data;
23664 retval.size = DW_BLOCK (attr)->size;
23665 }
23666 retval.per_cu = cu->per_cu;
23667
23668 age_cached_comp_units (dwarf2_per_objfile);
23669
23670 return retval;
23671 }
23672
23673 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23674 offset. */
23675
23676 struct dwarf2_locexpr_baton
23677 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23678 struct dwarf2_per_cu_data *per_cu,
23679 CORE_ADDR (*get_frame_pc) (void *baton),
23680 void *baton)
23681 {
23682 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23683
23684 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23685 }
23686
23687 /* Write a constant of a given type as target-ordered bytes into
23688 OBSTACK. */
23689
23690 static const gdb_byte *
23691 write_constant_as_bytes (struct obstack *obstack,
23692 enum bfd_endian byte_order,
23693 struct type *type,
23694 ULONGEST value,
23695 LONGEST *len)
23696 {
23697 gdb_byte *result;
23698
23699 *len = TYPE_LENGTH (type);
23700 result = (gdb_byte *) obstack_alloc (obstack, *len);
23701 store_unsigned_integer (result, *len, byte_order, value);
23702
23703 return result;
23704 }
23705
23706 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23707 pointer to the constant bytes and set LEN to the length of the
23708 data. If memory is needed, allocate it on OBSTACK. If the DIE
23709 does not have a DW_AT_const_value, return NULL. */
23710
23711 const gdb_byte *
23712 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23713 struct dwarf2_per_cu_data *per_cu,
23714 struct obstack *obstack,
23715 LONGEST *len)
23716 {
23717 struct dwarf2_cu *cu;
23718 struct die_info *die;
23719 struct attribute *attr;
23720 const gdb_byte *result = NULL;
23721 struct type *type;
23722 LONGEST value;
23723 enum bfd_endian byte_order;
23724 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23725
23726 if (per_cu->cu == NULL)
23727 load_cu (per_cu, false);
23728 cu = per_cu->cu;
23729 if (cu == NULL)
23730 {
23731 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23732 Instead just throw an error, not much else we can do. */
23733 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23734 sect_offset_str (sect_off), objfile_name (objfile));
23735 }
23736
23737 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23738 if (!die)
23739 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23740 sect_offset_str (sect_off), objfile_name (objfile));
23741
23742 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23743 if (attr == NULL)
23744 return NULL;
23745
23746 byte_order = (bfd_big_endian (objfile->obfd)
23747 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23748
23749 switch (attr->form)
23750 {
23751 case DW_FORM_addr:
23752 case DW_FORM_addrx:
23753 case DW_FORM_GNU_addr_index:
23754 {
23755 gdb_byte *tem;
23756
23757 *len = cu->header.addr_size;
23758 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23759 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23760 result = tem;
23761 }
23762 break;
23763 case DW_FORM_string:
23764 case DW_FORM_strp:
23765 case DW_FORM_strx:
23766 case DW_FORM_GNU_str_index:
23767 case DW_FORM_GNU_strp_alt:
23768 /* DW_STRING is already allocated on the objfile obstack, point
23769 directly to it. */
23770 result = (const gdb_byte *) DW_STRING (attr);
23771 *len = strlen (DW_STRING (attr));
23772 break;
23773 case DW_FORM_block1:
23774 case DW_FORM_block2:
23775 case DW_FORM_block4:
23776 case DW_FORM_block:
23777 case DW_FORM_exprloc:
23778 case DW_FORM_data16:
23779 result = DW_BLOCK (attr)->data;
23780 *len = DW_BLOCK (attr)->size;
23781 break;
23782
23783 /* The DW_AT_const_value attributes are supposed to carry the
23784 symbol's value "represented as it would be on the target
23785 architecture." By the time we get here, it's already been
23786 converted to host endianness, so we just need to sign- or
23787 zero-extend it as appropriate. */
23788 case DW_FORM_data1:
23789 type = die_type (die, cu);
23790 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23791 if (result == NULL)
23792 result = write_constant_as_bytes (obstack, byte_order,
23793 type, value, len);
23794 break;
23795 case DW_FORM_data2:
23796 type = die_type (die, cu);
23797 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23798 if (result == NULL)
23799 result = write_constant_as_bytes (obstack, byte_order,
23800 type, value, len);
23801 break;
23802 case DW_FORM_data4:
23803 type = die_type (die, cu);
23804 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23805 if (result == NULL)
23806 result = write_constant_as_bytes (obstack, byte_order,
23807 type, value, len);
23808 break;
23809 case DW_FORM_data8:
23810 type = die_type (die, cu);
23811 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23812 if (result == NULL)
23813 result = write_constant_as_bytes (obstack, byte_order,
23814 type, value, len);
23815 break;
23816
23817 case DW_FORM_sdata:
23818 case DW_FORM_implicit_const:
23819 type = die_type (die, cu);
23820 result = write_constant_as_bytes (obstack, byte_order,
23821 type, DW_SND (attr), len);
23822 break;
23823
23824 case DW_FORM_udata:
23825 type = die_type (die, cu);
23826 result = write_constant_as_bytes (obstack, byte_order,
23827 type, DW_UNSND (attr), len);
23828 break;
23829
23830 default:
23831 complaint (_("unsupported const value attribute form: '%s'"),
23832 dwarf_form_name (attr->form));
23833 break;
23834 }
23835
23836 return result;
23837 }
23838
23839 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23840 valid type for this die is found. */
23841
23842 struct type *
23843 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23844 struct dwarf2_per_cu_data *per_cu)
23845 {
23846 struct dwarf2_cu *cu;
23847 struct die_info *die;
23848
23849 if (per_cu->cu == NULL)
23850 load_cu (per_cu, false);
23851 cu = per_cu->cu;
23852 if (!cu)
23853 return NULL;
23854
23855 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23856 if (!die)
23857 return NULL;
23858
23859 return die_type (die, cu);
23860 }
23861
23862 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23863 PER_CU. */
23864
23865 struct type *
23866 dwarf2_get_die_type (cu_offset die_offset,
23867 struct dwarf2_per_cu_data *per_cu)
23868 {
23869 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23870 return get_die_type_at_offset (die_offset_sect, per_cu);
23871 }
23872
23873 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23874 On entry *REF_CU is the CU of SRC_DIE.
23875 On exit *REF_CU is the CU of the result.
23876 Returns NULL if the referenced DIE isn't found. */
23877
23878 static struct die_info *
23879 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23880 struct dwarf2_cu **ref_cu)
23881 {
23882 struct die_info temp_die;
23883 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23884 struct die_info *die;
23885
23886 /* While it might be nice to assert sig_type->type == NULL here,
23887 we can get here for DW_AT_imported_declaration where we need
23888 the DIE not the type. */
23889
23890 /* If necessary, add it to the queue and load its DIEs. */
23891
23892 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23893 read_signatured_type (sig_type);
23894
23895 sig_cu = sig_type->per_cu.cu;
23896 gdb_assert (sig_cu != NULL);
23897 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23898 temp_die.sect_off = sig_type->type_offset_in_section;
23899 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23900 to_underlying (temp_die.sect_off));
23901 if (die)
23902 {
23903 struct dwarf2_per_objfile *dwarf2_per_objfile
23904 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23905
23906 /* For .gdb_index version 7 keep track of included TUs.
23907 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23908 if (dwarf2_per_objfile->index_table != NULL
23909 && dwarf2_per_objfile->index_table->version <= 7)
23910 {
23911 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23912 }
23913
23914 *ref_cu = sig_cu;
23915 if (sig_cu != cu)
23916 sig_cu->ancestor = cu;
23917
23918 return die;
23919 }
23920
23921 return NULL;
23922 }
23923
23924 /* Follow signatured type referenced by ATTR in SRC_DIE.
23925 On entry *REF_CU is the CU of SRC_DIE.
23926 On exit *REF_CU is the CU of the result.
23927 The result is the DIE of the type.
23928 If the referenced type cannot be found an error is thrown. */
23929
23930 static struct die_info *
23931 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23932 struct dwarf2_cu **ref_cu)
23933 {
23934 ULONGEST signature = DW_SIGNATURE (attr);
23935 struct signatured_type *sig_type;
23936 struct die_info *die;
23937
23938 gdb_assert (attr->form == DW_FORM_ref_sig8);
23939
23940 sig_type = lookup_signatured_type (*ref_cu, signature);
23941 /* sig_type will be NULL if the signatured type is missing from
23942 the debug info. */
23943 if (sig_type == NULL)
23944 {
23945 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23946 " from DIE at %s [in module %s]"),
23947 hex_string (signature), sect_offset_str (src_die->sect_off),
23948 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23949 }
23950
23951 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23952 if (die == NULL)
23953 {
23954 dump_die_for_error (src_die);
23955 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23956 " from DIE at %s [in module %s]"),
23957 hex_string (signature), sect_offset_str (src_die->sect_off),
23958 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23959 }
23960
23961 return die;
23962 }
23963
23964 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23965 reading in and processing the type unit if necessary. */
23966
23967 static struct type *
23968 get_signatured_type (struct die_info *die, ULONGEST signature,
23969 struct dwarf2_cu *cu)
23970 {
23971 struct dwarf2_per_objfile *dwarf2_per_objfile
23972 = cu->per_cu->dwarf2_per_objfile;
23973 struct signatured_type *sig_type;
23974 struct dwarf2_cu *type_cu;
23975 struct die_info *type_die;
23976 struct type *type;
23977
23978 sig_type = lookup_signatured_type (cu, signature);
23979 /* sig_type will be NULL if the signatured type is missing from
23980 the debug info. */
23981 if (sig_type == NULL)
23982 {
23983 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23984 " from DIE at %s [in module %s]"),
23985 hex_string (signature), sect_offset_str (die->sect_off),
23986 objfile_name (dwarf2_per_objfile->objfile));
23987 return build_error_marker_type (cu, die);
23988 }
23989
23990 /* If we already know the type we're done. */
23991 if (sig_type->type != NULL)
23992 return sig_type->type;
23993
23994 type_cu = cu;
23995 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23996 if (type_die != NULL)
23997 {
23998 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23999 is created. This is important, for example, because for c++ classes
24000 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24001 type = read_type_die (type_die, type_cu);
24002 if (type == NULL)
24003 {
24004 complaint (_("Dwarf Error: Cannot build signatured type %s"
24005 " referenced from DIE at %s [in module %s]"),
24006 hex_string (signature), sect_offset_str (die->sect_off),
24007 objfile_name (dwarf2_per_objfile->objfile));
24008 type = build_error_marker_type (cu, die);
24009 }
24010 }
24011 else
24012 {
24013 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24014 " from DIE at %s [in module %s]"),
24015 hex_string (signature), sect_offset_str (die->sect_off),
24016 objfile_name (dwarf2_per_objfile->objfile));
24017 type = build_error_marker_type (cu, die);
24018 }
24019 sig_type->type = type;
24020
24021 return type;
24022 }
24023
24024 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24025 reading in and processing the type unit if necessary. */
24026
24027 static struct type *
24028 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24029 struct dwarf2_cu *cu) /* ARI: editCase function */
24030 {
24031 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24032 if (attr_form_is_ref (attr))
24033 {
24034 struct dwarf2_cu *type_cu = cu;
24035 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24036
24037 return read_type_die (type_die, type_cu);
24038 }
24039 else if (attr->form == DW_FORM_ref_sig8)
24040 {
24041 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
24042 }
24043 else
24044 {
24045 struct dwarf2_per_objfile *dwarf2_per_objfile
24046 = cu->per_cu->dwarf2_per_objfile;
24047
24048 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24049 " at %s [in module %s]"),
24050 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24051 objfile_name (dwarf2_per_objfile->objfile));
24052 return build_error_marker_type (cu, die);
24053 }
24054 }
24055
24056 /* Load the DIEs associated with type unit PER_CU into memory. */
24057
24058 static void
24059 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
24060 {
24061 struct signatured_type *sig_type;
24062
24063 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24064 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
24065
24066 /* We have the per_cu, but we need the signatured_type.
24067 Fortunately this is an easy translation. */
24068 gdb_assert (per_cu->is_debug_types);
24069 sig_type = (struct signatured_type *) per_cu;
24070
24071 gdb_assert (per_cu->cu == NULL);
24072
24073 read_signatured_type (sig_type);
24074
24075 gdb_assert (per_cu->cu != NULL);
24076 }
24077
24078 /* die_reader_func for read_signatured_type.
24079 This is identical to load_full_comp_unit_reader,
24080 but is kept separate for now. */
24081
24082 static void
24083 read_signatured_type_reader (const struct die_reader_specs *reader,
24084 const gdb_byte *info_ptr,
24085 struct die_info *comp_unit_die,
24086 int has_children,
24087 void *data)
24088 {
24089 struct dwarf2_cu *cu = reader->cu;
24090
24091 gdb_assert (cu->die_hash == NULL);
24092 cu->die_hash =
24093 htab_create_alloc_ex (cu->header.length / 12,
24094 die_hash,
24095 die_eq,
24096 NULL,
24097 &cu->comp_unit_obstack,
24098 hashtab_obstack_allocate,
24099 dummy_obstack_deallocate);
24100
24101 if (has_children)
24102 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
24103 &info_ptr, comp_unit_die);
24104 cu->dies = comp_unit_die;
24105 /* comp_unit_die is not stored in die_hash, no need. */
24106
24107 /* We try not to read any attributes in this function, because not
24108 all CUs needed for references have been loaded yet, and symbol
24109 table processing isn't initialized. But we have to set the CU language,
24110 or we won't be able to build types correctly.
24111 Similarly, if we do not read the producer, we can not apply
24112 producer-specific interpretation. */
24113 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24114 }
24115
24116 /* Read in a signatured type and build its CU and DIEs.
24117 If the type is a stub for the real type in a DWO file,
24118 read in the real type from the DWO file as well. */
24119
24120 static void
24121 read_signatured_type (struct signatured_type *sig_type)
24122 {
24123 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24124
24125 gdb_assert (per_cu->is_debug_types);
24126 gdb_assert (per_cu->cu == NULL);
24127
24128 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
24129 read_signatured_type_reader, NULL);
24130 sig_type->per_cu.tu_read = 1;
24131 }
24132
24133 /* Decode simple location descriptions.
24134 Given a pointer to a dwarf block that defines a location, compute
24135 the location and return the value.
24136
24137 NOTE drow/2003-11-18: This function is called in two situations
24138 now: for the address of static or global variables (partial symbols
24139 only) and for offsets into structures which are expected to be
24140 (more or less) constant. The partial symbol case should go away,
24141 and only the constant case should remain. That will let this
24142 function complain more accurately. A few special modes are allowed
24143 without complaint for global variables (for instance, global
24144 register values and thread-local values).
24145
24146 A location description containing no operations indicates that the
24147 object is optimized out. The return value is 0 for that case.
24148 FIXME drow/2003-11-16: No callers check for this case any more; soon all
24149 callers will only want a very basic result and this can become a
24150 complaint.
24151
24152 Note that stack[0] is unused except as a default error return. */
24153
24154 static CORE_ADDR
24155 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
24156 {
24157 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
24158 size_t i;
24159 size_t size = blk->size;
24160 const gdb_byte *data = blk->data;
24161 CORE_ADDR stack[64];
24162 int stacki;
24163 unsigned int bytes_read, unsnd;
24164 gdb_byte op;
24165
24166 i = 0;
24167 stacki = 0;
24168 stack[stacki] = 0;
24169 stack[++stacki] = 0;
24170
24171 while (i < size)
24172 {
24173 op = data[i++];
24174 switch (op)
24175 {
24176 case DW_OP_lit0:
24177 case DW_OP_lit1:
24178 case DW_OP_lit2:
24179 case DW_OP_lit3:
24180 case DW_OP_lit4:
24181 case DW_OP_lit5:
24182 case DW_OP_lit6:
24183 case DW_OP_lit7:
24184 case DW_OP_lit8:
24185 case DW_OP_lit9:
24186 case DW_OP_lit10:
24187 case DW_OP_lit11:
24188 case DW_OP_lit12:
24189 case DW_OP_lit13:
24190 case DW_OP_lit14:
24191 case DW_OP_lit15:
24192 case DW_OP_lit16:
24193 case DW_OP_lit17:
24194 case DW_OP_lit18:
24195 case DW_OP_lit19:
24196 case DW_OP_lit20:
24197 case DW_OP_lit21:
24198 case DW_OP_lit22:
24199 case DW_OP_lit23:
24200 case DW_OP_lit24:
24201 case DW_OP_lit25:
24202 case DW_OP_lit26:
24203 case DW_OP_lit27:
24204 case DW_OP_lit28:
24205 case DW_OP_lit29:
24206 case DW_OP_lit30:
24207 case DW_OP_lit31:
24208 stack[++stacki] = op - DW_OP_lit0;
24209 break;
24210
24211 case DW_OP_reg0:
24212 case DW_OP_reg1:
24213 case DW_OP_reg2:
24214 case DW_OP_reg3:
24215 case DW_OP_reg4:
24216 case DW_OP_reg5:
24217 case DW_OP_reg6:
24218 case DW_OP_reg7:
24219 case DW_OP_reg8:
24220 case DW_OP_reg9:
24221 case DW_OP_reg10:
24222 case DW_OP_reg11:
24223 case DW_OP_reg12:
24224 case DW_OP_reg13:
24225 case DW_OP_reg14:
24226 case DW_OP_reg15:
24227 case DW_OP_reg16:
24228 case DW_OP_reg17:
24229 case DW_OP_reg18:
24230 case DW_OP_reg19:
24231 case DW_OP_reg20:
24232 case DW_OP_reg21:
24233 case DW_OP_reg22:
24234 case DW_OP_reg23:
24235 case DW_OP_reg24:
24236 case DW_OP_reg25:
24237 case DW_OP_reg26:
24238 case DW_OP_reg27:
24239 case DW_OP_reg28:
24240 case DW_OP_reg29:
24241 case DW_OP_reg30:
24242 case DW_OP_reg31:
24243 stack[++stacki] = op - DW_OP_reg0;
24244 if (i < size)
24245 dwarf2_complex_location_expr_complaint ();
24246 break;
24247
24248 case DW_OP_regx:
24249 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24250 i += bytes_read;
24251 stack[++stacki] = unsnd;
24252 if (i < size)
24253 dwarf2_complex_location_expr_complaint ();
24254 break;
24255
24256 case DW_OP_addr:
24257 stack[++stacki] = read_address (objfile->obfd, &data[i],
24258 cu, &bytes_read);
24259 i += bytes_read;
24260 break;
24261
24262 case DW_OP_const1u:
24263 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24264 i += 1;
24265 break;
24266
24267 case DW_OP_const1s:
24268 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24269 i += 1;
24270 break;
24271
24272 case DW_OP_const2u:
24273 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24274 i += 2;
24275 break;
24276
24277 case DW_OP_const2s:
24278 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24279 i += 2;
24280 break;
24281
24282 case DW_OP_const4u:
24283 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24284 i += 4;
24285 break;
24286
24287 case DW_OP_const4s:
24288 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24289 i += 4;
24290 break;
24291
24292 case DW_OP_const8u:
24293 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24294 i += 8;
24295 break;
24296
24297 case DW_OP_constu:
24298 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24299 &bytes_read);
24300 i += bytes_read;
24301 break;
24302
24303 case DW_OP_consts:
24304 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24305 i += bytes_read;
24306 break;
24307
24308 case DW_OP_dup:
24309 stack[stacki + 1] = stack[stacki];
24310 stacki++;
24311 break;
24312
24313 case DW_OP_plus:
24314 stack[stacki - 1] += stack[stacki];
24315 stacki--;
24316 break;
24317
24318 case DW_OP_plus_uconst:
24319 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24320 &bytes_read);
24321 i += bytes_read;
24322 break;
24323
24324 case DW_OP_minus:
24325 stack[stacki - 1] -= stack[stacki];
24326 stacki--;
24327 break;
24328
24329 case DW_OP_deref:
24330 /* If we're not the last op, then we definitely can't encode
24331 this using GDB's address_class enum. This is valid for partial
24332 global symbols, although the variable's address will be bogus
24333 in the psymtab. */
24334 if (i < size)
24335 dwarf2_complex_location_expr_complaint ();
24336 break;
24337
24338 case DW_OP_GNU_push_tls_address:
24339 case DW_OP_form_tls_address:
24340 /* The top of the stack has the offset from the beginning
24341 of the thread control block at which the variable is located. */
24342 /* Nothing should follow this operator, so the top of stack would
24343 be returned. */
24344 /* This is valid for partial global symbols, but the variable's
24345 address will be bogus in the psymtab. Make it always at least
24346 non-zero to not look as a variable garbage collected by linker
24347 which have DW_OP_addr 0. */
24348 if (i < size)
24349 dwarf2_complex_location_expr_complaint ();
24350 stack[stacki]++;
24351 break;
24352
24353 case DW_OP_GNU_uninit:
24354 break;
24355
24356 case DW_OP_addrx:
24357 case DW_OP_GNU_addr_index:
24358 case DW_OP_GNU_const_index:
24359 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24360 &bytes_read);
24361 i += bytes_read;
24362 break;
24363
24364 default:
24365 {
24366 const char *name = get_DW_OP_name (op);
24367
24368 if (name)
24369 complaint (_("unsupported stack op: '%s'"),
24370 name);
24371 else
24372 complaint (_("unsupported stack op: '%02x'"),
24373 op);
24374 }
24375
24376 return (stack[stacki]);
24377 }
24378
24379 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24380 outside of the allocated space. Also enforce minimum>0. */
24381 if (stacki >= ARRAY_SIZE (stack) - 1)
24382 {
24383 complaint (_("location description stack overflow"));
24384 return 0;
24385 }
24386
24387 if (stacki <= 0)
24388 {
24389 complaint (_("location description stack underflow"));
24390 return 0;
24391 }
24392 }
24393 return (stack[stacki]);
24394 }
24395
24396 /* memory allocation interface */
24397
24398 static struct dwarf_block *
24399 dwarf_alloc_block (struct dwarf2_cu *cu)
24400 {
24401 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24402 }
24403
24404 static struct die_info *
24405 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24406 {
24407 struct die_info *die;
24408 size_t size = sizeof (struct die_info);
24409
24410 if (num_attrs > 1)
24411 size += (num_attrs - 1) * sizeof (struct attribute);
24412
24413 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24414 memset (die, 0, sizeof (struct die_info));
24415 return (die);
24416 }
24417
24418 \f
24419 /* Macro support. */
24420
24421 /* Return file name relative to the compilation directory of file number I in
24422 *LH's file name table. The result is allocated using xmalloc; the caller is
24423 responsible for freeing it. */
24424
24425 static char *
24426 file_file_name (int file, struct line_header *lh)
24427 {
24428 /* Is the file number a valid index into the line header's file name
24429 table? Remember that file numbers start with one, not zero. */
24430 if (lh->is_valid_file_index (file))
24431 {
24432 const file_entry *fe = lh->file_name_at (file);
24433
24434 if (!IS_ABSOLUTE_PATH (fe->name))
24435 {
24436 const char *dir = fe->include_dir (lh);
24437 if (dir != NULL)
24438 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
24439 }
24440 return xstrdup (fe->name);
24441 }
24442 else
24443 {
24444 /* The compiler produced a bogus file number. We can at least
24445 record the macro definitions made in the file, even if we
24446 won't be able to find the file by name. */
24447 char fake_name[80];
24448
24449 xsnprintf (fake_name, sizeof (fake_name),
24450 "<bad macro file number %d>", file);
24451
24452 complaint (_("bad file number in macro information (%d)"),
24453 file);
24454
24455 return xstrdup (fake_name);
24456 }
24457 }
24458
24459 /* Return the full name of file number I in *LH's file name table.
24460 Use COMP_DIR as the name of the current directory of the
24461 compilation. The result is allocated using xmalloc; the caller is
24462 responsible for freeing it. */
24463 static char *
24464 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24465 {
24466 /* Is the file number a valid index into the line header's file name
24467 table? Remember that file numbers start with one, not zero. */
24468 if (lh->is_valid_file_index (file))
24469 {
24470 char *relative = file_file_name (file, lh);
24471
24472 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24473 return relative;
24474 return reconcat (relative, comp_dir, SLASH_STRING,
24475 relative, (char *) NULL);
24476 }
24477 else
24478 return file_file_name (file, lh);
24479 }
24480
24481
24482 static struct macro_source_file *
24483 macro_start_file (struct dwarf2_cu *cu,
24484 int file, int line,
24485 struct macro_source_file *current_file,
24486 struct line_header *lh)
24487 {
24488 /* File name relative to the compilation directory of this source file. */
24489 char *file_name = file_file_name (file, lh);
24490
24491 if (! current_file)
24492 {
24493 /* Note: We don't create a macro table for this compilation unit
24494 at all until we actually get a filename. */
24495 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
24496
24497 /* If we have no current file, then this must be the start_file
24498 directive for the compilation unit's main source file. */
24499 current_file = macro_set_main (macro_table, file_name);
24500 macro_define_special (macro_table);
24501 }
24502 else
24503 current_file = macro_include (current_file, line, file_name);
24504
24505 xfree (file_name);
24506
24507 return current_file;
24508 }
24509
24510 static const char *
24511 consume_improper_spaces (const char *p, const char *body)
24512 {
24513 if (*p == ' ')
24514 {
24515 complaint (_("macro definition contains spaces "
24516 "in formal argument list:\n`%s'"),
24517 body);
24518
24519 while (*p == ' ')
24520 p++;
24521 }
24522
24523 return p;
24524 }
24525
24526
24527 static void
24528 parse_macro_definition (struct macro_source_file *file, int line,
24529 const char *body)
24530 {
24531 const char *p;
24532
24533 /* The body string takes one of two forms. For object-like macro
24534 definitions, it should be:
24535
24536 <macro name> " " <definition>
24537
24538 For function-like macro definitions, it should be:
24539
24540 <macro name> "() " <definition>
24541 or
24542 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24543
24544 Spaces may appear only where explicitly indicated, and in the
24545 <definition>.
24546
24547 The Dwarf 2 spec says that an object-like macro's name is always
24548 followed by a space, but versions of GCC around March 2002 omit
24549 the space when the macro's definition is the empty string.
24550
24551 The Dwarf 2 spec says that there should be no spaces between the
24552 formal arguments in a function-like macro's formal argument list,
24553 but versions of GCC around March 2002 include spaces after the
24554 commas. */
24555
24556
24557 /* Find the extent of the macro name. The macro name is terminated
24558 by either a space or null character (for an object-like macro) or
24559 an opening paren (for a function-like macro). */
24560 for (p = body; *p; p++)
24561 if (*p == ' ' || *p == '(')
24562 break;
24563
24564 if (*p == ' ' || *p == '\0')
24565 {
24566 /* It's an object-like macro. */
24567 int name_len = p - body;
24568 std::string name (body, name_len);
24569 const char *replacement;
24570
24571 if (*p == ' ')
24572 replacement = body + name_len + 1;
24573 else
24574 {
24575 dwarf2_macro_malformed_definition_complaint (body);
24576 replacement = body + name_len;
24577 }
24578
24579 macro_define_object (file, line, name.c_str (), replacement);
24580 }
24581 else if (*p == '(')
24582 {
24583 /* It's a function-like macro. */
24584 std::string name (body, p - body);
24585 int argc = 0;
24586 int argv_size = 1;
24587 char **argv = XNEWVEC (char *, argv_size);
24588
24589 p++;
24590
24591 p = consume_improper_spaces (p, body);
24592
24593 /* Parse the formal argument list. */
24594 while (*p && *p != ')')
24595 {
24596 /* Find the extent of the current argument name. */
24597 const char *arg_start = p;
24598
24599 while (*p && *p != ',' && *p != ')' && *p != ' ')
24600 p++;
24601
24602 if (! *p || p == arg_start)
24603 dwarf2_macro_malformed_definition_complaint (body);
24604 else
24605 {
24606 /* Make sure argv has room for the new argument. */
24607 if (argc >= argv_size)
24608 {
24609 argv_size *= 2;
24610 argv = XRESIZEVEC (char *, argv, argv_size);
24611 }
24612
24613 argv[argc++] = savestring (arg_start, p - arg_start);
24614 }
24615
24616 p = consume_improper_spaces (p, body);
24617
24618 /* Consume the comma, if present. */
24619 if (*p == ',')
24620 {
24621 p++;
24622
24623 p = consume_improper_spaces (p, body);
24624 }
24625 }
24626
24627 if (*p == ')')
24628 {
24629 p++;
24630
24631 if (*p == ' ')
24632 /* Perfectly formed definition, no complaints. */
24633 macro_define_function (file, line, name.c_str (),
24634 argc, (const char **) argv,
24635 p + 1);
24636 else if (*p == '\0')
24637 {
24638 /* Complain, but do define it. */
24639 dwarf2_macro_malformed_definition_complaint (body);
24640 macro_define_function (file, line, name.c_str (),
24641 argc, (const char **) argv,
24642 p);
24643 }
24644 else
24645 /* Just complain. */
24646 dwarf2_macro_malformed_definition_complaint (body);
24647 }
24648 else
24649 /* Just complain. */
24650 dwarf2_macro_malformed_definition_complaint (body);
24651
24652 {
24653 int i;
24654
24655 for (i = 0; i < argc; i++)
24656 xfree (argv[i]);
24657 }
24658 xfree (argv);
24659 }
24660 else
24661 dwarf2_macro_malformed_definition_complaint (body);
24662 }
24663
24664 /* Skip some bytes from BYTES according to the form given in FORM.
24665 Returns the new pointer. */
24666
24667 static const gdb_byte *
24668 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24669 enum dwarf_form form,
24670 unsigned int offset_size,
24671 struct dwarf2_section_info *section)
24672 {
24673 unsigned int bytes_read;
24674
24675 switch (form)
24676 {
24677 case DW_FORM_data1:
24678 case DW_FORM_flag:
24679 ++bytes;
24680 break;
24681
24682 case DW_FORM_data2:
24683 bytes += 2;
24684 break;
24685
24686 case DW_FORM_data4:
24687 bytes += 4;
24688 break;
24689
24690 case DW_FORM_data8:
24691 bytes += 8;
24692 break;
24693
24694 case DW_FORM_data16:
24695 bytes += 16;
24696 break;
24697
24698 case DW_FORM_string:
24699 read_direct_string (abfd, bytes, &bytes_read);
24700 bytes += bytes_read;
24701 break;
24702
24703 case DW_FORM_sec_offset:
24704 case DW_FORM_strp:
24705 case DW_FORM_GNU_strp_alt:
24706 bytes += offset_size;
24707 break;
24708
24709 case DW_FORM_block:
24710 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24711 bytes += bytes_read;
24712 break;
24713
24714 case DW_FORM_block1:
24715 bytes += 1 + read_1_byte (abfd, bytes);
24716 break;
24717 case DW_FORM_block2:
24718 bytes += 2 + read_2_bytes (abfd, bytes);
24719 break;
24720 case DW_FORM_block4:
24721 bytes += 4 + read_4_bytes (abfd, bytes);
24722 break;
24723
24724 case DW_FORM_addrx:
24725 case DW_FORM_sdata:
24726 case DW_FORM_strx:
24727 case DW_FORM_udata:
24728 case DW_FORM_GNU_addr_index:
24729 case DW_FORM_GNU_str_index:
24730 bytes = gdb_skip_leb128 (bytes, buffer_end);
24731 if (bytes == NULL)
24732 {
24733 dwarf2_section_buffer_overflow_complaint (section);
24734 return NULL;
24735 }
24736 break;
24737
24738 case DW_FORM_implicit_const:
24739 break;
24740
24741 default:
24742 {
24743 complaint (_("invalid form 0x%x in `%s'"),
24744 form, get_section_name (section));
24745 return NULL;
24746 }
24747 }
24748
24749 return bytes;
24750 }
24751
24752 /* A helper for dwarf_decode_macros that handles skipping an unknown
24753 opcode. Returns an updated pointer to the macro data buffer; or,
24754 on error, issues a complaint and returns NULL. */
24755
24756 static const gdb_byte *
24757 skip_unknown_opcode (unsigned int opcode,
24758 const gdb_byte **opcode_definitions,
24759 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24760 bfd *abfd,
24761 unsigned int offset_size,
24762 struct dwarf2_section_info *section)
24763 {
24764 unsigned int bytes_read, i;
24765 unsigned long arg;
24766 const gdb_byte *defn;
24767
24768 if (opcode_definitions[opcode] == NULL)
24769 {
24770 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24771 opcode);
24772 return NULL;
24773 }
24774
24775 defn = opcode_definitions[opcode];
24776 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24777 defn += bytes_read;
24778
24779 for (i = 0; i < arg; ++i)
24780 {
24781 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24782 (enum dwarf_form) defn[i], offset_size,
24783 section);
24784 if (mac_ptr == NULL)
24785 {
24786 /* skip_form_bytes already issued the complaint. */
24787 return NULL;
24788 }
24789 }
24790
24791 return mac_ptr;
24792 }
24793
24794 /* A helper function which parses the header of a macro section.
24795 If the macro section is the extended (for now called "GNU") type,
24796 then this updates *OFFSET_SIZE. Returns a pointer to just after
24797 the header, or issues a complaint and returns NULL on error. */
24798
24799 static const gdb_byte *
24800 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24801 bfd *abfd,
24802 const gdb_byte *mac_ptr,
24803 unsigned int *offset_size,
24804 int section_is_gnu)
24805 {
24806 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24807
24808 if (section_is_gnu)
24809 {
24810 unsigned int version, flags;
24811
24812 version = read_2_bytes (abfd, mac_ptr);
24813 if (version != 4 && version != 5)
24814 {
24815 complaint (_("unrecognized version `%d' in .debug_macro section"),
24816 version);
24817 return NULL;
24818 }
24819 mac_ptr += 2;
24820
24821 flags = read_1_byte (abfd, mac_ptr);
24822 ++mac_ptr;
24823 *offset_size = (flags & 1) ? 8 : 4;
24824
24825 if ((flags & 2) != 0)
24826 /* We don't need the line table offset. */
24827 mac_ptr += *offset_size;
24828
24829 /* Vendor opcode descriptions. */
24830 if ((flags & 4) != 0)
24831 {
24832 unsigned int i, count;
24833
24834 count = read_1_byte (abfd, mac_ptr);
24835 ++mac_ptr;
24836 for (i = 0; i < count; ++i)
24837 {
24838 unsigned int opcode, bytes_read;
24839 unsigned long arg;
24840
24841 opcode = read_1_byte (abfd, mac_ptr);
24842 ++mac_ptr;
24843 opcode_definitions[opcode] = mac_ptr;
24844 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24845 mac_ptr += bytes_read;
24846 mac_ptr += arg;
24847 }
24848 }
24849 }
24850
24851 return mac_ptr;
24852 }
24853
24854 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24855 including DW_MACRO_import. */
24856
24857 static void
24858 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24859 bfd *abfd,
24860 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24861 struct macro_source_file *current_file,
24862 struct line_header *lh,
24863 struct dwarf2_section_info *section,
24864 int section_is_gnu, int section_is_dwz,
24865 unsigned int offset_size,
24866 htab_t include_hash)
24867 {
24868 struct dwarf2_per_objfile *dwarf2_per_objfile
24869 = cu->per_cu->dwarf2_per_objfile;
24870 struct objfile *objfile = dwarf2_per_objfile->objfile;
24871 enum dwarf_macro_record_type macinfo_type;
24872 int at_commandline;
24873 const gdb_byte *opcode_definitions[256];
24874
24875 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24876 &offset_size, section_is_gnu);
24877 if (mac_ptr == NULL)
24878 {
24879 /* We already issued a complaint. */
24880 return;
24881 }
24882
24883 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24884 GDB is still reading the definitions from command line. First
24885 DW_MACINFO_start_file will need to be ignored as it was already executed
24886 to create CURRENT_FILE for the main source holding also the command line
24887 definitions. On first met DW_MACINFO_start_file this flag is reset to
24888 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24889
24890 at_commandline = 1;
24891
24892 do
24893 {
24894 /* Do we at least have room for a macinfo type byte? */
24895 if (mac_ptr >= mac_end)
24896 {
24897 dwarf2_section_buffer_overflow_complaint (section);
24898 break;
24899 }
24900
24901 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24902 mac_ptr++;
24903
24904 /* Note that we rely on the fact that the corresponding GNU and
24905 DWARF constants are the same. */
24906 DIAGNOSTIC_PUSH
24907 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24908 switch (macinfo_type)
24909 {
24910 /* A zero macinfo type indicates the end of the macro
24911 information. */
24912 case 0:
24913 break;
24914
24915 case DW_MACRO_define:
24916 case DW_MACRO_undef:
24917 case DW_MACRO_define_strp:
24918 case DW_MACRO_undef_strp:
24919 case DW_MACRO_define_sup:
24920 case DW_MACRO_undef_sup:
24921 {
24922 unsigned int bytes_read;
24923 int line;
24924 const char *body;
24925 int is_define;
24926
24927 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24928 mac_ptr += bytes_read;
24929
24930 if (macinfo_type == DW_MACRO_define
24931 || macinfo_type == DW_MACRO_undef)
24932 {
24933 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24934 mac_ptr += bytes_read;
24935 }
24936 else
24937 {
24938 LONGEST str_offset;
24939
24940 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24941 mac_ptr += offset_size;
24942
24943 if (macinfo_type == DW_MACRO_define_sup
24944 || macinfo_type == DW_MACRO_undef_sup
24945 || section_is_dwz)
24946 {
24947 struct dwz_file *dwz
24948 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24949
24950 body = read_indirect_string_from_dwz (objfile,
24951 dwz, str_offset);
24952 }
24953 else
24954 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24955 abfd, str_offset);
24956 }
24957
24958 is_define = (macinfo_type == DW_MACRO_define
24959 || macinfo_type == DW_MACRO_define_strp
24960 || macinfo_type == DW_MACRO_define_sup);
24961 if (! current_file)
24962 {
24963 /* DWARF violation as no main source is present. */
24964 complaint (_("debug info with no main source gives macro %s "
24965 "on line %d: %s"),
24966 is_define ? _("definition") : _("undefinition"),
24967 line, body);
24968 break;
24969 }
24970 if ((line == 0 && !at_commandline)
24971 || (line != 0 && at_commandline))
24972 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
24973 at_commandline ? _("command-line") : _("in-file"),
24974 is_define ? _("definition") : _("undefinition"),
24975 line == 0 ? _("zero") : _("non-zero"), line, body);
24976
24977 if (body == NULL)
24978 {
24979 /* Fedora's rpm-build's "debugedit" binary
24980 corrupted .debug_macro sections.
24981
24982 For more info, see
24983 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24984 complaint (_("debug info gives %s invalid macro %s "
24985 "without body (corrupted?) at line %d "
24986 "on file %s"),
24987 at_commandline ? _("command-line") : _("in-file"),
24988 is_define ? _("definition") : _("undefinition"),
24989 line, current_file->filename);
24990 }
24991 else if (is_define)
24992 parse_macro_definition (current_file, line, body);
24993 else
24994 {
24995 gdb_assert (macinfo_type == DW_MACRO_undef
24996 || macinfo_type == DW_MACRO_undef_strp
24997 || macinfo_type == DW_MACRO_undef_sup);
24998 macro_undef (current_file, line, body);
24999 }
25000 }
25001 break;
25002
25003 case DW_MACRO_start_file:
25004 {
25005 unsigned int bytes_read;
25006 int line, file;
25007
25008 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25009 mac_ptr += bytes_read;
25010 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25011 mac_ptr += bytes_read;
25012
25013 if ((line == 0 && !at_commandline)
25014 || (line != 0 && at_commandline))
25015 complaint (_("debug info gives source %d included "
25016 "from %s at %s line %d"),
25017 file, at_commandline ? _("command-line") : _("file"),
25018 line == 0 ? _("zero") : _("non-zero"), line);
25019
25020 if (at_commandline)
25021 {
25022 /* This DW_MACRO_start_file was executed in the
25023 pass one. */
25024 at_commandline = 0;
25025 }
25026 else
25027 current_file = macro_start_file (cu, file, line, current_file,
25028 lh);
25029 }
25030 break;
25031
25032 case DW_MACRO_end_file:
25033 if (! current_file)
25034 complaint (_("macro debug info has an unmatched "
25035 "`close_file' directive"));
25036 else
25037 {
25038 current_file = current_file->included_by;
25039 if (! current_file)
25040 {
25041 enum dwarf_macro_record_type next_type;
25042
25043 /* GCC circa March 2002 doesn't produce the zero
25044 type byte marking the end of the compilation
25045 unit. Complain if it's not there, but exit no
25046 matter what. */
25047
25048 /* Do we at least have room for a macinfo type byte? */
25049 if (mac_ptr >= mac_end)
25050 {
25051 dwarf2_section_buffer_overflow_complaint (section);
25052 return;
25053 }
25054
25055 /* We don't increment mac_ptr here, so this is just
25056 a look-ahead. */
25057 next_type
25058 = (enum dwarf_macro_record_type) read_1_byte (abfd,
25059 mac_ptr);
25060 if (next_type != 0)
25061 complaint (_("no terminating 0-type entry for "
25062 "macros in `.debug_macinfo' section"));
25063
25064 return;
25065 }
25066 }
25067 break;
25068
25069 case DW_MACRO_import:
25070 case DW_MACRO_import_sup:
25071 {
25072 LONGEST offset;
25073 void **slot;
25074 bfd *include_bfd = abfd;
25075 struct dwarf2_section_info *include_section = section;
25076 const gdb_byte *include_mac_end = mac_end;
25077 int is_dwz = section_is_dwz;
25078 const gdb_byte *new_mac_ptr;
25079
25080 offset = read_offset_1 (abfd, mac_ptr, offset_size);
25081 mac_ptr += offset_size;
25082
25083 if (macinfo_type == DW_MACRO_import_sup)
25084 {
25085 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
25086
25087 dwarf2_read_section (objfile, &dwz->macro);
25088
25089 include_section = &dwz->macro;
25090 include_bfd = get_section_bfd_owner (include_section);
25091 include_mac_end = dwz->macro.buffer + dwz->macro.size;
25092 is_dwz = 1;
25093 }
25094
25095 new_mac_ptr = include_section->buffer + offset;
25096 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
25097
25098 if (*slot != NULL)
25099 {
25100 /* This has actually happened; see
25101 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
25102 complaint (_("recursive DW_MACRO_import in "
25103 ".debug_macro section"));
25104 }
25105 else
25106 {
25107 *slot = (void *) new_mac_ptr;
25108
25109 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
25110 include_mac_end, current_file, lh,
25111 section, section_is_gnu, is_dwz,
25112 offset_size, include_hash);
25113
25114 htab_remove_elt (include_hash, (void *) new_mac_ptr);
25115 }
25116 }
25117 break;
25118
25119 case DW_MACINFO_vendor_ext:
25120 if (!section_is_gnu)
25121 {
25122 unsigned int bytes_read;
25123
25124 /* This reads the constant, but since we don't recognize
25125 any vendor extensions, we ignore it. */
25126 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25127 mac_ptr += bytes_read;
25128 read_direct_string (abfd, mac_ptr, &bytes_read);
25129 mac_ptr += bytes_read;
25130
25131 /* We don't recognize any vendor extensions. */
25132 break;
25133 }
25134 /* FALLTHROUGH */
25135
25136 default:
25137 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25138 mac_ptr, mac_end, abfd, offset_size,
25139 section);
25140 if (mac_ptr == NULL)
25141 return;
25142 break;
25143 }
25144 DIAGNOSTIC_POP
25145 } while (macinfo_type != 0);
25146 }
25147
25148 static void
25149 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
25150 int section_is_gnu)
25151 {
25152 struct dwarf2_per_objfile *dwarf2_per_objfile
25153 = cu->per_cu->dwarf2_per_objfile;
25154 struct objfile *objfile = dwarf2_per_objfile->objfile;
25155 struct line_header *lh = cu->line_header;
25156 bfd *abfd;
25157 const gdb_byte *mac_ptr, *mac_end;
25158 struct macro_source_file *current_file = 0;
25159 enum dwarf_macro_record_type macinfo_type;
25160 unsigned int offset_size = cu->header.offset_size;
25161 const gdb_byte *opcode_definitions[256];
25162 void **slot;
25163 struct dwarf2_section_info *section;
25164 const char *section_name;
25165
25166 if (cu->dwo_unit != NULL)
25167 {
25168 if (section_is_gnu)
25169 {
25170 section = &cu->dwo_unit->dwo_file->sections.macro;
25171 section_name = ".debug_macro.dwo";
25172 }
25173 else
25174 {
25175 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25176 section_name = ".debug_macinfo.dwo";
25177 }
25178 }
25179 else
25180 {
25181 if (section_is_gnu)
25182 {
25183 section = &dwarf2_per_objfile->macro;
25184 section_name = ".debug_macro";
25185 }
25186 else
25187 {
25188 section = &dwarf2_per_objfile->macinfo;
25189 section_name = ".debug_macinfo";
25190 }
25191 }
25192
25193 dwarf2_read_section (objfile, section);
25194 if (section->buffer == NULL)
25195 {
25196 complaint (_("missing %s section"), section_name);
25197 return;
25198 }
25199 abfd = get_section_bfd_owner (section);
25200
25201 /* First pass: Find the name of the base filename.
25202 This filename is needed in order to process all macros whose definition
25203 (or undefinition) comes from the command line. These macros are defined
25204 before the first DW_MACINFO_start_file entry, and yet still need to be
25205 associated to the base file.
25206
25207 To determine the base file name, we scan the macro definitions until we
25208 reach the first DW_MACINFO_start_file entry. We then initialize
25209 CURRENT_FILE accordingly so that any macro definition found before the
25210 first DW_MACINFO_start_file can still be associated to the base file. */
25211
25212 mac_ptr = section->buffer + offset;
25213 mac_end = section->buffer + section->size;
25214
25215 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25216 &offset_size, section_is_gnu);
25217 if (mac_ptr == NULL)
25218 {
25219 /* We already issued a complaint. */
25220 return;
25221 }
25222
25223 do
25224 {
25225 /* Do we at least have room for a macinfo type byte? */
25226 if (mac_ptr >= mac_end)
25227 {
25228 /* Complaint is printed during the second pass as GDB will probably
25229 stop the first pass earlier upon finding
25230 DW_MACINFO_start_file. */
25231 break;
25232 }
25233
25234 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
25235 mac_ptr++;
25236
25237 /* Note that we rely on the fact that the corresponding GNU and
25238 DWARF constants are the same. */
25239 DIAGNOSTIC_PUSH
25240 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
25241 switch (macinfo_type)
25242 {
25243 /* A zero macinfo type indicates the end of the macro
25244 information. */
25245 case 0:
25246 break;
25247
25248 case DW_MACRO_define:
25249 case DW_MACRO_undef:
25250 /* Only skip the data by MAC_PTR. */
25251 {
25252 unsigned int bytes_read;
25253
25254 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25255 mac_ptr += bytes_read;
25256 read_direct_string (abfd, mac_ptr, &bytes_read);
25257 mac_ptr += bytes_read;
25258 }
25259 break;
25260
25261 case DW_MACRO_start_file:
25262 {
25263 unsigned int bytes_read;
25264 int line, file;
25265
25266 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25267 mac_ptr += bytes_read;
25268 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25269 mac_ptr += bytes_read;
25270
25271 current_file = macro_start_file (cu, file, line, current_file, lh);
25272 }
25273 break;
25274
25275 case DW_MACRO_end_file:
25276 /* No data to skip by MAC_PTR. */
25277 break;
25278
25279 case DW_MACRO_define_strp:
25280 case DW_MACRO_undef_strp:
25281 case DW_MACRO_define_sup:
25282 case DW_MACRO_undef_sup:
25283 {
25284 unsigned int bytes_read;
25285
25286 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25287 mac_ptr += bytes_read;
25288 mac_ptr += offset_size;
25289 }
25290 break;
25291
25292 case DW_MACRO_import:
25293 case DW_MACRO_import_sup:
25294 /* Note that, according to the spec, a transparent include
25295 chain cannot call DW_MACRO_start_file. So, we can just
25296 skip this opcode. */
25297 mac_ptr += offset_size;
25298 break;
25299
25300 case DW_MACINFO_vendor_ext:
25301 /* Only skip the data by MAC_PTR. */
25302 if (!section_is_gnu)
25303 {
25304 unsigned int bytes_read;
25305
25306 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25307 mac_ptr += bytes_read;
25308 read_direct_string (abfd, mac_ptr, &bytes_read);
25309 mac_ptr += bytes_read;
25310 }
25311 /* FALLTHROUGH */
25312
25313 default:
25314 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25315 mac_ptr, mac_end, abfd, offset_size,
25316 section);
25317 if (mac_ptr == NULL)
25318 return;
25319 break;
25320 }
25321 DIAGNOSTIC_POP
25322 } while (macinfo_type != 0 && current_file == NULL);
25323
25324 /* Second pass: Process all entries.
25325
25326 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25327 command-line macro definitions/undefinitions. This flag is unset when we
25328 reach the first DW_MACINFO_start_file entry. */
25329
25330 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25331 htab_eq_pointer,
25332 NULL, xcalloc, xfree));
25333 mac_ptr = section->buffer + offset;
25334 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
25335 *slot = (void *) mac_ptr;
25336 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
25337 current_file, lh, section,
25338 section_is_gnu, 0, offset_size,
25339 include_hash.get ());
25340 }
25341
25342 /* Check if the attribute's form is a DW_FORM_block*
25343 if so return true else false. */
25344
25345 static int
25346 attr_form_is_block (const struct attribute *attr)
25347 {
25348 return (attr == NULL ? 0 :
25349 attr->form == DW_FORM_block1
25350 || attr->form == DW_FORM_block2
25351 || attr->form == DW_FORM_block4
25352 || attr->form == DW_FORM_block
25353 || attr->form == DW_FORM_exprloc);
25354 }
25355
25356 /* Return non-zero if ATTR's value is a section offset --- classes
25357 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25358 You may use DW_UNSND (attr) to retrieve such offsets.
25359
25360 Section 7.5.4, "Attribute Encodings", explains that no attribute
25361 may have a value that belongs to more than one of these classes; it
25362 would be ambiguous if we did, because we use the same forms for all
25363 of them. */
25364
25365 static int
25366 attr_form_is_section_offset (const struct attribute *attr)
25367 {
25368 return (attr->form == DW_FORM_data4
25369 || attr->form == DW_FORM_data8
25370 || attr->form == DW_FORM_sec_offset);
25371 }
25372
25373 /* Return non-zero if ATTR's value falls in the 'constant' class, or
25374 zero otherwise. When this function returns true, you can apply
25375 dwarf2_get_attr_constant_value to it.
25376
25377 However, note that for some attributes you must check
25378 attr_form_is_section_offset before using this test. DW_FORM_data4
25379 and DW_FORM_data8 are members of both the constant class, and of
25380 the classes that contain offsets into other debug sections
25381 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25382 that, if an attribute's can be either a constant or one of the
25383 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25384 taken as section offsets, not constants.
25385
25386 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25387 cannot handle that. */
25388
25389 static int
25390 attr_form_is_constant (const struct attribute *attr)
25391 {
25392 switch (attr->form)
25393 {
25394 case DW_FORM_sdata:
25395 case DW_FORM_udata:
25396 case DW_FORM_data1:
25397 case DW_FORM_data2:
25398 case DW_FORM_data4:
25399 case DW_FORM_data8:
25400 case DW_FORM_implicit_const:
25401 return 1;
25402 default:
25403 return 0;
25404 }
25405 }
25406
25407
25408 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25409 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25410
25411 static int
25412 attr_form_is_ref (const struct attribute *attr)
25413 {
25414 switch (attr->form)
25415 {
25416 case DW_FORM_ref_addr:
25417 case DW_FORM_ref1:
25418 case DW_FORM_ref2:
25419 case DW_FORM_ref4:
25420 case DW_FORM_ref8:
25421 case DW_FORM_ref_udata:
25422 case DW_FORM_GNU_ref_alt:
25423 return 1;
25424 default:
25425 return 0;
25426 }
25427 }
25428
25429 /* Return the .debug_loc section to use for CU.
25430 For DWO files use .debug_loc.dwo. */
25431
25432 static struct dwarf2_section_info *
25433 cu_debug_loc_section (struct dwarf2_cu *cu)
25434 {
25435 struct dwarf2_per_objfile *dwarf2_per_objfile
25436 = cu->per_cu->dwarf2_per_objfile;
25437
25438 if (cu->dwo_unit)
25439 {
25440 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25441
25442 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25443 }
25444 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25445 : &dwarf2_per_objfile->loc);
25446 }
25447
25448 /* A helper function that fills in a dwarf2_loclist_baton. */
25449
25450 static void
25451 fill_in_loclist_baton (struct dwarf2_cu *cu,
25452 struct dwarf2_loclist_baton *baton,
25453 const struct attribute *attr)
25454 {
25455 struct dwarf2_per_objfile *dwarf2_per_objfile
25456 = cu->per_cu->dwarf2_per_objfile;
25457 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25458
25459 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25460
25461 baton->per_cu = cu->per_cu;
25462 gdb_assert (baton->per_cu);
25463 /* We don't know how long the location list is, but make sure we
25464 don't run off the edge of the section. */
25465 baton->size = section->size - DW_UNSND (attr);
25466 baton->data = section->buffer + DW_UNSND (attr);
25467 baton->base_address = cu->base_address;
25468 baton->from_dwo = cu->dwo_unit != NULL;
25469 }
25470
25471 static void
25472 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25473 struct dwarf2_cu *cu, int is_block)
25474 {
25475 struct dwarf2_per_objfile *dwarf2_per_objfile
25476 = cu->per_cu->dwarf2_per_objfile;
25477 struct objfile *objfile = dwarf2_per_objfile->objfile;
25478 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25479
25480 if (attr_form_is_section_offset (attr)
25481 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25482 the section. If so, fall through to the complaint in the
25483 other branch. */
25484 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25485 {
25486 struct dwarf2_loclist_baton *baton;
25487
25488 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25489
25490 fill_in_loclist_baton (cu, baton, attr);
25491
25492 if (cu->base_known == 0)
25493 complaint (_("Location list used without "
25494 "specifying the CU base address."));
25495
25496 SYMBOL_ACLASS_INDEX (sym) = (is_block
25497 ? dwarf2_loclist_block_index
25498 : dwarf2_loclist_index);
25499 SYMBOL_LOCATION_BATON (sym) = baton;
25500 }
25501 else
25502 {
25503 struct dwarf2_locexpr_baton *baton;
25504
25505 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25506 baton->per_cu = cu->per_cu;
25507 gdb_assert (baton->per_cu);
25508
25509 if (attr_form_is_block (attr))
25510 {
25511 /* Note that we're just copying the block's data pointer
25512 here, not the actual data. We're still pointing into the
25513 info_buffer for SYM's objfile; right now we never release
25514 that buffer, but when we do clean up properly this may
25515 need to change. */
25516 baton->size = DW_BLOCK (attr)->size;
25517 baton->data = DW_BLOCK (attr)->data;
25518 }
25519 else
25520 {
25521 dwarf2_invalid_attrib_class_complaint ("location description",
25522 sym->natural_name ());
25523 baton->size = 0;
25524 }
25525
25526 SYMBOL_ACLASS_INDEX (sym) = (is_block
25527 ? dwarf2_locexpr_block_index
25528 : dwarf2_locexpr_index);
25529 SYMBOL_LOCATION_BATON (sym) = baton;
25530 }
25531 }
25532
25533 /* Return the OBJFILE associated with the compilation unit CU. If CU
25534 came from a separate debuginfo file, then the master objfile is
25535 returned. */
25536
25537 struct objfile *
25538 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25539 {
25540 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25541
25542 /* Return the master objfile, so that we can report and look up the
25543 correct file containing this variable. */
25544 if (objfile->separate_debug_objfile_backlink)
25545 objfile = objfile->separate_debug_objfile_backlink;
25546
25547 return objfile;
25548 }
25549
25550 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25551 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25552 CU_HEADERP first. */
25553
25554 static const struct comp_unit_head *
25555 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25556 struct dwarf2_per_cu_data *per_cu)
25557 {
25558 const gdb_byte *info_ptr;
25559
25560 if (per_cu->cu)
25561 return &per_cu->cu->header;
25562
25563 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25564
25565 memset (cu_headerp, 0, sizeof (*cu_headerp));
25566 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25567 rcuh_kind::COMPILE);
25568
25569 return cu_headerp;
25570 }
25571
25572 /* Return the address size given in the compilation unit header for CU. */
25573
25574 int
25575 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25576 {
25577 struct comp_unit_head cu_header_local;
25578 const struct comp_unit_head *cu_headerp;
25579
25580 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25581
25582 return cu_headerp->addr_size;
25583 }
25584
25585 /* Return the offset size given in the compilation unit header for CU. */
25586
25587 int
25588 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25589 {
25590 struct comp_unit_head cu_header_local;
25591 const struct comp_unit_head *cu_headerp;
25592
25593 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25594
25595 return cu_headerp->offset_size;
25596 }
25597
25598 /* See its dwarf2loc.h declaration. */
25599
25600 int
25601 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25602 {
25603 struct comp_unit_head cu_header_local;
25604 const struct comp_unit_head *cu_headerp;
25605
25606 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25607
25608 if (cu_headerp->version == 2)
25609 return cu_headerp->addr_size;
25610 else
25611 return cu_headerp->offset_size;
25612 }
25613
25614 /* Return the text offset of the CU. The returned offset comes from
25615 this CU's objfile. If this objfile came from a separate debuginfo
25616 file, then the offset may be different from the corresponding
25617 offset in the parent objfile. */
25618
25619 CORE_ADDR
25620 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25621 {
25622 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25623
25624 return objfile->section_offsets[SECT_OFF_TEXT (objfile)];
25625 }
25626
25627 /* Return a type that is a generic pointer type, the size of which matches
25628 the address size given in the compilation unit header for PER_CU. */
25629 static struct type *
25630 dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25631 {
25632 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25633 struct type *void_type = objfile_type (objfile)->builtin_void;
25634 struct type *addr_type = lookup_pointer_type (void_type);
25635 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25636
25637 if (TYPE_LENGTH (addr_type) == addr_size)
25638 return addr_type;
25639
25640 addr_type
25641 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25642 return addr_type;
25643 }
25644
25645 /* Return DWARF version number of PER_CU. */
25646
25647 short
25648 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25649 {
25650 return per_cu->dwarf_version;
25651 }
25652
25653 /* Locate the .debug_info compilation unit from CU's objfile which contains
25654 the DIE at OFFSET. Raises an error on failure. */
25655
25656 static struct dwarf2_per_cu_data *
25657 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25658 unsigned int offset_in_dwz,
25659 struct dwarf2_per_objfile *dwarf2_per_objfile)
25660 {
25661 struct dwarf2_per_cu_data *this_cu;
25662 int low, high;
25663
25664 low = 0;
25665 high = dwarf2_per_objfile->all_comp_units.size () - 1;
25666 while (high > low)
25667 {
25668 struct dwarf2_per_cu_data *mid_cu;
25669 int mid = low + (high - low) / 2;
25670
25671 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25672 if (mid_cu->is_dwz > offset_in_dwz
25673 || (mid_cu->is_dwz == offset_in_dwz
25674 && mid_cu->sect_off + mid_cu->length >= sect_off))
25675 high = mid;
25676 else
25677 low = mid + 1;
25678 }
25679 gdb_assert (low == high);
25680 this_cu = dwarf2_per_objfile->all_comp_units[low];
25681 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25682 {
25683 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25684 error (_("Dwarf Error: could not find partial DIE containing "
25685 "offset %s [in module %s]"),
25686 sect_offset_str (sect_off),
25687 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25688
25689 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25690 <= sect_off);
25691 return dwarf2_per_objfile->all_comp_units[low-1];
25692 }
25693 else
25694 {
25695 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25696 && sect_off >= this_cu->sect_off + this_cu->length)
25697 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25698 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25699 return this_cu;
25700 }
25701 }
25702
25703 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25704
25705 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25706 : per_cu (per_cu_),
25707 mark (false),
25708 has_loclist (false),
25709 checked_producer (false),
25710 producer_is_gxx_lt_4_6 (false),
25711 producer_is_gcc_lt_4_3 (false),
25712 producer_is_icc (false),
25713 producer_is_icc_lt_14 (false),
25714 producer_is_codewarrior (false),
25715 processing_has_namespace_info (false)
25716 {
25717 per_cu->cu = this;
25718 }
25719
25720 /* Destroy a dwarf2_cu. */
25721
25722 dwarf2_cu::~dwarf2_cu ()
25723 {
25724 per_cu->cu = NULL;
25725 }
25726
25727 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25728
25729 static void
25730 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25731 enum language pretend_language)
25732 {
25733 struct attribute *attr;
25734
25735 /* Set the language we're debugging. */
25736 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25737 if (attr != nullptr)
25738 set_cu_language (DW_UNSND (attr), cu);
25739 else
25740 {
25741 cu->language = pretend_language;
25742 cu->language_defn = language_def (cu->language);
25743 }
25744
25745 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25746 }
25747
25748 /* Increase the age counter on each cached compilation unit, and free
25749 any that are too old. */
25750
25751 static void
25752 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25753 {
25754 struct dwarf2_per_cu_data *per_cu, **last_chain;
25755
25756 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25757 per_cu = dwarf2_per_objfile->read_in_chain;
25758 while (per_cu != NULL)
25759 {
25760 per_cu->cu->last_used ++;
25761 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25762 dwarf2_mark (per_cu->cu);
25763 per_cu = per_cu->cu->read_in_chain;
25764 }
25765
25766 per_cu = dwarf2_per_objfile->read_in_chain;
25767 last_chain = &dwarf2_per_objfile->read_in_chain;
25768 while (per_cu != NULL)
25769 {
25770 struct dwarf2_per_cu_data *next_cu;
25771
25772 next_cu = per_cu->cu->read_in_chain;
25773
25774 if (!per_cu->cu->mark)
25775 {
25776 delete per_cu->cu;
25777 *last_chain = next_cu;
25778 }
25779 else
25780 last_chain = &per_cu->cu->read_in_chain;
25781
25782 per_cu = next_cu;
25783 }
25784 }
25785
25786 /* Remove a single compilation unit from the cache. */
25787
25788 static void
25789 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25790 {
25791 struct dwarf2_per_cu_data *per_cu, **last_chain;
25792 struct dwarf2_per_objfile *dwarf2_per_objfile
25793 = target_per_cu->dwarf2_per_objfile;
25794
25795 per_cu = dwarf2_per_objfile->read_in_chain;
25796 last_chain = &dwarf2_per_objfile->read_in_chain;
25797 while (per_cu != NULL)
25798 {
25799 struct dwarf2_per_cu_data *next_cu;
25800
25801 next_cu = per_cu->cu->read_in_chain;
25802
25803 if (per_cu == target_per_cu)
25804 {
25805 delete per_cu->cu;
25806 per_cu->cu = NULL;
25807 *last_chain = next_cu;
25808 break;
25809 }
25810 else
25811 last_chain = &per_cu->cu->read_in_chain;
25812
25813 per_cu = next_cu;
25814 }
25815 }
25816
25817 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25818 We store these in a hash table separate from the DIEs, and preserve them
25819 when the DIEs are flushed out of cache.
25820
25821 The CU "per_cu" pointer is needed because offset alone is not enough to
25822 uniquely identify the type. A file may have multiple .debug_types sections,
25823 or the type may come from a DWO file. Furthermore, while it's more logical
25824 to use per_cu->section+offset, with Fission the section with the data is in
25825 the DWO file but we don't know that section at the point we need it.
25826 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25827 because we can enter the lookup routine, get_die_type_at_offset, from
25828 outside this file, and thus won't necessarily have PER_CU->cu.
25829 Fortunately, PER_CU is stable for the life of the objfile. */
25830
25831 struct dwarf2_per_cu_offset_and_type
25832 {
25833 const struct dwarf2_per_cu_data *per_cu;
25834 sect_offset sect_off;
25835 struct type *type;
25836 };
25837
25838 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25839
25840 static hashval_t
25841 per_cu_offset_and_type_hash (const void *item)
25842 {
25843 const struct dwarf2_per_cu_offset_and_type *ofs
25844 = (const struct dwarf2_per_cu_offset_and_type *) item;
25845
25846 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25847 }
25848
25849 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25850
25851 static int
25852 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25853 {
25854 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25855 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25856 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25857 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25858
25859 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25860 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25861 }
25862
25863 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25864 table if necessary. For convenience, return TYPE.
25865
25866 The DIEs reading must have careful ordering to:
25867 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25868 reading current DIE.
25869 * Not trying to dereference contents of still incompletely read in types
25870 while reading in other DIEs.
25871 * Enable referencing still incompletely read in types just by a pointer to
25872 the type without accessing its fields.
25873
25874 Therefore caller should follow these rules:
25875 * Try to fetch any prerequisite types we may need to build this DIE type
25876 before building the type and calling set_die_type.
25877 * After building type call set_die_type for current DIE as soon as
25878 possible before fetching more types to complete the current type.
25879 * Make the type as complete as possible before fetching more types. */
25880
25881 static struct type *
25882 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25883 {
25884 struct dwarf2_per_objfile *dwarf2_per_objfile
25885 = cu->per_cu->dwarf2_per_objfile;
25886 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25887 struct objfile *objfile = dwarf2_per_objfile->objfile;
25888 struct attribute *attr;
25889 struct dynamic_prop prop;
25890
25891 /* For Ada types, make sure that the gnat-specific data is always
25892 initialized (if not already set). There are a few types where
25893 we should not be doing so, because the type-specific area is
25894 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25895 where the type-specific area is used to store the floatformat).
25896 But this is not a problem, because the gnat-specific information
25897 is actually not needed for these types. */
25898 if (need_gnat_info (cu)
25899 && TYPE_CODE (type) != TYPE_CODE_FUNC
25900 && TYPE_CODE (type) != TYPE_CODE_FLT
25901 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25902 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25903 && TYPE_CODE (type) != TYPE_CODE_METHOD
25904 && !HAVE_GNAT_AUX_INFO (type))
25905 INIT_GNAT_SPECIFIC (type);
25906
25907 /* Read DW_AT_allocated and set in type. */
25908 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25909 if (attr_form_is_block (attr))
25910 {
25911 struct type *prop_type
25912 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25913 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25914 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25915 }
25916 else if (attr != NULL)
25917 {
25918 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25919 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25920 sect_offset_str (die->sect_off));
25921 }
25922
25923 /* Read DW_AT_associated and set in type. */
25924 attr = dwarf2_attr (die, DW_AT_associated, cu);
25925 if (attr_form_is_block (attr))
25926 {
25927 struct type *prop_type
25928 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25929 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25930 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25931 }
25932 else if (attr != NULL)
25933 {
25934 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25935 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25936 sect_offset_str (die->sect_off));
25937 }
25938
25939 /* Read DW_AT_data_location and set in type. */
25940 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25941 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25942 dwarf2_per_cu_addr_type (cu->per_cu)))
25943 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25944
25945 if (dwarf2_per_objfile->die_type_hash == NULL)
25946 {
25947 dwarf2_per_objfile->die_type_hash =
25948 htab_create_alloc_ex (127,
25949 per_cu_offset_and_type_hash,
25950 per_cu_offset_and_type_eq,
25951 NULL,
25952 &objfile->objfile_obstack,
25953 hashtab_obstack_allocate,
25954 dummy_obstack_deallocate);
25955 }
25956
25957 ofs.per_cu = cu->per_cu;
25958 ofs.sect_off = die->sect_off;
25959 ofs.type = type;
25960 slot = (struct dwarf2_per_cu_offset_and_type **)
25961 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25962 if (*slot)
25963 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25964 sect_offset_str (die->sect_off));
25965 *slot = XOBNEW (&objfile->objfile_obstack,
25966 struct dwarf2_per_cu_offset_and_type);
25967 **slot = ofs;
25968 return type;
25969 }
25970
25971 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25972 or return NULL if the die does not have a saved type. */
25973
25974 static struct type *
25975 get_die_type_at_offset (sect_offset sect_off,
25976 struct dwarf2_per_cu_data *per_cu)
25977 {
25978 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25979 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25980
25981 if (dwarf2_per_objfile->die_type_hash == NULL)
25982 return NULL;
25983
25984 ofs.per_cu = per_cu;
25985 ofs.sect_off = sect_off;
25986 slot = ((struct dwarf2_per_cu_offset_and_type *)
25987 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25988 if (slot)
25989 return slot->type;
25990 else
25991 return NULL;
25992 }
25993
25994 /* Look up the type for DIE in CU in die_type_hash,
25995 or return NULL if DIE does not have a saved type. */
25996
25997 static struct type *
25998 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25999 {
26000 return get_die_type_at_offset (die->sect_off, cu->per_cu);
26001 }
26002
26003 /* Add a dependence relationship from CU to REF_PER_CU. */
26004
26005 static void
26006 dwarf2_add_dependence (struct dwarf2_cu *cu,
26007 struct dwarf2_per_cu_data *ref_per_cu)
26008 {
26009 void **slot;
26010
26011 if (cu->dependencies == NULL)
26012 cu->dependencies
26013 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
26014 NULL, &cu->comp_unit_obstack,
26015 hashtab_obstack_allocate,
26016 dummy_obstack_deallocate);
26017
26018 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
26019 if (*slot == NULL)
26020 *slot = ref_per_cu;
26021 }
26022
26023 /* Subroutine of dwarf2_mark to pass to htab_traverse.
26024 Set the mark field in every compilation unit in the
26025 cache that we must keep because we are keeping CU. */
26026
26027 static int
26028 dwarf2_mark_helper (void **slot, void *data)
26029 {
26030 struct dwarf2_per_cu_data *per_cu;
26031
26032 per_cu = (struct dwarf2_per_cu_data *) *slot;
26033
26034 /* cu->dependencies references may not yet have been ever read if QUIT aborts
26035 reading of the chain. As such dependencies remain valid it is not much
26036 useful to track and undo them during QUIT cleanups. */
26037 if (per_cu->cu == NULL)
26038 return 1;
26039
26040 if (per_cu->cu->mark)
26041 return 1;
26042 per_cu->cu->mark = true;
26043
26044 if (per_cu->cu->dependencies != NULL)
26045 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
26046
26047 return 1;
26048 }
26049
26050 /* Set the mark field in CU and in every other compilation unit in the
26051 cache that we must keep because we are keeping CU. */
26052
26053 static void
26054 dwarf2_mark (struct dwarf2_cu *cu)
26055 {
26056 if (cu->mark)
26057 return;
26058 cu->mark = true;
26059 if (cu->dependencies != NULL)
26060 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
26061 }
26062
26063 static void
26064 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
26065 {
26066 while (per_cu)
26067 {
26068 per_cu->cu->mark = false;
26069 per_cu = per_cu->cu->read_in_chain;
26070 }
26071 }
26072
26073 /* Trivial hash function for partial_die_info: the hash value of a DIE
26074 is its offset in .debug_info for this objfile. */
26075
26076 static hashval_t
26077 partial_die_hash (const void *item)
26078 {
26079 const struct partial_die_info *part_die
26080 = (const struct partial_die_info *) item;
26081
26082 return to_underlying (part_die->sect_off);
26083 }
26084
26085 /* Trivial comparison function for partial_die_info structures: two DIEs
26086 are equal if they have the same offset. */
26087
26088 static int
26089 partial_die_eq (const void *item_lhs, const void *item_rhs)
26090 {
26091 const struct partial_die_info *part_die_lhs
26092 = (const struct partial_die_info *) item_lhs;
26093 const struct partial_die_info *part_die_rhs
26094 = (const struct partial_die_info *) item_rhs;
26095
26096 return part_die_lhs->sect_off == part_die_rhs->sect_off;
26097 }
26098
26099 struct cmd_list_element *set_dwarf_cmdlist;
26100 struct cmd_list_element *show_dwarf_cmdlist;
26101
26102 static void
26103 set_dwarf_cmd (const char *args, int from_tty)
26104 {
26105 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
26106 gdb_stdout);
26107 }
26108
26109 static void
26110 show_dwarf_cmd (const char *args, int from_tty)
26111 {
26112 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
26113 }
26114
26115 bool dwarf_always_disassemble;
26116
26117 static void
26118 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
26119 struct cmd_list_element *c, const char *value)
26120 {
26121 fprintf_filtered (file,
26122 _("Whether to always disassemble "
26123 "DWARF expressions is %s.\n"),
26124 value);
26125 }
26126
26127 static void
26128 show_check_physname (struct ui_file *file, int from_tty,
26129 struct cmd_list_element *c, const char *value)
26130 {
26131 fprintf_filtered (file,
26132 _("Whether to check \"physname\" is %s.\n"),
26133 value);
26134 }
26135
26136 void
26137 _initialize_dwarf2_read (void)
26138 {
26139 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26140 Set DWARF specific variables.\n\
26141 Configure DWARF variables such as the cache size."),
26142 &set_dwarf_cmdlist, "maintenance set dwarf ",
26143 0/*allow-unknown*/, &maintenance_set_cmdlist);
26144
26145 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
26146 Show DWARF specific variables.\n\
26147 Show DWARF variables such as the cache size."),
26148 &show_dwarf_cmdlist, "maintenance show dwarf ",
26149 0/*allow-unknown*/, &maintenance_show_cmdlist);
26150
26151 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26152 &dwarf_max_cache_age, _("\
26153 Set the upper bound on the age of cached DWARF compilation units."), _("\
26154 Show the upper bound on the age of cached DWARF compilation units."), _("\
26155 A higher limit means that cached compilation units will be stored\n\
26156 in memory longer, and more total memory will be used. Zero disables\n\
26157 caching, which can slow down startup."),
26158 NULL,
26159 show_dwarf_max_cache_age,
26160 &set_dwarf_cmdlist,
26161 &show_dwarf_cmdlist);
26162
26163 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
26164 &dwarf_always_disassemble, _("\
26165 Set whether `info address' always disassembles DWARF expressions."), _("\
26166 Show whether `info address' always disassembles DWARF expressions."), _("\
26167 When enabled, DWARF expressions are always printed in an assembly-like\n\
26168 syntax. When disabled, expressions will be printed in a more\n\
26169 conversational style, when possible."),
26170 NULL,
26171 show_dwarf_always_disassemble,
26172 &set_dwarf_cmdlist,
26173 &show_dwarf_cmdlist);
26174
26175 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26176 Set debugging of the DWARF reader."), _("\
26177 Show debugging of the DWARF reader."), _("\
26178 When enabled (non-zero), debugging messages are printed during DWARF\n\
26179 reading and symtab expansion. A value of 1 (one) provides basic\n\
26180 information. A value greater than 1 provides more verbose information."),
26181 NULL,
26182 NULL,
26183 &setdebuglist, &showdebuglist);
26184
26185 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26186 Set debugging of the DWARF DIE reader."), _("\
26187 Show debugging of the DWARF DIE reader."), _("\
26188 When enabled (non-zero), DIEs are dumped after they are read in.\n\
26189 The value is the maximum depth to print."),
26190 NULL,
26191 NULL,
26192 &setdebuglist, &showdebuglist);
26193
26194 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
26195 Set debugging of the dwarf line reader."), _("\
26196 Show debugging of the dwarf line reader."), _("\
26197 When enabled (non-zero), line number entries are dumped as they are read in.\n\
26198 A value of 1 (one) provides basic information.\n\
26199 A value greater than 1 provides more verbose information."),
26200 NULL,
26201 NULL,
26202 &setdebuglist, &showdebuglist);
26203
26204 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
26205 Set cross-checking of \"physname\" code against demangler."), _("\
26206 Show cross-checking of \"physname\" code against demangler."), _("\
26207 When enabled, GDB's internal \"physname\" code is checked against\n\
26208 the demangler."),
26209 NULL, show_check_physname,
26210 &setdebuglist, &showdebuglist);
26211
26212 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26213 no_class, &use_deprecated_index_sections, _("\
26214 Set whether to use deprecated gdb_index sections."), _("\
26215 Show whether to use deprecated gdb_index sections."), _("\
26216 When enabled, deprecated .gdb_index sections are used anyway.\n\
26217 Normally they are ignored either because of a missing feature or\n\
26218 performance issue.\n\
26219 Warning: This option must be enabled before gdb reads the file."),
26220 NULL,
26221 NULL,
26222 &setlist, &showlist);
26223
26224 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26225 &dwarf2_locexpr_funcs);
26226 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26227 &dwarf2_loclist_funcs);
26228
26229 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26230 &dwarf2_block_frame_base_locexpr_funcs);
26231 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26232 &dwarf2_block_frame_base_loclist_funcs);
26233
26234 #if GDB_SELF_TEST
26235 selftests::register_test ("dw2_expand_symtabs_matching",
26236 selftests::dw2_expand_symtabs_matching::run_test);
26237 #endif
26238 }
This page took 0.592772 seconds and 4 git commands to generate.