Use std::string in dwarf2read.c
[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 = ANOFFSET (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 = ANOFFSET (objfile->section_offsets,
3220 SECT_OFF_TEXT (objfile));
3221
3222 auto_obstack temp_obstack;
3223 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3224
3225 std::unordered_map<sect_offset,
3226 dwarf2_per_cu_data *,
3227 gdb::hash_enum<sect_offset>>
3228 debug_info_offset_to_per_cu;
3229 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3230 {
3231 const auto insertpair
3232 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3233 if (!insertpair.second)
3234 {
3235 warning (_("Section .debug_aranges in %s has duplicate "
3236 "debug_info_offset %s, ignoring .debug_aranges."),
3237 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3238 return;
3239 }
3240 }
3241
3242 dwarf2_read_section (objfile, section);
3243
3244 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3245
3246 const gdb_byte *addr = section->buffer;
3247
3248 while (addr < section->buffer + section->size)
3249 {
3250 const gdb_byte *const entry_addr = addr;
3251 unsigned int bytes_read;
3252
3253 const LONGEST entry_length = read_initial_length (abfd, addr,
3254 &bytes_read);
3255 addr += bytes_read;
3256
3257 const gdb_byte *const entry_end = addr + entry_length;
3258 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3259 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3260 if (addr + entry_length > section->buffer + section->size)
3261 {
3262 warning (_("Section .debug_aranges in %s entry at offset %s "
3263 "length %s exceeds section length %s, "
3264 "ignoring .debug_aranges."),
3265 objfile_name (objfile),
3266 plongest (entry_addr - section->buffer),
3267 plongest (bytes_read + entry_length),
3268 pulongest (section->size));
3269 return;
3270 }
3271
3272 /* The version number. */
3273 const uint16_t version = read_2_bytes (abfd, addr);
3274 addr += 2;
3275 if (version != 2)
3276 {
3277 warning (_("Section .debug_aranges in %s entry at offset %s "
3278 "has unsupported version %d, ignoring .debug_aranges."),
3279 objfile_name (objfile),
3280 plongest (entry_addr - section->buffer), version);
3281 return;
3282 }
3283
3284 const uint64_t debug_info_offset
3285 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3286 addr += offset_size;
3287 const auto per_cu_it
3288 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3289 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3290 {
3291 warning (_("Section .debug_aranges in %s entry at offset %s "
3292 "debug_info_offset %s does not exists, "
3293 "ignoring .debug_aranges."),
3294 objfile_name (objfile),
3295 plongest (entry_addr - section->buffer),
3296 pulongest (debug_info_offset));
3297 return;
3298 }
3299 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3300
3301 const uint8_t address_size = *addr++;
3302 if (address_size < 1 || address_size > 8)
3303 {
3304 warning (_("Section .debug_aranges in %s entry at offset %s "
3305 "address_size %u is invalid, ignoring .debug_aranges."),
3306 objfile_name (objfile),
3307 plongest (entry_addr - section->buffer), address_size);
3308 return;
3309 }
3310
3311 const uint8_t segment_selector_size = *addr++;
3312 if (segment_selector_size != 0)
3313 {
3314 warning (_("Section .debug_aranges in %s entry at offset %s "
3315 "segment_selector_size %u is not supported, "
3316 "ignoring .debug_aranges."),
3317 objfile_name (objfile),
3318 plongest (entry_addr - section->buffer),
3319 segment_selector_size);
3320 return;
3321 }
3322
3323 /* Must pad to an alignment boundary that is twice the address
3324 size. It is undocumented by the DWARF standard but GCC does
3325 use it. */
3326 for (size_t padding = ((-(addr - section->buffer))
3327 & (2 * address_size - 1));
3328 padding > 0; padding--)
3329 if (*addr++ != 0)
3330 {
3331 warning (_("Section .debug_aranges in %s entry at offset %s "
3332 "padding is not zero, ignoring .debug_aranges."),
3333 objfile_name (objfile),
3334 plongest (entry_addr - section->buffer));
3335 return;
3336 }
3337
3338 for (;;)
3339 {
3340 if (addr + 2 * address_size > entry_end)
3341 {
3342 warning (_("Section .debug_aranges in %s entry at offset %s "
3343 "address list is not properly terminated, "
3344 "ignoring .debug_aranges."),
3345 objfile_name (objfile),
3346 plongest (entry_addr - section->buffer));
3347 return;
3348 }
3349 ULONGEST start = extract_unsigned_integer (addr, address_size,
3350 dwarf5_byte_order);
3351 addr += address_size;
3352 ULONGEST length = extract_unsigned_integer (addr, address_size,
3353 dwarf5_byte_order);
3354 addr += address_size;
3355 if (start == 0 && length == 0)
3356 break;
3357 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3358 {
3359 /* Symbol was eliminated due to a COMDAT group. */
3360 continue;
3361 }
3362 ULONGEST end = start + length;
3363 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3364 - baseaddr);
3365 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3366 - baseaddr);
3367 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3368 }
3369 }
3370
3371 objfile->partial_symtabs->psymtabs_addrmap
3372 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3373 }
3374
3375 /* Find a slot in the mapped index INDEX for the object named NAME.
3376 If NAME is found, set *VEC_OUT to point to the CU vector in the
3377 constant pool and return true. If NAME cannot be found, return
3378 false. */
3379
3380 static bool
3381 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3382 offset_type **vec_out)
3383 {
3384 offset_type hash;
3385 offset_type slot, step;
3386 int (*cmp) (const char *, const char *);
3387
3388 gdb::unique_xmalloc_ptr<char> without_params;
3389 if (current_language->la_language == language_cplus
3390 || current_language->la_language == language_fortran
3391 || current_language->la_language == language_d)
3392 {
3393 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3394 not contain any. */
3395
3396 if (strchr (name, '(') != NULL)
3397 {
3398 without_params = cp_remove_params (name);
3399
3400 if (without_params != NULL)
3401 name = without_params.get ();
3402 }
3403 }
3404
3405 /* Index version 4 did not support case insensitive searches. But the
3406 indices for case insensitive languages are built in lowercase, therefore
3407 simulate our NAME being searched is also lowercased. */
3408 hash = mapped_index_string_hash ((index->version == 4
3409 && case_sensitivity == case_sensitive_off
3410 ? 5 : index->version),
3411 name);
3412
3413 slot = hash & (index->symbol_table.size () - 1);
3414 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3415 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3416
3417 for (;;)
3418 {
3419 const char *str;
3420
3421 const auto &bucket = index->symbol_table[slot];
3422 if (bucket.name == 0 && bucket.vec == 0)
3423 return false;
3424
3425 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3426 if (!cmp (name, str))
3427 {
3428 *vec_out = (offset_type *) (index->constant_pool
3429 + MAYBE_SWAP (bucket.vec));
3430 return true;
3431 }
3432
3433 slot = (slot + step) & (index->symbol_table.size () - 1);
3434 }
3435 }
3436
3437 /* A helper function that reads the .gdb_index from BUFFER and fills
3438 in MAP. FILENAME is the name of the file containing the data;
3439 it is used for error reporting. DEPRECATED_OK is true if it is
3440 ok to use deprecated sections.
3441
3442 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3443 out parameters that are filled in with information about the CU and
3444 TU lists in the section.
3445
3446 Returns true if all went well, false otherwise. */
3447
3448 static bool
3449 read_gdb_index_from_buffer (struct objfile *objfile,
3450 const char *filename,
3451 bool deprecated_ok,
3452 gdb::array_view<const gdb_byte> buffer,
3453 struct mapped_index *map,
3454 const gdb_byte **cu_list,
3455 offset_type *cu_list_elements,
3456 const gdb_byte **types_list,
3457 offset_type *types_list_elements)
3458 {
3459 const gdb_byte *addr = &buffer[0];
3460
3461 /* Version check. */
3462 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3463 /* Versions earlier than 3 emitted every copy of a psymbol. This
3464 causes the index to behave very poorly for certain requests. Version 3
3465 contained incomplete addrmap. So, it seems better to just ignore such
3466 indices. */
3467 if (version < 4)
3468 {
3469 static int warning_printed = 0;
3470 if (!warning_printed)
3471 {
3472 warning (_("Skipping obsolete .gdb_index section in %s."),
3473 filename);
3474 warning_printed = 1;
3475 }
3476 return 0;
3477 }
3478 /* Index version 4 uses a different hash function than index version
3479 5 and later.
3480
3481 Versions earlier than 6 did not emit psymbols for inlined
3482 functions. Using these files will cause GDB not to be able to
3483 set breakpoints on inlined functions by name, so we ignore these
3484 indices unless the user has done
3485 "set use-deprecated-index-sections on". */
3486 if (version < 6 && !deprecated_ok)
3487 {
3488 static int warning_printed = 0;
3489 if (!warning_printed)
3490 {
3491 warning (_("\
3492 Skipping deprecated .gdb_index section in %s.\n\
3493 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3494 to use the section anyway."),
3495 filename);
3496 warning_printed = 1;
3497 }
3498 return 0;
3499 }
3500 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3501 of the TU (for symbols coming from TUs),
3502 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3503 Plus gold-generated indices can have duplicate entries for global symbols,
3504 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3505 These are just performance bugs, and we can't distinguish gdb-generated
3506 indices from gold-generated ones, so issue no warning here. */
3507
3508 /* Indexes with higher version than the one supported by GDB may be no
3509 longer backward compatible. */
3510 if (version > 8)
3511 return 0;
3512
3513 map->version = version;
3514
3515 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3516
3517 int i = 0;
3518 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3519 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3520 / 8);
3521 ++i;
3522
3523 *types_list = addr + MAYBE_SWAP (metadata[i]);
3524 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3525 - MAYBE_SWAP (metadata[i]))
3526 / 8);
3527 ++i;
3528
3529 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3530 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3531 map->address_table
3532 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3533 ++i;
3534
3535 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3536 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3537 map->symbol_table
3538 = gdb::array_view<mapped_index::symbol_table_slot>
3539 ((mapped_index::symbol_table_slot *) symbol_table,
3540 (mapped_index::symbol_table_slot *) symbol_table_end);
3541
3542 ++i;
3543 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3544
3545 return 1;
3546 }
3547
3548 /* Callback types for dwarf2_read_gdb_index. */
3549
3550 typedef gdb::function_view
3551 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3552 get_gdb_index_contents_ftype;
3553 typedef gdb::function_view
3554 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3555 get_gdb_index_contents_dwz_ftype;
3556
3557 /* Read .gdb_index. If everything went ok, initialize the "quick"
3558 elements of all the CUs and return 1. Otherwise, return 0. */
3559
3560 static int
3561 dwarf2_read_gdb_index
3562 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3563 get_gdb_index_contents_ftype get_gdb_index_contents,
3564 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3565 {
3566 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3567 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3568 struct dwz_file *dwz;
3569 struct objfile *objfile = dwarf2_per_objfile->objfile;
3570
3571 gdb::array_view<const gdb_byte> main_index_contents
3572 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3573
3574 if (main_index_contents.empty ())
3575 return 0;
3576
3577 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3578 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3579 use_deprecated_index_sections,
3580 main_index_contents, map.get (), &cu_list,
3581 &cu_list_elements, &types_list,
3582 &types_list_elements))
3583 return 0;
3584
3585 /* Don't use the index if it's empty. */
3586 if (map->symbol_table.empty ())
3587 return 0;
3588
3589 /* If there is a .dwz file, read it so we can get its CU list as
3590 well. */
3591 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3592 if (dwz != NULL)
3593 {
3594 struct mapped_index dwz_map;
3595 const gdb_byte *dwz_types_ignore;
3596 offset_type dwz_types_elements_ignore;
3597
3598 gdb::array_view<const gdb_byte> dwz_index_content
3599 = get_gdb_index_contents_dwz (objfile, dwz);
3600
3601 if (dwz_index_content.empty ())
3602 return 0;
3603
3604 if (!read_gdb_index_from_buffer (objfile,
3605 bfd_get_filename (dwz->dwz_bfd.get ()),
3606 1, dwz_index_content, &dwz_map,
3607 &dwz_list, &dwz_list_elements,
3608 &dwz_types_ignore,
3609 &dwz_types_elements_ignore))
3610 {
3611 warning (_("could not read '.gdb_index' section from %s; skipping"),
3612 bfd_get_filename (dwz->dwz_bfd.get ()));
3613 return 0;
3614 }
3615 }
3616
3617 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3618 dwz_list, dwz_list_elements);
3619
3620 if (types_list_elements)
3621 {
3622 /* We can only handle a single .debug_types when we have an
3623 index. */
3624 if (dwarf2_per_objfile->types.size () != 1)
3625 return 0;
3626
3627 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3628
3629 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3630 types_list, types_list_elements);
3631 }
3632
3633 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3634
3635 dwarf2_per_objfile->index_table = std::move (map);
3636 dwarf2_per_objfile->using_index = 1;
3637 dwarf2_per_objfile->quick_file_names_table =
3638 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3639
3640 return 1;
3641 }
3642
3643 /* die_reader_func for dw2_get_file_names. */
3644
3645 static void
3646 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3647 const gdb_byte *info_ptr,
3648 struct die_info *comp_unit_die,
3649 int has_children,
3650 void *data)
3651 {
3652 struct dwarf2_cu *cu = reader->cu;
3653 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3654 struct dwarf2_per_objfile *dwarf2_per_objfile
3655 = cu->per_cu->dwarf2_per_objfile;
3656 struct objfile *objfile = dwarf2_per_objfile->objfile;
3657 struct dwarf2_per_cu_data *lh_cu;
3658 struct attribute *attr;
3659 void **slot;
3660 struct quick_file_names *qfn;
3661
3662 gdb_assert (! this_cu->is_debug_types);
3663
3664 /* Our callers never want to match partial units -- instead they
3665 will match the enclosing full CU. */
3666 if (comp_unit_die->tag == DW_TAG_partial_unit)
3667 {
3668 this_cu->v.quick->no_file_data = 1;
3669 return;
3670 }
3671
3672 lh_cu = this_cu;
3673 slot = NULL;
3674
3675 line_header_up lh;
3676 sect_offset line_offset {};
3677
3678 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3679 if (attr != nullptr)
3680 {
3681 struct quick_file_names find_entry;
3682
3683 line_offset = (sect_offset) DW_UNSND (attr);
3684
3685 /* We may have already read in this line header (TU line header sharing).
3686 If we have we're done. */
3687 find_entry.hash.dwo_unit = cu->dwo_unit;
3688 find_entry.hash.line_sect_off = line_offset;
3689 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3690 &find_entry, INSERT);
3691 if (*slot != NULL)
3692 {
3693 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3694 return;
3695 }
3696
3697 lh = dwarf_decode_line_header (line_offset, cu);
3698 }
3699 if (lh == NULL)
3700 {
3701 lh_cu->v.quick->no_file_data = 1;
3702 return;
3703 }
3704
3705 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3706 qfn->hash.dwo_unit = cu->dwo_unit;
3707 qfn->hash.line_sect_off = line_offset;
3708 gdb_assert (slot != NULL);
3709 *slot = qfn;
3710
3711 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3712
3713 int offset = 0;
3714 if (strcmp (fnd.name, "<unknown>") != 0)
3715 ++offset;
3716
3717 qfn->num_file_names = offset + lh->file_names_size ();
3718 qfn->file_names =
3719 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3720 if (offset != 0)
3721 qfn->file_names[0] = xstrdup (fnd.name);
3722 for (int i = 0; i < lh->file_names_size (); ++i)
3723 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3724 qfn->real_names = NULL;
3725
3726 lh_cu->v.quick->file_names = qfn;
3727 }
3728
3729 /* A helper for the "quick" functions which attempts to read the line
3730 table for THIS_CU. */
3731
3732 static struct quick_file_names *
3733 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3734 {
3735 /* This should never be called for TUs. */
3736 gdb_assert (! this_cu->is_debug_types);
3737 /* Nor type unit groups. */
3738 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3739
3740 if (this_cu->v.quick->file_names != NULL)
3741 return this_cu->v.quick->file_names;
3742 /* If we know there is no line data, no point in looking again. */
3743 if (this_cu->v.quick->no_file_data)
3744 return NULL;
3745
3746 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3747
3748 if (this_cu->v.quick->no_file_data)
3749 return NULL;
3750 return this_cu->v.quick->file_names;
3751 }
3752
3753 /* A helper for the "quick" functions which computes and caches the
3754 real path for a given file name from the line table. */
3755
3756 static const char *
3757 dw2_get_real_path (struct objfile *objfile,
3758 struct quick_file_names *qfn, int index)
3759 {
3760 if (qfn->real_names == NULL)
3761 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3762 qfn->num_file_names, const char *);
3763
3764 if (qfn->real_names[index] == NULL)
3765 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3766
3767 return qfn->real_names[index];
3768 }
3769
3770 static struct symtab *
3771 dw2_find_last_source_symtab (struct objfile *objfile)
3772 {
3773 struct dwarf2_per_objfile *dwarf2_per_objfile
3774 = get_dwarf2_per_objfile (objfile);
3775 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3776 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3777
3778 if (cust == NULL)
3779 return NULL;
3780
3781 return compunit_primary_filetab (cust);
3782 }
3783
3784 /* Traversal function for dw2_forget_cached_source_info. */
3785
3786 static int
3787 dw2_free_cached_file_names (void **slot, void *info)
3788 {
3789 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3790
3791 if (file_data->real_names)
3792 {
3793 int i;
3794
3795 for (i = 0; i < file_data->num_file_names; ++i)
3796 {
3797 xfree ((void*) file_data->real_names[i]);
3798 file_data->real_names[i] = NULL;
3799 }
3800 }
3801
3802 return 1;
3803 }
3804
3805 static void
3806 dw2_forget_cached_source_info (struct objfile *objfile)
3807 {
3808 struct dwarf2_per_objfile *dwarf2_per_objfile
3809 = get_dwarf2_per_objfile (objfile);
3810
3811 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3812 dw2_free_cached_file_names, NULL);
3813 }
3814
3815 /* Helper function for dw2_map_symtabs_matching_filename that expands
3816 the symtabs and calls the iterator. */
3817
3818 static int
3819 dw2_map_expand_apply (struct objfile *objfile,
3820 struct dwarf2_per_cu_data *per_cu,
3821 const char *name, const char *real_path,
3822 gdb::function_view<bool (symtab *)> callback)
3823 {
3824 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3825
3826 /* Don't visit already-expanded CUs. */
3827 if (per_cu->v.quick->compunit_symtab)
3828 return 0;
3829
3830 /* This may expand more than one symtab, and we want to iterate over
3831 all of them. */
3832 dw2_instantiate_symtab (per_cu, false);
3833
3834 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3835 last_made, callback);
3836 }
3837
3838 /* Implementation of the map_symtabs_matching_filename method. */
3839
3840 static bool
3841 dw2_map_symtabs_matching_filename
3842 (struct objfile *objfile, const char *name, const char *real_path,
3843 gdb::function_view<bool (symtab *)> callback)
3844 {
3845 const char *name_basename = lbasename (name);
3846 struct dwarf2_per_objfile *dwarf2_per_objfile
3847 = get_dwarf2_per_objfile (objfile);
3848
3849 /* The rule is CUs specify all the files, including those used by
3850 any TU, so there's no need to scan TUs here. */
3851
3852 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3853 {
3854 /* We only need to look at symtabs not already expanded. */
3855 if (per_cu->v.quick->compunit_symtab)
3856 continue;
3857
3858 quick_file_names *file_data = dw2_get_file_names (per_cu);
3859 if (file_data == NULL)
3860 continue;
3861
3862 for (int j = 0; j < file_data->num_file_names; ++j)
3863 {
3864 const char *this_name = file_data->file_names[j];
3865 const char *this_real_name;
3866
3867 if (compare_filenames_for_search (this_name, name))
3868 {
3869 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3870 callback))
3871 return true;
3872 continue;
3873 }
3874
3875 /* Before we invoke realpath, which can get expensive when many
3876 files are involved, do a quick comparison of the basenames. */
3877 if (! basenames_may_differ
3878 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3879 continue;
3880
3881 this_real_name = dw2_get_real_path (objfile, file_data, j);
3882 if (compare_filenames_for_search (this_real_name, name))
3883 {
3884 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3885 callback))
3886 return true;
3887 continue;
3888 }
3889
3890 if (real_path != NULL)
3891 {
3892 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3893 gdb_assert (IS_ABSOLUTE_PATH (name));
3894 if (this_real_name != NULL
3895 && FILENAME_CMP (real_path, this_real_name) == 0)
3896 {
3897 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3898 callback))
3899 return true;
3900 continue;
3901 }
3902 }
3903 }
3904 }
3905
3906 return false;
3907 }
3908
3909 /* Struct used to manage iterating over all CUs looking for a symbol. */
3910
3911 struct dw2_symtab_iterator
3912 {
3913 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3914 struct dwarf2_per_objfile *dwarf2_per_objfile;
3915 /* If set, only look for symbols that match that block. Valid values are
3916 GLOBAL_BLOCK and STATIC_BLOCK. */
3917 gdb::optional<block_enum> block_index;
3918 /* The kind of symbol we're looking for. */
3919 domain_enum domain;
3920 /* The list of CUs from the index entry of the symbol,
3921 or NULL if not found. */
3922 offset_type *vec;
3923 /* The next element in VEC to look at. */
3924 int next;
3925 /* The number of elements in VEC, or zero if there is no match. */
3926 int length;
3927 /* Have we seen a global version of the symbol?
3928 If so we can ignore all further global instances.
3929 This is to work around gold/15646, inefficient gold-generated
3930 indices. */
3931 int global_seen;
3932 };
3933
3934 /* Initialize the index symtab iterator ITER. */
3935
3936 static void
3937 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3938 struct dwarf2_per_objfile *dwarf2_per_objfile,
3939 gdb::optional<block_enum> block_index,
3940 domain_enum domain,
3941 const char *name)
3942 {
3943 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3944 iter->block_index = block_index;
3945 iter->domain = domain;
3946 iter->next = 0;
3947 iter->global_seen = 0;
3948
3949 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3950
3951 /* index is NULL if OBJF_READNOW. */
3952 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3953 iter->length = MAYBE_SWAP (*iter->vec);
3954 else
3955 {
3956 iter->vec = NULL;
3957 iter->length = 0;
3958 }
3959 }
3960
3961 /* Return the next matching CU or NULL if there are no more. */
3962
3963 static struct dwarf2_per_cu_data *
3964 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3965 {
3966 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3967
3968 for ( ; iter->next < iter->length; ++iter->next)
3969 {
3970 offset_type cu_index_and_attrs =
3971 MAYBE_SWAP (iter->vec[iter->next + 1]);
3972 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3973 gdb_index_symbol_kind symbol_kind =
3974 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3975 /* Only check the symbol attributes if they're present.
3976 Indices prior to version 7 don't record them,
3977 and indices >= 7 may elide them for certain symbols
3978 (gold does this). */
3979 int attrs_valid =
3980 (dwarf2_per_objfile->index_table->version >= 7
3981 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3982
3983 /* Don't crash on bad data. */
3984 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3985 + dwarf2_per_objfile->all_type_units.size ()))
3986 {
3987 complaint (_(".gdb_index entry has bad CU index"
3988 " [in module %s]"),
3989 objfile_name (dwarf2_per_objfile->objfile));
3990 continue;
3991 }
3992
3993 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3994
3995 /* Skip if already read in. */
3996 if (per_cu->v.quick->compunit_symtab)
3997 continue;
3998
3999 /* Check static vs global. */
4000 if (attrs_valid)
4001 {
4002 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4003
4004 if (iter->block_index.has_value ())
4005 {
4006 bool want_static = *iter->block_index == STATIC_BLOCK;
4007
4008 if (is_static != want_static)
4009 continue;
4010 }
4011
4012 /* Work around gold/15646. */
4013 if (!is_static && iter->global_seen)
4014 continue;
4015 if (!is_static)
4016 iter->global_seen = 1;
4017 }
4018
4019 /* Only check the symbol's kind if it has one. */
4020 if (attrs_valid)
4021 {
4022 switch (iter->domain)
4023 {
4024 case VAR_DOMAIN:
4025 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4026 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4027 /* Some types are also in VAR_DOMAIN. */
4028 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4029 continue;
4030 break;
4031 case STRUCT_DOMAIN:
4032 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4033 continue;
4034 break;
4035 case LABEL_DOMAIN:
4036 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4037 continue;
4038 break;
4039 case MODULE_DOMAIN:
4040 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4041 continue;
4042 break;
4043 default:
4044 break;
4045 }
4046 }
4047
4048 ++iter->next;
4049 return per_cu;
4050 }
4051
4052 return NULL;
4053 }
4054
4055 static struct compunit_symtab *
4056 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
4057 const char *name, domain_enum domain)
4058 {
4059 struct compunit_symtab *stab_best = NULL;
4060 struct dwarf2_per_objfile *dwarf2_per_objfile
4061 = get_dwarf2_per_objfile (objfile);
4062
4063 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4064
4065 struct dw2_symtab_iterator iter;
4066 struct dwarf2_per_cu_data *per_cu;
4067
4068 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
4069
4070 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4071 {
4072 struct symbol *sym, *with_opaque = NULL;
4073 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
4074 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4075 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4076
4077 sym = block_find_symbol (block, name, domain,
4078 block_find_non_opaque_type_preferred,
4079 &with_opaque);
4080
4081 /* Some caution must be observed with overloaded functions
4082 and methods, since the index will not contain any overload
4083 information (but NAME might contain it). */
4084
4085 if (sym != NULL
4086 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4087 return stab;
4088 if (with_opaque != NULL
4089 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4090 stab_best = stab;
4091
4092 /* Keep looking through other CUs. */
4093 }
4094
4095 return stab_best;
4096 }
4097
4098 static void
4099 dw2_print_stats (struct objfile *objfile)
4100 {
4101 struct dwarf2_per_objfile *dwarf2_per_objfile
4102 = get_dwarf2_per_objfile (objfile);
4103 int total = (dwarf2_per_objfile->all_comp_units.size ()
4104 + dwarf2_per_objfile->all_type_units.size ());
4105 int count = 0;
4106
4107 for (int i = 0; i < total; ++i)
4108 {
4109 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4110
4111 if (!per_cu->v.quick->compunit_symtab)
4112 ++count;
4113 }
4114 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4115 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4116 }
4117
4118 /* This dumps minimal information about the index.
4119 It is called via "mt print objfiles".
4120 One use is to verify .gdb_index has been loaded by the
4121 gdb.dwarf2/gdb-index.exp testcase. */
4122
4123 static void
4124 dw2_dump (struct objfile *objfile)
4125 {
4126 struct dwarf2_per_objfile *dwarf2_per_objfile
4127 = get_dwarf2_per_objfile (objfile);
4128
4129 gdb_assert (dwarf2_per_objfile->using_index);
4130 printf_filtered (".gdb_index:");
4131 if (dwarf2_per_objfile->index_table != NULL)
4132 {
4133 printf_filtered (" version %d\n",
4134 dwarf2_per_objfile->index_table->version);
4135 }
4136 else
4137 printf_filtered (" faked for \"readnow\"\n");
4138 printf_filtered ("\n");
4139 }
4140
4141 static void
4142 dw2_expand_symtabs_for_function (struct objfile *objfile,
4143 const char *func_name)
4144 {
4145 struct dwarf2_per_objfile *dwarf2_per_objfile
4146 = get_dwarf2_per_objfile (objfile);
4147
4148 struct dw2_symtab_iterator iter;
4149 struct dwarf2_per_cu_data *per_cu;
4150
4151 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
4152
4153 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4154 dw2_instantiate_symtab (per_cu, false);
4155
4156 }
4157
4158 static void
4159 dw2_expand_all_symtabs (struct objfile *objfile)
4160 {
4161 struct dwarf2_per_objfile *dwarf2_per_objfile
4162 = get_dwarf2_per_objfile (objfile);
4163 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4164 + dwarf2_per_objfile->all_type_units.size ());
4165
4166 for (int i = 0; i < total_units; ++i)
4167 {
4168 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4169
4170 /* We don't want to directly expand a partial CU, because if we
4171 read it with the wrong language, then assertion failures can
4172 be triggered later on. See PR symtab/23010. So, tell
4173 dw2_instantiate_symtab to skip partial CUs -- any important
4174 partial CU will be read via DW_TAG_imported_unit anyway. */
4175 dw2_instantiate_symtab (per_cu, true);
4176 }
4177 }
4178
4179 static void
4180 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4181 const char *fullname)
4182 {
4183 struct dwarf2_per_objfile *dwarf2_per_objfile
4184 = get_dwarf2_per_objfile (objfile);
4185
4186 /* We don't need to consider type units here.
4187 This is only called for examining code, e.g. expand_line_sal.
4188 There can be an order of magnitude (or more) more type units
4189 than comp units, and we avoid them if we can. */
4190
4191 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4192 {
4193 /* We only need to look at symtabs not already expanded. */
4194 if (per_cu->v.quick->compunit_symtab)
4195 continue;
4196
4197 quick_file_names *file_data = dw2_get_file_names (per_cu);
4198 if (file_data == NULL)
4199 continue;
4200
4201 for (int j = 0; j < file_data->num_file_names; ++j)
4202 {
4203 const char *this_fullname = file_data->file_names[j];
4204
4205 if (filename_cmp (this_fullname, fullname) == 0)
4206 {
4207 dw2_instantiate_symtab (per_cu, false);
4208 break;
4209 }
4210 }
4211 }
4212 }
4213
4214 static void
4215 dw2_map_matching_symbols
4216 (struct objfile *objfile,
4217 const lookup_name_info &name, domain_enum domain,
4218 int global,
4219 gdb::function_view<symbol_found_callback_ftype> callback,
4220 symbol_compare_ftype *ordered_compare)
4221 {
4222 /* Currently unimplemented; used for Ada. The function can be called if the
4223 current language is Ada for a non-Ada objfile using GNU index. As Ada
4224 does not look for non-Ada symbols this function should just return. */
4225 }
4226
4227 /* Starting from a search name, return the string that finds the upper
4228 bound of all strings that start with SEARCH_NAME in a sorted name
4229 list. Returns the empty string to indicate that the upper bound is
4230 the end of the list. */
4231
4232 static std::string
4233 make_sort_after_prefix_name (const char *search_name)
4234 {
4235 /* When looking to complete "func", we find the upper bound of all
4236 symbols that start with "func" by looking for where we'd insert
4237 the closest string that would follow "func" in lexicographical
4238 order. Usually, that's "func"-with-last-character-incremented,
4239 i.e. "fund". Mind non-ASCII characters, though. Usually those
4240 will be UTF-8 multi-byte sequences, but we can't be certain.
4241 Especially mind the 0xff character, which is a valid character in
4242 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4243 rule out compilers allowing it in identifiers. Note that
4244 conveniently, strcmp/strcasecmp are specified to compare
4245 characters interpreted as unsigned char. So what we do is treat
4246 the whole string as a base 256 number composed of a sequence of
4247 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4248 to 0, and carries 1 to the following more-significant position.
4249 If the very first character in SEARCH_NAME ends up incremented
4250 and carries/overflows, then the upper bound is the end of the
4251 list. The string after the empty string is also the empty
4252 string.
4253
4254 Some examples of this operation:
4255
4256 SEARCH_NAME => "+1" RESULT
4257
4258 "abc" => "abd"
4259 "ab\xff" => "ac"
4260 "\xff" "a" "\xff" => "\xff" "b"
4261 "\xff" => ""
4262 "\xff\xff" => ""
4263 "" => ""
4264
4265 Then, with these symbols for example:
4266
4267 func
4268 func1
4269 fund
4270
4271 completing "func" looks for symbols between "func" and
4272 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4273 which finds "func" and "func1", but not "fund".
4274
4275 And with:
4276
4277 funcÿ (Latin1 'ÿ' [0xff])
4278 funcÿ1
4279 fund
4280
4281 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4282 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4283
4284 And with:
4285
4286 ÿÿ (Latin1 'ÿ' [0xff])
4287 ÿÿ1
4288
4289 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4290 the end of the list.
4291 */
4292 std::string after = search_name;
4293 while (!after.empty () && (unsigned char) after.back () == 0xff)
4294 after.pop_back ();
4295 if (!after.empty ())
4296 after.back () = (unsigned char) after.back () + 1;
4297 return after;
4298 }
4299
4300 /* See declaration. */
4301
4302 std::pair<std::vector<name_component>::const_iterator,
4303 std::vector<name_component>::const_iterator>
4304 mapped_index_base::find_name_components_bounds
4305 (const lookup_name_info &lookup_name_without_params, language lang) const
4306 {
4307 auto *name_cmp
4308 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4309
4310 const char *lang_name
4311 = lookup_name_without_params.language_lookup_name (lang).c_str ();
4312
4313 /* Comparison function object for lower_bound that matches against a
4314 given symbol name. */
4315 auto lookup_compare_lower = [&] (const name_component &elem,
4316 const char *name)
4317 {
4318 const char *elem_qualified = this->symbol_name_at (elem.idx);
4319 const char *elem_name = elem_qualified + elem.name_offset;
4320 return name_cmp (elem_name, name) < 0;
4321 };
4322
4323 /* Comparison function object for upper_bound that matches against a
4324 given symbol name. */
4325 auto lookup_compare_upper = [&] (const char *name,
4326 const name_component &elem)
4327 {
4328 const char *elem_qualified = this->symbol_name_at (elem.idx);
4329 const char *elem_name = elem_qualified + elem.name_offset;
4330 return name_cmp (name, elem_name) < 0;
4331 };
4332
4333 auto begin = this->name_components.begin ();
4334 auto end = this->name_components.end ();
4335
4336 /* Find the lower bound. */
4337 auto lower = [&] ()
4338 {
4339 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4340 return begin;
4341 else
4342 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4343 } ();
4344
4345 /* Find the upper bound. */
4346 auto upper = [&] ()
4347 {
4348 if (lookup_name_without_params.completion_mode ())
4349 {
4350 /* In completion mode, we want UPPER to point past all
4351 symbols names that have the same prefix. I.e., with
4352 these symbols, and completing "func":
4353
4354 function << lower bound
4355 function1
4356 other_function << upper bound
4357
4358 We find the upper bound by looking for the insertion
4359 point of "func"-with-last-character-incremented,
4360 i.e. "fund". */
4361 std::string after = make_sort_after_prefix_name (lang_name);
4362 if (after.empty ())
4363 return end;
4364 return std::lower_bound (lower, end, after.c_str (),
4365 lookup_compare_lower);
4366 }
4367 else
4368 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4369 } ();
4370
4371 return {lower, upper};
4372 }
4373
4374 /* See declaration. */
4375
4376 void
4377 mapped_index_base::build_name_components ()
4378 {
4379 if (!this->name_components.empty ())
4380 return;
4381
4382 this->name_components_casing = case_sensitivity;
4383 auto *name_cmp
4384 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4385
4386 /* The code below only knows how to break apart components of C++
4387 symbol names (and other languages that use '::' as
4388 namespace/module separator) and Ada symbol names. */
4389 auto count = this->symbol_name_count ();
4390 for (offset_type idx = 0; idx < count; idx++)
4391 {
4392 if (this->symbol_name_slot_invalid (idx))
4393 continue;
4394
4395 const char *name = this->symbol_name_at (idx);
4396
4397 /* Add each name component to the name component table. */
4398 unsigned int previous_len = 0;
4399
4400 if (strstr (name, "::") != nullptr)
4401 {
4402 for (unsigned int current_len = cp_find_first_component (name);
4403 name[current_len] != '\0';
4404 current_len += cp_find_first_component (name + current_len))
4405 {
4406 gdb_assert (name[current_len] == ':');
4407 this->name_components.push_back ({previous_len, idx});
4408 /* Skip the '::'. */
4409 current_len += 2;
4410 previous_len = current_len;
4411 }
4412 }
4413 else
4414 {
4415 /* Handle the Ada encoded (aka mangled) form here. */
4416 for (const char *iter = strstr (name, "__");
4417 iter != nullptr;
4418 iter = strstr (iter, "__"))
4419 {
4420 this->name_components.push_back ({previous_len, idx});
4421 iter += 2;
4422 previous_len = iter - name;
4423 }
4424 }
4425
4426 this->name_components.push_back ({previous_len, idx});
4427 }
4428
4429 /* Sort name_components elements by name. */
4430 auto name_comp_compare = [&] (const name_component &left,
4431 const name_component &right)
4432 {
4433 const char *left_qualified = this->symbol_name_at (left.idx);
4434 const char *right_qualified = this->symbol_name_at (right.idx);
4435
4436 const char *left_name = left_qualified + left.name_offset;
4437 const char *right_name = right_qualified + right.name_offset;
4438
4439 return name_cmp (left_name, right_name) < 0;
4440 };
4441
4442 std::sort (this->name_components.begin (),
4443 this->name_components.end (),
4444 name_comp_compare);
4445 }
4446
4447 /* Helper for dw2_expand_symtabs_matching that works with a
4448 mapped_index_base instead of the containing objfile. This is split
4449 to a separate function in order to be able to unit test the
4450 name_components matching using a mock mapped_index_base. For each
4451 symbol name that matches, calls MATCH_CALLBACK, passing it the
4452 symbol's index in the mapped_index_base symbol table. */
4453
4454 static void
4455 dw2_expand_symtabs_matching_symbol
4456 (mapped_index_base &index,
4457 const lookup_name_info &lookup_name_in,
4458 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4459 enum search_domain kind,
4460 gdb::function_view<bool (offset_type)> match_callback)
4461 {
4462 lookup_name_info lookup_name_without_params
4463 = lookup_name_in.make_ignore_params ();
4464
4465 /* Build the symbol name component sorted vector, if we haven't
4466 yet. */
4467 index.build_name_components ();
4468
4469 /* The same symbol may appear more than once in the range though.
4470 E.g., if we're looking for symbols that complete "w", and we have
4471 a symbol named "w1::w2", we'll find the two name components for
4472 that same symbol in the range. To be sure we only call the
4473 callback once per symbol, we first collect the symbol name
4474 indexes that matched in a temporary vector and ignore
4475 duplicates. */
4476 std::vector<offset_type> matches;
4477
4478 struct name_and_matcher
4479 {
4480 symbol_name_matcher_ftype *matcher;
4481 const std::string &name;
4482
4483 bool operator== (const name_and_matcher &other) const
4484 {
4485 return matcher == other.matcher && name == other.name;
4486 }
4487 };
4488
4489 /* A vector holding all the different symbol name matchers, for all
4490 languages. */
4491 std::vector<name_and_matcher> matchers;
4492
4493 for (int i = 0; i < nr_languages; i++)
4494 {
4495 enum language lang_e = (enum language) i;
4496
4497 const language_defn *lang = language_def (lang_e);
4498 symbol_name_matcher_ftype *name_matcher
4499 = get_symbol_name_matcher (lang, lookup_name_without_params);
4500
4501 name_and_matcher key {
4502 name_matcher,
4503 lookup_name_without_params.language_lookup_name (lang_e)
4504 };
4505
4506 /* Don't insert the same comparison routine more than once.
4507 Note that we do this linear walk. This is not a problem in
4508 practice because the number of supported languages is
4509 low. */
4510 if (std::find (matchers.begin (), matchers.end (), key)
4511 != matchers.end ())
4512 continue;
4513 matchers.push_back (std::move (key));
4514
4515 auto bounds
4516 = index.find_name_components_bounds (lookup_name_without_params,
4517 lang_e);
4518
4519 /* Now for each symbol name in range, check to see if we have a name
4520 match, and if so, call the MATCH_CALLBACK callback. */
4521
4522 for (; bounds.first != bounds.second; ++bounds.first)
4523 {
4524 const char *qualified = index.symbol_name_at (bounds.first->idx);
4525
4526 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4527 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4528 continue;
4529
4530 matches.push_back (bounds.first->idx);
4531 }
4532 }
4533
4534 std::sort (matches.begin (), matches.end ());
4535
4536 /* Finally call the callback, once per match. */
4537 ULONGEST prev = -1;
4538 for (offset_type idx : matches)
4539 {
4540 if (prev != idx)
4541 {
4542 if (!match_callback (idx))
4543 break;
4544 prev = idx;
4545 }
4546 }
4547
4548 /* Above we use a type wider than idx's for 'prev', since 0 and
4549 (offset_type)-1 are both possible values. */
4550 static_assert (sizeof (prev) > sizeof (offset_type), "");
4551 }
4552
4553 #if GDB_SELF_TEST
4554
4555 namespace selftests { namespace dw2_expand_symtabs_matching {
4556
4557 /* A mock .gdb_index/.debug_names-like name index table, enough to
4558 exercise dw2_expand_symtabs_matching_symbol, which works with the
4559 mapped_index_base interface. Builds an index from the symbol list
4560 passed as parameter to the constructor. */
4561 class mock_mapped_index : public mapped_index_base
4562 {
4563 public:
4564 mock_mapped_index (gdb::array_view<const char *> symbols)
4565 : m_symbol_table (symbols)
4566 {}
4567
4568 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4569
4570 /* Return the number of names in the symbol table. */
4571 size_t symbol_name_count () const override
4572 {
4573 return m_symbol_table.size ();
4574 }
4575
4576 /* Get the name of the symbol at IDX in the symbol table. */
4577 const char *symbol_name_at (offset_type idx) const override
4578 {
4579 return m_symbol_table[idx];
4580 }
4581
4582 private:
4583 gdb::array_view<const char *> m_symbol_table;
4584 };
4585
4586 /* Convenience function that converts a NULL pointer to a "<null>"
4587 string, to pass to print routines. */
4588
4589 static const char *
4590 string_or_null (const char *str)
4591 {
4592 return str != NULL ? str : "<null>";
4593 }
4594
4595 /* Check if a lookup_name_info built from
4596 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4597 index. EXPECTED_LIST is the list of expected matches, in expected
4598 matching order. If no match expected, then an empty list is
4599 specified. Returns true on success. On failure prints a warning
4600 indicating the file:line that failed, and returns false. */
4601
4602 static bool
4603 check_match (const char *file, int line,
4604 mock_mapped_index &mock_index,
4605 const char *name, symbol_name_match_type match_type,
4606 bool completion_mode,
4607 std::initializer_list<const char *> expected_list)
4608 {
4609 lookup_name_info lookup_name (name, match_type, completion_mode);
4610
4611 bool matched = true;
4612
4613 auto mismatch = [&] (const char *expected_str,
4614 const char *got)
4615 {
4616 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4617 "expected=\"%s\", got=\"%s\"\n"),
4618 file, line,
4619 (match_type == symbol_name_match_type::FULL
4620 ? "FULL" : "WILD"),
4621 name, string_or_null (expected_str), string_or_null (got));
4622 matched = false;
4623 };
4624
4625 auto expected_it = expected_list.begin ();
4626 auto expected_end = expected_list.end ();
4627
4628 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4629 NULL, ALL_DOMAIN,
4630 [&] (offset_type idx)
4631 {
4632 const char *matched_name = mock_index.symbol_name_at (idx);
4633 const char *expected_str
4634 = expected_it == expected_end ? NULL : *expected_it++;
4635
4636 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4637 mismatch (expected_str, matched_name);
4638 return true;
4639 });
4640
4641 const char *expected_str
4642 = expected_it == expected_end ? NULL : *expected_it++;
4643 if (expected_str != NULL)
4644 mismatch (expected_str, NULL);
4645
4646 return matched;
4647 }
4648
4649 /* The symbols added to the mock mapped_index for testing (in
4650 canonical form). */
4651 static const char *test_symbols[] = {
4652 "function",
4653 "std::bar",
4654 "std::zfunction",
4655 "std::zfunction2",
4656 "w1::w2",
4657 "ns::foo<char*>",
4658 "ns::foo<int>",
4659 "ns::foo<long>",
4660 "ns2::tmpl<int>::foo2",
4661 "(anonymous namespace)::A::B::C",
4662
4663 /* These are used to check that the increment-last-char in the
4664 matching algorithm for completion doesn't match "t1_fund" when
4665 completing "t1_func". */
4666 "t1_func",
4667 "t1_func1",
4668 "t1_fund",
4669 "t1_fund1",
4670
4671 /* A UTF-8 name with multi-byte sequences to make sure that
4672 cp-name-parser understands this as a single identifier ("função"
4673 is "function" in PT). */
4674 u8"u8função",
4675
4676 /* \377 (0xff) is Latin1 'ÿ'. */
4677 "yfunc\377",
4678
4679 /* \377 (0xff) is Latin1 'ÿ'. */
4680 "\377",
4681 "\377\377123",
4682
4683 /* A name with all sorts of complications. Starts with "z" to make
4684 it easier for the completion tests below. */
4685 #define Z_SYM_NAME \
4686 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4687 "::tuple<(anonymous namespace)::ui*, " \
4688 "std::default_delete<(anonymous namespace)::ui>, void>"
4689
4690 Z_SYM_NAME
4691 };
4692
4693 /* Returns true if the mapped_index_base::find_name_component_bounds
4694 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4695 in completion mode. */
4696
4697 static bool
4698 check_find_bounds_finds (mapped_index_base &index,
4699 const char *search_name,
4700 gdb::array_view<const char *> expected_syms)
4701 {
4702 lookup_name_info lookup_name (search_name,
4703 symbol_name_match_type::FULL, true);
4704
4705 auto bounds = index.find_name_components_bounds (lookup_name,
4706 language_cplus);
4707
4708 size_t distance = std::distance (bounds.first, bounds.second);
4709 if (distance != expected_syms.size ())
4710 return false;
4711
4712 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4713 {
4714 auto nc_elem = bounds.first + exp_elem;
4715 const char *qualified = index.symbol_name_at (nc_elem->idx);
4716 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4717 return false;
4718 }
4719
4720 return true;
4721 }
4722
4723 /* Test the lower-level mapped_index::find_name_component_bounds
4724 method. */
4725
4726 static void
4727 test_mapped_index_find_name_component_bounds ()
4728 {
4729 mock_mapped_index mock_index (test_symbols);
4730
4731 mock_index.build_name_components ();
4732
4733 /* Test the lower-level mapped_index::find_name_component_bounds
4734 method in completion mode. */
4735 {
4736 static const char *expected_syms[] = {
4737 "t1_func",
4738 "t1_func1",
4739 };
4740
4741 SELF_CHECK (check_find_bounds_finds (mock_index,
4742 "t1_func", expected_syms));
4743 }
4744
4745 /* Check that the increment-last-char in the name matching algorithm
4746 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4747 {
4748 static const char *expected_syms1[] = {
4749 "\377",
4750 "\377\377123",
4751 };
4752 SELF_CHECK (check_find_bounds_finds (mock_index,
4753 "\377", expected_syms1));
4754
4755 static const char *expected_syms2[] = {
4756 "\377\377123",
4757 };
4758 SELF_CHECK (check_find_bounds_finds (mock_index,
4759 "\377\377", expected_syms2));
4760 }
4761 }
4762
4763 /* Test dw2_expand_symtabs_matching_symbol. */
4764
4765 static void
4766 test_dw2_expand_symtabs_matching_symbol ()
4767 {
4768 mock_mapped_index mock_index (test_symbols);
4769
4770 /* We let all tests run until the end even if some fails, for debug
4771 convenience. */
4772 bool any_mismatch = false;
4773
4774 /* Create the expected symbols list (an initializer_list). Needed
4775 because lists have commas, and we need to pass them to CHECK,
4776 which is a macro. */
4777 #define EXPECT(...) { __VA_ARGS__ }
4778
4779 /* Wrapper for check_match that passes down the current
4780 __FILE__/__LINE__. */
4781 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4782 any_mismatch |= !check_match (__FILE__, __LINE__, \
4783 mock_index, \
4784 NAME, MATCH_TYPE, COMPLETION_MODE, \
4785 EXPECTED_LIST)
4786
4787 /* Identity checks. */
4788 for (const char *sym : test_symbols)
4789 {
4790 /* Should be able to match all existing symbols. */
4791 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4792 EXPECT (sym));
4793
4794 /* Should be able to match all existing symbols with
4795 parameters. */
4796 std::string with_params = std::string (sym) + "(int)";
4797 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4798 EXPECT (sym));
4799
4800 /* Should be able to match all existing symbols with
4801 parameters and qualifiers. */
4802 with_params = std::string (sym) + " ( int ) const";
4803 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4804 EXPECT (sym));
4805
4806 /* This should really find sym, but cp-name-parser.y doesn't
4807 know about lvalue/rvalue qualifiers yet. */
4808 with_params = std::string (sym) + " ( int ) &&";
4809 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4810 {});
4811 }
4812
4813 /* Check that the name matching algorithm for completion doesn't get
4814 confused with Latin1 'ÿ' / 0xff. */
4815 {
4816 static const char str[] = "\377";
4817 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4818 EXPECT ("\377", "\377\377123"));
4819 }
4820
4821 /* Check that the increment-last-char in the matching algorithm for
4822 completion doesn't match "t1_fund" when completing "t1_func". */
4823 {
4824 static const char str[] = "t1_func";
4825 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4826 EXPECT ("t1_func", "t1_func1"));
4827 }
4828
4829 /* Check that completion mode works at each prefix of the expected
4830 symbol name. */
4831 {
4832 static const char str[] = "function(int)";
4833 size_t len = strlen (str);
4834 std::string lookup;
4835
4836 for (size_t i = 1; i < len; i++)
4837 {
4838 lookup.assign (str, i);
4839 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4840 EXPECT ("function"));
4841 }
4842 }
4843
4844 /* While "w" is a prefix of both components, the match function
4845 should still only be called once. */
4846 {
4847 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4848 EXPECT ("w1::w2"));
4849 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4850 EXPECT ("w1::w2"));
4851 }
4852
4853 /* Same, with a "complicated" symbol. */
4854 {
4855 static const char str[] = Z_SYM_NAME;
4856 size_t len = strlen (str);
4857 std::string lookup;
4858
4859 for (size_t i = 1; i < len; i++)
4860 {
4861 lookup.assign (str, i);
4862 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4863 EXPECT (Z_SYM_NAME));
4864 }
4865 }
4866
4867 /* In FULL mode, an incomplete symbol doesn't match. */
4868 {
4869 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4870 {});
4871 }
4872
4873 /* A complete symbol with parameters matches any overload, since the
4874 index has no overload info. */
4875 {
4876 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4877 EXPECT ("std::zfunction", "std::zfunction2"));
4878 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4879 EXPECT ("std::zfunction", "std::zfunction2"));
4880 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4881 EXPECT ("std::zfunction", "std::zfunction2"));
4882 }
4883
4884 /* Check that whitespace is ignored appropriately. A symbol with a
4885 template argument list. */
4886 {
4887 static const char expected[] = "ns::foo<int>";
4888 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4889 EXPECT (expected));
4890 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4891 EXPECT (expected));
4892 }
4893
4894 /* Check that whitespace is ignored appropriately. A symbol with a
4895 template argument list that includes a pointer. */
4896 {
4897 static const char expected[] = "ns::foo<char*>";
4898 /* Try both completion and non-completion modes. */
4899 static const bool completion_mode[2] = {false, true};
4900 for (size_t i = 0; i < 2; i++)
4901 {
4902 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4903 completion_mode[i], EXPECT (expected));
4904 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4905 completion_mode[i], EXPECT (expected));
4906
4907 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4908 completion_mode[i], EXPECT (expected));
4909 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4910 completion_mode[i], EXPECT (expected));
4911 }
4912 }
4913
4914 {
4915 /* Check method qualifiers are ignored. */
4916 static const char expected[] = "ns::foo<char*>";
4917 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4918 symbol_name_match_type::FULL, true, EXPECT (expected));
4919 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4920 symbol_name_match_type::FULL, true, EXPECT (expected));
4921 CHECK_MATCH ("foo < char * > ( int ) const",
4922 symbol_name_match_type::WILD, true, EXPECT (expected));
4923 CHECK_MATCH ("foo < char * > ( int ) &&",
4924 symbol_name_match_type::WILD, true, EXPECT (expected));
4925 }
4926
4927 /* Test lookup names that don't match anything. */
4928 {
4929 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4930 {});
4931
4932 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4933 {});
4934 }
4935
4936 /* Some wild matching tests, exercising "(anonymous namespace)",
4937 which should not be confused with a parameter list. */
4938 {
4939 static const char *syms[] = {
4940 "A::B::C",
4941 "B::C",
4942 "C",
4943 "A :: B :: C ( int )",
4944 "B :: C ( int )",
4945 "C ( int )",
4946 };
4947
4948 for (const char *s : syms)
4949 {
4950 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4951 EXPECT ("(anonymous namespace)::A::B::C"));
4952 }
4953 }
4954
4955 {
4956 static const char expected[] = "ns2::tmpl<int>::foo2";
4957 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4958 EXPECT (expected));
4959 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4960 EXPECT (expected));
4961 }
4962
4963 SELF_CHECK (!any_mismatch);
4964
4965 #undef EXPECT
4966 #undef CHECK_MATCH
4967 }
4968
4969 static void
4970 run_test ()
4971 {
4972 test_mapped_index_find_name_component_bounds ();
4973 test_dw2_expand_symtabs_matching_symbol ();
4974 }
4975
4976 }} // namespace selftests::dw2_expand_symtabs_matching
4977
4978 #endif /* GDB_SELF_TEST */
4979
4980 /* If FILE_MATCHER is NULL or if PER_CU has
4981 dwarf2_per_cu_quick_data::MARK set (see
4982 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4983 EXPANSION_NOTIFY on it. */
4984
4985 static void
4986 dw2_expand_symtabs_matching_one
4987 (struct dwarf2_per_cu_data *per_cu,
4988 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4989 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4990 {
4991 if (file_matcher == NULL || per_cu->v.quick->mark)
4992 {
4993 bool symtab_was_null
4994 = (per_cu->v.quick->compunit_symtab == NULL);
4995
4996 dw2_instantiate_symtab (per_cu, false);
4997
4998 if (expansion_notify != NULL
4999 && symtab_was_null
5000 && per_cu->v.quick->compunit_symtab != NULL)
5001 expansion_notify (per_cu->v.quick->compunit_symtab);
5002 }
5003 }
5004
5005 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5006 matched, to expand corresponding CUs that were marked. IDX is the
5007 index of the symbol name that matched. */
5008
5009 static void
5010 dw2_expand_marked_cus
5011 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5012 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5013 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5014 search_domain kind)
5015 {
5016 offset_type *vec, vec_len, vec_idx;
5017 bool global_seen = false;
5018 mapped_index &index = *dwarf2_per_objfile->index_table;
5019
5020 vec = (offset_type *) (index.constant_pool
5021 + MAYBE_SWAP (index.symbol_table[idx].vec));
5022 vec_len = MAYBE_SWAP (vec[0]);
5023 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5024 {
5025 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5026 /* This value is only valid for index versions >= 7. */
5027 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5028 gdb_index_symbol_kind symbol_kind =
5029 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5030 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5031 /* Only check the symbol attributes if they're present.
5032 Indices prior to version 7 don't record them,
5033 and indices >= 7 may elide them for certain symbols
5034 (gold does this). */
5035 int attrs_valid =
5036 (index.version >= 7
5037 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5038
5039 /* Work around gold/15646. */
5040 if (attrs_valid)
5041 {
5042 if (!is_static && global_seen)
5043 continue;
5044 if (!is_static)
5045 global_seen = true;
5046 }
5047
5048 /* Only check the symbol's kind if it has one. */
5049 if (attrs_valid)
5050 {
5051 switch (kind)
5052 {
5053 case VARIABLES_DOMAIN:
5054 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5055 continue;
5056 break;
5057 case FUNCTIONS_DOMAIN:
5058 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5059 continue;
5060 break;
5061 case TYPES_DOMAIN:
5062 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5063 continue;
5064 break;
5065 case MODULES_DOMAIN:
5066 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
5067 continue;
5068 break;
5069 default:
5070 break;
5071 }
5072 }
5073
5074 /* Don't crash on bad data. */
5075 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5076 + dwarf2_per_objfile->all_type_units.size ()))
5077 {
5078 complaint (_(".gdb_index entry has bad CU index"
5079 " [in module %s]"),
5080 objfile_name (dwarf2_per_objfile->objfile));
5081 continue;
5082 }
5083
5084 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5085 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5086 expansion_notify);
5087 }
5088 }
5089
5090 /* If FILE_MATCHER is non-NULL, set all the
5091 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5092 that match FILE_MATCHER. */
5093
5094 static void
5095 dw_expand_symtabs_matching_file_matcher
5096 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5097 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5098 {
5099 if (file_matcher == NULL)
5100 return;
5101
5102 objfile *const objfile = dwarf2_per_objfile->objfile;
5103
5104 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5105 htab_eq_pointer,
5106 NULL, xcalloc, xfree));
5107 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5108 htab_eq_pointer,
5109 NULL, xcalloc, xfree));
5110
5111 /* The rule is CUs specify all the files, including those used by
5112 any TU, so there's no need to scan TUs here. */
5113
5114 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5115 {
5116 QUIT;
5117
5118 per_cu->v.quick->mark = 0;
5119
5120 /* We only need to look at symtabs not already expanded. */
5121 if (per_cu->v.quick->compunit_symtab)
5122 continue;
5123
5124 quick_file_names *file_data = dw2_get_file_names (per_cu);
5125 if (file_data == NULL)
5126 continue;
5127
5128 if (htab_find (visited_not_found.get (), file_data) != NULL)
5129 continue;
5130 else if (htab_find (visited_found.get (), file_data) != NULL)
5131 {
5132 per_cu->v.quick->mark = 1;
5133 continue;
5134 }
5135
5136 for (int j = 0; j < file_data->num_file_names; ++j)
5137 {
5138 const char *this_real_name;
5139
5140 if (file_matcher (file_data->file_names[j], false))
5141 {
5142 per_cu->v.quick->mark = 1;
5143 break;
5144 }
5145
5146 /* Before we invoke realpath, which can get expensive when many
5147 files are involved, do a quick comparison of the basenames. */
5148 if (!basenames_may_differ
5149 && !file_matcher (lbasename (file_data->file_names[j]),
5150 true))
5151 continue;
5152
5153 this_real_name = dw2_get_real_path (objfile, file_data, j);
5154 if (file_matcher (this_real_name, false))
5155 {
5156 per_cu->v.quick->mark = 1;
5157 break;
5158 }
5159 }
5160
5161 void **slot = htab_find_slot (per_cu->v.quick->mark
5162 ? visited_found.get ()
5163 : visited_not_found.get (),
5164 file_data, INSERT);
5165 *slot = file_data;
5166 }
5167 }
5168
5169 static void
5170 dw2_expand_symtabs_matching
5171 (struct objfile *objfile,
5172 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5173 const lookup_name_info &lookup_name,
5174 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5175 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5176 enum search_domain kind)
5177 {
5178 struct dwarf2_per_objfile *dwarf2_per_objfile
5179 = get_dwarf2_per_objfile (objfile);
5180
5181 /* index_table is NULL if OBJF_READNOW. */
5182 if (!dwarf2_per_objfile->index_table)
5183 return;
5184
5185 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5186
5187 mapped_index &index = *dwarf2_per_objfile->index_table;
5188
5189 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5190 symbol_matcher,
5191 kind, [&] (offset_type idx)
5192 {
5193 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5194 expansion_notify, kind);
5195 return true;
5196 });
5197 }
5198
5199 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5200 symtab. */
5201
5202 static struct compunit_symtab *
5203 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5204 CORE_ADDR pc)
5205 {
5206 int i;
5207
5208 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5209 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5210 return cust;
5211
5212 if (cust->includes == NULL)
5213 return NULL;
5214
5215 for (i = 0; cust->includes[i]; ++i)
5216 {
5217 struct compunit_symtab *s = cust->includes[i];
5218
5219 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5220 if (s != NULL)
5221 return s;
5222 }
5223
5224 return NULL;
5225 }
5226
5227 static struct compunit_symtab *
5228 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5229 struct bound_minimal_symbol msymbol,
5230 CORE_ADDR pc,
5231 struct obj_section *section,
5232 int warn_if_readin)
5233 {
5234 struct dwarf2_per_cu_data *data;
5235 struct compunit_symtab *result;
5236
5237 if (!objfile->partial_symtabs->psymtabs_addrmap)
5238 return NULL;
5239
5240 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5241 SECT_OFF_TEXT (objfile));
5242 data = (struct dwarf2_per_cu_data *) addrmap_find
5243 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
5244 if (!data)
5245 return NULL;
5246
5247 if (warn_if_readin && data->v.quick->compunit_symtab)
5248 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5249 paddress (get_objfile_arch (objfile), pc));
5250
5251 result
5252 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5253 false),
5254 pc);
5255 gdb_assert (result != NULL);
5256 return result;
5257 }
5258
5259 static void
5260 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5261 void *data, int need_fullname)
5262 {
5263 struct dwarf2_per_objfile *dwarf2_per_objfile
5264 = get_dwarf2_per_objfile (objfile);
5265
5266 if (!dwarf2_per_objfile->filenames_cache)
5267 {
5268 dwarf2_per_objfile->filenames_cache.emplace ();
5269
5270 htab_up visited (htab_create_alloc (10,
5271 htab_hash_pointer, htab_eq_pointer,
5272 NULL, xcalloc, xfree));
5273
5274 /* The rule is CUs specify all the files, including those used
5275 by any TU, so there's no need to scan TUs here. We can
5276 ignore file names coming from already-expanded CUs. */
5277
5278 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5279 {
5280 if (per_cu->v.quick->compunit_symtab)
5281 {
5282 void **slot = htab_find_slot (visited.get (),
5283 per_cu->v.quick->file_names,
5284 INSERT);
5285
5286 *slot = per_cu->v.quick->file_names;
5287 }
5288 }
5289
5290 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5291 {
5292 /* We only need to look at symtabs not already expanded. */
5293 if (per_cu->v.quick->compunit_symtab)
5294 continue;
5295
5296 quick_file_names *file_data = dw2_get_file_names (per_cu);
5297 if (file_data == NULL)
5298 continue;
5299
5300 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5301 if (*slot)
5302 {
5303 /* Already visited. */
5304 continue;
5305 }
5306 *slot = file_data;
5307
5308 for (int j = 0; j < file_data->num_file_names; ++j)
5309 {
5310 const char *filename = file_data->file_names[j];
5311 dwarf2_per_objfile->filenames_cache->seen (filename);
5312 }
5313 }
5314 }
5315
5316 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5317 {
5318 gdb::unique_xmalloc_ptr<char> this_real_name;
5319
5320 if (need_fullname)
5321 this_real_name = gdb_realpath (filename);
5322 (*fun) (filename, this_real_name.get (), data);
5323 });
5324 }
5325
5326 static int
5327 dw2_has_symbols (struct objfile *objfile)
5328 {
5329 return 1;
5330 }
5331
5332 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5333 {
5334 dw2_has_symbols,
5335 dw2_find_last_source_symtab,
5336 dw2_forget_cached_source_info,
5337 dw2_map_symtabs_matching_filename,
5338 dw2_lookup_symbol,
5339 dw2_print_stats,
5340 dw2_dump,
5341 dw2_expand_symtabs_for_function,
5342 dw2_expand_all_symtabs,
5343 dw2_expand_symtabs_with_fullname,
5344 dw2_map_matching_symbols,
5345 dw2_expand_symtabs_matching,
5346 dw2_find_pc_sect_compunit_symtab,
5347 NULL,
5348 dw2_map_symbol_filenames
5349 };
5350
5351 /* DWARF-5 debug_names reader. */
5352
5353 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5354 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5355
5356 /* A helper function that reads the .debug_names section in SECTION
5357 and fills in MAP. FILENAME is the name of the file containing the
5358 section; it is used for error reporting.
5359
5360 Returns true if all went well, false otherwise. */
5361
5362 static bool
5363 read_debug_names_from_section (struct objfile *objfile,
5364 const char *filename,
5365 struct dwarf2_section_info *section,
5366 mapped_debug_names &map)
5367 {
5368 if (dwarf2_section_empty_p (section))
5369 return false;
5370
5371 /* Older elfutils strip versions could keep the section in the main
5372 executable while splitting it for the separate debug info file. */
5373 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5374 return false;
5375
5376 dwarf2_read_section (objfile, section);
5377
5378 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5379
5380 const gdb_byte *addr = section->buffer;
5381
5382 bfd *const abfd = get_section_bfd_owner (section);
5383
5384 unsigned int bytes_read;
5385 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5386 addr += bytes_read;
5387
5388 map.dwarf5_is_dwarf64 = bytes_read != 4;
5389 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5390 if (bytes_read + length != section->size)
5391 {
5392 /* There may be multiple per-CU indices. */
5393 warning (_("Section .debug_names in %s length %s does not match "
5394 "section length %s, ignoring .debug_names."),
5395 filename, plongest (bytes_read + length),
5396 pulongest (section->size));
5397 return false;
5398 }
5399
5400 /* The version number. */
5401 uint16_t version = read_2_bytes (abfd, addr);
5402 addr += 2;
5403 if (version != 5)
5404 {
5405 warning (_("Section .debug_names in %s has unsupported version %d, "
5406 "ignoring .debug_names."),
5407 filename, version);
5408 return false;
5409 }
5410
5411 /* Padding. */
5412 uint16_t padding = read_2_bytes (abfd, addr);
5413 addr += 2;
5414 if (padding != 0)
5415 {
5416 warning (_("Section .debug_names in %s has unsupported padding %d, "
5417 "ignoring .debug_names."),
5418 filename, padding);
5419 return false;
5420 }
5421
5422 /* comp_unit_count - The number of CUs in the CU list. */
5423 map.cu_count = read_4_bytes (abfd, addr);
5424 addr += 4;
5425
5426 /* local_type_unit_count - The number of TUs in the local TU
5427 list. */
5428 map.tu_count = read_4_bytes (abfd, addr);
5429 addr += 4;
5430
5431 /* foreign_type_unit_count - The number of TUs in the foreign TU
5432 list. */
5433 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5434 addr += 4;
5435 if (foreign_tu_count != 0)
5436 {
5437 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5438 "ignoring .debug_names."),
5439 filename, static_cast<unsigned long> (foreign_tu_count));
5440 return false;
5441 }
5442
5443 /* bucket_count - The number of hash buckets in the hash lookup
5444 table. */
5445 map.bucket_count = read_4_bytes (abfd, addr);
5446 addr += 4;
5447
5448 /* name_count - The number of unique names in the index. */
5449 map.name_count = read_4_bytes (abfd, addr);
5450 addr += 4;
5451
5452 /* abbrev_table_size - The size in bytes of the abbreviations
5453 table. */
5454 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5455 addr += 4;
5456
5457 /* augmentation_string_size - The size in bytes of the augmentation
5458 string. This value is rounded up to a multiple of 4. */
5459 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5460 addr += 4;
5461 map.augmentation_is_gdb = ((augmentation_string_size
5462 == sizeof (dwarf5_augmentation))
5463 && memcmp (addr, dwarf5_augmentation,
5464 sizeof (dwarf5_augmentation)) == 0);
5465 augmentation_string_size += (-augmentation_string_size) & 3;
5466 addr += augmentation_string_size;
5467
5468 /* List of CUs */
5469 map.cu_table_reordered = addr;
5470 addr += map.cu_count * map.offset_size;
5471
5472 /* List of Local TUs */
5473 map.tu_table_reordered = addr;
5474 addr += map.tu_count * map.offset_size;
5475
5476 /* Hash Lookup Table */
5477 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5478 addr += map.bucket_count * 4;
5479 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5480 addr += map.name_count * 4;
5481
5482 /* Name Table */
5483 map.name_table_string_offs_reordered = addr;
5484 addr += map.name_count * map.offset_size;
5485 map.name_table_entry_offs_reordered = addr;
5486 addr += map.name_count * map.offset_size;
5487
5488 const gdb_byte *abbrev_table_start = addr;
5489 for (;;)
5490 {
5491 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5492 addr += bytes_read;
5493 if (index_num == 0)
5494 break;
5495
5496 const auto insertpair
5497 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5498 if (!insertpair.second)
5499 {
5500 warning (_("Section .debug_names in %s has duplicate index %s, "
5501 "ignoring .debug_names."),
5502 filename, pulongest (index_num));
5503 return false;
5504 }
5505 mapped_debug_names::index_val &indexval = insertpair.first->second;
5506 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5507 addr += bytes_read;
5508
5509 for (;;)
5510 {
5511 mapped_debug_names::index_val::attr attr;
5512 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5513 addr += bytes_read;
5514 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5515 addr += bytes_read;
5516 if (attr.form == DW_FORM_implicit_const)
5517 {
5518 attr.implicit_const = read_signed_leb128 (abfd, addr,
5519 &bytes_read);
5520 addr += bytes_read;
5521 }
5522 if (attr.dw_idx == 0 && attr.form == 0)
5523 break;
5524 indexval.attr_vec.push_back (std::move (attr));
5525 }
5526 }
5527 if (addr != abbrev_table_start + abbrev_table_size)
5528 {
5529 warning (_("Section .debug_names in %s has abbreviation_table "
5530 "of size %s vs. written as %u, ignoring .debug_names."),
5531 filename, plongest (addr - abbrev_table_start),
5532 abbrev_table_size);
5533 return false;
5534 }
5535 map.entry_pool = addr;
5536
5537 return true;
5538 }
5539
5540 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5541 list. */
5542
5543 static void
5544 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5545 const mapped_debug_names &map,
5546 dwarf2_section_info &section,
5547 bool is_dwz)
5548 {
5549 sect_offset sect_off_prev;
5550 for (uint32_t i = 0; i <= map.cu_count; ++i)
5551 {
5552 sect_offset sect_off_next;
5553 if (i < map.cu_count)
5554 {
5555 sect_off_next
5556 = (sect_offset) (extract_unsigned_integer
5557 (map.cu_table_reordered + i * map.offset_size,
5558 map.offset_size,
5559 map.dwarf5_byte_order));
5560 }
5561 else
5562 sect_off_next = (sect_offset) section.size;
5563 if (i >= 1)
5564 {
5565 const ULONGEST length = sect_off_next - sect_off_prev;
5566 dwarf2_per_cu_data *per_cu
5567 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5568 sect_off_prev, length);
5569 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5570 }
5571 sect_off_prev = sect_off_next;
5572 }
5573 }
5574
5575 /* Read the CU list from the mapped index, and use it to create all
5576 the CU objects for this dwarf2_per_objfile. */
5577
5578 static void
5579 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5580 const mapped_debug_names &map,
5581 const mapped_debug_names &dwz_map)
5582 {
5583 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5584 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5585
5586 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5587 dwarf2_per_objfile->info,
5588 false /* is_dwz */);
5589
5590 if (dwz_map.cu_count == 0)
5591 return;
5592
5593 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5594 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5595 true /* is_dwz */);
5596 }
5597
5598 /* Read .debug_names. If everything went ok, initialize the "quick"
5599 elements of all the CUs and return true. Otherwise, return false. */
5600
5601 static bool
5602 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5603 {
5604 std::unique_ptr<mapped_debug_names> map
5605 (new mapped_debug_names (dwarf2_per_objfile));
5606 mapped_debug_names dwz_map (dwarf2_per_objfile);
5607 struct objfile *objfile = dwarf2_per_objfile->objfile;
5608
5609 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5610 &dwarf2_per_objfile->debug_names,
5611 *map))
5612 return false;
5613
5614 /* Don't use the index if it's empty. */
5615 if (map->name_count == 0)
5616 return false;
5617
5618 /* If there is a .dwz file, read it so we can get its CU list as
5619 well. */
5620 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5621 if (dwz != NULL)
5622 {
5623 if (!read_debug_names_from_section (objfile,
5624 bfd_get_filename (dwz->dwz_bfd.get ()),
5625 &dwz->debug_names, dwz_map))
5626 {
5627 warning (_("could not read '.debug_names' section from %s; skipping"),
5628 bfd_get_filename (dwz->dwz_bfd.get ()));
5629 return false;
5630 }
5631 }
5632
5633 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5634
5635 if (map->tu_count != 0)
5636 {
5637 /* We can only handle a single .debug_types when we have an
5638 index. */
5639 if (dwarf2_per_objfile->types.size () != 1)
5640 return false;
5641
5642 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5643
5644 create_signatured_type_table_from_debug_names
5645 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5646 }
5647
5648 create_addrmap_from_aranges (dwarf2_per_objfile,
5649 &dwarf2_per_objfile->debug_aranges);
5650
5651 dwarf2_per_objfile->debug_names_table = std::move (map);
5652 dwarf2_per_objfile->using_index = 1;
5653 dwarf2_per_objfile->quick_file_names_table =
5654 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5655
5656 return true;
5657 }
5658
5659 /* Type used to manage iterating over all CUs looking for a symbol for
5660 .debug_names. */
5661
5662 class dw2_debug_names_iterator
5663 {
5664 public:
5665 dw2_debug_names_iterator (const mapped_debug_names &map,
5666 gdb::optional<block_enum> block_index,
5667 domain_enum domain,
5668 const char *name)
5669 : m_map (map), m_block_index (block_index), m_domain (domain),
5670 m_addr (find_vec_in_debug_names (map, name))
5671 {}
5672
5673 dw2_debug_names_iterator (const mapped_debug_names &map,
5674 search_domain search, uint32_t namei)
5675 : m_map (map),
5676 m_search (search),
5677 m_addr (find_vec_in_debug_names (map, namei))
5678 {}
5679
5680 dw2_debug_names_iterator (const mapped_debug_names &map,
5681 block_enum block_index, domain_enum domain,
5682 uint32_t namei)
5683 : m_map (map), m_block_index (block_index), m_domain (domain),
5684 m_addr (find_vec_in_debug_names (map, namei))
5685 {}
5686
5687 /* Return the next matching CU or NULL if there are no more. */
5688 dwarf2_per_cu_data *next ();
5689
5690 private:
5691 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5692 const char *name);
5693 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5694 uint32_t namei);
5695
5696 /* The internalized form of .debug_names. */
5697 const mapped_debug_names &m_map;
5698
5699 /* If set, only look for symbols that match that block. Valid values are
5700 GLOBAL_BLOCK and STATIC_BLOCK. */
5701 const gdb::optional<block_enum> m_block_index;
5702
5703 /* The kind of symbol we're looking for. */
5704 const domain_enum m_domain = UNDEF_DOMAIN;
5705 const search_domain m_search = ALL_DOMAIN;
5706
5707 /* The list of CUs from the index entry of the symbol, or NULL if
5708 not found. */
5709 const gdb_byte *m_addr;
5710 };
5711
5712 const char *
5713 mapped_debug_names::namei_to_name (uint32_t namei) const
5714 {
5715 const ULONGEST namei_string_offs
5716 = extract_unsigned_integer ((name_table_string_offs_reordered
5717 + namei * offset_size),
5718 offset_size,
5719 dwarf5_byte_order);
5720 return read_indirect_string_at_offset
5721 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5722 }
5723
5724 /* Find a slot in .debug_names for the object named NAME. If NAME is
5725 found, return pointer to its pool data. If NAME cannot be found,
5726 return NULL. */
5727
5728 const gdb_byte *
5729 dw2_debug_names_iterator::find_vec_in_debug_names
5730 (const mapped_debug_names &map, const char *name)
5731 {
5732 int (*cmp) (const char *, const char *);
5733
5734 gdb::unique_xmalloc_ptr<char> without_params;
5735 if (current_language->la_language == language_cplus
5736 || current_language->la_language == language_fortran
5737 || current_language->la_language == language_d)
5738 {
5739 /* NAME is already canonical. Drop any qualifiers as
5740 .debug_names does not contain any. */
5741
5742 if (strchr (name, '(') != NULL)
5743 {
5744 without_params = cp_remove_params (name);
5745 if (without_params != NULL)
5746 name = without_params.get ();
5747 }
5748 }
5749
5750 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5751
5752 const uint32_t full_hash = dwarf5_djb_hash (name);
5753 uint32_t namei
5754 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5755 (map.bucket_table_reordered
5756 + (full_hash % map.bucket_count)), 4,
5757 map.dwarf5_byte_order);
5758 if (namei == 0)
5759 return NULL;
5760 --namei;
5761 if (namei >= map.name_count)
5762 {
5763 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5764 "[in module %s]"),
5765 namei, map.name_count,
5766 objfile_name (map.dwarf2_per_objfile->objfile));
5767 return NULL;
5768 }
5769
5770 for (;;)
5771 {
5772 const uint32_t namei_full_hash
5773 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5774 (map.hash_table_reordered + namei), 4,
5775 map.dwarf5_byte_order);
5776 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5777 return NULL;
5778
5779 if (full_hash == namei_full_hash)
5780 {
5781 const char *const namei_string = map.namei_to_name (namei);
5782
5783 #if 0 /* An expensive sanity check. */
5784 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5785 {
5786 complaint (_("Wrong .debug_names hash for string at index %u "
5787 "[in module %s]"),
5788 namei, objfile_name (dwarf2_per_objfile->objfile));
5789 return NULL;
5790 }
5791 #endif
5792
5793 if (cmp (namei_string, name) == 0)
5794 {
5795 const ULONGEST namei_entry_offs
5796 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5797 + namei * map.offset_size),
5798 map.offset_size, map.dwarf5_byte_order);
5799 return map.entry_pool + namei_entry_offs;
5800 }
5801 }
5802
5803 ++namei;
5804 if (namei >= map.name_count)
5805 return NULL;
5806 }
5807 }
5808
5809 const gdb_byte *
5810 dw2_debug_names_iterator::find_vec_in_debug_names
5811 (const mapped_debug_names &map, uint32_t namei)
5812 {
5813 if (namei >= map.name_count)
5814 {
5815 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5816 "[in module %s]"),
5817 namei, map.name_count,
5818 objfile_name (map.dwarf2_per_objfile->objfile));
5819 return NULL;
5820 }
5821
5822 const ULONGEST namei_entry_offs
5823 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5824 + namei * map.offset_size),
5825 map.offset_size, map.dwarf5_byte_order);
5826 return map.entry_pool + namei_entry_offs;
5827 }
5828
5829 /* See dw2_debug_names_iterator. */
5830
5831 dwarf2_per_cu_data *
5832 dw2_debug_names_iterator::next ()
5833 {
5834 if (m_addr == NULL)
5835 return NULL;
5836
5837 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5838 struct objfile *objfile = dwarf2_per_objfile->objfile;
5839 bfd *const abfd = objfile->obfd;
5840
5841 again:
5842
5843 unsigned int bytes_read;
5844 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5845 m_addr += bytes_read;
5846 if (abbrev == 0)
5847 return NULL;
5848
5849 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5850 if (indexval_it == m_map.abbrev_map.cend ())
5851 {
5852 complaint (_("Wrong .debug_names undefined abbrev code %s "
5853 "[in module %s]"),
5854 pulongest (abbrev), objfile_name (objfile));
5855 return NULL;
5856 }
5857 const mapped_debug_names::index_val &indexval = indexval_it->second;
5858 enum class symbol_linkage {
5859 unknown,
5860 static_,
5861 extern_,
5862 } symbol_linkage_ = symbol_linkage::unknown;
5863 dwarf2_per_cu_data *per_cu = NULL;
5864 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5865 {
5866 ULONGEST ull;
5867 switch (attr.form)
5868 {
5869 case DW_FORM_implicit_const:
5870 ull = attr.implicit_const;
5871 break;
5872 case DW_FORM_flag_present:
5873 ull = 1;
5874 break;
5875 case DW_FORM_udata:
5876 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5877 m_addr += bytes_read;
5878 break;
5879 default:
5880 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5881 dwarf_form_name (attr.form),
5882 objfile_name (objfile));
5883 return NULL;
5884 }
5885 switch (attr.dw_idx)
5886 {
5887 case DW_IDX_compile_unit:
5888 /* Don't crash on bad data. */
5889 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5890 {
5891 complaint (_(".debug_names entry has bad CU index %s"
5892 " [in module %s]"),
5893 pulongest (ull),
5894 objfile_name (dwarf2_per_objfile->objfile));
5895 continue;
5896 }
5897 per_cu = dwarf2_per_objfile->get_cutu (ull);
5898 break;
5899 case DW_IDX_type_unit:
5900 /* Don't crash on bad data. */
5901 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5902 {
5903 complaint (_(".debug_names entry has bad TU index %s"
5904 " [in module %s]"),
5905 pulongest (ull),
5906 objfile_name (dwarf2_per_objfile->objfile));
5907 continue;
5908 }
5909 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5910 break;
5911 case DW_IDX_GNU_internal:
5912 if (!m_map.augmentation_is_gdb)
5913 break;
5914 symbol_linkage_ = symbol_linkage::static_;
5915 break;
5916 case DW_IDX_GNU_external:
5917 if (!m_map.augmentation_is_gdb)
5918 break;
5919 symbol_linkage_ = symbol_linkage::extern_;
5920 break;
5921 }
5922 }
5923
5924 /* Skip if already read in. */
5925 if (per_cu->v.quick->compunit_symtab)
5926 goto again;
5927
5928 /* Check static vs global. */
5929 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5930 {
5931 const bool want_static = *m_block_index == STATIC_BLOCK;
5932 const bool symbol_is_static =
5933 symbol_linkage_ == symbol_linkage::static_;
5934 if (want_static != symbol_is_static)
5935 goto again;
5936 }
5937
5938 /* Match dw2_symtab_iter_next, symbol_kind
5939 and debug_names::psymbol_tag. */
5940 switch (m_domain)
5941 {
5942 case VAR_DOMAIN:
5943 switch (indexval.dwarf_tag)
5944 {
5945 case DW_TAG_variable:
5946 case DW_TAG_subprogram:
5947 /* Some types are also in VAR_DOMAIN. */
5948 case DW_TAG_typedef:
5949 case DW_TAG_structure_type:
5950 break;
5951 default:
5952 goto again;
5953 }
5954 break;
5955 case STRUCT_DOMAIN:
5956 switch (indexval.dwarf_tag)
5957 {
5958 case DW_TAG_typedef:
5959 case DW_TAG_structure_type:
5960 break;
5961 default:
5962 goto again;
5963 }
5964 break;
5965 case LABEL_DOMAIN:
5966 switch (indexval.dwarf_tag)
5967 {
5968 case 0:
5969 case DW_TAG_variable:
5970 break;
5971 default:
5972 goto again;
5973 }
5974 break;
5975 case MODULE_DOMAIN:
5976 switch (indexval.dwarf_tag)
5977 {
5978 case DW_TAG_module:
5979 break;
5980 default:
5981 goto again;
5982 }
5983 break;
5984 default:
5985 break;
5986 }
5987
5988 /* Match dw2_expand_symtabs_matching, symbol_kind and
5989 debug_names::psymbol_tag. */
5990 switch (m_search)
5991 {
5992 case VARIABLES_DOMAIN:
5993 switch (indexval.dwarf_tag)
5994 {
5995 case DW_TAG_variable:
5996 break;
5997 default:
5998 goto again;
5999 }
6000 break;
6001 case FUNCTIONS_DOMAIN:
6002 switch (indexval.dwarf_tag)
6003 {
6004 case DW_TAG_subprogram:
6005 break;
6006 default:
6007 goto again;
6008 }
6009 break;
6010 case TYPES_DOMAIN:
6011 switch (indexval.dwarf_tag)
6012 {
6013 case DW_TAG_typedef:
6014 case DW_TAG_structure_type:
6015 break;
6016 default:
6017 goto again;
6018 }
6019 break;
6020 case MODULES_DOMAIN:
6021 switch (indexval.dwarf_tag)
6022 {
6023 case DW_TAG_module:
6024 break;
6025 default:
6026 goto again;
6027 }
6028 default:
6029 break;
6030 }
6031
6032 return per_cu;
6033 }
6034
6035 static struct compunit_symtab *
6036 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
6037 const char *name, domain_enum domain)
6038 {
6039 struct dwarf2_per_objfile *dwarf2_per_objfile
6040 = get_dwarf2_per_objfile (objfile);
6041
6042 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6043 if (!mapp)
6044 {
6045 /* index is NULL if OBJF_READNOW. */
6046 return NULL;
6047 }
6048 const auto &map = *mapp;
6049
6050 dw2_debug_names_iterator iter (map, block_index, domain, name);
6051
6052 struct compunit_symtab *stab_best = NULL;
6053 struct dwarf2_per_cu_data *per_cu;
6054 while ((per_cu = iter.next ()) != NULL)
6055 {
6056 struct symbol *sym, *with_opaque = NULL;
6057 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
6058 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6059 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6060
6061 sym = block_find_symbol (block, name, domain,
6062 block_find_non_opaque_type_preferred,
6063 &with_opaque);
6064
6065 /* Some caution must be observed with overloaded functions and
6066 methods, since the index will not contain any overload
6067 information (but NAME might contain it). */
6068
6069 if (sym != NULL
6070 && strcmp_iw (sym->search_name (), name) == 0)
6071 return stab;
6072 if (with_opaque != NULL
6073 && strcmp_iw (with_opaque->search_name (), name) == 0)
6074 stab_best = stab;
6075
6076 /* Keep looking through other CUs. */
6077 }
6078
6079 return stab_best;
6080 }
6081
6082 /* This dumps minimal information about .debug_names. It is called
6083 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6084 uses this to verify that .debug_names has been loaded. */
6085
6086 static void
6087 dw2_debug_names_dump (struct objfile *objfile)
6088 {
6089 struct dwarf2_per_objfile *dwarf2_per_objfile
6090 = get_dwarf2_per_objfile (objfile);
6091
6092 gdb_assert (dwarf2_per_objfile->using_index);
6093 printf_filtered (".debug_names:");
6094 if (dwarf2_per_objfile->debug_names_table)
6095 printf_filtered (" exists\n");
6096 else
6097 printf_filtered (" faked for \"readnow\"\n");
6098 printf_filtered ("\n");
6099 }
6100
6101 static void
6102 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6103 const char *func_name)
6104 {
6105 struct dwarf2_per_objfile *dwarf2_per_objfile
6106 = get_dwarf2_per_objfile (objfile);
6107
6108 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6109 if (dwarf2_per_objfile->debug_names_table)
6110 {
6111 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6112
6113 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
6114
6115 struct dwarf2_per_cu_data *per_cu;
6116 while ((per_cu = iter.next ()) != NULL)
6117 dw2_instantiate_symtab (per_cu, false);
6118 }
6119 }
6120
6121 static void
6122 dw2_debug_names_map_matching_symbols
6123 (struct objfile *objfile,
6124 const lookup_name_info &name, domain_enum domain,
6125 int global,
6126 gdb::function_view<symbol_found_callback_ftype> callback,
6127 symbol_compare_ftype *ordered_compare)
6128 {
6129 struct dwarf2_per_objfile *dwarf2_per_objfile
6130 = get_dwarf2_per_objfile (objfile);
6131
6132 /* debug_names_table is NULL if OBJF_READNOW. */
6133 if (!dwarf2_per_objfile->debug_names_table)
6134 return;
6135
6136 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6137 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6138
6139 const char *match_name = name.ada ().lookup_name ().c_str ();
6140 auto matcher = [&] (const char *symname)
6141 {
6142 if (ordered_compare == nullptr)
6143 return true;
6144 return ordered_compare (symname, match_name) == 0;
6145 };
6146
6147 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6148 [&] (offset_type namei)
6149 {
6150 /* The name was matched, now expand corresponding CUs that were
6151 marked. */
6152 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6153
6154 struct dwarf2_per_cu_data *per_cu;
6155 while ((per_cu = iter.next ()) != NULL)
6156 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6157 return true;
6158 });
6159
6160 /* It's a shame we couldn't do this inside the
6161 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6162 that have already been expanded. Instead, this loop matches what
6163 the psymtab code does. */
6164 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6165 {
6166 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6167 if (cust != nullptr)
6168 {
6169 const struct block *block
6170 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6171 if (!iterate_over_symbols_terminated (block, name,
6172 domain, callback))
6173 break;
6174 }
6175 }
6176 }
6177
6178 static void
6179 dw2_debug_names_expand_symtabs_matching
6180 (struct objfile *objfile,
6181 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6182 const lookup_name_info &lookup_name,
6183 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6184 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6185 enum search_domain kind)
6186 {
6187 struct dwarf2_per_objfile *dwarf2_per_objfile
6188 = get_dwarf2_per_objfile (objfile);
6189
6190 /* debug_names_table is NULL if OBJF_READNOW. */
6191 if (!dwarf2_per_objfile->debug_names_table)
6192 return;
6193
6194 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6195
6196 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6197
6198 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6199 symbol_matcher,
6200 kind, [&] (offset_type namei)
6201 {
6202 /* The name was matched, now expand corresponding CUs that were
6203 marked. */
6204 dw2_debug_names_iterator iter (map, kind, namei);
6205
6206 struct dwarf2_per_cu_data *per_cu;
6207 while ((per_cu = iter.next ()) != NULL)
6208 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6209 expansion_notify);
6210 return true;
6211 });
6212 }
6213
6214 const struct quick_symbol_functions dwarf2_debug_names_functions =
6215 {
6216 dw2_has_symbols,
6217 dw2_find_last_source_symtab,
6218 dw2_forget_cached_source_info,
6219 dw2_map_symtabs_matching_filename,
6220 dw2_debug_names_lookup_symbol,
6221 dw2_print_stats,
6222 dw2_debug_names_dump,
6223 dw2_debug_names_expand_symtabs_for_function,
6224 dw2_expand_all_symtabs,
6225 dw2_expand_symtabs_with_fullname,
6226 dw2_debug_names_map_matching_symbols,
6227 dw2_debug_names_expand_symtabs_matching,
6228 dw2_find_pc_sect_compunit_symtab,
6229 NULL,
6230 dw2_map_symbol_filenames
6231 };
6232
6233 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6234 to either a dwarf2_per_objfile or dwz_file object. */
6235
6236 template <typename T>
6237 static gdb::array_view<const gdb_byte>
6238 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6239 {
6240 dwarf2_section_info *section = &section_owner->gdb_index;
6241
6242 if (dwarf2_section_empty_p (section))
6243 return {};
6244
6245 /* Older elfutils strip versions could keep the section in the main
6246 executable while splitting it for the separate debug info file. */
6247 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6248 return {};
6249
6250 dwarf2_read_section (obj, section);
6251
6252 /* dwarf2_section_info::size is a bfd_size_type, while
6253 gdb::array_view works with size_t. On 32-bit hosts, with
6254 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6255 is 32-bit. So we need an explicit narrowing conversion here.
6256 This is fine, because it's impossible to allocate or mmap an
6257 array/buffer larger than what size_t can represent. */
6258 return gdb::make_array_view (section->buffer, section->size);
6259 }
6260
6261 /* Lookup the index cache for the contents of the index associated to
6262 DWARF2_OBJ. */
6263
6264 static gdb::array_view<const gdb_byte>
6265 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6266 {
6267 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6268 if (build_id == nullptr)
6269 return {};
6270
6271 return global_index_cache.lookup_gdb_index (build_id,
6272 &dwarf2_obj->index_cache_res);
6273 }
6274
6275 /* Same as the above, but for DWZ. */
6276
6277 static gdb::array_view<const gdb_byte>
6278 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6279 {
6280 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6281 if (build_id == nullptr)
6282 return {};
6283
6284 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6285 }
6286
6287 /* See symfile.h. */
6288
6289 bool
6290 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6291 {
6292 struct dwarf2_per_objfile *dwarf2_per_objfile
6293 = get_dwarf2_per_objfile (objfile);
6294
6295 /* If we're about to read full symbols, don't bother with the
6296 indices. In this case we also don't care if some other debug
6297 format is making psymtabs, because they are all about to be
6298 expanded anyway. */
6299 if ((objfile->flags & OBJF_READNOW))
6300 {
6301 dwarf2_per_objfile->using_index = 1;
6302 create_all_comp_units (dwarf2_per_objfile);
6303 create_all_type_units (dwarf2_per_objfile);
6304 dwarf2_per_objfile->quick_file_names_table
6305 = create_quick_file_names_table
6306 (dwarf2_per_objfile->all_comp_units.size ());
6307
6308 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6309 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6310 {
6311 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6312
6313 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6314 struct dwarf2_per_cu_quick_data);
6315 }
6316
6317 /* Return 1 so that gdb sees the "quick" functions. However,
6318 these functions will be no-ops because we will have expanded
6319 all symtabs. */
6320 *index_kind = dw_index_kind::GDB_INDEX;
6321 return true;
6322 }
6323
6324 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6325 {
6326 *index_kind = dw_index_kind::DEBUG_NAMES;
6327 return true;
6328 }
6329
6330 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6331 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6332 get_gdb_index_contents_from_section<dwz_file>))
6333 {
6334 *index_kind = dw_index_kind::GDB_INDEX;
6335 return true;
6336 }
6337
6338 /* ... otherwise, try to find the index in the index cache. */
6339 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6340 get_gdb_index_contents_from_cache,
6341 get_gdb_index_contents_from_cache_dwz))
6342 {
6343 global_index_cache.hit ();
6344 *index_kind = dw_index_kind::GDB_INDEX;
6345 return true;
6346 }
6347
6348 global_index_cache.miss ();
6349 return false;
6350 }
6351
6352 \f
6353
6354 /* Build a partial symbol table. */
6355
6356 void
6357 dwarf2_build_psymtabs (struct objfile *objfile)
6358 {
6359 struct dwarf2_per_objfile *dwarf2_per_objfile
6360 = get_dwarf2_per_objfile (objfile);
6361
6362 init_psymbol_list (objfile, 1024);
6363
6364 try
6365 {
6366 /* This isn't really ideal: all the data we allocate on the
6367 objfile's obstack is still uselessly kept around. However,
6368 freeing it seems unsafe. */
6369 psymtab_discarder psymtabs (objfile);
6370 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6371 psymtabs.keep ();
6372
6373 /* (maybe) store an index in the cache. */
6374 global_index_cache.store (dwarf2_per_objfile);
6375 }
6376 catch (const gdb_exception_error &except)
6377 {
6378 exception_print (gdb_stderr, except);
6379 }
6380 }
6381
6382 /* Return the total length of the CU described by HEADER. */
6383
6384 static unsigned int
6385 get_cu_length (const struct comp_unit_head *header)
6386 {
6387 return header->initial_length_size + header->length;
6388 }
6389
6390 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6391
6392 static inline bool
6393 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6394 {
6395 sect_offset bottom = cu_header->sect_off;
6396 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6397
6398 return sect_off >= bottom && sect_off < top;
6399 }
6400
6401 /* Find the base address of the compilation unit for range lists and
6402 location lists. It will normally be specified by DW_AT_low_pc.
6403 In DWARF-3 draft 4, the base address could be overridden by
6404 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6405 compilation units with discontinuous ranges. */
6406
6407 static void
6408 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6409 {
6410 struct attribute *attr;
6411
6412 cu->base_known = 0;
6413 cu->base_address = 0;
6414
6415 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6416 if (attr != nullptr)
6417 {
6418 cu->base_address = attr_value_as_address (attr);
6419 cu->base_known = 1;
6420 }
6421 else
6422 {
6423 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6424 if (attr != nullptr)
6425 {
6426 cu->base_address = attr_value_as_address (attr);
6427 cu->base_known = 1;
6428 }
6429 }
6430 }
6431
6432 /* Read in the comp unit header information from the debug_info at info_ptr.
6433 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6434 NOTE: This leaves members offset, first_die_offset to be filled in
6435 by the caller. */
6436
6437 static const gdb_byte *
6438 read_comp_unit_head (struct comp_unit_head *cu_header,
6439 const gdb_byte *info_ptr,
6440 struct dwarf2_section_info *section,
6441 rcuh_kind section_kind)
6442 {
6443 int signed_addr;
6444 unsigned int bytes_read;
6445 const char *filename = get_section_file_name (section);
6446 bfd *abfd = get_section_bfd_owner (section);
6447
6448 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6449 cu_header->initial_length_size = bytes_read;
6450 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6451 info_ptr += bytes_read;
6452 cu_header->version = read_2_bytes (abfd, info_ptr);
6453 if (cu_header->version < 2 || cu_header->version > 5)
6454 error (_("Dwarf Error: wrong version in compilation unit header "
6455 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6456 cu_header->version, filename);
6457 info_ptr += 2;
6458 if (cu_header->version < 5)
6459 switch (section_kind)
6460 {
6461 case rcuh_kind::COMPILE:
6462 cu_header->unit_type = DW_UT_compile;
6463 break;
6464 case rcuh_kind::TYPE:
6465 cu_header->unit_type = DW_UT_type;
6466 break;
6467 default:
6468 internal_error (__FILE__, __LINE__,
6469 _("read_comp_unit_head: invalid section_kind"));
6470 }
6471 else
6472 {
6473 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6474 (read_1_byte (abfd, info_ptr));
6475 info_ptr += 1;
6476 switch (cu_header->unit_type)
6477 {
6478 case DW_UT_compile:
6479 case DW_UT_partial:
6480 case DW_UT_skeleton:
6481 case DW_UT_split_compile:
6482 if (section_kind != rcuh_kind::COMPILE)
6483 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6484 "(is %s, should be %s) [in module %s]"),
6485 dwarf_unit_type_name (cu_header->unit_type),
6486 dwarf_unit_type_name (DW_UT_type), filename);
6487 break;
6488 case DW_UT_type:
6489 case DW_UT_split_type:
6490 section_kind = rcuh_kind::TYPE;
6491 break;
6492 default:
6493 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6494 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6495 "[in module %s]"), cu_header->unit_type,
6496 dwarf_unit_type_name (DW_UT_compile),
6497 dwarf_unit_type_name (DW_UT_skeleton),
6498 dwarf_unit_type_name (DW_UT_split_compile),
6499 dwarf_unit_type_name (DW_UT_type),
6500 dwarf_unit_type_name (DW_UT_split_type), filename);
6501 }
6502
6503 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6504 info_ptr += 1;
6505 }
6506 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6507 cu_header,
6508 &bytes_read);
6509 info_ptr += bytes_read;
6510 if (cu_header->version < 5)
6511 {
6512 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6513 info_ptr += 1;
6514 }
6515 signed_addr = bfd_get_sign_extend_vma (abfd);
6516 if (signed_addr < 0)
6517 internal_error (__FILE__, __LINE__,
6518 _("read_comp_unit_head: dwarf from non elf file"));
6519 cu_header->signed_addr_p = signed_addr;
6520
6521 bool header_has_signature = section_kind == rcuh_kind::TYPE
6522 || cu_header->unit_type == DW_UT_skeleton
6523 || cu_header->unit_type == DW_UT_split_compile;
6524
6525 if (header_has_signature)
6526 {
6527 cu_header->signature = read_8_bytes (abfd, info_ptr);
6528 info_ptr += 8;
6529 }
6530
6531 if (section_kind == rcuh_kind::TYPE)
6532 {
6533 LONGEST type_offset;
6534 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6535 info_ptr += bytes_read;
6536 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6537 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6538 error (_("Dwarf Error: Too big type_offset in compilation unit "
6539 "header (is %s) [in module %s]"), plongest (type_offset),
6540 filename);
6541 }
6542
6543 return info_ptr;
6544 }
6545
6546 /* Helper function that returns the proper abbrev section for
6547 THIS_CU. */
6548
6549 static struct dwarf2_section_info *
6550 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6551 {
6552 struct dwarf2_section_info *abbrev;
6553 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6554
6555 if (this_cu->is_dwz)
6556 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6557 else
6558 abbrev = &dwarf2_per_objfile->abbrev;
6559
6560 return abbrev;
6561 }
6562
6563 /* Subroutine of read_and_check_comp_unit_head and
6564 read_and_check_type_unit_head to simplify them.
6565 Perform various error checking on the header. */
6566
6567 static void
6568 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6569 struct comp_unit_head *header,
6570 struct dwarf2_section_info *section,
6571 struct dwarf2_section_info *abbrev_section)
6572 {
6573 const char *filename = get_section_file_name (section);
6574
6575 if (to_underlying (header->abbrev_sect_off)
6576 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6577 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6578 "(offset %s + 6) [in module %s]"),
6579 sect_offset_str (header->abbrev_sect_off),
6580 sect_offset_str (header->sect_off),
6581 filename);
6582
6583 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6584 avoid potential 32-bit overflow. */
6585 if (((ULONGEST) header->sect_off + get_cu_length (header))
6586 > section->size)
6587 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6588 "(offset %s + 0) [in module %s]"),
6589 header->length, sect_offset_str (header->sect_off),
6590 filename);
6591 }
6592
6593 /* Read in a CU/TU header and perform some basic error checking.
6594 The contents of the header are stored in HEADER.
6595 The result is a pointer to the start of the first DIE. */
6596
6597 static const gdb_byte *
6598 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6599 struct comp_unit_head *header,
6600 struct dwarf2_section_info *section,
6601 struct dwarf2_section_info *abbrev_section,
6602 const gdb_byte *info_ptr,
6603 rcuh_kind section_kind)
6604 {
6605 const gdb_byte *beg_of_comp_unit = info_ptr;
6606
6607 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6608
6609 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6610
6611 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6612
6613 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6614 abbrev_section);
6615
6616 return info_ptr;
6617 }
6618
6619 /* Fetch the abbreviation table offset from a comp or type unit header. */
6620
6621 static sect_offset
6622 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6623 struct dwarf2_section_info *section,
6624 sect_offset sect_off)
6625 {
6626 bfd *abfd = get_section_bfd_owner (section);
6627 const gdb_byte *info_ptr;
6628 unsigned int initial_length_size, offset_size;
6629 uint16_t version;
6630
6631 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6632 info_ptr = section->buffer + to_underlying (sect_off);
6633 read_initial_length (abfd, info_ptr, &initial_length_size);
6634 offset_size = initial_length_size == 4 ? 4 : 8;
6635 info_ptr += initial_length_size;
6636
6637 version = read_2_bytes (abfd, info_ptr);
6638 info_ptr += 2;
6639 if (version >= 5)
6640 {
6641 /* Skip unit type and address size. */
6642 info_ptr += 2;
6643 }
6644
6645 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6646 }
6647
6648 /* Allocate a new partial symtab for file named NAME and mark this new
6649 partial symtab as being an include of PST. */
6650
6651 static void
6652 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6653 struct objfile *objfile)
6654 {
6655 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6656
6657 if (!IS_ABSOLUTE_PATH (subpst->filename))
6658 {
6659 /* It shares objfile->objfile_obstack. */
6660 subpst->dirname = pst->dirname;
6661 }
6662
6663 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6664 subpst->dependencies[0] = pst;
6665 subpst->number_of_dependencies = 1;
6666
6667 subpst->read_symtab = pst->read_symtab;
6668
6669 /* No private part is necessary for include psymtabs. This property
6670 can be used to differentiate between such include psymtabs and
6671 the regular ones. */
6672 subpst->read_symtab_private = NULL;
6673 }
6674
6675 /* Read the Line Number Program data and extract the list of files
6676 included by the source file represented by PST. Build an include
6677 partial symtab for each of these included files. */
6678
6679 static void
6680 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6681 struct die_info *die,
6682 struct partial_symtab *pst)
6683 {
6684 line_header_up lh;
6685 struct attribute *attr;
6686
6687 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6688 if (attr != nullptr)
6689 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6690 if (lh == NULL)
6691 return; /* No linetable, so no includes. */
6692
6693 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6694 that we pass in the raw text_low here; that is ok because we're
6695 only decoding the line table to make include partial symtabs, and
6696 so the addresses aren't really used. */
6697 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6698 pst->raw_text_low (), 1);
6699 }
6700
6701 static hashval_t
6702 hash_signatured_type (const void *item)
6703 {
6704 const struct signatured_type *sig_type
6705 = (const struct signatured_type *) item;
6706
6707 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6708 return sig_type->signature;
6709 }
6710
6711 static int
6712 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6713 {
6714 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6715 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6716
6717 return lhs->signature == rhs->signature;
6718 }
6719
6720 /* Allocate a hash table for signatured types. */
6721
6722 static htab_t
6723 allocate_signatured_type_table (struct objfile *objfile)
6724 {
6725 return htab_create_alloc_ex (41,
6726 hash_signatured_type,
6727 eq_signatured_type,
6728 NULL,
6729 &objfile->objfile_obstack,
6730 hashtab_obstack_allocate,
6731 dummy_obstack_deallocate);
6732 }
6733
6734 /* A helper function to add a signatured type CU to a table. */
6735
6736 static int
6737 add_signatured_type_cu_to_table (void **slot, void *datum)
6738 {
6739 struct signatured_type *sigt = (struct signatured_type *) *slot;
6740 std::vector<signatured_type *> *all_type_units
6741 = (std::vector<signatured_type *> *) datum;
6742
6743 all_type_units->push_back (sigt);
6744
6745 return 1;
6746 }
6747
6748 /* A helper for create_debug_types_hash_table. Read types from SECTION
6749 and fill them into TYPES_HTAB. It will process only type units,
6750 therefore DW_UT_type. */
6751
6752 static void
6753 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6754 struct dwo_file *dwo_file,
6755 dwarf2_section_info *section, htab_t &types_htab,
6756 rcuh_kind section_kind)
6757 {
6758 struct objfile *objfile = dwarf2_per_objfile->objfile;
6759 struct dwarf2_section_info *abbrev_section;
6760 bfd *abfd;
6761 const gdb_byte *info_ptr, *end_ptr;
6762
6763 abbrev_section = (dwo_file != NULL
6764 ? &dwo_file->sections.abbrev
6765 : &dwarf2_per_objfile->abbrev);
6766
6767 if (dwarf_read_debug)
6768 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6769 get_section_name (section),
6770 get_section_file_name (abbrev_section));
6771
6772 dwarf2_read_section (objfile, section);
6773 info_ptr = section->buffer;
6774
6775 if (info_ptr == NULL)
6776 return;
6777
6778 /* We can't set abfd until now because the section may be empty or
6779 not present, in which case the bfd is unknown. */
6780 abfd = get_section_bfd_owner (section);
6781
6782 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6783 because we don't need to read any dies: the signature is in the
6784 header. */
6785
6786 end_ptr = info_ptr + section->size;
6787 while (info_ptr < end_ptr)
6788 {
6789 struct signatured_type *sig_type;
6790 struct dwo_unit *dwo_tu;
6791 void **slot;
6792 const gdb_byte *ptr = info_ptr;
6793 struct comp_unit_head header;
6794 unsigned int length;
6795
6796 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6797
6798 /* Initialize it due to a false compiler warning. */
6799 header.signature = -1;
6800 header.type_cu_offset_in_tu = (cu_offset) -1;
6801
6802 /* We need to read the type's signature in order to build the hash
6803 table, but we don't need anything else just yet. */
6804
6805 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6806 abbrev_section, ptr, section_kind);
6807
6808 length = get_cu_length (&header);
6809
6810 /* Skip dummy type units. */
6811 if (ptr >= info_ptr + length
6812 || peek_abbrev_code (abfd, ptr) == 0
6813 || header.unit_type != DW_UT_type)
6814 {
6815 info_ptr += length;
6816 continue;
6817 }
6818
6819 if (types_htab == NULL)
6820 {
6821 if (dwo_file)
6822 types_htab = allocate_dwo_unit_table (objfile);
6823 else
6824 types_htab = allocate_signatured_type_table (objfile);
6825 }
6826
6827 if (dwo_file)
6828 {
6829 sig_type = NULL;
6830 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6831 struct dwo_unit);
6832 dwo_tu->dwo_file = dwo_file;
6833 dwo_tu->signature = header.signature;
6834 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6835 dwo_tu->section = section;
6836 dwo_tu->sect_off = sect_off;
6837 dwo_tu->length = length;
6838 }
6839 else
6840 {
6841 /* N.B.: type_offset is not usable if this type uses a DWO file.
6842 The real type_offset is in the DWO file. */
6843 dwo_tu = NULL;
6844 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6845 struct signatured_type);
6846 sig_type->signature = header.signature;
6847 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6848 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6849 sig_type->per_cu.is_debug_types = 1;
6850 sig_type->per_cu.section = section;
6851 sig_type->per_cu.sect_off = sect_off;
6852 sig_type->per_cu.length = length;
6853 }
6854
6855 slot = htab_find_slot (types_htab,
6856 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6857 INSERT);
6858 gdb_assert (slot != NULL);
6859 if (*slot != NULL)
6860 {
6861 sect_offset dup_sect_off;
6862
6863 if (dwo_file)
6864 {
6865 const struct dwo_unit *dup_tu
6866 = (const struct dwo_unit *) *slot;
6867
6868 dup_sect_off = dup_tu->sect_off;
6869 }
6870 else
6871 {
6872 const struct signatured_type *dup_tu
6873 = (const struct signatured_type *) *slot;
6874
6875 dup_sect_off = dup_tu->per_cu.sect_off;
6876 }
6877
6878 complaint (_("debug type entry at offset %s is duplicate to"
6879 " the entry at offset %s, signature %s"),
6880 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6881 hex_string (header.signature));
6882 }
6883 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6884
6885 if (dwarf_read_debug > 1)
6886 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6887 sect_offset_str (sect_off),
6888 hex_string (header.signature));
6889
6890 info_ptr += length;
6891 }
6892 }
6893
6894 /* Create the hash table of all entries in the .debug_types
6895 (or .debug_types.dwo) section(s).
6896 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6897 otherwise it is NULL.
6898
6899 The result is a pointer to the hash table or NULL if there are no types.
6900
6901 Note: This function processes DWO files only, not DWP files. */
6902
6903 static void
6904 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6905 struct dwo_file *dwo_file,
6906 gdb::array_view<dwarf2_section_info> type_sections,
6907 htab_t &types_htab)
6908 {
6909 for (dwarf2_section_info &section : type_sections)
6910 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6911 types_htab, rcuh_kind::TYPE);
6912 }
6913
6914 /* Create the hash table of all entries in the .debug_types section,
6915 and initialize all_type_units.
6916 The result is zero if there is an error (e.g. missing .debug_types section),
6917 otherwise non-zero. */
6918
6919 static int
6920 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6921 {
6922 htab_t types_htab = NULL;
6923
6924 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6925 &dwarf2_per_objfile->info, types_htab,
6926 rcuh_kind::COMPILE);
6927 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6928 dwarf2_per_objfile->types, types_htab);
6929 if (types_htab == NULL)
6930 {
6931 dwarf2_per_objfile->signatured_types = NULL;
6932 return 0;
6933 }
6934
6935 dwarf2_per_objfile->signatured_types = types_htab;
6936
6937 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6938 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6939
6940 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6941 &dwarf2_per_objfile->all_type_units);
6942
6943 return 1;
6944 }
6945
6946 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6947 If SLOT is non-NULL, it is the entry to use in the hash table.
6948 Otherwise we find one. */
6949
6950 static struct signatured_type *
6951 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6952 void **slot)
6953 {
6954 struct objfile *objfile = dwarf2_per_objfile->objfile;
6955
6956 if (dwarf2_per_objfile->all_type_units.size ()
6957 == dwarf2_per_objfile->all_type_units.capacity ())
6958 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6959
6960 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6961 struct signatured_type);
6962
6963 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6964 sig_type->signature = sig;
6965 sig_type->per_cu.is_debug_types = 1;
6966 if (dwarf2_per_objfile->using_index)
6967 {
6968 sig_type->per_cu.v.quick =
6969 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6970 struct dwarf2_per_cu_quick_data);
6971 }
6972
6973 if (slot == NULL)
6974 {
6975 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6976 sig_type, INSERT);
6977 }
6978 gdb_assert (*slot == NULL);
6979 *slot = sig_type;
6980 /* The rest of sig_type must be filled in by the caller. */
6981 return sig_type;
6982 }
6983
6984 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6985 Fill in SIG_ENTRY with DWO_ENTRY. */
6986
6987 static void
6988 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6989 struct signatured_type *sig_entry,
6990 struct dwo_unit *dwo_entry)
6991 {
6992 /* Make sure we're not clobbering something we don't expect to. */
6993 gdb_assert (! sig_entry->per_cu.queued);
6994 gdb_assert (sig_entry->per_cu.cu == NULL);
6995 if (dwarf2_per_objfile->using_index)
6996 {
6997 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6998 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6999 }
7000 else
7001 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7002 gdb_assert (sig_entry->signature == dwo_entry->signature);
7003 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7004 gdb_assert (sig_entry->type_unit_group == NULL);
7005 gdb_assert (sig_entry->dwo_unit == NULL);
7006
7007 sig_entry->per_cu.section = dwo_entry->section;
7008 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7009 sig_entry->per_cu.length = dwo_entry->length;
7010 sig_entry->per_cu.reading_dwo_directly = 1;
7011 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7012 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7013 sig_entry->dwo_unit = dwo_entry;
7014 }
7015
7016 /* Subroutine of lookup_signatured_type.
7017 If we haven't read the TU yet, create the signatured_type data structure
7018 for a TU to be read in directly from a DWO file, bypassing the stub.
7019 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7020 using .gdb_index, then when reading a CU we want to stay in the DWO file
7021 containing that CU. Otherwise we could end up reading several other DWO
7022 files (due to comdat folding) to process the transitive closure of all the
7023 mentioned TUs, and that can be slow. The current DWO file will have every
7024 type signature that it needs.
7025 We only do this for .gdb_index because in the psymtab case we already have
7026 to read all the DWOs to build the type unit groups. */
7027
7028 static struct signatured_type *
7029 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7030 {
7031 struct dwarf2_per_objfile *dwarf2_per_objfile
7032 = cu->per_cu->dwarf2_per_objfile;
7033 struct objfile *objfile = dwarf2_per_objfile->objfile;
7034 struct dwo_file *dwo_file;
7035 struct dwo_unit find_dwo_entry, *dwo_entry;
7036 struct signatured_type find_sig_entry, *sig_entry;
7037 void **slot;
7038
7039 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7040
7041 /* If TU skeletons have been removed then we may not have read in any
7042 TUs yet. */
7043 if (dwarf2_per_objfile->signatured_types == NULL)
7044 {
7045 dwarf2_per_objfile->signatured_types
7046 = allocate_signatured_type_table (objfile);
7047 }
7048
7049 /* We only ever need to read in one copy of a signatured type.
7050 Use the global signatured_types array to do our own comdat-folding
7051 of types. If this is the first time we're reading this TU, and
7052 the TU has an entry in .gdb_index, replace the recorded data from
7053 .gdb_index with this TU. */
7054
7055 find_sig_entry.signature = sig;
7056 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7057 &find_sig_entry, INSERT);
7058 sig_entry = (struct signatured_type *) *slot;
7059
7060 /* We can get here with the TU already read, *or* in the process of being
7061 read. Don't reassign the global entry to point to this DWO if that's
7062 the case. Also note that if the TU is already being read, it may not
7063 have come from a DWO, the program may be a mix of Fission-compiled
7064 code and non-Fission-compiled code. */
7065
7066 /* Have we already tried to read this TU?
7067 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7068 needn't exist in the global table yet). */
7069 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7070 return sig_entry;
7071
7072 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7073 dwo_unit of the TU itself. */
7074 dwo_file = cu->dwo_unit->dwo_file;
7075
7076 /* Ok, this is the first time we're reading this TU. */
7077 if (dwo_file->tus == NULL)
7078 return NULL;
7079 find_dwo_entry.signature = sig;
7080 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7081 if (dwo_entry == NULL)
7082 return NULL;
7083
7084 /* If the global table doesn't have an entry for this TU, add one. */
7085 if (sig_entry == NULL)
7086 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7087
7088 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7089 sig_entry->per_cu.tu_read = 1;
7090 return sig_entry;
7091 }
7092
7093 /* Subroutine of lookup_signatured_type.
7094 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7095 then try the DWP file. If the TU stub (skeleton) has been removed then
7096 it won't be in .gdb_index. */
7097
7098 static struct signatured_type *
7099 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7100 {
7101 struct dwarf2_per_objfile *dwarf2_per_objfile
7102 = cu->per_cu->dwarf2_per_objfile;
7103 struct objfile *objfile = dwarf2_per_objfile->objfile;
7104 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7105 struct dwo_unit *dwo_entry;
7106 struct signatured_type find_sig_entry, *sig_entry;
7107 void **slot;
7108
7109 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7110 gdb_assert (dwp_file != NULL);
7111
7112 /* If TU skeletons have been removed then we may not have read in any
7113 TUs yet. */
7114 if (dwarf2_per_objfile->signatured_types == NULL)
7115 {
7116 dwarf2_per_objfile->signatured_types
7117 = allocate_signatured_type_table (objfile);
7118 }
7119
7120 find_sig_entry.signature = sig;
7121 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7122 &find_sig_entry, INSERT);
7123 sig_entry = (struct signatured_type *) *slot;
7124
7125 /* Have we already tried to read this TU?
7126 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7127 needn't exist in the global table yet). */
7128 if (sig_entry != NULL)
7129 return sig_entry;
7130
7131 if (dwp_file->tus == NULL)
7132 return NULL;
7133 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7134 sig, 1 /* is_debug_types */);
7135 if (dwo_entry == NULL)
7136 return NULL;
7137
7138 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7139 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7140
7141 return sig_entry;
7142 }
7143
7144 /* Lookup a signature based type for DW_FORM_ref_sig8.
7145 Returns NULL if signature SIG is not present in the table.
7146 It is up to the caller to complain about this. */
7147
7148 static struct signatured_type *
7149 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7150 {
7151 struct dwarf2_per_objfile *dwarf2_per_objfile
7152 = cu->per_cu->dwarf2_per_objfile;
7153
7154 if (cu->dwo_unit
7155 && dwarf2_per_objfile->using_index)
7156 {
7157 /* We're in a DWO/DWP file, and we're using .gdb_index.
7158 These cases require special processing. */
7159 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7160 return lookup_dwo_signatured_type (cu, sig);
7161 else
7162 return lookup_dwp_signatured_type (cu, sig);
7163 }
7164 else
7165 {
7166 struct signatured_type find_entry, *entry;
7167
7168 if (dwarf2_per_objfile->signatured_types == NULL)
7169 return NULL;
7170 find_entry.signature = sig;
7171 entry = ((struct signatured_type *)
7172 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7173 return entry;
7174 }
7175 }
7176 \f
7177 /* Low level DIE reading support. */
7178
7179 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7180
7181 static void
7182 init_cu_die_reader (struct die_reader_specs *reader,
7183 struct dwarf2_cu *cu,
7184 struct dwarf2_section_info *section,
7185 struct dwo_file *dwo_file,
7186 struct abbrev_table *abbrev_table)
7187 {
7188 gdb_assert (section->readin && section->buffer != NULL);
7189 reader->abfd = get_section_bfd_owner (section);
7190 reader->cu = cu;
7191 reader->dwo_file = dwo_file;
7192 reader->die_section = section;
7193 reader->buffer = section->buffer;
7194 reader->buffer_end = section->buffer + section->size;
7195 reader->comp_dir = NULL;
7196 reader->abbrev_table = abbrev_table;
7197 }
7198
7199 /* Subroutine of init_cutu_and_read_dies to simplify it.
7200 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7201 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7202 already.
7203
7204 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7205 from it to the DIE in the DWO. If NULL we are skipping the stub.
7206 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7207 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7208 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7209 STUB_COMP_DIR may be non-NULL.
7210 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7211 are filled in with the info of the DIE from the DWO file.
7212 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7213 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7214 kept around for at least as long as *RESULT_READER.
7215
7216 The result is non-zero if a valid (non-dummy) DIE was found. */
7217
7218 static int
7219 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7220 struct dwo_unit *dwo_unit,
7221 struct die_info *stub_comp_unit_die,
7222 const char *stub_comp_dir,
7223 struct die_reader_specs *result_reader,
7224 const gdb_byte **result_info_ptr,
7225 struct die_info **result_comp_unit_die,
7226 int *result_has_children,
7227 abbrev_table_up *result_dwo_abbrev_table)
7228 {
7229 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7230 struct objfile *objfile = dwarf2_per_objfile->objfile;
7231 struct dwarf2_cu *cu = this_cu->cu;
7232 bfd *abfd;
7233 const gdb_byte *begin_info_ptr, *info_ptr;
7234 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7235 int i,num_extra_attrs;
7236 struct dwarf2_section_info *dwo_abbrev_section;
7237 struct attribute *attr;
7238 struct die_info *comp_unit_die;
7239
7240 /* At most one of these may be provided. */
7241 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7242
7243 /* These attributes aren't processed until later:
7244 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7245 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7246 referenced later. However, these attributes are found in the stub
7247 which we won't have later. In order to not impose this complication
7248 on the rest of the code, we read them here and copy them to the
7249 DWO CU/TU die. */
7250
7251 stmt_list = NULL;
7252 low_pc = NULL;
7253 high_pc = NULL;
7254 ranges = NULL;
7255 comp_dir = NULL;
7256
7257 if (stub_comp_unit_die != NULL)
7258 {
7259 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7260 DWO file. */
7261 if (! this_cu->is_debug_types)
7262 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7263 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7264 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7265 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7266 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7267
7268 /* There should be a DW_AT_addr_base attribute here (if needed).
7269 We need the value before we can process DW_FORM_GNU_addr_index
7270 or DW_FORM_addrx. */
7271 cu->addr_base = 0;
7272 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7273 if (attr != nullptr)
7274 cu->addr_base = DW_UNSND (attr);
7275
7276 /* There should be a DW_AT_ranges_base attribute here (if needed).
7277 We need the value before we can process DW_AT_ranges. */
7278 cu->ranges_base = 0;
7279 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7280 if (attr != nullptr)
7281 cu->ranges_base = DW_UNSND (attr);
7282 }
7283 else if (stub_comp_dir != NULL)
7284 {
7285 /* Reconstruct the comp_dir attribute to simplify the code below. */
7286 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7287 comp_dir->name = DW_AT_comp_dir;
7288 comp_dir->form = DW_FORM_string;
7289 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7290 DW_STRING (comp_dir) = stub_comp_dir;
7291 }
7292
7293 /* Set up for reading the DWO CU/TU. */
7294 cu->dwo_unit = dwo_unit;
7295 dwarf2_section_info *section = dwo_unit->section;
7296 dwarf2_read_section (objfile, section);
7297 abfd = get_section_bfd_owner (section);
7298 begin_info_ptr = info_ptr = (section->buffer
7299 + to_underlying (dwo_unit->sect_off));
7300 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7301
7302 if (this_cu->is_debug_types)
7303 {
7304 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7305
7306 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7307 &cu->header, section,
7308 dwo_abbrev_section,
7309 info_ptr, rcuh_kind::TYPE);
7310 /* This is not an assert because it can be caused by bad debug info. */
7311 if (sig_type->signature != cu->header.signature)
7312 {
7313 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7314 " TU at offset %s [in module %s]"),
7315 hex_string (sig_type->signature),
7316 hex_string (cu->header.signature),
7317 sect_offset_str (dwo_unit->sect_off),
7318 bfd_get_filename (abfd));
7319 }
7320 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7321 /* For DWOs coming from DWP files, we don't know the CU length
7322 nor the type's offset in the TU until now. */
7323 dwo_unit->length = get_cu_length (&cu->header);
7324 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7325
7326 /* Establish the type offset that can be used to lookup the type.
7327 For DWO files, we don't know it until now. */
7328 sig_type->type_offset_in_section
7329 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7330 }
7331 else
7332 {
7333 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7334 &cu->header, section,
7335 dwo_abbrev_section,
7336 info_ptr, rcuh_kind::COMPILE);
7337 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7338 /* For DWOs coming from DWP files, we don't know the CU length
7339 until now. */
7340 dwo_unit->length = get_cu_length (&cu->header);
7341 }
7342
7343 *result_dwo_abbrev_table
7344 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7345 cu->header.abbrev_sect_off);
7346 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7347 result_dwo_abbrev_table->get ());
7348
7349 /* Read in the die, but leave space to copy over the attributes
7350 from the stub. This has the benefit of simplifying the rest of
7351 the code - all the work to maintain the illusion of a single
7352 DW_TAG_{compile,type}_unit DIE is done here. */
7353 num_extra_attrs = ((stmt_list != NULL)
7354 + (low_pc != NULL)
7355 + (high_pc != NULL)
7356 + (ranges != NULL)
7357 + (comp_dir != NULL));
7358 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7359 result_has_children, num_extra_attrs);
7360
7361 /* Copy over the attributes from the stub to the DIE we just read in. */
7362 comp_unit_die = *result_comp_unit_die;
7363 i = comp_unit_die->num_attrs;
7364 if (stmt_list != NULL)
7365 comp_unit_die->attrs[i++] = *stmt_list;
7366 if (low_pc != NULL)
7367 comp_unit_die->attrs[i++] = *low_pc;
7368 if (high_pc != NULL)
7369 comp_unit_die->attrs[i++] = *high_pc;
7370 if (ranges != NULL)
7371 comp_unit_die->attrs[i++] = *ranges;
7372 if (comp_dir != NULL)
7373 comp_unit_die->attrs[i++] = *comp_dir;
7374 comp_unit_die->num_attrs += num_extra_attrs;
7375
7376 if (dwarf_die_debug)
7377 {
7378 fprintf_unfiltered (gdb_stdlog,
7379 "Read die from %s@0x%x of %s:\n",
7380 get_section_name (section),
7381 (unsigned) (begin_info_ptr - section->buffer),
7382 bfd_get_filename (abfd));
7383 dump_die (comp_unit_die, dwarf_die_debug);
7384 }
7385
7386 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7387 TUs by skipping the stub and going directly to the entry in the DWO file.
7388 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7389 to get it via circuitous means. Blech. */
7390 if (comp_dir != NULL)
7391 result_reader->comp_dir = DW_STRING (comp_dir);
7392
7393 /* Skip dummy compilation units. */
7394 if (info_ptr >= begin_info_ptr + dwo_unit->length
7395 || peek_abbrev_code (abfd, info_ptr) == 0)
7396 return 0;
7397
7398 *result_info_ptr = info_ptr;
7399 return 1;
7400 }
7401
7402 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7403 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7404 signature is part of the header. */
7405 static gdb::optional<ULONGEST>
7406 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7407 {
7408 if (cu->header.version >= 5)
7409 return cu->header.signature;
7410 struct attribute *attr;
7411 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7412 if (attr == nullptr)
7413 return gdb::optional<ULONGEST> ();
7414 return DW_UNSND (attr);
7415 }
7416
7417 /* Subroutine of init_cutu_and_read_dies to simplify it.
7418 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7419 Returns NULL if the specified DWO unit cannot be found. */
7420
7421 static struct dwo_unit *
7422 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7423 struct die_info *comp_unit_die)
7424 {
7425 struct dwarf2_cu *cu = this_cu->cu;
7426 struct dwo_unit *dwo_unit;
7427 const char *comp_dir, *dwo_name;
7428
7429 gdb_assert (cu != NULL);
7430
7431 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7432 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7433 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7434
7435 if (this_cu->is_debug_types)
7436 {
7437 struct signatured_type *sig_type;
7438
7439 /* Since this_cu is the first member of struct signatured_type,
7440 we can go from a pointer to one to a pointer to the other. */
7441 sig_type = (struct signatured_type *) this_cu;
7442 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7443 }
7444 else
7445 {
7446 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7447 if (!signature.has_value ())
7448 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7449 " [in module %s]"),
7450 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7451 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7452 *signature);
7453 }
7454
7455 return dwo_unit;
7456 }
7457
7458 /* Subroutine of init_cutu_and_read_dies to simplify it.
7459 See it for a description of the parameters.
7460 Read a TU directly from a DWO file, bypassing the stub. */
7461
7462 static void
7463 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7464 int use_existing_cu, int keep,
7465 die_reader_func_ftype *die_reader_func,
7466 void *data)
7467 {
7468 std::unique_ptr<dwarf2_cu> new_cu;
7469 struct signatured_type *sig_type;
7470 struct die_reader_specs reader;
7471 const gdb_byte *info_ptr;
7472 struct die_info *comp_unit_die;
7473 int has_children;
7474 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7475
7476 /* Verify we can do the following downcast, and that we have the
7477 data we need. */
7478 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7479 sig_type = (struct signatured_type *) this_cu;
7480 gdb_assert (sig_type->dwo_unit != NULL);
7481
7482 if (use_existing_cu && this_cu->cu != NULL)
7483 {
7484 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7485 /* There's no need to do the rereading_dwo_cu handling that
7486 init_cutu_and_read_dies does since we don't read the stub. */
7487 }
7488 else
7489 {
7490 /* If !use_existing_cu, this_cu->cu must be NULL. */
7491 gdb_assert (this_cu->cu == NULL);
7492 new_cu.reset (new dwarf2_cu (this_cu));
7493 }
7494
7495 /* A future optimization, if needed, would be to use an existing
7496 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7497 could share abbrev tables. */
7498
7499 /* The abbreviation table used by READER, this must live at least as long as
7500 READER. */
7501 abbrev_table_up dwo_abbrev_table;
7502
7503 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7504 NULL /* stub_comp_unit_die */,
7505 sig_type->dwo_unit->dwo_file->comp_dir,
7506 &reader, &info_ptr,
7507 &comp_unit_die, &has_children,
7508 &dwo_abbrev_table) == 0)
7509 {
7510 /* Dummy die. */
7511 return;
7512 }
7513
7514 /* All the "real" work is done here. */
7515 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7516
7517 /* This duplicates the code in init_cutu_and_read_dies,
7518 but the alternative is making the latter more complex.
7519 This function is only for the special case of using DWO files directly:
7520 no point in overly complicating the general case just to handle this. */
7521 if (new_cu != NULL && keep)
7522 {
7523 /* Link this CU into read_in_chain. */
7524 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7525 dwarf2_per_objfile->read_in_chain = this_cu;
7526 /* The chain owns it now. */
7527 new_cu.release ();
7528 }
7529 }
7530
7531 /* Initialize a CU (or TU) and read its DIEs.
7532 If the CU defers to a DWO file, read the DWO file as well.
7533
7534 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7535 Otherwise the table specified in the comp unit header is read in and used.
7536 This is an optimization for when we already have the abbrev table.
7537
7538 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7539 Otherwise, a new CU is allocated with xmalloc.
7540
7541 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7542 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7543
7544 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7545 linker) then DIE_READER_FUNC will not get called. */
7546
7547 static void
7548 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7549 struct abbrev_table *abbrev_table,
7550 int use_existing_cu, int keep,
7551 bool skip_partial,
7552 die_reader_func_ftype *die_reader_func,
7553 void *data)
7554 {
7555 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7556 struct objfile *objfile = dwarf2_per_objfile->objfile;
7557 struct dwarf2_section_info *section = this_cu->section;
7558 bfd *abfd = get_section_bfd_owner (section);
7559 struct dwarf2_cu *cu;
7560 const gdb_byte *begin_info_ptr, *info_ptr;
7561 struct die_reader_specs reader;
7562 struct die_info *comp_unit_die;
7563 int has_children;
7564 struct signatured_type *sig_type = NULL;
7565 struct dwarf2_section_info *abbrev_section;
7566 /* Non-zero if CU currently points to a DWO file and we need to
7567 reread it. When this happens we need to reread the skeleton die
7568 before we can reread the DWO file (this only applies to CUs, not TUs). */
7569 int rereading_dwo_cu = 0;
7570
7571 if (dwarf_die_debug)
7572 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7573 this_cu->is_debug_types ? "type" : "comp",
7574 sect_offset_str (this_cu->sect_off));
7575
7576 if (use_existing_cu)
7577 gdb_assert (keep);
7578
7579 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7580 file (instead of going through the stub), short-circuit all of this. */
7581 if (this_cu->reading_dwo_directly)
7582 {
7583 /* Narrow down the scope of possibilities to have to understand. */
7584 gdb_assert (this_cu->is_debug_types);
7585 gdb_assert (abbrev_table == NULL);
7586 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7587 die_reader_func, data);
7588 return;
7589 }
7590
7591 /* This is cheap if the section is already read in. */
7592 dwarf2_read_section (objfile, section);
7593
7594 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7595
7596 abbrev_section = get_abbrev_section_for_cu (this_cu);
7597
7598 std::unique_ptr<dwarf2_cu> new_cu;
7599 if (use_existing_cu && this_cu->cu != NULL)
7600 {
7601 cu = this_cu->cu;
7602 /* If this CU is from a DWO file we need to start over, we need to
7603 refetch the attributes from the skeleton CU.
7604 This could be optimized by retrieving those attributes from when we
7605 were here the first time: the previous comp_unit_die was stored in
7606 comp_unit_obstack. But there's no data yet that we need this
7607 optimization. */
7608 if (cu->dwo_unit != NULL)
7609 rereading_dwo_cu = 1;
7610 }
7611 else
7612 {
7613 /* If !use_existing_cu, this_cu->cu must be NULL. */
7614 gdb_assert (this_cu->cu == NULL);
7615 new_cu.reset (new dwarf2_cu (this_cu));
7616 cu = new_cu.get ();
7617 }
7618
7619 /* Get the header. */
7620 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7621 {
7622 /* We already have the header, there's no need to read it in again. */
7623 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7624 }
7625 else
7626 {
7627 if (this_cu->is_debug_types)
7628 {
7629 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7630 &cu->header, section,
7631 abbrev_section, info_ptr,
7632 rcuh_kind::TYPE);
7633
7634 /* Since per_cu is the first member of struct signatured_type,
7635 we can go from a pointer to one to a pointer to the other. */
7636 sig_type = (struct signatured_type *) this_cu;
7637 gdb_assert (sig_type->signature == cu->header.signature);
7638 gdb_assert (sig_type->type_offset_in_tu
7639 == cu->header.type_cu_offset_in_tu);
7640 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7641
7642 /* LENGTH has not been set yet for type units if we're
7643 using .gdb_index. */
7644 this_cu->length = get_cu_length (&cu->header);
7645
7646 /* Establish the type offset that can be used to lookup the type. */
7647 sig_type->type_offset_in_section =
7648 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7649
7650 this_cu->dwarf_version = cu->header.version;
7651 }
7652 else
7653 {
7654 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7655 &cu->header, section,
7656 abbrev_section,
7657 info_ptr,
7658 rcuh_kind::COMPILE);
7659
7660 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7661 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7662 this_cu->dwarf_version = cu->header.version;
7663 }
7664 }
7665
7666 /* Skip dummy compilation units. */
7667 if (info_ptr >= begin_info_ptr + this_cu->length
7668 || peek_abbrev_code (abfd, info_ptr) == 0)
7669 return;
7670
7671 /* If we don't have them yet, read the abbrevs for this compilation unit.
7672 And if we need to read them now, make sure they're freed when we're
7673 done (own the table through ABBREV_TABLE_HOLDER). */
7674 abbrev_table_up abbrev_table_holder;
7675 if (abbrev_table != NULL)
7676 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7677 else
7678 {
7679 abbrev_table_holder
7680 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7681 cu->header.abbrev_sect_off);
7682 abbrev_table = abbrev_table_holder.get ();
7683 }
7684
7685 /* Read the top level CU/TU die. */
7686 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7687 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7688
7689 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7690 return;
7691
7692 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7693 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7694 table from the DWO file and pass the ownership over to us. It will be
7695 referenced from READER, so we must make sure to free it after we're done
7696 with READER.
7697
7698 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7699 DWO CU, that this test will fail (the attribute will not be present). */
7700 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7701 abbrev_table_up dwo_abbrev_table;
7702 if (dwo_name != nullptr)
7703 {
7704 struct dwo_unit *dwo_unit;
7705 struct die_info *dwo_comp_unit_die;
7706
7707 if (has_children)
7708 {
7709 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7710 " has children (offset %s) [in module %s]"),
7711 sect_offset_str (this_cu->sect_off),
7712 bfd_get_filename (abfd));
7713 }
7714 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7715 if (dwo_unit != NULL)
7716 {
7717 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7718 comp_unit_die, NULL,
7719 &reader, &info_ptr,
7720 &dwo_comp_unit_die, &has_children,
7721 &dwo_abbrev_table) == 0)
7722 {
7723 /* Dummy die. */
7724 return;
7725 }
7726 comp_unit_die = dwo_comp_unit_die;
7727 }
7728 else
7729 {
7730 /* Yikes, we couldn't find the rest of the DIE, we only have
7731 the stub. A complaint has already been logged. There's
7732 not much more we can do except pass on the stub DIE to
7733 die_reader_func. We don't want to throw an error on bad
7734 debug info. */
7735 }
7736 }
7737
7738 /* All of the above is setup for this call. Yikes. */
7739 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7740
7741 /* Done, clean up. */
7742 if (new_cu != NULL && keep)
7743 {
7744 /* Link this CU into read_in_chain. */
7745 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7746 dwarf2_per_objfile->read_in_chain = this_cu;
7747 /* The chain owns it now. */
7748 new_cu.release ();
7749 }
7750 }
7751
7752 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7753 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7754 to have already done the lookup to find the DWO file).
7755
7756 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7757 THIS_CU->is_debug_types, but nothing else.
7758
7759 We fill in THIS_CU->length.
7760
7761 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7762 linker) then DIE_READER_FUNC will not get called.
7763
7764 THIS_CU->cu is always freed when done.
7765 This is done in order to not leave THIS_CU->cu in a state where we have
7766 to care whether it refers to the "main" CU or the DWO CU. */
7767
7768 static void
7769 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7770 struct dwo_file *dwo_file,
7771 die_reader_func_ftype *die_reader_func,
7772 void *data)
7773 {
7774 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7775 struct objfile *objfile = dwarf2_per_objfile->objfile;
7776 struct dwarf2_section_info *section = this_cu->section;
7777 bfd *abfd = get_section_bfd_owner (section);
7778 struct dwarf2_section_info *abbrev_section;
7779 const gdb_byte *begin_info_ptr, *info_ptr;
7780 struct die_reader_specs reader;
7781 struct die_info *comp_unit_die;
7782 int has_children;
7783
7784 if (dwarf_die_debug)
7785 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7786 this_cu->is_debug_types ? "type" : "comp",
7787 sect_offset_str (this_cu->sect_off));
7788
7789 gdb_assert (this_cu->cu == NULL);
7790
7791 abbrev_section = (dwo_file != NULL
7792 ? &dwo_file->sections.abbrev
7793 : get_abbrev_section_for_cu (this_cu));
7794
7795 /* This is cheap if the section is already read in. */
7796 dwarf2_read_section (objfile, section);
7797
7798 struct dwarf2_cu cu (this_cu);
7799
7800 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7801 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7802 &cu.header, section,
7803 abbrev_section, info_ptr,
7804 (this_cu->is_debug_types
7805 ? rcuh_kind::TYPE
7806 : rcuh_kind::COMPILE));
7807
7808 this_cu->length = get_cu_length (&cu.header);
7809
7810 /* Skip dummy compilation units. */
7811 if (info_ptr >= begin_info_ptr + this_cu->length
7812 || peek_abbrev_code (abfd, info_ptr) == 0)
7813 return;
7814
7815 abbrev_table_up abbrev_table
7816 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7817 cu.header.abbrev_sect_off);
7818
7819 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
7820 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7821
7822 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7823 }
7824
7825 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7826 does not lookup the specified DWO file.
7827 This cannot be used to read DWO files.
7828
7829 THIS_CU->cu is always freed when done.
7830 This is done in order to not leave THIS_CU->cu in a state where we have
7831 to care whether it refers to the "main" CU or the DWO CU.
7832 We can revisit this if the data shows there's a performance issue. */
7833
7834 static void
7835 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7836 die_reader_func_ftype *die_reader_func,
7837 void *data)
7838 {
7839 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
7840 }
7841 \f
7842 /* Type Unit Groups.
7843
7844 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7845 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7846 so that all types coming from the same compilation (.o file) are grouped
7847 together. A future step could be to put the types in the same symtab as
7848 the CU the types ultimately came from. */
7849
7850 static hashval_t
7851 hash_type_unit_group (const void *item)
7852 {
7853 const struct type_unit_group *tu_group
7854 = (const struct type_unit_group *) item;
7855
7856 return hash_stmt_list_entry (&tu_group->hash);
7857 }
7858
7859 static int
7860 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7861 {
7862 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7863 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7864
7865 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7866 }
7867
7868 /* Allocate a hash table for type unit groups. */
7869
7870 static htab_t
7871 allocate_type_unit_groups_table (struct objfile *objfile)
7872 {
7873 return htab_create_alloc_ex (3,
7874 hash_type_unit_group,
7875 eq_type_unit_group,
7876 NULL,
7877 &objfile->objfile_obstack,
7878 hashtab_obstack_allocate,
7879 dummy_obstack_deallocate);
7880 }
7881
7882 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7883 partial symtabs. We combine several TUs per psymtab to not let the size
7884 of any one psymtab grow too big. */
7885 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7886 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7887
7888 /* Helper routine for get_type_unit_group.
7889 Create the type_unit_group object used to hold one or more TUs. */
7890
7891 static struct type_unit_group *
7892 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7893 {
7894 struct dwarf2_per_objfile *dwarf2_per_objfile
7895 = cu->per_cu->dwarf2_per_objfile;
7896 struct objfile *objfile = dwarf2_per_objfile->objfile;
7897 struct dwarf2_per_cu_data *per_cu;
7898 struct type_unit_group *tu_group;
7899
7900 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7901 struct type_unit_group);
7902 per_cu = &tu_group->per_cu;
7903 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7904
7905 if (dwarf2_per_objfile->using_index)
7906 {
7907 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7908 struct dwarf2_per_cu_quick_data);
7909 }
7910 else
7911 {
7912 unsigned int line_offset = to_underlying (line_offset_struct);
7913 struct partial_symtab *pst;
7914 std::string name;
7915
7916 /* Give the symtab a useful name for debug purposes. */
7917 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7918 name = string_printf ("<type_units_%d>",
7919 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7920 else
7921 name = string_printf ("<type_units_at_0x%x>", line_offset);
7922
7923 pst = create_partial_symtab (per_cu, name.c_str ());
7924 pst->anonymous = 1;
7925 }
7926
7927 tu_group->hash.dwo_unit = cu->dwo_unit;
7928 tu_group->hash.line_sect_off = line_offset_struct;
7929
7930 return tu_group;
7931 }
7932
7933 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7934 STMT_LIST is a DW_AT_stmt_list attribute. */
7935
7936 static struct type_unit_group *
7937 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7938 {
7939 struct dwarf2_per_objfile *dwarf2_per_objfile
7940 = cu->per_cu->dwarf2_per_objfile;
7941 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7942 struct type_unit_group *tu_group;
7943 void **slot;
7944 unsigned int line_offset;
7945 struct type_unit_group type_unit_group_for_lookup;
7946
7947 if (dwarf2_per_objfile->type_unit_groups == NULL)
7948 {
7949 dwarf2_per_objfile->type_unit_groups =
7950 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7951 }
7952
7953 /* Do we need to create a new group, or can we use an existing one? */
7954
7955 if (stmt_list)
7956 {
7957 line_offset = DW_UNSND (stmt_list);
7958 ++tu_stats->nr_symtab_sharers;
7959 }
7960 else
7961 {
7962 /* Ugh, no stmt_list. Rare, but we have to handle it.
7963 We can do various things here like create one group per TU or
7964 spread them over multiple groups to split up the expansion work.
7965 To avoid worst case scenarios (too many groups or too large groups)
7966 we, umm, group them in bunches. */
7967 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7968 | (tu_stats->nr_stmt_less_type_units
7969 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7970 ++tu_stats->nr_stmt_less_type_units;
7971 }
7972
7973 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7974 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7975 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7976 &type_unit_group_for_lookup, INSERT);
7977 if (*slot != NULL)
7978 {
7979 tu_group = (struct type_unit_group *) *slot;
7980 gdb_assert (tu_group != NULL);
7981 }
7982 else
7983 {
7984 sect_offset line_offset_struct = (sect_offset) line_offset;
7985 tu_group = create_type_unit_group (cu, line_offset_struct);
7986 *slot = tu_group;
7987 ++tu_stats->nr_symtabs;
7988 }
7989
7990 return tu_group;
7991 }
7992 \f
7993 /* Partial symbol tables. */
7994
7995 /* Create a psymtab named NAME and assign it to PER_CU.
7996
7997 The caller must fill in the following details:
7998 dirname, textlow, texthigh. */
7999
8000 static struct partial_symtab *
8001 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8002 {
8003 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
8004 struct partial_symtab *pst;
8005
8006 pst = start_psymtab_common (objfile, name, 0);
8007
8008 pst->psymtabs_addrmap_supported = 1;
8009
8010 /* This is the glue that links PST into GDB's symbol API. */
8011 pst->read_symtab_private = per_cu;
8012 pst->read_symtab = dwarf2_read_symtab;
8013 per_cu->v.psymtab = pst;
8014
8015 return pst;
8016 }
8017
8018 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8019 type. */
8020
8021 struct process_psymtab_comp_unit_data
8022 {
8023 /* True if we are reading a DW_TAG_partial_unit. */
8024
8025 int want_partial_unit;
8026
8027 /* The "pretend" language that is used if the CU doesn't declare a
8028 language. */
8029
8030 enum language pretend_language;
8031 };
8032
8033 /* die_reader_func for process_psymtab_comp_unit. */
8034
8035 static void
8036 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8037 const gdb_byte *info_ptr,
8038 struct die_info *comp_unit_die,
8039 int has_children,
8040 void *data)
8041 {
8042 struct dwarf2_cu *cu = reader->cu;
8043 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8044 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8045 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8046 CORE_ADDR baseaddr;
8047 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8048 struct partial_symtab *pst;
8049 enum pc_bounds_kind cu_bounds_kind;
8050 const char *filename;
8051 struct process_psymtab_comp_unit_data *info
8052 = (struct process_psymtab_comp_unit_data *) data;
8053
8054 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8055 return;
8056
8057 gdb_assert (! per_cu->is_debug_types);
8058
8059 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8060
8061 /* Allocate a new partial symbol table structure. */
8062 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8063 if (filename == NULL)
8064 filename = "";
8065
8066 pst = create_partial_symtab (per_cu, filename);
8067
8068 /* This must be done before calling dwarf2_build_include_psymtabs. */
8069 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8070
8071 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8072
8073 dwarf2_find_base_address (comp_unit_die, cu);
8074
8075 /* Possibly set the default values of LOWPC and HIGHPC from
8076 `DW_AT_ranges'. */
8077 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8078 &best_highpc, cu, pst);
8079 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8080 {
8081 CORE_ADDR low
8082 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8083 - baseaddr);
8084 CORE_ADDR high
8085 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8086 - baseaddr - 1);
8087 /* Store the contiguous range if it is not empty; it can be
8088 empty for CUs with no code. */
8089 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8090 low, high, pst);
8091 }
8092
8093 /* Check if comp unit has_children.
8094 If so, read the rest of the partial symbols from this comp unit.
8095 If not, there's no more debug_info for this comp unit. */
8096 if (has_children)
8097 {
8098 struct partial_die_info *first_die;
8099 CORE_ADDR lowpc, highpc;
8100
8101 lowpc = ((CORE_ADDR) -1);
8102 highpc = ((CORE_ADDR) 0);
8103
8104 first_die = load_partial_dies (reader, info_ptr, 1);
8105
8106 scan_partial_symbols (first_die, &lowpc, &highpc,
8107 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8108
8109 /* If we didn't find a lowpc, set it to highpc to avoid
8110 complaints from `maint check'. */
8111 if (lowpc == ((CORE_ADDR) -1))
8112 lowpc = highpc;
8113
8114 /* If the compilation unit didn't have an explicit address range,
8115 then use the information extracted from its child dies. */
8116 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8117 {
8118 best_lowpc = lowpc;
8119 best_highpc = highpc;
8120 }
8121 }
8122 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
8123 best_lowpc + baseaddr)
8124 - baseaddr);
8125 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
8126 best_highpc + baseaddr)
8127 - baseaddr);
8128
8129 end_psymtab_common (objfile, pst);
8130
8131 if (!cu->per_cu->imported_symtabs_empty ())
8132 {
8133 int i;
8134 int len = cu->per_cu->imported_symtabs_size ();
8135
8136 /* Fill in 'dependencies' here; we fill in 'users' in a
8137 post-pass. */
8138 pst->number_of_dependencies = len;
8139 pst->dependencies
8140 = objfile->partial_symtabs->allocate_dependencies (len);
8141 for (i = 0; i < len; ++i)
8142 {
8143 pst->dependencies[i]
8144 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8145 }
8146
8147 cu->per_cu->imported_symtabs_free ();
8148 }
8149
8150 /* Get the list of files included in the current compilation unit,
8151 and build a psymtab for each of them. */
8152 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8153
8154 if (dwarf_read_debug)
8155 fprintf_unfiltered (gdb_stdlog,
8156 "Psymtab for %s unit @%s: %s - %s"
8157 ", %d global, %d static syms\n",
8158 per_cu->is_debug_types ? "type" : "comp",
8159 sect_offset_str (per_cu->sect_off),
8160 paddress (gdbarch, pst->text_low (objfile)),
8161 paddress (gdbarch, pst->text_high (objfile)),
8162 pst->n_global_syms, pst->n_static_syms);
8163 }
8164
8165 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8166 Process compilation unit THIS_CU for a psymtab. */
8167
8168 static void
8169 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8170 int want_partial_unit,
8171 enum language pretend_language)
8172 {
8173 /* If this compilation unit was already read in, free the
8174 cached copy in order to read it in again. This is
8175 necessary because we skipped some symbols when we first
8176 read in the compilation unit (see load_partial_dies).
8177 This problem could be avoided, but the benefit is unclear. */
8178 if (this_cu->cu != NULL)
8179 free_one_cached_comp_unit (this_cu);
8180
8181 if (this_cu->is_debug_types)
8182 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8183 build_type_psymtabs_reader, NULL);
8184 else
8185 {
8186 process_psymtab_comp_unit_data info;
8187 info.want_partial_unit = want_partial_unit;
8188 info.pretend_language = pretend_language;
8189 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8190 process_psymtab_comp_unit_reader, &info);
8191 }
8192
8193 /* Age out any secondary CUs. */
8194 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8195 }
8196
8197 /* Reader function for build_type_psymtabs. */
8198
8199 static void
8200 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8201 const gdb_byte *info_ptr,
8202 struct die_info *type_unit_die,
8203 int has_children,
8204 void *data)
8205 {
8206 struct dwarf2_per_objfile *dwarf2_per_objfile
8207 = reader->cu->per_cu->dwarf2_per_objfile;
8208 struct objfile *objfile = dwarf2_per_objfile->objfile;
8209 struct dwarf2_cu *cu = reader->cu;
8210 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8211 struct signatured_type *sig_type;
8212 struct type_unit_group *tu_group;
8213 struct attribute *attr;
8214 struct partial_die_info *first_die;
8215 CORE_ADDR lowpc, highpc;
8216 struct partial_symtab *pst;
8217
8218 gdb_assert (data == NULL);
8219 gdb_assert (per_cu->is_debug_types);
8220 sig_type = (struct signatured_type *) per_cu;
8221
8222 if (! has_children)
8223 return;
8224
8225 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8226 tu_group = get_type_unit_group (cu, attr);
8227
8228 if (tu_group->tus == nullptr)
8229 tu_group->tus = new std::vector<signatured_type *>;
8230 tu_group->tus->push_back (sig_type);
8231
8232 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8233 pst = create_partial_symtab (per_cu, "");
8234 pst->anonymous = 1;
8235
8236 first_die = load_partial_dies (reader, info_ptr, 1);
8237
8238 lowpc = (CORE_ADDR) -1;
8239 highpc = (CORE_ADDR) 0;
8240 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8241
8242 end_psymtab_common (objfile, pst);
8243 }
8244
8245 /* Struct used to sort TUs by their abbreviation table offset. */
8246
8247 struct tu_abbrev_offset
8248 {
8249 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8250 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8251 {}
8252
8253 signatured_type *sig_type;
8254 sect_offset abbrev_offset;
8255 };
8256
8257 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8258
8259 static bool
8260 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8261 const struct tu_abbrev_offset &b)
8262 {
8263 return a.abbrev_offset < b.abbrev_offset;
8264 }
8265
8266 /* Efficiently read all the type units.
8267 This does the bulk of the work for build_type_psymtabs.
8268
8269 The efficiency is because we sort TUs by the abbrev table they use and
8270 only read each abbrev table once. In one program there are 200K TUs
8271 sharing 8K abbrev tables.
8272
8273 The main purpose of this function is to support building the
8274 dwarf2_per_objfile->type_unit_groups table.
8275 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8276 can collapse the search space by grouping them by stmt_list.
8277 The savings can be significant, in the same program from above the 200K TUs
8278 share 8K stmt_list tables.
8279
8280 FUNC is expected to call get_type_unit_group, which will create the
8281 struct type_unit_group if necessary and add it to
8282 dwarf2_per_objfile->type_unit_groups. */
8283
8284 static void
8285 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8286 {
8287 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8288 abbrev_table_up abbrev_table;
8289 sect_offset abbrev_offset;
8290
8291 /* It's up to the caller to not call us multiple times. */
8292 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8293
8294 if (dwarf2_per_objfile->all_type_units.empty ())
8295 return;
8296
8297 /* TUs typically share abbrev tables, and there can be way more TUs than
8298 abbrev tables. Sort by abbrev table to reduce the number of times we
8299 read each abbrev table in.
8300 Alternatives are to punt or to maintain a cache of abbrev tables.
8301 This is simpler and efficient enough for now.
8302
8303 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8304 symtab to use). Typically TUs with the same abbrev offset have the same
8305 stmt_list value too so in practice this should work well.
8306
8307 The basic algorithm here is:
8308
8309 sort TUs by abbrev table
8310 for each TU with same abbrev table:
8311 read abbrev table if first user
8312 read TU top level DIE
8313 [IWBN if DWO skeletons had DW_AT_stmt_list]
8314 call FUNC */
8315
8316 if (dwarf_read_debug)
8317 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8318
8319 /* Sort in a separate table to maintain the order of all_type_units
8320 for .gdb_index: TU indices directly index all_type_units. */
8321 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8322 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8323
8324 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8325 sorted_by_abbrev.emplace_back
8326 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8327 sig_type->per_cu.section,
8328 sig_type->per_cu.sect_off));
8329
8330 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8331 sort_tu_by_abbrev_offset);
8332
8333 abbrev_offset = (sect_offset) ~(unsigned) 0;
8334
8335 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8336 {
8337 /* Switch to the next abbrev table if necessary. */
8338 if (abbrev_table == NULL
8339 || tu.abbrev_offset != abbrev_offset)
8340 {
8341 abbrev_offset = tu.abbrev_offset;
8342 abbrev_table =
8343 abbrev_table_read_table (dwarf2_per_objfile,
8344 &dwarf2_per_objfile->abbrev,
8345 abbrev_offset);
8346 ++tu_stats->nr_uniq_abbrev_tables;
8347 }
8348
8349 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
8350 0, 0, false, build_type_psymtabs_reader, NULL);
8351 }
8352 }
8353
8354 /* Print collected type unit statistics. */
8355
8356 static void
8357 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8358 {
8359 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8360
8361 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8362 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8363 dwarf2_per_objfile->all_type_units.size ());
8364 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8365 tu_stats->nr_uniq_abbrev_tables);
8366 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8367 tu_stats->nr_symtabs);
8368 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8369 tu_stats->nr_symtab_sharers);
8370 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8371 tu_stats->nr_stmt_less_type_units);
8372 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8373 tu_stats->nr_all_type_units_reallocs);
8374 }
8375
8376 /* Traversal function for build_type_psymtabs. */
8377
8378 static int
8379 build_type_psymtab_dependencies (void **slot, void *info)
8380 {
8381 struct dwarf2_per_objfile *dwarf2_per_objfile
8382 = (struct dwarf2_per_objfile *) info;
8383 struct objfile *objfile = dwarf2_per_objfile->objfile;
8384 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8385 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8386 struct partial_symtab *pst = per_cu->v.psymtab;
8387 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8388 int i;
8389
8390 gdb_assert (len > 0);
8391 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8392
8393 pst->number_of_dependencies = len;
8394 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8395 for (i = 0; i < len; ++i)
8396 {
8397 struct signatured_type *iter = tu_group->tus->at (i);
8398 gdb_assert (iter->per_cu.is_debug_types);
8399 pst->dependencies[i] = iter->per_cu.v.psymtab;
8400 iter->type_unit_group = tu_group;
8401 }
8402
8403 delete tu_group->tus;
8404 tu_group->tus = nullptr;
8405
8406 return 1;
8407 }
8408
8409 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8410 Build partial symbol tables for the .debug_types comp-units. */
8411
8412 static void
8413 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8414 {
8415 if (! create_all_type_units (dwarf2_per_objfile))
8416 return;
8417
8418 build_type_psymtabs_1 (dwarf2_per_objfile);
8419 }
8420
8421 /* Traversal function for process_skeletonless_type_unit.
8422 Read a TU in a DWO file and build partial symbols for it. */
8423
8424 static int
8425 process_skeletonless_type_unit (void **slot, void *info)
8426 {
8427 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8428 struct dwarf2_per_objfile *dwarf2_per_objfile
8429 = (struct dwarf2_per_objfile *) info;
8430 struct signatured_type find_entry, *entry;
8431
8432 /* If this TU doesn't exist in the global table, add it and read it in. */
8433
8434 if (dwarf2_per_objfile->signatured_types == NULL)
8435 {
8436 dwarf2_per_objfile->signatured_types
8437 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8438 }
8439
8440 find_entry.signature = dwo_unit->signature;
8441 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8442 INSERT);
8443 /* If we've already seen this type there's nothing to do. What's happening
8444 is we're doing our own version of comdat-folding here. */
8445 if (*slot != NULL)
8446 return 1;
8447
8448 /* This does the job that create_all_type_units would have done for
8449 this TU. */
8450 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8451 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8452 *slot = entry;
8453
8454 /* This does the job that build_type_psymtabs_1 would have done. */
8455 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
8456 build_type_psymtabs_reader, NULL);
8457
8458 return 1;
8459 }
8460
8461 /* Traversal function for process_skeletonless_type_units. */
8462
8463 static int
8464 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8465 {
8466 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8467
8468 if (dwo_file->tus != NULL)
8469 {
8470 htab_traverse_noresize (dwo_file->tus,
8471 process_skeletonless_type_unit, info);
8472 }
8473
8474 return 1;
8475 }
8476
8477 /* Scan all TUs of DWO files, verifying we've processed them.
8478 This is needed in case a TU was emitted without its skeleton.
8479 Note: This can't be done until we know what all the DWO files are. */
8480
8481 static void
8482 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8483 {
8484 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8485 if (get_dwp_file (dwarf2_per_objfile) == NULL
8486 && dwarf2_per_objfile->dwo_files != NULL)
8487 {
8488 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
8489 process_dwo_file_for_skeletonless_type_units,
8490 dwarf2_per_objfile);
8491 }
8492 }
8493
8494 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8495
8496 static void
8497 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8498 {
8499 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8500 {
8501 struct partial_symtab *pst = per_cu->v.psymtab;
8502
8503 if (pst == NULL)
8504 continue;
8505
8506 for (int j = 0; j < pst->number_of_dependencies; ++j)
8507 {
8508 /* Set the 'user' field only if it is not already set. */
8509 if (pst->dependencies[j]->user == NULL)
8510 pst->dependencies[j]->user = pst;
8511 }
8512 }
8513 }
8514
8515 /* Build the partial symbol table by doing a quick pass through the
8516 .debug_info and .debug_abbrev sections. */
8517
8518 static void
8519 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8520 {
8521 struct objfile *objfile = dwarf2_per_objfile->objfile;
8522
8523 if (dwarf_read_debug)
8524 {
8525 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8526 objfile_name (objfile));
8527 }
8528
8529 dwarf2_per_objfile->reading_partial_symbols = 1;
8530
8531 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8532
8533 /* Any cached compilation units will be linked by the per-objfile
8534 read_in_chain. Make sure to free them when we're done. */
8535 free_cached_comp_units freer (dwarf2_per_objfile);
8536
8537 build_type_psymtabs (dwarf2_per_objfile);
8538
8539 create_all_comp_units (dwarf2_per_objfile);
8540
8541 /* Create a temporary address map on a temporary obstack. We later
8542 copy this to the final obstack. */
8543 auto_obstack temp_obstack;
8544
8545 scoped_restore save_psymtabs_addrmap
8546 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8547 addrmap_create_mutable (&temp_obstack));
8548
8549 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8550 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8551
8552 /* This has to wait until we read the CUs, we need the list of DWOs. */
8553 process_skeletonless_type_units (dwarf2_per_objfile);
8554
8555 /* Now that all TUs have been processed we can fill in the dependencies. */
8556 if (dwarf2_per_objfile->type_unit_groups != NULL)
8557 {
8558 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8559 build_type_psymtab_dependencies, dwarf2_per_objfile);
8560 }
8561
8562 if (dwarf_read_debug)
8563 print_tu_stats (dwarf2_per_objfile);
8564
8565 set_partial_user (dwarf2_per_objfile);
8566
8567 objfile->partial_symtabs->psymtabs_addrmap
8568 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8569 objfile->partial_symtabs->obstack ());
8570 /* At this point we want to keep the address map. */
8571 save_psymtabs_addrmap.release ();
8572
8573 if (dwarf_read_debug)
8574 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8575 objfile_name (objfile));
8576 }
8577
8578 /* die_reader_func for load_partial_comp_unit. */
8579
8580 static void
8581 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8582 const gdb_byte *info_ptr,
8583 struct die_info *comp_unit_die,
8584 int has_children,
8585 void *data)
8586 {
8587 struct dwarf2_cu *cu = reader->cu;
8588
8589 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8590
8591 /* Check if comp unit has_children.
8592 If so, read the rest of the partial symbols from this comp unit.
8593 If not, there's no more debug_info for this comp unit. */
8594 if (has_children)
8595 load_partial_dies (reader, info_ptr, 0);
8596 }
8597
8598 /* Load the partial DIEs for a secondary CU into memory.
8599 This is also used when rereading a primary CU with load_all_dies. */
8600
8601 static void
8602 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8603 {
8604 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
8605 load_partial_comp_unit_reader, NULL);
8606 }
8607
8608 static void
8609 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8610 struct dwarf2_section_info *section,
8611 struct dwarf2_section_info *abbrev_section,
8612 unsigned int is_dwz)
8613 {
8614 const gdb_byte *info_ptr;
8615 struct objfile *objfile = dwarf2_per_objfile->objfile;
8616
8617 if (dwarf_read_debug)
8618 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8619 get_section_name (section),
8620 get_section_file_name (section));
8621
8622 dwarf2_read_section (objfile, section);
8623
8624 info_ptr = section->buffer;
8625
8626 while (info_ptr < section->buffer + section->size)
8627 {
8628 struct dwarf2_per_cu_data *this_cu;
8629
8630 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8631
8632 comp_unit_head cu_header;
8633 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8634 abbrev_section, info_ptr,
8635 rcuh_kind::COMPILE);
8636
8637 /* Save the compilation unit for later lookup. */
8638 if (cu_header.unit_type != DW_UT_type)
8639 {
8640 this_cu = XOBNEW (&objfile->objfile_obstack,
8641 struct dwarf2_per_cu_data);
8642 memset (this_cu, 0, sizeof (*this_cu));
8643 }
8644 else
8645 {
8646 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8647 struct signatured_type);
8648 memset (sig_type, 0, sizeof (*sig_type));
8649 sig_type->signature = cu_header.signature;
8650 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8651 this_cu = &sig_type->per_cu;
8652 }
8653 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8654 this_cu->sect_off = sect_off;
8655 this_cu->length = cu_header.length + cu_header.initial_length_size;
8656 this_cu->is_dwz = is_dwz;
8657 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8658 this_cu->section = section;
8659
8660 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8661
8662 info_ptr = info_ptr + this_cu->length;
8663 }
8664 }
8665
8666 /* Create a list of all compilation units in OBJFILE.
8667 This is only done for -readnow and building partial symtabs. */
8668
8669 static void
8670 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8671 {
8672 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8673 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8674 &dwarf2_per_objfile->abbrev, 0);
8675
8676 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8677 if (dwz != NULL)
8678 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8679 1);
8680 }
8681
8682 /* Process all loaded DIEs for compilation unit CU, starting at
8683 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8684 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8685 DW_AT_ranges). See the comments of add_partial_subprogram on how
8686 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8687
8688 static void
8689 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8690 CORE_ADDR *highpc, int set_addrmap,
8691 struct dwarf2_cu *cu)
8692 {
8693 struct partial_die_info *pdi;
8694
8695 /* Now, march along the PDI's, descending into ones which have
8696 interesting children but skipping the children of the other ones,
8697 until we reach the end of the compilation unit. */
8698
8699 pdi = first_die;
8700
8701 while (pdi != NULL)
8702 {
8703 pdi->fixup (cu);
8704
8705 /* Anonymous namespaces or modules have no name but have interesting
8706 children, so we need to look at them. Ditto for anonymous
8707 enums. */
8708
8709 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8710 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8711 || pdi->tag == DW_TAG_imported_unit
8712 || pdi->tag == DW_TAG_inlined_subroutine)
8713 {
8714 switch (pdi->tag)
8715 {
8716 case DW_TAG_subprogram:
8717 case DW_TAG_inlined_subroutine:
8718 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8719 break;
8720 case DW_TAG_constant:
8721 case DW_TAG_variable:
8722 case DW_TAG_typedef:
8723 case DW_TAG_union_type:
8724 if (!pdi->is_declaration)
8725 {
8726 add_partial_symbol (pdi, cu);
8727 }
8728 break;
8729 case DW_TAG_class_type:
8730 case DW_TAG_interface_type:
8731 case DW_TAG_structure_type:
8732 if (!pdi->is_declaration)
8733 {
8734 add_partial_symbol (pdi, cu);
8735 }
8736 if ((cu->language == language_rust
8737 || cu->language == language_cplus) && pdi->has_children)
8738 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8739 set_addrmap, cu);
8740 break;
8741 case DW_TAG_enumeration_type:
8742 if (!pdi->is_declaration)
8743 add_partial_enumeration (pdi, cu);
8744 break;
8745 case DW_TAG_base_type:
8746 case DW_TAG_subrange_type:
8747 /* File scope base type definitions are added to the partial
8748 symbol table. */
8749 add_partial_symbol (pdi, cu);
8750 break;
8751 case DW_TAG_namespace:
8752 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8753 break;
8754 case DW_TAG_module:
8755 if (!pdi->is_declaration)
8756 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8757 break;
8758 case DW_TAG_imported_unit:
8759 {
8760 struct dwarf2_per_cu_data *per_cu;
8761
8762 /* For now we don't handle imported units in type units. */
8763 if (cu->per_cu->is_debug_types)
8764 {
8765 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8766 " supported in type units [in module %s]"),
8767 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8768 }
8769
8770 per_cu = dwarf2_find_containing_comp_unit
8771 (pdi->d.sect_off, pdi->is_dwz,
8772 cu->per_cu->dwarf2_per_objfile);
8773
8774 /* Go read the partial unit, if needed. */
8775 if (per_cu->v.psymtab == NULL)
8776 process_psymtab_comp_unit (per_cu, 1, cu->language);
8777
8778 cu->per_cu->imported_symtabs_push (per_cu);
8779 }
8780 break;
8781 case DW_TAG_imported_declaration:
8782 add_partial_symbol (pdi, cu);
8783 break;
8784 default:
8785 break;
8786 }
8787 }
8788
8789 /* If the die has a sibling, skip to the sibling. */
8790
8791 pdi = pdi->die_sibling;
8792 }
8793 }
8794
8795 /* Functions used to compute the fully scoped name of a partial DIE.
8796
8797 Normally, this is simple. For C++, the parent DIE's fully scoped
8798 name is concatenated with "::" and the partial DIE's name.
8799 Enumerators are an exception; they use the scope of their parent
8800 enumeration type, i.e. the name of the enumeration type is not
8801 prepended to the enumerator.
8802
8803 There are two complexities. One is DW_AT_specification; in this
8804 case "parent" means the parent of the target of the specification,
8805 instead of the direct parent of the DIE. The other is compilers
8806 which do not emit DW_TAG_namespace; in this case we try to guess
8807 the fully qualified name of structure types from their members'
8808 linkage names. This must be done using the DIE's children rather
8809 than the children of any DW_AT_specification target. We only need
8810 to do this for structures at the top level, i.e. if the target of
8811 any DW_AT_specification (if any; otherwise the DIE itself) does not
8812 have a parent. */
8813
8814 /* Compute the scope prefix associated with PDI's parent, in
8815 compilation unit CU. The result will be allocated on CU's
8816 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8817 field. NULL is returned if no prefix is necessary. */
8818 static const char *
8819 partial_die_parent_scope (struct partial_die_info *pdi,
8820 struct dwarf2_cu *cu)
8821 {
8822 const char *grandparent_scope;
8823 struct partial_die_info *parent, *real_pdi;
8824
8825 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8826 then this means the parent of the specification DIE. */
8827
8828 real_pdi = pdi;
8829 while (real_pdi->has_specification)
8830 {
8831 auto res = find_partial_die (real_pdi->spec_offset,
8832 real_pdi->spec_is_dwz, cu);
8833 real_pdi = res.pdi;
8834 cu = res.cu;
8835 }
8836
8837 parent = real_pdi->die_parent;
8838 if (parent == NULL)
8839 return NULL;
8840
8841 if (parent->scope_set)
8842 return parent->scope;
8843
8844 parent->fixup (cu);
8845
8846 grandparent_scope = partial_die_parent_scope (parent, cu);
8847
8848 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8849 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8850 Work around this problem here. */
8851 if (cu->language == language_cplus
8852 && parent->tag == DW_TAG_namespace
8853 && strcmp (parent->name, "::") == 0
8854 && grandparent_scope == NULL)
8855 {
8856 parent->scope = NULL;
8857 parent->scope_set = 1;
8858 return NULL;
8859 }
8860
8861 /* Nested subroutines in Fortran get a prefix. */
8862 if (pdi->tag == DW_TAG_enumerator)
8863 /* Enumerators should not get the name of the enumeration as a prefix. */
8864 parent->scope = grandparent_scope;
8865 else if (parent->tag == DW_TAG_namespace
8866 || parent->tag == DW_TAG_module
8867 || parent->tag == DW_TAG_structure_type
8868 || parent->tag == DW_TAG_class_type
8869 || parent->tag == DW_TAG_interface_type
8870 || parent->tag == DW_TAG_union_type
8871 || parent->tag == DW_TAG_enumeration_type
8872 || (cu->language == language_fortran
8873 && parent->tag == DW_TAG_subprogram
8874 && pdi->tag == DW_TAG_subprogram))
8875 {
8876 if (grandparent_scope == NULL)
8877 parent->scope = parent->name;
8878 else
8879 parent->scope = typename_concat (&cu->comp_unit_obstack,
8880 grandparent_scope,
8881 parent->name, 0, cu);
8882 }
8883 else
8884 {
8885 /* FIXME drow/2004-04-01: What should we be doing with
8886 function-local names? For partial symbols, we should probably be
8887 ignoring them. */
8888 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8889 dwarf_tag_name (parent->tag),
8890 sect_offset_str (pdi->sect_off));
8891 parent->scope = grandparent_scope;
8892 }
8893
8894 parent->scope_set = 1;
8895 return parent->scope;
8896 }
8897
8898 /* Return the fully scoped name associated with PDI, from compilation unit
8899 CU. The result will be allocated with malloc. */
8900
8901 static gdb::unique_xmalloc_ptr<char>
8902 partial_die_full_name (struct partial_die_info *pdi,
8903 struct dwarf2_cu *cu)
8904 {
8905 const char *parent_scope;
8906
8907 /* If this is a template instantiation, we can not work out the
8908 template arguments from partial DIEs. So, unfortunately, we have
8909 to go through the full DIEs. At least any work we do building
8910 types here will be reused if full symbols are loaded later. */
8911 if (pdi->has_template_arguments)
8912 {
8913 pdi->fixup (cu);
8914
8915 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8916 {
8917 struct die_info *die;
8918 struct attribute attr;
8919 struct dwarf2_cu *ref_cu = cu;
8920
8921 /* DW_FORM_ref_addr is using section offset. */
8922 attr.name = (enum dwarf_attribute) 0;
8923 attr.form = DW_FORM_ref_addr;
8924 attr.u.unsnd = to_underlying (pdi->sect_off);
8925 die = follow_die_ref (NULL, &attr, &ref_cu);
8926
8927 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8928 }
8929 }
8930
8931 parent_scope = partial_die_parent_scope (pdi, cu);
8932 if (parent_scope == NULL)
8933 return NULL;
8934 else
8935 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8936 pdi->name, 0, cu));
8937 }
8938
8939 static void
8940 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8941 {
8942 struct dwarf2_per_objfile *dwarf2_per_objfile
8943 = cu->per_cu->dwarf2_per_objfile;
8944 struct objfile *objfile = dwarf2_per_objfile->objfile;
8945 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8946 CORE_ADDR addr = 0;
8947 const char *actual_name = NULL;
8948 CORE_ADDR baseaddr;
8949
8950 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8951
8952 gdb::unique_xmalloc_ptr<char> built_actual_name
8953 = partial_die_full_name (pdi, cu);
8954 if (built_actual_name != NULL)
8955 actual_name = built_actual_name.get ();
8956
8957 if (actual_name == NULL)
8958 actual_name = pdi->name;
8959
8960 switch (pdi->tag)
8961 {
8962 case DW_TAG_inlined_subroutine:
8963 case DW_TAG_subprogram:
8964 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8965 - baseaddr);
8966 if (pdi->is_external
8967 || cu->language == language_ada
8968 || (cu->language == language_fortran
8969 && pdi->die_parent != NULL
8970 && pdi->die_parent->tag == DW_TAG_subprogram))
8971 {
8972 /* Normally, only "external" DIEs are part of the global scope.
8973 But in Ada and Fortran, we want to be able to access nested
8974 procedures globally. So all Ada and Fortran subprograms are
8975 stored in the global scope. */
8976 add_psymbol_to_list (actual_name,
8977 built_actual_name != NULL,
8978 VAR_DOMAIN, LOC_BLOCK,
8979 SECT_OFF_TEXT (objfile),
8980 psymbol_placement::GLOBAL,
8981 addr,
8982 cu->language, objfile);
8983 }
8984 else
8985 {
8986 add_psymbol_to_list (actual_name,
8987 built_actual_name != NULL,
8988 VAR_DOMAIN, LOC_BLOCK,
8989 SECT_OFF_TEXT (objfile),
8990 psymbol_placement::STATIC,
8991 addr, cu->language, objfile);
8992 }
8993
8994 if (pdi->main_subprogram && actual_name != NULL)
8995 set_objfile_main_name (objfile, actual_name, cu->language);
8996 break;
8997 case DW_TAG_constant:
8998 add_psymbol_to_list (actual_name,
8999 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9000 -1, (pdi->is_external
9001 ? psymbol_placement::GLOBAL
9002 : psymbol_placement::STATIC),
9003 0, cu->language, objfile);
9004 break;
9005 case DW_TAG_variable:
9006 if (pdi->d.locdesc)
9007 addr = decode_locdesc (pdi->d.locdesc, cu);
9008
9009 if (pdi->d.locdesc
9010 && addr == 0
9011 && !dwarf2_per_objfile->has_section_at_zero)
9012 {
9013 /* A global or static variable may also have been stripped
9014 out by the linker if unused, in which case its address
9015 will be nullified; do not add such variables into partial
9016 symbol table then. */
9017 }
9018 else if (pdi->is_external)
9019 {
9020 /* Global Variable.
9021 Don't enter into the minimal symbol tables as there is
9022 a minimal symbol table entry from the ELF symbols already.
9023 Enter into partial symbol table if it has a location
9024 descriptor or a type.
9025 If the location descriptor is missing, new_symbol will create
9026 a LOC_UNRESOLVED symbol, the address of the variable will then
9027 be determined from the minimal symbol table whenever the variable
9028 is referenced.
9029 The address for the partial symbol table entry is not
9030 used by GDB, but it comes in handy for debugging partial symbol
9031 table building. */
9032
9033 if (pdi->d.locdesc || pdi->has_type)
9034 add_psymbol_to_list (actual_name,
9035 built_actual_name != NULL,
9036 VAR_DOMAIN, LOC_STATIC,
9037 SECT_OFF_TEXT (objfile),
9038 psymbol_placement::GLOBAL,
9039 addr, cu->language, objfile);
9040 }
9041 else
9042 {
9043 int has_loc = pdi->d.locdesc != NULL;
9044
9045 /* Static Variable. Skip symbols whose value we cannot know (those
9046 without location descriptors or constant values). */
9047 if (!has_loc && !pdi->has_const_value)
9048 return;
9049
9050 add_psymbol_to_list (actual_name,
9051 built_actual_name != NULL,
9052 VAR_DOMAIN, LOC_STATIC,
9053 SECT_OFF_TEXT (objfile),
9054 psymbol_placement::STATIC,
9055 has_loc ? addr : 0,
9056 cu->language, objfile);
9057 }
9058 break;
9059 case DW_TAG_typedef:
9060 case DW_TAG_base_type:
9061 case DW_TAG_subrange_type:
9062 add_psymbol_to_list (actual_name,
9063 built_actual_name != NULL,
9064 VAR_DOMAIN, LOC_TYPEDEF, -1,
9065 psymbol_placement::STATIC,
9066 0, cu->language, objfile);
9067 break;
9068 case DW_TAG_imported_declaration:
9069 case DW_TAG_namespace:
9070 add_psymbol_to_list (actual_name,
9071 built_actual_name != NULL,
9072 VAR_DOMAIN, LOC_TYPEDEF, -1,
9073 psymbol_placement::GLOBAL,
9074 0, cu->language, objfile);
9075 break;
9076 case DW_TAG_module:
9077 /* With Fortran 77 there might be a "BLOCK DATA" module
9078 available without any name. If so, we skip the module as it
9079 doesn't bring any value. */
9080 if (actual_name != nullptr)
9081 add_psymbol_to_list (actual_name,
9082 built_actual_name != NULL,
9083 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9084 psymbol_placement::GLOBAL,
9085 0, cu->language, objfile);
9086 break;
9087 case DW_TAG_class_type:
9088 case DW_TAG_interface_type:
9089 case DW_TAG_structure_type:
9090 case DW_TAG_union_type:
9091 case DW_TAG_enumeration_type:
9092 /* Skip external references. The DWARF standard says in the section
9093 about "Structure, Union, and Class Type Entries": "An incomplete
9094 structure, union or class type is represented by a structure,
9095 union or class entry that does not have a byte size attribute
9096 and that has a DW_AT_declaration attribute." */
9097 if (!pdi->has_byte_size && pdi->is_declaration)
9098 return;
9099
9100 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9101 static vs. global. */
9102 add_psymbol_to_list (actual_name,
9103 built_actual_name != NULL,
9104 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9105 cu->language == language_cplus
9106 ? psymbol_placement::GLOBAL
9107 : psymbol_placement::STATIC,
9108 0, cu->language, objfile);
9109
9110 break;
9111 case DW_TAG_enumerator:
9112 add_psymbol_to_list (actual_name,
9113 built_actual_name != NULL,
9114 VAR_DOMAIN, LOC_CONST, -1,
9115 cu->language == language_cplus
9116 ? psymbol_placement::GLOBAL
9117 : psymbol_placement::STATIC,
9118 0, cu->language, objfile);
9119 break;
9120 default:
9121 break;
9122 }
9123 }
9124
9125 /* Read a partial die corresponding to a namespace; also, add a symbol
9126 corresponding to that namespace to the symbol table. NAMESPACE is
9127 the name of the enclosing namespace. */
9128
9129 static void
9130 add_partial_namespace (struct partial_die_info *pdi,
9131 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9132 int set_addrmap, struct dwarf2_cu *cu)
9133 {
9134 /* Add a symbol for the namespace. */
9135
9136 add_partial_symbol (pdi, cu);
9137
9138 /* Now scan partial symbols in that namespace. */
9139
9140 if (pdi->has_children)
9141 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9142 }
9143
9144 /* Read a partial die corresponding to a Fortran module. */
9145
9146 static void
9147 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9148 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9149 {
9150 /* Add a symbol for the namespace. */
9151
9152 add_partial_symbol (pdi, cu);
9153
9154 /* Now scan partial symbols in that module. */
9155
9156 if (pdi->has_children)
9157 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9158 }
9159
9160 /* Read a partial die corresponding to a subprogram or an inlined
9161 subprogram and create a partial symbol for that subprogram.
9162 When the CU language allows it, this routine also defines a partial
9163 symbol for each nested subprogram that this subprogram contains.
9164 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9165 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9166
9167 PDI may also be a lexical block, in which case we simply search
9168 recursively for subprograms defined inside that lexical block.
9169 Again, this is only performed when the CU language allows this
9170 type of definitions. */
9171
9172 static void
9173 add_partial_subprogram (struct partial_die_info *pdi,
9174 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9175 int set_addrmap, struct dwarf2_cu *cu)
9176 {
9177 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9178 {
9179 if (pdi->has_pc_info)
9180 {
9181 if (pdi->lowpc < *lowpc)
9182 *lowpc = pdi->lowpc;
9183 if (pdi->highpc > *highpc)
9184 *highpc = pdi->highpc;
9185 if (set_addrmap)
9186 {
9187 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9188 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9189 CORE_ADDR baseaddr;
9190 CORE_ADDR this_highpc;
9191 CORE_ADDR this_lowpc;
9192
9193 baseaddr = ANOFFSET (objfile->section_offsets,
9194 SECT_OFF_TEXT (objfile));
9195 this_lowpc
9196 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9197 pdi->lowpc + baseaddr)
9198 - baseaddr);
9199 this_highpc
9200 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9201 pdi->highpc + baseaddr)
9202 - baseaddr);
9203 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
9204 this_lowpc, this_highpc - 1,
9205 cu->per_cu->v.psymtab);
9206 }
9207 }
9208
9209 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9210 {
9211 if (!pdi->is_declaration)
9212 /* Ignore subprogram DIEs that do not have a name, they are
9213 illegal. Do not emit a complaint at this point, we will
9214 do so when we convert this psymtab into a symtab. */
9215 if (pdi->name)
9216 add_partial_symbol (pdi, cu);
9217 }
9218 }
9219
9220 if (! pdi->has_children)
9221 return;
9222
9223 if (cu->language == language_ada || cu->language == language_fortran)
9224 {
9225 pdi = pdi->die_child;
9226 while (pdi != NULL)
9227 {
9228 pdi->fixup (cu);
9229 if (pdi->tag == DW_TAG_subprogram
9230 || pdi->tag == DW_TAG_inlined_subroutine
9231 || pdi->tag == DW_TAG_lexical_block)
9232 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9233 pdi = pdi->die_sibling;
9234 }
9235 }
9236 }
9237
9238 /* Read a partial die corresponding to an enumeration type. */
9239
9240 static void
9241 add_partial_enumeration (struct partial_die_info *enum_pdi,
9242 struct dwarf2_cu *cu)
9243 {
9244 struct partial_die_info *pdi;
9245
9246 if (enum_pdi->name != NULL)
9247 add_partial_symbol (enum_pdi, cu);
9248
9249 pdi = enum_pdi->die_child;
9250 while (pdi)
9251 {
9252 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9253 complaint (_("malformed enumerator DIE ignored"));
9254 else
9255 add_partial_symbol (pdi, cu);
9256 pdi = pdi->die_sibling;
9257 }
9258 }
9259
9260 /* Return the initial uleb128 in the die at INFO_PTR. */
9261
9262 static unsigned int
9263 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9264 {
9265 unsigned int bytes_read;
9266
9267 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9268 }
9269
9270 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9271 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9272
9273 Return the corresponding abbrev, or NULL if the number is zero (indicating
9274 an empty DIE). In either case *BYTES_READ will be set to the length of
9275 the initial number. */
9276
9277 static struct abbrev_info *
9278 peek_die_abbrev (const die_reader_specs &reader,
9279 const gdb_byte *info_ptr, unsigned int *bytes_read)
9280 {
9281 dwarf2_cu *cu = reader.cu;
9282 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9283 unsigned int abbrev_number
9284 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9285
9286 if (abbrev_number == 0)
9287 return NULL;
9288
9289 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9290 if (!abbrev)
9291 {
9292 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9293 " at offset %s [in module %s]"),
9294 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9295 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9296 }
9297
9298 return abbrev;
9299 }
9300
9301 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9302 Returns a pointer to the end of a series of DIEs, terminated by an empty
9303 DIE. Any children of the skipped DIEs will also be skipped. */
9304
9305 static const gdb_byte *
9306 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9307 {
9308 while (1)
9309 {
9310 unsigned int bytes_read;
9311 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9312
9313 if (abbrev == NULL)
9314 return info_ptr + bytes_read;
9315 else
9316 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9317 }
9318 }
9319
9320 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9321 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9322 abbrev corresponding to that skipped uleb128 should be passed in
9323 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9324 children. */
9325
9326 static const gdb_byte *
9327 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9328 struct abbrev_info *abbrev)
9329 {
9330 unsigned int bytes_read;
9331 struct attribute attr;
9332 bfd *abfd = reader->abfd;
9333 struct dwarf2_cu *cu = reader->cu;
9334 const gdb_byte *buffer = reader->buffer;
9335 const gdb_byte *buffer_end = reader->buffer_end;
9336 unsigned int form, i;
9337
9338 for (i = 0; i < abbrev->num_attrs; i++)
9339 {
9340 /* The only abbrev we care about is DW_AT_sibling. */
9341 if (abbrev->attrs[i].name == DW_AT_sibling)
9342 {
9343 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9344 if (attr.form == DW_FORM_ref_addr)
9345 complaint (_("ignoring absolute DW_AT_sibling"));
9346 else
9347 {
9348 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9349 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9350
9351 if (sibling_ptr < info_ptr)
9352 complaint (_("DW_AT_sibling points backwards"));
9353 else if (sibling_ptr > reader->buffer_end)
9354 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9355 else
9356 return sibling_ptr;
9357 }
9358 }
9359
9360 /* If it isn't DW_AT_sibling, skip this attribute. */
9361 form = abbrev->attrs[i].form;
9362 skip_attribute:
9363 switch (form)
9364 {
9365 case DW_FORM_ref_addr:
9366 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9367 and later it is offset sized. */
9368 if (cu->header.version == 2)
9369 info_ptr += cu->header.addr_size;
9370 else
9371 info_ptr += cu->header.offset_size;
9372 break;
9373 case DW_FORM_GNU_ref_alt:
9374 info_ptr += cu->header.offset_size;
9375 break;
9376 case DW_FORM_addr:
9377 info_ptr += cu->header.addr_size;
9378 break;
9379 case DW_FORM_data1:
9380 case DW_FORM_ref1:
9381 case DW_FORM_flag:
9382 case DW_FORM_strx1:
9383 info_ptr += 1;
9384 break;
9385 case DW_FORM_flag_present:
9386 case DW_FORM_implicit_const:
9387 break;
9388 case DW_FORM_data2:
9389 case DW_FORM_ref2:
9390 case DW_FORM_strx2:
9391 info_ptr += 2;
9392 break;
9393 case DW_FORM_strx3:
9394 info_ptr += 3;
9395 break;
9396 case DW_FORM_data4:
9397 case DW_FORM_ref4:
9398 case DW_FORM_strx4:
9399 info_ptr += 4;
9400 break;
9401 case DW_FORM_data8:
9402 case DW_FORM_ref8:
9403 case DW_FORM_ref_sig8:
9404 info_ptr += 8;
9405 break;
9406 case DW_FORM_data16:
9407 info_ptr += 16;
9408 break;
9409 case DW_FORM_string:
9410 read_direct_string (abfd, info_ptr, &bytes_read);
9411 info_ptr += bytes_read;
9412 break;
9413 case DW_FORM_sec_offset:
9414 case DW_FORM_strp:
9415 case DW_FORM_GNU_strp_alt:
9416 info_ptr += cu->header.offset_size;
9417 break;
9418 case DW_FORM_exprloc:
9419 case DW_FORM_block:
9420 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9421 info_ptr += bytes_read;
9422 break;
9423 case DW_FORM_block1:
9424 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9425 break;
9426 case DW_FORM_block2:
9427 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9428 break;
9429 case DW_FORM_block4:
9430 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9431 break;
9432 case DW_FORM_addrx:
9433 case DW_FORM_strx:
9434 case DW_FORM_sdata:
9435 case DW_FORM_udata:
9436 case DW_FORM_ref_udata:
9437 case DW_FORM_GNU_addr_index:
9438 case DW_FORM_GNU_str_index:
9439 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9440 break;
9441 case DW_FORM_indirect:
9442 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9443 info_ptr += bytes_read;
9444 /* We need to continue parsing from here, so just go back to
9445 the top. */
9446 goto skip_attribute;
9447
9448 default:
9449 error (_("Dwarf Error: Cannot handle %s "
9450 "in DWARF reader [in module %s]"),
9451 dwarf_form_name (form),
9452 bfd_get_filename (abfd));
9453 }
9454 }
9455
9456 if (abbrev->has_children)
9457 return skip_children (reader, info_ptr);
9458 else
9459 return info_ptr;
9460 }
9461
9462 /* Locate ORIG_PDI's sibling.
9463 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9464
9465 static const gdb_byte *
9466 locate_pdi_sibling (const struct die_reader_specs *reader,
9467 struct partial_die_info *orig_pdi,
9468 const gdb_byte *info_ptr)
9469 {
9470 /* Do we know the sibling already? */
9471
9472 if (orig_pdi->sibling)
9473 return orig_pdi->sibling;
9474
9475 /* Are there any children to deal with? */
9476
9477 if (!orig_pdi->has_children)
9478 return info_ptr;
9479
9480 /* Skip the children the long way. */
9481
9482 return skip_children (reader, info_ptr);
9483 }
9484
9485 /* Expand this partial symbol table into a full symbol table. SELF is
9486 not NULL. */
9487
9488 static void
9489 dwarf2_read_symtab (struct partial_symtab *self,
9490 struct objfile *objfile)
9491 {
9492 struct dwarf2_per_objfile *dwarf2_per_objfile
9493 = get_dwarf2_per_objfile (objfile);
9494
9495 if (self->readin)
9496 {
9497 warning (_("bug: psymtab for %s is already read in."),
9498 self->filename);
9499 }
9500 else
9501 {
9502 if (info_verbose)
9503 {
9504 printf_filtered (_("Reading in symbols for %s..."),
9505 self->filename);
9506 gdb_flush (gdb_stdout);
9507 }
9508
9509 /* If this psymtab is constructed from a debug-only objfile, the
9510 has_section_at_zero flag will not necessarily be correct. We
9511 can get the correct value for this flag by looking at the data
9512 associated with the (presumably stripped) associated objfile. */
9513 if (objfile->separate_debug_objfile_backlink)
9514 {
9515 struct dwarf2_per_objfile *dpo_backlink
9516 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9517
9518 dwarf2_per_objfile->has_section_at_zero
9519 = dpo_backlink->has_section_at_zero;
9520 }
9521
9522 dwarf2_per_objfile->reading_partial_symbols = 0;
9523
9524 psymtab_to_symtab_1 (self);
9525
9526 /* Finish up the debug error message. */
9527 if (info_verbose)
9528 printf_filtered (_("done.\n"));
9529 }
9530
9531 process_cu_includes (dwarf2_per_objfile);
9532 }
9533 \f
9534 /* Reading in full CUs. */
9535
9536 /* Add PER_CU to the queue. */
9537
9538 static void
9539 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9540 enum language pretend_language)
9541 {
9542 struct dwarf2_queue_item *item;
9543
9544 per_cu->queued = 1;
9545 item = XNEW (struct dwarf2_queue_item);
9546 item->per_cu = per_cu;
9547 item->pretend_language = pretend_language;
9548 item->next = NULL;
9549
9550 if (dwarf2_queue == NULL)
9551 dwarf2_queue = item;
9552 else
9553 dwarf2_queue_tail->next = item;
9554
9555 dwarf2_queue_tail = item;
9556 }
9557
9558 /* If PER_CU is not yet queued, add it to the queue.
9559 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9560 dependency.
9561 The result is non-zero if PER_CU was queued, otherwise the result is zero
9562 meaning either PER_CU is already queued or it is already loaded.
9563
9564 N.B. There is an invariant here that if a CU is queued then it is loaded.
9565 The caller is required to load PER_CU if we return non-zero. */
9566
9567 static int
9568 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9569 struct dwarf2_per_cu_data *per_cu,
9570 enum language pretend_language)
9571 {
9572 /* We may arrive here during partial symbol reading, if we need full
9573 DIEs to process an unusual case (e.g. template arguments). Do
9574 not queue PER_CU, just tell our caller to load its DIEs. */
9575 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9576 {
9577 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9578 return 1;
9579 return 0;
9580 }
9581
9582 /* Mark the dependence relation so that we don't flush PER_CU
9583 too early. */
9584 if (dependent_cu != NULL)
9585 dwarf2_add_dependence (dependent_cu, per_cu);
9586
9587 /* If it's already on the queue, we have nothing to do. */
9588 if (per_cu->queued)
9589 return 0;
9590
9591 /* If the compilation unit is already loaded, just mark it as
9592 used. */
9593 if (per_cu->cu != NULL)
9594 {
9595 per_cu->cu->last_used = 0;
9596 return 0;
9597 }
9598
9599 /* Add it to the queue. */
9600 queue_comp_unit (per_cu, pretend_language);
9601
9602 return 1;
9603 }
9604
9605 /* Process the queue. */
9606
9607 static void
9608 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9609 {
9610 struct dwarf2_queue_item *item, *next_item;
9611
9612 if (dwarf_read_debug)
9613 {
9614 fprintf_unfiltered (gdb_stdlog,
9615 "Expanding one or more symtabs of objfile %s ...\n",
9616 objfile_name (dwarf2_per_objfile->objfile));
9617 }
9618
9619 /* The queue starts out with one item, but following a DIE reference
9620 may load a new CU, adding it to the end of the queue. */
9621 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9622 {
9623 if ((dwarf2_per_objfile->using_index
9624 ? !item->per_cu->v.quick->compunit_symtab
9625 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9626 /* Skip dummy CUs. */
9627 && item->per_cu->cu != NULL)
9628 {
9629 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9630 unsigned int debug_print_threshold;
9631 char buf[100];
9632
9633 if (per_cu->is_debug_types)
9634 {
9635 struct signatured_type *sig_type =
9636 (struct signatured_type *) per_cu;
9637
9638 sprintf (buf, "TU %s at offset %s",
9639 hex_string (sig_type->signature),
9640 sect_offset_str (per_cu->sect_off));
9641 /* There can be 100s of TUs.
9642 Only print them in verbose mode. */
9643 debug_print_threshold = 2;
9644 }
9645 else
9646 {
9647 sprintf (buf, "CU at offset %s",
9648 sect_offset_str (per_cu->sect_off));
9649 debug_print_threshold = 1;
9650 }
9651
9652 if (dwarf_read_debug >= debug_print_threshold)
9653 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9654
9655 if (per_cu->is_debug_types)
9656 process_full_type_unit (per_cu, item->pretend_language);
9657 else
9658 process_full_comp_unit (per_cu, item->pretend_language);
9659
9660 if (dwarf_read_debug >= debug_print_threshold)
9661 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9662 }
9663
9664 item->per_cu->queued = 0;
9665 next_item = item->next;
9666 xfree (item);
9667 }
9668
9669 dwarf2_queue_tail = NULL;
9670
9671 if (dwarf_read_debug)
9672 {
9673 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9674 objfile_name (dwarf2_per_objfile->objfile));
9675 }
9676 }
9677
9678 /* Read in full symbols for PST, and anything it depends on. */
9679
9680 static void
9681 psymtab_to_symtab_1 (struct partial_symtab *pst)
9682 {
9683 struct dwarf2_per_cu_data *per_cu;
9684 int i;
9685
9686 if (pst->readin)
9687 return;
9688
9689 for (i = 0; i < pst->number_of_dependencies; i++)
9690 if (!pst->dependencies[i]->readin
9691 && pst->dependencies[i]->user == NULL)
9692 {
9693 /* Inform about additional files that need to be read in. */
9694 if (info_verbose)
9695 {
9696 /* FIXME: i18n: Need to make this a single string. */
9697 fputs_filtered (" ", gdb_stdout);
9698 wrap_here ("");
9699 fputs_filtered ("and ", gdb_stdout);
9700 wrap_here ("");
9701 printf_filtered ("%s...", pst->dependencies[i]->filename);
9702 wrap_here (""); /* Flush output. */
9703 gdb_flush (gdb_stdout);
9704 }
9705 psymtab_to_symtab_1 (pst->dependencies[i]);
9706 }
9707
9708 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9709
9710 if (per_cu == NULL)
9711 {
9712 /* It's an include file, no symbols to read for it.
9713 Everything is in the parent symtab. */
9714 pst->readin = 1;
9715 return;
9716 }
9717
9718 dw2_do_instantiate_symtab (per_cu, false);
9719 }
9720
9721 /* Trivial hash function for die_info: the hash value of a DIE
9722 is its offset in .debug_info for this objfile. */
9723
9724 static hashval_t
9725 die_hash (const void *item)
9726 {
9727 const struct die_info *die = (const struct die_info *) item;
9728
9729 return to_underlying (die->sect_off);
9730 }
9731
9732 /* Trivial comparison function for die_info structures: two DIEs
9733 are equal if they have the same offset. */
9734
9735 static int
9736 die_eq (const void *item_lhs, const void *item_rhs)
9737 {
9738 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9739 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9740
9741 return die_lhs->sect_off == die_rhs->sect_off;
9742 }
9743
9744 /* die_reader_func for load_full_comp_unit.
9745 This is identical to read_signatured_type_reader,
9746 but is kept separate for now. */
9747
9748 static void
9749 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9750 const gdb_byte *info_ptr,
9751 struct die_info *comp_unit_die,
9752 int has_children,
9753 void *data)
9754 {
9755 struct dwarf2_cu *cu = reader->cu;
9756 enum language *language_ptr = (enum language *) data;
9757
9758 gdb_assert (cu->die_hash == NULL);
9759 cu->die_hash =
9760 htab_create_alloc_ex (cu->header.length / 12,
9761 die_hash,
9762 die_eq,
9763 NULL,
9764 &cu->comp_unit_obstack,
9765 hashtab_obstack_allocate,
9766 dummy_obstack_deallocate);
9767
9768 if (has_children)
9769 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9770 &info_ptr, comp_unit_die);
9771 cu->dies = comp_unit_die;
9772 /* comp_unit_die is not stored in die_hash, no need. */
9773
9774 /* We try not to read any attributes in this function, because not
9775 all CUs needed for references have been loaded yet, and symbol
9776 table processing isn't initialized. But we have to set the CU language,
9777 or we won't be able to build types correctly.
9778 Similarly, if we do not read the producer, we can not apply
9779 producer-specific interpretation. */
9780 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
9781 }
9782
9783 /* Load the DIEs associated with PER_CU into memory. */
9784
9785 static void
9786 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9787 bool skip_partial,
9788 enum language pretend_language)
9789 {
9790 gdb_assert (! this_cu->is_debug_types);
9791
9792 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
9793 load_full_comp_unit_reader, &pretend_language);
9794 }
9795
9796 /* Add a DIE to the delayed physname list. */
9797
9798 static void
9799 add_to_method_list (struct type *type, int fnfield_index, int index,
9800 const char *name, struct die_info *die,
9801 struct dwarf2_cu *cu)
9802 {
9803 struct delayed_method_info mi;
9804 mi.type = type;
9805 mi.fnfield_index = fnfield_index;
9806 mi.index = index;
9807 mi.name = name;
9808 mi.die = die;
9809 cu->method_list.push_back (mi);
9810 }
9811
9812 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9813 "const" / "volatile". If so, decrements LEN by the length of the
9814 modifier and return true. Otherwise return false. */
9815
9816 template<size_t N>
9817 static bool
9818 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9819 {
9820 size_t mod_len = sizeof (mod) - 1;
9821 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9822 {
9823 len -= mod_len;
9824 return true;
9825 }
9826 return false;
9827 }
9828
9829 /* Compute the physnames of any methods on the CU's method list.
9830
9831 The computation of method physnames is delayed in order to avoid the
9832 (bad) condition that one of the method's formal parameters is of an as yet
9833 incomplete type. */
9834
9835 static void
9836 compute_delayed_physnames (struct dwarf2_cu *cu)
9837 {
9838 /* Only C++ delays computing physnames. */
9839 if (cu->method_list.empty ())
9840 return;
9841 gdb_assert (cu->language == language_cplus);
9842
9843 for (const delayed_method_info &mi : cu->method_list)
9844 {
9845 const char *physname;
9846 struct fn_fieldlist *fn_flp
9847 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9848 physname = dwarf2_physname (mi.name, mi.die, cu);
9849 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9850 = physname ? physname : "";
9851
9852 /* Since there's no tag to indicate whether a method is a
9853 const/volatile overload, extract that information out of the
9854 demangled name. */
9855 if (physname != NULL)
9856 {
9857 size_t len = strlen (physname);
9858
9859 while (1)
9860 {
9861 if (physname[len] == ')') /* shortcut */
9862 break;
9863 else if (check_modifier (physname, len, " const"))
9864 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9865 else if (check_modifier (physname, len, " volatile"))
9866 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9867 else
9868 break;
9869 }
9870 }
9871 }
9872
9873 /* The list is no longer needed. */
9874 cu->method_list.clear ();
9875 }
9876
9877 /* Go objects should be embedded in a DW_TAG_module DIE,
9878 and it's not clear if/how imported objects will appear.
9879 To keep Go support simple until that's worked out,
9880 go back through what we've read and create something usable.
9881 We could do this while processing each DIE, and feels kinda cleaner,
9882 but that way is more invasive.
9883 This is to, for example, allow the user to type "p var" or "b main"
9884 without having to specify the package name, and allow lookups
9885 of module.object to work in contexts that use the expression
9886 parser. */
9887
9888 static void
9889 fixup_go_packaging (struct dwarf2_cu *cu)
9890 {
9891 gdb::unique_xmalloc_ptr<char> package_name;
9892 struct pending *list;
9893 int i;
9894
9895 for (list = *cu->get_builder ()->get_global_symbols ();
9896 list != NULL;
9897 list = list->next)
9898 {
9899 for (i = 0; i < list->nsyms; ++i)
9900 {
9901 struct symbol *sym = list->symbol[i];
9902
9903 if (sym->language () == language_go
9904 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9905 {
9906 gdb::unique_xmalloc_ptr<char> this_package_name
9907 (go_symbol_package_name (sym));
9908
9909 if (this_package_name == NULL)
9910 continue;
9911 if (package_name == NULL)
9912 package_name = std::move (this_package_name);
9913 else
9914 {
9915 struct objfile *objfile
9916 = cu->per_cu->dwarf2_per_objfile->objfile;
9917 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9918 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9919 (symbol_symtab (sym) != NULL
9920 ? symtab_to_filename_for_display
9921 (symbol_symtab (sym))
9922 : objfile_name (objfile)),
9923 this_package_name.get (), package_name.get ());
9924 }
9925 }
9926 }
9927 }
9928
9929 if (package_name != NULL)
9930 {
9931 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9932 const char *saved_package_name
9933 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
9934 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9935 saved_package_name);
9936 struct symbol *sym;
9937
9938 sym = allocate_symbol (objfile);
9939 sym->set_language (language_go, &objfile->objfile_obstack);
9940 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9941 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9942 e.g., "main" finds the "main" module and not C's main(). */
9943 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9944 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9945 SYMBOL_TYPE (sym) = type;
9946
9947 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9948 }
9949 }
9950
9951 /* Allocate a fully-qualified name consisting of the two parts on the
9952 obstack. */
9953
9954 static const char *
9955 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9956 {
9957 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9958 }
9959
9960 /* A helper that allocates a struct discriminant_info to attach to a
9961 union type. */
9962
9963 static struct discriminant_info *
9964 alloc_discriminant_info (struct type *type, int discriminant_index,
9965 int default_index)
9966 {
9967 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9968 gdb_assert (discriminant_index == -1
9969 || (discriminant_index >= 0
9970 && discriminant_index < TYPE_NFIELDS (type)));
9971 gdb_assert (default_index == -1
9972 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9973
9974 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9975
9976 struct discriminant_info *disc
9977 = ((struct discriminant_info *)
9978 TYPE_ZALLOC (type,
9979 offsetof (struct discriminant_info, discriminants)
9980 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9981 disc->default_index = default_index;
9982 disc->discriminant_index = discriminant_index;
9983
9984 struct dynamic_prop prop;
9985 prop.kind = PROP_UNDEFINED;
9986 prop.data.baton = disc;
9987
9988 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9989
9990 return disc;
9991 }
9992
9993 /* Some versions of rustc emitted enums in an unusual way.
9994
9995 Ordinary enums were emitted as unions. The first element of each
9996 structure in the union was named "RUST$ENUM$DISR". This element
9997 held the discriminant.
9998
9999 These versions of Rust also implemented the "non-zero"
10000 optimization. When the enum had two values, and one is empty and
10001 the other holds a pointer that cannot be zero, the pointer is used
10002 as the discriminant, with a zero value meaning the empty variant.
10003 Here, the union's first member is of the form
10004 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
10005 where the fieldnos are the indices of the fields that should be
10006 traversed in order to find the field (which may be several fields deep)
10007 and the variantname is the name of the variant of the case when the
10008 field is zero.
10009
10010 This function recognizes whether TYPE is of one of these forms,
10011 and, if so, smashes it to be a variant type. */
10012
10013 static void
10014 quirk_rust_enum (struct type *type, struct objfile *objfile)
10015 {
10016 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10017
10018 /* We don't need to deal with empty enums. */
10019 if (TYPE_NFIELDS (type) == 0)
10020 return;
10021
10022 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10023 if (TYPE_NFIELDS (type) == 1
10024 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10025 {
10026 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10027
10028 /* Decode the field name to find the offset of the
10029 discriminant. */
10030 ULONGEST bit_offset = 0;
10031 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10032 while (name[0] >= '0' && name[0] <= '9')
10033 {
10034 char *tail;
10035 unsigned long index = strtoul (name, &tail, 10);
10036 name = tail;
10037 if (*name != '$'
10038 || index >= TYPE_NFIELDS (field_type)
10039 || (TYPE_FIELD_LOC_KIND (field_type, index)
10040 != FIELD_LOC_KIND_BITPOS))
10041 {
10042 complaint (_("Could not parse Rust enum encoding string \"%s\""
10043 "[in module %s]"),
10044 TYPE_FIELD_NAME (type, 0),
10045 objfile_name (objfile));
10046 return;
10047 }
10048 ++name;
10049
10050 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10051 field_type = TYPE_FIELD_TYPE (field_type, index);
10052 }
10053
10054 /* Make a union to hold the variants. */
10055 struct type *union_type = alloc_type (objfile);
10056 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10057 TYPE_NFIELDS (union_type) = 3;
10058 TYPE_FIELDS (union_type)
10059 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10060 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10061 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10062
10063 /* Put the discriminant must at index 0. */
10064 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10065 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10066 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10067 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10068
10069 /* The order of fields doesn't really matter, so put the real
10070 field at index 1 and the data-less field at index 2. */
10071 struct discriminant_info *disc
10072 = alloc_discriminant_info (union_type, 0, 1);
10073 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10074 TYPE_FIELD_NAME (union_type, 1)
10075 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10076 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10077 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10078 TYPE_FIELD_NAME (union_type, 1));
10079
10080 const char *dataless_name
10081 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10082 name);
10083 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10084 dataless_name);
10085 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10086 /* NAME points into the original discriminant name, which
10087 already has the correct lifetime. */
10088 TYPE_FIELD_NAME (union_type, 2) = name;
10089 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10090 disc->discriminants[2] = 0;
10091
10092 /* Smash this type to be a structure type. We have to do this
10093 because the type has already been recorded. */
10094 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10095 TYPE_NFIELDS (type) = 1;
10096 TYPE_FIELDS (type)
10097 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10098
10099 /* Install the variant part. */
10100 TYPE_FIELD_TYPE (type, 0) = union_type;
10101 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10102 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10103 }
10104 /* A union with a single anonymous field is probably an old-style
10105 univariant enum. */
10106 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
10107 {
10108 /* Smash this type to be a structure type. We have to do this
10109 because the type has already been recorded. */
10110 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10111
10112 /* Make a union to hold the variants. */
10113 struct type *union_type = alloc_type (objfile);
10114 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10115 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10116 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10117 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10118 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10119
10120 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10121 const char *variant_name
10122 = rust_last_path_segment (TYPE_NAME (field_type));
10123 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10124 TYPE_NAME (field_type)
10125 = rust_fully_qualify (&objfile->objfile_obstack,
10126 TYPE_NAME (type), variant_name);
10127
10128 /* Install the union in the outer struct type. */
10129 TYPE_NFIELDS (type) = 1;
10130 TYPE_FIELDS (type)
10131 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10132 TYPE_FIELD_TYPE (type, 0) = union_type;
10133 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10134 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10135
10136 alloc_discriminant_info (union_type, -1, 0);
10137 }
10138 else
10139 {
10140 struct type *disr_type = nullptr;
10141 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10142 {
10143 disr_type = TYPE_FIELD_TYPE (type, i);
10144
10145 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10146 {
10147 /* All fields of a true enum will be structs. */
10148 return;
10149 }
10150 else if (TYPE_NFIELDS (disr_type) == 0)
10151 {
10152 /* Could be data-less variant, so keep going. */
10153 disr_type = nullptr;
10154 }
10155 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10156 "RUST$ENUM$DISR") != 0)
10157 {
10158 /* Not a Rust enum. */
10159 return;
10160 }
10161 else
10162 {
10163 /* Found one. */
10164 break;
10165 }
10166 }
10167
10168 /* If we got here without a discriminant, then it's probably
10169 just a union. */
10170 if (disr_type == nullptr)
10171 return;
10172
10173 /* Smash this type to be a structure type. We have to do this
10174 because the type has already been recorded. */
10175 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10176
10177 /* Make a union to hold the variants. */
10178 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10179 struct type *union_type = alloc_type (objfile);
10180 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10181 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10182 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10183 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10184 TYPE_FIELDS (union_type)
10185 = (struct field *) TYPE_ZALLOC (union_type,
10186 (TYPE_NFIELDS (union_type)
10187 * sizeof (struct field)));
10188
10189 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10190 TYPE_NFIELDS (type) * sizeof (struct field));
10191
10192 /* Install the discriminant at index 0 in the union. */
10193 TYPE_FIELD (union_type, 0) = *disr_field;
10194 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10195 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10196
10197 /* Install the union in the outer struct type. */
10198 TYPE_FIELD_TYPE (type, 0) = union_type;
10199 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10200 TYPE_NFIELDS (type) = 1;
10201
10202 /* Set the size and offset of the union type. */
10203 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10204
10205 /* We need a way to find the correct discriminant given a
10206 variant name. For convenience we build a map here. */
10207 struct type *enum_type = FIELD_TYPE (*disr_field);
10208 std::unordered_map<std::string, ULONGEST> discriminant_map;
10209 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10210 {
10211 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10212 {
10213 const char *name
10214 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10215 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10216 }
10217 }
10218
10219 int n_fields = TYPE_NFIELDS (union_type);
10220 struct discriminant_info *disc
10221 = alloc_discriminant_info (union_type, 0, -1);
10222 /* Skip the discriminant here. */
10223 for (int i = 1; i < n_fields; ++i)
10224 {
10225 /* Find the final word in the name of this variant's type.
10226 That name can be used to look up the correct
10227 discriminant. */
10228 const char *variant_name
10229 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10230 i)));
10231
10232 auto iter = discriminant_map.find (variant_name);
10233 if (iter != discriminant_map.end ())
10234 disc->discriminants[i] = iter->second;
10235
10236 /* Remove the discriminant field, if it exists. */
10237 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10238 if (TYPE_NFIELDS (sub_type) > 0)
10239 {
10240 --TYPE_NFIELDS (sub_type);
10241 ++TYPE_FIELDS (sub_type);
10242 }
10243 TYPE_FIELD_NAME (union_type, i) = variant_name;
10244 TYPE_NAME (sub_type)
10245 = rust_fully_qualify (&objfile->objfile_obstack,
10246 TYPE_NAME (type), variant_name);
10247 }
10248 }
10249 }
10250
10251 /* Rewrite some Rust unions to be structures with variants parts. */
10252
10253 static void
10254 rust_union_quirks (struct dwarf2_cu *cu)
10255 {
10256 gdb_assert (cu->language == language_rust);
10257 for (type *type_ : cu->rust_unions)
10258 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
10259 /* We don't need this any more. */
10260 cu->rust_unions.clear ();
10261 }
10262
10263 /* Return the symtab for PER_CU. This works properly regardless of
10264 whether we're using the index or psymtabs. */
10265
10266 static struct compunit_symtab *
10267 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10268 {
10269 return (per_cu->dwarf2_per_objfile->using_index
10270 ? per_cu->v.quick->compunit_symtab
10271 : per_cu->v.psymtab->compunit_symtab);
10272 }
10273
10274 /* A helper function for computing the list of all symbol tables
10275 included by PER_CU. */
10276
10277 static void
10278 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
10279 htab_t all_children, htab_t all_type_symtabs,
10280 struct dwarf2_per_cu_data *per_cu,
10281 struct compunit_symtab *immediate_parent)
10282 {
10283 void **slot;
10284 struct compunit_symtab *cust;
10285
10286 slot = htab_find_slot (all_children, per_cu, INSERT);
10287 if (*slot != NULL)
10288 {
10289 /* This inclusion and its children have been processed. */
10290 return;
10291 }
10292
10293 *slot = per_cu;
10294 /* Only add a CU if it has a symbol table. */
10295 cust = get_compunit_symtab (per_cu);
10296 if (cust != NULL)
10297 {
10298 /* If this is a type unit only add its symbol table if we haven't
10299 seen it yet (type unit per_cu's can share symtabs). */
10300 if (per_cu->is_debug_types)
10301 {
10302 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10303 if (*slot == NULL)
10304 {
10305 *slot = cust;
10306 result->push_back (cust);
10307 if (cust->user == NULL)
10308 cust->user = immediate_parent;
10309 }
10310 }
10311 else
10312 {
10313 result->push_back (cust);
10314 if (cust->user == NULL)
10315 cust->user = immediate_parent;
10316 }
10317 }
10318
10319 if (!per_cu->imported_symtabs_empty ())
10320 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10321 {
10322 recursively_compute_inclusions (result, all_children,
10323 all_type_symtabs, ptr, cust);
10324 }
10325 }
10326
10327 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10328 PER_CU. */
10329
10330 static void
10331 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10332 {
10333 gdb_assert (! per_cu->is_debug_types);
10334
10335 if (!per_cu->imported_symtabs_empty ())
10336 {
10337 int len;
10338 std::vector<compunit_symtab *> result_symtabs;
10339 htab_t all_children, all_type_symtabs;
10340 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10341
10342 /* If we don't have a symtab, we can just skip this case. */
10343 if (cust == NULL)
10344 return;
10345
10346 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10347 NULL, xcalloc, xfree);
10348 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10349 NULL, xcalloc, xfree);
10350
10351 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10352 {
10353 recursively_compute_inclusions (&result_symtabs, all_children,
10354 all_type_symtabs, ptr, cust);
10355 }
10356
10357 /* Now we have a transitive closure of all the included symtabs. */
10358 len = result_symtabs.size ();
10359 cust->includes
10360 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10361 struct compunit_symtab *, len + 1);
10362 memcpy (cust->includes, result_symtabs.data (),
10363 len * sizeof (compunit_symtab *));
10364 cust->includes[len] = NULL;
10365
10366 htab_delete (all_children);
10367 htab_delete (all_type_symtabs);
10368 }
10369 }
10370
10371 /* Compute the 'includes' field for the symtabs of all the CUs we just
10372 read. */
10373
10374 static void
10375 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10376 {
10377 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
10378 {
10379 if (! iter->is_debug_types)
10380 compute_compunit_symtab_includes (iter);
10381 }
10382
10383 dwarf2_per_objfile->just_read_cus.clear ();
10384 }
10385
10386 /* Generate full symbol information for PER_CU, whose DIEs have
10387 already been loaded into memory. */
10388
10389 static void
10390 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10391 enum language pretend_language)
10392 {
10393 struct dwarf2_cu *cu = per_cu->cu;
10394 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10395 struct objfile *objfile = dwarf2_per_objfile->objfile;
10396 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10397 CORE_ADDR lowpc, highpc;
10398 struct compunit_symtab *cust;
10399 CORE_ADDR baseaddr;
10400 struct block *static_block;
10401 CORE_ADDR addr;
10402
10403 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10404
10405 /* Clear the list here in case something was left over. */
10406 cu->method_list.clear ();
10407
10408 cu->language = pretend_language;
10409 cu->language_defn = language_def (cu->language);
10410
10411 /* Do line number decoding in read_file_scope () */
10412 process_die (cu->dies, cu);
10413
10414 /* For now fudge the Go package. */
10415 if (cu->language == language_go)
10416 fixup_go_packaging (cu);
10417
10418 /* Now that we have processed all the DIEs in the CU, all the types
10419 should be complete, and it should now be safe to compute all of the
10420 physnames. */
10421 compute_delayed_physnames (cu);
10422
10423 if (cu->language == language_rust)
10424 rust_union_quirks (cu);
10425
10426 /* Some compilers don't define a DW_AT_high_pc attribute for the
10427 compilation unit. If the DW_AT_high_pc is missing, synthesize
10428 it, by scanning the DIE's below the compilation unit. */
10429 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10430
10431 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10432 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10433
10434 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10435 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10436 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10437 addrmap to help ensure it has an accurate map of pc values belonging to
10438 this comp unit. */
10439 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10440
10441 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10442 SECT_OFF_TEXT (objfile),
10443 0);
10444
10445 if (cust != NULL)
10446 {
10447 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10448
10449 /* Set symtab language to language from DW_AT_language. If the
10450 compilation is from a C file generated by language preprocessors, do
10451 not set the language if it was already deduced by start_subfile. */
10452 if (!(cu->language == language_c
10453 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10454 COMPUNIT_FILETABS (cust)->language = cu->language;
10455
10456 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10457 produce DW_AT_location with location lists but it can be possibly
10458 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10459 there were bugs in prologue debug info, fixed later in GCC-4.5
10460 by "unwind info for epilogues" patch (which is not directly related).
10461
10462 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10463 needed, it would be wrong due to missing DW_AT_producer there.
10464
10465 Still one can confuse GDB by using non-standard GCC compilation
10466 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10467 */
10468 if (cu->has_loclist && gcc_4_minor >= 5)
10469 cust->locations_valid = 1;
10470
10471 if (gcc_4_minor >= 5)
10472 cust->epilogue_unwind_valid = 1;
10473
10474 cust->call_site_htab = cu->call_site_htab;
10475 }
10476
10477 if (dwarf2_per_objfile->using_index)
10478 per_cu->v.quick->compunit_symtab = cust;
10479 else
10480 {
10481 struct partial_symtab *pst = per_cu->v.psymtab;
10482 pst->compunit_symtab = cust;
10483 pst->readin = 1;
10484 }
10485
10486 /* Push it for inclusion processing later. */
10487 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10488
10489 /* Not needed any more. */
10490 cu->reset_builder ();
10491 }
10492
10493 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10494 already been loaded into memory. */
10495
10496 static void
10497 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10498 enum language pretend_language)
10499 {
10500 struct dwarf2_cu *cu = per_cu->cu;
10501 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10502 struct objfile *objfile = dwarf2_per_objfile->objfile;
10503 struct compunit_symtab *cust;
10504 struct signatured_type *sig_type;
10505
10506 gdb_assert (per_cu->is_debug_types);
10507 sig_type = (struct signatured_type *) per_cu;
10508
10509 /* Clear the list here in case something was left over. */
10510 cu->method_list.clear ();
10511
10512 cu->language = pretend_language;
10513 cu->language_defn = language_def (cu->language);
10514
10515 /* The symbol tables are set up in read_type_unit_scope. */
10516 process_die (cu->dies, cu);
10517
10518 /* For now fudge the Go package. */
10519 if (cu->language == language_go)
10520 fixup_go_packaging (cu);
10521
10522 /* Now that we have processed all the DIEs in the CU, all the types
10523 should be complete, and it should now be safe to compute all of the
10524 physnames. */
10525 compute_delayed_physnames (cu);
10526
10527 if (cu->language == language_rust)
10528 rust_union_quirks (cu);
10529
10530 /* TUs share symbol tables.
10531 If this is the first TU to use this symtab, complete the construction
10532 of it with end_expandable_symtab. Otherwise, complete the addition of
10533 this TU's symbols to the existing symtab. */
10534 if (sig_type->type_unit_group->compunit_symtab == NULL)
10535 {
10536 buildsym_compunit *builder = cu->get_builder ();
10537 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10538 sig_type->type_unit_group->compunit_symtab = cust;
10539
10540 if (cust != NULL)
10541 {
10542 /* Set symtab language to language from DW_AT_language. If the
10543 compilation is from a C file generated by language preprocessors,
10544 do not set the language if it was already deduced by
10545 start_subfile. */
10546 if (!(cu->language == language_c
10547 && COMPUNIT_FILETABS (cust)->language != language_c))
10548 COMPUNIT_FILETABS (cust)->language = cu->language;
10549 }
10550 }
10551 else
10552 {
10553 cu->get_builder ()->augment_type_symtab ();
10554 cust = sig_type->type_unit_group->compunit_symtab;
10555 }
10556
10557 if (dwarf2_per_objfile->using_index)
10558 per_cu->v.quick->compunit_symtab = cust;
10559 else
10560 {
10561 struct partial_symtab *pst = per_cu->v.psymtab;
10562 pst->compunit_symtab = cust;
10563 pst->readin = 1;
10564 }
10565
10566 /* Not needed any more. */
10567 cu->reset_builder ();
10568 }
10569
10570 /* Process an imported unit DIE. */
10571
10572 static void
10573 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10574 {
10575 struct attribute *attr;
10576
10577 /* For now we don't handle imported units in type units. */
10578 if (cu->per_cu->is_debug_types)
10579 {
10580 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10581 " supported in type units [in module %s]"),
10582 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10583 }
10584
10585 attr = dwarf2_attr (die, DW_AT_import, cu);
10586 if (attr != NULL)
10587 {
10588 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10589 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10590 dwarf2_per_cu_data *per_cu
10591 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10592 cu->per_cu->dwarf2_per_objfile);
10593
10594 /* If necessary, add it to the queue and load its DIEs. */
10595 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10596 load_full_comp_unit (per_cu, false, cu->language);
10597
10598 cu->per_cu->imported_symtabs_push (per_cu);
10599 }
10600 }
10601
10602 /* RAII object that represents a process_die scope: i.e.,
10603 starts/finishes processing a DIE. */
10604 class process_die_scope
10605 {
10606 public:
10607 process_die_scope (die_info *die, dwarf2_cu *cu)
10608 : m_die (die), m_cu (cu)
10609 {
10610 /* We should only be processing DIEs not already in process. */
10611 gdb_assert (!m_die->in_process);
10612 m_die->in_process = true;
10613 }
10614
10615 ~process_die_scope ()
10616 {
10617 m_die->in_process = false;
10618
10619 /* If we're done processing the DIE for the CU that owns the line
10620 header, we don't need the line header anymore. */
10621 if (m_cu->line_header_die_owner == m_die)
10622 {
10623 delete m_cu->line_header;
10624 m_cu->line_header = NULL;
10625 m_cu->line_header_die_owner = NULL;
10626 }
10627 }
10628
10629 private:
10630 die_info *m_die;
10631 dwarf2_cu *m_cu;
10632 };
10633
10634 /* Process a die and its children. */
10635
10636 static void
10637 process_die (struct die_info *die, struct dwarf2_cu *cu)
10638 {
10639 process_die_scope scope (die, cu);
10640
10641 switch (die->tag)
10642 {
10643 case DW_TAG_padding:
10644 break;
10645 case DW_TAG_compile_unit:
10646 case DW_TAG_partial_unit:
10647 read_file_scope (die, cu);
10648 break;
10649 case DW_TAG_type_unit:
10650 read_type_unit_scope (die, cu);
10651 break;
10652 case DW_TAG_subprogram:
10653 /* Nested subprograms in Fortran get a prefix. */
10654 if (cu->language == language_fortran
10655 && die->parent != NULL
10656 && die->parent->tag == DW_TAG_subprogram)
10657 cu->processing_has_namespace_info = true;
10658 /* Fall through. */
10659 case DW_TAG_inlined_subroutine:
10660 read_func_scope (die, cu);
10661 break;
10662 case DW_TAG_lexical_block:
10663 case DW_TAG_try_block:
10664 case DW_TAG_catch_block:
10665 read_lexical_block_scope (die, cu);
10666 break;
10667 case DW_TAG_call_site:
10668 case DW_TAG_GNU_call_site:
10669 read_call_site_scope (die, cu);
10670 break;
10671 case DW_TAG_class_type:
10672 case DW_TAG_interface_type:
10673 case DW_TAG_structure_type:
10674 case DW_TAG_union_type:
10675 process_structure_scope (die, cu);
10676 break;
10677 case DW_TAG_enumeration_type:
10678 process_enumeration_scope (die, cu);
10679 break;
10680
10681 /* These dies have a type, but processing them does not create
10682 a symbol or recurse to process the children. Therefore we can
10683 read them on-demand through read_type_die. */
10684 case DW_TAG_subroutine_type:
10685 case DW_TAG_set_type:
10686 case DW_TAG_array_type:
10687 case DW_TAG_pointer_type:
10688 case DW_TAG_ptr_to_member_type:
10689 case DW_TAG_reference_type:
10690 case DW_TAG_rvalue_reference_type:
10691 case DW_TAG_string_type:
10692 break;
10693
10694 case DW_TAG_base_type:
10695 case DW_TAG_subrange_type:
10696 case DW_TAG_typedef:
10697 /* Add a typedef symbol for the type definition, if it has a
10698 DW_AT_name. */
10699 new_symbol (die, read_type_die (die, cu), cu);
10700 break;
10701 case DW_TAG_common_block:
10702 read_common_block (die, cu);
10703 break;
10704 case DW_TAG_common_inclusion:
10705 break;
10706 case DW_TAG_namespace:
10707 cu->processing_has_namespace_info = true;
10708 read_namespace (die, cu);
10709 break;
10710 case DW_TAG_module:
10711 cu->processing_has_namespace_info = true;
10712 read_module (die, cu);
10713 break;
10714 case DW_TAG_imported_declaration:
10715 cu->processing_has_namespace_info = true;
10716 if (read_namespace_alias (die, cu))
10717 break;
10718 /* The declaration is not a global namespace alias. */
10719 /* Fall through. */
10720 case DW_TAG_imported_module:
10721 cu->processing_has_namespace_info = true;
10722 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10723 || cu->language != language_fortran))
10724 complaint (_("Tag '%s' has unexpected children"),
10725 dwarf_tag_name (die->tag));
10726 read_import_statement (die, cu);
10727 break;
10728
10729 case DW_TAG_imported_unit:
10730 process_imported_unit_die (die, cu);
10731 break;
10732
10733 case DW_TAG_variable:
10734 read_variable (die, cu);
10735 break;
10736
10737 default:
10738 new_symbol (die, NULL, cu);
10739 break;
10740 }
10741 }
10742 \f
10743 /* DWARF name computation. */
10744
10745 /* A helper function for dwarf2_compute_name which determines whether DIE
10746 needs to have the name of the scope prepended to the name listed in the
10747 die. */
10748
10749 static int
10750 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10751 {
10752 struct attribute *attr;
10753
10754 switch (die->tag)
10755 {
10756 case DW_TAG_namespace:
10757 case DW_TAG_typedef:
10758 case DW_TAG_class_type:
10759 case DW_TAG_interface_type:
10760 case DW_TAG_structure_type:
10761 case DW_TAG_union_type:
10762 case DW_TAG_enumeration_type:
10763 case DW_TAG_enumerator:
10764 case DW_TAG_subprogram:
10765 case DW_TAG_inlined_subroutine:
10766 case DW_TAG_member:
10767 case DW_TAG_imported_declaration:
10768 return 1;
10769
10770 case DW_TAG_variable:
10771 case DW_TAG_constant:
10772 /* We only need to prefix "globally" visible variables. These include
10773 any variable marked with DW_AT_external or any variable that
10774 lives in a namespace. [Variables in anonymous namespaces
10775 require prefixing, but they are not DW_AT_external.] */
10776
10777 if (dwarf2_attr (die, DW_AT_specification, cu))
10778 {
10779 struct dwarf2_cu *spec_cu = cu;
10780
10781 return die_needs_namespace (die_specification (die, &spec_cu),
10782 spec_cu);
10783 }
10784
10785 attr = dwarf2_attr (die, DW_AT_external, cu);
10786 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10787 && die->parent->tag != DW_TAG_module)
10788 return 0;
10789 /* A variable in a lexical block of some kind does not need a
10790 namespace, even though in C++ such variables may be external
10791 and have a mangled name. */
10792 if (die->parent->tag == DW_TAG_lexical_block
10793 || die->parent->tag == DW_TAG_try_block
10794 || die->parent->tag == DW_TAG_catch_block
10795 || die->parent->tag == DW_TAG_subprogram)
10796 return 0;
10797 return 1;
10798
10799 default:
10800 return 0;
10801 }
10802 }
10803
10804 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10805 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10806 defined for the given DIE. */
10807
10808 static struct attribute *
10809 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10810 {
10811 struct attribute *attr;
10812
10813 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10814 if (attr == NULL)
10815 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10816
10817 return attr;
10818 }
10819
10820 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10821 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10822 defined for the given DIE. */
10823
10824 static const char *
10825 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10826 {
10827 const char *linkage_name;
10828
10829 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10830 if (linkage_name == NULL)
10831 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10832
10833 return linkage_name;
10834 }
10835
10836 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10837 compute the physname for the object, which include a method's:
10838 - formal parameters (C++),
10839 - receiver type (Go),
10840
10841 The term "physname" is a bit confusing.
10842 For C++, for example, it is the demangled name.
10843 For Go, for example, it's the mangled name.
10844
10845 For Ada, return the DIE's linkage name rather than the fully qualified
10846 name. PHYSNAME is ignored..
10847
10848 The result is allocated on the objfile_obstack and canonicalized. */
10849
10850 static const char *
10851 dwarf2_compute_name (const char *name,
10852 struct die_info *die, struct dwarf2_cu *cu,
10853 int physname)
10854 {
10855 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10856
10857 if (name == NULL)
10858 name = dwarf2_name (die, cu);
10859
10860 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10861 but otherwise compute it by typename_concat inside GDB.
10862 FIXME: Actually this is not really true, or at least not always true.
10863 It's all very confusing. compute_and_set_names doesn't try to demangle
10864 Fortran names because there is no mangling standard. So new_symbol
10865 will set the demangled name to the result of dwarf2_full_name, and it is
10866 the demangled name that GDB uses if it exists. */
10867 if (cu->language == language_ada
10868 || (cu->language == language_fortran && physname))
10869 {
10870 /* For Ada unit, we prefer the linkage name over the name, as
10871 the former contains the exported name, which the user expects
10872 to be able to reference. Ideally, we want the user to be able
10873 to reference this entity using either natural or linkage name,
10874 but we haven't started looking at this enhancement yet. */
10875 const char *linkage_name = dw2_linkage_name (die, cu);
10876
10877 if (linkage_name != NULL)
10878 return linkage_name;
10879 }
10880
10881 /* These are the only languages we know how to qualify names in. */
10882 if (name != NULL
10883 && (cu->language == language_cplus
10884 || cu->language == language_fortran || cu->language == language_d
10885 || cu->language == language_rust))
10886 {
10887 if (die_needs_namespace (die, cu))
10888 {
10889 const char *prefix;
10890 const char *canonical_name = NULL;
10891
10892 string_file buf;
10893
10894 prefix = determine_prefix (die, cu);
10895 if (*prefix != '\0')
10896 {
10897 gdb::unique_xmalloc_ptr<char> prefixed_name
10898 (typename_concat (NULL, prefix, name, physname, cu));
10899
10900 buf.puts (prefixed_name.get ());
10901 }
10902 else
10903 buf.puts (name);
10904
10905 /* Template parameters may be specified in the DIE's DW_AT_name, or
10906 as children with DW_TAG_template_type_param or
10907 DW_TAG_value_type_param. If the latter, add them to the name
10908 here. If the name already has template parameters, then
10909 skip this step; some versions of GCC emit both, and
10910 it is more efficient to use the pre-computed name.
10911
10912 Something to keep in mind about this process: it is very
10913 unlikely, or in some cases downright impossible, to produce
10914 something that will match the mangled name of a function.
10915 If the definition of the function has the same debug info,
10916 we should be able to match up with it anyway. But fallbacks
10917 using the minimal symbol, for instance to find a method
10918 implemented in a stripped copy of libstdc++, will not work.
10919 If we do not have debug info for the definition, we will have to
10920 match them up some other way.
10921
10922 When we do name matching there is a related problem with function
10923 templates; two instantiated function templates are allowed to
10924 differ only by their return types, which we do not add here. */
10925
10926 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10927 {
10928 struct attribute *attr;
10929 struct die_info *child;
10930 int first = 1;
10931
10932 die->building_fullname = 1;
10933
10934 for (child = die->child; child != NULL; child = child->sibling)
10935 {
10936 struct type *type;
10937 LONGEST value;
10938 const gdb_byte *bytes;
10939 struct dwarf2_locexpr_baton *baton;
10940 struct value *v;
10941
10942 if (child->tag != DW_TAG_template_type_param
10943 && child->tag != DW_TAG_template_value_param)
10944 continue;
10945
10946 if (first)
10947 {
10948 buf.puts ("<");
10949 first = 0;
10950 }
10951 else
10952 buf.puts (", ");
10953
10954 attr = dwarf2_attr (child, DW_AT_type, cu);
10955 if (attr == NULL)
10956 {
10957 complaint (_("template parameter missing DW_AT_type"));
10958 buf.puts ("UNKNOWN_TYPE");
10959 continue;
10960 }
10961 type = die_type (child, cu);
10962
10963 if (child->tag == DW_TAG_template_type_param)
10964 {
10965 c_print_type (type, "", &buf, -1, 0, cu->language,
10966 &type_print_raw_options);
10967 continue;
10968 }
10969
10970 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10971 if (attr == NULL)
10972 {
10973 complaint (_("template parameter missing "
10974 "DW_AT_const_value"));
10975 buf.puts ("UNKNOWN_VALUE");
10976 continue;
10977 }
10978
10979 dwarf2_const_value_attr (attr, type, name,
10980 &cu->comp_unit_obstack, cu,
10981 &value, &bytes, &baton);
10982
10983 if (TYPE_NOSIGN (type))
10984 /* GDB prints characters as NUMBER 'CHAR'. If that's
10985 changed, this can use value_print instead. */
10986 c_printchar (value, type, &buf);
10987 else
10988 {
10989 struct value_print_options opts;
10990
10991 if (baton != NULL)
10992 v = dwarf2_evaluate_loc_desc (type, NULL,
10993 baton->data,
10994 baton->size,
10995 baton->per_cu);
10996 else if (bytes != NULL)
10997 {
10998 v = allocate_value (type);
10999 memcpy (value_contents_writeable (v), bytes,
11000 TYPE_LENGTH (type));
11001 }
11002 else
11003 v = value_from_longest (type, value);
11004
11005 /* Specify decimal so that we do not depend on
11006 the radix. */
11007 get_formatted_print_options (&opts, 'd');
11008 opts.raw = 1;
11009 value_print (v, &buf, &opts);
11010 release_value (v);
11011 }
11012 }
11013
11014 die->building_fullname = 0;
11015
11016 if (!first)
11017 {
11018 /* Close the argument list, with a space if necessary
11019 (nested templates). */
11020 if (!buf.empty () && buf.string ().back () == '>')
11021 buf.puts (" >");
11022 else
11023 buf.puts (">");
11024 }
11025 }
11026
11027 /* For C++ methods, append formal parameter type
11028 information, if PHYSNAME. */
11029
11030 if (physname && die->tag == DW_TAG_subprogram
11031 && cu->language == language_cplus)
11032 {
11033 struct type *type = read_type_die (die, cu);
11034
11035 c_type_print_args (type, &buf, 1, cu->language,
11036 &type_print_raw_options);
11037
11038 if (cu->language == language_cplus)
11039 {
11040 /* Assume that an artificial first parameter is
11041 "this", but do not crash if it is not. RealView
11042 marks unnamed (and thus unused) parameters as
11043 artificial; there is no way to differentiate
11044 the two cases. */
11045 if (TYPE_NFIELDS (type) > 0
11046 && TYPE_FIELD_ARTIFICIAL (type, 0)
11047 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11048 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11049 0))))
11050 buf.puts (" const");
11051 }
11052 }
11053
11054 const std::string &intermediate_name = buf.string ();
11055
11056 if (cu->language == language_cplus)
11057 canonical_name
11058 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11059 &objfile->per_bfd->storage_obstack);
11060
11061 /* If we only computed INTERMEDIATE_NAME, or if
11062 INTERMEDIATE_NAME is already canonical, then we need to
11063 copy it to the appropriate obstack. */
11064 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11065 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11066 intermediate_name);
11067 else
11068 name = canonical_name;
11069 }
11070 }
11071
11072 return name;
11073 }
11074
11075 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11076 If scope qualifiers are appropriate they will be added. The result
11077 will be allocated on the storage_obstack, or NULL if the DIE does
11078 not have a name. NAME may either be from a previous call to
11079 dwarf2_name or NULL.
11080
11081 The output string will be canonicalized (if C++). */
11082
11083 static const char *
11084 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11085 {
11086 return dwarf2_compute_name (name, die, cu, 0);
11087 }
11088
11089 /* Construct a physname for the given DIE in CU. NAME may either be
11090 from a previous call to dwarf2_name or NULL. The result will be
11091 allocated on the objfile_objstack or NULL if the DIE does not have a
11092 name.
11093
11094 The output string will be canonicalized (if C++). */
11095
11096 static const char *
11097 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11098 {
11099 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11100 const char *retval, *mangled = NULL, *canon = NULL;
11101 int need_copy = 1;
11102
11103 /* In this case dwarf2_compute_name is just a shortcut not building anything
11104 on its own. */
11105 if (!die_needs_namespace (die, cu))
11106 return dwarf2_compute_name (name, die, cu, 1);
11107
11108 mangled = dw2_linkage_name (die, cu);
11109
11110 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11111 See https://github.com/rust-lang/rust/issues/32925. */
11112 if (cu->language == language_rust && mangled != NULL
11113 && strchr (mangled, '{') != NULL)
11114 mangled = NULL;
11115
11116 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11117 has computed. */
11118 gdb::unique_xmalloc_ptr<char> demangled;
11119 if (mangled != NULL)
11120 {
11121
11122 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11123 {
11124 /* Do nothing (do not demangle the symbol name). */
11125 }
11126 else if (cu->language == language_go)
11127 {
11128 /* This is a lie, but we already lie to the caller new_symbol.
11129 new_symbol assumes we return the mangled name.
11130 This just undoes that lie until things are cleaned up. */
11131 }
11132 else
11133 {
11134 /* Use DMGL_RET_DROP for C++ template functions to suppress
11135 their return type. It is easier for GDB users to search
11136 for such functions as `name(params)' than `long name(params)'.
11137 In such case the minimal symbol names do not match the full
11138 symbol names but for template functions there is never a need
11139 to look up their definition from their declaration so
11140 the only disadvantage remains the minimal symbol variant
11141 `long name(params)' does not have the proper inferior type. */
11142 demangled.reset (gdb_demangle (mangled,
11143 (DMGL_PARAMS | DMGL_ANSI
11144 | DMGL_RET_DROP)));
11145 }
11146 if (demangled)
11147 canon = demangled.get ();
11148 else
11149 {
11150 canon = mangled;
11151 need_copy = 0;
11152 }
11153 }
11154
11155 if (canon == NULL || check_physname)
11156 {
11157 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11158
11159 if (canon != NULL && strcmp (physname, canon) != 0)
11160 {
11161 /* It may not mean a bug in GDB. The compiler could also
11162 compute DW_AT_linkage_name incorrectly. But in such case
11163 GDB would need to be bug-to-bug compatible. */
11164
11165 complaint (_("Computed physname <%s> does not match demangled <%s> "
11166 "(from linkage <%s>) - DIE at %s [in module %s]"),
11167 physname, canon, mangled, sect_offset_str (die->sect_off),
11168 objfile_name (objfile));
11169
11170 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11171 is available here - over computed PHYSNAME. It is safer
11172 against both buggy GDB and buggy compilers. */
11173
11174 retval = canon;
11175 }
11176 else
11177 {
11178 retval = physname;
11179 need_copy = 0;
11180 }
11181 }
11182 else
11183 retval = canon;
11184
11185 if (need_copy)
11186 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
11187
11188 return retval;
11189 }
11190
11191 /* Inspect DIE in CU for a namespace alias. If one exists, record
11192 a new symbol for it.
11193
11194 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11195
11196 static int
11197 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11198 {
11199 struct attribute *attr;
11200
11201 /* If the die does not have a name, this is not a namespace
11202 alias. */
11203 attr = dwarf2_attr (die, DW_AT_name, cu);
11204 if (attr != NULL)
11205 {
11206 int num;
11207 struct die_info *d = die;
11208 struct dwarf2_cu *imported_cu = cu;
11209
11210 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11211 keep inspecting DIEs until we hit the underlying import. */
11212 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11213 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11214 {
11215 attr = dwarf2_attr (d, DW_AT_import, cu);
11216 if (attr == NULL)
11217 break;
11218
11219 d = follow_die_ref (d, attr, &imported_cu);
11220 if (d->tag != DW_TAG_imported_declaration)
11221 break;
11222 }
11223
11224 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11225 {
11226 complaint (_("DIE at %s has too many recursively imported "
11227 "declarations"), sect_offset_str (d->sect_off));
11228 return 0;
11229 }
11230
11231 if (attr != NULL)
11232 {
11233 struct type *type;
11234 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11235
11236 type = get_die_type_at_offset (sect_off, cu->per_cu);
11237 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11238 {
11239 /* This declaration is a global namespace alias. Add
11240 a symbol for it whose type is the aliased namespace. */
11241 new_symbol (die, type, cu);
11242 return 1;
11243 }
11244 }
11245 }
11246
11247 return 0;
11248 }
11249
11250 /* Return the using directives repository (global or local?) to use in the
11251 current context for CU.
11252
11253 For Ada, imported declarations can materialize renamings, which *may* be
11254 global. However it is impossible (for now?) in DWARF to distinguish
11255 "external" imported declarations and "static" ones. As all imported
11256 declarations seem to be static in all other languages, make them all CU-wide
11257 global only in Ada. */
11258
11259 static struct using_direct **
11260 using_directives (struct dwarf2_cu *cu)
11261 {
11262 if (cu->language == language_ada
11263 && cu->get_builder ()->outermost_context_p ())
11264 return cu->get_builder ()->get_global_using_directives ();
11265 else
11266 return cu->get_builder ()->get_local_using_directives ();
11267 }
11268
11269 /* Read the import statement specified by the given die and record it. */
11270
11271 static void
11272 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11273 {
11274 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11275 struct attribute *import_attr;
11276 struct die_info *imported_die, *child_die;
11277 struct dwarf2_cu *imported_cu;
11278 const char *imported_name;
11279 const char *imported_name_prefix;
11280 const char *canonical_name;
11281 const char *import_alias;
11282 const char *imported_declaration = NULL;
11283 const char *import_prefix;
11284 std::vector<const char *> excludes;
11285
11286 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11287 if (import_attr == NULL)
11288 {
11289 complaint (_("Tag '%s' has no DW_AT_import"),
11290 dwarf_tag_name (die->tag));
11291 return;
11292 }
11293
11294 imported_cu = cu;
11295 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11296 imported_name = dwarf2_name (imported_die, imported_cu);
11297 if (imported_name == NULL)
11298 {
11299 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11300
11301 The import in the following code:
11302 namespace A
11303 {
11304 typedef int B;
11305 }
11306
11307 int main ()
11308 {
11309 using A::B;
11310 B b;
11311 return b;
11312 }
11313
11314 ...
11315 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11316 <52> DW_AT_decl_file : 1
11317 <53> DW_AT_decl_line : 6
11318 <54> DW_AT_import : <0x75>
11319 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11320 <59> DW_AT_name : B
11321 <5b> DW_AT_decl_file : 1
11322 <5c> DW_AT_decl_line : 2
11323 <5d> DW_AT_type : <0x6e>
11324 ...
11325 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11326 <76> DW_AT_byte_size : 4
11327 <77> DW_AT_encoding : 5 (signed)
11328
11329 imports the wrong die ( 0x75 instead of 0x58 ).
11330 This case will be ignored until the gcc bug is fixed. */
11331 return;
11332 }
11333
11334 /* Figure out the local name after import. */
11335 import_alias = dwarf2_name (die, cu);
11336
11337 /* Figure out where the statement is being imported to. */
11338 import_prefix = determine_prefix (die, cu);
11339
11340 /* Figure out what the scope of the imported die is and prepend it
11341 to the name of the imported die. */
11342 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11343
11344 if (imported_die->tag != DW_TAG_namespace
11345 && imported_die->tag != DW_TAG_module)
11346 {
11347 imported_declaration = imported_name;
11348 canonical_name = imported_name_prefix;
11349 }
11350 else if (strlen (imported_name_prefix) > 0)
11351 canonical_name = obconcat (&objfile->objfile_obstack,
11352 imported_name_prefix,
11353 (cu->language == language_d ? "." : "::"),
11354 imported_name, (char *) NULL);
11355 else
11356 canonical_name = imported_name;
11357
11358 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11359 for (child_die = die->child; child_die && child_die->tag;
11360 child_die = sibling_die (child_die))
11361 {
11362 /* DWARF-4: A Fortran use statement with a “rename list” may be
11363 represented by an imported module entry with an import attribute
11364 referring to the module and owned entries corresponding to those
11365 entities that are renamed as part of being imported. */
11366
11367 if (child_die->tag != DW_TAG_imported_declaration)
11368 {
11369 complaint (_("child DW_TAG_imported_declaration expected "
11370 "- DIE at %s [in module %s]"),
11371 sect_offset_str (child_die->sect_off),
11372 objfile_name (objfile));
11373 continue;
11374 }
11375
11376 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11377 if (import_attr == NULL)
11378 {
11379 complaint (_("Tag '%s' has no DW_AT_import"),
11380 dwarf_tag_name (child_die->tag));
11381 continue;
11382 }
11383
11384 imported_cu = cu;
11385 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11386 &imported_cu);
11387 imported_name = dwarf2_name (imported_die, imported_cu);
11388 if (imported_name == NULL)
11389 {
11390 complaint (_("child DW_TAG_imported_declaration has unknown "
11391 "imported name - DIE at %s [in module %s]"),
11392 sect_offset_str (child_die->sect_off),
11393 objfile_name (objfile));
11394 continue;
11395 }
11396
11397 excludes.push_back (imported_name);
11398
11399 process_die (child_die, cu);
11400 }
11401
11402 add_using_directive (using_directives (cu),
11403 import_prefix,
11404 canonical_name,
11405 import_alias,
11406 imported_declaration,
11407 excludes,
11408 0,
11409 &objfile->objfile_obstack);
11410 }
11411
11412 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11413 types, but gives them a size of zero. Starting with version 14,
11414 ICC is compatible with GCC. */
11415
11416 static bool
11417 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11418 {
11419 if (!cu->checked_producer)
11420 check_producer (cu);
11421
11422 return cu->producer_is_icc_lt_14;
11423 }
11424
11425 /* ICC generates a DW_AT_type for C void functions. This was observed on
11426 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11427 which says that void functions should not have a DW_AT_type. */
11428
11429 static bool
11430 producer_is_icc (struct dwarf2_cu *cu)
11431 {
11432 if (!cu->checked_producer)
11433 check_producer (cu);
11434
11435 return cu->producer_is_icc;
11436 }
11437
11438 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11439 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11440 this, it was first present in GCC release 4.3.0. */
11441
11442 static bool
11443 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11444 {
11445 if (!cu->checked_producer)
11446 check_producer (cu);
11447
11448 return cu->producer_is_gcc_lt_4_3;
11449 }
11450
11451 static file_and_directory
11452 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11453 {
11454 file_and_directory res;
11455
11456 /* Find the filename. Do not use dwarf2_name here, since the filename
11457 is not a source language identifier. */
11458 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11459 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11460
11461 if (res.comp_dir == NULL
11462 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11463 && IS_ABSOLUTE_PATH (res.name))
11464 {
11465 res.comp_dir_storage = ldirname (res.name);
11466 if (!res.comp_dir_storage.empty ())
11467 res.comp_dir = res.comp_dir_storage.c_str ();
11468 }
11469 if (res.comp_dir != NULL)
11470 {
11471 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11472 directory, get rid of it. */
11473 const char *cp = strchr (res.comp_dir, ':');
11474
11475 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11476 res.comp_dir = cp + 1;
11477 }
11478
11479 if (res.name == NULL)
11480 res.name = "<unknown>";
11481
11482 return res;
11483 }
11484
11485 /* Handle DW_AT_stmt_list for a compilation unit.
11486 DIE is the DW_TAG_compile_unit die for CU.
11487 COMP_DIR is the compilation directory. LOWPC is passed to
11488 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11489
11490 static void
11491 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11492 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11493 {
11494 struct dwarf2_per_objfile *dwarf2_per_objfile
11495 = cu->per_cu->dwarf2_per_objfile;
11496 struct objfile *objfile = dwarf2_per_objfile->objfile;
11497 struct attribute *attr;
11498 struct line_header line_header_local;
11499 hashval_t line_header_local_hash;
11500 void **slot;
11501 int decode_mapping;
11502
11503 gdb_assert (! cu->per_cu->is_debug_types);
11504
11505 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11506 if (attr == NULL)
11507 return;
11508
11509 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11510
11511 /* The line header hash table is only created if needed (it exists to
11512 prevent redundant reading of the line table for partial_units).
11513 If we're given a partial_unit, we'll need it. If we're given a
11514 compile_unit, then use the line header hash table if it's already
11515 created, but don't create one just yet. */
11516
11517 if (dwarf2_per_objfile->line_header_hash == NULL
11518 && die->tag == DW_TAG_partial_unit)
11519 {
11520 dwarf2_per_objfile->line_header_hash
11521 = htab_create_alloc_ex (127, line_header_hash_voidp,
11522 line_header_eq_voidp,
11523 free_line_header_voidp,
11524 &objfile->objfile_obstack,
11525 hashtab_obstack_allocate,
11526 dummy_obstack_deallocate);
11527 }
11528
11529 line_header_local.sect_off = line_offset;
11530 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11531 line_header_local_hash = line_header_hash (&line_header_local);
11532 if (dwarf2_per_objfile->line_header_hash != NULL)
11533 {
11534 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11535 &line_header_local,
11536 line_header_local_hash, NO_INSERT);
11537
11538 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11539 is not present in *SLOT (since if there is something in *SLOT then
11540 it will be for a partial_unit). */
11541 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11542 {
11543 gdb_assert (*slot != NULL);
11544 cu->line_header = (struct line_header *) *slot;
11545 return;
11546 }
11547 }
11548
11549 /* dwarf_decode_line_header does not yet provide sufficient information.
11550 We always have to call also dwarf_decode_lines for it. */
11551 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11552 if (lh == NULL)
11553 return;
11554
11555 cu->line_header = lh.release ();
11556 cu->line_header_die_owner = die;
11557
11558 if (dwarf2_per_objfile->line_header_hash == NULL)
11559 slot = NULL;
11560 else
11561 {
11562 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11563 &line_header_local,
11564 line_header_local_hash, INSERT);
11565 gdb_assert (slot != NULL);
11566 }
11567 if (slot != NULL && *slot == NULL)
11568 {
11569 /* This newly decoded line number information unit will be owned
11570 by line_header_hash hash table. */
11571 *slot = cu->line_header;
11572 cu->line_header_die_owner = NULL;
11573 }
11574 else
11575 {
11576 /* We cannot free any current entry in (*slot) as that struct line_header
11577 may be already used by multiple CUs. Create only temporary decoded
11578 line_header for this CU - it may happen at most once for each line
11579 number information unit. And if we're not using line_header_hash
11580 then this is what we want as well. */
11581 gdb_assert (die->tag != DW_TAG_partial_unit);
11582 }
11583 decode_mapping = (die->tag != DW_TAG_partial_unit);
11584 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11585 decode_mapping);
11586
11587 }
11588
11589 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11590
11591 static void
11592 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11593 {
11594 struct dwarf2_per_objfile *dwarf2_per_objfile
11595 = cu->per_cu->dwarf2_per_objfile;
11596 struct objfile *objfile = dwarf2_per_objfile->objfile;
11597 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11598 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11599 CORE_ADDR highpc = ((CORE_ADDR) 0);
11600 struct attribute *attr;
11601 struct die_info *child_die;
11602 CORE_ADDR baseaddr;
11603
11604 prepare_one_comp_unit (cu, die, cu->language);
11605 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11606
11607 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11608
11609 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11610 from finish_block. */
11611 if (lowpc == ((CORE_ADDR) -1))
11612 lowpc = highpc;
11613 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11614
11615 file_and_directory fnd = find_file_and_directory (die, cu);
11616
11617 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11618 standardised yet. As a workaround for the language detection we fall
11619 back to the DW_AT_producer string. */
11620 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11621 cu->language = language_opencl;
11622
11623 /* Similar hack for Go. */
11624 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11625 set_cu_language (DW_LANG_Go, cu);
11626
11627 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11628
11629 /* Decode line number information if present. We do this before
11630 processing child DIEs, so that the line header table is available
11631 for DW_AT_decl_file. */
11632 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11633
11634 /* Process all dies in compilation unit. */
11635 if (die->child != NULL)
11636 {
11637 child_die = die->child;
11638 while (child_die && child_die->tag)
11639 {
11640 process_die (child_die, cu);
11641 child_die = sibling_die (child_die);
11642 }
11643 }
11644
11645 /* Decode macro information, if present. Dwarf 2 macro information
11646 refers to information in the line number info statement program
11647 header, so we can only read it if we've read the header
11648 successfully. */
11649 attr = dwarf2_attr (die, DW_AT_macros, cu);
11650 if (attr == NULL)
11651 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11652 if (attr && cu->line_header)
11653 {
11654 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11655 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11656
11657 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11658 }
11659 else
11660 {
11661 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11662 if (attr && cu->line_header)
11663 {
11664 unsigned int macro_offset = DW_UNSND (attr);
11665
11666 dwarf_decode_macros (cu, macro_offset, 0);
11667 }
11668 }
11669 }
11670
11671 void
11672 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11673 {
11674 struct type_unit_group *tu_group;
11675 int first_time;
11676 struct attribute *attr;
11677 unsigned int i;
11678 struct signatured_type *sig_type;
11679
11680 gdb_assert (per_cu->is_debug_types);
11681 sig_type = (struct signatured_type *) per_cu;
11682
11683 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11684
11685 /* If we're using .gdb_index (includes -readnow) then
11686 per_cu->type_unit_group may not have been set up yet. */
11687 if (sig_type->type_unit_group == NULL)
11688 sig_type->type_unit_group = get_type_unit_group (this, attr);
11689 tu_group = sig_type->type_unit_group;
11690
11691 /* If we've already processed this stmt_list there's no real need to
11692 do it again, we could fake it and just recreate the part we need
11693 (file name,index -> symtab mapping). If data shows this optimization
11694 is useful we can do it then. */
11695 first_time = tu_group->compunit_symtab == NULL;
11696
11697 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11698 debug info. */
11699 line_header_up lh;
11700 if (attr != NULL)
11701 {
11702 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11703 lh = dwarf_decode_line_header (line_offset, this);
11704 }
11705 if (lh == NULL)
11706 {
11707 if (first_time)
11708 start_symtab ("", NULL, 0);
11709 else
11710 {
11711 gdb_assert (tu_group->symtabs == NULL);
11712 gdb_assert (m_builder == nullptr);
11713 struct compunit_symtab *cust = tu_group->compunit_symtab;
11714 m_builder.reset (new struct buildsym_compunit
11715 (COMPUNIT_OBJFILE (cust), "",
11716 COMPUNIT_DIRNAME (cust),
11717 compunit_language (cust),
11718 0, cust));
11719 }
11720 return;
11721 }
11722
11723 line_header = lh.release ();
11724 line_header_die_owner = die;
11725
11726 if (first_time)
11727 {
11728 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11729
11730 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11731 still initializing it, and our caller (a few levels up)
11732 process_full_type_unit still needs to know if this is the first
11733 time. */
11734
11735 tu_group->num_symtabs = line_header->file_names_size ();
11736 tu_group->symtabs = XNEWVEC (struct symtab *,
11737 line_header->file_names_size ());
11738
11739 auto &file_names = line_header->file_names ();
11740 for (i = 0; i < file_names.size (); ++i)
11741 {
11742 file_entry &fe = file_names[i];
11743 dwarf2_start_subfile (this, fe.name,
11744 fe.include_dir (line_header));
11745 buildsym_compunit *b = get_builder ();
11746 if (b->get_current_subfile ()->symtab == NULL)
11747 {
11748 /* NOTE: start_subfile will recognize when it's been
11749 passed a file it has already seen. So we can't
11750 assume there's a simple mapping from
11751 cu->line_header->file_names to subfiles, plus
11752 cu->line_header->file_names may contain dups. */
11753 b->get_current_subfile ()->symtab
11754 = allocate_symtab (cust, b->get_current_subfile ()->name);
11755 }
11756
11757 fe.symtab = b->get_current_subfile ()->symtab;
11758 tu_group->symtabs[i] = fe.symtab;
11759 }
11760 }
11761 else
11762 {
11763 gdb_assert (m_builder == nullptr);
11764 struct compunit_symtab *cust = tu_group->compunit_symtab;
11765 m_builder.reset (new struct buildsym_compunit
11766 (COMPUNIT_OBJFILE (cust), "",
11767 COMPUNIT_DIRNAME (cust),
11768 compunit_language (cust),
11769 0, cust));
11770
11771 auto &file_names = line_header->file_names ();
11772 for (i = 0; i < file_names.size (); ++i)
11773 {
11774 file_entry &fe = file_names[i];
11775 fe.symtab = tu_group->symtabs[i];
11776 }
11777 }
11778
11779 /* The main symtab is allocated last. Type units don't have DW_AT_name
11780 so they don't have a "real" (so to speak) symtab anyway.
11781 There is later code that will assign the main symtab to all symbols
11782 that don't have one. We need to handle the case of a symbol with a
11783 missing symtab (DW_AT_decl_file) anyway. */
11784 }
11785
11786 /* Process DW_TAG_type_unit.
11787 For TUs we want to skip the first top level sibling if it's not the
11788 actual type being defined by this TU. In this case the first top
11789 level sibling is there to provide context only. */
11790
11791 static void
11792 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11793 {
11794 struct die_info *child_die;
11795
11796 prepare_one_comp_unit (cu, die, language_minimal);
11797
11798 /* Initialize (or reinitialize) the machinery for building symtabs.
11799 We do this before processing child DIEs, so that the line header table
11800 is available for DW_AT_decl_file. */
11801 cu->setup_type_unit_groups (die);
11802
11803 if (die->child != NULL)
11804 {
11805 child_die = die->child;
11806 while (child_die && child_die->tag)
11807 {
11808 process_die (child_die, cu);
11809 child_die = sibling_die (child_die);
11810 }
11811 }
11812 }
11813 \f
11814 /* DWO/DWP files.
11815
11816 http://gcc.gnu.org/wiki/DebugFission
11817 http://gcc.gnu.org/wiki/DebugFissionDWP
11818
11819 To simplify handling of both DWO files ("object" files with the DWARF info)
11820 and DWP files (a file with the DWOs packaged up into one file), we treat
11821 DWP files as having a collection of virtual DWO files. */
11822
11823 static hashval_t
11824 hash_dwo_file (const void *item)
11825 {
11826 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11827 hashval_t hash;
11828
11829 hash = htab_hash_string (dwo_file->dwo_name);
11830 if (dwo_file->comp_dir != NULL)
11831 hash += htab_hash_string (dwo_file->comp_dir);
11832 return hash;
11833 }
11834
11835 static int
11836 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11837 {
11838 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11839 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11840
11841 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11842 return 0;
11843 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11844 return lhs->comp_dir == rhs->comp_dir;
11845 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11846 }
11847
11848 /* Allocate a hash table for DWO files. */
11849
11850 static htab_up
11851 allocate_dwo_file_hash_table (struct objfile *objfile)
11852 {
11853 auto delete_dwo_file = [] (void *item)
11854 {
11855 struct dwo_file *dwo_file = (struct dwo_file *) item;
11856
11857 delete dwo_file;
11858 };
11859
11860 return htab_up (htab_create_alloc_ex (41,
11861 hash_dwo_file,
11862 eq_dwo_file,
11863 delete_dwo_file,
11864 &objfile->objfile_obstack,
11865 hashtab_obstack_allocate,
11866 dummy_obstack_deallocate));
11867 }
11868
11869 /* Lookup DWO file DWO_NAME. */
11870
11871 static void **
11872 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11873 const char *dwo_name,
11874 const char *comp_dir)
11875 {
11876 struct dwo_file find_entry;
11877 void **slot;
11878
11879 if (dwarf2_per_objfile->dwo_files == NULL)
11880 dwarf2_per_objfile->dwo_files
11881 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11882
11883 find_entry.dwo_name = dwo_name;
11884 find_entry.comp_dir = comp_dir;
11885 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11886 INSERT);
11887
11888 return slot;
11889 }
11890
11891 static hashval_t
11892 hash_dwo_unit (const void *item)
11893 {
11894 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11895
11896 /* This drops the top 32 bits of the id, but is ok for a hash. */
11897 return dwo_unit->signature;
11898 }
11899
11900 static int
11901 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11902 {
11903 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11904 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11905
11906 /* The signature is assumed to be unique within the DWO file.
11907 So while object file CU dwo_id's always have the value zero,
11908 that's OK, assuming each object file DWO file has only one CU,
11909 and that's the rule for now. */
11910 return lhs->signature == rhs->signature;
11911 }
11912
11913 /* Allocate a hash table for DWO CUs,TUs.
11914 There is one of these tables for each of CUs,TUs for each DWO file. */
11915
11916 static htab_t
11917 allocate_dwo_unit_table (struct objfile *objfile)
11918 {
11919 /* Start out with a pretty small number.
11920 Generally DWO files contain only one CU and maybe some TUs. */
11921 return htab_create_alloc_ex (3,
11922 hash_dwo_unit,
11923 eq_dwo_unit,
11924 NULL,
11925 &objfile->objfile_obstack,
11926 hashtab_obstack_allocate,
11927 dummy_obstack_deallocate);
11928 }
11929
11930 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11931
11932 struct create_dwo_cu_data
11933 {
11934 struct dwo_file *dwo_file;
11935 struct dwo_unit dwo_unit;
11936 };
11937
11938 /* die_reader_func for create_dwo_cu. */
11939
11940 static void
11941 create_dwo_cu_reader (const struct die_reader_specs *reader,
11942 const gdb_byte *info_ptr,
11943 struct die_info *comp_unit_die,
11944 int has_children,
11945 void *datap)
11946 {
11947 struct dwarf2_cu *cu = reader->cu;
11948 sect_offset sect_off = cu->per_cu->sect_off;
11949 struct dwarf2_section_info *section = cu->per_cu->section;
11950 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11951 struct dwo_file *dwo_file = data->dwo_file;
11952 struct dwo_unit *dwo_unit = &data->dwo_unit;
11953
11954 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11955 if (!signature.has_value ())
11956 {
11957 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11958 " its dwo_id [in module %s]"),
11959 sect_offset_str (sect_off), dwo_file->dwo_name);
11960 return;
11961 }
11962
11963 dwo_unit->dwo_file = dwo_file;
11964 dwo_unit->signature = *signature;
11965 dwo_unit->section = section;
11966 dwo_unit->sect_off = sect_off;
11967 dwo_unit->length = cu->per_cu->length;
11968
11969 if (dwarf_read_debug)
11970 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11971 sect_offset_str (sect_off),
11972 hex_string (dwo_unit->signature));
11973 }
11974
11975 /* Create the dwo_units for the CUs in a DWO_FILE.
11976 Note: This function processes DWO files only, not DWP files. */
11977
11978 static void
11979 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11980 struct dwo_file &dwo_file, dwarf2_section_info &section,
11981 htab_t &cus_htab)
11982 {
11983 struct objfile *objfile = dwarf2_per_objfile->objfile;
11984 const gdb_byte *info_ptr, *end_ptr;
11985
11986 dwarf2_read_section (objfile, &section);
11987 info_ptr = section.buffer;
11988
11989 if (info_ptr == NULL)
11990 return;
11991
11992 if (dwarf_read_debug)
11993 {
11994 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11995 get_section_name (&section),
11996 get_section_file_name (&section));
11997 }
11998
11999 end_ptr = info_ptr + section.size;
12000 while (info_ptr < end_ptr)
12001 {
12002 struct dwarf2_per_cu_data per_cu;
12003 struct create_dwo_cu_data create_dwo_cu_data;
12004 struct dwo_unit *dwo_unit;
12005 void **slot;
12006 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
12007
12008 memset (&create_dwo_cu_data.dwo_unit, 0,
12009 sizeof (create_dwo_cu_data.dwo_unit));
12010 memset (&per_cu, 0, sizeof (per_cu));
12011 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
12012 per_cu.is_debug_types = 0;
12013 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12014 per_cu.section = &section;
12015 create_dwo_cu_data.dwo_file = &dwo_file;
12016
12017 init_cutu_and_read_dies_no_follow (
12018 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12019 info_ptr += per_cu.length;
12020
12021 // If the unit could not be parsed, skip it.
12022 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12023 continue;
12024
12025 if (cus_htab == NULL)
12026 cus_htab = allocate_dwo_unit_table (objfile);
12027
12028 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12029 *dwo_unit = create_dwo_cu_data.dwo_unit;
12030 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12031 gdb_assert (slot != NULL);
12032 if (*slot != NULL)
12033 {
12034 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12035 sect_offset dup_sect_off = dup_cu->sect_off;
12036
12037 complaint (_("debug cu entry at offset %s is duplicate to"
12038 " the entry at offset %s, signature %s"),
12039 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
12040 hex_string (dwo_unit->signature));
12041 }
12042 *slot = (void *)dwo_unit;
12043 }
12044 }
12045
12046 /* DWP file .debug_{cu,tu}_index section format:
12047 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12048
12049 DWP Version 1:
12050
12051 Both index sections have the same format, and serve to map a 64-bit
12052 signature to a set of section numbers. Each section begins with a header,
12053 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12054 indexes, and a pool of 32-bit section numbers. The index sections will be
12055 aligned at 8-byte boundaries in the file.
12056
12057 The index section header consists of:
12058
12059 V, 32 bit version number
12060 -, 32 bits unused
12061 N, 32 bit number of compilation units or type units in the index
12062 M, 32 bit number of slots in the hash table
12063
12064 Numbers are recorded using the byte order of the application binary.
12065
12066 The hash table begins at offset 16 in the section, and consists of an array
12067 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12068 order of the application binary). Unused slots in the hash table are 0.
12069 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12070
12071 The parallel table begins immediately after the hash table
12072 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12073 array of 32-bit indexes (using the byte order of the application binary),
12074 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12075 table contains a 32-bit index into the pool of section numbers. For unused
12076 hash table slots, the corresponding entry in the parallel table will be 0.
12077
12078 The pool of section numbers begins immediately following the hash table
12079 (at offset 16 + 12 * M from the beginning of the section). The pool of
12080 section numbers consists of an array of 32-bit words (using the byte order
12081 of the application binary). Each item in the array is indexed starting
12082 from 0. The hash table entry provides the index of the first section
12083 number in the set. Additional section numbers in the set follow, and the
12084 set is terminated by a 0 entry (section number 0 is not used in ELF).
12085
12086 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12087 section must be the first entry in the set, and the .debug_abbrev.dwo must
12088 be the second entry. Other members of the set may follow in any order.
12089
12090 ---
12091
12092 DWP Version 2:
12093
12094 DWP Version 2 combines all the .debug_info, etc. sections into one,
12095 and the entries in the index tables are now offsets into these sections.
12096 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12097 section.
12098
12099 Index Section Contents:
12100 Header
12101 Hash Table of Signatures dwp_hash_table.hash_table
12102 Parallel Table of Indices dwp_hash_table.unit_table
12103 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12104 Table of Section Sizes dwp_hash_table.v2.sizes
12105
12106 The index section header consists of:
12107
12108 V, 32 bit version number
12109 L, 32 bit number of columns in the table of section offsets
12110 N, 32 bit number of compilation units or type units in the index
12111 M, 32 bit number of slots in the hash table
12112
12113 Numbers are recorded using the byte order of the application binary.
12114
12115 The hash table has the same format as version 1.
12116 The parallel table of indices has the same format as version 1,
12117 except that the entries are origin-1 indices into the table of sections
12118 offsets and the table of section sizes.
12119
12120 The table of offsets begins immediately following the parallel table
12121 (at offset 16 + 12 * M from the beginning of the section). The table is
12122 a two-dimensional array of 32-bit words (using the byte order of the
12123 application binary), with L columns and N+1 rows, in row-major order.
12124 Each row in the array is indexed starting from 0. The first row provides
12125 a key to the remaining rows: each column in this row provides an identifier
12126 for a debug section, and the offsets in the same column of subsequent rows
12127 refer to that section. The section identifiers are:
12128
12129 DW_SECT_INFO 1 .debug_info.dwo
12130 DW_SECT_TYPES 2 .debug_types.dwo
12131 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12132 DW_SECT_LINE 4 .debug_line.dwo
12133 DW_SECT_LOC 5 .debug_loc.dwo
12134 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12135 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12136 DW_SECT_MACRO 8 .debug_macro.dwo
12137
12138 The offsets provided by the CU and TU index sections are the base offsets
12139 for the contributions made by each CU or TU to the corresponding section
12140 in the package file. Each CU and TU header contains an abbrev_offset
12141 field, used to find the abbreviations table for that CU or TU within the
12142 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12143 be interpreted as relative to the base offset given in the index section.
12144 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12145 should be interpreted as relative to the base offset for .debug_line.dwo,
12146 and offsets into other debug sections obtained from DWARF attributes should
12147 also be interpreted as relative to the corresponding base offset.
12148
12149 The table of sizes begins immediately following the table of offsets.
12150 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12151 with L columns and N rows, in row-major order. Each row in the array is
12152 indexed starting from 1 (row 0 is shared by the two tables).
12153
12154 ---
12155
12156 Hash table lookup is handled the same in version 1 and 2:
12157
12158 We assume that N and M will not exceed 2^32 - 1.
12159 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12160
12161 Given a 64-bit compilation unit signature or a type signature S, an entry
12162 in the hash table is located as follows:
12163
12164 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12165 the low-order k bits all set to 1.
12166
12167 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12168
12169 3) If the hash table entry at index H matches the signature, use that
12170 entry. If the hash table entry at index H is unused (all zeroes),
12171 terminate the search: the signature is not present in the table.
12172
12173 4) Let H = (H + H') modulo M. Repeat at Step 3.
12174
12175 Because M > N and H' and M are relatively prime, the search is guaranteed
12176 to stop at an unused slot or find the match. */
12177
12178 /* Create a hash table to map DWO IDs to their CU/TU entry in
12179 .debug_{info,types}.dwo in DWP_FILE.
12180 Returns NULL if there isn't one.
12181 Note: This function processes DWP files only, not DWO files. */
12182
12183 static struct dwp_hash_table *
12184 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12185 struct dwp_file *dwp_file, int is_debug_types)
12186 {
12187 struct objfile *objfile = dwarf2_per_objfile->objfile;
12188 bfd *dbfd = dwp_file->dbfd.get ();
12189 const gdb_byte *index_ptr, *index_end;
12190 struct dwarf2_section_info *index;
12191 uint32_t version, nr_columns, nr_units, nr_slots;
12192 struct dwp_hash_table *htab;
12193
12194 if (is_debug_types)
12195 index = &dwp_file->sections.tu_index;
12196 else
12197 index = &dwp_file->sections.cu_index;
12198
12199 if (dwarf2_section_empty_p (index))
12200 return NULL;
12201 dwarf2_read_section (objfile, index);
12202
12203 index_ptr = index->buffer;
12204 index_end = index_ptr + index->size;
12205
12206 version = read_4_bytes (dbfd, index_ptr);
12207 index_ptr += 4;
12208 if (version == 2)
12209 nr_columns = read_4_bytes (dbfd, index_ptr);
12210 else
12211 nr_columns = 0;
12212 index_ptr += 4;
12213 nr_units = read_4_bytes (dbfd, index_ptr);
12214 index_ptr += 4;
12215 nr_slots = read_4_bytes (dbfd, index_ptr);
12216 index_ptr += 4;
12217
12218 if (version != 1 && version != 2)
12219 {
12220 error (_("Dwarf Error: unsupported DWP file version (%s)"
12221 " [in module %s]"),
12222 pulongest (version), dwp_file->name);
12223 }
12224 if (nr_slots != (nr_slots & -nr_slots))
12225 {
12226 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12227 " is not power of 2 [in module %s]"),
12228 pulongest (nr_slots), dwp_file->name);
12229 }
12230
12231 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12232 htab->version = version;
12233 htab->nr_columns = nr_columns;
12234 htab->nr_units = nr_units;
12235 htab->nr_slots = nr_slots;
12236 htab->hash_table = index_ptr;
12237 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12238
12239 /* Exit early if the table is empty. */
12240 if (nr_slots == 0 || nr_units == 0
12241 || (version == 2 && nr_columns == 0))
12242 {
12243 /* All must be zero. */
12244 if (nr_slots != 0 || nr_units != 0
12245 || (version == 2 && nr_columns != 0))
12246 {
12247 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
12248 " all zero [in modules %s]"),
12249 dwp_file->name);
12250 }
12251 return htab;
12252 }
12253
12254 if (version == 1)
12255 {
12256 htab->section_pool.v1.indices =
12257 htab->unit_table + sizeof (uint32_t) * nr_slots;
12258 /* It's harder to decide whether the section is too small in v1.
12259 V1 is deprecated anyway so we punt. */
12260 }
12261 else
12262 {
12263 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12264 int *ids = htab->section_pool.v2.section_ids;
12265 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
12266 /* Reverse map for error checking. */
12267 int ids_seen[DW_SECT_MAX + 1];
12268 int i;
12269
12270 if (nr_columns < 2)
12271 {
12272 error (_("Dwarf Error: bad DWP hash table, too few columns"
12273 " in section table [in module %s]"),
12274 dwp_file->name);
12275 }
12276 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12277 {
12278 error (_("Dwarf Error: bad DWP hash table, too many columns"
12279 " in section table [in module %s]"),
12280 dwp_file->name);
12281 }
12282 memset (ids, 255, sizeof_ids);
12283 memset (ids_seen, 255, sizeof (ids_seen));
12284 for (i = 0; i < nr_columns; ++i)
12285 {
12286 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12287
12288 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12289 {
12290 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12291 " in section table [in module %s]"),
12292 id, dwp_file->name);
12293 }
12294 if (ids_seen[id] != -1)
12295 {
12296 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12297 " id %d in section table [in module %s]"),
12298 id, dwp_file->name);
12299 }
12300 ids_seen[id] = i;
12301 ids[i] = id;
12302 }
12303 /* Must have exactly one info or types section. */
12304 if (((ids_seen[DW_SECT_INFO] != -1)
12305 + (ids_seen[DW_SECT_TYPES] != -1))
12306 != 1)
12307 {
12308 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12309 " DWO info/types section [in module %s]"),
12310 dwp_file->name);
12311 }
12312 /* Must have an abbrev section. */
12313 if (ids_seen[DW_SECT_ABBREV] == -1)
12314 {
12315 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12316 " section [in module %s]"),
12317 dwp_file->name);
12318 }
12319 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12320 htab->section_pool.v2.sizes =
12321 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12322 * nr_units * nr_columns);
12323 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12324 * nr_units * nr_columns))
12325 > index_end)
12326 {
12327 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12328 " [in module %s]"),
12329 dwp_file->name);
12330 }
12331 }
12332
12333 return htab;
12334 }
12335
12336 /* Update SECTIONS with the data from SECTP.
12337
12338 This function is like the other "locate" section routines that are
12339 passed to bfd_map_over_sections, but in this context the sections to
12340 read comes from the DWP V1 hash table, not the full ELF section table.
12341
12342 The result is non-zero for success, or zero if an error was found. */
12343
12344 static int
12345 locate_v1_virtual_dwo_sections (asection *sectp,
12346 struct virtual_v1_dwo_sections *sections)
12347 {
12348 const struct dwop_section_names *names = &dwop_section_names;
12349
12350 if (section_is_p (sectp->name, &names->abbrev_dwo))
12351 {
12352 /* There can be only one. */
12353 if (sections->abbrev.s.section != NULL)
12354 return 0;
12355 sections->abbrev.s.section = sectp;
12356 sections->abbrev.size = bfd_section_size (sectp);
12357 }
12358 else if (section_is_p (sectp->name, &names->info_dwo)
12359 || section_is_p (sectp->name, &names->types_dwo))
12360 {
12361 /* There can be only one. */
12362 if (sections->info_or_types.s.section != NULL)
12363 return 0;
12364 sections->info_or_types.s.section = sectp;
12365 sections->info_or_types.size = bfd_section_size (sectp);
12366 }
12367 else if (section_is_p (sectp->name, &names->line_dwo))
12368 {
12369 /* There can be only one. */
12370 if (sections->line.s.section != NULL)
12371 return 0;
12372 sections->line.s.section = sectp;
12373 sections->line.size = bfd_section_size (sectp);
12374 }
12375 else if (section_is_p (sectp->name, &names->loc_dwo))
12376 {
12377 /* There can be only one. */
12378 if (sections->loc.s.section != NULL)
12379 return 0;
12380 sections->loc.s.section = sectp;
12381 sections->loc.size = bfd_section_size (sectp);
12382 }
12383 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12384 {
12385 /* There can be only one. */
12386 if (sections->macinfo.s.section != NULL)
12387 return 0;
12388 sections->macinfo.s.section = sectp;
12389 sections->macinfo.size = bfd_section_size (sectp);
12390 }
12391 else if (section_is_p (sectp->name, &names->macro_dwo))
12392 {
12393 /* There can be only one. */
12394 if (sections->macro.s.section != NULL)
12395 return 0;
12396 sections->macro.s.section = sectp;
12397 sections->macro.size = bfd_section_size (sectp);
12398 }
12399 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12400 {
12401 /* There can be only one. */
12402 if (sections->str_offsets.s.section != NULL)
12403 return 0;
12404 sections->str_offsets.s.section = sectp;
12405 sections->str_offsets.size = bfd_section_size (sectp);
12406 }
12407 else
12408 {
12409 /* No other kind of section is valid. */
12410 return 0;
12411 }
12412
12413 return 1;
12414 }
12415
12416 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12417 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12418 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12419 This is for DWP version 1 files. */
12420
12421 static struct dwo_unit *
12422 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12423 struct dwp_file *dwp_file,
12424 uint32_t unit_index,
12425 const char *comp_dir,
12426 ULONGEST signature, int is_debug_types)
12427 {
12428 struct objfile *objfile = dwarf2_per_objfile->objfile;
12429 const struct dwp_hash_table *dwp_htab =
12430 is_debug_types ? dwp_file->tus : dwp_file->cus;
12431 bfd *dbfd = dwp_file->dbfd.get ();
12432 const char *kind = is_debug_types ? "TU" : "CU";
12433 struct dwo_file *dwo_file;
12434 struct dwo_unit *dwo_unit;
12435 struct virtual_v1_dwo_sections sections;
12436 void **dwo_file_slot;
12437 int i;
12438
12439 gdb_assert (dwp_file->version == 1);
12440
12441 if (dwarf_read_debug)
12442 {
12443 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12444 kind,
12445 pulongest (unit_index), hex_string (signature),
12446 dwp_file->name);
12447 }
12448
12449 /* Fetch the sections of this DWO unit.
12450 Put a limit on the number of sections we look for so that bad data
12451 doesn't cause us to loop forever. */
12452
12453 #define MAX_NR_V1_DWO_SECTIONS \
12454 (1 /* .debug_info or .debug_types */ \
12455 + 1 /* .debug_abbrev */ \
12456 + 1 /* .debug_line */ \
12457 + 1 /* .debug_loc */ \
12458 + 1 /* .debug_str_offsets */ \
12459 + 1 /* .debug_macro or .debug_macinfo */ \
12460 + 1 /* trailing zero */)
12461
12462 memset (&sections, 0, sizeof (sections));
12463
12464 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12465 {
12466 asection *sectp;
12467 uint32_t section_nr =
12468 read_4_bytes (dbfd,
12469 dwp_htab->section_pool.v1.indices
12470 + (unit_index + i) * sizeof (uint32_t));
12471
12472 if (section_nr == 0)
12473 break;
12474 if (section_nr >= dwp_file->num_sections)
12475 {
12476 error (_("Dwarf Error: bad DWP hash table, section number too large"
12477 " [in module %s]"),
12478 dwp_file->name);
12479 }
12480
12481 sectp = dwp_file->elf_sections[section_nr];
12482 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12483 {
12484 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12485 " [in module %s]"),
12486 dwp_file->name);
12487 }
12488 }
12489
12490 if (i < 2
12491 || dwarf2_section_empty_p (&sections.info_or_types)
12492 || dwarf2_section_empty_p (&sections.abbrev))
12493 {
12494 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12495 " [in module %s]"),
12496 dwp_file->name);
12497 }
12498 if (i == MAX_NR_V1_DWO_SECTIONS)
12499 {
12500 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12501 " [in module %s]"),
12502 dwp_file->name);
12503 }
12504
12505 /* It's easier for the rest of the code if we fake a struct dwo_file and
12506 have dwo_unit "live" in that. At least for now.
12507
12508 The DWP file can be made up of a random collection of CUs and TUs.
12509 However, for each CU + set of TUs that came from the same original DWO
12510 file, we can combine them back into a virtual DWO file to save space
12511 (fewer struct dwo_file objects to allocate). Remember that for really
12512 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12513
12514 std::string virtual_dwo_name =
12515 string_printf ("virtual-dwo/%d-%d-%d-%d",
12516 get_section_id (&sections.abbrev),
12517 get_section_id (&sections.line),
12518 get_section_id (&sections.loc),
12519 get_section_id (&sections.str_offsets));
12520 /* Can we use an existing virtual DWO file? */
12521 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12522 virtual_dwo_name.c_str (),
12523 comp_dir);
12524 /* Create one if necessary. */
12525 if (*dwo_file_slot == NULL)
12526 {
12527 if (dwarf_read_debug)
12528 {
12529 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12530 virtual_dwo_name.c_str ());
12531 }
12532 dwo_file = new struct dwo_file;
12533 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12534 virtual_dwo_name);
12535 dwo_file->comp_dir = comp_dir;
12536 dwo_file->sections.abbrev = sections.abbrev;
12537 dwo_file->sections.line = sections.line;
12538 dwo_file->sections.loc = sections.loc;
12539 dwo_file->sections.macinfo = sections.macinfo;
12540 dwo_file->sections.macro = sections.macro;
12541 dwo_file->sections.str_offsets = sections.str_offsets;
12542 /* The "str" section is global to the entire DWP file. */
12543 dwo_file->sections.str = dwp_file->sections.str;
12544 /* The info or types section is assigned below to dwo_unit,
12545 there's no need to record it in dwo_file.
12546 Also, we can't simply record type sections in dwo_file because
12547 we record a pointer into the vector in dwo_unit. As we collect more
12548 types we'll grow the vector and eventually have to reallocate space
12549 for it, invalidating all copies of pointers into the previous
12550 contents. */
12551 *dwo_file_slot = dwo_file;
12552 }
12553 else
12554 {
12555 if (dwarf_read_debug)
12556 {
12557 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12558 virtual_dwo_name.c_str ());
12559 }
12560 dwo_file = (struct dwo_file *) *dwo_file_slot;
12561 }
12562
12563 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12564 dwo_unit->dwo_file = dwo_file;
12565 dwo_unit->signature = signature;
12566 dwo_unit->section =
12567 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12568 *dwo_unit->section = sections.info_or_types;
12569 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12570
12571 return dwo_unit;
12572 }
12573
12574 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12575 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12576 piece within that section used by a TU/CU, return a virtual section
12577 of just that piece. */
12578
12579 static struct dwarf2_section_info
12580 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12581 struct dwarf2_section_info *section,
12582 bfd_size_type offset, bfd_size_type size)
12583 {
12584 struct dwarf2_section_info result;
12585 asection *sectp;
12586
12587 gdb_assert (section != NULL);
12588 gdb_assert (!section->is_virtual);
12589
12590 memset (&result, 0, sizeof (result));
12591 result.s.containing_section = section;
12592 result.is_virtual = true;
12593
12594 if (size == 0)
12595 return result;
12596
12597 sectp = get_section_bfd_section (section);
12598
12599 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12600 bounds of the real section. This is a pretty-rare event, so just
12601 flag an error (easier) instead of a warning and trying to cope. */
12602 if (sectp == NULL
12603 || offset + size > bfd_section_size (sectp))
12604 {
12605 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12606 " in section %s [in module %s]"),
12607 sectp ? bfd_section_name (sectp) : "<unknown>",
12608 objfile_name (dwarf2_per_objfile->objfile));
12609 }
12610
12611 result.virtual_offset = offset;
12612 result.size = size;
12613 return result;
12614 }
12615
12616 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12617 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12618 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12619 This is for DWP version 2 files. */
12620
12621 static struct dwo_unit *
12622 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12623 struct dwp_file *dwp_file,
12624 uint32_t unit_index,
12625 const char *comp_dir,
12626 ULONGEST signature, int is_debug_types)
12627 {
12628 struct objfile *objfile = dwarf2_per_objfile->objfile;
12629 const struct dwp_hash_table *dwp_htab =
12630 is_debug_types ? dwp_file->tus : dwp_file->cus;
12631 bfd *dbfd = dwp_file->dbfd.get ();
12632 const char *kind = is_debug_types ? "TU" : "CU";
12633 struct dwo_file *dwo_file;
12634 struct dwo_unit *dwo_unit;
12635 struct virtual_v2_dwo_sections sections;
12636 void **dwo_file_slot;
12637 int i;
12638
12639 gdb_assert (dwp_file->version == 2);
12640
12641 if (dwarf_read_debug)
12642 {
12643 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12644 kind,
12645 pulongest (unit_index), hex_string (signature),
12646 dwp_file->name);
12647 }
12648
12649 /* Fetch the section offsets of this DWO unit. */
12650
12651 memset (&sections, 0, sizeof (sections));
12652
12653 for (i = 0; i < dwp_htab->nr_columns; ++i)
12654 {
12655 uint32_t offset = read_4_bytes (dbfd,
12656 dwp_htab->section_pool.v2.offsets
12657 + (((unit_index - 1) * dwp_htab->nr_columns
12658 + i)
12659 * sizeof (uint32_t)));
12660 uint32_t size = read_4_bytes (dbfd,
12661 dwp_htab->section_pool.v2.sizes
12662 + (((unit_index - 1) * dwp_htab->nr_columns
12663 + i)
12664 * sizeof (uint32_t)));
12665
12666 switch (dwp_htab->section_pool.v2.section_ids[i])
12667 {
12668 case DW_SECT_INFO:
12669 case DW_SECT_TYPES:
12670 sections.info_or_types_offset = offset;
12671 sections.info_or_types_size = size;
12672 break;
12673 case DW_SECT_ABBREV:
12674 sections.abbrev_offset = offset;
12675 sections.abbrev_size = size;
12676 break;
12677 case DW_SECT_LINE:
12678 sections.line_offset = offset;
12679 sections.line_size = size;
12680 break;
12681 case DW_SECT_LOC:
12682 sections.loc_offset = offset;
12683 sections.loc_size = size;
12684 break;
12685 case DW_SECT_STR_OFFSETS:
12686 sections.str_offsets_offset = offset;
12687 sections.str_offsets_size = size;
12688 break;
12689 case DW_SECT_MACINFO:
12690 sections.macinfo_offset = offset;
12691 sections.macinfo_size = size;
12692 break;
12693 case DW_SECT_MACRO:
12694 sections.macro_offset = offset;
12695 sections.macro_size = size;
12696 break;
12697 }
12698 }
12699
12700 /* It's easier for the rest of the code if we fake a struct dwo_file and
12701 have dwo_unit "live" in that. At least for now.
12702
12703 The DWP file can be made up of a random collection of CUs and TUs.
12704 However, for each CU + set of TUs that came from the same original DWO
12705 file, we can combine them back into a virtual DWO file to save space
12706 (fewer struct dwo_file objects to allocate). Remember that for really
12707 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12708
12709 std::string virtual_dwo_name =
12710 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12711 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12712 (long) (sections.line_size ? sections.line_offset : 0),
12713 (long) (sections.loc_size ? sections.loc_offset : 0),
12714 (long) (sections.str_offsets_size
12715 ? sections.str_offsets_offset : 0));
12716 /* Can we use an existing virtual DWO file? */
12717 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12718 virtual_dwo_name.c_str (),
12719 comp_dir);
12720 /* Create one if necessary. */
12721 if (*dwo_file_slot == NULL)
12722 {
12723 if (dwarf_read_debug)
12724 {
12725 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12726 virtual_dwo_name.c_str ());
12727 }
12728 dwo_file = new struct dwo_file;
12729 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12730 virtual_dwo_name);
12731 dwo_file->comp_dir = comp_dir;
12732 dwo_file->sections.abbrev =
12733 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12734 sections.abbrev_offset, sections.abbrev_size);
12735 dwo_file->sections.line =
12736 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12737 sections.line_offset, sections.line_size);
12738 dwo_file->sections.loc =
12739 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12740 sections.loc_offset, sections.loc_size);
12741 dwo_file->sections.macinfo =
12742 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12743 sections.macinfo_offset, sections.macinfo_size);
12744 dwo_file->sections.macro =
12745 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12746 sections.macro_offset, sections.macro_size);
12747 dwo_file->sections.str_offsets =
12748 create_dwp_v2_section (dwarf2_per_objfile,
12749 &dwp_file->sections.str_offsets,
12750 sections.str_offsets_offset,
12751 sections.str_offsets_size);
12752 /* The "str" section is global to the entire DWP file. */
12753 dwo_file->sections.str = dwp_file->sections.str;
12754 /* The info or types section is assigned below to dwo_unit,
12755 there's no need to record it in dwo_file.
12756 Also, we can't simply record type sections in dwo_file because
12757 we record a pointer into the vector in dwo_unit. As we collect more
12758 types we'll grow the vector and eventually have to reallocate space
12759 for it, invalidating all copies of pointers into the previous
12760 contents. */
12761 *dwo_file_slot = dwo_file;
12762 }
12763 else
12764 {
12765 if (dwarf_read_debug)
12766 {
12767 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12768 virtual_dwo_name.c_str ());
12769 }
12770 dwo_file = (struct dwo_file *) *dwo_file_slot;
12771 }
12772
12773 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12774 dwo_unit->dwo_file = dwo_file;
12775 dwo_unit->signature = signature;
12776 dwo_unit->section =
12777 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12778 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12779 is_debug_types
12780 ? &dwp_file->sections.types
12781 : &dwp_file->sections.info,
12782 sections.info_or_types_offset,
12783 sections.info_or_types_size);
12784 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12785
12786 return dwo_unit;
12787 }
12788
12789 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12790 Returns NULL if the signature isn't found. */
12791
12792 static struct dwo_unit *
12793 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12794 struct dwp_file *dwp_file, const char *comp_dir,
12795 ULONGEST signature, int is_debug_types)
12796 {
12797 const struct dwp_hash_table *dwp_htab =
12798 is_debug_types ? dwp_file->tus : dwp_file->cus;
12799 bfd *dbfd = dwp_file->dbfd.get ();
12800 uint32_t mask = dwp_htab->nr_slots - 1;
12801 uint32_t hash = signature & mask;
12802 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12803 unsigned int i;
12804 void **slot;
12805 struct dwo_unit find_dwo_cu;
12806
12807 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12808 find_dwo_cu.signature = signature;
12809 slot = htab_find_slot (is_debug_types
12810 ? dwp_file->loaded_tus
12811 : dwp_file->loaded_cus,
12812 &find_dwo_cu, INSERT);
12813
12814 if (*slot != NULL)
12815 return (struct dwo_unit *) *slot;
12816
12817 /* Use a for loop so that we don't loop forever on bad debug info. */
12818 for (i = 0; i < dwp_htab->nr_slots; ++i)
12819 {
12820 ULONGEST signature_in_table;
12821
12822 signature_in_table =
12823 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12824 if (signature_in_table == signature)
12825 {
12826 uint32_t unit_index =
12827 read_4_bytes (dbfd,
12828 dwp_htab->unit_table + hash * sizeof (uint32_t));
12829
12830 if (dwp_file->version == 1)
12831 {
12832 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12833 dwp_file, unit_index,
12834 comp_dir, signature,
12835 is_debug_types);
12836 }
12837 else
12838 {
12839 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12840 dwp_file, unit_index,
12841 comp_dir, signature,
12842 is_debug_types);
12843 }
12844 return (struct dwo_unit *) *slot;
12845 }
12846 if (signature_in_table == 0)
12847 return NULL;
12848 hash = (hash + hash2) & mask;
12849 }
12850
12851 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12852 " [in module %s]"),
12853 dwp_file->name);
12854 }
12855
12856 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12857 Open the file specified by FILE_NAME and hand it off to BFD for
12858 preliminary analysis. Return a newly initialized bfd *, which
12859 includes a canonicalized copy of FILE_NAME.
12860 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12861 SEARCH_CWD is true if the current directory is to be searched.
12862 It will be searched before debug-file-directory.
12863 If successful, the file is added to the bfd include table of the
12864 objfile's bfd (see gdb_bfd_record_inclusion).
12865 If unable to find/open the file, return NULL.
12866 NOTE: This function is derived from symfile_bfd_open. */
12867
12868 static gdb_bfd_ref_ptr
12869 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12870 const char *file_name, int is_dwp, int search_cwd)
12871 {
12872 int desc;
12873 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12874 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12875 to debug_file_directory. */
12876 const char *search_path;
12877 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12878
12879 gdb::unique_xmalloc_ptr<char> search_path_holder;
12880 if (search_cwd)
12881 {
12882 if (*debug_file_directory != '\0')
12883 {
12884 search_path_holder.reset (concat (".", dirname_separator_string,
12885 debug_file_directory,
12886 (char *) NULL));
12887 search_path = search_path_holder.get ();
12888 }
12889 else
12890 search_path = ".";
12891 }
12892 else
12893 search_path = debug_file_directory;
12894
12895 openp_flags flags = OPF_RETURN_REALPATH;
12896 if (is_dwp)
12897 flags |= OPF_SEARCH_IN_PATH;
12898
12899 gdb::unique_xmalloc_ptr<char> absolute_name;
12900 desc = openp (search_path, flags, file_name,
12901 O_RDONLY | O_BINARY, &absolute_name);
12902 if (desc < 0)
12903 return NULL;
12904
12905 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12906 gnutarget, desc));
12907 if (sym_bfd == NULL)
12908 return NULL;
12909 bfd_set_cacheable (sym_bfd.get (), 1);
12910
12911 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12912 return NULL;
12913
12914 /* Success. Record the bfd as having been included by the objfile's bfd.
12915 This is important because things like demangled_names_hash lives in the
12916 objfile's per_bfd space and may have references to things like symbol
12917 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12918 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12919
12920 return sym_bfd;
12921 }
12922
12923 /* Try to open DWO file FILE_NAME.
12924 COMP_DIR is the DW_AT_comp_dir attribute.
12925 The result is the bfd handle of the file.
12926 If there is a problem finding or opening the file, return NULL.
12927 Upon success, the canonicalized path of the file is stored in the bfd,
12928 same as symfile_bfd_open. */
12929
12930 static gdb_bfd_ref_ptr
12931 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12932 const char *file_name, const char *comp_dir)
12933 {
12934 if (IS_ABSOLUTE_PATH (file_name))
12935 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12936 0 /*is_dwp*/, 0 /*search_cwd*/);
12937
12938 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12939
12940 if (comp_dir != NULL)
12941 {
12942 gdb::unique_xmalloc_ptr<char> path_to_try
12943 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12944
12945 /* NOTE: If comp_dir is a relative path, this will also try the
12946 search path, which seems useful. */
12947 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12948 path_to_try.get (),
12949 0 /*is_dwp*/,
12950 1 /*search_cwd*/));
12951 if (abfd != NULL)
12952 return abfd;
12953 }
12954
12955 /* That didn't work, try debug-file-directory, which, despite its name,
12956 is a list of paths. */
12957
12958 if (*debug_file_directory == '\0')
12959 return NULL;
12960
12961 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12962 0 /*is_dwp*/, 1 /*search_cwd*/);
12963 }
12964
12965 /* This function is mapped across the sections and remembers the offset and
12966 size of each of the DWO debugging sections we are interested in. */
12967
12968 static void
12969 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12970 {
12971 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12972 const struct dwop_section_names *names = &dwop_section_names;
12973
12974 if (section_is_p (sectp->name, &names->abbrev_dwo))
12975 {
12976 dwo_sections->abbrev.s.section = sectp;
12977 dwo_sections->abbrev.size = bfd_section_size (sectp);
12978 }
12979 else if (section_is_p (sectp->name, &names->info_dwo))
12980 {
12981 dwo_sections->info.s.section = sectp;
12982 dwo_sections->info.size = bfd_section_size (sectp);
12983 }
12984 else if (section_is_p (sectp->name, &names->line_dwo))
12985 {
12986 dwo_sections->line.s.section = sectp;
12987 dwo_sections->line.size = bfd_section_size (sectp);
12988 }
12989 else if (section_is_p (sectp->name, &names->loc_dwo))
12990 {
12991 dwo_sections->loc.s.section = sectp;
12992 dwo_sections->loc.size = bfd_section_size (sectp);
12993 }
12994 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12995 {
12996 dwo_sections->macinfo.s.section = sectp;
12997 dwo_sections->macinfo.size = bfd_section_size (sectp);
12998 }
12999 else if (section_is_p (sectp->name, &names->macro_dwo))
13000 {
13001 dwo_sections->macro.s.section = sectp;
13002 dwo_sections->macro.size = bfd_section_size (sectp);
13003 }
13004 else if (section_is_p (sectp->name, &names->str_dwo))
13005 {
13006 dwo_sections->str.s.section = sectp;
13007 dwo_sections->str.size = bfd_section_size (sectp);
13008 }
13009 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13010 {
13011 dwo_sections->str_offsets.s.section = sectp;
13012 dwo_sections->str_offsets.size = bfd_section_size (sectp);
13013 }
13014 else if (section_is_p (sectp->name, &names->types_dwo))
13015 {
13016 struct dwarf2_section_info type_section;
13017
13018 memset (&type_section, 0, sizeof (type_section));
13019 type_section.s.section = sectp;
13020 type_section.size = bfd_section_size (sectp);
13021 dwo_sections->types.push_back (type_section);
13022 }
13023 }
13024
13025 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
13026 by PER_CU. This is for the non-DWP case.
13027 The result is NULL if DWO_NAME can't be found. */
13028
13029 static struct dwo_file *
13030 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13031 const char *dwo_name, const char *comp_dir)
13032 {
13033 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13034
13035 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
13036 if (dbfd == NULL)
13037 {
13038 if (dwarf_read_debug)
13039 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13040 return NULL;
13041 }
13042
13043 dwo_file_up dwo_file (new struct dwo_file);
13044 dwo_file->dwo_name = dwo_name;
13045 dwo_file->comp_dir = comp_dir;
13046 dwo_file->dbfd = std::move (dbfd);
13047
13048 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
13049 &dwo_file->sections);
13050
13051 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13052 dwo_file->cus);
13053
13054 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
13055 dwo_file->sections.types, dwo_file->tus);
13056
13057 if (dwarf_read_debug)
13058 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13059
13060 return dwo_file.release ();
13061 }
13062
13063 /* This function is mapped across the sections and remembers the offset and
13064 size of each of the DWP debugging sections common to version 1 and 2 that
13065 we are interested in. */
13066
13067 static void
13068 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13069 void *dwp_file_ptr)
13070 {
13071 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13072 const struct dwop_section_names *names = &dwop_section_names;
13073 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13074
13075 /* Record the ELF section number for later lookup: this is what the
13076 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13077 gdb_assert (elf_section_nr < dwp_file->num_sections);
13078 dwp_file->elf_sections[elf_section_nr] = sectp;
13079
13080 /* Look for specific sections that we need. */
13081 if (section_is_p (sectp->name, &names->str_dwo))
13082 {
13083 dwp_file->sections.str.s.section = sectp;
13084 dwp_file->sections.str.size = bfd_section_size (sectp);
13085 }
13086 else if (section_is_p (sectp->name, &names->cu_index))
13087 {
13088 dwp_file->sections.cu_index.s.section = sectp;
13089 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
13090 }
13091 else if (section_is_p (sectp->name, &names->tu_index))
13092 {
13093 dwp_file->sections.tu_index.s.section = sectp;
13094 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
13095 }
13096 }
13097
13098 /* This function is mapped across the sections and remembers the offset and
13099 size of each of the DWP version 2 debugging sections that we are interested
13100 in. This is split into a separate function because we don't know if we
13101 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13102
13103 static void
13104 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13105 {
13106 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13107 const struct dwop_section_names *names = &dwop_section_names;
13108 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13109
13110 /* Record the ELF section number for later lookup: this is what the
13111 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13112 gdb_assert (elf_section_nr < dwp_file->num_sections);
13113 dwp_file->elf_sections[elf_section_nr] = sectp;
13114
13115 /* Look for specific sections that we need. */
13116 if (section_is_p (sectp->name, &names->abbrev_dwo))
13117 {
13118 dwp_file->sections.abbrev.s.section = sectp;
13119 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13120 }
13121 else if (section_is_p (sectp->name, &names->info_dwo))
13122 {
13123 dwp_file->sections.info.s.section = sectp;
13124 dwp_file->sections.info.size = bfd_section_size (sectp);
13125 }
13126 else if (section_is_p (sectp->name, &names->line_dwo))
13127 {
13128 dwp_file->sections.line.s.section = sectp;
13129 dwp_file->sections.line.size = bfd_section_size (sectp);
13130 }
13131 else if (section_is_p (sectp->name, &names->loc_dwo))
13132 {
13133 dwp_file->sections.loc.s.section = sectp;
13134 dwp_file->sections.loc.size = bfd_section_size (sectp);
13135 }
13136 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13137 {
13138 dwp_file->sections.macinfo.s.section = sectp;
13139 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
13140 }
13141 else if (section_is_p (sectp->name, &names->macro_dwo))
13142 {
13143 dwp_file->sections.macro.s.section = sectp;
13144 dwp_file->sections.macro.size = bfd_section_size (sectp);
13145 }
13146 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13147 {
13148 dwp_file->sections.str_offsets.s.section = sectp;
13149 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13150 }
13151 else if (section_is_p (sectp->name, &names->types_dwo))
13152 {
13153 dwp_file->sections.types.s.section = sectp;
13154 dwp_file->sections.types.size = bfd_section_size (sectp);
13155 }
13156 }
13157
13158 /* Hash function for dwp_file loaded CUs/TUs. */
13159
13160 static hashval_t
13161 hash_dwp_loaded_cutus (const void *item)
13162 {
13163 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13164
13165 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13166 return dwo_unit->signature;
13167 }
13168
13169 /* Equality function for dwp_file loaded CUs/TUs. */
13170
13171 static int
13172 eq_dwp_loaded_cutus (const void *a, const void *b)
13173 {
13174 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13175 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13176
13177 return dua->signature == dub->signature;
13178 }
13179
13180 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13181
13182 static htab_t
13183 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13184 {
13185 return htab_create_alloc_ex (3,
13186 hash_dwp_loaded_cutus,
13187 eq_dwp_loaded_cutus,
13188 NULL,
13189 &objfile->objfile_obstack,
13190 hashtab_obstack_allocate,
13191 dummy_obstack_deallocate);
13192 }
13193
13194 /* Try to open DWP file FILE_NAME.
13195 The result is the bfd handle of the file.
13196 If there is a problem finding or opening the file, return NULL.
13197 Upon success, the canonicalized path of the file is stored in the bfd,
13198 same as symfile_bfd_open. */
13199
13200 static gdb_bfd_ref_ptr
13201 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13202 const char *file_name)
13203 {
13204 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13205 1 /*is_dwp*/,
13206 1 /*search_cwd*/));
13207 if (abfd != NULL)
13208 return abfd;
13209
13210 /* Work around upstream bug 15652.
13211 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13212 [Whether that's a "bug" is debatable, but it is getting in our way.]
13213 We have no real idea where the dwp file is, because gdb's realpath-ing
13214 of the executable's path may have discarded the needed info.
13215 [IWBN if the dwp file name was recorded in the executable, akin to
13216 .gnu_debuglink, but that doesn't exist yet.]
13217 Strip the directory from FILE_NAME and search again. */
13218 if (*debug_file_directory != '\0')
13219 {
13220 /* Don't implicitly search the current directory here.
13221 If the user wants to search "." to handle this case,
13222 it must be added to debug-file-directory. */
13223 return try_open_dwop_file (dwarf2_per_objfile,
13224 lbasename (file_name), 1 /*is_dwp*/,
13225 0 /*search_cwd*/);
13226 }
13227
13228 return NULL;
13229 }
13230
13231 /* Initialize the use of the DWP file for the current objfile.
13232 By convention the name of the DWP file is ${objfile}.dwp.
13233 The result is NULL if it can't be found. */
13234
13235 static std::unique_ptr<struct dwp_file>
13236 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13237 {
13238 struct objfile *objfile = dwarf2_per_objfile->objfile;
13239
13240 /* Try to find first .dwp for the binary file before any symbolic links
13241 resolving. */
13242
13243 /* If the objfile is a debug file, find the name of the real binary
13244 file and get the name of dwp file from there. */
13245 std::string dwp_name;
13246 if (objfile->separate_debug_objfile_backlink != NULL)
13247 {
13248 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13249 const char *backlink_basename = lbasename (backlink->original_name);
13250
13251 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13252 }
13253 else
13254 dwp_name = objfile->original_name;
13255
13256 dwp_name += ".dwp";
13257
13258 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13259 if (dbfd == NULL
13260 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13261 {
13262 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13263 dwp_name = objfile_name (objfile);
13264 dwp_name += ".dwp";
13265 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13266 }
13267
13268 if (dbfd == NULL)
13269 {
13270 if (dwarf_read_debug)
13271 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13272 return std::unique_ptr<dwp_file> ();
13273 }
13274
13275 const char *name = bfd_get_filename (dbfd.get ());
13276 std::unique_ptr<struct dwp_file> dwp_file
13277 (new struct dwp_file (name, std::move (dbfd)));
13278
13279 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13280 dwp_file->elf_sections =
13281 OBSTACK_CALLOC (&objfile->objfile_obstack,
13282 dwp_file->num_sections, asection *);
13283
13284 bfd_map_over_sections (dwp_file->dbfd.get (),
13285 dwarf2_locate_common_dwp_sections,
13286 dwp_file.get ());
13287
13288 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13289 0);
13290
13291 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13292 1);
13293
13294 /* The DWP file version is stored in the hash table. Oh well. */
13295 if (dwp_file->cus && dwp_file->tus
13296 && dwp_file->cus->version != dwp_file->tus->version)
13297 {
13298 /* Technically speaking, we should try to limp along, but this is
13299 pretty bizarre. We use pulongest here because that's the established
13300 portability solution (e.g, we cannot use %u for uint32_t). */
13301 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13302 " TU version %s [in DWP file %s]"),
13303 pulongest (dwp_file->cus->version),
13304 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13305 }
13306
13307 if (dwp_file->cus)
13308 dwp_file->version = dwp_file->cus->version;
13309 else if (dwp_file->tus)
13310 dwp_file->version = dwp_file->tus->version;
13311 else
13312 dwp_file->version = 2;
13313
13314 if (dwp_file->version == 2)
13315 bfd_map_over_sections (dwp_file->dbfd.get (),
13316 dwarf2_locate_v2_dwp_sections,
13317 dwp_file.get ());
13318
13319 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13320 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13321
13322 if (dwarf_read_debug)
13323 {
13324 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13325 fprintf_unfiltered (gdb_stdlog,
13326 " %s CUs, %s TUs\n",
13327 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13328 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13329 }
13330
13331 return dwp_file;
13332 }
13333
13334 /* Wrapper around open_and_init_dwp_file, only open it once. */
13335
13336 static struct dwp_file *
13337 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13338 {
13339 if (! dwarf2_per_objfile->dwp_checked)
13340 {
13341 dwarf2_per_objfile->dwp_file
13342 = open_and_init_dwp_file (dwarf2_per_objfile);
13343 dwarf2_per_objfile->dwp_checked = 1;
13344 }
13345 return dwarf2_per_objfile->dwp_file.get ();
13346 }
13347
13348 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13349 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13350 or in the DWP file for the objfile, referenced by THIS_UNIT.
13351 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13352 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13353
13354 This is called, for example, when wanting to read a variable with a
13355 complex location. Therefore we don't want to do file i/o for every call.
13356 Therefore we don't want to look for a DWO file on every call.
13357 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13358 then we check if we've already seen DWO_NAME, and only THEN do we check
13359 for a DWO file.
13360
13361 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13362 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13363
13364 static struct dwo_unit *
13365 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13366 const char *dwo_name, const char *comp_dir,
13367 ULONGEST signature, int is_debug_types)
13368 {
13369 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13370 struct objfile *objfile = dwarf2_per_objfile->objfile;
13371 const char *kind = is_debug_types ? "TU" : "CU";
13372 void **dwo_file_slot;
13373 struct dwo_file *dwo_file;
13374 struct dwp_file *dwp_file;
13375
13376 /* First see if there's a DWP file.
13377 If we have a DWP file but didn't find the DWO inside it, don't
13378 look for the original DWO file. It makes gdb behave differently
13379 depending on whether one is debugging in the build tree. */
13380
13381 dwp_file = get_dwp_file (dwarf2_per_objfile);
13382 if (dwp_file != NULL)
13383 {
13384 const struct dwp_hash_table *dwp_htab =
13385 is_debug_types ? dwp_file->tus : dwp_file->cus;
13386
13387 if (dwp_htab != NULL)
13388 {
13389 struct dwo_unit *dwo_cutu =
13390 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13391 signature, is_debug_types);
13392
13393 if (dwo_cutu != NULL)
13394 {
13395 if (dwarf_read_debug)
13396 {
13397 fprintf_unfiltered (gdb_stdlog,
13398 "Virtual DWO %s %s found: @%s\n",
13399 kind, hex_string (signature),
13400 host_address_to_string (dwo_cutu));
13401 }
13402 return dwo_cutu;
13403 }
13404 }
13405 }
13406 else
13407 {
13408 /* No DWP file, look for the DWO file. */
13409
13410 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13411 dwo_name, comp_dir);
13412 if (*dwo_file_slot == NULL)
13413 {
13414 /* Read in the file and build a table of the CUs/TUs it contains. */
13415 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13416 }
13417 /* NOTE: This will be NULL if unable to open the file. */
13418 dwo_file = (struct dwo_file *) *dwo_file_slot;
13419
13420 if (dwo_file != NULL)
13421 {
13422 struct dwo_unit *dwo_cutu = NULL;
13423
13424 if (is_debug_types && dwo_file->tus)
13425 {
13426 struct dwo_unit find_dwo_cutu;
13427
13428 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13429 find_dwo_cutu.signature = signature;
13430 dwo_cutu
13431 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13432 }
13433 else if (!is_debug_types && dwo_file->cus)
13434 {
13435 struct dwo_unit find_dwo_cutu;
13436
13437 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13438 find_dwo_cutu.signature = signature;
13439 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13440 &find_dwo_cutu);
13441 }
13442
13443 if (dwo_cutu != NULL)
13444 {
13445 if (dwarf_read_debug)
13446 {
13447 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13448 kind, dwo_name, hex_string (signature),
13449 host_address_to_string (dwo_cutu));
13450 }
13451 return dwo_cutu;
13452 }
13453 }
13454 }
13455
13456 /* We didn't find it. This could mean a dwo_id mismatch, or
13457 someone deleted the DWO/DWP file, or the search path isn't set up
13458 correctly to find the file. */
13459
13460 if (dwarf_read_debug)
13461 {
13462 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13463 kind, dwo_name, hex_string (signature));
13464 }
13465
13466 /* This is a warning and not a complaint because it can be caused by
13467 pilot error (e.g., user accidentally deleting the DWO). */
13468 {
13469 /* Print the name of the DWP file if we looked there, helps the user
13470 better diagnose the problem. */
13471 std::string dwp_text;
13472
13473 if (dwp_file != NULL)
13474 dwp_text = string_printf (" [in DWP file %s]",
13475 lbasename (dwp_file->name));
13476
13477 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13478 " [in module %s]"),
13479 kind, dwo_name, hex_string (signature),
13480 dwp_text.c_str (),
13481 this_unit->is_debug_types ? "TU" : "CU",
13482 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13483 }
13484 return NULL;
13485 }
13486
13487 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13488 See lookup_dwo_cutu_unit for details. */
13489
13490 static struct dwo_unit *
13491 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13492 const char *dwo_name, const char *comp_dir,
13493 ULONGEST signature)
13494 {
13495 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13496 }
13497
13498 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13499 See lookup_dwo_cutu_unit for details. */
13500
13501 static struct dwo_unit *
13502 lookup_dwo_type_unit (struct signatured_type *this_tu,
13503 const char *dwo_name, const char *comp_dir)
13504 {
13505 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13506 }
13507
13508 /* Traversal function for queue_and_load_all_dwo_tus. */
13509
13510 static int
13511 queue_and_load_dwo_tu (void **slot, void *info)
13512 {
13513 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13514 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13515 ULONGEST signature = dwo_unit->signature;
13516 struct signatured_type *sig_type =
13517 lookup_dwo_signatured_type (per_cu->cu, signature);
13518
13519 if (sig_type != NULL)
13520 {
13521 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13522
13523 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13524 a real dependency of PER_CU on SIG_TYPE. That is detected later
13525 while processing PER_CU. */
13526 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13527 load_full_type_unit (sig_cu);
13528 per_cu->imported_symtabs_push (sig_cu);
13529 }
13530
13531 return 1;
13532 }
13533
13534 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13535 The DWO may have the only definition of the type, though it may not be
13536 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13537 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13538
13539 static void
13540 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13541 {
13542 struct dwo_unit *dwo_unit;
13543 struct dwo_file *dwo_file;
13544
13545 gdb_assert (!per_cu->is_debug_types);
13546 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13547 gdb_assert (per_cu->cu != NULL);
13548
13549 dwo_unit = per_cu->cu->dwo_unit;
13550 gdb_assert (dwo_unit != NULL);
13551
13552 dwo_file = dwo_unit->dwo_file;
13553 if (dwo_file->tus != NULL)
13554 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13555 }
13556
13557 /* Read in various DIEs. */
13558
13559 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13560 Inherit only the children of the DW_AT_abstract_origin DIE not being
13561 already referenced by DW_AT_abstract_origin from the children of the
13562 current DIE. */
13563
13564 static void
13565 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13566 {
13567 struct die_info *child_die;
13568 sect_offset *offsetp;
13569 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13570 struct die_info *origin_die;
13571 /* Iterator of the ORIGIN_DIE children. */
13572 struct die_info *origin_child_die;
13573 struct attribute *attr;
13574 struct dwarf2_cu *origin_cu;
13575 struct pending **origin_previous_list_in_scope;
13576
13577 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13578 if (!attr)
13579 return;
13580
13581 /* Note that following die references may follow to a die in a
13582 different cu. */
13583
13584 origin_cu = cu;
13585 origin_die = follow_die_ref (die, attr, &origin_cu);
13586
13587 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13588 symbols in. */
13589 origin_previous_list_in_scope = origin_cu->list_in_scope;
13590 origin_cu->list_in_scope = cu->list_in_scope;
13591
13592 if (die->tag != origin_die->tag
13593 && !(die->tag == DW_TAG_inlined_subroutine
13594 && origin_die->tag == DW_TAG_subprogram))
13595 complaint (_("DIE %s and its abstract origin %s have different tags"),
13596 sect_offset_str (die->sect_off),
13597 sect_offset_str (origin_die->sect_off));
13598
13599 std::vector<sect_offset> offsets;
13600
13601 for (child_die = die->child;
13602 child_die && child_die->tag;
13603 child_die = sibling_die (child_die))
13604 {
13605 struct die_info *child_origin_die;
13606 struct dwarf2_cu *child_origin_cu;
13607
13608 /* We are trying to process concrete instance entries:
13609 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13610 it's not relevant to our analysis here. i.e. detecting DIEs that are
13611 present in the abstract instance but not referenced in the concrete
13612 one. */
13613 if (child_die->tag == DW_TAG_call_site
13614 || child_die->tag == DW_TAG_GNU_call_site)
13615 continue;
13616
13617 /* For each CHILD_DIE, find the corresponding child of
13618 ORIGIN_DIE. If there is more than one layer of
13619 DW_AT_abstract_origin, follow them all; there shouldn't be,
13620 but GCC versions at least through 4.4 generate this (GCC PR
13621 40573). */
13622 child_origin_die = child_die;
13623 child_origin_cu = cu;
13624 while (1)
13625 {
13626 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13627 child_origin_cu);
13628 if (attr == NULL)
13629 break;
13630 child_origin_die = follow_die_ref (child_origin_die, attr,
13631 &child_origin_cu);
13632 }
13633
13634 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13635 counterpart may exist. */
13636 if (child_origin_die != child_die)
13637 {
13638 if (child_die->tag != child_origin_die->tag
13639 && !(child_die->tag == DW_TAG_inlined_subroutine
13640 && child_origin_die->tag == DW_TAG_subprogram))
13641 complaint (_("Child DIE %s and its abstract origin %s have "
13642 "different tags"),
13643 sect_offset_str (child_die->sect_off),
13644 sect_offset_str (child_origin_die->sect_off));
13645 if (child_origin_die->parent != origin_die)
13646 complaint (_("Child DIE %s and its abstract origin %s have "
13647 "different parents"),
13648 sect_offset_str (child_die->sect_off),
13649 sect_offset_str (child_origin_die->sect_off));
13650 else
13651 offsets.push_back (child_origin_die->sect_off);
13652 }
13653 }
13654 std::sort (offsets.begin (), offsets.end ());
13655 sect_offset *offsets_end = offsets.data () + offsets.size ();
13656 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13657 if (offsetp[-1] == *offsetp)
13658 complaint (_("Multiple children of DIE %s refer "
13659 "to DIE %s as their abstract origin"),
13660 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13661
13662 offsetp = offsets.data ();
13663 origin_child_die = origin_die->child;
13664 while (origin_child_die && origin_child_die->tag)
13665 {
13666 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13667 while (offsetp < offsets_end
13668 && *offsetp < origin_child_die->sect_off)
13669 offsetp++;
13670 if (offsetp >= offsets_end
13671 || *offsetp > origin_child_die->sect_off)
13672 {
13673 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13674 Check whether we're already processing ORIGIN_CHILD_DIE.
13675 This can happen with mutually referenced abstract_origins.
13676 PR 16581. */
13677 if (!origin_child_die->in_process)
13678 process_die (origin_child_die, origin_cu);
13679 }
13680 origin_child_die = sibling_die (origin_child_die);
13681 }
13682 origin_cu->list_in_scope = origin_previous_list_in_scope;
13683
13684 if (cu != origin_cu)
13685 compute_delayed_physnames (origin_cu);
13686 }
13687
13688 static void
13689 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13690 {
13691 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13692 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13693 struct context_stack *newobj;
13694 CORE_ADDR lowpc;
13695 CORE_ADDR highpc;
13696 struct die_info *child_die;
13697 struct attribute *attr, *call_line, *call_file;
13698 const char *name;
13699 CORE_ADDR baseaddr;
13700 struct block *block;
13701 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13702 std::vector<struct symbol *> template_args;
13703 struct template_symbol *templ_func = NULL;
13704
13705 if (inlined_func)
13706 {
13707 /* If we do not have call site information, we can't show the
13708 caller of this inlined function. That's too confusing, so
13709 only use the scope for local variables. */
13710 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13711 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13712 if (call_line == NULL || call_file == NULL)
13713 {
13714 read_lexical_block_scope (die, cu);
13715 return;
13716 }
13717 }
13718
13719 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13720
13721 name = dwarf2_name (die, cu);
13722
13723 /* Ignore functions with missing or empty names. These are actually
13724 illegal according to the DWARF standard. */
13725 if (name == NULL)
13726 {
13727 complaint (_("missing name for subprogram DIE at %s"),
13728 sect_offset_str (die->sect_off));
13729 return;
13730 }
13731
13732 /* Ignore functions with missing or invalid low and high pc attributes. */
13733 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13734 <= PC_BOUNDS_INVALID)
13735 {
13736 attr = dwarf2_attr (die, DW_AT_external, cu);
13737 if (!attr || !DW_UNSND (attr))
13738 complaint (_("cannot get low and high bounds "
13739 "for subprogram DIE at %s"),
13740 sect_offset_str (die->sect_off));
13741 return;
13742 }
13743
13744 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13745 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13746
13747 /* If we have any template arguments, then we must allocate a
13748 different sort of symbol. */
13749 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13750 {
13751 if (child_die->tag == DW_TAG_template_type_param
13752 || child_die->tag == DW_TAG_template_value_param)
13753 {
13754 templ_func = allocate_template_symbol (objfile);
13755 templ_func->subclass = SYMBOL_TEMPLATE;
13756 break;
13757 }
13758 }
13759
13760 newobj = cu->get_builder ()->push_context (0, lowpc);
13761 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13762 (struct symbol *) templ_func);
13763
13764 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13765 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13766 cu->language);
13767
13768 /* If there is a location expression for DW_AT_frame_base, record
13769 it. */
13770 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13771 if (attr != nullptr)
13772 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13773
13774 /* If there is a location for the static link, record it. */
13775 newobj->static_link = NULL;
13776 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13777 if (attr != nullptr)
13778 {
13779 newobj->static_link
13780 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13781 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13782 dwarf2_per_cu_addr_type (cu->per_cu));
13783 }
13784
13785 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13786
13787 if (die->child != NULL)
13788 {
13789 child_die = die->child;
13790 while (child_die && child_die->tag)
13791 {
13792 if (child_die->tag == DW_TAG_template_type_param
13793 || child_die->tag == DW_TAG_template_value_param)
13794 {
13795 struct symbol *arg = new_symbol (child_die, NULL, cu);
13796
13797 if (arg != NULL)
13798 template_args.push_back (arg);
13799 }
13800 else
13801 process_die (child_die, cu);
13802 child_die = sibling_die (child_die);
13803 }
13804 }
13805
13806 inherit_abstract_dies (die, cu);
13807
13808 /* If we have a DW_AT_specification, we might need to import using
13809 directives from the context of the specification DIE. See the
13810 comment in determine_prefix. */
13811 if (cu->language == language_cplus
13812 && dwarf2_attr (die, DW_AT_specification, cu))
13813 {
13814 struct dwarf2_cu *spec_cu = cu;
13815 struct die_info *spec_die = die_specification (die, &spec_cu);
13816
13817 while (spec_die)
13818 {
13819 child_die = spec_die->child;
13820 while (child_die && child_die->tag)
13821 {
13822 if (child_die->tag == DW_TAG_imported_module)
13823 process_die (child_die, spec_cu);
13824 child_die = sibling_die (child_die);
13825 }
13826
13827 /* In some cases, GCC generates specification DIEs that
13828 themselves contain DW_AT_specification attributes. */
13829 spec_die = die_specification (spec_die, &spec_cu);
13830 }
13831 }
13832
13833 struct context_stack cstk = cu->get_builder ()->pop_context ();
13834 /* Make a block for the local symbols within. */
13835 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13836 cstk.static_link, lowpc, highpc);
13837
13838 /* For C++, set the block's scope. */
13839 if ((cu->language == language_cplus
13840 || cu->language == language_fortran
13841 || cu->language == language_d
13842 || cu->language == language_rust)
13843 && cu->processing_has_namespace_info)
13844 block_set_scope (block, determine_prefix (die, cu),
13845 &objfile->objfile_obstack);
13846
13847 /* If we have address ranges, record them. */
13848 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13849
13850 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13851
13852 /* Attach template arguments to function. */
13853 if (!template_args.empty ())
13854 {
13855 gdb_assert (templ_func != NULL);
13856
13857 templ_func->n_template_arguments = template_args.size ();
13858 templ_func->template_arguments
13859 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13860 templ_func->n_template_arguments);
13861 memcpy (templ_func->template_arguments,
13862 template_args.data (),
13863 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13864
13865 /* Make sure that the symtab is set on the new symbols. Even
13866 though they don't appear in this symtab directly, other parts
13867 of gdb assume that symbols do, and this is reasonably
13868 true. */
13869 for (symbol *sym : template_args)
13870 symbol_set_symtab (sym, symbol_symtab (templ_func));
13871 }
13872
13873 /* In C++, we can have functions nested inside functions (e.g., when
13874 a function declares a class that has methods). This means that
13875 when we finish processing a function scope, we may need to go
13876 back to building a containing block's symbol lists. */
13877 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13878 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13879
13880 /* If we've finished processing a top-level function, subsequent
13881 symbols go in the file symbol list. */
13882 if (cu->get_builder ()->outermost_context_p ())
13883 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13884 }
13885
13886 /* Process all the DIES contained within a lexical block scope. Start
13887 a new scope, process the dies, and then close the scope. */
13888
13889 static void
13890 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13891 {
13892 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13893 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13894 CORE_ADDR lowpc, highpc;
13895 struct die_info *child_die;
13896 CORE_ADDR baseaddr;
13897
13898 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13899
13900 /* Ignore blocks with missing or invalid low and high pc attributes. */
13901 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13902 as multiple lexical blocks? Handling children in a sane way would
13903 be nasty. Might be easier to properly extend generic blocks to
13904 describe ranges. */
13905 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13906 {
13907 case PC_BOUNDS_NOT_PRESENT:
13908 /* DW_TAG_lexical_block has no attributes, process its children as if
13909 there was no wrapping by that DW_TAG_lexical_block.
13910 GCC does no longer produces such DWARF since GCC r224161. */
13911 for (child_die = die->child;
13912 child_die != NULL && child_die->tag;
13913 child_die = sibling_die (child_die))
13914 process_die (child_die, cu);
13915 return;
13916 case PC_BOUNDS_INVALID:
13917 return;
13918 }
13919 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13920 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13921
13922 cu->get_builder ()->push_context (0, lowpc);
13923 if (die->child != NULL)
13924 {
13925 child_die = die->child;
13926 while (child_die && child_die->tag)
13927 {
13928 process_die (child_die, cu);
13929 child_die = sibling_die (child_die);
13930 }
13931 }
13932 inherit_abstract_dies (die, cu);
13933 struct context_stack cstk = cu->get_builder ()->pop_context ();
13934
13935 if (*cu->get_builder ()->get_local_symbols () != NULL
13936 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13937 {
13938 struct block *block
13939 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13940 cstk.start_addr, highpc);
13941
13942 /* Note that recording ranges after traversing children, as we
13943 do here, means that recording a parent's ranges entails
13944 walking across all its children's ranges as they appear in
13945 the address map, which is quadratic behavior.
13946
13947 It would be nicer to record the parent's ranges before
13948 traversing its children, simply overriding whatever you find
13949 there. But since we don't even decide whether to create a
13950 block until after we've traversed its children, that's hard
13951 to do. */
13952 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13953 }
13954 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13955 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13956 }
13957
13958 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13959
13960 static void
13961 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13962 {
13963 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13964 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13965 CORE_ADDR pc, baseaddr;
13966 struct attribute *attr;
13967 struct call_site *call_site, call_site_local;
13968 void **slot;
13969 int nparams;
13970 struct die_info *child_die;
13971
13972 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13973
13974 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13975 if (attr == NULL)
13976 {
13977 /* This was a pre-DWARF-5 GNU extension alias
13978 for DW_AT_call_return_pc. */
13979 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13980 }
13981 if (!attr)
13982 {
13983 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13984 "DIE %s [in module %s]"),
13985 sect_offset_str (die->sect_off), objfile_name (objfile));
13986 return;
13987 }
13988 pc = attr_value_as_address (attr) + baseaddr;
13989 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13990
13991 if (cu->call_site_htab == NULL)
13992 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13993 NULL, &objfile->objfile_obstack,
13994 hashtab_obstack_allocate, NULL);
13995 call_site_local.pc = pc;
13996 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13997 if (*slot != NULL)
13998 {
13999 complaint (_("Duplicate PC %s for DW_TAG_call_site "
14000 "DIE %s [in module %s]"),
14001 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
14002 objfile_name (objfile));
14003 return;
14004 }
14005
14006 /* Count parameters at the caller. */
14007
14008 nparams = 0;
14009 for (child_die = die->child; child_die && child_die->tag;
14010 child_die = sibling_die (child_die))
14011 {
14012 if (child_die->tag != DW_TAG_call_site_parameter
14013 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14014 {
14015 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
14016 "DW_TAG_call_site child DIE %s [in module %s]"),
14017 child_die->tag, sect_offset_str (child_die->sect_off),
14018 objfile_name (objfile));
14019 continue;
14020 }
14021
14022 nparams++;
14023 }
14024
14025 call_site
14026 = ((struct call_site *)
14027 obstack_alloc (&objfile->objfile_obstack,
14028 sizeof (*call_site)
14029 + (sizeof (*call_site->parameter) * (nparams - 1))));
14030 *slot = call_site;
14031 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14032 call_site->pc = pc;
14033
14034 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14035 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14036 {
14037 struct die_info *func_die;
14038
14039 /* Skip also over DW_TAG_inlined_subroutine. */
14040 for (func_die = die->parent;
14041 func_die && func_die->tag != DW_TAG_subprogram
14042 && func_die->tag != DW_TAG_subroutine_type;
14043 func_die = func_die->parent);
14044
14045 /* DW_AT_call_all_calls is a superset
14046 of DW_AT_call_all_tail_calls. */
14047 if (func_die
14048 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14049 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14050 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14051 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14052 {
14053 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14054 not complete. But keep CALL_SITE for look ups via call_site_htab,
14055 both the initial caller containing the real return address PC and
14056 the final callee containing the current PC of a chain of tail
14057 calls do not need to have the tail call list complete. But any
14058 function candidate for a virtual tail call frame searched via
14059 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14060 determined unambiguously. */
14061 }
14062 else
14063 {
14064 struct type *func_type = NULL;
14065
14066 if (func_die)
14067 func_type = get_die_type (func_die, cu);
14068 if (func_type != NULL)
14069 {
14070 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14071
14072 /* Enlist this call site to the function. */
14073 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14074 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14075 }
14076 else
14077 complaint (_("Cannot find function owning DW_TAG_call_site "
14078 "DIE %s [in module %s]"),
14079 sect_offset_str (die->sect_off), objfile_name (objfile));
14080 }
14081 }
14082
14083 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14084 if (attr == NULL)
14085 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14086 if (attr == NULL)
14087 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14088 if (attr == NULL)
14089 {
14090 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14091 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14092 }
14093 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14094 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14095 /* Keep NULL DWARF_BLOCK. */;
14096 else if (attr_form_is_block (attr))
14097 {
14098 struct dwarf2_locexpr_baton *dlbaton;
14099
14100 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14101 dlbaton->data = DW_BLOCK (attr)->data;
14102 dlbaton->size = DW_BLOCK (attr)->size;
14103 dlbaton->per_cu = cu->per_cu;
14104
14105 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14106 }
14107 else if (attr_form_is_ref (attr))
14108 {
14109 struct dwarf2_cu *target_cu = cu;
14110 struct die_info *target_die;
14111
14112 target_die = follow_die_ref (die, attr, &target_cu);
14113 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14114 if (die_is_declaration (target_die, target_cu))
14115 {
14116 const char *target_physname;
14117
14118 /* Prefer the mangled name; otherwise compute the demangled one. */
14119 target_physname = dw2_linkage_name (target_die, target_cu);
14120 if (target_physname == NULL)
14121 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14122 if (target_physname == NULL)
14123 complaint (_("DW_AT_call_target target DIE has invalid "
14124 "physname, for referencing DIE %s [in module %s]"),
14125 sect_offset_str (die->sect_off), objfile_name (objfile));
14126 else
14127 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14128 }
14129 else
14130 {
14131 CORE_ADDR lowpc;
14132
14133 /* DW_AT_entry_pc should be preferred. */
14134 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14135 <= PC_BOUNDS_INVALID)
14136 complaint (_("DW_AT_call_target target DIE has invalid "
14137 "low pc, for referencing DIE %s [in module %s]"),
14138 sect_offset_str (die->sect_off), objfile_name (objfile));
14139 else
14140 {
14141 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14142 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14143 }
14144 }
14145 }
14146 else
14147 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14148 "block nor reference, for DIE %s [in module %s]"),
14149 sect_offset_str (die->sect_off), objfile_name (objfile));
14150
14151 call_site->per_cu = cu->per_cu;
14152
14153 for (child_die = die->child;
14154 child_die && child_die->tag;
14155 child_die = sibling_die (child_die))
14156 {
14157 struct call_site_parameter *parameter;
14158 struct attribute *loc, *origin;
14159
14160 if (child_die->tag != DW_TAG_call_site_parameter
14161 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14162 {
14163 /* Already printed the complaint above. */
14164 continue;
14165 }
14166
14167 gdb_assert (call_site->parameter_count < nparams);
14168 parameter = &call_site->parameter[call_site->parameter_count];
14169
14170 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14171 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14172 register is contained in DW_AT_call_value. */
14173
14174 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14175 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14176 if (origin == NULL)
14177 {
14178 /* This was a pre-DWARF-5 GNU extension alias
14179 for DW_AT_call_parameter. */
14180 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14181 }
14182 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14183 {
14184 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14185
14186 sect_offset sect_off
14187 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14188 if (!offset_in_cu_p (&cu->header, sect_off))
14189 {
14190 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14191 binding can be done only inside one CU. Such referenced DIE
14192 therefore cannot be even moved to DW_TAG_partial_unit. */
14193 complaint (_("DW_AT_call_parameter offset is not in CU for "
14194 "DW_TAG_call_site child DIE %s [in module %s]"),
14195 sect_offset_str (child_die->sect_off),
14196 objfile_name (objfile));
14197 continue;
14198 }
14199 parameter->u.param_cu_off
14200 = (cu_offset) (sect_off - cu->header.sect_off);
14201 }
14202 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14203 {
14204 complaint (_("No DW_FORM_block* DW_AT_location for "
14205 "DW_TAG_call_site child DIE %s [in module %s]"),
14206 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14207 continue;
14208 }
14209 else
14210 {
14211 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14212 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14213 if (parameter->u.dwarf_reg != -1)
14214 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14215 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14216 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14217 &parameter->u.fb_offset))
14218 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14219 else
14220 {
14221 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14222 "for DW_FORM_block* DW_AT_location is supported for "
14223 "DW_TAG_call_site child DIE %s "
14224 "[in module %s]"),
14225 sect_offset_str (child_die->sect_off),
14226 objfile_name (objfile));
14227 continue;
14228 }
14229 }
14230
14231 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14232 if (attr == NULL)
14233 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14234 if (!attr_form_is_block (attr))
14235 {
14236 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14237 "DW_TAG_call_site child DIE %s [in module %s]"),
14238 sect_offset_str (child_die->sect_off),
14239 objfile_name (objfile));
14240 continue;
14241 }
14242 parameter->value = DW_BLOCK (attr)->data;
14243 parameter->value_size = DW_BLOCK (attr)->size;
14244
14245 /* Parameters are not pre-cleared by memset above. */
14246 parameter->data_value = NULL;
14247 parameter->data_value_size = 0;
14248 call_site->parameter_count++;
14249
14250 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14251 if (attr == NULL)
14252 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14253 if (attr != nullptr)
14254 {
14255 if (!attr_form_is_block (attr))
14256 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14257 "DW_TAG_call_site child DIE %s [in module %s]"),
14258 sect_offset_str (child_die->sect_off),
14259 objfile_name (objfile));
14260 else
14261 {
14262 parameter->data_value = DW_BLOCK (attr)->data;
14263 parameter->data_value_size = DW_BLOCK (attr)->size;
14264 }
14265 }
14266 }
14267 }
14268
14269 /* Helper function for read_variable. If DIE represents a virtual
14270 table, then return the type of the concrete object that is
14271 associated with the virtual table. Otherwise, return NULL. */
14272
14273 static struct type *
14274 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14275 {
14276 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14277 if (attr == NULL)
14278 return NULL;
14279
14280 /* Find the type DIE. */
14281 struct die_info *type_die = NULL;
14282 struct dwarf2_cu *type_cu = cu;
14283
14284 if (attr_form_is_ref (attr))
14285 type_die = follow_die_ref (die, attr, &type_cu);
14286 if (type_die == NULL)
14287 return NULL;
14288
14289 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14290 return NULL;
14291 return die_containing_type (type_die, type_cu);
14292 }
14293
14294 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14295
14296 static void
14297 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14298 {
14299 struct rust_vtable_symbol *storage = NULL;
14300
14301 if (cu->language == language_rust)
14302 {
14303 struct type *containing_type = rust_containing_type (die, cu);
14304
14305 if (containing_type != NULL)
14306 {
14307 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14308
14309 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
14310 initialize_objfile_symbol (storage);
14311 storage->concrete_type = containing_type;
14312 storage->subclass = SYMBOL_RUST_VTABLE;
14313 }
14314 }
14315
14316 struct symbol *res = new_symbol (die, NULL, cu, storage);
14317 struct attribute *abstract_origin
14318 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14319 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14320 if (res == NULL && loc && abstract_origin)
14321 {
14322 /* We have a variable without a name, but with a location and an abstract
14323 origin. This may be a concrete instance of an abstract variable
14324 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14325 later. */
14326 struct dwarf2_cu *origin_cu = cu;
14327 struct die_info *origin_die
14328 = follow_die_ref (die, abstract_origin, &origin_cu);
14329 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
14330 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
14331 }
14332 }
14333
14334 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14335 reading .debug_rnglists.
14336 Callback's type should be:
14337 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14338 Return true if the attributes are present and valid, otherwise,
14339 return false. */
14340
14341 template <typename Callback>
14342 static bool
14343 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14344 Callback &&callback)
14345 {
14346 struct dwarf2_per_objfile *dwarf2_per_objfile
14347 = cu->per_cu->dwarf2_per_objfile;
14348 struct objfile *objfile = dwarf2_per_objfile->objfile;
14349 bfd *obfd = objfile->obfd;
14350 /* Base address selection entry. */
14351 CORE_ADDR base;
14352 int found_base;
14353 const gdb_byte *buffer;
14354 CORE_ADDR baseaddr;
14355 bool overflow = false;
14356
14357 found_base = cu->base_known;
14358 base = cu->base_address;
14359
14360 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14361 if (offset >= dwarf2_per_objfile->rnglists.size)
14362 {
14363 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14364 offset);
14365 return false;
14366 }
14367 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14368
14369 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14370
14371 while (1)
14372 {
14373 /* Initialize it due to a false compiler warning. */
14374 CORE_ADDR range_beginning = 0, range_end = 0;
14375 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14376 + dwarf2_per_objfile->rnglists.size);
14377 unsigned int bytes_read;
14378
14379 if (buffer == buf_end)
14380 {
14381 overflow = true;
14382 break;
14383 }
14384 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14385 switch (rlet)
14386 {
14387 case DW_RLE_end_of_list:
14388 break;
14389 case DW_RLE_base_address:
14390 if (buffer + cu->header.addr_size > buf_end)
14391 {
14392 overflow = true;
14393 break;
14394 }
14395 base = read_address (obfd, buffer, cu, &bytes_read);
14396 found_base = 1;
14397 buffer += bytes_read;
14398 break;
14399 case DW_RLE_start_length:
14400 if (buffer + cu->header.addr_size > buf_end)
14401 {
14402 overflow = true;
14403 break;
14404 }
14405 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14406 buffer += bytes_read;
14407 range_end = (range_beginning
14408 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14409 buffer += bytes_read;
14410 if (buffer > buf_end)
14411 {
14412 overflow = true;
14413 break;
14414 }
14415 break;
14416 case DW_RLE_offset_pair:
14417 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14418 buffer += bytes_read;
14419 if (buffer > buf_end)
14420 {
14421 overflow = true;
14422 break;
14423 }
14424 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14425 buffer += bytes_read;
14426 if (buffer > buf_end)
14427 {
14428 overflow = true;
14429 break;
14430 }
14431 break;
14432 case DW_RLE_start_end:
14433 if (buffer + 2 * cu->header.addr_size > buf_end)
14434 {
14435 overflow = true;
14436 break;
14437 }
14438 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14439 buffer += bytes_read;
14440 range_end = read_address (obfd, buffer, cu, &bytes_read);
14441 buffer += bytes_read;
14442 break;
14443 default:
14444 complaint (_("Invalid .debug_rnglists data (no base address)"));
14445 return false;
14446 }
14447 if (rlet == DW_RLE_end_of_list || overflow)
14448 break;
14449 if (rlet == DW_RLE_base_address)
14450 continue;
14451
14452 if (!found_base)
14453 {
14454 /* We have no valid base address for the ranges
14455 data. */
14456 complaint (_("Invalid .debug_rnglists data (no base address)"));
14457 return false;
14458 }
14459
14460 if (range_beginning > range_end)
14461 {
14462 /* Inverted range entries are invalid. */
14463 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14464 return false;
14465 }
14466
14467 /* Empty range entries have no effect. */
14468 if (range_beginning == range_end)
14469 continue;
14470
14471 range_beginning += base;
14472 range_end += base;
14473
14474 /* A not-uncommon case of bad debug info.
14475 Don't pollute the addrmap with bad data. */
14476 if (range_beginning + baseaddr == 0
14477 && !dwarf2_per_objfile->has_section_at_zero)
14478 {
14479 complaint (_(".debug_rnglists entry has start address of zero"
14480 " [in module %s]"), objfile_name (objfile));
14481 continue;
14482 }
14483
14484 callback (range_beginning, range_end);
14485 }
14486
14487 if (overflow)
14488 {
14489 complaint (_("Offset %d is not terminated "
14490 "for DW_AT_ranges attribute"),
14491 offset);
14492 return false;
14493 }
14494
14495 return true;
14496 }
14497
14498 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14499 Callback's type should be:
14500 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14501 Return 1 if the attributes are present and valid, otherwise, return 0. */
14502
14503 template <typename Callback>
14504 static int
14505 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14506 Callback &&callback)
14507 {
14508 struct dwarf2_per_objfile *dwarf2_per_objfile
14509 = cu->per_cu->dwarf2_per_objfile;
14510 struct objfile *objfile = dwarf2_per_objfile->objfile;
14511 struct comp_unit_head *cu_header = &cu->header;
14512 bfd *obfd = objfile->obfd;
14513 unsigned int addr_size = cu_header->addr_size;
14514 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14515 /* Base address selection entry. */
14516 CORE_ADDR base;
14517 int found_base;
14518 unsigned int dummy;
14519 const gdb_byte *buffer;
14520 CORE_ADDR baseaddr;
14521
14522 if (cu_header->version >= 5)
14523 return dwarf2_rnglists_process (offset, cu, callback);
14524
14525 found_base = cu->base_known;
14526 base = cu->base_address;
14527
14528 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14529 if (offset >= dwarf2_per_objfile->ranges.size)
14530 {
14531 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14532 offset);
14533 return 0;
14534 }
14535 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14536
14537 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14538
14539 while (1)
14540 {
14541 CORE_ADDR range_beginning, range_end;
14542
14543 range_beginning = read_address (obfd, buffer, cu, &dummy);
14544 buffer += addr_size;
14545 range_end = read_address (obfd, buffer, cu, &dummy);
14546 buffer += addr_size;
14547 offset += 2 * addr_size;
14548
14549 /* An end of list marker is a pair of zero addresses. */
14550 if (range_beginning == 0 && range_end == 0)
14551 /* Found the end of list entry. */
14552 break;
14553
14554 /* Each base address selection entry is a pair of 2 values.
14555 The first is the largest possible address, the second is
14556 the base address. Check for a base address here. */
14557 if ((range_beginning & mask) == mask)
14558 {
14559 /* If we found the largest possible address, then we already
14560 have the base address in range_end. */
14561 base = range_end;
14562 found_base = 1;
14563 continue;
14564 }
14565
14566 if (!found_base)
14567 {
14568 /* We have no valid base address for the ranges
14569 data. */
14570 complaint (_("Invalid .debug_ranges data (no base address)"));
14571 return 0;
14572 }
14573
14574 if (range_beginning > range_end)
14575 {
14576 /* Inverted range entries are invalid. */
14577 complaint (_("Invalid .debug_ranges data (inverted range)"));
14578 return 0;
14579 }
14580
14581 /* Empty range entries have no effect. */
14582 if (range_beginning == range_end)
14583 continue;
14584
14585 range_beginning += base;
14586 range_end += base;
14587
14588 /* A not-uncommon case of bad debug info.
14589 Don't pollute the addrmap with bad data. */
14590 if (range_beginning + baseaddr == 0
14591 && !dwarf2_per_objfile->has_section_at_zero)
14592 {
14593 complaint (_(".debug_ranges entry has start address of zero"
14594 " [in module %s]"), objfile_name (objfile));
14595 continue;
14596 }
14597
14598 callback (range_beginning, range_end);
14599 }
14600
14601 return 1;
14602 }
14603
14604 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14605 Return 1 if the attributes are present and valid, otherwise, return 0.
14606 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14607
14608 static int
14609 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14610 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14611 struct partial_symtab *ranges_pst)
14612 {
14613 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14614 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14615 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14616 SECT_OFF_TEXT (objfile));
14617 int low_set = 0;
14618 CORE_ADDR low = 0;
14619 CORE_ADDR high = 0;
14620 int retval;
14621
14622 retval = dwarf2_ranges_process (offset, cu,
14623 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14624 {
14625 if (ranges_pst != NULL)
14626 {
14627 CORE_ADDR lowpc;
14628 CORE_ADDR highpc;
14629
14630 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14631 range_beginning + baseaddr)
14632 - baseaddr);
14633 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14634 range_end + baseaddr)
14635 - baseaddr);
14636 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14637 lowpc, highpc - 1, ranges_pst);
14638 }
14639
14640 /* FIXME: This is recording everything as a low-high
14641 segment of consecutive addresses. We should have a
14642 data structure for discontiguous block ranges
14643 instead. */
14644 if (! low_set)
14645 {
14646 low = range_beginning;
14647 high = range_end;
14648 low_set = 1;
14649 }
14650 else
14651 {
14652 if (range_beginning < low)
14653 low = range_beginning;
14654 if (range_end > high)
14655 high = range_end;
14656 }
14657 });
14658 if (!retval)
14659 return 0;
14660
14661 if (! low_set)
14662 /* If the first entry is an end-of-list marker, the range
14663 describes an empty scope, i.e. no instructions. */
14664 return 0;
14665
14666 if (low_return)
14667 *low_return = low;
14668 if (high_return)
14669 *high_return = high;
14670 return 1;
14671 }
14672
14673 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14674 definition for the return value. *LOWPC and *HIGHPC are set iff
14675 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14676
14677 static enum pc_bounds_kind
14678 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14679 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14680 struct partial_symtab *pst)
14681 {
14682 struct dwarf2_per_objfile *dwarf2_per_objfile
14683 = cu->per_cu->dwarf2_per_objfile;
14684 struct attribute *attr;
14685 struct attribute *attr_high;
14686 CORE_ADDR low = 0;
14687 CORE_ADDR high = 0;
14688 enum pc_bounds_kind ret;
14689
14690 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14691 if (attr_high)
14692 {
14693 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14694 if (attr != nullptr)
14695 {
14696 low = attr_value_as_address (attr);
14697 high = attr_value_as_address (attr_high);
14698 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14699 high += low;
14700 }
14701 else
14702 /* Found high w/o low attribute. */
14703 return PC_BOUNDS_INVALID;
14704
14705 /* Found consecutive range of addresses. */
14706 ret = PC_BOUNDS_HIGH_LOW;
14707 }
14708 else
14709 {
14710 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14711 if (attr != NULL)
14712 {
14713 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14714 We take advantage of the fact that DW_AT_ranges does not appear
14715 in DW_TAG_compile_unit of DWO files. */
14716 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14717 unsigned int ranges_offset = (DW_UNSND (attr)
14718 + (need_ranges_base
14719 ? cu->ranges_base
14720 : 0));
14721
14722 /* Value of the DW_AT_ranges attribute is the offset in the
14723 .debug_ranges section. */
14724 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14725 return PC_BOUNDS_INVALID;
14726 /* Found discontinuous range of addresses. */
14727 ret = PC_BOUNDS_RANGES;
14728 }
14729 else
14730 return PC_BOUNDS_NOT_PRESENT;
14731 }
14732
14733 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14734 if (high <= low)
14735 return PC_BOUNDS_INVALID;
14736
14737 /* When using the GNU linker, .gnu.linkonce. sections are used to
14738 eliminate duplicate copies of functions and vtables and such.
14739 The linker will arbitrarily choose one and discard the others.
14740 The AT_*_pc values for such functions refer to local labels in
14741 these sections. If the section from that file was discarded, the
14742 labels are not in the output, so the relocs get a value of 0.
14743 If this is a discarded function, mark the pc bounds as invalid,
14744 so that GDB will ignore it. */
14745 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14746 return PC_BOUNDS_INVALID;
14747
14748 *lowpc = low;
14749 if (highpc)
14750 *highpc = high;
14751 return ret;
14752 }
14753
14754 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14755 its low and high PC addresses. Do nothing if these addresses could not
14756 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14757 and HIGHPC to the high address if greater than HIGHPC. */
14758
14759 static void
14760 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14761 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14762 struct dwarf2_cu *cu)
14763 {
14764 CORE_ADDR low, high;
14765 struct die_info *child = die->child;
14766
14767 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14768 {
14769 *lowpc = std::min (*lowpc, low);
14770 *highpc = std::max (*highpc, high);
14771 }
14772
14773 /* If the language does not allow nested subprograms (either inside
14774 subprograms or lexical blocks), we're done. */
14775 if (cu->language != language_ada)
14776 return;
14777
14778 /* Check all the children of the given DIE. If it contains nested
14779 subprograms, then check their pc bounds. Likewise, we need to
14780 check lexical blocks as well, as they may also contain subprogram
14781 definitions. */
14782 while (child && child->tag)
14783 {
14784 if (child->tag == DW_TAG_subprogram
14785 || child->tag == DW_TAG_lexical_block)
14786 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14787 child = sibling_die (child);
14788 }
14789 }
14790
14791 /* Get the low and high pc's represented by the scope DIE, and store
14792 them in *LOWPC and *HIGHPC. If the correct values can't be
14793 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14794
14795 static void
14796 get_scope_pc_bounds (struct die_info *die,
14797 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14798 struct dwarf2_cu *cu)
14799 {
14800 CORE_ADDR best_low = (CORE_ADDR) -1;
14801 CORE_ADDR best_high = (CORE_ADDR) 0;
14802 CORE_ADDR current_low, current_high;
14803
14804 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14805 >= PC_BOUNDS_RANGES)
14806 {
14807 best_low = current_low;
14808 best_high = current_high;
14809 }
14810 else
14811 {
14812 struct die_info *child = die->child;
14813
14814 while (child && child->tag)
14815 {
14816 switch (child->tag) {
14817 case DW_TAG_subprogram:
14818 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14819 break;
14820 case DW_TAG_namespace:
14821 case DW_TAG_module:
14822 /* FIXME: carlton/2004-01-16: Should we do this for
14823 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14824 that current GCC's always emit the DIEs corresponding
14825 to definitions of methods of classes as children of a
14826 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14827 the DIEs giving the declarations, which could be
14828 anywhere). But I don't see any reason why the
14829 standards says that they have to be there. */
14830 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14831
14832 if (current_low != ((CORE_ADDR) -1))
14833 {
14834 best_low = std::min (best_low, current_low);
14835 best_high = std::max (best_high, current_high);
14836 }
14837 break;
14838 default:
14839 /* Ignore. */
14840 break;
14841 }
14842
14843 child = sibling_die (child);
14844 }
14845 }
14846
14847 *lowpc = best_low;
14848 *highpc = best_high;
14849 }
14850
14851 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14852 in DIE. */
14853
14854 static void
14855 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14856 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14857 {
14858 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14859 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14860 struct attribute *attr;
14861 struct attribute *attr_high;
14862
14863 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14864 if (attr_high)
14865 {
14866 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14867 if (attr != nullptr)
14868 {
14869 CORE_ADDR low = attr_value_as_address (attr);
14870 CORE_ADDR high = attr_value_as_address (attr_high);
14871
14872 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14873 high += low;
14874
14875 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14876 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14877 cu->get_builder ()->record_block_range (block, low, high - 1);
14878 }
14879 }
14880
14881 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14882 if (attr != nullptr)
14883 {
14884 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14885 We take advantage of the fact that DW_AT_ranges does not appear
14886 in DW_TAG_compile_unit of DWO files. */
14887 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14888
14889 /* The value of the DW_AT_ranges attribute is the offset of the
14890 address range list in the .debug_ranges section. */
14891 unsigned long offset = (DW_UNSND (attr)
14892 + (need_ranges_base ? cu->ranges_base : 0));
14893
14894 std::vector<blockrange> blockvec;
14895 dwarf2_ranges_process (offset, cu,
14896 [&] (CORE_ADDR start, CORE_ADDR end)
14897 {
14898 start += baseaddr;
14899 end += baseaddr;
14900 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14901 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14902 cu->get_builder ()->record_block_range (block, start, end - 1);
14903 blockvec.emplace_back (start, end);
14904 });
14905
14906 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14907 }
14908 }
14909
14910 /* Check whether the producer field indicates either of GCC < 4.6, or the
14911 Intel C/C++ compiler, and cache the result in CU. */
14912
14913 static void
14914 check_producer (struct dwarf2_cu *cu)
14915 {
14916 int major, minor;
14917
14918 if (cu->producer == NULL)
14919 {
14920 /* For unknown compilers expect their behavior is DWARF version
14921 compliant.
14922
14923 GCC started to support .debug_types sections by -gdwarf-4 since
14924 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14925 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14926 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14927 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14928 }
14929 else if (producer_is_gcc (cu->producer, &major, &minor))
14930 {
14931 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14932 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14933 }
14934 else if (producer_is_icc (cu->producer, &major, &minor))
14935 {
14936 cu->producer_is_icc = true;
14937 cu->producer_is_icc_lt_14 = major < 14;
14938 }
14939 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14940 cu->producer_is_codewarrior = true;
14941 else
14942 {
14943 /* For other non-GCC compilers, expect their behavior is DWARF version
14944 compliant. */
14945 }
14946
14947 cu->checked_producer = true;
14948 }
14949
14950 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14951 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14952 during 4.6.0 experimental. */
14953
14954 static bool
14955 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14956 {
14957 if (!cu->checked_producer)
14958 check_producer (cu);
14959
14960 return cu->producer_is_gxx_lt_4_6;
14961 }
14962
14963
14964 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14965 with incorrect is_stmt attributes. */
14966
14967 static bool
14968 producer_is_codewarrior (struct dwarf2_cu *cu)
14969 {
14970 if (!cu->checked_producer)
14971 check_producer (cu);
14972
14973 return cu->producer_is_codewarrior;
14974 }
14975
14976 /* Return the default accessibility type if it is not overridden by
14977 DW_AT_accessibility. */
14978
14979 static enum dwarf_access_attribute
14980 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14981 {
14982 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14983 {
14984 /* The default DWARF 2 accessibility for members is public, the default
14985 accessibility for inheritance is private. */
14986
14987 if (die->tag != DW_TAG_inheritance)
14988 return DW_ACCESS_public;
14989 else
14990 return DW_ACCESS_private;
14991 }
14992 else
14993 {
14994 /* DWARF 3+ defines the default accessibility a different way. The same
14995 rules apply now for DW_TAG_inheritance as for the members and it only
14996 depends on the container kind. */
14997
14998 if (die->parent->tag == DW_TAG_class_type)
14999 return DW_ACCESS_private;
15000 else
15001 return DW_ACCESS_public;
15002 }
15003 }
15004
15005 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15006 offset. If the attribute was not found return 0, otherwise return
15007 1. If it was found but could not properly be handled, set *OFFSET
15008 to 0. */
15009
15010 static int
15011 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15012 LONGEST *offset)
15013 {
15014 struct attribute *attr;
15015
15016 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15017 if (attr != NULL)
15018 {
15019 *offset = 0;
15020
15021 /* Note that we do not check for a section offset first here.
15022 This is because DW_AT_data_member_location is new in DWARF 4,
15023 so if we see it, we can assume that a constant form is really
15024 a constant and not a section offset. */
15025 if (attr_form_is_constant (attr))
15026 *offset = dwarf2_get_attr_constant_value (attr, 0);
15027 else if (attr_form_is_section_offset (attr))
15028 dwarf2_complex_location_expr_complaint ();
15029 else if (attr_form_is_block (attr))
15030 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15031 else
15032 dwarf2_complex_location_expr_complaint ();
15033
15034 return 1;
15035 }
15036
15037 return 0;
15038 }
15039
15040 /* Add an aggregate field to the field list. */
15041
15042 static void
15043 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15044 struct dwarf2_cu *cu)
15045 {
15046 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15047 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15048 struct nextfield *new_field;
15049 struct attribute *attr;
15050 struct field *fp;
15051 const char *fieldname = "";
15052
15053 if (die->tag == DW_TAG_inheritance)
15054 {
15055 fip->baseclasses.emplace_back ();
15056 new_field = &fip->baseclasses.back ();
15057 }
15058 else
15059 {
15060 fip->fields.emplace_back ();
15061 new_field = &fip->fields.back ();
15062 }
15063
15064 fip->nfields++;
15065
15066 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15067 if (attr != nullptr)
15068 new_field->accessibility = DW_UNSND (attr);
15069 else
15070 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15071 if (new_field->accessibility != DW_ACCESS_public)
15072 fip->non_public_fields = 1;
15073
15074 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15075 if (attr != nullptr)
15076 new_field->virtuality = DW_UNSND (attr);
15077 else
15078 new_field->virtuality = DW_VIRTUALITY_none;
15079
15080 fp = &new_field->field;
15081
15082 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15083 {
15084 LONGEST offset;
15085
15086 /* Data member other than a C++ static data member. */
15087
15088 /* Get type of field. */
15089 fp->type = die_type (die, cu);
15090
15091 SET_FIELD_BITPOS (*fp, 0);
15092
15093 /* Get bit size of field (zero if none). */
15094 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15095 if (attr != nullptr)
15096 {
15097 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15098 }
15099 else
15100 {
15101 FIELD_BITSIZE (*fp) = 0;
15102 }
15103
15104 /* Get bit offset of field. */
15105 if (handle_data_member_location (die, cu, &offset))
15106 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15107 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15108 if (attr != nullptr)
15109 {
15110 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15111 {
15112 /* For big endian bits, the DW_AT_bit_offset gives the
15113 additional bit offset from the MSB of the containing
15114 anonymous object to the MSB of the field. We don't
15115 have to do anything special since we don't need to
15116 know the size of the anonymous object. */
15117 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15118 }
15119 else
15120 {
15121 /* For little endian bits, compute the bit offset to the
15122 MSB of the anonymous object, subtract off the number of
15123 bits from the MSB of the field to the MSB of the
15124 object, and then subtract off the number of bits of
15125 the field itself. The result is the bit offset of
15126 the LSB of the field. */
15127 int anonymous_size;
15128 int bit_offset = DW_UNSND (attr);
15129
15130 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15131 if (attr != nullptr)
15132 {
15133 /* The size of the anonymous object containing
15134 the bit field is explicit, so use the
15135 indicated size (in bytes). */
15136 anonymous_size = DW_UNSND (attr);
15137 }
15138 else
15139 {
15140 /* The size of the anonymous object containing
15141 the bit field must be inferred from the type
15142 attribute of the data member containing the
15143 bit field. */
15144 anonymous_size = TYPE_LENGTH (fp->type);
15145 }
15146 SET_FIELD_BITPOS (*fp,
15147 (FIELD_BITPOS (*fp)
15148 + anonymous_size * bits_per_byte
15149 - bit_offset - FIELD_BITSIZE (*fp)));
15150 }
15151 }
15152 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15153 if (attr != NULL)
15154 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15155 + dwarf2_get_attr_constant_value (attr, 0)));
15156
15157 /* Get name of field. */
15158 fieldname = dwarf2_name (die, cu);
15159 if (fieldname == NULL)
15160 fieldname = "";
15161
15162 /* The name is already allocated along with this objfile, so we don't
15163 need to duplicate it for the type. */
15164 fp->name = fieldname;
15165
15166 /* Change accessibility for artificial fields (e.g. virtual table
15167 pointer or virtual base class pointer) to private. */
15168 if (dwarf2_attr (die, DW_AT_artificial, cu))
15169 {
15170 FIELD_ARTIFICIAL (*fp) = 1;
15171 new_field->accessibility = DW_ACCESS_private;
15172 fip->non_public_fields = 1;
15173 }
15174 }
15175 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15176 {
15177 /* C++ static member. */
15178
15179 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15180 is a declaration, but all versions of G++ as of this writing
15181 (so through at least 3.2.1) incorrectly generate
15182 DW_TAG_variable tags. */
15183
15184 const char *physname;
15185
15186 /* Get name of field. */
15187 fieldname = dwarf2_name (die, cu);
15188 if (fieldname == NULL)
15189 return;
15190
15191 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15192 if (attr
15193 /* Only create a symbol if this is an external value.
15194 new_symbol checks this and puts the value in the global symbol
15195 table, which we want. If it is not external, new_symbol
15196 will try to put the value in cu->list_in_scope which is wrong. */
15197 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15198 {
15199 /* A static const member, not much different than an enum as far as
15200 we're concerned, except that we can support more types. */
15201 new_symbol (die, NULL, cu);
15202 }
15203
15204 /* Get physical name. */
15205 physname = dwarf2_physname (fieldname, die, cu);
15206
15207 /* The name is already allocated along with this objfile, so we don't
15208 need to duplicate it for the type. */
15209 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15210 FIELD_TYPE (*fp) = die_type (die, cu);
15211 FIELD_NAME (*fp) = fieldname;
15212 }
15213 else if (die->tag == DW_TAG_inheritance)
15214 {
15215 LONGEST offset;
15216
15217 /* C++ base class field. */
15218 if (handle_data_member_location (die, cu, &offset))
15219 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15220 FIELD_BITSIZE (*fp) = 0;
15221 FIELD_TYPE (*fp) = die_type (die, cu);
15222 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
15223 }
15224 else if (die->tag == DW_TAG_variant_part)
15225 {
15226 /* process_structure_scope will treat this DIE as a union. */
15227 process_structure_scope (die, cu);
15228
15229 /* The variant part is relative to the start of the enclosing
15230 structure. */
15231 SET_FIELD_BITPOS (*fp, 0);
15232 fp->type = get_die_type (die, cu);
15233 fp->artificial = 1;
15234 fp->name = "<<variant>>";
15235
15236 /* Normally a DW_TAG_variant_part won't have a size, but our
15237 representation requires one, so set it to the maximum of the
15238 child sizes, being sure to account for the offset at which
15239 each child is seen. */
15240 if (TYPE_LENGTH (fp->type) == 0)
15241 {
15242 unsigned max = 0;
15243 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15244 {
15245 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
15246 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
15247 if (len > max)
15248 max = len;
15249 }
15250 TYPE_LENGTH (fp->type) = max;
15251 }
15252 }
15253 else
15254 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15255 }
15256
15257 /* Can the type given by DIE define another type? */
15258
15259 static bool
15260 type_can_define_types (const struct die_info *die)
15261 {
15262 switch (die->tag)
15263 {
15264 case DW_TAG_typedef:
15265 case DW_TAG_class_type:
15266 case DW_TAG_structure_type:
15267 case DW_TAG_union_type:
15268 case DW_TAG_enumeration_type:
15269 return true;
15270
15271 default:
15272 return false;
15273 }
15274 }
15275
15276 /* Add a type definition defined in the scope of the FIP's class. */
15277
15278 static void
15279 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15280 struct dwarf2_cu *cu)
15281 {
15282 struct decl_field fp;
15283 memset (&fp, 0, sizeof (fp));
15284
15285 gdb_assert (type_can_define_types (die));
15286
15287 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15288 fp.name = dwarf2_name (die, cu);
15289 fp.type = read_type_die (die, cu);
15290
15291 /* Save accessibility. */
15292 enum dwarf_access_attribute accessibility;
15293 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15294 if (attr != NULL)
15295 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15296 else
15297 accessibility = dwarf2_default_access_attribute (die, cu);
15298 switch (accessibility)
15299 {
15300 case DW_ACCESS_public:
15301 /* The assumed value if neither private nor protected. */
15302 break;
15303 case DW_ACCESS_private:
15304 fp.is_private = 1;
15305 break;
15306 case DW_ACCESS_protected:
15307 fp.is_protected = 1;
15308 break;
15309 default:
15310 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15311 }
15312
15313 if (die->tag == DW_TAG_typedef)
15314 fip->typedef_field_list.push_back (fp);
15315 else
15316 fip->nested_types_list.push_back (fp);
15317 }
15318
15319 /* Create the vector of fields, and attach it to the type. */
15320
15321 static void
15322 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15323 struct dwarf2_cu *cu)
15324 {
15325 int nfields = fip->nfields;
15326
15327 /* Record the field count, allocate space for the array of fields,
15328 and create blank accessibility bitfields if necessary. */
15329 TYPE_NFIELDS (type) = nfields;
15330 TYPE_FIELDS (type) = (struct field *)
15331 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15332
15333 if (fip->non_public_fields && cu->language != language_ada)
15334 {
15335 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15336
15337 TYPE_FIELD_PRIVATE_BITS (type) =
15338 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15339 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15340
15341 TYPE_FIELD_PROTECTED_BITS (type) =
15342 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15343 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15344
15345 TYPE_FIELD_IGNORE_BITS (type) =
15346 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15347 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15348 }
15349
15350 /* If the type has baseclasses, allocate and clear a bit vector for
15351 TYPE_FIELD_VIRTUAL_BITS. */
15352 if (!fip->baseclasses.empty () && cu->language != language_ada)
15353 {
15354 int num_bytes = B_BYTES (fip->baseclasses.size ());
15355 unsigned char *pointer;
15356
15357 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15358 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15359 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15360 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15361 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15362 }
15363
15364 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15365 {
15366 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15367
15368 for (int index = 0; index < nfields; ++index)
15369 {
15370 struct nextfield &field = fip->fields[index];
15371
15372 if (field.variant.is_discriminant)
15373 di->discriminant_index = index;
15374 else if (field.variant.default_branch)
15375 di->default_index = index;
15376 else
15377 di->discriminants[index] = field.variant.discriminant_value;
15378 }
15379 }
15380
15381 /* Copy the saved-up fields into the field vector. */
15382 for (int i = 0; i < nfields; ++i)
15383 {
15384 struct nextfield &field
15385 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15386 : fip->fields[i - fip->baseclasses.size ()]);
15387
15388 TYPE_FIELD (type, i) = field.field;
15389 switch (field.accessibility)
15390 {
15391 case DW_ACCESS_private:
15392 if (cu->language != language_ada)
15393 SET_TYPE_FIELD_PRIVATE (type, i);
15394 break;
15395
15396 case DW_ACCESS_protected:
15397 if (cu->language != language_ada)
15398 SET_TYPE_FIELD_PROTECTED (type, i);
15399 break;
15400
15401 case DW_ACCESS_public:
15402 break;
15403
15404 default:
15405 /* Unknown accessibility. Complain and treat it as public. */
15406 {
15407 complaint (_("unsupported accessibility %d"),
15408 field.accessibility);
15409 }
15410 break;
15411 }
15412 if (i < fip->baseclasses.size ())
15413 {
15414 switch (field.virtuality)
15415 {
15416 case DW_VIRTUALITY_virtual:
15417 case DW_VIRTUALITY_pure_virtual:
15418 if (cu->language == language_ada)
15419 error (_("unexpected virtuality in component of Ada type"));
15420 SET_TYPE_FIELD_VIRTUAL (type, i);
15421 break;
15422 }
15423 }
15424 }
15425 }
15426
15427 /* Return true if this member function is a constructor, false
15428 otherwise. */
15429
15430 static int
15431 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15432 {
15433 const char *fieldname;
15434 const char *type_name;
15435 int len;
15436
15437 if (die->parent == NULL)
15438 return 0;
15439
15440 if (die->parent->tag != DW_TAG_structure_type
15441 && die->parent->tag != DW_TAG_union_type
15442 && die->parent->tag != DW_TAG_class_type)
15443 return 0;
15444
15445 fieldname = dwarf2_name (die, cu);
15446 type_name = dwarf2_name (die->parent, cu);
15447 if (fieldname == NULL || type_name == NULL)
15448 return 0;
15449
15450 len = strlen (fieldname);
15451 return (strncmp (fieldname, type_name, len) == 0
15452 && (type_name[len] == '\0' || type_name[len] == '<'));
15453 }
15454
15455 /* Check if the given VALUE is a recognized enum
15456 dwarf_defaulted_attribute constant according to DWARF5 spec,
15457 Table 7.24. */
15458
15459 static bool
15460 is_valid_DW_AT_defaulted (ULONGEST value)
15461 {
15462 switch (value)
15463 {
15464 case DW_DEFAULTED_no:
15465 case DW_DEFAULTED_in_class:
15466 case DW_DEFAULTED_out_of_class:
15467 return true;
15468 }
15469
15470 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15471 return false;
15472 }
15473
15474 /* Add a member function to the proper fieldlist. */
15475
15476 static void
15477 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15478 struct type *type, struct dwarf2_cu *cu)
15479 {
15480 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15481 struct attribute *attr;
15482 int i;
15483 struct fnfieldlist *flp = nullptr;
15484 struct fn_field *fnp;
15485 const char *fieldname;
15486 struct type *this_type;
15487 enum dwarf_access_attribute accessibility;
15488
15489 if (cu->language == language_ada)
15490 error (_("unexpected member function in Ada type"));
15491
15492 /* Get name of member function. */
15493 fieldname = dwarf2_name (die, cu);
15494 if (fieldname == NULL)
15495 return;
15496
15497 /* Look up member function name in fieldlist. */
15498 for (i = 0; i < fip->fnfieldlists.size (); i++)
15499 {
15500 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15501 {
15502 flp = &fip->fnfieldlists[i];
15503 break;
15504 }
15505 }
15506
15507 /* Create a new fnfieldlist if necessary. */
15508 if (flp == nullptr)
15509 {
15510 fip->fnfieldlists.emplace_back ();
15511 flp = &fip->fnfieldlists.back ();
15512 flp->name = fieldname;
15513 i = fip->fnfieldlists.size () - 1;
15514 }
15515
15516 /* Create a new member function field and add it to the vector of
15517 fnfieldlists. */
15518 flp->fnfields.emplace_back ();
15519 fnp = &flp->fnfields.back ();
15520
15521 /* Delay processing of the physname until later. */
15522 if (cu->language == language_cplus)
15523 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15524 die, cu);
15525 else
15526 {
15527 const char *physname = dwarf2_physname (fieldname, die, cu);
15528 fnp->physname = physname ? physname : "";
15529 }
15530
15531 fnp->type = alloc_type (objfile);
15532 this_type = read_type_die (die, cu);
15533 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15534 {
15535 int nparams = TYPE_NFIELDS (this_type);
15536
15537 /* TYPE is the domain of this method, and THIS_TYPE is the type
15538 of the method itself (TYPE_CODE_METHOD). */
15539 smash_to_method_type (fnp->type, type,
15540 TYPE_TARGET_TYPE (this_type),
15541 TYPE_FIELDS (this_type),
15542 TYPE_NFIELDS (this_type),
15543 TYPE_VARARGS (this_type));
15544
15545 /* Handle static member functions.
15546 Dwarf2 has no clean way to discern C++ static and non-static
15547 member functions. G++ helps GDB by marking the first
15548 parameter for non-static member functions (which is the this
15549 pointer) as artificial. We obtain this information from
15550 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15551 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15552 fnp->voffset = VOFFSET_STATIC;
15553 }
15554 else
15555 complaint (_("member function type missing for '%s'"),
15556 dwarf2_full_name (fieldname, die, cu));
15557
15558 /* Get fcontext from DW_AT_containing_type if present. */
15559 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15560 fnp->fcontext = die_containing_type (die, cu);
15561
15562 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15563 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15564
15565 /* Get accessibility. */
15566 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15567 if (attr != nullptr)
15568 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15569 else
15570 accessibility = dwarf2_default_access_attribute (die, cu);
15571 switch (accessibility)
15572 {
15573 case DW_ACCESS_private:
15574 fnp->is_private = 1;
15575 break;
15576 case DW_ACCESS_protected:
15577 fnp->is_protected = 1;
15578 break;
15579 }
15580
15581 /* Check for artificial methods. */
15582 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15583 if (attr && DW_UNSND (attr) != 0)
15584 fnp->is_artificial = 1;
15585
15586 /* Check for defaulted methods. */
15587 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15588 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15589 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15590
15591 /* Check for deleted methods. */
15592 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15593 if (attr != nullptr && DW_UNSND (attr) != 0)
15594 fnp->is_deleted = 1;
15595
15596 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15597
15598 /* Get index in virtual function table if it is a virtual member
15599 function. For older versions of GCC, this is an offset in the
15600 appropriate virtual table, as specified by DW_AT_containing_type.
15601 For everyone else, it is an expression to be evaluated relative
15602 to the object address. */
15603
15604 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15605 if (attr != nullptr)
15606 {
15607 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15608 {
15609 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15610 {
15611 /* Old-style GCC. */
15612 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15613 }
15614 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15615 || (DW_BLOCK (attr)->size > 1
15616 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15617 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15618 {
15619 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15620 if ((fnp->voffset % cu->header.addr_size) != 0)
15621 dwarf2_complex_location_expr_complaint ();
15622 else
15623 fnp->voffset /= cu->header.addr_size;
15624 fnp->voffset += 2;
15625 }
15626 else
15627 dwarf2_complex_location_expr_complaint ();
15628
15629 if (!fnp->fcontext)
15630 {
15631 /* If there is no `this' field and no DW_AT_containing_type,
15632 we cannot actually find a base class context for the
15633 vtable! */
15634 if (TYPE_NFIELDS (this_type) == 0
15635 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15636 {
15637 complaint (_("cannot determine context for virtual member "
15638 "function \"%s\" (offset %s)"),
15639 fieldname, sect_offset_str (die->sect_off));
15640 }
15641 else
15642 {
15643 fnp->fcontext
15644 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15645 }
15646 }
15647 }
15648 else if (attr_form_is_section_offset (attr))
15649 {
15650 dwarf2_complex_location_expr_complaint ();
15651 }
15652 else
15653 {
15654 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15655 fieldname);
15656 }
15657 }
15658 else
15659 {
15660 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15661 if (attr && DW_UNSND (attr))
15662 {
15663 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15664 complaint (_("Member function \"%s\" (offset %s) is virtual "
15665 "but the vtable offset is not specified"),
15666 fieldname, sect_offset_str (die->sect_off));
15667 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15668 TYPE_CPLUS_DYNAMIC (type) = 1;
15669 }
15670 }
15671 }
15672
15673 /* Create the vector of member function fields, and attach it to the type. */
15674
15675 static void
15676 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15677 struct dwarf2_cu *cu)
15678 {
15679 if (cu->language == language_ada)
15680 error (_("unexpected member functions in Ada type"));
15681
15682 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15683 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15684 TYPE_ALLOC (type,
15685 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15686
15687 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15688 {
15689 struct fnfieldlist &nf = fip->fnfieldlists[i];
15690 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15691
15692 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15693 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15694 fn_flp->fn_fields = (struct fn_field *)
15695 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15696
15697 for (int k = 0; k < nf.fnfields.size (); ++k)
15698 fn_flp->fn_fields[k] = nf.fnfields[k];
15699 }
15700
15701 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15702 }
15703
15704 /* Returns non-zero if NAME is the name of a vtable member in CU's
15705 language, zero otherwise. */
15706 static int
15707 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15708 {
15709 static const char vptr[] = "_vptr";
15710
15711 /* Look for the C++ form of the vtable. */
15712 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15713 return 1;
15714
15715 return 0;
15716 }
15717
15718 /* GCC outputs unnamed structures that are really pointers to member
15719 functions, with the ABI-specified layout. If TYPE describes
15720 such a structure, smash it into a member function type.
15721
15722 GCC shouldn't do this; it should just output pointer to member DIEs.
15723 This is GCC PR debug/28767. */
15724
15725 static void
15726 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15727 {
15728 struct type *pfn_type, *self_type, *new_type;
15729
15730 /* Check for a structure with no name and two children. */
15731 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15732 return;
15733
15734 /* Check for __pfn and __delta members. */
15735 if (TYPE_FIELD_NAME (type, 0) == NULL
15736 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15737 || TYPE_FIELD_NAME (type, 1) == NULL
15738 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15739 return;
15740
15741 /* Find the type of the method. */
15742 pfn_type = TYPE_FIELD_TYPE (type, 0);
15743 if (pfn_type == NULL
15744 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15745 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15746 return;
15747
15748 /* Look for the "this" argument. */
15749 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15750 if (TYPE_NFIELDS (pfn_type) == 0
15751 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15752 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15753 return;
15754
15755 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15756 new_type = alloc_type (objfile);
15757 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15758 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15759 TYPE_VARARGS (pfn_type));
15760 smash_to_methodptr_type (type, new_type);
15761 }
15762
15763 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15764 appropriate error checking and issuing complaints if there is a
15765 problem. */
15766
15767 static ULONGEST
15768 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15769 {
15770 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15771
15772 if (attr == nullptr)
15773 return 0;
15774
15775 if (!attr_form_is_constant (attr))
15776 {
15777 complaint (_("DW_AT_alignment must have constant form"
15778 " - DIE at %s [in module %s]"),
15779 sect_offset_str (die->sect_off),
15780 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15781 return 0;
15782 }
15783
15784 ULONGEST align;
15785 if (attr->form == DW_FORM_sdata)
15786 {
15787 LONGEST val = DW_SND (attr);
15788 if (val < 0)
15789 {
15790 complaint (_("DW_AT_alignment value must not be negative"
15791 " - DIE at %s [in module %s]"),
15792 sect_offset_str (die->sect_off),
15793 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15794 return 0;
15795 }
15796 align = val;
15797 }
15798 else
15799 align = DW_UNSND (attr);
15800
15801 if (align == 0)
15802 {
15803 complaint (_("DW_AT_alignment value must not be zero"
15804 " - DIE at %s [in module %s]"),
15805 sect_offset_str (die->sect_off),
15806 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15807 return 0;
15808 }
15809 if ((align & (align - 1)) != 0)
15810 {
15811 complaint (_("DW_AT_alignment value must be a power of 2"
15812 " - DIE at %s [in module %s]"),
15813 sect_offset_str (die->sect_off),
15814 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15815 return 0;
15816 }
15817
15818 return align;
15819 }
15820
15821 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15822 the alignment for TYPE. */
15823
15824 static void
15825 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15826 struct type *type)
15827 {
15828 if (!set_type_align (type, get_alignment (cu, die)))
15829 complaint (_("DW_AT_alignment value too large"
15830 " - DIE at %s [in module %s]"),
15831 sect_offset_str (die->sect_off),
15832 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15833 }
15834
15835 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15836 constant for a type, according to DWARF5 spec, Table 5.5. */
15837
15838 static bool
15839 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15840 {
15841 switch (value)
15842 {
15843 case DW_CC_normal:
15844 case DW_CC_pass_by_reference:
15845 case DW_CC_pass_by_value:
15846 return true;
15847
15848 default:
15849 complaint (_("unrecognized DW_AT_calling_convention value "
15850 "(%s) for a type"), pulongest (value));
15851 return false;
15852 }
15853 }
15854
15855 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15856 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15857 also according to GNU-specific values (see include/dwarf2.h). */
15858
15859 static bool
15860 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15861 {
15862 switch (value)
15863 {
15864 case DW_CC_normal:
15865 case DW_CC_program:
15866 case DW_CC_nocall:
15867 return true;
15868
15869 case DW_CC_GNU_renesas_sh:
15870 case DW_CC_GNU_borland_fastcall_i386:
15871 case DW_CC_GDB_IBM_OpenCL:
15872 return true;
15873
15874 default:
15875 complaint (_("unrecognized DW_AT_calling_convention value "
15876 "(%s) for a subroutine"), pulongest (value));
15877 return false;
15878 }
15879 }
15880
15881 /* Called when we find the DIE that starts a structure or union scope
15882 (definition) to create a type for the structure or union. Fill in
15883 the type's name and general properties; the members will not be
15884 processed until process_structure_scope. A symbol table entry for
15885 the type will also not be done until process_structure_scope (assuming
15886 the type has a name).
15887
15888 NOTE: we need to call these functions regardless of whether or not the
15889 DIE has a DW_AT_name attribute, since it might be an anonymous
15890 structure or union. This gets the type entered into our set of
15891 user defined types. */
15892
15893 static struct type *
15894 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15895 {
15896 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15897 struct type *type;
15898 struct attribute *attr;
15899 const char *name;
15900
15901 /* If the definition of this type lives in .debug_types, read that type.
15902 Don't follow DW_AT_specification though, that will take us back up
15903 the chain and we want to go down. */
15904 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15905 if (attr != nullptr)
15906 {
15907 type = get_DW_AT_signature_type (die, attr, cu);
15908
15909 /* The type's CU may not be the same as CU.
15910 Ensure TYPE is recorded with CU in die_type_hash. */
15911 return set_die_type (die, type, cu);
15912 }
15913
15914 type = alloc_type (objfile);
15915 INIT_CPLUS_SPECIFIC (type);
15916
15917 name = dwarf2_name (die, cu);
15918 if (name != NULL)
15919 {
15920 if (cu->language == language_cplus
15921 || cu->language == language_d
15922 || cu->language == language_rust)
15923 {
15924 const char *full_name = dwarf2_full_name (name, die, cu);
15925
15926 /* dwarf2_full_name might have already finished building the DIE's
15927 type. If so, there is no need to continue. */
15928 if (get_die_type (die, cu) != NULL)
15929 return get_die_type (die, cu);
15930
15931 TYPE_NAME (type) = full_name;
15932 }
15933 else
15934 {
15935 /* The name is already allocated along with this objfile, so
15936 we don't need to duplicate it for the type. */
15937 TYPE_NAME (type) = name;
15938 }
15939 }
15940
15941 if (die->tag == DW_TAG_structure_type)
15942 {
15943 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15944 }
15945 else if (die->tag == DW_TAG_union_type)
15946 {
15947 TYPE_CODE (type) = TYPE_CODE_UNION;
15948 }
15949 else if (die->tag == DW_TAG_variant_part)
15950 {
15951 TYPE_CODE (type) = TYPE_CODE_UNION;
15952 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15953 }
15954 else
15955 {
15956 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15957 }
15958
15959 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15960 TYPE_DECLARED_CLASS (type) = 1;
15961
15962 /* Store the calling convention in the type if it's available in
15963 the die. Otherwise the calling convention remains set to
15964 the default value DW_CC_normal. */
15965 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15966 if (attr != nullptr
15967 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15968 {
15969 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15970 TYPE_CPLUS_CALLING_CONVENTION (type)
15971 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15972 }
15973
15974 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15975 if (attr != nullptr)
15976 {
15977 if (attr_form_is_constant (attr))
15978 TYPE_LENGTH (type) = DW_UNSND (attr);
15979 else
15980 {
15981 /* For the moment, dynamic type sizes are not supported
15982 by GDB's struct type. The actual size is determined
15983 on-demand when resolving the type of a given object,
15984 so set the type's length to zero for now. Otherwise,
15985 we record an expression as the length, and that expression
15986 could lead to a very large value, which could eventually
15987 lead to us trying to allocate that much memory when creating
15988 a value of that type. */
15989 TYPE_LENGTH (type) = 0;
15990 }
15991 }
15992 else
15993 {
15994 TYPE_LENGTH (type) = 0;
15995 }
15996
15997 maybe_set_alignment (cu, die, type);
15998
15999 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16000 {
16001 /* ICC<14 does not output the required DW_AT_declaration on
16002 incomplete types, but gives them a size of zero. */
16003 TYPE_STUB (type) = 1;
16004 }
16005 else
16006 TYPE_STUB_SUPPORTED (type) = 1;
16007
16008 if (die_is_declaration (die, cu))
16009 TYPE_STUB (type) = 1;
16010 else if (attr == NULL && die->child == NULL
16011 && producer_is_realview (cu->producer))
16012 /* RealView does not output the required DW_AT_declaration
16013 on incomplete types. */
16014 TYPE_STUB (type) = 1;
16015
16016 /* We need to add the type field to the die immediately so we don't
16017 infinitely recurse when dealing with pointers to the structure
16018 type within the structure itself. */
16019 set_die_type (die, type, cu);
16020
16021 /* set_die_type should be already done. */
16022 set_descriptive_type (type, die, cu);
16023
16024 return type;
16025 }
16026
16027 /* A helper for process_structure_scope that handles a single member
16028 DIE. */
16029
16030 static void
16031 handle_struct_member_die (struct die_info *child_die, struct type *type,
16032 struct field_info *fi,
16033 std::vector<struct symbol *> *template_args,
16034 struct dwarf2_cu *cu)
16035 {
16036 if (child_die->tag == DW_TAG_member
16037 || child_die->tag == DW_TAG_variable
16038 || child_die->tag == DW_TAG_variant_part)
16039 {
16040 /* NOTE: carlton/2002-11-05: A C++ static data member
16041 should be a DW_TAG_member that is a declaration, but
16042 all versions of G++ as of this writing (so through at
16043 least 3.2.1) incorrectly generate DW_TAG_variable
16044 tags for them instead. */
16045 dwarf2_add_field (fi, child_die, cu);
16046 }
16047 else if (child_die->tag == DW_TAG_subprogram)
16048 {
16049 /* Rust doesn't have member functions in the C++ sense.
16050 However, it does emit ordinary functions as children
16051 of a struct DIE. */
16052 if (cu->language == language_rust)
16053 read_func_scope (child_die, cu);
16054 else
16055 {
16056 /* C++ member function. */
16057 dwarf2_add_member_fn (fi, child_die, type, cu);
16058 }
16059 }
16060 else if (child_die->tag == DW_TAG_inheritance)
16061 {
16062 /* C++ base class field. */
16063 dwarf2_add_field (fi, child_die, cu);
16064 }
16065 else if (type_can_define_types (child_die))
16066 dwarf2_add_type_defn (fi, child_die, cu);
16067 else if (child_die->tag == DW_TAG_template_type_param
16068 || child_die->tag == DW_TAG_template_value_param)
16069 {
16070 struct symbol *arg = new_symbol (child_die, NULL, cu);
16071
16072 if (arg != NULL)
16073 template_args->push_back (arg);
16074 }
16075 else if (child_die->tag == DW_TAG_variant)
16076 {
16077 /* In a variant we want to get the discriminant and also add a
16078 field for our sole member child. */
16079 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16080
16081 for (die_info *variant_child = child_die->child;
16082 variant_child != NULL;
16083 variant_child = sibling_die (variant_child))
16084 {
16085 if (variant_child->tag == DW_TAG_member)
16086 {
16087 handle_struct_member_die (variant_child, type, fi,
16088 template_args, cu);
16089 /* Only handle the one. */
16090 break;
16091 }
16092 }
16093
16094 /* We don't handle this but we might as well report it if we see
16095 it. */
16096 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
16097 complaint (_("DW_AT_discr_list is not supported yet"
16098 " - DIE at %s [in module %s]"),
16099 sect_offset_str (child_die->sect_off),
16100 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16101
16102 /* The first field was just added, so we can stash the
16103 discriminant there. */
16104 gdb_assert (!fi->fields.empty ());
16105 if (discr == NULL)
16106 fi->fields.back ().variant.default_branch = true;
16107 else
16108 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
16109 }
16110 }
16111
16112 /* Finish creating a structure or union type, including filling in
16113 its members and creating a symbol for it. */
16114
16115 static void
16116 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16117 {
16118 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16119 struct die_info *child_die;
16120 struct type *type;
16121
16122 type = get_die_type (die, cu);
16123 if (type == NULL)
16124 type = read_structure_type (die, cu);
16125
16126 /* When reading a DW_TAG_variant_part, we need to notice when we
16127 read the discriminant member, so we can record it later in the
16128 discriminant_info. */
16129 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16130 sect_offset discr_offset {};
16131 bool has_template_parameters = false;
16132
16133 if (is_variant_part)
16134 {
16135 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16136 if (discr == NULL)
16137 {
16138 /* Maybe it's a univariant form, an extension we support.
16139 In this case arrange not to check the offset. */
16140 is_variant_part = false;
16141 }
16142 else if (attr_form_is_ref (discr))
16143 {
16144 struct dwarf2_cu *target_cu = cu;
16145 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16146
16147 discr_offset = target_die->sect_off;
16148 }
16149 else
16150 {
16151 complaint (_("DW_AT_discr does not have DIE reference form"
16152 " - DIE at %s [in module %s]"),
16153 sect_offset_str (die->sect_off),
16154 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16155 is_variant_part = false;
16156 }
16157 }
16158
16159 if (die->child != NULL && ! die_is_declaration (die, cu))
16160 {
16161 struct field_info fi;
16162 std::vector<struct symbol *> template_args;
16163
16164 child_die = die->child;
16165
16166 while (child_die && child_die->tag)
16167 {
16168 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16169
16170 if (is_variant_part && discr_offset == child_die->sect_off)
16171 fi.fields.back ().variant.is_discriminant = true;
16172
16173 child_die = sibling_die (child_die);
16174 }
16175
16176 /* Attach template arguments to type. */
16177 if (!template_args.empty ())
16178 {
16179 has_template_parameters = true;
16180 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16181 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16182 TYPE_TEMPLATE_ARGUMENTS (type)
16183 = XOBNEWVEC (&objfile->objfile_obstack,
16184 struct symbol *,
16185 TYPE_N_TEMPLATE_ARGUMENTS (type));
16186 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16187 template_args.data (),
16188 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16189 * sizeof (struct symbol *)));
16190 }
16191
16192 /* Attach fields and member functions to the type. */
16193 if (fi.nfields)
16194 dwarf2_attach_fields_to_type (&fi, type, cu);
16195 if (!fi.fnfieldlists.empty ())
16196 {
16197 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16198
16199 /* Get the type which refers to the base class (possibly this
16200 class itself) which contains the vtable pointer for the current
16201 class from the DW_AT_containing_type attribute. This use of
16202 DW_AT_containing_type is a GNU extension. */
16203
16204 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16205 {
16206 struct type *t = die_containing_type (die, cu);
16207
16208 set_type_vptr_basetype (type, t);
16209 if (type == t)
16210 {
16211 int i;
16212
16213 /* Our own class provides vtbl ptr. */
16214 for (i = TYPE_NFIELDS (t) - 1;
16215 i >= TYPE_N_BASECLASSES (t);
16216 --i)
16217 {
16218 const char *fieldname = TYPE_FIELD_NAME (t, i);
16219
16220 if (is_vtable_name (fieldname, cu))
16221 {
16222 set_type_vptr_fieldno (type, i);
16223 break;
16224 }
16225 }
16226
16227 /* Complain if virtual function table field not found. */
16228 if (i < TYPE_N_BASECLASSES (t))
16229 complaint (_("virtual function table pointer "
16230 "not found when defining class '%s'"),
16231 TYPE_NAME (type) ? TYPE_NAME (type) : "");
16232 }
16233 else
16234 {
16235 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16236 }
16237 }
16238 else if (cu->producer
16239 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16240 {
16241 /* The IBM XLC compiler does not provide direct indication
16242 of the containing type, but the vtable pointer is
16243 always named __vfp. */
16244
16245 int i;
16246
16247 for (i = TYPE_NFIELDS (type) - 1;
16248 i >= TYPE_N_BASECLASSES (type);
16249 --i)
16250 {
16251 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16252 {
16253 set_type_vptr_fieldno (type, i);
16254 set_type_vptr_basetype (type, type);
16255 break;
16256 }
16257 }
16258 }
16259 }
16260
16261 /* Copy fi.typedef_field_list linked list elements content into the
16262 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16263 if (!fi.typedef_field_list.empty ())
16264 {
16265 int count = fi.typedef_field_list.size ();
16266
16267 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16268 TYPE_TYPEDEF_FIELD_ARRAY (type)
16269 = ((struct decl_field *)
16270 TYPE_ALLOC (type,
16271 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16272 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16273
16274 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16275 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16276 }
16277
16278 /* Copy fi.nested_types_list linked list elements content into the
16279 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16280 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16281 {
16282 int count = fi.nested_types_list.size ();
16283
16284 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16285 TYPE_NESTED_TYPES_ARRAY (type)
16286 = ((struct decl_field *)
16287 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16288 TYPE_NESTED_TYPES_COUNT (type) = count;
16289
16290 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16291 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16292 }
16293 }
16294
16295 quirk_gcc_member_function_pointer (type, objfile);
16296 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16297 cu->rust_unions.push_back (type);
16298
16299 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16300 snapshots) has been known to create a die giving a declaration
16301 for a class that has, as a child, a die giving a definition for a
16302 nested class. So we have to process our children even if the
16303 current die is a declaration. Normally, of course, a declaration
16304 won't have any children at all. */
16305
16306 child_die = die->child;
16307
16308 while (child_die != NULL && child_die->tag)
16309 {
16310 if (child_die->tag == DW_TAG_member
16311 || child_die->tag == DW_TAG_variable
16312 || child_die->tag == DW_TAG_inheritance
16313 || child_die->tag == DW_TAG_template_value_param
16314 || child_die->tag == DW_TAG_template_type_param)
16315 {
16316 /* Do nothing. */
16317 }
16318 else
16319 process_die (child_die, cu);
16320
16321 child_die = sibling_die (child_die);
16322 }
16323
16324 /* Do not consider external references. According to the DWARF standard,
16325 these DIEs are identified by the fact that they have no byte_size
16326 attribute, and a declaration attribute. */
16327 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16328 || !die_is_declaration (die, cu))
16329 {
16330 struct symbol *sym = new_symbol (die, type, cu);
16331
16332 if (has_template_parameters)
16333 {
16334 struct symtab *symtab;
16335 if (sym != nullptr)
16336 symtab = symbol_symtab (sym);
16337 else if (cu->line_header != nullptr)
16338 {
16339 /* Any related symtab will do. */
16340 symtab
16341 = cu->line_header->file_names ()[0].symtab;
16342 }
16343 else
16344 {
16345 symtab = nullptr;
16346 complaint (_("could not find suitable "
16347 "symtab for template parameter"
16348 " - DIE at %s [in module %s]"),
16349 sect_offset_str (die->sect_off),
16350 objfile_name (objfile));
16351 }
16352
16353 if (symtab != nullptr)
16354 {
16355 /* Make sure that the symtab is set on the new symbols.
16356 Even though they don't appear in this symtab directly,
16357 other parts of gdb assume that symbols do, and this is
16358 reasonably true. */
16359 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16360 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16361 }
16362 }
16363 }
16364 }
16365
16366 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16367 update TYPE using some information only available in DIE's children. */
16368
16369 static void
16370 update_enumeration_type_from_children (struct die_info *die,
16371 struct type *type,
16372 struct dwarf2_cu *cu)
16373 {
16374 struct die_info *child_die;
16375 int unsigned_enum = 1;
16376 int flag_enum = 1;
16377 ULONGEST mask = 0;
16378
16379 auto_obstack obstack;
16380
16381 for (child_die = die->child;
16382 child_die != NULL && child_die->tag;
16383 child_die = sibling_die (child_die))
16384 {
16385 struct attribute *attr;
16386 LONGEST value;
16387 const gdb_byte *bytes;
16388 struct dwarf2_locexpr_baton *baton;
16389 const char *name;
16390
16391 if (child_die->tag != DW_TAG_enumerator)
16392 continue;
16393
16394 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16395 if (attr == NULL)
16396 continue;
16397
16398 name = dwarf2_name (child_die, cu);
16399 if (name == NULL)
16400 name = "<anonymous enumerator>";
16401
16402 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16403 &value, &bytes, &baton);
16404 if (value < 0)
16405 {
16406 unsigned_enum = 0;
16407 flag_enum = 0;
16408 }
16409 else if ((mask & value) != 0)
16410 flag_enum = 0;
16411 else
16412 mask |= value;
16413
16414 /* If we already know that the enum type is neither unsigned, nor
16415 a flag type, no need to look at the rest of the enumerates. */
16416 if (!unsigned_enum && !flag_enum)
16417 break;
16418 }
16419
16420 if (unsigned_enum)
16421 TYPE_UNSIGNED (type) = 1;
16422 if (flag_enum)
16423 TYPE_FLAG_ENUM (type) = 1;
16424 }
16425
16426 /* Given a DW_AT_enumeration_type die, set its type. We do not
16427 complete the type's fields yet, or create any symbols. */
16428
16429 static struct type *
16430 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16431 {
16432 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16433 struct type *type;
16434 struct attribute *attr;
16435 const char *name;
16436
16437 /* If the definition of this type lives in .debug_types, read that type.
16438 Don't follow DW_AT_specification though, that will take us back up
16439 the chain and we want to go down. */
16440 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16441 if (attr != nullptr)
16442 {
16443 type = get_DW_AT_signature_type (die, attr, cu);
16444
16445 /* The type's CU may not be the same as CU.
16446 Ensure TYPE is recorded with CU in die_type_hash. */
16447 return set_die_type (die, type, cu);
16448 }
16449
16450 type = alloc_type (objfile);
16451
16452 TYPE_CODE (type) = TYPE_CODE_ENUM;
16453 name = dwarf2_full_name (NULL, die, cu);
16454 if (name != NULL)
16455 TYPE_NAME (type) = name;
16456
16457 attr = dwarf2_attr (die, DW_AT_type, cu);
16458 if (attr != NULL)
16459 {
16460 struct type *underlying_type = die_type (die, cu);
16461
16462 TYPE_TARGET_TYPE (type) = underlying_type;
16463 }
16464
16465 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16466 if (attr != nullptr)
16467 {
16468 TYPE_LENGTH (type) = DW_UNSND (attr);
16469 }
16470 else
16471 {
16472 TYPE_LENGTH (type) = 0;
16473 }
16474
16475 maybe_set_alignment (cu, die, type);
16476
16477 /* The enumeration DIE can be incomplete. In Ada, any type can be
16478 declared as private in the package spec, and then defined only
16479 inside the package body. Such types are known as Taft Amendment
16480 Types. When another package uses such a type, an incomplete DIE
16481 may be generated by the compiler. */
16482 if (die_is_declaration (die, cu))
16483 TYPE_STUB (type) = 1;
16484
16485 /* Finish the creation of this type by using the enum's children.
16486 We must call this even when the underlying type has been provided
16487 so that we can determine if we're looking at a "flag" enum. */
16488 update_enumeration_type_from_children (die, type, cu);
16489
16490 /* If this type has an underlying type that is not a stub, then we
16491 may use its attributes. We always use the "unsigned" attribute
16492 in this situation, because ordinarily we guess whether the type
16493 is unsigned -- but the guess can be wrong and the underlying type
16494 can tell us the reality. However, we defer to a local size
16495 attribute if one exists, because this lets the compiler override
16496 the underlying type if needed. */
16497 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16498 {
16499 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16500 if (TYPE_LENGTH (type) == 0)
16501 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16502 if (TYPE_RAW_ALIGN (type) == 0
16503 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16504 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16505 }
16506
16507 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16508
16509 return set_die_type (die, type, cu);
16510 }
16511
16512 /* Given a pointer to a die which begins an enumeration, process all
16513 the dies that define the members of the enumeration, and create the
16514 symbol for the enumeration type.
16515
16516 NOTE: We reverse the order of the element list. */
16517
16518 static void
16519 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16520 {
16521 struct type *this_type;
16522
16523 this_type = get_die_type (die, cu);
16524 if (this_type == NULL)
16525 this_type = read_enumeration_type (die, cu);
16526
16527 if (die->child != NULL)
16528 {
16529 struct die_info *child_die;
16530 struct symbol *sym;
16531 std::vector<struct field> fields;
16532 const char *name;
16533
16534 child_die = die->child;
16535 while (child_die && child_die->tag)
16536 {
16537 if (child_die->tag != DW_TAG_enumerator)
16538 {
16539 process_die (child_die, cu);
16540 }
16541 else
16542 {
16543 name = dwarf2_name (child_die, cu);
16544 if (name)
16545 {
16546 sym = new_symbol (child_die, this_type, cu);
16547
16548 fields.emplace_back ();
16549 struct field &field = fields.back ();
16550
16551 FIELD_NAME (field) = sym->linkage_name ();
16552 FIELD_TYPE (field) = NULL;
16553 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
16554 FIELD_BITSIZE (field) = 0;
16555 }
16556 }
16557
16558 child_die = sibling_die (child_die);
16559 }
16560
16561 if (!fields.empty ())
16562 {
16563 TYPE_NFIELDS (this_type) = fields.size ();
16564 TYPE_FIELDS (this_type) = (struct field *)
16565 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
16566 memcpy (TYPE_FIELDS (this_type), fields.data (),
16567 sizeof (struct field) * fields.size ());
16568 }
16569 }
16570
16571 /* If we are reading an enum from a .debug_types unit, and the enum
16572 is a declaration, and the enum is not the signatured type in the
16573 unit, then we do not want to add a symbol for it. Adding a
16574 symbol would in some cases obscure the true definition of the
16575 enum, giving users an incomplete type when the definition is
16576 actually available. Note that we do not want to do this for all
16577 enums which are just declarations, because C++0x allows forward
16578 enum declarations. */
16579 if (cu->per_cu->is_debug_types
16580 && die_is_declaration (die, cu))
16581 {
16582 struct signatured_type *sig_type;
16583
16584 sig_type = (struct signatured_type *) cu->per_cu;
16585 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16586 if (sig_type->type_offset_in_section != die->sect_off)
16587 return;
16588 }
16589
16590 new_symbol (die, this_type, cu);
16591 }
16592
16593 /* Extract all information from a DW_TAG_array_type DIE and put it in
16594 the DIE's type field. For now, this only handles one dimensional
16595 arrays. */
16596
16597 static struct type *
16598 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16599 {
16600 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16601 struct die_info *child_die;
16602 struct type *type;
16603 struct type *element_type, *range_type, *index_type;
16604 struct attribute *attr;
16605 const char *name;
16606 struct dynamic_prop *byte_stride_prop = NULL;
16607 unsigned int bit_stride = 0;
16608
16609 element_type = die_type (die, cu);
16610
16611 /* The die_type call above may have already set the type for this DIE. */
16612 type = get_die_type (die, cu);
16613 if (type)
16614 return type;
16615
16616 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16617 if (attr != NULL)
16618 {
16619 int stride_ok;
16620 struct type *prop_type
16621 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
16622
16623 byte_stride_prop
16624 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16625 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16626 prop_type);
16627 if (!stride_ok)
16628 {
16629 complaint (_("unable to read array DW_AT_byte_stride "
16630 " - DIE at %s [in module %s]"),
16631 sect_offset_str (die->sect_off),
16632 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16633 /* Ignore this attribute. We will likely not be able to print
16634 arrays of this type correctly, but there is little we can do
16635 to help if we cannot read the attribute's value. */
16636 byte_stride_prop = NULL;
16637 }
16638 }
16639
16640 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16641 if (attr != NULL)
16642 bit_stride = DW_UNSND (attr);
16643
16644 /* Irix 6.2 native cc creates array types without children for
16645 arrays with unspecified length. */
16646 if (die->child == NULL)
16647 {
16648 index_type = objfile_type (objfile)->builtin_int;
16649 range_type = create_static_range_type (NULL, index_type, 0, -1);
16650 type = create_array_type_with_stride (NULL, element_type, range_type,
16651 byte_stride_prop, bit_stride);
16652 return set_die_type (die, type, cu);
16653 }
16654
16655 std::vector<struct type *> range_types;
16656 child_die = die->child;
16657 while (child_die && child_die->tag)
16658 {
16659 if (child_die->tag == DW_TAG_subrange_type)
16660 {
16661 struct type *child_type = read_type_die (child_die, cu);
16662
16663 if (child_type != NULL)
16664 {
16665 /* The range type was succesfully read. Save it for the
16666 array type creation. */
16667 range_types.push_back (child_type);
16668 }
16669 }
16670 child_die = sibling_die (child_die);
16671 }
16672
16673 /* Dwarf2 dimensions are output from left to right, create the
16674 necessary array types in backwards order. */
16675
16676 type = element_type;
16677
16678 if (read_array_order (die, cu) == DW_ORD_col_major)
16679 {
16680 int i = 0;
16681
16682 while (i < range_types.size ())
16683 type = create_array_type_with_stride (NULL, type, range_types[i++],
16684 byte_stride_prop, bit_stride);
16685 }
16686 else
16687 {
16688 size_t ndim = range_types.size ();
16689 while (ndim-- > 0)
16690 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16691 byte_stride_prop, bit_stride);
16692 }
16693
16694 /* Understand Dwarf2 support for vector types (like they occur on
16695 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16696 array type. This is not part of the Dwarf2/3 standard yet, but a
16697 custom vendor extension. The main difference between a regular
16698 array and the vector variant is that vectors are passed by value
16699 to functions. */
16700 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16701 if (attr != nullptr)
16702 make_vector_type (type);
16703
16704 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16705 implementation may choose to implement triple vectors using this
16706 attribute. */
16707 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16708 if (attr != nullptr)
16709 {
16710 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16711 TYPE_LENGTH (type) = DW_UNSND (attr);
16712 else
16713 complaint (_("DW_AT_byte_size for array type smaller "
16714 "than the total size of elements"));
16715 }
16716
16717 name = dwarf2_name (die, cu);
16718 if (name)
16719 TYPE_NAME (type) = name;
16720
16721 maybe_set_alignment (cu, die, type);
16722
16723 /* Install the type in the die. */
16724 set_die_type (die, type, cu);
16725
16726 /* set_die_type should be already done. */
16727 set_descriptive_type (type, die, cu);
16728
16729 return type;
16730 }
16731
16732 static enum dwarf_array_dim_ordering
16733 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16734 {
16735 struct attribute *attr;
16736
16737 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16738
16739 if (attr != nullptr)
16740 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16741
16742 /* GNU F77 is a special case, as at 08/2004 array type info is the
16743 opposite order to the dwarf2 specification, but data is still
16744 laid out as per normal fortran.
16745
16746 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16747 version checking. */
16748
16749 if (cu->language == language_fortran
16750 && cu->producer && strstr (cu->producer, "GNU F77"))
16751 {
16752 return DW_ORD_row_major;
16753 }
16754
16755 switch (cu->language_defn->la_array_ordering)
16756 {
16757 case array_column_major:
16758 return DW_ORD_col_major;
16759 case array_row_major:
16760 default:
16761 return DW_ORD_row_major;
16762 };
16763 }
16764
16765 /* Extract all information from a DW_TAG_set_type DIE and put it in
16766 the DIE's type field. */
16767
16768 static struct type *
16769 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16770 {
16771 struct type *domain_type, *set_type;
16772 struct attribute *attr;
16773
16774 domain_type = die_type (die, cu);
16775
16776 /* The die_type call above may have already set the type for this DIE. */
16777 set_type = get_die_type (die, cu);
16778 if (set_type)
16779 return set_type;
16780
16781 set_type = create_set_type (NULL, domain_type);
16782
16783 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16784 if (attr != nullptr)
16785 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16786
16787 maybe_set_alignment (cu, die, set_type);
16788
16789 return set_die_type (die, set_type, cu);
16790 }
16791
16792 /* A helper for read_common_block that creates a locexpr baton.
16793 SYM is the symbol which we are marking as computed.
16794 COMMON_DIE is the DIE for the common block.
16795 COMMON_LOC is the location expression attribute for the common
16796 block itself.
16797 MEMBER_LOC is the location expression attribute for the particular
16798 member of the common block that we are processing.
16799 CU is the CU from which the above come. */
16800
16801 static void
16802 mark_common_block_symbol_computed (struct symbol *sym,
16803 struct die_info *common_die,
16804 struct attribute *common_loc,
16805 struct attribute *member_loc,
16806 struct dwarf2_cu *cu)
16807 {
16808 struct dwarf2_per_objfile *dwarf2_per_objfile
16809 = cu->per_cu->dwarf2_per_objfile;
16810 struct objfile *objfile = dwarf2_per_objfile->objfile;
16811 struct dwarf2_locexpr_baton *baton;
16812 gdb_byte *ptr;
16813 unsigned int cu_off;
16814 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16815 LONGEST offset = 0;
16816
16817 gdb_assert (common_loc && member_loc);
16818 gdb_assert (attr_form_is_block (common_loc));
16819 gdb_assert (attr_form_is_block (member_loc)
16820 || attr_form_is_constant (member_loc));
16821
16822 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16823 baton->per_cu = cu->per_cu;
16824 gdb_assert (baton->per_cu);
16825
16826 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16827
16828 if (attr_form_is_constant (member_loc))
16829 {
16830 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16831 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16832 }
16833 else
16834 baton->size += DW_BLOCK (member_loc)->size;
16835
16836 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16837 baton->data = ptr;
16838
16839 *ptr++ = DW_OP_call4;
16840 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16841 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16842 ptr += 4;
16843
16844 if (attr_form_is_constant (member_loc))
16845 {
16846 *ptr++ = DW_OP_addr;
16847 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16848 ptr += cu->header.addr_size;
16849 }
16850 else
16851 {
16852 /* We have to copy the data here, because DW_OP_call4 will only
16853 use a DW_AT_location attribute. */
16854 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16855 ptr += DW_BLOCK (member_loc)->size;
16856 }
16857
16858 *ptr++ = DW_OP_plus;
16859 gdb_assert (ptr - baton->data == baton->size);
16860
16861 SYMBOL_LOCATION_BATON (sym) = baton;
16862 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16863 }
16864
16865 /* Create appropriate locally-scoped variables for all the
16866 DW_TAG_common_block entries. Also create a struct common_block
16867 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16868 is used to separate the common blocks name namespace from regular
16869 variable names. */
16870
16871 static void
16872 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16873 {
16874 struct attribute *attr;
16875
16876 attr = dwarf2_attr (die, DW_AT_location, cu);
16877 if (attr != nullptr)
16878 {
16879 /* Support the .debug_loc offsets. */
16880 if (attr_form_is_block (attr))
16881 {
16882 /* Ok. */
16883 }
16884 else if (attr_form_is_section_offset (attr))
16885 {
16886 dwarf2_complex_location_expr_complaint ();
16887 attr = NULL;
16888 }
16889 else
16890 {
16891 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16892 "common block member");
16893 attr = NULL;
16894 }
16895 }
16896
16897 if (die->child != NULL)
16898 {
16899 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16900 struct die_info *child_die;
16901 size_t n_entries = 0, size;
16902 struct common_block *common_block;
16903 struct symbol *sym;
16904
16905 for (child_die = die->child;
16906 child_die && child_die->tag;
16907 child_die = sibling_die (child_die))
16908 ++n_entries;
16909
16910 size = (sizeof (struct common_block)
16911 + (n_entries - 1) * sizeof (struct symbol *));
16912 common_block
16913 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16914 size);
16915 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16916 common_block->n_entries = 0;
16917
16918 for (child_die = die->child;
16919 child_die && child_die->tag;
16920 child_die = sibling_die (child_die))
16921 {
16922 /* Create the symbol in the DW_TAG_common_block block in the current
16923 symbol scope. */
16924 sym = new_symbol (child_die, NULL, cu);
16925 if (sym != NULL)
16926 {
16927 struct attribute *member_loc;
16928
16929 common_block->contents[common_block->n_entries++] = sym;
16930
16931 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16932 cu);
16933 if (member_loc)
16934 {
16935 /* GDB has handled this for a long time, but it is
16936 not specified by DWARF. It seems to have been
16937 emitted by gfortran at least as recently as:
16938 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16939 complaint (_("Variable in common block has "
16940 "DW_AT_data_member_location "
16941 "- DIE at %s [in module %s]"),
16942 sect_offset_str (child_die->sect_off),
16943 objfile_name (objfile));
16944
16945 if (attr_form_is_section_offset (member_loc))
16946 dwarf2_complex_location_expr_complaint ();
16947 else if (attr_form_is_constant (member_loc)
16948 || attr_form_is_block (member_loc))
16949 {
16950 if (attr != nullptr)
16951 mark_common_block_symbol_computed (sym, die, attr,
16952 member_loc, cu);
16953 }
16954 else
16955 dwarf2_complex_location_expr_complaint ();
16956 }
16957 }
16958 }
16959
16960 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16961 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16962 }
16963 }
16964
16965 /* Create a type for a C++ namespace. */
16966
16967 static struct type *
16968 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16969 {
16970 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16971 const char *previous_prefix, *name;
16972 int is_anonymous;
16973 struct type *type;
16974
16975 /* For extensions, reuse the type of the original namespace. */
16976 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16977 {
16978 struct die_info *ext_die;
16979 struct dwarf2_cu *ext_cu = cu;
16980
16981 ext_die = dwarf2_extension (die, &ext_cu);
16982 type = read_type_die (ext_die, ext_cu);
16983
16984 /* EXT_CU may not be the same as CU.
16985 Ensure TYPE is recorded with CU in die_type_hash. */
16986 return set_die_type (die, type, cu);
16987 }
16988
16989 name = namespace_name (die, &is_anonymous, cu);
16990
16991 /* Now build the name of the current namespace. */
16992
16993 previous_prefix = determine_prefix (die, cu);
16994 if (previous_prefix[0] != '\0')
16995 name = typename_concat (&objfile->objfile_obstack,
16996 previous_prefix, name, 0, cu);
16997
16998 /* Create the type. */
16999 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17000
17001 return set_die_type (die, type, cu);
17002 }
17003
17004 /* Read a namespace scope. */
17005
17006 static void
17007 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17008 {
17009 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17010 int is_anonymous;
17011
17012 /* Add a symbol associated to this if we haven't seen the namespace
17013 before. Also, add a using directive if it's an anonymous
17014 namespace. */
17015
17016 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17017 {
17018 struct type *type;
17019
17020 type = read_type_die (die, cu);
17021 new_symbol (die, type, cu);
17022
17023 namespace_name (die, &is_anonymous, cu);
17024 if (is_anonymous)
17025 {
17026 const char *previous_prefix = determine_prefix (die, cu);
17027
17028 std::vector<const char *> excludes;
17029 add_using_directive (using_directives (cu),
17030 previous_prefix, TYPE_NAME (type), NULL,
17031 NULL, excludes, 0, &objfile->objfile_obstack);
17032 }
17033 }
17034
17035 if (die->child != NULL)
17036 {
17037 struct die_info *child_die = die->child;
17038
17039 while (child_die && child_die->tag)
17040 {
17041 process_die (child_die, cu);
17042 child_die = sibling_die (child_die);
17043 }
17044 }
17045 }
17046
17047 /* Read a Fortran module as type. This DIE can be only a declaration used for
17048 imported module. Still we need that type as local Fortran "use ... only"
17049 declaration imports depend on the created type in determine_prefix. */
17050
17051 static struct type *
17052 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17053 {
17054 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17055 const char *module_name;
17056 struct type *type;
17057
17058 module_name = dwarf2_name (die, cu);
17059 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17060
17061 return set_die_type (die, type, cu);
17062 }
17063
17064 /* Read a Fortran module. */
17065
17066 static void
17067 read_module (struct die_info *die, struct dwarf2_cu *cu)
17068 {
17069 struct die_info *child_die = die->child;
17070 struct type *type;
17071
17072 type = read_type_die (die, cu);
17073 new_symbol (die, type, cu);
17074
17075 while (child_die && child_die->tag)
17076 {
17077 process_die (child_die, cu);
17078 child_die = sibling_die (child_die);
17079 }
17080 }
17081
17082 /* Return the name of the namespace represented by DIE. Set
17083 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17084 namespace. */
17085
17086 static const char *
17087 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17088 {
17089 struct die_info *current_die;
17090 const char *name = NULL;
17091
17092 /* Loop through the extensions until we find a name. */
17093
17094 for (current_die = die;
17095 current_die != NULL;
17096 current_die = dwarf2_extension (die, &cu))
17097 {
17098 /* We don't use dwarf2_name here so that we can detect the absence
17099 of a name -> anonymous namespace. */
17100 name = dwarf2_string_attr (die, DW_AT_name, cu);
17101
17102 if (name != NULL)
17103 break;
17104 }
17105
17106 /* Is it an anonymous namespace? */
17107
17108 *is_anonymous = (name == NULL);
17109 if (*is_anonymous)
17110 name = CP_ANONYMOUS_NAMESPACE_STR;
17111
17112 return name;
17113 }
17114
17115 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17116 the user defined type vector. */
17117
17118 static struct type *
17119 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17120 {
17121 struct gdbarch *gdbarch
17122 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
17123 struct comp_unit_head *cu_header = &cu->header;
17124 struct type *type;
17125 struct attribute *attr_byte_size;
17126 struct attribute *attr_address_class;
17127 int byte_size, addr_class;
17128 struct type *target_type;
17129
17130 target_type = die_type (die, cu);
17131
17132 /* The die_type call above may have already set the type for this DIE. */
17133 type = get_die_type (die, cu);
17134 if (type)
17135 return type;
17136
17137 type = lookup_pointer_type (target_type);
17138
17139 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17140 if (attr_byte_size)
17141 byte_size = DW_UNSND (attr_byte_size);
17142 else
17143 byte_size = cu_header->addr_size;
17144
17145 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17146 if (attr_address_class)
17147 addr_class = DW_UNSND (attr_address_class);
17148 else
17149 addr_class = DW_ADDR_none;
17150
17151 ULONGEST alignment = get_alignment (cu, die);
17152
17153 /* If the pointer size, alignment, or address class is different
17154 than the default, create a type variant marked as such and set
17155 the length accordingly. */
17156 if (TYPE_LENGTH (type) != byte_size
17157 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17158 && alignment != TYPE_RAW_ALIGN (type))
17159 || addr_class != DW_ADDR_none)
17160 {
17161 if (gdbarch_address_class_type_flags_p (gdbarch))
17162 {
17163 int type_flags;
17164
17165 type_flags = gdbarch_address_class_type_flags
17166 (gdbarch, byte_size, addr_class);
17167 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17168 == 0);
17169 type = make_type_with_address_space (type, type_flags);
17170 }
17171 else if (TYPE_LENGTH (type) != byte_size)
17172 {
17173 complaint (_("invalid pointer size %d"), byte_size);
17174 }
17175 else if (TYPE_RAW_ALIGN (type) != alignment)
17176 {
17177 complaint (_("Invalid DW_AT_alignment"
17178 " - DIE at %s [in module %s]"),
17179 sect_offset_str (die->sect_off),
17180 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17181 }
17182 else
17183 {
17184 /* Should we also complain about unhandled address classes? */
17185 }
17186 }
17187
17188 TYPE_LENGTH (type) = byte_size;
17189 set_type_align (type, alignment);
17190 return set_die_type (die, type, cu);
17191 }
17192
17193 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17194 the user defined type vector. */
17195
17196 static struct type *
17197 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17198 {
17199 struct type *type;
17200 struct type *to_type;
17201 struct type *domain;
17202
17203 to_type = die_type (die, cu);
17204 domain = die_containing_type (die, cu);
17205
17206 /* The calls above may have already set the type for this DIE. */
17207 type = get_die_type (die, cu);
17208 if (type)
17209 return type;
17210
17211 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17212 type = lookup_methodptr_type (to_type);
17213 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17214 {
17215 struct type *new_type
17216 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17217
17218 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17219 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17220 TYPE_VARARGS (to_type));
17221 type = lookup_methodptr_type (new_type);
17222 }
17223 else
17224 type = lookup_memberptr_type (to_type, domain);
17225
17226 return set_die_type (die, type, cu);
17227 }
17228
17229 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17230 the user defined type vector. */
17231
17232 static struct type *
17233 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17234 enum type_code refcode)
17235 {
17236 struct comp_unit_head *cu_header = &cu->header;
17237 struct type *type, *target_type;
17238 struct attribute *attr;
17239
17240 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17241
17242 target_type = die_type (die, cu);
17243
17244 /* The die_type call above may have already set the type for this DIE. */
17245 type = get_die_type (die, cu);
17246 if (type)
17247 return type;
17248
17249 type = lookup_reference_type (target_type, refcode);
17250 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17251 if (attr != nullptr)
17252 {
17253 TYPE_LENGTH (type) = DW_UNSND (attr);
17254 }
17255 else
17256 {
17257 TYPE_LENGTH (type) = cu_header->addr_size;
17258 }
17259 maybe_set_alignment (cu, die, type);
17260 return set_die_type (die, type, cu);
17261 }
17262
17263 /* Add the given cv-qualifiers to the element type of the array. GCC
17264 outputs DWARF type qualifiers that apply to an array, not the
17265 element type. But GDB relies on the array element type to carry
17266 the cv-qualifiers. This mimics section 6.7.3 of the C99
17267 specification. */
17268
17269 static struct type *
17270 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17271 struct type *base_type, int cnst, int voltl)
17272 {
17273 struct type *el_type, *inner_array;
17274
17275 base_type = copy_type (base_type);
17276 inner_array = base_type;
17277
17278 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17279 {
17280 TYPE_TARGET_TYPE (inner_array) =
17281 copy_type (TYPE_TARGET_TYPE (inner_array));
17282 inner_array = TYPE_TARGET_TYPE (inner_array);
17283 }
17284
17285 el_type = TYPE_TARGET_TYPE (inner_array);
17286 cnst |= TYPE_CONST (el_type);
17287 voltl |= TYPE_VOLATILE (el_type);
17288 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17289
17290 return set_die_type (die, base_type, cu);
17291 }
17292
17293 static struct type *
17294 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17295 {
17296 struct type *base_type, *cv_type;
17297
17298 base_type = die_type (die, cu);
17299
17300 /* The die_type call above may have already set the type for this DIE. */
17301 cv_type = get_die_type (die, cu);
17302 if (cv_type)
17303 return cv_type;
17304
17305 /* In case the const qualifier is applied to an array type, the element type
17306 is so qualified, not the array type (section 6.7.3 of C99). */
17307 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17308 return add_array_cv_type (die, cu, base_type, 1, 0);
17309
17310 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17311 return set_die_type (die, cv_type, cu);
17312 }
17313
17314 static struct type *
17315 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17316 {
17317 struct type *base_type, *cv_type;
17318
17319 base_type = die_type (die, cu);
17320
17321 /* The die_type call above may have already set the type for this DIE. */
17322 cv_type = get_die_type (die, cu);
17323 if (cv_type)
17324 return cv_type;
17325
17326 /* In case the volatile qualifier is applied to an array type, the
17327 element type is so qualified, not the array type (section 6.7.3
17328 of C99). */
17329 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17330 return add_array_cv_type (die, cu, base_type, 0, 1);
17331
17332 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17333 return set_die_type (die, cv_type, cu);
17334 }
17335
17336 /* Handle DW_TAG_restrict_type. */
17337
17338 static struct type *
17339 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17340 {
17341 struct type *base_type, *cv_type;
17342
17343 base_type = die_type (die, cu);
17344
17345 /* The die_type call above may have already set the type for this DIE. */
17346 cv_type = get_die_type (die, cu);
17347 if (cv_type)
17348 return cv_type;
17349
17350 cv_type = make_restrict_type (base_type);
17351 return set_die_type (die, cv_type, cu);
17352 }
17353
17354 /* Handle DW_TAG_atomic_type. */
17355
17356 static struct type *
17357 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17358 {
17359 struct type *base_type, *cv_type;
17360
17361 base_type = die_type (die, cu);
17362
17363 /* The die_type call above may have already set the type for this DIE. */
17364 cv_type = get_die_type (die, cu);
17365 if (cv_type)
17366 return cv_type;
17367
17368 cv_type = make_atomic_type (base_type);
17369 return set_die_type (die, cv_type, cu);
17370 }
17371
17372 /* Extract all information from a DW_TAG_string_type DIE and add to
17373 the user defined type vector. It isn't really a user defined type,
17374 but it behaves like one, with other DIE's using an AT_user_def_type
17375 attribute to reference it. */
17376
17377 static struct type *
17378 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17379 {
17380 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17381 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17382 struct type *type, *range_type, *index_type, *char_type;
17383 struct attribute *attr;
17384 struct dynamic_prop prop;
17385 bool length_is_constant = true;
17386 LONGEST length;
17387
17388 /* There are a couple of places where bit sizes might be made use of
17389 when parsing a DW_TAG_string_type, however, no producer that we know
17390 of make use of these. Handling bit sizes that are a multiple of the
17391 byte size is easy enough, but what about other bit sizes? Lets deal
17392 with that problem when we have to. Warn about these attributes being
17393 unsupported, then parse the type and ignore them like we always
17394 have. */
17395 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17396 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17397 {
17398 static bool warning_printed = false;
17399 if (!warning_printed)
17400 {
17401 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17402 "currently supported on DW_TAG_string_type."));
17403 warning_printed = true;
17404 }
17405 }
17406
17407 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17408 if (attr != nullptr && !attr_form_is_constant (attr))
17409 {
17410 /* The string length describes the location at which the length of
17411 the string can be found. The size of the length field can be
17412 specified with one of the attributes below. */
17413 struct type *prop_type;
17414 struct attribute *len
17415 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17416 if (len == nullptr)
17417 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17418 if (len != nullptr && attr_form_is_constant (len))
17419 {
17420 /* Pass 0 as the default as we know this attribute is constant
17421 and the default value will not be returned. */
17422 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17423 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17424 }
17425 else
17426 {
17427 /* If the size is not specified then we assume it is the size of
17428 an address on this target. */
17429 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17430 }
17431
17432 /* Convert the attribute into a dynamic property. */
17433 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17434 length = 1;
17435 else
17436 length_is_constant = false;
17437 }
17438 else if (attr != nullptr)
17439 {
17440 /* This DW_AT_string_length just contains the length with no
17441 indirection. There's no need to create a dynamic property in this
17442 case. Pass 0 for the default value as we know it will not be
17443 returned in this case. */
17444 length = dwarf2_get_attr_constant_value (attr, 0);
17445 }
17446 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17447 {
17448 /* We don't currently support non-constant byte sizes for strings. */
17449 length = dwarf2_get_attr_constant_value (attr, 1);
17450 }
17451 else
17452 {
17453 /* Use 1 as a fallback length if we have nothing else. */
17454 length = 1;
17455 }
17456
17457 index_type = objfile_type (objfile)->builtin_int;
17458 if (length_is_constant)
17459 range_type = create_static_range_type (NULL, index_type, 1, length);
17460 else
17461 {
17462 struct dynamic_prop low_bound;
17463
17464 low_bound.kind = PROP_CONST;
17465 low_bound.data.const_val = 1;
17466 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17467 }
17468 char_type = language_string_char_type (cu->language_defn, gdbarch);
17469 type = create_string_type (NULL, char_type, range_type);
17470
17471 return set_die_type (die, type, cu);
17472 }
17473
17474 /* Assuming that DIE corresponds to a function, returns nonzero
17475 if the function is prototyped. */
17476
17477 static int
17478 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17479 {
17480 struct attribute *attr;
17481
17482 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17483 if (attr && (DW_UNSND (attr) != 0))
17484 return 1;
17485
17486 /* The DWARF standard implies that the DW_AT_prototyped attribute
17487 is only meaningful for C, but the concept also extends to other
17488 languages that allow unprototyped functions (Eg: Objective C).
17489 For all other languages, assume that functions are always
17490 prototyped. */
17491 if (cu->language != language_c
17492 && cu->language != language_objc
17493 && cu->language != language_opencl)
17494 return 1;
17495
17496 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17497 prototyped and unprototyped functions; default to prototyped,
17498 since that is more common in modern code (and RealView warns
17499 about unprototyped functions). */
17500 if (producer_is_realview (cu->producer))
17501 return 1;
17502
17503 return 0;
17504 }
17505
17506 /* Handle DIES due to C code like:
17507
17508 struct foo
17509 {
17510 int (*funcp)(int a, long l);
17511 int b;
17512 };
17513
17514 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17515
17516 static struct type *
17517 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17518 {
17519 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17520 struct type *type; /* Type that this function returns. */
17521 struct type *ftype; /* Function that returns above type. */
17522 struct attribute *attr;
17523
17524 type = die_type (die, cu);
17525
17526 /* The die_type call above may have already set the type for this DIE. */
17527 ftype = get_die_type (die, cu);
17528 if (ftype)
17529 return ftype;
17530
17531 ftype = lookup_function_type (type);
17532
17533 if (prototyped_function_p (die, cu))
17534 TYPE_PROTOTYPED (ftype) = 1;
17535
17536 /* Store the calling convention in the type if it's available in
17537 the subroutine die. Otherwise set the calling convention to
17538 the default value DW_CC_normal. */
17539 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17540 if (attr != nullptr
17541 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17542 TYPE_CALLING_CONVENTION (ftype)
17543 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17544 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17545 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17546 else
17547 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17548
17549 /* Record whether the function returns normally to its caller or not
17550 if the DWARF producer set that information. */
17551 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17552 if (attr && (DW_UNSND (attr) != 0))
17553 TYPE_NO_RETURN (ftype) = 1;
17554
17555 /* We need to add the subroutine type to the die immediately so
17556 we don't infinitely recurse when dealing with parameters
17557 declared as the same subroutine type. */
17558 set_die_type (die, ftype, cu);
17559
17560 if (die->child != NULL)
17561 {
17562 struct type *void_type = objfile_type (objfile)->builtin_void;
17563 struct die_info *child_die;
17564 int nparams, iparams;
17565
17566 /* Count the number of parameters.
17567 FIXME: GDB currently ignores vararg functions, but knows about
17568 vararg member functions. */
17569 nparams = 0;
17570 child_die = die->child;
17571 while (child_die && child_die->tag)
17572 {
17573 if (child_die->tag == DW_TAG_formal_parameter)
17574 nparams++;
17575 else if (child_die->tag == DW_TAG_unspecified_parameters)
17576 TYPE_VARARGS (ftype) = 1;
17577 child_die = sibling_die (child_die);
17578 }
17579
17580 /* Allocate storage for parameters and fill them in. */
17581 TYPE_NFIELDS (ftype) = nparams;
17582 TYPE_FIELDS (ftype) = (struct field *)
17583 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17584
17585 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17586 even if we error out during the parameters reading below. */
17587 for (iparams = 0; iparams < nparams; iparams++)
17588 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17589
17590 iparams = 0;
17591 child_die = die->child;
17592 while (child_die && child_die->tag)
17593 {
17594 if (child_die->tag == DW_TAG_formal_parameter)
17595 {
17596 struct type *arg_type;
17597
17598 /* DWARF version 2 has no clean way to discern C++
17599 static and non-static member functions. G++ helps
17600 GDB by marking the first parameter for non-static
17601 member functions (which is the this pointer) as
17602 artificial. We pass this information to
17603 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17604
17605 DWARF version 3 added DW_AT_object_pointer, which GCC
17606 4.5 does not yet generate. */
17607 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17608 if (attr != nullptr)
17609 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17610 else
17611 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17612 arg_type = die_type (child_die, cu);
17613
17614 /* RealView does not mark THIS as const, which the testsuite
17615 expects. GCC marks THIS as const in method definitions,
17616 but not in the class specifications (GCC PR 43053). */
17617 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17618 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17619 {
17620 int is_this = 0;
17621 struct dwarf2_cu *arg_cu = cu;
17622 const char *name = dwarf2_name (child_die, cu);
17623
17624 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17625 if (attr != nullptr)
17626 {
17627 /* If the compiler emits this, use it. */
17628 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17629 is_this = 1;
17630 }
17631 else if (name && strcmp (name, "this") == 0)
17632 /* Function definitions will have the argument names. */
17633 is_this = 1;
17634 else if (name == NULL && iparams == 0)
17635 /* Declarations may not have the names, so like
17636 elsewhere in GDB, assume an artificial first
17637 argument is "this". */
17638 is_this = 1;
17639
17640 if (is_this)
17641 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17642 arg_type, 0);
17643 }
17644
17645 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17646 iparams++;
17647 }
17648 child_die = sibling_die (child_die);
17649 }
17650 }
17651
17652 return ftype;
17653 }
17654
17655 static struct type *
17656 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17657 {
17658 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17659 const char *name = NULL;
17660 struct type *this_type, *target_type;
17661
17662 name = dwarf2_full_name (NULL, die, cu);
17663 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17664 TYPE_TARGET_STUB (this_type) = 1;
17665 set_die_type (die, this_type, cu);
17666 target_type = die_type (die, cu);
17667 if (target_type != this_type)
17668 TYPE_TARGET_TYPE (this_type) = target_type;
17669 else
17670 {
17671 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17672 spec and cause infinite loops in GDB. */
17673 complaint (_("Self-referential DW_TAG_typedef "
17674 "- DIE at %s [in module %s]"),
17675 sect_offset_str (die->sect_off), objfile_name (objfile));
17676 TYPE_TARGET_TYPE (this_type) = NULL;
17677 }
17678 return this_type;
17679 }
17680
17681 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17682 (which may be different from NAME) to the architecture back-end to allow
17683 it to guess the correct format if necessary. */
17684
17685 static struct type *
17686 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17687 const char *name_hint, enum bfd_endian byte_order)
17688 {
17689 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17690 const struct floatformat **format;
17691 struct type *type;
17692
17693 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17694 if (format)
17695 type = init_float_type (objfile, bits, name, format, byte_order);
17696 else
17697 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17698
17699 return type;
17700 }
17701
17702 /* Allocate an integer type of size BITS and name NAME. */
17703
17704 static struct type *
17705 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17706 int bits, int unsigned_p, const char *name)
17707 {
17708 struct type *type;
17709
17710 /* Versions of Intel's C Compiler generate an integer type called "void"
17711 instead of using DW_TAG_unspecified_type. This has been seen on
17712 at least versions 14, 17, and 18. */
17713 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17714 && strcmp (name, "void") == 0)
17715 type = objfile_type (objfile)->builtin_void;
17716 else
17717 type = init_integer_type (objfile, bits, unsigned_p, name);
17718
17719 return type;
17720 }
17721
17722 /* Initialise and return a floating point type of size BITS suitable for
17723 use as a component of a complex number. The NAME_HINT is passed through
17724 when initialising the floating point type and is the name of the complex
17725 type.
17726
17727 As DWARF doesn't currently provide an explicit name for the components
17728 of a complex number, but it can be helpful to have these components
17729 named, we try to select a suitable name based on the size of the
17730 component. */
17731 static struct type *
17732 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17733 struct objfile *objfile,
17734 int bits, const char *name_hint,
17735 enum bfd_endian byte_order)
17736 {
17737 gdbarch *gdbarch = get_objfile_arch (objfile);
17738 struct type *tt = nullptr;
17739
17740 /* Try to find a suitable floating point builtin type of size BITS.
17741 We're going to use the name of this type as the name for the complex
17742 target type that we are about to create. */
17743 switch (cu->language)
17744 {
17745 case language_fortran:
17746 switch (bits)
17747 {
17748 case 32:
17749 tt = builtin_f_type (gdbarch)->builtin_real;
17750 break;
17751 case 64:
17752 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17753 break;
17754 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17755 case 128:
17756 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17757 break;
17758 }
17759 break;
17760 default:
17761 switch (bits)
17762 {
17763 case 32:
17764 tt = builtin_type (gdbarch)->builtin_float;
17765 break;
17766 case 64:
17767 tt = builtin_type (gdbarch)->builtin_double;
17768 break;
17769 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17770 case 128:
17771 tt = builtin_type (gdbarch)->builtin_long_double;
17772 break;
17773 }
17774 break;
17775 }
17776
17777 /* If the type we found doesn't match the size we were looking for, then
17778 pretend we didn't find a type at all, the complex target type we
17779 create will then be nameless. */
17780 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17781 tt = nullptr;
17782
17783 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17784 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17785 }
17786
17787 /* Find a representation of a given base type and install
17788 it in the TYPE field of the die. */
17789
17790 static struct type *
17791 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17792 {
17793 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17794 struct type *type;
17795 struct attribute *attr;
17796 int encoding = 0, bits = 0;
17797 const char *name;
17798 gdbarch *arch;
17799
17800 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17801 if (attr != nullptr)
17802 encoding = DW_UNSND (attr);
17803 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17804 if (attr != nullptr)
17805 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17806 name = dwarf2_name (die, cu);
17807 if (!name)
17808 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17809
17810 arch = get_objfile_arch (objfile);
17811 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17812
17813 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17814 if (attr)
17815 {
17816 int endianity = DW_UNSND (attr);
17817
17818 switch (endianity)
17819 {
17820 case DW_END_big:
17821 byte_order = BFD_ENDIAN_BIG;
17822 break;
17823 case DW_END_little:
17824 byte_order = BFD_ENDIAN_LITTLE;
17825 break;
17826 default:
17827 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17828 break;
17829 }
17830 }
17831
17832 switch (encoding)
17833 {
17834 case DW_ATE_address:
17835 /* Turn DW_ATE_address into a void * pointer. */
17836 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17837 type = init_pointer_type (objfile, bits, name, type);
17838 break;
17839 case DW_ATE_boolean:
17840 type = init_boolean_type (objfile, bits, 1, name);
17841 break;
17842 case DW_ATE_complex_float:
17843 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17844 byte_order);
17845 type = init_complex_type (objfile, name, type);
17846 break;
17847 case DW_ATE_decimal_float:
17848 type = init_decfloat_type (objfile, bits, name);
17849 break;
17850 case DW_ATE_float:
17851 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17852 break;
17853 case DW_ATE_signed:
17854 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17855 break;
17856 case DW_ATE_unsigned:
17857 if (cu->language == language_fortran
17858 && name
17859 && startswith (name, "character("))
17860 type = init_character_type (objfile, bits, 1, name);
17861 else
17862 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17863 break;
17864 case DW_ATE_signed_char:
17865 if (cu->language == language_ada || cu->language == language_m2
17866 || cu->language == language_pascal
17867 || cu->language == language_fortran)
17868 type = init_character_type (objfile, bits, 0, name);
17869 else
17870 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17871 break;
17872 case DW_ATE_unsigned_char:
17873 if (cu->language == language_ada || cu->language == language_m2
17874 || cu->language == language_pascal
17875 || cu->language == language_fortran
17876 || cu->language == language_rust)
17877 type = init_character_type (objfile, bits, 1, name);
17878 else
17879 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17880 break;
17881 case DW_ATE_UTF:
17882 {
17883 if (bits == 16)
17884 type = builtin_type (arch)->builtin_char16;
17885 else if (bits == 32)
17886 type = builtin_type (arch)->builtin_char32;
17887 else
17888 {
17889 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17890 bits);
17891 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17892 }
17893 return set_die_type (die, type, cu);
17894 }
17895 break;
17896
17897 default:
17898 complaint (_("unsupported DW_AT_encoding: '%s'"),
17899 dwarf_type_encoding_name (encoding));
17900 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17901 break;
17902 }
17903
17904 if (name && strcmp (name, "char") == 0)
17905 TYPE_NOSIGN (type) = 1;
17906
17907 maybe_set_alignment (cu, die, type);
17908
17909 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17910
17911 return set_die_type (die, type, cu);
17912 }
17913
17914 /* Parse dwarf attribute if it's a block, reference or constant and put the
17915 resulting value of the attribute into struct bound_prop.
17916 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17917
17918 static int
17919 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17920 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17921 struct type *default_type)
17922 {
17923 struct dwarf2_property_baton *baton;
17924 struct obstack *obstack
17925 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17926
17927 gdb_assert (default_type != NULL);
17928
17929 if (attr == NULL || prop == NULL)
17930 return 0;
17931
17932 if (attr_form_is_block (attr))
17933 {
17934 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17935 baton->property_type = default_type;
17936 baton->locexpr.per_cu = cu->per_cu;
17937 baton->locexpr.size = DW_BLOCK (attr)->size;
17938 baton->locexpr.data = DW_BLOCK (attr)->data;
17939 switch (attr->name)
17940 {
17941 case DW_AT_string_length:
17942 baton->locexpr.is_reference = true;
17943 break;
17944 default:
17945 baton->locexpr.is_reference = false;
17946 break;
17947 }
17948 prop->data.baton = baton;
17949 prop->kind = PROP_LOCEXPR;
17950 gdb_assert (prop->data.baton != NULL);
17951 }
17952 else if (attr_form_is_ref (attr))
17953 {
17954 struct dwarf2_cu *target_cu = cu;
17955 struct die_info *target_die;
17956 struct attribute *target_attr;
17957
17958 target_die = follow_die_ref (die, attr, &target_cu);
17959 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17960 if (target_attr == NULL)
17961 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17962 target_cu);
17963 if (target_attr == NULL)
17964 return 0;
17965
17966 switch (target_attr->name)
17967 {
17968 case DW_AT_location:
17969 if (attr_form_is_section_offset (target_attr))
17970 {
17971 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17972 baton->property_type = die_type (target_die, target_cu);
17973 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17974 prop->data.baton = baton;
17975 prop->kind = PROP_LOCLIST;
17976 gdb_assert (prop->data.baton != NULL);
17977 }
17978 else if (attr_form_is_block (target_attr))
17979 {
17980 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17981 baton->property_type = die_type (target_die, target_cu);
17982 baton->locexpr.per_cu = cu->per_cu;
17983 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17984 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17985 baton->locexpr.is_reference = true;
17986 prop->data.baton = baton;
17987 prop->kind = PROP_LOCEXPR;
17988 gdb_assert (prop->data.baton != NULL);
17989 }
17990 else
17991 {
17992 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17993 "dynamic property");
17994 return 0;
17995 }
17996 break;
17997 case DW_AT_data_member_location:
17998 {
17999 LONGEST offset;
18000
18001 if (!handle_data_member_location (target_die, target_cu,
18002 &offset))
18003 return 0;
18004
18005 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18006 baton->property_type = read_type_die (target_die->parent,
18007 target_cu);
18008 baton->offset_info.offset = offset;
18009 baton->offset_info.type = die_type (target_die, target_cu);
18010 prop->data.baton = baton;
18011 prop->kind = PROP_ADDR_OFFSET;
18012 break;
18013 }
18014 }
18015 }
18016 else if (attr_form_is_constant (attr))
18017 {
18018 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
18019 prop->kind = PROP_CONST;
18020 }
18021 else
18022 {
18023 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18024 dwarf2_name (die, cu));
18025 return 0;
18026 }
18027
18028 return 1;
18029 }
18030
18031 /* Find an integer type SIZE_IN_BYTES bytes in size and return it.
18032 UNSIGNED_P controls if the integer is unsigned or not. */
18033
18034 static struct type *
18035 dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
18036 int size_in_bytes, bool unsigned_p)
18037 {
18038 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
18039 struct type *int_type;
18040
18041 /* Helper macro to examine the various builtin types. */
18042 #define TRY_TYPE(F) \
18043 int_type = (unsigned_p \
18044 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18045 : objfile_type (objfile)->builtin_ ## F); \
18046 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18047 return int_type
18048
18049 TRY_TYPE (char);
18050 TRY_TYPE (short);
18051 TRY_TYPE (int);
18052 TRY_TYPE (long);
18053 TRY_TYPE (long_long);
18054
18055 #undef TRY_TYPE
18056
18057 gdb_assert_not_reached ("unable to find suitable integer type");
18058 }
18059
18060 /* Find an integer type the same size as the address size given in the
18061 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
18062 is unsigned or not. */
18063
18064 static struct type *
18065 dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
18066 bool unsigned_p)
18067 {
18068 int addr_size = dwarf2_per_cu_addr_size (per_cu);
18069 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
18070 }
18071
18072 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18073 present (which is valid) then compute the default type based on the
18074 compilation units address size. */
18075
18076 static struct type *
18077 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18078 {
18079 struct type *index_type = die_type (die, cu);
18080
18081 /* Dwarf-2 specifications explicitly allows to create subrange types
18082 without specifying a base type.
18083 In that case, the base type must be set to the type of
18084 the lower bound, upper bound or count, in that order, if any of these
18085 three attributes references an object that has a type.
18086 If no base type is found, the Dwarf-2 specifications say that
18087 a signed integer type of size equal to the size of an address should
18088 be used.
18089 For the following C code: `extern char gdb_int [];'
18090 GCC produces an empty range DIE.
18091 FIXME: muller/2010-05-28: Possible references to object for low bound,
18092 high bound or count are not yet handled by this code. */
18093 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
18094 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18095
18096 return index_type;
18097 }
18098
18099 /* Read the given DW_AT_subrange DIE. */
18100
18101 static struct type *
18102 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18103 {
18104 struct type *base_type, *orig_base_type;
18105 struct type *range_type;
18106 struct attribute *attr;
18107 struct dynamic_prop low, high;
18108 int low_default_is_valid;
18109 int high_bound_is_count = 0;
18110 const char *name;
18111 ULONGEST negative_mask;
18112
18113 orig_base_type = read_subrange_index_type (die, cu);
18114
18115 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18116 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18117 creating the range type, but we use the result of check_typedef
18118 when examining properties of the type. */
18119 base_type = check_typedef (orig_base_type);
18120
18121 /* The die_type call above may have already set the type for this DIE. */
18122 range_type = get_die_type (die, cu);
18123 if (range_type)
18124 return range_type;
18125
18126 low.kind = PROP_CONST;
18127 high.kind = PROP_CONST;
18128 high.data.const_val = 0;
18129
18130 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18131 omitting DW_AT_lower_bound. */
18132 switch (cu->language)
18133 {
18134 case language_c:
18135 case language_cplus:
18136 low.data.const_val = 0;
18137 low_default_is_valid = 1;
18138 break;
18139 case language_fortran:
18140 low.data.const_val = 1;
18141 low_default_is_valid = 1;
18142 break;
18143 case language_d:
18144 case language_objc:
18145 case language_rust:
18146 low.data.const_val = 0;
18147 low_default_is_valid = (cu->header.version >= 4);
18148 break;
18149 case language_ada:
18150 case language_m2:
18151 case language_pascal:
18152 low.data.const_val = 1;
18153 low_default_is_valid = (cu->header.version >= 4);
18154 break;
18155 default:
18156 low.data.const_val = 0;
18157 low_default_is_valid = 0;
18158 break;
18159 }
18160
18161 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18162 if (attr != nullptr)
18163 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18164 else if (!low_default_is_valid)
18165 complaint (_("Missing DW_AT_lower_bound "
18166 "- DIE at %s [in module %s]"),
18167 sect_offset_str (die->sect_off),
18168 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18169
18170 struct attribute *attr_ub, *attr_count;
18171 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18172 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18173 {
18174 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18175 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18176 {
18177 /* If bounds are constant do the final calculation here. */
18178 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18179 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18180 else
18181 high_bound_is_count = 1;
18182 }
18183 else
18184 {
18185 if (attr_ub != NULL)
18186 complaint (_("Unresolved DW_AT_upper_bound "
18187 "- DIE at %s [in module %s]"),
18188 sect_offset_str (die->sect_off),
18189 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18190 if (attr_count != NULL)
18191 complaint (_("Unresolved DW_AT_count "
18192 "- DIE at %s [in module %s]"),
18193 sect_offset_str (die->sect_off),
18194 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18195 }
18196 }
18197
18198 LONGEST bias = 0;
18199 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18200 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18201 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18202
18203 /* Normally, the DWARF producers are expected to use a signed
18204 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18205 But this is unfortunately not always the case, as witnessed
18206 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18207 is used instead. To work around that ambiguity, we treat
18208 the bounds as signed, and thus sign-extend their values, when
18209 the base type is signed. */
18210 negative_mask =
18211 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18212 if (low.kind == PROP_CONST
18213 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18214 low.data.const_val |= negative_mask;
18215 if (high.kind == PROP_CONST
18216 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18217 high.data.const_val |= negative_mask;
18218
18219 /* Check for bit and byte strides. */
18220 struct dynamic_prop byte_stride_prop;
18221 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18222 if (attr_byte_stride != nullptr)
18223 {
18224 struct type *prop_type
18225 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18226 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18227 prop_type);
18228 }
18229
18230 struct dynamic_prop bit_stride_prop;
18231 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18232 if (attr_bit_stride != nullptr)
18233 {
18234 /* It only makes sense to have either a bit or byte stride. */
18235 if (attr_byte_stride != nullptr)
18236 {
18237 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18238 "- DIE at %s [in module %s]"),
18239 sect_offset_str (die->sect_off),
18240 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18241 attr_bit_stride = nullptr;
18242 }
18243 else
18244 {
18245 struct type *prop_type
18246 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18247 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18248 prop_type);
18249 }
18250 }
18251
18252 if (attr_byte_stride != nullptr
18253 || attr_bit_stride != nullptr)
18254 {
18255 bool byte_stride_p = (attr_byte_stride != nullptr);
18256 struct dynamic_prop *stride
18257 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18258
18259 range_type
18260 = create_range_type_with_stride (NULL, orig_base_type, &low,
18261 &high, bias, stride, byte_stride_p);
18262 }
18263 else
18264 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18265
18266 if (high_bound_is_count)
18267 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18268
18269 /* Ada expects an empty array on no boundary attributes. */
18270 if (attr == NULL && cu->language != language_ada)
18271 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
18272
18273 name = dwarf2_name (die, cu);
18274 if (name)
18275 TYPE_NAME (range_type) = name;
18276
18277 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18278 if (attr != nullptr)
18279 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18280
18281 maybe_set_alignment (cu, die, range_type);
18282
18283 set_die_type (die, range_type, cu);
18284
18285 /* set_die_type should be already done. */
18286 set_descriptive_type (range_type, die, cu);
18287
18288 return range_type;
18289 }
18290
18291 static struct type *
18292 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18293 {
18294 struct type *type;
18295
18296 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18297 NULL);
18298 TYPE_NAME (type) = dwarf2_name (die, cu);
18299
18300 /* In Ada, an unspecified type is typically used when the description
18301 of the type is deferred to a different unit. When encountering
18302 such a type, we treat it as a stub, and try to resolve it later on,
18303 when needed. */
18304 if (cu->language == language_ada)
18305 TYPE_STUB (type) = 1;
18306
18307 return set_die_type (die, type, cu);
18308 }
18309
18310 /* Read a single die and all its descendents. Set the die's sibling
18311 field to NULL; set other fields in the die correctly, and set all
18312 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18313 location of the info_ptr after reading all of those dies. PARENT
18314 is the parent of the die in question. */
18315
18316 static struct die_info *
18317 read_die_and_children (const struct die_reader_specs *reader,
18318 const gdb_byte *info_ptr,
18319 const gdb_byte **new_info_ptr,
18320 struct die_info *parent)
18321 {
18322 struct die_info *die;
18323 const gdb_byte *cur_ptr;
18324 int has_children;
18325
18326 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18327 if (die == NULL)
18328 {
18329 *new_info_ptr = cur_ptr;
18330 return NULL;
18331 }
18332 store_in_ref_table (die, reader->cu);
18333
18334 if (has_children)
18335 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18336 else
18337 {
18338 die->child = NULL;
18339 *new_info_ptr = cur_ptr;
18340 }
18341
18342 die->sibling = NULL;
18343 die->parent = parent;
18344 return die;
18345 }
18346
18347 /* Read a die, all of its descendents, and all of its siblings; set
18348 all of the fields of all of the dies correctly. Arguments are as
18349 in read_die_and_children. */
18350
18351 static struct die_info *
18352 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18353 const gdb_byte *info_ptr,
18354 const gdb_byte **new_info_ptr,
18355 struct die_info *parent)
18356 {
18357 struct die_info *first_die, *last_sibling;
18358 const gdb_byte *cur_ptr;
18359
18360 cur_ptr = info_ptr;
18361 first_die = last_sibling = NULL;
18362
18363 while (1)
18364 {
18365 struct die_info *die
18366 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18367
18368 if (die == NULL)
18369 {
18370 *new_info_ptr = cur_ptr;
18371 return first_die;
18372 }
18373
18374 if (!first_die)
18375 first_die = die;
18376 else
18377 last_sibling->sibling = die;
18378
18379 last_sibling = die;
18380 }
18381 }
18382
18383 /* Read a die, all of its descendents, and all of its siblings; set
18384 all of the fields of all of the dies correctly. Arguments are as
18385 in read_die_and_children.
18386 This the main entry point for reading a DIE and all its children. */
18387
18388 static struct die_info *
18389 read_die_and_siblings (const struct die_reader_specs *reader,
18390 const gdb_byte *info_ptr,
18391 const gdb_byte **new_info_ptr,
18392 struct die_info *parent)
18393 {
18394 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18395 new_info_ptr, parent);
18396
18397 if (dwarf_die_debug)
18398 {
18399 fprintf_unfiltered (gdb_stdlog,
18400 "Read die from %s@0x%x of %s:\n",
18401 get_section_name (reader->die_section),
18402 (unsigned) (info_ptr - reader->die_section->buffer),
18403 bfd_get_filename (reader->abfd));
18404 dump_die (die, dwarf_die_debug);
18405 }
18406
18407 return die;
18408 }
18409
18410 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18411 attributes.
18412 The caller is responsible for filling in the extra attributes
18413 and updating (*DIEP)->num_attrs.
18414 Set DIEP to point to a newly allocated die with its information,
18415 except for its child, sibling, and parent fields.
18416 Set HAS_CHILDREN to tell whether the die has children or not. */
18417
18418 static const gdb_byte *
18419 read_full_die_1 (const struct die_reader_specs *reader,
18420 struct die_info **diep, const gdb_byte *info_ptr,
18421 int *has_children, int num_extra_attrs)
18422 {
18423 unsigned int abbrev_number, bytes_read, i;
18424 struct abbrev_info *abbrev;
18425 struct die_info *die;
18426 struct dwarf2_cu *cu = reader->cu;
18427 bfd *abfd = reader->abfd;
18428
18429 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18430 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18431 info_ptr += bytes_read;
18432 if (!abbrev_number)
18433 {
18434 *diep = NULL;
18435 *has_children = 0;
18436 return info_ptr;
18437 }
18438
18439 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18440 if (!abbrev)
18441 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18442 abbrev_number,
18443 bfd_get_filename (abfd));
18444
18445 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18446 die->sect_off = sect_off;
18447 die->tag = abbrev->tag;
18448 die->abbrev = abbrev_number;
18449
18450 /* Make the result usable.
18451 The caller needs to update num_attrs after adding the extra
18452 attributes. */
18453 die->num_attrs = abbrev->num_attrs;
18454
18455 for (i = 0; i < abbrev->num_attrs; ++i)
18456 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18457 info_ptr);
18458
18459 *diep = die;
18460 *has_children = abbrev->has_children;
18461 return info_ptr;
18462 }
18463
18464 /* Read a die and all its attributes.
18465 Set DIEP to point to a newly allocated die with its information,
18466 except for its child, sibling, and parent fields.
18467 Set HAS_CHILDREN to tell whether the die has children or not. */
18468
18469 static const gdb_byte *
18470 read_full_die (const struct die_reader_specs *reader,
18471 struct die_info **diep, const gdb_byte *info_ptr,
18472 int *has_children)
18473 {
18474 const gdb_byte *result;
18475
18476 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18477
18478 if (dwarf_die_debug)
18479 {
18480 fprintf_unfiltered (gdb_stdlog,
18481 "Read die from %s@0x%x of %s:\n",
18482 get_section_name (reader->die_section),
18483 (unsigned) (info_ptr - reader->die_section->buffer),
18484 bfd_get_filename (reader->abfd));
18485 dump_die (*diep, dwarf_die_debug);
18486 }
18487
18488 return result;
18489 }
18490 \f
18491 /* Abbreviation tables.
18492
18493 In DWARF version 2, the description of the debugging information is
18494 stored in a separate .debug_abbrev section. Before we read any
18495 dies from a section we read in all abbreviations and install them
18496 in a hash table. */
18497
18498 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18499
18500 struct abbrev_info *
18501 abbrev_table::alloc_abbrev ()
18502 {
18503 struct abbrev_info *abbrev;
18504
18505 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18506 memset (abbrev, 0, sizeof (struct abbrev_info));
18507
18508 return abbrev;
18509 }
18510
18511 /* Add an abbreviation to the table. */
18512
18513 void
18514 abbrev_table::add_abbrev (unsigned int abbrev_number,
18515 struct abbrev_info *abbrev)
18516 {
18517 unsigned int hash_number;
18518
18519 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18520 abbrev->next = m_abbrevs[hash_number];
18521 m_abbrevs[hash_number] = abbrev;
18522 }
18523
18524 /* Look up an abbrev in the table.
18525 Returns NULL if the abbrev is not found. */
18526
18527 struct abbrev_info *
18528 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18529 {
18530 unsigned int hash_number;
18531 struct abbrev_info *abbrev;
18532
18533 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18534 abbrev = m_abbrevs[hash_number];
18535
18536 while (abbrev)
18537 {
18538 if (abbrev->number == abbrev_number)
18539 return abbrev;
18540 abbrev = abbrev->next;
18541 }
18542 return NULL;
18543 }
18544
18545 /* Read in an abbrev table. */
18546
18547 static abbrev_table_up
18548 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18549 struct dwarf2_section_info *section,
18550 sect_offset sect_off)
18551 {
18552 struct objfile *objfile = dwarf2_per_objfile->objfile;
18553 bfd *abfd = get_section_bfd_owner (section);
18554 const gdb_byte *abbrev_ptr;
18555 struct abbrev_info *cur_abbrev;
18556 unsigned int abbrev_number, bytes_read, abbrev_name;
18557 unsigned int abbrev_form;
18558 std::vector<struct attr_abbrev> cur_attrs;
18559
18560 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18561
18562 dwarf2_read_section (objfile, section);
18563 abbrev_ptr = section->buffer + to_underlying (sect_off);
18564 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18565 abbrev_ptr += bytes_read;
18566
18567 /* Loop until we reach an abbrev number of 0. */
18568 while (abbrev_number)
18569 {
18570 cur_attrs.clear ();
18571 cur_abbrev = abbrev_table->alloc_abbrev ();
18572
18573 /* read in abbrev header */
18574 cur_abbrev->number = abbrev_number;
18575 cur_abbrev->tag
18576 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18577 abbrev_ptr += bytes_read;
18578 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18579 abbrev_ptr += 1;
18580
18581 /* now read in declarations */
18582 for (;;)
18583 {
18584 LONGEST implicit_const;
18585
18586 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18587 abbrev_ptr += bytes_read;
18588 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18589 abbrev_ptr += bytes_read;
18590 if (abbrev_form == DW_FORM_implicit_const)
18591 {
18592 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18593 &bytes_read);
18594 abbrev_ptr += bytes_read;
18595 }
18596 else
18597 {
18598 /* Initialize it due to a false compiler warning. */
18599 implicit_const = -1;
18600 }
18601
18602 if (abbrev_name == 0)
18603 break;
18604
18605 cur_attrs.emplace_back ();
18606 struct attr_abbrev &cur_attr = cur_attrs.back ();
18607 cur_attr.name = (enum dwarf_attribute) abbrev_name;
18608 cur_attr.form = (enum dwarf_form) abbrev_form;
18609 cur_attr.implicit_const = implicit_const;
18610 ++cur_abbrev->num_attrs;
18611 }
18612
18613 cur_abbrev->attrs =
18614 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18615 cur_abbrev->num_attrs);
18616 memcpy (cur_abbrev->attrs, cur_attrs.data (),
18617 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18618
18619 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18620
18621 /* Get next abbreviation.
18622 Under Irix6 the abbreviations for a compilation unit are not
18623 always properly terminated with an abbrev number of 0.
18624 Exit loop if we encounter an abbreviation which we have
18625 already read (which means we are about to read the abbreviations
18626 for the next compile unit) or if the end of the abbreviation
18627 table is reached. */
18628 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18629 break;
18630 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18631 abbrev_ptr += bytes_read;
18632 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18633 break;
18634 }
18635
18636 return abbrev_table;
18637 }
18638
18639 /* Returns nonzero if TAG represents a type that we might generate a partial
18640 symbol for. */
18641
18642 static int
18643 is_type_tag_for_partial (int tag)
18644 {
18645 switch (tag)
18646 {
18647 #if 0
18648 /* Some types that would be reasonable to generate partial symbols for,
18649 that we don't at present. */
18650 case DW_TAG_array_type:
18651 case DW_TAG_file_type:
18652 case DW_TAG_ptr_to_member_type:
18653 case DW_TAG_set_type:
18654 case DW_TAG_string_type:
18655 case DW_TAG_subroutine_type:
18656 #endif
18657 case DW_TAG_base_type:
18658 case DW_TAG_class_type:
18659 case DW_TAG_interface_type:
18660 case DW_TAG_enumeration_type:
18661 case DW_TAG_structure_type:
18662 case DW_TAG_subrange_type:
18663 case DW_TAG_typedef:
18664 case DW_TAG_union_type:
18665 return 1;
18666 default:
18667 return 0;
18668 }
18669 }
18670
18671 /* Load all DIEs that are interesting for partial symbols into memory. */
18672
18673 static struct partial_die_info *
18674 load_partial_dies (const struct die_reader_specs *reader,
18675 const gdb_byte *info_ptr, int building_psymtab)
18676 {
18677 struct dwarf2_cu *cu = reader->cu;
18678 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18679 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18680 unsigned int bytes_read;
18681 unsigned int load_all = 0;
18682 int nesting_level = 1;
18683
18684 parent_die = NULL;
18685 last_die = NULL;
18686
18687 gdb_assert (cu->per_cu != NULL);
18688 if (cu->per_cu->load_all_dies)
18689 load_all = 1;
18690
18691 cu->partial_dies
18692 = htab_create_alloc_ex (cu->header.length / 12,
18693 partial_die_hash,
18694 partial_die_eq,
18695 NULL,
18696 &cu->comp_unit_obstack,
18697 hashtab_obstack_allocate,
18698 dummy_obstack_deallocate);
18699
18700 while (1)
18701 {
18702 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18703
18704 /* A NULL abbrev means the end of a series of children. */
18705 if (abbrev == NULL)
18706 {
18707 if (--nesting_level == 0)
18708 return first_die;
18709
18710 info_ptr += bytes_read;
18711 last_die = parent_die;
18712 parent_die = parent_die->die_parent;
18713 continue;
18714 }
18715
18716 /* Check for template arguments. We never save these; if
18717 they're seen, we just mark the parent, and go on our way. */
18718 if (parent_die != NULL
18719 && cu->language == language_cplus
18720 && (abbrev->tag == DW_TAG_template_type_param
18721 || abbrev->tag == DW_TAG_template_value_param))
18722 {
18723 parent_die->has_template_arguments = 1;
18724
18725 if (!load_all)
18726 {
18727 /* We don't need a partial DIE for the template argument. */
18728 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18729 continue;
18730 }
18731 }
18732
18733 /* We only recurse into c++ subprograms looking for template arguments.
18734 Skip their other children. */
18735 if (!load_all
18736 && cu->language == language_cplus
18737 && parent_die != NULL
18738 && parent_die->tag == DW_TAG_subprogram)
18739 {
18740 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18741 continue;
18742 }
18743
18744 /* Check whether this DIE is interesting enough to save. Normally
18745 we would not be interested in members here, but there may be
18746 later variables referencing them via DW_AT_specification (for
18747 static members). */
18748 if (!load_all
18749 && !is_type_tag_for_partial (abbrev->tag)
18750 && abbrev->tag != DW_TAG_constant
18751 && abbrev->tag != DW_TAG_enumerator
18752 && abbrev->tag != DW_TAG_subprogram
18753 && abbrev->tag != DW_TAG_inlined_subroutine
18754 && abbrev->tag != DW_TAG_lexical_block
18755 && abbrev->tag != DW_TAG_variable
18756 && abbrev->tag != DW_TAG_namespace
18757 && abbrev->tag != DW_TAG_module
18758 && abbrev->tag != DW_TAG_member
18759 && abbrev->tag != DW_TAG_imported_unit
18760 && abbrev->tag != DW_TAG_imported_declaration)
18761 {
18762 /* Otherwise we skip to the next sibling, if any. */
18763 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18764 continue;
18765 }
18766
18767 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18768 abbrev);
18769
18770 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18771
18772 /* This two-pass algorithm for processing partial symbols has a
18773 high cost in cache pressure. Thus, handle some simple cases
18774 here which cover the majority of C partial symbols. DIEs
18775 which neither have specification tags in them, nor could have
18776 specification tags elsewhere pointing at them, can simply be
18777 processed and discarded.
18778
18779 This segment is also optional; scan_partial_symbols and
18780 add_partial_symbol will handle these DIEs if we chain
18781 them in normally. When compilers which do not emit large
18782 quantities of duplicate debug information are more common,
18783 this code can probably be removed. */
18784
18785 /* Any complete simple types at the top level (pretty much all
18786 of them, for a language without namespaces), can be processed
18787 directly. */
18788 if (parent_die == NULL
18789 && pdi.has_specification == 0
18790 && pdi.is_declaration == 0
18791 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18792 || pdi.tag == DW_TAG_base_type
18793 || pdi.tag == DW_TAG_subrange_type))
18794 {
18795 if (building_psymtab && pdi.name != NULL)
18796 add_psymbol_to_list (pdi.name, false,
18797 VAR_DOMAIN, LOC_TYPEDEF, -1,
18798 psymbol_placement::STATIC,
18799 0, cu->language, objfile);
18800 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18801 continue;
18802 }
18803
18804 /* The exception for DW_TAG_typedef with has_children above is
18805 a workaround of GCC PR debug/47510. In the case of this complaint
18806 type_name_or_error will error on such types later.
18807
18808 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18809 it could not find the child DIEs referenced later, this is checked
18810 above. In correct DWARF DW_TAG_typedef should have no children. */
18811
18812 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18813 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18814 "- DIE at %s [in module %s]"),
18815 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18816
18817 /* If we're at the second level, and we're an enumerator, and
18818 our parent has no specification (meaning possibly lives in a
18819 namespace elsewhere), then we can add the partial symbol now
18820 instead of queueing it. */
18821 if (pdi.tag == DW_TAG_enumerator
18822 && parent_die != NULL
18823 && parent_die->die_parent == NULL
18824 && parent_die->tag == DW_TAG_enumeration_type
18825 && parent_die->has_specification == 0)
18826 {
18827 if (pdi.name == NULL)
18828 complaint (_("malformed enumerator DIE ignored"));
18829 else if (building_psymtab)
18830 add_psymbol_to_list (pdi.name, false,
18831 VAR_DOMAIN, LOC_CONST, -1,
18832 cu->language == language_cplus
18833 ? psymbol_placement::GLOBAL
18834 : psymbol_placement::STATIC,
18835 0, cu->language, objfile);
18836
18837 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18838 continue;
18839 }
18840
18841 struct partial_die_info *part_die
18842 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18843
18844 /* We'll save this DIE so link it in. */
18845 part_die->die_parent = parent_die;
18846 part_die->die_sibling = NULL;
18847 part_die->die_child = NULL;
18848
18849 if (last_die && last_die == parent_die)
18850 last_die->die_child = part_die;
18851 else if (last_die)
18852 last_die->die_sibling = part_die;
18853
18854 last_die = part_die;
18855
18856 if (first_die == NULL)
18857 first_die = part_die;
18858
18859 /* Maybe add the DIE to the hash table. Not all DIEs that we
18860 find interesting need to be in the hash table, because we
18861 also have the parent/sibling/child chains; only those that we
18862 might refer to by offset later during partial symbol reading.
18863
18864 For now this means things that might have be the target of a
18865 DW_AT_specification, DW_AT_abstract_origin, or
18866 DW_AT_extension. DW_AT_extension will refer only to
18867 namespaces; DW_AT_abstract_origin refers to functions (and
18868 many things under the function DIE, but we do not recurse
18869 into function DIEs during partial symbol reading) and
18870 possibly variables as well; DW_AT_specification refers to
18871 declarations. Declarations ought to have the DW_AT_declaration
18872 flag. It happens that GCC forgets to put it in sometimes, but
18873 only for functions, not for types.
18874
18875 Adding more things than necessary to the hash table is harmless
18876 except for the performance cost. Adding too few will result in
18877 wasted time in find_partial_die, when we reread the compilation
18878 unit with load_all_dies set. */
18879
18880 if (load_all
18881 || abbrev->tag == DW_TAG_constant
18882 || abbrev->tag == DW_TAG_subprogram
18883 || abbrev->tag == DW_TAG_variable
18884 || abbrev->tag == DW_TAG_namespace
18885 || part_die->is_declaration)
18886 {
18887 void **slot;
18888
18889 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18890 to_underlying (part_die->sect_off),
18891 INSERT);
18892 *slot = part_die;
18893 }
18894
18895 /* For some DIEs we want to follow their children (if any). For C
18896 we have no reason to follow the children of structures; for other
18897 languages we have to, so that we can get at method physnames
18898 to infer fully qualified class names, for DW_AT_specification,
18899 and for C++ template arguments. For C++, we also look one level
18900 inside functions to find template arguments (if the name of the
18901 function does not already contain the template arguments).
18902
18903 For Ada and Fortran, we need to scan the children of subprograms
18904 and lexical blocks as well because these languages allow the
18905 definition of nested entities that could be interesting for the
18906 debugger, such as nested subprograms for instance. */
18907 if (last_die->has_children
18908 && (load_all
18909 || last_die->tag == DW_TAG_namespace
18910 || last_die->tag == DW_TAG_module
18911 || last_die->tag == DW_TAG_enumeration_type
18912 || (cu->language == language_cplus
18913 && last_die->tag == DW_TAG_subprogram
18914 && (last_die->name == NULL
18915 || strchr (last_die->name, '<') == NULL))
18916 || (cu->language != language_c
18917 && (last_die->tag == DW_TAG_class_type
18918 || last_die->tag == DW_TAG_interface_type
18919 || last_die->tag == DW_TAG_structure_type
18920 || last_die->tag == DW_TAG_union_type))
18921 || ((cu->language == language_ada
18922 || cu->language == language_fortran)
18923 && (last_die->tag == DW_TAG_subprogram
18924 || last_die->tag == DW_TAG_lexical_block))))
18925 {
18926 nesting_level++;
18927 parent_die = last_die;
18928 continue;
18929 }
18930
18931 /* Otherwise we skip to the next sibling, if any. */
18932 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18933
18934 /* Back to the top, do it again. */
18935 }
18936 }
18937
18938 partial_die_info::partial_die_info (sect_offset sect_off_,
18939 struct abbrev_info *abbrev)
18940 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18941 {
18942 }
18943
18944 /* Read a minimal amount of information into the minimal die structure.
18945 INFO_PTR should point just after the initial uleb128 of a DIE. */
18946
18947 const gdb_byte *
18948 partial_die_info::read (const struct die_reader_specs *reader,
18949 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18950 {
18951 struct dwarf2_cu *cu = reader->cu;
18952 struct dwarf2_per_objfile *dwarf2_per_objfile
18953 = cu->per_cu->dwarf2_per_objfile;
18954 unsigned int i;
18955 int has_low_pc_attr = 0;
18956 int has_high_pc_attr = 0;
18957 int high_pc_relative = 0;
18958
18959 for (i = 0; i < abbrev.num_attrs; ++i)
18960 {
18961 struct attribute attr;
18962
18963 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18964
18965 /* Store the data if it is of an attribute we want to keep in a
18966 partial symbol table. */
18967 switch (attr.name)
18968 {
18969 case DW_AT_name:
18970 switch (tag)
18971 {
18972 case DW_TAG_compile_unit:
18973 case DW_TAG_partial_unit:
18974 case DW_TAG_type_unit:
18975 /* Compilation units have a DW_AT_name that is a filename, not
18976 a source language identifier. */
18977 case DW_TAG_enumeration_type:
18978 case DW_TAG_enumerator:
18979 /* These tags always have simple identifiers already; no need
18980 to canonicalize them. */
18981 name = DW_STRING (&attr);
18982 break;
18983 default:
18984 {
18985 struct objfile *objfile = dwarf2_per_objfile->objfile;
18986
18987 name
18988 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18989 &objfile->per_bfd->storage_obstack);
18990 }
18991 break;
18992 }
18993 break;
18994 case DW_AT_linkage_name:
18995 case DW_AT_MIPS_linkage_name:
18996 /* Note that both forms of linkage name might appear. We
18997 assume they will be the same, and we only store the last
18998 one we see. */
18999 linkage_name = DW_STRING (&attr);
19000 break;
19001 case DW_AT_low_pc:
19002 has_low_pc_attr = 1;
19003 lowpc = attr_value_as_address (&attr);
19004 break;
19005 case DW_AT_high_pc:
19006 has_high_pc_attr = 1;
19007 highpc = attr_value_as_address (&attr);
19008 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
19009 high_pc_relative = 1;
19010 break;
19011 case DW_AT_location:
19012 /* Support the .debug_loc offsets. */
19013 if (attr_form_is_block (&attr))
19014 {
19015 d.locdesc = DW_BLOCK (&attr);
19016 }
19017 else if (attr_form_is_section_offset (&attr))
19018 {
19019 dwarf2_complex_location_expr_complaint ();
19020 }
19021 else
19022 {
19023 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19024 "partial symbol information");
19025 }
19026 break;
19027 case DW_AT_external:
19028 is_external = DW_UNSND (&attr);
19029 break;
19030 case DW_AT_declaration:
19031 is_declaration = DW_UNSND (&attr);
19032 break;
19033 case DW_AT_type:
19034 has_type = 1;
19035 break;
19036 case DW_AT_abstract_origin:
19037 case DW_AT_specification:
19038 case DW_AT_extension:
19039 has_specification = 1;
19040 spec_offset = dwarf2_get_ref_die_offset (&attr);
19041 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19042 || cu->per_cu->is_dwz);
19043 break;
19044 case DW_AT_sibling:
19045 /* Ignore absolute siblings, they might point outside of
19046 the current compile unit. */
19047 if (attr.form == DW_FORM_ref_addr)
19048 complaint (_("ignoring absolute DW_AT_sibling"));
19049 else
19050 {
19051 const gdb_byte *buffer = reader->buffer;
19052 sect_offset off = dwarf2_get_ref_die_offset (&attr);
19053 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19054
19055 if (sibling_ptr < info_ptr)
19056 complaint (_("DW_AT_sibling points backwards"));
19057 else if (sibling_ptr > reader->buffer_end)
19058 dwarf2_section_buffer_overflow_complaint (reader->die_section);
19059 else
19060 sibling = sibling_ptr;
19061 }
19062 break;
19063 case DW_AT_byte_size:
19064 has_byte_size = 1;
19065 break;
19066 case DW_AT_const_value:
19067 has_const_value = 1;
19068 break;
19069 case DW_AT_calling_convention:
19070 /* DWARF doesn't provide a way to identify a program's source-level
19071 entry point. DW_AT_calling_convention attributes are only meant
19072 to describe functions' calling conventions.
19073
19074 However, because it's a necessary piece of information in
19075 Fortran, and before DWARF 4 DW_CC_program was the only
19076 piece of debugging information whose definition refers to
19077 a 'main program' at all, several compilers marked Fortran
19078 main programs with DW_CC_program --- even when those
19079 functions use the standard calling conventions.
19080
19081 Although DWARF now specifies a way to provide this
19082 information, we support this practice for backward
19083 compatibility. */
19084 if (DW_UNSND (&attr) == DW_CC_program
19085 && cu->language == language_fortran)
19086 main_subprogram = 1;
19087 break;
19088 case DW_AT_inline:
19089 if (DW_UNSND (&attr) == DW_INL_inlined
19090 || DW_UNSND (&attr) == DW_INL_declared_inlined)
19091 may_be_inlined = 1;
19092 break;
19093
19094 case DW_AT_import:
19095 if (tag == DW_TAG_imported_unit)
19096 {
19097 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19098 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19099 || cu->per_cu->is_dwz);
19100 }
19101 break;
19102
19103 case DW_AT_main_subprogram:
19104 main_subprogram = DW_UNSND (&attr);
19105 break;
19106
19107 case DW_AT_ranges:
19108 {
19109 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19110 but that requires a full DIE, so instead we just
19111 reimplement it. */
19112 int need_ranges_base = tag != DW_TAG_compile_unit;
19113 unsigned int ranges_offset = (DW_UNSND (&attr)
19114 + (need_ranges_base
19115 ? cu->ranges_base
19116 : 0));
19117
19118 /* Value of the DW_AT_ranges attribute is the offset in the
19119 .debug_ranges section. */
19120 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19121 nullptr))
19122 has_pc_info = 1;
19123 }
19124 break;
19125
19126 default:
19127 break;
19128 }
19129 }
19130
19131 /* For Ada, if both the name and the linkage name appear, we prefer
19132 the latter. This lets "catch exception" work better, regardless
19133 of the order in which the name and linkage name were emitted.
19134 Really, though, this is just a workaround for the fact that gdb
19135 doesn't store both the name and the linkage name. */
19136 if (cu->language == language_ada && linkage_name != nullptr)
19137 name = linkage_name;
19138
19139 if (high_pc_relative)
19140 highpc += lowpc;
19141
19142 if (has_low_pc_attr && has_high_pc_attr)
19143 {
19144 /* When using the GNU linker, .gnu.linkonce. sections are used to
19145 eliminate duplicate copies of functions and vtables and such.
19146 The linker will arbitrarily choose one and discard the others.
19147 The AT_*_pc values for such functions refer to local labels in
19148 these sections. If the section from that file was discarded, the
19149 labels are not in the output, so the relocs get a value of 0.
19150 If this is a discarded function, mark the pc bounds as invalid,
19151 so that GDB will ignore it. */
19152 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
19153 {
19154 struct objfile *objfile = dwarf2_per_objfile->objfile;
19155 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19156
19157 complaint (_("DW_AT_low_pc %s is zero "
19158 "for DIE at %s [in module %s]"),
19159 paddress (gdbarch, lowpc),
19160 sect_offset_str (sect_off),
19161 objfile_name (objfile));
19162 }
19163 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19164 else if (lowpc >= highpc)
19165 {
19166 struct objfile *objfile = dwarf2_per_objfile->objfile;
19167 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19168
19169 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19170 "for DIE at %s [in module %s]"),
19171 paddress (gdbarch, lowpc),
19172 paddress (gdbarch, highpc),
19173 sect_offset_str (sect_off),
19174 objfile_name (objfile));
19175 }
19176 else
19177 has_pc_info = 1;
19178 }
19179
19180 return info_ptr;
19181 }
19182
19183 /* Find a cached partial DIE at OFFSET in CU. */
19184
19185 struct partial_die_info *
19186 dwarf2_cu::find_partial_die (sect_offset sect_off)
19187 {
19188 struct partial_die_info *lookup_die = NULL;
19189 struct partial_die_info part_die (sect_off);
19190
19191 lookup_die = ((struct partial_die_info *)
19192 htab_find_with_hash (partial_dies, &part_die,
19193 to_underlying (sect_off)));
19194
19195 return lookup_die;
19196 }
19197
19198 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19199 except in the case of .debug_types DIEs which do not reference
19200 outside their CU (they do however referencing other types via
19201 DW_FORM_ref_sig8). */
19202
19203 static const struct cu_partial_die_info
19204 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19205 {
19206 struct dwarf2_per_objfile *dwarf2_per_objfile
19207 = cu->per_cu->dwarf2_per_objfile;
19208 struct objfile *objfile = dwarf2_per_objfile->objfile;
19209 struct dwarf2_per_cu_data *per_cu = NULL;
19210 struct partial_die_info *pd = NULL;
19211
19212 if (offset_in_dwz == cu->per_cu->is_dwz
19213 && offset_in_cu_p (&cu->header, sect_off))
19214 {
19215 pd = cu->find_partial_die (sect_off);
19216 if (pd != NULL)
19217 return { cu, pd };
19218 /* We missed recording what we needed.
19219 Load all dies and try again. */
19220 per_cu = cu->per_cu;
19221 }
19222 else
19223 {
19224 /* TUs don't reference other CUs/TUs (except via type signatures). */
19225 if (cu->per_cu->is_debug_types)
19226 {
19227 error (_("Dwarf Error: Type Unit at offset %s contains"
19228 " external reference to offset %s [in module %s].\n"),
19229 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19230 bfd_get_filename (objfile->obfd));
19231 }
19232 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19233 dwarf2_per_objfile);
19234
19235 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19236 load_partial_comp_unit (per_cu);
19237
19238 per_cu->cu->last_used = 0;
19239 pd = per_cu->cu->find_partial_die (sect_off);
19240 }
19241
19242 /* If we didn't find it, and not all dies have been loaded,
19243 load them all and try again. */
19244
19245 if (pd == NULL && per_cu->load_all_dies == 0)
19246 {
19247 per_cu->load_all_dies = 1;
19248
19249 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19250 THIS_CU->cu may already be in use. So we can't just free it and
19251 replace its DIEs with the ones we read in. Instead, we leave those
19252 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19253 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19254 set. */
19255 load_partial_comp_unit (per_cu);
19256
19257 pd = per_cu->cu->find_partial_die (sect_off);
19258 }
19259
19260 if (pd == NULL)
19261 internal_error (__FILE__, __LINE__,
19262 _("could not find partial DIE %s "
19263 "in cache [from module %s]\n"),
19264 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19265 return { per_cu->cu, pd };
19266 }
19267
19268 /* See if we can figure out if the class lives in a namespace. We do
19269 this by looking for a member function; its demangled name will
19270 contain namespace info, if there is any. */
19271
19272 static void
19273 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19274 struct dwarf2_cu *cu)
19275 {
19276 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19277 what template types look like, because the demangler
19278 frequently doesn't give the same name as the debug info. We
19279 could fix this by only using the demangled name to get the
19280 prefix (but see comment in read_structure_type). */
19281
19282 struct partial_die_info *real_pdi;
19283 struct partial_die_info *child_pdi;
19284
19285 /* If this DIE (this DIE's specification, if any) has a parent, then
19286 we should not do this. We'll prepend the parent's fully qualified
19287 name when we create the partial symbol. */
19288
19289 real_pdi = struct_pdi;
19290 while (real_pdi->has_specification)
19291 {
19292 auto res = find_partial_die (real_pdi->spec_offset,
19293 real_pdi->spec_is_dwz, cu);
19294 real_pdi = res.pdi;
19295 cu = res.cu;
19296 }
19297
19298 if (real_pdi->die_parent != NULL)
19299 return;
19300
19301 for (child_pdi = struct_pdi->die_child;
19302 child_pdi != NULL;
19303 child_pdi = child_pdi->die_sibling)
19304 {
19305 if (child_pdi->tag == DW_TAG_subprogram
19306 && child_pdi->linkage_name != NULL)
19307 {
19308 gdb::unique_xmalloc_ptr<char> actual_class_name
19309 (language_class_name_from_physname (cu->language_defn,
19310 child_pdi->linkage_name));
19311 if (actual_class_name != NULL)
19312 {
19313 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19314 struct_pdi->name
19315 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19316 actual_class_name.get ());
19317 }
19318 break;
19319 }
19320 }
19321 }
19322
19323 void
19324 partial_die_info::fixup (struct dwarf2_cu *cu)
19325 {
19326 /* Once we've fixed up a die, there's no point in doing so again.
19327 This also avoids a memory leak if we were to call
19328 guess_partial_die_structure_name multiple times. */
19329 if (fixup_called)
19330 return;
19331
19332 /* If we found a reference attribute and the DIE has no name, try
19333 to find a name in the referred to DIE. */
19334
19335 if (name == NULL && has_specification)
19336 {
19337 struct partial_die_info *spec_die;
19338
19339 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19340 spec_die = res.pdi;
19341 cu = res.cu;
19342
19343 spec_die->fixup (cu);
19344
19345 if (spec_die->name)
19346 {
19347 name = spec_die->name;
19348
19349 /* Copy DW_AT_external attribute if it is set. */
19350 if (spec_die->is_external)
19351 is_external = spec_die->is_external;
19352 }
19353 }
19354
19355 /* Set default names for some unnamed DIEs. */
19356
19357 if (name == NULL && tag == DW_TAG_namespace)
19358 name = CP_ANONYMOUS_NAMESPACE_STR;
19359
19360 /* If there is no parent die to provide a namespace, and there are
19361 children, see if we can determine the namespace from their linkage
19362 name. */
19363 if (cu->language == language_cplus
19364 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
19365 && die_parent == NULL
19366 && has_children
19367 && (tag == DW_TAG_class_type
19368 || tag == DW_TAG_structure_type
19369 || tag == DW_TAG_union_type))
19370 guess_partial_die_structure_name (this, cu);
19371
19372 /* GCC might emit a nameless struct or union that has a linkage
19373 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19374 if (name == NULL
19375 && (tag == DW_TAG_class_type
19376 || tag == DW_TAG_interface_type
19377 || tag == DW_TAG_structure_type
19378 || tag == DW_TAG_union_type)
19379 && linkage_name != NULL)
19380 {
19381 gdb::unique_xmalloc_ptr<char> demangled
19382 (gdb_demangle (linkage_name, DMGL_TYPES));
19383 if (demangled != nullptr)
19384 {
19385 const char *base;
19386
19387 /* Strip any leading namespaces/classes, keep only the base name.
19388 DW_AT_name for named DIEs does not contain the prefixes. */
19389 base = strrchr (demangled.get (), ':');
19390 if (base && base > demangled.get () && base[-1] == ':')
19391 base++;
19392 else
19393 base = demangled.get ();
19394
19395 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19396 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
19397 }
19398 }
19399
19400 fixup_called = 1;
19401 }
19402
19403 /* Read an attribute value described by an attribute form. */
19404
19405 static const gdb_byte *
19406 read_attribute_value (const struct die_reader_specs *reader,
19407 struct attribute *attr, unsigned form,
19408 LONGEST implicit_const, const gdb_byte *info_ptr)
19409 {
19410 struct dwarf2_cu *cu = reader->cu;
19411 struct dwarf2_per_objfile *dwarf2_per_objfile
19412 = cu->per_cu->dwarf2_per_objfile;
19413 struct objfile *objfile = dwarf2_per_objfile->objfile;
19414 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19415 bfd *abfd = reader->abfd;
19416 struct comp_unit_head *cu_header = &cu->header;
19417 unsigned int bytes_read;
19418 struct dwarf_block *blk;
19419
19420 attr->form = (enum dwarf_form) form;
19421 switch (form)
19422 {
19423 case DW_FORM_ref_addr:
19424 if (cu->header.version == 2)
19425 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19426 else
19427 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19428 &cu->header, &bytes_read);
19429 info_ptr += bytes_read;
19430 break;
19431 case DW_FORM_GNU_ref_alt:
19432 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19433 info_ptr += bytes_read;
19434 break;
19435 case DW_FORM_addr:
19436 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19437 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19438 info_ptr += bytes_read;
19439 break;
19440 case DW_FORM_block2:
19441 blk = dwarf_alloc_block (cu);
19442 blk->size = read_2_bytes (abfd, info_ptr);
19443 info_ptr += 2;
19444 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19445 info_ptr += blk->size;
19446 DW_BLOCK (attr) = blk;
19447 break;
19448 case DW_FORM_block4:
19449 blk = dwarf_alloc_block (cu);
19450 blk->size = read_4_bytes (abfd, info_ptr);
19451 info_ptr += 4;
19452 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19453 info_ptr += blk->size;
19454 DW_BLOCK (attr) = blk;
19455 break;
19456 case DW_FORM_data2:
19457 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19458 info_ptr += 2;
19459 break;
19460 case DW_FORM_data4:
19461 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19462 info_ptr += 4;
19463 break;
19464 case DW_FORM_data8:
19465 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19466 info_ptr += 8;
19467 break;
19468 case DW_FORM_data16:
19469 blk = dwarf_alloc_block (cu);
19470 blk->size = 16;
19471 blk->data = read_n_bytes (abfd, info_ptr, 16);
19472 info_ptr += 16;
19473 DW_BLOCK (attr) = blk;
19474 break;
19475 case DW_FORM_sec_offset:
19476 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19477 info_ptr += bytes_read;
19478 break;
19479 case DW_FORM_string:
19480 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19481 DW_STRING_IS_CANONICAL (attr) = 0;
19482 info_ptr += bytes_read;
19483 break;
19484 case DW_FORM_strp:
19485 if (!cu->per_cu->is_dwz)
19486 {
19487 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19488 abfd, info_ptr, cu_header,
19489 &bytes_read);
19490 DW_STRING_IS_CANONICAL (attr) = 0;
19491 info_ptr += bytes_read;
19492 break;
19493 }
19494 /* FALLTHROUGH */
19495 case DW_FORM_line_strp:
19496 if (!cu->per_cu->is_dwz)
19497 {
19498 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19499 abfd, info_ptr,
19500 cu_header, &bytes_read);
19501 DW_STRING_IS_CANONICAL (attr) = 0;
19502 info_ptr += bytes_read;
19503 break;
19504 }
19505 /* FALLTHROUGH */
19506 case DW_FORM_GNU_strp_alt:
19507 {
19508 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19509 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19510 &bytes_read);
19511
19512 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19513 dwz, str_offset);
19514 DW_STRING_IS_CANONICAL (attr) = 0;
19515 info_ptr += bytes_read;
19516 }
19517 break;
19518 case DW_FORM_exprloc:
19519 case DW_FORM_block:
19520 blk = dwarf_alloc_block (cu);
19521 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19522 info_ptr += bytes_read;
19523 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19524 info_ptr += blk->size;
19525 DW_BLOCK (attr) = blk;
19526 break;
19527 case DW_FORM_block1:
19528 blk = dwarf_alloc_block (cu);
19529 blk->size = read_1_byte (abfd, info_ptr);
19530 info_ptr += 1;
19531 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19532 info_ptr += blk->size;
19533 DW_BLOCK (attr) = blk;
19534 break;
19535 case DW_FORM_data1:
19536 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19537 info_ptr += 1;
19538 break;
19539 case DW_FORM_flag:
19540 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19541 info_ptr += 1;
19542 break;
19543 case DW_FORM_flag_present:
19544 DW_UNSND (attr) = 1;
19545 break;
19546 case DW_FORM_sdata:
19547 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19548 info_ptr += bytes_read;
19549 break;
19550 case DW_FORM_udata:
19551 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19552 info_ptr += bytes_read;
19553 break;
19554 case DW_FORM_ref1:
19555 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19556 + read_1_byte (abfd, info_ptr));
19557 info_ptr += 1;
19558 break;
19559 case DW_FORM_ref2:
19560 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19561 + read_2_bytes (abfd, info_ptr));
19562 info_ptr += 2;
19563 break;
19564 case DW_FORM_ref4:
19565 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19566 + read_4_bytes (abfd, info_ptr));
19567 info_ptr += 4;
19568 break;
19569 case DW_FORM_ref8:
19570 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19571 + read_8_bytes (abfd, info_ptr));
19572 info_ptr += 8;
19573 break;
19574 case DW_FORM_ref_sig8:
19575 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19576 info_ptr += 8;
19577 break;
19578 case DW_FORM_ref_udata:
19579 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19580 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19581 info_ptr += bytes_read;
19582 break;
19583 case DW_FORM_indirect:
19584 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19585 info_ptr += bytes_read;
19586 if (form == DW_FORM_implicit_const)
19587 {
19588 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19589 info_ptr += bytes_read;
19590 }
19591 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19592 info_ptr);
19593 break;
19594 case DW_FORM_implicit_const:
19595 DW_SND (attr) = implicit_const;
19596 break;
19597 case DW_FORM_addrx:
19598 case DW_FORM_GNU_addr_index:
19599 if (reader->dwo_file == NULL)
19600 {
19601 /* For now flag a hard error.
19602 Later we can turn this into a complaint. */
19603 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19604 dwarf_form_name (form),
19605 bfd_get_filename (abfd));
19606 }
19607 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19608 info_ptr += bytes_read;
19609 break;
19610 case DW_FORM_strx:
19611 case DW_FORM_strx1:
19612 case DW_FORM_strx2:
19613 case DW_FORM_strx3:
19614 case DW_FORM_strx4:
19615 case DW_FORM_GNU_str_index:
19616 if (reader->dwo_file == NULL)
19617 {
19618 /* For now flag a hard error.
19619 Later we can turn this into a complaint if warranted. */
19620 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19621 dwarf_form_name (form),
19622 bfd_get_filename (abfd));
19623 }
19624 {
19625 ULONGEST str_index;
19626 if (form == DW_FORM_strx1)
19627 {
19628 str_index = read_1_byte (abfd, info_ptr);
19629 info_ptr += 1;
19630 }
19631 else if (form == DW_FORM_strx2)
19632 {
19633 str_index = read_2_bytes (abfd, info_ptr);
19634 info_ptr += 2;
19635 }
19636 else if (form == DW_FORM_strx3)
19637 {
19638 str_index = read_3_bytes (abfd, info_ptr);
19639 info_ptr += 3;
19640 }
19641 else if (form == DW_FORM_strx4)
19642 {
19643 str_index = read_4_bytes (abfd, info_ptr);
19644 info_ptr += 4;
19645 }
19646 else
19647 {
19648 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19649 info_ptr += bytes_read;
19650 }
19651 DW_STRING (attr) = read_str_index (reader, str_index);
19652 DW_STRING_IS_CANONICAL (attr) = 0;
19653 }
19654 break;
19655 default:
19656 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19657 dwarf_form_name (form),
19658 bfd_get_filename (abfd));
19659 }
19660
19661 /* Super hack. */
19662 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19663 attr->form = DW_FORM_GNU_ref_alt;
19664
19665 /* We have seen instances where the compiler tried to emit a byte
19666 size attribute of -1 which ended up being encoded as an unsigned
19667 0xffffffff. Although 0xffffffff is technically a valid size value,
19668 an object of this size seems pretty unlikely so we can relatively
19669 safely treat these cases as if the size attribute was invalid and
19670 treat them as zero by default. */
19671 if (attr->name == DW_AT_byte_size
19672 && form == DW_FORM_data4
19673 && DW_UNSND (attr) >= 0xffffffff)
19674 {
19675 complaint
19676 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19677 hex_string (DW_UNSND (attr)));
19678 DW_UNSND (attr) = 0;
19679 }
19680
19681 return info_ptr;
19682 }
19683
19684 /* Read an attribute described by an abbreviated attribute. */
19685
19686 static const gdb_byte *
19687 read_attribute (const struct die_reader_specs *reader,
19688 struct attribute *attr, struct attr_abbrev *abbrev,
19689 const gdb_byte *info_ptr)
19690 {
19691 attr->name = abbrev->name;
19692 return read_attribute_value (reader, attr, abbrev->form,
19693 abbrev->implicit_const, info_ptr);
19694 }
19695
19696 /* Read dwarf information from a buffer. */
19697
19698 static unsigned int
19699 read_1_byte (bfd *abfd, const gdb_byte *buf)
19700 {
19701 return bfd_get_8 (abfd, buf);
19702 }
19703
19704 static int
19705 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19706 {
19707 return bfd_get_signed_8 (abfd, buf);
19708 }
19709
19710 static unsigned int
19711 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19712 {
19713 return bfd_get_16 (abfd, buf);
19714 }
19715
19716 static int
19717 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19718 {
19719 return bfd_get_signed_16 (abfd, buf);
19720 }
19721
19722 static unsigned int
19723 read_3_bytes (bfd *abfd, const gdb_byte *buf)
19724 {
19725 unsigned int result = 0;
19726 for (int i = 0; i < 3; ++i)
19727 {
19728 unsigned char byte = bfd_get_8 (abfd, buf);
19729 buf++;
19730 result |= ((unsigned int) byte << (i * 8));
19731 }
19732 return result;
19733 }
19734
19735 static unsigned int
19736 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19737 {
19738 return bfd_get_32 (abfd, buf);
19739 }
19740
19741 static int
19742 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19743 {
19744 return bfd_get_signed_32 (abfd, buf);
19745 }
19746
19747 static ULONGEST
19748 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19749 {
19750 return bfd_get_64 (abfd, buf);
19751 }
19752
19753 static CORE_ADDR
19754 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19755 unsigned int *bytes_read)
19756 {
19757 struct comp_unit_head *cu_header = &cu->header;
19758 CORE_ADDR retval = 0;
19759
19760 if (cu_header->signed_addr_p)
19761 {
19762 switch (cu_header->addr_size)
19763 {
19764 case 2:
19765 retval = bfd_get_signed_16 (abfd, buf);
19766 break;
19767 case 4:
19768 retval = bfd_get_signed_32 (abfd, buf);
19769 break;
19770 case 8:
19771 retval = bfd_get_signed_64 (abfd, buf);
19772 break;
19773 default:
19774 internal_error (__FILE__, __LINE__,
19775 _("read_address: bad switch, signed [in module %s]"),
19776 bfd_get_filename (abfd));
19777 }
19778 }
19779 else
19780 {
19781 switch (cu_header->addr_size)
19782 {
19783 case 2:
19784 retval = bfd_get_16 (abfd, buf);
19785 break;
19786 case 4:
19787 retval = bfd_get_32 (abfd, buf);
19788 break;
19789 case 8:
19790 retval = bfd_get_64 (abfd, buf);
19791 break;
19792 default:
19793 internal_error (__FILE__, __LINE__,
19794 _("read_address: bad switch, "
19795 "unsigned [in module %s]"),
19796 bfd_get_filename (abfd));
19797 }
19798 }
19799
19800 *bytes_read = cu_header->addr_size;
19801 return retval;
19802 }
19803
19804 /* Read the initial length from a section. The (draft) DWARF 3
19805 specification allows the initial length to take up either 4 bytes
19806 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19807 bytes describe the length and all offsets will be 8 bytes in length
19808 instead of 4.
19809
19810 An older, non-standard 64-bit format is also handled by this
19811 function. The older format in question stores the initial length
19812 as an 8-byte quantity without an escape value. Lengths greater
19813 than 2^32 aren't very common which means that the initial 4 bytes
19814 is almost always zero. Since a length value of zero doesn't make
19815 sense for the 32-bit format, this initial zero can be considered to
19816 be an escape value which indicates the presence of the older 64-bit
19817 format. As written, the code can't detect (old format) lengths
19818 greater than 4GB. If it becomes necessary to handle lengths
19819 somewhat larger than 4GB, we could allow other small values (such
19820 as the non-sensical values of 1, 2, and 3) to also be used as
19821 escape values indicating the presence of the old format.
19822
19823 The value returned via bytes_read should be used to increment the
19824 relevant pointer after calling read_initial_length().
19825
19826 [ Note: read_initial_length() and read_offset() are based on the
19827 document entitled "DWARF Debugging Information Format", revision
19828 3, draft 8, dated November 19, 2001. This document was obtained
19829 from:
19830
19831 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19832
19833 This document is only a draft and is subject to change. (So beware.)
19834
19835 Details regarding the older, non-standard 64-bit format were
19836 determined empirically by examining 64-bit ELF files produced by
19837 the SGI toolchain on an IRIX 6.5 machine.
19838
19839 - Kevin, July 16, 2002
19840 ] */
19841
19842 static LONGEST
19843 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19844 {
19845 LONGEST length = bfd_get_32 (abfd, buf);
19846
19847 if (length == 0xffffffff)
19848 {
19849 length = bfd_get_64 (abfd, buf + 4);
19850 *bytes_read = 12;
19851 }
19852 else if (length == 0)
19853 {
19854 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19855 length = bfd_get_64 (abfd, buf);
19856 *bytes_read = 8;
19857 }
19858 else
19859 {
19860 *bytes_read = 4;
19861 }
19862
19863 return length;
19864 }
19865
19866 /* Cover function for read_initial_length.
19867 Returns the length of the object at BUF, and stores the size of the
19868 initial length in *BYTES_READ and stores the size that offsets will be in
19869 *OFFSET_SIZE.
19870 If the initial length size is not equivalent to that specified in
19871 CU_HEADER then issue a complaint.
19872 This is useful when reading non-comp-unit headers. */
19873
19874 static LONGEST
19875 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19876 const struct comp_unit_head *cu_header,
19877 unsigned int *bytes_read,
19878 unsigned int *offset_size)
19879 {
19880 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19881
19882 gdb_assert (cu_header->initial_length_size == 4
19883 || cu_header->initial_length_size == 8
19884 || cu_header->initial_length_size == 12);
19885
19886 if (cu_header->initial_length_size != *bytes_read)
19887 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19888
19889 *offset_size = (*bytes_read == 4) ? 4 : 8;
19890 return length;
19891 }
19892
19893 /* Read an offset from the data stream. The size of the offset is
19894 given by cu_header->offset_size. */
19895
19896 static LONGEST
19897 read_offset (bfd *abfd, const gdb_byte *buf,
19898 const struct comp_unit_head *cu_header,
19899 unsigned int *bytes_read)
19900 {
19901 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19902
19903 *bytes_read = cu_header->offset_size;
19904 return offset;
19905 }
19906
19907 /* Read an offset from the data stream. */
19908
19909 static LONGEST
19910 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19911 {
19912 LONGEST retval = 0;
19913
19914 switch (offset_size)
19915 {
19916 case 4:
19917 retval = bfd_get_32 (abfd, buf);
19918 break;
19919 case 8:
19920 retval = bfd_get_64 (abfd, buf);
19921 break;
19922 default:
19923 internal_error (__FILE__, __LINE__,
19924 _("read_offset_1: bad switch [in module %s]"),
19925 bfd_get_filename (abfd));
19926 }
19927
19928 return retval;
19929 }
19930
19931 static const gdb_byte *
19932 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19933 {
19934 /* If the size of a host char is 8 bits, we can return a pointer
19935 to the buffer, otherwise we have to copy the data to a buffer
19936 allocated on the temporary obstack. */
19937 gdb_assert (HOST_CHAR_BIT == 8);
19938 return buf;
19939 }
19940
19941 static const char *
19942 read_direct_string (bfd *abfd, const gdb_byte *buf,
19943 unsigned int *bytes_read_ptr)
19944 {
19945 /* If the size of a host char is 8 bits, we can return a pointer
19946 to the string, otherwise we have to copy the string to a buffer
19947 allocated on the temporary obstack. */
19948 gdb_assert (HOST_CHAR_BIT == 8);
19949 if (*buf == '\0')
19950 {
19951 *bytes_read_ptr = 1;
19952 return NULL;
19953 }
19954 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19955 return (const char *) buf;
19956 }
19957
19958 /* Return pointer to string at section SECT offset STR_OFFSET with error
19959 reporting strings FORM_NAME and SECT_NAME. */
19960
19961 static const char *
19962 read_indirect_string_at_offset_from (struct objfile *objfile,
19963 bfd *abfd, LONGEST str_offset,
19964 struct dwarf2_section_info *sect,
19965 const char *form_name,
19966 const char *sect_name)
19967 {
19968 dwarf2_read_section (objfile, sect);
19969 if (sect->buffer == NULL)
19970 error (_("%s used without %s section [in module %s]"),
19971 form_name, sect_name, bfd_get_filename (abfd));
19972 if (str_offset >= sect->size)
19973 error (_("%s pointing outside of %s section [in module %s]"),
19974 form_name, sect_name, bfd_get_filename (abfd));
19975 gdb_assert (HOST_CHAR_BIT == 8);
19976 if (sect->buffer[str_offset] == '\0')
19977 return NULL;
19978 return (const char *) (sect->buffer + str_offset);
19979 }
19980
19981 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19982
19983 static const char *
19984 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19985 bfd *abfd, LONGEST str_offset)
19986 {
19987 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19988 abfd, str_offset,
19989 &dwarf2_per_objfile->str,
19990 "DW_FORM_strp", ".debug_str");
19991 }
19992
19993 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19994
19995 static const char *
19996 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19997 bfd *abfd, LONGEST str_offset)
19998 {
19999 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
20000 abfd, str_offset,
20001 &dwarf2_per_objfile->line_str,
20002 "DW_FORM_line_strp",
20003 ".debug_line_str");
20004 }
20005
20006 /* Read a string at offset STR_OFFSET in the .debug_str section from
20007 the .dwz file DWZ. Throw an error if the offset is too large. If
20008 the string consists of a single NUL byte, return NULL; otherwise
20009 return a pointer to the string. */
20010
20011 static const char *
20012 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
20013 LONGEST str_offset)
20014 {
20015 dwarf2_read_section (objfile, &dwz->str);
20016
20017 if (dwz->str.buffer == NULL)
20018 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
20019 "section [in module %s]"),
20020 bfd_get_filename (dwz->dwz_bfd.get ()));
20021 if (str_offset >= dwz->str.size)
20022 error (_("DW_FORM_GNU_strp_alt pointing outside of "
20023 ".debug_str section [in module %s]"),
20024 bfd_get_filename (dwz->dwz_bfd.get ()));
20025 gdb_assert (HOST_CHAR_BIT == 8);
20026 if (dwz->str.buffer[str_offset] == '\0')
20027 return NULL;
20028 return (const char *) (dwz->str.buffer + str_offset);
20029 }
20030
20031 /* Return pointer to string at .debug_str offset as read from BUF.
20032 BUF is assumed to be in a compilation unit described by CU_HEADER.
20033 Return *BYTES_READ_PTR count of bytes read from BUF. */
20034
20035 static const char *
20036 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
20037 const gdb_byte *buf,
20038 const struct comp_unit_head *cu_header,
20039 unsigned int *bytes_read_ptr)
20040 {
20041 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20042
20043 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
20044 }
20045
20046 /* Return pointer to string at .debug_line_str offset as read from BUF.
20047 BUF is assumed to be in a compilation unit described by CU_HEADER.
20048 Return *BYTES_READ_PTR count of bytes read from BUF. */
20049
20050 static const char *
20051 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
20052 bfd *abfd, const gdb_byte *buf,
20053 const struct comp_unit_head *cu_header,
20054 unsigned int *bytes_read_ptr)
20055 {
20056 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20057
20058 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
20059 str_offset);
20060 }
20061
20062 ULONGEST
20063 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
20064 unsigned int *bytes_read_ptr)
20065 {
20066 ULONGEST result;
20067 unsigned int num_read;
20068 int shift;
20069 unsigned char byte;
20070
20071 result = 0;
20072 shift = 0;
20073 num_read = 0;
20074 while (1)
20075 {
20076 byte = bfd_get_8 (abfd, buf);
20077 buf++;
20078 num_read++;
20079 result |= ((ULONGEST) (byte & 127) << shift);
20080 if ((byte & 128) == 0)
20081 {
20082 break;
20083 }
20084 shift += 7;
20085 }
20086 *bytes_read_ptr = num_read;
20087 return result;
20088 }
20089
20090 static LONGEST
20091 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
20092 unsigned int *bytes_read_ptr)
20093 {
20094 ULONGEST result;
20095 int shift, num_read;
20096 unsigned char byte;
20097
20098 result = 0;
20099 shift = 0;
20100 num_read = 0;
20101 while (1)
20102 {
20103 byte = bfd_get_8 (abfd, buf);
20104 buf++;
20105 num_read++;
20106 result |= ((ULONGEST) (byte & 127) << shift);
20107 shift += 7;
20108 if ((byte & 128) == 0)
20109 {
20110 break;
20111 }
20112 }
20113 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
20114 result |= -(((ULONGEST) 1) << shift);
20115 *bytes_read_ptr = num_read;
20116 return result;
20117 }
20118
20119 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20120 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
20121 ADDR_SIZE is the size of addresses from the CU header. */
20122
20123 static CORE_ADDR
20124 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20125 unsigned int addr_index, ULONGEST addr_base, int addr_size)
20126 {
20127 struct objfile *objfile = dwarf2_per_objfile->objfile;
20128 bfd *abfd = objfile->obfd;
20129 const gdb_byte *info_ptr;
20130
20131 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20132 if (dwarf2_per_objfile->addr.buffer == NULL)
20133 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20134 objfile_name (objfile));
20135 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
20136 error (_("DW_FORM_addr_index pointing outside of "
20137 ".debug_addr section [in module %s]"),
20138 objfile_name (objfile));
20139 info_ptr = (dwarf2_per_objfile->addr.buffer
20140 + addr_base + addr_index * addr_size);
20141 if (addr_size == 4)
20142 return bfd_get_32 (abfd, info_ptr);
20143 else
20144 return bfd_get_64 (abfd, info_ptr);
20145 }
20146
20147 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20148
20149 static CORE_ADDR
20150 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20151 {
20152 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20153 cu->addr_base, cu->header.addr_size);
20154 }
20155
20156 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20157
20158 static CORE_ADDR
20159 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20160 unsigned int *bytes_read)
20161 {
20162 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
20163 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20164
20165 return read_addr_index (cu, addr_index);
20166 }
20167
20168 /* Data structure to pass results from dwarf2_read_addr_index_reader
20169 back to dwarf2_read_addr_index. */
20170
20171 struct dwarf2_read_addr_index_data
20172 {
20173 ULONGEST addr_base;
20174 int addr_size;
20175 };
20176
20177 /* die_reader_func for dwarf2_read_addr_index. */
20178
20179 static void
20180 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
20181 const gdb_byte *info_ptr,
20182 struct die_info *comp_unit_die,
20183 int has_children,
20184 void *data)
20185 {
20186 struct dwarf2_cu *cu = reader->cu;
20187 struct dwarf2_read_addr_index_data *aidata =
20188 (struct dwarf2_read_addr_index_data *) data;
20189
20190 aidata->addr_base = cu->addr_base;
20191 aidata->addr_size = cu->header.addr_size;
20192 }
20193
20194 /* Given an index in .debug_addr, fetch the value.
20195 NOTE: This can be called during dwarf expression evaluation,
20196 long after the debug information has been read, and thus per_cu->cu
20197 may no longer exist. */
20198
20199 CORE_ADDR
20200 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20201 unsigned int addr_index)
20202 {
20203 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
20204 struct dwarf2_cu *cu = per_cu->cu;
20205 ULONGEST addr_base;
20206 int addr_size;
20207
20208 /* We need addr_base and addr_size.
20209 If we don't have PER_CU->cu, we have to get it.
20210 Nasty, but the alternative is storing the needed info in PER_CU,
20211 which at this point doesn't seem justified: it's not clear how frequently
20212 it would get used and it would increase the size of every PER_CU.
20213 Entry points like dwarf2_per_cu_addr_size do a similar thing
20214 so we're not in uncharted territory here.
20215 Alas we need to be a bit more complicated as addr_base is contained
20216 in the DIE.
20217
20218 We don't need to read the entire CU(/TU).
20219 We just need the header and top level die.
20220
20221 IWBN to use the aging mechanism to let us lazily later discard the CU.
20222 For now we skip this optimization. */
20223
20224 if (cu != NULL)
20225 {
20226 addr_base = cu->addr_base;
20227 addr_size = cu->header.addr_size;
20228 }
20229 else
20230 {
20231 struct dwarf2_read_addr_index_data aidata;
20232
20233 /* Note: We can't use init_cutu_and_read_dies_simple here,
20234 we need addr_base. */
20235 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
20236 dwarf2_read_addr_index_reader, &aidata);
20237 addr_base = aidata.addr_base;
20238 addr_size = aidata.addr_size;
20239 }
20240
20241 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20242 addr_size);
20243 }
20244
20245 /* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
20246 This is only used by the Fission support. */
20247
20248 static const char *
20249 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20250 {
20251 struct dwarf2_cu *cu = reader->cu;
20252 struct dwarf2_per_objfile *dwarf2_per_objfile
20253 = cu->per_cu->dwarf2_per_objfile;
20254 struct objfile *objfile = dwarf2_per_objfile->objfile;
20255 const char *objf_name = objfile_name (objfile);
20256 bfd *abfd = objfile->obfd;
20257 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20258 struct dwarf2_section_info *str_offsets_section =
20259 &reader->dwo_file->sections.str_offsets;
20260 const gdb_byte *info_ptr;
20261 ULONGEST str_offset;
20262 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20263
20264 dwarf2_read_section (objfile, str_section);
20265 dwarf2_read_section (objfile, str_offsets_section);
20266 if (str_section->buffer == NULL)
20267 error (_("%s used without .debug_str.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_offsets_section->buffer == NULL)
20271 error (_("%s used without .debug_str_offsets.dwo section"
20272 " in CU at offset %s [in module %s]"),
20273 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20274 if (str_index * cu->header.offset_size >= str_offsets_section->size)
20275 error (_("%s pointing outside of .debug_str_offsets.dwo"
20276 " section in CU at offset %s [in module %s]"),
20277 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20278 info_ptr = (str_offsets_section->buffer
20279 + str_index * cu->header.offset_size);
20280 if (cu->header.offset_size == 4)
20281 str_offset = bfd_get_32 (abfd, info_ptr);
20282 else
20283 str_offset = bfd_get_64 (abfd, info_ptr);
20284 if (str_offset >= str_section->size)
20285 error (_("Offset from %s pointing outside of"
20286 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20287 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20288 return (const char *) (str_section->buffer + str_offset);
20289 }
20290
20291 /* Return the length of an LEB128 number in BUF. */
20292
20293 static int
20294 leb128_size (const gdb_byte *buf)
20295 {
20296 const gdb_byte *begin = buf;
20297 gdb_byte byte;
20298
20299 while (1)
20300 {
20301 byte = *buf++;
20302 if ((byte & 128) == 0)
20303 return buf - begin;
20304 }
20305 }
20306
20307 static void
20308 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20309 {
20310 switch (lang)
20311 {
20312 case DW_LANG_C89:
20313 case DW_LANG_C99:
20314 case DW_LANG_C11:
20315 case DW_LANG_C:
20316 case DW_LANG_UPC:
20317 cu->language = language_c;
20318 break;
20319 case DW_LANG_Java:
20320 case DW_LANG_C_plus_plus:
20321 case DW_LANG_C_plus_plus_11:
20322 case DW_LANG_C_plus_plus_14:
20323 cu->language = language_cplus;
20324 break;
20325 case DW_LANG_D:
20326 cu->language = language_d;
20327 break;
20328 case DW_LANG_Fortran77:
20329 case DW_LANG_Fortran90:
20330 case DW_LANG_Fortran95:
20331 case DW_LANG_Fortran03:
20332 case DW_LANG_Fortran08:
20333 cu->language = language_fortran;
20334 break;
20335 case DW_LANG_Go:
20336 cu->language = language_go;
20337 break;
20338 case DW_LANG_Mips_Assembler:
20339 cu->language = language_asm;
20340 break;
20341 case DW_LANG_Ada83:
20342 case DW_LANG_Ada95:
20343 cu->language = language_ada;
20344 break;
20345 case DW_LANG_Modula2:
20346 cu->language = language_m2;
20347 break;
20348 case DW_LANG_Pascal83:
20349 cu->language = language_pascal;
20350 break;
20351 case DW_LANG_ObjC:
20352 cu->language = language_objc;
20353 break;
20354 case DW_LANG_Rust:
20355 case DW_LANG_Rust_old:
20356 cu->language = language_rust;
20357 break;
20358 case DW_LANG_Cobol74:
20359 case DW_LANG_Cobol85:
20360 default:
20361 cu->language = language_minimal;
20362 break;
20363 }
20364 cu->language_defn = language_def (cu->language);
20365 }
20366
20367 /* Return the named attribute or NULL if not there. */
20368
20369 static struct attribute *
20370 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20371 {
20372 for (;;)
20373 {
20374 unsigned int i;
20375 struct attribute *spec = NULL;
20376
20377 for (i = 0; i < die->num_attrs; ++i)
20378 {
20379 if (die->attrs[i].name == name)
20380 return &die->attrs[i];
20381 if (die->attrs[i].name == DW_AT_specification
20382 || die->attrs[i].name == DW_AT_abstract_origin)
20383 spec = &die->attrs[i];
20384 }
20385
20386 if (!spec)
20387 break;
20388
20389 die = follow_die_ref (die, spec, &cu);
20390 }
20391
20392 return NULL;
20393 }
20394
20395 /* Return the named attribute or NULL if not there,
20396 but do not follow DW_AT_specification, etc.
20397 This is for use in contexts where we're reading .debug_types dies.
20398 Following DW_AT_specification, DW_AT_abstract_origin will take us
20399 back up the chain, and we want to go down. */
20400
20401 static struct attribute *
20402 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20403 {
20404 unsigned int i;
20405
20406 for (i = 0; i < die->num_attrs; ++i)
20407 if (die->attrs[i].name == name)
20408 return &die->attrs[i];
20409
20410 return NULL;
20411 }
20412
20413 /* Return the string associated with a string-typed attribute, or NULL if it
20414 is either not found or is of an incorrect type. */
20415
20416 static const char *
20417 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20418 {
20419 struct attribute *attr;
20420 const char *str = NULL;
20421
20422 attr = dwarf2_attr (die, name, cu);
20423
20424 if (attr != NULL)
20425 {
20426 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20427 || attr->form == DW_FORM_string
20428 || attr->form == DW_FORM_strx
20429 || attr->form == DW_FORM_strx1
20430 || attr->form == DW_FORM_strx2
20431 || attr->form == DW_FORM_strx3
20432 || attr->form == DW_FORM_strx4
20433 || attr->form == DW_FORM_GNU_str_index
20434 || attr->form == DW_FORM_GNU_strp_alt)
20435 str = DW_STRING (attr);
20436 else
20437 complaint (_("string type expected for attribute %s for "
20438 "DIE at %s in module %s"),
20439 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20440 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20441 }
20442
20443 return str;
20444 }
20445
20446 /* Return the dwo name or NULL if not present. If present, it is in either
20447 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20448 static const char *
20449 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20450 {
20451 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20452 if (dwo_name == nullptr)
20453 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20454 return dwo_name;
20455 }
20456
20457 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20458 and holds a non-zero value. This function should only be used for
20459 DW_FORM_flag or DW_FORM_flag_present attributes. */
20460
20461 static int
20462 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20463 {
20464 struct attribute *attr = dwarf2_attr (die, name, cu);
20465
20466 return (attr && DW_UNSND (attr));
20467 }
20468
20469 static int
20470 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20471 {
20472 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20473 which value is non-zero. However, we have to be careful with
20474 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20475 (via dwarf2_flag_true_p) follows this attribute. So we may
20476 end up accidently finding a declaration attribute that belongs
20477 to a different DIE referenced by the specification attribute,
20478 even though the given DIE does not have a declaration attribute. */
20479 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20480 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20481 }
20482
20483 /* Return the die giving the specification for DIE, if there is
20484 one. *SPEC_CU is the CU containing DIE on input, and the CU
20485 containing the return value on output. If there is no
20486 specification, but there is an abstract origin, that is
20487 returned. */
20488
20489 static struct die_info *
20490 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20491 {
20492 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20493 *spec_cu);
20494
20495 if (spec_attr == NULL)
20496 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20497
20498 if (spec_attr == NULL)
20499 return NULL;
20500 else
20501 return follow_die_ref (die, spec_attr, spec_cu);
20502 }
20503
20504 /* Stub for free_line_header to match void * callback types. */
20505
20506 static void
20507 free_line_header_voidp (void *arg)
20508 {
20509 struct line_header *lh = (struct line_header *) arg;
20510
20511 delete lh;
20512 }
20513
20514 void
20515 line_header::add_include_dir (const char *include_dir)
20516 {
20517 if (dwarf_line_debug >= 2)
20518 {
20519 size_t new_size;
20520 if (version >= 5)
20521 new_size = m_include_dirs.size ();
20522 else
20523 new_size = m_include_dirs.size () + 1;
20524 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20525 new_size, include_dir);
20526 }
20527 m_include_dirs.push_back (include_dir);
20528 }
20529
20530 void
20531 line_header::add_file_name (const char *name,
20532 dir_index d_index,
20533 unsigned int mod_time,
20534 unsigned int length)
20535 {
20536 if (dwarf_line_debug >= 2)
20537 {
20538 size_t new_size;
20539 if (version >= 5)
20540 new_size = file_names_size ();
20541 else
20542 new_size = file_names_size () + 1;
20543 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20544 new_size, name);
20545 }
20546 m_file_names.emplace_back (name, d_index, mod_time, length);
20547 }
20548
20549 /* A convenience function to find the proper .debug_line section for a CU. */
20550
20551 static struct dwarf2_section_info *
20552 get_debug_line_section (struct dwarf2_cu *cu)
20553 {
20554 struct dwarf2_section_info *section;
20555 struct dwarf2_per_objfile *dwarf2_per_objfile
20556 = cu->per_cu->dwarf2_per_objfile;
20557
20558 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20559 DWO file. */
20560 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20561 section = &cu->dwo_unit->dwo_file->sections.line;
20562 else if (cu->per_cu->is_dwz)
20563 {
20564 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20565
20566 section = &dwz->line;
20567 }
20568 else
20569 section = &dwarf2_per_objfile->line;
20570
20571 return section;
20572 }
20573
20574 /* Read directory or file name entry format, starting with byte of
20575 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20576 entries count and the entries themselves in the described entry
20577 format. */
20578
20579 static void
20580 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20581 bfd *abfd, const gdb_byte **bufp,
20582 struct line_header *lh,
20583 const struct comp_unit_head *cu_header,
20584 void (*callback) (struct line_header *lh,
20585 const char *name,
20586 dir_index d_index,
20587 unsigned int mod_time,
20588 unsigned int length))
20589 {
20590 gdb_byte format_count, formati;
20591 ULONGEST data_count, datai;
20592 const gdb_byte *buf = *bufp;
20593 const gdb_byte *format_header_data;
20594 unsigned int bytes_read;
20595
20596 format_count = read_1_byte (abfd, buf);
20597 buf += 1;
20598 format_header_data = buf;
20599 for (formati = 0; formati < format_count; formati++)
20600 {
20601 read_unsigned_leb128 (abfd, buf, &bytes_read);
20602 buf += bytes_read;
20603 read_unsigned_leb128 (abfd, buf, &bytes_read);
20604 buf += bytes_read;
20605 }
20606
20607 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20608 buf += bytes_read;
20609 for (datai = 0; datai < data_count; datai++)
20610 {
20611 const gdb_byte *format = format_header_data;
20612 struct file_entry fe;
20613
20614 for (formati = 0; formati < format_count; formati++)
20615 {
20616 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20617 format += bytes_read;
20618
20619 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20620 format += bytes_read;
20621
20622 gdb::optional<const char *> string;
20623 gdb::optional<unsigned int> uint;
20624
20625 switch (form)
20626 {
20627 case DW_FORM_string:
20628 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20629 buf += bytes_read;
20630 break;
20631
20632 case DW_FORM_line_strp:
20633 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20634 abfd, buf,
20635 cu_header,
20636 &bytes_read));
20637 buf += bytes_read;
20638 break;
20639
20640 case DW_FORM_data1:
20641 uint.emplace (read_1_byte (abfd, buf));
20642 buf += 1;
20643 break;
20644
20645 case DW_FORM_data2:
20646 uint.emplace (read_2_bytes (abfd, buf));
20647 buf += 2;
20648 break;
20649
20650 case DW_FORM_data4:
20651 uint.emplace (read_4_bytes (abfd, buf));
20652 buf += 4;
20653 break;
20654
20655 case DW_FORM_data8:
20656 uint.emplace (read_8_bytes (abfd, buf));
20657 buf += 8;
20658 break;
20659
20660 case DW_FORM_data16:
20661 /* This is used for MD5, but file_entry does not record MD5s. */
20662 buf += 16;
20663 break;
20664
20665 case DW_FORM_udata:
20666 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20667 buf += bytes_read;
20668 break;
20669
20670 case DW_FORM_block:
20671 /* It is valid only for DW_LNCT_timestamp which is ignored by
20672 current GDB. */
20673 break;
20674 }
20675
20676 switch (content_type)
20677 {
20678 case DW_LNCT_path:
20679 if (string.has_value ())
20680 fe.name = *string;
20681 break;
20682 case DW_LNCT_directory_index:
20683 if (uint.has_value ())
20684 fe.d_index = (dir_index) *uint;
20685 break;
20686 case DW_LNCT_timestamp:
20687 if (uint.has_value ())
20688 fe.mod_time = *uint;
20689 break;
20690 case DW_LNCT_size:
20691 if (uint.has_value ())
20692 fe.length = *uint;
20693 break;
20694 case DW_LNCT_MD5:
20695 break;
20696 default:
20697 complaint (_("Unknown format content type %s"),
20698 pulongest (content_type));
20699 }
20700 }
20701
20702 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20703 }
20704
20705 *bufp = buf;
20706 }
20707
20708 /* Read the statement program header starting at OFFSET in
20709 .debug_line, or .debug_line.dwo. Return a pointer
20710 to a struct line_header, allocated using xmalloc.
20711 Returns NULL if there is a problem reading the header, e.g., if it
20712 has a version we don't understand.
20713
20714 NOTE: the strings in the include directory and file name tables of
20715 the returned object point into the dwarf line section buffer,
20716 and must not be freed. */
20717
20718 static line_header_up
20719 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20720 {
20721 const gdb_byte *line_ptr;
20722 unsigned int bytes_read, offset_size;
20723 int i;
20724 const char *cur_dir, *cur_file;
20725 struct dwarf2_section_info *section;
20726 bfd *abfd;
20727 struct dwarf2_per_objfile *dwarf2_per_objfile
20728 = cu->per_cu->dwarf2_per_objfile;
20729
20730 section = get_debug_line_section (cu);
20731 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20732 if (section->buffer == NULL)
20733 {
20734 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20735 complaint (_("missing .debug_line.dwo section"));
20736 else
20737 complaint (_("missing .debug_line section"));
20738 return 0;
20739 }
20740
20741 /* We can't do this until we know the section is non-empty.
20742 Only then do we know we have such a section. */
20743 abfd = get_section_bfd_owner (section);
20744
20745 /* Make sure that at least there's room for the total_length field.
20746 That could be 12 bytes long, but we're just going to fudge that. */
20747 if (to_underlying (sect_off) + 4 >= section->size)
20748 {
20749 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20750 return 0;
20751 }
20752
20753 line_header_up lh (new line_header ());
20754
20755 lh->sect_off = sect_off;
20756 lh->offset_in_dwz = cu->per_cu->is_dwz;
20757
20758 line_ptr = section->buffer + to_underlying (sect_off);
20759
20760 /* Read in the header. */
20761 lh->total_length =
20762 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20763 &bytes_read, &offset_size);
20764 line_ptr += bytes_read;
20765
20766 const gdb_byte *start_here = line_ptr;
20767
20768 if (line_ptr + lh->total_length > (section->buffer + section->size))
20769 {
20770 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20771 return 0;
20772 }
20773 lh->statement_program_end = start_here + lh->total_length;
20774 lh->version = read_2_bytes (abfd, line_ptr);
20775 line_ptr += 2;
20776 if (lh->version > 5)
20777 {
20778 /* This is a version we don't understand. The format could have
20779 changed in ways we don't handle properly so just punt. */
20780 complaint (_("unsupported version in .debug_line section"));
20781 return NULL;
20782 }
20783 if (lh->version >= 5)
20784 {
20785 gdb_byte segment_selector_size;
20786
20787 /* Skip address size. */
20788 read_1_byte (abfd, line_ptr);
20789 line_ptr += 1;
20790
20791 segment_selector_size = read_1_byte (abfd, line_ptr);
20792 line_ptr += 1;
20793 if (segment_selector_size != 0)
20794 {
20795 complaint (_("unsupported segment selector size %u "
20796 "in .debug_line section"),
20797 segment_selector_size);
20798 return NULL;
20799 }
20800 }
20801 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20802 line_ptr += offset_size;
20803 lh->statement_program_start = line_ptr + lh->header_length;
20804 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20805 line_ptr += 1;
20806 if (lh->version >= 4)
20807 {
20808 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20809 line_ptr += 1;
20810 }
20811 else
20812 lh->maximum_ops_per_instruction = 1;
20813
20814 if (lh->maximum_ops_per_instruction == 0)
20815 {
20816 lh->maximum_ops_per_instruction = 1;
20817 complaint (_("invalid maximum_ops_per_instruction "
20818 "in `.debug_line' section"));
20819 }
20820
20821 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20822 line_ptr += 1;
20823 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20824 line_ptr += 1;
20825 lh->line_range = read_1_byte (abfd, line_ptr);
20826 line_ptr += 1;
20827 lh->opcode_base = read_1_byte (abfd, line_ptr);
20828 line_ptr += 1;
20829 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20830
20831 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20832 for (i = 1; i < lh->opcode_base; ++i)
20833 {
20834 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20835 line_ptr += 1;
20836 }
20837
20838 if (lh->version >= 5)
20839 {
20840 /* Read directory table. */
20841 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20842 &cu->header,
20843 [] (struct line_header *header, const char *name,
20844 dir_index d_index, unsigned int mod_time,
20845 unsigned int length)
20846 {
20847 header->add_include_dir (name);
20848 });
20849
20850 /* Read file name table. */
20851 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20852 &cu->header,
20853 [] (struct line_header *header, const char *name,
20854 dir_index d_index, unsigned int mod_time,
20855 unsigned int length)
20856 {
20857 header->add_file_name (name, d_index, mod_time, length);
20858 });
20859 }
20860 else
20861 {
20862 /* Read directory table. */
20863 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20864 {
20865 line_ptr += bytes_read;
20866 lh->add_include_dir (cur_dir);
20867 }
20868 line_ptr += bytes_read;
20869
20870 /* Read file name table. */
20871 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20872 {
20873 unsigned int mod_time, length;
20874 dir_index d_index;
20875
20876 line_ptr += bytes_read;
20877 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20878 line_ptr += bytes_read;
20879 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20880 line_ptr += bytes_read;
20881 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20882 line_ptr += bytes_read;
20883
20884 lh->add_file_name (cur_file, d_index, mod_time, length);
20885 }
20886 line_ptr += bytes_read;
20887 }
20888
20889 if (line_ptr > (section->buffer + section->size))
20890 complaint (_("line number info header doesn't "
20891 "fit in `.debug_line' section"));
20892
20893 return lh;
20894 }
20895
20896 /* Subroutine of dwarf_decode_lines to simplify it.
20897 Return the file name of the psymtab for the given file_entry.
20898 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20899 If space for the result is malloc'd, *NAME_HOLDER will be set.
20900 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20901
20902 static const char *
20903 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20904 const struct partial_symtab *pst,
20905 const char *comp_dir,
20906 gdb::unique_xmalloc_ptr<char> *name_holder)
20907 {
20908 const char *include_name = fe.name;
20909 const char *include_name_to_compare = include_name;
20910 const char *pst_filename;
20911 int file_is_pst;
20912
20913 const char *dir_name = fe.include_dir (lh);
20914
20915 gdb::unique_xmalloc_ptr<char> hold_compare;
20916 if (!IS_ABSOLUTE_PATH (include_name)
20917 && (dir_name != NULL || comp_dir != NULL))
20918 {
20919 /* Avoid creating a duplicate psymtab for PST.
20920 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20921 Before we do the comparison, however, we need to account
20922 for DIR_NAME and COMP_DIR.
20923 First prepend dir_name (if non-NULL). If we still don't
20924 have an absolute path prepend comp_dir (if non-NULL).
20925 However, the directory we record in the include-file's
20926 psymtab does not contain COMP_DIR (to match the
20927 corresponding symtab(s)).
20928
20929 Example:
20930
20931 bash$ cd /tmp
20932 bash$ gcc -g ./hello.c
20933 include_name = "hello.c"
20934 dir_name = "."
20935 DW_AT_comp_dir = comp_dir = "/tmp"
20936 DW_AT_name = "./hello.c"
20937
20938 */
20939
20940 if (dir_name != NULL)
20941 {
20942 name_holder->reset (concat (dir_name, SLASH_STRING,
20943 include_name, (char *) NULL));
20944 include_name = name_holder->get ();
20945 include_name_to_compare = include_name;
20946 }
20947 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20948 {
20949 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20950 include_name, (char *) NULL));
20951 include_name_to_compare = hold_compare.get ();
20952 }
20953 }
20954
20955 pst_filename = pst->filename;
20956 gdb::unique_xmalloc_ptr<char> copied_name;
20957 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20958 {
20959 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20960 pst_filename, (char *) NULL));
20961 pst_filename = copied_name.get ();
20962 }
20963
20964 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20965
20966 if (file_is_pst)
20967 return NULL;
20968 return include_name;
20969 }
20970
20971 /* State machine to track the state of the line number program. */
20972
20973 class lnp_state_machine
20974 {
20975 public:
20976 /* Initialize a machine state for the start of a line number
20977 program. */
20978 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20979 bool record_lines_p);
20980
20981 file_entry *current_file ()
20982 {
20983 /* lh->file_names is 0-based, but the file name numbers in the
20984 statement program are 1-based. */
20985 return m_line_header->file_name_at (m_file);
20986 }
20987
20988 /* Record the line in the state machine. END_SEQUENCE is true if
20989 we're processing the end of a sequence. */
20990 void record_line (bool end_sequence);
20991
20992 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20993 nop-out rest of the lines in this sequence. */
20994 void check_line_address (struct dwarf2_cu *cu,
20995 const gdb_byte *line_ptr,
20996 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20997
20998 void handle_set_discriminator (unsigned int discriminator)
20999 {
21000 m_discriminator = discriminator;
21001 m_line_has_non_zero_discriminator |= discriminator != 0;
21002 }
21003
21004 /* Handle DW_LNE_set_address. */
21005 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21006 {
21007 m_op_index = 0;
21008 address += baseaddr;
21009 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21010 }
21011
21012 /* Handle DW_LNS_advance_pc. */
21013 void handle_advance_pc (CORE_ADDR adjust);
21014
21015 /* Handle a special opcode. */
21016 void handle_special_opcode (unsigned char op_code);
21017
21018 /* Handle DW_LNS_advance_line. */
21019 void handle_advance_line (int line_delta)
21020 {
21021 advance_line (line_delta);
21022 }
21023
21024 /* Handle DW_LNS_set_file. */
21025 void handle_set_file (file_name_index file);
21026
21027 /* Handle DW_LNS_negate_stmt. */
21028 void handle_negate_stmt ()
21029 {
21030 m_is_stmt = !m_is_stmt;
21031 }
21032
21033 /* Handle DW_LNS_const_add_pc. */
21034 void handle_const_add_pc ();
21035
21036 /* Handle DW_LNS_fixed_advance_pc. */
21037 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21038 {
21039 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21040 m_op_index = 0;
21041 }
21042
21043 /* Handle DW_LNS_copy. */
21044 void handle_copy ()
21045 {
21046 record_line (false);
21047 m_discriminator = 0;
21048 }
21049
21050 /* Handle DW_LNE_end_sequence. */
21051 void handle_end_sequence ()
21052 {
21053 m_currently_recording_lines = true;
21054 }
21055
21056 private:
21057 /* Advance the line by LINE_DELTA. */
21058 void advance_line (int line_delta)
21059 {
21060 m_line += line_delta;
21061
21062 if (line_delta != 0)
21063 m_line_has_non_zero_discriminator = m_discriminator != 0;
21064 }
21065
21066 struct dwarf2_cu *m_cu;
21067
21068 gdbarch *m_gdbarch;
21069
21070 /* True if we're recording lines.
21071 Otherwise we're building partial symtabs and are just interested in
21072 finding include files mentioned by the line number program. */
21073 bool m_record_lines_p;
21074
21075 /* The line number header. */
21076 line_header *m_line_header;
21077
21078 /* These are part of the standard DWARF line number state machine,
21079 and initialized according to the DWARF spec. */
21080
21081 unsigned char m_op_index = 0;
21082 /* The line table index of the current file. */
21083 file_name_index m_file = 1;
21084 unsigned int m_line = 1;
21085
21086 /* These are initialized in the constructor. */
21087
21088 CORE_ADDR m_address;
21089 bool m_is_stmt;
21090 unsigned int m_discriminator;
21091
21092 /* Additional bits of state we need to track. */
21093
21094 /* The last file that we called dwarf2_start_subfile for.
21095 This is only used for TLLs. */
21096 unsigned int m_last_file = 0;
21097 /* The last file a line number was recorded for. */
21098 struct subfile *m_last_subfile = NULL;
21099
21100 /* When true, record the lines we decode. */
21101 bool m_currently_recording_lines = false;
21102
21103 /* The last line number that was recorded, used to coalesce
21104 consecutive entries for the same line. This can happen, for
21105 example, when discriminators are present. PR 17276. */
21106 unsigned int m_last_line = 0;
21107 bool m_line_has_non_zero_discriminator = false;
21108 };
21109
21110 void
21111 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21112 {
21113 CORE_ADDR addr_adj = (((m_op_index + adjust)
21114 / m_line_header->maximum_ops_per_instruction)
21115 * m_line_header->minimum_instruction_length);
21116 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21117 m_op_index = ((m_op_index + adjust)
21118 % m_line_header->maximum_ops_per_instruction);
21119 }
21120
21121 void
21122 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21123 {
21124 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21125 CORE_ADDR addr_adj = (((m_op_index
21126 + (adj_opcode / m_line_header->line_range))
21127 / m_line_header->maximum_ops_per_instruction)
21128 * m_line_header->minimum_instruction_length);
21129 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21130 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
21131 % m_line_header->maximum_ops_per_instruction);
21132
21133 int line_delta = (m_line_header->line_base
21134 + (adj_opcode % m_line_header->line_range));
21135 advance_line (line_delta);
21136 record_line (false);
21137 m_discriminator = 0;
21138 }
21139
21140 void
21141 lnp_state_machine::handle_set_file (file_name_index file)
21142 {
21143 m_file = file;
21144
21145 const file_entry *fe = current_file ();
21146 if (fe == NULL)
21147 dwarf2_debug_line_missing_file_complaint ();
21148 else if (m_record_lines_p)
21149 {
21150 const char *dir = fe->include_dir (m_line_header);
21151
21152 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21153 m_line_has_non_zero_discriminator = m_discriminator != 0;
21154 dwarf2_start_subfile (m_cu, fe->name, dir);
21155 }
21156 }
21157
21158 void
21159 lnp_state_machine::handle_const_add_pc ()
21160 {
21161 CORE_ADDR adjust
21162 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21163
21164 CORE_ADDR addr_adj
21165 = (((m_op_index + adjust)
21166 / m_line_header->maximum_ops_per_instruction)
21167 * m_line_header->minimum_instruction_length);
21168
21169 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21170 m_op_index = ((m_op_index + adjust)
21171 % m_line_header->maximum_ops_per_instruction);
21172 }
21173
21174 /* Return non-zero if we should add LINE to the line number table.
21175 LINE is the line to add, LAST_LINE is the last line that was added,
21176 LAST_SUBFILE is the subfile for LAST_LINE.
21177 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21178 had a non-zero discriminator.
21179
21180 We have to be careful in the presence of discriminators.
21181 E.g., for this line:
21182
21183 for (i = 0; i < 100000; i++);
21184
21185 clang can emit four line number entries for that one line,
21186 each with a different discriminator.
21187 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21188
21189 However, we want gdb to coalesce all four entries into one.
21190 Otherwise the user could stepi into the middle of the line and
21191 gdb would get confused about whether the pc really was in the
21192 middle of the line.
21193
21194 Things are further complicated by the fact that two consecutive
21195 line number entries for the same line is a heuristic used by gcc
21196 to denote the end of the prologue. So we can't just discard duplicate
21197 entries, we have to be selective about it. The heuristic we use is
21198 that we only collapse consecutive entries for the same line if at least
21199 one of those entries has a non-zero discriminator. PR 17276.
21200
21201 Note: Addresses in the line number state machine can never go backwards
21202 within one sequence, thus this coalescing is ok. */
21203
21204 static int
21205 dwarf_record_line_p (struct dwarf2_cu *cu,
21206 unsigned int line, unsigned int last_line,
21207 int line_has_non_zero_discriminator,
21208 struct subfile *last_subfile)
21209 {
21210 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21211 return 1;
21212 if (line != last_line)
21213 return 1;
21214 /* Same line for the same file that we've seen already.
21215 As a last check, for pr 17276, only record the line if the line
21216 has never had a non-zero discriminator. */
21217 if (!line_has_non_zero_discriminator)
21218 return 1;
21219 return 0;
21220 }
21221
21222 /* Use the CU's builder to record line number LINE beginning at
21223 address ADDRESS in the line table of subfile SUBFILE. */
21224
21225 static void
21226 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21227 unsigned int line, CORE_ADDR address,
21228 struct dwarf2_cu *cu)
21229 {
21230 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21231
21232 if (dwarf_line_debug)
21233 {
21234 fprintf_unfiltered (gdb_stdlog,
21235 "Recording line %u, file %s, address %s\n",
21236 line, lbasename (subfile->name),
21237 paddress (gdbarch, address));
21238 }
21239
21240 if (cu != nullptr)
21241 cu->get_builder ()->record_line (subfile, line, addr);
21242 }
21243
21244 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21245 Mark the end of a set of line number records.
21246 The arguments are the same as for dwarf_record_line_1.
21247 If SUBFILE is NULL the request is ignored. */
21248
21249 static void
21250 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21251 CORE_ADDR address, struct dwarf2_cu *cu)
21252 {
21253 if (subfile == NULL)
21254 return;
21255
21256 if (dwarf_line_debug)
21257 {
21258 fprintf_unfiltered (gdb_stdlog,
21259 "Finishing current line, file %s, address %s\n",
21260 lbasename (subfile->name),
21261 paddress (gdbarch, address));
21262 }
21263
21264 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
21265 }
21266
21267 void
21268 lnp_state_machine::record_line (bool end_sequence)
21269 {
21270 if (dwarf_line_debug)
21271 {
21272 fprintf_unfiltered (gdb_stdlog,
21273 "Processing actual line %u: file %u,"
21274 " address %s, is_stmt %u, discrim %u\n",
21275 m_line, m_file,
21276 paddress (m_gdbarch, m_address),
21277 m_is_stmt, m_discriminator);
21278 }
21279
21280 file_entry *fe = current_file ();
21281
21282 if (fe == NULL)
21283 dwarf2_debug_line_missing_file_complaint ();
21284 /* For now we ignore lines not starting on an instruction boundary.
21285 But not when processing end_sequence for compatibility with the
21286 previous version of the code. */
21287 else if (m_op_index == 0 || end_sequence)
21288 {
21289 fe->included_p = 1;
21290 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
21291 {
21292 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
21293 || end_sequence)
21294 {
21295 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21296 m_currently_recording_lines ? m_cu : nullptr);
21297 }
21298
21299 if (!end_sequence)
21300 {
21301 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21302 m_line_has_non_zero_discriminator,
21303 m_last_subfile))
21304 {
21305 buildsym_compunit *builder = m_cu->get_builder ();
21306 dwarf_record_line_1 (m_gdbarch,
21307 builder->get_current_subfile (),
21308 m_line, m_address,
21309 m_currently_recording_lines ? m_cu : nullptr);
21310 }
21311 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21312 m_last_line = m_line;
21313 }
21314 }
21315 }
21316 }
21317
21318 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21319 line_header *lh, bool record_lines_p)
21320 {
21321 m_cu = cu;
21322 m_gdbarch = arch;
21323 m_record_lines_p = record_lines_p;
21324 m_line_header = lh;
21325
21326 m_currently_recording_lines = true;
21327
21328 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21329 was a line entry for it so that the backend has a chance to adjust it
21330 and also record it in case it needs it. This is currently used by MIPS
21331 code, cf. `mips_adjust_dwarf2_line'. */
21332 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21333 m_is_stmt = lh->default_is_stmt;
21334 m_discriminator = 0;
21335 }
21336
21337 void
21338 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21339 const gdb_byte *line_ptr,
21340 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21341 {
21342 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21343 the pc range of the CU. However, we restrict the test to only ADDRESS
21344 values of zero to preserve GDB's previous behaviour which is to handle
21345 the specific case of a function being GC'd by the linker. */
21346
21347 if (address == 0 && address < unrelocated_lowpc)
21348 {
21349 /* This line table is for a function which has been
21350 GCd by the linker. Ignore it. PR gdb/12528 */
21351
21352 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21353 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21354
21355 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21356 line_offset, objfile_name (objfile));
21357 m_currently_recording_lines = false;
21358 /* Note: m_currently_recording_lines is left as false until we see
21359 DW_LNE_end_sequence. */
21360 }
21361 }
21362
21363 /* Subroutine of dwarf_decode_lines to simplify it.
21364 Process the line number information in LH.
21365 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21366 program in order to set included_p for every referenced header. */
21367
21368 static void
21369 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21370 const int decode_for_pst_p, CORE_ADDR lowpc)
21371 {
21372 const gdb_byte *line_ptr, *extended_end;
21373 const gdb_byte *line_end;
21374 unsigned int bytes_read, extended_len;
21375 unsigned char op_code, extended_op;
21376 CORE_ADDR baseaddr;
21377 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21378 bfd *abfd = objfile->obfd;
21379 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21380 /* True if we're recording line info (as opposed to building partial
21381 symtabs and just interested in finding include files mentioned by
21382 the line number program). */
21383 bool record_lines_p = !decode_for_pst_p;
21384
21385 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21386
21387 line_ptr = lh->statement_program_start;
21388 line_end = lh->statement_program_end;
21389
21390 /* Read the statement sequences until there's nothing left. */
21391 while (line_ptr < line_end)
21392 {
21393 /* The DWARF line number program state machine. Reset the state
21394 machine at the start of each sequence. */
21395 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21396 bool end_sequence = false;
21397
21398 if (record_lines_p)
21399 {
21400 /* Start a subfile for the current file of the state
21401 machine. */
21402 const file_entry *fe = state_machine.current_file ();
21403
21404 if (fe != NULL)
21405 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21406 }
21407
21408 /* Decode the table. */
21409 while (line_ptr < line_end && !end_sequence)
21410 {
21411 op_code = read_1_byte (abfd, line_ptr);
21412 line_ptr += 1;
21413
21414 if (op_code >= lh->opcode_base)
21415 {
21416 /* Special opcode. */
21417 state_machine.handle_special_opcode (op_code);
21418 }
21419 else switch (op_code)
21420 {
21421 case DW_LNS_extended_op:
21422 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21423 &bytes_read);
21424 line_ptr += bytes_read;
21425 extended_end = line_ptr + extended_len;
21426 extended_op = read_1_byte (abfd, line_ptr);
21427 line_ptr += 1;
21428 switch (extended_op)
21429 {
21430 case DW_LNE_end_sequence:
21431 state_machine.handle_end_sequence ();
21432 end_sequence = true;
21433 break;
21434 case DW_LNE_set_address:
21435 {
21436 CORE_ADDR address
21437 = read_address (abfd, line_ptr, cu, &bytes_read);
21438 line_ptr += bytes_read;
21439
21440 state_machine.check_line_address (cu, line_ptr,
21441 lowpc - baseaddr, address);
21442 state_machine.handle_set_address (baseaddr, address);
21443 }
21444 break;
21445 case DW_LNE_define_file:
21446 {
21447 const char *cur_file;
21448 unsigned int mod_time, length;
21449 dir_index dindex;
21450
21451 cur_file = read_direct_string (abfd, line_ptr,
21452 &bytes_read);
21453 line_ptr += bytes_read;
21454 dindex = (dir_index)
21455 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21456 line_ptr += bytes_read;
21457 mod_time =
21458 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21459 line_ptr += bytes_read;
21460 length =
21461 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21462 line_ptr += bytes_read;
21463 lh->add_file_name (cur_file, dindex, mod_time, length);
21464 }
21465 break;
21466 case DW_LNE_set_discriminator:
21467 {
21468 /* The discriminator is not interesting to the
21469 debugger; just ignore it. We still need to
21470 check its value though:
21471 if there are consecutive entries for the same
21472 (non-prologue) line we want to coalesce them.
21473 PR 17276. */
21474 unsigned int discr
21475 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21476 line_ptr += bytes_read;
21477
21478 state_machine.handle_set_discriminator (discr);
21479 }
21480 break;
21481 default:
21482 complaint (_("mangled .debug_line section"));
21483 return;
21484 }
21485 /* Make sure that we parsed the extended op correctly. If e.g.
21486 we expected a different address size than the producer used,
21487 we may have read the wrong number of bytes. */
21488 if (line_ptr != extended_end)
21489 {
21490 complaint (_("mangled .debug_line section"));
21491 return;
21492 }
21493 break;
21494 case DW_LNS_copy:
21495 state_machine.handle_copy ();
21496 break;
21497 case DW_LNS_advance_pc:
21498 {
21499 CORE_ADDR adjust
21500 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21501 line_ptr += bytes_read;
21502
21503 state_machine.handle_advance_pc (adjust);
21504 }
21505 break;
21506 case DW_LNS_advance_line:
21507 {
21508 int line_delta
21509 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21510 line_ptr += bytes_read;
21511
21512 state_machine.handle_advance_line (line_delta);
21513 }
21514 break;
21515 case DW_LNS_set_file:
21516 {
21517 file_name_index file
21518 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21519 &bytes_read);
21520 line_ptr += bytes_read;
21521
21522 state_machine.handle_set_file (file);
21523 }
21524 break;
21525 case DW_LNS_set_column:
21526 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21527 line_ptr += bytes_read;
21528 break;
21529 case DW_LNS_negate_stmt:
21530 state_machine.handle_negate_stmt ();
21531 break;
21532 case DW_LNS_set_basic_block:
21533 break;
21534 /* Add to the address register of the state machine the
21535 address increment value corresponding to special opcode
21536 255. I.e., this value is scaled by the minimum
21537 instruction length since special opcode 255 would have
21538 scaled the increment. */
21539 case DW_LNS_const_add_pc:
21540 state_machine.handle_const_add_pc ();
21541 break;
21542 case DW_LNS_fixed_advance_pc:
21543 {
21544 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21545 line_ptr += 2;
21546
21547 state_machine.handle_fixed_advance_pc (addr_adj);
21548 }
21549 break;
21550 default:
21551 {
21552 /* Unknown standard opcode, ignore it. */
21553 int i;
21554
21555 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21556 {
21557 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21558 line_ptr += bytes_read;
21559 }
21560 }
21561 }
21562 }
21563
21564 if (!end_sequence)
21565 dwarf2_debug_line_missing_end_sequence_complaint ();
21566
21567 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21568 in which case we still finish recording the last line). */
21569 state_machine.record_line (true);
21570 }
21571 }
21572
21573 /* Decode the Line Number Program (LNP) for the given line_header
21574 structure and CU. The actual information extracted and the type
21575 of structures created from the LNP depends on the value of PST.
21576
21577 1. If PST is NULL, then this procedure uses the data from the program
21578 to create all necessary symbol tables, and their linetables.
21579
21580 2. If PST is not NULL, this procedure reads the program to determine
21581 the list of files included by the unit represented by PST, and
21582 builds all the associated partial symbol tables.
21583
21584 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21585 It is used for relative paths in the line table.
21586 NOTE: When processing partial symtabs (pst != NULL),
21587 comp_dir == pst->dirname.
21588
21589 NOTE: It is important that psymtabs have the same file name (via strcmp)
21590 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21591 symtab we don't use it in the name of the psymtabs we create.
21592 E.g. expand_line_sal requires this when finding psymtabs to expand.
21593 A good testcase for this is mb-inline.exp.
21594
21595 LOWPC is the lowest address in CU (or 0 if not known).
21596
21597 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21598 for its PC<->lines mapping information. Otherwise only the filename
21599 table is read in. */
21600
21601 static void
21602 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21603 struct dwarf2_cu *cu, struct partial_symtab *pst,
21604 CORE_ADDR lowpc, int decode_mapping)
21605 {
21606 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21607 const int decode_for_pst_p = (pst != NULL);
21608
21609 if (decode_mapping)
21610 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21611
21612 if (decode_for_pst_p)
21613 {
21614 /* Now that we're done scanning the Line Header Program, we can
21615 create the psymtab of each included file. */
21616 for (auto &file_entry : lh->file_names ())
21617 if (file_entry.included_p == 1)
21618 {
21619 gdb::unique_xmalloc_ptr<char> name_holder;
21620 const char *include_name =
21621 psymtab_include_file_name (lh, file_entry, pst,
21622 comp_dir, &name_holder);
21623 if (include_name != NULL)
21624 dwarf2_create_include_psymtab (include_name, pst, objfile);
21625 }
21626 }
21627 else
21628 {
21629 /* Make sure a symtab is created for every file, even files
21630 which contain only variables (i.e. no code with associated
21631 line numbers). */
21632 buildsym_compunit *builder = cu->get_builder ();
21633 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21634
21635 for (auto &fe : lh->file_names ())
21636 {
21637 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21638 if (builder->get_current_subfile ()->symtab == NULL)
21639 {
21640 builder->get_current_subfile ()->symtab
21641 = allocate_symtab (cust,
21642 builder->get_current_subfile ()->name);
21643 }
21644 fe.symtab = builder->get_current_subfile ()->symtab;
21645 }
21646 }
21647 }
21648
21649 /* Start a subfile for DWARF. FILENAME is the name of the file and
21650 DIRNAME the name of the source directory which contains FILENAME
21651 or NULL if not known.
21652 This routine tries to keep line numbers from identical absolute and
21653 relative file names in a common subfile.
21654
21655 Using the `list' example from the GDB testsuite, which resides in
21656 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21657 of /srcdir/list0.c yields the following debugging information for list0.c:
21658
21659 DW_AT_name: /srcdir/list0.c
21660 DW_AT_comp_dir: /compdir
21661 files.files[0].name: list0.h
21662 files.files[0].dir: /srcdir
21663 files.files[1].name: list0.c
21664 files.files[1].dir: /srcdir
21665
21666 The line number information for list0.c has to end up in a single
21667 subfile, so that `break /srcdir/list0.c:1' works as expected.
21668 start_subfile will ensure that this happens provided that we pass the
21669 concatenation of files.files[1].dir and files.files[1].name as the
21670 subfile's name. */
21671
21672 static void
21673 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21674 const char *dirname)
21675 {
21676 gdb::unique_xmalloc_ptr<char> copy;
21677
21678 /* In order not to lose the line information directory,
21679 we concatenate it to the filename when it makes sense.
21680 Note that the Dwarf3 standard says (speaking of filenames in line
21681 information): ``The directory index is ignored for file names
21682 that represent full path names''. Thus ignoring dirname in the
21683 `else' branch below isn't an issue. */
21684
21685 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21686 {
21687 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21688 filename = copy.get ();
21689 }
21690
21691 cu->get_builder ()->start_subfile (filename);
21692 }
21693
21694 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21695 buildsym_compunit constructor. */
21696
21697 struct compunit_symtab *
21698 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21699 CORE_ADDR low_pc)
21700 {
21701 gdb_assert (m_builder == nullptr);
21702
21703 m_builder.reset (new struct buildsym_compunit
21704 (per_cu->dwarf2_per_objfile->objfile,
21705 name, comp_dir, language, low_pc));
21706
21707 list_in_scope = get_builder ()->get_file_symbols ();
21708
21709 get_builder ()->record_debugformat ("DWARF 2");
21710 get_builder ()->record_producer (producer);
21711
21712 processing_has_namespace_info = false;
21713
21714 return get_builder ()->get_compunit_symtab ();
21715 }
21716
21717 static void
21718 var_decode_location (struct attribute *attr, struct symbol *sym,
21719 struct dwarf2_cu *cu)
21720 {
21721 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21722 struct comp_unit_head *cu_header = &cu->header;
21723
21724 /* NOTE drow/2003-01-30: There used to be a comment and some special
21725 code here to turn a symbol with DW_AT_external and a
21726 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21727 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21728 with some versions of binutils) where shared libraries could have
21729 relocations against symbols in their debug information - the
21730 minimal symbol would have the right address, but the debug info
21731 would not. It's no longer necessary, because we will explicitly
21732 apply relocations when we read in the debug information now. */
21733
21734 /* A DW_AT_location attribute with no contents indicates that a
21735 variable has been optimized away. */
21736 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21737 {
21738 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21739 return;
21740 }
21741
21742 /* Handle one degenerate form of location expression specially, to
21743 preserve GDB's previous behavior when section offsets are
21744 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21745 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21746
21747 if (attr_form_is_block (attr)
21748 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21749 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21750 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21751 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21752 && (DW_BLOCK (attr)->size
21753 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21754 {
21755 unsigned int dummy;
21756
21757 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21758 SET_SYMBOL_VALUE_ADDRESS (sym,
21759 read_address (objfile->obfd,
21760 DW_BLOCK (attr)->data + 1,
21761 cu, &dummy));
21762 else
21763 SET_SYMBOL_VALUE_ADDRESS
21764 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21765 &dummy));
21766 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21767 fixup_symbol_section (sym, objfile);
21768 SET_SYMBOL_VALUE_ADDRESS (sym,
21769 SYMBOL_VALUE_ADDRESS (sym)
21770 + ANOFFSET (objfile->section_offsets,
21771 SYMBOL_SECTION (sym)));
21772 return;
21773 }
21774
21775 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21776 expression evaluator, and use LOC_COMPUTED only when necessary
21777 (i.e. when the value of a register or memory location is
21778 referenced, or a thread-local block, etc.). Then again, it might
21779 not be worthwhile. I'm assuming that it isn't unless performance
21780 or memory numbers show me otherwise. */
21781
21782 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21783
21784 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21785 cu->has_loclist = true;
21786 }
21787
21788 /* Given a pointer to a DWARF information entry, figure out if we need
21789 to make a symbol table entry for it, and if so, create a new entry
21790 and return a pointer to it.
21791 If TYPE is NULL, determine symbol type from the die, otherwise
21792 used the passed type.
21793 If SPACE is not NULL, use it to hold the new symbol. If it is
21794 NULL, allocate a new symbol on the objfile's obstack. */
21795
21796 static struct symbol *
21797 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21798 struct symbol *space)
21799 {
21800 struct dwarf2_per_objfile *dwarf2_per_objfile
21801 = cu->per_cu->dwarf2_per_objfile;
21802 struct objfile *objfile = dwarf2_per_objfile->objfile;
21803 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21804 struct symbol *sym = NULL;
21805 const char *name;
21806 struct attribute *attr = NULL;
21807 struct attribute *attr2 = NULL;
21808 CORE_ADDR baseaddr;
21809 struct pending **list_to_add = NULL;
21810
21811 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21812
21813 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21814
21815 name = dwarf2_name (die, cu);
21816 if (name)
21817 {
21818 const char *linkagename;
21819 int suppress_add = 0;
21820
21821 if (space)
21822 sym = space;
21823 else
21824 sym = allocate_symbol (objfile);
21825 OBJSTAT (objfile, n_syms++);
21826
21827 /* Cache this symbol's name and the name's demangled form (if any). */
21828 sym->set_language (cu->language, &objfile->objfile_obstack);
21829 linkagename = dwarf2_physname (name, die, cu);
21830 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
21831
21832 /* Fortran does not have mangling standard and the mangling does differ
21833 between gfortran, iFort etc. */
21834 if (cu->language == language_fortran
21835 && symbol_get_demangled_name (sym) == NULL)
21836 symbol_set_demangled_name (sym,
21837 dwarf2_full_name (name, die, cu),
21838 NULL);
21839
21840 /* Default assumptions.
21841 Use the passed type or decode it from the die. */
21842 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21843 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21844 if (type != NULL)
21845 SYMBOL_TYPE (sym) = type;
21846 else
21847 SYMBOL_TYPE (sym) = die_type (die, cu);
21848 attr = dwarf2_attr (die,
21849 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21850 cu);
21851 if (attr != nullptr)
21852 {
21853 SYMBOL_LINE (sym) = DW_UNSND (attr);
21854 }
21855
21856 attr = dwarf2_attr (die,
21857 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21858 cu);
21859 if (attr != nullptr)
21860 {
21861 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21862 struct file_entry *fe;
21863
21864 if (cu->line_header != NULL)
21865 fe = cu->line_header->file_name_at (file_index);
21866 else
21867 fe = NULL;
21868
21869 if (fe == NULL)
21870 complaint (_("file index out of range"));
21871 else
21872 symbol_set_symtab (sym, fe->symtab);
21873 }
21874
21875 switch (die->tag)
21876 {
21877 case DW_TAG_label:
21878 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21879 if (attr != nullptr)
21880 {
21881 CORE_ADDR addr;
21882
21883 addr = attr_value_as_address (attr);
21884 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21885 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21886 }
21887 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21888 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21889 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21890 add_symbol_to_list (sym, cu->list_in_scope);
21891 break;
21892 case DW_TAG_subprogram:
21893 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21894 finish_block. */
21895 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21896 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21897 if ((attr2 && (DW_UNSND (attr2) != 0))
21898 || cu->language == language_ada
21899 || cu->language == language_fortran)
21900 {
21901 /* Subprograms marked external are stored as a global symbol.
21902 Ada and Fortran subprograms, whether marked external or
21903 not, are always stored as a global symbol, because we want
21904 to be able to access them globally. For instance, we want
21905 to be able to break on a nested subprogram without having
21906 to specify the context. */
21907 list_to_add = cu->get_builder ()->get_global_symbols ();
21908 }
21909 else
21910 {
21911 list_to_add = cu->list_in_scope;
21912 }
21913 break;
21914 case DW_TAG_inlined_subroutine:
21915 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21916 finish_block. */
21917 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21918 SYMBOL_INLINED (sym) = 1;
21919 list_to_add = cu->list_in_scope;
21920 break;
21921 case DW_TAG_template_value_param:
21922 suppress_add = 1;
21923 /* Fall through. */
21924 case DW_TAG_constant:
21925 case DW_TAG_variable:
21926 case DW_TAG_member:
21927 /* Compilation with minimal debug info may result in
21928 variables with missing type entries. Change the
21929 misleading `void' type to something sensible. */
21930 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21931 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21932
21933 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21934 /* In the case of DW_TAG_member, we should only be called for
21935 static const members. */
21936 if (die->tag == DW_TAG_member)
21937 {
21938 /* dwarf2_add_field uses die_is_declaration,
21939 so we do the same. */
21940 gdb_assert (die_is_declaration (die, cu));
21941 gdb_assert (attr);
21942 }
21943 if (attr != nullptr)
21944 {
21945 dwarf2_const_value (attr, sym, cu);
21946 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21947 if (!suppress_add)
21948 {
21949 if (attr2 && (DW_UNSND (attr2) != 0))
21950 list_to_add = cu->get_builder ()->get_global_symbols ();
21951 else
21952 list_to_add = cu->list_in_scope;
21953 }
21954 break;
21955 }
21956 attr = dwarf2_attr (die, DW_AT_location, cu);
21957 if (attr != nullptr)
21958 {
21959 var_decode_location (attr, sym, cu);
21960 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21961
21962 /* Fortran explicitly imports any global symbols to the local
21963 scope by DW_TAG_common_block. */
21964 if (cu->language == language_fortran && die->parent
21965 && die->parent->tag == DW_TAG_common_block)
21966 attr2 = NULL;
21967
21968 if (SYMBOL_CLASS (sym) == LOC_STATIC
21969 && SYMBOL_VALUE_ADDRESS (sym) == 0
21970 && !dwarf2_per_objfile->has_section_at_zero)
21971 {
21972 /* When a static variable is eliminated by the linker,
21973 the corresponding debug information is not stripped
21974 out, but the variable address is set to null;
21975 do not add such variables into symbol table. */
21976 }
21977 else if (attr2 && (DW_UNSND (attr2) != 0))
21978 {
21979 if (SYMBOL_CLASS (sym) == LOC_STATIC
21980 && (objfile->flags & OBJF_MAINLINE) == 0
21981 && dwarf2_per_objfile->can_copy)
21982 {
21983 /* A global static variable might be subject to
21984 copy relocation. We first check for a local
21985 minsym, though, because maybe the symbol was
21986 marked hidden, in which case this would not
21987 apply. */
21988 bound_minimal_symbol found
21989 = (lookup_minimal_symbol_linkage
21990 (sym->linkage_name (), objfile));
21991 if (found.minsym != nullptr)
21992 sym->maybe_copied = 1;
21993 }
21994
21995 /* A variable with DW_AT_external is never static,
21996 but it may be block-scoped. */
21997 list_to_add
21998 = ((cu->list_in_scope
21999 == cu->get_builder ()->get_file_symbols ())
22000 ? cu->get_builder ()->get_global_symbols ()
22001 : cu->list_in_scope);
22002 }
22003 else
22004 list_to_add = cu->list_in_scope;
22005 }
22006 else
22007 {
22008 /* We do not know the address of this symbol.
22009 If it is an external symbol and we have type information
22010 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22011 The address of the variable will then be determined from
22012 the minimal symbol table whenever the variable is
22013 referenced. */
22014 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22015
22016 /* Fortran explicitly imports any global symbols to the local
22017 scope by DW_TAG_common_block. */
22018 if (cu->language == language_fortran && die->parent
22019 && die->parent->tag == DW_TAG_common_block)
22020 {
22021 /* SYMBOL_CLASS doesn't matter here because
22022 read_common_block is going to reset it. */
22023 if (!suppress_add)
22024 list_to_add = cu->list_in_scope;
22025 }
22026 else if (attr2 && (DW_UNSND (attr2) != 0)
22027 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22028 {
22029 /* A variable with DW_AT_external is never static, but it
22030 may be block-scoped. */
22031 list_to_add
22032 = ((cu->list_in_scope
22033 == cu->get_builder ()->get_file_symbols ())
22034 ? cu->get_builder ()->get_global_symbols ()
22035 : cu->list_in_scope);
22036
22037 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22038 }
22039 else if (!die_is_declaration (die, cu))
22040 {
22041 /* Use the default LOC_OPTIMIZED_OUT class. */
22042 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22043 if (!suppress_add)
22044 list_to_add = cu->list_in_scope;
22045 }
22046 }
22047 break;
22048 case DW_TAG_formal_parameter:
22049 {
22050 /* If we are inside a function, mark this as an argument. If
22051 not, we might be looking at an argument to an inlined function
22052 when we do not have enough information to show inlined frames;
22053 pretend it's a local variable in that case so that the user can
22054 still see it. */
22055 struct context_stack *curr
22056 = cu->get_builder ()->get_current_context_stack ();
22057 if (curr != nullptr && curr->name != nullptr)
22058 SYMBOL_IS_ARGUMENT (sym) = 1;
22059 attr = dwarf2_attr (die, DW_AT_location, cu);
22060 if (attr != nullptr)
22061 {
22062 var_decode_location (attr, sym, cu);
22063 }
22064 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22065 if (attr != nullptr)
22066 {
22067 dwarf2_const_value (attr, sym, cu);
22068 }
22069
22070 list_to_add = cu->list_in_scope;
22071 }
22072 break;
22073 case DW_TAG_unspecified_parameters:
22074 /* From varargs functions; gdb doesn't seem to have any
22075 interest in this information, so just ignore it for now.
22076 (FIXME?) */
22077 break;
22078 case DW_TAG_template_type_param:
22079 suppress_add = 1;
22080 /* Fall through. */
22081 case DW_TAG_class_type:
22082 case DW_TAG_interface_type:
22083 case DW_TAG_structure_type:
22084 case DW_TAG_union_type:
22085 case DW_TAG_set_type:
22086 case DW_TAG_enumeration_type:
22087 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22088 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22089
22090 {
22091 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22092 really ever be static objects: otherwise, if you try
22093 to, say, break of a class's method and you're in a file
22094 which doesn't mention that class, it won't work unless
22095 the check for all static symbols in lookup_symbol_aux
22096 saves you. See the OtherFileClass tests in
22097 gdb.c++/namespace.exp. */
22098
22099 if (!suppress_add)
22100 {
22101 buildsym_compunit *builder = cu->get_builder ();
22102 list_to_add
22103 = (cu->list_in_scope == builder->get_file_symbols ()
22104 && cu->language == language_cplus
22105 ? builder->get_global_symbols ()
22106 : cu->list_in_scope);
22107
22108 /* The semantics of C++ state that "struct foo {
22109 ... }" also defines a typedef for "foo". */
22110 if (cu->language == language_cplus
22111 || cu->language == language_ada
22112 || cu->language == language_d
22113 || cu->language == language_rust)
22114 {
22115 /* The symbol's name is already allocated along
22116 with this objfile, so we don't need to
22117 duplicate it for the type. */
22118 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
22119 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
22120 }
22121 }
22122 }
22123 break;
22124 case DW_TAG_typedef:
22125 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22126 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22127 list_to_add = cu->list_in_scope;
22128 break;
22129 case DW_TAG_base_type:
22130 case DW_TAG_subrange_type:
22131 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22132 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22133 list_to_add = cu->list_in_scope;
22134 break;
22135 case DW_TAG_enumerator:
22136 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22137 if (attr != nullptr)
22138 {
22139 dwarf2_const_value (attr, sym, cu);
22140 }
22141 {
22142 /* NOTE: carlton/2003-11-10: See comment above in the
22143 DW_TAG_class_type, etc. block. */
22144
22145 list_to_add
22146 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22147 && cu->language == language_cplus
22148 ? cu->get_builder ()->get_global_symbols ()
22149 : cu->list_in_scope);
22150 }
22151 break;
22152 case DW_TAG_imported_declaration:
22153 case DW_TAG_namespace:
22154 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22155 list_to_add = cu->get_builder ()->get_global_symbols ();
22156 break;
22157 case DW_TAG_module:
22158 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22159 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22160 list_to_add = cu->get_builder ()->get_global_symbols ();
22161 break;
22162 case DW_TAG_common_block:
22163 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22164 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22165 add_symbol_to_list (sym, cu->list_in_scope);
22166 break;
22167 default:
22168 /* Not a tag we recognize. Hopefully we aren't processing
22169 trash data, but since we must specifically ignore things
22170 we don't recognize, there is nothing else we should do at
22171 this point. */
22172 complaint (_("unsupported tag: '%s'"),
22173 dwarf_tag_name (die->tag));
22174 break;
22175 }
22176
22177 if (suppress_add)
22178 {
22179 sym->hash_next = objfile->template_symbols;
22180 objfile->template_symbols = sym;
22181 list_to_add = NULL;
22182 }
22183
22184 if (list_to_add != NULL)
22185 add_symbol_to_list (sym, list_to_add);
22186
22187 /* For the benefit of old versions of GCC, check for anonymous
22188 namespaces based on the demangled name. */
22189 if (!cu->processing_has_namespace_info
22190 && cu->language == language_cplus)
22191 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22192 }
22193 return (sym);
22194 }
22195
22196 /* Given an attr with a DW_FORM_dataN value in host byte order,
22197 zero-extend it as appropriate for the symbol's type. The DWARF
22198 standard (v4) is not entirely clear about the meaning of using
22199 DW_FORM_dataN for a constant with a signed type, where the type is
22200 wider than the data. The conclusion of a discussion on the DWARF
22201 list was that this is unspecified. We choose to always zero-extend
22202 because that is the interpretation long in use by GCC. */
22203
22204 static gdb_byte *
22205 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22206 struct dwarf2_cu *cu, LONGEST *value, int bits)
22207 {
22208 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22209 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22210 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22211 LONGEST l = DW_UNSND (attr);
22212
22213 if (bits < sizeof (*value) * 8)
22214 {
22215 l &= ((LONGEST) 1 << bits) - 1;
22216 *value = l;
22217 }
22218 else if (bits == sizeof (*value) * 8)
22219 *value = l;
22220 else
22221 {
22222 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22223 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22224 return bytes;
22225 }
22226
22227 return NULL;
22228 }
22229
22230 /* Read a constant value from an attribute. Either set *VALUE, or if
22231 the value does not fit in *VALUE, set *BYTES - either already
22232 allocated on the objfile obstack, or newly allocated on OBSTACK,
22233 or, set *BATON, if we translated the constant to a location
22234 expression. */
22235
22236 static void
22237 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22238 const char *name, struct obstack *obstack,
22239 struct dwarf2_cu *cu,
22240 LONGEST *value, const gdb_byte **bytes,
22241 struct dwarf2_locexpr_baton **baton)
22242 {
22243 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22244 struct comp_unit_head *cu_header = &cu->header;
22245 struct dwarf_block *blk;
22246 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22247 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22248
22249 *value = 0;
22250 *bytes = NULL;
22251 *baton = NULL;
22252
22253 switch (attr->form)
22254 {
22255 case DW_FORM_addr:
22256 case DW_FORM_addrx:
22257 case DW_FORM_GNU_addr_index:
22258 {
22259 gdb_byte *data;
22260
22261 if (TYPE_LENGTH (type) != cu_header->addr_size)
22262 dwarf2_const_value_length_mismatch_complaint (name,
22263 cu_header->addr_size,
22264 TYPE_LENGTH (type));
22265 /* Symbols of this form are reasonably rare, so we just
22266 piggyback on the existing location code rather than writing
22267 a new implementation of symbol_computed_ops. */
22268 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22269 (*baton)->per_cu = cu->per_cu;
22270 gdb_assert ((*baton)->per_cu);
22271
22272 (*baton)->size = 2 + cu_header->addr_size;
22273 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22274 (*baton)->data = data;
22275
22276 data[0] = DW_OP_addr;
22277 store_unsigned_integer (&data[1], cu_header->addr_size,
22278 byte_order, DW_ADDR (attr));
22279 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22280 }
22281 break;
22282 case DW_FORM_string:
22283 case DW_FORM_strp:
22284 case DW_FORM_strx:
22285 case DW_FORM_GNU_str_index:
22286 case DW_FORM_GNU_strp_alt:
22287 /* DW_STRING is already allocated on the objfile obstack, point
22288 directly to it. */
22289 *bytes = (const gdb_byte *) DW_STRING (attr);
22290 break;
22291 case DW_FORM_block1:
22292 case DW_FORM_block2:
22293 case DW_FORM_block4:
22294 case DW_FORM_block:
22295 case DW_FORM_exprloc:
22296 case DW_FORM_data16:
22297 blk = DW_BLOCK (attr);
22298 if (TYPE_LENGTH (type) != blk->size)
22299 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22300 TYPE_LENGTH (type));
22301 *bytes = blk->data;
22302 break;
22303
22304 /* The DW_AT_const_value attributes are supposed to carry the
22305 symbol's value "represented as it would be on the target
22306 architecture." By the time we get here, it's already been
22307 converted to host endianness, so we just need to sign- or
22308 zero-extend it as appropriate. */
22309 case DW_FORM_data1:
22310 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22311 break;
22312 case DW_FORM_data2:
22313 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22314 break;
22315 case DW_FORM_data4:
22316 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22317 break;
22318 case DW_FORM_data8:
22319 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22320 break;
22321
22322 case DW_FORM_sdata:
22323 case DW_FORM_implicit_const:
22324 *value = DW_SND (attr);
22325 break;
22326
22327 case DW_FORM_udata:
22328 *value = DW_UNSND (attr);
22329 break;
22330
22331 default:
22332 complaint (_("unsupported const value attribute form: '%s'"),
22333 dwarf_form_name (attr->form));
22334 *value = 0;
22335 break;
22336 }
22337 }
22338
22339
22340 /* Copy constant value from an attribute to a symbol. */
22341
22342 static void
22343 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22344 struct dwarf2_cu *cu)
22345 {
22346 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22347 LONGEST value;
22348 const gdb_byte *bytes;
22349 struct dwarf2_locexpr_baton *baton;
22350
22351 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22352 sym->print_name (),
22353 &objfile->objfile_obstack, cu,
22354 &value, &bytes, &baton);
22355
22356 if (baton != NULL)
22357 {
22358 SYMBOL_LOCATION_BATON (sym) = baton;
22359 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22360 }
22361 else if (bytes != NULL)
22362 {
22363 SYMBOL_VALUE_BYTES (sym) = bytes;
22364 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22365 }
22366 else
22367 {
22368 SYMBOL_VALUE (sym) = value;
22369 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22370 }
22371 }
22372
22373 /* Return the type of the die in question using its DW_AT_type attribute. */
22374
22375 static struct type *
22376 die_type (struct die_info *die, struct dwarf2_cu *cu)
22377 {
22378 struct attribute *type_attr;
22379
22380 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22381 if (!type_attr)
22382 {
22383 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22384 /* A missing DW_AT_type represents a void type. */
22385 return objfile_type (objfile)->builtin_void;
22386 }
22387
22388 return lookup_die_type (die, type_attr, cu);
22389 }
22390
22391 /* True iff CU's producer generates GNAT Ada auxiliary information
22392 that allows to find parallel types through that information instead
22393 of having to do expensive parallel lookups by type name. */
22394
22395 static int
22396 need_gnat_info (struct dwarf2_cu *cu)
22397 {
22398 /* Assume that the Ada compiler was GNAT, which always produces
22399 the auxiliary information. */
22400 return (cu->language == language_ada);
22401 }
22402
22403 /* Return the auxiliary type of the die in question using its
22404 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22405 attribute is not present. */
22406
22407 static struct type *
22408 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22409 {
22410 struct attribute *type_attr;
22411
22412 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22413 if (!type_attr)
22414 return NULL;
22415
22416 return lookup_die_type (die, type_attr, cu);
22417 }
22418
22419 /* If DIE has a descriptive_type attribute, then set the TYPE's
22420 descriptive type accordingly. */
22421
22422 static void
22423 set_descriptive_type (struct type *type, struct die_info *die,
22424 struct dwarf2_cu *cu)
22425 {
22426 struct type *descriptive_type = die_descriptive_type (die, cu);
22427
22428 if (descriptive_type)
22429 {
22430 ALLOCATE_GNAT_AUX_TYPE (type);
22431 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22432 }
22433 }
22434
22435 /* Return the containing type of the die in question using its
22436 DW_AT_containing_type attribute. */
22437
22438 static struct type *
22439 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22440 {
22441 struct attribute *type_attr;
22442 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22443
22444 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22445 if (!type_attr)
22446 error (_("Dwarf Error: Problem turning containing type into gdb type "
22447 "[in module %s]"), objfile_name (objfile));
22448
22449 return lookup_die_type (die, type_attr, cu);
22450 }
22451
22452 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22453
22454 static struct type *
22455 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22456 {
22457 struct dwarf2_per_objfile *dwarf2_per_objfile
22458 = cu->per_cu->dwarf2_per_objfile;
22459 struct objfile *objfile = dwarf2_per_objfile->objfile;
22460 char *saved;
22461
22462 std::string message
22463 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22464 objfile_name (objfile),
22465 sect_offset_str (cu->header.sect_off),
22466 sect_offset_str (die->sect_off));
22467 saved = obstack_strdup (&objfile->objfile_obstack, message);
22468
22469 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22470 }
22471
22472 /* Look up the type of DIE in CU using its type attribute ATTR.
22473 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22474 DW_AT_containing_type.
22475 If there is no type substitute an error marker. */
22476
22477 static struct type *
22478 lookup_die_type (struct die_info *die, const struct attribute *attr,
22479 struct dwarf2_cu *cu)
22480 {
22481 struct dwarf2_per_objfile *dwarf2_per_objfile
22482 = cu->per_cu->dwarf2_per_objfile;
22483 struct objfile *objfile = dwarf2_per_objfile->objfile;
22484 struct type *this_type;
22485
22486 gdb_assert (attr->name == DW_AT_type
22487 || attr->name == DW_AT_GNAT_descriptive_type
22488 || attr->name == DW_AT_containing_type);
22489
22490 /* First see if we have it cached. */
22491
22492 if (attr->form == DW_FORM_GNU_ref_alt)
22493 {
22494 struct dwarf2_per_cu_data *per_cu;
22495 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22496
22497 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22498 dwarf2_per_objfile);
22499 this_type = get_die_type_at_offset (sect_off, per_cu);
22500 }
22501 else if (attr_form_is_ref (attr))
22502 {
22503 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22504
22505 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22506 }
22507 else if (attr->form == DW_FORM_ref_sig8)
22508 {
22509 ULONGEST signature = DW_SIGNATURE (attr);
22510
22511 return get_signatured_type (die, signature, cu);
22512 }
22513 else
22514 {
22515 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22516 " at %s [in module %s]"),
22517 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22518 objfile_name (objfile));
22519 return build_error_marker_type (cu, die);
22520 }
22521
22522 /* If not cached we need to read it in. */
22523
22524 if (this_type == NULL)
22525 {
22526 struct die_info *type_die = NULL;
22527 struct dwarf2_cu *type_cu = cu;
22528
22529 if (attr_form_is_ref (attr))
22530 type_die = follow_die_ref (die, attr, &type_cu);
22531 if (type_die == NULL)
22532 return build_error_marker_type (cu, die);
22533 /* If we find the type now, it's probably because the type came
22534 from an inter-CU reference and the type's CU got expanded before
22535 ours. */
22536 this_type = read_type_die (type_die, type_cu);
22537 }
22538
22539 /* If we still don't have a type use an error marker. */
22540
22541 if (this_type == NULL)
22542 return build_error_marker_type (cu, die);
22543
22544 return this_type;
22545 }
22546
22547 /* Return the type in DIE, CU.
22548 Returns NULL for invalid types.
22549
22550 This first does a lookup in die_type_hash,
22551 and only reads the die in if necessary.
22552
22553 NOTE: This can be called when reading in partial or full symbols. */
22554
22555 static struct type *
22556 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22557 {
22558 struct type *this_type;
22559
22560 this_type = get_die_type (die, cu);
22561 if (this_type)
22562 return this_type;
22563
22564 return read_type_die_1 (die, cu);
22565 }
22566
22567 /* Read the type in DIE, CU.
22568 Returns NULL for invalid types. */
22569
22570 static struct type *
22571 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22572 {
22573 struct type *this_type = NULL;
22574
22575 switch (die->tag)
22576 {
22577 case DW_TAG_class_type:
22578 case DW_TAG_interface_type:
22579 case DW_TAG_structure_type:
22580 case DW_TAG_union_type:
22581 this_type = read_structure_type (die, cu);
22582 break;
22583 case DW_TAG_enumeration_type:
22584 this_type = read_enumeration_type (die, cu);
22585 break;
22586 case DW_TAG_subprogram:
22587 case DW_TAG_subroutine_type:
22588 case DW_TAG_inlined_subroutine:
22589 this_type = read_subroutine_type (die, cu);
22590 break;
22591 case DW_TAG_array_type:
22592 this_type = read_array_type (die, cu);
22593 break;
22594 case DW_TAG_set_type:
22595 this_type = read_set_type (die, cu);
22596 break;
22597 case DW_TAG_pointer_type:
22598 this_type = read_tag_pointer_type (die, cu);
22599 break;
22600 case DW_TAG_ptr_to_member_type:
22601 this_type = read_tag_ptr_to_member_type (die, cu);
22602 break;
22603 case DW_TAG_reference_type:
22604 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22605 break;
22606 case DW_TAG_rvalue_reference_type:
22607 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22608 break;
22609 case DW_TAG_const_type:
22610 this_type = read_tag_const_type (die, cu);
22611 break;
22612 case DW_TAG_volatile_type:
22613 this_type = read_tag_volatile_type (die, cu);
22614 break;
22615 case DW_TAG_restrict_type:
22616 this_type = read_tag_restrict_type (die, cu);
22617 break;
22618 case DW_TAG_string_type:
22619 this_type = read_tag_string_type (die, cu);
22620 break;
22621 case DW_TAG_typedef:
22622 this_type = read_typedef (die, cu);
22623 break;
22624 case DW_TAG_subrange_type:
22625 this_type = read_subrange_type (die, cu);
22626 break;
22627 case DW_TAG_base_type:
22628 this_type = read_base_type (die, cu);
22629 break;
22630 case DW_TAG_unspecified_type:
22631 this_type = read_unspecified_type (die, cu);
22632 break;
22633 case DW_TAG_namespace:
22634 this_type = read_namespace_type (die, cu);
22635 break;
22636 case DW_TAG_module:
22637 this_type = read_module_type (die, cu);
22638 break;
22639 case DW_TAG_atomic_type:
22640 this_type = read_tag_atomic_type (die, cu);
22641 break;
22642 default:
22643 complaint (_("unexpected tag in read_type_die: '%s'"),
22644 dwarf_tag_name (die->tag));
22645 break;
22646 }
22647
22648 return this_type;
22649 }
22650
22651 /* See if we can figure out if the class lives in a namespace. We do
22652 this by looking for a member function; its demangled name will
22653 contain namespace info, if there is any.
22654 Return the computed name or NULL.
22655 Space for the result is allocated on the objfile's obstack.
22656 This is the full-die version of guess_partial_die_structure_name.
22657 In this case we know DIE has no useful parent. */
22658
22659 static const char *
22660 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22661 {
22662 struct die_info *spec_die;
22663 struct dwarf2_cu *spec_cu;
22664 struct die_info *child;
22665 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22666
22667 spec_cu = cu;
22668 spec_die = die_specification (die, &spec_cu);
22669 if (spec_die != NULL)
22670 {
22671 die = spec_die;
22672 cu = spec_cu;
22673 }
22674
22675 for (child = die->child;
22676 child != NULL;
22677 child = child->sibling)
22678 {
22679 if (child->tag == DW_TAG_subprogram)
22680 {
22681 const char *linkage_name = dw2_linkage_name (child, cu);
22682
22683 if (linkage_name != NULL)
22684 {
22685 gdb::unique_xmalloc_ptr<char> actual_name
22686 (language_class_name_from_physname (cu->language_defn,
22687 linkage_name));
22688 const char *name = NULL;
22689
22690 if (actual_name != NULL)
22691 {
22692 const char *die_name = dwarf2_name (die, cu);
22693
22694 if (die_name != NULL
22695 && strcmp (die_name, actual_name.get ()) != 0)
22696 {
22697 /* Strip off the class name from the full name.
22698 We want the prefix. */
22699 int die_name_len = strlen (die_name);
22700 int actual_name_len = strlen (actual_name.get ());
22701 const char *ptr = actual_name.get ();
22702
22703 /* Test for '::' as a sanity check. */
22704 if (actual_name_len > die_name_len + 2
22705 && ptr[actual_name_len - die_name_len - 1] == ':')
22706 name = obstack_strndup (
22707 &objfile->per_bfd->storage_obstack,
22708 ptr, actual_name_len - die_name_len - 2);
22709 }
22710 }
22711 return name;
22712 }
22713 }
22714 }
22715
22716 return NULL;
22717 }
22718
22719 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22720 prefix part in such case. See
22721 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22722
22723 static const char *
22724 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22725 {
22726 struct attribute *attr;
22727 const char *base;
22728
22729 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22730 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22731 return NULL;
22732
22733 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22734 return NULL;
22735
22736 attr = dw2_linkage_name_attr (die, cu);
22737 if (attr == NULL || DW_STRING (attr) == NULL)
22738 return NULL;
22739
22740 /* dwarf2_name had to be already called. */
22741 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22742
22743 /* Strip the base name, keep any leading namespaces/classes. */
22744 base = strrchr (DW_STRING (attr), ':');
22745 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22746 return "";
22747
22748 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22749 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22750 DW_STRING (attr),
22751 &base[-1] - DW_STRING (attr));
22752 }
22753
22754 /* Return the name of the namespace/class that DIE is defined within,
22755 or "" if we can't tell. The caller should not xfree the result.
22756
22757 For example, if we're within the method foo() in the following
22758 code:
22759
22760 namespace N {
22761 class C {
22762 void foo () {
22763 }
22764 };
22765 }
22766
22767 then determine_prefix on foo's die will return "N::C". */
22768
22769 static const char *
22770 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22771 {
22772 struct dwarf2_per_objfile *dwarf2_per_objfile
22773 = cu->per_cu->dwarf2_per_objfile;
22774 struct die_info *parent, *spec_die;
22775 struct dwarf2_cu *spec_cu;
22776 struct type *parent_type;
22777 const char *retval;
22778
22779 if (cu->language != language_cplus
22780 && cu->language != language_fortran && cu->language != language_d
22781 && cu->language != language_rust)
22782 return "";
22783
22784 retval = anonymous_struct_prefix (die, cu);
22785 if (retval)
22786 return retval;
22787
22788 /* We have to be careful in the presence of DW_AT_specification.
22789 For example, with GCC 3.4, given the code
22790
22791 namespace N {
22792 void foo() {
22793 // Definition of N::foo.
22794 }
22795 }
22796
22797 then we'll have a tree of DIEs like this:
22798
22799 1: DW_TAG_compile_unit
22800 2: DW_TAG_namespace // N
22801 3: DW_TAG_subprogram // declaration of N::foo
22802 4: DW_TAG_subprogram // definition of N::foo
22803 DW_AT_specification // refers to die #3
22804
22805 Thus, when processing die #4, we have to pretend that we're in
22806 the context of its DW_AT_specification, namely the contex of die
22807 #3. */
22808 spec_cu = cu;
22809 spec_die = die_specification (die, &spec_cu);
22810 if (spec_die == NULL)
22811 parent = die->parent;
22812 else
22813 {
22814 parent = spec_die->parent;
22815 cu = spec_cu;
22816 }
22817
22818 if (parent == NULL)
22819 return "";
22820 else if (parent->building_fullname)
22821 {
22822 const char *name;
22823 const char *parent_name;
22824
22825 /* It has been seen on RealView 2.2 built binaries,
22826 DW_TAG_template_type_param types actually _defined_ as
22827 children of the parent class:
22828
22829 enum E {};
22830 template class <class Enum> Class{};
22831 Class<enum E> class_e;
22832
22833 1: DW_TAG_class_type (Class)
22834 2: DW_TAG_enumeration_type (E)
22835 3: DW_TAG_enumerator (enum1:0)
22836 3: DW_TAG_enumerator (enum2:1)
22837 ...
22838 2: DW_TAG_template_type_param
22839 DW_AT_type DW_FORM_ref_udata (E)
22840
22841 Besides being broken debug info, it can put GDB into an
22842 infinite loop. Consider:
22843
22844 When we're building the full name for Class<E>, we'll start
22845 at Class, and go look over its template type parameters,
22846 finding E. We'll then try to build the full name of E, and
22847 reach here. We're now trying to build the full name of E,
22848 and look over the parent DIE for containing scope. In the
22849 broken case, if we followed the parent DIE of E, we'd again
22850 find Class, and once again go look at its template type
22851 arguments, etc., etc. Simply don't consider such parent die
22852 as source-level parent of this die (it can't be, the language
22853 doesn't allow it), and break the loop here. */
22854 name = dwarf2_name (die, cu);
22855 parent_name = dwarf2_name (parent, cu);
22856 complaint (_("template param type '%s' defined within parent '%s'"),
22857 name ? name : "<unknown>",
22858 parent_name ? parent_name : "<unknown>");
22859 return "";
22860 }
22861 else
22862 switch (parent->tag)
22863 {
22864 case DW_TAG_namespace:
22865 parent_type = read_type_die (parent, cu);
22866 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22867 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22868 Work around this problem here. */
22869 if (cu->language == language_cplus
22870 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22871 return "";
22872 /* We give a name to even anonymous namespaces. */
22873 return TYPE_NAME (parent_type);
22874 case DW_TAG_class_type:
22875 case DW_TAG_interface_type:
22876 case DW_TAG_structure_type:
22877 case DW_TAG_union_type:
22878 case DW_TAG_module:
22879 parent_type = read_type_die (parent, cu);
22880 if (TYPE_NAME (parent_type) != NULL)
22881 return TYPE_NAME (parent_type);
22882 else
22883 /* An anonymous structure is only allowed non-static data
22884 members; no typedefs, no member functions, et cetera.
22885 So it does not need a prefix. */
22886 return "";
22887 case DW_TAG_compile_unit:
22888 case DW_TAG_partial_unit:
22889 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22890 if (cu->language == language_cplus
22891 && !dwarf2_per_objfile->types.empty ()
22892 && die->child != NULL
22893 && (die->tag == DW_TAG_class_type
22894 || die->tag == DW_TAG_structure_type
22895 || die->tag == DW_TAG_union_type))
22896 {
22897 const char *name = guess_full_die_structure_name (die, cu);
22898 if (name != NULL)
22899 return name;
22900 }
22901 return "";
22902 case DW_TAG_subprogram:
22903 /* Nested subroutines in Fortran get a prefix with the name
22904 of the parent's subroutine. */
22905 if (cu->language == language_fortran)
22906 {
22907 if ((die->tag == DW_TAG_subprogram)
22908 && (dwarf2_name (parent, cu) != NULL))
22909 return dwarf2_name (parent, cu);
22910 }
22911 return determine_prefix (parent, cu);
22912 case DW_TAG_enumeration_type:
22913 parent_type = read_type_die (parent, cu);
22914 if (TYPE_DECLARED_CLASS (parent_type))
22915 {
22916 if (TYPE_NAME (parent_type) != NULL)
22917 return TYPE_NAME (parent_type);
22918 return "";
22919 }
22920 /* Fall through. */
22921 default:
22922 return determine_prefix (parent, cu);
22923 }
22924 }
22925
22926 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22927 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22928 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22929 an obconcat, otherwise allocate storage for the result. The CU argument is
22930 used to determine the language and hence, the appropriate separator. */
22931
22932 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22933
22934 static char *
22935 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22936 int physname, struct dwarf2_cu *cu)
22937 {
22938 const char *lead = "";
22939 const char *sep;
22940
22941 if (suffix == NULL || suffix[0] == '\0'
22942 || prefix == NULL || prefix[0] == '\0')
22943 sep = "";
22944 else if (cu->language == language_d)
22945 {
22946 /* For D, the 'main' function could be defined in any module, but it
22947 should never be prefixed. */
22948 if (strcmp (suffix, "D main") == 0)
22949 {
22950 prefix = "";
22951 sep = "";
22952 }
22953 else
22954 sep = ".";
22955 }
22956 else if (cu->language == language_fortran && physname)
22957 {
22958 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22959 DW_AT_MIPS_linkage_name is preferred and used instead. */
22960
22961 lead = "__";
22962 sep = "_MOD_";
22963 }
22964 else
22965 sep = "::";
22966
22967 if (prefix == NULL)
22968 prefix = "";
22969 if (suffix == NULL)
22970 suffix = "";
22971
22972 if (obs == NULL)
22973 {
22974 char *retval
22975 = ((char *)
22976 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22977
22978 strcpy (retval, lead);
22979 strcat (retval, prefix);
22980 strcat (retval, sep);
22981 strcat (retval, suffix);
22982 return retval;
22983 }
22984 else
22985 {
22986 /* We have an obstack. */
22987 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22988 }
22989 }
22990
22991 /* Return sibling of die, NULL if no sibling. */
22992
22993 static struct die_info *
22994 sibling_die (struct die_info *die)
22995 {
22996 return die->sibling;
22997 }
22998
22999 /* Get name of a die, return NULL if not found. */
23000
23001 static const char *
23002 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23003 struct obstack *obstack)
23004 {
23005 if (name && cu->language == language_cplus)
23006 {
23007 std::string canon_name = cp_canonicalize_string (name);
23008
23009 if (!canon_name.empty ())
23010 {
23011 if (canon_name != name)
23012 name = obstack_strdup (obstack, canon_name);
23013 }
23014 }
23015
23016 return name;
23017 }
23018
23019 /* Get name of a die, return NULL if not found.
23020 Anonymous namespaces are converted to their magic string. */
23021
23022 static const char *
23023 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23024 {
23025 struct attribute *attr;
23026 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23027
23028 attr = dwarf2_attr (die, DW_AT_name, cu);
23029 if ((!attr || !DW_STRING (attr))
23030 && die->tag != DW_TAG_namespace
23031 && die->tag != DW_TAG_class_type
23032 && die->tag != DW_TAG_interface_type
23033 && die->tag != DW_TAG_structure_type
23034 && die->tag != DW_TAG_union_type)
23035 return NULL;
23036
23037 switch (die->tag)
23038 {
23039 case DW_TAG_compile_unit:
23040 case DW_TAG_partial_unit:
23041 /* Compilation units have a DW_AT_name that is a filename, not
23042 a source language identifier. */
23043 case DW_TAG_enumeration_type:
23044 case DW_TAG_enumerator:
23045 /* These tags always have simple identifiers already; no need
23046 to canonicalize them. */
23047 return DW_STRING (attr);
23048
23049 case DW_TAG_namespace:
23050 if (attr != NULL && DW_STRING (attr) != NULL)
23051 return DW_STRING (attr);
23052 return CP_ANONYMOUS_NAMESPACE_STR;
23053
23054 case DW_TAG_class_type:
23055 case DW_TAG_interface_type:
23056 case DW_TAG_structure_type:
23057 case DW_TAG_union_type:
23058 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23059 structures or unions. These were of the form "._%d" in GCC 4.1,
23060 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23061 and GCC 4.4. We work around this problem by ignoring these. */
23062 if (attr && DW_STRING (attr)
23063 && (startswith (DW_STRING (attr), "._")
23064 || startswith (DW_STRING (attr), "<anonymous")))
23065 return NULL;
23066
23067 /* GCC might emit a nameless typedef that has a linkage name. See
23068 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23069 if (!attr || DW_STRING (attr) == NULL)
23070 {
23071 attr = dw2_linkage_name_attr (die, cu);
23072 if (attr == NULL || DW_STRING (attr) == NULL)
23073 return NULL;
23074
23075 /* Avoid demangling DW_STRING (attr) the second time on a second
23076 call for the same DIE. */
23077 if (!DW_STRING_IS_CANONICAL (attr))
23078 {
23079 gdb::unique_xmalloc_ptr<char> demangled
23080 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
23081
23082 const char *base;
23083
23084 /* FIXME: we already did this for the partial symbol... */
23085 DW_STRING (attr)
23086 = obstack_strdup (&objfile->per_bfd->storage_obstack,
23087 demangled.get ());
23088 DW_STRING_IS_CANONICAL (attr) = 1;
23089
23090 /* Strip any leading namespaces/classes, keep only the base name.
23091 DW_AT_name for named DIEs does not contain the prefixes. */
23092 base = strrchr (DW_STRING (attr), ':');
23093 if (base && base > DW_STRING (attr) && base[-1] == ':')
23094 return &base[1];
23095 else
23096 return DW_STRING (attr);
23097 }
23098 }
23099 break;
23100
23101 default:
23102 break;
23103 }
23104
23105 if (!DW_STRING_IS_CANONICAL (attr))
23106 {
23107 DW_STRING (attr)
23108 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
23109 &objfile->per_bfd->storage_obstack);
23110 DW_STRING_IS_CANONICAL (attr) = 1;
23111 }
23112 return DW_STRING (attr);
23113 }
23114
23115 /* Return the die that this die in an extension of, or NULL if there
23116 is none. *EXT_CU is the CU containing DIE on input, and the CU
23117 containing the return value on output. */
23118
23119 static struct die_info *
23120 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23121 {
23122 struct attribute *attr;
23123
23124 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23125 if (attr == NULL)
23126 return NULL;
23127
23128 return follow_die_ref (die, attr, ext_cu);
23129 }
23130
23131 /* A convenience function that returns an "unknown" DWARF name,
23132 including the value of V. STR is the name of the entity being
23133 printed, e.g., "TAG". */
23134
23135 static const char *
23136 dwarf_unknown (const char *str, unsigned v)
23137 {
23138 char *cell = get_print_cell ();
23139 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
23140 return cell;
23141 }
23142
23143 /* Convert a DIE tag into its string name. */
23144
23145 static const char *
23146 dwarf_tag_name (unsigned tag)
23147 {
23148 const char *name = get_DW_TAG_name (tag);
23149
23150 if (name == NULL)
23151 return dwarf_unknown ("TAG", tag);
23152
23153 return name;
23154 }
23155
23156 /* Convert a DWARF attribute code into its string name. */
23157
23158 static const char *
23159 dwarf_attr_name (unsigned attr)
23160 {
23161 const char *name;
23162
23163 #ifdef MIPS /* collides with DW_AT_HP_block_index */
23164 if (attr == DW_AT_MIPS_fde)
23165 return "DW_AT_MIPS_fde";
23166 #else
23167 if (attr == DW_AT_HP_block_index)
23168 return "DW_AT_HP_block_index";
23169 #endif
23170
23171 name = get_DW_AT_name (attr);
23172
23173 if (name == NULL)
23174 return dwarf_unknown ("AT", attr);
23175
23176 return name;
23177 }
23178
23179 /* Convert a unit type to corresponding DW_UT name. */
23180
23181 static const char *
23182 dwarf_unit_type_name (int unit_type) {
23183 switch (unit_type)
23184 {
23185 case 0x01:
23186 return "DW_UT_compile (0x01)";
23187 case 0x02:
23188 return "DW_UT_type (0x02)";
23189 case 0x03:
23190 return "DW_UT_partial (0x03)";
23191 case 0x04:
23192 return "DW_UT_skeleton (0x04)";
23193 case 0x05:
23194 return "DW_UT_split_compile (0x05)";
23195 case 0x06:
23196 return "DW_UT_split_type (0x06)";
23197 case 0x80:
23198 return "DW_UT_lo_user (0x80)";
23199 case 0xff:
23200 return "DW_UT_hi_user (0xff)";
23201 default:
23202 return nullptr;
23203 }
23204 }
23205
23206 /* Convert a DWARF value form code into its string name. */
23207
23208 static const char *
23209 dwarf_form_name (unsigned form)
23210 {
23211 const char *name = get_DW_FORM_name (form);
23212
23213 if (name == NULL)
23214 return dwarf_unknown ("FORM", form);
23215
23216 return name;
23217 }
23218
23219 static const char *
23220 dwarf_bool_name (unsigned mybool)
23221 {
23222 if (mybool)
23223 return "TRUE";
23224 else
23225 return "FALSE";
23226 }
23227
23228 /* Convert a DWARF type code into its string name. */
23229
23230 static const char *
23231 dwarf_type_encoding_name (unsigned enc)
23232 {
23233 const char *name = get_DW_ATE_name (enc);
23234
23235 if (name == NULL)
23236 return dwarf_unknown ("ATE", enc);
23237
23238 return name;
23239 }
23240
23241 static void
23242 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23243 {
23244 unsigned int i;
23245
23246 print_spaces (indent, f);
23247 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23248 dwarf_tag_name (die->tag), die->abbrev,
23249 sect_offset_str (die->sect_off));
23250
23251 if (die->parent != NULL)
23252 {
23253 print_spaces (indent, f);
23254 fprintf_unfiltered (f, " parent at offset: %s\n",
23255 sect_offset_str (die->parent->sect_off));
23256 }
23257
23258 print_spaces (indent, f);
23259 fprintf_unfiltered (f, " has children: %s\n",
23260 dwarf_bool_name (die->child != NULL));
23261
23262 print_spaces (indent, f);
23263 fprintf_unfiltered (f, " attributes:\n");
23264
23265 for (i = 0; i < die->num_attrs; ++i)
23266 {
23267 print_spaces (indent, f);
23268 fprintf_unfiltered (f, " %s (%s) ",
23269 dwarf_attr_name (die->attrs[i].name),
23270 dwarf_form_name (die->attrs[i].form));
23271
23272 switch (die->attrs[i].form)
23273 {
23274 case DW_FORM_addr:
23275 case DW_FORM_addrx:
23276 case DW_FORM_GNU_addr_index:
23277 fprintf_unfiltered (f, "address: ");
23278 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
23279 break;
23280 case DW_FORM_block2:
23281 case DW_FORM_block4:
23282 case DW_FORM_block:
23283 case DW_FORM_block1:
23284 fprintf_unfiltered (f, "block: size %s",
23285 pulongest (DW_BLOCK (&die->attrs[i])->size));
23286 break;
23287 case DW_FORM_exprloc:
23288 fprintf_unfiltered (f, "expression: size %s",
23289 pulongest (DW_BLOCK (&die->attrs[i])->size));
23290 break;
23291 case DW_FORM_data16:
23292 fprintf_unfiltered (f, "constant of 16 bytes");
23293 break;
23294 case DW_FORM_ref_addr:
23295 fprintf_unfiltered (f, "ref address: ");
23296 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23297 break;
23298 case DW_FORM_GNU_ref_alt:
23299 fprintf_unfiltered (f, "alt ref address: ");
23300 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23301 break;
23302 case DW_FORM_ref1:
23303 case DW_FORM_ref2:
23304 case DW_FORM_ref4:
23305 case DW_FORM_ref8:
23306 case DW_FORM_ref_udata:
23307 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23308 (long) (DW_UNSND (&die->attrs[i])));
23309 break;
23310 case DW_FORM_data1:
23311 case DW_FORM_data2:
23312 case DW_FORM_data4:
23313 case DW_FORM_data8:
23314 case DW_FORM_udata:
23315 case DW_FORM_sdata:
23316 fprintf_unfiltered (f, "constant: %s",
23317 pulongest (DW_UNSND (&die->attrs[i])));
23318 break;
23319 case DW_FORM_sec_offset:
23320 fprintf_unfiltered (f, "section offset: %s",
23321 pulongest (DW_UNSND (&die->attrs[i])));
23322 break;
23323 case DW_FORM_ref_sig8:
23324 fprintf_unfiltered (f, "signature: %s",
23325 hex_string (DW_SIGNATURE (&die->attrs[i])));
23326 break;
23327 case DW_FORM_string:
23328 case DW_FORM_strp:
23329 case DW_FORM_line_strp:
23330 case DW_FORM_strx:
23331 case DW_FORM_GNU_str_index:
23332 case DW_FORM_GNU_strp_alt:
23333 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23334 DW_STRING (&die->attrs[i])
23335 ? DW_STRING (&die->attrs[i]) : "",
23336 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
23337 break;
23338 case DW_FORM_flag:
23339 if (DW_UNSND (&die->attrs[i]))
23340 fprintf_unfiltered (f, "flag: TRUE");
23341 else
23342 fprintf_unfiltered (f, "flag: FALSE");
23343 break;
23344 case DW_FORM_flag_present:
23345 fprintf_unfiltered (f, "flag: TRUE");
23346 break;
23347 case DW_FORM_indirect:
23348 /* The reader will have reduced the indirect form to
23349 the "base form" so this form should not occur. */
23350 fprintf_unfiltered (f,
23351 "unexpected attribute form: DW_FORM_indirect");
23352 break;
23353 case DW_FORM_implicit_const:
23354 fprintf_unfiltered (f, "constant: %s",
23355 plongest (DW_SND (&die->attrs[i])));
23356 break;
23357 default:
23358 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23359 die->attrs[i].form);
23360 break;
23361 }
23362 fprintf_unfiltered (f, "\n");
23363 }
23364 }
23365
23366 static void
23367 dump_die_for_error (struct die_info *die)
23368 {
23369 dump_die_shallow (gdb_stderr, 0, die);
23370 }
23371
23372 static void
23373 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23374 {
23375 int indent = level * 4;
23376
23377 gdb_assert (die != NULL);
23378
23379 if (level >= max_level)
23380 return;
23381
23382 dump_die_shallow (f, indent, die);
23383
23384 if (die->child != NULL)
23385 {
23386 print_spaces (indent, f);
23387 fprintf_unfiltered (f, " Children:");
23388 if (level + 1 < max_level)
23389 {
23390 fprintf_unfiltered (f, "\n");
23391 dump_die_1 (f, level + 1, max_level, die->child);
23392 }
23393 else
23394 {
23395 fprintf_unfiltered (f,
23396 " [not printed, max nesting level reached]\n");
23397 }
23398 }
23399
23400 if (die->sibling != NULL && level > 0)
23401 {
23402 dump_die_1 (f, level, max_level, die->sibling);
23403 }
23404 }
23405
23406 /* This is called from the pdie macro in gdbinit.in.
23407 It's not static so gcc will keep a copy callable from gdb. */
23408
23409 void
23410 dump_die (struct die_info *die, int max_level)
23411 {
23412 dump_die_1 (gdb_stdlog, 0, max_level, die);
23413 }
23414
23415 static void
23416 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23417 {
23418 void **slot;
23419
23420 slot = htab_find_slot_with_hash (cu->die_hash, die,
23421 to_underlying (die->sect_off),
23422 INSERT);
23423
23424 *slot = die;
23425 }
23426
23427 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23428 required kind. */
23429
23430 static sect_offset
23431 dwarf2_get_ref_die_offset (const struct attribute *attr)
23432 {
23433 if (attr_form_is_ref (attr))
23434 return (sect_offset) DW_UNSND (attr);
23435
23436 complaint (_("unsupported die ref attribute form: '%s'"),
23437 dwarf_form_name (attr->form));
23438 return {};
23439 }
23440
23441 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23442 * the value held by the attribute is not constant. */
23443
23444 static LONGEST
23445 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23446 {
23447 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23448 return DW_SND (attr);
23449 else if (attr->form == DW_FORM_udata
23450 || attr->form == DW_FORM_data1
23451 || attr->form == DW_FORM_data2
23452 || attr->form == DW_FORM_data4
23453 || attr->form == DW_FORM_data8)
23454 return DW_UNSND (attr);
23455 else
23456 {
23457 /* For DW_FORM_data16 see attr_form_is_constant. */
23458 complaint (_("Attribute value is not a constant (%s)"),
23459 dwarf_form_name (attr->form));
23460 return default_value;
23461 }
23462 }
23463
23464 /* Follow reference or signature attribute ATTR of SRC_DIE.
23465 On entry *REF_CU is the CU of SRC_DIE.
23466 On exit *REF_CU is the CU of the result. */
23467
23468 static struct die_info *
23469 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23470 struct dwarf2_cu **ref_cu)
23471 {
23472 struct die_info *die;
23473
23474 if (attr_form_is_ref (attr))
23475 die = follow_die_ref (src_die, attr, ref_cu);
23476 else if (attr->form == DW_FORM_ref_sig8)
23477 die = follow_die_sig (src_die, attr, ref_cu);
23478 else
23479 {
23480 dump_die_for_error (src_die);
23481 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23482 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23483 }
23484
23485 return die;
23486 }
23487
23488 /* Follow reference OFFSET.
23489 On entry *REF_CU is the CU of the source die referencing OFFSET.
23490 On exit *REF_CU is the CU of the result.
23491 Returns NULL if OFFSET is invalid. */
23492
23493 static struct die_info *
23494 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23495 struct dwarf2_cu **ref_cu)
23496 {
23497 struct die_info temp_die;
23498 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23499 struct dwarf2_per_objfile *dwarf2_per_objfile
23500 = cu->per_cu->dwarf2_per_objfile;
23501
23502 gdb_assert (cu->per_cu != NULL);
23503
23504 target_cu = cu;
23505
23506 if (cu->per_cu->is_debug_types)
23507 {
23508 /* .debug_types CUs cannot reference anything outside their CU.
23509 If they need to, they have to reference a signatured type via
23510 DW_FORM_ref_sig8. */
23511 if (!offset_in_cu_p (&cu->header, sect_off))
23512 return NULL;
23513 }
23514 else if (offset_in_dwz != cu->per_cu->is_dwz
23515 || !offset_in_cu_p (&cu->header, sect_off))
23516 {
23517 struct dwarf2_per_cu_data *per_cu;
23518
23519 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23520 dwarf2_per_objfile);
23521
23522 /* If necessary, add it to the queue and load its DIEs. */
23523 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23524 load_full_comp_unit (per_cu, false, cu->language);
23525
23526 target_cu = per_cu->cu;
23527 }
23528 else if (cu->dies == NULL)
23529 {
23530 /* We're loading full DIEs during partial symbol reading. */
23531 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23532 load_full_comp_unit (cu->per_cu, false, language_minimal);
23533 }
23534
23535 *ref_cu = target_cu;
23536 temp_die.sect_off = sect_off;
23537
23538 if (target_cu != cu)
23539 target_cu->ancestor = cu;
23540
23541 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23542 &temp_die,
23543 to_underlying (sect_off));
23544 }
23545
23546 /* Follow reference attribute ATTR of SRC_DIE.
23547 On entry *REF_CU is the CU of SRC_DIE.
23548 On exit *REF_CU is the CU of the result. */
23549
23550 static struct die_info *
23551 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23552 struct dwarf2_cu **ref_cu)
23553 {
23554 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23555 struct dwarf2_cu *cu = *ref_cu;
23556 struct die_info *die;
23557
23558 die = follow_die_offset (sect_off,
23559 (attr->form == DW_FORM_GNU_ref_alt
23560 || cu->per_cu->is_dwz),
23561 ref_cu);
23562 if (!die)
23563 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23564 "at %s [in module %s]"),
23565 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23566 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23567
23568 return die;
23569 }
23570
23571 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23572 Returned value is intended for DW_OP_call*. Returned
23573 dwarf2_locexpr_baton->data has lifetime of
23574 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23575
23576 struct dwarf2_locexpr_baton
23577 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23578 struct dwarf2_per_cu_data *per_cu,
23579 CORE_ADDR (*get_frame_pc) (void *baton),
23580 void *baton, bool resolve_abstract_p)
23581 {
23582 struct dwarf2_cu *cu;
23583 struct die_info *die;
23584 struct attribute *attr;
23585 struct dwarf2_locexpr_baton retval;
23586 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23587 struct objfile *objfile = dwarf2_per_objfile->objfile;
23588
23589 if (per_cu->cu == NULL)
23590 load_cu (per_cu, false);
23591 cu = per_cu->cu;
23592 if (cu == NULL)
23593 {
23594 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23595 Instead just throw an error, not much else we can do. */
23596 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23597 sect_offset_str (sect_off), objfile_name (objfile));
23598 }
23599
23600 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23601 if (!die)
23602 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23603 sect_offset_str (sect_off), objfile_name (objfile));
23604
23605 attr = dwarf2_attr (die, DW_AT_location, cu);
23606 if (!attr && resolve_abstract_p
23607 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
23608 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23609 {
23610 CORE_ADDR pc = (*get_frame_pc) (baton);
23611 CORE_ADDR baseaddr
23612 = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23613 struct gdbarch *gdbarch = get_objfile_arch (objfile);
23614
23615 for (const auto &cand_off
23616 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
23617 {
23618 struct dwarf2_cu *cand_cu = cu;
23619 struct die_info *cand
23620 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23621 if (!cand
23622 || !cand->parent
23623 || cand->parent->tag != DW_TAG_subprogram)
23624 continue;
23625
23626 CORE_ADDR pc_low, pc_high;
23627 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23628 if (pc_low == ((CORE_ADDR) -1))
23629 continue;
23630 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23631 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23632 if (!(pc_low <= pc && pc < pc_high))
23633 continue;
23634
23635 die = cand;
23636 attr = dwarf2_attr (die, DW_AT_location, cu);
23637 break;
23638 }
23639 }
23640
23641 if (!attr)
23642 {
23643 /* DWARF: "If there is no such attribute, then there is no effect.".
23644 DATA is ignored if SIZE is 0. */
23645
23646 retval.data = NULL;
23647 retval.size = 0;
23648 }
23649 else if (attr_form_is_section_offset (attr))
23650 {
23651 struct dwarf2_loclist_baton loclist_baton;
23652 CORE_ADDR pc = (*get_frame_pc) (baton);
23653 size_t size;
23654
23655 fill_in_loclist_baton (cu, &loclist_baton, attr);
23656
23657 retval.data = dwarf2_find_location_expression (&loclist_baton,
23658 &size, pc);
23659 retval.size = size;
23660 }
23661 else
23662 {
23663 if (!attr_form_is_block (attr))
23664 error (_("Dwarf Error: DIE at %s referenced in module %s "
23665 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23666 sect_offset_str (sect_off), objfile_name (objfile));
23667
23668 retval.data = DW_BLOCK (attr)->data;
23669 retval.size = DW_BLOCK (attr)->size;
23670 }
23671 retval.per_cu = cu->per_cu;
23672
23673 age_cached_comp_units (dwarf2_per_objfile);
23674
23675 return retval;
23676 }
23677
23678 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23679 offset. */
23680
23681 struct dwarf2_locexpr_baton
23682 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23683 struct dwarf2_per_cu_data *per_cu,
23684 CORE_ADDR (*get_frame_pc) (void *baton),
23685 void *baton)
23686 {
23687 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23688
23689 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23690 }
23691
23692 /* Write a constant of a given type as target-ordered bytes into
23693 OBSTACK. */
23694
23695 static const gdb_byte *
23696 write_constant_as_bytes (struct obstack *obstack,
23697 enum bfd_endian byte_order,
23698 struct type *type,
23699 ULONGEST value,
23700 LONGEST *len)
23701 {
23702 gdb_byte *result;
23703
23704 *len = TYPE_LENGTH (type);
23705 result = (gdb_byte *) obstack_alloc (obstack, *len);
23706 store_unsigned_integer (result, *len, byte_order, value);
23707
23708 return result;
23709 }
23710
23711 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23712 pointer to the constant bytes and set LEN to the length of the
23713 data. If memory is needed, allocate it on OBSTACK. If the DIE
23714 does not have a DW_AT_const_value, return NULL. */
23715
23716 const gdb_byte *
23717 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23718 struct dwarf2_per_cu_data *per_cu,
23719 struct obstack *obstack,
23720 LONGEST *len)
23721 {
23722 struct dwarf2_cu *cu;
23723 struct die_info *die;
23724 struct attribute *attr;
23725 const gdb_byte *result = NULL;
23726 struct type *type;
23727 LONGEST value;
23728 enum bfd_endian byte_order;
23729 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23730
23731 if (per_cu->cu == NULL)
23732 load_cu (per_cu, false);
23733 cu = per_cu->cu;
23734 if (cu == NULL)
23735 {
23736 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23737 Instead just throw an error, not much else we can do. */
23738 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23739 sect_offset_str (sect_off), objfile_name (objfile));
23740 }
23741
23742 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23743 if (!die)
23744 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23745 sect_offset_str (sect_off), objfile_name (objfile));
23746
23747 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23748 if (attr == NULL)
23749 return NULL;
23750
23751 byte_order = (bfd_big_endian (objfile->obfd)
23752 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23753
23754 switch (attr->form)
23755 {
23756 case DW_FORM_addr:
23757 case DW_FORM_addrx:
23758 case DW_FORM_GNU_addr_index:
23759 {
23760 gdb_byte *tem;
23761
23762 *len = cu->header.addr_size;
23763 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23764 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23765 result = tem;
23766 }
23767 break;
23768 case DW_FORM_string:
23769 case DW_FORM_strp:
23770 case DW_FORM_strx:
23771 case DW_FORM_GNU_str_index:
23772 case DW_FORM_GNU_strp_alt:
23773 /* DW_STRING is already allocated on the objfile obstack, point
23774 directly to it. */
23775 result = (const gdb_byte *) DW_STRING (attr);
23776 *len = strlen (DW_STRING (attr));
23777 break;
23778 case DW_FORM_block1:
23779 case DW_FORM_block2:
23780 case DW_FORM_block4:
23781 case DW_FORM_block:
23782 case DW_FORM_exprloc:
23783 case DW_FORM_data16:
23784 result = DW_BLOCK (attr)->data;
23785 *len = DW_BLOCK (attr)->size;
23786 break;
23787
23788 /* The DW_AT_const_value attributes are supposed to carry the
23789 symbol's value "represented as it would be on the target
23790 architecture." By the time we get here, it's already been
23791 converted to host endianness, so we just need to sign- or
23792 zero-extend it as appropriate. */
23793 case DW_FORM_data1:
23794 type = die_type (die, cu);
23795 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23796 if (result == NULL)
23797 result = write_constant_as_bytes (obstack, byte_order,
23798 type, value, len);
23799 break;
23800 case DW_FORM_data2:
23801 type = die_type (die, cu);
23802 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23803 if (result == NULL)
23804 result = write_constant_as_bytes (obstack, byte_order,
23805 type, value, len);
23806 break;
23807 case DW_FORM_data4:
23808 type = die_type (die, cu);
23809 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23810 if (result == NULL)
23811 result = write_constant_as_bytes (obstack, byte_order,
23812 type, value, len);
23813 break;
23814 case DW_FORM_data8:
23815 type = die_type (die, cu);
23816 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23817 if (result == NULL)
23818 result = write_constant_as_bytes (obstack, byte_order,
23819 type, value, len);
23820 break;
23821
23822 case DW_FORM_sdata:
23823 case DW_FORM_implicit_const:
23824 type = die_type (die, cu);
23825 result = write_constant_as_bytes (obstack, byte_order,
23826 type, DW_SND (attr), len);
23827 break;
23828
23829 case DW_FORM_udata:
23830 type = die_type (die, cu);
23831 result = write_constant_as_bytes (obstack, byte_order,
23832 type, DW_UNSND (attr), len);
23833 break;
23834
23835 default:
23836 complaint (_("unsupported const value attribute form: '%s'"),
23837 dwarf_form_name (attr->form));
23838 break;
23839 }
23840
23841 return result;
23842 }
23843
23844 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23845 valid type for this die is found. */
23846
23847 struct type *
23848 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23849 struct dwarf2_per_cu_data *per_cu)
23850 {
23851 struct dwarf2_cu *cu;
23852 struct die_info *die;
23853
23854 if (per_cu->cu == NULL)
23855 load_cu (per_cu, false);
23856 cu = per_cu->cu;
23857 if (!cu)
23858 return NULL;
23859
23860 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23861 if (!die)
23862 return NULL;
23863
23864 return die_type (die, cu);
23865 }
23866
23867 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23868 PER_CU. */
23869
23870 struct type *
23871 dwarf2_get_die_type (cu_offset die_offset,
23872 struct dwarf2_per_cu_data *per_cu)
23873 {
23874 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23875 return get_die_type_at_offset (die_offset_sect, per_cu);
23876 }
23877
23878 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23879 On entry *REF_CU is the CU of SRC_DIE.
23880 On exit *REF_CU is the CU of the result.
23881 Returns NULL if the referenced DIE isn't found. */
23882
23883 static struct die_info *
23884 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23885 struct dwarf2_cu **ref_cu)
23886 {
23887 struct die_info temp_die;
23888 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23889 struct die_info *die;
23890
23891 /* While it might be nice to assert sig_type->type == NULL here,
23892 we can get here for DW_AT_imported_declaration where we need
23893 the DIE not the type. */
23894
23895 /* If necessary, add it to the queue and load its DIEs. */
23896
23897 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23898 read_signatured_type (sig_type);
23899
23900 sig_cu = sig_type->per_cu.cu;
23901 gdb_assert (sig_cu != NULL);
23902 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23903 temp_die.sect_off = sig_type->type_offset_in_section;
23904 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23905 to_underlying (temp_die.sect_off));
23906 if (die)
23907 {
23908 struct dwarf2_per_objfile *dwarf2_per_objfile
23909 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23910
23911 /* For .gdb_index version 7 keep track of included TUs.
23912 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23913 if (dwarf2_per_objfile->index_table != NULL
23914 && dwarf2_per_objfile->index_table->version <= 7)
23915 {
23916 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23917 }
23918
23919 *ref_cu = sig_cu;
23920 if (sig_cu != cu)
23921 sig_cu->ancestor = cu;
23922
23923 return die;
23924 }
23925
23926 return NULL;
23927 }
23928
23929 /* Follow signatured type referenced by ATTR in SRC_DIE.
23930 On entry *REF_CU is the CU of SRC_DIE.
23931 On exit *REF_CU is the CU of the result.
23932 The result is the DIE of the type.
23933 If the referenced type cannot be found an error is thrown. */
23934
23935 static struct die_info *
23936 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23937 struct dwarf2_cu **ref_cu)
23938 {
23939 ULONGEST signature = DW_SIGNATURE (attr);
23940 struct signatured_type *sig_type;
23941 struct die_info *die;
23942
23943 gdb_assert (attr->form == DW_FORM_ref_sig8);
23944
23945 sig_type = lookup_signatured_type (*ref_cu, signature);
23946 /* sig_type will be NULL if the signatured type is missing from
23947 the debug info. */
23948 if (sig_type == NULL)
23949 {
23950 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23951 " from DIE at %s [in module %s]"),
23952 hex_string (signature), sect_offset_str (src_die->sect_off),
23953 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23954 }
23955
23956 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23957 if (die == NULL)
23958 {
23959 dump_die_for_error (src_die);
23960 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23961 " from DIE at %s [in module %s]"),
23962 hex_string (signature), sect_offset_str (src_die->sect_off),
23963 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23964 }
23965
23966 return die;
23967 }
23968
23969 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23970 reading in and processing the type unit if necessary. */
23971
23972 static struct type *
23973 get_signatured_type (struct die_info *die, ULONGEST signature,
23974 struct dwarf2_cu *cu)
23975 {
23976 struct dwarf2_per_objfile *dwarf2_per_objfile
23977 = cu->per_cu->dwarf2_per_objfile;
23978 struct signatured_type *sig_type;
23979 struct dwarf2_cu *type_cu;
23980 struct die_info *type_die;
23981 struct type *type;
23982
23983 sig_type = lookup_signatured_type (cu, signature);
23984 /* sig_type will be NULL if the signatured type is missing from
23985 the debug info. */
23986 if (sig_type == NULL)
23987 {
23988 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23989 " from DIE at %s [in module %s]"),
23990 hex_string (signature), sect_offset_str (die->sect_off),
23991 objfile_name (dwarf2_per_objfile->objfile));
23992 return build_error_marker_type (cu, die);
23993 }
23994
23995 /* If we already know the type we're done. */
23996 if (sig_type->type != NULL)
23997 return sig_type->type;
23998
23999 type_cu = cu;
24000 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24001 if (type_die != NULL)
24002 {
24003 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24004 is created. This is important, for example, because for c++ classes
24005 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24006 type = read_type_die (type_die, type_cu);
24007 if (type == NULL)
24008 {
24009 complaint (_("Dwarf Error: Cannot build signatured type %s"
24010 " referenced from DIE at %s [in module %s]"),
24011 hex_string (signature), sect_offset_str (die->sect_off),
24012 objfile_name (dwarf2_per_objfile->objfile));
24013 type = build_error_marker_type (cu, die);
24014 }
24015 }
24016 else
24017 {
24018 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24019 " from DIE at %s [in module %s]"),
24020 hex_string (signature), sect_offset_str (die->sect_off),
24021 objfile_name (dwarf2_per_objfile->objfile));
24022 type = build_error_marker_type (cu, die);
24023 }
24024 sig_type->type = type;
24025
24026 return type;
24027 }
24028
24029 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24030 reading in and processing the type unit if necessary. */
24031
24032 static struct type *
24033 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24034 struct dwarf2_cu *cu) /* ARI: editCase function */
24035 {
24036 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24037 if (attr_form_is_ref (attr))
24038 {
24039 struct dwarf2_cu *type_cu = cu;
24040 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24041
24042 return read_type_die (type_die, type_cu);
24043 }
24044 else if (attr->form == DW_FORM_ref_sig8)
24045 {
24046 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
24047 }
24048 else
24049 {
24050 struct dwarf2_per_objfile *dwarf2_per_objfile
24051 = cu->per_cu->dwarf2_per_objfile;
24052
24053 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24054 " at %s [in module %s]"),
24055 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24056 objfile_name (dwarf2_per_objfile->objfile));
24057 return build_error_marker_type (cu, die);
24058 }
24059 }
24060
24061 /* Load the DIEs associated with type unit PER_CU into memory. */
24062
24063 static void
24064 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
24065 {
24066 struct signatured_type *sig_type;
24067
24068 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24069 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
24070
24071 /* We have the per_cu, but we need the signatured_type.
24072 Fortunately this is an easy translation. */
24073 gdb_assert (per_cu->is_debug_types);
24074 sig_type = (struct signatured_type *) per_cu;
24075
24076 gdb_assert (per_cu->cu == NULL);
24077
24078 read_signatured_type (sig_type);
24079
24080 gdb_assert (per_cu->cu != NULL);
24081 }
24082
24083 /* die_reader_func for read_signatured_type.
24084 This is identical to load_full_comp_unit_reader,
24085 but is kept separate for now. */
24086
24087 static void
24088 read_signatured_type_reader (const struct die_reader_specs *reader,
24089 const gdb_byte *info_ptr,
24090 struct die_info *comp_unit_die,
24091 int has_children,
24092 void *data)
24093 {
24094 struct dwarf2_cu *cu = reader->cu;
24095
24096 gdb_assert (cu->die_hash == NULL);
24097 cu->die_hash =
24098 htab_create_alloc_ex (cu->header.length / 12,
24099 die_hash,
24100 die_eq,
24101 NULL,
24102 &cu->comp_unit_obstack,
24103 hashtab_obstack_allocate,
24104 dummy_obstack_deallocate);
24105
24106 if (has_children)
24107 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
24108 &info_ptr, comp_unit_die);
24109 cu->dies = comp_unit_die;
24110 /* comp_unit_die is not stored in die_hash, no need. */
24111
24112 /* We try not to read any attributes in this function, because not
24113 all CUs needed for references have been loaded yet, and symbol
24114 table processing isn't initialized. But we have to set the CU language,
24115 or we won't be able to build types correctly.
24116 Similarly, if we do not read the producer, we can not apply
24117 producer-specific interpretation. */
24118 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24119 }
24120
24121 /* Read in a signatured type and build its CU and DIEs.
24122 If the type is a stub for the real type in a DWO file,
24123 read in the real type from the DWO file as well. */
24124
24125 static void
24126 read_signatured_type (struct signatured_type *sig_type)
24127 {
24128 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24129
24130 gdb_assert (per_cu->is_debug_types);
24131 gdb_assert (per_cu->cu == NULL);
24132
24133 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
24134 read_signatured_type_reader, NULL);
24135 sig_type->per_cu.tu_read = 1;
24136 }
24137
24138 /* Decode simple location descriptions.
24139 Given a pointer to a dwarf block that defines a location, compute
24140 the location and return the value.
24141
24142 NOTE drow/2003-11-18: This function is called in two situations
24143 now: for the address of static or global variables (partial symbols
24144 only) and for offsets into structures which are expected to be
24145 (more or less) constant. The partial symbol case should go away,
24146 and only the constant case should remain. That will let this
24147 function complain more accurately. A few special modes are allowed
24148 without complaint for global variables (for instance, global
24149 register values and thread-local values).
24150
24151 A location description containing no operations indicates that the
24152 object is optimized out. The return value is 0 for that case.
24153 FIXME drow/2003-11-16: No callers check for this case any more; soon all
24154 callers will only want a very basic result and this can become a
24155 complaint.
24156
24157 Note that stack[0] is unused except as a default error return. */
24158
24159 static CORE_ADDR
24160 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
24161 {
24162 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
24163 size_t i;
24164 size_t size = blk->size;
24165 const gdb_byte *data = blk->data;
24166 CORE_ADDR stack[64];
24167 int stacki;
24168 unsigned int bytes_read, unsnd;
24169 gdb_byte op;
24170
24171 i = 0;
24172 stacki = 0;
24173 stack[stacki] = 0;
24174 stack[++stacki] = 0;
24175
24176 while (i < size)
24177 {
24178 op = data[i++];
24179 switch (op)
24180 {
24181 case DW_OP_lit0:
24182 case DW_OP_lit1:
24183 case DW_OP_lit2:
24184 case DW_OP_lit3:
24185 case DW_OP_lit4:
24186 case DW_OP_lit5:
24187 case DW_OP_lit6:
24188 case DW_OP_lit7:
24189 case DW_OP_lit8:
24190 case DW_OP_lit9:
24191 case DW_OP_lit10:
24192 case DW_OP_lit11:
24193 case DW_OP_lit12:
24194 case DW_OP_lit13:
24195 case DW_OP_lit14:
24196 case DW_OP_lit15:
24197 case DW_OP_lit16:
24198 case DW_OP_lit17:
24199 case DW_OP_lit18:
24200 case DW_OP_lit19:
24201 case DW_OP_lit20:
24202 case DW_OP_lit21:
24203 case DW_OP_lit22:
24204 case DW_OP_lit23:
24205 case DW_OP_lit24:
24206 case DW_OP_lit25:
24207 case DW_OP_lit26:
24208 case DW_OP_lit27:
24209 case DW_OP_lit28:
24210 case DW_OP_lit29:
24211 case DW_OP_lit30:
24212 case DW_OP_lit31:
24213 stack[++stacki] = op - DW_OP_lit0;
24214 break;
24215
24216 case DW_OP_reg0:
24217 case DW_OP_reg1:
24218 case DW_OP_reg2:
24219 case DW_OP_reg3:
24220 case DW_OP_reg4:
24221 case DW_OP_reg5:
24222 case DW_OP_reg6:
24223 case DW_OP_reg7:
24224 case DW_OP_reg8:
24225 case DW_OP_reg9:
24226 case DW_OP_reg10:
24227 case DW_OP_reg11:
24228 case DW_OP_reg12:
24229 case DW_OP_reg13:
24230 case DW_OP_reg14:
24231 case DW_OP_reg15:
24232 case DW_OP_reg16:
24233 case DW_OP_reg17:
24234 case DW_OP_reg18:
24235 case DW_OP_reg19:
24236 case DW_OP_reg20:
24237 case DW_OP_reg21:
24238 case DW_OP_reg22:
24239 case DW_OP_reg23:
24240 case DW_OP_reg24:
24241 case DW_OP_reg25:
24242 case DW_OP_reg26:
24243 case DW_OP_reg27:
24244 case DW_OP_reg28:
24245 case DW_OP_reg29:
24246 case DW_OP_reg30:
24247 case DW_OP_reg31:
24248 stack[++stacki] = op - DW_OP_reg0;
24249 if (i < size)
24250 dwarf2_complex_location_expr_complaint ();
24251 break;
24252
24253 case DW_OP_regx:
24254 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24255 i += bytes_read;
24256 stack[++stacki] = unsnd;
24257 if (i < size)
24258 dwarf2_complex_location_expr_complaint ();
24259 break;
24260
24261 case DW_OP_addr:
24262 stack[++stacki] = read_address (objfile->obfd, &data[i],
24263 cu, &bytes_read);
24264 i += bytes_read;
24265 break;
24266
24267 case DW_OP_const1u:
24268 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24269 i += 1;
24270 break;
24271
24272 case DW_OP_const1s:
24273 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24274 i += 1;
24275 break;
24276
24277 case DW_OP_const2u:
24278 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24279 i += 2;
24280 break;
24281
24282 case DW_OP_const2s:
24283 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24284 i += 2;
24285 break;
24286
24287 case DW_OP_const4u:
24288 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24289 i += 4;
24290 break;
24291
24292 case DW_OP_const4s:
24293 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24294 i += 4;
24295 break;
24296
24297 case DW_OP_const8u:
24298 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24299 i += 8;
24300 break;
24301
24302 case DW_OP_constu:
24303 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24304 &bytes_read);
24305 i += bytes_read;
24306 break;
24307
24308 case DW_OP_consts:
24309 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24310 i += bytes_read;
24311 break;
24312
24313 case DW_OP_dup:
24314 stack[stacki + 1] = stack[stacki];
24315 stacki++;
24316 break;
24317
24318 case DW_OP_plus:
24319 stack[stacki - 1] += stack[stacki];
24320 stacki--;
24321 break;
24322
24323 case DW_OP_plus_uconst:
24324 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24325 &bytes_read);
24326 i += bytes_read;
24327 break;
24328
24329 case DW_OP_minus:
24330 stack[stacki - 1] -= stack[stacki];
24331 stacki--;
24332 break;
24333
24334 case DW_OP_deref:
24335 /* If we're not the last op, then we definitely can't encode
24336 this using GDB's address_class enum. This is valid for partial
24337 global symbols, although the variable's address will be bogus
24338 in the psymtab. */
24339 if (i < size)
24340 dwarf2_complex_location_expr_complaint ();
24341 break;
24342
24343 case DW_OP_GNU_push_tls_address:
24344 case DW_OP_form_tls_address:
24345 /* The top of the stack has the offset from the beginning
24346 of the thread control block at which the variable is located. */
24347 /* Nothing should follow this operator, so the top of stack would
24348 be returned. */
24349 /* This is valid for partial global symbols, but the variable's
24350 address will be bogus in the psymtab. Make it always at least
24351 non-zero to not look as a variable garbage collected by linker
24352 which have DW_OP_addr 0. */
24353 if (i < size)
24354 dwarf2_complex_location_expr_complaint ();
24355 stack[stacki]++;
24356 break;
24357
24358 case DW_OP_GNU_uninit:
24359 break;
24360
24361 case DW_OP_addrx:
24362 case DW_OP_GNU_addr_index:
24363 case DW_OP_GNU_const_index:
24364 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24365 &bytes_read);
24366 i += bytes_read;
24367 break;
24368
24369 default:
24370 {
24371 const char *name = get_DW_OP_name (op);
24372
24373 if (name)
24374 complaint (_("unsupported stack op: '%s'"),
24375 name);
24376 else
24377 complaint (_("unsupported stack op: '%02x'"),
24378 op);
24379 }
24380
24381 return (stack[stacki]);
24382 }
24383
24384 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24385 outside of the allocated space. Also enforce minimum>0. */
24386 if (stacki >= ARRAY_SIZE (stack) - 1)
24387 {
24388 complaint (_("location description stack overflow"));
24389 return 0;
24390 }
24391
24392 if (stacki <= 0)
24393 {
24394 complaint (_("location description stack underflow"));
24395 return 0;
24396 }
24397 }
24398 return (stack[stacki]);
24399 }
24400
24401 /* memory allocation interface */
24402
24403 static struct dwarf_block *
24404 dwarf_alloc_block (struct dwarf2_cu *cu)
24405 {
24406 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24407 }
24408
24409 static struct die_info *
24410 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24411 {
24412 struct die_info *die;
24413 size_t size = sizeof (struct die_info);
24414
24415 if (num_attrs > 1)
24416 size += (num_attrs - 1) * sizeof (struct attribute);
24417
24418 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24419 memset (die, 0, sizeof (struct die_info));
24420 return (die);
24421 }
24422
24423 \f
24424 /* Macro support. */
24425
24426 /* Return file name relative to the compilation directory of file number I in
24427 *LH's file name table. The result is allocated using xmalloc; the caller is
24428 responsible for freeing it. */
24429
24430 static char *
24431 file_file_name (int file, struct line_header *lh)
24432 {
24433 /* Is the file number a valid index into the line header's file name
24434 table? Remember that file numbers start with one, not zero. */
24435 if (lh->is_valid_file_index (file))
24436 {
24437 const file_entry *fe = lh->file_name_at (file);
24438
24439 if (!IS_ABSOLUTE_PATH (fe->name))
24440 {
24441 const char *dir = fe->include_dir (lh);
24442 if (dir != NULL)
24443 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
24444 }
24445 return xstrdup (fe->name);
24446 }
24447 else
24448 {
24449 /* The compiler produced a bogus file number. We can at least
24450 record the macro definitions made in the file, even if we
24451 won't be able to find the file by name. */
24452 char fake_name[80];
24453
24454 xsnprintf (fake_name, sizeof (fake_name),
24455 "<bad macro file number %d>", file);
24456
24457 complaint (_("bad file number in macro information (%d)"),
24458 file);
24459
24460 return xstrdup (fake_name);
24461 }
24462 }
24463
24464 /* Return the full name of file number I in *LH's file name table.
24465 Use COMP_DIR as the name of the current directory of the
24466 compilation. The result is allocated using xmalloc; the caller is
24467 responsible for freeing it. */
24468 static char *
24469 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24470 {
24471 /* Is the file number a valid index into the line header's file name
24472 table? Remember that file numbers start with one, not zero. */
24473 if (lh->is_valid_file_index (file))
24474 {
24475 char *relative = file_file_name (file, lh);
24476
24477 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24478 return relative;
24479 return reconcat (relative, comp_dir, SLASH_STRING,
24480 relative, (char *) NULL);
24481 }
24482 else
24483 return file_file_name (file, lh);
24484 }
24485
24486
24487 static struct macro_source_file *
24488 macro_start_file (struct dwarf2_cu *cu,
24489 int file, int line,
24490 struct macro_source_file *current_file,
24491 struct line_header *lh)
24492 {
24493 /* File name relative to the compilation directory of this source file. */
24494 char *file_name = file_file_name (file, lh);
24495
24496 if (! current_file)
24497 {
24498 /* Note: We don't create a macro table for this compilation unit
24499 at all until we actually get a filename. */
24500 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
24501
24502 /* If we have no current file, then this must be the start_file
24503 directive for the compilation unit's main source file. */
24504 current_file = macro_set_main (macro_table, file_name);
24505 macro_define_special (macro_table);
24506 }
24507 else
24508 current_file = macro_include (current_file, line, file_name);
24509
24510 xfree (file_name);
24511
24512 return current_file;
24513 }
24514
24515 static const char *
24516 consume_improper_spaces (const char *p, const char *body)
24517 {
24518 if (*p == ' ')
24519 {
24520 complaint (_("macro definition contains spaces "
24521 "in formal argument list:\n`%s'"),
24522 body);
24523
24524 while (*p == ' ')
24525 p++;
24526 }
24527
24528 return p;
24529 }
24530
24531
24532 static void
24533 parse_macro_definition (struct macro_source_file *file, int line,
24534 const char *body)
24535 {
24536 const char *p;
24537
24538 /* The body string takes one of two forms. For object-like macro
24539 definitions, it should be:
24540
24541 <macro name> " " <definition>
24542
24543 For function-like macro definitions, it should be:
24544
24545 <macro name> "() " <definition>
24546 or
24547 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24548
24549 Spaces may appear only where explicitly indicated, and in the
24550 <definition>.
24551
24552 The Dwarf 2 spec says that an object-like macro's name is always
24553 followed by a space, but versions of GCC around March 2002 omit
24554 the space when the macro's definition is the empty string.
24555
24556 The Dwarf 2 spec says that there should be no spaces between the
24557 formal arguments in a function-like macro's formal argument list,
24558 but versions of GCC around March 2002 include spaces after the
24559 commas. */
24560
24561
24562 /* Find the extent of the macro name. The macro name is terminated
24563 by either a space or null character (for an object-like macro) or
24564 an opening paren (for a function-like macro). */
24565 for (p = body; *p; p++)
24566 if (*p == ' ' || *p == '(')
24567 break;
24568
24569 if (*p == ' ' || *p == '\0')
24570 {
24571 /* It's an object-like macro. */
24572 int name_len = p - body;
24573 std::string name (body, name_len);
24574 const char *replacement;
24575
24576 if (*p == ' ')
24577 replacement = body + name_len + 1;
24578 else
24579 {
24580 dwarf2_macro_malformed_definition_complaint (body);
24581 replacement = body + name_len;
24582 }
24583
24584 macro_define_object (file, line, name.c_str (), replacement);
24585 }
24586 else if (*p == '(')
24587 {
24588 /* It's a function-like macro. */
24589 std::string name (body, p - body);
24590 int argc = 0;
24591 int argv_size = 1;
24592 char **argv = XNEWVEC (char *, argv_size);
24593
24594 p++;
24595
24596 p = consume_improper_spaces (p, body);
24597
24598 /* Parse the formal argument list. */
24599 while (*p && *p != ')')
24600 {
24601 /* Find the extent of the current argument name. */
24602 const char *arg_start = p;
24603
24604 while (*p && *p != ',' && *p != ')' && *p != ' ')
24605 p++;
24606
24607 if (! *p || p == arg_start)
24608 dwarf2_macro_malformed_definition_complaint (body);
24609 else
24610 {
24611 /* Make sure argv has room for the new argument. */
24612 if (argc >= argv_size)
24613 {
24614 argv_size *= 2;
24615 argv = XRESIZEVEC (char *, argv, argv_size);
24616 }
24617
24618 argv[argc++] = savestring (arg_start, p - arg_start);
24619 }
24620
24621 p = consume_improper_spaces (p, body);
24622
24623 /* Consume the comma, if present. */
24624 if (*p == ',')
24625 {
24626 p++;
24627
24628 p = consume_improper_spaces (p, body);
24629 }
24630 }
24631
24632 if (*p == ')')
24633 {
24634 p++;
24635
24636 if (*p == ' ')
24637 /* Perfectly formed definition, no complaints. */
24638 macro_define_function (file, line, name.c_str (),
24639 argc, (const char **) argv,
24640 p + 1);
24641 else if (*p == '\0')
24642 {
24643 /* Complain, but do define it. */
24644 dwarf2_macro_malformed_definition_complaint (body);
24645 macro_define_function (file, line, name.c_str (),
24646 argc, (const char **) argv,
24647 p);
24648 }
24649 else
24650 /* Just complain. */
24651 dwarf2_macro_malformed_definition_complaint (body);
24652 }
24653 else
24654 /* Just complain. */
24655 dwarf2_macro_malformed_definition_complaint (body);
24656
24657 {
24658 int i;
24659
24660 for (i = 0; i < argc; i++)
24661 xfree (argv[i]);
24662 }
24663 xfree (argv);
24664 }
24665 else
24666 dwarf2_macro_malformed_definition_complaint (body);
24667 }
24668
24669 /* Skip some bytes from BYTES according to the form given in FORM.
24670 Returns the new pointer. */
24671
24672 static const gdb_byte *
24673 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24674 enum dwarf_form form,
24675 unsigned int offset_size,
24676 struct dwarf2_section_info *section)
24677 {
24678 unsigned int bytes_read;
24679
24680 switch (form)
24681 {
24682 case DW_FORM_data1:
24683 case DW_FORM_flag:
24684 ++bytes;
24685 break;
24686
24687 case DW_FORM_data2:
24688 bytes += 2;
24689 break;
24690
24691 case DW_FORM_data4:
24692 bytes += 4;
24693 break;
24694
24695 case DW_FORM_data8:
24696 bytes += 8;
24697 break;
24698
24699 case DW_FORM_data16:
24700 bytes += 16;
24701 break;
24702
24703 case DW_FORM_string:
24704 read_direct_string (abfd, bytes, &bytes_read);
24705 bytes += bytes_read;
24706 break;
24707
24708 case DW_FORM_sec_offset:
24709 case DW_FORM_strp:
24710 case DW_FORM_GNU_strp_alt:
24711 bytes += offset_size;
24712 break;
24713
24714 case DW_FORM_block:
24715 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24716 bytes += bytes_read;
24717 break;
24718
24719 case DW_FORM_block1:
24720 bytes += 1 + read_1_byte (abfd, bytes);
24721 break;
24722 case DW_FORM_block2:
24723 bytes += 2 + read_2_bytes (abfd, bytes);
24724 break;
24725 case DW_FORM_block4:
24726 bytes += 4 + read_4_bytes (abfd, bytes);
24727 break;
24728
24729 case DW_FORM_addrx:
24730 case DW_FORM_sdata:
24731 case DW_FORM_strx:
24732 case DW_FORM_udata:
24733 case DW_FORM_GNU_addr_index:
24734 case DW_FORM_GNU_str_index:
24735 bytes = gdb_skip_leb128 (bytes, buffer_end);
24736 if (bytes == NULL)
24737 {
24738 dwarf2_section_buffer_overflow_complaint (section);
24739 return NULL;
24740 }
24741 break;
24742
24743 case DW_FORM_implicit_const:
24744 break;
24745
24746 default:
24747 {
24748 complaint (_("invalid form 0x%x in `%s'"),
24749 form, get_section_name (section));
24750 return NULL;
24751 }
24752 }
24753
24754 return bytes;
24755 }
24756
24757 /* A helper for dwarf_decode_macros that handles skipping an unknown
24758 opcode. Returns an updated pointer to the macro data buffer; or,
24759 on error, issues a complaint and returns NULL. */
24760
24761 static const gdb_byte *
24762 skip_unknown_opcode (unsigned int opcode,
24763 const gdb_byte **opcode_definitions,
24764 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24765 bfd *abfd,
24766 unsigned int offset_size,
24767 struct dwarf2_section_info *section)
24768 {
24769 unsigned int bytes_read, i;
24770 unsigned long arg;
24771 const gdb_byte *defn;
24772
24773 if (opcode_definitions[opcode] == NULL)
24774 {
24775 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24776 opcode);
24777 return NULL;
24778 }
24779
24780 defn = opcode_definitions[opcode];
24781 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24782 defn += bytes_read;
24783
24784 for (i = 0; i < arg; ++i)
24785 {
24786 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24787 (enum dwarf_form) defn[i], offset_size,
24788 section);
24789 if (mac_ptr == NULL)
24790 {
24791 /* skip_form_bytes already issued the complaint. */
24792 return NULL;
24793 }
24794 }
24795
24796 return mac_ptr;
24797 }
24798
24799 /* A helper function which parses the header of a macro section.
24800 If the macro section is the extended (for now called "GNU") type,
24801 then this updates *OFFSET_SIZE. Returns a pointer to just after
24802 the header, or issues a complaint and returns NULL on error. */
24803
24804 static const gdb_byte *
24805 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24806 bfd *abfd,
24807 const gdb_byte *mac_ptr,
24808 unsigned int *offset_size,
24809 int section_is_gnu)
24810 {
24811 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24812
24813 if (section_is_gnu)
24814 {
24815 unsigned int version, flags;
24816
24817 version = read_2_bytes (abfd, mac_ptr);
24818 if (version != 4 && version != 5)
24819 {
24820 complaint (_("unrecognized version `%d' in .debug_macro section"),
24821 version);
24822 return NULL;
24823 }
24824 mac_ptr += 2;
24825
24826 flags = read_1_byte (abfd, mac_ptr);
24827 ++mac_ptr;
24828 *offset_size = (flags & 1) ? 8 : 4;
24829
24830 if ((flags & 2) != 0)
24831 /* We don't need the line table offset. */
24832 mac_ptr += *offset_size;
24833
24834 /* Vendor opcode descriptions. */
24835 if ((flags & 4) != 0)
24836 {
24837 unsigned int i, count;
24838
24839 count = read_1_byte (abfd, mac_ptr);
24840 ++mac_ptr;
24841 for (i = 0; i < count; ++i)
24842 {
24843 unsigned int opcode, bytes_read;
24844 unsigned long arg;
24845
24846 opcode = read_1_byte (abfd, mac_ptr);
24847 ++mac_ptr;
24848 opcode_definitions[opcode] = mac_ptr;
24849 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24850 mac_ptr += bytes_read;
24851 mac_ptr += arg;
24852 }
24853 }
24854 }
24855
24856 return mac_ptr;
24857 }
24858
24859 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24860 including DW_MACRO_import. */
24861
24862 static void
24863 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24864 bfd *abfd,
24865 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24866 struct macro_source_file *current_file,
24867 struct line_header *lh,
24868 struct dwarf2_section_info *section,
24869 int section_is_gnu, int section_is_dwz,
24870 unsigned int offset_size,
24871 htab_t include_hash)
24872 {
24873 struct dwarf2_per_objfile *dwarf2_per_objfile
24874 = cu->per_cu->dwarf2_per_objfile;
24875 struct objfile *objfile = dwarf2_per_objfile->objfile;
24876 enum dwarf_macro_record_type macinfo_type;
24877 int at_commandline;
24878 const gdb_byte *opcode_definitions[256];
24879
24880 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24881 &offset_size, section_is_gnu);
24882 if (mac_ptr == NULL)
24883 {
24884 /* We already issued a complaint. */
24885 return;
24886 }
24887
24888 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24889 GDB is still reading the definitions from command line. First
24890 DW_MACINFO_start_file will need to be ignored as it was already executed
24891 to create CURRENT_FILE for the main source holding also the command line
24892 definitions. On first met DW_MACINFO_start_file this flag is reset to
24893 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24894
24895 at_commandline = 1;
24896
24897 do
24898 {
24899 /* Do we at least have room for a macinfo type byte? */
24900 if (mac_ptr >= mac_end)
24901 {
24902 dwarf2_section_buffer_overflow_complaint (section);
24903 break;
24904 }
24905
24906 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24907 mac_ptr++;
24908
24909 /* Note that we rely on the fact that the corresponding GNU and
24910 DWARF constants are the same. */
24911 DIAGNOSTIC_PUSH
24912 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24913 switch (macinfo_type)
24914 {
24915 /* A zero macinfo type indicates the end of the macro
24916 information. */
24917 case 0:
24918 break;
24919
24920 case DW_MACRO_define:
24921 case DW_MACRO_undef:
24922 case DW_MACRO_define_strp:
24923 case DW_MACRO_undef_strp:
24924 case DW_MACRO_define_sup:
24925 case DW_MACRO_undef_sup:
24926 {
24927 unsigned int bytes_read;
24928 int line;
24929 const char *body;
24930 int is_define;
24931
24932 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24933 mac_ptr += bytes_read;
24934
24935 if (macinfo_type == DW_MACRO_define
24936 || macinfo_type == DW_MACRO_undef)
24937 {
24938 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24939 mac_ptr += bytes_read;
24940 }
24941 else
24942 {
24943 LONGEST str_offset;
24944
24945 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24946 mac_ptr += offset_size;
24947
24948 if (macinfo_type == DW_MACRO_define_sup
24949 || macinfo_type == DW_MACRO_undef_sup
24950 || section_is_dwz)
24951 {
24952 struct dwz_file *dwz
24953 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24954
24955 body = read_indirect_string_from_dwz (objfile,
24956 dwz, str_offset);
24957 }
24958 else
24959 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24960 abfd, str_offset);
24961 }
24962
24963 is_define = (macinfo_type == DW_MACRO_define
24964 || macinfo_type == DW_MACRO_define_strp
24965 || macinfo_type == DW_MACRO_define_sup);
24966 if (! current_file)
24967 {
24968 /* DWARF violation as no main source is present. */
24969 complaint (_("debug info with no main source gives macro %s "
24970 "on line %d: %s"),
24971 is_define ? _("definition") : _("undefinition"),
24972 line, body);
24973 break;
24974 }
24975 if ((line == 0 && !at_commandline)
24976 || (line != 0 && at_commandline))
24977 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
24978 at_commandline ? _("command-line") : _("in-file"),
24979 is_define ? _("definition") : _("undefinition"),
24980 line == 0 ? _("zero") : _("non-zero"), line, body);
24981
24982 if (body == NULL)
24983 {
24984 /* Fedora's rpm-build's "debugedit" binary
24985 corrupted .debug_macro sections.
24986
24987 For more info, see
24988 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24989 complaint (_("debug info gives %s invalid macro %s "
24990 "without body (corrupted?) at line %d "
24991 "on file %s"),
24992 at_commandline ? _("command-line") : _("in-file"),
24993 is_define ? _("definition") : _("undefinition"),
24994 line, current_file->filename);
24995 }
24996 else if (is_define)
24997 parse_macro_definition (current_file, line, body);
24998 else
24999 {
25000 gdb_assert (macinfo_type == DW_MACRO_undef
25001 || macinfo_type == DW_MACRO_undef_strp
25002 || macinfo_type == DW_MACRO_undef_sup);
25003 macro_undef (current_file, line, body);
25004 }
25005 }
25006 break;
25007
25008 case DW_MACRO_start_file:
25009 {
25010 unsigned int bytes_read;
25011 int line, file;
25012
25013 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25014 mac_ptr += bytes_read;
25015 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25016 mac_ptr += bytes_read;
25017
25018 if ((line == 0 && !at_commandline)
25019 || (line != 0 && at_commandline))
25020 complaint (_("debug info gives source %d included "
25021 "from %s at %s line %d"),
25022 file, at_commandline ? _("command-line") : _("file"),
25023 line == 0 ? _("zero") : _("non-zero"), line);
25024
25025 if (at_commandline)
25026 {
25027 /* This DW_MACRO_start_file was executed in the
25028 pass one. */
25029 at_commandline = 0;
25030 }
25031 else
25032 current_file = macro_start_file (cu, file, line, current_file,
25033 lh);
25034 }
25035 break;
25036
25037 case DW_MACRO_end_file:
25038 if (! current_file)
25039 complaint (_("macro debug info has an unmatched "
25040 "`close_file' directive"));
25041 else
25042 {
25043 current_file = current_file->included_by;
25044 if (! current_file)
25045 {
25046 enum dwarf_macro_record_type next_type;
25047
25048 /* GCC circa March 2002 doesn't produce the zero
25049 type byte marking the end of the compilation
25050 unit. Complain if it's not there, but exit no
25051 matter what. */
25052
25053 /* Do we at least have room for a macinfo type byte? */
25054 if (mac_ptr >= mac_end)
25055 {
25056 dwarf2_section_buffer_overflow_complaint (section);
25057 return;
25058 }
25059
25060 /* We don't increment mac_ptr here, so this is just
25061 a look-ahead. */
25062 next_type
25063 = (enum dwarf_macro_record_type) read_1_byte (abfd,
25064 mac_ptr);
25065 if (next_type != 0)
25066 complaint (_("no terminating 0-type entry for "
25067 "macros in `.debug_macinfo' section"));
25068
25069 return;
25070 }
25071 }
25072 break;
25073
25074 case DW_MACRO_import:
25075 case DW_MACRO_import_sup:
25076 {
25077 LONGEST offset;
25078 void **slot;
25079 bfd *include_bfd = abfd;
25080 struct dwarf2_section_info *include_section = section;
25081 const gdb_byte *include_mac_end = mac_end;
25082 int is_dwz = section_is_dwz;
25083 const gdb_byte *new_mac_ptr;
25084
25085 offset = read_offset_1 (abfd, mac_ptr, offset_size);
25086 mac_ptr += offset_size;
25087
25088 if (macinfo_type == DW_MACRO_import_sup)
25089 {
25090 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
25091
25092 dwarf2_read_section (objfile, &dwz->macro);
25093
25094 include_section = &dwz->macro;
25095 include_bfd = get_section_bfd_owner (include_section);
25096 include_mac_end = dwz->macro.buffer + dwz->macro.size;
25097 is_dwz = 1;
25098 }
25099
25100 new_mac_ptr = include_section->buffer + offset;
25101 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
25102
25103 if (*slot != NULL)
25104 {
25105 /* This has actually happened; see
25106 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
25107 complaint (_("recursive DW_MACRO_import in "
25108 ".debug_macro section"));
25109 }
25110 else
25111 {
25112 *slot = (void *) new_mac_ptr;
25113
25114 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
25115 include_mac_end, current_file, lh,
25116 section, section_is_gnu, is_dwz,
25117 offset_size, include_hash);
25118
25119 htab_remove_elt (include_hash, (void *) new_mac_ptr);
25120 }
25121 }
25122 break;
25123
25124 case DW_MACINFO_vendor_ext:
25125 if (!section_is_gnu)
25126 {
25127 unsigned int bytes_read;
25128
25129 /* This reads the constant, but since we don't recognize
25130 any vendor extensions, we ignore it. */
25131 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25132 mac_ptr += bytes_read;
25133 read_direct_string (abfd, mac_ptr, &bytes_read);
25134 mac_ptr += bytes_read;
25135
25136 /* We don't recognize any vendor extensions. */
25137 break;
25138 }
25139 /* FALLTHROUGH */
25140
25141 default:
25142 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25143 mac_ptr, mac_end, abfd, offset_size,
25144 section);
25145 if (mac_ptr == NULL)
25146 return;
25147 break;
25148 }
25149 DIAGNOSTIC_POP
25150 } while (macinfo_type != 0);
25151 }
25152
25153 static void
25154 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
25155 int section_is_gnu)
25156 {
25157 struct dwarf2_per_objfile *dwarf2_per_objfile
25158 = cu->per_cu->dwarf2_per_objfile;
25159 struct objfile *objfile = dwarf2_per_objfile->objfile;
25160 struct line_header *lh = cu->line_header;
25161 bfd *abfd;
25162 const gdb_byte *mac_ptr, *mac_end;
25163 struct macro_source_file *current_file = 0;
25164 enum dwarf_macro_record_type macinfo_type;
25165 unsigned int offset_size = cu->header.offset_size;
25166 const gdb_byte *opcode_definitions[256];
25167 void **slot;
25168 struct dwarf2_section_info *section;
25169 const char *section_name;
25170
25171 if (cu->dwo_unit != NULL)
25172 {
25173 if (section_is_gnu)
25174 {
25175 section = &cu->dwo_unit->dwo_file->sections.macro;
25176 section_name = ".debug_macro.dwo";
25177 }
25178 else
25179 {
25180 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25181 section_name = ".debug_macinfo.dwo";
25182 }
25183 }
25184 else
25185 {
25186 if (section_is_gnu)
25187 {
25188 section = &dwarf2_per_objfile->macro;
25189 section_name = ".debug_macro";
25190 }
25191 else
25192 {
25193 section = &dwarf2_per_objfile->macinfo;
25194 section_name = ".debug_macinfo";
25195 }
25196 }
25197
25198 dwarf2_read_section (objfile, section);
25199 if (section->buffer == NULL)
25200 {
25201 complaint (_("missing %s section"), section_name);
25202 return;
25203 }
25204 abfd = get_section_bfd_owner (section);
25205
25206 /* First pass: Find the name of the base filename.
25207 This filename is needed in order to process all macros whose definition
25208 (or undefinition) comes from the command line. These macros are defined
25209 before the first DW_MACINFO_start_file entry, and yet still need to be
25210 associated to the base file.
25211
25212 To determine the base file name, we scan the macro definitions until we
25213 reach the first DW_MACINFO_start_file entry. We then initialize
25214 CURRENT_FILE accordingly so that any macro definition found before the
25215 first DW_MACINFO_start_file can still be associated to the base file. */
25216
25217 mac_ptr = section->buffer + offset;
25218 mac_end = section->buffer + section->size;
25219
25220 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25221 &offset_size, section_is_gnu);
25222 if (mac_ptr == NULL)
25223 {
25224 /* We already issued a complaint. */
25225 return;
25226 }
25227
25228 do
25229 {
25230 /* Do we at least have room for a macinfo type byte? */
25231 if (mac_ptr >= mac_end)
25232 {
25233 /* Complaint is printed during the second pass as GDB will probably
25234 stop the first pass earlier upon finding
25235 DW_MACINFO_start_file. */
25236 break;
25237 }
25238
25239 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
25240 mac_ptr++;
25241
25242 /* Note that we rely on the fact that the corresponding GNU and
25243 DWARF constants are the same. */
25244 DIAGNOSTIC_PUSH
25245 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
25246 switch (macinfo_type)
25247 {
25248 /* A zero macinfo type indicates the end of the macro
25249 information. */
25250 case 0:
25251 break;
25252
25253 case DW_MACRO_define:
25254 case DW_MACRO_undef:
25255 /* Only skip the data by MAC_PTR. */
25256 {
25257 unsigned int bytes_read;
25258
25259 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25260 mac_ptr += bytes_read;
25261 read_direct_string (abfd, mac_ptr, &bytes_read);
25262 mac_ptr += bytes_read;
25263 }
25264 break;
25265
25266 case DW_MACRO_start_file:
25267 {
25268 unsigned int bytes_read;
25269 int line, file;
25270
25271 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25272 mac_ptr += bytes_read;
25273 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25274 mac_ptr += bytes_read;
25275
25276 current_file = macro_start_file (cu, file, line, current_file, lh);
25277 }
25278 break;
25279
25280 case DW_MACRO_end_file:
25281 /* No data to skip by MAC_PTR. */
25282 break;
25283
25284 case DW_MACRO_define_strp:
25285 case DW_MACRO_undef_strp:
25286 case DW_MACRO_define_sup:
25287 case DW_MACRO_undef_sup:
25288 {
25289 unsigned int bytes_read;
25290
25291 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25292 mac_ptr += bytes_read;
25293 mac_ptr += offset_size;
25294 }
25295 break;
25296
25297 case DW_MACRO_import:
25298 case DW_MACRO_import_sup:
25299 /* Note that, according to the spec, a transparent include
25300 chain cannot call DW_MACRO_start_file. So, we can just
25301 skip this opcode. */
25302 mac_ptr += offset_size;
25303 break;
25304
25305 case DW_MACINFO_vendor_ext:
25306 /* Only skip the data by MAC_PTR. */
25307 if (!section_is_gnu)
25308 {
25309 unsigned int bytes_read;
25310
25311 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25312 mac_ptr += bytes_read;
25313 read_direct_string (abfd, mac_ptr, &bytes_read);
25314 mac_ptr += bytes_read;
25315 }
25316 /* FALLTHROUGH */
25317
25318 default:
25319 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25320 mac_ptr, mac_end, abfd, offset_size,
25321 section);
25322 if (mac_ptr == NULL)
25323 return;
25324 break;
25325 }
25326 DIAGNOSTIC_POP
25327 } while (macinfo_type != 0 && current_file == NULL);
25328
25329 /* Second pass: Process all entries.
25330
25331 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25332 command-line macro definitions/undefinitions. This flag is unset when we
25333 reach the first DW_MACINFO_start_file entry. */
25334
25335 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25336 htab_eq_pointer,
25337 NULL, xcalloc, xfree));
25338 mac_ptr = section->buffer + offset;
25339 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
25340 *slot = (void *) mac_ptr;
25341 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
25342 current_file, lh, section,
25343 section_is_gnu, 0, offset_size,
25344 include_hash.get ());
25345 }
25346
25347 /* Check if the attribute's form is a DW_FORM_block*
25348 if so return true else false. */
25349
25350 static int
25351 attr_form_is_block (const struct attribute *attr)
25352 {
25353 return (attr == NULL ? 0 :
25354 attr->form == DW_FORM_block1
25355 || attr->form == DW_FORM_block2
25356 || attr->form == DW_FORM_block4
25357 || attr->form == DW_FORM_block
25358 || attr->form == DW_FORM_exprloc);
25359 }
25360
25361 /* Return non-zero if ATTR's value is a section offset --- classes
25362 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25363 You may use DW_UNSND (attr) to retrieve such offsets.
25364
25365 Section 7.5.4, "Attribute Encodings", explains that no attribute
25366 may have a value that belongs to more than one of these classes; it
25367 would be ambiguous if we did, because we use the same forms for all
25368 of them. */
25369
25370 static int
25371 attr_form_is_section_offset (const struct attribute *attr)
25372 {
25373 return (attr->form == DW_FORM_data4
25374 || attr->form == DW_FORM_data8
25375 || attr->form == DW_FORM_sec_offset);
25376 }
25377
25378 /* Return non-zero if ATTR's value falls in the 'constant' class, or
25379 zero otherwise. When this function returns true, you can apply
25380 dwarf2_get_attr_constant_value to it.
25381
25382 However, note that for some attributes you must check
25383 attr_form_is_section_offset before using this test. DW_FORM_data4
25384 and DW_FORM_data8 are members of both the constant class, and of
25385 the classes that contain offsets into other debug sections
25386 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25387 that, if an attribute's can be either a constant or one of the
25388 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25389 taken as section offsets, not constants.
25390
25391 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25392 cannot handle that. */
25393
25394 static int
25395 attr_form_is_constant (const struct attribute *attr)
25396 {
25397 switch (attr->form)
25398 {
25399 case DW_FORM_sdata:
25400 case DW_FORM_udata:
25401 case DW_FORM_data1:
25402 case DW_FORM_data2:
25403 case DW_FORM_data4:
25404 case DW_FORM_data8:
25405 case DW_FORM_implicit_const:
25406 return 1;
25407 default:
25408 return 0;
25409 }
25410 }
25411
25412
25413 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25414 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25415
25416 static int
25417 attr_form_is_ref (const struct attribute *attr)
25418 {
25419 switch (attr->form)
25420 {
25421 case DW_FORM_ref_addr:
25422 case DW_FORM_ref1:
25423 case DW_FORM_ref2:
25424 case DW_FORM_ref4:
25425 case DW_FORM_ref8:
25426 case DW_FORM_ref_udata:
25427 case DW_FORM_GNU_ref_alt:
25428 return 1;
25429 default:
25430 return 0;
25431 }
25432 }
25433
25434 /* Return the .debug_loc section to use for CU.
25435 For DWO files use .debug_loc.dwo. */
25436
25437 static struct dwarf2_section_info *
25438 cu_debug_loc_section (struct dwarf2_cu *cu)
25439 {
25440 struct dwarf2_per_objfile *dwarf2_per_objfile
25441 = cu->per_cu->dwarf2_per_objfile;
25442
25443 if (cu->dwo_unit)
25444 {
25445 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25446
25447 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25448 }
25449 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25450 : &dwarf2_per_objfile->loc);
25451 }
25452
25453 /* A helper function that fills in a dwarf2_loclist_baton. */
25454
25455 static void
25456 fill_in_loclist_baton (struct dwarf2_cu *cu,
25457 struct dwarf2_loclist_baton *baton,
25458 const struct attribute *attr)
25459 {
25460 struct dwarf2_per_objfile *dwarf2_per_objfile
25461 = cu->per_cu->dwarf2_per_objfile;
25462 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25463
25464 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25465
25466 baton->per_cu = cu->per_cu;
25467 gdb_assert (baton->per_cu);
25468 /* We don't know how long the location list is, but make sure we
25469 don't run off the edge of the section. */
25470 baton->size = section->size - DW_UNSND (attr);
25471 baton->data = section->buffer + DW_UNSND (attr);
25472 baton->base_address = cu->base_address;
25473 baton->from_dwo = cu->dwo_unit != NULL;
25474 }
25475
25476 static void
25477 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25478 struct dwarf2_cu *cu, int is_block)
25479 {
25480 struct dwarf2_per_objfile *dwarf2_per_objfile
25481 = cu->per_cu->dwarf2_per_objfile;
25482 struct objfile *objfile = dwarf2_per_objfile->objfile;
25483 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25484
25485 if (attr_form_is_section_offset (attr)
25486 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25487 the section. If so, fall through to the complaint in the
25488 other branch. */
25489 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25490 {
25491 struct dwarf2_loclist_baton *baton;
25492
25493 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25494
25495 fill_in_loclist_baton (cu, baton, attr);
25496
25497 if (cu->base_known == 0)
25498 complaint (_("Location list used without "
25499 "specifying the CU base address."));
25500
25501 SYMBOL_ACLASS_INDEX (sym) = (is_block
25502 ? dwarf2_loclist_block_index
25503 : dwarf2_loclist_index);
25504 SYMBOL_LOCATION_BATON (sym) = baton;
25505 }
25506 else
25507 {
25508 struct dwarf2_locexpr_baton *baton;
25509
25510 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25511 baton->per_cu = cu->per_cu;
25512 gdb_assert (baton->per_cu);
25513
25514 if (attr_form_is_block (attr))
25515 {
25516 /* Note that we're just copying the block's data pointer
25517 here, not the actual data. We're still pointing into the
25518 info_buffer for SYM's objfile; right now we never release
25519 that buffer, but when we do clean up properly this may
25520 need to change. */
25521 baton->size = DW_BLOCK (attr)->size;
25522 baton->data = DW_BLOCK (attr)->data;
25523 }
25524 else
25525 {
25526 dwarf2_invalid_attrib_class_complaint ("location description",
25527 sym->natural_name ());
25528 baton->size = 0;
25529 }
25530
25531 SYMBOL_ACLASS_INDEX (sym) = (is_block
25532 ? dwarf2_locexpr_block_index
25533 : dwarf2_locexpr_index);
25534 SYMBOL_LOCATION_BATON (sym) = baton;
25535 }
25536 }
25537
25538 /* Return the OBJFILE associated with the compilation unit CU. If CU
25539 came from a separate debuginfo file, then the master objfile is
25540 returned. */
25541
25542 struct objfile *
25543 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25544 {
25545 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25546
25547 /* Return the master objfile, so that we can report and look up the
25548 correct file containing this variable. */
25549 if (objfile->separate_debug_objfile_backlink)
25550 objfile = objfile->separate_debug_objfile_backlink;
25551
25552 return objfile;
25553 }
25554
25555 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25556 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25557 CU_HEADERP first. */
25558
25559 static const struct comp_unit_head *
25560 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25561 struct dwarf2_per_cu_data *per_cu)
25562 {
25563 const gdb_byte *info_ptr;
25564
25565 if (per_cu->cu)
25566 return &per_cu->cu->header;
25567
25568 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25569
25570 memset (cu_headerp, 0, sizeof (*cu_headerp));
25571 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25572 rcuh_kind::COMPILE);
25573
25574 return cu_headerp;
25575 }
25576
25577 /* Return the address size given in the compilation unit header for CU. */
25578
25579 int
25580 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25581 {
25582 struct comp_unit_head cu_header_local;
25583 const struct comp_unit_head *cu_headerp;
25584
25585 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25586
25587 return cu_headerp->addr_size;
25588 }
25589
25590 /* Return the offset size given in the compilation unit header for CU. */
25591
25592 int
25593 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25594 {
25595 struct comp_unit_head cu_header_local;
25596 const struct comp_unit_head *cu_headerp;
25597
25598 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25599
25600 return cu_headerp->offset_size;
25601 }
25602
25603 /* See its dwarf2loc.h declaration. */
25604
25605 int
25606 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25607 {
25608 struct comp_unit_head cu_header_local;
25609 const struct comp_unit_head *cu_headerp;
25610
25611 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25612
25613 if (cu_headerp->version == 2)
25614 return cu_headerp->addr_size;
25615 else
25616 return cu_headerp->offset_size;
25617 }
25618
25619 /* Return the text offset of the CU. The returned offset comes from
25620 this CU's objfile. If this objfile came from a separate debuginfo
25621 file, then the offset may be different from the corresponding
25622 offset in the parent objfile. */
25623
25624 CORE_ADDR
25625 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25626 {
25627 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25628
25629 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25630 }
25631
25632 /* Return a type that is a generic pointer type, the size of which matches
25633 the address size given in the compilation unit header for PER_CU. */
25634 static struct type *
25635 dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25636 {
25637 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25638 struct type *void_type = objfile_type (objfile)->builtin_void;
25639 struct type *addr_type = lookup_pointer_type (void_type);
25640 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25641
25642 if (TYPE_LENGTH (addr_type) == addr_size)
25643 return addr_type;
25644
25645 addr_type
25646 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25647 return addr_type;
25648 }
25649
25650 /* Return DWARF version number of PER_CU. */
25651
25652 short
25653 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25654 {
25655 return per_cu->dwarf_version;
25656 }
25657
25658 /* Locate the .debug_info compilation unit from CU's objfile which contains
25659 the DIE at OFFSET. Raises an error on failure. */
25660
25661 static struct dwarf2_per_cu_data *
25662 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25663 unsigned int offset_in_dwz,
25664 struct dwarf2_per_objfile *dwarf2_per_objfile)
25665 {
25666 struct dwarf2_per_cu_data *this_cu;
25667 int low, high;
25668
25669 low = 0;
25670 high = dwarf2_per_objfile->all_comp_units.size () - 1;
25671 while (high > low)
25672 {
25673 struct dwarf2_per_cu_data *mid_cu;
25674 int mid = low + (high - low) / 2;
25675
25676 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25677 if (mid_cu->is_dwz > offset_in_dwz
25678 || (mid_cu->is_dwz == offset_in_dwz
25679 && mid_cu->sect_off + mid_cu->length >= sect_off))
25680 high = mid;
25681 else
25682 low = mid + 1;
25683 }
25684 gdb_assert (low == high);
25685 this_cu = dwarf2_per_objfile->all_comp_units[low];
25686 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25687 {
25688 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25689 error (_("Dwarf Error: could not find partial DIE containing "
25690 "offset %s [in module %s]"),
25691 sect_offset_str (sect_off),
25692 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25693
25694 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25695 <= sect_off);
25696 return dwarf2_per_objfile->all_comp_units[low-1];
25697 }
25698 else
25699 {
25700 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25701 && sect_off >= this_cu->sect_off + this_cu->length)
25702 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25703 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25704 return this_cu;
25705 }
25706 }
25707
25708 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25709
25710 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25711 : per_cu (per_cu_),
25712 mark (false),
25713 has_loclist (false),
25714 checked_producer (false),
25715 producer_is_gxx_lt_4_6 (false),
25716 producer_is_gcc_lt_4_3 (false),
25717 producer_is_icc (false),
25718 producer_is_icc_lt_14 (false),
25719 producer_is_codewarrior (false),
25720 processing_has_namespace_info (false)
25721 {
25722 per_cu->cu = this;
25723 }
25724
25725 /* Destroy a dwarf2_cu. */
25726
25727 dwarf2_cu::~dwarf2_cu ()
25728 {
25729 per_cu->cu = NULL;
25730 }
25731
25732 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25733
25734 static void
25735 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25736 enum language pretend_language)
25737 {
25738 struct attribute *attr;
25739
25740 /* Set the language we're debugging. */
25741 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25742 if (attr != nullptr)
25743 set_cu_language (DW_UNSND (attr), cu);
25744 else
25745 {
25746 cu->language = pretend_language;
25747 cu->language_defn = language_def (cu->language);
25748 }
25749
25750 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25751 }
25752
25753 /* Increase the age counter on each cached compilation unit, and free
25754 any that are too old. */
25755
25756 static void
25757 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25758 {
25759 struct dwarf2_per_cu_data *per_cu, **last_chain;
25760
25761 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25762 per_cu = dwarf2_per_objfile->read_in_chain;
25763 while (per_cu != NULL)
25764 {
25765 per_cu->cu->last_used ++;
25766 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25767 dwarf2_mark (per_cu->cu);
25768 per_cu = per_cu->cu->read_in_chain;
25769 }
25770
25771 per_cu = dwarf2_per_objfile->read_in_chain;
25772 last_chain = &dwarf2_per_objfile->read_in_chain;
25773 while (per_cu != NULL)
25774 {
25775 struct dwarf2_per_cu_data *next_cu;
25776
25777 next_cu = per_cu->cu->read_in_chain;
25778
25779 if (!per_cu->cu->mark)
25780 {
25781 delete per_cu->cu;
25782 *last_chain = next_cu;
25783 }
25784 else
25785 last_chain = &per_cu->cu->read_in_chain;
25786
25787 per_cu = next_cu;
25788 }
25789 }
25790
25791 /* Remove a single compilation unit from the cache. */
25792
25793 static void
25794 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25795 {
25796 struct dwarf2_per_cu_data *per_cu, **last_chain;
25797 struct dwarf2_per_objfile *dwarf2_per_objfile
25798 = target_per_cu->dwarf2_per_objfile;
25799
25800 per_cu = dwarf2_per_objfile->read_in_chain;
25801 last_chain = &dwarf2_per_objfile->read_in_chain;
25802 while (per_cu != NULL)
25803 {
25804 struct dwarf2_per_cu_data *next_cu;
25805
25806 next_cu = per_cu->cu->read_in_chain;
25807
25808 if (per_cu == target_per_cu)
25809 {
25810 delete per_cu->cu;
25811 per_cu->cu = NULL;
25812 *last_chain = next_cu;
25813 break;
25814 }
25815 else
25816 last_chain = &per_cu->cu->read_in_chain;
25817
25818 per_cu = next_cu;
25819 }
25820 }
25821
25822 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25823 We store these in a hash table separate from the DIEs, and preserve them
25824 when the DIEs are flushed out of cache.
25825
25826 The CU "per_cu" pointer is needed because offset alone is not enough to
25827 uniquely identify the type. A file may have multiple .debug_types sections,
25828 or the type may come from a DWO file. Furthermore, while it's more logical
25829 to use per_cu->section+offset, with Fission the section with the data is in
25830 the DWO file but we don't know that section at the point we need it.
25831 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25832 because we can enter the lookup routine, get_die_type_at_offset, from
25833 outside this file, and thus won't necessarily have PER_CU->cu.
25834 Fortunately, PER_CU is stable for the life of the objfile. */
25835
25836 struct dwarf2_per_cu_offset_and_type
25837 {
25838 const struct dwarf2_per_cu_data *per_cu;
25839 sect_offset sect_off;
25840 struct type *type;
25841 };
25842
25843 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25844
25845 static hashval_t
25846 per_cu_offset_and_type_hash (const void *item)
25847 {
25848 const struct dwarf2_per_cu_offset_and_type *ofs
25849 = (const struct dwarf2_per_cu_offset_and_type *) item;
25850
25851 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25852 }
25853
25854 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25855
25856 static int
25857 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25858 {
25859 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25860 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25861 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25862 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25863
25864 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25865 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25866 }
25867
25868 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25869 table if necessary. For convenience, return TYPE.
25870
25871 The DIEs reading must have careful ordering to:
25872 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25873 reading current DIE.
25874 * Not trying to dereference contents of still incompletely read in types
25875 while reading in other DIEs.
25876 * Enable referencing still incompletely read in types just by a pointer to
25877 the type without accessing its fields.
25878
25879 Therefore caller should follow these rules:
25880 * Try to fetch any prerequisite types we may need to build this DIE type
25881 before building the type and calling set_die_type.
25882 * After building type call set_die_type for current DIE as soon as
25883 possible before fetching more types to complete the current type.
25884 * Make the type as complete as possible before fetching more types. */
25885
25886 static struct type *
25887 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25888 {
25889 struct dwarf2_per_objfile *dwarf2_per_objfile
25890 = cu->per_cu->dwarf2_per_objfile;
25891 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25892 struct objfile *objfile = dwarf2_per_objfile->objfile;
25893 struct attribute *attr;
25894 struct dynamic_prop prop;
25895
25896 /* For Ada types, make sure that the gnat-specific data is always
25897 initialized (if not already set). There are a few types where
25898 we should not be doing so, because the type-specific area is
25899 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25900 where the type-specific area is used to store the floatformat).
25901 But this is not a problem, because the gnat-specific information
25902 is actually not needed for these types. */
25903 if (need_gnat_info (cu)
25904 && TYPE_CODE (type) != TYPE_CODE_FUNC
25905 && TYPE_CODE (type) != TYPE_CODE_FLT
25906 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25907 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25908 && TYPE_CODE (type) != TYPE_CODE_METHOD
25909 && !HAVE_GNAT_AUX_INFO (type))
25910 INIT_GNAT_SPECIFIC (type);
25911
25912 /* Read DW_AT_allocated and set in type. */
25913 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25914 if (attr_form_is_block (attr))
25915 {
25916 struct type *prop_type
25917 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25918 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25919 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25920 }
25921 else if (attr != NULL)
25922 {
25923 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25924 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25925 sect_offset_str (die->sect_off));
25926 }
25927
25928 /* Read DW_AT_associated and set in type. */
25929 attr = dwarf2_attr (die, DW_AT_associated, cu);
25930 if (attr_form_is_block (attr))
25931 {
25932 struct type *prop_type
25933 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25934 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25935 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25936 }
25937 else if (attr != NULL)
25938 {
25939 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25940 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25941 sect_offset_str (die->sect_off));
25942 }
25943
25944 /* Read DW_AT_data_location and set in type. */
25945 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25946 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25947 dwarf2_per_cu_addr_type (cu->per_cu)))
25948 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25949
25950 if (dwarf2_per_objfile->die_type_hash == NULL)
25951 {
25952 dwarf2_per_objfile->die_type_hash =
25953 htab_create_alloc_ex (127,
25954 per_cu_offset_and_type_hash,
25955 per_cu_offset_and_type_eq,
25956 NULL,
25957 &objfile->objfile_obstack,
25958 hashtab_obstack_allocate,
25959 dummy_obstack_deallocate);
25960 }
25961
25962 ofs.per_cu = cu->per_cu;
25963 ofs.sect_off = die->sect_off;
25964 ofs.type = type;
25965 slot = (struct dwarf2_per_cu_offset_and_type **)
25966 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25967 if (*slot)
25968 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25969 sect_offset_str (die->sect_off));
25970 *slot = XOBNEW (&objfile->objfile_obstack,
25971 struct dwarf2_per_cu_offset_and_type);
25972 **slot = ofs;
25973 return type;
25974 }
25975
25976 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25977 or return NULL if the die does not have a saved type. */
25978
25979 static struct type *
25980 get_die_type_at_offset (sect_offset sect_off,
25981 struct dwarf2_per_cu_data *per_cu)
25982 {
25983 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25984 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25985
25986 if (dwarf2_per_objfile->die_type_hash == NULL)
25987 return NULL;
25988
25989 ofs.per_cu = per_cu;
25990 ofs.sect_off = sect_off;
25991 slot = ((struct dwarf2_per_cu_offset_and_type *)
25992 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25993 if (slot)
25994 return slot->type;
25995 else
25996 return NULL;
25997 }
25998
25999 /* Look up the type for DIE in CU in die_type_hash,
26000 or return NULL if DIE does not have a saved type. */
26001
26002 static struct type *
26003 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
26004 {
26005 return get_die_type_at_offset (die->sect_off, cu->per_cu);
26006 }
26007
26008 /* Add a dependence relationship from CU to REF_PER_CU. */
26009
26010 static void
26011 dwarf2_add_dependence (struct dwarf2_cu *cu,
26012 struct dwarf2_per_cu_data *ref_per_cu)
26013 {
26014 void **slot;
26015
26016 if (cu->dependencies == NULL)
26017 cu->dependencies
26018 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
26019 NULL, &cu->comp_unit_obstack,
26020 hashtab_obstack_allocate,
26021 dummy_obstack_deallocate);
26022
26023 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
26024 if (*slot == NULL)
26025 *slot = ref_per_cu;
26026 }
26027
26028 /* Subroutine of dwarf2_mark to pass to htab_traverse.
26029 Set the mark field in every compilation unit in the
26030 cache that we must keep because we are keeping CU. */
26031
26032 static int
26033 dwarf2_mark_helper (void **slot, void *data)
26034 {
26035 struct dwarf2_per_cu_data *per_cu;
26036
26037 per_cu = (struct dwarf2_per_cu_data *) *slot;
26038
26039 /* cu->dependencies references may not yet have been ever read if QUIT aborts
26040 reading of the chain. As such dependencies remain valid it is not much
26041 useful to track and undo them during QUIT cleanups. */
26042 if (per_cu->cu == NULL)
26043 return 1;
26044
26045 if (per_cu->cu->mark)
26046 return 1;
26047 per_cu->cu->mark = true;
26048
26049 if (per_cu->cu->dependencies != NULL)
26050 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
26051
26052 return 1;
26053 }
26054
26055 /* Set the mark field in CU and in every other compilation unit in the
26056 cache that we must keep because we are keeping CU. */
26057
26058 static void
26059 dwarf2_mark (struct dwarf2_cu *cu)
26060 {
26061 if (cu->mark)
26062 return;
26063 cu->mark = true;
26064 if (cu->dependencies != NULL)
26065 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
26066 }
26067
26068 static void
26069 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
26070 {
26071 while (per_cu)
26072 {
26073 per_cu->cu->mark = false;
26074 per_cu = per_cu->cu->read_in_chain;
26075 }
26076 }
26077
26078 /* Trivial hash function for partial_die_info: the hash value of a DIE
26079 is its offset in .debug_info for this objfile. */
26080
26081 static hashval_t
26082 partial_die_hash (const void *item)
26083 {
26084 const struct partial_die_info *part_die
26085 = (const struct partial_die_info *) item;
26086
26087 return to_underlying (part_die->sect_off);
26088 }
26089
26090 /* Trivial comparison function for partial_die_info structures: two DIEs
26091 are equal if they have the same offset. */
26092
26093 static int
26094 partial_die_eq (const void *item_lhs, const void *item_rhs)
26095 {
26096 const struct partial_die_info *part_die_lhs
26097 = (const struct partial_die_info *) item_lhs;
26098 const struct partial_die_info *part_die_rhs
26099 = (const struct partial_die_info *) item_rhs;
26100
26101 return part_die_lhs->sect_off == part_die_rhs->sect_off;
26102 }
26103
26104 struct cmd_list_element *set_dwarf_cmdlist;
26105 struct cmd_list_element *show_dwarf_cmdlist;
26106
26107 static void
26108 set_dwarf_cmd (const char *args, int from_tty)
26109 {
26110 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
26111 gdb_stdout);
26112 }
26113
26114 static void
26115 show_dwarf_cmd (const char *args, int from_tty)
26116 {
26117 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
26118 }
26119
26120 bool dwarf_always_disassemble;
26121
26122 static void
26123 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
26124 struct cmd_list_element *c, const char *value)
26125 {
26126 fprintf_filtered (file,
26127 _("Whether to always disassemble "
26128 "DWARF expressions is %s.\n"),
26129 value);
26130 }
26131
26132 static void
26133 show_check_physname (struct ui_file *file, int from_tty,
26134 struct cmd_list_element *c, const char *value)
26135 {
26136 fprintf_filtered (file,
26137 _("Whether to check \"physname\" is %s.\n"),
26138 value);
26139 }
26140
26141 void
26142 _initialize_dwarf2_read (void)
26143 {
26144 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26145 Set DWARF specific variables.\n\
26146 Configure DWARF variables such as the cache size."),
26147 &set_dwarf_cmdlist, "maintenance set dwarf ",
26148 0/*allow-unknown*/, &maintenance_set_cmdlist);
26149
26150 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
26151 Show DWARF specific variables.\n\
26152 Show DWARF variables such as the cache size."),
26153 &show_dwarf_cmdlist, "maintenance show dwarf ",
26154 0/*allow-unknown*/, &maintenance_show_cmdlist);
26155
26156 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26157 &dwarf_max_cache_age, _("\
26158 Set the upper bound on the age of cached DWARF compilation units."), _("\
26159 Show the upper bound on the age of cached DWARF compilation units."), _("\
26160 A higher limit means that cached compilation units will be stored\n\
26161 in memory longer, and more total memory will be used. Zero disables\n\
26162 caching, which can slow down startup."),
26163 NULL,
26164 show_dwarf_max_cache_age,
26165 &set_dwarf_cmdlist,
26166 &show_dwarf_cmdlist);
26167
26168 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
26169 &dwarf_always_disassemble, _("\
26170 Set whether `info address' always disassembles DWARF expressions."), _("\
26171 Show whether `info address' always disassembles DWARF expressions."), _("\
26172 When enabled, DWARF expressions are always printed in an assembly-like\n\
26173 syntax. When disabled, expressions will be printed in a more\n\
26174 conversational style, when possible."),
26175 NULL,
26176 show_dwarf_always_disassemble,
26177 &set_dwarf_cmdlist,
26178 &show_dwarf_cmdlist);
26179
26180 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26181 Set debugging of the DWARF reader."), _("\
26182 Show debugging of the DWARF reader."), _("\
26183 When enabled (non-zero), debugging messages are printed during DWARF\n\
26184 reading and symtab expansion. A value of 1 (one) provides basic\n\
26185 information. A value greater than 1 provides more verbose information."),
26186 NULL,
26187 NULL,
26188 &setdebuglist, &showdebuglist);
26189
26190 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26191 Set debugging of the DWARF DIE reader."), _("\
26192 Show debugging of the DWARF DIE reader."), _("\
26193 When enabled (non-zero), DIEs are dumped after they are read in.\n\
26194 The value is the maximum depth to print."),
26195 NULL,
26196 NULL,
26197 &setdebuglist, &showdebuglist);
26198
26199 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
26200 Set debugging of the dwarf line reader."), _("\
26201 Show debugging of the dwarf line reader."), _("\
26202 When enabled (non-zero), line number entries are dumped as they are read in.\n\
26203 A value of 1 (one) provides basic information.\n\
26204 A value greater than 1 provides more verbose information."),
26205 NULL,
26206 NULL,
26207 &setdebuglist, &showdebuglist);
26208
26209 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
26210 Set cross-checking of \"physname\" code against demangler."), _("\
26211 Show cross-checking of \"physname\" code against demangler."), _("\
26212 When enabled, GDB's internal \"physname\" code is checked against\n\
26213 the demangler."),
26214 NULL, show_check_physname,
26215 &setdebuglist, &showdebuglist);
26216
26217 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26218 no_class, &use_deprecated_index_sections, _("\
26219 Set whether to use deprecated gdb_index sections."), _("\
26220 Show whether to use deprecated gdb_index sections."), _("\
26221 When enabled, deprecated .gdb_index sections are used anyway.\n\
26222 Normally they are ignored either because of a missing feature or\n\
26223 performance issue.\n\
26224 Warning: This option must be enabled before gdb reads the file."),
26225 NULL,
26226 NULL,
26227 &setlist, &showlist);
26228
26229 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26230 &dwarf2_locexpr_funcs);
26231 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26232 &dwarf2_loclist_funcs);
26233
26234 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26235 &dwarf2_block_frame_base_locexpr_funcs);
26236 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26237 &dwarf2_block_frame_base_loclist_funcs);
26238
26239 #if GDB_SELF_TEST
26240 selftests::register_test ("dw2_expand_symtabs_matching",
26241 selftests::dw2_expand_symtabs_matching::run_test);
26242 #endif
26243 }
This page took 0.706079 seconds and 4 git commands to generate.